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