1// SPDX-License-Identifier: GPL-2.0
2/*
3 * DSM-G600 board-setup
4 *
5 * Copyright (C) 2008 Rod Whitby <rod@whitby.id.au>
6 * Copyright (C) 2006 Tower Technologies
7 *
8 * based on ixdp425-setup.c:
9 *      Copyright (C) 2003-2004 MontaVista Software, Inc.
10 * based on nslu2-power.c:
11 *	Copyright (C) 2005 Tower Technologies
12 * based on nslu2-io.c:
13 *	Copyright (C) 2004 Karen Spearel
14 *
15 * Author: Alessandro Zummo <a.zummo@towertech.it>
16 * Author: Michael Westerhof <mwester@dls.net>
17 * Author: Rod Whitby <rod@whitby.id.au>
18 * Maintainers: http://www.nslu2-linux.org/
19 */
20#include <linux/gpio.h>
21#include <linux/irq.h>
22#include <linux/jiffies.h>
23#include <linux/timer.h>
24#include <linux/serial.h>
25#include <linux/serial_8250.h>
26#include <linux/leds.h>
27#include <linux/reboot.h>
28#include <linux/i2c.h>
29#include <linux/gpio/machine.h>
30
31#include <mach/hardware.h>
32
33#include <asm/mach-types.h>
34#include <asm/mach/arch.h>
35#include <asm/mach/flash.h>
36#include <asm/mach/time.h>
37
38#include "irqs.h"
39
40#define DSMG600_SDA_PIN		5
41#define DSMG600_SCL_PIN		4
42
43/* DSM-G600 Timer Setting */
44#define DSMG600_FREQ		66000000
45
46/* Buttons */
47#define DSMG600_PB_GPIO		15	/* power button */
48#define DSMG600_RB_GPIO		3	/* reset button */
49
50/* Power control */
51#define DSMG600_PO_GPIO		2	/* power off */
52
53/* LEDs */
54#define DSMG600_LED_PWR_GPIO	0
55#define DSMG600_LED_WLAN_GPIO	14
56
57static struct flash_platform_data dsmg600_flash_data = {
58	.map_name		= "cfi_probe",
59	.width			= 2,
60};
61
62static struct resource dsmg600_flash_resource = {
63	.flags			= IORESOURCE_MEM,
64};
65
66static struct platform_device dsmg600_flash = {
67	.name			= "IXP4XX-Flash",
68	.id			= 0,
69	.dev.platform_data	= &dsmg600_flash_data,
70	.num_resources		= 1,
71	.resource		= &dsmg600_flash_resource,
72};
73
74static struct gpiod_lookup_table dsmg600_i2c_gpiod_table = {
75	.dev_id		= "i2c-gpio.0",
76	.table		= {
77		GPIO_LOOKUP_IDX("IXP4XX_GPIO_CHIP", DSMG600_SDA_PIN,
78				NULL, 0, GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
79		GPIO_LOOKUP_IDX("IXP4XX_GPIO_CHIP", DSMG600_SCL_PIN,
80				NULL, 1, GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
81	},
82};
83
84static struct platform_device dsmg600_i2c_gpio = {
85	.name			= "i2c-gpio",
86	.id			= 0,
87	.dev	 = {
88		.platform_data	= NULL,
89	},
90};
91
92static struct i2c_board_info __initdata dsmg600_i2c_board_info [] = {
93	{
94		I2C_BOARD_INFO("pcf8563", 0x51),
95	},
96};
97
98static struct gpio_led dsmg600_led_pins[] = {
99	{
100		.name		= "dsmg600:green:power",
101		.gpio		= DSMG600_LED_PWR_GPIO,
102	},
103	{
104		.name		= "dsmg600:green:wlan",
105		.gpio		= DSMG600_LED_WLAN_GPIO,
106		.active_low	= true,
107	},
108};
109
110static struct gpio_led_platform_data dsmg600_led_data = {
111	.num_leds		= ARRAY_SIZE(dsmg600_led_pins),
112	.leds			= dsmg600_led_pins,
113};
114
115static struct platform_device dsmg600_leds = {
116	.name			= "leds-gpio",
117	.id			= -1,
118	.dev.platform_data	= &dsmg600_led_data,
119};
120
121static struct resource dsmg600_uart_resources[] = {
122	{
123		.start		= IXP4XX_UART1_BASE_PHYS,
124		.end		= IXP4XX_UART1_BASE_PHYS + 0x0fff,
125		.flags		= IORESOURCE_MEM,
126	},
127	{
128		.start		= IXP4XX_UART2_BASE_PHYS,
129		.end		= IXP4XX_UART2_BASE_PHYS + 0x0fff,
130		.flags		= IORESOURCE_MEM,
131	}
132};
133
134static struct plat_serial8250_port dsmg600_uart_data[] = {
135	{
136		.mapbase	= IXP4XX_UART1_BASE_PHYS,
137		.membase	= (char *)IXP4XX_UART1_BASE_VIRT + REG_OFFSET,
138		.irq		= IRQ_IXP4XX_UART1,
139		.flags		= UPF_BOOT_AUTOCONF,
140		.iotype		= UPIO_MEM,
141		.regshift	= 2,
142		.uartclk	= IXP4XX_UART_XTAL,
143	},
144	{
145		.mapbase	= IXP4XX_UART2_BASE_PHYS,
146		.membase	= (char *)IXP4XX_UART2_BASE_VIRT + REG_OFFSET,
147		.irq		= IRQ_IXP4XX_UART2,
148		.flags		= UPF_BOOT_AUTOCONF,
149		.iotype		= UPIO_MEM,
150		.regshift	= 2,
151		.uartclk	= IXP4XX_UART_XTAL,
152	},
153	{ }
154};
155
156static struct platform_device dsmg600_uart = {
157	.name			= "serial8250",
158	.id			= PLAT8250_DEV_PLATFORM,
159	.dev.platform_data	= dsmg600_uart_data,
160	.num_resources		= ARRAY_SIZE(dsmg600_uart_resources),
161	.resource		= dsmg600_uart_resources,
162};
163
164static struct platform_device *dsmg600_devices[] __initdata = {
165	&dsmg600_i2c_gpio,
166	&dsmg600_flash,
167	&dsmg600_leds,
168};
169
170static void dsmg600_power_off(void)
171{
172	/* enable the pwr cntl and drive it high */
173	gpio_direction_output(DSMG600_PO_GPIO, 1);
174}
175
176/* This is used to make sure the power-button pusher is serious.  The button
177 * must be held until the value of this counter reaches zero.
178 */
179static int power_button_countdown;
180
181/* Must hold the button down for at least this many counts to be processed */
182#define PBUTTON_HOLDDOWN_COUNT 4 /* 2 secs */
183
184static void dsmg600_power_handler(struct timer_list *unused);
185static DEFINE_TIMER(dsmg600_power_timer, dsmg600_power_handler);
186
187static void dsmg600_power_handler(struct timer_list *unused)
188{
189	/* This routine is called twice per second to check the
190	 * state of the power button.
191	 */
192
193	if (gpio_get_value(DSMG600_PB_GPIO)) {
194
195		/* IO Pin is 1 (button pushed) */
196		if (power_button_countdown > 0)
197			power_button_countdown--;
198
199	} else {
200
201		/* Done on button release, to allow for auto-power-on mods. */
202		if (power_button_countdown == 0) {
203			/* Signal init to do the ctrlaltdel action,
204			 * this will bypass init if it hasn't started
205			 * and do a kernel_restart.
206			 */
207			ctrl_alt_del();
208
209			/* Change the state of the power LED to "blink" */
210			gpio_set_value(DSMG600_LED_PWR_GPIO, 0);
211		} else {
212			power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
213		}
214	}
215
216	mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
217}
218
219static irqreturn_t dsmg600_reset_handler(int irq, void *dev_id)
220{
221	/* This is the paper-clip reset, it shuts the machine down directly. */
222	machine_power_off();
223
224	return IRQ_HANDLED;
225}
226
227static void __init dsmg600_timer_init(void)
228{
229    /* The xtal on this machine is non-standard. */
230    ixp4xx_timer_freq = DSMG600_FREQ;
231
232    /* Call standard timer_init function. */
233    ixp4xx_timer_init();
234}
235
236static int __init dsmg600_gpio_init(void)
237{
238	if (!machine_is_dsmg600())
239		return 0;
240
241	gpio_request(DSMG600_RB_GPIO, "reset button");
242	if (request_irq(gpio_to_irq(DSMG600_RB_GPIO), &dsmg600_reset_handler,
243		IRQF_TRIGGER_LOW, "DSM-G600 reset button", NULL) < 0) {
244
245		printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
246			gpio_to_irq(DSMG600_RB_GPIO));
247	}
248
249	/*
250	 * The power button on the D-Link DSM-G600 is on GPIO 15, but
251	 * it cannot handle interrupts on that GPIO line.  So we'll
252	 * have to poll it with a kernel timer.
253	 */
254
255	/* Make sure that the power button GPIO is set up as an input */
256	gpio_request(DSMG600_PB_GPIO, "power button");
257	gpio_direction_input(DSMG600_PB_GPIO);
258	/* Request poweroff GPIO line */
259	gpio_request(DSMG600_PO_GPIO, "power off button");
260
261	/* Set the initial value for the power button IRQ handler */
262	power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
263
264	mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
265	return 0;
266}
267device_initcall(dsmg600_gpio_init);
268
269static void __init dsmg600_init(void)
270{
271	ixp4xx_sys_init();
272
273	dsmg600_flash_resource.start = IXP4XX_EXP_BUS_BASE(0);
274	dsmg600_flash_resource.end =
275		IXP4XX_EXP_BUS_BASE(0) + ixp4xx_exp_bus_size - 1;
276
277	gpiod_add_lookup_table(&dsmg600_i2c_gpiod_table);
278	i2c_register_board_info(0, dsmg600_i2c_board_info,
279				ARRAY_SIZE(dsmg600_i2c_board_info));
280
281	/* The UART is required on the DSM-G600 (Redboot cannot use the
282	 * NIC) -- do it here so that it does *not* get removed if
283	 * platform_add_devices fails!
284         */
285        (void)platform_device_register(&dsmg600_uart);
286
287	platform_add_devices(dsmg600_devices, ARRAY_SIZE(dsmg600_devices));
288
289	pm_power_off = dsmg600_power_off;
290}
291
292MACHINE_START(DSMG600, "D-Link DSM-G600 RevA")
293	/* Maintainer: www.nslu2-linux.org */
294	.atag_offset	= 0x100,
295	.map_io		= ixp4xx_map_io,
296	.init_early	= ixp4xx_init_early,
297	.init_irq	= ixp4xx_init_irq,
298	.init_time	= dsmg600_timer_init,
299	.init_machine	= dsmg600_init,
300#if defined(CONFIG_PCI)
301	.dma_zone_size	= SZ_64M,
302#endif
303	.restart	= ixp4xx_restart,
304MACHINE_END
305