1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2012 (C), Jason Cooper <jason@lakedaemon.net>
4  *
5  * arch/arm/mach-mvebu/kirkwood.c
6  *
7  * Flattened Device Tree board initialization
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mbus.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_net.h>
17 #include <linux/of_platform.h>
18 #include <linux/slab.h>
19 #include <asm/hardware/cache-feroceon-l2.h>
20 #include <asm/mach/arch.h>
21 #include <asm/mach/map.h>
22 #include "kirkwood.h"
23 #include "kirkwood-pm.h"
24 #include "common.h"
25 
26 static struct resource kirkwood_cpufreq_resources[] = {
27 	[0] = {
28 		.start  = CPU_CONTROL_PHYS,
29 		.end    = CPU_CONTROL_PHYS + 3,
30 		.flags  = IORESOURCE_MEM,
31 	},
32 };
33 
34 static struct platform_device kirkwood_cpufreq_device = {
35 	.name		= "kirkwood-cpufreq",
36 	.id		= -1,
37 	.num_resources	= ARRAY_SIZE(kirkwood_cpufreq_resources),
38 	.resource	= kirkwood_cpufreq_resources,
39 };
40 
kirkwood_cpufreq_init(void)41 static void __init kirkwood_cpufreq_init(void)
42 {
43 	platform_device_register(&kirkwood_cpufreq_device);
44 }
45 
46 static struct resource kirkwood_cpuidle_resource[] = {
47 	{
48 		.flags	= IORESOURCE_MEM,
49 		.start	= DDR_OPERATION_BASE,
50 		.end	= DDR_OPERATION_BASE + 3,
51 	},
52 };
53 
54 static struct platform_device kirkwood_cpuidle = {
55 	.name		= "kirkwood_cpuidle",
56 	.id		= -1,
57 	.resource	= kirkwood_cpuidle_resource,
58 	.num_resources	= 1,
59 };
60 
kirkwood_cpuidle_init(void)61 static void __init kirkwood_cpuidle_init(void)
62 {
63 	platform_device_register(&kirkwood_cpuidle);
64 }
65 
66 #define MV643XX_ETH_MAC_ADDR_LOW	0x0414
67 #define MV643XX_ETH_MAC_ADDR_HIGH	0x0418
68 
kirkwood_dt_eth_fixup(void)69 static void __init kirkwood_dt_eth_fixup(void)
70 {
71 	struct device_node *np;
72 
73 	/*
74 	 * The ethernet interfaces forget the MAC address assigned by u-boot
75 	 * if the clocks are turned off. Usually, u-boot on kirkwood boards
76 	 * has no DT support to properly set local-mac-address property.
77 	 * As a workaround, we get the MAC address from mv643xx_eth registers
78 	 * and update the port device node if no valid MAC address is set.
79 	 */
80 	for_each_compatible_node(np, NULL, "marvell,kirkwood-eth-port") {
81 		struct device_node *pnp = of_get_parent(np);
82 		struct clk *clk;
83 		struct property *pmac;
84 		u8 tmpmac[ETH_ALEN];
85 		void __iomem *io;
86 		u8 *macaddr;
87 		u32 reg;
88 
89 		if (!pnp)
90 			continue;
91 
92 		/* skip disabled nodes or nodes with valid MAC address*/
93 		if (!of_device_is_available(pnp) ||
94 		    !of_get_mac_address(np, tmpmac))
95 			goto eth_fixup_skip;
96 
97 		clk = of_clk_get(pnp, 0);
98 		if (IS_ERR(clk))
99 			goto eth_fixup_skip;
100 
101 		io = of_iomap(pnp, 0);
102 		if (!io)
103 			goto eth_fixup_no_map;
104 
105 		/* ensure port clock is not gated to not hang CPU */
106 		clk_prepare_enable(clk);
107 
108 		/* store MAC address register contents in local-mac-address */
109 		pmac = kzalloc(sizeof(*pmac) + 6, GFP_KERNEL);
110 		if (!pmac)
111 			goto eth_fixup_no_mem;
112 
113 		pmac->value = pmac + 1;
114 		pmac->length = 6;
115 		pmac->name = kstrdup("local-mac-address", GFP_KERNEL);
116 		if (!pmac->name) {
117 			kfree(pmac);
118 			goto eth_fixup_no_mem;
119 		}
120 
121 		macaddr = pmac->value;
122 		reg = readl(io + MV643XX_ETH_MAC_ADDR_HIGH);
123 		macaddr[0] = (reg >> 24) & 0xff;
124 		macaddr[1] = (reg >> 16) & 0xff;
125 		macaddr[2] = (reg >> 8) & 0xff;
126 		macaddr[3] = reg & 0xff;
127 
128 		reg = readl(io + MV643XX_ETH_MAC_ADDR_LOW);
129 		macaddr[4] = (reg >> 8) & 0xff;
130 		macaddr[5] = reg & 0xff;
131 
132 		of_update_property(np, pmac);
133 
134 eth_fixup_no_mem:
135 		iounmap(io);
136 		clk_disable_unprepare(clk);
137 eth_fixup_no_map:
138 		clk_put(clk);
139 eth_fixup_skip:
140 		of_node_put(pnp);
141 	}
142 }
143 
144 /*
145  * Disable propagation of mbus errors to the CPU local bus, as this
146  * causes mbus errors (which can occur for example for PCI aborts) to
147  * throw CPU aborts, which we're not set up to deal with.
148  */
kirkwood_disable_mbus_error_propagation(void)149 static void kirkwood_disable_mbus_error_propagation(void)
150 {
151 	void __iomem *cpu_config;
152 
153 	cpu_config = ioremap(CPU_CONFIG_PHYS, 4);
154 	writel(readl(cpu_config) & ~CPU_CONFIG_ERROR_PROP, cpu_config);
155 }
156 
157 static struct of_dev_auxdata auxdata[] __initdata = {
158 	OF_DEV_AUXDATA("marvell,kirkwood-audio", 0xf10a0000,
159 		       "mvebu-audio", NULL),
160 	{ /* sentinel */ }
161 };
162 
kirkwood_dt_init(void)163 static void __init kirkwood_dt_init(void)
164 {
165 	kirkwood_disable_mbus_error_propagation();
166 
167 	BUG_ON(mvebu_mbus_dt_init(false));
168 
169 #ifdef CONFIG_CACHE_FEROCEON_L2
170 	feroceon_of_init();
171 #endif
172 	kirkwood_cpufreq_init();
173 	kirkwood_cpuidle_init();
174 
175 	kirkwood_pm_init();
176 	kirkwood_dt_eth_fixup();
177 
178 	of_platform_default_populate(NULL, auxdata, NULL);
179 }
180 
181 static const char * const kirkwood_dt_board_compat[] __initconst = {
182 	"marvell,kirkwood",
183 	NULL
184 };
185 
186 DT_MACHINE_START(KIRKWOOD_DT, "Marvell Kirkwood (Flattened Device Tree)")
187 	/* Maintainer: Jason Cooper <jason@lakedaemon.net> */
188 	.init_machine	= kirkwood_dt_init,
189 	.restart	= mvebu_restart,
190 	.dt_compat	= kirkwood_dt_board_compat,
191 MACHINE_END
192