1 /* 2 * Device manager 3 * 4 * Copyright (c) 2013 Google, Inc 5 * 6 * (C) Copyright 2012 7 * Pavel Herrmann <morpheus.ibis@gmail.com> 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include <common.h> 13 #include <fdtdec.h> 14 #include <malloc.h> 15 #include <dm/device.h> 16 #include <dm/device-internal.h> 17 #include <dm/lists.h> 18 #include <dm/platdata.h> 19 #include <dm/uclass.h> 20 #include <dm/uclass-internal.h> 21 #include <dm/util.h> 22 #include <linux/err.h> 23 #include <linux/list.h> 24 25 DECLARE_GLOBAL_DATA_PTR; 26 27 int device_bind(struct udevice *parent, const struct driver *drv, 28 const char *name, void *platdata, int of_offset, 29 struct udevice **devp) 30 { 31 struct udevice *dev; 32 struct uclass *uc; 33 int size, ret = 0; 34 35 *devp = NULL; 36 if (!name) 37 return -EINVAL; 38 39 ret = uclass_get(drv->id, &uc); 40 if (ret) 41 return ret; 42 43 dev = calloc(1, sizeof(struct udevice)); 44 if (!dev) 45 return -ENOMEM; 46 47 INIT_LIST_HEAD(&dev->sibling_node); 48 INIT_LIST_HEAD(&dev->child_head); 49 INIT_LIST_HEAD(&dev->uclass_node); 50 dev->platdata = platdata; 51 dev->name = name; 52 dev->of_offset = of_offset; 53 dev->parent = parent; 54 dev->driver = drv; 55 dev->uclass = uc; 56 57 dev->seq = -1; 58 dev->req_seq = -1; 59 if (IS_ENABLED(CONFIG_OF_CONTROL) && IS_ENABLED(CONFIG_DM_SEQ_ALIAS)) { 60 /* 61 * Some devices, such as a SPI bus, I2C bus and serial ports 62 * are numbered using aliases. 63 * 64 * This is just a 'requested' sequence, and will be 65 * resolved (and ->seq updated) when the device is probed. 66 */ 67 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) { 68 if (uc->uc_drv->name && of_offset != -1) { 69 fdtdec_get_alias_seq(gd->fdt_blob, 70 uc->uc_drv->name, of_offset, 71 &dev->req_seq); 72 } 73 } 74 } 75 76 if (!dev->platdata && drv->platdata_auto_alloc_size) { 77 dev->flags |= DM_FLAG_ALLOC_PDATA; 78 dev->platdata = calloc(1, drv->platdata_auto_alloc_size); 79 if (!dev->platdata) { 80 ret = -ENOMEM; 81 goto fail_alloc1; 82 } 83 } 84 85 size = uc->uc_drv->per_device_platdata_auto_alloc_size; 86 if (size) { 87 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; 88 dev->uclass_platdata = calloc(1, size); 89 if (!dev->uclass_platdata) { 90 ret = -ENOMEM; 91 goto fail_alloc2; 92 } 93 } 94 95 if (parent) { 96 size = parent->driver->per_child_platdata_auto_alloc_size; 97 if (!size) { 98 size = parent->uclass->uc_drv-> 99 per_child_platdata_auto_alloc_size; 100 } 101 if (size) { 102 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; 103 dev->parent_platdata = calloc(1, size); 104 if (!dev->parent_platdata) { 105 ret = -ENOMEM; 106 goto fail_alloc3; 107 } 108 } 109 } 110 111 /* put dev into parent's successor list */ 112 if (parent) 113 list_add_tail(&dev->sibling_node, &parent->child_head); 114 115 ret = uclass_bind_device(dev); 116 if (ret) 117 goto fail_uclass_bind; 118 119 /* if we fail to bind we remove device from successors and free it */ 120 if (drv->bind) { 121 ret = drv->bind(dev); 122 if (ret) 123 goto fail_bind; 124 } 125 if (parent && parent->driver->child_post_bind) { 126 ret = parent->driver->child_post_bind(dev); 127 if (ret) 128 goto fail_child_post_bind; 129 } 130 131 if (parent) 132 dm_dbg("Bound device %s to %s\n", dev->name, parent->name); 133 *devp = dev; 134 135 return 0; 136 137 fail_child_post_bind: 138 if (drv->unbind && drv->unbind(dev)) { 139 dm_warn("unbind() method failed on dev '%s' on error path\n", 140 dev->name); 141 } 142 143 fail_bind: 144 if (uclass_unbind_device(dev)) { 145 dm_warn("Failed to unbind dev '%s' on error path\n", 146 dev->name); 147 } 148 fail_uclass_bind: 149 list_del(&dev->sibling_node); 150 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { 151 free(dev->parent_platdata); 152 dev->parent_platdata = NULL; 153 } 154 fail_alloc3: 155 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { 156 free(dev->uclass_platdata); 157 dev->uclass_platdata = NULL; 158 } 159 fail_alloc2: 160 if (dev->flags & DM_FLAG_ALLOC_PDATA) { 161 free(dev->platdata); 162 dev->platdata = NULL; 163 } 164 fail_alloc1: 165 free(dev); 166 167 return ret; 168 } 169 170 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, 171 const struct driver_info *info, struct udevice **devp) 172 { 173 struct driver *drv; 174 175 drv = lists_driver_lookup_name(info->name); 176 if (!drv) 177 return -ENOENT; 178 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) 179 return -EPERM; 180 181 return device_bind(parent, drv, info->name, (void *)info->platdata, 182 -1, devp); 183 } 184 185 static void *alloc_priv(int size, uint flags) 186 { 187 void *priv; 188 189 if (flags & DM_FLAG_ALLOC_PRIV_DMA) { 190 priv = memalign(ARCH_DMA_MINALIGN, size); 191 if (priv) 192 memset(priv, '\0', size); 193 } else { 194 priv = calloc(1, size); 195 } 196 197 return priv; 198 } 199 200 int device_probe_child(struct udevice *dev, void *parent_priv) 201 { 202 const struct driver *drv; 203 int size = 0; 204 int ret; 205 int seq; 206 207 if (!dev) 208 return -EINVAL; 209 210 if (dev->flags & DM_FLAG_ACTIVATED) 211 return 0; 212 213 drv = dev->driver; 214 assert(drv); 215 216 /* Allocate private data if requested */ 217 if (drv->priv_auto_alloc_size) { 218 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags); 219 if (!dev->priv) { 220 ret = -ENOMEM; 221 goto fail; 222 } 223 } 224 /* Allocate private data if requested */ 225 size = dev->uclass->uc_drv->per_device_auto_alloc_size; 226 if (size) { 227 dev->uclass_priv = calloc(1, size); 228 if (!dev->uclass_priv) { 229 ret = -ENOMEM; 230 goto fail; 231 } 232 } 233 234 /* Ensure all parents are probed */ 235 if (dev->parent) { 236 size = dev->parent->driver->per_child_auto_alloc_size; 237 if (!size) { 238 size = dev->parent->uclass->uc_drv-> 239 per_child_auto_alloc_size; 240 } 241 if (size) { 242 dev->parent_priv = alloc_priv(size, drv->flags); 243 if (!dev->parent_priv) { 244 ret = -ENOMEM; 245 goto fail; 246 } 247 if (parent_priv) 248 memcpy(dev->parent_priv, parent_priv, size); 249 } 250 251 ret = device_probe(dev->parent); 252 if (ret) 253 goto fail; 254 } 255 256 seq = uclass_resolve_seq(dev); 257 if (seq < 0) { 258 ret = seq; 259 goto fail; 260 } 261 dev->seq = seq; 262 263 dev->flags |= DM_FLAG_ACTIVATED; 264 265 ret = uclass_pre_probe_device(dev); 266 if (ret) 267 goto fail; 268 269 if (dev->parent && dev->parent->driver->child_pre_probe) { 270 ret = dev->parent->driver->child_pre_probe(dev); 271 if (ret) 272 goto fail; 273 } 274 275 if (drv->ofdata_to_platdata && dev->of_offset >= 0) { 276 ret = drv->ofdata_to_platdata(dev); 277 if (ret) 278 goto fail; 279 } 280 281 dev->flags |= DM_FLAG_ACTIVATED; 282 if (drv->probe) { 283 ret = drv->probe(dev); 284 if (ret) { 285 dev->flags &= ~DM_FLAG_ACTIVATED; 286 goto fail; 287 } 288 } 289 290 ret = uclass_post_probe_device(dev); 291 if (ret) 292 goto fail_uclass; 293 294 return 0; 295 fail_uclass: 296 if (device_remove(dev)) { 297 dm_warn("%s: Device '%s' failed to remove on error path\n", 298 __func__, dev->name); 299 } 300 fail: 301 dev->flags &= ~DM_FLAG_ACTIVATED; 302 303 dev->seq = -1; 304 device_free(dev); 305 306 return ret; 307 } 308 309 int device_probe(struct udevice *dev) 310 { 311 return device_probe_child(dev, NULL); 312 } 313 314 void *dev_get_platdata(struct udevice *dev) 315 { 316 if (!dev) { 317 dm_warn("%s: null device\n", __func__); 318 return NULL; 319 } 320 321 return dev->platdata; 322 } 323 324 void *dev_get_parent_platdata(struct udevice *dev) 325 { 326 if (!dev) { 327 dm_warn("%s: null device", __func__); 328 return NULL; 329 } 330 331 return dev->parent_platdata; 332 } 333 334 void *dev_get_uclass_platdata(struct udevice *dev) 335 { 336 if (!dev) { 337 dm_warn("%s: null device", __func__); 338 return NULL; 339 } 340 341 return dev->uclass_platdata; 342 } 343 344 void *dev_get_priv(struct udevice *dev) 345 { 346 if (!dev) { 347 dm_warn("%s: null device\n", __func__); 348 return NULL; 349 } 350 351 return dev->priv; 352 } 353 354 void *dev_get_uclass_priv(struct udevice *dev) 355 { 356 if (!dev) { 357 dm_warn("%s: null device\n", __func__); 358 return NULL; 359 } 360 361 return dev->uclass_priv; 362 } 363 364 void *dev_get_parentdata(struct udevice *dev) 365 { 366 if (!dev) { 367 dm_warn("%s: null device\n", __func__); 368 return NULL; 369 } 370 371 return dev->parent_priv; 372 } 373 374 static int device_get_device_tail(struct udevice *dev, int ret, 375 struct udevice **devp) 376 { 377 if (ret) 378 return ret; 379 380 ret = device_probe(dev); 381 if (ret) 382 return ret; 383 384 *devp = dev; 385 386 return 0; 387 } 388 389 int device_get_child(struct udevice *parent, int index, struct udevice **devp) 390 { 391 struct udevice *dev; 392 393 list_for_each_entry(dev, &parent->child_head, sibling_node) { 394 if (!index--) 395 return device_get_device_tail(dev, 0, devp); 396 } 397 398 return -ENODEV; 399 } 400 401 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 402 bool find_req_seq, struct udevice **devp) 403 { 404 struct udevice *dev; 405 406 *devp = NULL; 407 if (seq_or_req_seq == -1) 408 return -ENODEV; 409 410 list_for_each_entry(dev, &parent->child_head, sibling_node) { 411 if ((find_req_seq ? dev->req_seq : dev->seq) == 412 seq_or_req_seq) { 413 *devp = dev; 414 return 0; 415 } 416 } 417 418 return -ENODEV; 419 } 420 421 int device_get_child_by_seq(struct udevice *parent, int seq, 422 struct udevice **devp) 423 { 424 struct udevice *dev; 425 int ret; 426 427 *devp = NULL; 428 ret = device_find_child_by_seq(parent, seq, false, &dev); 429 if (ret == -ENODEV) { 430 /* 431 * We didn't find it in probed devices. See if there is one 432 * that will request this seq if probed. 433 */ 434 ret = device_find_child_by_seq(parent, seq, true, &dev); 435 } 436 return device_get_device_tail(dev, ret, devp); 437 } 438 439 int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 440 struct udevice **devp) 441 { 442 struct udevice *dev; 443 444 *devp = NULL; 445 446 list_for_each_entry(dev, &parent->child_head, sibling_node) { 447 if (dev->of_offset == of_offset) { 448 *devp = dev; 449 return 0; 450 } 451 } 452 453 return -ENODEV; 454 } 455 456 int device_get_child_by_of_offset(struct udevice *parent, int seq, 457 struct udevice **devp) 458 { 459 struct udevice *dev; 460 int ret; 461 462 *devp = NULL; 463 ret = device_find_child_by_of_offset(parent, seq, &dev); 464 return device_get_device_tail(dev, ret, devp); 465 } 466 467 int device_find_first_child(struct udevice *parent, struct udevice **devp) 468 { 469 if (list_empty(&parent->child_head)) { 470 *devp = NULL; 471 } else { 472 *devp = list_first_entry(&parent->child_head, struct udevice, 473 sibling_node); 474 } 475 476 return 0; 477 } 478 479 int device_find_next_child(struct udevice **devp) 480 { 481 struct udevice *dev = *devp; 482 struct udevice *parent = dev->parent; 483 484 if (list_is_last(&dev->sibling_node, &parent->child_head)) { 485 *devp = NULL; 486 } else { 487 *devp = list_entry(dev->sibling_node.next, struct udevice, 488 sibling_node); 489 } 490 491 return 0; 492 } 493 494 struct udevice *dev_get_parent(struct udevice *child) 495 { 496 return child->parent; 497 } 498 499 ulong dev_get_driver_data(struct udevice *dev) 500 { 501 return dev->driver_data; 502 } 503 504 const void *dev_get_driver_ops(struct udevice *dev) 505 { 506 if (!dev || !dev->driver->ops) 507 return NULL; 508 509 return dev->driver->ops; 510 } 511 512 enum uclass_id device_get_uclass_id(struct udevice *dev) 513 { 514 return dev->uclass->uc_drv->id; 515 } 516 517 const char *dev_get_uclass_name(struct udevice *dev) 518 { 519 if (!dev) 520 return NULL; 521 522 return dev->uclass->uc_drv->name; 523 } 524 525 #ifdef CONFIG_OF_CONTROL 526 fdt_addr_t dev_get_addr(struct udevice *dev) 527 { 528 return fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); 529 } 530 #else 531 fdt_addr_t dev_get_addr(struct udevice *dev) 532 { 533 return FDT_ADDR_T_NONE; 534 } 535 #endif 536 537 bool device_has_children(struct udevice *dev) 538 { 539 return !list_empty(&dev->child_head); 540 } 541 542 bool device_has_active_children(struct udevice *dev) 543 { 544 struct udevice *child; 545 546 for (device_find_first_child(dev, &child); 547 child; 548 device_find_next_child(&child)) { 549 if (device_active(child)) 550 return true; 551 } 552 553 return false; 554 } 555 556 bool device_is_last_sibling(struct udevice *dev) 557 { 558 struct udevice *parent = dev->parent; 559 560 if (!parent) 561 return false; 562 return list_is_last(&dev->sibling_node, &parent->child_head); 563 } 564