Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1 | /* |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 2 | * linux/drivers/char/atmel_serial.c |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 3 | * |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 4 | * Driver for Atmel AT91 / AT32 Serial ports |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Rick Bronson |
| 6 | * |
| 7 | * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. |
| 8 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/tty.h> |
| 27 | #include <linux/ioport.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/serial.h> |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 31 | #include <linux/clk.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 32 | #include <linux/console.h> |
| 33 | #include <linux/sysrq.h> |
| 34 | #include <linux/tty_flip.h> |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 35 | #include <linux/platform_device.h> |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 36 | #include <linux/atmel_pdc.h> |
Guennadi Liakhovetski | fa3218d | 2008-01-29 15:43:13 +0100 | [diff] [blame] | 37 | #include <linux/atmel_serial.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 38 | |
| 39 | #include <asm/io.h> |
| 40 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 41 | #include <asm/mach/serial_at91.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 42 | #include <asm/arch/board.h> |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 43 | |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 44 | #ifdef CONFIG_ARM |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 45 | #include <asm/arch/cpu.h> |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 46 | #include <asm/arch/gpio.h> |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 47 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 48 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 49 | #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 50 | #define SUPPORT_SYSRQ |
| 51 | #endif |
| 52 | |
| 53 | #include <linux/serial_core.h> |
| 54 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_SERIAL_ATMEL_TTYAT |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 56 | |
| 57 | /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we |
| 58 | * should coexist with the 8250 driver, such as if we have an external 16C550 |
| 59 | * UART. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 60 | #define SERIAL_ATMEL_MAJOR 204 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 61 | #define MINOR_START 154 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 62 | #define ATMEL_DEVICENAME "ttyAT" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 63 | |
| 64 | #else |
| 65 | |
| 66 | /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port |
| 67 | * name, but it is legally reserved for the 8250 driver. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 68 | #define SERIAL_ATMEL_MAJOR TTY_MAJOR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 69 | #define MINOR_START 64 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 70 | #define ATMEL_DEVICENAME "ttyS" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 71 | |
| 72 | #endif |
| 73 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 74 | #define ATMEL_ISR_PASS_LIMIT 256 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 75 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 76 | /* UART registers. CR is write-only, hence no GET macro */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 77 | #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) |
| 78 | #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) |
| 79 | #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) |
| 80 | #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) |
| 81 | #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) |
| 82 | #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) |
| 83 | #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) |
| 84 | #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) |
| 85 | #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) |
| 86 | #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) |
| 87 | #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) |
| 88 | #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 89 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 90 | /* PDC registers */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 91 | #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) |
| 92 | #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 93 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 94 | #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) |
| 95 | #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) |
| 96 | #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) |
| 97 | #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) |
| 98 | #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 99 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 100 | #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) |
| 101 | #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 102 | |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 103 | static int (*atmel_open_hook)(struct uart_port *); |
| 104 | static void (*atmel_close_hook)(struct uart_port *); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 105 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 106 | /* |
| 107 | * We wrap our port structure around the generic uart_port. |
| 108 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 109 | struct atmel_uart_port { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 110 | struct uart_port uart; /* uart */ |
| 111 | struct clk *clk; /* uart clock */ |
| 112 | unsigned short suspended; /* is port suspended? */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 113 | int break_active; /* break being received */ |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 116 | static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 117 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 118 | #ifdef SUPPORT_SYSRQ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 119 | static struct console atmel_console; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | /* |
| 123 | * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. |
| 124 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 125 | static u_int atmel_tx_empty(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 126 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 127 | return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Set state of the modem control output lines |
| 132 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 133 | static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 134 | { |
| 135 | unsigned int control = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 136 | unsigned int mode; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 137 | |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 138 | #ifdef CONFIG_ARCH_AT91RM9200 |
Andrew Victor | 79da7a6 | 2006-11-30 11:58:14 +0100 | [diff] [blame] | 139 | if (cpu_is_at91rm9200()) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 140 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 141 | * AT91RM9200 Errata #39: RTS0 is not internally connected |
| 142 | * to PA21. We need to drive the pin manually. |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 143 | */ |
Andrew Victor | 7272991 | 2006-09-27 09:44:11 +0100 | [diff] [blame] | 144 | if (port->mapbase == AT91RM9200_BASE_US0) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 145 | if (mctrl & TIOCM_RTS) |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 146 | at91_set_gpio_value(AT91_PIN_PA21, 0); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 147 | else |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 148 | at91_set_gpio_value(AT91_PIN_PA21, 1); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 149 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 150 | } |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 151 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 152 | |
| 153 | if (mctrl & TIOCM_RTS) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 154 | control |= ATMEL_US_RTSEN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 155 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 156 | control |= ATMEL_US_RTSDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 157 | |
| 158 | if (mctrl & TIOCM_DTR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 159 | control |= ATMEL_US_DTREN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 160 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 161 | control |= ATMEL_US_DTRDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 162 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 163 | UART_PUT_CR(port, control); |
| 164 | |
| 165 | /* Local loopback mode? */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 166 | mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 167 | if (mctrl & TIOCM_LOOP) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 168 | mode |= ATMEL_US_CHMODE_LOC_LOOP; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 169 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 170 | mode |= ATMEL_US_CHMODE_NORMAL; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 171 | UART_PUT_MR(port, mode); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Get state of the modem control input lines |
| 176 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 177 | static u_int atmel_get_mctrl(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 178 | { |
| 179 | unsigned int status, ret = 0; |
| 180 | |
| 181 | status = UART_GET_CSR(port); |
| 182 | |
| 183 | /* |
| 184 | * The control signals are active low. |
| 185 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 186 | if (!(status & ATMEL_US_DCD)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 187 | ret |= TIOCM_CD; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 188 | if (!(status & ATMEL_US_CTS)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 189 | ret |= TIOCM_CTS; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 190 | if (!(status & ATMEL_US_DSR)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 191 | ret |= TIOCM_DSR; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 192 | if (!(status & ATMEL_US_RI)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 193 | ret |= TIOCM_RI; |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Stop transmitting. |
| 200 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 201 | static void atmel_stop_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 202 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 203 | UART_PUT_IDR(port, ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Start transmitting. |
| 208 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 209 | static void atmel_start_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 210 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 211 | UART_PUT_IER(port, ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Stop receiving - port is in process of being closed. |
| 216 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 217 | static void atmel_stop_rx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 218 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 219 | UART_PUT_IDR(port, ATMEL_US_RXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Enable modem status interrupts |
| 224 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 225 | static void atmel_enable_ms(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 226 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 227 | UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC |
| 228 | | ATMEL_US_DCDIC | ATMEL_US_CTSIC); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Control the transmission of a break signal |
| 233 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 234 | static void atmel_break_ctl(struct uart_port *port, int break_state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 235 | { |
| 236 | if (break_state != 0) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 237 | UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 238 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 239 | UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Characters received (called from interrupt handler) |
| 244 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 245 | static void atmel_rx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 246 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 247 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 248 | struct tty_struct *tty = port->info->tty; |
| 249 | unsigned int status, ch, flg; |
| 250 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 251 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 252 | while (status & ATMEL_US_RXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 253 | ch = UART_GET_CHAR(port); |
| 254 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 255 | port->icount.rx++; |
| 256 | |
| 257 | flg = TTY_NORMAL; |
| 258 | |
| 259 | /* |
| 260 | * note that the error handling code is |
| 261 | * out of the main execution path |
| 262 | */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 263 | if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME |
| 264 | | ATMEL_US_OVRE | ATMEL_US_RXBRK) |
| 265 | || atmel_port->break_active)) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 266 | /* clear error */ |
| 267 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 268 | if (status & ATMEL_US_RXBRK |
| 269 | && !atmel_port->break_active) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 270 | /* ignore side-effect */ |
| 271 | status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 272 | port->icount.brk++; |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 273 | atmel_port->break_active = 1; |
| 274 | UART_PUT_IER(port, ATMEL_US_RXBRK); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 275 | if (uart_handle_break(port)) |
| 276 | goto ignore_char; |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 277 | } else { |
| 278 | /* |
| 279 | * This is either the end-of-break |
| 280 | * condition or we've received at |
| 281 | * least one character without RXBRK |
| 282 | * being set. In both cases, the next |
| 283 | * RXBRK will indicate start-of-break. |
| 284 | */ |
| 285 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 286 | status &= ~ATMEL_US_RXBRK; |
| 287 | atmel_port->break_active = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 288 | } |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 289 | if (status & ATMEL_US_PARE) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 290 | port->icount.parity++; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 291 | if (status & ATMEL_US_FRAME) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 292 | port->icount.frame++; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 293 | if (status & ATMEL_US_OVRE) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 294 | port->icount.overrun++; |
| 295 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 296 | status &= port->read_status_mask; |
| 297 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 298 | if (status & ATMEL_US_RXBRK) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 299 | flg = TTY_BREAK; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 300 | else if (status & ATMEL_US_PARE) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 301 | flg = TTY_PARITY; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 302 | else if (status & ATMEL_US_FRAME) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 303 | flg = TTY_FRAME; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 306 | if (uart_handle_sysrq_char(port, ch)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 307 | goto ignore_char; |
| 308 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 309 | uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 310 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 311 | ignore_char: |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 312 | status = UART_GET_CSR(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | tty_flip_buffer_push(tty); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Transmit characters (called from interrupt handler) |
| 320 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 321 | static void atmel_tx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 322 | { |
| 323 | struct circ_buf *xmit = &port->info->xmit; |
| 324 | |
| 325 | if (port->x_char) { |
| 326 | UART_PUT_CHAR(port, port->x_char); |
| 327 | port->icount.tx++; |
| 328 | port->x_char = 0; |
| 329 | return; |
| 330 | } |
| 331 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 332 | atmel_stop_tx(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 336 | while (UART_GET_CSR(port) & ATMEL_US_TXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 337 | UART_PUT_CHAR(port, xmit->buf[xmit->tail]); |
| 338 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 339 | port->icount.tx++; |
| 340 | if (uart_circ_empty(xmit)) |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 345 | uart_write_wakeup(port); |
| 346 | |
| 347 | if (uart_circ_empty(xmit)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 348 | atmel_stop_tx(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 352 | * receive interrupt handler. |
| 353 | */ |
| 354 | static void |
| 355 | atmel_handle_receive(struct uart_port *port, unsigned int pending) |
| 356 | { |
| 357 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
| 358 | |
| 359 | /* Interrupt receive */ |
| 360 | if (pending & ATMEL_US_RXRDY) |
| 361 | atmel_rx_chars(port); |
| 362 | else if (pending & ATMEL_US_RXBRK) { |
| 363 | /* |
| 364 | * End of break detected. If it came along with a |
| 365 | * character, atmel_rx_chars will handle it. |
| 366 | */ |
| 367 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 368 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 369 | atmel_port->break_active = 0; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * transmit interrupt handler. |
| 375 | */ |
| 376 | static void |
| 377 | atmel_handle_transmit(struct uart_port *port, unsigned int pending) |
| 378 | { |
| 379 | /* Interrupt transmit */ |
| 380 | if (pending & ATMEL_US_TXRDY) |
| 381 | atmel_tx_chars(port); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * status flags interrupt handler. |
| 386 | */ |
| 387 | static void |
| 388 | atmel_handle_status(struct uart_port *port, unsigned int pending, |
| 389 | unsigned int status) |
| 390 | { |
| 391 | /* TODO: All reads to CSR will clear these interrupts! */ |
| 392 | if (pending & ATMEL_US_RIIC) |
| 393 | port->icount.rng++; |
| 394 | if (pending & ATMEL_US_DSRIC) |
| 395 | port->icount.dsr++; |
| 396 | if (pending & ATMEL_US_DCDIC) |
| 397 | uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); |
| 398 | if (pending & ATMEL_US_CTSIC) |
| 399 | uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); |
| 400 | if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC |
| 401 | | ATMEL_US_CTSIC)) |
| 402 | wake_up_interruptible(&port->info->delta_msr_wait); |
| 403 | } |
| 404 | |
| 405 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 406 | * Interrupt handler |
| 407 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 408 | static irqreturn_t atmel_interrupt(int irq, void *dev_id) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 409 | { |
| 410 | struct uart_port *port = dev_id; |
| 411 | unsigned int status, pending, pass_counter = 0; |
| 412 | |
| 413 | status = UART_GET_CSR(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 414 | pending = status & UART_GET_IMR(port); |
| 415 | while (pending) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 416 | atmel_handle_receive(port, pending); |
| 417 | atmel_handle_status(port, pending, status); |
| 418 | atmel_handle_transmit(port, pending); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 419 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 420 | if (pass_counter++ > ATMEL_ISR_PASS_LIMIT) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 421 | break; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 422 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 423 | status = UART_GET_CSR(port); |
| 424 | pending = status & UART_GET_IMR(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 425 | } |
| 426 | return IRQ_HANDLED; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Perform initialization and enable port for reception |
| 431 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 432 | static int atmel_startup(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 433 | { |
| 434 | int retval; |
| 435 | |
| 436 | /* |
| 437 | * Ensure that no interrupts are enabled otherwise when |
| 438 | * request_irq() is called we could get stuck trying to |
| 439 | * handle an unexpected interrupt |
| 440 | */ |
| 441 | UART_PUT_IDR(port, -1); |
| 442 | |
| 443 | /* |
| 444 | * Allocate the IRQ |
| 445 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 446 | retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, |
| 447 | "atmel_serial", port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 448 | if (retval) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 449 | printk("atmel_serial: atmel_startup - Can't get irq\n"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 450 | return retval; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * If there is a specific "open" function (to register |
| 455 | * control line interrupts) |
| 456 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 457 | if (atmel_open_hook) { |
| 458 | retval = atmel_open_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 459 | if (retval) { |
| 460 | free_irq(port->irq, port); |
| 461 | return retval; |
| 462 | } |
| 463 | } |
| 464 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 465 | /* |
| 466 | * Finally, enable the serial port |
| 467 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 468 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 469 | /* enable xmit & rcvr */ |
| 470 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 471 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 472 | /* enable receive only */ |
| 473 | UART_PUT_IER(port, ATMEL_US_RXRDY); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 474 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Disable the port |
| 480 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 481 | static void atmel_shutdown(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 482 | { |
| 483 | /* |
| 484 | * Disable all interrupts, port and break condition. |
| 485 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 486 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 487 | UART_PUT_IDR(port, -1); |
| 488 | |
| 489 | /* |
| 490 | * Free the interrupt |
| 491 | */ |
| 492 | free_irq(port->irq, port); |
| 493 | |
| 494 | /* |
| 495 | * If there is a specific "close" function (to unregister |
| 496 | * control line interrupts) |
| 497 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 498 | if (atmel_close_hook) |
| 499 | atmel_close_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Power / Clock management. |
| 504 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 505 | static void atmel_serial_pm(struct uart_port *port, unsigned int state, |
| 506 | unsigned int oldstate) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 507 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 508 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 509 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 510 | switch (state) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 511 | case 0: |
| 512 | /* |
| 513 | * Enable the peripheral clock for this serial port. |
| 514 | * This is called on uart_open() or a resume event. |
| 515 | */ |
| 516 | clk_enable(atmel_port->clk); |
| 517 | break; |
| 518 | case 3: |
| 519 | /* |
| 520 | * Disable the peripheral clock for this serial port. |
| 521 | * This is called on uart_close() or a suspend event. |
| 522 | */ |
| 523 | clk_disable(atmel_port->clk); |
| 524 | break; |
| 525 | default: |
| 526 | printk(KERN_ERR "atmel_serial: unknown pm %d\n", state); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Change the port parameters |
| 532 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 533 | static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, |
| 534 | struct ktermios *old) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 535 | { |
| 536 | unsigned long flags; |
| 537 | unsigned int mode, imr, quot, baud; |
| 538 | |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 539 | /* Get current mode register */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 540 | mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL |
| 541 | | ATMEL_US_NBSTOP | ATMEL_US_PAR); |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 542 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 543 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 544 | quot = uart_get_divisor(port, baud); |
| 545 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 546 | if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 547 | quot /= 8; |
| 548 | mode |= ATMEL_US_USCLKS_MCK_DIV8; |
| 549 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 550 | |
| 551 | /* byte size */ |
| 552 | switch (termios->c_cflag & CSIZE) { |
| 553 | case CS5: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 554 | mode |= ATMEL_US_CHRL_5; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 555 | break; |
| 556 | case CS6: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 557 | mode |= ATMEL_US_CHRL_6; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 558 | break; |
| 559 | case CS7: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 560 | mode |= ATMEL_US_CHRL_7; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 561 | break; |
| 562 | default: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 563 | mode |= ATMEL_US_CHRL_8; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
| 567 | /* stop bits */ |
| 568 | if (termios->c_cflag & CSTOPB) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 569 | mode |= ATMEL_US_NBSTOP_2; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 570 | |
| 571 | /* parity */ |
| 572 | if (termios->c_cflag & PARENB) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 573 | /* Mark or Space parity */ |
| 574 | if (termios->c_cflag & CMSPAR) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 575 | if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 576 | mode |= ATMEL_US_PAR_MARK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 577 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 578 | mode |= ATMEL_US_PAR_SPACE; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 579 | } else if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 580 | mode |= ATMEL_US_PAR_ODD; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 581 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 582 | mode |= ATMEL_US_PAR_EVEN; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 583 | } else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 584 | mode |= ATMEL_US_PAR_NONE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 585 | |
| 586 | spin_lock_irqsave(&port->lock, flags); |
| 587 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 588 | port->read_status_mask = ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 589 | if (termios->c_iflag & INPCK) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 590 | port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 591 | if (termios->c_iflag & (BRKINT | PARMRK)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 592 | port->read_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * Characters to ignore |
| 596 | */ |
| 597 | port->ignore_status_mask = 0; |
| 598 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 599 | port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 600 | if (termios->c_iflag & IGNBRK) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 601 | port->ignore_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 602 | /* |
| 603 | * If we're ignoring parity and break indicators, |
| 604 | * ignore overruns too (for real raw support). |
| 605 | */ |
| 606 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 607 | port->ignore_status_mask |= ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 608 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 609 | /* TODO: Ignore all characters if CREAD is set.*/ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 610 | |
| 611 | /* update the per-port timeout */ |
| 612 | uart_update_timeout(port, termios->c_cflag, baud); |
| 613 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 614 | /* save/disable interrupts and drain transmitter */ |
| 615 | imr = UART_GET_IMR(port); |
| 616 | UART_PUT_IDR(port, -1); |
| 617 | while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) |
Haavard Skinnemoen | 829dd81 | 2008-02-08 04:21:02 -0800 | [diff] [blame] | 618 | cpu_relax(); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 619 | |
| 620 | /* disable receiver and transmitter */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 621 | UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 622 | |
| 623 | /* set the parity, stop bits and data size */ |
| 624 | UART_PUT_MR(port, mode); |
| 625 | |
| 626 | /* set the baud rate */ |
| 627 | UART_PUT_BRGR(port, quot); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 628 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 629 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 630 | |
| 631 | /* restore interrupts */ |
| 632 | UART_PUT_IER(port, imr); |
| 633 | |
| 634 | /* CTS flow-control and modem-status interrupts */ |
| 635 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 636 | port->ops->enable_ms(port); |
| 637 | |
| 638 | spin_unlock_irqrestore(&port->lock, flags); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Return string describing the specified port |
| 643 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 644 | static const char *atmel_type(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 645 | { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 646 | return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Release the memory region(s) being used by 'port'. |
| 651 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 652 | static void atmel_release_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 653 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 654 | struct platform_device *pdev = to_platform_device(port->dev); |
| 655 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
| 656 | |
| 657 | release_mem_region(port->mapbase, size); |
| 658 | |
| 659 | if (port->flags & UPF_IOREMAP) { |
| 660 | iounmap(port->membase); |
| 661 | port->membase = NULL; |
| 662 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Request the memory region(s) being used by 'port'. |
| 667 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 668 | static int atmel_request_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 669 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 670 | struct platform_device *pdev = to_platform_device(port->dev); |
| 671 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 672 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 673 | if (!request_mem_region(port->mapbase, size, "atmel_serial")) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 674 | return -EBUSY; |
| 675 | |
| 676 | if (port->flags & UPF_IOREMAP) { |
| 677 | port->membase = ioremap(port->mapbase, size); |
| 678 | if (port->membase == NULL) { |
| 679 | release_mem_region(port->mapbase, size); |
| 680 | return -ENOMEM; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Configure/autoconfigure the port. |
| 689 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 690 | static void atmel_config_port(struct uart_port *port, int flags) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 691 | { |
| 692 | if (flags & UART_CONFIG_TYPE) { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 693 | port->type = PORT_ATMEL; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 694 | atmel_request_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Verify the new serial_struct (for TIOCSSERIAL). |
| 700 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 701 | static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 702 | { |
| 703 | int ret = 0; |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 704 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 705 | ret = -EINVAL; |
| 706 | if (port->irq != ser->irq) |
| 707 | ret = -EINVAL; |
| 708 | if (ser->io_type != SERIAL_IO_MEM) |
| 709 | ret = -EINVAL; |
| 710 | if (port->uartclk / 16 != ser->baud_base) |
| 711 | ret = -EINVAL; |
| 712 | if ((void *)port->mapbase != ser->iomem_base) |
| 713 | ret = -EINVAL; |
| 714 | if (port->iobase != ser->port) |
| 715 | ret = -EINVAL; |
| 716 | if (ser->hub6 != 0) |
| 717 | ret = -EINVAL; |
| 718 | return ret; |
| 719 | } |
| 720 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 721 | static struct uart_ops atmel_pops = { |
| 722 | .tx_empty = atmel_tx_empty, |
| 723 | .set_mctrl = atmel_set_mctrl, |
| 724 | .get_mctrl = atmel_get_mctrl, |
| 725 | .stop_tx = atmel_stop_tx, |
| 726 | .start_tx = atmel_start_tx, |
| 727 | .stop_rx = atmel_stop_rx, |
| 728 | .enable_ms = atmel_enable_ms, |
| 729 | .break_ctl = atmel_break_ctl, |
| 730 | .startup = atmel_startup, |
| 731 | .shutdown = atmel_shutdown, |
| 732 | .set_termios = atmel_set_termios, |
| 733 | .type = atmel_type, |
| 734 | .release_port = atmel_release_port, |
| 735 | .request_port = atmel_request_port, |
| 736 | .config_port = atmel_config_port, |
| 737 | .verify_port = atmel_verify_port, |
| 738 | .pm = atmel_serial_pm, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 741 | /* |
| 742 | * Configure the port from the platform device resource info. |
| 743 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 744 | static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, |
| 745 | struct platform_device *pdev) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 746 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 747 | struct uart_port *port = &atmel_port->uart; |
Haavard Skinnemoen | 73e2798 | 2006-10-04 16:02:04 +0200 | [diff] [blame] | 748 | struct atmel_uart_data *data = pdev->dev.platform_data; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 749 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 750 | port->iotype = UPIO_MEM; |
Andrew Victor | a14d527 | 2007-01-09 09:03:42 +0100 | [diff] [blame] | 751 | port->flags = UPF_BOOT_AUTOCONF; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 752 | port->ops = &atmel_pops; |
Andrew Victor | a14d527 | 2007-01-09 09:03:42 +0100 | [diff] [blame] | 753 | port->fifosize = 1; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 754 | port->line = pdev->id; |
| 755 | port->dev = &pdev->dev; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 756 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 757 | port->mapbase = pdev->resource[0].start; |
| 758 | port->irq = pdev->resource[1].start; |
| 759 | |
Haavard Skinnemoen | 75d3521 | 2006-10-04 16:02:08 +0200 | [diff] [blame] | 760 | if (data->regs) |
| 761 | /* Already mapped by setup code */ |
| 762 | port->membase = data->regs; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 763 | else { |
| 764 | port->flags |= UPF_IOREMAP; |
| 765 | port->membase = NULL; |
| 766 | } |
| 767 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 768 | /* for console, the clock could already be configured */ |
| 769 | if (!atmel_port->clk) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 770 | atmel_port->clk = clk_get(&pdev->dev, "usart"); |
| 771 | clk_enable(atmel_port->clk); |
| 772 | port->uartclk = clk_get_rate(atmel_port->clk); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 773 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 776 | /* |
| 777 | * Register board-specific modem-control line handlers. |
| 778 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 779 | void __init atmel_register_uart_fns(struct atmel_port_fns *fns) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 780 | { |
| 781 | if (fns->enable_ms) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 782 | atmel_pops.enable_ms = fns->enable_ms; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 783 | if (fns->get_mctrl) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 784 | atmel_pops.get_mctrl = fns->get_mctrl; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 785 | if (fns->set_mctrl) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 786 | atmel_pops.set_mctrl = fns->set_mctrl; |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 787 | atmel_open_hook = fns->open; |
| 788 | atmel_close_hook = fns->close; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 789 | atmel_pops.pm = fns->pm; |
| 790 | atmel_pops.set_wake = fns->set_wake; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 793 | #ifdef CONFIG_SERIAL_ATMEL_CONSOLE |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 794 | static void atmel_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 795 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 796 | while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) |
Haavard Skinnemoen | 829dd81 | 2008-02-08 04:21:02 -0800 | [diff] [blame] | 797 | cpu_relax(); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 798 | UART_PUT_CHAR(port, ch); |
| 799 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * Interrupts are disabled on entering |
| 803 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 804 | static void atmel_console_write(struct console *co, const char *s, u_int count) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 805 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 806 | struct uart_port *port = &atmel_ports[co->index].uart; |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 807 | unsigned int status, imr; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 808 | |
| 809 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 810 | * First, save IMR and then disable interrupts |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 811 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 812 | imr = UART_GET_IMR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 813 | UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 814 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 815 | uart_console_write(port, s, count, atmel_console_putchar); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 816 | |
| 817 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 818 | * Finally, wait for transmitter to become empty |
| 819 | * and restore IMR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 820 | */ |
| 821 | do { |
| 822 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 823 | } while (!(status & ATMEL_US_TXRDY)); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 824 | /* set interrupts back the way they were */ |
| 825 | UART_PUT_IER(port, imr); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 829 | * If the port was already initialised (eg, by a boot loader), |
| 830 | * try to determine the current setup. |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 831 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 832 | static void __init atmel_console_get_options(struct uart_port *port, int *baud, |
| 833 | int *parity, int *bits) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 834 | { |
| 835 | unsigned int mr, quot; |
| 836 | |
Haavard Skinnemoen | 1c0fd82 | 2008-02-08 04:21:03 -0800 | [diff] [blame] | 837 | /* |
| 838 | * If the baud rate generator isn't running, the port wasn't |
| 839 | * initialized by the boot loader. |
| 840 | */ |
| 841 | quot = UART_GET_BRGR(port); |
| 842 | if (!quot) |
| 843 | return; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 844 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 845 | mr = UART_GET_MR(port) & ATMEL_US_CHRL; |
| 846 | if (mr == ATMEL_US_CHRL_8) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 847 | *bits = 8; |
| 848 | else |
| 849 | *bits = 7; |
| 850 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 851 | mr = UART_GET_MR(port) & ATMEL_US_PAR; |
| 852 | if (mr == ATMEL_US_PAR_EVEN) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 853 | *parity = 'e'; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 854 | else if (mr == ATMEL_US_PAR_ODD) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 855 | *parity = 'o'; |
| 856 | |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 857 | /* |
| 858 | * The serial core only rounds down when matching this to a |
| 859 | * supported baud rate. Make sure we don't end up slightly |
| 860 | * lower than one of those, as it would make us fall through |
| 861 | * to a much lower baud rate than we really want. |
| 862 | */ |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 863 | *baud = port->uartclk / (16 * (quot - 1)); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 866 | static int __init atmel_console_setup(struct console *co, char *options) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 867 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 868 | struct uart_port *port = &atmel_ports[co->index].uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 869 | int baud = 115200; |
| 870 | int bits = 8; |
| 871 | int parity = 'n'; |
| 872 | int flow = 'n'; |
| 873 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 874 | if (port->membase == NULL) { |
| 875 | /* Port not initialized yet - delay setup */ |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 876 | return -ENODEV; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 877 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 878 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 879 | UART_PUT_IDR(port, -1); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 880 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 881 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 882 | |
| 883 | if (options) |
| 884 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 885 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 886 | atmel_console_get_options(port, &baud, &parity, &bits); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 887 | |
| 888 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 889 | } |
| 890 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 891 | static struct uart_driver atmel_uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 892 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 893 | static struct console atmel_console = { |
| 894 | .name = ATMEL_DEVICENAME, |
| 895 | .write = atmel_console_write, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 896 | .device = uart_console_device, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 897 | .setup = atmel_console_setup, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 898 | .flags = CON_PRINTBUFFER, |
| 899 | .index = -1, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 900 | .data = &atmel_uart, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 903 | #define ATMEL_CONSOLE_DEVICE &atmel_console |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 904 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 905 | /* |
| 906 | * Early console initialization (before VM subsystem initialized). |
| 907 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 908 | static int __init atmel_console_init(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 909 | { |
Haavard Skinnemoen | 73e2798 | 2006-10-04 16:02:04 +0200 | [diff] [blame] | 910 | if (atmel_default_console_device) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 911 | add_preferred_console(ATMEL_DEVICENAME, |
| 912 | atmel_default_console_device->id, NULL); |
| 913 | atmel_init_port(&atmel_ports[atmel_default_console_device->id], |
| 914 | atmel_default_console_device); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 915 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 916 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 917 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 918 | return 0; |
| 919 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 920 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 921 | console_initcall(atmel_console_init); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 922 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 923 | /* |
| 924 | * Late console initialization. |
| 925 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 926 | static int __init atmel_late_console_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 927 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 928 | if (atmel_default_console_device |
| 929 | && !(atmel_console.flags & CON_ENABLED)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 930 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 931 | |
| 932 | return 0; |
| 933 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 934 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 935 | core_initcall(atmel_late_console_init); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 936 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame^] | 937 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 938 | { |
| 939 | return port->cons && port->cons->index == port->line; |
| 940 | } |
| 941 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 942 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 943 | #define ATMEL_CONSOLE_DEVICE NULL |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame^] | 944 | |
| 945 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 946 | { |
| 947 | return false; |
| 948 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 949 | #endif |
| 950 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 951 | static struct uart_driver atmel_uart = { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 952 | .owner = THIS_MODULE, |
| 953 | .driver_name = "atmel_serial", |
| 954 | .dev_name = ATMEL_DEVICENAME, |
| 955 | .major = SERIAL_ATMEL_MAJOR, |
| 956 | .minor = MINOR_START, |
| 957 | .nr = ATMEL_MAX_UART, |
| 958 | .cons = ATMEL_CONSOLE_DEVICE, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 959 | }; |
| 960 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 961 | #ifdef CONFIG_PM |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 962 | static int atmel_serial_suspend(struct platform_device *pdev, |
| 963 | pm_message_t state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 964 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 965 | struct uart_port *port = platform_get_drvdata(pdev); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 966 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 967 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 968 | if (device_may_wakeup(&pdev->dev) |
| 969 | && !at91_suspend_entering_slow_clock()) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 970 | enable_irq_wake(port->irq); |
| 971 | else { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 972 | uart_suspend_port(&atmel_uart, port); |
| 973 | atmel_port->suspended = 1; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 979 | static int atmel_serial_resume(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 980 | { |
| 981 | struct uart_port *port = platform_get_drvdata(pdev); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 982 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 983 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 984 | if (atmel_port->suspended) { |
| 985 | uart_resume_port(&atmel_uart, port); |
| 986 | atmel_port->suspended = 0; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 987 | } else |
Andrew Victor | 9b93816 | 2007-01-09 13:20:54 +0100 | [diff] [blame] | 988 | disable_irq_wake(port->irq); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 993 | #define atmel_serial_suspend NULL |
| 994 | #define atmel_serial_resume NULL |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 995 | #endif |
| 996 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 997 | static int __devinit atmel_serial_probe(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 998 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 999 | struct atmel_uart_port *port; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1000 | int ret; |
| 1001 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1002 | port = &atmel_ports[pdev->id]; |
| 1003 | atmel_init_port(port, pdev); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1004 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1005 | ret = uart_add_one_port(&atmel_uart, &port->uart); |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame^] | 1006 | if (ret) |
| 1007 | goto err_add_port; |
| 1008 | |
| 1009 | device_init_wakeup(&pdev->dev, 1); |
| 1010 | platform_set_drvdata(pdev, port); |
| 1011 | |
| 1012 | return 0; |
| 1013 | |
| 1014 | err_add_port: |
| 1015 | if (!atmel_is_console_port(&port->uart)) { |
| 1016 | clk_disable(port->clk); |
| 1017 | clk_put(port->clk); |
| 1018 | port->clk = NULL; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1024 | static int __devexit atmel_serial_remove(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1025 | { |
| 1026 | struct uart_port *port = platform_get_drvdata(pdev); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1027 | struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1028 | int ret = 0; |
| 1029 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1030 | device_init_wakeup(&pdev->dev, 0); |
| 1031 | platform_set_drvdata(pdev, NULL); |
| 1032 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame^] | 1033 | ret = uart_remove_one_port(&atmel_uart, port); |
| 1034 | |
| 1035 | /* "port" is allocated statically, so we shouldn't free it */ |
| 1036 | |
| 1037 | clk_disable(atmel_port->clk); |
| 1038 | clk_put(atmel_port->clk); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1039 | |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1043 | static struct platform_driver atmel_serial_driver = { |
| 1044 | .probe = atmel_serial_probe, |
| 1045 | .remove = __devexit_p(atmel_serial_remove), |
| 1046 | .suspend = atmel_serial_suspend, |
| 1047 | .resume = atmel_serial_resume, |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1048 | .driver = { |
Haavard Skinnemoen | 1e8ea80 | 2006-10-04 16:02:03 +0200 | [diff] [blame] | 1049 | .name = "atmel_usart", |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1050 | .owner = THIS_MODULE, |
| 1051 | }, |
| 1052 | }; |
| 1053 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1054 | static int __init atmel_serial_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1055 | { |
| 1056 | int ret; |
| 1057 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1058 | ret = uart_register_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1059 | if (ret) |
| 1060 | return ret; |
| 1061 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1062 | ret = platform_driver_register(&atmel_serial_driver); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1063 | if (ret) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1064 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1065 | |
| 1066 | return ret; |
| 1067 | } |
| 1068 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1069 | static void __exit atmel_serial_exit(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1070 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1071 | platform_driver_unregister(&atmel_serial_driver); |
| 1072 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1075 | module_init(atmel_serial_init); |
| 1076 | module_exit(atmel_serial_exit); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1077 | |
| 1078 | MODULE_AUTHOR("Rick Bronson"); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1079 | MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1080 | MODULE_LICENSE("GPL"); |