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 | * |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 10 | * DMA support added by Chip Coldwell. |
| 11 | * |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/tty.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/serial.h> |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 33 | #include <linux/clk.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 34 | #include <linux/console.h> |
| 35 | #include <linux/sysrq.h> |
| 36 | #include <linux/tty_flip.h> |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 37 | #include <linux/platform_device.h> |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 38 | #include <linux/dma-mapping.h> |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 39 | #include <linux/atmel_pdc.h> |
Guennadi Liakhovetski | fa3218d | 2008-01-29 15:43:13 +0100 | [diff] [blame] | 40 | #include <linux/atmel_serial.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 41 | |
| 42 | #include <asm/io.h> |
| 43 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 44 | #include <asm/mach/serial_at91.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 45 | #include <asm/arch/board.h> |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 46 | |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_ARM |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 48 | #include <asm/arch/cpu.h> |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 49 | #include <asm/arch/gpio.h> |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 50 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 51 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 52 | #define PDC_BUFFER_SIZE 512 |
| 53 | /* Revisit: We should calculate this based on the actual port settings */ |
| 54 | #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ |
| 55 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 56 | #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 57 | #define SUPPORT_SYSRQ |
| 58 | #endif |
| 59 | |
| 60 | #include <linux/serial_core.h> |
| 61 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 62 | #ifdef CONFIG_SERIAL_ATMEL_TTYAT |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 63 | |
| 64 | /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we |
| 65 | * should coexist with the 8250 driver, such as if we have an external 16C550 |
| 66 | * UART. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 67 | #define SERIAL_ATMEL_MAJOR 204 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 68 | #define MINOR_START 154 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 69 | #define ATMEL_DEVICENAME "ttyAT" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 70 | |
| 71 | #else |
| 72 | |
| 73 | /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port |
| 74 | * name, but it is legally reserved for the 8250 driver. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 75 | #define SERIAL_ATMEL_MAJOR TTY_MAJOR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 76 | #define MINOR_START 64 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 77 | #define ATMEL_DEVICENAME "ttyS" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 78 | |
| 79 | #endif |
| 80 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 81 | #define ATMEL_ISR_PASS_LIMIT 256 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 82 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 83 | /* UART registers. CR is write-only, hence no GET macro */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 84 | #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) |
| 85 | #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) |
| 86 | #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) |
| 87 | #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) |
| 88 | #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) |
| 89 | #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) |
| 90 | #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) |
| 91 | #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) |
| 92 | #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) |
| 93 | #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) |
| 94 | #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) |
| 95 | #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] | 96 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 97 | /* PDC registers */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 98 | #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) |
| 99 | #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 100 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 101 | #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) |
| 102 | #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) |
| 103 | #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) |
| 104 | #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) |
| 105 | #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] | 106 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 107 | #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) |
| 108 | #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] | 109 | |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 110 | static int (*atmel_open_hook)(struct uart_port *); |
| 111 | static void (*atmel_close_hook)(struct uart_port *); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 112 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 113 | struct atmel_dma_buffer { |
| 114 | unsigned char *buf; |
| 115 | dma_addr_t dma_addr; |
| 116 | unsigned int dma_size; |
| 117 | unsigned int ofs; |
| 118 | }; |
| 119 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 120 | struct atmel_uart_char { |
| 121 | u16 status; |
| 122 | u16 ch; |
| 123 | }; |
| 124 | |
| 125 | #define ATMEL_SERIAL_RINGSIZE 1024 |
| 126 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 127 | /* |
| 128 | * We wrap our port structure around the generic uart_port. |
| 129 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 130 | struct atmel_uart_port { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 131 | struct uart_port uart; /* uart */ |
| 132 | struct clk *clk; /* uart clock */ |
| 133 | unsigned short suspended; /* is port suspended? */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 134 | int break_active; /* break being received */ |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 135 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 136 | short use_dma_rx; /* enable PDC receiver */ |
| 137 | short pdc_rx_idx; /* current PDC RX buffer */ |
| 138 | struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ |
| 139 | |
| 140 | short use_dma_tx; /* enable PDC transmitter */ |
| 141 | struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ |
| 142 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 143 | struct tasklet_struct tasklet; |
| 144 | unsigned int irq_status; |
| 145 | unsigned int irq_status_prev; |
| 146 | |
| 147 | struct circ_buf rx_ring; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 150 | static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 151 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 152 | #ifdef SUPPORT_SYSRQ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 153 | static struct console atmel_console; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 156 | static inline struct atmel_uart_port * |
| 157 | to_atmel_uart_port(struct uart_port *uart) |
| 158 | { |
| 159 | return container_of(uart, struct atmel_uart_port, uart); |
| 160 | } |
| 161 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 162 | #ifdef CONFIG_SERIAL_ATMEL_PDC |
| 163 | static bool atmel_use_dma_rx(struct uart_port *port) |
| 164 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 165 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 166 | |
| 167 | return atmel_port->use_dma_rx; |
| 168 | } |
| 169 | |
| 170 | static bool atmel_use_dma_tx(struct uart_port *port) |
| 171 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 172 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 173 | |
| 174 | return atmel_port->use_dma_tx; |
| 175 | } |
| 176 | #else |
| 177 | static bool atmel_use_dma_rx(struct uart_port *port) |
| 178 | { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | static bool atmel_use_dma_tx(struct uart_port *port) |
| 183 | { |
| 184 | return false; |
| 185 | } |
| 186 | #endif |
| 187 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 188 | /* |
| 189 | * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. |
| 190 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 191 | static u_int atmel_tx_empty(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 192 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 193 | return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Set state of the modem control output lines |
| 198 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 199 | static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 200 | { |
| 201 | unsigned int control = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 202 | unsigned int mode; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 203 | |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 204 | #ifdef CONFIG_ARCH_AT91RM9200 |
Andrew Victor | 79da7a6 | 2006-11-30 11:58:14 +0100 | [diff] [blame] | 205 | if (cpu_is_at91rm9200()) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 206 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 207 | * AT91RM9200 Errata #39: RTS0 is not internally connected |
| 208 | * to PA21. We need to drive the pin manually. |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 209 | */ |
Andrew Victor | 7272991 | 2006-09-27 09:44:11 +0100 | [diff] [blame] | 210 | if (port->mapbase == AT91RM9200_BASE_US0) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 211 | if (mctrl & TIOCM_RTS) |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 212 | at91_set_gpio_value(AT91_PIN_PA21, 0); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 213 | else |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 214 | at91_set_gpio_value(AT91_PIN_PA21, 1); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 215 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 216 | } |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 217 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 218 | |
| 219 | if (mctrl & TIOCM_RTS) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 220 | control |= ATMEL_US_RTSEN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 221 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 222 | control |= ATMEL_US_RTSDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 223 | |
| 224 | if (mctrl & TIOCM_DTR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 225 | control |= ATMEL_US_DTREN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 226 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 227 | control |= ATMEL_US_DTRDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 228 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 229 | UART_PUT_CR(port, control); |
| 230 | |
| 231 | /* Local loopback mode? */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 232 | mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 233 | if (mctrl & TIOCM_LOOP) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 234 | mode |= ATMEL_US_CHMODE_LOC_LOOP; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 235 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 236 | mode |= ATMEL_US_CHMODE_NORMAL; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 237 | UART_PUT_MR(port, mode); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Get state of the modem control input lines |
| 242 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 243 | static u_int atmel_get_mctrl(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 244 | { |
| 245 | unsigned int status, ret = 0; |
| 246 | |
| 247 | status = UART_GET_CSR(port); |
| 248 | |
| 249 | /* |
| 250 | * The control signals are active low. |
| 251 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 252 | if (!(status & ATMEL_US_DCD)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 253 | ret |= TIOCM_CD; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 254 | if (!(status & ATMEL_US_CTS)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 255 | ret |= TIOCM_CTS; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 256 | if (!(status & ATMEL_US_DSR)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 257 | ret |= TIOCM_DSR; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 258 | if (!(status & ATMEL_US_RI)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 259 | ret |= TIOCM_RI; |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Stop transmitting. |
| 266 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 267 | static void atmel_stop_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 268 | { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 269 | if (atmel_use_dma_tx(port)) { |
| 270 | /* disable PDC transmit */ |
| 271 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
| 272 | UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE); |
| 273 | } else |
| 274 | UART_PUT_IDR(port, ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Start transmitting. |
| 279 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 280 | static void atmel_start_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 281 | { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 282 | if (atmel_use_dma_tx(port)) { |
| 283 | if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) |
| 284 | /* The transmitter is already running. Yes, we |
| 285 | really need this.*/ |
| 286 | return; |
| 287 | |
| 288 | UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE); |
| 289 | /* re-enable PDC transmit */ |
| 290 | UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); |
| 291 | } else |
| 292 | UART_PUT_IER(port, ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Stop receiving - port is in process of being closed. |
| 297 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 298 | static void atmel_stop_rx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 299 | { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 300 | if (atmel_use_dma_rx(port)) { |
| 301 | /* disable PDC receive */ |
| 302 | UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); |
| 303 | UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
| 304 | } else |
| 305 | UART_PUT_IDR(port, ATMEL_US_RXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Enable modem status interrupts |
| 310 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 311 | static void atmel_enable_ms(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 312 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 313 | UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC |
| 314 | | ATMEL_US_DCDIC | ATMEL_US_CTSIC); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Control the transmission of a break signal |
| 319 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 320 | static void atmel_break_ctl(struct uart_port *port, int break_state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 321 | { |
| 322 | if (break_state != 0) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 323 | UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 324 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 325 | UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 329 | * Stores the incoming character in the ring buffer |
| 330 | */ |
| 331 | static void |
| 332 | atmel_buffer_rx_char(struct uart_port *port, unsigned int status, |
| 333 | unsigned int ch) |
| 334 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 335 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 336 | struct circ_buf *ring = &atmel_port->rx_ring; |
| 337 | struct atmel_uart_char *c; |
| 338 | |
| 339 | if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) |
| 340 | /* Buffer overflow, ignore char */ |
| 341 | return; |
| 342 | |
| 343 | c = &((struct atmel_uart_char *)ring->buf)[ring->head]; |
| 344 | c->status = status; |
| 345 | c->ch = ch; |
| 346 | |
| 347 | /* Make sure the character is stored before we update head. */ |
| 348 | smp_wmb(); |
| 349 | |
| 350 | ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); |
| 351 | } |
| 352 | |
| 353 | /* |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 354 | * Deal with parity, framing and overrun errors. |
| 355 | */ |
| 356 | static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) |
| 357 | { |
| 358 | /* clear error */ |
| 359 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 360 | |
| 361 | if (status & ATMEL_US_RXBRK) { |
| 362 | /* ignore side-effect */ |
| 363 | status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); |
| 364 | port->icount.brk++; |
| 365 | } |
| 366 | if (status & ATMEL_US_PARE) |
| 367 | port->icount.parity++; |
| 368 | if (status & ATMEL_US_FRAME) |
| 369 | port->icount.frame++; |
| 370 | if (status & ATMEL_US_OVRE) |
| 371 | port->icount.overrun++; |
| 372 | } |
| 373 | |
| 374 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 375 | * Characters received (called from interrupt handler) |
| 376 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 377 | static void atmel_rx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 378 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 379 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 380 | unsigned int status, ch; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 381 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 382 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 383 | while (status & ATMEL_US_RXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 384 | ch = UART_GET_CHAR(port); |
| 385 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 386 | /* |
| 387 | * note that the error handling code is |
| 388 | * out of the main execution path |
| 389 | */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 390 | if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME |
| 391 | | ATMEL_US_OVRE | ATMEL_US_RXBRK) |
| 392 | || atmel_port->break_active)) { |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 393 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 394 | /* clear error */ |
| 395 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 396 | |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 397 | if (status & ATMEL_US_RXBRK |
| 398 | && !atmel_port->break_active) { |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 399 | atmel_port->break_active = 1; |
| 400 | UART_PUT_IER(port, ATMEL_US_RXBRK); |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 401 | } else { |
| 402 | /* |
| 403 | * This is either the end-of-break |
| 404 | * condition or we've received at |
| 405 | * least one character without RXBRK |
| 406 | * being set. In both cases, the next |
| 407 | * RXBRK will indicate start-of-break. |
| 408 | */ |
| 409 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 410 | status &= ~ATMEL_US_RXBRK; |
| 411 | atmel_port->break_active = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 412 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 415 | atmel_buffer_rx_char(port, status, ch); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 416 | status = UART_GET_CSR(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 419 | tasklet_schedule(&atmel_port->tasklet); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 423 | * Transmit characters (called from tasklet with TXRDY interrupt |
| 424 | * disabled) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 425 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 426 | static void atmel_tx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 427 | { |
| 428 | struct circ_buf *xmit = &port->info->xmit; |
| 429 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 430 | if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 431 | UART_PUT_CHAR(port, port->x_char); |
| 432 | port->icount.tx++; |
| 433 | port->x_char = 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 434 | } |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 435 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 436 | return; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 437 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 438 | while (UART_GET_CSR(port) & ATMEL_US_TXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 439 | UART_PUT_CHAR(port, xmit->buf[xmit->tail]); |
| 440 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 441 | port->icount.tx++; |
| 442 | if (uart_circ_empty(xmit)) |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 447 | uart_write_wakeup(port); |
| 448 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 449 | if (!uart_circ_empty(xmit)) |
| 450 | UART_PUT_IER(port, ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 454 | * receive interrupt handler. |
| 455 | */ |
| 456 | static void |
| 457 | atmel_handle_receive(struct uart_port *port, unsigned int pending) |
| 458 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 459 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 460 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 461 | if (atmel_use_dma_rx(port)) { |
| 462 | /* |
| 463 | * PDC receive. Just schedule the tasklet and let it |
| 464 | * figure out the details. |
| 465 | * |
| 466 | * TODO: We're not handling error flags correctly at |
| 467 | * the moment. |
| 468 | */ |
| 469 | if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { |
| 470 | UART_PUT_IDR(port, (ATMEL_US_ENDRX |
| 471 | | ATMEL_US_TIMEOUT)); |
| 472 | tasklet_schedule(&atmel_port->tasklet); |
| 473 | } |
| 474 | |
| 475 | if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | |
| 476 | ATMEL_US_FRAME | ATMEL_US_PARE)) |
| 477 | atmel_pdc_rxerr(port, pending); |
| 478 | } |
| 479 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 480 | /* Interrupt receive */ |
| 481 | if (pending & ATMEL_US_RXRDY) |
| 482 | atmel_rx_chars(port); |
| 483 | else if (pending & ATMEL_US_RXBRK) { |
| 484 | /* |
| 485 | * End of break detected. If it came along with a |
| 486 | * character, atmel_rx_chars will handle it. |
| 487 | */ |
| 488 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 489 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 490 | atmel_port->break_active = 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 495 | * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 496 | */ |
| 497 | static void |
| 498 | atmel_handle_transmit(struct uart_port *port, unsigned int pending) |
| 499 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 500 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 501 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 502 | if (atmel_use_dma_tx(port)) { |
| 503 | /* PDC transmit */ |
| 504 | if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) { |
| 505 | UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE); |
| 506 | tasklet_schedule(&atmel_port->tasklet); |
| 507 | } |
| 508 | } else { |
| 509 | /* Interrupt transmit */ |
| 510 | if (pending & ATMEL_US_TXRDY) { |
| 511 | UART_PUT_IDR(port, ATMEL_US_TXRDY); |
| 512 | tasklet_schedule(&atmel_port->tasklet); |
| 513 | } |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 514 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
| 518 | * status flags interrupt handler. |
| 519 | */ |
| 520 | static void |
| 521 | atmel_handle_status(struct uart_port *port, unsigned int pending, |
| 522 | unsigned int status) |
| 523 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 524 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 525 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 526 | if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 527 | | ATMEL_US_CTSIC)) { |
| 528 | atmel_port->irq_status = status; |
| 529 | tasklet_schedule(&atmel_port->tasklet); |
| 530 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 534 | * Interrupt handler |
| 535 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 536 | static irqreturn_t atmel_interrupt(int irq, void *dev_id) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 537 | { |
| 538 | struct uart_port *port = dev_id; |
| 539 | unsigned int status, pending, pass_counter = 0; |
| 540 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 541 | do { |
| 542 | status = UART_GET_CSR(port); |
| 543 | pending = status & UART_GET_IMR(port); |
| 544 | if (!pending) |
| 545 | break; |
| 546 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 547 | atmel_handle_receive(port, pending); |
| 548 | atmel_handle_status(port, pending, status); |
| 549 | atmel_handle_transmit(port, pending); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 550 | } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 551 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 552 | return IRQ_HANDLED; |
| 553 | } |
| 554 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 555 | /* |
| 556 | * Called from tasklet with ENDTX and TXBUFE interrupts disabled. |
| 557 | */ |
| 558 | static void atmel_tx_dma(struct uart_port *port) |
| 559 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 560 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 561 | struct circ_buf *xmit = &port->info->xmit; |
| 562 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 563 | int count; |
| 564 | |
| 565 | xmit->tail += pdc->ofs; |
| 566 | xmit->tail &= UART_XMIT_SIZE - 1; |
| 567 | |
| 568 | port->icount.tx += pdc->ofs; |
| 569 | pdc->ofs = 0; |
| 570 | |
| 571 | if (!uart_circ_empty(xmit)) { |
| 572 | /* more to transmit - setup next transfer */ |
| 573 | |
| 574 | /* disable PDC transmit */ |
| 575 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
| 576 | dma_sync_single_for_device(port->dev, |
| 577 | pdc->dma_addr, |
| 578 | pdc->dma_size, |
| 579 | DMA_TO_DEVICE); |
| 580 | |
| 581 | count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 582 | pdc->ofs = count; |
| 583 | |
| 584 | UART_PUT_TPR(port, pdc->dma_addr + xmit->tail); |
| 585 | UART_PUT_TCR(port, count); |
| 586 | /* re-enable PDC transmit and interrupts */ |
| 587 | UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); |
| 588 | UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE); |
| 589 | } else { |
| 590 | /* nothing left to transmit - disable the transmitter */ |
| 591 | |
| 592 | /* disable PDC transmit */ |
| 593 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
| 594 | } |
| 595 | |
| 596 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 597 | uart_write_wakeup(port); |
| 598 | } |
| 599 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 600 | static void atmel_rx_from_ring(struct uart_port *port) |
| 601 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 602 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 603 | struct circ_buf *ring = &atmel_port->rx_ring; |
| 604 | unsigned int flg; |
| 605 | unsigned int status; |
| 606 | |
| 607 | while (ring->head != ring->tail) { |
| 608 | struct atmel_uart_char c; |
| 609 | |
| 610 | /* Make sure c is loaded after head. */ |
| 611 | smp_rmb(); |
| 612 | |
| 613 | c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; |
| 614 | |
| 615 | ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); |
| 616 | |
| 617 | port->icount.rx++; |
| 618 | status = c.status; |
| 619 | flg = TTY_NORMAL; |
| 620 | |
| 621 | /* |
| 622 | * note that the error handling code is |
| 623 | * out of the main execution path |
| 624 | */ |
| 625 | if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME |
| 626 | | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { |
| 627 | if (status & ATMEL_US_RXBRK) { |
| 628 | /* ignore side-effect */ |
| 629 | status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); |
| 630 | |
| 631 | port->icount.brk++; |
| 632 | if (uart_handle_break(port)) |
| 633 | continue; |
| 634 | } |
| 635 | if (status & ATMEL_US_PARE) |
| 636 | port->icount.parity++; |
| 637 | if (status & ATMEL_US_FRAME) |
| 638 | port->icount.frame++; |
| 639 | if (status & ATMEL_US_OVRE) |
| 640 | port->icount.overrun++; |
| 641 | |
| 642 | status &= port->read_status_mask; |
| 643 | |
| 644 | if (status & ATMEL_US_RXBRK) |
| 645 | flg = TTY_BREAK; |
| 646 | else if (status & ATMEL_US_PARE) |
| 647 | flg = TTY_PARITY; |
| 648 | else if (status & ATMEL_US_FRAME) |
| 649 | flg = TTY_FRAME; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | if (uart_handle_sysrq_char(port, c.ch)) |
| 654 | continue; |
| 655 | |
| 656 | uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Drop the lock here since it might end up calling |
| 661 | * uart_start(), which takes the lock. |
| 662 | */ |
| 663 | spin_unlock(&port->lock); |
| 664 | tty_flip_buffer_push(port->info->tty); |
| 665 | spin_lock(&port->lock); |
| 666 | } |
| 667 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 668 | static void atmel_rx_from_dma(struct uart_port *port) |
| 669 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 670 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 671 | struct tty_struct *tty = port->info->tty; |
| 672 | struct atmel_dma_buffer *pdc; |
| 673 | int rx_idx = atmel_port->pdc_rx_idx; |
| 674 | unsigned int head; |
| 675 | unsigned int tail; |
| 676 | unsigned int count; |
| 677 | |
| 678 | do { |
| 679 | /* Reset the UART timeout early so that we don't miss one */ |
| 680 | UART_PUT_CR(port, ATMEL_US_STTTO); |
| 681 | |
| 682 | pdc = &atmel_port->pdc_rx[rx_idx]; |
| 683 | head = UART_GET_RPR(port) - pdc->dma_addr; |
| 684 | tail = pdc->ofs; |
| 685 | |
| 686 | /* If the PDC has switched buffers, RPR won't contain |
| 687 | * any address within the current buffer. Since head |
| 688 | * is unsigned, we just need a one-way comparison to |
| 689 | * find out. |
| 690 | * |
| 691 | * In this case, we just need to consume the entire |
| 692 | * buffer and resubmit it for DMA. This will clear the |
| 693 | * ENDRX bit as well, so that we can safely re-enable |
| 694 | * all interrupts below. |
| 695 | */ |
| 696 | head = min(head, pdc->dma_size); |
| 697 | |
| 698 | if (likely(head != tail)) { |
| 699 | dma_sync_single_for_cpu(port->dev, pdc->dma_addr, |
| 700 | pdc->dma_size, DMA_FROM_DEVICE); |
| 701 | |
| 702 | /* |
| 703 | * head will only wrap around when we recycle |
| 704 | * the DMA buffer, and when that happens, we |
| 705 | * explicitly set tail to 0. So head will |
| 706 | * always be greater than tail. |
| 707 | */ |
| 708 | count = head - tail; |
| 709 | |
| 710 | tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count); |
| 711 | |
| 712 | dma_sync_single_for_device(port->dev, pdc->dma_addr, |
| 713 | pdc->dma_size, DMA_FROM_DEVICE); |
| 714 | |
| 715 | port->icount.rx += count; |
| 716 | pdc->ofs = head; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * If the current buffer is full, we need to check if |
| 721 | * the next one contains any additional data. |
| 722 | */ |
| 723 | if (head >= pdc->dma_size) { |
| 724 | pdc->ofs = 0; |
| 725 | UART_PUT_RNPR(port, pdc->dma_addr); |
| 726 | UART_PUT_RNCR(port, pdc->dma_size); |
| 727 | |
| 728 | rx_idx = !rx_idx; |
| 729 | atmel_port->pdc_rx_idx = rx_idx; |
| 730 | } |
| 731 | } while (head >= pdc->dma_size); |
| 732 | |
| 733 | /* |
| 734 | * Drop the lock here since it might end up calling |
| 735 | * uart_start(), which takes the lock. |
| 736 | */ |
| 737 | spin_unlock(&port->lock); |
| 738 | tty_flip_buffer_push(tty); |
| 739 | spin_lock(&port->lock); |
| 740 | |
| 741 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
| 742 | } |
| 743 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 744 | /* |
| 745 | * tasklet handling tty stuff outside the interrupt handler. |
| 746 | */ |
| 747 | static void atmel_tasklet_func(unsigned long data) |
| 748 | { |
| 749 | struct uart_port *port = (struct uart_port *)data; |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 750 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 751 | unsigned int status; |
| 752 | unsigned int status_change; |
| 753 | |
| 754 | /* The interrupt handler does not take the lock */ |
| 755 | spin_lock(&port->lock); |
| 756 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 757 | if (atmel_use_dma_tx(port)) |
| 758 | atmel_tx_dma(port); |
| 759 | else |
| 760 | atmel_tx_chars(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 761 | |
| 762 | status = atmel_port->irq_status; |
| 763 | status_change = status ^ atmel_port->irq_status_prev; |
| 764 | |
| 765 | if (status_change & (ATMEL_US_RI | ATMEL_US_DSR |
| 766 | | ATMEL_US_DCD | ATMEL_US_CTS)) { |
| 767 | /* TODO: All reads to CSR will clear these interrupts! */ |
| 768 | if (status_change & ATMEL_US_RI) |
| 769 | port->icount.rng++; |
| 770 | if (status_change & ATMEL_US_DSR) |
| 771 | port->icount.dsr++; |
| 772 | if (status_change & ATMEL_US_DCD) |
| 773 | uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); |
| 774 | if (status_change & ATMEL_US_CTS) |
| 775 | uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); |
| 776 | |
| 777 | wake_up_interruptible(&port->info->delta_msr_wait); |
| 778 | |
| 779 | atmel_port->irq_status_prev = status; |
| 780 | } |
| 781 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 782 | if (atmel_use_dma_rx(port)) |
| 783 | atmel_rx_from_dma(port); |
| 784 | else |
| 785 | atmel_rx_from_ring(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 786 | |
| 787 | spin_unlock(&port->lock); |
| 788 | } |
| 789 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 790 | /* |
| 791 | * Perform initialization and enable port for reception |
| 792 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 793 | static int atmel_startup(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 794 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 795 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Haavard Skinnemoen | ae16106 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 796 | struct tty_struct *tty = port->info->tty; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 797 | int retval; |
| 798 | |
| 799 | /* |
| 800 | * Ensure that no interrupts are enabled otherwise when |
| 801 | * request_irq() is called we could get stuck trying to |
| 802 | * handle an unexpected interrupt |
| 803 | */ |
| 804 | UART_PUT_IDR(port, -1); |
| 805 | |
| 806 | /* |
| 807 | * Allocate the IRQ |
| 808 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 809 | retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, |
Haavard Skinnemoen | ae16106 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 810 | tty ? tty->name : "atmel_serial", port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 811 | if (retval) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 812 | printk("atmel_serial: atmel_startup - Can't get irq\n"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 813 | return retval; |
| 814 | } |
| 815 | |
| 816 | /* |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 817 | * Initialize DMA (if necessary) |
| 818 | */ |
| 819 | if (atmel_use_dma_rx(port)) { |
| 820 | int i; |
| 821 | |
| 822 | for (i = 0; i < 2; i++) { |
| 823 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; |
| 824 | |
| 825 | pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); |
| 826 | if (pdc->buf == NULL) { |
| 827 | if (i != 0) { |
| 828 | dma_unmap_single(port->dev, |
| 829 | atmel_port->pdc_rx[0].dma_addr, |
| 830 | PDC_BUFFER_SIZE, |
| 831 | DMA_FROM_DEVICE); |
| 832 | kfree(atmel_port->pdc_rx[0].buf); |
| 833 | } |
| 834 | free_irq(port->irq, port); |
| 835 | return -ENOMEM; |
| 836 | } |
| 837 | pdc->dma_addr = dma_map_single(port->dev, |
| 838 | pdc->buf, |
| 839 | PDC_BUFFER_SIZE, |
| 840 | DMA_FROM_DEVICE); |
| 841 | pdc->dma_size = PDC_BUFFER_SIZE; |
| 842 | pdc->ofs = 0; |
| 843 | } |
| 844 | |
| 845 | atmel_port->pdc_rx_idx = 0; |
| 846 | |
| 847 | UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); |
| 848 | UART_PUT_RCR(port, PDC_BUFFER_SIZE); |
| 849 | |
| 850 | UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); |
| 851 | UART_PUT_RNCR(port, PDC_BUFFER_SIZE); |
| 852 | } |
| 853 | if (atmel_use_dma_tx(port)) { |
| 854 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 855 | struct circ_buf *xmit = &port->info->xmit; |
| 856 | |
| 857 | pdc->buf = xmit->buf; |
| 858 | pdc->dma_addr = dma_map_single(port->dev, |
| 859 | pdc->buf, |
| 860 | UART_XMIT_SIZE, |
| 861 | DMA_TO_DEVICE); |
| 862 | pdc->dma_size = UART_XMIT_SIZE; |
| 863 | pdc->ofs = 0; |
| 864 | } |
| 865 | |
| 866 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 867 | * If there is a specific "open" function (to register |
| 868 | * control line interrupts) |
| 869 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 870 | if (atmel_open_hook) { |
| 871 | retval = atmel_open_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 872 | if (retval) { |
| 873 | free_irq(port->irq, port); |
| 874 | return retval; |
| 875 | } |
| 876 | } |
| 877 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 878 | /* |
| 879 | * Finally, enable the serial port |
| 880 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 881 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 882 | /* enable xmit & rcvr */ |
| 883 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 884 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 885 | if (atmel_use_dma_rx(port)) { |
| 886 | /* set UART timeout */ |
| 887 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); |
| 888 | UART_PUT_CR(port, ATMEL_US_STTTO); |
| 889 | |
| 890 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
| 891 | /* enable PDC controller */ |
| 892 | UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); |
| 893 | } else { |
| 894 | /* enable receive only */ |
| 895 | UART_PUT_IER(port, ATMEL_US_RXRDY); |
| 896 | } |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 897 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Disable the port |
| 903 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 904 | static void atmel_shutdown(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 905 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 906 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 907 | /* |
| 908 | * Ensure everything is stopped. |
| 909 | */ |
| 910 | atmel_stop_rx(port); |
| 911 | atmel_stop_tx(port); |
| 912 | |
| 913 | /* |
| 914 | * Shut-down the DMA. |
| 915 | */ |
| 916 | if (atmel_use_dma_rx(port)) { |
| 917 | int i; |
| 918 | |
| 919 | for (i = 0; i < 2; i++) { |
| 920 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; |
| 921 | |
| 922 | dma_unmap_single(port->dev, |
| 923 | pdc->dma_addr, |
| 924 | pdc->dma_size, |
| 925 | DMA_FROM_DEVICE); |
| 926 | kfree(pdc->buf); |
| 927 | } |
| 928 | } |
| 929 | if (atmel_use_dma_tx(port)) { |
| 930 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 931 | |
| 932 | dma_unmap_single(port->dev, |
| 933 | pdc->dma_addr, |
| 934 | pdc->dma_size, |
| 935 | DMA_TO_DEVICE); |
| 936 | } |
| 937 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 938 | /* |
| 939 | * Disable all interrupts, port and break condition. |
| 940 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 941 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 942 | UART_PUT_IDR(port, -1); |
| 943 | |
| 944 | /* |
| 945 | * Free the interrupt |
| 946 | */ |
| 947 | free_irq(port->irq, port); |
| 948 | |
| 949 | /* |
| 950 | * If there is a specific "close" function (to unregister |
| 951 | * control line interrupts) |
| 952 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 953 | if (atmel_close_hook) |
| 954 | atmel_close_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Power / Clock management. |
| 959 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 960 | static void atmel_serial_pm(struct uart_port *port, unsigned int state, |
| 961 | unsigned int oldstate) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 962 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 963 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 964 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 965 | switch (state) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 966 | case 0: |
| 967 | /* |
| 968 | * Enable the peripheral clock for this serial port. |
| 969 | * This is called on uart_open() or a resume event. |
| 970 | */ |
| 971 | clk_enable(atmel_port->clk); |
| 972 | break; |
| 973 | case 3: |
| 974 | /* |
| 975 | * Disable the peripheral clock for this serial port. |
| 976 | * This is called on uart_close() or a suspend event. |
| 977 | */ |
| 978 | clk_disable(atmel_port->clk); |
| 979 | break; |
| 980 | default: |
| 981 | printk(KERN_ERR "atmel_serial: unknown pm %d\n", state); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Change the port parameters |
| 987 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 988 | static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, |
| 989 | struct ktermios *old) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 990 | { |
| 991 | unsigned long flags; |
| 992 | unsigned int mode, imr, quot, baud; |
| 993 | |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 994 | /* Get current mode register */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 995 | mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL |
| 996 | | ATMEL_US_NBSTOP | ATMEL_US_PAR); |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 997 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 998 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 999 | quot = uart_get_divisor(port, baud); |
| 1000 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1001 | if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 1002 | quot /= 8; |
| 1003 | mode |= ATMEL_US_USCLKS_MCK_DIV8; |
| 1004 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* byte size */ |
| 1007 | switch (termios->c_cflag & CSIZE) { |
| 1008 | case CS5: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1009 | mode |= ATMEL_US_CHRL_5; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | case CS6: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1012 | mode |= ATMEL_US_CHRL_6; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | case CS7: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1015 | mode |= ATMEL_US_CHRL_7; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | default: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1018 | mode |= ATMEL_US_CHRL_8; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | /* stop bits */ |
| 1023 | if (termios->c_cflag & CSTOPB) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1024 | mode |= ATMEL_US_NBSTOP_2; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* parity */ |
| 1027 | if (termios->c_cflag & PARENB) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1028 | /* Mark or Space parity */ |
| 1029 | if (termios->c_cflag & CMSPAR) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1030 | if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1031 | mode |= ATMEL_US_PAR_MARK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1032 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1033 | mode |= ATMEL_US_PAR_SPACE; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1034 | } else if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1035 | mode |= ATMEL_US_PAR_ODD; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1036 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1037 | mode |= ATMEL_US_PAR_EVEN; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1038 | } else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1039 | mode |= ATMEL_US_PAR_NONE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1040 | |
| 1041 | spin_lock_irqsave(&port->lock, flags); |
| 1042 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1043 | port->read_status_mask = ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1044 | if (termios->c_iflag & INPCK) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1045 | port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1046 | if (termios->c_iflag & (BRKINT | PARMRK)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1047 | port->read_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1048 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1049 | if (atmel_use_dma_rx(port)) |
| 1050 | /* need to enable error interrupts */ |
| 1051 | UART_PUT_IER(port, port->read_status_mask); |
| 1052 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1053 | /* |
| 1054 | * Characters to ignore |
| 1055 | */ |
| 1056 | port->ignore_status_mask = 0; |
| 1057 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1058 | port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1059 | if (termios->c_iflag & IGNBRK) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1060 | port->ignore_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1061 | /* |
| 1062 | * If we're ignoring parity and break indicators, |
| 1063 | * ignore overruns too (for real raw support). |
| 1064 | */ |
| 1065 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1066 | port->ignore_status_mask |= ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1067 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1068 | /* TODO: Ignore all characters if CREAD is set.*/ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1069 | |
| 1070 | /* update the per-port timeout */ |
| 1071 | uart_update_timeout(port, termios->c_cflag, baud); |
| 1072 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1073 | /* save/disable interrupts and drain transmitter */ |
| 1074 | imr = UART_GET_IMR(port); |
| 1075 | UART_PUT_IDR(port, -1); |
| 1076 | while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) |
Haavard Skinnemoen | 829dd81 | 2008-02-08 04:21:02 -0800 | [diff] [blame] | 1077 | cpu_relax(); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* disable receiver and transmitter */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1080 | UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* set the parity, stop bits and data size */ |
| 1083 | UART_PUT_MR(port, mode); |
| 1084 | |
| 1085 | /* set the baud rate */ |
| 1086 | UART_PUT_BRGR(port, quot); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1087 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 1088 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* restore interrupts */ |
| 1091 | UART_PUT_IER(port, imr); |
| 1092 | |
| 1093 | /* CTS flow-control and modem-status interrupts */ |
| 1094 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 1095 | port->ops->enable_ms(port); |
| 1096 | |
| 1097 | spin_unlock_irqrestore(&port->lock, flags); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Return string describing the specified port |
| 1102 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1103 | static const char *atmel_type(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1104 | { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1105 | return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Release the memory region(s) being used by 'port'. |
| 1110 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1111 | static void atmel_release_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1112 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1113 | struct platform_device *pdev = to_platform_device(port->dev); |
| 1114 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
| 1115 | |
| 1116 | release_mem_region(port->mapbase, size); |
| 1117 | |
| 1118 | if (port->flags & UPF_IOREMAP) { |
| 1119 | iounmap(port->membase); |
| 1120 | port->membase = NULL; |
| 1121 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Request the memory region(s) being used by 'port'. |
| 1126 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1127 | static int atmel_request_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1128 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1129 | struct platform_device *pdev = to_platform_device(port->dev); |
| 1130 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1131 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1132 | if (!request_mem_region(port->mapbase, size, "atmel_serial")) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1133 | return -EBUSY; |
| 1134 | |
| 1135 | if (port->flags & UPF_IOREMAP) { |
| 1136 | port->membase = ioremap(port->mapbase, size); |
| 1137 | if (port->membase == NULL) { |
| 1138 | release_mem_region(port->mapbase, size); |
| 1139 | return -ENOMEM; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | return 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Configure/autoconfigure the port. |
| 1148 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1149 | static void atmel_config_port(struct uart_port *port, int flags) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1150 | { |
| 1151 | if (flags & UART_CONFIG_TYPE) { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1152 | port->type = PORT_ATMEL; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1153 | atmel_request_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Verify the new serial_struct (for TIOCSSERIAL). |
| 1159 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1160 | 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] | 1161 | { |
| 1162 | int ret = 0; |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1163 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1164 | ret = -EINVAL; |
| 1165 | if (port->irq != ser->irq) |
| 1166 | ret = -EINVAL; |
| 1167 | if (ser->io_type != SERIAL_IO_MEM) |
| 1168 | ret = -EINVAL; |
| 1169 | if (port->uartclk / 16 != ser->baud_base) |
| 1170 | ret = -EINVAL; |
| 1171 | if ((void *)port->mapbase != ser->iomem_base) |
| 1172 | ret = -EINVAL; |
| 1173 | if (port->iobase != ser->port) |
| 1174 | ret = -EINVAL; |
| 1175 | if (ser->hub6 != 0) |
| 1176 | ret = -EINVAL; |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1180 | static struct uart_ops atmel_pops = { |
| 1181 | .tx_empty = atmel_tx_empty, |
| 1182 | .set_mctrl = atmel_set_mctrl, |
| 1183 | .get_mctrl = atmel_get_mctrl, |
| 1184 | .stop_tx = atmel_stop_tx, |
| 1185 | .start_tx = atmel_start_tx, |
| 1186 | .stop_rx = atmel_stop_rx, |
| 1187 | .enable_ms = atmel_enable_ms, |
| 1188 | .break_ctl = atmel_break_ctl, |
| 1189 | .startup = atmel_startup, |
| 1190 | .shutdown = atmel_shutdown, |
| 1191 | .set_termios = atmel_set_termios, |
| 1192 | .type = atmel_type, |
| 1193 | .release_port = atmel_release_port, |
| 1194 | .request_port = atmel_request_port, |
| 1195 | .config_port = atmel_config_port, |
| 1196 | .verify_port = atmel_verify_port, |
| 1197 | .pm = atmel_serial_pm, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1198 | }; |
| 1199 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1200 | /* |
| 1201 | * Configure the port from the platform device resource info. |
| 1202 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1203 | static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, |
| 1204 | struct platform_device *pdev) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1205 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1206 | struct uart_port *port = &atmel_port->uart; |
Haavard Skinnemoen | 73e2798 | 2006-10-04 16:02:04 +0200 | [diff] [blame] | 1207 | struct atmel_uart_data *data = pdev->dev.platform_data; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1208 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1209 | port->iotype = UPIO_MEM; |
Andrew Victor | a14d527 | 2007-01-09 09:03:42 +0100 | [diff] [blame] | 1210 | port->flags = UPF_BOOT_AUTOCONF; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1211 | port->ops = &atmel_pops; |
Andrew Victor | a14d527 | 2007-01-09 09:03:42 +0100 | [diff] [blame] | 1212 | port->fifosize = 1; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1213 | port->line = pdev->id; |
| 1214 | port->dev = &pdev->dev; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1215 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1216 | port->mapbase = pdev->resource[0].start; |
| 1217 | port->irq = pdev->resource[1].start; |
| 1218 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1219 | tasklet_init(&atmel_port->tasklet, atmel_tasklet_func, |
| 1220 | (unsigned long)port); |
| 1221 | |
| 1222 | memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); |
| 1223 | |
Haavard Skinnemoen | 75d3521 | 2006-10-04 16:02:08 +0200 | [diff] [blame] | 1224 | if (data->regs) |
| 1225 | /* Already mapped by setup code */ |
| 1226 | port->membase = data->regs; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1227 | else { |
| 1228 | port->flags |= UPF_IOREMAP; |
| 1229 | port->membase = NULL; |
| 1230 | } |
| 1231 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1232 | /* for console, the clock could already be configured */ |
| 1233 | if (!atmel_port->clk) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1234 | atmel_port->clk = clk_get(&pdev->dev, "usart"); |
| 1235 | clk_enable(atmel_port->clk); |
| 1236 | port->uartclk = clk_get_rate(atmel_port->clk); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1237 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1238 | |
| 1239 | atmel_port->use_dma_rx = data->use_dma_rx; |
| 1240 | atmel_port->use_dma_tx = data->use_dma_tx; |
| 1241 | if (atmel_use_dma_tx(port)) |
| 1242 | port->fifosize = PDC_BUFFER_SIZE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1245 | /* |
| 1246 | * Register board-specific modem-control line handlers. |
| 1247 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 1248 | void __init atmel_register_uart_fns(struct atmel_port_fns *fns) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1249 | { |
| 1250 | if (fns->enable_ms) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1251 | atmel_pops.enable_ms = fns->enable_ms; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1252 | if (fns->get_mctrl) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1253 | atmel_pops.get_mctrl = fns->get_mctrl; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1254 | if (fns->set_mctrl) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1255 | atmel_pops.set_mctrl = fns->set_mctrl; |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 1256 | atmel_open_hook = fns->open; |
| 1257 | atmel_close_hook = fns->close; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1258 | atmel_pops.pm = fns->pm; |
| 1259 | atmel_pops.set_wake = fns->set_wake; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 1262 | #ifdef CONFIG_SERIAL_ATMEL_CONSOLE |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1263 | static void atmel_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 1264 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1265 | while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) |
Haavard Skinnemoen | 829dd81 | 2008-02-08 04:21:02 -0800 | [diff] [blame] | 1266 | cpu_relax(); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 1267 | UART_PUT_CHAR(port, ch); |
| 1268 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* |
| 1271 | * Interrupts are disabled on entering |
| 1272 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1273 | 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] | 1274 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1275 | struct uart_port *port = &atmel_ports[co->index].uart; |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 1276 | unsigned int status, imr; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1277 | |
| 1278 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1279 | * First, save IMR and then disable interrupts |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1280 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1281 | imr = UART_GET_IMR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1282 | UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1283 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1284 | uart_console_write(port, s, count, atmel_console_putchar); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1285 | |
| 1286 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1287 | * Finally, wait for transmitter to become empty |
| 1288 | * and restore IMR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1289 | */ |
| 1290 | do { |
| 1291 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1292 | } while (!(status & ATMEL_US_TXRDY)); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1293 | /* set interrupts back the way they were */ |
| 1294 | UART_PUT_IER(port, imr); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1298 | * If the port was already initialised (eg, by a boot loader), |
| 1299 | * try to determine the current setup. |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1300 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1301 | static void __init atmel_console_get_options(struct uart_port *port, int *baud, |
| 1302 | int *parity, int *bits) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1303 | { |
| 1304 | unsigned int mr, quot; |
| 1305 | |
Haavard Skinnemoen | 1c0fd82 | 2008-02-08 04:21:03 -0800 | [diff] [blame] | 1306 | /* |
| 1307 | * If the baud rate generator isn't running, the port wasn't |
| 1308 | * initialized by the boot loader. |
| 1309 | */ |
| 1310 | quot = UART_GET_BRGR(port); |
| 1311 | if (!quot) |
| 1312 | return; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1313 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1314 | mr = UART_GET_MR(port) & ATMEL_US_CHRL; |
| 1315 | if (mr == ATMEL_US_CHRL_8) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1316 | *bits = 8; |
| 1317 | else |
| 1318 | *bits = 7; |
| 1319 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1320 | mr = UART_GET_MR(port) & ATMEL_US_PAR; |
| 1321 | if (mr == ATMEL_US_PAR_EVEN) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1322 | *parity = 'e'; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1323 | else if (mr == ATMEL_US_PAR_ODD) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1324 | *parity = 'o'; |
| 1325 | |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 1326 | /* |
| 1327 | * The serial core only rounds down when matching this to a |
| 1328 | * supported baud rate. Make sure we don't end up slightly |
| 1329 | * lower than one of those, as it would make us fall through |
| 1330 | * to a much lower baud rate than we really want. |
| 1331 | */ |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 1332 | *baud = port->uartclk / (16 * (quot - 1)); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1335 | static int __init atmel_console_setup(struct console *co, char *options) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1336 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1337 | struct uart_port *port = &atmel_ports[co->index].uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1338 | int baud = 115200; |
| 1339 | int bits = 8; |
| 1340 | int parity = 'n'; |
| 1341 | int flow = 'n'; |
| 1342 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1343 | if (port->membase == NULL) { |
| 1344 | /* Port not initialized yet - delay setup */ |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1345 | return -ENODEV; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1346 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1347 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1348 | UART_PUT_IDR(port, -1); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1349 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 1350 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (options) |
| 1353 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1354 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1355 | atmel_console_get_options(port, &baud, &parity, &bits); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1356 | |
| 1357 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 1358 | } |
| 1359 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1360 | static struct uart_driver atmel_uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1361 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1362 | static struct console atmel_console = { |
| 1363 | .name = ATMEL_DEVICENAME, |
| 1364 | .write = atmel_console_write, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1365 | .device = uart_console_device, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1366 | .setup = atmel_console_setup, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1367 | .flags = CON_PRINTBUFFER, |
| 1368 | .index = -1, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1369 | .data = &atmel_uart, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1370 | }; |
| 1371 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1372 | #define ATMEL_CONSOLE_DEVICE &atmel_console |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1373 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1374 | /* |
| 1375 | * Early console initialization (before VM subsystem initialized). |
| 1376 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1377 | static int __init atmel_console_init(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1378 | { |
Haavard Skinnemoen | 73e2798 | 2006-10-04 16:02:04 +0200 | [diff] [blame] | 1379 | if (atmel_default_console_device) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1380 | add_preferred_console(ATMEL_DEVICENAME, |
| 1381 | atmel_default_console_device->id, NULL); |
| 1382 | atmel_init_port(&atmel_ports[atmel_default_console_device->id], |
| 1383 | atmel_default_console_device); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1384 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1385 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1386 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1387 | return 0; |
| 1388 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1389 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1390 | console_initcall(atmel_console_init); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1391 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1392 | /* |
| 1393 | * Late console initialization. |
| 1394 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1395 | static int __init atmel_late_console_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1396 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1397 | if (atmel_default_console_device |
| 1398 | && !(atmel_console.flags & CON_ENABLED)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1399 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1400 | |
| 1401 | return 0; |
| 1402 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1403 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1404 | core_initcall(atmel_late_console_init); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1405 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1406 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 1407 | { |
| 1408 | return port->cons && port->cons->index == port->line; |
| 1409 | } |
| 1410 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1411 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1412 | #define ATMEL_CONSOLE_DEVICE NULL |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1413 | |
| 1414 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 1415 | { |
| 1416 | return false; |
| 1417 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1418 | #endif |
| 1419 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1420 | static struct uart_driver atmel_uart = { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1421 | .owner = THIS_MODULE, |
| 1422 | .driver_name = "atmel_serial", |
| 1423 | .dev_name = ATMEL_DEVICENAME, |
| 1424 | .major = SERIAL_ATMEL_MAJOR, |
| 1425 | .minor = MINOR_START, |
| 1426 | .nr = ATMEL_MAX_UART, |
| 1427 | .cons = ATMEL_CONSOLE_DEVICE, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1428 | }; |
| 1429 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1430 | #ifdef CONFIG_PM |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1431 | static int atmel_serial_suspend(struct platform_device *pdev, |
| 1432 | pm_message_t state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1433 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1434 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1435 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1436 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1437 | if (device_may_wakeup(&pdev->dev) |
| 1438 | && !at91_suspend_entering_slow_clock()) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1439 | enable_irq_wake(port->irq); |
| 1440 | else { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1441 | uart_suspend_port(&atmel_uart, port); |
| 1442 | atmel_port->suspended = 1; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1448 | static int atmel_serial_resume(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1449 | { |
| 1450 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1451 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1452 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1453 | if (atmel_port->suspended) { |
| 1454 | uart_resume_port(&atmel_uart, port); |
| 1455 | atmel_port->suspended = 0; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1456 | } else |
Andrew Victor | 9b93816 | 2007-01-09 13:20:54 +0100 | [diff] [blame] | 1457 | disable_irq_wake(port->irq); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1462 | #define atmel_serial_suspend NULL |
| 1463 | #define atmel_serial_resume NULL |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1464 | #endif |
| 1465 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1466 | static int __devinit atmel_serial_probe(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1467 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1468 | struct atmel_uart_port *port; |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1469 | void *data; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1470 | int ret; |
| 1471 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1472 | BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE)); |
| 1473 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1474 | port = &atmel_ports[pdev->id]; |
| 1475 | atmel_init_port(port, pdev); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1476 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1477 | if (!atmel_use_dma_rx(&port->uart)) { |
| 1478 | ret = -ENOMEM; |
Haavard Skinnemoen | 6433471 | 2008-02-08 04:21:07 -0800 | [diff] [blame] | 1479 | data = kmalloc(sizeof(struct atmel_uart_char) |
| 1480 | * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1481 | if (!data) |
| 1482 | goto err_alloc_ring; |
| 1483 | port->rx_ring.buf = data; |
| 1484 | } |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1485 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1486 | ret = uart_add_one_port(&atmel_uart, &port->uart); |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1487 | if (ret) |
| 1488 | goto err_add_port; |
| 1489 | |
| 1490 | device_init_wakeup(&pdev->dev, 1); |
| 1491 | platform_set_drvdata(pdev, port); |
| 1492 | |
| 1493 | return 0; |
| 1494 | |
| 1495 | err_add_port: |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1496 | kfree(port->rx_ring.buf); |
| 1497 | port->rx_ring.buf = NULL; |
| 1498 | err_alloc_ring: |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1499 | if (!atmel_is_console_port(&port->uart)) { |
| 1500 | clk_disable(port->clk); |
| 1501 | clk_put(port->clk); |
| 1502 | port->clk = NULL; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1508 | static int __devexit atmel_serial_remove(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1509 | { |
| 1510 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1511 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1512 | int ret = 0; |
| 1513 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1514 | device_init_wakeup(&pdev->dev, 0); |
| 1515 | platform_set_drvdata(pdev, NULL); |
| 1516 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1517 | ret = uart_remove_one_port(&atmel_uart, port); |
| 1518 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1519 | tasklet_kill(&atmel_port->tasklet); |
| 1520 | kfree(atmel_port->rx_ring.buf); |
| 1521 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 1522 | /* "port" is allocated statically, so we shouldn't free it */ |
| 1523 | |
| 1524 | clk_disable(atmel_port->clk); |
| 1525 | clk_put(atmel_port->clk); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1526 | |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1530 | static struct platform_driver atmel_serial_driver = { |
| 1531 | .probe = atmel_serial_probe, |
| 1532 | .remove = __devexit_p(atmel_serial_remove), |
| 1533 | .suspend = atmel_serial_suspend, |
| 1534 | .resume = atmel_serial_resume, |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1535 | .driver = { |
Haavard Skinnemoen | 1e8ea80 | 2006-10-04 16:02:03 +0200 | [diff] [blame] | 1536 | .name = "atmel_usart", |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1537 | .owner = THIS_MODULE, |
| 1538 | }, |
| 1539 | }; |
| 1540 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1541 | static int __init atmel_serial_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1542 | { |
| 1543 | int ret; |
| 1544 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1545 | ret = uart_register_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1546 | if (ret) |
| 1547 | return ret; |
| 1548 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1549 | ret = platform_driver_register(&atmel_serial_driver); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1550 | if (ret) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1551 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1552 | |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1556 | static void __exit atmel_serial_exit(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1557 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1558 | platform_driver_unregister(&atmel_serial_driver); |
| 1559 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1562 | module_init(atmel_serial_init); |
| 1563 | module_exit(atmel_serial_exit); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1564 | |
| 1565 | MODULE_AUTHOR("Rick Bronson"); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1566 | MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1567 | MODULE_LICENSE("GPL"); |