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