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); |
| 75 | void scsi_setup_read_capacity(ccb * pccb); |
| 76 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks); |
| 77 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks); |
| 78 | void scsi_setup_inquiry(ccb * pccb); |
| 79 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); |
| 80 | |
| 81 | |
Grant Likely | 2089cab | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 82 | ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | /********************************************************************************* |
| 86 | * (re)-scan the scsi bus and reports scsi device info |
| 87 | * to the user if mode = 1 |
| 88 | */ |
| 89 | void scsi_scan(int mode) |
| 90 | { |
| 91 | unsigned char i,perq,modi,lun; |
| 92 | unsigned long capacity,blksz; |
| 93 | ccb* pccb=(ccb *)&tempccb; |
| 94 | |
| 95 | if(mode==1) { |
| 96 | printf("scanning bus for devices...\n"); |
| 97 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 98 | for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 99 | scsi_dev_desc[i].target=0xff; |
| 100 | scsi_dev_desc[i].lun=0xff; |
| 101 | scsi_dev_desc[i].lba=0; |
| 102 | scsi_dev_desc[i].blksz=0; |
| 103 | scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; |
| 104 | scsi_dev_desc[i].vendor[0]=0; |
| 105 | scsi_dev_desc[i].product[0]=0; |
| 106 | scsi_dev_desc[i].revision[0]=0; |
| 107 | scsi_dev_desc[i].removable=FALSE; |
| 108 | scsi_dev_desc[i].if_type=IF_TYPE_SCSI; |
| 109 | scsi_dev_desc[i].dev=i; |
| 110 | scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; |
| 111 | scsi_dev_desc[i].block_read=scsi_read; |
| 112 | } |
| 113 | scsi_max_devs=0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 114 | for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 115 | pccb->target=i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 117 | pccb->lun=lun; |
| 118 | pccb->pdata=(unsigned char *)&tempbuff; |
| 119 | pccb->datalen=512; |
| 120 | scsi_setup_inquiry(pccb); |
| 121 | if(scsi_exec(pccb)!=TRUE) { |
| 122 | if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 123 | debug ("Selection timeout ID %d\n",pccb->target); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 124 | continue; /* selection timeout => assuming no device present */ |
| 125 | } |
| 126 | scsi_print_error(pccb); |
| 127 | continue; |
| 128 | } |
| 129 | perq=tempbuff[0]; |
| 130 | modi=tempbuff[1]; |
| 131 | if((perq & 0x1f)==0x1f) { |
| 132 | continue; /* skip unknown devices */ |
| 133 | } |
| 134 | if((modi&0x80)==0x80) /* drive is removable */ |
| 135 | scsi_dev_desc[scsi_max_devs].removable=TRUE; |
| 136 | /* get info for this device */ |
Stefan Roese | 4404c16 | 2007-11-17 07:58:25 +0100 | [diff] [blame] | 137 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], |
| 138 | &tempbuff[8], 8); |
| 139 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], |
| 140 | &tempbuff[16], 16); |
| 141 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], |
| 142 | &tempbuff[32], 4); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 143 | scsi_dev_desc[scsi_max_devs].target=pccb->target; |
| 144 | scsi_dev_desc[scsi_max_devs].lun=pccb->lun; |
| 145 | |
| 146 | pccb->datalen=0; |
| 147 | scsi_setup_test_unit_ready(pccb); |
| 148 | if(scsi_exec(pccb)!=TRUE) { |
| 149 | if(scsi_dev_desc[scsi_max_devs].removable==TRUE) { |
| 150 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 151 | goto removable; |
| 152 | } |
| 153 | scsi_print_error(pccb); |
| 154 | continue; |
| 155 | } |
| 156 | pccb->datalen=8; |
| 157 | scsi_setup_read_capacity(pccb); |
| 158 | if(scsi_exec(pccb)!=TRUE) { |
| 159 | scsi_print_error(pccb); |
| 160 | continue; |
| 161 | } |
| 162 | capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)| |
| 163 | ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]); |
| 164 | blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)| |
| 165 | ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]); |
| 166 | scsi_dev_desc[scsi_max_devs].lba=capacity; |
| 167 | scsi_dev_desc[scsi_max_devs].blksz=blksz; |
| 168 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 169 | init_part(&scsi_dev_desc[scsi_max_devs]); |
| 170 | removable: |
| 171 | if(mode==1) { |
| 172 | printf (" Device %d: ", scsi_max_devs); |
| 173 | dev_print(&scsi_dev_desc[scsi_max_devs]); |
| 174 | } /* if mode */ |
| 175 | scsi_max_devs++; |
| 176 | } /* next LUN */ |
| 177 | } |
| 178 | if(scsi_max_devs>0) |
| 179 | scsi_curr_dev=0; |
| 180 | else |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 181 | scsi_curr_dev = -1; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Rob Herring | c2829ff | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 184 | #ifdef CONFIG_PCI |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 185 | void scsi_init(void) |
| 186 | { |
| 187 | int busdevfunc; |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame^] | 188 | int i; |
| 189 | /* |
| 190 | * Find a device from the list, this driver will support a single |
| 191 | * controller. |
| 192 | */ |
| 193 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 194 | /* get PCI Device ID */ |
| 195 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, |
| 196 | scsi_device_list[i].device, |
| 197 | 0); |
| 198 | if (busdevfunc != -1) |
| 199 | break; |
| 200 | } |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 201 | |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame^] | 202 | if (busdevfunc == -1) { |
| 203 | printf("Error: SCSI Controller(s) "); |
| 204 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 205 | printf("%04X:%04X ", |
| 206 | scsi_device_list[i].vendor, |
| 207 | scsi_device_list[i].device); |
| 208 | } |
| 209 | printf("not found\n"); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | #ifdef DEBUG |
| 213 | else { |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame^] | 214 | printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n", |
| 215 | scsi_device_list[i].vendor, |
| 216 | scsi_device_list[i].device, |
| 217 | (busdevfunc >> 16) & 0xFF, |
| 218 | (busdevfunc >> 11) & 0x1F, |
| 219 | (busdevfunc >> 8) & 0x7); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 220 | } |
| 221 | #endif |
| 222 | scsi_low_level_init(busdevfunc); |
| 223 | scsi_scan(1); |
| 224 | } |
Rob Herring | c2829ff | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 225 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 226 | |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 227 | #ifdef CONFIG_PARTITIONS |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 228 | block_dev_desc_t * scsi_get_dev(int dev) |
| 229 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 230 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 231 | } |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 232 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 233 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 234 | /****************************************************************************** |
| 235 | * scsi boot command intepreter. Derived from diskboot |
| 236 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 237 | 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] | 238 | { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 239 | return common_diskboot(cmdtp, "scsi", argc, argv); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /********************************************************************************* |
| 243 | * scsi command intepreter |
| 244 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | switch (argc) { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 248 | case 0: |
| 249 | case 1: |
| 250 | return CMD_RET_USAGE; |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 251 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 252 | case 2: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 253 | if (strncmp(argv[1],"res",3) == 0) { |
| 254 | printf("\nReset SCSI\n"); |
| 255 | scsi_bus_reset(); |
| 256 | scsi_scan(1); |
| 257 | return 0; |
| 258 | } |
| 259 | if (strncmp(argv[1],"inf",3) == 0) { |
| 260 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 261 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 262 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
| 263 | continue; /* list only known devices */ |
| 264 | printf ("SCSI dev. %d: ", i); |
| 265 | dev_print(&scsi_dev_desc[i]); |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | if (strncmp(argv[1],"dev",3) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 270 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 271 | printf("\nno SCSI devices available\n"); |
| 272 | return 1; |
| 273 | } |
| 274 | printf ("\n Device %d: ", scsi_curr_dev); |
| 275 | dev_print(&scsi_dev_desc[scsi_curr_dev]); |
| 276 | return 0; |
| 277 | } |
| 278 | if (strncmp(argv[1],"scan",4) == 0) { |
| 279 | scsi_scan(1); |
| 280 | return 0; |
| 281 | } |
| 282 | if (strncmp(argv[1],"part",4) == 0) { |
| 283 | int dev, ok; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 284 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 285 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
| 286 | ok++; |
| 287 | if (dev) |
| 288 | printf("\n"); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 289 | debug ("print_part of %x\n",dev); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 290 | print_part(&scsi_dev_desc[dev]); |
| 291 | } |
| 292 | } |
| 293 | if (!ok) |
| 294 | printf("\nno SCSI devices available\n"); |
| 295 | return 1; |
| 296 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 297 | return CMD_RET_USAGE; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 298 | case 3: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 299 | if (strncmp(argv[1],"dev",3) == 0) { |
| 300 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 301 | printf ("\nSCSI device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 302 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 303 | printf("unknown device\n"); |
| 304 | return 1; |
| 305 | } |
| 306 | printf ("\n Device %d: ", dev); |
| 307 | dev_print(&scsi_dev_desc[dev]); |
| 308 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 309 | return 1; |
| 310 | } |
| 311 | scsi_curr_dev = dev; |
| 312 | printf("... is now current device\n"); |
| 313 | return 0; |
| 314 | } |
| 315 | if (strncmp(argv[1],"part",4) == 0) { |
| 316 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 317 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { |
| 318 | print_part(&scsi_dev_desc[dev]); |
| 319 | } |
| 320 | else { |
| 321 | printf ("\nSCSI device %d not available\n", dev); |
| 322 | } |
| 323 | return 1; |
| 324 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 325 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 326 | default: |
| 327 | /* at least 4 args */ |
| 328 | if (strcmp(argv[1],"read") == 0) { |
| 329 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 330 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 331 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 332 | ulong n; |
| 333 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 334 | scsi_curr_dev, blk, cnt); |
| 335 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); |
| 336 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); |
| 337 | return 0; |
| 338 | } |
| 339 | } /* switch */ |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 340 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /**************************************************************************************** |
| 344 | * scsi_read |
| 345 | */ |
| 346 | |
| 347 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ |
| 348 | |
Grant Likely | 2089cab | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 349 | ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 350 | { |
| 351 | ulong start,blks, buf_addr; |
| 352 | unsigned short smallblks; |
| 353 | ccb* pccb=(ccb *)&tempccb; |
| 354 | device&=0xff; |
| 355 | /* Setup device |
| 356 | */ |
| 357 | pccb->target=scsi_dev_desc[device].target; |
| 358 | pccb->lun=scsi_dev_desc[device].lun; |
| 359 | buf_addr=(unsigned long)buffer; |
| 360 | start=blknr; |
| 361 | blks=blkcnt; |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 362 | 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] | 363 | do { |
| 364 | pccb->pdata=(unsigned char *)buf_addr; |
| 365 | if(blks>SCSI_MAX_READ_BLK) { |
| 366 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; |
| 367 | smallblks=SCSI_MAX_READ_BLK; |
| 368 | scsi_setup_read_ext(pccb,start,smallblks); |
| 369 | start+=SCSI_MAX_READ_BLK; |
| 370 | blks-=SCSI_MAX_READ_BLK; |
| 371 | } |
| 372 | else { |
| 373 | pccb->datalen=scsi_dev_desc[device].blksz * blks; |
| 374 | smallblks=(unsigned short) blks; |
| 375 | scsi_setup_read_ext(pccb,start,smallblks); |
| 376 | start+=blks; |
| 377 | blks=0; |
| 378 | } |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 379 | 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] | 380 | if(scsi_exec(pccb)!=TRUE) { |
| 381 | scsi_print_error(pccb); |
| 382 | blkcnt-=blks; |
| 383 | break; |
| 384 | } |
| 385 | buf_addr+=pccb->datalen; |
| 386 | } while(blks!=0); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 387 | 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] | 388 | return(blkcnt); |
| 389 | } |
| 390 | |
| 391 | /* copy src to dest, skipping leading and trailing blanks |
| 392 | * and null terminate the string |
| 393 | */ |
| 394 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) |
| 395 | { |
| 396 | int start,end; |
| 397 | |
| 398 | start=0; |
| 399 | while(start<len) { |
| 400 | if(src[start]!=' ') |
| 401 | break; |
| 402 | start++; |
| 403 | } |
| 404 | end=len-1; |
| 405 | while(end>start) { |
| 406 | if(src[end]!=' ') |
| 407 | break; |
| 408 | end--; |
| 409 | } |
| 410 | for( ; start<=end; start++) { |
| 411 | *dest++=src[start]; |
| 412 | } |
| 413 | *dest='\0'; |
| 414 | } |
| 415 | |
| 416 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 417 | /* Trim trailing blanks, and NUL-terminate string |
| 418 | */ |
| 419 | void scsi_trim_trail (unsigned char *str, unsigned int len) |
| 420 | { |
| 421 | unsigned char *p = str + len - 1; |
| 422 | |
| 423 | while (len-- > 0) { |
| 424 | *p-- = '\0'; |
| 425 | if (*p != ' ') { |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /************************************************************************************ |
| 433 | * Some setup (fill-in) routines |
| 434 | */ |
| 435 | void scsi_setup_test_unit_ready(ccb * pccb) |
| 436 | { |
| 437 | pccb->cmd[0]=SCSI_TST_U_RDY; |
| 438 | pccb->cmd[1]=pccb->lun<<5; |
| 439 | pccb->cmd[2]=0; |
| 440 | pccb->cmd[3]=0; |
| 441 | pccb->cmd[4]=0; |
| 442 | pccb->cmd[5]=0; |
| 443 | pccb->cmdlen=6; |
| 444 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 445 | } |
| 446 | |
| 447 | void scsi_setup_read_capacity(ccb * pccb) |
| 448 | { |
| 449 | pccb->cmd[0]=SCSI_RD_CAPAC; |
| 450 | pccb->cmd[1]=pccb->lun<<5; |
| 451 | pccb->cmd[2]=0; |
| 452 | pccb->cmd[3]=0; |
| 453 | pccb->cmd[4]=0; |
| 454 | pccb->cmd[5]=0; |
| 455 | pccb->cmd[6]=0; |
| 456 | pccb->cmd[7]=0; |
| 457 | pccb->cmd[8]=0; |
| 458 | pccb->cmd[9]=0; |
| 459 | pccb->cmdlen=10; |
| 460 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 461 | |
| 462 | } |
| 463 | |
| 464 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
| 465 | { |
| 466 | pccb->cmd[0]=SCSI_READ10; |
| 467 | pccb->cmd[1]=pccb->lun<<5; |
| 468 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; |
| 469 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; |
| 470 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; |
| 471 | pccb->cmd[5]=((unsigned char) (start))&0xff; |
| 472 | pccb->cmd[6]=0; |
| 473 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; |
| 474 | pccb->cmd[8]=(unsigned char) blocks & 0xff; |
| 475 | pccb->cmd[6]=0; |
| 476 | pccb->cmdlen=10; |
| 477 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 478 | 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] | 479 | pccb->cmd[0],pccb->cmd[1], |
| 480 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], |
| 481 | pccb->cmd[7],pccb->cmd[8]); |
| 482 | } |
| 483 | |
| 484 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
| 485 | { |
| 486 | pccb->cmd[0]=SCSI_READ6; |
| 487 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); |
| 488 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; |
| 489 | pccb->cmd[3]=((unsigned char) (start))&0xff; |
| 490 | pccb->cmd[4]=(unsigned char) blocks & 0xff; |
| 491 | pccb->cmd[5]=0; |
| 492 | pccb->cmdlen=6; |
| 493 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 494 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 495 | pccb->cmd[0],pccb->cmd[1], |
| 496 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | void scsi_setup_inquiry(ccb * pccb) |
| 501 | { |
| 502 | pccb->cmd[0]=SCSI_INQUIRY; |
| 503 | pccb->cmd[1]=pccb->lun<<5; |
| 504 | pccb->cmd[2]=0; |
| 505 | pccb->cmd[3]=0; |
| 506 | if(pccb->datalen>255) |
| 507 | pccb->cmd[4]=255; |
| 508 | else |
| 509 | pccb->cmd[4]=(unsigned char)pccb->datalen; |
| 510 | pccb->cmd[5]=0; |
| 511 | pccb->cmdlen=6; |
| 512 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 513 | } |
| 514 | |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 515 | |
| 516 | U_BOOT_CMD( |
| 517 | scsi, 5, 1, do_scsi, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 518 | "SCSI sub-system", |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 519 | "reset - reset SCSI controller\n" |
| 520 | "scsi info - show available SCSI devices\n" |
| 521 | "scsi scan - (re-)scan SCSI bus\n" |
| 522 | "scsi device [dev] - show or set current device\n" |
| 523 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 524 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 525 | " to memory address `addr'" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 526 | ); |
| 527 | |
| 528 | U_BOOT_CMD( |
| 529 | scsiboot, 3, 1, do_scsiboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 530 | "boot from SCSI device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 531 | "loadAddr dev:part" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 532 | ); |