3 * Denis Peter, MPL AG Switzerland
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/processor.h>
18 #ifdef CONFIG_SCSI_DEV_LIST
19 #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
21 #ifdef CONFIG_SCSI_SYM53C8XX
22 #define SCSI_VEND_ID 0x1000
23 #ifndef CONFIG_SCSI_DEV_ID
24 #define SCSI_DEV_ID 0x0001
26 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
28 #elif defined CONFIG_SATA_ULI5288
30 #define SCSI_VEND_ID 0x10b9
31 #define SCSI_DEV_ID 0x5288
33 #elif !defined(CONFIG_SCSI_AHCI_PLAT)
34 #error no scsi device defined
36 #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
40 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
42 static ccb tempccb; /* temporary scsi command buffer */
44 static unsigned char tempbuff[512]; /* temporary data buffer */
46 static int scsi_max_devs; /* number of highest available scsi device */
48 static int scsi_curr_dev; /* current device */
50 static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
52 /********************************************************************************
53 * forward declerations of some Setup Routines
55 void scsi_setup_test_unit_ready(ccb * pccb);
56 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
57 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
58 static void scsi_setup_write_ext(ccb *pccb, unsigned long start,
59 unsigned short blocks);
60 void scsi_setup_inquiry(ccb * pccb);
61 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
64 static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
65 unsigned long *blksz);
66 static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
68 static ulong scsi_write(int device, lbaint_t blknr,
69 lbaint_t blkcnt, const void *buffer);
72 /*********************************************************************************
73 * (re)-scan the scsi bus and reports scsi device info
74 * to the user if mode = 1
76 void scsi_scan(int mode)
78 unsigned char i,perq,modi,lun;
81 ccb* pccb=(ccb *)&tempccb;
84 printf("scanning bus for devices...\n");
86 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
87 scsi_dev_desc[i].target=0xff;
88 scsi_dev_desc[i].lun=0xff;
89 scsi_dev_desc[i].lba=0;
90 scsi_dev_desc[i].blksz=0;
91 scsi_dev_desc[i].log2blksz =
92 LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz));
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 scsi_dev_desc[i].block_write = scsi_write;
105 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
107 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
109 pccb->pdata=(unsigned char *)&tempbuff;
111 scsi_setup_inquiry(pccb);
112 if (scsi_exec(pccb) != true) {
113 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
114 debug ("Selection timeout ID %d\n",pccb->target);
115 continue; /* selection timeout => assuming no device present */
117 scsi_print_error(pccb);
122 if((perq & 0x1f)==0x1f) {
123 continue; /* skip unknown devices */
125 if((modi&0x80)==0x80) /* drive is removable */
126 scsi_dev_desc[scsi_max_devs].removable=true;
127 /* get info for this device */
128 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
130 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
134 scsi_dev_desc[scsi_max_devs].target=pccb->target;
135 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
138 scsi_setup_test_unit_ready(pccb);
139 if (scsi_exec(pccb) != true) {
140 if (scsi_dev_desc[scsi_max_devs].removable == true) {
141 scsi_dev_desc[scsi_max_devs].type=perq;
144 scsi_print_error(pccb);
147 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
148 scsi_print_error(pccb);
151 scsi_dev_desc[scsi_max_devs].lba=capacity;
152 scsi_dev_desc[scsi_max_devs].blksz=blksz;
153 scsi_dev_desc[scsi_max_devs].log2blksz =
154 LOG2(scsi_dev_desc[scsi_max_devs].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]);
170 printf("Found %d device(s).\n", scsi_max_devs);
171 setenv_ulong("scsidevs", scsi_max_devs);
174 int scsi_get_disk_count(void)
176 return scsi_max_devs;
185 * Find a device from the list, this driver will support a single
188 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
189 /* get PCI Device ID */
190 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
191 scsi_device_list[i].device,
193 if (busdevfunc != -1)
197 if (busdevfunc == -1) {
198 printf("Error: SCSI Controller(s) ");
199 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
201 scsi_device_list[i].vendor,
202 scsi_device_list[i].device);
204 printf("not found\n");
209 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
210 scsi_device_list[i].vendor,
211 scsi_device_list[i].device,
212 (busdevfunc >> 16) & 0xFF,
213 (busdevfunc >> 11) & 0x1F,
214 (busdevfunc >> 8) & 0x7);
217 scsi_low_level_init(busdevfunc);
222 #ifdef CONFIG_PARTITIONS
223 block_dev_desc_t * scsi_get_dev(int dev)
225 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
229 /******************************************************************************
230 * scsi boot command intepreter. Derived from diskboot
232 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
234 return common_diskboot(cmdtp, "scsi", argc, argv);
237 /*********************************************************************************
238 * scsi command intepreter
240 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
245 return CMD_RET_USAGE;
248 if (strncmp(argv[1],"res",3) == 0) {
249 printf("\nReset SCSI\n");
254 if (strncmp(argv[1],"inf",3) == 0) {
256 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
257 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
258 continue; /* list only known devices */
259 printf ("SCSI dev. %d: ", i);
260 dev_print(&scsi_dev_desc[i]);
264 if (strncmp(argv[1],"dev",3) == 0) {
265 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
266 printf("\nno SCSI devices available\n");
269 printf ("\n Device %d: ", scsi_curr_dev);
270 dev_print(&scsi_dev_desc[scsi_curr_dev]);
273 if (strncmp(argv[1],"scan",4) == 0) {
277 if (strncmp(argv[1],"part",4) == 0) {
279 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
280 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
284 debug ("print_part of %x\n",dev);
285 print_part(&scsi_dev_desc[dev]);
289 printf("\nno SCSI devices available\n");
292 return CMD_RET_USAGE;
294 if (strncmp(argv[1],"dev",3) == 0) {
295 int dev = (int)simple_strtoul(argv[2], NULL, 10);
296 printf ("\nSCSI device %d: ", dev);
297 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
298 printf("unknown device\n");
301 printf ("\n Device %d: ", dev);
302 dev_print(&scsi_dev_desc[dev]);
303 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
307 printf("... is now current device\n");
310 if (strncmp(argv[1],"part",4) == 0) {
311 int dev = (int)simple_strtoul(argv[2], NULL, 10);
312 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
313 print_part(&scsi_dev_desc[dev]);
316 printf ("\nSCSI device %d not available\n", dev);
320 return CMD_RET_USAGE;
322 /* at least 4 args */
323 if (strcmp(argv[1],"read") == 0) {
324 ulong addr = simple_strtoul(argv[2], NULL, 16);
325 ulong blk = simple_strtoul(argv[3], NULL, 16);
326 ulong cnt = simple_strtoul(argv[4], NULL, 16);
328 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
329 scsi_curr_dev, blk, cnt);
330 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
331 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
333 } else if (strcmp(argv[1], "write") == 0) {
334 ulong addr = simple_strtoul(argv[2], NULL, 16);
335 ulong blk = simple_strtoul(argv[3], NULL, 16);
336 ulong cnt = simple_strtoul(argv[4], NULL, 16);
338 printf("\nSCSI write: device %d block # %ld, "
340 scsi_curr_dev, blk, cnt);
341 n = scsi_write(scsi_curr_dev, blk, cnt,
343 printf("%ld blocks written: %s\n", n,
344 (n == cnt) ? "OK" : "ERROR");
348 return CMD_RET_USAGE;
351 /****************************************************************************************
355 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
357 static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
360 lbaint_t start, blks;
362 unsigned short smallblks;
363 ccb* pccb=(ccb *)&tempccb;
367 pccb->target=scsi_dev_desc[device].target;
368 pccb->lun=scsi_dev_desc[device].lun;
369 buf_addr=(unsigned long)buffer;
372 debug("\nscsi_read: dev %d startblk " LBAF
373 ", blccnt " LBAF " buffer %lx\n",
374 device, start, blks, (unsigned long)buffer);
376 pccb->pdata=(unsigned char *)buf_addr;
377 if(blks>SCSI_MAX_READ_BLK) {
378 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
379 smallblks=SCSI_MAX_READ_BLK;
380 scsi_setup_read_ext(pccb,start,smallblks);
381 start+=SCSI_MAX_READ_BLK;
382 blks-=SCSI_MAX_READ_BLK;
385 pccb->datalen=scsi_dev_desc[device].blksz * blks;
386 smallblks=(unsigned short) blks;
387 scsi_setup_read_ext(pccb,start,smallblks);
391 debug("scsi_read_ext: startblk " LBAF
392 ", blccnt %x buffer %lx\n",
393 start, smallblks, buf_addr);
394 if (scsi_exec(pccb) != true) {
395 scsi_print_error(pccb);
399 buf_addr+=pccb->datalen;
401 debug("scsi_read_ext: end startblk " LBAF
402 ", blccnt %x buffer %lx\n", start, smallblks, buf_addr);
406 /*******************************************************************************
410 /* Almost the maximum amount of the scsi_ext command.. */
411 #define SCSI_MAX_WRITE_BLK 0xFFFF
413 static ulong scsi_write(int device, lbaint_t blknr,
414 lbaint_t blkcnt, const void *buffer)
416 lbaint_t start, blks;
418 unsigned short smallblks;
419 ccb* pccb = (ccb *)&tempccb;
423 pccb->target = scsi_dev_desc[device].target;
424 pccb->lun = scsi_dev_desc[device].lun;
425 buf_addr = (unsigned long)buffer;
428 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
429 __func__, device, start, blks, (unsigned long)buffer);
431 pccb->pdata = (unsigned char *)buf_addr;
432 if (blks > SCSI_MAX_WRITE_BLK) {
433 pccb->datalen = (scsi_dev_desc[device].blksz *
435 smallblks = SCSI_MAX_WRITE_BLK;
436 scsi_setup_write_ext(pccb, start, smallblks);
437 start += SCSI_MAX_WRITE_BLK;
438 blks -= SCSI_MAX_WRITE_BLK;
440 pccb->datalen = scsi_dev_desc[device].blksz * blks;
441 smallblks = (unsigned short)blks;
442 scsi_setup_write_ext(pccb, start, smallblks);
446 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
447 __func__, start, smallblks, buf_addr);
448 if (scsi_exec(pccb) != true) {
449 scsi_print_error(pccb);
453 buf_addr += pccb->datalen;
455 debug("%s: end startblk " LBAF ", blccnt %x buffer %lx\n",
456 __func__, start, smallblks, buf_addr);
460 /* copy src to dest, skipping leading and trailing blanks
461 * and null terminate the string
463 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
479 for( ; start<=end; start++) {
486 /* Trim trailing blanks, and NUL-terminate string
488 void scsi_trim_trail (unsigned char *str, unsigned int len)
490 unsigned char *p = str + len - 1;
500 int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz)
504 memset(pccb->cmd, 0, sizeof(pccb->cmd));
505 pccb->cmd[0] = SCSI_RD_CAPAC10;
506 pccb->cmd[1] = pccb->lun << 5;
508 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
511 if (scsi_exec(pccb) != true)
514 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
515 ((lbaint_t)pccb->pdata[1] << 16) |
516 ((lbaint_t)pccb->pdata[2] << 8) |
517 ((lbaint_t)pccb->pdata[3]);
519 if (*capacity != 0xffffffff) {
520 /* Read capacity (10) was sufficient for this drive. */
521 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
522 ((unsigned long)pccb->pdata[5] << 16) |
523 ((unsigned long)pccb->pdata[6] << 8) |
524 ((unsigned long)pccb->pdata[7]);
528 /* Read capacity (10) was insufficient. Use read capacity (16). */
530 memset(pccb->cmd, 0, sizeof(pccb->cmd));
531 pccb->cmd[0] = SCSI_RD_CAPAC16;
534 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
537 if (scsi_exec(pccb) != true)
540 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
541 ((uint64_t)pccb->pdata[1] << 48) |
542 ((uint64_t)pccb->pdata[2] << 40) |
543 ((uint64_t)pccb->pdata[3] << 32) |
544 ((uint64_t)pccb->pdata[4] << 24) |
545 ((uint64_t)pccb->pdata[5] << 16) |
546 ((uint64_t)pccb->pdata[6] << 8) |
547 ((uint64_t)pccb->pdata[7]);
549 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
550 ((uint64_t)pccb->pdata[9] << 48) |
551 ((uint64_t)pccb->pdata[10] << 40) |
552 ((uint64_t)pccb->pdata[11] << 32) |
553 ((uint64_t)pccb->pdata[12] << 24) |
554 ((uint64_t)pccb->pdata[13] << 16) |
555 ((uint64_t)pccb->pdata[14] << 8) |
556 ((uint64_t)pccb->pdata[15]);
562 /************************************************************************************
563 * Some setup (fill-in) routines
565 void scsi_setup_test_unit_ready(ccb * pccb)
567 pccb->cmd[0]=SCSI_TST_U_RDY;
568 pccb->cmd[1]=pccb->lun<<5;
574 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
577 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
579 pccb->cmd[0]=SCSI_READ10;
580 pccb->cmd[1]=pccb->lun<<5;
581 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
582 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
583 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
584 pccb->cmd[5]=((unsigned char) (start))&0xff;
586 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
587 pccb->cmd[8]=(unsigned char) blocks & 0xff;
590 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
591 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
592 pccb->cmd[0],pccb->cmd[1],
593 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
594 pccb->cmd[7],pccb->cmd[8]);
597 void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks)
599 pccb->cmd[0] = SCSI_WRITE10;
600 pccb->cmd[1] = pccb->lun << 5;
601 pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff;
602 pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff;
603 pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff;
604 pccb->cmd[5] = ((unsigned char) (start)) & 0xff;
606 pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff;
607 pccb->cmd[8] = (unsigned char)blocks & 0xff;
610 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
611 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
613 pccb->cmd[0], pccb->cmd[1],
614 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
615 pccb->cmd[7], pccb->cmd[8]);
618 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
620 pccb->cmd[0]=SCSI_READ6;
621 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
622 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
623 pccb->cmd[3]=((unsigned char) (start))&0xff;
624 pccb->cmd[4]=(unsigned char) blocks & 0xff;
627 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
628 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
629 pccb->cmd[0],pccb->cmd[1],
630 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
634 void scsi_setup_inquiry(ccb * pccb)
636 pccb->cmd[0]=SCSI_INQUIRY;
637 pccb->cmd[1]=pccb->lun<<5;
640 if(pccb->datalen>255)
643 pccb->cmd[4]=(unsigned char)pccb->datalen;
646 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
653 "reset - reset SCSI controller\n"
654 "scsi info - show available SCSI devices\n"
655 "scsi scan - (re-)scan SCSI bus\n"
656 "scsi device [dev] - show or set current device\n"
657 "scsi part [dev] - print partition table of one or all SCSI devices\n"
658 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
659 " to memory address `addr'\n"
660 "scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
661 " `blk#' from memory address `addr'"
665 scsiboot, 3, 1, do_scsiboot,
666 "boot from SCSI device",