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