183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2500856ebSRafal Jaworowski /*
3f2302d44SStefan Roese * (C) Copyright 2007-2008 Semihalf
4500856ebSRafal Jaworowski *
5500856ebSRafal Jaworowski * Written by: Rafal Jaworowski <raj@semihalf.com>
6500856ebSRafal Jaworowski */
7500856ebSRafal Jaworowski
8500856ebSRafal Jaworowski #include <config.h>
9500856ebSRafal Jaworowski #include <common.h>
10500856ebSRafal Jaworowski #include <api_public.h>
11500856ebSRafal Jaworowski
123d0ea311SMatthias Fuchs #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
133d0ea311SMatthias Fuchs #include <usb.h>
143d0ea311SMatthias Fuchs #endif
153d0ea311SMatthias Fuchs
16500856ebSRafal Jaworowski #define DEBUG
17500856ebSRafal Jaworowski #undef DEBUG
18500856ebSRafal Jaworowski
19500856ebSRafal Jaworowski #ifdef DEBUG
20500856ebSRafal Jaworowski #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
21500856ebSRafal Jaworowski #else
22500856ebSRafal Jaworowski #define debugf(fmt, args...)
23500856ebSRafal Jaworowski #endif
24500856ebSRafal Jaworowski
25500856ebSRafal Jaworowski #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
26500856ebSRafal Jaworowski
27500856ebSRafal Jaworowski
28500856ebSRafal Jaworowski #define ENUM_IDE 0
29500856ebSRafal Jaworowski #define ENUM_USB 1
30500856ebSRafal Jaworowski #define ENUM_SCSI 2
31500856ebSRafal Jaworowski #define ENUM_MMC 3
32f2302d44SStefan Roese #define ENUM_SATA 4
33f2302d44SStefan Roese #define ENUM_MAX 5
34500856ebSRafal Jaworowski
35500856ebSRafal Jaworowski struct stor_spec {
36500856ebSRafal Jaworowski int max_dev;
37500856ebSRafal Jaworowski int enum_started;
38500856ebSRafal Jaworowski int enum_ended;
39500856ebSRafal Jaworowski int type; /* "external" type: DT_STOR_{IDE,USB,etc} */
40f2302d44SStefan Roese char *name;
41500856ebSRafal Jaworowski };
42500856ebSRafal Jaworowski
435d81c6dfSEmmanuel Vadot static struct stor_spec specs[ENUM_MAX] = { { 0, 0, 0, 0, NULL }, };
44500856ebSRafal Jaworowski
456d7a5707SEmmanuel Vadot #ifndef CONFIG_SYS_MMC_MAX_DEVICE
466d7a5707SEmmanuel Vadot #define CONFIG_SYS_MMC_MAX_DEVICE 1
476d7a5707SEmmanuel Vadot #endif
48500856ebSRafal Jaworowski
dev_stor_init(void)49500856ebSRafal Jaworowski void dev_stor_init(void)
50500856ebSRafal Jaworowski {
51fc843a02SSimon Glass #if defined(CONFIG_IDE)
526d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_IDE].max_dev = CONFIG_SYS_IDE_MAXDEVICE;
53500856ebSRafal Jaworowski specs[ENUM_IDE].enum_started = 0;
54500856ebSRafal Jaworowski specs[ENUM_IDE].enum_ended = 0;
55500856ebSRafal Jaworowski specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE;
56500856ebSRafal Jaworowski specs[ENUM_IDE].name = "ide";
57500856ebSRafal Jaworowski #endif
58f2302d44SStefan Roese #if defined(CONFIG_CMD_MMC)
596d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_MMC].max_dev = CONFIG_SYS_MMC_MAX_DEVICE;
60f2302d44SStefan Roese specs[ENUM_MMC].enum_started = 0;
61f2302d44SStefan Roese specs[ENUM_MMC].enum_ended = 0;
62f2302d44SStefan Roese specs[ENUM_MMC].type = DEV_TYP_STOR | DT_STOR_MMC;
63f2302d44SStefan Roese specs[ENUM_MMC].name = "mmc";
64f2302d44SStefan Roese #endif
6510e40d54SSimon Glass #if defined(CONFIG_SATA)
666d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_SATA].max_dev = CONFIG_SYS_SATA_MAX_DEVICE;
67f2302d44SStefan Roese specs[ENUM_SATA].enum_started = 0;
68f2302d44SStefan Roese specs[ENUM_SATA].enum_ended = 0;
69f2302d44SStefan Roese specs[ENUM_SATA].type = DEV_TYP_STOR | DT_STOR_SATA;
70f2302d44SStefan Roese specs[ENUM_SATA].name = "sata";
71500856ebSRafal Jaworowski #endif
72c649e3c9SSimon Glass #if defined(CONFIG_SCSI)
736d0f6bcfSJean-Christophe PLAGNIOL-VILLARD specs[ENUM_SCSI].max_dev = CONFIG_SYS_SCSI_MAX_DEVICE;
74500856ebSRafal Jaworowski specs[ENUM_SCSI].enum_started = 0;
75500856ebSRafal Jaworowski specs[ENUM_SCSI].enum_ended = 0;
76500856ebSRafal Jaworowski specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI;
77500856ebSRafal Jaworowski specs[ENUM_SCSI].name = "scsi";
78500856ebSRafal Jaworowski #endif
79f2302d44SStefan Roese #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
80f2302d44SStefan Roese specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV;
81f2302d44SStefan Roese specs[ENUM_USB].enum_started = 0;
82f2302d44SStefan Roese specs[ENUM_USB].enum_ended = 0;
83f2302d44SStefan Roese specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB;
84f2302d44SStefan Roese specs[ENUM_USB].name = "usb";
85f2302d44SStefan Roese #endif
86500856ebSRafal Jaworowski }
87500856ebSRafal Jaworowski
88500856ebSRafal Jaworowski /*
89500856ebSRafal Jaworowski * Finds next available device in the storage group
90500856ebSRafal Jaworowski *
91500856ebSRafal Jaworowski * type: storage group type - ENUM_IDE, ENUM_SCSI etc.
92500856ebSRafal Jaworowski *
93500856ebSRafal Jaworowski * more: returns 0/1 depending if there are more devices in this group
94500856ebSRafal Jaworowski * available (for future iterations)
95500856ebSRafal Jaworowski *
96500856ebSRafal Jaworowski * returns: 0/1 depending if device found in this iteration
97500856ebSRafal Jaworowski */
dev_stor_get(int type,int * more,struct device_info * di)98d3e8f630SEmmanuel Vadot static int dev_stor_get(int type, int *more, struct device_info *di)
99500856ebSRafal Jaworowski {
1004101f687SSimon Glass struct blk_desc *dd;
101d3e8f630SEmmanuel Vadot int found = 0;
102*8efae021SCristian Ciocaltea int found_last = 0;
103d3e8f630SEmmanuel Vadot int i = 0;
104500856ebSRafal Jaworowski
1055d81c6dfSEmmanuel Vadot /* Wasn't configured for this type, return 0 directly */
1065d81c6dfSEmmanuel Vadot if (specs[type].name == NULL)
1075d81c6dfSEmmanuel Vadot return 0;
1085d81c6dfSEmmanuel Vadot
109d3e8f630SEmmanuel Vadot if (di->cookie != NULL) {
110d3e8f630SEmmanuel Vadot /* Find the last device we've returned */
111d3e8f630SEmmanuel Vadot for (i = 0; i < specs[type].max_dev; i++) {
112db1d9e78SSimon Glass if (di->cookie ==
113db1d9e78SSimon Glass (void *)blk_get_dev(specs[type].name, i)) {
114d3e8f630SEmmanuel Vadot i += 1;
115*8efae021SCristian Ciocaltea found_last = 1;
116500856ebSRafal Jaworowski break;
117500856ebSRafal Jaworowski }
118d3e8f630SEmmanuel Vadot }
119*8efae021SCristian Ciocaltea
120*8efae021SCristian Ciocaltea if (!found_last)
121*8efae021SCristian Ciocaltea i = 0;
122d3e8f630SEmmanuel Vadot }
123500856ebSRafal Jaworowski
124d3e8f630SEmmanuel Vadot for (; i < specs[type].max_dev; i++) {
125d3e8f630SEmmanuel Vadot di->cookie = (void *)blk_get_dev(specs[type].name, i);
126d3e8f630SEmmanuel Vadot
127d3e8f630SEmmanuel Vadot if (di->cookie != NULL) {
128500856ebSRafal Jaworowski found = 1;
129500856ebSRafal Jaworowski break;
130500856ebSRafal Jaworowski }
131500856ebSRafal Jaworowski }
132500856ebSRafal Jaworowski
133d3e8f630SEmmanuel Vadot if (i == specs[type].max_dev)
134d3e8f630SEmmanuel Vadot *more = 0;
135d3e8f630SEmmanuel Vadot else
136d3e8f630SEmmanuel Vadot *more = 1;
137d3e8f630SEmmanuel Vadot
138500856ebSRafal Jaworowski if (found) {
139500856ebSRafal Jaworowski di->type = specs[type].type;
140500856ebSRafal Jaworowski
1414101f687SSimon Glass dd = (struct blk_desc *)di->cookie;
142500856ebSRafal Jaworowski if (dd->type == DEV_TYPE_UNKNOWN) {
143500856ebSRafal Jaworowski debugf("device instance exists, but is not active..");
144500856ebSRafal Jaworowski found = 0;
145500856ebSRafal Jaworowski } else {
146500856ebSRafal Jaworowski di->di_stor.block_count = dd->lba;
147500856ebSRafal Jaworowski di->di_stor.block_size = dd->blksz;
148500856ebSRafal Jaworowski }
149d3e8f630SEmmanuel Vadot } else {
150500856ebSRafal Jaworowski di->cookie = NULL;
151d3e8f630SEmmanuel Vadot }
152500856ebSRafal Jaworowski
153500856ebSRafal Jaworowski return found;
154500856ebSRafal Jaworowski }
155500856ebSRafal Jaworowski
156500856ebSRafal Jaworowski
157c9db75a0SWalt Feasel /* returns: ENUM_IDE, ENUM_USB etc. based on struct blk_desc */
158c9db75a0SWalt Feasel
dev_stor_type(struct blk_desc * dd)1594101f687SSimon Glass static int dev_stor_type(struct blk_desc *dd)
160500856ebSRafal Jaworowski {
161500856ebSRafal Jaworowski int i, j;
162500856ebSRafal Jaworowski
163500856ebSRafal Jaworowski for (i = ENUM_IDE; i < ENUM_MAX; i++)
164500856ebSRafal Jaworowski for (j = 0; j < specs[i].max_dev; j++)
165db1d9e78SSimon Glass if (dd == blk_get_dev(specs[i].name, j))
166500856ebSRafal Jaworowski return i;
167500856ebSRafal Jaworowski
168500856ebSRafal Jaworowski return ENUM_MAX;
169500856ebSRafal Jaworowski }
170500856ebSRafal Jaworowski
171500856ebSRafal Jaworowski
172c9db75a0SWalt Feasel /* returns: 0/1 whether cookie points to some device in this group */
173c9db75a0SWalt Feasel
dev_is_stor(int type,struct device_info * di)174500856ebSRafal Jaworowski static int dev_is_stor(int type, struct device_info *di)
175500856ebSRafal Jaworowski {
176500856ebSRafal Jaworowski return (dev_stor_type(di->cookie) == type) ? 1 : 0;
177500856ebSRafal Jaworowski }
178500856ebSRafal Jaworowski
179500856ebSRafal Jaworowski
dev_enum_stor(int type,struct device_info * di)180500856ebSRafal Jaworowski static int dev_enum_stor(int type, struct device_info *di)
181500856ebSRafal Jaworowski {
182500856ebSRafal Jaworowski int found = 0, more = 0;
183500856ebSRafal Jaworowski
184500856ebSRafal Jaworowski debugf("called, type %d\n", type);
185500856ebSRafal Jaworowski
186500856ebSRafal Jaworowski /*
187500856ebSRafal Jaworowski * Formulae for enumerating storage devices:
188500856ebSRafal Jaworowski * 1. if cookie (hint from previous enum call) is NULL we start again
189500856ebSRafal Jaworowski * with enumeration, so return the first available device, done.
190500856ebSRafal Jaworowski *
191500856ebSRafal Jaworowski * 2. if cookie is not NULL, check if it identifies some device in
192500856ebSRafal Jaworowski * this group:
193500856ebSRafal Jaworowski *
194500856ebSRafal Jaworowski * 2a. if cookie is a storage device from our group (IDE, USB etc.),
195500856ebSRafal Jaworowski * return next available (if exists) in this group
196500856ebSRafal Jaworowski *
197500856ebSRafal Jaworowski * 2b. if it isn't device from our group, check if such devices were
198500856ebSRafal Jaworowski * ever enumerated before:
199500856ebSRafal Jaworowski * - if not, return the first available device from this group
200500856ebSRafal Jaworowski * - else return 0
201500856ebSRafal Jaworowski */
202500856ebSRafal Jaworowski
203500856ebSRafal Jaworowski if (di->cookie == NULL) {
204500856ebSRafal Jaworowski debugf("group%d - enum restart\n", type);
205500856ebSRafal Jaworowski
206500856ebSRafal Jaworowski /*
207500856ebSRafal Jaworowski * 1. Enumeration (re-)started: take the first available
208500856ebSRafal Jaworowski * device, if exists
209500856ebSRafal Jaworowski */
210d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di);
211500856ebSRafal Jaworowski specs[type].enum_started = 1;
212500856ebSRafal Jaworowski
213500856ebSRafal Jaworowski } else if (dev_is_stor(type, di)) {
214500856ebSRafal Jaworowski debugf("group%d - enum continued for the next device\n", type);
215500856ebSRafal Jaworowski
216500856ebSRafal Jaworowski if (specs[type].enum_ended) {
217500856ebSRafal Jaworowski debugf("group%d - nothing more to enum!\n", type);
218500856ebSRafal Jaworowski return 0;
219500856ebSRafal Jaworowski }
220500856ebSRafal Jaworowski
221500856ebSRafal Jaworowski /* 2a. Attempt to take a next available device in the group */
222d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di);
223500856ebSRafal Jaworowski
224500856ebSRafal Jaworowski } else {
225500856ebSRafal Jaworowski if (specs[type].enum_ended) {
226500856ebSRafal Jaworowski debugf("group %d - already enumerated, skipping\n", type);
227500856ebSRafal Jaworowski return 0;
228500856ebSRafal Jaworowski }
229500856ebSRafal Jaworowski
230500856ebSRafal Jaworowski debugf("group%d - first time enum\n", type);
231500856ebSRafal Jaworowski
232500856ebSRafal Jaworowski if (specs[type].enum_started == 0) {
233500856ebSRafal Jaworowski /*
234500856ebSRafal Jaworowski * 2b. If enumerating devices in this group did not
235500856ebSRafal Jaworowski * happen before, it means the cookie pointed to a
23683f9ecbeSWalt Feasel * device from some other group (another storage
237500856ebSRafal Jaworowski * group, or network); in this case try to take the
238500856ebSRafal Jaworowski * first available device from our group
239500856ebSRafal Jaworowski */
240500856ebSRafal Jaworowski specs[type].enum_started = 1;
241500856ebSRafal Jaworowski
242500856ebSRafal Jaworowski /*
243500856ebSRafal Jaworowski * Attempt to take the first device in this group:
244500856ebSRafal Jaworowski *'first element' flag is set
245500856ebSRafal Jaworowski */
246d3e8f630SEmmanuel Vadot found = dev_stor_get(type, &more, di);
247500856ebSRafal Jaworowski
248500856ebSRafal Jaworowski } else {
249500856ebSRafal Jaworowski errf("group%d - out of order iteration\n", type);
250500856ebSRafal Jaworowski found = 0;
251500856ebSRafal Jaworowski more = 0;
252500856ebSRafal Jaworowski }
253500856ebSRafal Jaworowski }
254500856ebSRafal Jaworowski
255500856ebSRafal Jaworowski /*
256500856ebSRafal Jaworowski * If there are no more devices in this group, consider its
257500856ebSRafal Jaworowski * enumeration finished
258500856ebSRafal Jaworowski */
259500856ebSRafal Jaworowski specs[type].enum_ended = (!more) ? 1 : 0;
260500856ebSRafal Jaworowski
261500856ebSRafal Jaworowski if (found)
262500856ebSRafal Jaworowski debugf("device found, returning cookie 0x%08x\n",
263500856ebSRafal Jaworowski (u_int32_t)di->cookie);
264500856ebSRafal Jaworowski else
265500856ebSRafal Jaworowski debugf("no device found\n");
266500856ebSRafal Jaworowski
267500856ebSRafal Jaworowski return found;
268500856ebSRafal Jaworowski }
269500856ebSRafal Jaworowski
dev_enum_reset(void)270500856ebSRafal Jaworowski void dev_enum_reset(void)
271500856ebSRafal Jaworowski {
272500856ebSRafal Jaworowski int i;
273500856ebSRafal Jaworowski
274500856ebSRafal Jaworowski for (i = 0; i < ENUM_MAX; i ++) {
275500856ebSRafal Jaworowski specs[i].enum_started = 0;
276500856ebSRafal Jaworowski specs[i].enum_ended = 0;
277500856ebSRafal Jaworowski }
278500856ebSRafal Jaworowski }
279500856ebSRafal Jaworowski
dev_enum_storage(struct device_info * di)280500856ebSRafal Jaworowski int dev_enum_storage(struct device_info *di)
281500856ebSRafal Jaworowski {
282500856ebSRafal Jaworowski int i;
283500856ebSRafal Jaworowski
284c9db75a0SWalt Feasel /* check: ide, usb, scsi, mmc */
285500856ebSRafal Jaworowski for (i = ENUM_IDE; i < ENUM_MAX; i ++) {
286500856ebSRafal Jaworowski if (dev_enum_stor(i, di))
287500856ebSRafal Jaworowski return 1;
288500856ebSRafal Jaworowski }
289500856ebSRafal Jaworowski
290500856ebSRafal Jaworowski return 0;
291500856ebSRafal Jaworowski }
292500856ebSRafal Jaworowski
dev_stor_is_valid(int type,struct blk_desc * dd)2934101f687SSimon Glass static int dev_stor_is_valid(int type, struct blk_desc *dd)
294500856ebSRafal Jaworowski {
295500856ebSRafal Jaworowski int i;
296500856ebSRafal Jaworowski
297500856ebSRafal Jaworowski for (i = 0; i < specs[type].max_dev; i++)
298db1d9e78SSimon Glass if (dd == blk_get_dev(specs[type].name, i))
299500856ebSRafal Jaworowski if (dd->type != DEV_TYPE_UNKNOWN)
300500856ebSRafal Jaworowski return 1;
301500856ebSRafal Jaworowski
302500856ebSRafal Jaworowski return 0;
303500856ebSRafal Jaworowski }
304500856ebSRafal Jaworowski
305500856ebSRafal Jaworowski
dev_open_stor(void * cookie)306500856ebSRafal Jaworowski int dev_open_stor(void *cookie)
307500856ebSRafal Jaworowski {
308500856ebSRafal Jaworowski int type = dev_stor_type(cookie);
309500856ebSRafal Jaworowski
310500856ebSRafal Jaworowski if (type == ENUM_MAX)
311500856ebSRafal Jaworowski return API_ENODEV;
312500856ebSRafal Jaworowski
3134101f687SSimon Glass if (dev_stor_is_valid(type, (struct blk_desc *)cookie))
314500856ebSRafal Jaworowski return 0;
315500856ebSRafal Jaworowski
316500856ebSRafal Jaworowski return API_ENODEV;
317500856ebSRafal Jaworowski }
318500856ebSRafal Jaworowski
319500856ebSRafal Jaworowski
dev_close_stor(void * cookie)320500856ebSRafal Jaworowski int dev_close_stor(void *cookie)
321500856ebSRafal Jaworowski {
322500856ebSRafal Jaworowski /*
323500856ebSRafal Jaworowski * Not much to do as we actually do not alter storage devices upon
324500856ebSRafal Jaworowski * close
325500856ebSRafal Jaworowski */
326500856ebSRafal Jaworowski return 0;
327500856ebSRafal Jaworowski }
328500856ebSRafal Jaworowski
329500856ebSRafal Jaworowski
dev_read_stor(void * cookie,void * buf,lbasize_t len,lbastart_t start)330500856ebSRafal Jaworowski lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start)
331500856ebSRafal Jaworowski {
332500856ebSRafal Jaworowski int type;
3334101f687SSimon Glass struct blk_desc *dd = (struct blk_desc *)cookie;
334500856ebSRafal Jaworowski
335500856ebSRafal Jaworowski if ((type = dev_stor_type(dd)) == ENUM_MAX)
336500856ebSRafal Jaworowski return 0;
337500856ebSRafal Jaworowski
338500856ebSRafal Jaworowski if (!dev_stor_is_valid(type, dd))
339500856ebSRafal Jaworowski return 0;
340500856ebSRafal Jaworowski
341f2288c5aSfuz@fuz.su #ifdef CONFIG_BLK
342f2288c5aSfuz@fuz.su return blk_dread(dd, start, len, buf);
343f2288c5aSfuz@fuz.su #else
344500856ebSRafal Jaworowski if ((dd->block_read) == NULL) {
345f2302d44SStefan Roese debugf("no block_read() for device 0x%08x\n", cookie);
346500856ebSRafal Jaworowski return 0;
347500856ebSRafal Jaworowski }
348500856ebSRafal Jaworowski
3497c4213f6SStephen Warren return dd->block_read(dd, start, len, buf);
350f2288c5aSfuz@fuz.su #endif /* defined(CONFIG_BLK) */
351500856ebSRafal Jaworowski }
352