J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Freescale Management Complex (MC) bus driver |
| 3 | * |
| 4 | * Copyright (C) 2014 Freescale Semiconductor, Inc. |
| 5 | * Author: German Rivera <German.Rivera@freescale.com> |
| 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public |
| 8 | * License version 2. This program is licensed "as is" without any |
| 9 | * warranty of any kind, whether express or implied. |
| 10 | */ |
| 11 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/of_device.h> |
| 14 | #include <linux/of_address.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/limits.h> |
J. German Rivera | 660a24b | 2016-01-06 16:03:29 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
| 19 | #include <linux/msi.h> |
Stuart Yoder | 5143ecf | 2016-08-23 17:14:18 -0500 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Stuart Yoder | d4e7513 | 2016-08-23 17:14:23 -0500 | [diff] [blame] | 21 | #include "../include/mc-bus.h" |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 22 | #include "../include/dpmng.h" |
| 23 | #include "../include/mc-sys.h" |
Stuart Yoder | d4e7513 | 2016-08-23 17:14:23 -0500 | [diff] [blame] | 24 | |
Stuart Yoder | 243444f | 2016-08-23 17:13:30 -0500 | [diff] [blame] | 25 | #include "fsl-mc-private.h" |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 26 | #include "dprc-cmd.h" |
| 27 | |
| 28 | static struct kmem_cache *mc_dev_cache; |
| 29 | |
| 30 | /** |
Stuart Yoder | e730d86 | 2016-08-23 17:13:40 -0500 | [diff] [blame] | 31 | * Default DMA mask for devices on a fsl-mc bus |
| 32 | */ |
| 33 | #define FSL_MC_DEFAULT_DMA_MASK (~0ULL) |
| 34 | |
| 35 | /** |
| 36 | * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device |
| 37 | * @root_mc_bus_dev: MC object device representing the root DPRC |
| 38 | * @num_translation_ranges: number of entries in addr_translation_ranges |
| 39 | * @translation_ranges: array of bus to system address translation ranges |
| 40 | */ |
| 41 | struct fsl_mc { |
| 42 | struct fsl_mc_device *root_mc_bus_dev; |
| 43 | u8 num_translation_ranges; |
| 44 | struct fsl_mc_addr_translation_range *translation_ranges; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * struct fsl_mc_addr_translation_range - bus to system address translation |
| 49 | * range |
| 50 | * @mc_region_type: Type of MC region for the range being translated |
| 51 | * @start_mc_offset: Start MC offset of the range being translated |
| 52 | * @end_mc_offset: MC offset of the first byte after the range (last MC |
| 53 | * offset of the range is end_mc_offset - 1) |
| 54 | * @start_phys_addr: system physical address corresponding to start_mc_addr |
| 55 | */ |
| 56 | struct fsl_mc_addr_translation_range { |
| 57 | enum dprc_region_type mc_region_type; |
| 58 | u64 start_mc_offset; |
| 59 | u64 end_mc_offset; |
| 60 | phys_addr_t start_phys_addr; |
| 61 | }; |
| 62 | |
| 63 | /** |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 64 | * fsl_mc_bus_match - device to driver matching callback |
| 65 | * @dev: the MC object device structure to match against |
| 66 | * @drv: the device driver to search for matching MC object device id |
| 67 | * structures |
| 68 | * |
| 69 | * Returns 1 on success, 0 otherwise. |
| 70 | */ |
| 71 | static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv) |
| 72 | { |
Stuart Yoder | 57538af | 2016-06-22 16:40:44 -0500 | [diff] [blame] | 73 | const struct fsl_mc_device_id *id; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 74 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 75 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv); |
| 76 | bool found = false; |
| 77 | |
Itai Katz | 14f9280 | 2015-10-04 10:09:50 +0300 | [diff] [blame] | 78 | if (WARN_ON(!fsl_mc_bus_exists())) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 79 | goto out; |
| 80 | |
| 81 | if (!mc_drv->match_id_table) |
| 82 | goto out; |
| 83 | |
| 84 | /* |
| 85 | * If the object is not 'plugged' don't match. |
| 86 | * Only exception is the root DPRC, which is a special case. |
| 87 | */ |
| 88 | if ((mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED) == 0 && |
Itai Katz | b55f00c | 2015-10-04 10:09:51 +0300 | [diff] [blame] | 89 | !fsl_mc_is_root_dprc(&mc_dev->dev)) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 90 | goto out; |
| 91 | |
| 92 | /* |
| 93 | * Traverse the match_id table of the given driver, trying to find |
| 94 | * a matching for the given MC object device. |
| 95 | */ |
| 96 | for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) { |
| 97 | if (id->vendor == mc_dev->obj_desc.vendor && |
J. German Rivera | 3b0a9b1 | 2015-03-27 16:01:09 -0500 | [diff] [blame] | 98 | strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) { |
Itai Katz | 9787d4e | 2016-04-11 11:55:40 -0500 | [diff] [blame] | 99 | found = true; |
J. German Rivera | 3b0a9b1 | 2015-03-27 16:01:09 -0500 | [diff] [blame] | 100 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | out: |
| 106 | dev_dbg(dev, "%smatched\n", found ? "" : "not "); |
| 107 | return found; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * fsl_mc_bus_uevent - callback invoked when a device is added |
| 112 | */ |
| 113 | static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 114 | { |
Stuart Yoder | d568b76 | 2016-06-22 16:40:43 -0500 | [diff] [blame] | 115 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 116 | |
| 117 | if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s", |
| 118 | mc_dev->obj_desc.vendor, |
| 119 | mc_dev->obj_desc.type)) |
| 120 | return -ENOMEM; |
| 121 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Stuart Yoder | 3d579c3 | 2016-06-22 16:40:42 -0500 | [diff] [blame] | 125 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 126 | char *buf) |
| 127 | { |
| 128 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 129 | |
| 130 | return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor, |
| 131 | mc_dev->obj_desc.type); |
| 132 | } |
| 133 | static DEVICE_ATTR_RO(modalias); |
| 134 | |
| 135 | static struct attribute *fsl_mc_dev_attrs[] = { |
| 136 | &dev_attr_modalias.attr, |
| 137 | NULL, |
| 138 | }; |
| 139 | |
Wei Yongjun | a71a6d9 | 2016-08-28 16:19:29 +0000 | [diff] [blame] | 140 | ATTRIBUTE_GROUPS(fsl_mc_dev); |
Stuart Yoder | 3d579c3 | 2016-06-22 16:40:42 -0500 | [diff] [blame] | 141 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 142 | struct bus_type fsl_mc_bus_type = { |
| 143 | .name = "fsl-mc", |
| 144 | .match = fsl_mc_bus_match, |
| 145 | .uevent = fsl_mc_bus_uevent, |
Stuart Yoder | 3d579c3 | 2016-06-22 16:40:42 -0500 | [diff] [blame] | 146 | .dev_groups = fsl_mc_dev_groups, |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 147 | }; |
| 148 | EXPORT_SYMBOL_GPL(fsl_mc_bus_type); |
| 149 | |
Itai Katz | 565b945 | 2015-10-04 10:09:53 +0300 | [diff] [blame] | 150 | static atomic_t root_dprc_count = ATOMIC_INIT(0); |
| 151 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 152 | static int fsl_mc_driver_probe(struct device *dev) |
| 153 | { |
| 154 | struct fsl_mc_driver *mc_drv; |
| 155 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 156 | int error; |
| 157 | |
| 158 | if (WARN_ON(!dev->driver)) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | mc_drv = to_fsl_mc_driver(dev->driver); |
| 162 | if (WARN_ON(!mc_drv->probe)) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | error = mc_drv->probe(mc_dev); |
| 166 | if (error < 0) { |
| 167 | dev_err(dev, "MC object device probe callback failed: %d\n", |
| 168 | error); |
| 169 | return error; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int fsl_mc_driver_remove(struct device *dev) |
| 176 | { |
| 177 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); |
| 178 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 179 | int error; |
| 180 | |
| 181 | if (WARN_ON(!dev->driver)) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | error = mc_drv->remove(mc_dev); |
| 185 | if (error < 0) { |
| 186 | dev_err(dev, |
| 187 | "MC object device remove callback failed: %d\n", |
| 188 | error); |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void fsl_mc_driver_shutdown(struct device *dev) |
| 196 | { |
| 197 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); |
| 198 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 199 | |
| 200 | mc_drv->shutdown(mc_dev); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * __fsl_mc_driver_register - registers a child device driver with the |
| 205 | * MC bus |
| 206 | * |
| 207 | * This function is implicitly invoked from the registration function of |
| 208 | * fsl_mc device drivers, which is generated by the |
| 209 | * module_fsl_mc_driver() macro. |
| 210 | */ |
| 211 | int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver, |
| 212 | struct module *owner) |
| 213 | { |
| 214 | int error; |
| 215 | |
| 216 | mc_driver->driver.owner = owner; |
| 217 | mc_driver->driver.bus = &fsl_mc_bus_type; |
| 218 | |
| 219 | if (mc_driver->probe) |
| 220 | mc_driver->driver.probe = fsl_mc_driver_probe; |
| 221 | |
| 222 | if (mc_driver->remove) |
| 223 | mc_driver->driver.remove = fsl_mc_driver_remove; |
| 224 | |
| 225 | if (mc_driver->shutdown) |
| 226 | mc_driver->driver.shutdown = fsl_mc_driver_shutdown; |
| 227 | |
| 228 | error = driver_register(&mc_driver->driver); |
| 229 | if (error < 0) { |
| 230 | pr_err("driver_register() failed for %s: %d\n", |
| 231 | mc_driver->driver.name, error); |
| 232 | return error; |
| 233 | } |
| 234 | |
| 235 | pr_info("MC object device driver %s registered\n", |
| 236 | mc_driver->driver.name); |
| 237 | return 0; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(__fsl_mc_driver_register); |
| 240 | |
| 241 | /** |
| 242 | * fsl_mc_driver_unregister - unregisters a device driver from the |
| 243 | * MC bus |
| 244 | */ |
| 245 | void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver) |
| 246 | { |
| 247 | driver_unregister(&mc_driver->driver); |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister); |
| 250 | |
Itai Katz | 14f9280 | 2015-10-04 10:09:50 +0300 | [diff] [blame] | 251 | /** |
| 252 | * fsl_mc_bus_exists - check if a root dprc exists |
| 253 | */ |
| 254 | bool fsl_mc_bus_exists(void) |
| 255 | { |
Itai Katz | 565b945 | 2015-10-04 10:09:53 +0300 | [diff] [blame] | 256 | return atomic_read(&root_dprc_count) > 0; |
Itai Katz | 14f9280 | 2015-10-04 10:09:50 +0300 | [diff] [blame] | 257 | } |
| 258 | EXPORT_SYMBOL_GPL(fsl_mc_bus_exists); |
| 259 | |
Itai Katz | b55f00c | 2015-10-04 10:09:51 +0300 | [diff] [blame] | 260 | /** |
Ramiro Oliveira | f86a180 | 2016-09-30 15:01:21 +0100 | [diff] [blame] | 261 | * fsl_mc_get_root_dprc - function to traverse to the root dprc |
| 262 | */ |
Stuart Yoder | 27365d8 | 2016-08-23 17:13:46 -0500 | [diff] [blame] | 263 | void fsl_mc_get_root_dprc(struct device *dev, |
| 264 | struct device **root_dprc_dev) |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 265 | { |
| 266 | if (WARN_ON(!dev)) { |
| 267 | *root_dprc_dev = NULL; |
Nipun Gupta | df5e9b5 | 2016-06-29 22:44:39 +0530 | [diff] [blame] | 268 | } else if (WARN_ON(!dev_is_fsl_mc(dev))) { |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 269 | *root_dprc_dev = NULL; |
| 270 | } else { |
| 271 | *root_dprc_dev = dev; |
Nipun Gupta | df5e9b5 | 2016-06-29 22:44:39 +0530 | [diff] [blame] | 272 | while (dev_is_fsl_mc((*root_dprc_dev)->parent)) |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 273 | *root_dprc_dev = (*root_dprc_dev)->parent; |
| 274 | } |
| 275 | } |
Stuart Yoder | 27365d8 | 2016-08-23 17:13:46 -0500 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(fsl_mc_get_root_dprc); |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 277 | |
Itai Katz | 9529d16 | 2016-04-11 11:55:55 -0500 | [diff] [blame] | 278 | static int get_dprc_attr(struct fsl_mc_io *mc_io, |
| 279 | int container_id, struct dprc_attributes *attr) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 280 | { |
Nayeemahmed Badebade | a2f9ff6 | 2015-09-04 22:30:33 +0530 | [diff] [blame] | 281 | u16 dprc_handle; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 282 | int error; |
| 283 | |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 284 | error = dprc_open(mc_io, 0, container_id, &dprc_handle); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 285 | if (error < 0) { |
Cihangir Akturk | 2e11590 | 2016-03-14 18:14:07 +0200 | [diff] [blame] | 286 | dev_err(mc_io->dev, "dprc_open() failed: %d\n", error); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 287 | return error; |
| 288 | } |
| 289 | |
Itai Katz | 9529d16 | 2016-04-11 11:55:55 -0500 | [diff] [blame] | 290 | memset(attr, 0, sizeof(struct dprc_attributes)); |
| 291 | error = dprc_get_attributes(mc_io, 0, dprc_handle, attr); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 292 | if (error < 0) { |
Cihangir Akturk | 2e11590 | 2016-03-14 18:14:07 +0200 | [diff] [blame] | 293 | dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n", |
Bhumika Goyal | 454b0ec | 2016-03-04 19:15:55 +0530 | [diff] [blame] | 294 | error); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 295 | goto common_cleanup; |
| 296 | } |
| 297 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 298 | error = 0; |
| 299 | |
| 300 | common_cleanup: |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 301 | (void)dprc_close(mc_io, 0, dprc_handle); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 302 | return error; |
| 303 | } |
| 304 | |
Itai Katz | 9529d16 | 2016-04-11 11:55:55 -0500 | [diff] [blame] | 305 | static int get_dprc_icid(struct fsl_mc_io *mc_io, |
| 306 | int container_id, u16 *icid) |
| 307 | { |
| 308 | struct dprc_attributes attr; |
| 309 | int error; |
| 310 | |
| 311 | error = get_dprc_attr(mc_io, container_id, &attr); |
| 312 | if (error == 0) |
| 313 | *icid = attr.icid; |
| 314 | |
| 315 | return error; |
| 316 | } |
| 317 | |
| 318 | static int get_dprc_version(struct fsl_mc_io *mc_io, |
| 319 | int container_id, u16 *major, u16 *minor) |
| 320 | { |
| 321 | struct dprc_attributes attr; |
| 322 | int error; |
| 323 | |
| 324 | error = get_dprc_attr(mc_io, container_id, &attr); |
| 325 | if (error == 0) { |
| 326 | *major = attr.version.major; |
| 327 | *minor = attr.version.minor; |
| 328 | } |
| 329 | |
| 330 | return error; |
| 331 | } |
| 332 | |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 333 | static int translate_mc_addr(struct fsl_mc_device *mc_dev, |
| 334 | enum dprc_region_type mc_region_type, |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 335 | u64 mc_offset, phys_addr_t *phys_addr) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 336 | { |
| 337 | int i; |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 338 | struct device *root_dprc_dev; |
| 339 | struct fsl_mc *mc; |
| 340 | |
| 341 | fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev); |
| 342 | if (WARN_ON(!root_dprc_dev)) |
| 343 | return -EINVAL; |
| 344 | mc = dev_get_drvdata(root_dprc_dev->parent); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 345 | |
| 346 | if (mc->num_translation_ranges == 0) { |
| 347 | /* |
| 348 | * Do identity mapping: |
| 349 | */ |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 350 | *phys_addr = mc_offset; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | for (i = 0; i < mc->num_translation_ranges; i++) { |
| 355 | struct fsl_mc_addr_translation_range *range = |
| 356 | &mc->translation_ranges[i]; |
| 357 | |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 358 | if (mc_region_type == range->mc_region_type && |
| 359 | mc_offset >= range->start_mc_offset && |
| 360 | mc_offset < range->end_mc_offset) { |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 361 | *phys_addr = range->start_phys_addr + |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 362 | (mc_offset - range->start_mc_offset); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return -EFAULT; |
| 368 | } |
| 369 | |
| 370 | static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev, |
| 371 | struct fsl_mc_device *mc_bus_dev) |
| 372 | { |
| 373 | int i; |
| 374 | int error; |
| 375 | struct resource *regions; |
| 376 | struct dprc_obj_desc *obj_desc = &mc_dev->obj_desc; |
| 377 | struct device *parent_dev = mc_dev->dev.parent; |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 378 | enum dprc_region_type mc_region_type; |
| 379 | |
| 380 | if (strcmp(obj_desc->type, "dprc") == 0 || |
| 381 | strcmp(obj_desc->type, "dpmcp") == 0) { |
| 382 | mc_region_type = DPRC_REGION_TYPE_MC_PORTAL; |
| 383 | } else if (strcmp(obj_desc->type, "dpio") == 0) { |
| 384 | mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL; |
| 385 | } else { |
| 386 | /* |
| 387 | * This function should not have been called for this MC object |
| 388 | * type, as this object type is not supposed to have MMIO |
| 389 | * regions |
| 390 | */ |
| 391 | WARN_ON(true); |
| 392 | return -EINVAL; |
| 393 | } |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 394 | |
| 395 | regions = kmalloc_array(obj_desc->region_count, |
| 396 | sizeof(regions[0]), GFP_KERNEL); |
| 397 | if (!regions) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | for (i = 0; i < obj_desc->region_count; i++) { |
| 401 | struct dprc_region_desc region_desc; |
| 402 | |
| 403 | error = dprc_get_obj_region(mc_bus_dev->mc_io, |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 404 | 0, |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 405 | mc_bus_dev->mc_handle, |
| 406 | obj_desc->type, |
| 407 | obj_desc->id, i, ®ion_desc); |
| 408 | if (error < 0) { |
| 409 | dev_err(parent_dev, |
| 410 | "dprc_get_obj_region() failed: %d\n", error); |
| 411 | goto error_cleanup_regions; |
| 412 | } |
| 413 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 414 | WARN_ON(region_desc.size == 0); |
Itai Katz | e4ea40f | 2015-10-04 10:09:52 +0300 | [diff] [blame] | 415 | error = translate_mc_addr(mc_dev, mc_region_type, |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 416 | region_desc.base_offset, |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 417 | ®ions[i].start); |
| 418 | if (error < 0) { |
| 419 | dev_err(parent_dev, |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 420 | "Invalid MC offset: %#x (for %s.%d\'s region %d)\n", |
| 421 | region_desc.base_offset, |
| 422 | obj_desc->type, obj_desc->id, i); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 423 | goto error_cleanup_regions; |
| 424 | } |
| 425 | |
| 426 | regions[i].end = regions[i].start + region_desc.size - 1; |
| 427 | regions[i].name = "fsl-mc object MMIO region"; |
| 428 | regions[i].flags = IORESOURCE_IO; |
Itai Katz | b3721fc | 2016-04-11 11:55:48 -0500 | [diff] [blame] | 429 | if (region_desc.flags & DPRC_REGION_CACHEABLE) |
| 430 | regions[i].flags |= IORESOURCE_CACHEABLE; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | mc_dev->regions = regions; |
| 434 | return 0; |
| 435 | |
| 436 | error_cleanup_regions: |
| 437 | kfree(regions); |
| 438 | return error; |
| 439 | } |
| 440 | |
| 441 | /** |
Stuart Yoder | fde867d | 2016-06-22 16:40:47 -0500 | [diff] [blame] | 442 | * fsl_mc_is_root_dprc - function to check if a given device is a root dprc |
| 443 | */ |
| 444 | bool fsl_mc_is_root_dprc(struct device *dev) |
| 445 | { |
| 446 | struct device *root_dprc_dev; |
| 447 | |
| 448 | fsl_mc_get_root_dprc(dev, &root_dprc_dev); |
| 449 | if (!root_dprc_dev) |
| 450 | return false; |
| 451 | return dev == root_dprc_dev; |
| 452 | } |
| 453 | |
| 454 | /** |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 455 | * Add a newly discovered MC object device to be visible in Linux |
| 456 | */ |
| 457 | int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, |
| 458 | struct fsl_mc_io *mc_io, |
| 459 | struct device *parent_dev, |
| 460 | struct fsl_mc_device **new_mc_dev) |
| 461 | { |
| 462 | int error; |
| 463 | struct fsl_mc_device *mc_dev = NULL; |
| 464 | struct fsl_mc_bus *mc_bus = NULL; |
| 465 | struct fsl_mc_device *parent_mc_dev; |
| 466 | |
Nipun Gupta | df5e9b5 | 2016-06-29 22:44:39 +0530 | [diff] [blame] | 467 | if (dev_is_fsl_mc(parent_dev)) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 468 | parent_mc_dev = to_fsl_mc_device(parent_dev); |
| 469 | else |
| 470 | parent_mc_dev = NULL; |
| 471 | |
| 472 | if (strcmp(obj_desc->type, "dprc") == 0) { |
| 473 | /* |
| 474 | * Allocate an MC bus device object: |
| 475 | */ |
| 476 | mc_bus = devm_kzalloc(parent_dev, sizeof(*mc_bus), GFP_KERNEL); |
| 477 | if (!mc_bus) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | mc_dev = &mc_bus->mc_dev; |
| 481 | } else { |
| 482 | /* |
| 483 | * Allocate a regular fsl_mc_device object: |
| 484 | */ |
| 485 | mc_dev = kmem_cache_zalloc(mc_dev_cache, GFP_KERNEL); |
| 486 | if (!mc_dev) |
| 487 | return -ENOMEM; |
| 488 | } |
| 489 | |
| 490 | mc_dev->obj_desc = *obj_desc; |
| 491 | mc_dev->mc_io = mc_io; |
| 492 | device_initialize(&mc_dev->dev); |
| 493 | mc_dev->dev.parent = parent_dev; |
| 494 | mc_dev->dev.bus = &fsl_mc_bus_type; |
J. German Rivera | 77371fb | 2015-03-27 16:01:04 -0500 | [diff] [blame] | 495 | dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 496 | |
| 497 | if (strcmp(obj_desc->type, "dprc") == 0) { |
| 498 | struct fsl_mc_io *mc_io2; |
| 499 | |
| 500 | mc_dev->flags |= FSL_MC_IS_DPRC; |
| 501 | |
| 502 | /* |
| 503 | * To get the DPRC's ICID, we need to open the DPRC |
| 504 | * in get_dprc_icid(). For child DPRCs, we do so using the |
| 505 | * parent DPRC's MC portal instead of the child DPRC's MC |
| 506 | * portal, in case the child DPRC is already opened with |
| 507 | * its own portal (e.g., the DPRC used by AIOP). |
| 508 | * |
| 509 | * NOTE: There cannot be more than one active open for a |
| 510 | * given MC object, using the same MC portal. |
| 511 | */ |
| 512 | if (parent_mc_dev) { |
| 513 | /* |
| 514 | * device being added is a child DPRC device |
| 515 | */ |
| 516 | mc_io2 = parent_mc_dev->mc_io; |
| 517 | } else { |
| 518 | /* |
| 519 | * device being added is the root DPRC device |
| 520 | */ |
| 521 | if (WARN_ON(!mc_io)) { |
| 522 | error = -EINVAL; |
| 523 | goto error_cleanup_dev; |
| 524 | } |
| 525 | |
| 526 | mc_io2 = mc_io; |
| 527 | |
Itai Katz | 565b945 | 2015-10-04 10:09:53 +0300 | [diff] [blame] | 528 | atomic_inc(&root_dprc_count); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid); |
| 532 | if (error < 0) |
| 533 | goto error_cleanup_dev; |
| 534 | } else { |
| 535 | /* |
| 536 | * A non-DPRC MC object device has to be a child of another |
| 537 | * MC object (specifically a DPRC object) |
| 538 | */ |
| 539 | mc_dev->icid = parent_mc_dev->icid; |
| 540 | mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK; |
| 541 | mc_dev->dev.dma_mask = &mc_dev->dma_mask; |
J. German Rivera | 660a24b | 2016-01-06 16:03:29 -0600 | [diff] [blame] | 542 | dev_set_msi_domain(&mc_dev->dev, |
| 543 | dev_get_msi_domain(&parent_mc_dev->dev)); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Get MMIO regions for the device from the MC: |
| 548 | * |
| 549 | * NOTE: the root DPRC is a special case as its MMIO region is |
| 550 | * obtained from the device tree |
| 551 | */ |
| 552 | if (parent_mc_dev && obj_desc->region_count != 0) { |
| 553 | error = fsl_mc_device_get_mmio_regions(mc_dev, |
| 554 | parent_mc_dev); |
| 555 | if (error < 0) |
| 556 | goto error_cleanup_dev; |
| 557 | } |
| 558 | |
Stuart Yoder | 0f90f25 | 2016-04-11 11:49:13 -0500 | [diff] [blame] | 559 | /* Objects are coherent, unless 'no shareability' flag set. */ |
| 560 | if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY)) |
| 561 | arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true); |
| 562 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 563 | /* |
| 564 | * The device-specific probe callback will get invoked by device_add() |
| 565 | */ |
| 566 | error = device_add(&mc_dev->dev); |
| 567 | if (error < 0) { |
| 568 | dev_err(parent_dev, |
| 569 | "device_add() failed for device %s: %d\n", |
| 570 | dev_name(&mc_dev->dev), error); |
| 571 | goto error_cleanup_dev; |
| 572 | } |
| 573 | |
| 574 | (void)get_device(&mc_dev->dev); |
| 575 | dev_dbg(parent_dev, "Added MC object device %s\n", |
| 576 | dev_name(&mc_dev->dev)); |
| 577 | |
| 578 | *new_mc_dev = mc_dev; |
| 579 | return 0; |
| 580 | |
| 581 | error_cleanup_dev: |
| 582 | kfree(mc_dev->regions); |
| 583 | if (mc_bus) |
| 584 | devm_kfree(parent_dev, mc_bus); |
| 585 | else |
| 586 | kmem_cache_free(mc_dev_cache, mc_dev); |
| 587 | |
| 588 | return error; |
| 589 | } |
| 590 | EXPORT_SYMBOL_GPL(fsl_mc_device_add); |
| 591 | |
| 592 | /** |
| 593 | * fsl_mc_device_remove - Remove a MC object device from being visible to |
| 594 | * Linux |
| 595 | * |
| 596 | * @mc_dev: Pointer to a MC object device object |
| 597 | */ |
| 598 | void fsl_mc_device_remove(struct fsl_mc_device *mc_dev) |
| 599 | { |
| 600 | struct fsl_mc_bus *mc_bus = NULL; |
| 601 | |
| 602 | kfree(mc_dev->regions); |
| 603 | |
| 604 | /* |
| 605 | * The device-specific remove callback will get invoked by device_del() |
| 606 | */ |
| 607 | device_del(&mc_dev->dev); |
| 608 | put_device(&mc_dev->dev); |
| 609 | |
| 610 | if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) { |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 611 | mc_bus = to_fsl_mc_bus(mc_dev); |
J. German Rivera | 2bdc55d | 2015-03-27 16:01:07 -0500 | [diff] [blame] | 612 | |
Itai Katz | 565b945 | 2015-10-04 10:09:53 +0300 | [diff] [blame] | 613 | if (fsl_mc_is_root_dprc(&mc_dev->dev)) { |
Itai Katz | 565b945 | 2015-10-04 10:09:53 +0300 | [diff] [blame] | 614 | if (atomic_read(&root_dprc_count) > 0) |
| 615 | atomic_dec(&root_dprc_count); |
| 616 | else |
| 617 | WARN_ON(1); |
| 618 | } |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 619 | } |
| 620 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 621 | if (mc_bus) |
| 622 | devm_kfree(mc_dev->dev.parent, mc_bus); |
| 623 | else |
| 624 | kmem_cache_free(mc_dev_cache, mc_dev); |
| 625 | } |
| 626 | EXPORT_SYMBOL_GPL(fsl_mc_device_remove); |
| 627 | |
| 628 | static int parse_mc_ranges(struct device *dev, |
| 629 | int *paddr_cells, |
| 630 | int *mc_addr_cells, |
| 631 | int *mc_size_cells, |
| 632 | const __be32 **ranges_start, |
J. German Rivera | ba72f25 | 2015-09-25 11:21:01 -0500 | [diff] [blame] | 633 | u8 *num_ranges) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 634 | { |
| 635 | const __be32 *prop; |
| 636 | int range_tuple_cell_count; |
| 637 | int ranges_len; |
| 638 | int tuple_len; |
| 639 | struct device_node *mc_node = dev->of_node; |
| 640 | |
| 641 | *ranges_start = of_get_property(mc_node, "ranges", &ranges_len); |
| 642 | if (!(*ranges_start) || !ranges_len) { |
| 643 | dev_warn(dev, |
| 644 | "missing or empty ranges property for device tree node '%s'\n", |
| 645 | mc_node->name); |
| 646 | |
| 647 | *num_ranges = 0; |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | *paddr_cells = of_n_addr_cells(mc_node); |
| 652 | |
| 653 | prop = of_get_property(mc_node, "#address-cells", NULL); |
| 654 | if (prop) |
| 655 | *mc_addr_cells = be32_to_cpup(prop); |
| 656 | else |
| 657 | *mc_addr_cells = *paddr_cells; |
| 658 | |
| 659 | prop = of_get_property(mc_node, "#size-cells", NULL); |
| 660 | if (prop) |
| 661 | *mc_size_cells = be32_to_cpup(prop); |
| 662 | else |
| 663 | *mc_size_cells = of_n_size_cells(mc_node); |
| 664 | |
| 665 | range_tuple_cell_count = *paddr_cells + *mc_addr_cells + |
| 666 | *mc_size_cells; |
| 667 | |
| 668 | tuple_len = range_tuple_cell_count * sizeof(__be32); |
| 669 | if (ranges_len % tuple_len != 0) { |
| 670 | dev_err(dev, "malformed ranges property '%s'\n", mc_node->name); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | |
| 674 | *num_ranges = ranges_len / tuple_len; |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int get_mc_addr_translation_ranges(struct device *dev, |
| 679 | struct fsl_mc_addr_translation_range |
| 680 | **ranges, |
J. German Rivera | ba72f25 | 2015-09-25 11:21:01 -0500 | [diff] [blame] | 681 | u8 *num_ranges) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 682 | { |
| 683 | int error; |
| 684 | int paddr_cells; |
| 685 | int mc_addr_cells; |
| 686 | int mc_size_cells; |
| 687 | int i; |
| 688 | const __be32 *ranges_start; |
| 689 | const __be32 *cell; |
| 690 | |
| 691 | error = parse_mc_ranges(dev, |
| 692 | &paddr_cells, |
| 693 | &mc_addr_cells, |
| 694 | &mc_size_cells, |
| 695 | &ranges_start, |
| 696 | num_ranges); |
| 697 | if (error < 0) |
| 698 | return error; |
| 699 | |
| 700 | if (!(*num_ranges)) { |
| 701 | /* |
| 702 | * Missing or empty ranges property ("ranges;") for the |
| 703 | * 'fsl,qoriq-mc' node. In this case, identity mapping |
| 704 | * will be used. |
| 705 | */ |
| 706 | *ranges = NULL; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | *ranges = devm_kcalloc(dev, *num_ranges, |
| 711 | sizeof(struct fsl_mc_addr_translation_range), |
| 712 | GFP_KERNEL); |
| 713 | if (!(*ranges)) |
| 714 | return -ENOMEM; |
| 715 | |
| 716 | cell = ranges_start; |
| 717 | for (i = 0; i < *num_ranges; ++i) { |
| 718 | struct fsl_mc_addr_translation_range *range = &(*ranges)[i]; |
| 719 | |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 720 | range->mc_region_type = of_read_number(cell, 1); |
| 721 | range->start_mc_offset = of_read_number(cell + 1, |
| 722 | mc_addr_cells - 1); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 723 | cell += mc_addr_cells; |
| 724 | range->start_phys_addr = of_read_number(cell, paddr_cells); |
| 725 | cell += paddr_cells; |
J. German Rivera | 1ee695f | 2015-09-23 16:11:03 -0500 | [diff] [blame] | 726 | range->end_mc_offset = range->start_mc_offset + |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 727 | of_read_number(cell, mc_size_cells); |
| 728 | |
| 729 | cell += mc_size_cells; |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * fsl_mc_bus_probe - callback invoked when the root MC bus is being |
| 737 | * added |
| 738 | */ |
| 739 | static int fsl_mc_bus_probe(struct platform_device *pdev) |
| 740 | { |
| 741 | struct dprc_obj_desc obj_desc; |
| 742 | int error; |
| 743 | struct fsl_mc *mc; |
| 744 | struct fsl_mc_device *mc_bus_dev = NULL; |
| 745 | struct fsl_mc_io *mc_io = NULL; |
| 746 | int container_id; |
| 747 | phys_addr_t mc_portal_phys_addr; |
Nayeemahmed Badebade | a2f9ff6 | 2015-09-04 22:30:33 +0530 | [diff] [blame] | 748 | u32 mc_portal_size; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 749 | struct mc_version mc_version; |
| 750 | struct resource res; |
| 751 | |
| 752 | dev_info(&pdev->dev, "Root MC bus device probed"); |
| 753 | |
| 754 | mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL); |
| 755 | if (!mc) |
| 756 | return -ENOMEM; |
| 757 | |
| 758 | platform_set_drvdata(pdev, mc); |
| 759 | |
| 760 | /* |
| 761 | * Get physical address of MC portal for the root DPRC: |
| 762 | */ |
| 763 | error = of_address_to_resource(pdev->dev.of_node, 0, &res); |
| 764 | if (error < 0) { |
| 765 | dev_err(&pdev->dev, |
| 766 | "of_address_to_resource() failed for %s\n", |
| 767 | pdev->dev.of_node->full_name); |
| 768 | return error; |
| 769 | } |
| 770 | |
| 771 | mc_portal_phys_addr = res.start; |
| 772 | mc_portal_size = resource_size(&res); |
| 773 | error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr, |
J. German Rivera | 1129cde | 2016-01-06 16:03:24 -0600 | [diff] [blame] | 774 | mc_portal_size, NULL, |
| 775 | FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 776 | if (error < 0) |
| 777 | return error; |
| 778 | |
J. German Rivera | 4057743 | 2015-09-23 16:10:59 -0500 | [diff] [blame] | 779 | error = mc_get_version(mc_io, 0, &mc_version); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 780 | if (error != 0) { |
| 781 | dev_err(&pdev->dev, |
| 782 | "mc_get_version() failed with error %d\n", error); |
| 783 | goto error_cleanup_mc_io; |
| 784 | } |
| 785 | |
| 786 | dev_info(&pdev->dev, |
| 787 | "Freescale Management Complex Firmware version: %u.%u.%u\n", |
| 788 | mc_version.major, mc_version.minor, mc_version.revision); |
| 789 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 790 | error = get_mc_addr_translation_ranges(&pdev->dev, |
| 791 | &mc->translation_ranges, |
| 792 | &mc->num_translation_ranges); |
| 793 | if (error < 0) |
| 794 | goto error_cleanup_mc_io; |
| 795 | |
J. German Rivera | 4057743 | 2015-09-23 16:10:59 -0500 | [diff] [blame] | 796 | error = dpmng_get_container_id(mc_io, 0, &container_id); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 797 | if (error < 0) { |
| 798 | dev_err(&pdev->dev, |
| 799 | "dpmng_get_container_id() failed: %d\n", error); |
| 800 | goto error_cleanup_mc_io; |
| 801 | } |
| 802 | |
Itai Katz | 1716cb4 | 2016-04-11 11:56:05 -0500 | [diff] [blame] | 803 | memset(&obj_desc, 0, sizeof(struct dprc_obj_desc)); |
Itai Katz | 9529d16 | 2016-04-11 11:55:55 -0500 | [diff] [blame] | 804 | error = get_dprc_version(mc_io, container_id, |
| 805 | &obj_desc.ver_major, &obj_desc.ver_minor); |
| 806 | if (error < 0) |
| 807 | goto error_cleanup_mc_io; |
| 808 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 809 | obj_desc.vendor = FSL_MC_VENDOR_FREESCALE; |
| 810 | strcpy(obj_desc.type, "dprc"); |
| 811 | obj_desc.id = container_id; |
J. German Rivera | a17f4aa | 2015-10-17 11:18:23 -0500 | [diff] [blame] | 812 | obj_desc.irq_count = 1; |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 813 | obj_desc.region_count = 0; |
| 814 | |
| 815 | error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev); |
| 816 | if (error < 0) |
| 817 | goto error_cleanup_mc_io; |
| 818 | |
| 819 | mc->root_mc_bus_dev = mc_bus_dev; |
| 820 | return 0; |
| 821 | |
| 822 | error_cleanup_mc_io: |
| 823 | fsl_destroy_mc_io(mc_io); |
| 824 | return error; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * fsl_mc_bus_remove - callback invoked when the root MC bus is being |
| 829 | * removed |
| 830 | */ |
| 831 | static int fsl_mc_bus_remove(struct platform_device *pdev) |
| 832 | { |
| 833 | struct fsl_mc *mc = platform_get_drvdata(pdev); |
| 834 | |
Itai Katz | b55f00c | 2015-10-04 10:09:51 +0300 | [diff] [blame] | 835 | if (WARN_ON(!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))) |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 836 | return -EINVAL; |
| 837 | |
| 838 | fsl_mc_device_remove(mc->root_mc_bus_dev); |
Bharat Bhushan | f936271 | 2016-06-22 16:40:48 -0500 | [diff] [blame] | 839 | |
| 840 | fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io); |
| 841 | mc->root_mc_bus_dev->mc_io = NULL; |
| 842 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 843 | dev_info(&pdev->dev, "Root MC bus device removed"); |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static const struct of_device_id fsl_mc_bus_match_table[] = { |
| 848 | {.compatible = "fsl,qoriq-mc",}, |
| 849 | {}, |
| 850 | }; |
| 851 | |
| 852 | MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table); |
| 853 | |
| 854 | static struct platform_driver fsl_mc_bus_driver = { |
| 855 | .driver = { |
| 856 | .name = "fsl_mc_bus", |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 857 | .pm = NULL, |
| 858 | .of_match_table = fsl_mc_bus_match_table, |
| 859 | }, |
| 860 | .probe = fsl_mc_bus_probe, |
| 861 | .remove = fsl_mc_bus_remove, |
| 862 | }; |
| 863 | |
| 864 | static int __init fsl_mc_bus_driver_init(void) |
| 865 | { |
| 866 | int error; |
| 867 | |
| 868 | mc_dev_cache = kmem_cache_create("fsl_mc_device", |
| 869 | sizeof(struct fsl_mc_device), 0, 0, |
| 870 | NULL); |
| 871 | if (!mc_dev_cache) { |
| 872 | pr_err("Could not create fsl_mc_device cache\n"); |
| 873 | return -ENOMEM; |
| 874 | } |
| 875 | |
| 876 | error = bus_register(&fsl_mc_bus_type); |
| 877 | if (error < 0) { |
| 878 | pr_err("fsl-mc bus type registration failed: %d\n", error); |
| 879 | goto error_cleanup_cache; |
| 880 | } |
| 881 | |
| 882 | pr_info("fsl-mc bus type registered\n"); |
| 883 | |
| 884 | error = platform_driver_register(&fsl_mc_bus_driver); |
| 885 | if (error < 0) { |
| 886 | pr_err("platform_driver_register() failed: %d\n", error); |
| 887 | goto error_cleanup_bus; |
| 888 | } |
| 889 | |
J. German Rivera | f2f2726 | 2015-03-05 19:29:11 -0600 | [diff] [blame] | 890 | error = dprc_driver_init(); |
| 891 | if (error < 0) |
| 892 | goto error_cleanup_driver; |
| 893 | |
J. German Rivera | e91ffa9 | 2015-03-27 16:01:08 -0500 | [diff] [blame] | 894 | error = fsl_mc_allocator_driver_init(); |
| 895 | if (error < 0) |
| 896 | goto error_cleanup_dprc_driver; |
| 897 | |
J. German Rivera | 660a24b | 2016-01-06 16:03:29 -0600 | [diff] [blame] | 898 | error = its_fsl_mc_msi_init(); |
| 899 | if (error < 0) |
| 900 | goto error_cleanup_mc_allocator; |
| 901 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 902 | return 0; |
| 903 | |
J. German Rivera | 660a24b | 2016-01-06 16:03:29 -0600 | [diff] [blame] | 904 | error_cleanup_mc_allocator: |
| 905 | fsl_mc_allocator_driver_exit(); |
| 906 | |
J. German Rivera | e91ffa9 | 2015-03-27 16:01:08 -0500 | [diff] [blame] | 907 | error_cleanup_dprc_driver: |
| 908 | dprc_driver_exit(); |
| 909 | |
J. German Rivera | f2f2726 | 2015-03-05 19:29:11 -0600 | [diff] [blame] | 910 | error_cleanup_driver: |
| 911 | platform_driver_unregister(&fsl_mc_bus_driver); |
| 912 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 913 | error_cleanup_bus: |
| 914 | bus_unregister(&fsl_mc_bus_type); |
| 915 | |
| 916 | error_cleanup_cache: |
| 917 | kmem_cache_destroy(mc_dev_cache); |
| 918 | return error; |
| 919 | } |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 920 | postcore_initcall(fsl_mc_bus_driver_init); |