]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_scsi.c
Merge with /home/wd/git/u-boot/testing-NAND/ to add new NAND handling.
[people/ms/u-boot.git] / common / cmd_scsi.c
1 /*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 *
24 *
25 */
26
27 /*
28 * SCSI support.
29 */
30 #include <common.h>
31 #include <command.h>
32 #include <asm/processor.h>
33 #include <scsi.h>
34 #include <image.h>
35 #include <pci.h>
36
37 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
38
39 #ifdef CONFIG_SCSI_SYM53C8XX
40 #define SCSI_VEND_ID 0x1000
41 #ifndef CONFIG_SCSI_DEV_ID
42 #define SCSI_DEV_ID 0x0001
43 #else
44 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
45 #endif
46 #else
47 #error CONFIG_SCSI_SYM53C8XX must be defined
48 #endif
49
50
51 static ccb tempccb; /* temporary scsi command buffer */
52
53 static unsigned char tempbuff[512]; /* temporary data buffer */
54
55 static int scsi_max_devs; /* number of highest available scsi device */
56
57 static int scsi_curr_dev; /* current device */
58
59 static block_dev_desc_t scsi_dev_desc[CFG_SCSI_MAX_DEVICE];
60
61 /********************************************************************************
62 * forward declerations of some Setup Routines
63 */
64 void scsi_setup_test_unit_ready(ccb * pccb);
65 void scsi_setup_read_capacity(ccb * pccb);
66 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
67 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
68 void scsi_setup_inquiry(ccb * pccb);
69 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
70
71
72 ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer);
73
74
75 /*********************************************************************************
76 * (re)-scan the scsi bus and reports scsi device info
77 * to the user if mode = 1
78 */
79 void scsi_scan(int mode)
80 {
81 unsigned char i,perq,modi,lun;
82 unsigned long capacity,blksz;
83 ccb* pccb=(ccb *)&tempccb;
84
85 if(mode==1) {
86 printf("scanning bus for devices...\n");
87 }
88 for(i=0;i<CFG_SCSI_MAX_DEVICE;i++) {
89 scsi_dev_desc[i].target=0xff;
90 scsi_dev_desc[i].lun=0xff;
91 scsi_dev_desc[i].lba=0;
92 scsi_dev_desc[i].blksz=0;
93 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
94 scsi_dev_desc[i].vendor[0]=0;
95 scsi_dev_desc[i].product[0]=0;
96 scsi_dev_desc[i].revision[0]=0;
97 scsi_dev_desc[i].removable=FALSE;
98 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
99 scsi_dev_desc[i].dev=i;
100 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
101 scsi_dev_desc[i].block_read=scsi_read;
102 }
103 scsi_max_devs=0;
104 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
105 pccb->target=i;
106 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
107 pccb->lun=lun;
108 pccb->pdata=(unsigned char *)&tempbuff;
109 pccb->datalen=512;
110 scsi_setup_inquiry(pccb);
111 if(scsi_exec(pccb)!=TRUE) {
112 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
113 debug ("Selection timeout ID %d\n",pccb->target);
114 continue; /* selection timeout => assuming no device present */
115 }
116 scsi_print_error(pccb);
117 continue;
118 }
119 perq=tempbuff[0];
120 modi=tempbuff[1];
121 if((perq & 0x1f)==0x1f) {
122 continue; /* skip unknown devices */
123 }
124 if((modi&0x80)==0x80) /* drive is removable */
125 scsi_dev_desc[scsi_max_devs].removable=TRUE;
126 /* get info for this device */
127 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].vendor[0],&tempbuff[8],8);
128 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].product[0],&tempbuff[16],16);
129 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].revision[0],&tempbuff[32],4);
130 scsi_dev_desc[scsi_max_devs].target=pccb->target;
131 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
132
133 pccb->datalen=0;
134 scsi_setup_test_unit_ready(pccb);
135 if(scsi_exec(pccb)!=TRUE) {
136 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
137 scsi_dev_desc[scsi_max_devs].type=perq;
138 goto removable;
139 }
140 scsi_print_error(pccb);
141 continue;
142 }
143 pccb->datalen=8;
144 scsi_setup_read_capacity(pccb);
145 if(scsi_exec(pccb)!=TRUE) {
146 scsi_print_error(pccb);
147 continue;
148 }
149 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
150 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
151 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
152 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
153 scsi_dev_desc[scsi_max_devs].lba=capacity;
154 scsi_dev_desc[scsi_max_devs].blksz=blksz;
155 scsi_dev_desc[scsi_max_devs].type=perq;
156 init_part(&scsi_dev_desc[scsi_max_devs]);
157 removable:
158 if(mode==1) {
159 printf (" Device %d: ", scsi_max_devs);
160 dev_print(&scsi_dev_desc[scsi_max_devs]);
161 } /* if mode */
162 scsi_max_devs++;
163 } /* next LUN */
164 }
165 if(scsi_max_devs>0)
166 scsi_curr_dev=0;
167 else
168 scsi_curr_dev=-1;
169 }
170
171
172 void scsi_init(void)
173 {
174 int busdevfunc;
175
176 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
177 if(busdevfunc==-1) {
178 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
179 return;
180 }
181 #ifdef DEBUG
182 else {
183 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",SCSI_VEND_ID,SCSI_DEV_ID,(busdevfunc>>16)&0xFF,(busdevfunc>>11)&0x1F,(busdevfunc>>8)&0x7);
184 }
185 #endif
186 scsi_low_level_init(busdevfunc);
187 scsi_scan(1);
188 }
189
190 block_dev_desc_t * scsi_get_dev(int dev)
191 {
192 return((block_dev_desc_t *)&scsi_dev_desc[dev]);
193 }
194
195
196 /******************************************************************************
197 * scsi boot command intepreter. Derived from diskboot
198 */
199 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
200 {
201 char *boot_device = NULL;
202 char *ep;
203 int dev, part = 0;
204 ulong addr, cnt, checksum;
205 disk_partition_t info;
206 image_header_t *hdr;
207 int rcode = 0;
208
209 switch (argc) {
210 case 1:
211 addr = CFG_LOAD_ADDR;
212 boot_device = getenv ("bootdevice");
213 break;
214 case 2:
215 addr = simple_strtoul(argv[1], NULL, 16);
216 boot_device = getenv ("bootdevice");
217 break;
218 case 3:
219 addr = simple_strtoul(argv[1], NULL, 16);
220 boot_device = argv[2];
221 break;
222 default:
223 printf ("Usage:\n%s\n", cmdtp->usage);
224 return 1;
225 }
226
227 if (!boot_device) {
228 puts ("\n** No boot device **\n");
229 return 1;
230 }
231
232 dev = simple_strtoul(boot_device, &ep, 16);
233 printf("booting from dev %d\n",dev);
234 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
235 printf ("\n** Device %d not available\n", dev);
236 return 1;
237 }
238
239 if (*ep) {
240 if (*ep != ':') {
241 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
242 return 1;
243 }
244 part = simple_strtoul(++ep, NULL, 16);
245 }
246 if (get_partition_info (scsi_dev_desc, part, &info)) {
247 printf("error reading partinfo\n");
248 return 1;
249 }
250 if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
251 (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
252 printf ("\n** Invalid partition type \"%.32s\""
253 " (expect \"" BOOT_PART_TYPE "\")\n",
254 info.type);
255 return 1;
256 }
257
258 printf ("\nLoading from SCSI device %d, partition %d: "
259 "Name: %.32s Type: %.32s\n",
260 dev, part, info.name, info.type);
261
262 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
263 info.start, info.size, info.blksz);
264
265 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
266 printf ("** Read error on %d:%d\n", dev, part);
267 return 1;
268 }
269
270 hdr = (image_header_t *)addr;
271
272 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
273 printf("\n** Bad Magic Number **\n");
274 return 1;
275 }
276
277 checksum = ntohl(hdr->ih_hcrc);
278 hdr->ih_hcrc = 0;
279
280 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
281 puts ("\n** Bad Header Checksum **\n");
282 return 1;
283 }
284 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
285
286 print_image_hdr (hdr);
287 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
288 cnt += info.blksz - 1;
289 cnt /= info.blksz;
290 cnt -= 1;
291
292 if (scsi_read (dev, info.start+1, cnt,
293 (ulong *)(addr+info.blksz)) != cnt) {
294 printf ("** Read error on %d:%d\n", dev, part);
295 return 1;
296 }
297 /* Loading ok, update default load address */
298 load_addr = addr;
299
300 flush_cache (addr, (cnt+1)*info.blksz);
301
302 /* Check if we should attempt an auto-start */
303 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
304 char *local_args[2];
305 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
306 local_args[0] = argv[0];
307 local_args[1] = NULL;
308 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
309 rcode = do_bootm (cmdtp, 0, 1, local_args);
310 }
311 return rcode;
312 }
313
314 /*********************************************************************************
315 * scsi command intepreter
316 */
317 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
318 {
319 switch (argc) {
320 case 0:
321 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
322 case 2:
323 if (strncmp(argv[1],"res",3) == 0) {
324 printf("\nReset SCSI\n");
325 scsi_bus_reset();
326 scsi_scan(1);
327 return 0;
328 }
329 if (strncmp(argv[1],"inf",3) == 0) {
330 int i;
331 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
332 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
333 continue; /* list only known devices */
334 printf ("SCSI dev. %d: ", i);
335 dev_print(&scsi_dev_desc[i]);
336 }
337 return 0;
338 }
339 if (strncmp(argv[1],"dev",3) == 0) {
340 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
341 printf("\nno SCSI devices available\n");
342 return 1;
343 }
344 printf ("\n Device %d: ", scsi_curr_dev);
345 dev_print(&scsi_dev_desc[scsi_curr_dev]);
346 return 0;
347 }
348 if (strncmp(argv[1],"scan",4) == 0) {
349 scsi_scan(1);
350 return 0;
351 }
352 if (strncmp(argv[1],"part",4) == 0) {
353 int dev, ok;
354 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
355 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
356 ok++;
357 if (dev)
358 printf("\n");
359 debug ("print_part of %x\n",dev);
360 print_part(&scsi_dev_desc[dev]);
361 }
362 }
363 if (!ok)
364 printf("\nno SCSI devices available\n");
365 return 1;
366 }
367 printf ("Usage:\n%s\n", cmdtp->usage);
368 return 1;
369 case 3:
370 if (strncmp(argv[1],"dev",3) == 0) {
371 int dev = (int)simple_strtoul(argv[2], NULL, 10);
372 printf ("\nSCSI device %d: ", dev);
373 if (dev >= CFG_SCSI_MAX_DEVICE) {
374 printf("unknown device\n");
375 return 1;
376 }
377 printf ("\n Device %d: ", dev);
378 dev_print(&scsi_dev_desc[dev]);
379 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
380 return 1;
381 }
382 scsi_curr_dev = dev;
383 printf("... is now current device\n");
384 return 0;
385 }
386 if (strncmp(argv[1],"part",4) == 0) {
387 int dev = (int)simple_strtoul(argv[2], NULL, 10);
388 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
389 print_part(&scsi_dev_desc[dev]);
390 }
391 else {
392 printf ("\nSCSI device %d not available\n", dev);
393 }
394 return 1;
395 }
396 printf ("Usage:\n%s\n", cmdtp->usage);
397 return 1;
398 default:
399 /* at least 4 args */
400 if (strcmp(argv[1],"read") == 0) {
401 ulong addr = simple_strtoul(argv[2], NULL, 16);
402 ulong blk = simple_strtoul(argv[3], NULL, 16);
403 ulong cnt = simple_strtoul(argv[4], NULL, 16);
404 ulong n;
405 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
406 scsi_curr_dev, blk, cnt);
407 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
408 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
409 return 0;
410 }
411 } /* switch */
412 printf ("Usage:\n%s\n", cmdtp->usage);
413 return 1;
414 }
415
416 /****************************************************************************************
417 * scsi_read
418 */
419
420 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
421
422 ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer)
423 {
424 ulong start,blks, buf_addr;
425 unsigned short smallblks;
426 ccb* pccb=(ccb *)&tempccb;
427 device&=0xff;
428 /* Setup device
429 */
430 pccb->target=scsi_dev_desc[device].target;
431 pccb->lun=scsi_dev_desc[device].lun;
432 buf_addr=(unsigned long)buffer;
433 start=blknr;
434 blks=blkcnt;
435 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
436 do {
437 pccb->pdata=(unsigned char *)buf_addr;
438 if(blks>SCSI_MAX_READ_BLK) {
439 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
440 smallblks=SCSI_MAX_READ_BLK;
441 scsi_setup_read_ext(pccb,start,smallblks);
442 start+=SCSI_MAX_READ_BLK;
443 blks-=SCSI_MAX_READ_BLK;
444 }
445 else {
446 pccb->datalen=scsi_dev_desc[device].blksz * blks;
447 smallblks=(unsigned short) blks;
448 scsi_setup_read_ext(pccb,start,smallblks);
449 start+=blks;
450 blks=0;
451 }
452 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
453 if(scsi_exec(pccb)!=TRUE) {
454 scsi_print_error(pccb);
455 blkcnt-=blks;
456 break;
457 }
458 buf_addr+=pccb->datalen;
459 } while(blks!=0);
460 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
461 return(blkcnt);
462 }
463
464 /* copy src to dest, skipping leading and trailing blanks
465 * and null terminate the string
466 */
467 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
468 {
469 int start,end;
470
471 start=0;
472 while(start<len) {
473 if(src[start]!=' ')
474 break;
475 start++;
476 }
477 end=len-1;
478 while(end>start) {
479 if(src[end]!=' ')
480 break;
481 end--;
482 }
483 for( ; start<=end; start++) {
484 *dest++=src[start];
485 }
486 *dest='\0';
487 }
488
489
490 /* Trim trailing blanks, and NUL-terminate string
491 */
492 void scsi_trim_trail (unsigned char *str, unsigned int len)
493 {
494 unsigned char *p = str + len - 1;
495
496 while (len-- > 0) {
497 *p-- = '\0';
498 if (*p != ' ') {
499 return;
500 }
501 }
502 }
503
504
505 /************************************************************************************
506 * Some setup (fill-in) routines
507 */
508 void scsi_setup_test_unit_ready(ccb * pccb)
509 {
510 pccb->cmd[0]=SCSI_TST_U_RDY;
511 pccb->cmd[1]=pccb->lun<<5;
512 pccb->cmd[2]=0;
513 pccb->cmd[3]=0;
514 pccb->cmd[4]=0;
515 pccb->cmd[5]=0;
516 pccb->cmdlen=6;
517 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
518 }
519
520 void scsi_setup_read_capacity(ccb * pccb)
521 {
522 pccb->cmd[0]=SCSI_RD_CAPAC;
523 pccb->cmd[1]=pccb->lun<<5;
524 pccb->cmd[2]=0;
525 pccb->cmd[3]=0;
526 pccb->cmd[4]=0;
527 pccb->cmd[5]=0;
528 pccb->cmd[6]=0;
529 pccb->cmd[7]=0;
530 pccb->cmd[8]=0;
531 pccb->cmd[9]=0;
532 pccb->cmdlen=10;
533 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
534
535 }
536
537 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
538 {
539 pccb->cmd[0]=SCSI_READ10;
540 pccb->cmd[1]=pccb->lun<<5;
541 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
542 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
543 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
544 pccb->cmd[5]=((unsigned char) (start))&0xff;
545 pccb->cmd[6]=0;
546 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
547 pccb->cmd[8]=(unsigned char) blocks & 0xff;
548 pccb->cmd[6]=0;
549 pccb->cmdlen=10;
550 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
551 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
552 pccb->cmd[0],pccb->cmd[1],
553 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
554 pccb->cmd[7],pccb->cmd[8]);
555 }
556
557 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
558 {
559 pccb->cmd[0]=SCSI_READ6;
560 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
561 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
562 pccb->cmd[3]=((unsigned char) (start))&0xff;
563 pccb->cmd[4]=(unsigned char) blocks & 0xff;
564 pccb->cmd[5]=0;
565 pccb->cmdlen=6;
566 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
567 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
568 pccb->cmd[0],pccb->cmd[1],
569 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
570 }
571
572
573 void scsi_setup_inquiry(ccb * pccb)
574 {
575 pccb->cmd[0]=SCSI_INQUIRY;
576 pccb->cmd[1]=pccb->lun<<5;
577 pccb->cmd[2]=0;
578 pccb->cmd[3]=0;
579 if(pccb->datalen>255)
580 pccb->cmd[4]=255;
581 else
582 pccb->cmd[4]=(unsigned char)pccb->datalen;
583 pccb->cmd[5]=0;
584 pccb->cmdlen=6;
585 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
586 }
587
588
589 U_BOOT_CMD(
590 scsi, 5, 1, do_scsi,
591 "scsi - SCSI sub-system\n",
592 "reset - reset SCSI controller\n"
593 "scsi info - show available SCSI devices\n"
594 "scsi scan - (re-)scan SCSI bus\n"
595 "scsi device [dev] - show or set current device\n"
596 "scsi part [dev] - print partition table of one or all SCSI devices\n"
597 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
598 " to memory address `addr'\n"
599 );
600
601 U_BOOT_CMD(
602 scsiboot, 3, 1, do_scsiboot,
603 "scsiboot- boot from SCSI device\n",
604 "loadAddr dev:part\n"
605 );
606
607 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_SCSI) */