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 | |
| 12 | #include "../include/mc-private.h" |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/ioport.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/limits.h> |
| 19 | #include "../include/dpmng.h" |
| 20 | #include "../include/mc-sys.h" |
| 21 | #include "dprc-cmd.h" |
| 22 | |
| 23 | static struct kmem_cache *mc_dev_cache; |
| 24 | |
| 25 | /** |
| 26 | * fsl_mc_bus_match - device to driver matching callback |
| 27 | * @dev: the MC object device structure to match against |
| 28 | * @drv: the device driver to search for matching MC object device id |
| 29 | * structures |
| 30 | * |
| 31 | * Returns 1 on success, 0 otherwise. |
| 32 | */ |
| 33 | static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv) |
| 34 | { |
| 35 | const struct fsl_mc_device_match_id *id; |
| 36 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 37 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv); |
| 38 | bool found = false; |
| 39 | |
| 40 | if (WARN_ON(!fsl_mc_bus_type.dev_root)) |
| 41 | goto out; |
| 42 | |
| 43 | if (!mc_drv->match_id_table) |
| 44 | goto out; |
| 45 | |
| 46 | /* |
| 47 | * If the object is not 'plugged' don't match. |
| 48 | * Only exception is the root DPRC, which is a special case. |
| 49 | */ |
| 50 | if ((mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED) == 0 && |
| 51 | &mc_dev->dev != fsl_mc_bus_type.dev_root) |
| 52 | goto out; |
| 53 | |
| 54 | /* |
| 55 | * Traverse the match_id table of the given driver, trying to find |
| 56 | * a matching for the given MC object device. |
| 57 | */ |
| 58 | for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) { |
| 59 | if (id->vendor == mc_dev->obj_desc.vendor && |
| 60 | strcmp(id->obj_type, mc_dev->obj_desc.type) == 0 && |
| 61 | id->ver_major == mc_dev->obj_desc.ver_major && |
| 62 | id->ver_minor == mc_dev->obj_desc.ver_minor) { |
| 63 | found = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | out: |
| 69 | dev_dbg(dev, "%smatched\n", found ? "" : "not "); |
| 70 | return found; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * fsl_mc_bus_uevent - callback invoked when a device is added |
| 75 | */ |
| 76 | static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 77 | { |
| 78 | pr_debug("%s invoked\n", __func__); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | struct bus_type fsl_mc_bus_type = { |
| 83 | .name = "fsl-mc", |
| 84 | .match = fsl_mc_bus_match, |
| 85 | .uevent = fsl_mc_bus_uevent, |
| 86 | }; |
| 87 | EXPORT_SYMBOL_GPL(fsl_mc_bus_type); |
| 88 | |
| 89 | static int fsl_mc_driver_probe(struct device *dev) |
| 90 | { |
| 91 | struct fsl_mc_driver *mc_drv; |
| 92 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 93 | int error; |
| 94 | |
| 95 | if (WARN_ON(!dev->driver)) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | mc_drv = to_fsl_mc_driver(dev->driver); |
| 99 | if (WARN_ON(!mc_drv->probe)) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | error = mc_drv->probe(mc_dev); |
| 103 | if (error < 0) { |
| 104 | dev_err(dev, "MC object device probe callback failed: %d\n", |
| 105 | error); |
| 106 | return error; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int fsl_mc_driver_remove(struct device *dev) |
| 113 | { |
| 114 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); |
| 115 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 116 | int error; |
| 117 | |
| 118 | if (WARN_ON(!dev->driver)) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | error = mc_drv->remove(mc_dev); |
| 122 | if (error < 0) { |
| 123 | dev_err(dev, |
| 124 | "MC object device remove callback failed: %d\n", |
| 125 | error); |
| 126 | return error; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static void fsl_mc_driver_shutdown(struct device *dev) |
| 133 | { |
| 134 | struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); |
| 135 | struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); |
| 136 | |
| 137 | mc_drv->shutdown(mc_dev); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * __fsl_mc_driver_register - registers a child device driver with the |
| 142 | * MC bus |
| 143 | * |
| 144 | * This function is implicitly invoked from the registration function of |
| 145 | * fsl_mc device drivers, which is generated by the |
| 146 | * module_fsl_mc_driver() macro. |
| 147 | */ |
| 148 | int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver, |
| 149 | struct module *owner) |
| 150 | { |
| 151 | int error; |
| 152 | |
| 153 | mc_driver->driver.owner = owner; |
| 154 | mc_driver->driver.bus = &fsl_mc_bus_type; |
| 155 | |
| 156 | if (mc_driver->probe) |
| 157 | mc_driver->driver.probe = fsl_mc_driver_probe; |
| 158 | |
| 159 | if (mc_driver->remove) |
| 160 | mc_driver->driver.remove = fsl_mc_driver_remove; |
| 161 | |
| 162 | if (mc_driver->shutdown) |
| 163 | mc_driver->driver.shutdown = fsl_mc_driver_shutdown; |
| 164 | |
| 165 | error = driver_register(&mc_driver->driver); |
| 166 | if (error < 0) { |
| 167 | pr_err("driver_register() failed for %s: %d\n", |
| 168 | mc_driver->driver.name, error); |
| 169 | return error; |
| 170 | } |
| 171 | |
| 172 | pr_info("MC object device driver %s registered\n", |
| 173 | mc_driver->driver.name); |
| 174 | return 0; |
| 175 | } |
| 176 | EXPORT_SYMBOL_GPL(__fsl_mc_driver_register); |
| 177 | |
| 178 | /** |
| 179 | * fsl_mc_driver_unregister - unregisters a device driver from the |
| 180 | * MC bus |
| 181 | */ |
| 182 | void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver) |
| 183 | { |
| 184 | driver_unregister(&mc_driver->driver); |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister); |
| 187 | |
| 188 | static int get_dprc_icid(struct fsl_mc_io *mc_io, |
| 189 | int container_id, uint16_t *icid) |
| 190 | { |
| 191 | uint16_t dprc_handle; |
| 192 | struct dprc_attributes attr; |
| 193 | int error; |
| 194 | |
| 195 | error = dprc_open(mc_io, container_id, &dprc_handle); |
| 196 | if (error < 0) { |
| 197 | pr_err("dprc_open() failed: %d\n", error); |
| 198 | return error; |
| 199 | } |
| 200 | |
| 201 | memset(&attr, 0, sizeof(attr)); |
| 202 | error = dprc_get_attributes(mc_io, dprc_handle, &attr); |
| 203 | if (error < 0) { |
| 204 | pr_err("dprc_get_attributes() failed: %d\n", error); |
| 205 | goto common_cleanup; |
| 206 | } |
| 207 | |
| 208 | *icid = attr.icid; |
| 209 | error = 0; |
| 210 | |
| 211 | common_cleanup: |
| 212 | (void)dprc_close(mc_io, dprc_handle); |
| 213 | return error; |
| 214 | } |
| 215 | |
| 216 | static int translate_mc_addr(uint64_t mc_addr, phys_addr_t *phys_addr) |
| 217 | { |
| 218 | int i; |
| 219 | struct fsl_mc *mc = dev_get_drvdata(fsl_mc_bus_type.dev_root->parent); |
| 220 | |
| 221 | if (mc->num_translation_ranges == 0) { |
| 222 | /* |
| 223 | * Do identity mapping: |
| 224 | */ |
| 225 | *phys_addr = mc_addr; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | for (i = 0; i < mc->num_translation_ranges; i++) { |
| 230 | struct fsl_mc_addr_translation_range *range = |
| 231 | &mc->translation_ranges[i]; |
| 232 | |
| 233 | if (mc_addr >= range->start_mc_addr && |
| 234 | mc_addr < range->end_mc_addr) { |
| 235 | *phys_addr = range->start_phys_addr + |
| 236 | (mc_addr - range->start_mc_addr); |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return -EFAULT; |
| 242 | } |
| 243 | |
| 244 | static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev, |
| 245 | struct fsl_mc_device *mc_bus_dev) |
| 246 | { |
| 247 | int i; |
| 248 | int error; |
| 249 | struct resource *regions; |
| 250 | struct dprc_obj_desc *obj_desc = &mc_dev->obj_desc; |
| 251 | struct device *parent_dev = mc_dev->dev.parent; |
| 252 | |
| 253 | regions = kmalloc_array(obj_desc->region_count, |
| 254 | sizeof(regions[0]), GFP_KERNEL); |
| 255 | if (!regions) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | for (i = 0; i < obj_desc->region_count; i++) { |
| 259 | struct dprc_region_desc region_desc; |
| 260 | |
| 261 | error = dprc_get_obj_region(mc_bus_dev->mc_io, |
| 262 | mc_bus_dev->mc_handle, |
| 263 | obj_desc->type, |
| 264 | obj_desc->id, i, ®ion_desc); |
| 265 | if (error < 0) { |
| 266 | dev_err(parent_dev, |
| 267 | "dprc_get_obj_region() failed: %d\n", error); |
| 268 | goto error_cleanup_regions; |
| 269 | } |
| 270 | |
| 271 | WARN_ON(region_desc.base_paddr == 0x0); |
| 272 | WARN_ON(region_desc.size == 0); |
| 273 | error = translate_mc_addr(region_desc.base_paddr, |
| 274 | ®ions[i].start); |
| 275 | if (error < 0) { |
| 276 | dev_err(parent_dev, |
| 277 | "Invalid MC address: %#llx\n", |
| 278 | region_desc.base_paddr); |
| 279 | goto error_cleanup_regions; |
| 280 | } |
| 281 | |
| 282 | regions[i].end = regions[i].start + region_desc.size - 1; |
| 283 | regions[i].name = "fsl-mc object MMIO region"; |
| 284 | regions[i].flags = IORESOURCE_IO; |
| 285 | } |
| 286 | |
| 287 | mc_dev->regions = regions; |
| 288 | return 0; |
| 289 | |
| 290 | error_cleanup_regions: |
| 291 | kfree(regions); |
| 292 | return error; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Add a newly discovered MC object device to be visible in Linux |
| 297 | */ |
| 298 | int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, |
| 299 | struct fsl_mc_io *mc_io, |
| 300 | struct device *parent_dev, |
| 301 | struct fsl_mc_device **new_mc_dev) |
| 302 | { |
| 303 | int error; |
| 304 | struct fsl_mc_device *mc_dev = NULL; |
| 305 | struct fsl_mc_bus *mc_bus = NULL; |
| 306 | struct fsl_mc_device *parent_mc_dev; |
| 307 | |
| 308 | if (parent_dev->bus == &fsl_mc_bus_type) |
| 309 | parent_mc_dev = to_fsl_mc_device(parent_dev); |
| 310 | else |
| 311 | parent_mc_dev = NULL; |
| 312 | |
| 313 | if (strcmp(obj_desc->type, "dprc") == 0) { |
| 314 | /* |
| 315 | * Allocate an MC bus device object: |
| 316 | */ |
| 317 | mc_bus = devm_kzalloc(parent_dev, sizeof(*mc_bus), GFP_KERNEL); |
| 318 | if (!mc_bus) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | mc_dev = &mc_bus->mc_dev; |
| 322 | } else { |
| 323 | /* |
| 324 | * Allocate a regular fsl_mc_device object: |
| 325 | */ |
| 326 | mc_dev = kmem_cache_zalloc(mc_dev_cache, GFP_KERNEL); |
| 327 | if (!mc_dev) |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
| 331 | mc_dev->obj_desc = *obj_desc; |
| 332 | mc_dev->mc_io = mc_io; |
| 333 | device_initialize(&mc_dev->dev); |
| 334 | mc_dev->dev.parent = parent_dev; |
| 335 | mc_dev->dev.bus = &fsl_mc_bus_type; |
J. German Rivera | 77371fb | 2015-03-27 16:01:04 -0500 | [diff] [blame] | 336 | 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] | 337 | |
| 338 | if (strcmp(obj_desc->type, "dprc") == 0) { |
| 339 | struct fsl_mc_io *mc_io2; |
| 340 | |
| 341 | mc_dev->flags |= FSL_MC_IS_DPRC; |
| 342 | |
| 343 | /* |
| 344 | * To get the DPRC's ICID, we need to open the DPRC |
| 345 | * in get_dprc_icid(). For child DPRCs, we do so using the |
| 346 | * parent DPRC's MC portal instead of the child DPRC's MC |
| 347 | * portal, in case the child DPRC is already opened with |
| 348 | * its own portal (e.g., the DPRC used by AIOP). |
| 349 | * |
| 350 | * NOTE: There cannot be more than one active open for a |
| 351 | * given MC object, using the same MC portal. |
| 352 | */ |
| 353 | if (parent_mc_dev) { |
| 354 | /* |
| 355 | * device being added is a child DPRC device |
| 356 | */ |
| 357 | mc_io2 = parent_mc_dev->mc_io; |
| 358 | } else { |
| 359 | /* |
| 360 | * device being added is the root DPRC device |
| 361 | */ |
| 362 | if (WARN_ON(!mc_io)) { |
| 363 | error = -EINVAL; |
| 364 | goto error_cleanup_dev; |
| 365 | } |
| 366 | |
| 367 | mc_io2 = mc_io; |
| 368 | |
| 369 | if (!fsl_mc_bus_type.dev_root) |
| 370 | fsl_mc_bus_type.dev_root = &mc_dev->dev; |
| 371 | } |
| 372 | |
| 373 | error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid); |
| 374 | if (error < 0) |
| 375 | goto error_cleanup_dev; |
| 376 | } else { |
| 377 | /* |
| 378 | * A non-DPRC MC object device has to be a child of another |
| 379 | * MC object (specifically a DPRC object) |
| 380 | */ |
| 381 | mc_dev->icid = parent_mc_dev->icid; |
| 382 | mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK; |
| 383 | mc_dev->dev.dma_mask = &mc_dev->dma_mask; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Get MMIO regions for the device from the MC: |
| 388 | * |
| 389 | * NOTE: the root DPRC is a special case as its MMIO region is |
| 390 | * obtained from the device tree |
| 391 | */ |
| 392 | if (parent_mc_dev && obj_desc->region_count != 0) { |
| 393 | error = fsl_mc_device_get_mmio_regions(mc_dev, |
| 394 | parent_mc_dev); |
| 395 | if (error < 0) |
| 396 | goto error_cleanup_dev; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * The device-specific probe callback will get invoked by device_add() |
| 401 | */ |
| 402 | error = device_add(&mc_dev->dev); |
| 403 | if (error < 0) { |
| 404 | dev_err(parent_dev, |
| 405 | "device_add() failed for device %s: %d\n", |
| 406 | dev_name(&mc_dev->dev), error); |
| 407 | goto error_cleanup_dev; |
| 408 | } |
| 409 | |
| 410 | (void)get_device(&mc_dev->dev); |
| 411 | dev_dbg(parent_dev, "Added MC object device %s\n", |
| 412 | dev_name(&mc_dev->dev)); |
| 413 | |
| 414 | *new_mc_dev = mc_dev; |
| 415 | return 0; |
| 416 | |
| 417 | error_cleanup_dev: |
| 418 | kfree(mc_dev->regions); |
| 419 | if (mc_bus) |
| 420 | devm_kfree(parent_dev, mc_bus); |
| 421 | else |
| 422 | kmem_cache_free(mc_dev_cache, mc_dev); |
| 423 | |
| 424 | return error; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(fsl_mc_device_add); |
| 427 | |
| 428 | /** |
| 429 | * fsl_mc_device_remove - Remove a MC object device from being visible to |
| 430 | * Linux |
| 431 | * |
| 432 | * @mc_dev: Pointer to a MC object device object |
| 433 | */ |
| 434 | void fsl_mc_device_remove(struct fsl_mc_device *mc_dev) |
| 435 | { |
| 436 | struct fsl_mc_bus *mc_bus = NULL; |
| 437 | |
| 438 | kfree(mc_dev->regions); |
| 439 | |
| 440 | /* |
| 441 | * The device-specific remove callback will get invoked by device_del() |
| 442 | */ |
| 443 | device_del(&mc_dev->dev); |
| 444 | put_device(&mc_dev->dev); |
| 445 | |
| 446 | if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) { |
| 447 | struct fsl_mc_io *mc_io = mc_dev->mc_io; |
| 448 | |
| 449 | mc_bus = to_fsl_mc_bus(mc_dev); |
| 450 | fsl_destroy_mc_io(mc_io); |
| 451 | if (&mc_dev->dev == fsl_mc_bus_type.dev_root) |
| 452 | fsl_mc_bus_type.dev_root = NULL; |
| 453 | } |
| 454 | |
| 455 | mc_dev->mc_io = NULL; |
| 456 | if (mc_bus) |
| 457 | devm_kfree(mc_dev->dev.parent, mc_bus); |
| 458 | else |
| 459 | kmem_cache_free(mc_dev_cache, mc_dev); |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(fsl_mc_device_remove); |
| 462 | |
| 463 | static int parse_mc_ranges(struct device *dev, |
| 464 | int *paddr_cells, |
| 465 | int *mc_addr_cells, |
| 466 | int *mc_size_cells, |
| 467 | const __be32 **ranges_start, |
| 468 | uint8_t *num_ranges) |
| 469 | { |
| 470 | const __be32 *prop; |
| 471 | int range_tuple_cell_count; |
| 472 | int ranges_len; |
| 473 | int tuple_len; |
| 474 | struct device_node *mc_node = dev->of_node; |
| 475 | |
| 476 | *ranges_start = of_get_property(mc_node, "ranges", &ranges_len); |
| 477 | if (!(*ranges_start) || !ranges_len) { |
| 478 | dev_warn(dev, |
| 479 | "missing or empty ranges property for device tree node '%s'\n", |
| 480 | mc_node->name); |
| 481 | |
| 482 | *num_ranges = 0; |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | *paddr_cells = of_n_addr_cells(mc_node); |
| 487 | |
| 488 | prop = of_get_property(mc_node, "#address-cells", NULL); |
| 489 | if (prop) |
| 490 | *mc_addr_cells = be32_to_cpup(prop); |
| 491 | else |
| 492 | *mc_addr_cells = *paddr_cells; |
| 493 | |
| 494 | prop = of_get_property(mc_node, "#size-cells", NULL); |
| 495 | if (prop) |
| 496 | *mc_size_cells = be32_to_cpup(prop); |
| 497 | else |
| 498 | *mc_size_cells = of_n_size_cells(mc_node); |
| 499 | |
| 500 | range_tuple_cell_count = *paddr_cells + *mc_addr_cells + |
| 501 | *mc_size_cells; |
| 502 | |
| 503 | tuple_len = range_tuple_cell_count * sizeof(__be32); |
| 504 | if (ranges_len % tuple_len != 0) { |
| 505 | dev_err(dev, "malformed ranges property '%s'\n", mc_node->name); |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | *num_ranges = ranges_len / tuple_len; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int get_mc_addr_translation_ranges(struct device *dev, |
| 514 | struct fsl_mc_addr_translation_range |
| 515 | **ranges, |
| 516 | uint8_t *num_ranges) |
| 517 | { |
| 518 | int error; |
| 519 | int paddr_cells; |
| 520 | int mc_addr_cells; |
| 521 | int mc_size_cells; |
| 522 | int i; |
| 523 | const __be32 *ranges_start; |
| 524 | const __be32 *cell; |
| 525 | |
| 526 | error = parse_mc_ranges(dev, |
| 527 | &paddr_cells, |
| 528 | &mc_addr_cells, |
| 529 | &mc_size_cells, |
| 530 | &ranges_start, |
| 531 | num_ranges); |
| 532 | if (error < 0) |
| 533 | return error; |
| 534 | |
| 535 | if (!(*num_ranges)) { |
| 536 | /* |
| 537 | * Missing or empty ranges property ("ranges;") for the |
| 538 | * 'fsl,qoriq-mc' node. In this case, identity mapping |
| 539 | * will be used. |
| 540 | */ |
| 541 | *ranges = NULL; |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | *ranges = devm_kcalloc(dev, *num_ranges, |
| 546 | sizeof(struct fsl_mc_addr_translation_range), |
| 547 | GFP_KERNEL); |
| 548 | if (!(*ranges)) |
| 549 | return -ENOMEM; |
| 550 | |
| 551 | cell = ranges_start; |
| 552 | for (i = 0; i < *num_ranges; ++i) { |
| 553 | struct fsl_mc_addr_translation_range *range = &(*ranges)[i]; |
| 554 | |
| 555 | range->start_mc_addr = of_read_number(cell, mc_addr_cells); |
| 556 | cell += mc_addr_cells; |
| 557 | range->start_phys_addr = of_read_number(cell, paddr_cells); |
| 558 | cell += paddr_cells; |
| 559 | range->end_mc_addr = range->start_mc_addr + |
| 560 | of_read_number(cell, mc_size_cells); |
| 561 | |
| 562 | cell += mc_size_cells; |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * fsl_mc_bus_probe - callback invoked when the root MC bus is being |
| 570 | * added |
| 571 | */ |
| 572 | static int fsl_mc_bus_probe(struct platform_device *pdev) |
| 573 | { |
| 574 | struct dprc_obj_desc obj_desc; |
| 575 | int error; |
| 576 | struct fsl_mc *mc; |
| 577 | struct fsl_mc_device *mc_bus_dev = NULL; |
| 578 | struct fsl_mc_io *mc_io = NULL; |
| 579 | int container_id; |
| 580 | phys_addr_t mc_portal_phys_addr; |
| 581 | uint32_t mc_portal_size; |
| 582 | struct mc_version mc_version; |
| 583 | struct resource res; |
| 584 | |
| 585 | dev_info(&pdev->dev, "Root MC bus device probed"); |
| 586 | |
| 587 | mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL); |
| 588 | if (!mc) |
| 589 | return -ENOMEM; |
| 590 | |
| 591 | platform_set_drvdata(pdev, mc); |
| 592 | |
| 593 | /* |
| 594 | * Get physical address of MC portal for the root DPRC: |
| 595 | */ |
| 596 | error = of_address_to_resource(pdev->dev.of_node, 0, &res); |
| 597 | if (error < 0) { |
| 598 | dev_err(&pdev->dev, |
| 599 | "of_address_to_resource() failed for %s\n", |
| 600 | pdev->dev.of_node->full_name); |
| 601 | return error; |
| 602 | } |
| 603 | |
| 604 | mc_portal_phys_addr = res.start; |
| 605 | mc_portal_size = resource_size(&res); |
| 606 | error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr, |
J. German Rivera | 197f4d6 | 2015-03-05 19:35:25 -0600 | [diff] [blame] | 607 | mc_portal_size, NULL, 0, &mc_io); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 608 | if (error < 0) |
| 609 | return error; |
| 610 | |
| 611 | error = mc_get_version(mc_io, &mc_version); |
| 612 | if (error != 0) { |
| 613 | dev_err(&pdev->dev, |
| 614 | "mc_get_version() failed with error %d\n", error); |
| 615 | goto error_cleanup_mc_io; |
| 616 | } |
| 617 | |
| 618 | dev_info(&pdev->dev, |
| 619 | "Freescale Management Complex Firmware version: %u.%u.%u\n", |
| 620 | mc_version.major, mc_version.minor, mc_version.revision); |
| 621 | |
| 622 | if (mc_version.major < MC_VER_MAJOR) { |
| 623 | dev_err(&pdev->dev, |
| 624 | "ERROR: MC firmware version not supported by driver (driver version: %u.%u)\n", |
| 625 | MC_VER_MAJOR, MC_VER_MINOR); |
| 626 | error = -ENOTSUPP; |
| 627 | goto error_cleanup_mc_io; |
| 628 | } |
| 629 | |
| 630 | if (mc_version.major > MC_VER_MAJOR) { |
| 631 | dev_warn(&pdev->dev, |
| 632 | "WARNING: driver may not support newer MC firmware features (driver version: %u.%u)\n", |
| 633 | MC_VER_MAJOR, MC_VER_MINOR); |
| 634 | } |
| 635 | |
| 636 | error = get_mc_addr_translation_ranges(&pdev->dev, |
| 637 | &mc->translation_ranges, |
| 638 | &mc->num_translation_ranges); |
| 639 | if (error < 0) |
| 640 | goto error_cleanup_mc_io; |
| 641 | |
| 642 | error = dpmng_get_container_id(mc_io, &container_id); |
| 643 | if (error < 0) { |
| 644 | dev_err(&pdev->dev, |
| 645 | "dpmng_get_container_id() failed: %d\n", error); |
| 646 | goto error_cleanup_mc_io; |
| 647 | } |
| 648 | |
| 649 | obj_desc.vendor = FSL_MC_VENDOR_FREESCALE; |
| 650 | strcpy(obj_desc.type, "dprc"); |
| 651 | obj_desc.id = container_id; |
| 652 | obj_desc.ver_major = DPRC_VER_MAJOR; |
| 653 | obj_desc.ver_minor = DPRC_VER_MINOR; |
| 654 | obj_desc.region_count = 0; |
| 655 | |
| 656 | error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev); |
| 657 | if (error < 0) |
| 658 | goto error_cleanup_mc_io; |
| 659 | |
| 660 | mc->root_mc_bus_dev = mc_bus_dev; |
| 661 | return 0; |
| 662 | |
| 663 | error_cleanup_mc_io: |
| 664 | fsl_destroy_mc_io(mc_io); |
| 665 | return error; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * fsl_mc_bus_remove - callback invoked when the root MC bus is being |
| 670 | * removed |
| 671 | */ |
| 672 | static int fsl_mc_bus_remove(struct platform_device *pdev) |
| 673 | { |
| 674 | struct fsl_mc *mc = platform_get_drvdata(pdev); |
| 675 | |
| 676 | if (WARN_ON(&mc->root_mc_bus_dev->dev != fsl_mc_bus_type.dev_root)) |
| 677 | return -EINVAL; |
| 678 | |
| 679 | fsl_mc_device_remove(mc->root_mc_bus_dev); |
| 680 | dev_info(&pdev->dev, "Root MC bus device removed"); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static const struct of_device_id fsl_mc_bus_match_table[] = { |
| 685 | {.compatible = "fsl,qoriq-mc",}, |
| 686 | {}, |
| 687 | }; |
| 688 | |
| 689 | MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table); |
| 690 | |
| 691 | static struct platform_driver fsl_mc_bus_driver = { |
| 692 | .driver = { |
| 693 | .name = "fsl_mc_bus", |
| 694 | .owner = THIS_MODULE, |
| 695 | .pm = NULL, |
| 696 | .of_match_table = fsl_mc_bus_match_table, |
| 697 | }, |
| 698 | .probe = fsl_mc_bus_probe, |
| 699 | .remove = fsl_mc_bus_remove, |
| 700 | }; |
| 701 | |
| 702 | static int __init fsl_mc_bus_driver_init(void) |
| 703 | { |
| 704 | int error; |
| 705 | |
| 706 | mc_dev_cache = kmem_cache_create("fsl_mc_device", |
| 707 | sizeof(struct fsl_mc_device), 0, 0, |
| 708 | NULL); |
| 709 | if (!mc_dev_cache) { |
| 710 | pr_err("Could not create fsl_mc_device cache\n"); |
| 711 | return -ENOMEM; |
| 712 | } |
| 713 | |
| 714 | error = bus_register(&fsl_mc_bus_type); |
| 715 | if (error < 0) { |
| 716 | pr_err("fsl-mc bus type registration failed: %d\n", error); |
| 717 | goto error_cleanup_cache; |
| 718 | } |
| 719 | |
| 720 | pr_info("fsl-mc bus type registered\n"); |
| 721 | |
| 722 | error = platform_driver_register(&fsl_mc_bus_driver); |
| 723 | if (error < 0) { |
| 724 | pr_err("platform_driver_register() failed: %d\n", error); |
| 725 | goto error_cleanup_bus; |
| 726 | } |
| 727 | |
J. German Rivera | f2f2726 | 2015-03-05 19:29:11 -0600 | [diff] [blame] | 728 | error = dprc_driver_init(); |
| 729 | if (error < 0) |
| 730 | goto error_cleanup_driver; |
| 731 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 732 | return 0; |
| 733 | |
J. German Rivera | f2f2726 | 2015-03-05 19:29:11 -0600 | [diff] [blame] | 734 | error_cleanup_driver: |
| 735 | platform_driver_unregister(&fsl_mc_bus_driver); |
| 736 | |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 737 | error_cleanup_bus: |
| 738 | bus_unregister(&fsl_mc_bus_type); |
| 739 | |
| 740 | error_cleanup_cache: |
| 741 | kmem_cache_destroy(mc_dev_cache); |
| 742 | return error; |
| 743 | } |
| 744 | |
| 745 | postcore_initcall(fsl_mc_bus_driver_init); |
| 746 | |
| 747 | static void __exit fsl_mc_bus_driver_exit(void) |
| 748 | { |
| 749 | if (WARN_ON(!mc_dev_cache)) |
| 750 | return; |
| 751 | |
J. German Rivera | f2f2726 | 2015-03-05 19:29:11 -0600 | [diff] [blame] | 752 | dprc_driver_exit(); |
J. German Rivera | bbf9d17 | 2015-03-05 19:29:10 -0600 | [diff] [blame] | 753 | platform_driver_unregister(&fsl_mc_bus_driver); |
| 754 | bus_unregister(&fsl_mc_bus_type); |
| 755 | kmem_cache_destroy(mc_dev_cache); |
| 756 | pr_info("MC bus unregistered\n"); |
| 757 | } |
| 758 | |
| 759 | module_exit(fsl_mc_bus_driver_exit); |
| 760 | |
| 761 | MODULE_AUTHOR("Freescale Semiconductor Inc."); |
| 762 | MODULE_DESCRIPTION("Freescale Management Complex (MC) bus driver"); |
| 763 | MODULE_LICENSE("GPL"); |