Merge tag 'arc-5.2-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-2.6-microblaze.git] / arch / arm / mach-sa1100 / h3xxx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Support for Compaq iPAQ H3100 and H3600 handheld computers (common code)
4  *
5  * Copyright (c) 2000,1 Compaq Computer Corporation. (Author: Jamey Hicks)
6  * Copyright (c) 2009 Dmitry Artamonow <mad_soft@inbox.ru>
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/gpio/machine.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio_keys.h>
13 #include <linux/input.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/platform_data/gpio-htc-egpio.h>
17 #include <linux/platform_data/sa11x0-serial.h>
18 #include <linux/platform_device.h>
19 #include <linux/serial_core.h>
20
21 #include <asm/mach/flash.h>
22 #include <asm/mach/map.h>
23
24 #include <mach/h3xxx.h>
25 #include <mach/irqs.h>
26
27 #include "generic.h"
28
29 /*
30  * H3xxx flash support
31  */
32 static struct mtd_partition h3xxx_partitions[] = {
33         {
34                 .name           = "H3XXX boot firmware",
35                 .size           = 0x00040000,
36                 .offset         = 0,
37                 .mask_flags     = MTD_WRITEABLE,  /* force read-only */
38         }, {
39                 .name           = "H3XXX rootfs",
40                 .size           = MTDPART_SIZ_FULL,
41                 .offset         = 0x00040000,
42         }
43 };
44
45 static void h3xxx_set_vpp(int vpp)
46 {
47         gpio_set_value(H3XXX_EGPIO_VPP_ON, vpp);
48 }
49
50 static int h3xxx_flash_init(void)
51 {
52         int err = gpio_request(H3XXX_EGPIO_VPP_ON, "Flash Vpp");
53         if (err) {
54                 pr_err("%s: can't request H3XXX_EGPIO_VPP_ON\n", __func__);
55                 return err;
56         }
57
58         err = gpio_direction_output(H3XXX_EGPIO_VPP_ON, 0);
59         if (err)
60                 gpio_free(H3XXX_EGPIO_VPP_ON);
61
62         return err;
63 }
64
65 static void h3xxx_flash_exit(void)
66 {
67         gpio_free(H3XXX_EGPIO_VPP_ON);
68 }
69
70 static struct flash_platform_data h3xxx_flash_data = {
71         .map_name       = "cfi_probe",
72         .set_vpp        = h3xxx_set_vpp,
73         .init           = h3xxx_flash_init,
74         .exit           = h3xxx_flash_exit,
75         .parts          = h3xxx_partitions,
76         .nr_parts       = ARRAY_SIZE(h3xxx_partitions),
77 };
78
79 static struct resource h3xxx_flash_resource =
80         DEFINE_RES_MEM(SA1100_CS0_PHYS, SZ_32M);
81
82
83 /*
84  * H3xxx uart support
85  */
86 static struct gpio h3xxx_uart_gpio[] = {
87         { H3XXX_GPIO_COM_DCD,   GPIOF_IN,               "COM DCD" },
88         { H3XXX_GPIO_COM_CTS,   GPIOF_IN,               "COM CTS" },
89         { H3XXX_GPIO_COM_RTS,   GPIOF_OUT_INIT_LOW,     "COM RTS" },
90 };
91
92 static bool h3xxx_uart_request_gpios(void)
93 {
94         static bool h3xxx_uart_gpio_ok;
95         int rc;
96
97         if (h3xxx_uart_gpio_ok)
98                 return true;
99
100         rc = gpio_request_array(h3xxx_uart_gpio, ARRAY_SIZE(h3xxx_uart_gpio));
101         if (rc)
102                 pr_err("h3xxx_uart_request_gpios: error %d\n", rc);
103         else
104                 h3xxx_uart_gpio_ok = true;
105
106         return h3xxx_uart_gpio_ok;
107 }
108
109 static void h3xxx_uart_set_mctrl(struct uart_port *port, u_int mctrl)
110 {
111         if (port->mapbase == _Ser3UTCR0) {
112                 if (!h3xxx_uart_request_gpios())
113                         return;
114                 gpio_set_value(H3XXX_GPIO_COM_RTS, !(mctrl & TIOCM_RTS));
115         }
116 }
117
118 static u_int h3xxx_uart_get_mctrl(struct uart_port *port)
119 {
120         u_int ret = TIOCM_CD | TIOCM_CTS | TIOCM_DSR;
121
122         if (port->mapbase == _Ser3UTCR0) {
123                 if (!h3xxx_uart_request_gpios())
124                         return ret;
125                 /*
126                  * DCD and CTS bits are inverted in GPLR by RS232 transceiver
127                  */
128                 if (gpio_get_value(H3XXX_GPIO_COM_DCD))
129                         ret &= ~TIOCM_CD;
130                 if (gpio_get_value(H3XXX_GPIO_COM_CTS))
131                         ret &= ~TIOCM_CTS;
132         }
133
134         return ret;
135 }
136
137 static void h3xxx_uart_pm(struct uart_port *port, u_int state, u_int oldstate)
138 {
139         if (port->mapbase == _Ser3UTCR0) {
140                 if (!gpio_request(H3XXX_EGPIO_RS232_ON, "RS232 transceiver")) {
141                         gpio_direction_output(H3XXX_EGPIO_RS232_ON, !state);
142                         gpio_free(H3XXX_EGPIO_RS232_ON);
143                 } else {
144                         pr_err("%s: can't request H3XXX_EGPIO_RS232_ON\n",
145                                 __func__);
146                 }
147         }
148 }
149
150 /*
151  * Enable/Disable wake up events for this serial port.
152  * Obviously, we only support this on the normal COM port.
153  */
154 static int h3xxx_uart_set_wake(struct uart_port *port, u_int enable)
155 {
156         int err = -EINVAL;
157
158         if (port->mapbase == _Ser3UTCR0) {
159                 if (enable)
160                         PWER |= PWER_GPIO23 | PWER_GPIO25; /* DCD and CTS */
161                 else
162                         PWER &= ~(PWER_GPIO23 | PWER_GPIO25); /* DCD and CTS */
163                 err = 0;
164         }
165         return err;
166 }
167
168 static struct sa1100_port_fns h3xxx_port_fns __initdata = {
169         .set_mctrl      = h3xxx_uart_set_mctrl,
170         .get_mctrl      = h3xxx_uart_get_mctrl,
171         .pm             = h3xxx_uart_pm,
172         .set_wake       = h3xxx_uart_set_wake,
173 };
174
175 /*
176  * EGPIO
177  */
178
179 static struct resource egpio_resources[] = {
180         [0] = DEFINE_RES_MEM(H3600_EGPIO_PHYS, 0x4),
181 };
182
183 static struct htc_egpio_chip egpio_chips[] = {
184         [0] = {
185                 .reg_start      = 0,
186                 .gpio_base      = H3XXX_EGPIO_BASE,
187                 .num_gpios      = 16,
188                 .direction      = HTC_EGPIO_OUTPUT,
189                 .initial_values = 0x0080, /* H3XXX_EGPIO_RS232_ON */
190         },
191 };
192
193 static struct htc_egpio_platform_data egpio_info = {
194         .reg_width      = 16,
195         .bus_width      = 16,
196         .chip           = egpio_chips,
197         .num_chips      = ARRAY_SIZE(egpio_chips),
198 };
199
200 static struct platform_device h3xxx_egpio = {
201         .name           = "htc-egpio",
202         .id             = -1,
203         .resource       = egpio_resources,
204         .num_resources  = ARRAY_SIZE(egpio_resources),
205         .dev            = {
206                 .platform_data = &egpio_info,
207         },
208 };
209
210 /*
211  * GPIO keys
212  */
213
214 static struct gpio_keys_button h3xxx_button_table[] = {
215         {
216                 .code           = KEY_POWER,
217                 .gpio           = H3XXX_GPIO_PWR_BUTTON,
218                 .desc           = "Power Button",
219                 .active_low     = 1,
220                 .type           = EV_KEY,
221                 .wakeup         = 1,
222         }, {
223                 .code           = KEY_ENTER,
224                 .gpio           = H3XXX_GPIO_ACTION_BUTTON,
225                 .active_low     = 1,
226                 .desc           = "Action button",
227                 .type           = EV_KEY,
228                 .wakeup         = 0,
229         },
230 };
231
232 static struct gpio_keys_platform_data h3xxx_keys_data = {
233         .buttons  = h3xxx_button_table,
234         .nbuttons = ARRAY_SIZE(h3xxx_button_table),
235 };
236
237 static struct platform_device h3xxx_keys = {
238         .name   = "gpio-keys",
239         .id     = -1,
240         .dev    = {
241                 .platform_data = &h3xxx_keys_data,
242         },
243 };
244
245 static struct resource h3xxx_micro_resources[] = {
246         DEFINE_RES_MEM(0x80010000, SZ_4K),
247         DEFINE_RES_MEM(0x80020000, SZ_4K),
248         DEFINE_RES_IRQ(IRQ_Ser1UART),
249 };
250
251 struct platform_device h3xxx_micro_asic = {
252         .name = "ipaq-h3xxx-micro",
253         .id = -1,
254         .resource = h3xxx_micro_resources,
255         .num_resources = ARRAY_SIZE(h3xxx_micro_resources),
256 };
257
258 static struct platform_device *h3xxx_devices[] = {
259         &h3xxx_egpio,
260         &h3xxx_keys,
261         &h3xxx_micro_asic,
262 };
263
264 static struct gpiod_lookup_table h3xxx_pcmcia_gpio_table = {
265         .dev_id = "sa11x0-pcmcia",
266         .table = {
267                 GPIO_LOOKUP("gpio", H3XXX_GPIO_PCMCIA_CD0,
268                             "pcmcia0-detect", GPIO_ACTIVE_LOW),
269                 GPIO_LOOKUP("gpio", H3XXX_GPIO_PCMCIA_IRQ0,
270                             "pcmcia0-ready", GPIO_ACTIVE_HIGH),
271                 GPIO_LOOKUP("gpio", H3XXX_GPIO_PCMCIA_CD1,
272                             "pcmcia1-detect", GPIO_ACTIVE_LOW),
273                 GPIO_LOOKUP("gpio", H3XXX_GPIO_PCMCIA_IRQ1,
274                             "pcmcia1-ready", GPIO_ACTIVE_HIGH),
275                 { },
276         },
277 };
278
279 void __init h3xxx_mach_init(void)
280 {
281         gpiod_add_lookup_table(&h3xxx_pcmcia_gpio_table);
282         sa1100_register_uart_fns(&h3xxx_port_fns);
283         sa11x0_register_mtd(&h3xxx_flash_data, &h3xxx_flash_resource, 1);
284         platform_add_devices(h3xxx_devices, ARRAY_SIZE(h3xxx_devices));
285 }
286
287 static struct map_desc h3600_io_desc[] __initdata = {
288         {       /* static memory bank 2  CS#2 */
289                 .virtual        =  H3600_BANK_2_VIRT,
290                 .pfn            = __phys_to_pfn(SA1100_CS2_PHYS),
291                 .length         = 0x02800000,
292                 .type           = MT_DEVICE
293         }, {    /* static memory bank 4  CS#4 */
294                 .virtual        =  H3600_BANK_4_VIRT,
295                 .pfn            = __phys_to_pfn(SA1100_CS4_PHYS),
296                 .length         = 0x00800000,
297                 .type           = MT_DEVICE
298         }, {    /* EGPIO 0              CS#5 */
299                 .virtual        =  H3600_EGPIO_VIRT,
300                 .pfn            = __phys_to_pfn(H3600_EGPIO_PHYS),
301                 .length         = 0x01000000,
302                 .type           = MT_DEVICE
303         }
304 };
305
306 /*
307  * Common map_io initialization
308  */
309
310 void __init h3xxx_map_io(void)
311 {
312         sa1100_map_io();
313         iotable_init(h3600_io_desc, ARRAY_SIZE(h3600_io_desc));
314
315         sa1100_register_uart(0, 3); /* Common serial port */
316 //      sa1100_register_uart(1, 1); /* Microcontroller on 3100/3600 */
317
318         /* Ensure those pins are outputs and driving low  */
319         PPDR |= PPC_TXD4 | PPC_SCLK | PPC_SFRM;
320         PPSR &= ~(PPC_TXD4 | PPC_SCLK | PPC_SFRM);
321
322         /* Configure suspend conditions */
323         PGSR = 0;
324         PCFR = PCFR_OPDE;
325         PSDR = 0;
326
327         GPCR = 0x0fffffff;      /* All outputs are set low by default */
328         GPDR = 0;               /* Configure all GPIOs as input */
329 }
330