Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ocp.c | |
3 | * | |
4 | * (c) Benjamin Herrenschmidt (benh@kernel.crashing.org) | |
5 | * Mipsys - France | |
6 | * | |
7 | * Derived from work (c) Armin Kuster akuster@pacbell.net | |
8 | * | |
9 | * Additional support and port to 2.6 LDM/sysfs by | |
10 | * Matt Porter <mporter@kernel.crashing.org> | |
11 | * Copyright 2004 MontaVista Software, Inc. | |
12 | * | |
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. | |
17 | * | |
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. | |
25 | * | |
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. | |
36 | */ | |
37 | ||
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> | |
48 | ||
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> | |
54 | ||
55 | //#define DBG(x) printk x | |
56 | #define DBG(x) | |
57 | ||
58 | extern int mem_init_done; | |
59 | ||
60 | extern struct ocp_def core_ocp[]; /* Static list of devices, provided by | |
61 | CPU core */ | |
62 | ||
63 | LIST_HEAD(ocp_devices); /* List of all OCP devices */ | |
64 | DECLARE_RWSEM(ocp_devices_sem); /* Global semaphores for those lists */ | |
65 | ||
66 | static int ocp_inited; | |
67 | ||
68 | /* Sysfs support */ | |
69 | #define OCP_DEF_ATTR(field, format_string) \ | |
70 | static ssize_t \ | |
ff381d22 | 71 | show_##field(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
72 | { \ |
73 | struct ocp_device *odev = to_ocp_dev(dev); \ | |
74 | \ | |
75 | return sprintf(buf, format_string, odev->def->field); \ | |
76 | } \ | |
77 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); | |
78 | ||
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"); | |
89 | ||
90 | void ocp_create_sysfs_dev_files(struct ocp_device *odev) | |
91 | { | |
92 | struct device *dev = &odev->dev; | |
93 | ||
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); | |
104 | } | |
105 | ||
106 | /** | |
107 | * ocp_device_match - Match one driver to one device | |
108 | * @drv: driver to match | |
109 | * @dev: device to match | |
110 | * | |
111 | * This function returns 0 if the driver and device don't match | |
112 | */ | |
113 | static int | |
114 | ocp_device_match(struct device *dev, struct device_driver *drv) | |
115 | { | |
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; | |
119 | ||
120 | if (!ids) | |
121 | return 0; | |
122 | ||
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++; | |
130 | } | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int | |
135 | ocp_device_probe(struct device *dev) | |
136 | { | |
137 | int error = 0; | |
138 | struct ocp_driver *drv; | |
139 | struct ocp_device *ocp_dev; | |
140 | ||
141 | drv = to_ocp_drv(dev->driver); | |
142 | ocp_dev = to_ocp_dev(dev); | |
143 | ||
144 | if (drv->probe) { | |
145 | error = drv->probe(ocp_dev); | |
146 | if (error >= 0) { | |
147 | ocp_dev->driver = drv; | |
148 | error = 0; | |
149 | } | |
150 | } | |
151 | return error; | |
152 | } | |
153 | ||
154 | static int | |
155 | ocp_device_remove(struct device *dev) | |
156 | { | |
157 | struct ocp_device *ocp_dev = to_ocp_dev(dev); | |
158 | ||
159 | if (ocp_dev->driver) { | |
160 | if (ocp_dev->driver->remove) | |
161 | ocp_dev->driver->remove(ocp_dev); | |
162 | ocp_dev->driver = NULL; | |
163 | } | |
164 | return 0; | |
165 | } | |
166 | ||
167 | static int | |
842363ff | 168 | ocp_device_suspend(struct device *dev, pm_message_t state) |
1da177e4 LT |
169 | { |
170 | struct ocp_device *ocp_dev = to_ocp_dev(dev); | |
171 | struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver); | |
172 | ||
173 | if (dev->driver && ocp_drv->suspend) | |
174 | return ocp_drv->suspend(ocp_dev, state); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static int | |
179 | ocp_device_resume(struct device *dev) | |
180 | { | |
181 | struct ocp_device *ocp_dev = to_ocp_dev(dev); | |
182 | struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver); | |
183 | ||
184 | if (dev->driver && ocp_drv->resume) | |
185 | return ocp_drv->resume(ocp_dev); | |
186 | return 0; | |
187 | } | |
188 | ||
189 | struct bus_type ocp_bus_type = { | |
190 | .name = "ocp", | |
191 | .match = ocp_device_match, | |
91fb5386 RK |
192 | .probe = ocp_driver_probe, |
193 | .remove = ocp_driver_remove, | |
1da177e4 LT |
194 | .suspend = ocp_device_suspend, |
195 | .resume = ocp_device_resume, | |
196 | }; | |
197 | ||
198 | /** | |
199 | * ocp_register_driver - Register an OCP driver | |
200 | * @drv: pointer to statically defined ocp_driver structure | |
201 | * | |
202 | * The driver's probe() callback is called either recursively | |
203 | * by this function or upon later call of ocp_driver_init | |
204 | * | |
205 | * NOTE: Detection of devices is a 2 pass step on this implementation, | |
206 | * hotswap isn't supported. First, all OCP devices are put in the device | |
207 | * list, _then_ all drivers are probed on each match. | |
208 | */ | |
209 | int | |
210 | ocp_register_driver(struct ocp_driver *drv) | |
211 | { | |
212 | /* initialize common driver fields */ | |
213 | drv->driver.name = drv->name; | |
214 | drv->driver.bus = &ocp_bus_type; | |
1da177e4 LT |
215 | |
216 | /* register with core */ | |
217 | return driver_register(&drv->driver); | |
218 | } | |
219 | ||
220 | /** | |
221 | * ocp_unregister_driver - Unregister an OCP driver | |
222 | * @drv: pointer to statically defined ocp_driver structure | |
223 | * | |
224 | * The driver's remove() callback is called recursively | |
225 | * by this function for any device already registered | |
226 | */ | |
227 | void | |
228 | ocp_unregister_driver(struct ocp_driver *drv) | |
229 | { | |
230 | DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name)); | |
231 | ||
232 | driver_unregister(&drv->driver); | |
233 | ||
234 | DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name)); | |
235 | } | |
236 | ||
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) | |
240 | { | |
241 | struct list_head *entry; | |
242 | struct ocp_device *dev, *found = NULL; | |
243 | ||
244 | DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index)); | |
245 | ||
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; | |
256 | } | |
257 | ||
258 | DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index)); | |
259 | ||
260 | return found; | |
261 | } | |
262 | ||
263 | /** | |
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) | |
268 | * | |
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... | |
273 | */ | |
274 | struct ocp_device * | |
275 | ocp_find_device(unsigned int vendor, unsigned int function, int index) | |
276 | { | |
277 | struct ocp_device *dev; | |
278 | ||
279 | down_read(&ocp_devices_sem); | |
280 | dev = __ocp_find_device(vendor, function, index); | |
281 | up_read(&ocp_devices_sem); | |
282 | ||
283 | return dev; | |
284 | } | |
285 | ||
286 | /** | |
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) | |
291 | * | |
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 | |
295 | */ | |
296 | struct ocp_def * | |
297 | ocp_get_one_device(unsigned int vendor, unsigned int function, int index) | |
298 | { | |
299 | struct ocp_device *dev; | |
300 | struct ocp_def *found = NULL; | |
301 | ||
302 | DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n", | |
303 | vendor, function, index)); | |
304 | ||
305 | dev = ocp_find_device(vendor, function, index); | |
306 | ||
307 | if (dev) | |
308 | found = dev->def; | |
309 | ||
310 | DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n", | |
311 | vendor, function, index)); | |
312 | ||
313 | return found; | |
314 | } | |
315 | ||
316 | /** | |
317 | * ocp_add_one_device - Add a device | |
318 | * @def: static device definition structure | |
319 | * | |
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. | |
324 | */ | |
325 | int | |
326 | ocp_add_one_device(struct ocp_def *def) | |
327 | { | |
328 | struct ocp_device *dev; | |
329 | ||
330 | DBG(("ocp: ocp_add_one_device()...\n")); | |
331 | ||
332 | /* Can't be called after ocp driver init */ | |
333 | if (ocp_inited) | |
334 | return 1; | |
335 | ||
336 | if (mem_init_done) | |
337 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | |
338 | else | |
339 | dev = alloc_bootmem(sizeof(*dev)); | |
340 | ||
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); | |
351 | ||
352 | DBG(("ocp: ocp_add_one_device()...done\n")); | |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
357 | /** | |
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) | |
362 | * | |
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. | |
366 | */ | |
367 | int | |
368 | ocp_remove_one_device(unsigned int vendor, unsigned int function, int index) | |
369 | { | |
370 | struct ocp_device *dev; | |
371 | ||
372 | DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index)); | |
373 | ||
374 | /* Can't be called after ocp driver init */ | |
375 | if (ocp_inited) | |
376 | return 1; | |
377 | ||
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); | |
382 | ||
383 | DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index)); | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | /** | |
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. | |
392 | * | |
393 | * This routine holds the ocp_device semaphore, so the | |
394 | * callback routine cannot modify the ocp_device list. | |
395 | */ | |
396 | void | |
397 | ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg) | |
398 | { | |
399 | struct list_head *entry; | |
400 | ||
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); | |
407 | } | |
408 | } | |
409 | ||
410 | /** | |
411 | * ocp_early_init - Init OCP device management | |
412 | * | |
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) | |
417 | */ | |
418 | int __init | |
419 | ocp_early_init(void) | |
420 | { | |
421 | struct ocp_def *def; | |
422 | ||
423 | DBG(("ocp: ocp_early_init()...\n")); | |
424 | ||
425 | /* Fill the devices list */ | |
426 | for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++) | |
427 | ocp_add_one_device(def); | |
428 | ||
429 | DBG(("ocp: ocp_early_init()... done.\n")); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * ocp_driver_init - Init OCP device management | |
436 | * | |
437 | * This function is meant to be called via OCP bus registration. | |
438 | */ | |
439 | static int __init | |
440 | ocp_driver_init(void) | |
441 | { | |
442 | int ret = 0, index = 0; | |
443 | struct device *ocp_bus; | |
444 | struct list_head *entry; | |
445 | struct ocp_device *dev; | |
446 | ||
447 | if (ocp_inited) | |
448 | return ret; | |
449 | ocp_inited = 1; | |
450 | ||
451 | DBG(("ocp: ocp_driver_init()...\n")); | |
452 | ||
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"); | |
459 | ||
460 | bus_register(&ocp_bus_type); | |
461 | ||
462 | device_register(ocp_bus); | |
463 | ||
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++; | |
473 | } | |
474 | ||
475 | DBG(("ocp: ocp_driver_init()... done.\n")); | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | postcore_initcall(ocp_driver_init); | |
481 | ||
482 | EXPORT_SYMBOL(ocp_bus_type); | |
483 | EXPORT_SYMBOL(ocp_find_device); | |
484 | EXPORT_SYMBOL(ocp_register_driver); | |
485 | EXPORT_SYMBOL(ocp_unregister_driver); |