xref: /openbmc/u-boot/drivers/core/uclass.c (revision 23ff8633)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Pavel Herrmann <morpheus.ibis@gmail.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #include <malloc.h>
13 #include <dm/device.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
18 #include <dm/util.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 struct uclass *uclass_find(enum uclass_id key)
23 {
24 	struct uclass *uc;
25 
26 	if (!gd->dm_root)
27 		return NULL;
28 	/*
29 	 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
30 	 * node to the start of the list, or creating a linear array mapping
31 	 * id to node.
32 	 */
33 	list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
34 		if (uc->uc_drv->id == key)
35 			return uc;
36 	}
37 
38 	return NULL;
39 }
40 
41 /**
42  * uclass_add() - Create new uclass in list
43  * @id: Id number to create
44  * @ucp: Returns pointer to uclass, or NULL on error
45  * @return 0 on success, -ve on error
46  *
47  * The new uclass is added to the list. There must be only one uclass for
48  * each id.
49  */
50 static int uclass_add(enum uclass_id id, struct uclass **ucp)
51 {
52 	struct uclass_driver *uc_drv;
53 	struct uclass *uc;
54 	int ret;
55 
56 	*ucp = NULL;
57 	uc_drv = lists_uclass_lookup(id);
58 	if (!uc_drv) {
59 		debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 		      id);
61 		/*
62 		 * Use a strange error to make this case easier to find. When
63 		 * a uclass is not available it can prevent driver model from
64 		 * starting up and this failure is otherwise hard to debug.
65 		 */
66 		return -EPFNOSUPPORT;
67 	}
68 	uc = calloc(1, sizeof(*uc));
69 	if (!uc)
70 		return -ENOMEM;
71 	if (uc_drv->priv_auto_alloc_size) {
72 		uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
73 		if (!uc->priv) {
74 			ret = -ENOMEM;
75 			goto fail_mem;
76 		}
77 	}
78 	uc->uc_drv = uc_drv;
79 	INIT_LIST_HEAD(&uc->sibling_node);
80 	INIT_LIST_HEAD(&uc->dev_head);
81 	list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
82 
83 	if (uc_drv->init) {
84 		ret = uc_drv->init(uc);
85 		if (ret)
86 			goto fail;
87 	}
88 
89 	*ucp = uc;
90 
91 	return 0;
92 fail:
93 	if (uc_drv->priv_auto_alloc_size) {
94 		free(uc->priv);
95 		uc->priv = NULL;
96 	}
97 	list_del(&uc->sibling_node);
98 fail_mem:
99 	free(uc);
100 
101 	return ret;
102 }
103 
104 int uclass_destroy(struct uclass *uc)
105 {
106 	struct uclass_driver *uc_drv;
107 	struct udevice *dev;
108 	int ret;
109 
110 	/*
111 	 * We cannot use list_for_each_entry_safe() here. If a device in this
112 	 * uclass has a child device also in this uclass, it will be also be
113 	 * unbound (by the recursion in the call to device_unbind() below).
114 	 * We can loop until the list is empty.
115 	 */
116 	while (!list_empty(&uc->dev_head)) {
117 		dev = list_first_entry(&uc->dev_head, struct udevice,
118 				       uclass_node);
119 		ret = device_remove(dev);
120 		if (ret)
121 			return ret;
122 		ret = device_unbind(dev);
123 		if (ret)
124 			return ret;
125 	}
126 
127 	uc_drv = uc->uc_drv;
128 	if (uc_drv->destroy)
129 		uc_drv->destroy(uc);
130 	list_del(&uc->sibling_node);
131 	if (uc_drv->priv_auto_alloc_size)
132 		free(uc->priv);
133 	free(uc);
134 
135 	return 0;
136 }
137 
138 int uclass_get(enum uclass_id id, struct uclass **ucp)
139 {
140 	struct uclass *uc;
141 
142 	*ucp = NULL;
143 	uc = uclass_find(id);
144 	if (!uc)
145 		return uclass_add(id, ucp);
146 	*ucp = uc;
147 
148 	return 0;
149 }
150 
151 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
152 {
153 	struct uclass *uc;
154 	struct udevice *dev;
155 	int ret;
156 
157 	*devp = NULL;
158 	ret = uclass_get(id, &uc);
159 	if (ret)
160 		return ret;
161 	if (list_empty(&uc->dev_head))
162 		return -ENODEV;
163 
164 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
165 		if (!index--) {
166 			*devp = dev;
167 			return 0;
168 		}
169 	}
170 
171 	return -ENODEV;
172 }
173 
174 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
175 {
176 	struct uclass *uc;
177 	int ret;
178 
179 	*devp = NULL;
180 	ret = uclass_get(id, &uc);
181 	if (ret)
182 		return ret;
183 	if (list_empty(&uc->dev_head))
184 		return 0;
185 
186 	*devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
187 
188 	return 0;
189 }
190 
191 int uclass_find_next_device(struct udevice **devp)
192 {
193 	struct udevice *dev = *devp;
194 
195 	*devp = NULL;
196 	if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
197 		return 0;
198 
199 	*devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
200 
201 	return 0;
202 }
203 
204 int uclass_find_device_by_name(enum uclass_id id, const char *name,
205 			       struct udevice **devp)
206 {
207 	struct uclass *uc;
208 	struct udevice *dev;
209 	int ret;
210 
211 	*devp = NULL;
212 	if (!name)
213 		return -EINVAL;
214 	ret = uclass_get(id, &uc);
215 	if (ret)
216 		return ret;
217 
218 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
219 		if (!strncmp(dev->name, name, strlen(name))) {
220 			*devp = dev;
221 			return 0;
222 		}
223 	}
224 
225 	return -ENODEV;
226 }
227 
228 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
229 			      bool find_req_seq, struct udevice **devp)
230 {
231 	struct uclass *uc;
232 	struct udevice *dev;
233 	int ret;
234 
235 	*devp = NULL;
236 	debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
237 	if (seq_or_req_seq == -1)
238 		return -ENODEV;
239 	ret = uclass_get(id, &uc);
240 	if (ret)
241 		return ret;
242 
243 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
244 		debug("   - %d %d\n", dev->req_seq, dev->seq);
245 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
246 				seq_or_req_seq) {
247 			*devp = dev;
248 			debug("   - found\n");
249 			return 0;
250 		}
251 	}
252 	debug("   - not found\n");
253 
254 	return -ENODEV;
255 }
256 
257 static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
258 					   struct udevice **devp)
259 {
260 	struct uclass *uc;
261 	struct udevice *dev;
262 	int ret;
263 
264 	*devp = NULL;
265 	if (node < 0)
266 		return -ENODEV;
267 	ret = uclass_get(id, &uc);
268 	if (ret)
269 		return ret;
270 
271 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
272 		if (dev->of_offset == node) {
273 			*devp = dev;
274 			return 0;
275 		}
276 	}
277 
278 	return -ENODEV;
279 }
280 
281 static int uclass_find_device_by_phandle(enum uclass_id id,
282 					 struct udevice *parent,
283 					 const char *name,
284 					 struct udevice **devp)
285 {
286 	struct udevice *dev;
287 	struct uclass *uc;
288 	int find_phandle;
289 	int ret;
290 
291 	*devp = NULL;
292 	find_phandle = fdtdec_get_int(gd->fdt_blob, parent->of_offset, name,
293 				      -1);
294 	if (find_phandle <= 0)
295 		return -ENOENT;
296 	ret = uclass_get(id, &uc);
297 	if (ret)
298 		return ret;
299 
300 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
301 		uint phandle = fdt_get_phandle(gd->fdt_blob, dev->of_offset);
302 
303 		if (phandle == find_phandle) {
304 			*devp = dev;
305 			return 0;
306 		}
307 	}
308 
309 	return -ENODEV;
310 }
311 
312 int uclass_get_device_tail(struct udevice *dev, int ret,
313 				  struct udevice **devp)
314 {
315 	if (ret)
316 		return ret;
317 
318 	assert(dev);
319 	ret = device_probe(dev);
320 	if (ret)
321 		return ret;
322 
323 	*devp = dev;
324 
325 	return 0;
326 }
327 
328 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
329 {
330 	struct udevice *dev;
331 	int ret;
332 
333 	*devp = NULL;
334 	ret = uclass_find_device(id, index, &dev);
335 	return uclass_get_device_tail(dev, ret, devp);
336 }
337 
338 int uclass_get_device_by_name(enum uclass_id id, const char *name,
339 			      struct udevice **devp)
340 {
341 	struct udevice *dev;
342 	int ret;
343 
344 	*devp = NULL;
345 	ret = uclass_find_device_by_name(id, name, &dev);
346 	return uclass_get_device_tail(dev, ret, devp);
347 }
348 
349 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
350 {
351 	struct udevice *dev;
352 	int ret;
353 
354 	*devp = NULL;
355 	ret = uclass_find_device_by_seq(id, seq, false, &dev);
356 	if (ret == -ENODEV) {
357 		/*
358 		 * We didn't find it in probed devices. See if there is one
359 		 * that will request this seq if probed.
360 		 */
361 		ret = uclass_find_device_by_seq(id, seq, true, &dev);
362 	}
363 	return uclass_get_device_tail(dev, ret, devp);
364 }
365 
366 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
367 				   struct udevice **devp)
368 {
369 	struct udevice *dev;
370 	int ret;
371 
372 	*devp = NULL;
373 	ret = uclass_find_device_by_of_offset(id, node, &dev);
374 	return uclass_get_device_tail(dev, ret, devp);
375 }
376 
377 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
378 				 const char *name, struct udevice **devp)
379 {
380 	struct udevice *dev;
381 	int ret;
382 
383 	*devp = NULL;
384 	ret = uclass_find_device_by_phandle(id, parent, name, &dev);
385 	return uclass_get_device_tail(dev, ret, devp);
386 }
387 
388 int uclass_first_device(enum uclass_id id, struct udevice **devp)
389 {
390 	struct udevice *dev;
391 	int ret;
392 
393 	*devp = NULL;
394 	ret = uclass_find_first_device(id, &dev);
395 	if (!dev)
396 		return 0;
397 	return uclass_get_device_tail(dev, ret, devp);
398 }
399 
400 int uclass_next_device(struct udevice **devp)
401 {
402 	struct udevice *dev = *devp;
403 	int ret;
404 
405 	*devp = NULL;
406 	ret = uclass_find_next_device(&dev);
407 	if (!dev)
408 		return 0;
409 	return uclass_get_device_tail(dev, ret, devp);
410 }
411 
412 int uclass_bind_device(struct udevice *dev)
413 {
414 	struct uclass *uc;
415 	int ret;
416 
417 	uc = dev->uclass;
418 	list_add_tail(&dev->uclass_node, &uc->dev_head);
419 
420 	if (dev->parent) {
421 		struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
422 
423 		if (uc_drv->child_post_bind) {
424 			ret = uc_drv->child_post_bind(dev);
425 			if (ret)
426 				goto err;
427 		}
428 	}
429 	if (uc->uc_drv->post_bind) {
430 		ret = uc->uc_drv->post_bind(dev);
431 		if (ret)
432 			goto err;
433 	}
434 
435 	return 0;
436 err:
437 	/* There is no need to undo the parent's post_bind call */
438 	list_del(&dev->uclass_node);
439 
440 	return ret;
441 }
442 
443 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
444 int uclass_unbind_device(struct udevice *dev)
445 {
446 	struct uclass *uc;
447 	int ret;
448 
449 	uc = dev->uclass;
450 	if (uc->uc_drv->pre_unbind) {
451 		ret = uc->uc_drv->pre_unbind(dev);
452 		if (ret)
453 			return ret;
454 	}
455 
456 	list_del(&dev->uclass_node);
457 	return 0;
458 }
459 #endif
460 
461 int uclass_resolve_seq(struct udevice *dev)
462 {
463 	struct udevice *dup;
464 	int seq;
465 	int ret;
466 
467 	assert(dev->seq == -1);
468 	ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
469 					false, &dup);
470 	if (!ret) {
471 		dm_warn("Device '%s': seq %d is in use by '%s'\n",
472 			dev->name, dev->req_seq, dup->name);
473 	} else if (ret == -ENODEV) {
474 		/* Our requested sequence number is available */
475 		if (dev->req_seq != -1)
476 			return dev->req_seq;
477 	} else {
478 		return ret;
479 	}
480 
481 	for (seq = 0; seq < DM_MAX_SEQ; seq++) {
482 		ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
483 						false, &dup);
484 		if (ret == -ENODEV)
485 			break;
486 		if (ret)
487 			return ret;
488 	}
489 	return seq;
490 }
491 
492 int uclass_pre_probe_device(struct udevice *dev)
493 {
494 	struct uclass_driver *uc_drv;
495 	int ret;
496 
497 	uc_drv = dev->uclass->uc_drv;
498 	if (uc_drv->pre_probe) {
499 		ret = uc_drv->pre_probe(dev);
500 		if (ret)
501 			return ret;
502 	}
503 
504 	if (!dev->parent)
505 		return 0;
506 	uc_drv = dev->parent->uclass->uc_drv;
507 	if (uc_drv->child_pre_probe)
508 		return uc_drv->child_pre_probe(dev);
509 
510 	return 0;
511 }
512 
513 int uclass_post_probe_device(struct udevice *dev)
514 {
515 	struct uclass_driver *uc_drv = dev->uclass->uc_drv;
516 
517 	if (uc_drv->post_probe)
518 		return uc_drv->post_probe(dev);
519 
520 	return 0;
521 }
522 
523 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
524 int uclass_pre_remove_device(struct udevice *dev)
525 {
526 	struct uclass *uc;
527 	int ret;
528 
529 	uc = dev->uclass;
530 	if (uc->uc_drv->pre_remove) {
531 		ret = uc->uc_drv->pre_remove(dev);
532 		if (ret)
533 			return ret;
534 	}
535 
536 	return 0;
537 }
538 #endif
539