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 void scsi_setup_inquiry(ccb * pccb);
79 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
82 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer);
85 /*********************************************************************************
86 * (re)-scan the scsi bus and reports scsi device info
87 * to the user if mode = 1
89 void scsi_scan(int mode)
91 unsigned char i,perq,modi,lun;
92 unsigned long capacity,blksz;
93 ccb* pccb=(ccb *)&tempccb;
96 printf("scanning bus for devices...\n");
98 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
99 scsi_dev_desc[i].target=0xff;
100 scsi_dev_desc[i].lun=0xff;
101 scsi_dev_desc[i].lba=0;
102 scsi_dev_desc[i].blksz=0;
103 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
104 scsi_dev_desc[i].vendor[0]=0;
105 scsi_dev_desc[i].product[0]=0;
106 scsi_dev_desc[i].revision[0]=0;
107 scsi_dev_desc[i].removable=FALSE;
108 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
109 scsi_dev_desc[i].dev=i;
110 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
111 scsi_dev_desc[i].block_read=scsi_read;
114 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
116 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
118 pccb->pdata=(unsigned char *)&tempbuff;
120 scsi_setup_inquiry(pccb);
121 if(scsi_exec(pccb)!=TRUE) {
122 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
123 debug ("Selection timeout ID %d\n",pccb->target);
124 continue; /* selection timeout => assuming no device present */
126 scsi_print_error(pccb);
131 if((perq & 0x1f)==0x1f) {
132 continue; /* skip unknown devices */
134 if((modi&0x80)==0x80) /* drive is removable */
135 scsi_dev_desc[scsi_max_devs].removable=TRUE;
136 /* get info for this device */
137 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
139 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
141 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
143 scsi_dev_desc[scsi_max_devs].target=pccb->target;
144 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
147 scsi_setup_test_unit_ready(pccb);
148 if(scsi_exec(pccb)!=TRUE) {
149 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
150 scsi_dev_desc[scsi_max_devs].type=perq;
153 scsi_print_error(pccb);
157 scsi_setup_read_capacity(pccb);
158 if(scsi_exec(pccb)!=TRUE) {
159 scsi_print_error(pccb);
162 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
163 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
164 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
165 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
166 scsi_dev_desc[scsi_max_devs].lba=capacity;
167 scsi_dev_desc[scsi_max_devs].blksz=blksz;
168 scsi_dev_desc[scsi_max_devs].type=perq;
169 init_part(&scsi_dev_desc[scsi_max_devs]);
172 printf (" Device %d: ", scsi_max_devs);
173 dev_print(&scsi_dev_desc[scsi_max_devs]);
190 * Find a device from the list, this driver will support a single
193 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
194 /* get PCI Device ID */
195 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
196 scsi_device_list[i].device,
198 if (busdevfunc != -1)
202 if (busdevfunc == -1) {
203 printf("Error: SCSI Controller(s) ");
204 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
206 scsi_device_list[i].vendor,
207 scsi_device_list[i].device);
209 printf("not found\n");
214 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
215 scsi_device_list[i].vendor,
216 scsi_device_list[i].device,
217 (busdevfunc >> 16) & 0xFF,
218 (busdevfunc >> 11) & 0x1F,
219 (busdevfunc >> 8) & 0x7);
222 scsi_low_level_init(busdevfunc);
227 #ifdef CONFIG_PARTITIONS
228 block_dev_desc_t * scsi_get_dev(int dev)
230 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
234 /******************************************************************************
235 * scsi boot command intepreter. Derived from diskboot
237 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
239 return common_diskboot(cmdtp, "scsi", argc, argv);
242 /*********************************************************************************
243 * scsi command intepreter
245 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
250 return CMD_RET_USAGE;
253 if (strncmp(argv[1],"res",3) == 0) {
254 printf("\nReset SCSI\n");
259 if (strncmp(argv[1],"inf",3) == 0) {
261 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
262 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
263 continue; /* list only known devices */
264 printf ("SCSI dev. %d: ", i);
265 dev_print(&scsi_dev_desc[i]);
269 if (strncmp(argv[1],"dev",3) == 0) {
270 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
271 printf("\nno SCSI devices available\n");
274 printf ("\n Device %d: ", scsi_curr_dev);
275 dev_print(&scsi_dev_desc[scsi_curr_dev]);
278 if (strncmp(argv[1],"scan",4) == 0) {
282 if (strncmp(argv[1],"part",4) == 0) {
284 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
285 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
289 debug ("print_part of %x\n",dev);
290 print_part(&scsi_dev_desc[dev]);
294 printf("\nno SCSI devices available\n");
297 return CMD_RET_USAGE;
299 if (strncmp(argv[1],"dev",3) == 0) {
300 int dev = (int)simple_strtoul(argv[2], NULL, 10);
301 printf ("\nSCSI device %d: ", dev);
302 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
303 printf("unknown device\n");
306 printf ("\n Device %d: ", dev);
307 dev_print(&scsi_dev_desc[dev]);
308 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
312 printf("... is now current device\n");
315 if (strncmp(argv[1],"part",4) == 0) {
316 int dev = (int)simple_strtoul(argv[2], NULL, 10);
317 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
318 print_part(&scsi_dev_desc[dev]);
321 printf ("\nSCSI device %d not available\n", dev);
325 return CMD_RET_USAGE;
327 /* at least 4 args */
328 if (strcmp(argv[1],"read") == 0) {
329 ulong addr = simple_strtoul(argv[2], NULL, 16);
330 ulong blk = simple_strtoul(argv[3], NULL, 16);
331 ulong cnt = simple_strtoul(argv[4], NULL, 16);
333 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
334 scsi_curr_dev, blk, cnt);
335 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
336 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
340 return CMD_RET_USAGE;
343 /****************************************************************************************
347 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
349 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
351 ulong start,blks, buf_addr;
352 unsigned short smallblks;
353 ccb* pccb=(ccb *)&tempccb;
357 pccb->target=scsi_dev_desc[device].target;
358 pccb->lun=scsi_dev_desc[device].lun;
359 buf_addr=(unsigned long)buffer;
362 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
364 pccb->pdata=(unsigned char *)buf_addr;
365 if(blks>SCSI_MAX_READ_BLK) {
366 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
367 smallblks=SCSI_MAX_READ_BLK;
368 scsi_setup_read_ext(pccb,start,smallblks);
369 start+=SCSI_MAX_READ_BLK;
370 blks-=SCSI_MAX_READ_BLK;
373 pccb->datalen=scsi_dev_desc[device].blksz * blks;
374 smallblks=(unsigned short) blks;
375 scsi_setup_read_ext(pccb,start,smallblks);
379 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
380 if(scsi_exec(pccb)!=TRUE) {
381 scsi_print_error(pccb);
385 buf_addr+=pccb->datalen;
387 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
391 /* copy src to dest, skipping leading and trailing blanks
392 * and null terminate the string
394 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
410 for( ; start<=end; start++) {
417 /* Trim trailing blanks, and NUL-terminate string
419 void scsi_trim_trail (unsigned char *str, unsigned int len)
421 unsigned char *p = str + len - 1;
432 /************************************************************************************
433 * Some setup (fill-in) routines
435 void scsi_setup_test_unit_ready(ccb * pccb)
437 pccb->cmd[0]=SCSI_TST_U_RDY;
438 pccb->cmd[1]=pccb->lun<<5;
444 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
447 void scsi_setup_read_capacity(ccb * pccb)
449 pccb->cmd[0]=SCSI_RD_CAPAC;
450 pccb->cmd[1]=pccb->lun<<5;
460 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
464 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
466 pccb->cmd[0]=SCSI_READ10;
467 pccb->cmd[1]=pccb->lun<<5;
468 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
469 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
470 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
471 pccb->cmd[5]=((unsigned char) (start))&0xff;
473 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
474 pccb->cmd[8]=(unsigned char) blocks & 0xff;
477 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
478 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
479 pccb->cmd[0],pccb->cmd[1],
480 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
481 pccb->cmd[7],pccb->cmd[8]);
484 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
486 pccb->cmd[0]=SCSI_READ6;
487 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
488 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
489 pccb->cmd[3]=((unsigned char) (start))&0xff;
490 pccb->cmd[4]=(unsigned char) blocks & 0xff;
493 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
494 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
495 pccb->cmd[0],pccb->cmd[1],
496 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
500 void scsi_setup_inquiry(ccb * pccb)
502 pccb->cmd[0]=SCSI_INQUIRY;
503 pccb->cmd[1]=pccb->lun<<5;
506 if(pccb->datalen>255)
509 pccb->cmd[4]=(unsigned char)pccb->datalen;
512 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
519 "reset - reset SCSI controller\n"
520 "scsi info - show available SCSI devices\n"
521 "scsi scan - (re-)scan SCSI bus\n"
522 "scsi device [dev] - show or set current device\n"
523 "scsi part [dev] - print partition table of one or all SCSI devices\n"
524 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
525 " to memory address `addr'"
529 scsiboot, 3, 1, do_scsiboot,
530 "boot from SCSI device",