4 * (c) Benjamin Herrenschmidt (benh@kernel.crashing.org)
7 * Derived from work (c) Armin Kuster akuster@pacbell.net
9 * Additional support and port to 2.6 LDM/sysfs by
10 * Matt Porter <mporter@kernel.crashing.org>
11 * Copyright 2004 MontaVista Software, Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * OCP (On Chip Peripheral) is a software emulated "bus" with a
19 * pseudo discovery method for dumb peripherals. Usually these type
20 * of peripherals are found on embedded SoC (System On a Chip)
21 * processors or highly integrated system controllers that have
22 * a host bridge and many peripherals. Common examples where
23 * this is already used include the PPC4xx, MPC52xx,
26 * This subsystem creates a standard OCP bus type within the
27 * device model. The devices on the OCP bus are seeded by an
28 * an initial OCP device array created by the arch-specific
29 * Device entries can be added/removed/modified through OCP
30 * helper functions to accommodate system and board-specific
31 * parameters commonly found in embedded systems. OCP also
32 * provides a standard method for devices to describe extended
33 * attributes about themselves to the system. A standard access
34 * method allows OCP drivers to obtain the information, both
35 * SoC-specific and system/board-specific, needed for operation.
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/miscdevice.h>
41 #include <linux/slab.h>
42 #include <linux/types.h>
43 #include <linux/init.h>
45 #include <linux/bootmem.h>
46 #include <linux/device.h>
47 #include <linux/rwsem.h>
51 #include <asm/errno.h>
53 //#define DBG(x) printk x
56 extern int mem_init_done;
58 extern struct ocp_def core_ocp[]; /* Static list of devices, provided by
61 LIST_HEAD(ocp_devices); /* List of all OCP devices */
62 DECLARE_RWSEM(ocp_devices_sem); /* Global semaphores for those lists */
64 static int ocp_inited;
67 #define OCP_DEF_ATTR(field, format_string) \
69 show_##field(struct device *dev, struct device_attribute *attr, char *buf) \
71 struct ocp_device *odev = to_ocp_dev(dev); \
73 return sprintf(buf, format_string, odev->def->field); \
75 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
77 OCP_DEF_ATTR(vendor, "0x%04x\n");
78 OCP_DEF_ATTR(function, "0x%04x\n");
79 OCP_DEF_ATTR(index, "0x%04x\n");
80 #ifdef CONFIG_PTE_64BIT
81 OCP_DEF_ATTR(paddr, "0x%016Lx\n");
83 OCP_DEF_ATTR(paddr, "0x%08lx\n");
85 OCP_DEF_ATTR(irq, "%d\n");
86 OCP_DEF_ATTR(pm, "%lu\n");
88 void ocp_create_sysfs_dev_files(struct ocp_device *odev)
90 struct device *dev = &odev->dev;
92 /* Current OCP device def attributes */
93 device_create_file(dev, &dev_attr_vendor);
94 device_create_file(dev, &dev_attr_function);
95 device_create_file(dev, &dev_attr_index);
96 device_create_file(dev, &dev_attr_paddr);
97 device_create_file(dev, &dev_attr_irq);
98 device_create_file(dev, &dev_attr_pm);
99 /* Current OCP device additions attributes */
100 if (odev->def->additions && odev->def->show)
101 odev->def->show(dev);
105 * ocp_device_match - Match one driver to one device
106 * @drv: driver to match
107 * @dev: device to match
109 * This function returns 0 if the driver and device don't match
112 ocp_device_match(struct device *dev, struct device_driver *drv)
114 struct ocp_device *ocp_dev = to_ocp_dev(dev);
115 struct ocp_driver *ocp_drv = to_ocp_drv(drv);
116 const struct ocp_device_id *ids = ocp_drv->id_table;
121 while (ids->vendor || ids->function) {
122 if ((ids->vendor == OCP_ANY_ID
123 || ids->vendor == ocp_dev->def->vendor)
124 && (ids->function == OCP_ANY_ID
125 || ids->function == ocp_dev->def->function))
133 ocp_device_probe(struct device *dev)
136 struct ocp_driver *drv;
137 struct ocp_device *ocp_dev;
139 drv = to_ocp_drv(dev->driver);
140 ocp_dev = to_ocp_dev(dev);
143 error = drv->probe(ocp_dev);
145 ocp_dev->driver = drv;
153 ocp_device_remove(struct device *dev)
155 struct ocp_device *ocp_dev = to_ocp_dev(dev);
157 if (ocp_dev->driver) {
158 if (ocp_dev->driver->remove)
159 ocp_dev->driver->remove(ocp_dev);
160 ocp_dev->driver = NULL;
166 ocp_device_suspend(struct device *dev, pm_message_t state)
168 struct ocp_device *ocp_dev = to_ocp_dev(dev);
169 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
171 if (dev->driver && ocp_drv->suspend)
172 return ocp_drv->suspend(ocp_dev, state);
177 ocp_device_resume(struct device *dev)
179 struct ocp_device *ocp_dev = to_ocp_dev(dev);
180 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
182 if (dev->driver && ocp_drv->resume)
183 return ocp_drv->resume(ocp_dev);
187 struct bus_type ocp_bus_type = {
189 .match = ocp_device_match,
190 .probe = ocp_device_probe,
191 .remove = ocp_device_remove,
192 .suspend = ocp_device_suspend,
193 .resume = ocp_device_resume,
197 * ocp_register_driver - Register an OCP driver
198 * @drv: pointer to statically defined ocp_driver structure
200 * The driver's probe() callback is called either recursively
201 * by this function or upon later call of ocp_driver_init
203 * NOTE: Detection of devices is a 2 pass step on this implementation,
204 * hotswap isn't supported. First, all OCP devices are put in the device
205 * list, _then_ all drivers are probed on each match.
208 ocp_register_driver(struct ocp_driver *drv)
210 /* initialize common driver fields */
211 drv->driver.name = drv->name;
212 drv->driver.bus = &ocp_bus_type;
214 /* register with core */
215 return driver_register(&drv->driver);
219 * ocp_unregister_driver - Unregister an OCP driver
220 * @drv: pointer to statically defined ocp_driver structure
222 * The driver's remove() callback is called recursively
223 * by this function for any device already registered
226 ocp_unregister_driver(struct ocp_driver *drv)
228 DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name));
230 driver_unregister(&drv->driver);
232 DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name));
235 /* Core of ocp_find_device(). Caller must hold ocp_devices_sem */
236 static struct ocp_device *
237 __ocp_find_device(unsigned int vendor, unsigned int function, int index)
239 struct list_head *entry;
240 struct ocp_device *dev, *found = NULL;
242 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
244 list_for_each(entry, &ocp_devices) {
245 dev = list_entry(entry, struct ocp_device, link);
246 if (vendor != OCP_ANY_ID && vendor != dev->def->vendor)
248 if (function != OCP_ANY_ID && function != dev->def->function)
250 if (index != OCP_ANY_INDEX && index != dev->def->index)
256 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index));
262 * ocp_find_device - Find a device by function & index
263 * @vendor: vendor ID of the device (or OCP_ANY_ID)
264 * @function: function code of the device (or OCP_ANY_ID)
265 * @idx: index of the device (or OCP_ANY_INDEX)
267 * This function allows a lookup of a given function by it's
268 * index, it's typically used to find the MAL or ZMII associated
269 * with an EMAC or similar horrors.
270 * You can pass vendor, though you usually want OCP_ANY_ID there...
273 ocp_find_device(unsigned int vendor, unsigned int function, int index)
275 struct ocp_device *dev;
277 down_read(&ocp_devices_sem);
278 dev = __ocp_find_device(vendor, function, index);
279 up_read(&ocp_devices_sem);
285 * ocp_get_one_device - Find a def by function & index
286 * @vendor: vendor ID of the device (or OCP_ANY_ID)
287 * @function: function code of the device (or OCP_ANY_ID)
288 * @idx: index of the device (or OCP_ANY_INDEX)
290 * This function allows a lookup of a given ocp_def by it's
291 * vendor, function, and index. The main purpose for is to
292 * allow modification of the def before binding to the driver
295 ocp_get_one_device(unsigned int vendor, unsigned int function, int index)
297 struct ocp_device *dev;
298 struct ocp_def *found = NULL;
300 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n",
301 vendor, function, index));
303 dev = ocp_find_device(vendor, function, index);
308 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n",
309 vendor, function, index));
315 * ocp_add_one_device - Add a device
316 * @def: static device definition structure
318 * This function adds a device definition to the
319 * device list. It may only be called before
320 * ocp_driver_init() and will return an error
324 ocp_add_one_device(struct ocp_def *def)
326 struct ocp_device *dev;
328 DBG(("ocp: ocp_add_one_device()...\n"));
330 /* Can't be called after ocp driver init */
335 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
337 dev = alloc_bootmem(sizeof(*dev));
341 memset(dev, 0, sizeof(*dev));
343 dev->current_state = 4;
344 sprintf(dev->name, "OCP device %04x:%04x:%04x",
345 dev->def->vendor, dev->def->function, dev->def->index);
346 down_write(&ocp_devices_sem);
347 list_add_tail(&dev->link, &ocp_devices);
348 up_write(&ocp_devices_sem);
350 DBG(("ocp: ocp_add_one_device()...done\n"));
356 * ocp_remove_one_device - Remove a device by function & index
357 * @vendor: vendor ID of the device (or OCP_ANY_ID)
358 * @function: function code of the device (or OCP_ANY_ID)
359 * @idx: index of the device (or OCP_ANY_INDEX)
361 * This function allows removal of a given function by its
362 * index. It may only be called before ocp_driver_init()
363 * and will return an error otherwise.
366 ocp_remove_one_device(unsigned int vendor, unsigned int function, int index)
368 struct ocp_device *dev;
370 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
372 /* Can't be called after ocp driver init */
376 down_write(&ocp_devices_sem);
377 dev = __ocp_find_device(vendor, function, index);
378 list_del(&dev->link);
379 up_write(&ocp_devices_sem);
381 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index));
387 * ocp_for_each_device - Iterate over OCP devices
388 * @callback: routine to execute for each ocp device.
389 * @arg: user data to be passed to callback routine.
391 * This routine holds the ocp_device semaphore, so the
392 * callback routine cannot modify the ocp_device list.
395 ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg)
397 struct list_head *entry;
400 down_read(&ocp_devices_sem);
401 list_for_each(entry, &ocp_devices)
402 callback(list_entry(entry, struct ocp_device, link),
404 up_read(&ocp_devices_sem);
409 * ocp_early_init - Init OCP device management
411 * This function builds the list of devices before setup_arch.
412 * This allows platform code to modify the device lists before
413 * they are bound to drivers (changes to paddr, removing devices
421 DBG(("ocp: ocp_early_init()...\n"));
423 /* Fill the devices list */
424 for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++)
425 ocp_add_one_device(def);
427 DBG(("ocp: ocp_early_init()... done.\n"));
433 * ocp_driver_init - Init OCP device management
435 * This function is meant to be called via OCP bus registration.
438 ocp_driver_init(void)
440 int ret = 0, index = 0;
441 struct device *ocp_bus;
442 struct list_head *entry;
443 struct ocp_device *dev;
449 DBG(("ocp: ocp_driver_init()...\n"));
451 /* Allocate/register primary OCP bus */
452 ocp_bus = kzalloc(sizeof(struct device), GFP_KERNEL);
455 strcpy(ocp_bus->bus_id, "ocp");
457 bus_register(&ocp_bus_type);
459 device_register(ocp_bus);
461 /* Put each OCP device into global device list */
462 list_for_each(entry, &ocp_devices) {
463 dev = list_entry(entry, struct ocp_device, link);
464 sprintf(dev->dev.bus_id, "%2.2x", index);
465 dev->dev.parent = ocp_bus;
466 dev->dev.bus = &ocp_bus_type;
467 device_register(&dev->dev);
468 ocp_create_sysfs_dev_files(dev);
472 DBG(("ocp: ocp_driver_init()... done.\n"));
477 postcore_initcall(ocp_driver_init);
479 EXPORT_SYMBOL(ocp_bus_type);
480 EXPORT_SYMBOL(ocp_find_device);
481 EXPORT_SYMBOL(ocp_register_driver);
482 EXPORT_SYMBOL(ocp_unregister_driver);