Modified patch originates from Andy Green <andy@openmoko.com>
[u-boot-openmoko/mini2440.git] / common / cmd_scsi.c
blobf49531e96b708d5d0de7fd2cc665e2216f48905a
1 /*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
28 * SCSI support.
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>
37 #ifdef CONFIG_SCSI_SYM53C8XX
38 #define SCSI_VEND_ID 0x1000
39 #ifndef CONFIG_SCSI_DEV_ID
40 #define SCSI_DEV_ID 0x0001
41 #else
42 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
43 #endif
44 #elif defined CONFIG_SATA_ULI5288
46 #define SCSI_VEND_ID 0x10b9
47 #define SCSI_DEV_ID 0x5288
49 #else
50 #error no scsi device defined
51 #endif
54 static ccb tempccb; /* temporary scsi command buffer */
56 static unsigned char tempbuff[512]; /* temporary data buffer */
58 static int scsi_max_devs; /* number of highest available scsi device */
60 static int scsi_curr_dev; /* current device */
62 static block_dev_desc_t scsi_dev_desc[CFG_SCSI_MAX_DEVICE];
64 /********************************************************************************
65 * forward declerations of some Setup Routines
67 void scsi_setup_test_unit_ready(ccb * pccb);
68 void scsi_setup_read_capacity(ccb * pccb);
69 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
70 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
71 void scsi_setup_inquiry(ccb * pccb);
72 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
75 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer);
78 /*********************************************************************************
79 * (re)-scan the scsi bus and reports scsi device info
80 * to the user if mode = 1
82 void scsi_scan(int mode)
84 unsigned char i,perq,modi,lun;
85 unsigned long capacity,blksz;
86 ccb* pccb=(ccb *)&tempccb;
88 if(mode==1) {
89 printf("scanning bus for devices...\n");
91 for(i=0;i<CFG_SCSI_MAX_DEVICE;i++) {
92 scsi_dev_desc[i].target=0xff;
93 scsi_dev_desc[i].lun=0xff;
94 scsi_dev_desc[i].lba=0;
95 scsi_dev_desc[i].blksz=0;
96 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
97 scsi_dev_desc[i].vendor[0]=0;
98 scsi_dev_desc[i].product[0]=0;
99 scsi_dev_desc[i].revision[0]=0;
100 scsi_dev_desc[i].removable=FALSE;
101 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
102 scsi_dev_desc[i].dev=i;
103 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
104 scsi_dev_desc[i].block_read=scsi_read;
106 scsi_max_devs=0;
107 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
108 pccb->target=i;
109 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
110 pccb->lun=lun;
111 pccb->pdata=(unsigned char *)&tempbuff;
112 pccb->datalen=512;
113 scsi_setup_inquiry(pccb);
114 if(scsi_exec(pccb)!=TRUE) {
115 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
116 debug ("Selection timeout ID %d\n",pccb->target);
117 continue; /* selection timeout => assuming no device present */
119 scsi_print_error(pccb);
120 continue;
122 perq=tempbuff[0];
123 modi=tempbuff[1];
124 if((perq & 0x1f)==0x1f) {
125 continue; /* skip unknown devices */
127 if((modi&0x80)==0x80) /* drive is removable */
128 scsi_dev_desc[scsi_max_devs].removable=TRUE;
129 /* get info for this device */
130 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
131 &tempbuff[8], 8);
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
133 &tempbuff[16], 16);
134 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
135 &tempbuff[32], 4);
136 scsi_dev_desc[scsi_max_devs].target=pccb->target;
137 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
139 pccb->datalen=0;
140 scsi_setup_test_unit_ready(pccb);
141 if(scsi_exec(pccb)!=TRUE) {
142 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
143 scsi_dev_desc[scsi_max_devs].type=perq;
144 goto removable;
146 scsi_print_error(pccb);
147 continue;
149 pccb->datalen=8;
150 scsi_setup_read_capacity(pccb);
151 if(scsi_exec(pccb)!=TRUE) {
152 scsi_print_error(pccb);
153 continue;
155 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
156 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
157 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
158 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
159 scsi_dev_desc[scsi_max_devs].lba=capacity;
160 scsi_dev_desc[scsi_max_devs].blksz=blksz;
161 scsi_dev_desc[scsi_max_devs].type=perq;
162 init_part(&scsi_dev_desc[scsi_max_devs]);
163 removable:
164 if(mode==1) {
165 printf (" Device %d: ", scsi_max_devs);
166 dev_print(&scsi_dev_desc[scsi_max_devs]);
167 } /* if mode */
168 scsi_max_devs++;
169 } /* next LUN */
171 if(scsi_max_devs>0)
172 scsi_curr_dev=0;
173 else
174 scsi_curr_dev=-1;
178 void scsi_init(void)
180 int busdevfunc;
182 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
183 if(busdevfunc==-1) {
184 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
185 return;
187 #ifdef DEBUG
188 else {
189 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);
191 #endif
192 scsi_low_level_init(busdevfunc);
193 scsi_scan(1);
196 block_dev_desc_t * scsi_get_dev(int dev)
198 return (dev < CFG_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
202 /******************************************************************************
203 * scsi boot command intepreter. Derived from diskboot
205 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
207 char *boot_device = NULL;
208 char *ep;
209 int dev, part = 0;
210 ulong addr, cnt;
211 disk_partition_t info;
212 image_header_t *hdr;
213 int rcode = 0;
214 #if defined(CONFIG_FIT)
215 const void *fit_hdr;
216 #endif
218 switch (argc) {
219 case 1:
220 addr = CFG_LOAD_ADDR;
221 boot_device = getenv ("bootdevice");
222 break;
223 case 2:
224 addr = simple_strtoul(argv[1], NULL, 16);
225 boot_device = getenv ("bootdevice");
226 break;
227 case 3:
228 addr = simple_strtoul(argv[1], NULL, 16);
229 boot_device = argv[2];
230 break;
231 default:
232 printf ("Usage:\n%s\n", cmdtp->usage);
233 return 1;
236 if (!boot_device) {
237 puts ("\n** No boot device **\n");
238 return 1;
241 dev = simple_strtoul(boot_device, &ep, 16);
242 printf("booting from dev %d\n",dev);
243 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
244 printf ("\n** Device %d not available\n", dev);
245 return 1;
248 if (*ep) {
249 if (*ep != ':') {
250 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
251 return 1;
253 part = simple_strtoul(++ep, NULL, 16);
255 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
256 printf("error reading partinfo\n");
257 return 1;
259 if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
260 (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
261 printf ("\n** Invalid partition type \"%.32s\""
262 " (expect \"" BOOT_PART_TYPE "\")\n",
263 info.type);
264 return 1;
267 printf ("\nLoading from SCSI device %d, partition %d: "
268 "Name: %.32s Type: %.32s\n",
269 dev, part, info.name, info.type);
271 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
272 info.start, info.size, info.blksz);
274 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
275 printf ("** Read error on %d:%d\n", dev, part);
276 return 1;
279 switch (genimg_get_format ((void *)addr)) {
280 case IMAGE_FORMAT_LEGACY:
281 hdr = (image_header_t *)addr;
283 if (!image_check_hcrc (hdr)) {
284 puts ("\n** Bad Header Checksum **\n");
285 return 1;
288 image_print_contents (hdr);
289 cnt = image_get_image_size (hdr);
290 break;
291 #if defined(CONFIG_FIT)
292 case IMAGE_FORMAT_FIT:
293 fit_hdr = (const void *)addr;
294 if (!fit_check_format (fit_hdr)) {
295 puts ("** Bad FIT image format\n");
296 return 1;
298 puts ("Fit image detected...\n");
300 cnt = fit_get_size (fit_hdr);
301 break;
302 #endif
303 default:
304 puts ("** Unknown image type\n");
305 return 1;
308 cnt += info.blksz - 1;
309 cnt /= info.blksz;
310 cnt -= 1;
312 if (scsi_read (dev, info.start+1, cnt,
313 (ulong *)(addr+info.blksz)) != cnt) {
314 printf ("** Read error on %d:%d\n", dev, part);
315 return 1;
318 #if defined(CONFIG_FIT)
319 /* This cannot be done earlier, we need complete FIT image in RAM first */
320 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
321 fit_print_contents ((const void *)addr);
322 #endif
324 /* Loading ok, update default load address */
325 load_addr = addr;
327 flush_cache (addr, (cnt+1)*info.blksz);
329 /* Check if we should attempt an auto-start */
330 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
331 char *local_args[2];
332 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
333 local_args[0] = argv[0];
334 local_args[1] = NULL;
335 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
336 rcode = do_bootm (cmdtp, 0, 1, local_args);
338 return rcode;
341 /*********************************************************************************
342 * scsi command intepreter
344 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
346 switch (argc) {
347 case 0:
348 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
349 case 2:
350 if (strncmp(argv[1],"res",3) == 0) {
351 printf("\nReset SCSI\n");
352 scsi_bus_reset();
353 scsi_scan(1);
354 return 0;
356 if (strncmp(argv[1],"inf",3) == 0) {
357 int i;
358 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
359 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
360 continue; /* list only known devices */
361 printf ("SCSI dev. %d: ", i);
362 dev_print(&scsi_dev_desc[i]);
364 return 0;
366 if (strncmp(argv[1],"dev",3) == 0) {
367 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
368 printf("\nno SCSI devices available\n");
369 return 1;
371 printf ("\n Device %d: ", scsi_curr_dev);
372 dev_print(&scsi_dev_desc[scsi_curr_dev]);
373 return 0;
375 if (strncmp(argv[1],"scan",4) == 0) {
376 scsi_scan(1);
377 return 0;
379 if (strncmp(argv[1],"part",4) == 0) {
380 int dev, ok;
381 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
382 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
383 ok++;
384 if (dev)
385 printf("\n");
386 debug ("print_part of %x\n",dev);
387 print_part(&scsi_dev_desc[dev]);
390 if (!ok)
391 printf("\nno SCSI devices available\n");
392 return 1;
394 printf ("Usage:\n%s\n", cmdtp->usage);
395 return 1;
396 case 3:
397 if (strncmp(argv[1],"dev",3) == 0) {
398 int dev = (int)simple_strtoul(argv[2], NULL, 10);
399 printf ("\nSCSI device %d: ", dev);
400 if (dev >= CFG_SCSI_MAX_DEVICE) {
401 printf("unknown device\n");
402 return 1;
404 printf ("\n Device %d: ", dev);
405 dev_print(&scsi_dev_desc[dev]);
406 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
407 return 1;
409 scsi_curr_dev = dev;
410 printf("... is now current device\n");
411 return 0;
413 if (strncmp(argv[1],"part",4) == 0) {
414 int dev = (int)simple_strtoul(argv[2], NULL, 10);
415 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
416 print_part(&scsi_dev_desc[dev]);
418 else {
419 printf ("\nSCSI device %d not available\n", dev);
421 return 1;
423 printf ("Usage:\n%s\n", cmdtp->usage);
424 return 1;
425 default:
426 /* at least 4 args */
427 if (strcmp(argv[1],"read") == 0) {
428 ulong addr = simple_strtoul(argv[2], NULL, 16);
429 ulong blk = simple_strtoul(argv[3], NULL, 16);
430 ulong cnt = simple_strtoul(argv[4], NULL, 16);
431 ulong n;
432 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
433 scsi_curr_dev, blk, cnt);
434 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
435 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
436 return 0;
438 } /* switch */
439 printf ("Usage:\n%s\n", cmdtp->usage);
440 return 1;
443 /****************************************************************************************
444 * scsi_read
447 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
449 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
451 ulong start,blks, buf_addr;
452 unsigned short smallblks;
453 ccb* pccb=(ccb *)&tempccb;
454 device&=0xff;
455 /* Setup device
457 pccb->target=scsi_dev_desc[device].target;
458 pccb->lun=scsi_dev_desc[device].lun;
459 buf_addr=(unsigned long)buffer;
460 start=blknr;
461 blks=blkcnt;
462 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
463 do {
464 pccb->pdata=(unsigned char *)buf_addr;
465 if(blks>SCSI_MAX_READ_BLK) {
466 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
467 smallblks=SCSI_MAX_READ_BLK;
468 scsi_setup_read_ext(pccb,start,smallblks);
469 start+=SCSI_MAX_READ_BLK;
470 blks-=SCSI_MAX_READ_BLK;
472 else {
473 pccb->datalen=scsi_dev_desc[device].blksz * blks;
474 smallblks=(unsigned short) blks;
475 scsi_setup_read_ext(pccb,start,smallblks);
476 start+=blks;
477 blks=0;
479 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
480 if(scsi_exec(pccb)!=TRUE) {
481 scsi_print_error(pccb);
482 blkcnt-=blks;
483 break;
485 buf_addr+=pccb->datalen;
486 } while(blks!=0);
487 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
488 return(blkcnt);
491 /* copy src to dest, skipping leading and trailing blanks
492 * and null terminate the string
494 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
496 int start,end;
498 start=0;
499 while(start<len) {
500 if(src[start]!=' ')
501 break;
502 start++;
504 end=len-1;
505 while(end>start) {
506 if(src[end]!=' ')
507 break;
508 end--;
510 for( ; start<=end; start++) {
511 *dest++=src[start];
513 *dest='\0';
517 /* Trim trailing blanks, and NUL-terminate string
519 void scsi_trim_trail (unsigned char *str, unsigned int len)
521 unsigned char *p = str + len - 1;
523 while (len-- > 0) {
524 *p-- = '\0';
525 if (*p != ' ') {
526 return;
532 /************************************************************************************
533 * Some setup (fill-in) routines
535 void scsi_setup_test_unit_ready(ccb * pccb)
537 pccb->cmd[0]=SCSI_TST_U_RDY;
538 pccb->cmd[1]=pccb->lun<<5;
539 pccb->cmd[2]=0;
540 pccb->cmd[3]=0;
541 pccb->cmd[4]=0;
542 pccb->cmd[5]=0;
543 pccb->cmdlen=6;
544 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
547 void scsi_setup_read_capacity(ccb * pccb)
549 pccb->cmd[0]=SCSI_RD_CAPAC;
550 pccb->cmd[1]=pccb->lun<<5;
551 pccb->cmd[2]=0;
552 pccb->cmd[3]=0;
553 pccb->cmd[4]=0;
554 pccb->cmd[5]=0;
555 pccb->cmd[6]=0;
556 pccb->cmd[7]=0;
557 pccb->cmd[8]=0;
558 pccb->cmd[9]=0;
559 pccb->cmdlen=10;
560 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
564 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
566 pccb->cmd[0]=SCSI_READ10;
567 pccb->cmd[1]=pccb->lun<<5;
568 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
569 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
570 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
571 pccb->cmd[5]=((unsigned char) (start))&0xff;
572 pccb->cmd[6]=0;
573 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
574 pccb->cmd[8]=(unsigned char) blocks & 0xff;
575 pccb->cmd[6]=0;
576 pccb->cmdlen=10;
577 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
578 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
579 pccb->cmd[0],pccb->cmd[1],
580 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
581 pccb->cmd[7],pccb->cmd[8]);
584 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
586 pccb->cmd[0]=SCSI_READ6;
587 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
588 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
589 pccb->cmd[3]=((unsigned char) (start))&0xff;
590 pccb->cmd[4]=(unsigned char) blocks & 0xff;
591 pccb->cmd[5]=0;
592 pccb->cmdlen=6;
593 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
594 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
595 pccb->cmd[0],pccb->cmd[1],
596 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
600 void scsi_setup_inquiry(ccb * pccb)
602 pccb->cmd[0]=SCSI_INQUIRY;
603 pccb->cmd[1]=pccb->lun<<5;
604 pccb->cmd[2]=0;
605 pccb->cmd[3]=0;
606 if(pccb->datalen>255)
607 pccb->cmd[4]=255;
608 else
609 pccb->cmd[4]=(unsigned char)pccb->datalen;
610 pccb->cmd[5]=0;
611 pccb->cmdlen=6;
612 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
616 U_BOOT_CMD(
617 scsi, 5, 1, do_scsi,
618 "scsi - SCSI sub-system\n",
619 "reset - reset SCSI controller\n"
620 "scsi info - show available SCSI devices\n"
621 "scsi scan - (re-)scan SCSI bus\n"
622 "scsi device [dev] - show or set current device\n"
623 "scsi part [dev] - print partition table of one or all SCSI devices\n"
624 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
625 " to memory address `addr'\n"
628 U_BOOT_CMD(
629 scsiboot, 3, 1, do_scsiboot,
630 "scsiboot- boot from SCSI device\n",
631 "loadAddr dev:part\n"