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