Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/serial/imx.c |
| 3 | * |
| 4 | * Driver for Motorola IMX serial ports |
| 5 | * |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 7 | * |
| 8 | * Author: Sascha Hauer <sascha@saschahauer.de> |
| 9 | * Copyright (C) 2004 Pengutronix |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | * [29-Mar-2005] Mike Lee |
| 26 | * Added hardware handshake |
| 27 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 30 | #define SUPPORT_SYSRQ |
| 31 | #endif |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/ioport.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/console.h> |
| 37 | #include <linux/sysrq.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 38 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/tty.h> |
| 40 | #include <linux/tty_flip.h> |
| 41 | #include <linux/serial_core.h> |
| 42 | #include <linux/serial.h> |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 43 | #include <linux/clk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/io.h> |
| 46 | #include <asm/irq.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 47 | #include <mach/hardware.h> |
| 48 | #include <mach/imx-uart.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 50 | /* Register definitions */ |
| 51 | #define URXD0 0x0 /* Receiver Register */ |
| 52 | #define URTX0 0x40 /* Transmitter Register */ |
| 53 | #define UCR1 0x80 /* Control Register 1 */ |
| 54 | #define UCR2 0x84 /* Control Register 2 */ |
| 55 | #define UCR3 0x88 /* Control Register 3 */ |
| 56 | #define UCR4 0x8c /* Control Register 4 */ |
| 57 | #define UFCR 0x90 /* FIFO Control Register */ |
| 58 | #define USR1 0x94 /* Status Register 1 */ |
| 59 | #define USR2 0x98 /* Status Register 2 */ |
| 60 | #define UESC 0x9c /* Escape Character Register */ |
| 61 | #define UTIM 0xa0 /* Escape Timer Register */ |
| 62 | #define UBIR 0xa4 /* BRM Incremental Register */ |
| 63 | #define UBMR 0xa8 /* BRM Modulator Register */ |
| 64 | #define UBRC 0xac /* Baud Rate Count Register */ |
Sascha Hauer | 604cbad | 2008-07-05 10:02:58 +0200 | [diff] [blame] | 65 | #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2 |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 66 | #define ONEMS 0xb0 /* One Millisecond register */ |
| 67 | #define UTS 0xb4 /* UART Test Register */ |
| 68 | #endif |
Sascha Hauer | 8c8fdbc | 2009-04-01 12:40:15 +0200 | [diff] [blame^] | 69 | #ifdef CONFIG_ARCH_MX1 |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 70 | #define BIPR1 0xb0 /* Incremental Preset Register 1 */ |
| 71 | #define BIPR2 0xb4 /* Incremental Preset Register 2 */ |
| 72 | #define BIPR3 0xb8 /* Incremental Preset Register 3 */ |
| 73 | #define BIPR4 0xbc /* Incremental Preset Register 4 */ |
| 74 | #define BMPR1 0xc0 /* BRM Modulator Register 1 */ |
| 75 | #define BMPR2 0xc4 /* BRM Modulator Register 2 */ |
| 76 | #define BMPR3 0xc8 /* BRM Modulator Register 3 */ |
| 77 | #define BMPR4 0xcc /* BRM Modulator Register 4 */ |
| 78 | #define UTS 0xd0 /* UART Test Register */ |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 79 | #endif |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 80 | |
| 81 | /* UART Control Register Bit Fields.*/ |
| 82 | #define URXD_CHARRDY (1<<15) |
| 83 | #define URXD_ERR (1<<14) |
| 84 | #define URXD_OVRRUN (1<<13) |
| 85 | #define URXD_FRMERR (1<<12) |
| 86 | #define URXD_BRK (1<<11) |
| 87 | #define URXD_PRERR (1<<10) |
| 88 | #define UCR1_ADEN (1<<15) /* Auto dectect interrupt */ |
| 89 | #define UCR1_ADBR (1<<14) /* Auto detect baud rate */ |
| 90 | #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */ |
| 91 | #define UCR1_IDEN (1<<12) /* Idle condition interrupt */ |
| 92 | #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */ |
| 93 | #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */ |
| 94 | #define UCR1_IREN (1<<7) /* Infrared interface enable */ |
| 95 | #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */ |
| 96 | #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */ |
| 97 | #define UCR1_SNDBRK (1<<4) /* Send break */ |
| 98 | #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */ |
Sascha Hauer | 8c8fdbc | 2009-04-01 12:40:15 +0200 | [diff] [blame^] | 99 | #ifdef CONFIG_ARCH_MX1 |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 100 | #define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */ |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 101 | #endif |
Sascha Hauer | 604cbad | 2008-07-05 10:02:58 +0200 | [diff] [blame] | 102 | #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2 |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 103 | #define UCR1_UARTCLKEN (0) /* not present on mx2/mx3 */ |
| 104 | #endif |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 105 | #define UCR1_DOZE (1<<1) /* Doze */ |
| 106 | #define UCR1_UARTEN (1<<0) /* UART enabled */ |
| 107 | #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */ |
| 108 | #define UCR2_IRTS (1<<14) /* Ignore RTS pin */ |
| 109 | #define UCR2_CTSC (1<<13) /* CTS pin control */ |
| 110 | #define UCR2_CTS (1<<12) /* Clear to send */ |
| 111 | #define UCR2_ESCEN (1<<11) /* Escape enable */ |
| 112 | #define UCR2_PREN (1<<8) /* Parity enable */ |
| 113 | #define UCR2_PROE (1<<7) /* Parity odd/even */ |
| 114 | #define UCR2_STPB (1<<6) /* Stop */ |
| 115 | #define UCR2_WS (1<<5) /* Word size */ |
| 116 | #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */ |
| 117 | #define UCR2_TXEN (1<<2) /* Transmitter enabled */ |
| 118 | #define UCR2_RXEN (1<<1) /* Receiver enabled */ |
| 119 | #define UCR2_SRST (1<<0) /* SW reset */ |
| 120 | #define UCR3_DTREN (1<<13) /* DTR interrupt enable */ |
| 121 | #define UCR3_PARERREN (1<<12) /* Parity enable */ |
| 122 | #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */ |
| 123 | #define UCR3_DSR (1<<10) /* Data set ready */ |
| 124 | #define UCR3_DCD (1<<9) /* Data carrier detect */ |
| 125 | #define UCR3_RI (1<<8) /* Ring indicator */ |
| 126 | #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */ |
| 127 | #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */ |
| 128 | #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */ |
| 129 | #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */ |
Sascha Hauer | 8c8fdbc | 2009-04-01 12:40:15 +0200 | [diff] [blame^] | 130 | #ifdef CONFIG_ARCH_MX1 |
Marc Kleine-Budde | 4411805 | 2008-07-28 12:10:34 +0200 | [diff] [blame] | 131 | #define UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */ |
| 132 | #define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */ |
| 133 | #endif |
| 134 | #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3 |
| 135 | #define UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */ |
| 136 | #endif |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 137 | #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */ |
| 138 | #define UCR3_BPEN (1<<0) /* Preset registers enable */ |
| 139 | #define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */ |
| 140 | #define UCR4_INVR (1<<9) /* Inverted infrared reception */ |
| 141 | #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */ |
| 142 | #define UCR4_WKEN (1<<7) /* Wake interrupt enable */ |
| 143 | #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */ |
| 144 | #define UCR4_IRSC (1<<5) /* IR special case */ |
| 145 | #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */ |
| 146 | #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */ |
| 147 | #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */ |
| 148 | #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */ |
| 149 | #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */ |
| 150 | #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */ |
| 151 | #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */ |
| 152 | #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */ |
| 153 | #define USR1_RTSS (1<<14) /* RTS pin status */ |
| 154 | #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */ |
| 155 | #define USR1_RTSD (1<<12) /* RTS delta */ |
| 156 | #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */ |
| 157 | #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */ |
| 158 | #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */ |
| 159 | #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */ |
| 160 | #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */ |
| 161 | #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */ |
| 162 | #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */ |
| 163 | #define USR2_ADET (1<<15) /* Auto baud rate detect complete */ |
| 164 | #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */ |
| 165 | #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */ |
| 166 | #define USR2_IDLE (1<<12) /* Idle condition */ |
| 167 | #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */ |
| 168 | #define USR2_WAKE (1<<7) /* Wake */ |
| 169 | #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */ |
| 170 | #define USR2_TXDC (1<<3) /* Transmitter complete */ |
| 171 | #define USR2_BRCD (1<<2) /* Break condition */ |
| 172 | #define USR2_ORE (1<<1) /* Overrun error */ |
| 173 | #define USR2_RDR (1<<0) /* Recv data ready */ |
| 174 | #define UTS_FRCPERR (1<<13) /* Force parity error */ |
| 175 | #define UTS_LOOP (1<<12) /* Loop tx and rx */ |
| 176 | #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */ |
| 177 | #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */ |
| 178 | #define UTS_TXFULL (1<<4) /* TxFIFO full */ |
| 179 | #define UTS_RXFULL (1<<3) /* RxFIFO full */ |
| 180 | #define UTS_SOFTRST (1<<0) /* Software reset */ |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* We've been assigned a range on the "Low-density serial ports" major */ |
Paulius Zaleckas | bd006a9 | 2008-11-14 11:01:39 +0100 | [diff] [blame] | 183 | #ifdef CONFIG_ARCH_MXC |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 184 | #define SERIAL_IMX_MAJOR 207 |
| 185 | #define MINOR_START 16 |
| 186 | #define DEV_NAME "ttymxc" |
Sascha Hauer | 9d631b8 | 2008-12-18 11:08:55 +0100 | [diff] [blame] | 187 | #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 188 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * This determines how often we check the modem status signals |
| 192 | * for any change. They generally aren't connected to an IRQ |
| 193 | * so we have to poll them. We also check immediately before |
| 194 | * filling the TX fifo incase CTS has been dropped. |
| 195 | */ |
| 196 | #define MCTRL_TIMEOUT (250*HZ/1000) |
| 197 | |
| 198 | #define DRIVER_NAME "IMX-uart" |
| 199 | |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 200 | #define UART_NR 8 |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | struct imx_port { |
| 203 | struct uart_port port; |
| 204 | struct timer_list timer; |
| 205 | unsigned int old_status; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 206 | int txirq,rxirq,rtsirq; |
| 207 | int have_rtscts:1; |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 208 | struct clk *clk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | /* |
| 212 | * Handle any change of modem status signal since we were last called. |
| 213 | */ |
| 214 | static void imx_mctrl_check(struct imx_port *sport) |
| 215 | { |
| 216 | unsigned int status, changed; |
| 217 | |
| 218 | status = sport->port.ops->get_mctrl(&sport->port); |
| 219 | changed = status ^ sport->old_status; |
| 220 | |
| 221 | if (changed == 0) |
| 222 | return; |
| 223 | |
| 224 | sport->old_status = status; |
| 225 | |
| 226 | if (changed & TIOCM_RI) |
| 227 | sport->port.icount.rng++; |
| 228 | if (changed & TIOCM_DSR) |
| 229 | sport->port.icount.dsr++; |
| 230 | if (changed & TIOCM_CAR) |
| 231 | uart_handle_dcd_change(&sport->port, status & TIOCM_CAR); |
| 232 | if (changed & TIOCM_CTS) |
| 233 | uart_handle_cts_change(&sport->port, status & TIOCM_CTS); |
| 234 | |
| 235 | wake_up_interruptible(&sport->port.info->delta_msr_wait); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * This is our per-port timeout handler, for checking the |
| 240 | * modem status signals. |
| 241 | */ |
| 242 | static void imx_timeout(unsigned long data) |
| 243 | { |
| 244 | struct imx_port *sport = (struct imx_port *)data; |
| 245 | unsigned long flags; |
| 246 | |
| 247 | if (sport->port.info) { |
| 248 | spin_lock_irqsave(&sport->port.lock, flags); |
| 249 | imx_mctrl_check(sport); |
| 250 | spin_unlock_irqrestore(&sport->port.lock, flags); |
| 251 | |
| 252 | mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * interrupts disabled on entry |
| 258 | */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 259 | static void imx_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
| 261 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 262 | unsigned long temp; |
| 263 | |
| 264 | temp = readl(sport->port.membase + UCR1); |
| 265 | writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * interrupts disabled on entry |
| 270 | */ |
| 271 | static void imx_stop_rx(struct uart_port *port) |
| 272 | { |
| 273 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 274 | unsigned long temp; |
| 275 | |
| 276 | temp = readl(sport->port.membase + UCR2); |
| 277 | writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Set the modem control timer to fire immediately. |
| 282 | */ |
| 283 | static void imx_enable_ms(struct uart_port *port) |
| 284 | { |
| 285 | struct imx_port *sport = (struct imx_port *)port; |
| 286 | |
| 287 | mod_timer(&sport->timer, jiffies); |
| 288 | } |
| 289 | |
| 290 | static inline void imx_transmit_buffer(struct imx_port *sport) |
| 291 | { |
| 292 | struct circ_buf *xmit = &sport->port.info->xmit; |
| 293 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 294 | while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* send xmit->buf[xmit->tail] |
| 296 | * out the port here */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 297 | writel(xmit->buf[xmit->tail], sport->port.membase + URTX0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | xmit->tail = (xmit->tail + 1) & |
| 299 | (UART_XMIT_SIZE - 1); |
| 300 | sport->port.icount.tx++; |
| 301 | if (uart_circ_empty(xmit)) |
| 302 | break; |
Sascha Hauer | 8c0b254 | 2007-02-05 16:10:16 -0800 | [diff] [blame] | 303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | if (uart_circ_empty(xmit)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 306 | imx_stop_tx(&sport->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * interrupts disabled on entry |
| 311 | */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 312 | static void imx_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 315 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 317 | temp = readl(sport->port.membase + UCR1); |
| 318 | writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 320 | if (readl(sport->port.membase + UTS) & UTS_TXEMPTY) |
| 321 | imx_transmit_buffer(sport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 324 | static irqreturn_t imx_rtsint(int irq, void *dev_id) |
Sascha Hauer | ceca629 | 2005-10-12 19:58:08 +0100 | [diff] [blame] | 325 | { |
Jeff Garzik | 15aafa2 | 2008-02-06 01:36:20 -0800 | [diff] [blame] | 326 | struct imx_port *sport = dev_id; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 327 | unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS; |
Sascha Hauer | ceca629 | 2005-10-12 19:58:08 +0100 | [diff] [blame] | 328 | unsigned long flags; |
| 329 | |
| 330 | spin_lock_irqsave(&sport->port.lock, flags); |
| 331 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 332 | writel(USR1_RTSD, sport->port.membase + USR1); |
Sascha Hauer | ceca629 | 2005-10-12 19:58:08 +0100 | [diff] [blame] | 333 | uart_handle_cts_change(&sport->port, !!val); |
| 334 | wake_up_interruptible(&sport->port.info->delta_msr_wait); |
| 335 | |
| 336 | spin_unlock_irqrestore(&sport->port.lock, flags); |
| 337 | return IRQ_HANDLED; |
| 338 | } |
| 339 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 340 | static irqreturn_t imx_txint(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Jeff Garzik | 15aafa2 | 2008-02-06 01:36:20 -0800 | [diff] [blame] | 342 | struct imx_port *sport = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | struct circ_buf *xmit = &sport->port.info->xmit; |
| 344 | unsigned long flags; |
| 345 | |
| 346 | spin_lock_irqsave(&sport->port.lock,flags); |
| 347 | if (sport->port.x_char) |
| 348 | { |
| 349 | /* Send next char */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 350 | writel(sport->port.x_char, sport->port.membase + URTX0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | goto out; |
| 352 | } |
| 353 | |
| 354 | if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 355 | imx_stop_tx(&sport->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | imx_transmit_buffer(sport); |
| 360 | |
| 361 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 362 | uart_write_wakeup(&sport->port); |
| 363 | |
| 364 | out: |
| 365 | spin_unlock_irqrestore(&sport->port.lock,flags); |
| 366 | return IRQ_HANDLED; |
| 367 | } |
| 368 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 369 | static irqreturn_t imx_rxint(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct imx_port *sport = dev_id; |
| 372 | unsigned int rx,flg,ignored = 0; |
Takashi Iwai | a88487c | 2008-07-16 21:54:42 +0100 | [diff] [blame] | 373 | struct tty_struct *tty = sport->port.info->port.tty; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 374 | unsigned long flags, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | spin_lock_irqsave(&sport->port.lock,flags); |
| 377 | |
Sascha Hauer | 0d3c393 | 2008-04-17 08:43:14 +0100 | [diff] [blame] | 378 | while (readl(sport->port.membase + USR2) & USR2_RDR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | flg = TTY_NORMAL; |
| 380 | sport->port.icount.rx++; |
| 381 | |
Sascha Hauer | 0d3c393 | 2008-04-17 08:43:14 +0100 | [diff] [blame] | 382 | rx = readl(sport->port.membase + URXD0); |
| 383 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 384 | temp = readl(sport->port.membase + USR2); |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 385 | if (temp & USR2_BRCD) { |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 386 | writel(temp | USR2_BRCD, sport->port.membase + USR2); |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 387 | if (uart_handle_break(&sport->port)) |
| 388 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | if (uart_handle_sysrq_char |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 392 | (&sport->port, (unsigned char)rx)) |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 393 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 395 | if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) { |
| 396 | if (rx & URXD_PRERR) |
| 397 | sport->port.icount.parity++; |
| 398 | else if (rx & URXD_FRMERR) |
| 399 | sport->port.icount.frame++; |
| 400 | if (rx & URXD_OVRRUN) |
| 401 | sport->port.icount.overrun++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 403 | if (rx & sport->port.ignore_status_mask) { |
| 404 | if (++ignored > 100) |
| 405 | goto out; |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | rx &= sport->port.read_status_mask; |
| 410 | |
| 411 | if (rx & URXD_PRERR) |
| 412 | flg = TTY_PARITY; |
| 413 | else if (rx & URXD_FRMERR) |
| 414 | flg = TTY_FRAME; |
| 415 | if (rx & URXD_OVRRUN) |
| 416 | flg = TTY_OVERRUN; |
| 417 | |
| 418 | #ifdef SUPPORT_SYSRQ |
| 419 | sport->port.sysrq = 0; |
| 420 | #endif |
| 421 | } |
| 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | tty_insert_flip_char(tty, rx, flg); |
Sascha Hauer | 864eeed | 2008-04-17 08:39:22 +0100 | [diff] [blame] | 424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | out: |
| 427 | spin_unlock_irqrestore(&sport->port.lock,flags); |
| 428 | tty_flip_buffer_push(tty); |
| 429 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 432 | static irqreturn_t imx_int(int irq, void *dev_id) |
| 433 | { |
| 434 | struct imx_port *sport = dev_id; |
| 435 | unsigned int sts; |
| 436 | |
| 437 | sts = readl(sport->port.membase + USR1); |
| 438 | |
| 439 | if (sts & USR1_RRDY) |
| 440 | imx_rxint(irq, dev_id); |
| 441 | |
| 442 | if (sts & USR1_TRDY && |
| 443 | readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) |
| 444 | imx_txint(irq, dev_id); |
| 445 | |
Marc Kleine-Budde | 9fbe604 | 2008-07-28 21:26:01 +0200 | [diff] [blame] | 446 | if (sts & USR1_RTSD) |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 447 | imx_rtsint(irq, dev_id); |
| 448 | |
| 449 | return IRQ_HANDLED; |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Return TIOCSER_TEMT when transmitter is not busy. |
| 454 | */ |
| 455 | static unsigned int imx_tx_empty(struct uart_port *port) |
| 456 | { |
| 457 | struct imx_port *sport = (struct imx_port *)port; |
| 458 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 459 | return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 462 | /* |
| 463 | * We have a modem side uart, so the meanings of RTS and CTS are inverted. |
| 464 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | static unsigned int imx_get_mctrl(struct uart_port *port) |
| 466 | { |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 467 | struct imx_port *sport = (struct imx_port *)port; |
| 468 | unsigned int tmp = TIOCM_DSR | TIOCM_CAR; |
| 469 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 470 | if (readl(sport->port.membase + USR1) & USR1_RTSS) |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 471 | tmp |= TIOCM_CTS; |
| 472 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 473 | if (readl(sport->port.membase + UCR2) & UCR2_CTS) |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 474 | tmp |= TIOCM_RTS; |
| 475 | |
| 476 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 480 | { |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 481 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 482 | unsigned long temp; |
| 483 | |
| 484 | temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS; |
Sascha Hauer | 0f302dc | 2005-08-31 21:48:47 +0100 | [diff] [blame] | 485 | |
| 486 | if (mctrl & TIOCM_RTS) |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 487 | temp |= UCR2_CTS; |
| 488 | |
| 489 | writel(temp, sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Interrupts always disabled. |
| 494 | */ |
| 495 | static void imx_break_ctl(struct uart_port *port, int break_state) |
| 496 | { |
| 497 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 498 | unsigned long flags, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | spin_lock_irqsave(&sport->port.lock, flags); |
| 501 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 502 | temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if ( break_state != 0 ) |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 505 | temp |= UCR1_SNDBRK; |
| 506 | |
| 507 | writel(temp, sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | spin_unlock_irqrestore(&sport->port.lock, flags); |
| 510 | } |
| 511 | |
| 512 | #define TXTL 2 /* reset default */ |
| 513 | #define RXTL 1 /* reset default */ |
| 514 | |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 515 | static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode) |
| 516 | { |
| 517 | unsigned int val; |
| 518 | unsigned int ufcr_rfdiv; |
| 519 | |
| 520 | /* set receiver / transmitter trigger level. |
| 521 | * RFDIV is set such way to satisfy requested uartclk value |
| 522 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 523 | val = TXTL << 10 | RXTL; |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 524 | ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2) |
| 525 | / sport->port.uartclk; |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 526 | |
| 527 | if(!ufcr_rfdiv) |
| 528 | ufcr_rfdiv = 1; |
| 529 | |
| 530 | if(ufcr_rfdiv >= 7) |
| 531 | ufcr_rfdiv = 6; |
| 532 | else |
| 533 | ufcr_rfdiv = 6 - ufcr_rfdiv; |
| 534 | |
| 535 | val |= UFCR_RFDIV & (ufcr_rfdiv << 7); |
| 536 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 537 | writel(val, sport->port.membase + UFCR); |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | static int imx_startup(struct uart_port *port) |
| 543 | { |
| 544 | struct imx_port *sport = (struct imx_port *)port; |
| 545 | int retval; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 546 | unsigned long flags, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 548 | imx_setup_ufcr(sport, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* disable the DREN bit (Data Ready interrupt enable) before |
| 551 | * requesting IRQs |
| 552 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 553 | temp = readl(sport->port.membase + UCR4); |
| 554 | writel(temp & ~UCR4_DREN, sport->port.membase + UCR4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 557 | * Allocate the IRQ(s) i.MX1 has three interrupts whereas later |
| 558 | * chips only have one interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | */ |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 560 | if (sport->txirq > 0) { |
| 561 | retval = request_irq(sport->rxirq, imx_rxint, 0, |
| 562 | DRIVER_NAME, sport); |
| 563 | if (retval) |
| 564 | goto error_out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 566 | retval = request_irq(sport->txirq, imx_txint, 0, |
| 567 | DRIVER_NAME, sport); |
| 568 | if (retval) |
| 569 | goto error_out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 571 | retval = request_irq(sport->rtsirq, imx_rtsint, |
| 572 | (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 : |
Pavel Pisa | d7ea10d | 2007-02-05 16:10:20 -0800 | [diff] [blame] | 573 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 574 | DRIVER_NAME, sport); |
| 575 | if (retval) |
| 576 | goto error_out3; |
| 577 | } else { |
| 578 | retval = request_irq(sport->port.irq, imx_int, 0, |
| 579 | DRIVER_NAME, sport); |
| 580 | if (retval) { |
| 581 | free_irq(sport->port.irq, sport); |
| 582 | goto error_out1; |
| 583 | } |
| 584 | } |
Sascha Hauer | ceca629 | 2005-10-12 19:58:08 +0100 | [diff] [blame] | 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | /* |
| 587 | * Finally, clear and enable interrupts |
| 588 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 589 | writel(USR1_RTSD, sport->port.membase + USR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 591 | temp = readl(sport->port.membase + UCR1); |
Sascha Hauer | 789d525 | 2008-04-17 08:44:47 +0100 | [diff] [blame] | 592 | temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 593 | writel(temp, sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 595 | temp = readl(sport->port.membase + UCR2); |
| 596 | temp |= (UCR2_RXEN | UCR2_TXEN); |
| 597 | writel(temp, sport->port.membase + UCR2); |
| 598 | |
Marc Kleine-Budde | 4411805 | 2008-07-28 12:10:34 +0200 | [diff] [blame] | 599 | #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3 |
| 600 | temp = readl(sport->port.membase + UCR3); |
| 601 | temp |= UCR3_RXDMUXSEL; |
| 602 | writel(temp, sport->port.membase + UCR3); |
| 603 | #endif |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | /* |
| 606 | * Enable modem status interrupts |
| 607 | */ |
| 608 | spin_lock_irqsave(&sport->port.lock,flags); |
| 609 | imx_enable_ms(&sport->port); |
| 610 | spin_unlock_irqrestore(&sport->port.lock,flags); |
| 611 | |
| 612 | return 0; |
| 613 | |
Sascha Hauer | ceca629 | 2005-10-12 19:58:08 +0100 | [diff] [blame] | 614 | error_out3: |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 615 | if (sport->txirq) |
| 616 | free_irq(sport->txirq, sport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | error_out2: |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 618 | if (sport->rxirq) |
| 619 | free_irq(sport->rxirq, sport); |
Sascha Hauer | 86371d0 | 2005-10-10 10:17:42 +0100 | [diff] [blame] | 620 | error_out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return retval; |
| 622 | } |
| 623 | |
| 624 | static void imx_shutdown(struct uart_port *port) |
| 625 | { |
| 626 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 627 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Stop our timer. |
| 631 | */ |
| 632 | del_timer_sync(&sport->timer); |
| 633 | |
| 634 | /* |
| 635 | * Free the interrupts |
| 636 | */ |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 637 | if (sport->txirq > 0) { |
| 638 | free_irq(sport->rtsirq, sport); |
| 639 | free_irq(sport->txirq, sport); |
| 640 | free_irq(sport->rxirq, sport); |
| 641 | } else |
| 642 | free_irq(sport->port.irq, sport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Disable all interrupts, port and break condition. |
| 646 | */ |
| 647 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 648 | temp = readl(sport->port.membase + UCR1); |
| 649 | temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN); |
| 650 | writel(temp, sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 654 | imx_set_termios(struct uart_port *port, struct ktermios *termios, |
| 655 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct imx_port *sport = (struct imx_port *)port; |
| 658 | unsigned long flags; |
| 659 | unsigned int ucr2, old_ucr1, old_txrxen, baud, quot; |
| 660 | unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8; |
Sascha Hauer | 036bb15 | 2008-07-05 10:02:44 +0200 | [diff] [blame] | 661 | unsigned int div, num, denom, ufcr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * If we don't support modem control lines, don't allow |
| 665 | * these to be set. |
| 666 | */ |
| 667 | if (0) { |
| 668 | termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR); |
| 669 | termios->c_cflag |= CLOCAL; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * We only support CS7 and CS8. |
| 674 | */ |
| 675 | while ((termios->c_cflag & CSIZE) != CS7 && |
| 676 | (termios->c_cflag & CSIZE) != CS8) { |
| 677 | termios->c_cflag &= ~CSIZE; |
| 678 | termios->c_cflag |= old_csize; |
| 679 | old_csize = CS8; |
| 680 | } |
| 681 | |
| 682 | if ((termios->c_cflag & CSIZE) == CS8) |
| 683 | ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS; |
| 684 | else |
| 685 | ucr2 = UCR2_SRST | UCR2_IRTS; |
| 686 | |
| 687 | if (termios->c_cflag & CRTSCTS) { |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 688 | if( sport->have_rtscts ) { |
| 689 | ucr2 &= ~UCR2_IRTS; |
| 690 | ucr2 |= UCR2_CTSC; |
| 691 | } else { |
| 692 | termios->c_cflag &= ~CRTSCTS; |
| 693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | if (termios->c_cflag & CSTOPB) |
| 697 | ucr2 |= UCR2_STPB; |
| 698 | if (termios->c_cflag & PARENB) { |
| 699 | ucr2 |= UCR2_PREN; |
Matt Reimer | 3261e36 | 2006-01-13 20:51:44 +0000 | [diff] [blame] | 700 | if (termios->c_cflag & PARODD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | ucr2 |= UCR2_PROE; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Ask the core to calculate the divisor for us. |
| 706 | */ |
Sascha Hauer | 036bb15 | 2008-07-05 10:02:44 +0200 | [diff] [blame] | 707 | baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | quot = uart_get_divisor(port, baud); |
| 709 | |
| 710 | spin_lock_irqsave(&sport->port.lock, flags); |
| 711 | |
| 712 | sport->port.read_status_mask = 0; |
| 713 | if (termios->c_iflag & INPCK) |
| 714 | sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR); |
| 715 | if (termios->c_iflag & (BRKINT | PARMRK)) |
| 716 | sport->port.read_status_mask |= URXD_BRK; |
| 717 | |
| 718 | /* |
| 719 | * Characters to ignore |
| 720 | */ |
| 721 | sport->port.ignore_status_mask = 0; |
| 722 | if (termios->c_iflag & IGNPAR) |
| 723 | sport->port.ignore_status_mask |= URXD_PRERR; |
| 724 | if (termios->c_iflag & IGNBRK) { |
| 725 | sport->port.ignore_status_mask |= URXD_BRK; |
| 726 | /* |
| 727 | * If we're ignoring parity and break indicators, |
| 728 | * ignore overruns too (for real raw support). |
| 729 | */ |
| 730 | if (termios->c_iflag & IGNPAR) |
| 731 | sport->port.ignore_status_mask |= URXD_OVRRUN; |
| 732 | } |
| 733 | |
| 734 | del_timer_sync(&sport->timer); |
| 735 | |
| 736 | /* |
| 737 | * Update the per-port timeout. |
| 738 | */ |
| 739 | uart_update_timeout(port, termios->c_cflag, baud); |
| 740 | |
| 741 | /* |
| 742 | * disable interrupts and drain transmitter |
| 743 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 744 | old_ucr1 = readl(sport->port.membase + UCR1); |
| 745 | writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN), |
| 746 | sport->port.membase + UCR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 748 | while ( !(readl(sport->port.membase + USR2) & USR2_TXDC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | barrier(); |
| 750 | |
| 751 | /* then, disable everything */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 752 | old_txrxen = readl(sport->port.membase + UCR2); |
| 753 | writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN), |
| 754 | sport->port.membase + UCR2); |
| 755 | old_txrxen &= (UCR2_TXEN | UCR2_RXEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Sascha Hauer | 036bb15 | 2008-07-05 10:02:44 +0200 | [diff] [blame] | 757 | div = sport->port.uartclk / (baud * 16); |
| 758 | if (div > 7) |
| 759 | div = 7; |
| 760 | if (!div) |
| 761 | div = 1; |
| 762 | |
| 763 | num = baud; |
| 764 | denom = port->uartclk / div / 16; |
| 765 | |
| 766 | /* shift num and denom right until they fit into 16 bits */ |
| 767 | while (num > 0x10000 || denom > 0x10000) { |
| 768 | num >>= 1; |
| 769 | denom >>= 1; |
| 770 | } |
| 771 | if (num > 0) |
| 772 | num -= 1; |
| 773 | if (denom > 0) |
| 774 | denom -= 1; |
| 775 | |
| 776 | writel(num, sport->port.membase + UBIR); |
| 777 | writel(denom, sport->port.membase + UBMR); |
| 778 | |
| 779 | if (div == 7) |
| 780 | div = 6; /* 6 in RFDIV means divide by 7 */ |
| 781 | else |
| 782 | div = 6 - div; |
| 783 | |
| 784 | ufcr = readl(sport->port.membase + UFCR); |
| 785 | ufcr = (ufcr & (~UFCR_RFDIV)) | |
| 786 | (div << 7); |
| 787 | writel(ufcr, sport->port.membase + UFCR); |
| 788 | |
| 789 | #ifdef ONEMS |
| 790 | writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS); |
| 791 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 793 | writel(old_ucr1, sport->port.membase + UCR1); |
| 794 | |
| 795 | /* set the parity, stop bits and data size */ |
| 796 | writel(ucr2 | old_txrxen, sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | if (UART_ENABLE_MS(&sport->port, termios->c_cflag)) |
| 799 | imx_enable_ms(&sport->port); |
| 800 | |
| 801 | spin_unlock_irqrestore(&sport->port.lock, flags); |
| 802 | } |
| 803 | |
| 804 | static const char *imx_type(struct uart_port *port) |
| 805 | { |
| 806 | struct imx_port *sport = (struct imx_port *)port; |
| 807 | |
| 808 | return sport->port.type == PORT_IMX ? "IMX" : NULL; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Release the memory region(s) being used by 'port'. |
| 813 | */ |
| 814 | static void imx_release_port(struct uart_port *port) |
| 815 | { |
Sascha Hauer | 3d45444 | 2008-04-17 08:47:32 +0100 | [diff] [blame] | 816 | struct platform_device *pdev = to_platform_device(port->dev); |
| 817 | struct resource *mmres; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Sascha Hauer | 3d45444 | 2008-04-17 08:47:32 +0100 | [diff] [blame] | 819 | mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 820 | release_mem_region(mmres->start, mmres->end - mmres->start + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Request the memory region(s) being used by 'port'. |
| 825 | */ |
| 826 | static int imx_request_port(struct uart_port *port) |
| 827 | { |
Sascha Hauer | 3d45444 | 2008-04-17 08:47:32 +0100 | [diff] [blame] | 828 | struct platform_device *pdev = to_platform_device(port->dev); |
| 829 | struct resource *mmres; |
| 830 | void *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Sascha Hauer | 3d45444 | 2008-04-17 08:47:32 +0100 | [diff] [blame] | 832 | mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 833 | if (!mmres) |
| 834 | return -ENODEV; |
| 835 | |
| 836 | ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1, |
| 837 | "imx-uart"); |
| 838 | |
| 839 | return ret ? 0 : -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Configure/autoconfigure the port. |
| 844 | */ |
| 845 | static void imx_config_port(struct uart_port *port, int flags) |
| 846 | { |
| 847 | struct imx_port *sport = (struct imx_port *)port; |
| 848 | |
| 849 | if (flags & UART_CONFIG_TYPE && |
| 850 | imx_request_port(&sport->port) == 0) |
| 851 | sport->port.type = PORT_IMX; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Verify the new serial_struct (for TIOCSSERIAL). |
| 856 | * The only change we allow are to the flags and type, and |
| 857 | * even then only between PORT_IMX and PORT_UNKNOWN |
| 858 | */ |
| 859 | static int |
| 860 | imx_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 861 | { |
| 862 | struct imx_port *sport = (struct imx_port *)port; |
| 863 | int ret = 0; |
| 864 | |
| 865 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX) |
| 866 | ret = -EINVAL; |
| 867 | if (sport->port.irq != ser->irq) |
| 868 | ret = -EINVAL; |
| 869 | if (ser->io_type != UPIO_MEM) |
| 870 | ret = -EINVAL; |
| 871 | if (sport->port.uartclk / 16 != ser->baud_base) |
| 872 | ret = -EINVAL; |
| 873 | if ((void *)sport->port.mapbase != ser->iomem_base) |
| 874 | ret = -EINVAL; |
| 875 | if (sport->port.iobase != ser->port) |
| 876 | ret = -EINVAL; |
| 877 | if (ser->hub6 != 0) |
| 878 | ret = -EINVAL; |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | static struct uart_ops imx_pops = { |
| 883 | .tx_empty = imx_tx_empty, |
| 884 | .set_mctrl = imx_set_mctrl, |
| 885 | .get_mctrl = imx_get_mctrl, |
| 886 | .stop_tx = imx_stop_tx, |
| 887 | .start_tx = imx_start_tx, |
| 888 | .stop_rx = imx_stop_rx, |
| 889 | .enable_ms = imx_enable_ms, |
| 890 | .break_ctl = imx_break_ctl, |
| 891 | .startup = imx_startup, |
| 892 | .shutdown = imx_shutdown, |
| 893 | .set_termios = imx_set_termios, |
| 894 | .type = imx_type, |
| 895 | .release_port = imx_release_port, |
| 896 | .request_port = imx_request_port, |
| 897 | .config_port = imx_config_port, |
| 898 | .verify_port = imx_verify_port, |
| 899 | }; |
| 900 | |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 901 | static struct imx_port *imx_ports[UART_NR]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | #ifdef CONFIG_SERIAL_IMX_CONSOLE |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 904 | static void imx_console_putchar(struct uart_port *port, int ch) |
| 905 | { |
| 906 | struct imx_port *sport = (struct imx_port *)port; |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 907 | |
| 908 | while (readl(sport->port.membase + UTS) & UTS_TXFULL) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 909 | barrier(); |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 910 | |
| 911 | writel(ch, sport->port.membase + URTX0); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | /* |
| 915 | * Interrupts are disabled on entering |
| 916 | */ |
| 917 | static void |
| 918 | imx_console_write(struct console *co, const char *s, unsigned int count) |
| 919 | { |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 920 | struct imx_port *sport = imx_ports[co->index]; |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 921 | unsigned int old_ucr1, old_ucr2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | /* |
| 924 | * First, save UCR1/2 and then disable interrupts |
| 925 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 926 | old_ucr1 = readl(sport->port.membase + UCR1); |
| 927 | old_ucr2 = readl(sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 929 | writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) & |
| 930 | ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN), |
| 931 | sport->port.membase + UCR1); |
| 932 | |
| 933 | writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 935 | uart_console_write(&sport->port, s, count, imx_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
| 937 | /* |
| 938 | * Finally, wait for transmitter to become empty |
| 939 | * and restore UCR1/2 |
| 940 | */ |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 941 | while (!(readl(sport->port.membase + USR2) & USR2_TXDC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 943 | writel(old_ucr1, sport->port.membase + UCR1); |
| 944 | writel(old_ucr2, sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* |
| 948 | * If the port was already initialised (eg, by a boot loader), |
| 949 | * try to determine the current setup. |
| 950 | */ |
| 951 | static void __init |
| 952 | imx_console_get_options(struct imx_port *sport, int *baud, |
| 953 | int *parity, int *bits) |
| 954 | { |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 955 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 956 | if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | /* ok, the port was enabled */ |
| 958 | unsigned int ucr2, ubir,ubmr, uartclk; |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 959 | unsigned int baud_raw; |
| 960 | unsigned int ucfr_rfdiv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 962 | ucr2 = readl(sport->port.membase + UCR2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
| 964 | *parity = 'n'; |
| 965 | if (ucr2 & UCR2_PREN) { |
| 966 | if (ucr2 & UCR2_PROE) |
| 967 | *parity = 'o'; |
| 968 | else |
| 969 | *parity = 'e'; |
| 970 | } |
| 971 | |
| 972 | if (ucr2 & UCR2_WS) |
| 973 | *bits = 8; |
| 974 | else |
| 975 | *bits = 7; |
| 976 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 977 | ubir = readl(sport->port.membase + UBIR) & 0xffff; |
| 978 | ubmr = readl(sport->port.membase + UBMR) & 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
Sascha Hauer | ff4bfb2 | 2007-04-26 08:26:13 +0100 | [diff] [blame] | 980 | ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7; |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 981 | if (ucfr_rfdiv == 6) |
| 982 | ucfr_rfdiv = 7; |
| 983 | else |
| 984 | ucfr_rfdiv = 6 - ucfr_rfdiv; |
| 985 | |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 986 | uartclk = clk_get_rate(sport->clk); |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 987 | uartclk /= ucfr_rfdiv; |
| 988 | |
| 989 | { /* |
| 990 | * The next code provides exact computation of |
| 991 | * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1)) |
| 992 | * without need of float support or long long division, |
| 993 | * which would be required to prevent 32bit arithmetic overflow |
| 994 | */ |
| 995 | unsigned int mul = ubir + 1; |
| 996 | unsigned int div = 16 * (ubmr + 1); |
| 997 | unsigned int rem = uartclk % div; |
| 998 | |
| 999 | baud_raw = (uartclk / div) * mul; |
| 1000 | baud_raw += (rem * mul + div / 2) / div; |
| 1001 | *baud = (baud_raw + 50) / 100 * 100; |
| 1002 | } |
| 1003 | |
| 1004 | if(*baud != baud_raw) |
| 1005 | printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n", |
| 1006 | baud_raw, *baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | static int __init |
| 1011 | imx_console_setup(struct console *co, char *options) |
| 1012 | { |
| 1013 | struct imx_port *sport; |
| 1014 | int baud = 9600; |
| 1015 | int bits = 8; |
| 1016 | int parity = 'n'; |
| 1017 | int flow = 'n'; |
| 1018 | |
| 1019 | /* |
| 1020 | * Check whether an invalid uart number has been specified, and |
| 1021 | * if so, search for the first available port that does have |
| 1022 | * console support. |
| 1023 | */ |
| 1024 | if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports)) |
| 1025 | co->index = 0; |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1026 | sport = imx_ports[co->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (options) |
| 1029 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1030 | else |
| 1031 | imx_console_get_options(sport, &baud, &parity, &bits); |
| 1032 | |
Sascha Hauer | 587897f | 2005-04-29 22:46:40 +0100 | [diff] [blame] | 1033 | imx_setup_ufcr(sport, 0); |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | return uart_set_options(&sport->port, co, baud, parity, bits, flow); |
| 1036 | } |
| 1037 | |
Vincent Sanders | 9f4426d | 2005-10-01 22:56:34 +0100 | [diff] [blame] | 1038 | static struct uart_driver imx_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | static struct console imx_console = { |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 1040 | .name = DEV_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | .write = imx_console_write, |
| 1042 | .device = uart_console_device, |
| 1043 | .setup = imx_console_setup, |
| 1044 | .flags = CON_PRINTBUFFER, |
| 1045 | .index = -1, |
| 1046 | .data = &imx_reg, |
| 1047 | }; |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | #define IMX_CONSOLE &imx_console |
| 1050 | #else |
| 1051 | #define IMX_CONSOLE NULL |
| 1052 | #endif |
| 1053 | |
| 1054 | static struct uart_driver imx_reg = { |
| 1055 | .owner = THIS_MODULE, |
| 1056 | .driver_name = DRIVER_NAME, |
Sascha Hauer | e3d13ff | 2008-07-05 10:02:48 +0200 | [diff] [blame] | 1057 | .dev_name = DEV_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | .major = SERIAL_IMX_MAJOR, |
| 1059 | .minor = MINOR_START, |
| 1060 | .nr = ARRAY_SIZE(imx_ports), |
| 1061 | .cons = IMX_CONSOLE, |
| 1062 | }; |
| 1063 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1064 | static int serial_imx_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1066 | struct imx_port *sport = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1068 | if (sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | uart_suspend_port(&imx_reg, &sport->port); |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1074 | static int serial_imx_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1076 | struct imx_port *sport = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1078 | if (sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | uart_resume_port(&imx_reg, &sport->port); |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1084 | static int serial_imx_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1086 | struct imx_port *sport; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 1087 | struct imxuart_platform_data *pdata; |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1088 | void __iomem *base; |
| 1089 | int ret = 0; |
| 1090 | struct resource *res; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 1091 | |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1092 | sport = kzalloc(sizeof(*sport), GFP_KERNEL); |
| 1093 | if (!sport) |
| 1094 | return -ENOMEM; |
| 1095 | |
| 1096 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1097 | if (!res) { |
| 1098 | ret = -ENODEV; |
| 1099 | goto free; |
| 1100 | } |
| 1101 | |
| 1102 | base = ioremap(res->start, PAGE_SIZE); |
| 1103 | if (!base) { |
| 1104 | ret = -ENOMEM; |
| 1105 | goto free; |
| 1106 | } |
| 1107 | |
| 1108 | sport->port.dev = &pdev->dev; |
| 1109 | sport->port.mapbase = res->start; |
| 1110 | sport->port.membase = base; |
| 1111 | sport->port.type = PORT_IMX, |
| 1112 | sport->port.iotype = UPIO_MEM; |
| 1113 | sport->port.irq = platform_get_irq(pdev, 0); |
| 1114 | sport->rxirq = platform_get_irq(pdev, 0); |
| 1115 | sport->txirq = platform_get_irq(pdev, 1); |
| 1116 | sport->rtsirq = platform_get_irq(pdev, 2); |
| 1117 | sport->port.fifosize = 32; |
| 1118 | sport->port.ops = &imx_pops; |
| 1119 | sport->port.flags = UPF_BOOT_AUTOCONF; |
| 1120 | sport->port.line = pdev->id; |
| 1121 | init_timer(&sport->timer); |
| 1122 | sport->timer.function = imx_timeout; |
| 1123 | sport->timer.data = (unsigned long)sport; |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 1124 | |
Sascha Hauer | e65fb00 | 2009-02-16 14:29:10 +0100 | [diff] [blame] | 1125 | sport->clk = clk_get(&pdev->dev, "uart"); |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 1126 | if (IS_ERR(sport->clk)) { |
| 1127 | ret = PTR_ERR(sport->clk); |
| 1128 | goto unmap; |
| 1129 | } |
| 1130 | clk_enable(sport->clk); |
| 1131 | |
| 1132 | sport->port.uartclk = clk_get_rate(sport->clk); |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1133 | |
| 1134 | imx_ports[pdev->id] = sport; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 1135 | |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1136 | pdata = pdev->dev.platform_data; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 1137 | if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS)) |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1138 | sport->have_rtscts = 1; |
Sascha Hauer | 5b80234 | 2006-05-04 14:07:42 +0100 | [diff] [blame] | 1139 | |
Darius Augulis | c45e7d7 | 2008-09-02 10:19:29 +0200 | [diff] [blame] | 1140 | if (pdata->init) { |
| 1141 | ret = pdata->init(pdev); |
| 1142 | if (ret) |
| 1143 | goto clkput; |
| 1144 | } |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1145 | |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1146 | uart_add_one_port(&imx_reg, &sport->port); |
| 1147 | platform_set_drvdata(pdev, &sport->port); |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | return 0; |
Darius Augulis | c45e7d7 | 2008-09-02 10:19:29 +0200 | [diff] [blame] | 1150 | clkput: |
| 1151 | clk_put(sport->clk); |
| 1152 | clk_disable(sport->clk); |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 1153 | unmap: |
| 1154 | iounmap(sport->port.membase); |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1155 | free: |
| 1156 | kfree(sport); |
| 1157 | |
| 1158 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1161 | static int serial_imx_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | { |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1163 | struct imxuart_platform_data *pdata; |
| 1164 | struct imx_port *sport = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1166 | pdata = pdev->dev.platform_data; |
| 1167 | |
| 1168 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 1170 | if (sport) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | uart_remove_one_port(&imx_reg, &sport->port); |
Sascha Hauer | 38a41fd | 2008-07-05 10:02:46 +0200 | [diff] [blame] | 1172 | clk_put(sport->clk); |
| 1173 | } |
| 1174 | |
| 1175 | clk_disable(sport->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Sascha Hauer | 2582d8c | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1177 | if (pdata->exit) |
| 1178 | pdata->exit(pdev); |
| 1179 | |
Sascha Hauer | dbff4e9 | 2008-07-05 10:02:45 +0200 | [diff] [blame] | 1180 | iounmap(sport->port.membase); |
| 1181 | kfree(sport); |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1186 | static struct platform_driver serial_imx_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | .probe = serial_imx_probe, |
| 1188 | .remove = serial_imx_remove, |
| 1189 | |
| 1190 | .suspend = serial_imx_suspend, |
| 1191 | .resume = serial_imx_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1192 | .driver = { |
| 1193 | .name = "imx-uart", |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 1194 | .owner = THIS_MODULE, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1195 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | }; |
| 1197 | |
| 1198 | static int __init imx_serial_init(void) |
| 1199 | { |
| 1200 | int ret; |
| 1201 | |
| 1202 | printk(KERN_INFO "Serial: IMX driver\n"); |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | ret = uart_register_driver(&imx_reg); |
| 1205 | if (ret) |
| 1206 | return ret; |
| 1207 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1208 | ret = platform_driver_register(&serial_imx_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | if (ret != 0) |
| 1210 | uart_unregister_driver(&imx_reg); |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static void __exit imx_serial_exit(void) |
| 1216 | { |
Russell King | c889b89 | 2005-11-21 17:05:21 +0000 | [diff] [blame] | 1217 | platform_driver_unregister(&serial_imx_driver); |
Sascha Hauer | 4b300c3 | 2007-07-17 13:35:46 +0100 | [diff] [blame] | 1218 | uart_unregister_driver(&imx_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | module_init(imx_serial_init); |
| 1222 | module_exit(imx_serial_exit); |
| 1223 | |
| 1224 | MODULE_AUTHOR("Sascha Hauer"); |
| 1225 | MODULE_DESCRIPTION("IMX generic serial port driver"); |
| 1226 | MODULE_LICENSE("GPL"); |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 1227 | MODULE_ALIAS("platform:imx-uart"); |