1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f577ffd7STony Lindgren /*
3f30c2269SUwe Zeisberger * linux/arch/arm/mach-omap1/serial.c
4f577ffd7STony Lindgren *
565d873caSMarek Vašut * OMAP1 serial support.
6f577ffd7STony Lindgren */
7df89de97SLinus Walleij #include <linux/gpio/machine.h>
8df89de97SLinus Walleij #include <linux/gpio/consumer.h>
9f577ffd7STony Lindgren #include <linux/module.h>
10f577ffd7STony Lindgren #include <linux/kernel.h>
11f577ffd7STony Lindgren #include <linux/init.h>
12d533c128SThomas Gleixner #include <linux/irq.h>
13f577ffd7STony Lindgren #include <linux/delay.h>
14f577ffd7STony Lindgren #include <linux/serial.h>
15f577ffd7STony Lindgren #include <linux/tty.h>
16f577ffd7STony Lindgren #include <linux/serial_8250.h>
17f577ffd7STony Lindgren #include <linux/serial_reg.h>
18f8ce2547SRussell King #include <linux/clk.h>
19fced80c7SRussell King #include <linux/io.h>
20f577ffd7STony Lindgren
21f577ffd7STony Lindgren #include <asm/mach-types.h>
22f577ffd7STony Lindgren
23*94c1c0a2SArnd Bergmann #include "common.h"
247036440eSArnd Bergmann #include "serial.h"
257e0a9e62SArnd Bergmann #include "mux.h"
26706afddaSAaro Koskinen #include "pm.h"
27e99b32e2STony Lindgren #include "soc.h"
28706afddaSAaro Koskinen
29120db2cbSTony Lindgren static struct clk * uart1_ck;
30120db2cbSTony Lindgren static struct clk * uart2_ck;
31120db2cbSTony Lindgren static struct clk * uart3_ck;
32f577ffd7STony Lindgren
omap_serial_in(struct plat_serial8250_port * up,int offset)33f577ffd7STony Lindgren static inline unsigned int omap_serial_in(struct plat_serial8250_port *up,
34f577ffd7STony Lindgren int offset)
35f577ffd7STony Lindgren {
36f577ffd7STony Lindgren offset <<= up->regshift;
37f577ffd7STony Lindgren return (unsigned int)__raw_readb(up->membase + offset);
38f577ffd7STony Lindgren }
39f577ffd7STony Lindgren
omap_serial_outp(struct plat_serial8250_port * p,int offset,int value)40f577ffd7STony Lindgren static inline void omap_serial_outp(struct plat_serial8250_port *p, int offset,
41f577ffd7STony Lindgren int value)
42f577ffd7STony Lindgren {
43f577ffd7STony Lindgren offset <<= p->regshift;
44f577ffd7STony Lindgren __raw_writeb(value, p->membase + offset);
45f577ffd7STony Lindgren }
46f577ffd7STony Lindgren
47f577ffd7STony Lindgren /*
48f577ffd7STony Lindgren * Internal UARTs need to be initialized for the 8250 autoconfig to work
49f577ffd7STony Lindgren * properly. Note that the TX watermark initialization may not be needed
50f577ffd7STony Lindgren * once the 8250.c watermark handling code is merged.
51f577ffd7STony Lindgren */
omap_serial_reset(struct plat_serial8250_port * p)52f577ffd7STony Lindgren static void __init omap_serial_reset(struct plat_serial8250_port *p)
53f577ffd7STony Lindgren {
54498cb951SAndrei Emeltchenko omap_serial_outp(p, UART_OMAP_MDR1,
55498cb951SAndrei Emeltchenko UART_OMAP_MDR1_DISABLE); /* disable UART */
56f577ffd7STony Lindgren omap_serial_outp(p, UART_OMAP_SCR, 0x08); /* TX watermark */
57498cb951SAndrei Emeltchenko omap_serial_outp(p, UART_OMAP_MDR1,
58498cb951SAndrei Emeltchenko UART_OMAP_MDR1_16X_MODE); /* enable UART */
59f577ffd7STony Lindgren
6065d873caSMarek Vašut if (!cpu_is_omap15xx()) {
61f577ffd7STony Lindgren omap_serial_outp(p, UART_OMAP_SYSC, 0x01);
62f577ffd7STony Lindgren while (!(omap_serial_in(p, UART_OMAP_SYSC) & 0x01));
63f577ffd7STony Lindgren }
64f577ffd7STony Lindgren }
65f577ffd7STony Lindgren
66f577ffd7STony Lindgren static struct plat_serial8250_port serial_platform_data[] = {
67f577ffd7STony Lindgren {
684f2c49feSTony Lindgren .mapbase = OMAP1_UART1_BASE,
69f577ffd7STony Lindgren .irq = INT_UART1,
70f577ffd7STony Lindgren .flags = UPF_BOOT_AUTOCONF,
71f577ffd7STony Lindgren .iotype = UPIO_MEM,
72f577ffd7STony Lindgren .regshift = 2,
73f577ffd7STony Lindgren .uartclk = OMAP16XX_BASE_BAUD * 16,
74f577ffd7STony Lindgren },
75f577ffd7STony Lindgren {
764f2c49feSTony Lindgren .mapbase = OMAP1_UART2_BASE,
77f577ffd7STony Lindgren .irq = INT_UART2,
78f577ffd7STony Lindgren .flags = UPF_BOOT_AUTOCONF,
79f577ffd7STony Lindgren .iotype = UPIO_MEM,
80f577ffd7STony Lindgren .regshift = 2,
81f577ffd7STony Lindgren .uartclk = OMAP16XX_BASE_BAUD * 16,
82f577ffd7STony Lindgren },
83f577ffd7STony Lindgren {
844f2c49feSTony Lindgren .mapbase = OMAP1_UART3_BASE,
85f577ffd7STony Lindgren .irq = INT_UART3,
86f577ffd7STony Lindgren .flags = UPF_BOOT_AUTOCONF,
87f577ffd7STony Lindgren .iotype = UPIO_MEM,
88f577ffd7STony Lindgren .regshift = 2,
89f577ffd7STony Lindgren .uartclk = OMAP16XX_BASE_BAUD * 16,
90f577ffd7STony Lindgren },
91f577ffd7STony Lindgren { },
92f577ffd7STony Lindgren };
93f577ffd7STony Lindgren
94f577ffd7STony Lindgren static struct platform_device serial_device = {
95f577ffd7STony Lindgren .name = "serial8250",
966df29debSRussell King .id = PLAT8250_DEV_PLATFORM,
97f577ffd7STony Lindgren .dev = {
98f577ffd7STony Lindgren .platform_data = serial_platform_data,
99f577ffd7STony Lindgren },
100f577ffd7STony Lindgren };
101f577ffd7STony Lindgren
102f577ffd7STony Lindgren /*
103f577ffd7STony Lindgren * Note that on Innovator-1510 UART2 pins conflict with USB2.
104f577ffd7STony Lindgren * By default UART2 does not work on Innovator-1510 if you have
105f577ffd7STony Lindgren * USB OHCI enabled. To use UART2, you must disable USB2 first.
106f577ffd7STony Lindgren */
omap_serial_init(void)1073179a019STony Lindgren void __init omap_serial_init(void)
108f577ffd7STony Lindgren {
109f577ffd7STony Lindgren int i;
110f577ffd7STony Lindgren
11165d873caSMarek Vašut if (cpu_is_omap15xx()) {
112f577ffd7STony Lindgren serial_platform_data[0].uartclk = OMAP1510_BASE_BAUD * 16;
113f577ffd7STony Lindgren serial_platform_data[1].uartclk = OMAP1510_BASE_BAUD * 16;
114f577ffd7STony Lindgren serial_platform_data[2].uartclk = OMAP1510_BASE_BAUD * 16;
115f577ffd7STony Lindgren }
116f577ffd7STony Lindgren
1179d30b99fSAlexander Shishkin for (i = 0; i < ARRAY_SIZE(serial_platform_data) - 1; i++) {
11884f90c9cSTony Lindgren /* Static mapping, never released */
11984f90c9cSTony Lindgren serial_platform_data[i].membase =
12084f90c9cSTony Lindgren ioremap(serial_platform_data[i].mapbase, SZ_2K);
12184f90c9cSTony Lindgren if (!serial_platform_data[i].membase) {
12284f90c9cSTony Lindgren printk(KERN_ERR "Could not ioremap uart%i\n", i);
12384f90c9cSTony Lindgren continue;
12484f90c9cSTony Lindgren }
125f577ffd7STony Lindgren switch (i) {
126f577ffd7STony Lindgren case 0:
127f577ffd7STony Lindgren uart1_ck = clk_get(NULL, "uart1_ck");
128f577ffd7STony Lindgren if (IS_ERR(uart1_ck))
129f577ffd7STony Lindgren printk("Could not get uart1_ck\n");
130f577ffd7STony Lindgren else {
13198e0f634SJanusz Krzysztofik clk_prepare_enable(uart1_ck);
13265d873caSMarek Vašut if (cpu_is_omap15xx())
133f577ffd7STony Lindgren clk_set_rate(uart1_ck, 12000000);
134f577ffd7STony Lindgren }
135f577ffd7STony Lindgren break;
136f577ffd7STony Lindgren case 1:
137f577ffd7STony Lindgren uart2_ck = clk_get(NULL, "uart2_ck");
138f577ffd7STony Lindgren if (IS_ERR(uart2_ck))
139f577ffd7STony Lindgren printk("Could not get uart2_ck\n");
140f577ffd7STony Lindgren else {
14198e0f634SJanusz Krzysztofik clk_prepare_enable(uart2_ck);
14265d873caSMarek Vašut if (cpu_is_omap15xx())
143f577ffd7STony Lindgren clk_set_rate(uart2_ck, 12000000);
144f577ffd7STony Lindgren else
145f577ffd7STony Lindgren clk_set_rate(uart2_ck, 48000000);
146f577ffd7STony Lindgren }
147f577ffd7STony Lindgren break;
148f577ffd7STony Lindgren case 2:
149f577ffd7STony Lindgren uart3_ck = clk_get(NULL, "uart3_ck");
150f577ffd7STony Lindgren if (IS_ERR(uart3_ck))
151f577ffd7STony Lindgren printk("Could not get uart3_ck\n");
152f577ffd7STony Lindgren else {
15398e0f634SJanusz Krzysztofik clk_prepare_enable(uart3_ck);
15465d873caSMarek Vašut if (cpu_is_omap15xx())
155f577ffd7STony Lindgren clk_set_rate(uart3_ck, 12000000);
156f577ffd7STony Lindgren }
157f577ffd7STony Lindgren break;
158f577ffd7STony Lindgren }
159f577ffd7STony Lindgren omap_serial_reset(&serial_platform_data[i]);
160f577ffd7STony Lindgren }
161f577ffd7STony Lindgren }
162f577ffd7STony Lindgren
1637c38cf02STony Lindgren #ifdef CONFIG_OMAP_SERIAL_WAKE
1647c38cf02STony Lindgren
omap_serial_wake_interrupt(int irq,void * dev_id)1650cd61b68SLinus Torvalds static irqreturn_t omap_serial_wake_interrupt(int irq, void *dev_id)
1667c38cf02STony Lindgren {
1677c38cf02STony Lindgren /* Need to do something with serial port right after wake-up? */
1687c38cf02STony Lindgren return IRQ_HANDLED;
1697c38cf02STony Lindgren }
1707c38cf02STony Lindgren
1717c38cf02STony Lindgren /*
1727c38cf02STony Lindgren * Reroutes serial RX lines to GPIO lines for the duration of
1737c38cf02STony Lindgren * sleep to allow waking up the device from serial port even
1747c38cf02STony Lindgren * in deep sleep.
1757c38cf02STony Lindgren */
omap_serial_wake_trigger(int enable)1767c38cf02STony Lindgren void omap_serial_wake_trigger(int enable)
1777c38cf02STony Lindgren {
1787c38cf02STony Lindgren if (!cpu_is_omap16xx())
1797c38cf02STony Lindgren return;
1807c38cf02STony Lindgren
1817c38cf02STony Lindgren if (uart1_ck != NULL) {
1827c38cf02STony Lindgren if (enable)
1837c38cf02STony Lindgren omap_cfg_reg(V14_16XX_GPIO37);
1847c38cf02STony Lindgren else
1857c38cf02STony Lindgren omap_cfg_reg(V14_16XX_UART1_RX);
1867c38cf02STony Lindgren }
1877c38cf02STony Lindgren if (uart2_ck != NULL) {
1887c38cf02STony Lindgren if (enable)
1897c38cf02STony Lindgren omap_cfg_reg(R9_16XX_GPIO18);
1907c38cf02STony Lindgren else
1917c38cf02STony Lindgren omap_cfg_reg(R9_16XX_UART2_RX);
1927c38cf02STony Lindgren }
1937c38cf02STony Lindgren if (uart3_ck != NULL) {
1947c38cf02STony Lindgren if (enable)
1957c38cf02STony Lindgren omap_cfg_reg(L14_16XX_GPIO49);
1967c38cf02STony Lindgren else
1977c38cf02STony Lindgren omap_cfg_reg(L14_16XX_UART3_RX);
1987c38cf02STony Lindgren }
1997c38cf02STony Lindgren }
2007c38cf02STony Lindgren
omap_serial_set_port_wakeup(int idx)201df89de97SLinus Walleij static void __init omap_serial_set_port_wakeup(int idx)
2027c38cf02STony Lindgren {
203df89de97SLinus Walleij struct gpio_desc *d;
2047c38cf02STony Lindgren int ret;
2057c38cf02STony Lindgren
206df89de97SLinus Walleij d = gpiod_get_index(NULL, "wakeup", idx, GPIOD_IN);
207df89de97SLinus Walleij if (IS_ERR(d)) {
208df89de97SLinus Walleij pr_err("Unable to get UART wakeup GPIO descriptor\n");
2097c38cf02STony Lindgren return;
2107c38cf02STony Lindgren }
211df89de97SLinus Walleij ret = request_irq(gpiod_to_irq(d), &omap_serial_wake_interrupt,
21252e405eaSThomas Gleixner IRQF_TRIGGER_RISING, "serial wakeup", NULL);
2137c38cf02STony Lindgren if (ret) {
214df89de97SLinus Walleij gpiod_put(d);
215df89de97SLinus Walleij pr_err("No interrupt for UART%d wake GPIO\n", idx + 1);
2167c38cf02STony Lindgren return;
2177c38cf02STony Lindgren }
218df89de97SLinus Walleij enable_irq_wake(gpiod_to_irq(d));
2197c38cf02STony Lindgren }
2207c38cf02STony Lindgren
221df89de97SLinus Walleij
omap_serial_wakeup_init(void)22282c3bd03SShawn Guo int __init omap_serial_wakeup_init(void)
2237c38cf02STony Lindgren {
2247c38cf02STony Lindgren if (!cpu_is_omap16xx())
2257c38cf02STony Lindgren return 0;
2267c38cf02STony Lindgren
2277c38cf02STony Lindgren if (uart1_ck != NULL)
228df89de97SLinus Walleij omap_serial_set_port_wakeup(0);
2297c38cf02STony Lindgren if (uart2_ck != NULL)
230df89de97SLinus Walleij omap_serial_set_port_wakeup(1);
2317c38cf02STony Lindgren if (uart3_ck != NULL)
232df89de97SLinus Walleij omap_serial_set_port_wakeup(2);
2337c38cf02STony Lindgren
2347c38cf02STony Lindgren return 0;
2357c38cf02STony Lindgren }
2367c38cf02STony Lindgren
2377c38cf02STony Lindgren #endif /* CONFIG_OMAP_SERIAL_WAKE */
2387c38cf02STony Lindgren
omap_init(void)239f577ffd7STony Lindgren static int __init omap_init(void)
240f577ffd7STony Lindgren {
2417f9187c2STony Lindgren if (!cpu_class_is_omap1())
2427f9187c2STony Lindgren return -ENODEV;
2437f9187c2STony Lindgren
244f577ffd7STony Lindgren return platform_device_register(&serial_device);
245f577ffd7STony Lindgren }
246f577ffd7STony Lindgren arch_initcall(omap_init);
247