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 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/serial_reg.h>
20 #include <linux/clk.h>
21 #ifdef CONFIG_SERIAL_OMAP
22 #include <linux/platform_device.h>
25 #include <linux/delay.h>
26 #include <linux/debugfs.h>
28 #include <plat/common.h>
29 #include <plat/board.h>
30 #include <plat/clock.h>
31 #include <plat/control.h>
32 #include <mach/gpio.h>
33 #include <plat/omap-serial.h>
35 #include <asm/mach/serial_omap.h>
39 #include "prm-regbits-34xx.h"
41 #define OMAP_CTRL_REGADDR(reg) (OMAP2_L4_IO_ADDRESS(OMAP343X_CTRL_BASE) + (reg))
43 static int (*my_pwrdm_clkdm_state_switch
)(struct clockdomain
*) = (int (*)(struct clockdomain
*))0xc0041370;
46 void omap_ctrl_writew(u16 val
, u16 offset
)
48 __raw_writew(val
, OMAP_CTRL_REGADDR(offset
));
51 u16
omap_ctrl_readw(u16 offset
)
53 return __raw_readw(OMAP_CTRL_REGADDR(offset
));
56 #define DEFAULT_TIMEOUT HZ
58 struct omap_uart_state
{
61 struct timer_list timer
;
77 struct plat_serialomap_port
*p
;
78 struct list_head node
;
80 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
83 /* Registers to be saved/restored for OFF-mode */
93 static struct omap_uart_state omap_uart
[OMAP_MAX_NR_PORTS
];
94 static LIST_HEAD(uart_list
);
95 static unsigned int fifo_idleblks
= 0;
96 static int uart0_padconf
= 0x180;
98 static struct plat_serialomap_port serial_platform_data
[] = {
100 .membase
= OMAP2_L4_IO_ADDRESS(OMAP_UART1_BASE
),
103 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL
104 .ctsrts
= UART_EFR_CTS
| UART_EFR_RTS
,
106 .flags
= UPF_BOOT_AUTOCONF
,
109 .membase
= OMAP2_L4_IO_ADDRESS(OMAP_UART2_BASE
),
112 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL
113 .ctsrts
= UART_EFR_CTS
| UART_EFR_RTS
,
115 .flags
= UPF_BOOT_AUTOCONF
,
118 .membase
= OMAP2_L4_IO_ADDRESS(OMAP_UART3_BASE
),
121 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL
122 .ctsrts
= UART_EFR_RTS
,
124 .flags
= UPF_BOOT_AUTOCONF
,
129 static struct resource omap2_uart1_resources
[] = {
131 .start
= OMAP_UART1_BASE
,
132 .end
= OMAP_UART1_BASE
+ 0x3ff,
133 .flags
= IORESOURCE_MEM
,
136 .flags
= IORESOURCE_IRQ
,
140 static struct resource omap2_uart2_resources
[] = {
142 .start
= OMAP_UART2_BASE
,
143 .end
= OMAP_UART2_BASE
+ 0x3ff,
144 .flags
= IORESOURCE_MEM
,
147 .flags
= IORESOURCE_IRQ
,
152 static struct resource omap2_uart3_resources
[] = {
154 .start
= OMAP_UART3_BASE
,
155 .end
= OMAP_UART3_BASE
+ 0x3ff,
156 .flags
= IORESOURCE_MEM
,
159 .flags
= IORESOURCE_IRQ
,
163 #ifdef CONFIG_MACH_OMAP_ZOOM2
164 static struct resource omap2_quaduart_resources
[] = {
167 .end
= 0x10000000 + (0x16 << 1),
168 .flags
= IORESOURCE_MEM
,
170 .start
= OMAP_GPIO_IRQ(102),
171 .flags
= IORESOURCE_IRQ
,
177 /* OMAP UART platform structure */
178 static struct platform_device uart1_device
= {
181 .num_resources
= ARRAY_SIZE(omap2_uart1_resources
),
182 .resource
= omap2_uart1_resources
,
183 .dev
.platform_data
= &serial_platform_data
[0],
185 static struct platform_device uart2_device
= {
188 .num_resources
= ARRAY_SIZE(omap2_uart2_resources
),
189 .resource
= omap2_uart2_resources
,
190 .dev
.platform_data
= &serial_platform_data
[1],
193 static struct platform_device uart3_device
= {
196 .num_resources
= ARRAY_SIZE(omap2_uart3_resources
),
197 .resource
= omap2_uart3_resources
,
198 .dev
.platform_data
= &serial_platform_data
[2],
201 static struct platform_device
*uart_devices
[] = {
205 static inline unsigned int serial_read_reg(struct plat_serialomap_port
*up
,
208 offset
<<= up
->regshift
;
209 return (unsigned int)__raw_readb(up
->membase
+ offset
);
212 static inline void serial_write_reg(struct plat_serialomap_port
*p
, int offset
,
215 offset
<<= p
->regshift
;
216 __raw_writeb(value
, p
->membase
+ offset
);
220 * Internal UARTs need to be initialized for the 8250 autoconfig to work
221 * properly. Note that the TX watermark initialization may not be needed
222 * once the 8250.c watermark handling code is merged.
224 static inline void omap_uart_reset(struct omap_uart_state
*uart
)
226 struct plat_serialomap_port
*p
= uart
->p
;
228 serial_write_reg(p
, UART_OMAP_MDR1
, 0x07);
229 serial_write_reg(p
, UART_OMAP_SCR
, 0x08);
230 serial_write_reg(p
, UART_OMAP_MDR1
, 0x00);
231 serial_write_reg(p
, UART_OMAP_SYSC
, (0x02 << 3) | (1 << 2) | (1 << 0));
234 static inline void omap_uart_enable_clocks(struct omap_uart_state
*uart
)
236 printk(KERN_INFO
"UART clocked?\n");
240 printk(KERN_INFO
"enabling clocks\n");
241 clk_enable(uart
->ick
);
242 clk_enable(uart
->fck
);
244 if (uart
->ick
->clkdm
!= NULL
)
245 my_pwrdm_clkdm_state_switch(uart
->ick
->clkdm
);
247 if (uart
->fck
->clkdm
!= NULL
)
248 my_pwrdm_clkdm_state_switch(uart
->fck
->clkdm
);
254 #ifdef CONFIG_ARCH_OMAP3
256 static void omap_uart_save_context(struct omap_uart_state
*uart
)
259 struct plat_serialomap_port
*p
= uart
->p
;
261 if (!enable_off_mode
)
265 * For omap3430 CORE/PERR OFF isn't temporarily supported,
266 * so no need to save&restore the context of serial.
268 if (cpu_is_omap34xx())
271 lcr
= serial_read_reg(p
, UART_LCR
);
272 serial_write_reg(p
, UART_LCR
, 0xBF);
273 uart
->dll
= serial_read_reg(p
, UART_DLL
);
274 uart
->dlh
= serial_read_reg(p
, UART_DLM
);
275 serial_write_reg(p
, UART_LCR
, lcr
);
276 uart
->ier
= serial_read_reg(p
, UART_IER
);
277 uart
->sysc
= serial_read_reg(p
, UART_OMAP_SYSC
);
278 uart
->scr
= serial_read_reg(p
, UART_OMAP_SCR
);
279 uart
->wer
= serial_read_reg(p
, UART_OMAP_WER
);
281 uart
->context_valid
= 1;
284 static void omap_uart_restore_context(struct omap_uart_state
*uart
)
287 struct plat_serialomap_port
*p
= uart
->p
;
289 if (!enable_off_mode
)
292 if (!uart
->context_valid
)
296 * For omap3430 CORE/PERR OFF isn't temporarily supported,
297 * so no need to save&restore the context of serial.
299 if (cpu_is_omap34xx())
302 uart
->context_valid
= 0;
304 serial_write_reg(p
, UART_OMAP_MDR1
, 0x7);
305 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
306 efr
= serial_read_reg(p
, UART_EFR
);
307 serial_write_reg(p
, UART_EFR
, UART_EFR_ECB
);
308 serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */
309 serial_write_reg(p
, UART_IER
, 0x0);
310 #ifdef CONFIG_SERIAL_OMAP
311 serial_write_reg(p
, UART_FCR
, fcr
[uart
->num
]);
313 serial_write_reg(p
, UART_FCR
, 0xA1);
315 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
316 serial_write_reg(p
, UART_DLL
, uart
->dll
);
317 serial_write_reg(p
, UART_DLM
, uart
->dlh
);
318 serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */
319 serial_write_reg(p
, UART_IER
, uart
->ier
);
320 serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */
321 serial_write_reg(p
, UART_EFR
, efr
);
322 serial_write_reg(p
, UART_LCR
, UART_LCR_WLEN8
);
323 serial_write_reg(p
, UART_OMAP_SCR
, uart
->scr
);
324 serial_write_reg(p
, UART_OMAP_WER
, uart
->wer
);
325 serial_write_reg(p
, UART_OMAP_SYSC
, uart
->sysc
);
326 serial_write_reg(p
, UART_OMAP_MDR1
, 0x00); /* UART 16x mode */
329 static inline void omap_uart_save_context(struct omap_uart_state
*uart
) {}
330 static inline void omap_uart_restore_context(struct omap_uart_state
*uart
) {}
331 #endif /* CONFIG_ARCH_OMAP3 */
333 static void omap_uart_smart_idle_enable(struct omap_uart_state
*uart
,
336 struct plat_serialomap_port
*p
= uart
->p
;
339 sysc
= serial_read_reg(p
, UART_OMAP_SYSC
) & 0x7;
345 serial_write_reg(p
, UART_OMAP_SYSC
, sysc
);
348 static inline void omap_uart_disable_rtspullup(struct omap_uart_state
*uart
)
350 if (!uart
->rts_padconf
|| !uart
->rts_override
)
352 omap_ctrl_writew(uart
->rts_padvalue
, uart
->rts_padconf
);
353 uart
->rts_override
= 0;
356 static inline void omap_uart_enable_rtspullup(struct omap_uart_state
*uart
)
358 if (!uart
->rts_padconf
|| uart
->rts_override
)
361 uart
->rts_padvalue
= omap_ctrl_readw(uart
->rts_padconf
);
362 omap_ctrl_writew(0x18 | 0x7, uart
->rts_padconf
);
363 uart
->rts_override
= 1;
366 static inline void omap_uart_restore(struct omap_uart_state
*uart
)
368 omap_uart_enable_clocks(uart
);
369 omap_uart_restore_context(uart
);
372 static inline void omap_uart_disable_clocks(struct omap_uart_state
*uart
)
376 omap_uart_save_context(uart
);
378 clk_disable(uart
->ick
);
379 clk_disable(uart
->fck
);
382 static void _omap_uart_block_sleep(struct omap_uart_state
*uart
)
384 omap_uart_restore(uart
);
386 omap_uart_smart_idle_enable(uart
, 0);
389 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
391 del_timer(&uart
->timer
);
395 static void omap_uart_block_sleep(int num
)
397 struct omap_uart_state
*uart
;
399 list_for_each_entry(uart
, &uart_list
, node
) {
400 if (num
== uart
->num
)
401 _omap_uart_block_sleep(uart
);
405 EXPORT_SYMBOL(omap_uart_block_sleep
);
408 static void omap_uart_allow_sleep(struct omap_uart_state
*uart
)
413 omap_uart_smart_idle_enable(uart
, 1);
415 del_timer(&uart
->timer
);
418 static void omap_uart_idle_timer(unsigned long data
)
420 struct omap_uart_state
*uart
= (struct omap_uart_state
*)data
;
422 omap_uart_allow_sleep(uart
);
425 void omap_uart_prepare_idle(int num
)
427 struct omap_uart_state
*uart
;
429 list_for_each_entry(uart
, &uart_list
, node
) {
430 if (num
== uart
->num
&& uart
->can_sleep
) {
432 omap_uart_enable_rtspullup(uart
);
434 * There seems to be a window here where
435 * data could still be on the way to the
436 * fifo. This delay is ~1 byte time @ 115.2k
441 #ifdef CONFIG_SERIAL_OMAP
442 if (are_driveromap_uarts_active(num
)) {
444 _omap_uart_block_sleep(uart
);
445 omap_uart_disable_rtspullup(uart
);
449 omap_uart_disable_clocks(uart
);
455 void omap_uart_resume_idle(int num
)
457 struct omap_uart_state
*uart
;
459 list_for_each_entry(uart
, &uart_list
, node
) {
460 if (num
== uart
->num
) {
461 omap_uart_restore(uart
);
462 omap_uart_disable_rtspullup(uart
);
464 /* Check for IO pad wakeup */
465 if (cpu_is_omap34xx() && uart
->padconf
) {
466 u16 p
= omap_ctrl_readw(uart
->padconf
);
468 if (p
& OMAP3_PADCONF_WAKEUPEVENT0
)
469 _omap_uart_block_sleep(uart
);
472 /* Check for normal UART wakeup */
473 if (__raw_readl(uart
->wk_st
) & uart
->wk_mask
) {
474 _omap_uart_block_sleep(uart
);
482 void omap_uart_prepare_suspend(void)
484 struct omap_uart_state
*uart
;
486 list_for_each_entry(uart
, &uart_list
, node
) {
487 omap_uart_allow_sleep(uart
);
491 int omap_uart_can_sleep(void)
493 struct omap_uart_state
*uart
;
496 list_for_each_entry(uart
, &uart_list
, node
) {
500 if (!uart
->can_sleep
) {
505 #ifdef CONFIG_SERIAL_OMAP
506 if (are_driveromap_uarts_active(uart
->num
)) {
512 /* This UART can now safely sleep. */
513 omap_uart_allow_sleep(uart
);
520 * omap_uart_interrupt()
522 * This handler is used only to detect that *any* UART interrupt has
523 * occurred. It does _nothing_ to handle the interrupt. Rather,
524 * any UART interrupt will trigger the inactivity timer so the
525 * UART will not idle or sleep for its timeout period.
528 static irqreturn_t
omap_uart_interrupt(int irq
, void *dev_id
)
530 struct omap_uart_state
*uart
= dev_id
;
532 _omap_uart_block_sleep(uart
);
537 static u32 sleep_timeout
= DEFAULT_TIMEOUT
;
539 static void omap_uart_rtspad_init(struct omap_uart_state
*uart
)
541 if (!cpu_is_omap34xx())
545 uart
->rts_padconf
= 0x17e;
548 uart
->rts_padconf
= 0x176;
551 /* uart->rts_padconf = 0x19c; */
554 uart
->rts_padconf
= 0;
559 static void omap_uart_idle_init(struct omap_uart_state
*uart
)
562 struct plat_serialomap_port
*p
= uart
->p
;
566 uart
->timeout
= sleep_timeout
;
568 _omap_uart_block_sleep(uart
);
570 setup_timer(&uart
->timer
, omap_uart_idle_timer
,
571 (unsigned long) uart
);
572 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
573 omap_uart_smart_idle_enable(uart
, 0);
576 if (cpu_is_omap34xx()) {
577 u32 mod
= (uart
->num
== 2) ? OMAP3430_PER_MOD
: CORE_MOD
;
581 uart
->wk_en
= OMAP34XX_PRM_REGADDR(mod
, PM_WKEN1
);
582 uart
->wk_st
= OMAP34XX_PRM_REGADDR(mod
, PM_WKST1
);
585 wk_mask
= OMAP3430_ST_UART1_MASK
;
586 padconf
= uart0_padconf
;
589 wk_mask
= OMAP3430_ST_UART2_MASK
;
593 wk_mask
= OMAP3430_ST_UART3_MASK
;
597 uart
->wk_mask
= wk_mask
;
598 uart
->padconf
= padconf
;
599 } else if (cpu_is_omap24xx()) {
602 if (cpu_is_omap2430()) {
603 uart
->wk_en
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKEN1
);
604 uart
->wk_st
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKST1
);
605 } else if (cpu_is_omap2420()) {
606 uart
->wk_en
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKEN1
);
607 uart
->wk_st
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKST1
);
611 wk_mask
= OMAP24XX_ST_UART1_MASK
;
614 wk_mask
= OMAP24XX_ST_UART2_MASK
;
617 wk_mask
= OMAP24XX_ST_UART3_MASK
;
620 uart
->wk_mask
= wk_mask
;
628 /* Set wake-enable bit */
629 if (uart
->wk_en
&& uart
->wk_mask
) {
630 v
= __raw_readl(uart
->wk_en
);
632 __raw_writel(v
, uart
->wk_en
);
635 /* Ensure IOPAD wake-enables are set */
636 if (cpu_is_omap34xx() && uart
->padconf
) {
639 v
= omap_ctrl_readw(uart
->padconf
);
640 v
|= OMAP3_PADCONF_WAKEUPENABLE0
;
641 omap_ctrl_writew(v
, uart
->padconf
);
644 p
->flags
|= UPF_SHARE_IRQ
;
645 ret
= request_irq(p
->irq
, omap_uart_interrupt
, IRQF_SHARED
,
646 "serial idle", (void *)uart
);
650 void omap_uart_enable_irqs(int enable
)
653 struct omap_uart_state
*uart
;
655 list_for_each_entry(uart
, &uart_list
, node
) {
657 ret
= request_irq(uart
->p
->irq
, omap_uart_interrupt
,
658 IRQF_SHARED
, "serial idle", (void *)uart
);
660 free_irq(uart
->p
->irq
, (void *)uart
);
665 static ssize_t
sleep_timeout_show(struct kobject
*kobj
,
666 struct kobj_attribute
*attr
,
669 return sprintf(buf
, "%u\n", sleep_timeout
/ HZ
);
672 static ssize_t
sleep_timeout_store(struct kobject
*kobj
,
673 struct kobj_attribute
*attr
,
674 const char *buf
, size_t n
)
676 struct omap_uart_state
*uart
;
679 if (sscanf(buf
, "%u", &value
) != 1) {
680 printk(KERN_ERR
"sleep_timeout_store: Invalid value\n");
683 sleep_timeout
= value
* HZ
;
684 list_for_each_entry(uart
, &uart_list
, node
) {
685 uart
->timeout
= sleep_timeout
;
687 mod_timer(&uart
->timer
, jiffies
+ uart
->timeout
);
689 /* A zero value means disable timeout feature */
690 _omap_uart_block_sleep(uart
);
695 static struct kobj_attribute sleep_timeout_attr
=
696 __ATTR(sleep_timeout
, 0644, sleep_timeout_show
, sleep_timeout_store
);
700 static inline void omap_uart_idle_init(struct omap_uart_state
*uart
) {}
701 #endif /* CONFIG_PM */
703 static int fifo_idleblk_get(void *data
, u64
*val
)
705 *val
= fifo_idleblks
;
709 static int fifo_idleblk_set(void *data
, u64 val
)
715 DEFINE_SIMPLE_ATTRIBUTE(fifo_idleblk_fops
, fifo_idleblk_get
, fifo_idleblk_set
, "%llu\n");
716 void omap_serial_early_init(void)
720 void omap_serial_ctsrts_init(unsigned char ctsrts
[])
722 #if defined(CONFIG_SERIAL_OMAP) && \
723 defined(CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL)
724 serial_platform_data
[0].ctsrts
= ctsrts
[0];
725 serial_platform_data
[1].ctsrts
= ctsrts
[1];
726 serial_platform_data
[2].ctsrts
= ctsrts
[2];
730 void omap_uart_set_uart0_padconf(int padconf
)
732 uart0_padconf
= padconf
;
735 void my_omap_serial_init(int wake_gpio_strobe
,
736 unsigned int wake_strobe_enable_mask
)
741 debugfs_create_file("fifo_idle_block_count", 0644, NULL
, NULL
, &fifo_idleblk_fops
);
743 * Make sure the serial ports are muxed on at this point.
744 * You have to mux them off in device drivers later on
750 struct plat_serialomap_port
*p
= serial_platform_data
+ i
;
751 struct omap_uart_state
*uart
= &omap_uart
[i
];
753 if (wake_strobe_enable_mask
& (1 << i
))
754 p
->wake_gpio_strobe
= wake_gpio_strobe
;
756 sprintf(name
, "uart%d_ick", i
+1);
757 uart
->ick
= clk_get(NULL
, name
);
758 if (IS_ERR(uart
->ick
)) {
759 printk(KERN_ERR
"Could not get uart%d_ick\n", i
+1);
763 sprintf(name
, "uart%d_fck", i
+1);
764 uart
->fck
= clk_get(NULL
, name
);
765 if (IS_ERR(uart
->fck
)) {
766 printk(KERN_ERR
"Could not get uart%d_fck\n", i
+1);
770 if (!uart
->ick
|| !uart
->fck
)
774 p
->private_data
= uart
;
776 list_add(&uart
->node
, &uart_list
);
778 omap_uart_enable_clocks(uart
);
779 omap_uart_reset(uart
);
780 omap_uart_rtspad_init(uart
);
781 omap_uart_idle_init(uart
);
785 int my_omap_hs_init(void)
789 ret
= platform_add_devices(uart_devices
, ARRAY_SIZE(uart_devices
));
791 printk(KERN_ERR
"Error adding uart devices (%d)\n", ret
);