Linux-2.6.12-rc2
[linux-2.6/kvm.git] / arch / ppc / syslib / ocp.c
bloba5156c5179a617ac0ef4baeb109c30170d1d401c
1 /*
2 * ocp.c
4 * (c) Benjamin Herrenschmidt (benh@kernel.crashing.org)
5 * Mipsys - France
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, PPC85xx, MPC52xx,
24 * and MV64xxx parts.
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 accomodate 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/config.h>
40 #include <linux/list.h>
41 #include <linux/miscdevice.h>
42 #include <linux/slab.h>
43 #include <linux/types.h>
44 #include <linux/init.h>
45 #include <linux/pm.h>
46 #include <linux/bootmem.h>
47 #include <linux/device.h>
49 #include <asm/io.h>
50 #include <asm/ocp.h>
51 #include <asm/errno.h>
52 #include <asm/rwsem.h>
53 #include <asm/semaphore.h>
55 //#define DBG(x) printk x
56 #define DBG(x)
58 extern int mem_init_done;
60 extern struct ocp_def core_ocp[]; /* Static list of devices, provided by
61 CPU core */
63 LIST_HEAD(ocp_devices); /* List of all OCP devices */
64 DECLARE_RWSEM(ocp_devices_sem); /* Global semaphores for those lists */
66 static int ocp_inited;
68 /* Sysfs support */
69 #define OCP_DEF_ATTR(field, format_string) \
70 static ssize_t \
71 show_##field(struct device *dev, char *buf) \
72 { \
73 struct ocp_device *odev = to_ocp_dev(dev); \
75 return sprintf(buf, format_string, odev->def->field); \
76 } \
77 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
79 OCP_DEF_ATTR(vendor, "0x%04x\n");
80 OCP_DEF_ATTR(function, "0x%04x\n");
81 OCP_DEF_ATTR(index, "0x%04x\n");
82 #ifdef CONFIG_PTE_64BIT
83 OCP_DEF_ATTR(paddr, "0x%016Lx\n");
84 #else
85 OCP_DEF_ATTR(paddr, "0x%08lx\n");
86 #endif
87 OCP_DEF_ATTR(irq, "%d\n");
88 OCP_DEF_ATTR(pm, "%lu\n");
90 void ocp_create_sysfs_dev_files(struct ocp_device *odev)
92 struct device *dev = &odev->dev;
94 /* Current OCP device def attributes */
95 device_create_file(dev, &dev_attr_vendor);
96 device_create_file(dev, &dev_attr_function);
97 device_create_file(dev, &dev_attr_index);
98 device_create_file(dev, &dev_attr_paddr);
99 device_create_file(dev, &dev_attr_irq);
100 device_create_file(dev, &dev_attr_pm);
101 /* Current OCP device additions attributes */
102 if (odev->def->additions && odev->def->show)
103 odev->def->show(dev);
107 * ocp_device_match - Match one driver to one device
108 * @drv: driver to match
109 * @dev: device to match
111 * This function returns 0 if the driver and device don't match
113 static int
114 ocp_device_match(struct device *dev, struct device_driver *drv)
116 struct ocp_device *ocp_dev = to_ocp_dev(dev);
117 struct ocp_driver *ocp_drv = to_ocp_drv(drv);
118 const struct ocp_device_id *ids = ocp_drv->id_table;
120 if (!ids)
121 return 0;
123 while (ids->vendor || ids->function) {
124 if ((ids->vendor == OCP_ANY_ID
125 || ids->vendor == ocp_dev->def->vendor)
126 && (ids->function == OCP_ANY_ID
127 || ids->function == ocp_dev->def->function))
128 return 1;
129 ids++;
131 return 0;
134 static int
135 ocp_device_probe(struct device *dev)
137 int error = 0;
138 struct ocp_driver *drv;
139 struct ocp_device *ocp_dev;
141 drv = to_ocp_drv(dev->driver);
142 ocp_dev = to_ocp_dev(dev);
144 if (drv->probe) {
145 error = drv->probe(ocp_dev);
146 if (error >= 0) {
147 ocp_dev->driver = drv;
148 error = 0;
151 return error;
154 static int
155 ocp_device_remove(struct device *dev)
157 struct ocp_device *ocp_dev = to_ocp_dev(dev);
159 if (ocp_dev->driver) {
160 if (ocp_dev->driver->remove)
161 ocp_dev->driver->remove(ocp_dev);
162 ocp_dev->driver = NULL;
164 return 0;
167 static int
168 ocp_device_suspend(struct device *dev, u32 state)
170 struct ocp_device *ocp_dev = to_ocp_dev(dev);
171 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
173 if (dev->driver && ocp_drv->suspend)
174 return ocp_drv->suspend(ocp_dev, state);
175 return 0;
178 static int
179 ocp_device_resume(struct device *dev)
181 struct ocp_device *ocp_dev = to_ocp_dev(dev);
182 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
184 if (dev->driver && ocp_drv->resume)
185 return ocp_drv->resume(ocp_dev);
186 return 0;
189 struct bus_type ocp_bus_type = {
190 .name = "ocp",
191 .match = ocp_device_match,
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;
213 drv->driver.probe = ocp_device_probe;
214 drv->driver.remove = ocp_device_remove;
216 /* register with core */
217 return driver_register(&drv->driver);
221 * ocp_unregister_driver - Unregister an OCP driver
222 * @drv: pointer to statically defined ocp_driver structure
224 * The driver's remove() callback is called recursively
225 * by this function for any device already registered
227 void
228 ocp_unregister_driver(struct ocp_driver *drv)
230 DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name));
232 driver_unregister(&drv->driver);
234 DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name));
237 /* Core of ocp_find_device(). Caller must hold ocp_devices_sem */
238 static struct ocp_device *
239 __ocp_find_device(unsigned int vendor, unsigned int function, int index)
241 struct list_head *entry;
242 struct ocp_device *dev, *found = NULL;
244 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
246 list_for_each(entry, &ocp_devices) {
247 dev = list_entry(entry, struct ocp_device, link);
248 if (vendor != OCP_ANY_ID && vendor != dev->def->vendor)
249 continue;
250 if (function != OCP_ANY_ID && function != dev->def->function)
251 continue;
252 if (index != OCP_ANY_INDEX && index != dev->def->index)
253 continue;
254 found = dev;
255 break;
258 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index));
260 return found;
264 * ocp_find_device - Find a device by function & index
265 * @vendor: vendor ID of the device (or OCP_ANY_ID)
266 * @function: function code of the device (or OCP_ANY_ID)
267 * @idx: index of the device (or OCP_ANY_INDEX)
269 * This function allows a lookup of a given function by it's
270 * index, it's typically used to find the MAL or ZMII associated
271 * with an EMAC or similar horrors.
272 * You can pass vendor, though you usually want OCP_ANY_ID there...
274 struct ocp_device *
275 ocp_find_device(unsigned int vendor, unsigned int function, int index)
277 struct ocp_device *dev;
279 down_read(&ocp_devices_sem);
280 dev = __ocp_find_device(vendor, function, index);
281 up_read(&ocp_devices_sem);
283 return dev;
287 * ocp_get_one_device - Find a def by function & index
288 * @vendor: vendor ID of the device (or OCP_ANY_ID)
289 * @function: function code of the device (or OCP_ANY_ID)
290 * @idx: index of the device (or OCP_ANY_INDEX)
292 * This function allows a lookup of a given ocp_def by it's
293 * vendor, function, and index. The main purpose for is to
294 * allow modification of the def before binding to the driver
296 struct ocp_def *
297 ocp_get_one_device(unsigned int vendor, unsigned int function, int index)
299 struct ocp_device *dev;
300 struct ocp_def *found = NULL;
302 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n",
303 vendor, function, index));
305 dev = ocp_find_device(vendor, function, index);
307 if (dev)
308 found = dev->def;
310 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n",
311 vendor, function, index));
313 return found;
317 * ocp_add_one_device - Add a device
318 * @def: static device definition structure
320 * This function adds a device definition to the
321 * device list. It may only be called before
322 * ocp_driver_init() and will return an error
323 * otherwise.
326 ocp_add_one_device(struct ocp_def *def)
328 struct ocp_device *dev;
330 DBG(("ocp: ocp_add_one_device()...\n"));
332 /* Can't be called after ocp driver init */
333 if (ocp_inited)
334 return 1;
336 if (mem_init_done)
337 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
338 else
339 dev = alloc_bootmem(sizeof(*dev));
341 if (dev == NULL)
342 return 1;
343 memset(dev, 0, sizeof(*dev));
344 dev->def = def;
345 dev->current_state = 4;
346 sprintf(dev->name, "OCP device %04x:%04x:%04x",
347 dev->def->vendor, dev->def->function, dev->def->index);
348 down_write(&ocp_devices_sem);
349 list_add_tail(&dev->link, &ocp_devices);
350 up_write(&ocp_devices_sem);
352 DBG(("ocp: ocp_add_one_device()...done\n"));
354 return 0;
358 * ocp_remove_one_device - Remove a device by function & index
359 * @vendor: vendor ID of the device (or OCP_ANY_ID)
360 * @function: function code of the device (or OCP_ANY_ID)
361 * @idx: index of the device (or OCP_ANY_INDEX)
363 * This function allows removal of a given function by its
364 * index. It may only be called before ocp_driver_init()
365 * and will return an error otherwise.
368 ocp_remove_one_device(unsigned int vendor, unsigned int function, int index)
370 struct ocp_device *dev;
372 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
374 /* Can't be called after ocp driver init */
375 if (ocp_inited)
376 return 1;
378 down_write(&ocp_devices_sem);
379 dev = __ocp_find_device(vendor, function, index);
380 list_del((struct list_head *)dev);
381 up_write(&ocp_devices_sem);
383 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index));
385 return 0;
389 * ocp_for_each_device - Iterate over OCP devices
390 * @callback: routine to execute for each ocp device.
391 * @arg: user data to be passed to callback routine.
393 * This routine holds the ocp_device semaphore, so the
394 * callback routine cannot modify the ocp_device list.
396 void
397 ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg)
399 struct list_head *entry;
401 if (callback) {
402 down_read(&ocp_devices_sem);
403 list_for_each(entry, &ocp_devices)
404 callback(list_entry(entry, struct ocp_device, link),
405 arg);
406 up_read(&ocp_devices_sem);
411 * ocp_early_init - Init OCP device management
413 * This function builds the list of devices before setup_arch.
414 * This allows platform code to modify the device lists before
415 * they are bound to drivers (changes to paddr, removing devices
416 * etc)
418 int __init
419 ocp_early_init(void)
421 struct ocp_def *def;
423 DBG(("ocp: ocp_early_init()...\n"));
425 /* Fill the devices list */
426 for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++)
427 ocp_add_one_device(def);
429 DBG(("ocp: ocp_early_init()... done.\n"));
431 return 0;
435 * ocp_driver_init - Init OCP device management
437 * This function is meant to be called via OCP bus registration.
439 static int __init
440 ocp_driver_init(void)
442 int ret = 0, index = 0;
443 struct device *ocp_bus;
444 struct list_head *entry;
445 struct ocp_device *dev;
447 if (ocp_inited)
448 return ret;
449 ocp_inited = 1;
451 DBG(("ocp: ocp_driver_init()...\n"));
453 /* Allocate/register primary OCP bus */
454 ocp_bus = kmalloc(sizeof(struct device), GFP_KERNEL);
455 if (ocp_bus == NULL)
456 return 1;
457 memset(ocp_bus, 0, sizeof(struct device));
458 strcpy(ocp_bus->bus_id, "ocp");
460 bus_register(&ocp_bus_type);
462 device_register(ocp_bus);
464 /* Put each OCP device into global device list */
465 list_for_each(entry, &ocp_devices) {
466 dev = list_entry(entry, struct ocp_device, link);
467 sprintf(dev->dev.bus_id, "%2.2x", index);
468 dev->dev.parent = ocp_bus;
469 dev->dev.bus = &ocp_bus_type;
470 device_register(&dev->dev);
471 ocp_create_sysfs_dev_files(dev);
472 index++;
475 DBG(("ocp: ocp_driver_init()... done.\n"));
477 return 0;
480 postcore_initcall(ocp_driver_init);
482 EXPORT_SYMBOL(ocp_bus_type);
483 EXPORT_SYMBOL(ocp_find_device);
484 EXPORT_SYMBOL(ocp_register_driver);
485 EXPORT_SYMBOL(ocp_unregister_driver);