1500856ebSRafal Jaworowski /* 2f2302d44SStefan Roese * (C) Copyright 2007-2008 Semihalf 3500856ebSRafal Jaworowski * 4500856ebSRafal Jaworowski * Written by: Rafal Jaworowski <raj@semihalf.com> 5500856ebSRafal Jaworowski * 61a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 7500856ebSRafal Jaworowski */ 8500856ebSRafal Jaworowski 9500856ebSRafal Jaworowski #include <config.h> 10500856ebSRafal Jaworowski #include <common.h> 11500856ebSRafal Jaworowski #include <api_public.h> 12500856ebSRafal Jaworowski 133d0ea311SMatthias Fuchs #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) 143d0ea311SMatthias Fuchs #include <usb.h> 153d0ea311SMatthias Fuchs #endif 163d0ea311SMatthias Fuchs 17500856ebSRafal Jaworowski #define DEBUG 18500856ebSRafal Jaworowski #undef DEBUG 19500856ebSRafal Jaworowski 20500856ebSRafal Jaworowski #ifdef DEBUG 21500856ebSRafal Jaworowski #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0) 22500856ebSRafal Jaworowski #else 23500856ebSRafal Jaworowski #define debugf(fmt, args...) 24500856ebSRafal Jaworowski #endif 25500856ebSRafal Jaworowski 26500856ebSRafal Jaworowski #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0) 27500856ebSRafal Jaworowski 28500856ebSRafal Jaworowski 29500856ebSRafal Jaworowski #define ENUM_IDE 0 30500856ebSRafal Jaworowski #define ENUM_USB 1 31500856ebSRafal Jaworowski #define ENUM_SCSI 2 32500856ebSRafal Jaworowski #define ENUM_MMC 3 33f2302d44SStefan Roese #define ENUM_SATA 4 34f2302d44SStefan Roese #define ENUM_MAX 5 35500856ebSRafal Jaworowski 36500856ebSRafal Jaworowski struct stor_spec { 37500856ebSRafal Jaworowski int max_dev; 38500856ebSRafal Jaworowski int enum_started; 39500856ebSRafal Jaworowski int enum_ended; 40500856ebSRafal Jaworowski int type; /* "external" type: DT_STOR_{IDE,USB,etc} */ 41f2302d44SStefan Roese char *name; 42500856ebSRafal Jaworowski }; 43500856ebSRafal Jaworowski 445d81c6dfSEmmanuel Vadot static struct stor_spec specs[ENUM_MAX] = { { 0, 0, 0, 0, NULL }, }; 45500856ebSRafal Jaworowski 46500856ebSRafal Jaworowski 47500856ebSRafal Jaworowski void dev_stor_init(void) 48500856ebSRafal Jaworowski { 49*fc843a02SSimon Glass #if defined(CONFIG_IDE) 506d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_IDE].max_dev = CONFIG_SYS_IDE_MAXDEVICE; 51500856ebSRafal Jaworowski specs[ENUM_IDE].enum_started = 0; 52500856ebSRafal Jaworowski specs[ENUM_IDE].enum_ended = 0; 53500856ebSRafal Jaworowski specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE; 54500856ebSRafal Jaworowski specs[ENUM_IDE].name = "ide"; 55500856ebSRafal Jaworowski #endif 56f2302d44SStefan Roese #if defined(CONFIG_CMD_MMC) 576d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_MMC].max_dev = CONFIG_SYS_MMC_MAX_DEVICE; 58f2302d44SStefan Roese specs[ENUM_MMC].enum_started = 0; 59f2302d44SStefan Roese specs[ENUM_MMC].enum_ended = 0; 60f2302d44SStefan Roese specs[ENUM_MMC].type = DEV_TYP_STOR | DT_STOR_MMC; 61f2302d44SStefan Roese specs[ENUM_MMC].name = "mmc"; 62f2302d44SStefan Roese #endif 63f2302d44SStefan Roese #if defined(CONFIG_CMD_SATA) 646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_SATA].max_dev = CONFIG_SYS_SATA_MAX_DEVICE; 65f2302d44SStefan Roese specs[ENUM_SATA].enum_started = 0; 66f2302d44SStefan Roese specs[ENUM_SATA].enum_ended = 0; 67f2302d44SStefan Roese specs[ENUM_SATA].type = DEV_TYP_STOR | DT_STOR_SATA; 68f2302d44SStefan Roese specs[ENUM_SATA].name = "sata"; 69500856ebSRafal Jaworowski #endif 70c649e3c9SSimon Glass #if defined(CONFIG_SCSI) 716d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_SCSI].max_dev = CONFIG_SYS_SCSI_MAX_DEVICE; 72500856ebSRafal Jaworowski specs[ENUM_SCSI].enum_started = 0; 73500856ebSRafal Jaworowski specs[ENUM_SCSI].enum_ended = 0; 74500856ebSRafal Jaworowski specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI; 75500856ebSRafal Jaworowski specs[ENUM_SCSI].name = "scsi"; 76500856ebSRafal Jaworowski #endif 77f2302d44SStefan Roese #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) 78f2302d44SStefan Roese specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV; 79f2302d44SStefan Roese specs[ENUM_USB].enum_started = 0; 80f2302d44SStefan Roese specs[ENUM_USB].enum_ended = 0; 81f2302d44SStefan Roese specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB; 82f2302d44SStefan Roese specs[ENUM_USB].name = "usb"; 83f2302d44SStefan Roese #endif 84500856ebSRafal Jaworowski } 85500856ebSRafal Jaworowski 86500856ebSRafal Jaworowski /* 87500856ebSRafal Jaworowski * Finds next available device in the storage group 88500856ebSRafal Jaworowski * 89500856ebSRafal Jaworowski * type: storage group type - ENUM_IDE, ENUM_SCSI etc. 90500856ebSRafal Jaworowski * 91500856ebSRafal Jaworowski * more: returns 0/1 depending if there are more devices in this group 92500856ebSRafal Jaworowski * available (for future iterations) 93500856ebSRafal Jaworowski * 94500856ebSRafal Jaworowski * returns: 0/1 depending if device found in this iteration 95500856ebSRafal Jaworowski */ 96d3e8f630SEmmanuel Vadot static int dev_stor_get(int type, int *more, struct device_info *di) 97500856ebSRafal Jaworowski { 984101f687SSimon Glass struct blk_desc *dd; 99d3e8f630SEmmanuel Vadot int found = 0; 100d3e8f630SEmmanuel Vadot int i = 0; 101500856ebSRafal Jaworowski 1025d81c6dfSEmmanuel Vadot /* Wasn't configured for this type, return 0 directly */ 1035d81c6dfSEmmanuel Vadot if (specs[type].name == NULL) 1045d81c6dfSEmmanuel Vadot return 0; 1055d81c6dfSEmmanuel Vadot 106d3e8f630SEmmanuel Vadot if (di->cookie != NULL) { 107d3e8f630SEmmanuel Vadot /* Find the last device we've returned */ 108d3e8f630SEmmanuel Vadot for (i = 0; i < specs[type].max_dev; i++) { 109db1d9e78SSimon Glass if (di->cookie == 110db1d9e78SSimon Glass (void *)blk_get_dev(specs[type].name, i)) { 111d3e8f630SEmmanuel Vadot i += 1; 112500856ebSRafal Jaworowski break; 113500856ebSRafal Jaworowski } 114d3e8f630SEmmanuel Vadot } 115d3e8f630SEmmanuel Vadot } 116500856ebSRafal Jaworowski 117d3e8f630SEmmanuel Vadot for (; i < specs[type].max_dev; i++) { 118d3e8f630SEmmanuel Vadot di->cookie = (void *)blk_get_dev(specs[type].name, i); 119d3e8f630SEmmanuel Vadot 120d3e8f630SEmmanuel Vadot if (di->cookie != NULL) { 121500856ebSRafal Jaworowski found = 1; 122500856ebSRafal Jaworowski break; 123500856ebSRafal Jaworowski } 124500856ebSRafal Jaworowski } 125500856ebSRafal Jaworowski 126d3e8f630SEmmanuel Vadot if (i == specs[type].max_dev) 127d3e8f630SEmmanuel Vadot *more = 0; 128d3e8f630SEmmanuel Vadot else 129d3e8f630SEmmanuel Vadot *more = 1; 130d3e8f630SEmmanuel Vadot 131500856ebSRafal Jaworowski if (found) { 132500856ebSRafal Jaworowski di->type = specs[type].type; 133500856ebSRafal Jaworowski 1344101f687SSimon Glass dd = (struct blk_desc *)di->cookie; 135500856ebSRafal Jaworowski if (dd->type == DEV_TYPE_UNKNOWN) { 136500856ebSRafal Jaworowski debugf("device instance exists, but is not active.."); 137500856ebSRafal Jaworowski found = 0; 138500856ebSRafal Jaworowski } else { 139500856ebSRafal Jaworowski di->di_stor.block_count = dd->lba; 140500856ebSRafal Jaworowski di->di_stor.block_size = dd->blksz; 141500856ebSRafal Jaworowski } 142d3e8f630SEmmanuel Vadot } else { 143500856ebSRafal Jaworowski di->cookie = NULL; 144d3e8f630SEmmanuel Vadot } 145500856ebSRafal Jaworowski 146500856ebSRafal Jaworowski return found; 147500856ebSRafal Jaworowski } 148500856ebSRafal Jaworowski 149500856ebSRafal Jaworowski 150c9db75a0SWalt Feasel /* returns: ENUM_IDE, ENUM_USB etc. based on struct blk_desc */ 151c9db75a0SWalt Feasel 1524101f687SSimon Glass static int dev_stor_type(struct blk_desc *dd) 153500856ebSRafal Jaworowski { 154500856ebSRafal Jaworowski int i, j; 155500856ebSRafal Jaworowski 156500856ebSRafal Jaworowski for (i = ENUM_IDE; i < ENUM_MAX; i++) 157500856ebSRafal Jaworowski for (j = 0; j < specs[i].max_dev; j++) 158db1d9e78SSimon Glass if (dd == blk_get_dev(specs[i].name, j)) 159500856ebSRafal Jaworowski return i; 160500856ebSRafal Jaworowski 161500856ebSRafal Jaworowski return ENUM_MAX; 162500856ebSRafal Jaworowski } 163500856ebSRafal Jaworowski 164500856ebSRafal Jaworowski 165c9db75a0SWalt Feasel /* returns: 0/1 whether cookie points to some device in this group */ 166c9db75a0SWalt Feasel 167500856ebSRafal Jaworowski static int dev_is_stor(int type, struct device_info *di) 168500856ebSRafal Jaworowski { 169500856ebSRafal Jaworowski return (dev_stor_type(di->cookie) == type) ? 1 : 0; 170500856ebSRafal Jaworowski } 171500856ebSRafal Jaworowski 172500856ebSRafal Jaworowski 173500856ebSRafal Jaworowski static int dev_enum_stor(int type, struct device_info *di) 174500856ebSRafal Jaworowski { 175500856ebSRafal Jaworowski int found = 0, more = 0; 176500856ebSRafal Jaworowski 177500856ebSRafal Jaworowski debugf("called, type %d\n", type); 178500856ebSRafal Jaworowski 179500856ebSRafal Jaworowski /* 180500856ebSRafal Jaworowski * Formulae for enumerating storage devices: 181500856ebSRafal Jaworowski * 1. if cookie (hint from previous enum call) is NULL we start again 182500856ebSRafal Jaworowski * with enumeration, so return the first available device, done. 183500856ebSRafal Jaworowski * 184500856ebSRafal Jaworowski * 2. if cookie is not NULL, check if it identifies some device in 185500856ebSRafal Jaworowski * this group: 186500856ebSRafal Jaworowski * 187500856ebSRafal Jaworowski * 2a. if cookie is a storage device from our group (IDE, USB etc.), 188500856ebSRafal Jaworowski * return next available (if exists) in this group 189500856ebSRafal Jaworowski * 190500856ebSRafal Jaworowski * 2b. if it isn't device from our group, check if such devices were 191500856ebSRafal Jaworowski * ever enumerated before: 192500856ebSRafal Jaworowski * - if not, return the first available device from this group 193500856ebSRafal Jaworowski * - else return 0 194500856ebSRafal Jaworowski */ 195500856ebSRafal Jaworowski 196500856ebSRafal Jaworowski if (di->cookie == NULL) { 197500856ebSRafal Jaworowski debugf("group%d - enum restart\n", type); 198500856ebSRafal Jaworowski 199500856ebSRafal Jaworowski /* 200500856ebSRafal Jaworowski * 1. Enumeration (re-)started: take the first available 201500856ebSRafal Jaworowski * device, if exists 202500856ebSRafal Jaworowski */ 203d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di); 204500856ebSRafal Jaworowski specs[type].enum_started = 1; 205500856ebSRafal Jaworowski 206500856ebSRafal Jaworowski } else if (dev_is_stor(type, di)) { 207500856ebSRafal Jaworowski debugf("group%d - enum continued for the next device\n", type); 208500856ebSRafal Jaworowski 209500856ebSRafal Jaworowski if (specs[type].enum_ended) { 210500856ebSRafal Jaworowski debugf("group%d - nothing more to enum!\n", type); 211500856ebSRafal Jaworowski return 0; 212500856ebSRafal Jaworowski } 213500856ebSRafal Jaworowski 214500856ebSRafal Jaworowski /* 2a. Attempt to take a next available device in the group */ 215d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di); 216500856ebSRafal Jaworowski 217500856ebSRafal Jaworowski } else { 218500856ebSRafal Jaworowski if (specs[type].enum_ended) { 219500856ebSRafal Jaworowski debugf("group %d - already enumerated, skipping\n", type); 220500856ebSRafal Jaworowski return 0; 221500856ebSRafal Jaworowski } 222500856ebSRafal Jaworowski 223500856ebSRafal Jaworowski debugf("group%d - first time enum\n", type); 224500856ebSRafal Jaworowski 225500856ebSRafal Jaworowski if (specs[type].enum_started == 0) { 226500856ebSRafal Jaworowski /* 227500856ebSRafal Jaworowski * 2b. If enumerating devices in this group did not 228500856ebSRafal Jaworowski * happen before, it means the cookie pointed to a 22983f9ecbeSWalt Feasel * device from some other group (another storage 230500856ebSRafal Jaworowski * group, or network); in this case try to take the 231500856ebSRafal Jaworowski * first available device from our group 232500856ebSRafal Jaworowski */ 233500856ebSRafal Jaworowski specs[type].enum_started = 1; 234500856ebSRafal Jaworowski 235500856ebSRafal Jaworowski /* 236500856ebSRafal Jaworowski * Attempt to take the first device in this group: 237500856ebSRafal Jaworowski *'first element' flag is set 238500856ebSRafal Jaworowski */ 239d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di); 240500856ebSRafal Jaworowski 241500856ebSRafal Jaworowski } else { 242500856ebSRafal Jaworowski errf("group%d - out of order iteration\n", type); 243500856ebSRafal Jaworowski found = 0; 244500856ebSRafal Jaworowski more = 0; 245500856ebSRafal Jaworowski } 246500856ebSRafal Jaworowski } 247500856ebSRafal Jaworowski 248500856ebSRafal Jaworowski /* 249500856ebSRafal Jaworowski * If there are no more devices in this group, consider its 250500856ebSRafal Jaworowski * enumeration finished 251500856ebSRafal Jaworowski */ 252500856ebSRafal Jaworowski specs[type].enum_ended = (!more) ? 1 : 0; 253500856ebSRafal Jaworowski 254500856ebSRafal Jaworowski if (found) 255500856ebSRafal Jaworowski debugf("device found, returning cookie 0x%08x\n", 256500856ebSRafal Jaworowski (u_int32_t)di->cookie); 257500856ebSRafal Jaworowski else 258500856ebSRafal Jaworowski debugf("no device found\n"); 259500856ebSRafal Jaworowski 260500856ebSRafal Jaworowski return found; 261500856ebSRafal Jaworowski } 262500856ebSRafal Jaworowski 263500856ebSRafal Jaworowski void dev_enum_reset(void) 264500856ebSRafal Jaworowski { 265500856ebSRafal Jaworowski int i; 266500856ebSRafal Jaworowski 267500856ebSRafal Jaworowski for (i = 0; i < ENUM_MAX; i ++) { 268500856ebSRafal Jaworowski specs[i].enum_started = 0; 269500856ebSRafal Jaworowski specs[i].enum_ended = 0; 270500856ebSRafal Jaworowski } 271500856ebSRafal Jaworowski } 272500856ebSRafal Jaworowski 273500856ebSRafal Jaworowski int dev_enum_storage(struct device_info *di) 274500856ebSRafal Jaworowski { 275500856ebSRafal Jaworowski int i; 276500856ebSRafal Jaworowski 277c9db75a0SWalt Feasel /* check: ide, usb, scsi, mmc */ 278500856ebSRafal Jaworowski for (i = ENUM_IDE; i < ENUM_MAX; i ++) { 279500856ebSRafal Jaworowski if (dev_enum_stor(i, di)) 280500856ebSRafal Jaworowski return 1; 281500856ebSRafal Jaworowski } 282500856ebSRafal Jaworowski 283500856ebSRafal Jaworowski return 0; 284500856ebSRafal Jaworowski } 285500856ebSRafal Jaworowski 2864101f687SSimon Glass static int dev_stor_is_valid(int type, struct blk_desc *dd) 287500856ebSRafal Jaworowski { 288500856ebSRafal Jaworowski int i; 289500856ebSRafal Jaworowski 290500856ebSRafal Jaworowski for (i = 0; i < specs[type].max_dev; i++) 291db1d9e78SSimon Glass if (dd == blk_get_dev(specs[type].name, i)) 292500856ebSRafal Jaworowski if (dd->type != DEV_TYPE_UNKNOWN) 293500856ebSRafal Jaworowski return 1; 294500856ebSRafal Jaworowski 295500856ebSRafal Jaworowski return 0; 296500856ebSRafal Jaworowski } 297500856ebSRafal Jaworowski 298500856ebSRafal Jaworowski 299500856ebSRafal Jaworowski int dev_open_stor(void *cookie) 300500856ebSRafal Jaworowski { 301500856ebSRafal Jaworowski int type = dev_stor_type(cookie); 302500856ebSRafal Jaworowski 303500856ebSRafal Jaworowski if (type == ENUM_MAX) 304500856ebSRafal Jaworowski return API_ENODEV; 305500856ebSRafal Jaworowski 3064101f687SSimon Glass if (dev_stor_is_valid(type, (struct blk_desc *)cookie)) 307500856ebSRafal Jaworowski return 0; 308500856ebSRafal Jaworowski 309500856ebSRafal Jaworowski return API_ENODEV; 310500856ebSRafal Jaworowski } 311500856ebSRafal Jaworowski 312500856ebSRafal Jaworowski 313500856ebSRafal Jaworowski int dev_close_stor(void *cookie) 314500856ebSRafal Jaworowski { 315500856ebSRafal Jaworowski /* 316500856ebSRafal Jaworowski * Not much to do as we actually do not alter storage devices upon 317500856ebSRafal Jaworowski * close 318500856ebSRafal Jaworowski */ 319500856ebSRafal Jaworowski return 0; 320500856ebSRafal Jaworowski } 321500856ebSRafal Jaworowski 322500856ebSRafal Jaworowski 323500856ebSRafal Jaworowski lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start) 324500856ebSRafal Jaworowski { 325500856ebSRafal Jaworowski int type; 3264101f687SSimon Glass struct blk_desc *dd = (struct blk_desc *)cookie; 327500856ebSRafal Jaworowski 328500856ebSRafal Jaworowski if ((type = dev_stor_type(dd)) == ENUM_MAX) 329500856ebSRafal Jaworowski return 0; 330500856ebSRafal Jaworowski 331500856ebSRafal Jaworowski if (!dev_stor_is_valid(type, dd)) 332500856ebSRafal Jaworowski return 0; 333500856ebSRafal Jaworowski 334f2288c5aSfuz@fuz.su #ifdef CONFIG_BLK 335f2288c5aSfuz@fuz.su return blk_dread(dd, start, len, buf); 336f2288c5aSfuz@fuz.su #else 337500856ebSRafal Jaworowski if ((dd->block_read) == NULL) { 338f2302d44SStefan Roese debugf("no block_read() for device 0x%08x\n", cookie); 339500856ebSRafal Jaworowski return 0; 340500856ebSRafal Jaworowski } 341500856ebSRafal Jaworowski 3427c4213f6SStephen Warren return dd->block_read(dd, start, len, buf); 343f2288c5aSfuz@fuz.su #endif /* defined(CONFIG_BLK) */ 344500856ebSRafal Jaworowski } 345