xref: /rk3399_rockchip-uboot/drivers/block/blk-uclass.c (revision 5821df21ae36d9ef252d346a5abb76be773c5d69)
1 /*
2  * Copyright (C) 2016 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <blk.h>
10 #include <dm.h>
11 #include <dm/device-internal.h>
12 #include <dm/lists.h>
13 
14 static const char *if_typename_str[IF_TYPE_COUNT] = {
15 	[IF_TYPE_IDE]		= "ide",
16 	[IF_TYPE_SCSI]		= "scsi",
17 	[IF_TYPE_ATAPI]		= "atapi",
18 	[IF_TYPE_USB]		= "usb",
19 	[IF_TYPE_DOC]		= "doc",
20 	[IF_TYPE_MMC]		= "mmc",
21 	[IF_TYPE_SD]		= "sd",
22 	[IF_TYPE_SATA]		= "sata",
23 	[IF_TYPE_HOST]		= "host",
24 	[IF_TYPE_SYSTEMACE]	= "ace",
25 	[IF_TYPE_NVME]		= "nvme",
26 	[IF_TYPE_RKNAND]	= "rknand",
27 };
28 
29 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
30 	[IF_TYPE_IDE]		= UCLASS_IDE,
31 	[IF_TYPE_SCSI]		= UCLASS_SCSI,
32 	[IF_TYPE_ATAPI]		= UCLASS_INVALID,
33 	[IF_TYPE_USB]		= UCLASS_MASS_STORAGE,
34 	[IF_TYPE_DOC]		= UCLASS_INVALID,
35 	[IF_TYPE_MMC]		= UCLASS_MMC,
36 	[IF_TYPE_SD]		= UCLASS_INVALID,
37 	[IF_TYPE_SATA]		= UCLASS_AHCI,
38 	[IF_TYPE_HOST]		= UCLASS_ROOT,
39 	[IF_TYPE_NVME]		= UCLASS_NVME,
40 	[IF_TYPE_RKNAND]	= UCLASS_RKNAND,
41 	[IF_TYPE_SYSTEMACE]	= UCLASS_INVALID,
42 };
43 
44 static enum if_type if_typename_to_iftype(const char *if_typename)
45 {
46 	int i;
47 
48 	for (i = 0; i < IF_TYPE_COUNT; i++) {
49 		if (if_typename_str[i] &&
50 		    !strcmp(if_typename, if_typename_str[i]))
51 			return i;
52 	}
53 
54 	return IF_TYPE_UNKNOWN;
55 }
56 
57 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
58 {
59 	return if_type_uclass_id[if_type];
60 }
61 
62 const char *blk_get_if_type_name(enum if_type if_type)
63 {
64 	return if_typename_str[if_type];
65 }
66 
67 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
68 {
69 	struct blk_desc *desc;
70 	struct udevice *dev;
71 	int ret;
72 
73 	ret = blk_get_device(if_type, devnum, &dev);
74 	if (ret)
75 		return NULL;
76 	desc = dev_get_uclass_platdata(dev);
77 
78 	return desc;
79 }
80 
81 /*
82  * This function is complicated with driver model. We look up the interface
83  * name in a local table. This gives us an interface type which we can match
84  * against the uclass of the block device's parent.
85  */
86 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
87 {
88 	enum uclass_id uclass_id;
89 	enum if_type if_type;
90 	struct udevice *dev;
91 	struct uclass *uc;
92 	int ret;
93 
94 	if_type = if_typename_to_iftype(if_typename);
95 	if (if_type == IF_TYPE_UNKNOWN) {
96 		debug("%s: Unknown interface type '%s'\n", __func__,
97 		      if_typename);
98 		return NULL;
99 	}
100 	uclass_id = if_type_to_uclass_id(if_type);
101 	if (uclass_id == UCLASS_INVALID) {
102 		debug("%s: Unknown uclass for interface type'\n",
103 		      if_typename_str[if_type]);
104 		return NULL;
105 	}
106 
107 	ret = uclass_get(UCLASS_BLK, &uc);
108 	if (ret)
109 		return NULL;
110 	uclass_foreach_dev(dev, uc) {
111 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
112 
113 		debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
114 		      if_type, devnum, dev->name, desc->if_type, desc->devnum);
115 		if (desc->devnum != devnum)
116 			continue;
117 
118 		/* Find out the parent device uclass */
119 		if (device_get_uclass_id(dev->parent) != uclass_id) {
120 			debug("%s: parent uclass %d, this dev %d\n", __func__,
121 			      device_get_uclass_id(dev->parent), uclass_id);
122 			continue;
123 		}
124 
125 		if (device_probe(dev))
126 			return NULL;
127 
128 		debug("%s: Device desc %p\n", __func__, desc);
129 		return desc;
130 	}
131 	debug("%s: No device found\n", __func__);
132 
133 	return NULL;
134 }
135 
136 /**
137  * get_desc() - Get the block device descriptor for the given device number
138  *
139  * @if_type:	Interface type
140  * @devnum:	Device number (0 = first)
141  * @descp:	Returns block device descriptor on success
142  * @return 0 on success, -ENODEV if there is no such device and no device
143  * with a higher device number, -ENOENT if there is no such device but there
144  * is one with a higher number, or other -ve on other error.
145  */
146 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
147 {
148 	bool found_more = false;
149 	struct udevice *dev;
150 	struct uclass *uc;
151 	int ret;
152 
153 	*descp = NULL;
154 	ret = uclass_get(UCLASS_BLK, &uc);
155 	if (ret)
156 		return ret;
157 	uclass_foreach_dev(dev, uc) {
158 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
159 
160 		debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
161 		      if_type, devnum, dev->name, desc->if_type, desc->devnum);
162 		if (desc->if_type == if_type) {
163 			if (desc->devnum == devnum) {
164 				ret = device_probe(dev);
165 				if (ret)
166 					return ret;
167 
168 				*descp = desc;
169 				return 0;
170 			} else if (desc->devnum > devnum) {
171 				found_more = true;
172 			}
173 		}
174 	}
175 
176 	return found_more ? -ENOENT : -ENODEV;
177 }
178 
179 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
180 {
181 	struct udevice *dev;
182 	int ret;
183 
184 	ret = blk_get_device(if_type, devnum, &dev);
185 	if (ret)
186 		return ret;
187 
188 	return blk_select_hwpart(dev, hwpart);
189 }
190 
191 int blk_list_part(enum if_type if_type)
192 {
193 	struct blk_desc *desc;
194 	int devnum, ok;
195 	int ret;
196 
197 	for (ok = 0, devnum = 0;; ++devnum) {
198 		ret = get_desc(if_type, devnum, &desc);
199 		if (ret == -ENODEV)
200 			break;
201 		else if (ret)
202 			continue;
203 		if (desc->part_type != PART_TYPE_UNKNOWN) {
204 			++ok;
205 			if (devnum)
206 				putc('\n');
207 			part_print(desc);
208 		}
209 	}
210 	if (!ok)
211 		return -ENODEV;
212 
213 	return 0;
214 }
215 
216 int blk_print_part_devnum(enum if_type if_type, int devnum)
217 {
218 	struct blk_desc *desc;
219 	int ret;
220 
221 	ret = get_desc(if_type, devnum, &desc);
222 	if (ret)
223 		return ret;
224 	if (desc->type == DEV_TYPE_UNKNOWN)
225 		return -ENOENT;
226 	part_print(desc);
227 
228 	return 0;
229 }
230 
231 void blk_list_devices(enum if_type if_type)
232 {
233 	struct blk_desc *desc;
234 	int ret;
235 	int i;
236 
237 	for (i = 0;; ++i) {
238 		ret = get_desc(if_type, i, &desc);
239 		if (ret == -ENODEV)
240 			break;
241 		else if (ret)
242 			continue;
243 		if (desc->type == DEV_TYPE_UNKNOWN)
244 			continue;  /* list only known devices */
245 		printf("Device %d: ", i);
246 		dev_print(desc);
247 	}
248 }
249 
250 int blk_print_device_num(enum if_type if_type, int devnum)
251 {
252 	struct blk_desc *desc;
253 	int ret;
254 
255 	ret = get_desc(if_type, devnum, &desc);
256 	if (ret)
257 		return ret;
258 	printf("\nIDE device %d: ", devnum);
259 	dev_print(desc);
260 
261 	return 0;
262 }
263 
264 int blk_show_device(enum if_type if_type, int devnum)
265 {
266 	struct blk_desc *desc;
267 	int ret;
268 
269 	printf("\nDevice %d: ", devnum);
270 	ret = get_desc(if_type, devnum, &desc);
271 	if (ret == -ENODEV || ret == -ENOENT) {
272 		printf("unknown device\n");
273 		return -ENODEV;
274 	}
275 	if (ret)
276 		return ret;
277 	dev_print(desc);
278 
279 	if (desc->type == DEV_TYPE_UNKNOWN)
280 		return -ENOENT;
281 
282 	return 0;
283 }
284 
285 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
286 		      lbaint_t blkcnt, void *buffer)
287 {
288 	struct blk_desc *desc;
289 	ulong n;
290 	int ret;
291 
292 	ret = get_desc(if_type, devnum, &desc);
293 	if (ret)
294 		return ret;
295 	n = blk_dread(desc, start, blkcnt, buffer);
296 	if (IS_ERR_VALUE(n))
297 		return n;
298 
299 	return n;
300 }
301 
302 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
303 		       lbaint_t blkcnt, const void *buffer)
304 {
305 	struct blk_desc *desc;
306 	int ret;
307 
308 	ret = get_desc(if_type, devnum, &desc);
309 	if (ret)
310 		return ret;
311 	return blk_dwrite(desc, start, blkcnt, buffer);
312 }
313 
314 int blk_select_hwpart(struct udevice *dev, int hwpart)
315 {
316 	const struct blk_ops *ops = blk_get_ops(dev);
317 
318 	if (!ops)
319 		return -ENOSYS;
320 	if (!ops->select_hwpart)
321 		return 0;
322 
323 	return ops->select_hwpart(dev, hwpart);
324 }
325 
326 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
327 {
328 	return blk_select_hwpart(desc->bdev, hwpart);
329 }
330 
331 int blk_first_device(int if_type, struct udevice **devp)
332 {
333 	struct blk_desc *desc;
334 	int ret;
335 
336 	ret = uclass_first_device(UCLASS_BLK, devp);
337 	if (ret)
338 		return ret;
339 	if (!*devp)
340 		return -ENODEV;
341 	do {
342 		desc = dev_get_uclass_platdata(*devp);
343 		if (desc->if_type == if_type)
344 			return 0;
345 		ret = uclass_next_device(devp);
346 		if (ret)
347 			return ret;
348 	} while (*devp);
349 
350 	return -ENODEV;
351 }
352 
353 int blk_next_device(struct udevice **devp)
354 {
355 	struct blk_desc *desc;
356 	int ret, if_type;
357 
358 	desc = dev_get_uclass_platdata(*devp);
359 	if_type = desc->if_type;
360 	do {
361 		ret = uclass_next_device(devp);
362 		if (ret)
363 			return ret;
364 		if (!*devp)
365 			return -ENODEV;
366 		desc = dev_get_uclass_platdata(*devp);
367 		if (desc->if_type == if_type)
368 			return 0;
369 	} while (1);
370 }
371 
372 int blk_find_device(int if_type, int devnum, struct udevice **devp)
373 {
374 	struct uclass *uc;
375 	struct udevice *dev;
376 	int ret;
377 
378 	ret = uclass_get(UCLASS_BLK, &uc);
379 	if (ret)
380 		return ret;
381 	uclass_foreach_dev(dev, uc) {
382 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
383 
384 		debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
385 		      if_type, devnum, dev->name, desc->if_type, desc->devnum);
386 		if (desc->if_type == if_type && desc->devnum == devnum) {
387 			*devp = dev;
388 			return 0;
389 		}
390 	}
391 
392 	return -ENODEV;
393 }
394 
395 int blk_get_device(int if_type, int devnum, struct udevice **devp)
396 {
397 	int ret;
398 
399 	ret = blk_find_device(if_type, devnum, devp);
400 	if (ret)
401 		return ret;
402 
403 	return device_probe(*devp);
404 }
405 
406 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
407 			lbaint_t blkcnt, void *buffer)
408 {
409 	struct udevice *dev = block_dev->bdev;
410 	const struct blk_ops *ops = blk_get_ops(dev);
411 	ulong blks_read;
412 
413 	if (!ops->read)
414 		return -ENOSYS;
415 
416 	if (blkcache_read(block_dev->if_type, block_dev->devnum,
417 			  start, blkcnt, block_dev->blksz, buffer))
418 		return blkcnt;
419 	blks_read = ops->read(dev, start, blkcnt, buffer);
420 	if (blks_read == blkcnt)
421 		blkcache_fill(block_dev->if_type, block_dev->devnum,
422 			      start, blkcnt, block_dev->blksz, buffer);
423 
424 	return blks_read;
425 }
426 
427 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
428 			 lbaint_t blkcnt, const void *buffer)
429 {
430 	struct udevice *dev = block_dev->bdev;
431 	const struct blk_ops *ops = blk_get_ops(dev);
432 
433 	if (!ops->write)
434 		return -ENOSYS;
435 
436 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
437 	return ops->write(dev, start, blkcnt, buffer);
438 }
439 
440 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
441 			 lbaint_t blkcnt)
442 {
443 	struct udevice *dev = block_dev->bdev;
444 	const struct blk_ops *ops = blk_get_ops(dev);
445 
446 	if (!ops->erase)
447 		return -ENOSYS;
448 
449 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
450 	return ops->erase(dev, start, blkcnt);
451 }
452 
453 int blk_prepare_device(struct udevice *dev)
454 {
455 	struct blk_desc *desc = dev_get_uclass_platdata(dev);
456 
457 	part_init(desc);
458 
459 	return 0;
460 }
461 
462 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
463 {
464 	struct udevice *dev;
465 	enum uclass_id id;
466 	int ret;
467 
468 	device_find_first_child(parent, &dev);
469 	if (!dev) {
470 		debug("%s: No block device found for parent '%s'\n", __func__,
471 		      parent->name);
472 		return -ENODEV;
473 	}
474 	id = device_get_uclass_id(dev);
475 	if (id != UCLASS_BLK) {
476 		debug("%s: Incorrect uclass %s for block device '%s'\n",
477 		      __func__, uclass_get_name(id), dev->name);
478 		return -ENOTBLK;
479 	}
480 	ret = device_probe(dev);
481 	if (ret)
482 		return ret;
483 	*devp = dev;
484 
485 	return 0;
486 }
487 
488 int blk_find_max_devnum(enum if_type if_type)
489 {
490 	struct udevice *dev;
491 	int max_devnum = -ENODEV;
492 	struct uclass *uc;
493 	int ret;
494 
495 	ret = uclass_get(UCLASS_BLK, &uc);
496 	if (ret)
497 		return ret;
498 	uclass_foreach_dev(dev, uc) {
499 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
500 
501 		if (desc->if_type == if_type && desc->devnum > max_devnum)
502 			max_devnum = desc->devnum;
503 	}
504 
505 	return max_devnum;
506 }
507 
508 static int blk_next_free_devnum(enum if_type if_type)
509 {
510 	int ret;
511 
512 	ret = blk_find_max_devnum(if_type);
513 	if (ret == -ENODEV)
514 		return 0;
515 	if (ret < 0)
516 		return ret;
517 
518 	return ret + 1;
519 }
520 
521 static int blk_claim_devnum(enum if_type if_type, int devnum)
522 {
523 	struct udevice *dev;
524 	struct uclass *uc;
525 	int ret;
526 
527 	ret = uclass_get(UCLASS_BLK, &uc);
528 	if (ret)
529 		return ret;
530 	uclass_foreach_dev(dev, uc) {
531 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
532 
533 		if (desc->if_type == if_type && desc->devnum == devnum) {
534 			int next = blk_next_free_devnum(if_type);
535 
536 			if (next < 0)
537 				return next;
538 			desc->devnum = next;
539 			return 0;
540 		}
541 	}
542 
543 	return -ENOENT;
544 }
545 
546 int blk_create_device(struct udevice *parent, const char *drv_name,
547 		      const char *name, int if_type, int devnum, int blksz,
548 		      lbaint_t size, struct udevice **devp)
549 {
550 	struct blk_desc *desc;
551 	struct udevice *dev;
552 	int ret;
553 
554 	if (devnum == -1) {
555 		devnum = blk_next_free_devnum(if_type);
556 	} else {
557 		ret = blk_claim_devnum(if_type, devnum);
558 		if (ret < 0 && ret != -ENOENT)
559 			return ret;
560 	}
561 	if (devnum < 0)
562 		return devnum;
563 	ret = device_bind_driver(parent, drv_name, name, &dev);
564 	if (ret)
565 		return ret;
566 	desc = dev_get_uclass_platdata(dev);
567 	desc->if_type = if_type;
568 	desc->blksz = blksz;
569 	desc->lba = size / blksz;
570 	desc->part_type = PART_TYPE_UNKNOWN;
571 	desc->bdev = dev;
572 	desc->devnum = devnum;
573 	*devp = dev;
574 
575 	return 0;
576 }
577 
578 int blk_create_devicef(struct udevice *parent, const char *drv_name,
579 		       const char *name, int if_type, int devnum, int blksz,
580 		       lbaint_t size, struct udevice **devp)
581 {
582 	char dev_name[30], *str;
583 	int ret;
584 
585 	snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
586 	str = strdup(dev_name);
587 	if (!str)
588 		return -ENOMEM;
589 
590 	ret = blk_create_device(parent, drv_name, str, if_type, devnum,
591 				blksz, size, devp);
592 	if (ret) {
593 		free(str);
594 		return ret;
595 	}
596 	device_set_name_alloced(*devp);
597 
598 	return 0;
599 }
600 
601 int blk_unbind_all(int if_type)
602 {
603 	struct uclass *uc;
604 	struct udevice *dev, *next;
605 	int ret;
606 
607 	ret = uclass_get(UCLASS_BLK, &uc);
608 	if (ret)
609 		return ret;
610 	uclass_foreach_dev_safe(dev, next, uc) {
611 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
612 
613 		if (desc->if_type == if_type) {
614 			ret = device_remove(dev, DM_REMOVE_NORMAL);
615 			if (ret)
616 				return ret;
617 			ret = device_unbind(dev);
618 			if (ret)
619 				return ret;
620 		}
621 	}
622 
623 	return 0;
624 }
625 
626 UCLASS_DRIVER(blk) = {
627 	.id		= UCLASS_BLK,
628 	.name		= "blk",
629 	.per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),
630 };
631