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