xref: /linux/arch/powerpc/platforms/embedded6xx/wii.c (revision ce6d73c94d7cfbdc3dc31860fe14af2f37d1b4fe)
15a7ee319SAlbert Herranz /*
25a7ee319SAlbert Herranz  * arch/powerpc/platforms/embedded6xx/wii.c
35a7ee319SAlbert Herranz  *
45a7ee319SAlbert Herranz  * Nintendo Wii board-specific support
55a7ee319SAlbert Herranz  * Copyright (C) 2008-2009 The GameCube Linux Team
65a7ee319SAlbert Herranz  * Copyright (C) 2008,2009 Albert Herranz
75a7ee319SAlbert Herranz  *
85a7ee319SAlbert Herranz  * This program is free software; you can redistribute it and/or
95a7ee319SAlbert Herranz  * modify it under the terms of the GNU General Public License
105a7ee319SAlbert Herranz  * as published by the Free Software Foundation; either version 2
115a7ee319SAlbert Herranz  * of the License, or (at your option) any later version.
125a7ee319SAlbert Herranz  *
135a7ee319SAlbert Herranz  */
145a7ee319SAlbert Herranz #define DRV_MODULE_NAME "wii"
155a7ee319SAlbert Herranz #define pr_fmt(fmt) DRV_MODULE_NAME ": " fmt
165a7ee319SAlbert Herranz 
175a7ee319SAlbert Herranz #include <linux/kernel.h>
185a7ee319SAlbert Herranz #include <linux/init.h>
195a7ee319SAlbert Herranz #include <linux/irq.h>
205a7ee319SAlbert Herranz #include <linux/seq_file.h>
215a7ee319SAlbert Herranz #include <linux/of_platform.h>
2295f72d1eSYinghai Lu #include <linux/memblock.h>
23de32400dSAlbert Herranz #include <mm/mmu_decl.h>
245a7ee319SAlbert Herranz 
255a7ee319SAlbert Herranz #include <asm/io.h>
265a7ee319SAlbert Herranz #include <asm/machdep.h>
275a7ee319SAlbert Herranz #include <asm/prom.h>
285a7ee319SAlbert Herranz #include <asm/time.h>
295a7ee319SAlbert Herranz #include <asm/udbg.h>
305a7ee319SAlbert Herranz 
315a7ee319SAlbert Herranz #include "flipper-pic.h"
325a7ee319SAlbert Herranz #include "hlwd-pic.h"
335a7ee319SAlbert Herranz #include "usbgecko_udbg.h"
345a7ee319SAlbert Herranz 
355a7ee319SAlbert Herranz /* control block */
365a7ee319SAlbert Herranz #define HW_CTRL_COMPATIBLE	"nintendo,hollywood-control"
375a7ee319SAlbert Herranz 
385a7ee319SAlbert Herranz #define HW_CTRL_RESETS		0x94
395a7ee319SAlbert Herranz #define HW_CTRL_RESETS_SYS	(1<<0)
405a7ee319SAlbert Herranz 
415a7ee319SAlbert Herranz /* gpio */
425a7ee319SAlbert Herranz #define HW_GPIO_COMPATIBLE	"nintendo,hollywood-gpio"
435a7ee319SAlbert Herranz 
445a7ee319SAlbert Herranz #define HW_GPIO_BASE(idx)	(idx * 0x20)
455a7ee319SAlbert Herranz #define HW_GPIO_OUT(idx)	(HW_GPIO_BASE(idx) + 0)
465a7ee319SAlbert Herranz #define HW_GPIO_DIR(idx)	(HW_GPIO_BASE(idx) + 4)
475a7ee319SAlbert Herranz 
485a7ee319SAlbert Herranz #define HW_GPIO_SHUTDOWN	(1<<1)
495a7ee319SAlbert Herranz #define HW_GPIO_SLOT_LED	(1<<5)
505a7ee319SAlbert Herranz #define HW_GPIO_SENSOR_BAR	(1<<8)
515a7ee319SAlbert Herranz 
525a7ee319SAlbert Herranz 
535a7ee319SAlbert Herranz static void __iomem *hw_ctrl;
545a7ee319SAlbert Herranz static void __iomem *hw_gpio;
555a7ee319SAlbert Herranz 
56de32400dSAlbert Herranz unsigned long wii_hole_start;
57de32400dSAlbert Herranz unsigned long wii_hole_size;
58de32400dSAlbert Herranz 
59de32400dSAlbert Herranz 
60de32400dSAlbert Herranz static int __init page_aligned(unsigned long x)
61de32400dSAlbert Herranz {
62de32400dSAlbert Herranz 	return !(x & (PAGE_SIZE-1));
63de32400dSAlbert Herranz }
64de32400dSAlbert Herranz 
65de32400dSAlbert Herranz void __init wii_memory_fixups(void)
66de32400dSAlbert Herranz {
67823108a0SYinghai Lu 	struct memblock_region *p = memblock.memory.regions;
68de32400dSAlbert Herranz 
69de32400dSAlbert Herranz 	/*
70de32400dSAlbert Herranz 	 * This is part of a workaround to allow the use of two
71c4b9f9a9SThomas Weber 	 * discontinuous RAM ranges on the Wii, even if this is
72de32400dSAlbert Herranz 	 * currently unsupported on 32-bit PowerPC Linux.
73de32400dSAlbert Herranz 	 *
74c4b9f9a9SThomas Weber 	 * We coalesce the two memory ranges of the Wii into a
75de32400dSAlbert Herranz 	 * single range, then create a reservation for the "hole"
76de32400dSAlbert Herranz 	 * between both ranges.
77de32400dSAlbert Herranz 	 */
78de32400dSAlbert Herranz 
7995f72d1eSYinghai Lu 	BUG_ON(memblock.memory.cnt != 2);
80de32400dSAlbert Herranz 	BUG_ON(!page_aligned(p[0].base) || !page_aligned(p[1].base));
81de32400dSAlbert Herranz 
826fbef13cSTejun Heo 	/* trim unaligned tail */
836fbef13cSTejun Heo 	memblock_remove(ALIGN(p[1].base + p[1].size, PAGE_SIZE),
846fbef13cSTejun Heo 			(phys_addr_t)ULLONG_MAX);
85de32400dSAlbert Herranz 
866fbef13cSTejun Heo 	/* determine hole, add & reserve them */
876fbef13cSTejun Heo 	wii_hole_start = ALIGN(p[0].base + p[0].size, PAGE_SIZE);
88de32400dSAlbert Herranz 	wii_hole_size = p[1].base - wii_hole_start;
896fbef13cSTejun Heo 	memblock_add(wii_hole_start, wii_hole_size);
906fbef13cSTejun Heo 	memblock_reserve(wii_hole_start, wii_hole_size);
91de32400dSAlbert Herranz 
926fbef13cSTejun Heo 	BUG_ON(memblock.memory.cnt != 1);
936fbef13cSTejun Heo 	__memblock_dump_all();
94c0577eeeSAlbert Herranz 
95c0577eeeSAlbert Herranz 	/* allow ioremapping the address space in the hole */
96c0577eeeSAlbert Herranz 	__allow_ioremap_reserved = 1;
97de32400dSAlbert Herranz }
98de32400dSAlbert Herranz 
99de32400dSAlbert Herranz unsigned long __init wii_mmu_mapin_mem2(unsigned long top)
100de32400dSAlbert Herranz {
101de32400dSAlbert Herranz 	unsigned long delta, size, bl;
102de32400dSAlbert Herranz 	unsigned long max_size = (256<<20);
103de32400dSAlbert Herranz 
104de32400dSAlbert Herranz 	/* MEM2 64MB@0x10000000 */
105de32400dSAlbert Herranz 	delta = wii_hole_start + wii_hole_size;
106de32400dSAlbert Herranz 	size = top - delta;
107de32400dSAlbert Herranz 	for (bl = 128<<10; bl < max_size; bl <<= 1) {
108de32400dSAlbert Herranz 		if (bl * 2 > size)
109de32400dSAlbert Herranz 			break;
110de32400dSAlbert Herranz 	}
111de32400dSAlbert Herranz 	setbat(4, PAGE_OFFSET+delta, delta, bl, PAGE_KERNEL_X);
112de32400dSAlbert Herranz 	return delta + bl;
113de32400dSAlbert Herranz }
114de32400dSAlbert Herranz 
1155a7ee319SAlbert Herranz static void wii_spin(void)
1165a7ee319SAlbert Herranz {
1175a7ee319SAlbert Herranz 	local_irq_disable();
1185a7ee319SAlbert Herranz 	for (;;)
1195a7ee319SAlbert Herranz 		cpu_relax();
1205a7ee319SAlbert Herranz }
1215a7ee319SAlbert Herranz 
1225a7ee319SAlbert Herranz static void __iomem *wii_ioremap_hw_regs(char *name, char *compatible)
1235a7ee319SAlbert Herranz {
1245a7ee319SAlbert Herranz 	void __iomem *hw_regs = NULL;
1255a7ee319SAlbert Herranz 	struct device_node *np;
1265a7ee319SAlbert Herranz 	struct resource res;
1275a7ee319SAlbert Herranz 	int error = -ENODEV;
1285a7ee319SAlbert Herranz 
1295a7ee319SAlbert Herranz 	np = of_find_compatible_node(NULL, NULL, compatible);
1305a7ee319SAlbert Herranz 	if (!np) {
1315a7ee319SAlbert Herranz 		pr_err("no compatible node found for %s\n", compatible);
1325a7ee319SAlbert Herranz 		goto out;
1335a7ee319SAlbert Herranz 	}
1345a7ee319SAlbert Herranz 	error = of_address_to_resource(np, 0, &res);
1355a7ee319SAlbert Herranz 	if (error) {
1365a7ee319SAlbert Herranz 		pr_err("no valid reg found for %s\n", np->name);
1375a7ee319SAlbert Herranz 		goto out_put;
1385a7ee319SAlbert Herranz 	}
1395a7ee319SAlbert Herranz 
1405a7ee319SAlbert Herranz 	hw_regs = ioremap(res.start, resource_size(&res));
1415a7ee319SAlbert Herranz 	if (hw_regs) {
1425a7ee319SAlbert Herranz 		pr_info("%s at 0x%08x mapped to 0x%p\n", name,
1435a7ee319SAlbert Herranz 			res.start, hw_regs);
1445a7ee319SAlbert Herranz 	}
1455a7ee319SAlbert Herranz 
1465a7ee319SAlbert Herranz out_put:
1475a7ee319SAlbert Herranz 	of_node_put(np);
1485a7ee319SAlbert Herranz out:
1495a7ee319SAlbert Herranz 	return hw_regs;
1505a7ee319SAlbert Herranz }
1515a7ee319SAlbert Herranz 
1525a7ee319SAlbert Herranz static void __init wii_setup_arch(void)
1535a7ee319SAlbert Herranz {
1545a7ee319SAlbert Herranz 	hw_ctrl = wii_ioremap_hw_regs("hw_ctrl", HW_CTRL_COMPATIBLE);
1555a7ee319SAlbert Herranz 	hw_gpio = wii_ioremap_hw_regs("hw_gpio", HW_GPIO_COMPATIBLE);
1565a7ee319SAlbert Herranz 	if (hw_gpio) {
1575a7ee319SAlbert Herranz 		/* turn off the front blue led and IR light */
1585a7ee319SAlbert Herranz 		clrbits32(hw_gpio + HW_GPIO_OUT(0),
1595a7ee319SAlbert Herranz 			  HW_GPIO_SLOT_LED | HW_GPIO_SENSOR_BAR);
1605a7ee319SAlbert Herranz 	}
1615a7ee319SAlbert Herranz }
1625a7ee319SAlbert Herranz 
1635a7ee319SAlbert Herranz static void wii_restart(char *cmd)
1645a7ee319SAlbert Herranz {
1655a7ee319SAlbert Herranz 	local_irq_disable();
1665a7ee319SAlbert Herranz 
1675a7ee319SAlbert Herranz 	if (hw_ctrl) {
1685a7ee319SAlbert Herranz 		/* clear the system reset pin to cause a reset */
1695a7ee319SAlbert Herranz 		clrbits32(hw_ctrl + HW_CTRL_RESETS, HW_CTRL_RESETS_SYS);
1705a7ee319SAlbert Herranz 	}
1715a7ee319SAlbert Herranz 	wii_spin();
1725a7ee319SAlbert Herranz }
1735a7ee319SAlbert Herranz 
1745a7ee319SAlbert Herranz static void wii_power_off(void)
1755a7ee319SAlbert Herranz {
1765a7ee319SAlbert Herranz 	local_irq_disable();
1775a7ee319SAlbert Herranz 
1785a7ee319SAlbert Herranz 	if (hw_gpio) {
1795a7ee319SAlbert Herranz 		/* make sure that the poweroff GPIO is configured as output */
1805a7ee319SAlbert Herranz 		setbits32(hw_gpio + HW_GPIO_DIR(1), HW_GPIO_SHUTDOWN);
1815a7ee319SAlbert Herranz 
1825a7ee319SAlbert Herranz 		/* drive the poweroff GPIO high */
1835a7ee319SAlbert Herranz 		setbits32(hw_gpio + HW_GPIO_OUT(1), HW_GPIO_SHUTDOWN);
1845a7ee319SAlbert Herranz 	}
1855a7ee319SAlbert Herranz 	wii_spin();
1865a7ee319SAlbert Herranz }
1875a7ee319SAlbert Herranz 
1885a7ee319SAlbert Herranz static void wii_halt(void)
1895a7ee319SAlbert Herranz {
1905a7ee319SAlbert Herranz 	if (ppc_md.restart)
1915a7ee319SAlbert Herranz 		ppc_md.restart(NULL);
1925a7ee319SAlbert Herranz 	wii_spin();
1935a7ee319SAlbert Herranz }
1945a7ee319SAlbert Herranz 
1955a7ee319SAlbert Herranz static void __init wii_init_early(void)
1965a7ee319SAlbert Herranz {
1975a7ee319SAlbert Herranz 	ug_udbg_init();
1985a7ee319SAlbert Herranz }
1995a7ee319SAlbert Herranz 
2005a7ee319SAlbert Herranz static void __init wii_pic_probe(void)
2015a7ee319SAlbert Herranz {
2025a7ee319SAlbert Herranz 	flipper_pic_probe();
2035a7ee319SAlbert Herranz 	hlwd_pic_probe();
2045a7ee319SAlbert Herranz }
2055a7ee319SAlbert Herranz 
2065a7ee319SAlbert Herranz static int __init wii_probe(void)
2075a7ee319SAlbert Herranz {
2085a7ee319SAlbert Herranz 	unsigned long dt_root;
2095a7ee319SAlbert Herranz 
2105a7ee319SAlbert Herranz 	dt_root = of_get_flat_dt_root();
2115a7ee319SAlbert Herranz 	if (!of_flat_dt_is_compatible(dt_root, "nintendo,wii"))
2125a7ee319SAlbert Herranz 		return 0;
2135a7ee319SAlbert Herranz 
2145a7ee319SAlbert Herranz 	return 1;
2155a7ee319SAlbert Herranz }
2165a7ee319SAlbert Herranz 
2175a7ee319SAlbert Herranz static void wii_shutdown(void)
2185a7ee319SAlbert Herranz {
2195a7ee319SAlbert Herranz 	hlwd_quiesce();
2205a7ee319SAlbert Herranz 	flipper_quiesce();
2215a7ee319SAlbert Herranz }
2225a7ee319SAlbert Herranz 
2235a7ee319SAlbert Herranz define_machine(wii) {
2245a7ee319SAlbert Herranz 	.name			= "wii",
2255a7ee319SAlbert Herranz 	.probe			= wii_probe,
2265a7ee319SAlbert Herranz 	.init_early		= wii_init_early,
2275a7ee319SAlbert Herranz 	.setup_arch		= wii_setup_arch,
2285a7ee319SAlbert Herranz 	.restart		= wii_restart,
2295a7ee319SAlbert Herranz 	.power_off		= wii_power_off,
2305a7ee319SAlbert Herranz 	.halt			= wii_halt,
2315a7ee319SAlbert Herranz 	.init_IRQ		= wii_pic_probe,
2325a7ee319SAlbert Herranz 	.get_irq		= flipper_pic_get_irq,
2335a7ee319SAlbert Herranz 	.calibrate_decr		= generic_calibrate_decr,
2345a7ee319SAlbert Herranz 	.progress		= udbg_progress,
2355a7ee319SAlbert Herranz 	.machine_shutdown	= wii_shutdown,
2365a7ee319SAlbert Herranz };
2375a7ee319SAlbert Herranz 
238*ce6d73c9SUwe Kleine-König static const struct of_device_id wii_of_bus[] = {
2395a7ee319SAlbert Herranz 	{ .compatible = "nintendo,hollywood", },
2405a7ee319SAlbert Herranz 	{ },
2415a7ee319SAlbert Herranz };
2425a7ee319SAlbert Herranz 
2435a7ee319SAlbert Herranz static int __init wii_device_probe(void)
2445a7ee319SAlbert Herranz {
2455a7ee319SAlbert Herranz 	if (!machine_is(wii))
2465a7ee319SAlbert Herranz 		return 0;
2475a7ee319SAlbert Herranz 
2485a7ee319SAlbert Herranz 	of_platform_bus_probe(NULL, wii_of_bus, NULL);
2495a7ee319SAlbert Herranz 	return 0;
2505a7ee319SAlbert Herranz }
2515a7ee319SAlbert Herranz device_initcall(wii_device_probe);
2525a7ee319SAlbert Herranz 
253