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 #ifdef CONFIG_SCSI_SYM53C8XX
38 #define SCSI_VEND_ID 0x1000
39 #ifndef CONFIG_SCSI_DEV_ID
40 #define SCSI_DEV_ID 0x0001
42 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
44 #elif defined CONFIG_SATA_ULI5288
46 #define SCSI_VEND_ID 0x10b9
47 #define SCSI_DEV_ID 0x5288
50 #error no scsi device defined
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;
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;
107 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
109 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
111 pccb->pdata=(unsigned char *)&tempbuff;
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);
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],
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
134 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
136 scsi_dev_desc[scsi_max_devs].target=pccb->target;
137 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
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;
146 scsi_print_error(pccb);
150 scsi_setup_read_capacity(pccb);
151 if(scsi_exec(pccb)!=TRUE) {
152 scsi_print_error(pccb);
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]);
165 printf (" Device %d: ", scsi_max_devs);
166 dev_print(&scsi_dev_desc[scsi_max_devs]);
182 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
184 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
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);
192 scsi_low_level_init(busdevfunc);
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;
210 ulong addr, cnt, checksum;
211 disk_partition_t info;
217 addr = CFG_LOAD_ADDR;
218 boot_device = getenv ("bootdevice");
221 addr = simple_strtoul(argv[1], NULL, 16);
222 boot_device = getenv ("bootdevice");
225 addr = simple_strtoul(argv[1], NULL, 16);
226 boot_device = argv[2];
229 printf ("Usage:\n%s\n", cmdtp->usage);
234 puts ("\n** No boot device **\n");
238 dev = simple_strtoul(boot_device, &ep, 16);
239 printf("booting from dev %d\n",dev);
240 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
241 printf ("\n** Device %d not available\n", dev);
247 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
250 part = simple_strtoul(++ep, NULL, 16);
252 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
253 printf("error reading partinfo\n");
256 if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
257 (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
258 printf ("\n** Invalid partition type \"%.32s\""
259 " (expect \"" BOOT_PART_TYPE "\")\n",
264 printf ("\nLoading from SCSI device %d, partition %d: "
265 "Name: %.32s Type: %.32s\n",
266 dev, part, info.name, info.type);
268 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
269 info.start, info.size, info.blksz);
271 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
272 printf ("** Read error on %d:%d\n", dev, part);
276 hdr = (image_header_t *)addr;
278 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
279 printf("\n** Bad Magic Number **\n");
283 checksum = ntohl(hdr->ih_hcrc);
286 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
287 puts ("\n** Bad Header Checksum **\n");
290 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
292 print_image_hdr (hdr);
293 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
294 cnt += info.blksz - 1;
298 if (scsi_read (dev, info.start+1, cnt,
299 (ulong *)(addr+info.blksz)) != cnt) {
300 printf ("** Read error on %d:%d\n", dev, part);
303 /* Loading ok, update default load address */
306 flush_cache (addr, (cnt+1)*info.blksz);
308 /* Check if we should attempt an auto-start */
309 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
311 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
312 local_args[0] = argv[0];
313 local_args[1] = NULL;
314 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
315 rcode = do_bootm (cmdtp, 0, 1, local_args);
320 /*********************************************************************************
321 * scsi command intepreter
323 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
327 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
329 if (strncmp(argv[1],"res",3) == 0) {
330 printf("\nReset SCSI\n");
335 if (strncmp(argv[1],"inf",3) == 0) {
337 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
338 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
339 continue; /* list only known devices */
340 printf ("SCSI dev. %d: ", i);
341 dev_print(&scsi_dev_desc[i]);
345 if (strncmp(argv[1],"dev",3) == 0) {
346 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
347 printf("\nno SCSI devices available\n");
350 printf ("\n Device %d: ", scsi_curr_dev);
351 dev_print(&scsi_dev_desc[scsi_curr_dev]);
354 if (strncmp(argv[1],"scan",4) == 0) {
358 if (strncmp(argv[1],"part",4) == 0) {
360 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
361 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
365 debug ("print_part of %x\n",dev);
366 print_part(&scsi_dev_desc[dev]);
370 printf("\nno SCSI devices available\n");
373 printf ("Usage:\n%s\n", cmdtp->usage);
376 if (strncmp(argv[1],"dev",3) == 0) {
377 int dev = (int)simple_strtoul(argv[2], NULL, 10);
378 printf ("\nSCSI device %d: ", dev);
379 if (dev >= CFG_SCSI_MAX_DEVICE) {
380 printf("unknown device\n");
383 printf ("\n Device %d: ", dev);
384 dev_print(&scsi_dev_desc[dev]);
385 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
389 printf("... is now current device\n");
392 if (strncmp(argv[1],"part",4) == 0) {
393 int dev = (int)simple_strtoul(argv[2], NULL, 10);
394 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
395 print_part(&scsi_dev_desc[dev]);
398 printf ("\nSCSI device %d not available\n", dev);
402 printf ("Usage:\n%s\n", cmdtp->usage);
405 /* at least 4 args */
406 if (strcmp(argv[1],"read") == 0) {
407 ulong addr = simple_strtoul(argv[2], NULL, 16);
408 ulong blk = simple_strtoul(argv[3], NULL, 16);
409 ulong cnt = simple_strtoul(argv[4], NULL, 16);
411 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
412 scsi_curr_dev, blk, cnt);
413 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
414 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
418 printf ("Usage:\n%s\n", cmdtp->usage);
422 /****************************************************************************************
426 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
428 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
430 ulong start,blks, buf_addr;
431 unsigned short smallblks;
432 ccb* pccb=(ccb *)&tempccb;
436 pccb->target=scsi_dev_desc[device].target;
437 pccb->lun=scsi_dev_desc[device].lun;
438 buf_addr=(unsigned long)buffer;
441 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
443 pccb->pdata=(unsigned char *)buf_addr;
444 if(blks>SCSI_MAX_READ_BLK) {
445 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
446 smallblks=SCSI_MAX_READ_BLK;
447 scsi_setup_read_ext(pccb,start,smallblks);
448 start+=SCSI_MAX_READ_BLK;
449 blks-=SCSI_MAX_READ_BLK;
452 pccb->datalen=scsi_dev_desc[device].blksz * blks;
453 smallblks=(unsigned short) blks;
454 scsi_setup_read_ext(pccb,start,smallblks);
458 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
459 if(scsi_exec(pccb)!=TRUE) {
460 scsi_print_error(pccb);
464 buf_addr+=pccb->datalen;
466 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
470 /* copy src to dest, skipping leading and trailing blanks
471 * and null terminate the string
473 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
489 for( ; start<=end; start++) {
496 /* Trim trailing blanks, and NUL-terminate string
498 void scsi_trim_trail (unsigned char *str, unsigned int len)
500 unsigned char *p = str + len - 1;
511 /************************************************************************************
512 * Some setup (fill-in) routines
514 void scsi_setup_test_unit_ready(ccb * pccb)
516 pccb->cmd[0]=SCSI_TST_U_RDY;
517 pccb->cmd[1]=pccb->lun<<5;
523 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
526 void scsi_setup_read_capacity(ccb * pccb)
528 pccb->cmd[0]=SCSI_RD_CAPAC;
529 pccb->cmd[1]=pccb->lun<<5;
539 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
543 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
545 pccb->cmd[0]=SCSI_READ10;
546 pccb->cmd[1]=pccb->lun<<5;
547 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
548 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
549 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
550 pccb->cmd[5]=((unsigned char) (start))&0xff;
552 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
553 pccb->cmd[8]=(unsigned char) blocks & 0xff;
556 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
557 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
558 pccb->cmd[0],pccb->cmd[1],
559 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
560 pccb->cmd[7],pccb->cmd[8]);
563 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
565 pccb->cmd[0]=SCSI_READ6;
566 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
567 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
568 pccb->cmd[3]=((unsigned char) (start))&0xff;
569 pccb->cmd[4]=(unsigned char) blocks & 0xff;
572 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
573 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
574 pccb->cmd[0],pccb->cmd[1],
575 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
579 void scsi_setup_inquiry(ccb * pccb)
581 pccb->cmd[0]=SCSI_INQUIRY;
582 pccb->cmd[1]=pccb->lun<<5;
585 if(pccb->datalen>255)
588 pccb->cmd[4]=(unsigned char)pccb->datalen;
591 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
597 "scsi - SCSI sub-system\n",
598 "reset - reset SCSI controller\n"
599 "scsi info - show available SCSI devices\n"
600 "scsi scan - (re-)scan SCSI bus\n"
601 "scsi device [dev] - show or set current device\n"
602 "scsi part [dev] - print partition table of one or all SCSI devices\n"
603 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
604 " to memory address `addr'\n"
608 scsiboot, 3, 1, do_scsiboot,
609 "scsiboot- boot from SCSI device\n",
610 "loadAddr dev:part\n"