[POWERPC] Move of_device allocation into of_device.[ch]
[deliverable/linux.git] / arch / powerpc / kernel / ibmebus.c
CommitLineData
d7a30103
HS
1/*
2 * IBM PowerPC IBM eBus Infrastructure Support.
3 *
4 * Copyright (c) 2005 IBM Corporation
6bccf755 5 * Joachim Fenkes <fenkes@de.ibm.com>
d7a30103 6 * Heiko J Schick <schickhj@de.ibm.com>
a8308800 7 *
d7a30103
HS
8 * All rights reserved.
9 *
a8308800
JF
10 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11 * BSD.
d7a30103
HS
12 *
13 * OpenIB BSD License
14 *
a8308800
JF
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
d7a30103 17 *
a8308800
JF
18 * Redistributions of source code must retain the above copyright notice, this
19 * list of conditions and the following disclaimer.
d7a30103 20 *
a8308800
JF
21 * Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
d7a30103 23 * and/or other materials
a8308800 24 * provided with the distribution.
d7a30103 25 *
a8308800
JF
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d7a30103
HS
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
a8308800
JF
34 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d7a30103
HS
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/kobject.h>
42#include <linux/dma-mapping.h>
43#include <linux/interrupt.h>
44#include <asm/ibmebus.h>
45#include <asm/abs_addr.h>
46
6bccf755
JF
47static struct device ibmebus_bus_device = { /* fake "parent" device */
48 .bus_id = "ibmebus",
d7a30103
HS
49};
50
6bccf755
JF
51struct bus_type ibmebus_bus_type;
52
d7a30103
HS
53static void *ibmebus_alloc_coherent(struct device *dev,
54 size_t size,
55 dma_addr_t *dma_handle,
56 gfp_t flag)
57{
58 void *mem;
a8308800 59
d7a30103
HS
60 mem = kmalloc(size, flag);
61 *dma_handle = (dma_addr_t)mem;
62
63 return mem;
64}
65
66static void ibmebus_free_coherent(struct device *dev,
a8308800 67 size_t size, void *vaddr,
d7a30103
HS
68 dma_addr_t dma_handle)
69{
70 kfree(vaddr);
71}
72
73static dma_addr_t ibmebus_map_single(struct device *dev,
74 void *ptr,
75 size_t size,
76 enum dma_data_direction direction)
77{
78 return (dma_addr_t)(ptr);
79}
80
81static void ibmebus_unmap_single(struct device *dev,
82 dma_addr_t dma_addr,
a8308800 83 size_t size,
d7a30103
HS
84 enum dma_data_direction direction)
85{
86 return;
87}
88
89static int ibmebus_map_sg(struct device *dev,
78bdc310 90 struct scatterlist *sgl,
d7a30103
HS
91 int nents, enum dma_data_direction direction)
92{
78bdc310 93 struct scatterlist *sg;
d7a30103 94 int i;
a8308800 95
78bdc310
JA
96 for_each_sg(sgl, sg, nents, i) {
97 sg->dma_address = (dma_addr_t)page_address(sg->page)
98 + sg->offset;
99 sg->dma_length = sg->length;
d7a30103 100 }
a8308800 101
d7a30103
HS
102 return nents;
103}
104
105static void ibmebus_unmap_sg(struct device *dev,
106 struct scatterlist *sg,
107 int nents, enum dma_data_direction direction)
108{
109 return;
110}
111
112static int ibmebus_dma_supported(struct device *dev, u64 mask)
113{
114 return 1;
115}
116
12d04eef 117static struct dma_mapping_ops ibmebus_dma_ops = {
d7a30103
HS
118 .alloc_coherent = ibmebus_alloc_coherent,
119 .free_coherent = ibmebus_free_coherent,
120 .map_single = ibmebus_map_single,
121 .unmap_single = ibmebus_unmap_single,
122 .map_sg = ibmebus_map_sg,
123 .unmap_sg = ibmebus_unmap_sg,
124 .dma_supported = ibmebus_dma_supported,
125};
126
127static int ibmebus_bus_probe(struct device *dev)
128{
129 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
130 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
131 const struct of_device_id *id;
132 int error = -ENODEV;
a8308800 133
d7a30103
HS
134 if (!ibmebusdrv->probe)
135 return error;
a8308800 136
d7a30103
HS
137 id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev);
138 if (id) {
139 error = ibmebusdrv->probe(ibmebusdev, id);
140 }
a8308800 141
d7a30103
HS
142 return error;
143}
144
145static int ibmebus_bus_remove(struct device *dev)
146{
147 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
148 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
a8308800 149
d7a30103
HS
150 if (ibmebusdrv->remove) {
151 return ibmebusdrv->remove(ibmebusdev);
152 }
a8308800 153
d7a30103
HS
154 return 0;
155}
156
157static void __devinit ibmebus_dev_release(struct device *dev)
158{
159 of_node_put(to_ibmebus_dev(dev)->ofdev.node);
160 kfree(to_ibmebus_dev(dev));
161}
162
6bccf755 163static int __devinit ibmebus_register_device_common(
a7f67bdf 164 struct ibmebus_dev *dev, const char *name)
d7a30103
HS
165{
166 int err = 0;
167
6bccf755 168 dev->ofdev.dev.parent = &ibmebus_bus_device;
d7a30103
HS
169 dev->ofdev.dev.bus = &ibmebus_bus_type;
170 dev->ofdev.dev.release = ibmebus_dev_release;
171
12d04eef
BH
172 dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
173 dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
174 dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
175
d7a30103 176 /* An ibmebusdev is based on a of_device. We have to change the
a8308800
JF
177 * bus type to use our own DMA mapping operations.
178 */
d7a30103
HS
179 if ((err = of_device_register(&dev->ofdev)) != 0) {
180 printk(KERN_ERR "%s: failed to register device (%d).\n",
181 __FUNCTION__, err);
6bccf755 182 return -ENODEV;
d7a30103 183 }
a8308800 184
6bccf755 185 return 0;
d7a30103
HS
186}
187
188static struct ibmebus_dev* __devinit ibmebus_register_device_node(
189 struct device_node *dn)
190{
191 struct ibmebus_dev *dev;
d8612417 192 int i, len, bus_len;
d7a30103 193
f8485350 194 dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
d8612417 195 if (!dev)
6bccf755 196 return ERR_PTR(-ENOMEM);
d7a30103
HS
197
198 dev->ofdev.node = of_node_get(dn);
a8308800 199
d8612417
JF
200 len = strlen(dn->full_name + 1);
201 bus_len = min(len, BUS_ID_SIZE - 1);
202 memcpy(dev->ofdev.dev.bus_id, dn->full_name + 1
203 + (len - bus_len), bus_len);
204 for (i = 0; i < bus_len; i++)
205 if (dev->ofdev.dev.bus_id[i] == '/')
206 dev->ofdev.dev.bus_id[i] = '_';
d7a30103
HS
207
208 /* Register with generic device framework. */
6bccf755 209 if (ibmebus_register_device_common(dev, dn->name) != 0) {
d7a30103 210 kfree(dev);
6bccf755 211 return ERR_PTR(-ENODEV);
d7a30103
HS
212 }
213
214 return dev;
215}
216
217static void ibmebus_probe_of_nodes(char* name)
218{
219 struct device_node *dn = NULL;
a8308800 220
d7a30103 221 while ((dn = of_find_node_by_name(dn, name))) {
6bccf755 222 if (IS_ERR(ibmebus_register_device_node(dn))) {
d7a30103 223 of_node_put(dn);
d7a30103
HS
224 return;
225 }
226 }
a8308800 227
d7a30103 228 of_node_put(dn);
a8308800 229
d7a30103
HS
230 return;
231}
232
233static void ibmebus_add_devices_by_id(struct of_device_id *idt)
234{
235 while (strlen(idt->name) > 0) {
236 ibmebus_probe_of_nodes(idt->name);
237 idt++;
238 }
239
240 return;
241}
242
0727702a 243static int ibmebus_match_name(struct device *dev, void *data)
d7a30103 244{
6bccf755 245 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
e2eb6392 246 const char *name;
6bccf755 247
e2eb6392 248 name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
6bccf755 249
e2eb6392 250 if (name && (strcmp(data, name) == 0))
d7a30103 251 return 1;
a8308800 252
d7a30103
HS
253 return 0;
254}
255
256static int ibmebus_unregister_device(struct device *dev)
257{
d7a30103
HS
258 of_device_unregister(to_of_device(dev));
259
260 return 0;
261}
262
263static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
264{
265 struct device *dev;
a8308800 266
d7a30103 267 while (strlen(idt->name) > 0) {
a8308800 268 while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
d7a30103 269 (void*)idt->name,
0727702a 270 ibmebus_match_name))) {
d7a30103
HS
271 ibmebus_unregister_device(dev);
272 }
273 idt++;
d7a30103 274 }
a8308800 275
d7a30103
HS
276 return;
277}
278
279int ibmebus_register_driver(struct ibmebus_driver *drv)
280{
281 int err = 0;
282
283 drv->driver.name = drv->name;
284 drv->driver.bus = &ibmebus_bus_type;
285 drv->driver.probe = ibmebus_bus_probe;
286 drv->driver.remove = ibmebus_bus_remove;
287
288 if ((err = driver_register(&drv->driver) != 0))
289 return err;
290
6bccf755
JF
291 /* remove all supported devices first, in case someone
292 * probed them manually before registering the driver */
293 ibmebus_remove_devices_by_id(drv->id_table);
d7a30103 294 ibmebus_add_devices_by_id(drv->id_table);
a8308800 295
d7a30103
HS
296 return 0;
297}
298EXPORT_SYMBOL(ibmebus_register_driver);
299
300void ibmebus_unregister_driver(struct ibmebus_driver *drv)
a8308800 301{
d7a30103
HS
302 driver_unregister(&drv->driver);
303 ibmebus_remove_devices_by_id(drv->id_table);
304}
305EXPORT_SYMBOL(ibmebus_unregister_driver);
306
307int ibmebus_request_irq(struct ibmebus_dev *dev,
a8308800 308 u32 ist,
40220c1a 309 irq_handler_t handler,
d7a30103
HS
310 unsigned long irq_flags, const char * devname,
311 void *dev_id)
312{
6e99e458 313 unsigned int irq = irq_create_mapping(NULL, ist);
a8308800 314
d7a30103
HS
315 if (irq == NO_IRQ)
316 return -EINVAL;
a8308800 317
d7a30103
HS
318 return request_irq(irq, handler,
319 irq_flags, devname, dev_id);
320}
321EXPORT_SYMBOL(ibmebus_request_irq);
322
323void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
324{
0ebfff14 325 unsigned int irq = irq_find_mapping(NULL, ist);
a8308800 326
d7a30103 327 free_irq(irq, dev_id);
d7a30103
HS
328}
329EXPORT_SYMBOL(ibmebus_free_irq);
330
331static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
a8308800 332{
d7a30103
HS
333 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
334 struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
335 const struct of_device_id *ids = ebus_drv->id_table;
336 const struct of_device_id *found_id;
a8308800 337
d7a30103
HS
338 if (!ids)
339 return 0;
a8308800 340
d7a30103
HS
341 found_id = of_match_device(ids, &ebus_dev->ofdev);
342 if (found_id)
343 return 1;
a8308800 344
d7a30103
HS
345 return 0;
346}
347
6bccf755
JF
348static ssize_t name_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
e2eb6392 352 const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
6bccf755
JF
353 return sprintf(buf, "%s\n", name);
354}
355
356static struct device_attribute ibmebus_dev_attrs[] = {
357 __ATTR_RO(name),
358 __ATTR_NULL
359};
360
0727702a 361static int ibmebus_match_path(struct device *dev, void *data)
6bccf755 362{
0727702a
JF
363 int rc;
364 struct device_node *dn =
365 of_node_get(to_ibmebus_dev(dev)->ofdev.node);
6bccf755 366
0727702a 367 rc = (dn->full_name && (strcasecmp((char*)data, dn->full_name) == 0));
6bccf755 368
0727702a
JF
369 of_node_put(dn);
370 return rc;
371}
6bccf755 372
0727702a
JF
373static char *ibmebus_chomp(const char *in, size_t count)
374{
61a564fd
JJ
375 char *out = kmalloc(count + 1, GFP_KERNEL);
376
0727702a
JF
377 if (!out)
378 return NULL;
379
380 memcpy(out, in, count);
381 out[count] = '\0';
382 if (out[count - 1] == '\n')
383 out[count - 1] = '\0';
384
385 return out;
6bccf755
JF
386}
387
388static ssize_t ibmebus_store_probe(struct bus_type *bus,
389 const char *buf, size_t count)
390{
391 struct device_node *dn = NULL;
392 struct ibmebus_dev *dev;
0727702a
JF
393 char *path;
394 ssize_t rc;
395
396 path = ibmebus_chomp(buf, count);
397 if (!path)
398 return -ENOMEM;
399
400 if (bus_find_device(&ibmebus_bus_type, NULL, path,
74c9b99d 401 ibmebus_match_path)) {
0727702a
JF
402 printk(KERN_WARNING "%s: %s has already been probed\n",
403 __FUNCTION__, path);
74c9b99d 404 rc = -EEXIST;
0727702a 405 goto out;
6bccf755
JF
406 }
407
0727702a
JF
408 if ((dn = of_find_node_by_path(path))) {
409 dev = ibmebus_register_device_node(dn);
410 of_node_put(dn);
411 rc = IS_ERR(dev) ? PTR_ERR(dev) : count;
412 } else {
413 printk(KERN_WARNING "%s: no such device node: %s\n",
414 __FUNCTION__, path);
415 rc = -ENODEV;
6bccf755
JF
416 }
417
0727702a
JF
418out:
419 kfree(path);
420 return rc;
6bccf755
JF
421}
422
423static ssize_t ibmebus_store_remove(struct bus_type *bus,
424 const char *buf, size_t count)
425{
426 struct device *dev;
0727702a 427 char *path;
6bccf755 428
0727702a
JF
429 path = ibmebus_chomp(buf, count);
430 if (!path)
431 return -ENOMEM;
432
433 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
434 ibmebus_match_path))) {
6bccf755 435 ibmebus_unregister_device(dev);
0727702a
JF
436
437 kfree(path);
438 return count;
6bccf755 439 } else {
0727702a
JF
440 printk(KERN_WARNING "%s: %s not on the bus\n",
441 __FUNCTION__, path);
442
443 kfree(path);
6bccf755
JF
444 return -ENODEV;
445 }
6bccf755
JF
446}
447
448static struct bus_attribute ibmebus_bus_attrs[] = {
449 __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
450 __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
451 __ATTR_NULL
452};
453
d7a30103 454struct bus_type ibmebus_bus_type = {
6bccf755
JF
455 .name = "ibmebus",
456 .match = ibmebus_bus_match,
457 .dev_attrs = ibmebus_dev_attrs,
458 .bus_attrs = ibmebus_bus_attrs
d7a30103
HS
459};
460EXPORT_SYMBOL(ibmebus_bus_type);
461
462static int __init ibmebus_bus_init(void)
463{
464 int err;
a8308800 465
d7a30103 466 printk(KERN_INFO "IBM eBus Device Driver\n");
a8308800 467
d7a30103
HS
468 err = bus_register(&ibmebus_bus_type);
469 if (err) {
470 printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
471 __FUNCTION__);
472 return err;
473 }
a8308800 474
6bccf755 475 err = device_register(&ibmebus_bus_device);
d7a30103 476 if (err) {
a8308800 477 printk(KERN_WARNING "%s: device_register returned %i\n",
d7a30103
HS
478 __FUNCTION__, err);
479 bus_unregister(&ibmebus_bus_type);
480
481 return err;
482 }
a8308800 483
d7a30103
HS
484 return 0;
485}
486__initcall(ibmebus_bus_init);
This page took 0.227831 seconds and 5 git commands to generate.