0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) "board_staging: " fmt
0008
0009 #include <linux/clkdev.h>
0010 #include <linux/init.h>
0011 #include <linux/irq.h>
0012 #include <linux/device.h>
0013 #include <linux/kernel.h>
0014 #include <linux/of.h>
0015 #include <linux/of_address.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/pm_domain.h>
0019
0020 #include "board.h"
0021
0022 static struct device_node *irqc_node __initdata;
0023 static unsigned int irqc_base __initdata;
0024
0025 static bool find_by_address(u64 base_address)
0026 {
0027 struct device_node *dn = of_find_all_nodes(NULL);
0028 struct resource res;
0029
0030 while (dn) {
0031 if (!of_address_to_resource(dn, 0, &res)) {
0032 if (res.start == base_address) {
0033 of_node_put(dn);
0034 return true;
0035 }
0036 }
0037 dn = of_find_all_nodes(dn);
0038 }
0039
0040 return false;
0041 }
0042
0043 bool __init board_staging_dt_node_available(const struct resource *resource,
0044 unsigned int num_resources)
0045 {
0046 unsigned int i;
0047
0048 for (i = 0; i < num_resources; i++) {
0049 const struct resource *r = resource + i;
0050
0051 if (resource_type(r) == IORESOURCE_MEM)
0052 if (find_by_address(r->start))
0053 return true;
0054 }
0055
0056 return false;
0057 }
0058
0059 int __init board_staging_gic_setup_xlate(const char *gic_match,
0060 unsigned int base)
0061 {
0062 WARN_ON(irqc_node);
0063
0064 irqc_node = of_find_compatible_node(NULL, NULL, gic_match);
0065
0066 WARN_ON(!irqc_node);
0067 if (!irqc_node)
0068 return -ENOENT;
0069
0070 irqc_base = base;
0071 return 0;
0072 }
0073
0074 static void __init gic_fixup_resource(struct resource *res)
0075 {
0076 struct of_phandle_args irq_data;
0077 unsigned int hwirq = res->start;
0078 unsigned int virq;
0079
0080 if (resource_type(res) != IORESOURCE_IRQ || !irqc_node)
0081 return;
0082
0083 irq_data.np = irqc_node;
0084 irq_data.args_count = 3;
0085 irq_data.args[0] = 0;
0086 irq_data.args[1] = hwirq - irqc_base;
0087 switch (res->flags &
0088 (IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE |
0089 IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL)) {
0090 case IORESOURCE_IRQ_LOWEDGE:
0091 irq_data.args[2] = IRQ_TYPE_EDGE_FALLING;
0092 break;
0093 case IORESOURCE_IRQ_HIGHEDGE:
0094 irq_data.args[2] = IRQ_TYPE_EDGE_RISING;
0095 break;
0096 case IORESOURCE_IRQ_LOWLEVEL:
0097 irq_data.args[2] = IRQ_TYPE_LEVEL_LOW;
0098 break;
0099 case IORESOURCE_IRQ_HIGHLEVEL:
0100 default:
0101 irq_data.args[2] = IRQ_TYPE_LEVEL_HIGH;
0102 break;
0103 }
0104
0105 virq = irq_create_of_mapping(&irq_data);
0106 if (WARN_ON(!virq))
0107 return;
0108
0109 pr_debug("hwirq %u -> virq %u\n", hwirq, virq);
0110 res->start = virq;
0111 }
0112
0113 void __init board_staging_gic_fixup_resources(struct resource *res,
0114 unsigned int nres)
0115 {
0116 unsigned int i;
0117
0118 for (i = 0; i < nres; i++)
0119 gic_fixup_resource(&res[i]);
0120 }
0121
0122 int __init board_staging_register_clock(const struct board_staging_clk *bsc)
0123 {
0124 int error;
0125
0126 pr_debug("Aliasing clock %s for con_id %s dev_id %s\n", bsc->clk,
0127 bsc->con_id, bsc->dev_id);
0128 error = clk_add_alias(bsc->con_id, bsc->dev_id, bsc->clk, NULL);
0129 if (error)
0130 pr_err("Failed to alias clock %s (%d)\n", bsc->clk, error);
0131
0132 return error;
0133 }
0134
0135 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
0136 static int board_staging_add_dev_domain(struct platform_device *pdev,
0137 const char *domain)
0138 {
0139 struct device *dev = &pdev->dev;
0140 struct of_phandle_args pd_args;
0141 struct device_node *np;
0142
0143 np = of_find_node_by_path(domain);
0144 if (!np) {
0145 pr_err("Cannot find domain node %s\n", domain);
0146 return -ENOENT;
0147 }
0148
0149 pd_args.np = np;
0150 pd_args.args_count = 0;
0151
0152
0153 spin_lock_init(&dev->power.lock);
0154 dev->power.early_init = true;
0155
0156 return of_genpd_add_device(&pd_args, dev);
0157 }
0158 #else
0159 static inline int board_staging_add_dev_domain(struct platform_device *pdev,
0160 const char *domain)
0161 {
0162 return 0;
0163 }
0164 #endif
0165
0166 int __init board_staging_register_device(const struct board_staging_dev *dev)
0167 {
0168 struct platform_device *pdev = dev->pdev;
0169 unsigned int i;
0170 int error;
0171
0172 pr_debug("Trying to register device %s\n", pdev->name);
0173 if (board_staging_dt_node_available(pdev->resource,
0174 pdev->num_resources)) {
0175 pr_warn("Skipping %s, already in DT\n", pdev->name);
0176 return -EEXIST;
0177 }
0178
0179 board_staging_gic_fixup_resources(pdev->resource, pdev->num_resources);
0180
0181 for (i = 0; i < dev->nclocks; i++)
0182 board_staging_register_clock(&dev->clocks[i]);
0183
0184 if (dev->domain)
0185 board_staging_add_dev_domain(pdev, dev->domain);
0186
0187 error = platform_device_register(pdev);
0188 if (error) {
0189 pr_err("Failed to register device %s (%d)\n", pdev->name,
0190 error);
0191 return error;
0192 }
0193
0194 return error;
0195 }
0196
0197 void __init board_staging_register_devices(const struct board_staging_dev *devs,
0198 unsigned int ndevs)
0199 {
0200 unsigned int i;
0201
0202 for (i = 0; i < ndevs; i++)
0203 board_staging_register_device(&devs[i]);
0204 }