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, PPC85xx, 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 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>
46 #include <linux/bootmem.h>
47 #include <linux/device.h>
51 #include <asm/errno.h>
52 #include <asm/rwsem.h>
53 #include <asm/semaphore.h>
55 //#define DBG(x) printk x
58 extern int mem_init_done
;
60 extern struct ocp_def core_ocp
[]; /* Static list of devices, provided by
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
;
69 #define OCP_DEF_ATTR(field, format_string) \
71 show_##field(struct device *dev, char *buf) \
73 struct ocp_device *odev = to_ocp_dev(dev); \
75 return sprintf(buf, format_string, odev->def->field); \
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");
85 OCP_DEF_ATTR(paddr
, "0x%08lx\n");
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
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
;
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
))
135 ocp_device_probe(struct device
*dev
)
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
);
145 error
= drv
->probe(ocp_dev
);
147 ocp_dev
->driver
= drv
;
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
;
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
);
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
);
189 struct bus_type ocp_bus_type
= {
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
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
)
250 if (function
!= OCP_ANY_ID
&& function
!= dev
->def
->function
)
252 if (index
!= OCP_ANY_INDEX
&& index
!= dev
->def
->index
)
258 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor
, function
, index
));
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...
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
);
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
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
);
310 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n",
311 vendor
, function
, index
));
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
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 */
337 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
339 dev
= alloc_bootmem(sizeof(*dev
));
343 memset(dev
, 0, sizeof(*dev
));
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"));
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 */
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
));
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.
397 ocp_for_each_device(void(*callback
)(struct ocp_device
*, void *arg
), void *arg
)
399 struct list_head
*entry
;
402 down_read(&ocp_devices_sem
);
403 list_for_each(entry
, &ocp_devices
)
404 callback(list_entry(entry
, struct ocp_device
, link
),
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
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"));
435 * ocp_driver_init - Init OCP device management
437 * This function is meant to be called via OCP bus registration.
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
;
451 DBG(("ocp: ocp_driver_init()...\n"));
453 /* Allocate/register primary OCP bus */
454 ocp_bus
= kmalloc(sizeof(struct device
), GFP_KERNEL
);
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
);
475 DBG(("ocp: ocp_driver_init()... done.\n"));
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
);