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