2014-02-26 22:59:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Marek Vasut <marex@denx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dm/device.h>
|
|
|
|
#include <dm/device-internal.h>
|
2014-06-25 19:57:45 +00:00
|
|
|
#include <dm/lists.h>
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <dm/platdata.h>
|
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <dm/util.h>
|
2014-06-12 05:29:48 +00:00
|
|
|
#include <fdtdec.h>
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <linux/compiler.h>
|
|
|
|
|
|
|
|
struct driver *lists_driver_lookup_name(const char *name)
|
|
|
|
{
|
|
|
|
struct driver *drv =
|
|
|
|
ll_entry_start(struct driver, driver);
|
|
|
|
const int n_ents = ll_entry_count(struct driver, driver);
|
|
|
|
struct driver *entry;
|
|
|
|
|
|
|
|
for (entry = drv; entry != drv + n_ents; entry++) {
|
2014-09-28 13:52:27 +00:00
|
|
|
if (!strcmp(name, entry->name))
|
2014-02-26 22:59:18 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not found */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
|
|
|
|
{
|
|
|
|
struct uclass_driver *uclass =
|
|
|
|
ll_entry_start(struct uclass_driver, uclass);
|
|
|
|
const int n_ents = ll_entry_count(struct uclass_driver, uclass);
|
|
|
|
struct uclass_driver *entry;
|
|
|
|
|
|
|
|
for (entry = uclass; entry != uclass + n_ents; entry++) {
|
|
|
|
if (entry->id == id)
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:55:03 +00:00
|
|
|
int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct driver_info *info =
|
|
|
|
ll_entry_start(struct driver_info, driver_info);
|
|
|
|
const int n_ents = ll_entry_count(struct driver_info, driver_info);
|
|
|
|
struct driver_info *entry;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:18 +00:00
|
|
|
int result = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (entry = info; entry != info + n_ents; entry++) {
|
2014-07-23 12:55:03 +00:00
|
|
|
ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
|
|
|
|
if (ret && ret != -EPERM) {
|
2014-02-26 22:59:18 +00:00
|
|
|
dm_warn("No match for driver '%s'\n", entry->name);
|
|
|
|
if (!result || ret != -ENOENT)
|
|
|
|
result = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-11 17:46:21 +00:00
|
|
|
int device_bind_driver(struct udevice *parent, const char *drv_name,
|
|
|
|
const char *dev_name, struct udevice **devp)
|
2015-04-29 02:25:04 +00:00
|
|
|
{
|
|
|
|
return device_bind_driver_to_node(parent, drv_name, dev_name, -1, devp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
|
|
|
|
const char *dev_name, int node,
|
|
|
|
struct udevice **devp)
|
2014-11-11 17:46:21 +00:00
|
|
|
{
|
|
|
|
struct driver *drv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
drv = lists_driver_lookup_name(drv_name);
|
|
|
|
if (!drv) {
|
2015-06-23 21:38:36 +00:00
|
|
|
debug("Cannot find driver '%s'\n", drv_name);
|
2014-11-11 17:46:21 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-04-29 02:25:04 +00:00
|
|
|
ret = device_bind(parent, drv, dev_name, NULL, node, devp);
|
2014-11-11 17:46:21 +00:00
|
|
|
if (ret) {
|
2015-06-23 21:38:36 +00:00
|
|
|
debug("Cannot create device named '%s' (err=%d)\n",
|
|
|
|
dev_name, ret);
|
2014-11-11 17:46:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-04 17:57:58 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2014-02-26 22:59:18 +00:00
|
|
|
/**
|
2016-09-08 06:47:36 +00:00
|
|
|
* driver_check_compatible() - Check if a driver matches a compatible string
|
2014-02-26 22:59:18 +00:00
|
|
|
*
|
2014-11-11 17:46:18 +00:00
|
|
|
* @param of_match: List of compatible strings to match
|
|
|
|
* @param of_idp: Returns the match that was found
|
2016-09-08 06:47:36 +00:00
|
|
|
* @param compat: The compatible string to search for
|
|
|
|
* @return 0 if there is a match, -ENOENT if no match
|
2014-02-26 22:59:18 +00:00
|
|
|
*/
|
2016-09-08 06:47:36 +00:00
|
|
|
static int driver_check_compatible(const struct udevice_id *of_match,
|
|
|
|
const struct udevice_id **of_idp,
|
|
|
|
const char *compat)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
if (!of_match)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
while (of_match->compatible) {
|
2016-09-08 06:47:36 +00:00
|
|
|
if (!strcmp(of_match->compatible, compat)) {
|
2014-11-11 17:46:18 +00:00
|
|
|
*of_idp = of_match;
|
2014-02-26 22:59:18 +00:00
|
|
|
return 0;
|
2014-11-11 17:46:18 +00:00
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
of_match++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:25 +00:00
|
|
|
int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
|
|
|
|
struct udevice **devp)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct driver *driver = ll_entry_start(struct driver, driver);
|
|
|
|
const int n_ents = ll_entry_count(struct driver, driver);
|
2014-11-11 17:46:18 +00:00
|
|
|
const struct udevice_id *id;
|
2014-02-26 22:59:18 +00:00
|
|
|
struct driver *entry;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-07-23 12:55:22 +00:00
|
|
|
bool found = false;
|
2016-09-08 06:47:36 +00:00
|
|
|
const char *name, *compat_list, *compat;
|
|
|
|
int compat_length, i;
|
2014-02-26 22:59:18 +00:00
|
|
|
int result = 0;
|
2014-07-23 12:55:22 +00:00
|
|
|
int ret = 0;
|
2014-02-26 22:59:18 +00:00
|
|
|
|
2016-09-08 06:47:36 +00:00
|
|
|
name = fdt_get_name(blob, offset, NULL);
|
|
|
|
dm_dbg("bind node %s\n", name);
|
2014-09-04 22:27:25 +00:00
|
|
|
if (devp)
|
|
|
|
*devp = NULL;
|
2016-09-08 06:47:36 +00:00
|
|
|
|
|
|
|
compat_list = fdt_getprop(blob, offset, "compatible", &compat_length);
|
|
|
|
if (!compat_list) {
|
|
|
|
if (compat_length == -FDT_ERR_NOTFOUND) {
|
2014-07-23 12:55:22 +00:00
|
|
|
dm_dbg("Device '%s' has no compatible string\n", name);
|
2016-09-08 06:47:36 +00:00
|
|
|
return 0;
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 06:47:36 +00:00
|
|
|
dm_warn("Device tree error at offset %d\n", offset);
|
|
|
|
return compat_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through the compatible string list, attempting to match each
|
|
|
|
* compatible string in order such that we match in order of priority
|
|
|
|
* from the first string to the last.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < compat_length; i += strlen(compat) + 1) {
|
|
|
|
compat = compat_list + i;
|
|
|
|
dm_dbg(" - attempt to match compatible string '%s'\n",
|
|
|
|
compat);
|
|
|
|
|
|
|
|
for (entry = driver; entry != driver + n_ents; entry++) {
|
|
|
|
ret = driver_check_compatible(entry->of_match, &id,
|
|
|
|
compat);
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry == driver + n_ents)
|
|
|
|
continue;
|
|
|
|
|
2014-02-26 22:59:18 +00:00
|
|
|
dm_dbg(" - found match at '%s'\n", entry->name);
|
2016-05-11 21:26:24 +00:00
|
|
|
ret = device_bind_with_driver_data(parent, entry, name,
|
|
|
|
id->data, offset, &dev);
|
2016-04-19 22:19:29 +00:00
|
|
|
if (ret == -ENODEV) {
|
|
|
|
dm_dbg("Driver '%s' refuses to bind\n", entry->name);
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
if (ret) {
|
2016-01-17 23:11:06 +00:00
|
|
|
dm_warn("Error binding driver '%s': %d\n", entry->name,
|
|
|
|
ret);
|
2014-09-04 22:27:25 +00:00
|
|
|
return ret;
|
2014-07-23 12:55:22 +00:00
|
|
|
} else {
|
|
|
|
found = true;
|
2014-09-04 22:27:25 +00:00
|
|
|
if (devp)
|
|
|
|
*devp = dev;
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
2014-07-23 12:55:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-08 06:47:36 +00:00
|
|
|
if (!found && !result && ret != -ENODEV)
|
|
|
|
dm_dbg("No match for node '%s'\n", name);
|
2014-02-26 22:59:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|