Merge tag 'linux-watchdog-4.20-rc1' of git://www.linux-watchdog.org/linux-watchdog
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / embedded6xx / wii.c
1 /*
2  * arch/powerpc/platforms/embedded6xx/wii.c
3  *
4  * Nintendo Wii board-specific support
5  * Copyright (C) 2008-2009 The GameCube Linux Team
6  * Copyright (C) 2008,2009 Albert Herranz
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  */
14 #define DRV_MODULE_NAME "wii"
15 #define pr_fmt(fmt) DRV_MODULE_NAME ": " fmt
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/irq.h>
20 #include <linux/seq_file.h>
21 #include <linux/of_platform.h>
22 #include <linux/memblock.h>
23 #include <mm/mmu_decl.h>
24
25 #include <asm/io.h>
26 #include <asm/machdep.h>
27 #include <asm/prom.h>
28 #include <asm/time.h>
29 #include <asm/udbg.h>
30
31 #include "flipper-pic.h"
32 #include "hlwd-pic.h"
33 #include "usbgecko_udbg.h"
34
35 /* control block */
36 #define HW_CTRL_COMPATIBLE      "nintendo,hollywood-control"
37
38 #define HW_CTRL_RESETS          0x94
39 #define HW_CTRL_RESETS_SYS      (1<<0)
40
41 /* gpio */
42 #define HW_GPIO_COMPATIBLE      "nintendo,hollywood-gpio"
43
44 #define HW_GPIO_BASE(idx)       (idx * 0x20)
45 #define HW_GPIO_OUT(idx)        (HW_GPIO_BASE(idx) + 0)
46 #define HW_GPIO_DIR(idx)        (HW_GPIO_BASE(idx) + 4)
47 #define HW_GPIO_OWNER           (HW_GPIO_BASE(1) + 0x1c)
48
49 #define HW_GPIO_SHUTDOWN        (1<<1)
50 #define HW_GPIO_SLOT_LED        (1<<5)
51 #define HW_GPIO_SENSOR_BAR      (1<<8)
52
53
54 static void __iomem *hw_ctrl;
55 static void __iomem *hw_gpio;
56
57 unsigned long wii_hole_start;
58 unsigned long wii_hole_size;
59
60
61 static int __init page_aligned(unsigned long x)
62 {
63         return !(x & (PAGE_SIZE-1));
64 }
65
66 void __init wii_memory_fixups(void)
67 {
68         struct memblock_region *p = memblock.memory.regions;
69
70         BUG_ON(memblock.memory.cnt != 2);
71         BUG_ON(!page_aligned(p[0].base) || !page_aligned(p[1].base));
72
73         /* determine hole */
74         wii_hole_start = ALIGN(p[0].base + p[0].size, PAGE_SIZE);
75         wii_hole_size = p[1].base - wii_hole_start;
76 }
77
78 unsigned long __init wii_mmu_mapin_mem2(unsigned long top)
79 {
80         unsigned long delta, size, bl;
81         unsigned long max_size = (256<<20);
82
83         /* MEM2 64MB@0x10000000 */
84         delta = wii_hole_start + wii_hole_size;
85         size = top - delta;
86         for (bl = 128<<10; bl < max_size; bl <<= 1) {
87                 if (bl * 2 > size)
88                         break;
89         }
90         setbat(4, PAGE_OFFSET+delta, delta, bl, PAGE_KERNEL_X);
91         return delta + bl;
92 }
93
94 static void __noreturn wii_spin(void)
95 {
96         local_irq_disable();
97         for (;;)
98                 cpu_relax();
99 }
100
101 static void __iomem *wii_ioremap_hw_regs(char *name, char *compatible)
102 {
103         void __iomem *hw_regs = NULL;
104         struct device_node *np;
105         struct resource res;
106         int error = -ENODEV;
107
108         np = of_find_compatible_node(NULL, NULL, compatible);
109         if (!np) {
110                 pr_err("no compatible node found for %s\n", compatible);
111                 goto out;
112         }
113         error = of_address_to_resource(np, 0, &res);
114         if (error) {
115                 pr_err("no valid reg found for %pOFn\n", np);
116                 goto out_put;
117         }
118
119         hw_regs = ioremap(res.start, resource_size(&res));
120         if (hw_regs) {
121                 pr_info("%s at 0x%08x mapped to 0x%p\n", name,
122                         res.start, hw_regs);
123         }
124
125 out_put:
126         of_node_put(np);
127 out:
128         return hw_regs;
129 }
130
131 static void __init wii_setup_arch(void)
132 {
133         hw_ctrl = wii_ioremap_hw_regs("hw_ctrl", HW_CTRL_COMPATIBLE);
134         hw_gpio = wii_ioremap_hw_regs("hw_gpio", HW_GPIO_COMPATIBLE);
135         if (hw_gpio) {
136                 /* turn off the front blue led and IR light */
137                 clrbits32(hw_gpio + HW_GPIO_OUT(0),
138                           HW_GPIO_SLOT_LED | HW_GPIO_SENSOR_BAR);
139         }
140 }
141
142 static void __noreturn wii_restart(char *cmd)
143 {
144         local_irq_disable();
145
146         if (hw_ctrl) {
147                 /* clear the system reset pin to cause a reset */
148                 clrbits32(hw_ctrl + HW_CTRL_RESETS, HW_CTRL_RESETS_SYS);
149         }
150         wii_spin();
151 }
152
153 static void wii_power_off(void)
154 {
155         local_irq_disable();
156
157         if (hw_gpio) {
158                 /*
159                  * set the owner of the shutdown pin to ARM, because it is
160                  * accessed through the registers for the ARM, below
161                  */
162                 clrbits32(hw_gpio + HW_GPIO_OWNER, HW_GPIO_SHUTDOWN);
163
164                 /* make sure that the poweroff GPIO is configured as output */
165                 setbits32(hw_gpio + HW_GPIO_DIR(1), HW_GPIO_SHUTDOWN);
166
167                 /* drive the poweroff GPIO high */
168                 setbits32(hw_gpio + HW_GPIO_OUT(1), HW_GPIO_SHUTDOWN);
169         }
170         wii_spin();
171 }
172
173 static void __noreturn wii_halt(void)
174 {
175         if (ppc_md.restart)
176                 ppc_md.restart(NULL);
177         wii_spin();
178 }
179
180 static void __init wii_pic_probe(void)
181 {
182         flipper_pic_probe();
183         hlwd_pic_probe();
184 }
185
186 static int __init wii_probe(void)
187 {
188         if (!of_machine_is_compatible("nintendo,wii"))
189                 return 0;
190
191         pm_power_off = wii_power_off;
192
193         ug_udbg_init();
194
195         return 1;
196 }
197
198 static void wii_shutdown(void)
199 {
200         hlwd_quiesce();
201         flipper_quiesce();
202 }
203
204 define_machine(wii) {
205         .name                   = "wii",
206         .probe                  = wii_probe,
207         .setup_arch             = wii_setup_arch,
208         .restart                = wii_restart,
209         .halt                   = wii_halt,
210         .init_IRQ               = wii_pic_probe,
211         .get_irq                = flipper_pic_get_irq,
212         .calibrate_decr         = generic_calibrate_decr,
213         .progress               = udbg_progress,
214         .machine_shutdown       = wii_shutdown,
215 };
216
217 static const struct of_device_id wii_of_bus[] = {
218         { .compatible = "nintendo,hollywood", },
219         { },
220 };
221
222 static int __init wii_device_probe(void)
223 {
224         if (!machine_is(wii))
225                 return 0;
226
227         of_platform_populate(NULL, wii_of_bus, NULL, NULL);
228         return 0;
229 }
230 device_initcall(wii_device_probe);
231