2 * arch/arm/mach-omap2/serial.c
4 * OMAP2 serial support.
6 * Copyright (C) 2005-2008 Nokia Corporation
7 * Author: Paul Mundt <paul.mundt@nokia.com>
9 * Major rework for PM support by Kevin Hilman
11 * Based off of arch/arm/mach-omap/omap1/serial.c
13 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/serial_8250.h>
23 #include <linux/serial_reg.h>
24 #include <linux/clk.h>
27 #include <plat/common.h>
28 #include <plat/board.h>
29 #include <plat/clock.h>
30 #include <plat/control.h>
34 #include "prm-regbits-34xx.h"
36 #define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
37 #define UART_OMAP_WER 0x17 /* Wake-up enable register */
40 * NOTE: By default the serial timeout is disabled as it causes lost characters
41 * over the serial ports. This means that the UART clocks will stay on until
42 * disabled via sysfs. This also causes that any deeper omap sleep states are
45 #define DEFAULT_TIMEOUT 0
47 struct omap_uart_state {
50 struct timer_list timer;
62 struct plat_serial8250_port *p;
63 struct list_head node;
64 struct platform_device pdev;
66 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
69 /* Registers to be saved/restored for OFF-mode */
79 static LIST_HEAD(uart_list);
81 static struct plat_serial8250_port serial_platform_data0[] = {
84 .flags = UPF_BOOT_AUTOCONF,
87 .uartclk = OMAP24XX_BASE_BAUD * 16,
93 static struct plat_serial8250_port serial_platform_data1[] = {
96 .flags = UPF_BOOT_AUTOCONF,
99 .uartclk = OMAP24XX_BASE_BAUD * 16,
105 static struct plat_serial8250_port serial_platform_data2[] = {
108 .flags = UPF_BOOT_AUTOCONF,
111 .uartclk = OMAP24XX_BASE_BAUD * 16,
117 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
118 static struct plat_serial8250_port serial_platform_data3[] = {
121 .flags = UPF_BOOT_AUTOCONF,
124 .uartclk = OMAP24XX_BASE_BAUD * 16,
130 static inline void omap2_set_globals_uart4(struct omap_globals *omap2_globals)
132 serial_platform_data3[0].mapbase = omap2_globals->uart4_phys;
135 static inline void omap2_set_globals_uart4(struct omap_globals *omap2_globals)
140 void __init omap2_set_globals_uart(struct omap_globals *omap2_globals)
142 serial_platform_data0[0].mapbase = omap2_globals->uart1_phys;
143 serial_platform_data1[0].mapbase = omap2_globals->uart2_phys;
144 serial_platform_data2[0].mapbase = omap2_globals->uart3_phys;
145 if (cpu_is_omap3630() || cpu_is_omap44xx())
146 omap2_set_globals_uart4(omap2_globals);
149 static inline unsigned int __serial_read_reg(struct uart_port *up,
152 offset <<= up->regshift;
153 return (unsigned int)__raw_readb(up->membase + offset);
156 static inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
159 offset <<= up->regshift;
160 return (unsigned int)__raw_readb(up->membase + offset);
163 static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
166 offset <<= p->regshift;
167 __raw_writeb(value, p->membase + offset);
171 * Internal UARTs need to be initialized for the 8250 autoconfig to work
172 * properly. Note that the TX watermark initialization may not be needed
173 * once the 8250.c watermark handling code is merged.
175 static inline void __init omap_uart_reset(struct omap_uart_state *uart)
177 struct plat_serial8250_port *p = uart->p;
179 serial_write_reg(p, UART_OMAP_MDR1, 0x07);
180 serial_write_reg(p, UART_OMAP_SCR, 0x08);
181 serial_write_reg(p, UART_OMAP_MDR1, 0x00);
182 serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
185 #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
187 static void omap_uart_save_context(struct omap_uart_state *uart)
190 struct plat_serial8250_port *p = uart->p;
192 if (!enable_off_mode)
195 lcr = serial_read_reg(p, UART_LCR);
196 serial_write_reg(p, UART_LCR, 0xBF);
197 uart->dll = serial_read_reg(p, UART_DLL);
198 uart->dlh = serial_read_reg(p, UART_DLM);
199 serial_write_reg(p, UART_LCR, lcr);
200 uart->ier = serial_read_reg(p, UART_IER);
201 uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
202 uart->scr = serial_read_reg(p, UART_OMAP_SCR);
203 uart->wer = serial_read_reg(p, UART_OMAP_WER);
205 uart->context_valid = 1;
208 static void omap_uart_restore_context(struct omap_uart_state *uart)
211 struct plat_serial8250_port *p = uart->p;
213 if (!enable_off_mode)
216 if (!uart->context_valid)
219 uart->context_valid = 0;
221 serial_write_reg(p, UART_OMAP_MDR1, 0x7);
222 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
223 efr = serial_read_reg(p, UART_EFR);
224 serial_write_reg(p, UART_EFR, UART_EFR_ECB);
225 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
226 serial_write_reg(p, UART_IER, 0x0);
227 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
228 serial_write_reg(p, UART_DLL, uart->dll);
229 serial_write_reg(p, UART_DLM, uart->dlh);
230 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
231 serial_write_reg(p, UART_IER, uart->ier);
232 serial_write_reg(p, UART_FCR, 0xA1);
233 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
234 serial_write_reg(p, UART_EFR, efr);
235 serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
236 serial_write_reg(p, UART_OMAP_SCR, uart->scr);
237 serial_write_reg(p, UART_OMAP_WER, uart->wer);
238 serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
239 serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
242 static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
243 static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
244 #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
246 static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
251 clk_enable(uart->ick);
252 clk_enable(uart->fck);
254 omap_uart_restore_context(uart);
259 static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
264 omap_uart_save_context(uart);
266 clk_disable(uart->ick);
267 clk_disable(uart->fck);
270 static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
272 /* Set wake-enable bit */
273 if (uart->wk_en && uart->wk_mask) {
274 u32 v = __raw_readl(uart->wk_en);
276 __raw_writel(v, uart->wk_en);
279 /* Ensure IOPAD wake-enables are set */
280 if (cpu_is_omap34xx() && uart->padconf) {
281 u16 v = omap_ctrl_readw(uart->padconf);
282 v |= OMAP3_PADCONF_WAKEUPENABLE0;
283 omap_ctrl_writew(v, uart->padconf);
287 static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
289 /* Clear wake-enable bit */
290 if (uart->wk_en && uart->wk_mask) {
291 u32 v = __raw_readl(uart->wk_en);
293 __raw_writel(v, uart->wk_en);
296 /* Ensure IOPAD wake-enables are cleared */
297 if (cpu_is_omap34xx() && uart->padconf) {
298 u16 v = omap_ctrl_readw(uart->padconf);
299 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
300 omap_ctrl_writew(v, uart->padconf);
304 static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
307 struct plat_serial8250_port *p = uart->p;
310 sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
316 serial_write_reg(p, UART_OMAP_SYSC, sysc);
319 static void omap_uart_block_sleep(struct omap_uart_state *uart)
321 omap_uart_enable_clocks(uart);
323 omap_uart_smart_idle_enable(uart, 0);
326 mod_timer(&uart->timer, jiffies + uart->timeout);
328 del_timer(&uart->timer);
331 static void omap_uart_allow_sleep(struct omap_uart_state *uart)
333 if (device_may_wakeup(&uart->pdev.dev))
334 omap_uart_enable_wakeup(uart);
336 omap_uart_disable_wakeup(uart);
341 omap_uart_smart_idle_enable(uart, 1);
343 del_timer(&uart->timer);
346 static void omap_uart_idle_timer(unsigned long data)
348 struct omap_uart_state *uart = (struct omap_uart_state *)data;
350 omap_uart_allow_sleep(uart);
353 void omap_uart_prepare_idle(int num)
355 struct omap_uart_state *uart;
357 list_for_each_entry(uart, &uart_list, node) {
358 if (num == uart->num && uart->can_sleep) {
359 omap_uart_disable_clocks(uart);
365 void omap_uart_resume_idle(int num)
367 struct omap_uart_state *uart;
369 list_for_each_entry(uart, &uart_list, node) {
370 if (num == uart->num) {
371 omap_uart_enable_clocks(uart);
373 /* Check for IO pad wakeup */
374 if (cpu_is_omap34xx() && uart->padconf) {
375 u16 p = omap_ctrl_readw(uart->padconf);
377 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
378 omap_uart_block_sleep(uart);
381 /* Check for normal UART wakeup */
382 if (__raw_readl(uart->wk_st) & uart->wk_mask)
383 omap_uart_block_sleep(uart);
389 void omap_uart_prepare_suspend(void)
391 struct omap_uart_state *uart;
393 list_for_each_entry(uart, &uart_list, node) {
394 omap_uart_allow_sleep(uart);
398 int omap_uart_can_sleep(void)
400 struct omap_uart_state *uart;
403 list_for_each_entry(uart, &uart_list, node) {
407 if (!uart->can_sleep) {
412 /* This UART can now safely sleep. */
413 omap_uart_allow_sleep(uart);
420 * omap_uart_interrupt()
422 * This handler is used only to detect that *any* UART interrupt has
423 * occurred. It does _nothing_ to handle the interrupt. Rather,
424 * any UART interrupt will trigger the inactivity timer so the
425 * UART will not idle or sleep for its timeout period.
428 static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
430 struct omap_uart_state *uart = dev_id;
432 omap_uart_block_sleep(uart);
437 static void omap_uart_idle_init(struct omap_uart_state *uart)
439 struct plat_serial8250_port *p = uart->p;
443 uart->timeout = DEFAULT_TIMEOUT;
444 setup_timer(&uart->timer, omap_uart_idle_timer,
445 (unsigned long) uart);
447 mod_timer(&uart->timer, jiffies + uart->timeout);
448 omap_uart_smart_idle_enable(uart, 0);
450 if (cpu_is_omap34xx()) {
451 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
455 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
456 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
459 wk_mask = OMAP3430_ST_UART1_MASK;
463 wk_mask = OMAP3430_ST_UART2_MASK;
467 wk_mask = OMAP3430_ST_UART3_MASK;
471 uart->wk_mask = wk_mask;
472 uart->padconf = padconf;
473 } else if (cpu_is_omap24xx()) {
476 if (cpu_is_omap2430()) {
477 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
478 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
479 } else if (cpu_is_omap2420()) {
480 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
481 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
485 wk_mask = OMAP24XX_ST_UART1_MASK;
488 wk_mask = OMAP24XX_ST_UART2_MASK;
491 wk_mask = OMAP24XX_ST_UART3_MASK;
494 uart->wk_mask = wk_mask;
502 p->irqflags |= IRQF_SHARED;
503 ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
504 "serial idle", (void *)uart);
508 void omap_uart_enable_irqs(int enable)
511 struct omap_uart_state *uart;
513 list_for_each_entry(uart, &uart_list, node) {
515 ret = request_irq(uart->p->irq, omap_uart_interrupt,
516 IRQF_SHARED, "serial idle", (void *)uart);
518 free_irq(uart->p->irq, (void *)uart);
522 static ssize_t sleep_timeout_show(struct device *dev,
523 struct device_attribute *attr,
526 struct platform_device *pdev = container_of(dev,
527 struct platform_device, dev);
528 struct omap_uart_state *uart = container_of(pdev,
529 struct omap_uart_state, pdev);
531 return sprintf(buf, "%u\n", uart->timeout / HZ);
534 static ssize_t sleep_timeout_store(struct device *dev,
535 struct device_attribute *attr,
536 const char *buf, size_t n)
538 struct platform_device *pdev = container_of(dev,
539 struct platform_device, dev);
540 struct omap_uart_state *uart = container_of(pdev,
541 struct omap_uart_state, pdev);
544 if (sscanf(buf, "%u", &value) != 1) {
545 printk(KERN_ERR "sleep_timeout_store: Invalid value\n");
549 uart->timeout = value * HZ;
551 mod_timer(&uart->timer, jiffies + uart->timeout);
553 /* A zero value means disable timeout feature */
554 omap_uart_block_sleep(uart);
559 DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
560 #define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
562 static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
563 #define DEV_CREATE_FILE(dev, attr)
564 #endif /* CONFIG_PM */
566 static struct omap_uart_state omap_uart[] = {
569 .name = "serial8250",
570 .id = PLAT8250_DEV_PLATFORM,
572 .platform_data = serial_platform_data0,
577 .name = "serial8250",
578 .id = PLAT8250_DEV_PLATFORM1,
580 .platform_data = serial_platform_data1,
585 .name = "serial8250",
586 .id = PLAT8250_DEV_PLATFORM2,
588 .platform_data = serial_platform_data2,
592 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
595 .name = "serial8250",
598 .platform_data = serial_platform_data3,
606 * Override the default 8250 read handler: mem_serial_in()
607 * Empty RX fifo read causes an abort on omap3630 and omap4
608 * This function makes sure that an empty rx fifo is not read on these silicons
609 * (OMAP1/2/3430 are not affected)
611 static unsigned int serial_in_override(struct uart_port *up, int offset)
613 if (UART_RX == offset) {
615 lsr = __serial_read_reg(up, UART_LSR);
616 if (!(lsr & UART_LSR_DR))
620 return __serial_read_reg(up, offset);
623 void __init omap_serial_early_init(void)
629 * Make sure the serial ports are muxed on at this point.
630 * You have to mux them off in device drivers later on
634 for (i = 0; i < ARRAY_SIZE(omap_uart); i++) {
635 struct omap_uart_state *uart = &omap_uart[i];
636 struct platform_device *pdev = &uart->pdev;
637 struct device *dev = &pdev->dev;
638 struct plat_serial8250_port *p = dev->platform_data;
641 * Module 4KB + L4 interconnect 4KB
642 * Static mapping, never released
644 p->membase = ioremap(p->mapbase, SZ_8K);
646 printk(KERN_ERR "ioremap failed for uart%i\n", i + 1);
650 sprintf(name, "uart%d_ick", i+1);
651 uart->ick = clk_get(NULL, name);
652 if (IS_ERR(uart->ick)) {
653 printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
657 sprintf(name, "uart%d_fck", i+1);
658 uart->fck = clk_get(NULL, name);
659 if (IS_ERR(uart->fck)) {
660 printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
664 /* FIXME: Remove this once the clkdev is ready */
665 if (!cpu_is_omap44xx()) {
666 if (!uart->ick || !uart->fck)
671 p->private_data = uart;
674 if (cpu_is_omap44xx())
680 * omap_serial_init_port() - initialize single serial port
681 * @port: serial port number (0-3)
683 * This function initialies serial driver for given @port only.
684 * Platforms can call this function instead of omap_serial_init()
685 * if they don't plan to use all available UARTs as serial ports.
687 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
688 * use only one of the two.
690 void __init omap_serial_init_port(int port)
692 struct omap_uart_state *uart;
693 struct platform_device *pdev;
697 BUG_ON(port >= ARRAY_SIZE(omap_uart));
699 uart = &omap_uart[port];
703 omap_uart_enable_clocks(uart);
705 omap_uart_reset(uart);
706 omap_uart_idle_init(uart);
708 list_add_tail(&uart->node, &uart_list);
710 if (WARN_ON(platform_device_register(pdev)))
713 if ((cpu_is_omap34xx() && uart->padconf) ||
714 (uart->wk_en && uart->wk_mask)) {
715 device_init_wakeup(dev, true);
716 DEV_CREATE_FILE(dev, &dev_attr_sleep_timeout);
719 /* omap44xx: Never read empty UART fifo
720 * omap3xxx: Never read empty UART fifo on UARTs
723 if (cpu_is_omap44xx())
724 uart->p->serial_in = serial_in_override;
725 else if ((serial_read_reg(uart->p, UART_OMAP_MVER) & 0xFF)
726 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
727 uart->p->serial_in = serial_in_override;
731 * omap_serial_init() - intialize all supported serial ports
733 * Initializes all available UARTs as serial ports. Platforms
734 * can call this function when they want to have default behaviour
735 * for serial ports (e.g initialize them all as serial ports).
737 void __init omap_serial_init(void)
741 if (!(cpu_is_omap3630() || cpu_is_omap4430()))
744 nr_ports = ARRAY_SIZE(omap_uart);
746 for (i = 0; i < nr_ports; i++)
747 omap_serial_init_port(i);