1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/arm/mach-ixp4xx/nas100d-setup.c
4  *
5  * NAS 100d board-setup
6  *
7  * Copyright (C) 2008 Rod Whitby <rod@whitby.id.au>
8  *
9  * based on ixdp425-setup.c:
10  *      Copyright (C) 2003-2004 MontaVista Software, Inc.
11  * based on nas100d-power.c:
12  *	Copyright (C) 2005 Tower Technologies
13  * based on nas100d-io.c
14  *	Copyright (C) 2004 Karen Spearel
15  *
16  * Author: Alessandro Zummo <a.zummo@towertech.it>
17  * Author: Rod Whitby <rod@whitby.id.au>
18  * Maintainers: http://www.nslu2-linux.org/
19  *
20  */
21 #include <linux/gpio.h>
22 #include <linux/if_ether.h>
23 #include <linux/irq.h>
24 #include <linux/jiffies.h>
25 #include <linux/timer.h>
26 #include <linux/serial.h>
27 #include <linux/serial_8250.h>
28 #include <linux/leds.h>
29 #include <linux/reboot.h>
30 #include <linux/i2c.h>
31 #include <linux/gpio/machine.h>
32 #include <linux/io.h>
33 #include <asm/mach-types.h>
34 #include <asm/mach/arch.h>
35 #include <asm/mach/flash.h>
36 
37 #define NAS100D_SDA_PIN		5
38 #define NAS100D_SCL_PIN		6
39 
40 /* Buttons */
41 #define NAS100D_PB_GPIO         14   /* power button */
42 #define NAS100D_RB_GPIO         4    /* reset button */
43 
44 /* Power control */
45 #define NAS100D_PO_GPIO         12   /* power off */
46 
47 /* LEDs */
48 #define NAS100D_LED_WLAN_GPIO	0
49 #define NAS100D_LED_DISK_GPIO	3
50 #define NAS100D_LED_PWR_GPIO	15
51 
52 static struct flash_platform_data nas100d_flash_data = {
53 	.map_name		= "cfi_probe",
54 	.width			= 2,
55 };
56 
57 static struct resource nas100d_flash_resource = {
58 	.flags			= IORESOURCE_MEM,
59 };
60 
61 static struct platform_device nas100d_flash = {
62 	.name			= "IXP4XX-Flash",
63 	.id			= 0,
64 	.dev.platform_data	= &nas100d_flash_data,
65 	.num_resources		= 1,
66 	.resource		= &nas100d_flash_resource,
67 };
68 
69 static struct i2c_board_info __initdata nas100d_i2c_board_info [] = {
70 	{
71 		I2C_BOARD_INFO("pcf8563", 0x51),
72 	},
73 };
74 
75 static struct gpio_led nas100d_led_pins[] = {
76 	{
77 		.name		= "nas100d:green:wlan",
78 		.gpio		= NAS100D_LED_WLAN_GPIO,
79 		.active_low	= true,
80 	},
81 	{
82 		.name		= "nas100d:blue:power",  /* (off=flashing) */
83 		.gpio		= NAS100D_LED_PWR_GPIO,
84 		.active_low	= true,
85 	},
86 	{
87 		.name		= "nas100d:yellow:disk",
88 		.gpio		= NAS100D_LED_DISK_GPIO,
89 		.active_low	= true,
90 	},
91 };
92 
93 static struct gpio_led_platform_data nas100d_led_data = {
94 	.num_leds		= ARRAY_SIZE(nas100d_led_pins),
95 	.leds			= nas100d_led_pins,
96 };
97 
98 static struct platform_device nas100d_leds = {
99 	.name			= "leds-gpio",
100 	.id			= -1,
101 	.dev.platform_data	= &nas100d_led_data,
102 };
103 
104 static struct gpiod_lookup_table nas100d_i2c_gpiod_table = {
105 	.dev_id		= "i2c-gpio.0",
106 	.table		= {
107 		GPIO_LOOKUP_IDX("IXP4XX_GPIO_CHIP", NAS100D_SDA_PIN,
108 				NULL, 0, GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
109 		GPIO_LOOKUP_IDX("IXP4XX_GPIO_CHIP", NAS100D_SCL_PIN,
110 				NULL, 1, GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
111 	},
112 };
113 
114 static struct platform_device nas100d_i2c_gpio = {
115 	.name			= "i2c-gpio",
116 	.id			= 0,
117 	.dev	 = {
118 		.platform_data	= NULL,
119 	},
120 };
121 
122 static struct resource nas100d_uart_resources[] = {
123 	{
124 		.start		= IXP4XX_UART1_BASE_PHYS,
125 		.end		= IXP4XX_UART1_BASE_PHYS + 0x0fff,
126 		.flags		= IORESOURCE_MEM,
127 	},
128 	{
129 		.start		= IXP4XX_UART2_BASE_PHYS,
130 		.end		= IXP4XX_UART2_BASE_PHYS + 0x0fff,
131 		.flags		= IORESOURCE_MEM,
132 	}
133 };
134 
135 static struct plat_serial8250_port nas100d_uart_data[] = {
136 	{
137 		.mapbase	= IXP4XX_UART1_BASE_PHYS,
138 		.membase	= (char *)IXP4XX_UART1_BASE_VIRT + REG_OFFSET,
139 		.irq		= IRQ_IXP4XX_UART1,
140 		.flags		= UPF_BOOT_AUTOCONF,
141 		.iotype		= UPIO_MEM,
142 		.regshift	= 2,
143 		.uartclk	= IXP4XX_UART_XTAL,
144 	},
145 	{
146 		.mapbase	= IXP4XX_UART2_BASE_PHYS,
147 		.membase	= (char *)IXP4XX_UART2_BASE_VIRT + REG_OFFSET,
148 		.irq		= IRQ_IXP4XX_UART2,
149 		.flags		= UPF_BOOT_AUTOCONF,
150 		.iotype		= UPIO_MEM,
151 		.regshift	= 2,
152 		.uartclk	= IXP4XX_UART_XTAL,
153 	},
154 	{ }
155 };
156 
157 static struct platform_device nas100d_uart = {
158 	.name			= "serial8250",
159 	.id			= PLAT8250_DEV_PLATFORM,
160 	.dev.platform_data	= nas100d_uart_data,
161 	.num_resources		= 2,
162 	.resource		= nas100d_uart_resources,
163 };
164 
165 /* Built-in 10/100 Ethernet MAC interfaces */
166 static struct eth_plat_info nas100d_plat_eth[] = {
167 	{
168 		.phy		= 0,
169 		.rxq		= 3,
170 		.txreadyq	= 20,
171 	}
172 };
173 
174 static struct platform_device nas100d_eth[] = {
175 	{
176 		.name			= "ixp4xx_eth",
177 		.id			= IXP4XX_ETH_NPEB,
178 		.dev.platform_data	= nas100d_plat_eth,
179 	}
180 };
181 
182 static struct platform_device *nas100d_devices[] __initdata = {
183 	&nas100d_i2c_gpio,
184 	&nas100d_flash,
185 	&nas100d_leds,
186 	&nas100d_eth[0],
187 };
188 
nas100d_power_off(void)189 static void nas100d_power_off(void)
190 {
191 	/* This causes the box to drop the power and go dead. */
192 
193 	/* enable the pwr cntl gpio and assert power off */
194 	gpio_direction_output(NAS100D_PO_GPIO, 1);
195 }
196 
197 /* This is used to make sure the power-button pusher is serious.  The button
198  * must be held until the value of this counter reaches zero.
199  */
200 static int power_button_countdown;
201 
202 /* Must hold the button down for at least this many counts to be processed */
203 #define PBUTTON_HOLDDOWN_COUNT 4 /* 2 secs */
204 
205 static void nas100d_power_handler(struct timer_list *unused);
206 static DEFINE_TIMER(nas100d_power_timer, nas100d_power_handler);
207 
nas100d_power_handler(struct timer_list * unused)208 static void nas100d_power_handler(struct timer_list *unused)
209 {
210 	/* This routine is called twice per second to check the
211 	 * state of the power button.
212 	 */
213 
214 	if (gpio_get_value(NAS100D_PB_GPIO)) {
215 
216 		/* IO Pin is 1 (button pushed) */
217 		if (power_button_countdown > 0)
218 			power_button_countdown--;
219 
220 	} else {
221 
222 		/* Done on button release, to allow for auto-power-on mods. */
223 		if (power_button_countdown == 0) {
224 			/* Signal init to do the ctrlaltdel action,
225 			 * this will bypass init if it hasn't started
226 			 * and do a kernel_restart.
227 			 */
228 			ctrl_alt_del();
229 
230 			/* Change the state of the power LED to "blink" */
231 			gpio_set_value(NAS100D_LED_PWR_GPIO, 0);
232 		} else {
233 			power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
234 		}
235 	}
236 
237 	mod_timer(&nas100d_power_timer, jiffies + msecs_to_jiffies(500));
238 }
239 
nas100d_reset_handler(int irq,void * dev_id)240 static irqreturn_t nas100d_reset_handler(int irq, void *dev_id)
241 {
242 	/* This is the paper-clip reset, it shuts the machine down directly. */
243 	machine_power_off();
244 
245 	return IRQ_HANDLED;
246 }
247 
nas100d_gpio_init(void)248 static int __init nas100d_gpio_init(void)
249 {
250 	if (!machine_is_nas100d())
251 		return 0;
252 
253 	/*
254 	 * The power button on the Iomega NAS100d is on GPIO 14, but
255 	 * it cannot handle interrupts on that GPIO line.  So we'll
256 	 * have to poll it with a kernel timer.
257 	 */
258 
259 	/* Request the power off GPIO */
260 	gpio_request(NAS100D_PO_GPIO, "power off");
261 
262 	/* Make sure that the power button GPIO is set up as an input */
263 	gpio_request(NAS100D_PB_GPIO, "power button");
264 	gpio_direction_input(NAS100D_PB_GPIO);
265 
266 	/* Set the initial value for the power button IRQ handler */
267 	power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
268 
269 	mod_timer(&nas100d_power_timer, jiffies + msecs_to_jiffies(500));
270 
271 	return 0;
272 }
273 device_initcall(nas100d_gpio_init);
274 
nas100d_init(void)275 static void __init nas100d_init(void)
276 {
277 	uint8_t __iomem *f;
278 	int i;
279 
280 	ixp4xx_sys_init();
281 
282 	/* gpio 14 and 15 are _not_ clocks */
283 	*IXP4XX_GPIO_GPCLKR = 0;
284 
285 	nas100d_flash_resource.start = IXP4XX_EXP_BUS_BASE(0);
286 	nas100d_flash_resource.end =
287 		IXP4XX_EXP_BUS_BASE(0) + ixp4xx_exp_bus_size - 1;
288 
289 	gpiod_add_lookup_table(&nas100d_i2c_gpiod_table);
290 	i2c_register_board_info(0, nas100d_i2c_board_info,
291 				ARRAY_SIZE(nas100d_i2c_board_info));
292 
293 	/*
294 	 * This is only useful on a modified machine, but it is valuable
295 	 * to have it first in order to see debug messages, and so that
296 	 * it does *not* get removed if platform_add_devices fails!
297 	 */
298 	(void)platform_device_register(&nas100d_uart);
299 
300 	platform_add_devices(nas100d_devices, ARRAY_SIZE(nas100d_devices));
301 
302 	pm_power_off = nas100d_power_off;
303 
304 	if (request_irq(gpio_to_irq(NAS100D_RB_GPIO), &nas100d_reset_handler,
305 		IRQF_TRIGGER_LOW, "NAS100D reset button", NULL) < 0) {
306 
307 		printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
308 			gpio_to_irq(NAS100D_RB_GPIO));
309 	}
310 
311 	/*
312 	 * Map in a portion of the flash and read the MAC address.
313 	 * Since it is stored in BE in the flash itself, we need to
314 	 * byteswap it if we're in LE mode.
315 	 */
316 	f = ioremap(IXP4XX_EXP_BUS_BASE(0), 0x1000000);
317 	if (f) {
318 		for (i = 0; i < 6; i++)
319 #ifdef __ARMEB__
320 			nas100d_plat_eth[0].hwaddr[i] = readb(f + 0xFC0FD8 + i);
321 #else
322 			nas100d_plat_eth[0].hwaddr[i] = readb(f + 0xFC0FD8 + (i^3));
323 #endif
324 		iounmap(f);
325 	}
326 	printk(KERN_INFO "NAS100D: Using MAC address %pM for port 0\n",
327 	       nas100d_plat_eth[0].hwaddr);
328 
329 }
330 
331 MACHINE_START(NAS100D, "Iomega NAS 100d")
332 	/* Maintainer: www.nslu2-linux.org */
333 	.atag_offset	= 0x100,
334 	.map_io		= ixp4xx_map_io,
335 	.init_early	= ixp4xx_init_early,
336 	.init_irq	= ixp4xx_init_irq,
337 	.init_time	= ixp4xx_timer_init,
338 	.init_machine	= nas100d_init,
339 #if defined(CONFIG_PCI)
340 	.dma_zone_size	= SZ_64M,
341 #endif
342 	.restart	= ixp4xx_restart,
343 MACHINE_END
344