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_DEV_LIST
38 #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
40 #ifdef CONFIG_SCSI_SYM53C8XX
41 #define SCSI_VEND_ID 0x1000
42 #ifndef CONFIG_SCSI_DEV_ID
43 #define SCSI_DEV_ID 0x0001
45 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
47 #elif defined CONFIG_SATA_ULI5288
49 #define SCSI_VEND_ID 0x10b9
50 #define SCSI_DEV_ID 0x5288
52 #elif !defined(CONFIG_SCSI_AHCI_PLAT)
53 #error no scsi device defined
55 #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
59 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
61 static ccb tempccb; /* temporary scsi command buffer */
63 static unsigned char tempbuff[512]; /* temporary data buffer */
65 static int scsi_max_devs; /* number of highest available scsi device */
67 static int scsi_curr_dev; /* current device */
69 static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
71 /********************************************************************************
72 * forward declerations of some Setup Routines
74 void scsi_setup_test_unit_ready(ccb * pccb);
75 void scsi_setup_read_capacity(ccb * pccb);
76 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
77 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
78 static void scsi_setup_write_ext(ccb *pccb, unsigned long start,
79 unsigned short blocks);
80 void scsi_setup_inquiry(ccb * pccb);
81 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
84 static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer);
85 static ulong scsi_write(int device, ulong blknr,
86 lbaint_t blkcnt, const void *buffer);
89 /*********************************************************************************
90 * (re)-scan the scsi bus and reports scsi device info
91 * to the user if mode = 1
93 void scsi_scan(int mode)
95 unsigned char i,perq,modi,lun;
96 unsigned long capacity,blksz;
97 ccb* pccb=(ccb *)&tempccb;
100 printf("scanning bus for devices...\n");
102 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
103 scsi_dev_desc[i].target=0xff;
104 scsi_dev_desc[i].lun=0xff;
105 scsi_dev_desc[i].lba=0;
106 scsi_dev_desc[i].blksz=0;
107 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
108 scsi_dev_desc[i].vendor[0]=0;
109 scsi_dev_desc[i].product[0]=0;
110 scsi_dev_desc[i].revision[0]=0;
111 scsi_dev_desc[i].removable=FALSE;
112 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
113 scsi_dev_desc[i].dev=i;
114 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
115 scsi_dev_desc[i].block_read=scsi_read;
116 scsi_dev_desc[i].block_write = scsi_write;
119 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
121 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
123 pccb->pdata=(unsigned char *)&tempbuff;
125 scsi_setup_inquiry(pccb);
126 if(scsi_exec(pccb)!=TRUE) {
127 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
128 debug ("Selection timeout ID %d\n",pccb->target);
129 continue; /* selection timeout => assuming no device present */
131 scsi_print_error(pccb);
136 if((perq & 0x1f)==0x1f) {
137 continue; /* skip unknown devices */
139 if((modi&0x80)==0x80) /* drive is removable */
140 scsi_dev_desc[scsi_max_devs].removable=TRUE;
141 /* get info for this device */
142 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
144 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
146 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
148 scsi_dev_desc[scsi_max_devs].target=pccb->target;
149 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
152 scsi_setup_test_unit_ready(pccb);
153 if(scsi_exec(pccb)!=TRUE) {
154 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
155 scsi_dev_desc[scsi_max_devs].type=perq;
158 scsi_print_error(pccb);
162 scsi_setup_read_capacity(pccb);
163 if(scsi_exec(pccb)!=TRUE) {
164 scsi_print_error(pccb);
167 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
168 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
169 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
170 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
171 scsi_dev_desc[scsi_max_devs].lba=capacity;
172 scsi_dev_desc[scsi_max_devs].blksz=blksz;
173 scsi_dev_desc[scsi_max_devs].type=perq;
174 init_part(&scsi_dev_desc[scsi_max_devs]);
177 printf (" Device %d: ", scsi_max_devs);
178 dev_print(&scsi_dev_desc[scsi_max_devs]);
188 printf("Found %d device(s).\n", scsi_max_devs);
189 setenv_ulong("scsidevs", scsi_max_devs);
192 int scsi_get_disk_count(void)
194 return scsi_max_devs;
203 * Find a device from the list, this driver will support a single
206 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
207 /* get PCI Device ID */
208 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
209 scsi_device_list[i].device,
211 if (busdevfunc != -1)
215 if (busdevfunc == -1) {
216 printf("Error: SCSI Controller(s) ");
217 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
219 scsi_device_list[i].vendor,
220 scsi_device_list[i].device);
222 printf("not found\n");
227 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
228 scsi_device_list[i].vendor,
229 scsi_device_list[i].device,
230 (busdevfunc >> 16) & 0xFF,
231 (busdevfunc >> 11) & 0x1F,
232 (busdevfunc >> 8) & 0x7);
235 scsi_low_level_init(busdevfunc);
240 #ifdef CONFIG_PARTITIONS
241 block_dev_desc_t * scsi_get_dev(int dev)
243 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
247 /******************************************************************************
248 * scsi boot command intepreter. Derived from diskboot
250 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
252 return common_diskboot(cmdtp, "scsi", argc, argv);
255 /*********************************************************************************
256 * scsi command intepreter
258 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
263 return CMD_RET_USAGE;
266 if (strncmp(argv[1],"res",3) == 0) {
267 printf("\nReset SCSI\n");
272 if (strncmp(argv[1],"inf",3) == 0) {
274 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
275 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
276 continue; /* list only known devices */
277 printf ("SCSI dev. %d: ", i);
278 dev_print(&scsi_dev_desc[i]);
282 if (strncmp(argv[1],"dev",3) == 0) {
283 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
284 printf("\nno SCSI devices available\n");
287 printf ("\n Device %d: ", scsi_curr_dev);
288 dev_print(&scsi_dev_desc[scsi_curr_dev]);
291 if (strncmp(argv[1],"scan",4) == 0) {
295 if (strncmp(argv[1],"part",4) == 0) {
297 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
298 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
302 debug ("print_part of %x\n",dev);
303 print_part(&scsi_dev_desc[dev]);
307 printf("\nno SCSI devices available\n");
310 return CMD_RET_USAGE;
312 if (strncmp(argv[1],"dev",3) == 0) {
313 int dev = (int)simple_strtoul(argv[2], NULL, 10);
314 printf ("\nSCSI device %d: ", dev);
315 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
316 printf("unknown device\n");
319 printf ("\n Device %d: ", dev);
320 dev_print(&scsi_dev_desc[dev]);
321 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
325 printf("... is now current device\n");
328 if (strncmp(argv[1],"part",4) == 0) {
329 int dev = (int)simple_strtoul(argv[2], NULL, 10);
330 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
331 print_part(&scsi_dev_desc[dev]);
334 printf ("\nSCSI device %d not available\n", dev);
338 return CMD_RET_USAGE;
340 /* at least 4 args */
341 if (strcmp(argv[1],"read") == 0) {
342 ulong addr = simple_strtoul(argv[2], NULL, 16);
343 ulong blk = simple_strtoul(argv[3], NULL, 16);
344 ulong cnt = simple_strtoul(argv[4], NULL, 16);
346 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
347 scsi_curr_dev, blk, cnt);
348 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
349 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
351 } else if (strcmp(argv[1], "write") == 0) {
352 ulong addr = simple_strtoul(argv[2], NULL, 16);
353 ulong blk = simple_strtoul(argv[3], NULL, 16);
354 ulong cnt = simple_strtoul(argv[4], NULL, 16);
356 printf("\nSCSI write: device %d block # %ld, "
358 scsi_curr_dev, blk, cnt);
359 n = scsi_write(scsi_curr_dev, blk, cnt,
361 printf("%ld blocks written: %s\n", n,
362 (n == cnt) ? "OK" : "ERROR");
366 return CMD_RET_USAGE;
369 /****************************************************************************************
373 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
375 static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
377 lbaint_t start, blks;
379 unsigned short smallblks;
380 ccb* pccb=(ccb *)&tempccb;
384 pccb->target=scsi_dev_desc[device].target;
385 pccb->lun=scsi_dev_desc[device].lun;
386 buf_addr=(unsigned long)buffer;
389 debug("\nscsi_read: dev %d startblk " LBAF
390 ", blccnt " LBAF " buffer %lx\n",
391 device, start, blks, (unsigned long)buffer);
393 pccb->pdata=(unsigned char *)buf_addr;
394 if(blks>SCSI_MAX_READ_BLK) {
395 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
396 smallblks=SCSI_MAX_READ_BLK;
397 scsi_setup_read_ext(pccb,start,smallblks);
398 start+=SCSI_MAX_READ_BLK;
399 blks-=SCSI_MAX_READ_BLK;
402 pccb->datalen=scsi_dev_desc[device].blksz * blks;
403 smallblks=(unsigned short) blks;
404 scsi_setup_read_ext(pccb,start,smallblks);
408 debug("scsi_read_ext: startblk " LBAF
409 ", blccnt %x buffer %lx\n",
410 start, smallblks, buf_addr);
411 if(scsi_exec(pccb)!=TRUE) {
412 scsi_print_error(pccb);
416 buf_addr+=pccb->datalen;
418 debug("scsi_read_ext: end startblk " LBAF
419 ", blccnt %x buffer %lx\n", start, smallblks, buf_addr);
423 /*******************************************************************************
427 /* Almost the maximum amount of the scsi_ext command.. */
428 #define SCSI_MAX_WRITE_BLK 0xFFFF
430 static ulong scsi_write(int device, ulong blknr,
431 lbaint_t blkcnt, const void *buffer)
433 lbaint_t start, blks;
435 unsigned short smallblks;
436 ccb* pccb = (ccb *)&tempccb;
440 pccb->target = scsi_dev_desc[device].target;
441 pccb->lun = scsi_dev_desc[device].lun;
442 buf_addr = (unsigned long)buffer;
445 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
446 __func__, device, start, blks, (unsigned long)buffer);
448 pccb->pdata = (unsigned char *)buf_addr;
449 if (blks > SCSI_MAX_WRITE_BLK) {
450 pccb->datalen = (scsi_dev_desc[device].blksz *
452 smallblks = SCSI_MAX_WRITE_BLK;
453 scsi_setup_write_ext(pccb, start, smallblks);
454 start += SCSI_MAX_WRITE_BLK;
455 blks -= SCSI_MAX_WRITE_BLK;
457 pccb->datalen = scsi_dev_desc[device].blksz * blks;
458 smallblks = (unsigned short)blks;
459 scsi_setup_write_ext(pccb, start, smallblks);
463 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
464 __func__, start, smallblks, buf_addr);
465 if (scsi_exec(pccb) != TRUE) {
466 scsi_print_error(pccb);
470 buf_addr += pccb->datalen;
472 debug("%s: end startblk " LBAF ", blccnt %x buffer %lx\n",
473 __func__, start, smallblks, buf_addr);
477 /* copy src to dest, skipping leading and trailing blanks
478 * and null terminate the string
480 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
496 for( ; start<=end; start++) {
503 /* Trim trailing blanks, and NUL-terminate string
505 void scsi_trim_trail (unsigned char *str, unsigned int len)
507 unsigned char *p = str + len - 1;
518 /************************************************************************************
519 * Some setup (fill-in) routines
521 void scsi_setup_test_unit_ready(ccb * pccb)
523 pccb->cmd[0]=SCSI_TST_U_RDY;
524 pccb->cmd[1]=pccb->lun<<5;
530 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
533 void scsi_setup_read_capacity(ccb * pccb)
535 pccb->cmd[0]=SCSI_RD_CAPAC;
536 pccb->cmd[1]=pccb->lun<<5;
546 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
550 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
552 pccb->cmd[0]=SCSI_READ10;
553 pccb->cmd[1]=pccb->lun<<5;
554 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
555 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
556 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
557 pccb->cmd[5]=((unsigned char) (start))&0xff;
559 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
560 pccb->cmd[8]=(unsigned char) blocks & 0xff;
563 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
564 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
565 pccb->cmd[0],pccb->cmd[1],
566 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
567 pccb->cmd[7],pccb->cmd[8]);
570 void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks)
572 pccb->cmd[0] = SCSI_WRITE10;
573 pccb->cmd[1] = pccb->lun << 5;
574 pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff;
575 pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff;
576 pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff;
577 pccb->cmd[5] = ((unsigned char) (start)) & 0xff;
579 pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff;
580 pccb->cmd[8] = (unsigned char)blocks & 0xff;
583 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
584 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
586 pccb->cmd[0], pccb->cmd[1],
587 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
588 pccb->cmd[7], pccb->cmd[8]);
591 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
593 pccb->cmd[0]=SCSI_READ6;
594 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
595 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
596 pccb->cmd[3]=((unsigned char) (start))&0xff;
597 pccb->cmd[4]=(unsigned char) blocks & 0xff;
600 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
601 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
602 pccb->cmd[0],pccb->cmd[1],
603 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
607 void scsi_setup_inquiry(ccb * pccb)
609 pccb->cmd[0]=SCSI_INQUIRY;
610 pccb->cmd[1]=pccb->lun<<5;
613 if(pccb->datalen>255)
616 pccb->cmd[4]=(unsigned char)pccb->datalen;
619 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
626 "reset - reset SCSI controller\n"
627 "scsi info - show available SCSI devices\n"
628 "scsi scan - (re-)scan SCSI bus\n"
629 "scsi device [dev] - show or set current device\n"
630 "scsi part [dev] - print partition table of one or all SCSI devices\n"
631 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
632 " to memory address `addr'\n"
633 "scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
634 " `blk#' from memory address `addr'"
638 scsiboot, 3, 1, do_scsiboot,
639 "boot from SCSI device",