softlockup: fix invalid proc_handler for softlockup_panic
[pandora-kernel.git] / arch / ppc / syslib / ocp.c
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, 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 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.
36  */
37
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>
44 #include <linux/pm.h>
45 #include <linux/bootmem.h>
46 #include <linux/device.h>
47 #include <linux/rwsem.h>
48
49 #include <asm/io.h>
50 #include <asm/ocp.h>
51 #include <asm/errno.h>
52
53 //#define DBG(x)        printk x
54 #define DBG(x)
55
56 extern int mem_init_done;
57
58 extern struct ocp_def core_ocp[];       /* Static list of devices, provided by
59                                            CPU core */
60
61 LIST_HEAD(ocp_devices);                 /* List of all OCP devices */
62 DECLARE_RWSEM(ocp_devices_sem);         /* Global semaphores for those lists */
63
64 static int ocp_inited;
65
66 /* Sysfs support */
67 #define OCP_DEF_ATTR(field, format_string)                              \
68 static ssize_t                                                          \
69 show_##field(struct device *dev, struct device_attribute *attr, char *buf)                              \
70 {                                                                       \
71         struct ocp_device *odev = to_ocp_dev(dev);                      \
72                                                                         \
73         return sprintf(buf, format_string, odev->def->field);           \
74 }                                                                       \
75 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
76
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");
82 #else
83 OCP_DEF_ATTR(paddr, "0x%08lx\n");
84 #endif
85 OCP_DEF_ATTR(irq, "%d\n");
86 OCP_DEF_ATTR(pm, "%lu\n");
87
88 void ocp_create_sysfs_dev_files(struct ocp_device *odev)
89 {
90         struct device *dev = &odev->dev;
91
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);
102 }
103
104 /**
105  *      ocp_device_match        -       Match one driver to one device
106  *      @drv: driver to match
107  *      @dev: device to match
108  *
109  *      This function returns 0 if the driver and device don't match
110  */
111 static int
112 ocp_device_match(struct device *dev, struct device_driver *drv)
113 {
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;
117
118         if (!ids)
119                 return 0;
120
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))
126                         return 1;
127                 ids++;
128         }
129         return 0;
130 }
131
132 static int
133 ocp_device_probe(struct device *dev)
134 {
135         int error = 0;
136         struct ocp_driver *drv;
137         struct ocp_device *ocp_dev;
138
139         drv = to_ocp_drv(dev->driver);
140         ocp_dev = to_ocp_dev(dev);
141
142         if (drv->probe) {
143                 error = drv->probe(ocp_dev);
144                 if (error >= 0) {
145                         ocp_dev->driver = drv;
146                         error = 0;
147                 }
148         }
149         return error;
150 }
151
152 static int
153 ocp_device_remove(struct device *dev)
154 {
155         struct ocp_device *ocp_dev = to_ocp_dev(dev);
156
157         if (ocp_dev->driver) {
158                 if (ocp_dev->driver->remove)
159                         ocp_dev->driver->remove(ocp_dev);
160                 ocp_dev->driver = NULL;
161         }
162         return 0;
163 }
164
165 static int
166 ocp_device_suspend(struct device *dev, pm_message_t state)
167 {
168         struct ocp_device *ocp_dev = to_ocp_dev(dev);
169         struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
170
171         if (dev->driver && ocp_drv->suspend)
172                 return ocp_drv->suspend(ocp_dev, state);
173         return 0;
174 }
175
176 static int
177 ocp_device_resume(struct device *dev)
178 {
179         struct ocp_device *ocp_dev = to_ocp_dev(dev);
180         struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
181
182         if (dev->driver && ocp_drv->resume)
183                 return ocp_drv->resume(ocp_dev);
184         return 0;
185 }
186
187 struct bus_type ocp_bus_type = {
188         .name = "ocp",
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,
194 };
195
196 /**
197  *      ocp_register_driver     -       Register an OCP driver
198  *      @drv: pointer to statically defined ocp_driver structure
199  *
200  *      The driver's probe() callback is called either recursively
201  *      by this function or upon later call of ocp_driver_init
202  *
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.
206  */
207 int
208 ocp_register_driver(struct ocp_driver *drv)
209 {
210         /* initialize common driver fields */
211         drv->driver.name = drv->name;
212         drv->driver.bus = &ocp_bus_type;
213
214         /* register with core */
215         return driver_register(&drv->driver);
216 }
217
218 /**
219  *      ocp_unregister_driver   -       Unregister an OCP driver
220  *      @drv: pointer to statically defined ocp_driver structure
221  *
222  *      The driver's remove() callback is called recursively
223  *      by this function for any device already registered
224  */
225 void
226 ocp_unregister_driver(struct ocp_driver *drv)
227 {
228         DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name));
229
230         driver_unregister(&drv->driver);
231
232         DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name));
233 }
234
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)
238 {
239         struct list_head        *entry;
240         struct ocp_device       *dev, *found = NULL;
241
242         DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
243
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)
247                         continue;
248                 if (function != OCP_ANY_ID && function != dev->def->function)
249                         continue;
250                 if (index != OCP_ANY_INDEX && index != dev->def->index)
251                         continue;
252                 found = dev;
253                 break;
254         }
255
256         DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index));
257
258         return found;
259 }
260
261 /**
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)
266  *
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...
271  */
272 struct ocp_device *
273 ocp_find_device(unsigned int vendor, unsigned int function, int index)
274 {
275         struct ocp_device       *dev;
276
277         down_read(&ocp_devices_sem);
278         dev = __ocp_find_device(vendor, function, index);
279         up_read(&ocp_devices_sem);
280
281         return dev;
282 }
283
284 /**
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)
289  *
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
293  */
294 struct ocp_def *
295 ocp_get_one_device(unsigned int vendor, unsigned int function, int index)
296 {
297         struct ocp_device       *dev;
298         struct ocp_def          *found = NULL;
299
300         DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n",
301                 vendor, function, index));
302
303         dev = ocp_find_device(vendor, function, index);
304
305         if (dev)
306                 found = dev->def;
307
308         DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n",
309                 vendor, function, index));
310
311         return found;
312 }
313
314 /**
315  *      ocp_add_one_device      -       Add a device
316  *      @def: static device definition structure
317  *
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
321  *      otherwise.
322  */
323 int
324 ocp_add_one_device(struct ocp_def *def)
325 {
326         struct  ocp_device      *dev;
327
328         DBG(("ocp: ocp_add_one_device()...\n"));
329
330         /* Can't be called after ocp driver init */
331         if (ocp_inited)
332                 return 1;
333
334         if (mem_init_done)
335                 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
336         else
337                 dev = alloc_bootmem(sizeof(*dev));
338
339         if (dev == NULL)
340                 return 1;
341         memset(dev, 0, sizeof(*dev));
342         dev->def = def;
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);
349
350         DBG(("ocp: ocp_add_one_device()...done\n"));
351
352         return 0;
353 }
354
355 /**
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)
360  *
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.
364  */
365 int
366 ocp_remove_one_device(unsigned int vendor, unsigned int function, int index)
367 {
368         struct ocp_device *dev;
369
370         DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
371
372         /* Can't be called after ocp driver init */
373         if (ocp_inited)
374                 return 1;
375
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);
380
381         DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index));
382
383         return 0;
384 }
385
386 /**
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.
390  *
391  *      This routine holds the ocp_device semaphore, so the
392  *      callback routine cannot modify the ocp_device list.
393  */
394 void
395 ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg)
396 {
397         struct list_head *entry;
398
399         if (callback) {
400                 down_read(&ocp_devices_sem);
401                 list_for_each(entry, &ocp_devices)
402                         callback(list_entry(entry, struct ocp_device, link),
403                                 arg);
404                 up_read(&ocp_devices_sem);
405         }
406 }
407
408 /**
409  *      ocp_early_init  -       Init OCP device management
410  *
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
414  *      etc)
415  */
416 int __init
417 ocp_early_init(void)
418 {
419         struct ocp_def  *def;
420
421         DBG(("ocp: ocp_early_init()...\n"));
422
423         /* Fill the devices list */
424         for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++)
425                 ocp_add_one_device(def);
426
427         DBG(("ocp: ocp_early_init()... done.\n"));
428
429         return 0;
430 }
431
432 /**
433  *      ocp_driver_init -       Init OCP device management
434  *
435  *      This function is meant to be called via OCP bus registration.
436  */
437 static int __init
438 ocp_driver_init(void)
439 {
440         int ret = 0, index = 0;
441         struct device *ocp_bus;
442         struct list_head *entry;
443         struct ocp_device *dev;
444
445         if (ocp_inited)
446                 return ret;
447         ocp_inited = 1;
448
449         DBG(("ocp: ocp_driver_init()...\n"));
450
451         /* Allocate/register primary OCP bus */
452         ocp_bus = kzalloc(sizeof(struct device), GFP_KERNEL);
453         if (ocp_bus == NULL)
454                 return 1;
455         strcpy(ocp_bus->bus_id, "ocp");
456
457         bus_register(&ocp_bus_type);
458
459         device_register(ocp_bus);
460
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);
469                 index++;
470         }
471
472         DBG(("ocp: ocp_driver_init()... done.\n"));
473
474         return 0;
475 }
476
477 postcore_initcall(ocp_driver_init);
478
479 EXPORT_SYMBOL(ocp_bus_type);
480 EXPORT_SYMBOL(ocp_find_device);
481 EXPORT_SYMBOL(ocp_register_driver);
482 EXPORT_SYMBOL(ocp_unregister_driver);