wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 30 | #include <common.h> |
| 31 | #include <command.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 32 | #include <asm/processor.h> |
| 33 | #include <scsi.h> |
| 34 | #include <image.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 35 | #include <pci.h> |
| 36 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 37 | #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 Zhengxiong | ae180dc | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 44 | #elif defined CONFIG_SATA_ULI5288 |
| 45 | |
| 46 | #define SCSI_VEND_ID 0x10b9 |
| 47 | #define SCSI_DEV_ID 0x5288 |
| 48 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 49 | #else |
Jin Zhengxiong | ae180dc | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 50 | #error no scsi device defined |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | |
| 54 | static ccb tempccb; /* temporary scsi command buffer */ |
| 55 | |
| 56 | static unsigned char tempbuff[512]; /* temporary data buffer */ |
| 57 | |
| 58 | static int scsi_max_devs; /* number of highest available scsi device */ |
| 59 | |
| 60 | static int scsi_curr_dev; /* current device */ |
| 61 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 63 | |
| 64 | /******************************************************************************** |
| 65 | * forward declerations of some Setup Routines |
| 66 | */ |
| 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); |
| 73 | |
| 74 | |
Grant Likely | 2089cab | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 75 | ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /********************************************************************************* |
| 79 | * (re)-scan the scsi bus and reports scsi device info |
| 80 | * to the user if mode = 1 |
| 81 | */ |
| 82 | void 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-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 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; |
| 105 | } |
| 106 | scsi_max_devs=0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 108 | pccb->target=i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 109 | for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 110 | 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) { |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 116 | debug ("Selection timeout ID %d\n",pccb->target); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 117 | 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 Roese | 4404c16 | 2007-11-17 07:58:25 +0100 | [diff] [blame] | 130 | 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); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 136 | 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]); |
| 163 | removable: |
| 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 Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 174 | scsi_curr_dev = -1; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 178 | void 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 | } |
| 195 | |
| 196 | block_dev_desc_t * scsi_get_dev(int dev) |
| 197 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 198 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 202 | /****************************************************************************** |
| 203 | * scsi boot command intepreter. Derived from diskboot |
| 204 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 205 | int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 206 | { |
| 207 | char *boot_device = NULL; |
| 208 | char *ep; |
| 209 | int dev, part = 0; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 210 | ulong addr, cnt; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 211 | disk_partition_t info; |
| 212 | image_header_t *hdr; |
| 213 | int rcode = 0; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 214 | #if defined(CONFIG_FIT) |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 215 | const void *fit_hdr = NULL; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 216 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 217 | |
| 218 | switch (argc) { |
| 219 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 220 | addr = CONFIG_SYS_LOAD_ADDR; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 221 | 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 Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 232 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 233 | } |
| 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 Peter | 3957bc1 | 2007-04-13 09:13:33 +0200 | [diff] [blame] | 254 | if (get_partition_info (&scsi_dev_desc[dev], part, &info)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 255 | printf("error reading partinfo\n"); |
| 256 | return 1; |
| 257 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 258 | if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) && |
| 259 | (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 260 | 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 | |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 270 | debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 271 | 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 Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 278 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 279 | case IMAGE_FORMAT_LEGACY: |
| 280 | hdr = (image_header_t *)addr; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 281 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 282 | 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 Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 292 | fit_hdr = (const void *)addr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 293 | puts ("Fit image detected...\n"); |
| 294 | |
| 295 | cnt = fit_get_size (fit_hdr); |
| 296 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 297 | #endif |
| 298 | default: |
| 299 | puts ("** Unknown image type\n"); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 303 | cnt += info.blksz - 1; |
| 304 | cnt /= info.blksz; |
| 305 | cnt -= 1; |
| 306 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 307 | 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 Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 312 | |
| 313 | #if defined(CONFIG_FIT) |
| 314 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 315 | 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 Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 322 | #endif |
| 323 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 324 | /* Loading ok, update default load address */ |
| 325 | load_addr = addr; |
| 326 | |
| 327 | flush_cache (addr, (cnt+1)*info.blksz); |
| 328 | |
| 329 | /* Check if we should attempt an auto-start */ |
Mike Frysinger | 27c0b4a | 2010-10-20 07:17:55 -0400 | [diff] [blame] | 330 | if (getenv_yesno("autostart")) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 331 | char *local_args[2]; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 332 | local_args[0] = argv[0]; |
| 333 | local_args[1] = NULL; |
| 334 | printf ("Automatic boot of image at addr 0x%08lX ...\n", addr); |
| 335 | rcode = do_bootm (cmdtp, 0, 1, local_args); |
| 336 | } |
| 337 | return rcode; |
| 338 | } |
| 339 | |
| 340 | /********************************************************************************* |
| 341 | * scsi command intepreter |
| 342 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 343 | int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 344 | { |
| 345 | switch (argc) { |
| 346 | case 0: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 347 | case 1: return cmd_usage(cmdtp); |
| 348 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 349 | case 2: |
| 350 | if (strncmp(argv[1],"res",3) == 0) { |
| 351 | printf("\nReset SCSI\n"); |
| 352 | scsi_bus_reset(); |
| 353 | scsi_scan(1); |
| 354 | return 0; |
| 355 | } |
| 356 | if (strncmp(argv[1],"inf",3) == 0) { |
| 357 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 358 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 359 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
| 360 | continue; /* list only known devices */ |
| 361 | printf ("SCSI dev. %d: ", i); |
| 362 | dev_print(&scsi_dev_desc[i]); |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | if (strncmp(argv[1],"dev",3) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 367 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 368 | printf("\nno SCSI devices available\n"); |
| 369 | return 1; |
| 370 | } |
| 371 | printf ("\n Device %d: ", scsi_curr_dev); |
| 372 | dev_print(&scsi_dev_desc[scsi_curr_dev]); |
| 373 | return 0; |
| 374 | } |
| 375 | if (strncmp(argv[1],"scan",4) == 0) { |
| 376 | scsi_scan(1); |
| 377 | return 0; |
| 378 | } |
| 379 | if (strncmp(argv[1],"part",4) == 0) { |
| 380 | int dev, ok; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 381 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 382 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
| 383 | ok++; |
| 384 | if (dev) |
| 385 | printf("\n"); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 386 | debug ("print_part of %x\n",dev); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 387 | print_part(&scsi_dev_desc[dev]); |
| 388 | } |
| 389 | } |
| 390 | if (!ok) |
| 391 | printf("\nno SCSI devices available\n"); |
| 392 | return 1; |
| 393 | } |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 394 | return cmd_usage(cmdtp); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 395 | case 3: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 396 | if (strncmp(argv[1],"dev",3) == 0) { |
| 397 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 398 | printf ("\nSCSI device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 399 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 400 | printf("unknown device\n"); |
| 401 | return 1; |
| 402 | } |
| 403 | printf ("\n Device %d: ", dev); |
| 404 | dev_print(&scsi_dev_desc[dev]); |
| 405 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 406 | return 1; |
| 407 | } |
| 408 | scsi_curr_dev = dev; |
| 409 | printf("... is now current device\n"); |
| 410 | return 0; |
| 411 | } |
| 412 | if (strncmp(argv[1],"part",4) == 0) { |
| 413 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 414 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { |
| 415 | print_part(&scsi_dev_desc[dev]); |
| 416 | } |
| 417 | else { |
| 418 | printf ("\nSCSI device %d not available\n", dev); |
| 419 | } |
| 420 | return 1; |
| 421 | } |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 422 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 423 | default: |
| 424 | /* at least 4 args */ |
| 425 | if (strcmp(argv[1],"read") == 0) { |
| 426 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 427 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 428 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 429 | ulong n; |
| 430 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 431 | scsi_curr_dev, blk, cnt); |
| 432 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); |
| 433 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); |
| 434 | return 0; |
| 435 | } |
| 436 | } /* switch */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 437 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /**************************************************************************************** |
| 441 | * scsi_read |
| 442 | */ |
| 443 | |
| 444 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ |
| 445 | |
Grant Likely | 2089cab | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 446 | ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 447 | { |
| 448 | ulong start,blks, buf_addr; |
| 449 | unsigned short smallblks; |
| 450 | ccb* pccb=(ccb *)&tempccb; |
| 451 | device&=0xff; |
| 452 | /* Setup device |
| 453 | */ |
| 454 | pccb->target=scsi_dev_desc[device].target; |
| 455 | pccb->lun=scsi_dev_desc[device].lun; |
| 456 | buf_addr=(unsigned long)buffer; |
| 457 | start=blknr; |
| 458 | blks=blkcnt; |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 459 | debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 460 | do { |
| 461 | pccb->pdata=(unsigned char *)buf_addr; |
| 462 | if(blks>SCSI_MAX_READ_BLK) { |
| 463 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; |
| 464 | smallblks=SCSI_MAX_READ_BLK; |
| 465 | scsi_setup_read_ext(pccb,start,smallblks); |
| 466 | start+=SCSI_MAX_READ_BLK; |
| 467 | blks-=SCSI_MAX_READ_BLK; |
| 468 | } |
| 469 | else { |
| 470 | pccb->datalen=scsi_dev_desc[device].blksz * blks; |
| 471 | smallblks=(unsigned short) blks; |
| 472 | scsi_setup_read_ext(pccb,start,smallblks); |
| 473 | start+=blks; |
| 474 | blks=0; |
| 475 | } |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 476 | debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 477 | if(scsi_exec(pccb)!=TRUE) { |
| 478 | scsi_print_error(pccb); |
| 479 | blkcnt-=blks; |
| 480 | break; |
| 481 | } |
| 482 | buf_addr+=pccb->datalen; |
| 483 | } while(blks!=0); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 484 | debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 485 | return(blkcnt); |
| 486 | } |
| 487 | |
| 488 | /* copy src to dest, skipping leading and trailing blanks |
| 489 | * and null terminate the string |
| 490 | */ |
| 491 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) |
| 492 | { |
| 493 | int start,end; |
| 494 | |
| 495 | start=0; |
| 496 | while(start<len) { |
| 497 | if(src[start]!=' ') |
| 498 | break; |
| 499 | start++; |
| 500 | } |
| 501 | end=len-1; |
| 502 | while(end>start) { |
| 503 | if(src[end]!=' ') |
| 504 | break; |
| 505 | end--; |
| 506 | } |
| 507 | for( ; start<=end; start++) { |
| 508 | *dest++=src[start]; |
| 509 | } |
| 510 | *dest='\0'; |
| 511 | } |
| 512 | |
| 513 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 514 | /* Trim trailing blanks, and NUL-terminate string |
| 515 | */ |
| 516 | void scsi_trim_trail (unsigned char *str, unsigned int len) |
| 517 | { |
| 518 | unsigned char *p = str + len - 1; |
| 519 | |
| 520 | while (len-- > 0) { |
| 521 | *p-- = '\0'; |
| 522 | if (*p != ' ') { |
| 523 | return; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /************************************************************************************ |
| 530 | * Some setup (fill-in) routines |
| 531 | */ |
| 532 | void scsi_setup_test_unit_ready(ccb * pccb) |
| 533 | { |
| 534 | pccb->cmd[0]=SCSI_TST_U_RDY; |
| 535 | pccb->cmd[1]=pccb->lun<<5; |
| 536 | pccb->cmd[2]=0; |
| 537 | pccb->cmd[3]=0; |
| 538 | pccb->cmd[4]=0; |
| 539 | pccb->cmd[5]=0; |
| 540 | pccb->cmdlen=6; |
| 541 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 542 | } |
| 543 | |
| 544 | void scsi_setup_read_capacity(ccb * pccb) |
| 545 | { |
| 546 | pccb->cmd[0]=SCSI_RD_CAPAC; |
| 547 | pccb->cmd[1]=pccb->lun<<5; |
| 548 | pccb->cmd[2]=0; |
| 549 | pccb->cmd[3]=0; |
| 550 | pccb->cmd[4]=0; |
| 551 | pccb->cmd[5]=0; |
| 552 | pccb->cmd[6]=0; |
| 553 | pccb->cmd[7]=0; |
| 554 | pccb->cmd[8]=0; |
| 555 | pccb->cmd[9]=0; |
| 556 | pccb->cmdlen=10; |
| 557 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 558 | |
| 559 | } |
| 560 | |
| 561 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
| 562 | { |
| 563 | pccb->cmd[0]=SCSI_READ10; |
| 564 | pccb->cmd[1]=pccb->lun<<5; |
| 565 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; |
| 566 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; |
| 567 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; |
| 568 | pccb->cmd[5]=((unsigned char) (start))&0xff; |
| 569 | pccb->cmd[6]=0; |
| 570 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; |
| 571 | pccb->cmd[8]=(unsigned char) blocks & 0xff; |
| 572 | pccb->cmd[6]=0; |
| 573 | pccb->cmdlen=10; |
| 574 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 575 | debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 576 | pccb->cmd[0],pccb->cmd[1], |
| 577 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], |
| 578 | pccb->cmd[7],pccb->cmd[8]); |
| 579 | } |
| 580 | |
| 581 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
| 582 | { |
| 583 | pccb->cmd[0]=SCSI_READ6; |
| 584 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); |
| 585 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; |
| 586 | pccb->cmd[3]=((unsigned char) (start))&0xff; |
| 587 | pccb->cmd[4]=(unsigned char) blocks & 0xff; |
| 588 | pccb->cmd[5]=0; |
| 589 | pccb->cmdlen=6; |
| 590 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 591 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 592 | pccb->cmd[0],pccb->cmd[1], |
| 593 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); |
| 594 | } |
| 595 | |
| 596 | |
| 597 | void scsi_setup_inquiry(ccb * pccb) |
| 598 | { |
| 599 | pccb->cmd[0]=SCSI_INQUIRY; |
| 600 | pccb->cmd[1]=pccb->lun<<5; |
| 601 | pccb->cmd[2]=0; |
| 602 | pccb->cmd[3]=0; |
| 603 | if(pccb->datalen>255) |
| 604 | pccb->cmd[4]=255; |
| 605 | else |
| 606 | pccb->cmd[4]=(unsigned char)pccb->datalen; |
| 607 | pccb->cmd[5]=0; |
| 608 | pccb->cmdlen=6; |
| 609 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 610 | } |
| 611 | |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 612 | |
| 613 | U_BOOT_CMD( |
| 614 | scsi, 5, 1, do_scsi, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 615 | "SCSI sub-system", |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 616 | "reset - reset SCSI controller\n" |
| 617 | "scsi info - show available SCSI devices\n" |
| 618 | "scsi scan - (re-)scan SCSI bus\n" |
| 619 | "scsi device [dev] - show or set current device\n" |
| 620 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 621 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 622 | " to memory address `addr'" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 623 | ); |
| 624 | |
| 625 | U_BOOT_CMD( |
| 626 | scsiboot, 3, 1, do_scsiboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 627 | "boot from SCSI device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 628 | "loadAddr dev:part" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 629 | ); |