blob: 8e8e93086903ab6c147dab3c21186c9cae84b3b7 [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 *
23 *
24 *
25 */
26
27/*
28 * SCSI support.
29 */
wdenke887afc2002-08-27 09:44:07 +000030#include <common.h>
31#include <command.h>
wdenke887afc2002-08-27 09:44:07 +000032#include <asm/processor.h>
33#include <scsi.h>
34#include <image.h>
wdenke887afc2002-08-27 09:44:07 +000035#include <pci.h>
36
wdenke887afc2002-08-27 09:44:07 +000037#ifdef CONFIG_SCSI_SYM53C8XX
38#define SCSI_VEND_ID 0x1000
39#ifndef CONFIG_SCSI_DEV_ID
40#define SCSI_DEV_ID 0x0001
41#else
42#define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
43#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080044#elif defined CONFIG_SATA_ULI5288
45
46#define SCSI_VEND_ID 0x10b9
47#define SCSI_DEV_ID 0x5288
48
Rob Herring942e3142011-07-06 16:13:36 +000049#elif !defined(CONFIG_SCSI_AHCI_PLAT)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080050#error no scsi device defined
wdenke887afc2002-08-27 09:44:07 +000051#endif
52
53
54static ccb tempccb; /* temporary scsi command buffer */
55
56static unsigned char tempbuff[512]; /* temporary data buffer */
57
58static int scsi_max_devs; /* number of highest available scsi device */
59
60static int scsi_curr_dev; /* current device */
61
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
wdenke887afc2002-08-27 09:44:07 +000063
64/********************************************************************************
65 * forward declerations of some Setup Routines
66 */
67void scsi_setup_test_unit_ready(ccb * pccb);
68void scsi_setup_read_capacity(ccb * pccb);
69void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
70void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
71void scsi_setup_inquiry(ccb * pccb);
72void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
73
74
Grant Likelyeb867a72007-02-20 09:05:45 +010075ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer);
wdenke887afc2002-08-27 09:44:07 +000076
77
78/*********************************************************************************
79 * (re)-scan the scsi bus and reports scsi device info
80 * to the user if mode = 1
81 */
82void scsi_scan(int mode)
83{
84 unsigned char i,perq,modi,lun;
85 unsigned long capacity,blksz;
86 ccb* pccb=(ccb *)&tempccb;
87
88 if(mode==1) {
89 printf("scanning bus for devices...\n");
90 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
wdenke887afc2002-08-27 09:44:07 +000092 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;
105 }
106 scsi_max_devs=0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200107 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
wdenke887afc2002-08-27 09:44:07 +0000108 pccb->target=i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
wdenke887afc2002-08-27 09:44:07 +0000110 pccb->lun=lun;
111 pccb->pdata=(unsigned char *)&tempbuff;
112 pccb->datalen=512;
113 scsi_setup_inquiry(pccb);
114 if(scsi_exec(pccb)!=TRUE) {
115 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
wdenk1a344f22005-02-03 23:00:49 +0000116 debug ("Selection timeout ID %d\n",pccb->target);
wdenke887afc2002-08-27 09:44:07 +0000117 continue; /* selection timeout => assuming no device present */
118 }
119 scsi_print_error(pccb);
120 continue;
121 }
122 perq=tempbuff[0];
123 modi=tempbuff[1];
124 if((perq & 0x1f)==0x1f) {
125 continue; /* skip unknown devices */
126 }
127 if((modi&0x80)==0x80) /* drive is removable */
128 scsi_dev_desc[scsi_max_devs].removable=TRUE;
129 /* get info for this device */
Stefan Roese2309c132007-11-17 07:58:25 +0100130 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
131 &tempbuff[8], 8);
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
133 &tempbuff[16], 16);
134 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
135 &tempbuff[32], 4);
wdenke887afc2002-08-27 09:44:07 +0000136 scsi_dev_desc[scsi_max_devs].target=pccb->target;
137 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
138
139 pccb->datalen=0;
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;
144 goto removable;
145 }
146 scsi_print_error(pccb);
147 continue;
148 }
149 pccb->datalen=8;
150 scsi_setup_read_capacity(pccb);
151 if(scsi_exec(pccb)!=TRUE) {
152 scsi_print_error(pccb);
153 continue;
154 }
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]);
163removable:
164 if(mode==1) {
165 printf (" Device %d: ", scsi_max_devs);
166 dev_print(&scsi_dev_desc[scsi_max_devs]);
167 } /* if mode */
168 scsi_max_devs++;
169 } /* next LUN */
170 }
171 if(scsi_max_devs>0)
172 scsi_curr_dev=0;
173 else
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200174 scsi_curr_dev = -1;
wdenke887afc2002-08-27 09:44:07 +0000175}
176
Rob Herring942e3142011-07-06 16:13:36 +0000177#ifdef CONFIG_PCI
wdenke887afc2002-08-27 09:44:07 +0000178void scsi_init(void)
179{
180 int busdevfunc;
181
182 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
183 if(busdevfunc==-1) {
184 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
185 return;
186 }
187#ifdef DEBUG
188 else {
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);
190 }
191#endif
192 scsi_low_level_init(busdevfunc);
193 scsi_scan(1);
194}
Rob Herring942e3142011-07-06 16:13:36 +0000195#endif
wdenke887afc2002-08-27 09:44:07 +0000196
197block_dev_desc_t * scsi_get_dev(int dev)
198{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200199 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
wdenke887afc2002-08-27 09:44:07 +0000200}
201
202
wdenke887afc2002-08-27 09:44:07 +0000203/******************************************************************************
204 * scsi boot command intepreter. Derived from diskboot
205 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200206int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000207{
208 char *boot_device = NULL;
209 char *ep;
210 int dev, part = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100211 ulong addr, cnt;
wdenke887afc2002-08-27 09:44:07 +0000212 disk_partition_t info;
213 image_header_t *hdr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100214#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200215 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100216#endif
wdenke887afc2002-08-27 09:44:07 +0000217
218 switch (argc) {
219 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 addr = CONFIG_SYS_LOAD_ADDR;
wdenke887afc2002-08-27 09:44:07 +0000221 boot_device = getenv ("bootdevice");
222 break;
223 case 2:
224 addr = simple_strtoul(argv[1], NULL, 16);
225 boot_device = getenv ("bootdevice");
226 break;
227 case 3:
228 addr = simple_strtoul(argv[1], NULL, 16);
229 boot_device = argv[2];
230 break;
231 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200232 return cmd_usage(cmdtp);
wdenke887afc2002-08-27 09:44:07 +0000233 }
234
235 if (!boot_device) {
236 puts ("\n** No boot device **\n");
237 return 1;
238 }
239
240 dev = simple_strtoul(boot_device, &ep, 16);
241 printf("booting from dev %d\n",dev);
242 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
243 printf ("\n** Device %d not available\n", dev);
244 return 1;
245 }
246
247 if (*ep) {
248 if (*ep != ':') {
249 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
250 return 1;
251 }
252 part = simple_strtoul(++ep, NULL, 16);
253 }
Denis Peter78827512007-04-13 09:13:33 +0200254 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
wdenke887afc2002-08-27 09:44:07 +0000255 printf("error reading partinfo\n");
256 return 1;
257 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200258 if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
259 (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
wdenke887afc2002-08-27 09:44:07 +0000260 printf ("\n** Invalid partition type \"%.32s\""
261 " (expect \"" BOOT_PART_TYPE "\")\n",
262 info.type);
263 return 1;
264 }
265
266 printf ("\nLoading from SCSI device %d, partition %d: "
267 "Name: %.32s Type: %.32s\n",
268 dev, part, info.name, info.type);
269
wdenk1a344f22005-02-03 23:00:49 +0000270 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
wdenke887afc2002-08-27 09:44:07 +0000271 info.start, info.size, info.blksz);
272
273 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
274 printf ("** Read error on %d:%d\n", dev, part);
275 return 1;
276 }
277
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100278 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100279 case IMAGE_FORMAT_LEGACY:
280 hdr = (image_header_t *)addr;
wdenke887afc2002-08-27 09:44:07 +0000281
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100282 if (!image_check_hcrc (hdr)) {
283 puts ("\n** Bad Header Checksum **\n");
284 return 1;
285 }
286
287 image_print_contents (hdr);
288 cnt = image_get_image_size (hdr);
289 break;
290#if defined(CONFIG_FIT)
291 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100292 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100293 puts ("Fit image detected...\n");
294
295 cnt = fit_get_size (fit_hdr);
296 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100297#endif
298 default:
299 puts ("** Unknown image type\n");
wdenke887afc2002-08-27 09:44:07 +0000300 return 1;
301 }
302
wdenk1a344f22005-02-03 23:00:49 +0000303 cnt += info.blksz - 1;
304 cnt /= info.blksz;
305 cnt -= 1;
306
wdenke887afc2002-08-27 09:44:07 +0000307 if (scsi_read (dev, info.start+1, cnt,
308 (ulong *)(addr+info.blksz)) != cnt) {
309 printf ("** Read error on %d:%d\n", dev, part);
310 return 1;
311 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100312
313#if defined(CONFIG_FIT)
314 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200315 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
316 if (!fit_check_format (fit_hdr)) {
317 puts ("** Bad FIT image format\n");
318 return 1;
319 }
320 fit_print_contents (fit_hdr);
321 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100322#endif
323
wdenke887afc2002-08-27 09:44:07 +0000324 /* Loading ok, update default load address */
325 load_addr = addr;
326
327 flush_cache (addr, (cnt+1)*info.blksz);
328
Mike Frysinger67d668b2011-06-05 13:43:02 +0000329 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenke887afc2002-08-27 09:44:07 +0000330}
331
332/*********************************************************************************
333 * scsi command intepreter
334 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200335int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000336{
337 switch (argc) {
338 case 0:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200339 case 1: return cmd_usage(cmdtp);
340
wdenke887afc2002-08-27 09:44:07 +0000341 case 2:
342 if (strncmp(argv[1],"res",3) == 0) {
343 printf("\nReset SCSI\n");
344 scsi_bus_reset();
345 scsi_scan(1);
346 return 0;
347 }
348 if (strncmp(argv[1],"inf",3) == 0) {
349 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200350 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
wdenke887afc2002-08-27 09:44:07 +0000351 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
352 continue; /* list only known devices */
353 printf ("SCSI dev. %d: ", i);
354 dev_print(&scsi_dev_desc[i]);
355 }
356 return 0;
357 }
358 if (strncmp(argv[1],"dev",3) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200359 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
wdenke887afc2002-08-27 09:44:07 +0000360 printf("\nno SCSI devices available\n");
361 return 1;
362 }
363 printf ("\n Device %d: ", scsi_curr_dev);
364 dev_print(&scsi_dev_desc[scsi_curr_dev]);
365 return 0;
366 }
367 if (strncmp(argv[1],"scan",4) == 0) {
368 scsi_scan(1);
369 return 0;
370 }
371 if (strncmp(argv[1],"part",4) == 0) {
372 int dev, ok;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200373 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
wdenke887afc2002-08-27 09:44:07 +0000374 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
375 ok++;
376 if (dev)
377 printf("\n");
wdenk1a344f22005-02-03 23:00:49 +0000378 debug ("print_part of %x\n",dev);
wdenke887afc2002-08-27 09:44:07 +0000379 print_part(&scsi_dev_desc[dev]);
380 }
381 }
382 if (!ok)
383 printf("\nno SCSI devices available\n");
384 return 1;
385 }
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200386 return cmd_usage(cmdtp);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200387 case 3:
wdenke887afc2002-08-27 09:44:07 +0000388 if (strncmp(argv[1],"dev",3) == 0) {
389 int dev = (int)simple_strtoul(argv[2], NULL, 10);
390 printf ("\nSCSI device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
wdenke887afc2002-08-27 09:44:07 +0000392 printf("unknown device\n");
393 return 1;
394 }
395 printf ("\n Device %d: ", dev);
396 dev_print(&scsi_dev_desc[dev]);
397 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
398 return 1;
399 }
400 scsi_curr_dev = dev;
401 printf("... is now current device\n");
402 return 0;
403 }
404 if (strncmp(argv[1],"part",4) == 0) {
405 int dev = (int)simple_strtoul(argv[2], NULL, 10);
406 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
407 print_part(&scsi_dev_desc[dev]);
408 }
409 else {
410 printf ("\nSCSI device %d not available\n", dev);
411 }
412 return 1;
413 }
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200414 return cmd_usage(cmdtp);
wdenke887afc2002-08-27 09:44:07 +0000415 default:
416 /* at least 4 args */
417 if (strcmp(argv[1],"read") == 0) {
418 ulong addr = simple_strtoul(argv[2], NULL, 16);
419 ulong blk = simple_strtoul(argv[3], NULL, 16);
420 ulong cnt = simple_strtoul(argv[4], NULL, 16);
421 ulong n;
422 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
423 scsi_curr_dev, blk, cnt);
424 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
425 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
426 return 0;
427 }
428 } /* switch */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200429 return cmd_usage(cmdtp);
wdenke887afc2002-08-27 09:44:07 +0000430}
431
432/****************************************************************************************
433 * scsi_read
434 */
435
436#define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
437
Grant Likelyeb867a72007-02-20 09:05:45 +0100438ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
wdenke887afc2002-08-27 09:44:07 +0000439{
440 ulong start,blks, buf_addr;
441 unsigned short smallblks;
442 ccb* pccb=(ccb *)&tempccb;
443 device&=0xff;
444 /* Setup device
445 */
446 pccb->target=scsi_dev_desc[device].target;
447 pccb->lun=scsi_dev_desc[device].lun;
448 buf_addr=(unsigned long)buffer;
449 start=blknr;
450 blks=blkcnt;
wdenk1a344f22005-02-03 23:00:49 +0000451 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
wdenke887afc2002-08-27 09:44:07 +0000452 do {
453 pccb->pdata=(unsigned char *)buf_addr;
454 if(blks>SCSI_MAX_READ_BLK) {
455 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
456 smallblks=SCSI_MAX_READ_BLK;
457 scsi_setup_read_ext(pccb,start,smallblks);
458 start+=SCSI_MAX_READ_BLK;
459 blks-=SCSI_MAX_READ_BLK;
460 }
461 else {
462 pccb->datalen=scsi_dev_desc[device].blksz * blks;
463 smallblks=(unsigned short) blks;
464 scsi_setup_read_ext(pccb,start,smallblks);
465 start+=blks;
466 blks=0;
467 }
wdenk1a344f22005-02-03 23:00:49 +0000468 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
wdenke887afc2002-08-27 09:44:07 +0000469 if(scsi_exec(pccb)!=TRUE) {
470 scsi_print_error(pccb);
471 blkcnt-=blks;
472 break;
473 }
474 buf_addr+=pccb->datalen;
475 } while(blks!=0);
wdenk1a344f22005-02-03 23:00:49 +0000476 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
wdenke887afc2002-08-27 09:44:07 +0000477 return(blkcnt);
478}
479
480/* copy src to dest, skipping leading and trailing blanks
481 * and null terminate the string
482 */
483void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
484{
485 int start,end;
486
487 start=0;
488 while(start<len) {
489 if(src[start]!=' ')
490 break;
491 start++;
492 }
493 end=len-1;
494 while(end>start) {
495 if(src[end]!=' ')
496 break;
497 end--;
498 }
499 for( ; start<=end; start++) {
500 *dest++=src[start];
501 }
502 *dest='\0';
503}
504
505
wdenke887afc2002-08-27 09:44:07 +0000506/* Trim trailing blanks, and NUL-terminate string
507 */
508void scsi_trim_trail (unsigned char *str, unsigned int len)
509{
510 unsigned char *p = str + len - 1;
511
512 while (len-- > 0) {
513 *p-- = '\0';
514 if (*p != ' ') {
515 return;
516 }
517 }
518}
519
520
521/************************************************************************************
522 * Some setup (fill-in) routines
523 */
524void scsi_setup_test_unit_ready(ccb * pccb)
525{
526 pccb->cmd[0]=SCSI_TST_U_RDY;
527 pccb->cmd[1]=pccb->lun<<5;
528 pccb->cmd[2]=0;
529 pccb->cmd[3]=0;
530 pccb->cmd[4]=0;
531 pccb->cmd[5]=0;
532 pccb->cmdlen=6;
533 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
534}
535
536void scsi_setup_read_capacity(ccb * pccb)
537{
538 pccb->cmd[0]=SCSI_RD_CAPAC;
539 pccb->cmd[1]=pccb->lun<<5;
540 pccb->cmd[2]=0;
541 pccb->cmd[3]=0;
542 pccb->cmd[4]=0;
543 pccb->cmd[5]=0;
544 pccb->cmd[6]=0;
545 pccb->cmd[7]=0;
546 pccb->cmd[8]=0;
547 pccb->cmd[9]=0;
548 pccb->cmdlen=10;
549 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
550
551}
552
553void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
554{
555 pccb->cmd[0]=SCSI_READ10;
556 pccb->cmd[1]=pccb->lun<<5;
557 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
558 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
559 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
560 pccb->cmd[5]=((unsigned char) (start))&0xff;
561 pccb->cmd[6]=0;
562 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
563 pccb->cmd[8]=(unsigned char) blocks & 0xff;
564 pccb->cmd[6]=0;
565 pccb->cmdlen=10;
566 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenk1a344f22005-02-03 23:00:49 +0000567 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
wdenke887afc2002-08-27 09:44:07 +0000568 pccb->cmd[0],pccb->cmd[1],
569 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
570 pccb->cmd[7],pccb->cmd[8]);
571}
572
573void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
574{
575 pccb->cmd[0]=SCSI_READ6;
576 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
577 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
578 pccb->cmd[3]=((unsigned char) (start))&0xff;
579 pccb->cmd[4]=(unsigned char) blocks & 0xff;
580 pccb->cmd[5]=0;
581 pccb->cmdlen=6;
582 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenk1a344f22005-02-03 23:00:49 +0000583 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
wdenke887afc2002-08-27 09:44:07 +0000584 pccb->cmd[0],pccb->cmd[1],
585 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
586}
587
588
589void scsi_setup_inquiry(ccb * pccb)
590{
591 pccb->cmd[0]=SCSI_INQUIRY;
592 pccb->cmd[1]=pccb->lun<<5;
593 pccb->cmd[2]=0;
594 pccb->cmd[3]=0;
595 if(pccb->datalen>255)
596 pccb->cmd[4]=255;
597 else
598 pccb->cmd[4]=(unsigned char)pccb->datalen;
599 pccb->cmd[5]=0;
600 pccb->cmdlen=6;
601 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
602}
603
Wolfgang Denk460c3222005-08-04 01:14:12 +0200604
605U_BOOT_CMD(
606 scsi, 5, 1, do_scsi,
Peter Tyser2fb26042009-01-27 18:03:12 -0600607 "SCSI sub-system",
Wolfgang Denk460c3222005-08-04 01:14:12 +0200608 "reset - reset SCSI controller\n"
609 "scsi info - show available SCSI devices\n"
610 "scsi scan - (re-)scan SCSI bus\n"
611 "scsi device [dev] - show or set current device\n"
612 "scsi part [dev] - print partition table of one or all SCSI devices\n"
613 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200614 " to memory address `addr'"
Wolfgang Denk460c3222005-08-04 01:14:12 +0200615);
616
617U_BOOT_CMD(
618 scsiboot, 3, 1, do_scsiboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600619 "boot from SCSI device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200620 "loadAddr dev:part"
Wolfgang Denk460c3222005-08-04 01:14:12 +0200621);