MIPS: eBPF: Fix icache flush end address
[linux/fpc-iii.git] / drivers / bus / fsl-mc / fsl-mc-bus.c
blobf0404c6d1ff46bd2b5387624156b17cd2cbba598
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 /**
30 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
31 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
32 * @num_translation_ranges: number of entries in addr_translation_ranges
33 * @translation_ranges: array of bus to system address translation ranges
35 struct fsl_mc {
36 struct fsl_mc_device *root_mc_bus_dev;
37 u8 num_translation_ranges;
38 struct fsl_mc_addr_translation_range *translation_ranges;
41 /**
42 * struct fsl_mc_addr_translation_range - bus to system address translation
43 * range
44 * @mc_region_type: Type of MC region for the range being translated
45 * @start_mc_offset: Start MC offset of the range being translated
46 * @end_mc_offset: MC offset of the first byte after the range (last MC
47 * offset of the range is end_mc_offset - 1)
48 * @start_phys_addr: system physical address corresponding to start_mc_addr
50 struct fsl_mc_addr_translation_range {
51 enum dprc_region_type mc_region_type;
52 u64 start_mc_offset;
53 u64 end_mc_offset;
54 phys_addr_t start_phys_addr;
57 /**
58 * struct mc_version
59 * @major: Major version number: incremented on API compatibility changes
60 * @minor: Minor version number: incremented on API additions (that are
61 * backward compatible); reset when major version is incremented
62 * @revision: Internal revision number: incremented on implementation changes
63 * and/or bug fixes that have no impact on API
65 struct mc_version {
66 u32 major;
67 u32 minor;
68 u32 revision;
71 /**
72 * fsl_mc_bus_match - device to driver matching callback
73 * @dev: the fsl-mc device to match against
74 * @drv: the device driver to search for matching fsl-mc object type
75 * structures
77 * Returns 1 on success, 0 otherwise.
79 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
81 const struct fsl_mc_device_id *id;
82 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
83 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
84 bool found = false;
86 if (!mc_drv->match_id_table)
87 goto out;
90 * If the object is not 'plugged' don't match.
91 * Only exception is the root DPRC, which is a special case.
93 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
94 !fsl_mc_is_root_dprc(&mc_dev->dev))
95 goto out;
98 * Traverse the match_id table of the given driver, trying to find
99 * a matching for the given device.
101 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
102 if (id->vendor == mc_dev->obj_desc.vendor &&
103 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
104 found = true;
106 break;
110 out:
111 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
112 return found;
116 * fsl_mc_bus_uevent - callback invoked when a device is added
118 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
120 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
122 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
123 mc_dev->obj_desc.vendor,
124 mc_dev->obj_desc.type))
125 return -ENOMEM;
127 return 0;
130 static int fsl_mc_dma_configure(struct device *dev)
132 struct device *dma_dev = dev;
134 while (dev_is_fsl_mc(dma_dev))
135 dma_dev = dma_dev->parent;
137 return of_dma_configure(dev, dma_dev->of_node, 0);
140 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
141 char *buf)
143 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
145 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
146 mc_dev->obj_desc.type);
148 static DEVICE_ATTR_RO(modalias);
150 static struct attribute *fsl_mc_dev_attrs[] = {
151 &dev_attr_modalias.attr,
152 NULL,
155 ATTRIBUTE_GROUPS(fsl_mc_dev);
157 struct bus_type fsl_mc_bus_type = {
158 .name = "fsl-mc",
159 .match = fsl_mc_bus_match,
160 .uevent = fsl_mc_bus_uevent,
161 .dma_configure = fsl_mc_dma_configure,
162 .dev_groups = fsl_mc_dev_groups,
164 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
166 struct device_type fsl_mc_bus_dprc_type = {
167 .name = "fsl_mc_bus_dprc"
170 struct device_type fsl_mc_bus_dpni_type = {
171 .name = "fsl_mc_bus_dpni"
174 struct device_type fsl_mc_bus_dpio_type = {
175 .name = "fsl_mc_bus_dpio"
178 struct device_type fsl_mc_bus_dpsw_type = {
179 .name = "fsl_mc_bus_dpsw"
182 struct device_type fsl_mc_bus_dpbp_type = {
183 .name = "fsl_mc_bus_dpbp"
186 struct device_type fsl_mc_bus_dpcon_type = {
187 .name = "fsl_mc_bus_dpcon"
190 struct device_type fsl_mc_bus_dpmcp_type = {
191 .name = "fsl_mc_bus_dpmcp"
194 struct device_type fsl_mc_bus_dpmac_type = {
195 .name = "fsl_mc_bus_dpmac"
198 struct device_type fsl_mc_bus_dprtc_type = {
199 .name = "fsl_mc_bus_dprtc"
202 struct device_type fsl_mc_bus_dpseci_type = {
203 .name = "fsl_mc_bus_dpseci"
206 static struct device_type *fsl_mc_get_device_type(const char *type)
208 static const struct {
209 struct device_type *dev_type;
210 const char *type;
211 } dev_types[] = {
212 { &fsl_mc_bus_dprc_type, "dprc" },
213 { &fsl_mc_bus_dpni_type, "dpni" },
214 { &fsl_mc_bus_dpio_type, "dpio" },
215 { &fsl_mc_bus_dpsw_type, "dpsw" },
216 { &fsl_mc_bus_dpbp_type, "dpbp" },
217 { &fsl_mc_bus_dpcon_type, "dpcon" },
218 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
219 { &fsl_mc_bus_dpmac_type, "dpmac" },
220 { &fsl_mc_bus_dprtc_type, "dprtc" },
221 { &fsl_mc_bus_dpseci_type, "dpseci" },
222 { NULL, NULL }
224 int i;
226 for (i = 0; dev_types[i].dev_type; i++)
227 if (!strcmp(dev_types[i].type, type))
228 return dev_types[i].dev_type;
230 return NULL;
233 static int fsl_mc_driver_probe(struct device *dev)
235 struct fsl_mc_driver *mc_drv;
236 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
237 int error;
239 mc_drv = to_fsl_mc_driver(dev->driver);
241 error = mc_drv->probe(mc_dev);
242 if (error < 0) {
243 if (error != -EPROBE_DEFER)
244 dev_err(dev, "%s failed: %d\n", __func__, error);
245 return error;
248 return 0;
251 static int fsl_mc_driver_remove(struct device *dev)
253 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
254 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
255 int error;
257 error = mc_drv->remove(mc_dev);
258 if (error < 0) {
259 dev_err(dev, "%s failed: %d\n", __func__, error);
260 return error;
263 return 0;
266 static void fsl_mc_driver_shutdown(struct device *dev)
268 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
269 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
271 mc_drv->shutdown(mc_dev);
275 * __fsl_mc_driver_register - registers a child device driver with the
276 * MC bus
278 * This function is implicitly invoked from the registration function of
279 * fsl_mc device drivers, which is generated by the
280 * module_fsl_mc_driver() macro.
282 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
283 struct module *owner)
285 int error;
287 mc_driver->driver.owner = owner;
288 mc_driver->driver.bus = &fsl_mc_bus_type;
290 if (mc_driver->probe)
291 mc_driver->driver.probe = fsl_mc_driver_probe;
293 if (mc_driver->remove)
294 mc_driver->driver.remove = fsl_mc_driver_remove;
296 if (mc_driver->shutdown)
297 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
299 error = driver_register(&mc_driver->driver);
300 if (error < 0) {
301 pr_err("driver_register() failed for %s: %d\n",
302 mc_driver->driver.name, error);
303 return error;
306 return 0;
308 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
311 * fsl_mc_driver_unregister - unregisters a device driver from the
312 * MC bus
314 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
316 driver_unregister(&mc_driver->driver);
318 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
321 * mc_get_version() - Retrieves the Management Complex firmware
322 * version information
323 * @mc_io: Pointer to opaque I/O object
324 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
325 * @mc_ver_info: Returned version information structure
327 * Return: '0' on Success; Error code otherwise.
329 static int mc_get_version(struct fsl_mc_io *mc_io,
330 u32 cmd_flags,
331 struct mc_version *mc_ver_info)
333 struct fsl_mc_command cmd = { 0 };
334 struct dpmng_rsp_get_version *rsp_params;
335 int err;
337 /* prepare command */
338 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
339 cmd_flags,
342 /* send command to mc*/
343 err = mc_send_command(mc_io, &cmd);
344 if (err)
345 return err;
347 /* retrieve response parameters */
348 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
349 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
350 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
351 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
353 return 0;
357 * fsl_mc_get_root_dprc - function to traverse to the root dprc
359 static void fsl_mc_get_root_dprc(struct device *dev,
360 struct device **root_dprc_dev)
362 if (!dev) {
363 *root_dprc_dev = NULL;
364 } else if (!dev_is_fsl_mc(dev)) {
365 *root_dprc_dev = NULL;
366 } else {
367 *root_dprc_dev = dev;
368 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
369 *root_dprc_dev = (*root_dprc_dev)->parent;
373 static int get_dprc_attr(struct fsl_mc_io *mc_io,
374 int container_id, struct dprc_attributes *attr)
376 u16 dprc_handle;
377 int error;
379 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
380 if (error < 0) {
381 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
382 return error;
385 memset(attr, 0, sizeof(struct dprc_attributes));
386 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
387 if (error < 0) {
388 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
389 error);
390 goto common_cleanup;
393 error = 0;
395 common_cleanup:
396 (void)dprc_close(mc_io, 0, dprc_handle);
397 return error;
400 static int get_dprc_icid(struct fsl_mc_io *mc_io,
401 int container_id, u16 *icid)
403 struct dprc_attributes attr;
404 int error;
406 error = get_dprc_attr(mc_io, container_id, &attr);
407 if (error == 0)
408 *icid = attr.icid;
410 return error;
413 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
414 enum dprc_region_type mc_region_type,
415 u64 mc_offset, phys_addr_t *phys_addr)
417 int i;
418 struct device *root_dprc_dev;
419 struct fsl_mc *mc;
421 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
422 mc = dev_get_drvdata(root_dprc_dev->parent);
424 if (mc->num_translation_ranges == 0) {
426 * Do identity mapping:
428 *phys_addr = mc_offset;
429 return 0;
432 for (i = 0; i < mc->num_translation_ranges; i++) {
433 struct fsl_mc_addr_translation_range *range =
434 &mc->translation_ranges[i];
436 if (mc_region_type == range->mc_region_type &&
437 mc_offset >= range->start_mc_offset &&
438 mc_offset < range->end_mc_offset) {
439 *phys_addr = range->start_phys_addr +
440 (mc_offset - range->start_mc_offset);
441 return 0;
445 return -EFAULT;
448 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
449 struct fsl_mc_device *mc_bus_dev)
451 int i;
452 int error;
453 struct resource *regions;
454 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
455 struct device *parent_dev = mc_dev->dev.parent;
456 enum dprc_region_type mc_region_type;
458 if (is_fsl_mc_bus_dprc(mc_dev) ||
459 is_fsl_mc_bus_dpmcp(mc_dev)) {
460 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
461 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
462 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
463 } else {
465 * This function should not have been called for this MC object
466 * type, as this object type is not supposed to have MMIO
467 * regions
469 return -EINVAL;
472 regions = kmalloc_array(obj_desc->region_count,
473 sizeof(regions[0]), GFP_KERNEL);
474 if (!regions)
475 return -ENOMEM;
477 for (i = 0; i < obj_desc->region_count; i++) {
478 struct dprc_region_desc region_desc;
480 error = dprc_get_obj_region(mc_bus_dev->mc_io,
482 mc_bus_dev->mc_handle,
483 obj_desc->type,
484 obj_desc->id, i, &region_desc);
485 if (error < 0) {
486 dev_err(parent_dev,
487 "dprc_get_obj_region() failed: %d\n", error);
488 goto error_cleanup_regions;
491 error = translate_mc_addr(mc_dev, mc_region_type,
492 region_desc.base_offset,
493 &regions[i].start);
494 if (error < 0) {
495 dev_err(parent_dev,
496 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
497 region_desc.base_offset,
498 obj_desc->type, obj_desc->id, i);
499 goto error_cleanup_regions;
502 regions[i].end = regions[i].start + region_desc.size - 1;
503 regions[i].name = "fsl-mc object MMIO region";
504 regions[i].flags = IORESOURCE_IO;
505 if (region_desc.flags & DPRC_REGION_CACHEABLE)
506 regions[i].flags |= IORESOURCE_CACHEABLE;
509 mc_dev->regions = regions;
510 return 0;
512 error_cleanup_regions:
513 kfree(regions);
514 return error;
518 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
520 bool fsl_mc_is_root_dprc(struct device *dev)
522 struct device *root_dprc_dev;
524 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
525 if (!root_dprc_dev)
526 return false;
527 return dev == root_dprc_dev;
530 static void fsl_mc_device_release(struct device *dev)
532 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
534 kfree(mc_dev->regions);
536 if (is_fsl_mc_bus_dprc(mc_dev))
537 kfree(to_fsl_mc_bus(mc_dev));
538 else
539 kfree(mc_dev);
543 * Add a newly discovered fsl-mc device to be visible in Linux
545 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
546 struct fsl_mc_io *mc_io,
547 struct device *parent_dev,
548 struct fsl_mc_device **new_mc_dev)
550 int error;
551 struct fsl_mc_device *mc_dev = NULL;
552 struct fsl_mc_bus *mc_bus = NULL;
553 struct fsl_mc_device *parent_mc_dev;
555 if (dev_is_fsl_mc(parent_dev))
556 parent_mc_dev = to_fsl_mc_device(parent_dev);
557 else
558 parent_mc_dev = NULL;
560 if (strcmp(obj_desc->type, "dprc") == 0) {
562 * Allocate an MC bus device object:
564 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
565 if (!mc_bus)
566 return -ENOMEM;
568 mc_dev = &mc_bus->mc_dev;
569 } else {
571 * Allocate a regular fsl_mc_device object:
573 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
574 if (!mc_dev)
575 return -ENOMEM;
578 mc_dev->obj_desc = *obj_desc;
579 mc_dev->mc_io = mc_io;
580 device_initialize(&mc_dev->dev);
581 mc_dev->dev.parent = parent_dev;
582 mc_dev->dev.bus = &fsl_mc_bus_type;
583 mc_dev->dev.release = fsl_mc_device_release;
584 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
585 if (!mc_dev->dev.type) {
586 error = -ENODEV;
587 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
588 goto error_cleanup_dev;
590 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
592 if (strcmp(obj_desc->type, "dprc") == 0) {
593 struct fsl_mc_io *mc_io2;
595 mc_dev->flags |= FSL_MC_IS_DPRC;
598 * To get the DPRC's ICID, we need to open the DPRC
599 * in get_dprc_icid(). For child DPRCs, we do so using the
600 * parent DPRC's MC portal instead of the child DPRC's MC
601 * portal, in case the child DPRC is already opened with
602 * its own portal (e.g., the DPRC used by AIOP).
604 * NOTE: There cannot be more than one active open for a
605 * given MC object, using the same MC portal.
607 if (parent_mc_dev) {
609 * device being added is a child DPRC device
611 mc_io2 = parent_mc_dev->mc_io;
612 } else {
614 * device being added is the root DPRC device
616 if (!mc_io) {
617 error = -EINVAL;
618 goto error_cleanup_dev;
621 mc_io2 = mc_io;
624 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
625 if (error < 0)
626 goto error_cleanup_dev;
627 } else {
629 * A non-DPRC object has to be a child of a DPRC, use the
630 * parent's ICID and interrupt domain.
632 mc_dev->icid = parent_mc_dev->icid;
633 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
634 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
635 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
636 dev_set_msi_domain(&mc_dev->dev,
637 dev_get_msi_domain(&parent_mc_dev->dev));
641 * Get MMIO regions for the device from the MC:
643 * NOTE: the root DPRC is a special case as its MMIO region is
644 * obtained from the device tree
646 if (parent_mc_dev && obj_desc->region_count != 0) {
647 error = fsl_mc_device_get_mmio_regions(mc_dev,
648 parent_mc_dev);
649 if (error < 0)
650 goto error_cleanup_dev;
654 * The device-specific probe callback will get invoked by device_add()
656 error = device_add(&mc_dev->dev);
657 if (error < 0) {
658 dev_err(parent_dev,
659 "device_add() failed for device %s: %d\n",
660 dev_name(&mc_dev->dev), error);
661 goto error_cleanup_dev;
664 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
666 *new_mc_dev = mc_dev;
667 return 0;
669 error_cleanup_dev:
670 kfree(mc_dev->regions);
671 kfree(mc_bus);
672 kfree(mc_dev);
674 return error;
676 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
679 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
680 * Linux
682 * @mc_dev: Pointer to an fsl-mc device
684 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
687 * The device-specific remove callback will get invoked by device_del()
689 device_del(&mc_dev->dev);
690 put_device(&mc_dev->dev);
692 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
694 static int parse_mc_ranges(struct device *dev,
695 int *paddr_cells,
696 int *mc_addr_cells,
697 int *mc_size_cells,
698 const __be32 **ranges_start)
700 const __be32 *prop;
701 int range_tuple_cell_count;
702 int ranges_len;
703 int tuple_len;
704 struct device_node *mc_node = dev->of_node;
706 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
707 if (!(*ranges_start) || !ranges_len) {
708 dev_warn(dev,
709 "missing or empty ranges property for device tree node '%pOFn'\n",
710 mc_node);
711 return 0;
714 *paddr_cells = of_n_addr_cells(mc_node);
716 prop = of_get_property(mc_node, "#address-cells", NULL);
717 if (prop)
718 *mc_addr_cells = be32_to_cpup(prop);
719 else
720 *mc_addr_cells = *paddr_cells;
722 prop = of_get_property(mc_node, "#size-cells", NULL);
723 if (prop)
724 *mc_size_cells = be32_to_cpup(prop);
725 else
726 *mc_size_cells = of_n_size_cells(mc_node);
728 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
729 *mc_size_cells;
731 tuple_len = range_tuple_cell_count * sizeof(__be32);
732 if (ranges_len % tuple_len != 0) {
733 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
734 return -EINVAL;
737 return ranges_len / tuple_len;
740 static int get_mc_addr_translation_ranges(struct device *dev,
741 struct fsl_mc_addr_translation_range
742 **ranges,
743 u8 *num_ranges)
745 int ret;
746 int paddr_cells;
747 int mc_addr_cells;
748 int mc_size_cells;
749 int i;
750 const __be32 *ranges_start;
751 const __be32 *cell;
753 ret = parse_mc_ranges(dev,
754 &paddr_cells,
755 &mc_addr_cells,
756 &mc_size_cells,
757 &ranges_start);
758 if (ret < 0)
759 return ret;
761 *num_ranges = ret;
762 if (!ret) {
764 * Missing or empty ranges property ("ranges;") for the
765 * 'fsl,qoriq-mc' node. In this case, identity mapping
766 * will be used.
768 *ranges = NULL;
769 return 0;
772 *ranges = devm_kcalloc(dev, *num_ranges,
773 sizeof(struct fsl_mc_addr_translation_range),
774 GFP_KERNEL);
775 if (!(*ranges))
776 return -ENOMEM;
778 cell = ranges_start;
779 for (i = 0; i < *num_ranges; ++i) {
780 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
782 range->mc_region_type = of_read_number(cell, 1);
783 range->start_mc_offset = of_read_number(cell + 1,
784 mc_addr_cells - 1);
785 cell += mc_addr_cells;
786 range->start_phys_addr = of_read_number(cell, paddr_cells);
787 cell += paddr_cells;
788 range->end_mc_offset = range->start_mc_offset +
789 of_read_number(cell, mc_size_cells);
791 cell += mc_size_cells;
794 return 0;
798 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
799 * added
801 static int fsl_mc_bus_probe(struct platform_device *pdev)
803 struct fsl_mc_obj_desc obj_desc;
804 int error;
805 struct fsl_mc *mc;
806 struct fsl_mc_device *mc_bus_dev = NULL;
807 struct fsl_mc_io *mc_io = NULL;
808 int container_id;
809 phys_addr_t mc_portal_phys_addr;
810 u32 mc_portal_size;
811 struct mc_version mc_version;
812 struct resource res;
814 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
815 if (!mc)
816 return -ENOMEM;
818 platform_set_drvdata(pdev, mc);
821 * Get physical address of MC portal for the root DPRC:
823 error = of_address_to_resource(pdev->dev.of_node, 0, &res);
824 if (error < 0) {
825 dev_err(&pdev->dev,
826 "of_address_to_resource() failed for %pOF\n",
827 pdev->dev.of_node);
828 return error;
831 mc_portal_phys_addr = res.start;
832 mc_portal_size = resource_size(&res);
833 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
834 mc_portal_size, NULL,
835 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
836 if (error < 0)
837 return error;
839 error = mc_get_version(mc_io, 0, &mc_version);
840 if (error != 0) {
841 dev_err(&pdev->dev,
842 "mc_get_version() failed with error %d\n", error);
843 goto error_cleanup_mc_io;
846 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
847 mc_version.major, mc_version.minor, mc_version.revision);
849 error = get_mc_addr_translation_ranges(&pdev->dev,
850 &mc->translation_ranges,
851 &mc->num_translation_ranges);
852 if (error < 0)
853 goto error_cleanup_mc_io;
855 error = dprc_get_container_id(mc_io, 0, &container_id);
856 if (error < 0) {
857 dev_err(&pdev->dev,
858 "dprc_get_container_id() failed: %d\n", error);
859 goto error_cleanup_mc_io;
862 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
863 error = dprc_get_api_version(mc_io, 0,
864 &obj_desc.ver_major,
865 &obj_desc.ver_minor);
866 if (error < 0)
867 goto error_cleanup_mc_io;
869 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
870 strcpy(obj_desc.type, "dprc");
871 obj_desc.id = container_id;
872 obj_desc.irq_count = 1;
873 obj_desc.region_count = 0;
875 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
876 if (error < 0)
877 goto error_cleanup_mc_io;
879 mc->root_mc_bus_dev = mc_bus_dev;
880 return 0;
882 error_cleanup_mc_io:
883 fsl_destroy_mc_io(mc_io);
884 return error;
888 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
889 * removed
891 static int fsl_mc_bus_remove(struct platform_device *pdev)
893 struct fsl_mc *mc = platform_get_drvdata(pdev);
895 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
896 return -EINVAL;
898 fsl_mc_device_remove(mc->root_mc_bus_dev);
900 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
901 mc->root_mc_bus_dev->mc_io = NULL;
903 return 0;
906 static const struct of_device_id fsl_mc_bus_match_table[] = {
907 {.compatible = "fsl,qoriq-mc",},
911 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
913 static struct platform_driver fsl_mc_bus_driver = {
914 .driver = {
915 .name = "fsl_mc_bus",
916 .pm = NULL,
917 .of_match_table = fsl_mc_bus_match_table,
919 .probe = fsl_mc_bus_probe,
920 .remove = fsl_mc_bus_remove,
923 static int __init fsl_mc_bus_driver_init(void)
925 int error;
927 error = bus_register(&fsl_mc_bus_type);
928 if (error < 0) {
929 pr_err("bus type registration failed: %d\n", error);
930 goto error_cleanup_cache;
933 error = platform_driver_register(&fsl_mc_bus_driver);
934 if (error < 0) {
935 pr_err("platform_driver_register() failed: %d\n", error);
936 goto error_cleanup_bus;
939 error = dprc_driver_init();
940 if (error < 0)
941 goto error_cleanup_driver;
943 error = fsl_mc_allocator_driver_init();
944 if (error < 0)
945 goto error_cleanup_dprc_driver;
947 return 0;
949 error_cleanup_dprc_driver:
950 dprc_driver_exit();
952 error_cleanup_driver:
953 platform_driver_unregister(&fsl_mc_bus_driver);
955 error_cleanup_bus:
956 bus_unregister(&fsl_mc_bus_type);
958 error_cleanup_cache:
959 return error;
961 postcore_initcall(fsl_mc_bus_driver_init);