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 <mach/common.h>
28 #include <mach/board.h>
29 #include <mach/clock.h>
30 #include <mach/control.h>
34 #include "prm-regbits-34xx.h"
36 #define UART_OMAP_WER 0x17 /* Wake-up enable register */
38 #define DEFAULT_TIMEOUT (5 * HZ)
40 struct omap_uart_state
{
43 struct timer_list timer
;
55 struct plat_serial8250_port
*p
;
56 struct list_head node
;
57 struct platform_device pdev
;
59 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
62 /* Registers to be saved/restored for OFF-mode */
72 static LIST_HEAD(uart_list
);
74 static struct plat_serial8250_port serial_platform_data0
[] = {
76 .membase
= OMAP2_IO_ADDRESS(OMAP_UART1_BASE
),
77 .mapbase
= OMAP_UART1_BASE
,
79 .flags
= UPF_BOOT_AUTOCONF
,
82 .uartclk
= OMAP24XX_BASE_BAUD
* 16,
88 static struct plat_serial8250_port serial_platform_data1
[] = {
90 .membase
= OMAP2_IO_ADDRESS(OMAP_UART2_BASE
),
91 .mapbase
= OMAP_UART2_BASE
,
93 .flags
= UPF_BOOT_AUTOCONF
,
96 .uartclk
= OMAP24XX_BASE_BAUD
* 16,
102 static struct plat_serial8250_port serial_platform_data2
[] = {
104 .membase
= OMAP2_IO_ADDRESS(OMAP_UART3_BASE
),
105 .mapbase
= OMAP_UART3_BASE
,
107 .flags
= UPF_BOOT_AUTOCONF
,
110 .uartclk
= OMAP24XX_BASE_BAUD
* 16,
116 static inline unsigned int serial_read_reg(struct plat_serial8250_port
*up
,
119 offset
<<= up
->regshift
;
120 return (unsigned int)__raw_readb(up
->membase
+ offset
);
123 static inline void serial_write_reg(struct plat_serial8250_port
*p
, int offset
,
126 offset
<<= p
->regshift
;
127 __raw_writeb(value
, p
->membase
+ offset
);
131 * Internal UARTs need to be initialized for the 8250 autoconfig to work
132 * properly. Note that the TX watermark initialization may not be needed
133 * once the 8250.c watermark handling code is merged.
135 static inline void __init
omap_uart_reset(struct omap_uart_state
*uart
)
137 struct plat_serial8250_port
*p
= uart
->p
;
139 serial_write_reg(p
, UART_OMAP_MDR1
, 0x07);
140 serial_write_reg(p
, UART_OMAP_SCR
, 0x08);
141 serial_write_reg(p
, UART_OMAP_MDR1
, 0x00);
142 serial_write_reg(p
, UART_OMAP_SYSC
, (0x02 << 3) | (1 << 2) | (1 << 0));
145 #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
147 static int enable_off_mode
; /* to be removed by full off-mode patches */
149 static void omap_uart_save_context(struct omap_uart_state
*uart
)
152 struct plat_serial8250_port
*p
= uart
->p
;
154 if (!enable_off_mode
)
157 lcr
= serial_read_reg(p
, UART_LCR
);
158 serial_write_reg(p
, UART_LCR
, 0xBF);
159 uart
->dll
= serial_read_reg(p
, UART_DLL
);
160 uart
->dlh
= serial_read_reg(p
, UART_DLM
);
161 serial_write_reg(p
, UART_LCR
, lcr
);
162 uart
->ier
= serial_read_reg(p
, UART_IER
);
163 uart
->sysc
= serial_read_reg(p
, UART_OMAP_SYSC
);
164 uart
->scr
= serial_read_reg(p
, UART_OMAP_SCR
);
165 uart
->wer
= serial_read_reg(p
, UART_OMAP_WER
);
167 uart
->context_valid
= 1;
170 static void omap_uart_restore_context(struct omap_uart_state
*uart
)
173 struct plat_serial8250_port
*p
= uart
->p
;
175 if (!enable_off_mode
)
178 if (!uart
->context_valid
)
181 uart
->context_valid
= 0;
183 serial_write_reg(p
, UART_OMAP_MDR1
, 0x7);
184 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
185 efr
= serial_read_reg(p
, UART_EFR
);
186 serial_write_reg(p
, UART_EFR
, UART_EFR_ECB
);
187 serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */
188 serial_write_reg(p
, UART_IER
, 0x0);
189 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
190 serial_write_reg(p
, UART_DLL
, uart
->dll
);
191 serial_write_reg(p
, UART_DLM
, uart
->dlh
);
192 serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */
193 serial_write_reg(p
, UART_IER
, uart
->ier
);
194 serial_write_reg(p
, UART_FCR
, 0xA1);
195 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
196 serial_write_reg(p
, UART_EFR
, efr
);
197 serial_write_reg(p
, UART_LCR
, UART_LCR_WLEN8
);
198 serial_write_reg(p
, UART_OMAP_SCR
, uart
->scr
);
199 serial_write_reg(p
, UART_OMAP_WER
, uart
->wer
);
200 serial_write_reg(p
, UART_OMAP_SYSC
, uart
->sysc
);
201 serial_write_reg(p
, UART_OMAP_MDR1
, 0x00); /* UART 16x mode */
204 static inline void omap_uart_save_context(struct omap_uart_state
*uart
) {}
205 static inline void omap_uart_restore_context(struct omap_uart_state
*uart
) {}
206 #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
208 static inline void omap_uart_enable_clocks(struct omap_uart_state
*uart
)
213 clk_enable(uart
->ick
);
214 clk_enable(uart
->fck
);
216 omap_uart_restore_context(uart
);
221 static inline void omap_uart_disable_clocks(struct omap_uart_state
*uart
)
226 omap_uart_save_context(uart
);
228 clk_disable(uart
->ick
);
229 clk_disable(uart
->fck
);
232 static void omap_uart_enable_wakeup(struct omap_uart_state
*uart
)
234 /* Set wake-enable bit */
235 if (uart
->wk_en
&& uart
->wk_mask
) {
236 u32 v
= __raw_readl(uart
->wk_en
);
238 __raw_writel(v
, uart
->wk_en
);
241 /* Ensure IOPAD wake-enables are set */
242 if (cpu_is_omap34xx() && uart
->padconf
) {
243 u16 v
= omap_ctrl_readw(uart
->padconf
);
244 v
|= OMAP3_PADCONF_WAKEUPENABLE0
;
245 omap_ctrl_writew(v
, uart
->padconf
);
249 static void omap_uart_disable_wakeup(struct omap_uart_state
*uart
)
251 /* Clear wake-enable bit */
252 if (uart
->wk_en
&& uart
->wk_mask
) {
253 u32 v
= __raw_readl(uart
->wk_en
);
255 __raw_writel(v
, uart
->wk_en
);
258 /* Ensure IOPAD wake-enables are cleared */
259 if (cpu_is_omap34xx() && uart
->padconf
) {
260 u16 v
= omap_ctrl_readw(uart
->padconf
);
261 v
&= ~OMAP3_PADCONF_WAKEUPENABLE0
;
262 omap_ctrl_writew(v
, uart
->padconf
);
266 static void omap_uart_smart_idle_enable(struct omap_uart_state
*uart
,
269 struct plat_serial8250_port
*p
= uart
->p
;
272 sysc
= serial_read_reg(p
, UART_OMAP_SYSC
) & 0x7;
278 serial_write_reg(p
, UART_OMAP_SYSC
, sysc
);
281 static void omap_uart_block_sleep(struct omap_uart_state
*uart
)
283 omap_uart_enable_clocks(uart
);
285 omap_uart_smart_idle_enable(uart
, 0);
288 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
290 del_timer(&uart
->timer
);
293 static void omap_uart_allow_sleep(struct omap_uart_state
*uart
)
295 if (device_may_wakeup(&uart
->pdev
.dev
))
296 omap_uart_enable_wakeup(uart
);
298 omap_uart_disable_wakeup(uart
);
303 omap_uart_smart_idle_enable(uart
, 1);
305 del_timer(&uart
->timer
);
308 static void omap_uart_idle_timer(unsigned long data
)
310 struct omap_uart_state
*uart
= (struct omap_uart_state
*)data
;
312 omap_uart_allow_sleep(uart
);
315 void omap_uart_prepare_idle(int num
)
317 struct omap_uart_state
*uart
;
319 list_for_each_entry(uart
, &uart_list
, node
) {
320 if (num
== uart
->num
&& uart
->can_sleep
) {
321 omap_uart_disable_clocks(uart
);
327 void omap_uart_resume_idle(int num
)
329 struct omap_uart_state
*uart
;
331 list_for_each_entry(uart
, &uart_list
, node
) {
332 if (num
== uart
->num
) {
333 omap_uart_enable_clocks(uart
);
335 /* Check for IO pad wakeup */
336 if (cpu_is_omap34xx() && uart
->padconf
) {
337 u16 p
= omap_ctrl_readw(uart
->padconf
);
339 if (p
& OMAP3_PADCONF_WAKEUPEVENT0
)
340 omap_uart_block_sleep(uart
);
343 /* Check for normal UART wakeup */
344 if (__raw_readl(uart
->wk_st
) & uart
->wk_mask
)
345 omap_uart_block_sleep(uart
);
351 void omap_uart_prepare_suspend(void)
353 struct omap_uart_state
*uart
;
355 list_for_each_entry(uart
, &uart_list
, node
) {
356 omap_uart_allow_sleep(uart
);
360 int omap_uart_can_sleep(void)
362 struct omap_uart_state
*uart
;
365 list_for_each_entry(uart
, &uart_list
, node
) {
369 if (!uart
->can_sleep
) {
374 /* This UART can now safely sleep. */
375 omap_uart_allow_sleep(uart
);
382 * omap_uart_interrupt()
384 * This handler is used only to detect that *any* UART interrupt has
385 * occurred. It does _nothing_ to handle the interrupt. Rather,
386 * any UART interrupt will trigger the inactivity timer so the
387 * UART will not idle or sleep for its timeout period.
390 static irqreturn_t
omap_uart_interrupt(int irq
, void *dev_id
)
392 struct omap_uart_state
*uart
= dev_id
;
394 omap_uart_block_sleep(uart
);
399 static void omap_uart_idle_init(struct omap_uart_state
*uart
)
401 struct plat_serial8250_port
*p
= uart
->p
;
405 uart
->timeout
= DEFAULT_TIMEOUT
;
406 setup_timer(&uart
->timer
, omap_uart_idle_timer
,
407 (unsigned long) uart
);
408 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
409 omap_uart_smart_idle_enable(uart
, 0);
411 if (cpu_is_omap34xx()) {
412 u32 mod
= (uart
->num
== 2) ? OMAP3430_PER_MOD
: CORE_MOD
;
416 uart
->wk_en
= OMAP34XX_PRM_REGADDR(mod
, PM_WKEN1
);
417 uart
->wk_st
= OMAP34XX_PRM_REGADDR(mod
, PM_WKST1
);
420 wk_mask
= OMAP3430_ST_UART1_MASK
;
424 wk_mask
= OMAP3430_ST_UART2_MASK
;
428 wk_mask
= OMAP3430_ST_UART3_MASK
;
432 uart
->wk_mask
= wk_mask
;
433 uart
->padconf
= padconf
;
434 } else if (cpu_is_omap24xx()) {
437 if (cpu_is_omap2430()) {
438 uart
->wk_en
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKEN1
);
439 uart
->wk_st
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKST1
);
440 } else if (cpu_is_omap2420()) {
441 uart
->wk_en
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKEN1
);
442 uart
->wk_st
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKST1
);
446 wk_mask
= OMAP24XX_ST_UART1_MASK
;
449 wk_mask
= OMAP24XX_ST_UART2_MASK
;
452 wk_mask
= OMAP24XX_ST_UART3_MASK
;
455 uart
->wk_mask
= wk_mask
;
463 p
->flags
|= UPF_SHARE_IRQ
;
464 ret
= request_irq(p
->irq
, omap_uart_interrupt
, IRQF_SHARED
,
465 "serial idle", (void *)uart
);
469 void omap_uart_enable_irqs(int enable
)
472 struct omap_uart_state
*uart
;
474 list_for_each_entry(uart
, &uart_list
, node
) {
476 ret
= request_irq(uart
->p
->irq
, omap_uart_interrupt
,
477 IRQF_SHARED
, "serial idle", (void *)uart
);
479 free_irq(uart
->p
->irq
, (void *)uart
);
483 static ssize_t
sleep_timeout_show(struct device
*dev
,
484 struct device_attribute
*attr
,
487 struct platform_device
*pdev
= container_of(dev
,
488 struct platform_device
, dev
);
489 struct omap_uart_state
*uart
= container_of(pdev
,
490 struct omap_uart_state
, pdev
);
492 return sprintf(buf
, "%u\n", uart
->timeout
/ HZ
);
495 static ssize_t
sleep_timeout_store(struct device
*dev
,
496 struct device_attribute
*attr
,
497 const char *buf
, size_t n
)
499 struct platform_device
*pdev
= container_of(dev
,
500 struct platform_device
, dev
);
501 struct omap_uart_state
*uart
= container_of(pdev
,
502 struct omap_uart_state
, pdev
);
505 if (sscanf(buf
, "%u", &value
) != 1) {
506 printk(KERN_ERR
"sleep_timeout_store: Invalid value\n");
510 uart
->timeout
= value
* HZ
;
512 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
514 /* A zero value means disable timeout feature */
515 omap_uart_block_sleep(uart
);
520 DEVICE_ATTR(sleep_timeout
, 0644, sleep_timeout_show
, sleep_timeout_store
);
521 #define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
523 static inline void omap_uart_idle_init(struct omap_uart_state
*uart
) {}
524 #define DEV_CREATE_FILE(dev, attr)
525 #endif /* CONFIG_PM */
527 static struct omap_uart_state omap_uart
[OMAP_MAX_NR_PORTS
] = {
530 .name
= "serial8250",
531 .id
= PLAT8250_DEV_PLATFORM
,
533 .platform_data
= serial_platform_data0
,
538 .name
= "serial8250",
539 .id
= PLAT8250_DEV_PLATFORM1
,
541 .platform_data
= serial_platform_data1
,
546 .name
= "serial8250",
547 .id
= PLAT8250_DEV_PLATFORM2
,
549 .platform_data
= serial_platform_data2
,
555 void __init
omap_serial_init(void)
558 const struct omap_uart_config
*info
;
562 * Make sure the serial ports are muxed on at this point.
563 * You have to mux them off in device drivers later on
567 info
= omap_get_config(OMAP_TAG_UART
, struct omap_uart_config
);
572 for (i
= 0; i
< OMAP_MAX_NR_PORTS
; i
++) {
573 struct omap_uart_state
*uart
= &omap_uart
[i
];
574 struct platform_device
*pdev
= &uart
->pdev
;
575 struct device
*dev
= &pdev
->dev
;
576 struct plat_serial8250_port
*p
= dev
->platform_data
;
578 if (!(info
->enabled_uarts
& (1 << i
))) {
584 sprintf(name
, "uart%d_ick", i
+1);
585 uart
->ick
= clk_get(NULL
, name
);
586 if (IS_ERR(uart
->ick
)) {
587 printk(KERN_ERR
"Could not get uart%d_ick\n", i
+1);
591 sprintf(name
, "uart%d_fck", i
+1);
592 uart
->fck
= clk_get(NULL
, name
);
593 if (IS_ERR(uart
->fck
)) {
594 printk(KERN_ERR
"Could not get uart%d_fck\n", i
+1);
598 if (!uart
->ick
|| !uart
->fck
)
602 p
->private_data
= uart
;
604 list_add_tail(&uart
->node
, &uart_list
);
606 if (cpu_is_omap44xx())
609 omap_uart_enable_clocks(uart
);
610 omap_uart_reset(uart
);
611 omap_uart_idle_init(uart
);
613 if (WARN_ON(platform_device_register(pdev
)))
615 if ((cpu_is_omap34xx() && uart
->padconf
) ||
616 (uart
->wk_en
&& uart
->wk_mask
)) {
617 device_init_wakeup(dev
, true);
618 DEV_CREATE_FILE(dev
, &dev_attr_sleep_timeout
);