gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / bus / fsl-mc / fsl-mc-bus.c
blob40526da5c6a6efb0c763aaa06ada4f3dd2988844
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Freescale Management Complex (MC) bus driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6 * Author: German Rivera <German.Rivera@freescale.com>
8 */
10 #define pr_fmt(fmt) "fsl-mc: " fmt
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>
18 #include <linux/bitops.h>
19 #include <linux/msi.h>
20 #include <linux/dma-mapping.h>
22 #include "fsl-mc-private.h"
24 /**
25 * Default DMA mask for devices on a fsl-mc bus
27 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
29 static struct fsl_mc_version mc_version;
31 /**
32 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
33 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
34 * @num_translation_ranges: number of entries in addr_translation_ranges
35 * @translation_ranges: array of bus to system address translation ranges
37 struct fsl_mc {
38 struct fsl_mc_device *root_mc_bus_dev;
39 u8 num_translation_ranges;
40 struct fsl_mc_addr_translation_range *translation_ranges;
43 /**
44 * struct fsl_mc_addr_translation_range - bus to system address translation
45 * range
46 * @mc_region_type: Type of MC region for the range being translated
47 * @start_mc_offset: Start MC offset of the range being translated
48 * @end_mc_offset: MC offset of the first byte after the range (last MC
49 * offset of the range is end_mc_offset - 1)
50 * @start_phys_addr: system physical address corresponding to start_mc_addr
52 struct fsl_mc_addr_translation_range {
53 enum dprc_region_type mc_region_type;
54 u64 start_mc_offset;
55 u64 end_mc_offset;
56 phys_addr_t start_phys_addr;
59 /**
60 * fsl_mc_bus_match - device to driver matching callback
61 * @dev: the fsl-mc device to match against
62 * @drv: the device driver to search for matching fsl-mc object type
63 * structures
65 * Returns 1 on success, 0 otherwise.
67 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
69 const struct fsl_mc_device_id *id;
70 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
71 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
72 bool found = false;
74 if (!mc_drv->match_id_table)
75 goto out;
78 * If the object is not 'plugged' don't match.
79 * Only exception is the root DPRC, which is a special case.
81 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
82 !fsl_mc_is_root_dprc(&mc_dev->dev))
83 goto out;
86 * Traverse the match_id table of the given driver, trying to find
87 * a matching for the given device.
89 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
90 if (id->vendor == mc_dev->obj_desc.vendor &&
91 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
92 found = true;
94 break;
98 out:
99 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
100 return found;
104 * fsl_mc_bus_uevent - callback invoked when a device is added
106 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
108 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
110 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
111 mc_dev->obj_desc.vendor,
112 mc_dev->obj_desc.type))
113 return -ENOMEM;
115 return 0;
118 static int fsl_mc_dma_configure(struct device *dev)
120 struct device *dma_dev = dev;
122 while (dev_is_fsl_mc(dma_dev))
123 dma_dev = dma_dev->parent;
125 return of_dma_configure(dev, dma_dev->of_node, 0);
128 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
129 char *buf)
131 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
133 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
134 mc_dev->obj_desc.type);
136 static DEVICE_ATTR_RO(modalias);
138 static struct attribute *fsl_mc_dev_attrs[] = {
139 &dev_attr_modalias.attr,
140 NULL,
143 ATTRIBUTE_GROUPS(fsl_mc_dev);
145 struct bus_type fsl_mc_bus_type = {
146 .name = "fsl-mc",
147 .match = fsl_mc_bus_match,
148 .uevent = fsl_mc_bus_uevent,
149 .dma_configure = fsl_mc_dma_configure,
150 .dev_groups = fsl_mc_dev_groups,
152 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
154 struct device_type fsl_mc_bus_dprc_type = {
155 .name = "fsl_mc_bus_dprc"
157 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
159 struct device_type fsl_mc_bus_dpni_type = {
160 .name = "fsl_mc_bus_dpni"
162 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
164 struct device_type fsl_mc_bus_dpio_type = {
165 .name = "fsl_mc_bus_dpio"
167 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
169 struct device_type fsl_mc_bus_dpsw_type = {
170 .name = "fsl_mc_bus_dpsw"
172 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
174 struct device_type fsl_mc_bus_dpbp_type = {
175 .name = "fsl_mc_bus_dpbp"
177 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
179 struct device_type fsl_mc_bus_dpcon_type = {
180 .name = "fsl_mc_bus_dpcon"
182 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
184 struct device_type fsl_mc_bus_dpmcp_type = {
185 .name = "fsl_mc_bus_dpmcp"
187 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
189 struct device_type fsl_mc_bus_dpmac_type = {
190 .name = "fsl_mc_bus_dpmac"
192 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
194 struct device_type fsl_mc_bus_dprtc_type = {
195 .name = "fsl_mc_bus_dprtc"
197 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
199 struct device_type fsl_mc_bus_dpseci_type = {
200 .name = "fsl_mc_bus_dpseci"
202 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
204 static struct device_type *fsl_mc_get_device_type(const char *type)
206 static const struct {
207 struct device_type *dev_type;
208 const char *type;
209 } dev_types[] = {
210 { &fsl_mc_bus_dprc_type, "dprc" },
211 { &fsl_mc_bus_dpni_type, "dpni" },
212 { &fsl_mc_bus_dpio_type, "dpio" },
213 { &fsl_mc_bus_dpsw_type, "dpsw" },
214 { &fsl_mc_bus_dpbp_type, "dpbp" },
215 { &fsl_mc_bus_dpcon_type, "dpcon" },
216 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
217 { &fsl_mc_bus_dpmac_type, "dpmac" },
218 { &fsl_mc_bus_dprtc_type, "dprtc" },
219 { &fsl_mc_bus_dpseci_type, "dpseci" },
220 { NULL, NULL }
222 int i;
224 for (i = 0; dev_types[i].dev_type; i++)
225 if (!strcmp(dev_types[i].type, type))
226 return dev_types[i].dev_type;
228 return NULL;
231 static int fsl_mc_driver_probe(struct device *dev)
233 struct fsl_mc_driver *mc_drv;
234 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
235 int error;
237 mc_drv = to_fsl_mc_driver(dev->driver);
239 error = mc_drv->probe(mc_dev);
240 if (error < 0) {
241 if (error != -EPROBE_DEFER)
242 dev_err(dev, "%s failed: %d\n", __func__, error);
243 return error;
246 return 0;
249 static int fsl_mc_driver_remove(struct device *dev)
251 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
252 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
253 int error;
255 error = mc_drv->remove(mc_dev);
256 if (error < 0) {
257 dev_err(dev, "%s failed: %d\n", __func__, error);
258 return error;
261 return 0;
264 static void fsl_mc_driver_shutdown(struct device *dev)
266 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
267 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
269 mc_drv->shutdown(mc_dev);
273 * __fsl_mc_driver_register - registers a child device driver with the
274 * MC bus
276 * This function is implicitly invoked from the registration function of
277 * fsl_mc device drivers, which is generated by the
278 * module_fsl_mc_driver() macro.
280 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
281 struct module *owner)
283 int error;
285 mc_driver->driver.owner = owner;
286 mc_driver->driver.bus = &fsl_mc_bus_type;
288 if (mc_driver->probe)
289 mc_driver->driver.probe = fsl_mc_driver_probe;
291 if (mc_driver->remove)
292 mc_driver->driver.remove = fsl_mc_driver_remove;
294 if (mc_driver->shutdown)
295 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
297 error = driver_register(&mc_driver->driver);
298 if (error < 0) {
299 pr_err("driver_register() failed for %s: %d\n",
300 mc_driver->driver.name, error);
301 return error;
304 return 0;
306 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
309 * fsl_mc_driver_unregister - unregisters a device driver from the
310 * MC bus
312 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
314 driver_unregister(&mc_driver->driver);
316 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
319 * mc_get_version() - Retrieves the Management Complex firmware
320 * version information
321 * @mc_io: Pointer to opaque I/O object
322 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
323 * @mc_ver_info: Returned version information structure
325 * Return: '0' on Success; Error code otherwise.
327 static int mc_get_version(struct fsl_mc_io *mc_io,
328 u32 cmd_flags,
329 struct fsl_mc_version *mc_ver_info)
331 struct fsl_mc_command cmd = { 0 };
332 struct dpmng_rsp_get_version *rsp_params;
333 int err;
335 /* prepare command */
336 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
337 cmd_flags,
340 /* send command to mc*/
341 err = mc_send_command(mc_io, &cmd);
342 if (err)
343 return err;
345 /* retrieve response parameters */
346 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
347 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
348 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
349 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
351 return 0;
355 * fsl_mc_get_version - function to retrieve the MC f/w version information
357 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
359 struct fsl_mc_version *fsl_mc_get_version(void)
361 if (mc_version.major)
362 return &mc_version;
364 return NULL;
366 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
369 * fsl_mc_get_root_dprc - function to traverse to the root dprc
371 static void fsl_mc_get_root_dprc(struct device *dev,
372 struct device **root_dprc_dev)
374 if (!dev) {
375 *root_dprc_dev = NULL;
376 } else if (!dev_is_fsl_mc(dev)) {
377 *root_dprc_dev = NULL;
378 } else {
379 *root_dprc_dev = dev;
380 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
381 *root_dprc_dev = (*root_dprc_dev)->parent;
385 static int get_dprc_attr(struct fsl_mc_io *mc_io,
386 int container_id, struct dprc_attributes *attr)
388 u16 dprc_handle;
389 int error;
391 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
392 if (error < 0) {
393 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
394 return error;
397 memset(attr, 0, sizeof(struct dprc_attributes));
398 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
399 if (error < 0) {
400 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
401 error);
402 goto common_cleanup;
405 error = 0;
407 common_cleanup:
408 (void)dprc_close(mc_io, 0, dprc_handle);
409 return error;
412 static int get_dprc_icid(struct fsl_mc_io *mc_io,
413 int container_id, u16 *icid)
415 struct dprc_attributes attr;
416 int error;
418 error = get_dprc_attr(mc_io, container_id, &attr);
419 if (error == 0)
420 *icid = attr.icid;
422 return error;
425 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
426 enum dprc_region_type mc_region_type,
427 u64 mc_offset, phys_addr_t *phys_addr)
429 int i;
430 struct device *root_dprc_dev;
431 struct fsl_mc *mc;
433 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
434 mc = dev_get_drvdata(root_dprc_dev->parent);
436 if (mc->num_translation_ranges == 0) {
438 * Do identity mapping:
440 *phys_addr = mc_offset;
441 return 0;
444 for (i = 0; i < mc->num_translation_ranges; i++) {
445 struct fsl_mc_addr_translation_range *range =
446 &mc->translation_ranges[i];
448 if (mc_region_type == range->mc_region_type &&
449 mc_offset >= range->start_mc_offset &&
450 mc_offset < range->end_mc_offset) {
451 *phys_addr = range->start_phys_addr +
452 (mc_offset - range->start_mc_offset);
453 return 0;
457 return -EFAULT;
460 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
461 struct fsl_mc_device *mc_bus_dev)
463 int i;
464 int error;
465 struct resource *regions;
466 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
467 struct device *parent_dev = mc_dev->dev.parent;
468 enum dprc_region_type mc_region_type;
470 if (is_fsl_mc_bus_dprc(mc_dev) ||
471 is_fsl_mc_bus_dpmcp(mc_dev)) {
472 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
473 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
474 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
475 } else {
477 * This function should not have been called for this MC object
478 * type, as this object type is not supposed to have MMIO
479 * regions
481 return -EINVAL;
484 regions = kmalloc_array(obj_desc->region_count,
485 sizeof(regions[0]), GFP_KERNEL);
486 if (!regions)
487 return -ENOMEM;
489 for (i = 0; i < obj_desc->region_count; i++) {
490 struct dprc_region_desc region_desc;
492 error = dprc_get_obj_region(mc_bus_dev->mc_io,
494 mc_bus_dev->mc_handle,
495 obj_desc->type,
496 obj_desc->id, i, &region_desc);
497 if (error < 0) {
498 dev_err(parent_dev,
499 "dprc_get_obj_region() failed: %d\n", error);
500 goto error_cleanup_regions;
503 * Older MC only returned region offset and no base address
504 * If base address is in the region_desc use it otherwise
505 * revert to old mechanism
507 if (region_desc.base_address)
508 regions[i].start = region_desc.base_address +
509 region_desc.base_offset;
510 else
511 error = translate_mc_addr(mc_dev, mc_region_type,
512 region_desc.base_offset,
513 &regions[i].start);
515 if (error < 0) {
516 dev_err(parent_dev,
517 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
518 region_desc.base_offset,
519 obj_desc->type, obj_desc->id, i);
520 goto error_cleanup_regions;
523 regions[i].end = regions[i].start + region_desc.size - 1;
524 regions[i].name = "fsl-mc object MMIO region";
525 regions[i].flags = IORESOURCE_IO;
526 if (region_desc.flags & DPRC_REGION_CACHEABLE)
527 regions[i].flags |= IORESOURCE_CACHEABLE;
528 if (region_desc.flags & DPRC_REGION_SHAREABLE)
529 regions[i].flags |= IORESOURCE_MEM;
532 mc_dev->regions = regions;
533 return 0;
535 error_cleanup_regions:
536 kfree(regions);
537 return error;
541 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
543 bool fsl_mc_is_root_dprc(struct device *dev)
545 struct device *root_dprc_dev;
547 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
548 if (!root_dprc_dev)
549 return false;
550 return dev == root_dprc_dev;
553 static void fsl_mc_device_release(struct device *dev)
555 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
557 kfree(mc_dev->regions);
559 if (is_fsl_mc_bus_dprc(mc_dev))
560 kfree(to_fsl_mc_bus(mc_dev));
561 else
562 kfree(mc_dev);
566 * Add a newly discovered fsl-mc device to be visible in Linux
568 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
569 struct fsl_mc_io *mc_io,
570 struct device *parent_dev,
571 struct fsl_mc_device **new_mc_dev)
573 int error;
574 struct fsl_mc_device *mc_dev = NULL;
575 struct fsl_mc_bus *mc_bus = NULL;
576 struct fsl_mc_device *parent_mc_dev;
578 if (dev_is_fsl_mc(parent_dev))
579 parent_mc_dev = to_fsl_mc_device(parent_dev);
580 else
581 parent_mc_dev = NULL;
583 if (strcmp(obj_desc->type, "dprc") == 0) {
585 * Allocate an MC bus device object:
587 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
588 if (!mc_bus)
589 return -ENOMEM;
591 mc_dev = &mc_bus->mc_dev;
592 } else {
594 * Allocate a regular fsl_mc_device object:
596 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
597 if (!mc_dev)
598 return -ENOMEM;
601 mc_dev->obj_desc = *obj_desc;
602 mc_dev->mc_io = mc_io;
603 device_initialize(&mc_dev->dev);
604 mc_dev->dev.parent = parent_dev;
605 mc_dev->dev.bus = &fsl_mc_bus_type;
606 mc_dev->dev.release = fsl_mc_device_release;
607 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
608 if (!mc_dev->dev.type) {
609 error = -ENODEV;
610 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
611 goto error_cleanup_dev;
613 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
615 if (strcmp(obj_desc->type, "dprc") == 0) {
616 struct fsl_mc_io *mc_io2;
618 mc_dev->flags |= FSL_MC_IS_DPRC;
621 * To get the DPRC's ICID, we need to open the DPRC
622 * in get_dprc_icid(). For child DPRCs, we do so using the
623 * parent DPRC's MC portal instead of the child DPRC's MC
624 * portal, in case the child DPRC is already opened with
625 * its own portal (e.g., the DPRC used by AIOP).
627 * NOTE: There cannot be more than one active open for a
628 * given MC object, using the same MC portal.
630 if (parent_mc_dev) {
632 * device being added is a child DPRC device
634 mc_io2 = parent_mc_dev->mc_io;
635 } else {
637 * device being added is the root DPRC device
639 if (!mc_io) {
640 error = -EINVAL;
641 goto error_cleanup_dev;
644 mc_io2 = mc_io;
647 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
648 if (error < 0)
649 goto error_cleanup_dev;
650 } else {
652 * A non-DPRC object has to be a child of a DPRC, use the
653 * parent's ICID and interrupt domain.
655 mc_dev->icid = parent_mc_dev->icid;
656 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
657 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
658 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
659 dev_set_msi_domain(&mc_dev->dev,
660 dev_get_msi_domain(&parent_mc_dev->dev));
664 * Get MMIO regions for the device from the MC:
666 * NOTE: the root DPRC is a special case as its MMIO region is
667 * obtained from the device tree
669 if (parent_mc_dev && obj_desc->region_count != 0) {
670 error = fsl_mc_device_get_mmio_regions(mc_dev,
671 parent_mc_dev);
672 if (error < 0)
673 goto error_cleanup_dev;
677 * The device-specific probe callback will get invoked by device_add()
679 error = device_add(&mc_dev->dev);
680 if (error < 0) {
681 dev_err(parent_dev,
682 "device_add() failed for device %s: %d\n",
683 dev_name(&mc_dev->dev), error);
684 goto error_cleanup_dev;
687 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
689 *new_mc_dev = mc_dev;
690 return 0;
692 error_cleanup_dev:
693 kfree(mc_dev->regions);
694 kfree(mc_bus);
695 kfree(mc_dev);
697 return error;
699 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
702 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
703 * Linux
705 * @mc_dev: Pointer to an fsl-mc device
707 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
710 * The device-specific remove callback will get invoked by device_del()
712 device_del(&mc_dev->dev);
713 put_device(&mc_dev->dev);
715 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
717 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
719 struct fsl_mc_device *mc_bus_dev, *endpoint;
720 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
721 struct dprc_endpoint endpoint1 = {{ 0 }};
722 struct dprc_endpoint endpoint2 = {{ 0 }};
723 int state, err;
725 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
726 strcpy(endpoint1.type, mc_dev->obj_desc.type);
727 endpoint1.id = mc_dev->obj_desc.id;
729 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
730 mc_bus_dev->mc_handle,
731 &endpoint1, &endpoint2,
732 &state);
734 if (err == -ENOTCONN || state == -1)
735 return ERR_PTR(-ENOTCONN);
737 if (err < 0) {
738 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
739 return ERR_PTR(err);
742 strcpy(endpoint_desc.type, endpoint2.type);
743 endpoint_desc.id = endpoint2.id;
744 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
746 return endpoint;
748 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
750 static int parse_mc_ranges(struct device *dev,
751 int *paddr_cells,
752 int *mc_addr_cells,
753 int *mc_size_cells,
754 const __be32 **ranges_start)
756 const __be32 *prop;
757 int range_tuple_cell_count;
758 int ranges_len;
759 int tuple_len;
760 struct device_node *mc_node = dev->of_node;
762 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
763 if (!(*ranges_start) || !ranges_len) {
764 dev_warn(dev,
765 "missing or empty ranges property for device tree node '%pOFn'\n",
766 mc_node);
767 return 0;
770 *paddr_cells = of_n_addr_cells(mc_node);
772 prop = of_get_property(mc_node, "#address-cells", NULL);
773 if (prop)
774 *mc_addr_cells = be32_to_cpup(prop);
775 else
776 *mc_addr_cells = *paddr_cells;
778 prop = of_get_property(mc_node, "#size-cells", NULL);
779 if (prop)
780 *mc_size_cells = be32_to_cpup(prop);
781 else
782 *mc_size_cells = of_n_size_cells(mc_node);
784 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
785 *mc_size_cells;
787 tuple_len = range_tuple_cell_count * sizeof(__be32);
788 if (ranges_len % tuple_len != 0) {
789 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
790 return -EINVAL;
793 return ranges_len / tuple_len;
796 static int get_mc_addr_translation_ranges(struct device *dev,
797 struct fsl_mc_addr_translation_range
798 **ranges,
799 u8 *num_ranges)
801 int ret;
802 int paddr_cells;
803 int mc_addr_cells;
804 int mc_size_cells;
805 int i;
806 const __be32 *ranges_start;
807 const __be32 *cell;
809 ret = parse_mc_ranges(dev,
810 &paddr_cells,
811 &mc_addr_cells,
812 &mc_size_cells,
813 &ranges_start);
814 if (ret < 0)
815 return ret;
817 *num_ranges = ret;
818 if (!ret) {
820 * Missing or empty ranges property ("ranges;") for the
821 * 'fsl,qoriq-mc' node. In this case, identity mapping
822 * will be used.
824 *ranges = NULL;
825 return 0;
828 *ranges = devm_kcalloc(dev, *num_ranges,
829 sizeof(struct fsl_mc_addr_translation_range),
830 GFP_KERNEL);
831 if (!(*ranges))
832 return -ENOMEM;
834 cell = ranges_start;
835 for (i = 0; i < *num_ranges; ++i) {
836 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
838 range->mc_region_type = of_read_number(cell, 1);
839 range->start_mc_offset = of_read_number(cell + 1,
840 mc_addr_cells - 1);
841 cell += mc_addr_cells;
842 range->start_phys_addr = of_read_number(cell, paddr_cells);
843 cell += paddr_cells;
844 range->end_mc_offset = range->start_mc_offset +
845 of_read_number(cell, mc_size_cells);
847 cell += mc_size_cells;
850 return 0;
854 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
855 * added
857 static int fsl_mc_bus_probe(struct platform_device *pdev)
859 struct fsl_mc_obj_desc obj_desc;
860 int error;
861 struct fsl_mc *mc;
862 struct fsl_mc_device *mc_bus_dev = NULL;
863 struct fsl_mc_io *mc_io = NULL;
864 int container_id;
865 phys_addr_t mc_portal_phys_addr;
866 u32 mc_portal_size;
867 struct resource res;
869 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
870 if (!mc)
871 return -ENOMEM;
873 platform_set_drvdata(pdev, mc);
876 * Get physical address of MC portal for the root DPRC:
878 error = of_address_to_resource(pdev->dev.of_node, 0, &res);
879 if (error < 0) {
880 dev_err(&pdev->dev,
881 "of_address_to_resource() failed for %pOF\n",
882 pdev->dev.of_node);
883 return error;
886 mc_portal_phys_addr = res.start;
887 mc_portal_size = resource_size(&res);
888 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
889 mc_portal_size, NULL,
890 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
891 if (error < 0)
892 return error;
894 error = mc_get_version(mc_io, 0, &mc_version);
895 if (error != 0) {
896 dev_err(&pdev->dev,
897 "mc_get_version() failed with error %d\n", error);
898 goto error_cleanup_mc_io;
901 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
902 mc_version.major, mc_version.minor, mc_version.revision);
904 error = get_mc_addr_translation_ranges(&pdev->dev,
905 &mc->translation_ranges,
906 &mc->num_translation_ranges);
907 if (error < 0)
908 goto error_cleanup_mc_io;
910 error = dprc_get_container_id(mc_io, 0, &container_id);
911 if (error < 0) {
912 dev_err(&pdev->dev,
913 "dprc_get_container_id() failed: %d\n", error);
914 goto error_cleanup_mc_io;
917 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
918 error = dprc_get_api_version(mc_io, 0,
919 &obj_desc.ver_major,
920 &obj_desc.ver_minor);
921 if (error < 0)
922 goto error_cleanup_mc_io;
924 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
925 strcpy(obj_desc.type, "dprc");
926 obj_desc.id = container_id;
927 obj_desc.irq_count = 1;
928 obj_desc.region_count = 0;
930 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
931 if (error < 0)
932 goto error_cleanup_mc_io;
934 mc->root_mc_bus_dev = mc_bus_dev;
935 return 0;
937 error_cleanup_mc_io:
938 fsl_destroy_mc_io(mc_io);
939 return error;
943 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
944 * removed
946 static int fsl_mc_bus_remove(struct platform_device *pdev)
948 struct fsl_mc *mc = platform_get_drvdata(pdev);
950 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
951 return -EINVAL;
953 fsl_mc_device_remove(mc->root_mc_bus_dev);
955 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
956 mc->root_mc_bus_dev->mc_io = NULL;
958 return 0;
961 static const struct of_device_id fsl_mc_bus_match_table[] = {
962 {.compatible = "fsl,qoriq-mc",},
966 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
968 static struct platform_driver fsl_mc_bus_driver = {
969 .driver = {
970 .name = "fsl_mc_bus",
971 .pm = NULL,
972 .of_match_table = fsl_mc_bus_match_table,
974 .probe = fsl_mc_bus_probe,
975 .remove = fsl_mc_bus_remove,
978 static int __init fsl_mc_bus_driver_init(void)
980 int error;
982 error = bus_register(&fsl_mc_bus_type);
983 if (error < 0) {
984 pr_err("bus type registration failed: %d\n", error);
985 goto error_cleanup_cache;
988 error = platform_driver_register(&fsl_mc_bus_driver);
989 if (error < 0) {
990 pr_err("platform_driver_register() failed: %d\n", error);
991 goto error_cleanup_bus;
994 error = dprc_driver_init();
995 if (error < 0)
996 goto error_cleanup_driver;
998 error = fsl_mc_allocator_driver_init();
999 if (error < 0)
1000 goto error_cleanup_dprc_driver;
1002 return 0;
1004 error_cleanup_dprc_driver:
1005 dprc_driver_exit();
1007 error_cleanup_driver:
1008 platform_driver_unregister(&fsl_mc_bus_driver);
1010 error_cleanup_bus:
1011 bus_unregister(&fsl_mc_bus_type);
1013 error_cleanup_cache:
1014 return error;
1016 postcore_initcall(fsl_mc_bus_driver_init);