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 void omap_ctrl_writew(u16 val
, u16 offset
) 
  45         __raw_writew(val
, OMAP_CTRL_REGADDR(offset
)); 
  48 u16 
omap_ctrl_readw(u16 offset
) 
  50         return __raw_readw(OMAP_CTRL_REGADDR(offset
)); 
  53 #define DEFAULT_TIMEOUT HZ 
  55 struct omap_uart_state 
{ 
  58         struct timer_list timer
; 
  74         struct plat_serialomap_port 
*p
; 
  75         struct list_head node
; 
  77 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM) 
  80         /* Registers to be saved/restored for OFF-mode */ 
  90 static struct omap_uart_state omap_uart
[OMAP_MAX_NR_PORTS
]; 
  91 static LIST_HEAD(uart_list
); 
  92 static unsigned int fifo_idleblks 
= 0; 
  93 static int uart0_padconf 
= 0x180; 
  95 static struct plat_serialomap_port serial_platform_data
[] = { 
  97                 .membase        
= OMAP2_L4_IO_ADDRESS(OMAP_UART1_BASE
), 
 100 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL 
 101                 .ctsrts         
= UART_EFR_CTS 
| UART_EFR_RTS
, 
 103                 .flags          
= UPF_BOOT_AUTOCONF
, 
 106                 .membase        
= OMAP2_L4_IO_ADDRESS(OMAP_UART2_BASE
), 
 109 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL 
 110                 .ctsrts         
= UART_EFR_CTS 
| UART_EFR_RTS
, 
 112                 .flags          
= UPF_BOOT_AUTOCONF
, 
 115                 .membase        
= OMAP2_L4_IO_ADDRESS(OMAP_UART3_BASE
), 
 118 #ifdef CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL 
 119                 .ctsrts         
= UART_EFR_RTS
, 
 121                 .flags          
= UPF_BOOT_AUTOCONF
, 
 126 static struct resource omap2_uart1_resources
[] = { 
 128                 .start          
= OMAP_UART1_BASE
, 
 129                 .end            
= OMAP_UART1_BASE 
+ 0x3ff, 
 130                 .flags          
= IORESOURCE_MEM
, 
 133                 .flags          
= IORESOURCE_IRQ
, 
 137 static struct resource omap2_uart2_resources
[] = { 
 139                 .start          
= OMAP_UART2_BASE
, 
 140                 .end            
= OMAP_UART2_BASE 
+ 0x3ff, 
 141                 .flags          
= IORESOURCE_MEM
, 
 144                 .flags          
= IORESOURCE_IRQ
, 
 149 static struct resource omap2_uart3_resources
[] = { 
 151                 .start          
= OMAP_UART3_BASE
, 
 152                 .end            
= OMAP_UART3_BASE 
+ 0x3ff, 
 153                 .flags          
= IORESOURCE_MEM
, 
 156                 .flags          
= IORESOURCE_IRQ
, 
 160 #ifdef CONFIG_MACH_OMAP_ZOOM2 
 161 static struct resource omap2_quaduart_resources
[] = { 
 164                 .end            
= 0x10000000 + (0x16 << 1), 
 165                 .flags          
= IORESOURCE_MEM
, 
 167                 .start          
= OMAP_GPIO_IRQ(102), 
 168                 .flags          
= IORESOURCE_IRQ
, 
 174 /* OMAP UART platform structure */ 
 175 static struct platform_device uart1_device 
= { 
 178         .num_resources          
= ARRAY_SIZE(omap2_uart1_resources
), 
 179         .resource               
= omap2_uart1_resources
, 
 180         .dev
.platform_data      
= &serial_platform_data
[0], 
 182 static struct platform_device uart2_device 
= { 
 185         .num_resources          
= ARRAY_SIZE(omap2_uart2_resources
), 
 186         .resource               
= omap2_uart2_resources
, 
 187         .dev
.platform_data      
= &serial_platform_data
[1], 
 190 static struct platform_device uart3_device 
= { 
 191         .name                   
= "omap-hs-uart", 
 193         .num_resources          
= ARRAY_SIZE(omap2_uart3_resources
), 
 194         .resource               
= omap2_uart3_resources
, 
 195         .dev
.platform_data      
= &serial_platform_data
[2], 
 198 static struct platform_device 
*uart_devices
[] = { 
 202 static inline unsigned int serial_read_reg(struct plat_serialomap_port 
*up
, 
 205         offset 
<<= up
->regshift
; 
 206         return (unsigned int)__raw_readb(up
->membase 
+ offset
); 
 209 static inline void serial_write_reg(struct plat_serialomap_port 
*p
, int offset
, 
 212         offset 
<<= p
->regshift
; 
 213         __raw_writeb(value
, p
->membase 
+ offset
); 
 217  * Internal UARTs need to be initialized for the 8250 autoconfig to work 
 218  * properly. Note that the TX watermark initialization may not be needed 
 219  * once the 8250.c watermark handling code is merged. 
 221 static inline void omap_uart_reset(struct omap_uart_state 
*uart
) 
 223         struct plat_serialomap_port 
*p 
= uart
->p
; 
 225         serial_write_reg(p
, UART_OMAP_MDR1
, 0x07); 
 226         serial_write_reg(p
, UART_OMAP_SCR
, 0x08); 
 227         serial_write_reg(p
, UART_OMAP_MDR1
, 0x00); 
 228         serial_write_reg(p
, UART_OMAP_SYSC
, (0x02 << 3) | (1 << 2) | (1 << 0)); 
 231 static inline void omap_uart_enable_clocks(struct omap_uart_state 
*uart
) 
 236         clk_enable(uart
->ick
); 
 237         clk_enable(uart
->fck
); 
 242 #ifdef CONFIG_ARCH_OMAP3 
 244 static void omap_uart_save_context(struct omap_uart_state 
*uart
) 
 247         struct plat_serialomap_port 
*p 
= uart
->p
; 
 249         if (!enable_off_mode
) 
 253          * For omap3430 CORE/PERR OFF isn't temporarily supported, 
 254          * so no need to save&restore the context of serial. 
 256         if (cpu_is_omap34xx()) 
 259         lcr 
= serial_read_reg(p
, UART_LCR
); 
 260         serial_write_reg(p
, UART_LCR
, 0xBF); 
 261         uart
->dll 
= serial_read_reg(p
, UART_DLL
); 
 262         uart
->dlh 
= serial_read_reg(p
, UART_DLM
); 
 263         serial_write_reg(p
, UART_LCR
, lcr
); 
 264         uart
->ier 
= serial_read_reg(p
, UART_IER
); 
 265         uart
->sysc 
= serial_read_reg(p
, UART_OMAP_SYSC
); 
 266         uart
->scr 
= serial_read_reg(p
, UART_OMAP_SCR
); 
 267         uart
->wer 
= serial_read_reg(p
, UART_OMAP_WER
); 
 269         uart
->context_valid 
= 1; 
 272 static void omap_uart_restore_context(struct omap_uart_state 
*uart
) 
 275         struct plat_serialomap_port 
*p 
= uart
->p
; 
 277         if (!enable_off_mode
) 
 280         if (!uart
->context_valid
) 
 284          * For omap3430 CORE/PERR OFF isn't temporarily supported, 
 285          * so no need to save&restore the context of serial. 
 287         if (cpu_is_omap34xx()) 
 290         uart
->context_valid 
= 0; 
 292         serial_write_reg(p
, UART_OMAP_MDR1
, 0x7); 
 293         serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */ 
 294         efr 
= serial_read_reg(p
, UART_EFR
); 
 295         serial_write_reg(p
, UART_EFR
, UART_EFR_ECB
); 
 296         serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */ 
 297         serial_write_reg(p
, UART_IER
, 0x0); 
 298 #ifdef CONFIG_SERIAL_OMAP 
 299         serial_write_reg(p
, UART_FCR
, fcr
[uart
->num
]); 
 301         serial_write_reg(p
, UART_FCR
, 0xA1); 
 303         serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */ 
 304         serial_write_reg(p
, UART_DLL
, uart
->dll
); 
 305         serial_write_reg(p
, UART_DLM
, uart
->dlh
); 
 306         serial_write_reg(p
, UART_LCR
, 0x0); /* Operational mode */ 
 307         serial_write_reg(p
, UART_IER
, uart
->ier
); 
 308         serial_write_reg(p
, UART_LCR
, 0xBF); /* Config B mode */ 
 309         serial_write_reg(p
, UART_EFR
, efr
); 
 310         serial_write_reg(p
, UART_LCR
, UART_LCR_WLEN8
); 
 311         serial_write_reg(p
, UART_OMAP_SCR
, uart
->scr
); 
 312         serial_write_reg(p
, UART_OMAP_WER
, uart
->wer
); 
 313         serial_write_reg(p
, UART_OMAP_SYSC
, uart
->sysc
); 
 314         serial_write_reg(p
, UART_OMAP_MDR1
, 0x00); /* UART 16x mode */ 
 317 static inline void omap_uart_save_context(struct omap_uart_state 
*uart
) {} 
 318 static inline void omap_uart_restore_context(struct omap_uart_state 
*uart
) {} 
 319 #endif /* CONFIG_ARCH_OMAP3 */ 
 321 static void omap_uart_smart_idle_enable(struct omap_uart_state 
*uart
, 
 324         struct plat_serialomap_port 
*p 
= uart
->p
; 
 327         sysc 
= serial_read_reg(p
, UART_OMAP_SYSC
) & 0x7; 
 333         serial_write_reg(p
, UART_OMAP_SYSC
, sysc
); 
 336 static inline void omap_uart_disable_rtspullup(struct omap_uart_state 
*uart
) 
 338         if (!uart
->rts_padconf 
|| !uart
->rts_override
) 
 340         omap_ctrl_writew(uart
->rts_padvalue
, uart
->rts_padconf
); 
 341         uart
->rts_override 
= 0; 
 344 static inline void omap_uart_enable_rtspullup(struct omap_uart_state 
*uart
) 
 346         if (!uart
->rts_padconf 
|| uart
->rts_override
) 
 349         uart
->rts_padvalue 
= omap_ctrl_readw(uart
->rts_padconf
); 
 350         omap_ctrl_writew(0x18 | 0x7, uart
->rts_padconf
); 
 351         uart
->rts_override 
= 1; 
 354 static inline void omap_uart_restore(struct omap_uart_state 
*uart
) 
 356         omap_uart_enable_clocks(uart
); 
 357         omap_uart_restore_context(uart
); 
 360 static inline void omap_uart_disable_clocks(struct omap_uart_state 
*uart
) 
 364         omap_uart_save_context(uart
); 
 366         clk_disable(uart
->ick
); 
 367         clk_disable(uart
->fck
); 
 370 static void _omap_uart_block_sleep(struct omap_uart_state 
*uart
) 
 372         omap_uart_restore(uart
); 
 374         omap_uart_smart_idle_enable(uart
, 0); 
 377                 mod_timer(&uart
->timer
, jiffies 
+ uart
->timeout
); 
 379                 del_timer(&uart
->timer
); 
 383 static void omap_uart_block_sleep(int num
) 
 385         struct omap_uart_state 
*uart
; 
 387         list_for_each_entry(uart
, &uart_list
, node
) { 
 388                 if (num 
== uart
->num
) 
 389                         _omap_uart_block_sleep(uart
); 
 393 EXPORT_SYMBOL(omap_uart_block_sleep
); 
 396 static void omap_uart_allow_sleep(struct omap_uart_state 
*uart
) 
 401         omap_uart_smart_idle_enable(uart
, 1); 
 403         del_timer(&uart
->timer
); 
 406 static void omap_uart_idle_timer(unsigned long data
) 
 408         struct omap_uart_state 
*uart 
= (struct omap_uart_state 
*)data
; 
 410         omap_uart_allow_sleep(uart
); 
 413 void omap_uart_prepare_idle(int num
) 
 415         struct omap_uart_state 
*uart
; 
 417         list_for_each_entry(uart
, &uart_list
, node
) { 
 418                 if (num 
== uart
->num 
&& uart
->can_sleep
) { 
 420                         omap_uart_enable_rtspullup(uart
); 
 422                          * There seems to be a window here where 
 423                          * data could still be on the way to the 
 424                          * fifo. This delay is ~1 byte time @ 115.2k 
 429 #ifdef CONFIG_SERIAL_OMAP 
 430                         if (are_driveromap_uarts_active(num
)) { 
 432                                 _omap_uart_block_sleep(uart
); 
 433                                 omap_uart_disable_rtspullup(uart
); 
 437                         omap_uart_disable_clocks(uart
); 
 443 void omap_uart_resume_idle(int num
) 
 445         struct omap_uart_state 
*uart
; 
 447         list_for_each_entry(uart
, &uart_list
, node
) { 
 448                 if (num 
== uart
->num
) { 
 449                         omap_uart_restore(uart
); 
 450                         omap_uart_disable_rtspullup(uart
); 
 452                         /* Check for IO pad wakeup */ 
 453                         if (cpu_is_omap34xx() && uart
->padconf
) { 
 454                                 u16 p 
= omap_ctrl_readw(uart
->padconf
); 
 456                                 if (p 
& OMAP3_PADCONF_WAKEUPEVENT0
) 
 457                                         _omap_uart_block_sleep(uart
); 
 460                         /* Check for normal UART wakeup */ 
 461                         if (__raw_readl(uart
->wk_st
) & uart
->wk_mask
) { 
 462                                 _omap_uart_block_sleep(uart
); 
 470 void omap_uart_prepare_suspend(void) 
 472         struct omap_uart_state 
*uart
; 
 474         list_for_each_entry(uart
, &uart_list
, node
) { 
 475                 omap_uart_allow_sleep(uart
); 
 479 int omap_uart_can_sleep(void) 
 481         struct omap_uart_state 
*uart
; 
 484         list_for_each_entry(uart
, &uart_list
, node
) { 
 488                 if (!uart
->can_sleep
) { 
 493 #ifdef CONFIG_SERIAL_OMAP 
 494                 if (are_driveromap_uarts_active(uart
->num
)) { 
 500                 /* This UART can now safely sleep. */ 
 501                 omap_uart_allow_sleep(uart
); 
 508  * omap_uart_interrupt() 
 510  * This handler is used only to detect that *any* UART interrupt has 
 511  * occurred.  It does _nothing_ to handle the interrupt.  Rather, 
 512  * any UART interrupt will trigger the inactivity timer so the 
 513  * UART will not idle or sleep for its timeout period. 
 516 static irqreturn_t 
omap_uart_interrupt(int irq
, void *dev_id
) 
 518         struct omap_uart_state 
*uart 
= dev_id
; 
 520         _omap_uart_block_sleep(uart
); 
 525 static u32 sleep_timeout 
= DEFAULT_TIMEOUT
; 
 527 static void omap_uart_rtspad_init(struct omap_uart_state 
*uart
) 
 529         if (!cpu_is_omap34xx()) 
 533                 uart
->rts_padconf 
= 0x17e; 
 536                 uart
->rts_padconf 
= 0x176; 
 539 /*              uart->rts_padconf = 0x19c; */ 
 542                 uart
->rts_padconf 
= 0; 
 547 static void omap_uart_idle_init(struct omap_uart_state 
*uart
) 
 550         struct plat_serialomap_port 
*p 
= uart
->p
; 
 554         uart
->timeout 
= sleep_timeout
; 
 556                 _omap_uart_block_sleep(uart
); 
 558                 setup_timer(&uart
->timer
, omap_uart_idle_timer
, 
 559                             (unsigned long) uart
); 
 560                 mod_timer(&uart
->timer
, jiffies 
+ uart
->timeout
); 
 561                 omap_uart_smart_idle_enable(uart
, 0); 
 564         if (cpu_is_omap34xx()) { 
 565                 u32 mod 
= (uart
->num 
== 2) ? OMAP3430_PER_MOD 
: CORE_MOD
; 
 569                 uart
->wk_en 
= OMAP34XX_PRM_REGADDR(mod
, PM_WKEN1
); 
 570                 uart
->wk_st 
= OMAP34XX_PRM_REGADDR(mod
, PM_WKST1
); 
 573                         wk_mask 
= OMAP3430_ST_UART1_MASK
; 
 574                         padconf 
= uart0_padconf
; 
 577                         wk_mask 
= OMAP3430_ST_UART2_MASK
; 
 581                         wk_mask 
= OMAP3430_ST_UART3_MASK
; 
 585                 uart
->wk_mask 
= wk_mask
; 
 586                 uart
->padconf 
= padconf
; 
 587         } else if (cpu_is_omap24xx()) { 
 590                 if (cpu_is_omap2430()) { 
 591                         uart
->wk_en 
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKEN1
); 
 592                         uart
->wk_st 
= OMAP2430_PRM_REGADDR(CORE_MOD
, PM_WKST1
); 
 593                 } else if (cpu_is_omap2420()) { 
 594                         uart
->wk_en 
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKEN1
); 
 595                         uart
->wk_st 
= OMAP2420_PRM_REGADDR(CORE_MOD
, PM_WKST1
); 
 599                         wk_mask 
= OMAP24XX_ST_UART1_MASK
; 
 602                         wk_mask 
= OMAP24XX_ST_UART2_MASK
; 
 605                         wk_mask 
= OMAP24XX_ST_UART3_MASK
; 
 608                 uart
->wk_mask 
= wk_mask
; 
 616         /* Set wake-enable bit */ 
 617         if (uart
->wk_en 
&& uart
->wk_mask
) { 
 618                 v 
= __raw_readl(uart
->wk_en
); 
 620                 __raw_writel(v
, uart
->wk_en
); 
 623         /* Ensure IOPAD wake-enables are set */ 
 624         if (cpu_is_omap34xx() && uart
->padconf
) { 
 627                 v 
= omap_ctrl_readw(uart
->padconf
); 
 628                 v 
|= OMAP3_PADCONF_WAKEUPENABLE0
; 
 629                 omap_ctrl_writew(v
, uart
->padconf
); 
 632         p
->flags 
|= UPF_SHARE_IRQ
; 
 633         ret 
= request_irq(p
->irq
, omap_uart_interrupt
, IRQF_SHARED
, 
 634                           "serial idle", (void *)uart
); 
 638 void omap_uart_enable_irqs(int enable
) 
 641         struct omap_uart_state 
*uart
; 
 643         list_for_each_entry(uart
, &uart_list
, node
) { 
 645                         ret 
= request_irq(uart
->p
->irq
, omap_uart_interrupt
, 
 646                                 IRQF_SHARED
, "serial idle", (void *)uart
); 
 648                         free_irq(uart
->p
->irq
, (void *)uart
); 
 653 static ssize_t 
sleep_timeout_show(struct kobject 
*kobj
, 
 654                                   struct kobj_attribute 
*attr
, 
 657         return sprintf(buf
, "%u\n", sleep_timeout 
/ HZ
); 
 660 static ssize_t 
sleep_timeout_store(struct kobject 
*kobj
, 
 661                                    struct kobj_attribute 
*attr
, 
 662                                    const char *buf
, size_t n
) 
 664         struct omap_uart_state 
*uart
; 
 667         if (sscanf(buf
, "%u", &value
) != 1) { 
 668                 printk(KERN_ERR 
"sleep_timeout_store: Invalid value\n"); 
 671         sleep_timeout 
= value 
* HZ
; 
 672         list_for_each_entry(uart
, &uart_list
, node
) { 
 673                 uart
->timeout 
= sleep_timeout
; 
 675                         mod_timer(&uart
->timer
, jiffies 
+ uart
->timeout
); 
 677                         /* A zero value means disable timeout feature */ 
 678                         _omap_uart_block_sleep(uart
); 
 683 static struct kobj_attribute sleep_timeout_attr 
= 
 684         __ATTR(sleep_timeout
, 0644, sleep_timeout_show
, sleep_timeout_store
); 
 688 static inline void omap_uart_idle_init(struct omap_uart_state 
*uart
) {} 
 689 #endif /* CONFIG_PM */ 
 691 static int fifo_idleblk_get(void *data
, u64 
*val
) 
 693         *val 
= fifo_idleblks
; 
 697 static int fifo_idleblk_set(void *data
, u64 val
) 
 703 DEFINE_SIMPLE_ATTRIBUTE(fifo_idleblk_fops
, fifo_idleblk_get
, fifo_idleblk_set
, "%llu\n"); 
 704 void omap_serial_early_init(void) 
 708 void omap_serial_ctsrts_init(unsigned char ctsrts
[]) 
 710 #if defined(CONFIG_SERIAL_OMAP) && \ 
 711         defined(CONFIG_SERIAL_OMAP3430_HW_FLOW_CONTROL) 
 712         serial_platform_data
[0].ctsrts 
= ctsrts
[0]; 
 713         serial_platform_data
[1].ctsrts 
= ctsrts
[1]; 
 714         serial_platform_data
[2].ctsrts 
= ctsrts
[2]; 
 718 void omap_uart_set_uart0_padconf(int padconf
) 
 720         uart0_padconf 
= padconf
; 
 723 void my_omap_serial_init(int wake_gpio_strobe
, 
 724                              unsigned int wake_strobe_enable_mask
) 
 729         debugfs_create_file("fifo_idle_block_count", 0644, NULL
, NULL
, &fifo_idleblk_fops
); 
 731          * Make sure the serial ports are muxed on at this point. 
 732          * You have to mux them off in device drivers later on 
 738                 struct plat_serialomap_port 
*p 
= serial_platform_data 
+ i
; 
 739                 struct omap_uart_state 
*uart 
= &omap_uart
[i
]; 
 741                 if (wake_strobe_enable_mask 
& (1 << i
)) 
 742                         p
->wake_gpio_strobe 
= wake_gpio_strobe
; 
 744                 sprintf(name
, "uart%d_ick", i
+1); 
 745                 uart
->ick 
= clk_get(NULL
, name
); 
 746                 if (IS_ERR(uart
->ick
)) { 
 747                         printk(KERN_ERR 
"Could not get uart%d_ick\n", i
+1); 
 751                 sprintf(name
, "uart%d_fck", i
+1); 
 752                 uart
->fck 
= clk_get(NULL
, name
); 
 753                 if (IS_ERR(uart
->fck
)) { 
 754                         printk(KERN_ERR 
"Could not get uart%d_fck\n", i
+1); 
 758                 if (!uart
->ick 
|| !uart
->fck
) 
 762                 p
->private_data 
= uart
; 
 764                 list_add(&uart
->node
, &uart_list
); 
 766                 omap_uart_enable_clocks(uart
); 
 767                 omap_uart_reset(uart
); 
 768                 omap_uart_rtspad_init(uart
); 
 769                 omap_uart_idle_init(uart
); 
 773 int my_omap_hs_init(void) 
 777         ret 
= platform_add_devices(uart_devices
, ARRAY_SIZE(uart_devices
)); 
 779                 printk(KERN_ERR 
"Error adding uart devices (%d)\n", ret
);