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 | |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 37 | #ifdef CONFIG_SCSI_DEV_LIST |
| 38 | #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST |
| 39 | #else |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 40 | #ifdef CONFIG_SCSI_SYM53C8XX |
| 41 | #define SCSI_VEND_ID 0x1000 |
| 42 | #ifndef CONFIG_SCSI_DEV_ID |
| 43 | #define SCSI_DEV_ID 0x0001 |
| 44 | #else |
| 45 | #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID |
| 46 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 47 | #elif defined CONFIG_SATA_ULI5288 |
| 48 | |
| 49 | #define SCSI_VEND_ID 0x10b9 |
| 50 | #define SCSI_DEV_ID 0x5288 |
| 51 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 52 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 53 | #error no scsi device defined |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 54 | #endif |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 55 | #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID} |
| 56 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 57 | |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 58 | #ifdef CONFIG_PCI |
| 59 | const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST }; |
| 60 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 61 | static ccb tempccb; /* temporary scsi command buffer */ |
| 62 | |
| 63 | static unsigned char tempbuff[512]; /* temporary data buffer */ |
| 64 | |
| 65 | static int scsi_max_devs; /* number of highest available scsi device */ |
| 66 | |
| 67 | static int scsi_curr_dev; /* current device */ |
| 68 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 69 | static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 70 | |
| 71 | /******************************************************************************** |
| 72 | * forward declerations of some Setup Routines |
| 73 | */ |
| 74 | void scsi_setup_test_unit_ready(ccb * pccb); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 75 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks); |
| 76 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks); |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 77 | static void scsi_setup_write_ext(ccb *pccb, unsigned long start, |
| 78 | unsigned short blocks); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 79 | void scsi_setup_inquiry(ccb * pccb); |
| 80 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); |
| 81 | |
| 82 | |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 83 | static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, |
| 84 | unsigned long *blksz); |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 85 | static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer); |
| 86 | static ulong scsi_write(int device, ulong blknr, |
| 87 | lbaint_t blkcnt, const void *buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /********************************************************************************* |
| 91 | * (re)-scan the scsi bus and reports scsi device info |
| 92 | * to the user if mode = 1 |
| 93 | */ |
| 94 | void scsi_scan(int mode) |
| 95 | { |
| 96 | unsigned char i,perq,modi,lun; |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 97 | lbaint_t capacity; |
| 98 | unsigned long blksz; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 99 | ccb* pccb=(ccb *)&tempccb; |
| 100 | |
| 101 | if(mode==1) { |
| 102 | printf("scanning bus for devices...\n"); |
| 103 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 104 | for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 105 | scsi_dev_desc[i].target=0xff; |
| 106 | scsi_dev_desc[i].lun=0xff; |
| 107 | scsi_dev_desc[i].lba=0; |
| 108 | scsi_dev_desc[i].blksz=0; |
| 109 | scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; |
| 110 | scsi_dev_desc[i].vendor[0]=0; |
| 111 | scsi_dev_desc[i].product[0]=0; |
| 112 | scsi_dev_desc[i].revision[0]=0; |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 113 | scsi_dev_desc[i].removable = false; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 114 | scsi_dev_desc[i].if_type=IF_TYPE_SCSI; |
| 115 | scsi_dev_desc[i].dev=i; |
| 116 | scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; |
| 117 | scsi_dev_desc[i].block_read=scsi_read; |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 118 | scsi_dev_desc[i].block_write = scsi_write; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 119 | } |
| 120 | scsi_max_devs=0; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 121 | for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 122 | pccb->target=i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 123 | for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 124 | pccb->lun=lun; |
| 125 | pccb->pdata=(unsigned char *)&tempbuff; |
| 126 | pccb->datalen=512; |
| 127 | scsi_setup_inquiry(pccb); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 128 | if (scsi_exec(pccb) != true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 129 | if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 130 | debug ("Selection timeout ID %d\n",pccb->target); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 131 | continue; /* selection timeout => assuming no device present */ |
| 132 | } |
| 133 | scsi_print_error(pccb); |
| 134 | continue; |
| 135 | } |
| 136 | perq=tempbuff[0]; |
| 137 | modi=tempbuff[1]; |
| 138 | if((perq & 0x1f)==0x1f) { |
| 139 | continue; /* skip unknown devices */ |
| 140 | } |
| 141 | if((modi&0x80)==0x80) /* drive is removable */ |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 142 | scsi_dev_desc[scsi_max_devs].removable=true; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 143 | /* get info for this device */ |
Stefan Roese | 2309c13 | 2007-11-17 07:58:25 +0100 | [diff] [blame] | 144 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], |
| 145 | &tempbuff[8], 8); |
| 146 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], |
| 147 | &tempbuff[16], 16); |
| 148 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], |
| 149 | &tempbuff[32], 4); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 150 | scsi_dev_desc[scsi_max_devs].target=pccb->target; |
| 151 | scsi_dev_desc[scsi_max_devs].lun=pccb->lun; |
| 152 | |
| 153 | pccb->datalen=0; |
| 154 | scsi_setup_test_unit_ready(pccb); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 155 | if (scsi_exec(pccb) != true) { |
| 156 | if (scsi_dev_desc[scsi_max_devs].removable == true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 157 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 158 | goto removable; |
| 159 | } |
| 160 | scsi_print_error(pccb); |
| 161 | continue; |
| 162 | } |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 163 | if (scsi_read_capacity(pccb, &capacity, &blksz)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 164 | scsi_print_error(pccb); |
| 165 | continue; |
| 166 | } |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 167 | scsi_dev_desc[scsi_max_devs].lba=capacity; |
| 168 | scsi_dev_desc[scsi_max_devs].blksz=blksz; |
| 169 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 170 | init_part(&scsi_dev_desc[scsi_max_devs]); |
| 171 | removable: |
| 172 | if(mode==1) { |
| 173 | printf (" Device %d: ", scsi_max_devs); |
| 174 | dev_print(&scsi_dev_desc[scsi_max_devs]); |
| 175 | } /* if mode */ |
| 176 | scsi_max_devs++; |
| 177 | } /* next LUN */ |
| 178 | } |
| 179 | if(scsi_max_devs>0) |
| 180 | scsi_curr_dev=0; |
| 181 | else |
Wolfgang Denk | d0ff51b | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 182 | scsi_curr_dev = -1; |
Stefan Reinauer | 447c031 | 2012-10-29 05:23:48 +0000 | [diff] [blame] | 183 | |
| 184 | printf("Found %d device(s).\n", scsi_max_devs); |
| 185 | setenv_ulong("scsidevs", scsi_max_devs); |
| 186 | } |
| 187 | |
| 188 | int scsi_get_disk_count(void) |
| 189 | { |
| 190 | return scsi_max_devs; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 193 | #ifdef CONFIG_PCI |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 194 | void scsi_init(void) |
| 195 | { |
| 196 | int busdevfunc; |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 197 | int i; |
| 198 | /* |
| 199 | * Find a device from the list, this driver will support a single |
| 200 | * controller. |
| 201 | */ |
| 202 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 203 | /* get PCI Device ID */ |
| 204 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, |
| 205 | scsi_device_list[i].device, |
| 206 | 0); |
| 207 | if (busdevfunc != -1) |
| 208 | break; |
| 209 | } |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 210 | |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 211 | if (busdevfunc == -1) { |
| 212 | printf("Error: SCSI Controller(s) "); |
| 213 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 214 | printf("%04X:%04X ", |
| 215 | scsi_device_list[i].vendor, |
| 216 | scsi_device_list[i].device); |
| 217 | } |
| 218 | printf("not found\n"); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | #ifdef DEBUG |
| 222 | else { |
Vadim Bendebury | 4ae5eb7 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 223 | printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n", |
| 224 | scsi_device_list[i].vendor, |
| 225 | scsi_device_list[i].device, |
| 226 | (busdevfunc >> 16) & 0xFF, |
| 227 | (busdevfunc >> 11) & 0x1F, |
| 228 | (busdevfunc >> 8) & 0x7); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 229 | } |
| 230 | #endif |
| 231 | scsi_low_level_init(busdevfunc); |
| 232 | scsi_scan(1); |
| 233 | } |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 234 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 235 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 236 | #ifdef CONFIG_PARTITIONS |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 237 | block_dev_desc_t * scsi_get_dev(int dev) |
| 238 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 239 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 240 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 241 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 242 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 243 | /****************************************************************************** |
| 244 | * scsi boot command intepreter. Derived from diskboot |
| 245 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 246 | 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] | 247 | { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 248 | return common_diskboot(cmdtp, "scsi", argc, argv); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /********************************************************************************* |
| 252 | * scsi command intepreter |
| 253 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 254 | 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] | 255 | { |
| 256 | switch (argc) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 257 | case 0: |
| 258 | case 1: |
| 259 | return CMD_RET_USAGE; |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 260 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 261 | case 2: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 262 | if (strncmp(argv[1],"res",3) == 0) { |
| 263 | printf("\nReset SCSI\n"); |
| 264 | scsi_bus_reset(); |
| 265 | scsi_scan(1); |
| 266 | return 0; |
| 267 | } |
| 268 | if (strncmp(argv[1],"inf",3) == 0) { |
| 269 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 270 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 271 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
| 272 | continue; /* list only known devices */ |
| 273 | printf ("SCSI dev. %d: ", i); |
| 274 | dev_print(&scsi_dev_desc[i]); |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | if (strncmp(argv[1],"dev",3) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 279 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 280 | printf("\nno SCSI devices available\n"); |
| 281 | return 1; |
| 282 | } |
| 283 | printf ("\n Device %d: ", scsi_curr_dev); |
| 284 | dev_print(&scsi_dev_desc[scsi_curr_dev]); |
| 285 | return 0; |
| 286 | } |
| 287 | if (strncmp(argv[1],"scan",4) == 0) { |
| 288 | scsi_scan(1); |
| 289 | return 0; |
| 290 | } |
| 291 | if (strncmp(argv[1],"part",4) == 0) { |
| 292 | int dev, ok; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 293 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 294 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
| 295 | ok++; |
| 296 | if (dev) |
| 297 | printf("\n"); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 298 | debug ("print_part of %x\n",dev); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 299 | print_part(&scsi_dev_desc[dev]); |
| 300 | } |
| 301 | } |
| 302 | if (!ok) |
| 303 | printf("\nno SCSI devices available\n"); |
| 304 | return 1; |
| 305 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 306 | return CMD_RET_USAGE; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 307 | case 3: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 308 | if (strncmp(argv[1],"dev",3) == 0) { |
| 309 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 310 | printf ("\nSCSI device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 311 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 312 | printf("unknown device\n"); |
| 313 | return 1; |
| 314 | } |
| 315 | printf ("\n Device %d: ", dev); |
| 316 | dev_print(&scsi_dev_desc[dev]); |
| 317 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 318 | return 1; |
| 319 | } |
| 320 | scsi_curr_dev = dev; |
| 321 | printf("... is now current device\n"); |
| 322 | return 0; |
| 323 | } |
| 324 | if (strncmp(argv[1],"part",4) == 0) { |
| 325 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 326 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { |
| 327 | print_part(&scsi_dev_desc[dev]); |
| 328 | } |
| 329 | else { |
| 330 | printf ("\nSCSI device %d not available\n", dev); |
| 331 | } |
| 332 | return 1; |
| 333 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 334 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 335 | default: |
| 336 | /* at least 4 args */ |
| 337 | if (strcmp(argv[1],"read") == 0) { |
| 338 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 339 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 340 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 341 | ulong n; |
| 342 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 343 | scsi_curr_dev, blk, cnt); |
| 344 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); |
| 345 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); |
| 346 | return 0; |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 347 | } else if (strcmp(argv[1], "write") == 0) { |
| 348 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 349 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 350 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 351 | ulong n; |
| 352 | printf("\nSCSI write: device %d block # %ld, " |
| 353 | "count %ld ... ", |
| 354 | scsi_curr_dev, blk, cnt); |
| 355 | n = scsi_write(scsi_curr_dev, blk, cnt, |
| 356 | (ulong *)addr); |
| 357 | printf("%ld blocks written: %s\n", n, |
| 358 | (n == cnt) ? "OK" : "ERROR"); |
| 359 | return 0; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 360 | } |
| 361 | } /* switch */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 362 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /**************************************************************************************** |
| 366 | * scsi_read |
| 367 | */ |
| 368 | |
| 369 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ |
| 370 | |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 371 | static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 372 | { |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 373 | lbaint_t start, blks; |
| 374 | uintptr_t buf_addr; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 375 | unsigned short smallblks; |
| 376 | ccb* pccb=(ccb *)&tempccb; |
| 377 | device&=0xff; |
| 378 | /* Setup device |
| 379 | */ |
| 380 | pccb->target=scsi_dev_desc[device].target; |
| 381 | pccb->lun=scsi_dev_desc[device].lun; |
| 382 | buf_addr=(unsigned long)buffer; |
| 383 | start=blknr; |
| 384 | blks=blkcnt; |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 385 | debug("\nscsi_read: dev %d startblk " LBAF |
| 386 | ", blccnt " LBAF " buffer %lx\n", |
| 387 | device, start, blks, (unsigned long)buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 388 | do { |
| 389 | pccb->pdata=(unsigned char *)buf_addr; |
| 390 | if(blks>SCSI_MAX_READ_BLK) { |
| 391 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; |
| 392 | smallblks=SCSI_MAX_READ_BLK; |
| 393 | scsi_setup_read_ext(pccb,start,smallblks); |
| 394 | start+=SCSI_MAX_READ_BLK; |
| 395 | blks-=SCSI_MAX_READ_BLK; |
| 396 | } |
| 397 | else { |
| 398 | pccb->datalen=scsi_dev_desc[device].blksz * blks; |
| 399 | smallblks=(unsigned short) blks; |
| 400 | scsi_setup_read_ext(pccb,start,smallblks); |
| 401 | start+=blks; |
| 402 | blks=0; |
| 403 | } |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 404 | debug("scsi_read_ext: startblk " LBAF |
| 405 | ", blccnt %x buffer %lx\n", |
| 406 | start, smallblks, buf_addr); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 407 | if (scsi_exec(pccb) != true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 408 | scsi_print_error(pccb); |
| 409 | blkcnt-=blks; |
| 410 | break; |
| 411 | } |
| 412 | buf_addr+=pccb->datalen; |
| 413 | } while(blks!=0); |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 414 | debug("scsi_read_ext: end startblk " LBAF |
| 415 | ", blccnt %x buffer %lx\n", start, smallblks, buf_addr); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 416 | return(blkcnt); |
| 417 | } |
| 418 | |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 419 | /******************************************************************************* |
| 420 | * scsi_write |
| 421 | */ |
| 422 | |
| 423 | /* Almost the maximum amount of the scsi_ext command.. */ |
| 424 | #define SCSI_MAX_WRITE_BLK 0xFFFF |
| 425 | |
| 426 | static ulong scsi_write(int device, ulong blknr, |
| 427 | lbaint_t blkcnt, const void *buffer) |
| 428 | { |
| 429 | lbaint_t start, blks; |
| 430 | uintptr_t buf_addr; |
| 431 | unsigned short smallblks; |
| 432 | ccb* pccb = (ccb *)&tempccb; |
| 433 | device &= 0xff; |
| 434 | /* Setup device |
| 435 | */ |
| 436 | pccb->target = scsi_dev_desc[device].target; |
| 437 | pccb->lun = scsi_dev_desc[device].lun; |
| 438 | buf_addr = (unsigned long)buffer; |
| 439 | start = blknr; |
| 440 | blks = blkcnt; |
| 441 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", |
| 442 | __func__, device, start, blks, (unsigned long)buffer); |
| 443 | do { |
| 444 | pccb->pdata = (unsigned char *)buf_addr; |
| 445 | if (blks > SCSI_MAX_WRITE_BLK) { |
| 446 | pccb->datalen = (scsi_dev_desc[device].blksz * |
| 447 | SCSI_MAX_WRITE_BLK); |
| 448 | smallblks = SCSI_MAX_WRITE_BLK; |
| 449 | scsi_setup_write_ext(pccb, start, smallblks); |
| 450 | start += SCSI_MAX_WRITE_BLK; |
| 451 | blks -= SCSI_MAX_WRITE_BLK; |
| 452 | } else { |
| 453 | pccb->datalen = scsi_dev_desc[device].blksz * blks; |
| 454 | smallblks = (unsigned short)blks; |
| 455 | scsi_setup_write_ext(pccb, start, smallblks); |
| 456 | start += blks; |
| 457 | blks = 0; |
| 458 | } |
| 459 | debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n", |
| 460 | __func__, start, smallblks, buf_addr); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 461 | if (scsi_exec(pccb) != true) { |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 462 | scsi_print_error(pccb); |
| 463 | blkcnt -= blks; |
| 464 | break; |
| 465 | } |
| 466 | buf_addr += pccb->datalen; |
| 467 | } while (blks != 0); |
| 468 | debug("%s: end startblk " LBAF ", blccnt %x buffer %lx\n", |
| 469 | __func__, start, smallblks, buf_addr); |
| 470 | return blkcnt; |
| 471 | } |
| 472 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 473 | /* copy src to dest, skipping leading and trailing blanks |
| 474 | * and null terminate the string |
| 475 | */ |
| 476 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) |
| 477 | { |
| 478 | int start,end; |
| 479 | |
| 480 | start=0; |
| 481 | while(start<len) { |
| 482 | if(src[start]!=' ') |
| 483 | break; |
| 484 | start++; |
| 485 | } |
| 486 | end=len-1; |
| 487 | while(end>start) { |
| 488 | if(src[end]!=' ') |
| 489 | break; |
| 490 | end--; |
| 491 | } |
| 492 | for( ; start<=end; start++) { |
| 493 | *dest++=src[start]; |
| 494 | } |
| 495 | *dest='\0'; |
| 496 | } |
| 497 | |
| 498 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 499 | /* Trim trailing blanks, and NUL-terminate string |
| 500 | */ |
| 501 | void scsi_trim_trail (unsigned char *str, unsigned int len) |
| 502 | { |
| 503 | unsigned char *p = str + len - 1; |
| 504 | |
| 505 | while (len-- > 0) { |
| 506 | *p-- = '\0'; |
| 507 | if (*p != ' ') { |
| 508 | return; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 513 | int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz) |
| 514 | { |
| 515 | *capacity = 0; |
| 516 | |
| 517 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); |
| 518 | pccb->cmd[0] = SCSI_RD_CAPAC10; |
| 519 | pccb->cmd[1] = pccb->lun << 5; |
| 520 | pccb->cmdlen = 10; |
| 521 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 522 | |
| 523 | pccb->datalen = 8; |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 524 | if (scsi_exec(pccb) != true) |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 525 | return 1; |
| 526 | |
| 527 | *capacity = ((lbaint_t)pccb->pdata[0] << 24) | |
| 528 | ((lbaint_t)pccb->pdata[1] << 16) | |
| 529 | ((lbaint_t)pccb->pdata[2] << 8) | |
| 530 | ((lbaint_t)pccb->pdata[3]); |
| 531 | |
| 532 | if (*capacity != 0xffffffff) { |
| 533 | /* Read capacity (10) was sufficient for this drive. */ |
| 534 | *blksz = ((unsigned long)pccb->pdata[4] << 24) | |
| 535 | ((unsigned long)pccb->pdata[5] << 16) | |
| 536 | ((unsigned long)pccb->pdata[6] << 8) | |
| 537 | ((unsigned long)pccb->pdata[7]); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | /* Read capacity (10) was insufficient. Use read capacity (16). */ |
| 542 | |
| 543 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); |
| 544 | pccb->cmd[0] = SCSI_RD_CAPAC16; |
| 545 | pccb->cmd[1] = 0x10; |
| 546 | pccb->cmdlen = 16; |
| 547 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 548 | |
| 549 | pccb->datalen = 16; |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 550 | if (scsi_exec(pccb) != true) |
Gabe Black | b4c5bbc | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 551 | return 1; |
| 552 | |
| 553 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | |
| 554 | ((uint64_t)pccb->pdata[1] << 48) | |
| 555 | ((uint64_t)pccb->pdata[2] << 40) | |
| 556 | ((uint64_t)pccb->pdata[3] << 32) | |
| 557 | ((uint64_t)pccb->pdata[4] << 24) | |
| 558 | ((uint64_t)pccb->pdata[5] << 16) | |
| 559 | ((uint64_t)pccb->pdata[6] << 8) | |
| 560 | ((uint64_t)pccb->pdata[7]); |
| 561 | |
| 562 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | |
| 563 | ((uint64_t)pccb->pdata[9] << 48) | |
| 564 | ((uint64_t)pccb->pdata[10] << 40) | |
| 565 | ((uint64_t)pccb->pdata[11] << 32) | |
| 566 | ((uint64_t)pccb->pdata[12] << 24) | |
| 567 | ((uint64_t)pccb->pdata[13] << 16) | |
| 568 | ((uint64_t)pccb->pdata[14] << 8) | |
| 569 | ((uint64_t)pccb->pdata[15]); |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 574 | |
| 575 | /************************************************************************************ |
| 576 | * Some setup (fill-in) routines |
| 577 | */ |
| 578 | void scsi_setup_test_unit_ready(ccb * pccb) |
| 579 | { |
| 580 | pccb->cmd[0]=SCSI_TST_U_RDY; |
| 581 | pccb->cmd[1]=pccb->lun<<5; |
| 582 | pccb->cmd[2]=0; |
| 583 | pccb->cmd[3]=0; |
| 584 | pccb->cmd[4]=0; |
| 585 | pccb->cmd[5]=0; |
| 586 | pccb->cmdlen=6; |
| 587 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 588 | } |
| 589 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 590 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
| 591 | { |
| 592 | pccb->cmd[0]=SCSI_READ10; |
| 593 | pccb->cmd[1]=pccb->lun<<5; |
| 594 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; |
| 595 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; |
| 596 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; |
| 597 | pccb->cmd[5]=((unsigned char) (start))&0xff; |
| 598 | pccb->cmd[6]=0; |
| 599 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; |
| 600 | pccb->cmd[8]=(unsigned char) blocks & 0xff; |
| 601 | pccb->cmd[6]=0; |
| 602 | pccb->cmdlen=10; |
| 603 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 604 | 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] | 605 | pccb->cmd[0],pccb->cmd[1], |
| 606 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], |
| 607 | pccb->cmd[7],pccb->cmd[8]); |
| 608 | } |
| 609 | |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 610 | void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks) |
| 611 | { |
| 612 | pccb->cmd[0] = SCSI_WRITE10; |
| 613 | pccb->cmd[1] = pccb->lun << 5; |
| 614 | pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff; |
| 615 | pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff; |
| 616 | pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff; |
| 617 | pccb->cmd[5] = ((unsigned char) (start)) & 0xff; |
| 618 | pccb->cmd[6] = 0; |
| 619 | pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff; |
| 620 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 621 | pccb->cmd[9] = 0; |
| 622 | pccb->cmdlen = 10; |
| 623 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 624 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 625 | __func__, |
| 626 | pccb->cmd[0], pccb->cmd[1], |
| 627 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 628 | pccb->cmd[7], pccb->cmd[8]); |
| 629 | } |
| 630 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 631 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
| 632 | { |
| 633 | pccb->cmd[0]=SCSI_READ6; |
| 634 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); |
| 635 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; |
| 636 | pccb->cmd[3]=((unsigned char) (start))&0xff; |
| 637 | pccb->cmd[4]=(unsigned char) blocks & 0xff; |
| 638 | pccb->cmd[5]=0; |
| 639 | pccb->cmdlen=6; |
| 640 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 641 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 642 | pccb->cmd[0],pccb->cmd[1], |
| 643 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | void scsi_setup_inquiry(ccb * pccb) |
| 648 | { |
| 649 | pccb->cmd[0]=SCSI_INQUIRY; |
| 650 | pccb->cmd[1]=pccb->lun<<5; |
| 651 | pccb->cmd[2]=0; |
| 652 | pccb->cmd[3]=0; |
| 653 | if(pccb->datalen>255) |
| 654 | pccb->cmd[4]=255; |
| 655 | else |
| 656 | pccb->cmd[4]=(unsigned char)pccb->datalen; |
| 657 | pccb->cmd[5]=0; |
| 658 | pccb->cmdlen=6; |
| 659 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 660 | } |
| 661 | |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 662 | |
| 663 | U_BOOT_CMD( |
| 664 | scsi, 5, 1, do_scsi, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 665 | "SCSI sub-system", |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 666 | "reset - reset SCSI controller\n" |
| 667 | "scsi info - show available SCSI devices\n" |
| 668 | "scsi scan - (re-)scan SCSI bus\n" |
| 669 | "scsi device [dev] - show or set current device\n" |
| 670 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 671 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
Hung-Te Lin | 758c9e6 | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 672 | " to memory address `addr'\n" |
| 673 | "scsi write addr blk# cnt - write `cnt' blocks starting at block\n" |
| 674 | " `blk#' from memory address `addr'" |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 675 | ); |
| 676 | |
| 677 | U_BOOT_CMD( |
| 678 | scsiboot, 3, 1, do_scsiboot, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 679 | "boot from SCSI device", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 680 | "loadAddr dev:part" |
Wolfgang Denk | 460c322 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 681 | ); |