1/*
2 *  This program is free software; you can redistribute it and/or modify it
3 *  under the terms of the GNU General Public License version 2 as published
4 *  by the Free Software Foundation.
5 *
6 *  Copyright (C) 2010 John Crispin <blogic@openwrt.org>
7 */
8
9#include <linux/init.h>
10#include <linux/io.h>
11#include <linux/ioport.h>
12#include <linux/pm.h>
13#include <linux/export.h>
14#include <linux/delay.h>
15#include <linux/of_address.h>
16#include <linux/of_platform.h>
17
18#include <asm/reboot.h>
19
20#include <lantiq_soc.h>
21
22#include "../prom.h"
23
24#define ltq_rcu_w32(x, y)	ltq_w32((x), ltq_rcu_membase + (y))
25#define ltq_rcu_r32(x)		ltq_r32(ltq_rcu_membase + (x))
26
27/* reset request register */
28#define RCU_RST_REQ		0x0010
29/* reset status register */
30#define RCU_RST_STAT		0x0014
31/* vr9 gphy registers */
32#define RCU_GFS_ADD0_XRX200	0x0020
33#define RCU_GFS_ADD1_XRX200	0x0068
34
35/* reboot bit */
36#define RCU_RD_GPHY0_XRX200	BIT(31)
37#define RCU_RD_SRST		BIT(30)
38#define RCU_RD_GPHY1_XRX200	BIT(29)
39
40/* reset cause */
41#define RCU_STAT_SHIFT		26
42/* boot selection */
43#define RCU_BOOT_SEL(x)		((x >> 18) & 0x7)
44#define RCU_BOOT_SEL_XRX200(x)	(((x >> 17) & 0xf) | ((x >> 8) & 0x10))
45
46/* remapped base addr of the reset control unit */
47static void __iomem *ltq_rcu_membase;
48static struct device_node *ltq_rcu_np;
49
50/* This function is used by the watchdog driver */
51int ltq_reset_cause(void)
52{
53	u32 val = ltq_rcu_r32(RCU_RST_STAT);
54	return val >> RCU_STAT_SHIFT;
55}
56EXPORT_SYMBOL_GPL(ltq_reset_cause);
57
58/* allow platform code to find out what source we booted from */
59unsigned char ltq_boot_select(void)
60{
61	u32 val = ltq_rcu_r32(RCU_RST_STAT);
62
63	if (of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200"))
64		return RCU_BOOT_SEL_XRX200(val);
65
66	return RCU_BOOT_SEL(val);
67}
68
69/* reset / boot a gphy */
70static struct ltq_xrx200_gphy_reset {
71	u32 rd;
72	u32 addr;
73} xrx200_gphy[] = {
74	{RCU_RD_GPHY0_XRX200, RCU_GFS_ADD0_XRX200},
75	{RCU_RD_GPHY1_XRX200, RCU_GFS_ADD1_XRX200},
76};
77
78/* reset and boot a gphy. these phys only exist on xrx200 SoC */
79int xrx200_gphy_boot(struct device *dev, unsigned int id, dma_addr_t dev_addr)
80{
81	struct clk *clk;
82
83	if (!of_device_is_compatible(ltq_rcu_np, "lantiq,rcu-xrx200")) {
84		dev_err(dev, "this SoC has no GPHY\n");
85		return -EINVAL;
86	}
87
88	clk = clk_get_sys("1f203000.rcu", "gphy");
89	if (IS_ERR(clk))
90		return PTR_ERR(clk);
91
92	clk_enable(clk);
93
94	if (id > 1) {
95		dev_err(dev, "%u is an invalid gphy id\n", id);
96		return -EINVAL;
97	}
98	dev_info(dev, "booting GPHY%u firmware at %X\n", id, dev_addr);
99
100	ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | xrx200_gphy[id].rd,
101			RCU_RST_REQ);
102	ltq_rcu_w32(dev_addr, xrx200_gphy[id].addr);
103	ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) & ~xrx200_gphy[id].rd,
104			RCU_RST_REQ);
105	return 0;
106}
107
108/* reset a io domain for u micro seconds */
109void ltq_reset_once(unsigned int module, ulong u)
110{
111	ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | module, RCU_RST_REQ);
112	udelay(u);
113	ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) & ~module, RCU_RST_REQ);
114}
115
116static void ltq_machine_restart(char *command)
117{
118	local_irq_disable();
119	ltq_rcu_w32(ltq_rcu_r32(RCU_RST_REQ) | RCU_RD_SRST, RCU_RST_REQ);
120	unreachable();
121}
122
123static void ltq_machine_halt(void)
124{
125	local_irq_disable();
126	unreachable();
127}
128
129static void ltq_machine_power_off(void)
130{
131	local_irq_disable();
132	unreachable();
133}
134
135static int __init mips_reboot_setup(void)
136{
137	struct resource res;
138
139	ltq_rcu_np = of_find_compatible_node(NULL, NULL, "lantiq,rcu-xway");
140	if (!ltq_rcu_np)
141		ltq_rcu_np = of_find_compatible_node(NULL, NULL,
142							"lantiq,rcu-xrx200");
143
144	/* check if all the reset register range is available */
145	if (!ltq_rcu_np)
146		panic("Failed to load reset resources from devicetree");
147
148	if (of_address_to_resource(ltq_rcu_np, 0, &res))
149		panic("Failed to get rcu memory range");
150
151	if (request_mem_region(res.start, resource_size(&res), res.name) < 0)
152		pr_err("Failed to request rcu memory");
153
154	ltq_rcu_membase = ioremap_nocache(res.start, resource_size(&res));
155	if (!ltq_rcu_membase)
156		panic("Failed to remap core memory");
157
158	_machine_restart = ltq_machine_restart;
159	_machine_halt = ltq_machine_halt;
160	pm_power_off = ltq_machine_power_off;
161
162	return 0;
163}
164
165arch_initcall(mips_reboot_setup);
166