2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* driver.c - U-Boot driver model
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2007-11-08 11:01:52 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief U-Boot's driver model, and devinfo command
|
|
|
|
*/
|
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fs.h>
|
2007-10-11 18:20:36 +00:00
|
|
|
#include <list.h>
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
LIST_HEAD(device_list);
|
|
|
|
EXPORT_SYMBOL(device_list);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
LIST_HEAD(driver_list);
|
|
|
|
EXPORT_SYMBOL(driver_list);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
struct device_d *get_device_by_id(const char *id)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-10-11 18:20:36 +00:00
|
|
|
struct device_d *dev;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
for_each_device(dev) {
|
|
|
|
if(!strcmp(id, dev->id))
|
|
|
|
return dev;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
return NULL;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 09:06:03 +00:00
|
|
|
int get_free_deviceid(char *id, const char *id_template)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
sprintf(id, "%s%d", id_template, i);
|
|
|
|
if (!get_device_by_id(id))
|
|
|
|
return 0;
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match(struct driver_d *drv, struct device_d *dev)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (strcmp(dev->name, drv->name))
|
|
|
|
return -1;
|
|
|
|
if (dev->type != drv->type)
|
|
|
|
return -1;
|
|
|
|
if(drv->probe(dev))
|
|
|
|
return -1;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:56:18 +00:00
|
|
|
dev->driver = drv;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:56:18 +00:00
|
|
|
return 0;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int register_device(struct device_d *new_device)
|
|
|
|
{
|
|
|
|
struct driver_d *drv;
|
|
|
|
|
|
|
|
if(*new_device->id && get_device_by_id(new_device->id)) {
|
|
|
|
printf("device %s already exists\n", new_device->id);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2007-07-05 19:42:34 +00:00
|
|
|
debug ("register_device: %s\n",new_device->name);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
list_add_tail(&new_device->list, &device_list);
|
2007-10-19 06:45:57 +00:00
|
|
|
INIT_LIST_HEAD(&new_device->children);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
for_each_driver(drv) {
|
2007-10-11 18:56:18 +00:00
|
|
|
if (!match(drv, new_device))
|
|
|
|
break;
|
|
|
|
}
|
2007-07-05 16:02:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(register_device);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
int unregister_device(struct device_d *old_dev)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-07-14 13:36:49 +00:00
|
|
|
debug("unregister_device: %s:%s\n",old_dev->name, old_dev->id);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
if (!list_empty(&old_dev->children)) {
|
|
|
|
errno = -EBUSY;
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
if (old_dev->driver)
|
|
|
|
old_dev->driver->remove(old_dev);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
list_del(&old_dev->list);
|
2007-10-19 06:45:57 +00:00
|
|
|
|
|
|
|
/* remove device from parents child list */
|
|
|
|
if (old_dev->parent)
|
|
|
|
list_del(&old_dev->sibling);
|
|
|
|
|
|
|
|
return 0;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(unregister_device);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
int dev_add_child(struct device_d *dev, struct device_d *child)
|
|
|
|
{
|
|
|
|
child->parent = dev;
|
|
|
|
|
|
|
|
list_add_tail(&child->sibling, &dev->children);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-19 11:13:17 +00:00
|
|
|
EXPORT_SYMBOL(dev_add_child);
|
2007-10-19 06:45:57 +00:00
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
struct driver_d *get_driver_by_name(const char *name)
|
|
|
|
{
|
2007-10-11 18:20:36 +00:00
|
|
|
struct driver_d *drv;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
for_each_driver(drv) {
|
|
|
|
if(!strcmp(name, drv->name))
|
|
|
|
return drv;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
return NULL;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void noinfo(struct device_d *dev)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
printf("no info available for %s\n", dev->id);
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void noshortinfo(struct device_d *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
int register_driver(struct driver_d *drv)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
struct device_d *dev = NULL;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
debug("register_driver: %s\n", drv->name);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
list_add_tail(&drv->list, &driver_list);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:56:18 +00:00
|
|
|
if (!drv->info)
|
|
|
|
drv->info = noinfo;
|
|
|
|
if (!drv->shortinfo)
|
|
|
|
drv->shortinfo = noshortinfo;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
for_each_device(dev)
|
2007-10-11 18:56:18 +00:00
|
|
|
match(drv, dev);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(register_driver);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
|
|
|
/* Get a device struct from the beginning of the string. Default to mem if no
|
|
|
|
* device is given, return NULL if a unknown device is given.
|
|
|
|
* If endp is not NULL, this function stores a pointer to the first character
|
|
|
|
* after the device name in *endp.
|
|
|
|
*/
|
2007-10-19 06:45:57 +00:00
|
|
|
struct device_d *get_device_by_path(const char *path)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-11-16 15:49:51 +00:00
|
|
|
struct device_d *dev = NULL;
|
|
|
|
char *npath = normalise_path(path);
|
|
|
|
|
|
|
|
if (strncmp(npath, "/dev/", 5))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
dev = get_device_by_id(npath + 5);
|
2007-10-19 06:45:57 +00:00
|
|
|
|
2007-11-16 15:49:51 +00:00
|
|
|
out:
|
|
|
|
free(npath);
|
|
|
|
return dev;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
2007-10-19 11:13:17 +00:00
|
|
|
EXPORT_SYMBOL(get_device_by_path);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
|
|
|
ssize_t dev_read(struct device_d *dev, void *buf, size_t count, unsigned long offset, ulong flags)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (dev->driver->read)
|
|
|
|
return dev->driver->read(dev, buf, count, offset, flags);
|
2007-07-05 16:02:12 +00:00
|
|
|
errno = -ENOSYS;
|
2007-10-11 18:56:18 +00:00
|
|
|
return -ENOSYS;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t dev_write(struct device_d *dev, const void *buf, size_t count, unsigned long offset, ulong flags)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (dev->driver->write)
|
|
|
|
return dev->driver->write(dev, buf, count, offset, flags);
|
2007-07-05 16:02:12 +00:00
|
|
|
errno = -ENOSYS;
|
2007-10-11 18:56:18 +00:00
|
|
|
return -ENOSYS;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:25:13 +00:00
|
|
|
off_t dev_lseek(struct device_d *dev, off_t offset)
|
|
|
|
{
|
|
|
|
if (dev->driver->lseek)
|
|
|
|
return dev->driver->lseek(dev, offset);
|
|
|
|
errno = -ENOSYS;
|
2008-08-01 06:59:27 +00:00
|
|
|
return -1;
|
2008-06-06 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dev_open(struct device_d *dev, struct filep *f)
|
|
|
|
{
|
|
|
|
if (dev->driver->open)
|
|
|
|
return dev->driver->open(dev, f);
|
|
|
|
errno = -ENOSYS;
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dev_close(struct device_d *dev, struct filep *f)
|
|
|
|
{
|
|
|
|
if (dev->driver->close)
|
|
|
|
return dev->driver->close(dev, f);
|
|
|
|
errno = -ENOSYS;
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dev_ioctl(struct device_d *dev, int request, void *buf)
|
|
|
|
{
|
|
|
|
if (dev->driver->ioctl)
|
|
|
|
return dev->driver->ioctl(dev, request, buf);
|
|
|
|
errno = -ENOSYS;
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2007-10-11 20:00:22 +00:00
|
|
|
int dev_erase(struct device_d *dev, size_t count, unsigned long offset)
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (dev->driver->erase)
|
|
|
|
return dev->driver->erase(dev, count, offset);
|
2007-07-05 16:02:12 +00:00
|
|
|
errno = -ENOSYS;
|
2007-10-11 18:56:18 +00:00
|
|
|
return -ENOSYS;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 08:29:28 +00:00
|
|
|
int dev_protect(struct device_d *dev, size_t count, unsigned long offset, int prot)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (dev->driver->protect)
|
|
|
|
return dev->driver->protect(dev, count, offset, prot);
|
2007-07-16 08:29:28 +00:00
|
|
|
errno = -ENOSYS;
|
2007-10-11 18:56:18 +00:00
|
|
|
return -ENOSYS;
|
2007-07-16 08:29:28 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 20:00:22 +00:00
|
|
|
int dev_memmap(struct device_d *dev, void **map, int flags)
|
2007-07-15 11:50:04 +00:00
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
if (dev->driver->memmap)
|
|
|
|
return dev->driver->memmap(dev, map, flags);
|
2007-07-15 11:50:04 +00:00
|
|
|
errno = -ENOSYS;
|
2007-10-11 18:56:18 +00:00
|
|
|
return -ENOSYS;
|
2007-07-15 11:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int generic_memmap_ro(struct device_d *dev, void **map, int flags)
|
|
|
|
{
|
|
|
|
if (flags & PROT_WRITE)
|
|
|
|
return -EACCES;
|
|
|
|
*map = (void *)dev->map_base;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int generic_memmap_rw(struct device_d *dev, void **map, int flags)
|
|
|
|
{
|
|
|
|
*map = (void *)dev->map_base;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
int dummy_probe(struct device_d *dev)
|
|
|
|
{
|
2007-10-11 18:56:18 +00:00
|
|
|
return 0;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
2008-08-15 01:33:16 +00:00
|
|
|
EXPORT_SYMBOL(dummy_probe);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
static int do_devinfo_subtree(struct device_d *dev, int depth, char edge)
|
|
|
|
{
|
|
|
|
struct device_d *child;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
printf("| ");
|
|
|
|
|
|
|
|
if (*dev->id)
|
|
|
|
printf("%c----%s\n", edge, dev->id);
|
|
|
|
else if (dev->type == DEVICE_TYPE_FS)
|
2007-10-19 09:06:03 +00:00
|
|
|
printf("%c----filesystem: %s\n", edge,
|
|
|
|
fsdev_get_mountpoint((struct fs_device_d *)
|
|
|
|
dev->type_data));
|
2007-10-19 06:45:57 +00:00
|
|
|
|
|
|
|
if (!list_empty(&dev->children)) {
|
|
|
|
device_for_each_child(dev, child) {
|
2007-10-19 09:06:03 +00:00
|
|
|
do_devinfo_subtree(child, depth + 1,
|
2007-10-19 06:45:57 +00:00
|
|
|
list_is_last(&child->sibling,
|
|
|
|
&dev->children) ? '`' : '|');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-13 14:04:43 +00:00
|
|
|
#ifdef CONFIG_CMD_DEVINFO
|
|
|
|
|
2007-09-28 08:07:26 +00:00
|
|
|
static int do_devinfo ( cmd_tbl_t *cmdtp, int argc, char *argv[])
|
2007-07-05 16:02:12 +00:00
|
|
|
{
|
2007-10-11 18:20:36 +00:00
|
|
|
struct device_d *dev;
|
|
|
|
struct driver_d *drv;
|
|
|
|
struct param_d *param;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
printf("devices:\n");
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
for_each_device(dev) {
|
2007-10-19 06:45:57 +00:00
|
|
|
if (!dev->parent)
|
|
|
|
do_devinfo_subtree(dev, 0, '|');
|
2007-10-11 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\ndrivers:\n");
|
|
|
|
for_each_driver(drv)
|
|
|
|
printf("%10s\n",drv->name);
|
|
|
|
} else {
|
2007-10-19 06:45:57 +00:00
|
|
|
struct device_d *dev = get_device_by_path(argv[1]);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
if (!dev) {
|
|
|
|
printf("no such device: %s\n",argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
printf("base : 0x%08x\nsize : 0x%08x\ndriver: %s\n\n",
|
|
|
|
dev->map_base, dev->size,
|
|
|
|
dev->driver ?
|
|
|
|
dev->driver->name : "none");
|
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
if (dev->driver)
|
|
|
|
dev->driver->info(dev);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
param = dev->param;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
printf("%s\n", param ?
|
2007-07-05 19:42:34 +00:00
|
|
|
"Parameters:" : "no parameters available");
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
while (param) {
|
2007-07-05 16:02:12 +00:00
|
|
|
printf("%16s = %s\n", param->name, param->value);
|
2007-10-11 18:20:36 +00:00
|
|
|
param = param->next;
|
|
|
|
}
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
}
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2007-10-11 18:20:36 +00:00
|
|
|
return 0;
|
2007-07-05 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 09:46:55 +00:00
|
|
|
static const __maybe_unused char cmd_devinfo_help[] =
|
2007-07-05 16:02:12 +00:00
|
|
|
"Usage: devinfo [DEVICE]\n"
|
|
|
|
"If called without arguments devinfo shows a summary about known devices and\n"
|
2007-10-19 09:23:24 +00:00
|
|
|
"drivers. If called with a device path as argument devinfo shows more detailed\n"
|
2007-11-08 11:01:52 +00:00
|
|
|
"information about this device and its parameters.\n";
|
2007-07-05 16:02:12 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD_START(devinfo)
|
|
|
|
.maxargs = 2,
|
|
|
|
.cmd = do_devinfo,
|
|
|
|
.usage = "display info about devices and drivers",
|
|
|
|
U_BOOT_CMD_HELP(cmd_devinfo_help)
|
|
|
|
U_BOOT_CMD_END
|
2007-11-08 11:01:52 +00:00
|
|
|
|
2008-08-13 14:04:43 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-08 11:01:52 +00:00
|
|
|
/**
|
|
|
|
* @page devinfo_command devinfo
|
|
|
|
*
|
|
|
|
* Usage is: devinfo /dev/\<device>
|
|
|
|
*
|
|
|
|
* If called without arguments devinfo shows a summary about known devices and
|
|
|
|
* drivers. If called with a device path as argument devinfo shows more
|
|
|
|
* detailed information about this device and its parameters.
|
|
|
|
*
|
|
|
|
* Example from an MPC5200 based system:
|
|
|
|
@verbatim
|
|
|
|
uboot:/ devinfo /dev/eth0
|
|
|
|
base : 0x1002b000
|
|
|
|
size : 0x00000000
|
|
|
|
driver: fec_mpc5xxx
|
|
|
|
|
|
|
|
no info available for eth0
|
|
|
|
Parameters:
|
2007-11-12 11:29:09 +00:00
|
|
|
ipaddr = 192.168.23.197
|
2007-11-08 11:01:52 +00:00
|
|
|
ethaddr = 80:81:82:83:84:86
|
|
|
|
gateway = 192.168.23.1
|
|
|
|
netmask = 255.255.255.0
|
|
|
|
serverip = 192.168.23.2
|
|
|
|
@endverbatim
|
|
|
|
*
|
|
|
|
*/
|