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