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