3 * Denis Peter, MPL AG Switzerland
5 * See file CREDITS for list of people who contributed to this
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,
32 #include <asm/processor.h>
37 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
39 #ifdef CONFIG_SCSI_SYM53C8XX
40 #define SCSI_VEND_ID 0x1000
41 #ifndef CONFIG_SCSI_DEV_ID
42 #define SCSI_DEV_ID 0x0001
44 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
47 #error CONFIG_SCSI_SYM53C8XX must be defined
51 static ccb tempccb; /* temporary scsi command buffer */
53 static unsigned char tempbuff[512]; /* temporary data buffer */
55 static int scsi_max_devs; /* number of highest available scsi device */
57 static int scsi_curr_dev; /* current device */
59 static block_dev_desc_t scsi_dev_desc[CFG_SCSI_MAX_DEVICE];
61 /********************************************************************************
62 * forward declerations of some Setup Routines
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);
72 ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer);
75 /*********************************************************************************
76 * (re)-scan the scsi bus and reports scsi device info
77 * to the user if mode = 1
79 void scsi_scan(int mode)
81 unsigned char i,perq,modi,lun;
82 unsigned long capacity,blksz;
83 ccb* pccb=(ccb *)&tempccb;
86 printf("scanning bus for devices...\n");
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;
104 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
106 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
108 pccb->pdata=(unsigned char *)&tempbuff;
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 */
116 scsi_print_error(pccb);
121 if((perq & 0x1f)==0x1f) {
122 continue; /* skip unknown devices */
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;
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;
140 scsi_print_error(pccb);
144 scsi_setup_read_capacity(pccb);
145 if(scsi_exec(pccb)!=TRUE) {
146 scsi_print_error(pccb);
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]);
159 printf (" Device %d: ", scsi_max_devs);
160 dev_print(&scsi_dev_desc[scsi_max_devs]);
176 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
178 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
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);
186 scsi_low_level_init(busdevfunc);
190 block_dev_desc_t * scsi_get_dev(int dev)
192 return((block_dev_desc_t *)&scsi_dev_desc[dev]);
196 /******************************************************************************
197 * scsi boot command intepreter. Derived from diskboot
199 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
201 char *boot_device = NULL;
204 ulong addr, cnt, checksum;
205 disk_partition_t info;
211 addr = CFG_LOAD_ADDR;
212 boot_device = getenv ("bootdevice");
215 addr = simple_strtoul(argv[1], NULL, 16);
216 boot_device = getenv ("bootdevice");
219 addr = simple_strtoul(argv[1], NULL, 16);
220 boot_device = argv[2];
223 printf ("Usage:\n%s\n", cmdtp->usage);
228 puts ("\n** No boot device **\n");
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);
241 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
244 part = simple_strtoul(++ep, NULL, 16);
246 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
247 printf("error reading partinfo\n");
250 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
251 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
252 printf ("\n** Invalid partition type \"%.32s\""
253 " (expect \"" BOOT_PART_TYPE "\")\n",
258 printf ("\nLoading from SCSI device %d, partition %d: "
259 "Name: %.32s Type: %.32s\n",
260 dev, part, info.name, info.type);
262 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
263 info.start, info.size, info.blksz);
265 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
266 printf ("** Read error on %d:%d\n", dev, part);
270 hdr = (image_header_t *)addr;
272 if (hdr->ih_magic == IH_MAGIC) {
273 printf("\n** Bad Magic Number **\n");
277 checksum = ntohl(hdr->ih_hcrc);
280 if (crc32 (0, (char *)hdr, sizeof(image_header_t)) != checksum) {
281 puts ("\n** Bad Header Checksum **\n");
285 print_image_hdr (hdr);
286 cnt = (hdr->ih_size + sizeof(image_header_t));
287 cnt += info.blksz - 1;
291 if (scsi_read (dev, info.start+1, cnt,
292 (ulong *)(addr+info.blksz)) != cnt) {
293 printf ("** Read error on %d:%d\n", dev, part);
296 /* Loading ok, update default load address */
299 flush_cache (addr, (cnt+1)*info.blksz);
301 /* Check if we should attempt an auto-start */
302 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
304 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
305 local_args[0] = argv[0];
306 local_args[1] = NULL;
307 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
308 rcode = do_bootm (cmdtp, 0, 1, local_args);
313 /*********************************************************************************
314 * scsi command intepreter
316 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
320 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
322 if (strncmp(argv[1],"res",3) == 0) {
323 printf("\nReset SCSI\n");
328 if (strncmp(argv[1],"inf",3) == 0) {
330 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
331 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
332 continue; /* list only known devices */
333 printf ("SCSI dev. %d: ", i);
334 dev_print(&scsi_dev_desc[i]);
338 if (strncmp(argv[1],"dev",3) == 0) {
339 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
340 printf("\nno SCSI devices available\n");
343 printf ("\n Device %d: ", scsi_curr_dev);
344 dev_print(&scsi_dev_desc[scsi_curr_dev]);
347 if (strncmp(argv[1],"scan",4) == 0) {
351 if (strncmp(argv[1],"part",4) == 0) {
353 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
354 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
358 debug ("print_part of %x\n",dev);
359 print_part(&scsi_dev_desc[dev]);
363 printf("\nno SCSI devices available\n");
366 printf ("Usage:\n%s\n", cmdtp->usage);
369 if (strncmp(argv[1],"dev",3) == 0) {
370 int dev = (int)simple_strtoul(argv[2], NULL, 10);
371 printf ("\nSCSI device %d: ", dev);
372 if (dev >= CFG_SCSI_MAX_DEVICE) {
373 printf("unknown device\n");
376 printf ("\n Device %d: ", dev);
377 dev_print(&scsi_dev_desc[dev]);
378 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
382 printf("... is now current device\n");
385 if (strncmp(argv[1],"part",4) == 0) {
386 int dev = (int)simple_strtoul(argv[2], NULL, 10);
387 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
388 print_part(&scsi_dev_desc[dev]);
391 printf ("\nSCSI device %d not available\n", dev);
395 printf ("Usage:\n%s\n", cmdtp->usage);
398 /* at least 4 args */
399 if (strcmp(argv[1],"read") == 0) {
400 ulong addr = simple_strtoul(argv[2], NULL, 16);
401 ulong blk = simple_strtoul(argv[3], NULL, 16);
402 ulong cnt = simple_strtoul(argv[4], NULL, 16);
404 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
405 scsi_curr_dev, blk, cnt);
406 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
407 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
411 printf ("Usage:\n%s\n", cmdtp->usage);
415 /****************************************************************************************
419 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
421 ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer)
423 ulong start,blks, buf_addr;
424 unsigned short smallblks;
425 ccb* pccb=(ccb *)&tempccb;
429 pccb->target=scsi_dev_desc[device].target;
430 pccb->lun=scsi_dev_desc[device].lun;
431 buf_addr=(unsigned long)buffer;
434 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
436 pccb->pdata=(unsigned char *)buf_addr;
437 if(blks>SCSI_MAX_READ_BLK) {
438 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
439 smallblks=SCSI_MAX_READ_BLK;
440 scsi_setup_read_ext(pccb,start,smallblks);
441 start+=SCSI_MAX_READ_BLK;
442 blks-=SCSI_MAX_READ_BLK;
445 pccb->datalen=scsi_dev_desc[device].blksz * blks;
446 smallblks=(unsigned short) blks;
447 scsi_setup_read_ext(pccb,start,smallblks);
451 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
452 if(scsi_exec(pccb)!=TRUE) {
453 scsi_print_error(pccb);
457 buf_addr+=pccb->datalen;
459 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
463 /* copy src to dest, skipping leading and trailing blanks
464 * and null terminate the string
466 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
482 for( ; start<=end; start++) {
489 /* Trim trailing blanks, and NUL-terminate string
491 void scsi_trim_trail (unsigned char *str, unsigned int len)
493 unsigned char *p = str + len - 1;
504 /************************************************************************************
505 * Some setup (fill-in) routines
507 void scsi_setup_test_unit_ready(ccb * pccb)
509 pccb->cmd[0]=SCSI_TST_U_RDY;
510 pccb->cmd[1]=pccb->lun<<5;
516 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
519 void scsi_setup_read_capacity(ccb * pccb)
521 pccb->cmd[0]=SCSI_RD_CAPAC;
522 pccb->cmd[1]=pccb->lun<<5;
532 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
536 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
538 pccb->cmd[0]=SCSI_READ10;
539 pccb->cmd[1]=pccb->lun<<5;
540 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
541 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
542 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
543 pccb->cmd[5]=((unsigned char) (start))&0xff;
545 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
546 pccb->cmd[8]=(unsigned char) blocks & 0xff;
549 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
550 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
551 pccb->cmd[0],pccb->cmd[1],
552 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
553 pccb->cmd[7],pccb->cmd[8]);
556 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
558 pccb->cmd[0]=SCSI_READ6;
559 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
560 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
561 pccb->cmd[3]=((unsigned char) (start))&0xff;
562 pccb->cmd[4]=(unsigned char) blocks & 0xff;
565 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
566 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
567 pccb->cmd[0],pccb->cmd[1],
568 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
572 void scsi_setup_inquiry(ccb * pccb)
574 pccb->cmd[0]=SCSI_INQUIRY;
575 pccb->cmd[1]=pccb->lun<<5;
578 if(pccb->datalen>255)
581 pccb->cmd[4]=(unsigned char)pccb->datalen;
584 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
587 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_SCSI) */