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 | 4782ac8 | 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 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 49 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
Jin Zhengxiong | 4782ac8 | 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 | 6d0f6bc | 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 | eb867a7 | 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 | 6d0f6bc | 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 | 6d0f6bc | 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 | 6d0f6bc | 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 | 1a344f2 | 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 | 2309c13 | 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 | d0ff51b | 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 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 177 | #ifdef CONFIG_PCI |
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 | } |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 195 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 196 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 197 | #ifdef CONFIG_PARTITIONS |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 198 | block_dev_desc_t * scsi_get_dev(int dev) |
| 199 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 200 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 201 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 202 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 203 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 204 | /****************************************************************************** |
| 205 | * scsi boot command intepreter. Derived from diskboot |
| 206 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 207 | 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] | 208 | { |
| 209 | char *boot_device = NULL; |
| 210 | char *ep; |
| 211 | int dev, part = 0; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 212 | ulong addr, cnt; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 213 | disk_partition_t info; |
| 214 | image_header_t *hdr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 215 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 216 | const void *fit_hdr = NULL; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 217 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 218 | |
| 219 | switch (argc) { |
| 220 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 221 | addr = CONFIG_SYS_LOAD_ADDR; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 222 | boot_device = getenv ("bootdevice"); |
| 223 | break; |
| 224 | case 2: |
| 225 | addr = simple_strtoul(argv[1], NULL, 16); |
| 226 | boot_device = getenv ("bootdevice"); |
| 227 | break; |
| 228 | case 3: |
| 229 | addr = simple_strtoul(argv[1], NULL, 16); |
| 230 | boot_device = argv[2]; |
| 231 | break; |
| 232 | default: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 233 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | if (!boot_device) { |
| 237 | puts ("\n** No boot device **\n"); |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | dev = simple_strtoul(boot_device, &ep, 16); |
| 242 | printf("booting from dev %d\n",dev); |
| 243 | if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 244 | printf ("\n** Device %d not available\n", dev); |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | if (*ep) { |
| 249 | if (*ep != ':') { |
| 250 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 251 | return 1; |
| 252 | } |
| 253 | part = simple_strtoul(++ep, NULL, 16); |
| 254 | } |
Denis Peter | 7882751 | 2007-04-13 09:13:33 +0200 | [diff] [blame] | 255 | if (get_partition_info (&scsi_dev_desc[dev], part, &info)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 256 | printf("error reading partinfo\n"); |
| 257 | return 1; |
| 258 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 259 | if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) && |
| 260 | (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 261 | printf ("\n** Invalid partition type \"%.32s\"" |
| 262 | " (expect \"" BOOT_PART_TYPE "\")\n", |
| 263 | info.type); |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | printf ("\nLoading from SCSI device %d, partition %d: " |
| 268 | "Name: %.32s Type: %.32s\n", |
| 269 | dev, part, info.name, info.type); |
| 270 | |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 271 | debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 272 | info.start, info.size, info.blksz); |
| 273 | |
| 274 | if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) { |
| 275 | printf ("** Read error on %d:%d\n", dev, part); |
| 276 | return 1; |
| 277 | } |
| 278 | |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 279 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 280 | case IMAGE_FORMAT_LEGACY: |
| 281 | hdr = (image_header_t *)addr; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 282 | |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 283 | if (!image_check_hcrc (hdr)) { |
| 284 | puts ("\n** Bad Header Checksum **\n"); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | image_print_contents (hdr); |
| 289 | cnt = image_get_image_size (hdr); |
| 290 | break; |
| 291 | #if defined(CONFIG_FIT) |
| 292 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 293 | fit_hdr = (const void *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 294 | puts ("Fit image detected...\n"); |
| 295 | |
| 296 | cnt = fit_get_size (fit_hdr); |
| 297 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 298 | #endif |
| 299 | default: |
| 300 | puts ("** Unknown image type\n"); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 304 | cnt += info.blksz - 1; |
| 305 | cnt /= info.blksz; |
| 306 | cnt -= 1; |
| 307 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 308 | if (scsi_read (dev, info.start+1, cnt, |
| 309 | (ulong *)(addr+info.blksz)) != cnt) { |
| 310 | printf ("** Read error on %d:%d\n", dev, part); |
| 311 | return 1; |
| 312 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 313 | |
| 314 | #if defined(CONFIG_FIT) |
| 315 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 316 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 317 | if (!fit_check_format (fit_hdr)) { |
| 318 | puts ("** Bad FIT image format\n"); |
| 319 | return 1; |
| 320 | } |
| 321 | fit_print_contents (fit_hdr); |
| 322 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 323 | #endif |
| 324 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 325 | /* Loading ok, update default load address */ |
| 326 | load_addr = addr; |
| 327 | |
| 328 | flush_cache (addr, (cnt+1)*info.blksz); |
| 329 | |
Mike Frysinger | 67d668b | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 330 | return bootm_maybe_autostart(cmdtp, argv[0]); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /********************************************************************************* |
| 334 | * scsi command intepreter |
| 335 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 336 | 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] | 337 | { |
| 338 | switch (argc) { |
| 339 | case 0: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 340 | case 1: return cmd_usage(cmdtp); |
| 341 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 342 | case 2: |
| 343 | if (strncmp(argv[1],"res",3) == 0) { |
| 344 | printf("\nReset SCSI\n"); |
| 345 | scsi_bus_reset(); |
| 346 | scsi_scan(1); |
| 347 | return 0; |
| 348 | } |
| 349 | if (strncmp(argv[1],"inf",3) == 0) { |
| 350 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 351 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 352 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
| 353 | continue; /* list only known devices */ |
| 354 | printf ("SCSI dev. %d: ", i); |
| 355 | dev_print(&scsi_dev_desc[i]); |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | if (strncmp(argv[1],"dev",3) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 360 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 361 | printf("\nno SCSI devices available\n"); |
| 362 | return 1; |
| 363 | } |
| 364 | printf ("\n Device %d: ", scsi_curr_dev); |
| 365 | dev_print(&scsi_dev_desc[scsi_curr_dev]); |
| 366 | return 0; |
| 367 | } |
| 368 | if (strncmp(argv[1],"scan",4) == 0) { |
| 369 | scsi_scan(1); |
| 370 | return 0; |
| 371 | } |
| 372 | if (strncmp(argv[1],"part",4) == 0) { |
| 373 | int dev, ok; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 374 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 375 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
| 376 | ok++; |
| 377 | if (dev) |
| 378 | printf("\n"); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 379 | debug ("print_part of %x\n",dev); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 380 | print_part(&scsi_dev_desc[dev]); |
| 381 | } |
| 382 | } |
| 383 | if (!ok) |
| 384 | printf("\nno SCSI devices available\n"); |
| 385 | return 1; |
| 386 | } |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 387 | return cmd_usage(cmdtp); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 388 | case 3: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 389 | if (strncmp(argv[1],"dev",3) == 0) { |
| 390 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 391 | printf ("\nSCSI device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 392 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 393 | printf("unknown device\n"); |
| 394 | return 1; |
| 395 | } |
| 396 | printf ("\n Device %d: ", dev); |
| 397 | dev_print(&scsi_dev_desc[dev]); |
| 398 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 399 | return 1; |
| 400 | } |
| 401 | scsi_curr_dev = dev; |
| 402 | printf("... is now current device\n"); |
| 403 | return 0; |
| 404 | } |
| 405 | if (strncmp(argv[1],"part",4) == 0) { |
| 406 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 407 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { |
| 408 | print_part(&scsi_dev_desc[dev]); |
| 409 | } |
| 410 | else { |
| 411 | printf ("\nSCSI device %d not available\n", dev); |
| 412 | } |
| 413 | return 1; |
| 414 | } |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 415 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 416 | default: |
| 417 | /* at least 4 args */ |
| 418 | if (strcmp(argv[1],"read") == 0) { |
| 419 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 420 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 421 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 422 | ulong n; |
| 423 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 424 | scsi_curr_dev, blk, cnt); |
| 425 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); |
| 426 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); |
| 427 | return 0; |
| 428 | } |
| 429 | } /* switch */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 430 | return cmd_usage(cmdtp); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /**************************************************************************************** |
| 434 | * scsi_read |
| 435 | */ |
| 436 | |
| 437 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ |
| 438 | |
Grant Likely | eb867a7 | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 439 | ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 440 | { |
| 441 | ulong start,blks, buf_addr; |
| 442 | unsigned short smallblks; |
| 443 | ccb* pccb=(ccb *)&tempccb; |
| 444 | device&=0xff; |
| 445 | /* Setup device |
| 446 | */ |
| 447 | pccb->target=scsi_dev_desc[device].target; |
| 448 | pccb->lun=scsi_dev_desc[device].lun; |
| 449 | buf_addr=(unsigned long)buffer; |
| 450 | start=blknr; |
| 451 | blks=blkcnt; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 452 | 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] | 453 | do { |
| 454 | pccb->pdata=(unsigned char *)buf_addr; |
| 455 | if(blks>SCSI_MAX_READ_BLK) { |
| 456 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; |
| 457 | smallblks=SCSI_MAX_READ_BLK; |
| 458 | scsi_setup_read_ext(pccb,start,smallblks); |
| 459 | start+=SCSI_MAX_READ_BLK; |
| 460 | blks-=SCSI_MAX_READ_BLK; |
| 461 | } |
| 462 | else { |
| 463 | pccb->datalen=scsi_dev_desc[device].blksz * blks; |
| 464 | smallblks=(unsigned short) blks; |
| 465 | scsi_setup_read_ext(pccb,start,smallblks); |
| 466 | start+=blks; |
| 467 | blks=0; |
| 468 | } |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 469 | 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] | 470 | if(scsi_exec(pccb)!=TRUE) { |
| 471 | scsi_print_error(pccb); |
| 472 | blkcnt-=blks; |
| 473 | break; |
| 474 | } |
| 475 | buf_addr+=pccb->datalen; |
| 476 | } while(blks!=0); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 477 | 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] | 478 | return(blkcnt); |
| 479 | } |
| 480 | |
| 481 | /* copy src to dest, skipping leading and trailing blanks |
| 482 | * and null terminate the string |
| 483 | */ |
| 484 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) |
| 485 | { |
| 486 | int start,end; |
| 487 | |
| 488 | start=0; |
| 489 | while(start<len) { |
| 490 | if(src[start]!=' ') |
| 491 | break; |
| 492 | start++; |
| 493 | } |
| 494 | end=len-1; |
| 495 | while(end>start) { |
| 496 | if(src[end]!=' ') |
| 497 | break; |
| 498 | end--; |
| 499 | } |
| 500 | for( ; start<=end; start++) { |
| 501 | *dest++=src[start]; |
| 502 | } |
| 503 | *dest='\0'; |
| 504 | } |
| 505 | |
| 506 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 507 | /* Trim trailing blanks, and NUL-terminate string |
| 508 | */ |
| 509 | void scsi_trim_trail (unsigned char *str, unsigned int len) |
| 510 | { |
| 511 | unsigned char *p = str + len - 1; |
| 512 | |
| 513 | while (len-- > 0) { |
| 514 | *p-- = '\0'; |
| 515 | if (*p != ' ') { |
| 516 | return; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /************************************************************************************ |
| 523 | * Some setup (fill-in) routines |
| 524 | */ |
| 525 | void scsi_setup_test_unit_ready(ccb * pccb) |
| 526 | { |
| 527 | pccb->cmd[0]=SCSI_TST_U_RDY; |
| 528 | pccb->cmd[1]=pccb->lun<<5; |
| 529 | pccb->cmd[2]=0; |
| 530 | pccb->cmd[3]=0; |
| 531 | pccb->cmd[4]=0; |
| 532 | pccb->cmd[5]=0; |
| 533 | pccb->cmdlen=6; |
| 534 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 535 | } |
| 536 | |
| 537 | void scsi_setup_read_capacity(ccb * pccb) |
| 538 | { |
| 539 | pccb->cmd[0]=SCSI_RD_CAPAC; |
| 540 | pccb->cmd[1]=pccb->lun<<5; |
| 541 | pccb->cmd[2]=0; |
| 542 | pccb->cmd[3]=0; |
| 543 | pccb->cmd[4]=0; |
| 544 | pccb->cmd[5]=0; |
| 545 | pccb->cmd[6]=0; |
| 546 | pccb->cmd[7]=0; |
| 547 | pccb->cmd[8]=0; |
| 548 | pccb->cmd[9]=0; |
| 549 | pccb->cmdlen=10; |
| 550 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 551 | |
| 552 | } |
| 553 | |
| 554 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
| 555 | { |
| 556 | pccb->cmd[0]=SCSI_READ10; |
| 557 | pccb->cmd[1]=pccb->lun<<5; |
| 558 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; |
| 559 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; |
| 560 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; |
| 561 | pccb->cmd[5]=((unsigned char) (start))&0xff; |
| 562 | pccb->cmd[6]=0; |
| 563 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; |
| 564 | pccb->cmd[8]=(unsigned char) blocks & 0xff; |
| 565 | pccb->cmd[6]=0; |
| 566 | pccb->cmdlen=10; |
| 567 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 568 | 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] | 569 | pccb->cmd[0],pccb->cmd[1], |
| 570 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], |
| 571 | pccb->cmd[7],pccb->cmd[8]); |
| 572 | } |
| 573 | |
| 574 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
| 575 | { |
| 576 | pccb->cmd[0]=SCSI_READ6; |
| 577 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); |
| 578 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; |
| 579 | pccb->cmd[3]=((unsigned char) (start))&0xff; |
| 580 | pccb->cmd[4]=(unsigned char) blocks & 0xff; |
| 581 | pccb->cmd[5]=0; |
| 582 | pccb->cmdlen=6; |
| 583 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 584 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 585 | pccb->cmd[0],pccb->cmd[1], |
| 586 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | void scsi_setup_inquiry(ccb * pccb) |
| 591 | { |
| 592 | pccb->cmd[0]=SCSI_INQUIRY; |
| 593 | pccb->cmd[1]=pccb->lun<<5; |
| 594 | pccb->cmd[2]=0; |
| 595 | pccb->cmd[3]=0; |
| 596 | if(pccb->datalen>255) |
| 597 | pccb->cmd[4]=255; |
| 598 | else |
| 599 | pccb->cmd[4]=(unsigned char)pccb->datalen; |
| 600 | pccb->cmd[5]=0; |
| 601 | pccb->cmdlen=6; |
| 602 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 603 | } |
| 604 | |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 605 | |
| 606 | U_BOOT_CMD( |
| 607 | scsi, 5, 1, do_scsi, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 608 | "SCSI sub-system", |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 609 | "reset - reset SCSI controller\n" |
| 610 | "scsi info - show available SCSI devices\n" |
| 611 | "scsi scan - (re-)scan SCSI bus\n" |
| 612 | "scsi device [dev] - show or set current device\n" |
| 613 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 614 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 615 | " to memory address `addr'" |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 616 | ); |
| 617 | |
| 618 | U_BOOT_CMD( |
| 619 | scsiboot, 3, 1, do_scsiboot, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 620 | "boot from SCSI device", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 621 | "loadAddr dev:part" |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 622 | ); |