xref: /linux/arch/arm/mach-omap1/serial.c (revision 94c1c0a29bfccf94c70afef5d89734bce778add6)
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  */
72f8163baSRussell King #include <linux/gpio.h>
8f577ffd7STony Lindgren #include <linux/module.h>
9f577ffd7STony Lindgren #include <linux/kernel.h>
10f577ffd7STony Lindgren #include <linux/init.h>
11d533c128SThomas Gleixner #include <linux/irq.h>
12f577ffd7STony Lindgren #include <linux/delay.h>
13f577ffd7STony Lindgren #include <linux/serial.h>
14f577ffd7STony Lindgren #include <linux/tty.h>
15f577ffd7STony Lindgren #include <linux/serial_8250.h>
16f577ffd7STony Lindgren #include <linux/serial_reg.h>
17f8ce2547SRussell King #include <linux/clk.h>
18fced80c7SRussell King #include <linux/io.h>
19f577ffd7STony Lindgren 
20f577ffd7STony Lindgren #include <asm/mach-types.h>
21f577ffd7STony Lindgren 
22*94c1c0a2SArnd Bergmann #include "common.h"
237036440eSArnd Bergmann #include "serial.h"
247e0a9e62SArnd Bergmann #include "mux.h"
25706afddaSAaro Koskinen #include "pm.h"
26e99b32e2STony Lindgren #include "soc.h"
27706afddaSAaro Koskinen 
28120db2cbSTony Lindgren static struct clk * uart1_ck;
29120db2cbSTony Lindgren static struct clk * uart2_ck;
30120db2cbSTony Lindgren static struct clk * uart3_ck;
31f577ffd7STony Lindgren 
32f577ffd7STony Lindgren static inline unsigned int omap_serial_in(struct plat_serial8250_port *up,
33f577ffd7STony Lindgren 					  int offset)
34f577ffd7STony Lindgren {
35f577ffd7STony Lindgren 	offset <<= up->regshift;
36f577ffd7STony Lindgren 	return (unsigned int)__raw_readb(up->membase + offset);
37f577ffd7STony Lindgren }
38f577ffd7STony Lindgren 
39f577ffd7STony Lindgren static inline void omap_serial_outp(struct plat_serial8250_port *p, int offset,
40f577ffd7STony Lindgren 				    int value)
41f577ffd7STony Lindgren {
42f577ffd7STony Lindgren 	offset <<= p->regshift;
43f577ffd7STony Lindgren 	__raw_writeb(value, p->membase + offset);
44f577ffd7STony Lindgren }
45f577ffd7STony Lindgren 
46f577ffd7STony Lindgren /*
47f577ffd7STony Lindgren  * Internal UARTs need to be initialized for the 8250 autoconfig to work
48f577ffd7STony Lindgren  * properly. Note that the TX watermark initialization may not be needed
49f577ffd7STony Lindgren  * once the 8250.c watermark handling code is merged.
50f577ffd7STony Lindgren  */
51f577ffd7STony Lindgren static void __init omap_serial_reset(struct plat_serial8250_port *p)
52f577ffd7STony Lindgren {
53498cb951SAndrei Emeltchenko 	omap_serial_outp(p, UART_OMAP_MDR1,
54498cb951SAndrei Emeltchenko 			UART_OMAP_MDR1_DISABLE);	/* disable UART */
55f577ffd7STony Lindgren 	omap_serial_outp(p, UART_OMAP_SCR, 0x08);	/* TX watermark */
56498cb951SAndrei Emeltchenko 	omap_serial_outp(p, UART_OMAP_MDR1,
57498cb951SAndrei Emeltchenko 			UART_OMAP_MDR1_16X_MODE);	/* enable UART */
58f577ffd7STony Lindgren 
5965d873caSMarek Vašut 	if (!cpu_is_omap15xx()) {
60f577ffd7STony Lindgren 		omap_serial_outp(p, UART_OMAP_SYSC, 0x01);
61f577ffd7STony Lindgren 		while (!(omap_serial_in(p, UART_OMAP_SYSC) & 0x01));
62f577ffd7STony Lindgren 	}
63f577ffd7STony Lindgren }
64f577ffd7STony Lindgren 
65f577ffd7STony Lindgren static struct plat_serial8250_port serial_platform_data[] = {
66f577ffd7STony Lindgren 	{
674f2c49feSTony Lindgren 		.mapbase	= OMAP1_UART1_BASE,
68f577ffd7STony Lindgren 		.irq		= INT_UART1,
69f577ffd7STony Lindgren 		.flags		= UPF_BOOT_AUTOCONF,
70f577ffd7STony Lindgren 		.iotype		= UPIO_MEM,
71f577ffd7STony Lindgren 		.regshift	= 2,
72f577ffd7STony Lindgren 		.uartclk	= OMAP16XX_BASE_BAUD * 16,
73f577ffd7STony Lindgren 	},
74f577ffd7STony Lindgren 	{
754f2c49feSTony Lindgren 		.mapbase	= OMAP1_UART2_BASE,
76f577ffd7STony Lindgren 		.irq		= INT_UART2,
77f577ffd7STony Lindgren 		.flags		= UPF_BOOT_AUTOCONF,
78f577ffd7STony Lindgren 		.iotype		= UPIO_MEM,
79f577ffd7STony Lindgren 		.regshift	= 2,
80f577ffd7STony Lindgren 		.uartclk	= OMAP16XX_BASE_BAUD * 16,
81f577ffd7STony Lindgren 	},
82f577ffd7STony Lindgren 	{
834f2c49feSTony Lindgren 		.mapbase	= OMAP1_UART3_BASE,
84f577ffd7STony Lindgren 		.irq		= INT_UART3,
85f577ffd7STony Lindgren 		.flags		= UPF_BOOT_AUTOCONF,
86f577ffd7STony Lindgren 		.iotype		= UPIO_MEM,
87f577ffd7STony Lindgren 		.regshift	= 2,
88f577ffd7STony Lindgren 		.uartclk	= OMAP16XX_BASE_BAUD * 16,
89f577ffd7STony Lindgren 	},
90f577ffd7STony Lindgren 	{ },
91f577ffd7STony Lindgren };
92f577ffd7STony Lindgren 
93f577ffd7STony Lindgren static struct platform_device serial_device = {
94f577ffd7STony Lindgren 	.name			= "serial8250",
956df29debSRussell King 	.id			= PLAT8250_DEV_PLATFORM,
96f577ffd7STony Lindgren 	.dev			= {
97f577ffd7STony Lindgren 		.platform_data	= serial_platform_data,
98f577ffd7STony Lindgren 	},
99f577ffd7STony Lindgren };
100f577ffd7STony Lindgren 
101f577ffd7STony Lindgren /*
102f577ffd7STony Lindgren  * Note that on Innovator-1510 UART2 pins conflict with USB2.
103f577ffd7STony Lindgren  * By default UART2 does not work on Innovator-1510 if you have
104f577ffd7STony Lindgren  * USB OHCI enabled. To use UART2, you must disable USB2 first.
105f577ffd7STony Lindgren  */
1063179a019STony Lindgren void __init omap_serial_init(void)
107f577ffd7STony Lindgren {
108f577ffd7STony Lindgren 	int i;
109f577ffd7STony Lindgren 
11065d873caSMarek Vašut 	if (cpu_is_omap15xx()) {
111f577ffd7STony Lindgren 		serial_platform_data[0].uartclk = OMAP1510_BASE_BAUD * 16;
112f577ffd7STony Lindgren 		serial_platform_data[1].uartclk = OMAP1510_BASE_BAUD * 16;
113f577ffd7STony Lindgren 		serial_platform_data[2].uartclk = OMAP1510_BASE_BAUD * 16;
114f577ffd7STony Lindgren 	}
115f577ffd7STony Lindgren 
1169d30b99fSAlexander Shishkin 	for (i = 0; i < ARRAY_SIZE(serial_platform_data) - 1; i++) {
11784f90c9cSTony Lindgren 		/* Static mapping, never released */
11884f90c9cSTony Lindgren 		serial_platform_data[i].membase =
11984f90c9cSTony Lindgren 			ioremap(serial_platform_data[i].mapbase, SZ_2K);
12084f90c9cSTony Lindgren 		if (!serial_platform_data[i].membase) {
12184f90c9cSTony Lindgren 			printk(KERN_ERR "Could not ioremap uart%i\n", i);
12284f90c9cSTony Lindgren 			continue;
12384f90c9cSTony Lindgren 		}
124f577ffd7STony Lindgren 		switch (i) {
125f577ffd7STony Lindgren 		case 0:
126f577ffd7STony Lindgren 			uart1_ck = clk_get(NULL, "uart1_ck");
127f577ffd7STony Lindgren 			if (IS_ERR(uart1_ck))
128f577ffd7STony Lindgren 				printk("Could not get uart1_ck\n");
129f577ffd7STony Lindgren 			else {
13098e0f634SJanusz Krzysztofik 				clk_prepare_enable(uart1_ck);
13165d873caSMarek Vašut 				if (cpu_is_omap15xx())
132f577ffd7STony Lindgren 					clk_set_rate(uart1_ck, 12000000);
133f577ffd7STony Lindgren 			}
134f577ffd7STony Lindgren 			break;
135f577ffd7STony Lindgren 		case 1:
136f577ffd7STony Lindgren 			uart2_ck = clk_get(NULL, "uart2_ck");
137f577ffd7STony Lindgren 			if (IS_ERR(uart2_ck))
138f577ffd7STony Lindgren 				printk("Could not get uart2_ck\n");
139f577ffd7STony Lindgren 			else {
14098e0f634SJanusz Krzysztofik 				clk_prepare_enable(uart2_ck);
14165d873caSMarek Vašut 				if (cpu_is_omap15xx())
142f577ffd7STony Lindgren 					clk_set_rate(uart2_ck, 12000000);
143f577ffd7STony Lindgren 				else
144f577ffd7STony Lindgren 					clk_set_rate(uart2_ck, 48000000);
145f577ffd7STony Lindgren 			}
146f577ffd7STony Lindgren 			break;
147f577ffd7STony Lindgren 		case 2:
148f577ffd7STony Lindgren 			uart3_ck = clk_get(NULL, "uart3_ck");
149f577ffd7STony Lindgren 			if (IS_ERR(uart3_ck))
150f577ffd7STony Lindgren 				printk("Could not get uart3_ck\n");
151f577ffd7STony Lindgren 			else {
15298e0f634SJanusz Krzysztofik 				clk_prepare_enable(uart3_ck);
15365d873caSMarek Vašut 				if (cpu_is_omap15xx())
154f577ffd7STony Lindgren 					clk_set_rate(uart3_ck, 12000000);
155f577ffd7STony Lindgren 			}
156f577ffd7STony Lindgren 			break;
157f577ffd7STony Lindgren 		}
158f577ffd7STony Lindgren 		omap_serial_reset(&serial_platform_data[i]);
159f577ffd7STony Lindgren 	}
160f577ffd7STony Lindgren }
161f577ffd7STony Lindgren 
1627c38cf02STony Lindgren #ifdef CONFIG_OMAP_SERIAL_WAKE
1637c38cf02STony Lindgren 
1640cd61b68SLinus Torvalds static irqreturn_t omap_serial_wake_interrupt(int irq, void *dev_id)
1657c38cf02STony Lindgren {
1667c38cf02STony Lindgren 	/* Need to do something with serial port right after wake-up? */
1677c38cf02STony Lindgren 	return IRQ_HANDLED;
1687c38cf02STony Lindgren }
1697c38cf02STony Lindgren 
1707c38cf02STony Lindgren /*
1717c38cf02STony Lindgren  * Reroutes serial RX lines to GPIO lines for the duration of
1727c38cf02STony Lindgren  * sleep to allow waking up the device from serial port even
1737c38cf02STony Lindgren  * in deep sleep.
1747c38cf02STony Lindgren  */
1757c38cf02STony Lindgren void omap_serial_wake_trigger(int enable)
1767c38cf02STony Lindgren {
1777c38cf02STony Lindgren 	if (!cpu_is_omap16xx())
1787c38cf02STony Lindgren 		return;
1797c38cf02STony Lindgren 
1807c38cf02STony Lindgren 	if (uart1_ck != NULL) {
1817c38cf02STony Lindgren 		if (enable)
1827c38cf02STony Lindgren 			omap_cfg_reg(V14_16XX_GPIO37);
1837c38cf02STony Lindgren 		else
1847c38cf02STony Lindgren 			omap_cfg_reg(V14_16XX_UART1_RX);
1857c38cf02STony Lindgren 	}
1867c38cf02STony Lindgren 	if (uart2_ck != NULL) {
1877c38cf02STony Lindgren 		if (enable)
1887c38cf02STony Lindgren 			omap_cfg_reg(R9_16XX_GPIO18);
1897c38cf02STony Lindgren 		else
1907c38cf02STony Lindgren 			omap_cfg_reg(R9_16XX_UART2_RX);
1917c38cf02STony Lindgren 	}
1927c38cf02STony Lindgren 	if (uart3_ck != NULL) {
1937c38cf02STony Lindgren 		if (enable)
1947c38cf02STony Lindgren 			omap_cfg_reg(L14_16XX_GPIO49);
1957c38cf02STony Lindgren 		else
1967c38cf02STony Lindgren 			omap_cfg_reg(L14_16XX_UART3_RX);
1977c38cf02STony Lindgren 	}
1987c38cf02STony Lindgren }
1997c38cf02STony Lindgren 
2007c38cf02STony Lindgren static void __init omap_serial_set_port_wakeup(int gpio_nr)
2017c38cf02STony Lindgren {
2027c38cf02STony Lindgren 	int ret;
2037c38cf02STony Lindgren 
204f2d18feaSJarkko Nikula 	ret = gpio_request(gpio_nr, "UART wake");
2057c38cf02STony Lindgren 	if (ret < 0) {
2067c38cf02STony Lindgren 		printk(KERN_ERR "Could not request UART wake GPIO: %i\n",
2077c38cf02STony Lindgren 		       gpio_nr);
2087c38cf02STony Lindgren 		return;
2097c38cf02STony Lindgren 	}
21040e3925bSDavid Brownell 	gpio_direction_input(gpio_nr);
21115f74b03SDavid Brownell 	ret = request_irq(gpio_to_irq(gpio_nr), &omap_serial_wake_interrupt,
21252e405eaSThomas Gleixner 			  IRQF_TRIGGER_RISING, "serial wakeup", NULL);
2137c38cf02STony Lindgren 	if (ret) {
214f2d18feaSJarkko Nikula 		gpio_free(gpio_nr);
2157c38cf02STony Lindgren 		printk(KERN_ERR "No interrupt for UART wake GPIO: %i\n",
2167c38cf02STony Lindgren 		       gpio_nr);
2177c38cf02STony Lindgren 		return;
2187c38cf02STony Lindgren 	}
21915f74b03SDavid Brownell 	enable_irq_wake(gpio_to_irq(gpio_nr));
2207c38cf02STony Lindgren }
2217c38cf02STony Lindgren 
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)
2287c38cf02STony Lindgren 		omap_serial_set_port_wakeup(37);
2297c38cf02STony Lindgren 	if (uart2_ck != NULL)
2307c38cf02STony Lindgren 		omap_serial_set_port_wakeup(18);
2317c38cf02STony Lindgren 	if (uart3_ck != NULL)
2327c38cf02STony Lindgren 		omap_serial_set_port_wakeup(49);
2337c38cf02STony Lindgren 
2347c38cf02STony Lindgren 	return 0;
2357c38cf02STony Lindgren }
2367c38cf02STony Lindgren 
2377c38cf02STony Lindgren #endif	/* CONFIG_OMAP_SERIAL_WAKE */
2387c38cf02STony Lindgren 
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