Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1 | /* |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2 | * Driver for Atmel AT91 / AT32 Serial ports |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003 Rick Bronson |
| 4 | * |
| 5 | * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 7 | * |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 8 | * DMA support added by Chip Coldwell. |
| 9 | * |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 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> |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 36 | #include <linux/of.h> |
| 37 | #include <linux/of_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> |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
Jean-Christophe PLAGNIOL-VILLARD | bcd2360 | 2012-10-30 05:12:23 +0800 | [diff] [blame] | 42 | #include <linux/platform_data/atmel.h> |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 43 | #include <linux/timer.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 44 | |
| 45 | #include <asm/io.h> |
Peter Huewe | f7512e7 | 2010-06-29 19:35:39 +0200 | [diff] [blame] | 46 | #include <asm/ioctls.h> |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 47 | |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_ARM |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 49 | #include <mach/cpu.h> |
Russell King | 60e8972 | 2011-07-26 10:56:19 +0100 | [diff] [blame] | 50 | #include <asm/gpio.h> |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 51 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 52 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 53 | #define PDC_BUFFER_SIZE 512 |
| 54 | /* Revisit: We should calculate this based on the actual port settings */ |
| 55 | #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ |
| 56 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 57 | #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 58 | #define SUPPORT_SYSRQ |
| 59 | #endif |
| 60 | |
| 61 | #include <linux/serial_core.h> |
| 62 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 63 | static void atmel_start_rx(struct uart_port *port); |
| 64 | static void atmel_stop_rx(struct uart_port *port); |
| 65 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_SERIAL_ATMEL_TTYAT |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 67 | |
| 68 | /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we |
| 69 | * should coexist with the 8250 driver, such as if we have an external 16C550 |
| 70 | * UART. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 71 | #define SERIAL_ATMEL_MAJOR 204 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 72 | #define MINOR_START 154 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 73 | #define ATMEL_DEVICENAME "ttyAT" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 74 | |
| 75 | #else |
| 76 | |
| 77 | /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port |
| 78 | * name, but it is legally reserved for the 8250 driver. */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 79 | #define SERIAL_ATMEL_MAJOR TTY_MAJOR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 80 | #define MINOR_START 64 |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 81 | #define ATMEL_DEVICENAME "ttyS" |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 82 | |
| 83 | #endif |
| 84 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 85 | #define ATMEL_ISR_PASS_LIMIT 256 |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 86 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 87 | /* UART registers. CR is write-only, hence no GET macro */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 88 | #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) |
| 89 | #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) |
| 90 | #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) |
| 91 | #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) |
| 92 | #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) |
| 93 | #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) |
| 94 | #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) |
| 95 | #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) |
| 96 | #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) |
| 97 | #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) |
| 98 | #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) |
| 99 | #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 100 | #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 101 | #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME) |
Nicolas Ferre | 731d9ca | 2013-10-17 17:37:12 +0200 | [diff] [blame] | 102 | #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 103 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 104 | /* PDC registers */ |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 105 | #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) |
| 106 | #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 107 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 108 | #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) |
| 109 | #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) |
| 110 | #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) |
| 111 | #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) |
| 112 | #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] | 113 | |
Haavard Skinnemoen | 544fc72 | 2007-02-12 00:52:15 -0800 | [diff] [blame] | 114 | #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) |
| 115 | #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] | 116 | #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 117 | |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 118 | static int (*atmel_open_hook)(struct uart_port *); |
| 119 | static void (*atmel_close_hook)(struct uart_port *); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 120 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 121 | struct atmel_dma_buffer { |
| 122 | unsigned char *buf; |
| 123 | dma_addr_t dma_addr; |
| 124 | unsigned int dma_size; |
| 125 | unsigned int ofs; |
| 126 | }; |
| 127 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 128 | struct atmel_uart_char { |
| 129 | u16 status; |
| 130 | u16 ch; |
| 131 | }; |
| 132 | |
| 133 | #define ATMEL_SERIAL_RINGSIZE 1024 |
| 134 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 135 | /* |
| 136 | * We wrap our port structure around the generic uart_port. |
| 137 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 138 | struct atmel_uart_port { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 139 | struct uart_port uart; /* uart */ |
| 140 | struct clk *clk; /* uart clock */ |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 141 | int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ |
| 142 | u32 backup_imr; /* IMR saved during suspend */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 143 | int break_active; /* break being received */ |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 144 | |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 145 | bool use_dma_rx; /* enable DMA receiver */ |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 146 | bool use_pdc_rx; /* enable PDC receiver */ |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 147 | short pdc_rx_idx; /* current PDC RX buffer */ |
| 148 | struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ |
| 149 | |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 150 | bool use_dma_tx; /* enable DMA transmitter */ |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 151 | bool use_pdc_tx; /* enable PDC transmitter */ |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 152 | struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ |
| 153 | |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 154 | spinlock_t lock_tx; /* port lock */ |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 155 | spinlock_t lock_rx; /* port lock */ |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 156 | struct dma_chan *chan_tx; |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 157 | struct dma_chan *chan_rx; |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 158 | struct dma_async_tx_descriptor *desc_tx; |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 159 | struct dma_async_tx_descriptor *desc_rx; |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 160 | dma_cookie_t cookie_tx; |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 161 | dma_cookie_t cookie_rx; |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 162 | struct scatterlist sg_tx; |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 163 | struct scatterlist sg_rx; |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 164 | struct tasklet_struct tasklet; |
| 165 | unsigned int irq_status; |
| 166 | unsigned int irq_status_prev; |
| 167 | |
| 168 | struct circ_buf rx_ring; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 169 | |
| 170 | struct serial_rs485 rs485; /* rs485 settings */ |
| 171 | unsigned int tx_done_mask; |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 172 | bool is_usart; /* usart or uart */ |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 173 | struct timer_list uart_timer; /* uart timer */ |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 174 | int (*prepare_rx)(struct uart_port *port); |
| 175 | int (*prepare_tx)(struct uart_port *port); |
| 176 | void (*schedule_rx)(struct uart_port *port); |
| 177 | void (*schedule_tx)(struct uart_port *port); |
| 178 | void (*release_rx)(struct uart_port *port); |
| 179 | void (*release_tx)(struct uart_port *port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 182 | static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; |
Pawel Wieczorkiewicz | 503bded | 2013-02-20 17:26:20 +0100 | [diff] [blame] | 183 | static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 184 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 185 | #ifdef SUPPORT_SYSRQ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 186 | static struct console atmel_console; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 187 | #endif |
| 188 | |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 189 | #if defined(CONFIG_OF) |
| 190 | static const struct of_device_id atmel_serial_dt_ids[] = { |
| 191 | { .compatible = "atmel,at91rm9200-usart" }, |
| 192 | { .compatible = "atmel,at91sam9260-usart" }, |
| 193 | { /* sentinel */ } |
| 194 | }; |
| 195 | |
| 196 | MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids); |
| 197 | #endif |
| 198 | |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 199 | static inline struct atmel_uart_port * |
| 200 | to_atmel_uart_port(struct uart_port *uart) |
| 201 | { |
| 202 | return container_of(uart, struct atmel_uart_port, uart); |
| 203 | } |
| 204 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 205 | #ifdef CONFIG_SERIAL_ATMEL_PDC |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 206 | static bool atmel_use_pdc_rx(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 207 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 208 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 209 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 210 | return atmel_port->use_pdc_rx; |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 213 | static bool atmel_use_pdc_tx(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 214 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 215 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 216 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 217 | return atmel_port->use_pdc_tx; |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 218 | } |
| 219 | #else |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 220 | static bool atmel_use_pdc_rx(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 221 | { |
| 222 | return false; |
| 223 | } |
| 224 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 225 | static bool atmel_use_pdc_tx(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 226 | { |
| 227 | return false; |
| 228 | } |
| 229 | #endif |
| 230 | |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 231 | static bool atmel_use_dma_tx(struct uart_port *port) |
| 232 | { |
| 233 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 234 | |
| 235 | return atmel_port->use_dma_tx; |
| 236 | } |
| 237 | |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 238 | static bool atmel_use_dma_rx(struct uart_port *port) |
| 239 | { |
| 240 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 241 | |
| 242 | return atmel_port->use_dma_rx; |
| 243 | } |
| 244 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 245 | /* Enable or disable the rs485 support */ |
| 246 | void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) |
| 247 | { |
| 248 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 249 | unsigned int mode; |
Claudio Scordino | dbf1115 | 2011-12-16 15:08:49 +0100 | [diff] [blame] | 250 | unsigned long flags; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 251 | |
Claudio Scordino | dbf1115 | 2011-12-16 15:08:49 +0100 | [diff] [blame] | 252 | spin_lock_irqsave(&port->lock, flags); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 253 | |
| 254 | /* Disable interrupts */ |
| 255 | UART_PUT_IDR(port, atmel_port->tx_done_mask); |
| 256 | |
| 257 | mode = UART_GET_MR(port); |
| 258 | |
| 259 | /* Resetting serial mode to RS232 (0x0) */ |
| 260 | mode &= ~ATMEL_US_USMODE; |
| 261 | |
| 262 | atmel_port->rs485 = *rs485conf; |
| 263 | |
| 264 | if (rs485conf->flags & SER_RS485_ENABLED) { |
| 265 | dev_dbg(port->dev, "Setting UART to RS485\n"); |
| 266 | atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; |
Claudio Scordino | 93f3350 | 2011-11-09 15:51:49 +0100 | [diff] [blame] | 267 | if ((rs485conf->delay_rts_after_send) > 0) |
Claudio Scordino | 1b63318 | 2010-07-20 15:26:47 -0700 | [diff] [blame] | 268 | UART_PUT_TTGR(port, rs485conf->delay_rts_after_send); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 269 | mode |= ATMEL_US_USMODE_RS485; |
| 270 | } else { |
| 271 | dev_dbg(port->dev, "Setting UART to RS232\n"); |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 272 | if (atmel_use_pdc_tx(port)) |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 273 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | |
| 274 | ATMEL_US_TXBUFE; |
| 275 | else |
| 276 | atmel_port->tx_done_mask = ATMEL_US_TXRDY; |
| 277 | } |
| 278 | UART_PUT_MR(port, mode); |
| 279 | |
| 280 | /* Enable interrupts */ |
| 281 | UART_PUT_IER(port, atmel_port->tx_done_mask); |
| 282 | |
Claudio Scordino | dbf1115 | 2011-12-16 15:08:49 +0100 | [diff] [blame] | 283 | spin_unlock_irqrestore(&port->lock, flags); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 284 | |
| 285 | } |
| 286 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 287 | /* |
| 288 | * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. |
| 289 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 290 | static u_int atmel_tx_empty(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 291 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 292 | return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Set state of the modem control output lines |
| 297 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 298 | static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 299 | { |
| 300 | unsigned int control = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 301 | unsigned int mode; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 302 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 303 | |
Andrew Victor | c2f5ccf | 2006-12-04 09:37:20 +0100 | [diff] [blame] | 304 | #ifdef CONFIG_ARCH_AT91RM9200 |
Andrew Victor | 79da7a6 | 2006-11-30 11:58:14 +0100 | [diff] [blame] | 305 | if (cpu_is_at91rm9200()) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 306 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 307 | * AT91RM9200 Errata #39: RTS0 is not internally connected |
| 308 | * to PA21. We need to drive the pin manually. |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 309 | */ |
Andrew Victor | 7272991 | 2006-09-27 09:44:11 +0100 | [diff] [blame] | 310 | if (port->mapbase == AT91RM9200_BASE_US0) { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 311 | if (mctrl & TIOCM_RTS) |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 312 | at91_set_gpio_value(AT91_PIN_PA21, 0); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 313 | else |
Andrew Victor | 20e6527 | 2006-07-05 14:31:13 +0100 | [diff] [blame] | 314 | at91_set_gpio_value(AT91_PIN_PA21, 1); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 315 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 316 | } |
Haavard Skinnemoen | acca9b8 | 2006-10-04 16:02:09 +0200 | [diff] [blame] | 317 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 318 | |
| 319 | if (mctrl & TIOCM_RTS) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 320 | control |= ATMEL_US_RTSEN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 321 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 322 | control |= ATMEL_US_RTSDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 323 | |
| 324 | if (mctrl & TIOCM_DTR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 325 | control |= ATMEL_US_DTREN; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 326 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 327 | control |= ATMEL_US_DTRDIS; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 328 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 329 | UART_PUT_CR(port, control); |
| 330 | |
| 331 | /* Local loopback mode? */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 332 | mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 333 | if (mctrl & TIOCM_LOOP) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 334 | mode |= ATMEL_US_CHMODE_LOC_LOOP; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 335 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 336 | mode |= ATMEL_US_CHMODE_NORMAL; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 337 | |
| 338 | /* Resetting serial mode to RS232 (0x0) */ |
| 339 | mode &= ~ATMEL_US_USMODE; |
| 340 | |
| 341 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) { |
| 342 | dev_dbg(port->dev, "Setting UART to RS485\n"); |
Claudio Scordino | 93f3350 | 2011-11-09 15:51:49 +0100 | [diff] [blame] | 343 | if ((atmel_port->rs485.delay_rts_after_send) > 0) |
Claudio Scordino | 1b63318 | 2010-07-20 15:26:47 -0700 | [diff] [blame] | 344 | UART_PUT_TTGR(port, |
| 345 | atmel_port->rs485.delay_rts_after_send); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 346 | mode |= ATMEL_US_USMODE_RS485; |
| 347 | } else { |
| 348 | dev_dbg(port->dev, "Setting UART to RS232\n"); |
| 349 | } |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 350 | UART_PUT_MR(port, mode); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Get state of the modem control input lines |
| 355 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 356 | static u_int atmel_get_mctrl(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 357 | { |
| 358 | unsigned int status, ret = 0; |
| 359 | |
| 360 | status = UART_GET_CSR(port); |
| 361 | |
| 362 | /* |
| 363 | * The control signals are active low. |
| 364 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 365 | if (!(status & ATMEL_US_DCD)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 366 | ret |= TIOCM_CD; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 367 | if (!(status & ATMEL_US_CTS)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 368 | ret |= TIOCM_CTS; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 369 | if (!(status & ATMEL_US_DSR)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 370 | ret |= TIOCM_DSR; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 371 | if (!(status & ATMEL_US_RI)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 372 | ret |= TIOCM_RI; |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Stop transmitting. |
| 379 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 380 | static void atmel_stop_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 381 | { |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 382 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 383 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 384 | if (atmel_use_pdc_tx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 385 | /* disable PDC transmit */ |
| 386 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 387 | } |
| 388 | /* Disable interrupts */ |
| 389 | UART_PUT_IDR(port, atmel_port->tx_done_mask); |
| 390 | |
Bernhard Roth | 83cac9f | 2011-08-24 09:48:23 +0200 | [diff] [blame] | 391 | if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && |
| 392 | !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 393 | atmel_start_rx(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Start transmitting. |
| 398 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 399 | static void atmel_start_tx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 400 | { |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 401 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 402 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 403 | if (atmel_use_pdc_tx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 404 | if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) |
| 405 | /* The transmitter is already running. Yes, we |
| 406 | really need this.*/ |
| 407 | return; |
| 408 | |
Bernhard Roth | 83cac9f | 2011-08-24 09:48:23 +0200 | [diff] [blame] | 409 | if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && |
| 410 | !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 411 | atmel_stop_rx(port); |
| 412 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 413 | /* re-enable PDC transmit */ |
| 414 | UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 415 | } |
| 416 | /* Enable interrupts */ |
| 417 | UART_PUT_IER(port, atmel_port->tx_done_mask); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * start receiving - port is in process of being opened. |
| 422 | */ |
| 423 | static void atmel_start_rx(struct uart_port *port) |
| 424 | { |
| 425 | UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */ |
| 426 | |
Siftar, Gabe | 57c3686 | 2012-03-29 15:40:05 +0200 | [diff] [blame] | 427 | UART_PUT_CR(port, ATMEL_US_RXEN); |
| 428 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 429 | if (atmel_use_pdc_rx(port)) { |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 430 | /* enable PDC controller */ |
| 431 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | |
| 432 | port->read_status_mask); |
| 433 | UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); |
| 434 | } else { |
| 435 | UART_PUT_IER(port, ATMEL_US_RXRDY); |
| 436 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Stop receiving - port is in process of being closed. |
| 441 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 442 | static void atmel_stop_rx(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 443 | { |
Siftar, Gabe | 57c3686 | 2012-03-29 15:40:05 +0200 | [diff] [blame] | 444 | UART_PUT_CR(port, ATMEL_US_RXDIS); |
| 445 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 446 | if (atmel_use_pdc_rx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 447 | /* disable PDC receive */ |
| 448 | UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 449 | UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | |
| 450 | port->read_status_mask); |
| 451 | } else { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 452 | UART_PUT_IDR(port, ATMEL_US_RXRDY); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 453 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Enable modem status interrupts |
| 458 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 459 | static void atmel_enable_ms(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 460 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 461 | UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC |
| 462 | | ATMEL_US_DCDIC | ATMEL_US_CTSIC); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Control the transmission of a break signal |
| 467 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 468 | static void atmel_break_ctl(struct uart_port *port, int break_state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 469 | { |
| 470 | if (break_state != 0) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 471 | UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 472 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 473 | UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 477 | * Stores the incoming character in the ring buffer |
| 478 | */ |
| 479 | static void |
| 480 | atmel_buffer_rx_char(struct uart_port *port, unsigned int status, |
| 481 | unsigned int ch) |
| 482 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 483 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 484 | struct circ_buf *ring = &atmel_port->rx_ring; |
| 485 | struct atmel_uart_char *c; |
| 486 | |
| 487 | if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) |
| 488 | /* Buffer overflow, ignore char */ |
| 489 | return; |
| 490 | |
| 491 | c = &((struct atmel_uart_char *)ring->buf)[ring->head]; |
| 492 | c->status = status; |
| 493 | c->ch = ch; |
| 494 | |
| 495 | /* Make sure the character is stored before we update head. */ |
| 496 | smp_wmb(); |
| 497 | |
| 498 | ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); |
| 499 | } |
| 500 | |
| 501 | /* |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 502 | * Deal with parity, framing and overrun errors. |
| 503 | */ |
| 504 | static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) |
| 505 | { |
| 506 | /* clear error */ |
| 507 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 508 | |
| 509 | if (status & ATMEL_US_RXBRK) { |
| 510 | /* ignore side-effect */ |
| 511 | status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); |
| 512 | port->icount.brk++; |
| 513 | } |
| 514 | if (status & ATMEL_US_PARE) |
| 515 | port->icount.parity++; |
| 516 | if (status & ATMEL_US_FRAME) |
| 517 | port->icount.frame++; |
| 518 | if (status & ATMEL_US_OVRE) |
| 519 | port->icount.overrun++; |
| 520 | } |
| 521 | |
| 522 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 523 | * Characters received (called from interrupt handler) |
| 524 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 525 | static void atmel_rx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 526 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 527 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 528 | unsigned int status, ch; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 529 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 530 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 531 | while (status & ATMEL_US_RXRDY) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 532 | ch = UART_GET_CHAR(port); |
| 533 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 534 | /* |
| 535 | * note that the error handling code is |
| 536 | * out of the main execution path |
| 537 | */ |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 538 | if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME |
| 539 | | ATMEL_US_OVRE | ATMEL_US_RXBRK) |
| 540 | || atmel_port->break_active)) { |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 541 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 542 | /* clear error */ |
| 543 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 544 | |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 545 | if (status & ATMEL_US_RXBRK |
| 546 | && !atmel_port->break_active) { |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 547 | atmel_port->break_active = 1; |
| 548 | UART_PUT_IER(port, ATMEL_US_RXBRK); |
Haavard Skinnemoen | 9e6077b | 2007-07-15 23:40:36 -0700 | [diff] [blame] | 549 | } else { |
| 550 | /* |
| 551 | * This is either the end-of-break |
| 552 | * condition or we've received at |
| 553 | * least one character without RXBRK |
| 554 | * being set. In both cases, the next |
| 555 | * RXBRK will indicate start-of-break. |
| 556 | */ |
| 557 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 558 | status &= ~ATMEL_US_RXBRK; |
| 559 | atmel_port->break_active = 0; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 560 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 563 | atmel_buffer_rx_char(port, status, ch); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 564 | status = UART_GET_CSR(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 567 | tasklet_schedule(&atmel_port->tasklet); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 571 | * Transmit characters (called from tasklet with TXRDY interrupt |
| 572 | * disabled) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 573 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 574 | static void atmel_tx_chars(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 575 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 576 | struct circ_buf *xmit = &port->state->xmit; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 577 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 578 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 579 | if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 580 | UART_PUT_CHAR(port, port->x_char); |
| 581 | port->icount.tx++; |
| 582 | port->x_char = 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 583 | } |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 584 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 585 | return; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 586 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 587 | while (UART_GET_CSR(port) & atmel_port->tx_done_mask) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 588 | UART_PUT_CHAR(port, xmit->buf[xmit->tail]); |
| 589 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 590 | port->icount.tx++; |
| 591 | if (uart_circ_empty(xmit)) |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 596 | uart_write_wakeup(port); |
| 597 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 598 | if (!uart_circ_empty(xmit)) |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 599 | /* Enable interrupts */ |
| 600 | UART_PUT_IER(port, atmel_port->tx_done_mask); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 603 | static void atmel_complete_tx_dma(void *arg) |
| 604 | { |
| 605 | struct atmel_uart_port *atmel_port = arg; |
| 606 | struct uart_port *port = &atmel_port->uart; |
| 607 | struct circ_buf *xmit = &port->state->xmit; |
| 608 | struct dma_chan *chan = atmel_port->chan_tx; |
| 609 | unsigned long flags; |
| 610 | |
| 611 | spin_lock_irqsave(&port->lock, flags); |
| 612 | |
| 613 | if (chan) |
| 614 | dmaengine_terminate_all(chan); |
| 615 | xmit->tail += sg_dma_len(&atmel_port->sg_tx); |
| 616 | xmit->tail &= UART_XMIT_SIZE - 1; |
| 617 | |
| 618 | port->icount.tx += sg_dma_len(&atmel_port->sg_tx); |
| 619 | |
| 620 | spin_lock_irq(&atmel_port->lock_tx); |
| 621 | async_tx_ack(atmel_port->desc_tx); |
| 622 | atmel_port->cookie_tx = -EINVAL; |
| 623 | atmel_port->desc_tx = NULL; |
| 624 | spin_unlock_irq(&atmel_port->lock_tx); |
| 625 | |
| 626 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 627 | uart_write_wakeup(port); |
| 628 | |
| 629 | /* Do we really need this? */ |
| 630 | if (!uart_circ_empty(xmit)) |
| 631 | tasklet_schedule(&atmel_port->tasklet); |
| 632 | |
| 633 | spin_unlock_irqrestore(&port->lock, flags); |
| 634 | } |
| 635 | |
| 636 | static void atmel_release_tx_dma(struct uart_port *port) |
| 637 | { |
| 638 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 639 | struct dma_chan *chan = atmel_port->chan_tx; |
| 640 | |
| 641 | if (chan) { |
| 642 | dmaengine_terminate_all(chan); |
| 643 | dma_release_channel(chan); |
| 644 | dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, |
| 645 | DMA_MEM_TO_DEV); |
| 646 | } |
| 647 | |
| 648 | atmel_port->desc_tx = NULL; |
| 649 | atmel_port->chan_tx = NULL; |
| 650 | atmel_port->cookie_tx = -EINVAL; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Called from tasklet with TXRDY interrupt is disabled. |
| 655 | */ |
| 656 | static void atmel_tx_dma(struct uart_port *port) |
| 657 | { |
| 658 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 659 | struct circ_buf *xmit = &port->state->xmit; |
| 660 | struct dma_chan *chan = atmel_port->chan_tx; |
| 661 | struct dma_async_tx_descriptor *desc; |
| 662 | struct scatterlist *sg = &atmel_port->sg_tx; |
| 663 | |
| 664 | /* Make sure we have an idle channel */ |
| 665 | if (atmel_port->desc_tx != NULL) |
| 666 | return; |
| 667 | |
| 668 | if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { |
| 669 | /* |
| 670 | * DMA is idle now. |
| 671 | * Port xmit buffer is already mapped, |
| 672 | * and it is one page... Just adjust |
| 673 | * offsets and lengths. Since it is a circular buffer, |
| 674 | * we have to transmit till the end, and then the rest. |
| 675 | * Take the port lock to get a |
| 676 | * consistent xmit buffer state. |
| 677 | */ |
| 678 | sg->offset = xmit->tail & (UART_XMIT_SIZE - 1); |
| 679 | sg_dma_address(sg) = (sg_dma_address(sg) & |
| 680 | ~(UART_XMIT_SIZE - 1)) |
| 681 | + sg->offset; |
| 682 | sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head, |
| 683 | xmit->tail, |
| 684 | UART_XMIT_SIZE); |
| 685 | BUG_ON(!sg_dma_len(sg)); |
| 686 | |
| 687 | desc = dmaengine_prep_slave_sg(chan, |
| 688 | sg, |
| 689 | 1, |
| 690 | DMA_MEM_TO_DEV, |
| 691 | DMA_PREP_INTERRUPT | |
| 692 | DMA_CTRL_ACK); |
| 693 | if (!desc) { |
| 694 | dev_err(port->dev, "Failed to send via dma!\n"); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV); |
| 699 | |
| 700 | atmel_port->desc_tx = desc; |
| 701 | desc->callback = atmel_complete_tx_dma; |
| 702 | desc->callback_param = atmel_port; |
| 703 | atmel_port->cookie_tx = dmaengine_submit(desc); |
| 704 | |
| 705 | } else { |
| 706 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) { |
| 707 | /* DMA done, stop TX, start RX for RS485 */ |
| 708 | atmel_start_rx(port); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 713 | uart_write_wakeup(port); |
| 714 | } |
| 715 | |
| 716 | static int atmel_prepare_tx_dma(struct uart_port *port) |
| 717 | { |
| 718 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 719 | dma_cap_mask_t mask; |
| 720 | struct dma_slave_config config; |
| 721 | int ret, nent; |
| 722 | |
| 723 | dma_cap_zero(mask); |
| 724 | dma_cap_set(DMA_SLAVE, mask); |
| 725 | |
| 726 | atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); |
| 727 | if (atmel_port->chan_tx == NULL) |
| 728 | goto chan_err; |
| 729 | dev_info(port->dev, "using %s for tx DMA transfers\n", |
| 730 | dma_chan_name(atmel_port->chan_tx)); |
| 731 | |
| 732 | spin_lock_init(&atmel_port->lock_tx); |
| 733 | sg_init_table(&atmel_port->sg_tx, 1); |
| 734 | /* UART circular tx buffer is an aligned page. */ |
| 735 | BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); |
| 736 | sg_set_page(&atmel_port->sg_tx, |
| 737 | virt_to_page(port->state->xmit.buf), |
| 738 | UART_XMIT_SIZE, |
| 739 | (int)port->state->xmit.buf & ~PAGE_MASK); |
| 740 | nent = dma_map_sg(port->dev, |
| 741 | &atmel_port->sg_tx, |
| 742 | 1, |
| 743 | DMA_MEM_TO_DEV); |
| 744 | |
| 745 | if (!nent) { |
| 746 | dev_dbg(port->dev, "need to release resource of dma\n"); |
| 747 | goto chan_err; |
| 748 | } else { |
| 749 | dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, |
| 750 | sg_dma_len(&atmel_port->sg_tx), |
| 751 | port->state->xmit.buf, |
| 752 | sg_dma_address(&atmel_port->sg_tx)); |
| 753 | } |
| 754 | |
| 755 | /* Configure the slave DMA */ |
| 756 | memset(&config, 0, sizeof(config)); |
| 757 | config.direction = DMA_MEM_TO_DEV; |
| 758 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 759 | config.dst_addr = port->mapbase + ATMEL_US_THR; |
| 760 | |
| 761 | ret = dmaengine_device_control(atmel_port->chan_tx, |
| 762 | DMA_SLAVE_CONFIG, |
| 763 | (unsigned long)&config); |
| 764 | if (ret) { |
| 765 | dev_err(port->dev, "DMA tx slave configuration failed\n"); |
| 766 | goto chan_err; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | |
| 771 | chan_err: |
| 772 | dev_err(port->dev, "TX channel not available, switch to pio\n"); |
| 773 | atmel_port->use_dma_tx = 0; |
| 774 | if (atmel_port->chan_tx) |
| 775 | atmel_release_tx_dma(port); |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 779 | static void atmel_flip_buffer_rx_dma(struct uart_port *port, |
| 780 | char *buf, size_t count) |
| 781 | { |
| 782 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 783 | struct tty_port *tport = &port->state->port; |
| 784 | |
| 785 | dma_sync_sg_for_cpu(port->dev, |
| 786 | &atmel_port->sg_rx, |
| 787 | 1, |
| 788 | DMA_DEV_TO_MEM); |
| 789 | |
| 790 | tty_insert_flip_string(tport, buf, count); |
| 791 | |
| 792 | dma_sync_sg_for_device(port->dev, |
| 793 | &atmel_port->sg_rx, |
| 794 | 1, |
| 795 | DMA_DEV_TO_MEM); |
| 796 | /* |
| 797 | * Drop the lock here since it might end up calling |
| 798 | * uart_start(), which takes the lock. |
| 799 | */ |
| 800 | spin_unlock(&port->lock); |
| 801 | tty_flip_buffer_push(tport); |
| 802 | spin_lock(&port->lock); |
| 803 | } |
| 804 | |
| 805 | static void atmel_complete_rx_dma(void *arg) |
| 806 | { |
| 807 | struct uart_port *port = arg; |
| 808 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 809 | |
| 810 | tasklet_schedule(&atmel_port->tasklet); |
| 811 | } |
| 812 | |
| 813 | static void atmel_release_rx_dma(struct uart_port *port) |
| 814 | { |
| 815 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 816 | struct dma_chan *chan = atmel_port->chan_rx; |
| 817 | |
| 818 | if (chan) { |
| 819 | dmaengine_terminate_all(chan); |
| 820 | dma_release_channel(chan); |
| 821 | dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, |
| 822 | DMA_DEV_TO_MEM); |
| 823 | } |
| 824 | |
| 825 | atmel_port->desc_rx = NULL; |
| 826 | atmel_port->chan_rx = NULL; |
| 827 | atmel_port->cookie_rx = -EINVAL; |
| 828 | } |
| 829 | |
| 830 | static void atmel_rx_from_dma(struct uart_port *port) |
| 831 | { |
| 832 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 833 | struct circ_buf *ring = &atmel_port->rx_ring; |
| 834 | struct dma_chan *chan = atmel_port->chan_rx; |
| 835 | struct dma_tx_state state; |
| 836 | enum dma_status dmastat; |
| 837 | size_t pending, count; |
| 838 | |
| 839 | |
| 840 | /* Reset the UART timeout early so that we don't miss one */ |
| 841 | UART_PUT_CR(port, ATMEL_US_STTTO); |
| 842 | dmastat = dmaengine_tx_status(chan, |
| 843 | atmel_port->cookie_rx, |
| 844 | &state); |
| 845 | /* Restart a new tasklet if DMA status is error */ |
| 846 | if (dmastat == DMA_ERROR) { |
| 847 | dev_dbg(port->dev, "Get residue error, restart tasklet\n"); |
| 848 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); |
| 849 | tasklet_schedule(&atmel_port->tasklet); |
| 850 | return; |
| 851 | } |
| 852 | /* current transfer size should no larger than dma buffer */ |
| 853 | pending = sg_dma_len(&atmel_port->sg_rx) - state.residue; |
| 854 | BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx)); |
| 855 | |
| 856 | /* |
| 857 | * This will take the chars we have so far, |
| 858 | * ring->head will record the transfer size, only new bytes come |
| 859 | * will insert into the framework. |
| 860 | */ |
| 861 | if (pending > ring->head) { |
| 862 | count = pending - ring->head; |
| 863 | |
| 864 | atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count); |
| 865 | |
| 866 | ring->head += count; |
| 867 | if (ring->head == sg_dma_len(&atmel_port->sg_rx)) |
| 868 | ring->head = 0; |
| 869 | |
| 870 | port->icount.rx += count; |
| 871 | } |
| 872 | |
| 873 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); |
| 874 | } |
| 875 | |
| 876 | static int atmel_prepare_rx_dma(struct uart_port *port) |
| 877 | { |
| 878 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 879 | struct dma_async_tx_descriptor *desc; |
| 880 | dma_cap_mask_t mask; |
| 881 | struct dma_slave_config config; |
| 882 | struct circ_buf *ring; |
| 883 | int ret, nent; |
| 884 | |
| 885 | ring = &atmel_port->rx_ring; |
| 886 | |
| 887 | dma_cap_zero(mask); |
| 888 | dma_cap_set(DMA_CYCLIC, mask); |
| 889 | |
| 890 | atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); |
| 891 | if (atmel_port->chan_rx == NULL) |
| 892 | goto chan_err; |
| 893 | dev_info(port->dev, "using %s for rx DMA transfers\n", |
| 894 | dma_chan_name(atmel_port->chan_rx)); |
| 895 | |
| 896 | spin_lock_init(&atmel_port->lock_rx); |
| 897 | sg_init_table(&atmel_port->sg_rx, 1); |
| 898 | /* UART circular rx buffer is an aligned page. */ |
| 899 | BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); |
| 900 | sg_set_page(&atmel_port->sg_rx, |
| 901 | virt_to_page(ring->buf), |
| 902 | ATMEL_SERIAL_RINGSIZE, |
| 903 | (int)ring->buf & ~PAGE_MASK); |
| 904 | nent = dma_map_sg(port->dev, |
| 905 | &atmel_port->sg_rx, |
| 906 | 1, |
| 907 | DMA_DEV_TO_MEM); |
| 908 | |
| 909 | if (!nent) { |
| 910 | dev_dbg(port->dev, "need to release resource of dma\n"); |
| 911 | goto chan_err; |
| 912 | } else { |
| 913 | dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, |
| 914 | sg_dma_len(&atmel_port->sg_rx), |
| 915 | ring->buf, |
| 916 | sg_dma_address(&atmel_port->sg_rx)); |
| 917 | } |
| 918 | |
| 919 | /* Configure the slave DMA */ |
| 920 | memset(&config, 0, sizeof(config)); |
| 921 | config.direction = DMA_DEV_TO_MEM; |
| 922 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 923 | config.src_addr = port->mapbase + ATMEL_US_RHR; |
| 924 | |
| 925 | ret = dmaengine_device_control(atmel_port->chan_rx, |
| 926 | DMA_SLAVE_CONFIG, |
| 927 | (unsigned long)&config); |
| 928 | if (ret) { |
| 929 | dev_err(port->dev, "DMA rx slave configuration failed\n"); |
| 930 | goto chan_err; |
| 931 | } |
| 932 | /* |
| 933 | * Prepare a cyclic dma transfer, assign 2 descriptors, |
| 934 | * each one is half ring buffer size |
| 935 | */ |
| 936 | desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, |
| 937 | sg_dma_address(&atmel_port->sg_rx), |
| 938 | sg_dma_len(&atmel_port->sg_rx), |
| 939 | sg_dma_len(&atmel_port->sg_rx)/2, |
| 940 | DMA_DEV_TO_MEM, |
| 941 | DMA_PREP_INTERRUPT); |
| 942 | desc->callback = atmel_complete_rx_dma; |
| 943 | desc->callback_param = port; |
| 944 | atmel_port->desc_rx = desc; |
| 945 | atmel_port->cookie_rx = dmaengine_submit(desc); |
| 946 | |
| 947 | return 0; |
| 948 | |
| 949 | chan_err: |
| 950 | dev_err(port->dev, "RX channel not available, switch to pio\n"); |
| 951 | atmel_port->use_dma_rx = 0; |
| 952 | if (atmel_port->chan_rx) |
| 953 | atmel_release_rx_dma(port); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 957 | static void atmel_uart_timer_callback(unsigned long data) |
| 958 | { |
| 959 | struct uart_port *port = (void *)data; |
| 960 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 961 | |
| 962 | tasklet_schedule(&atmel_port->tasklet); |
| 963 | mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port)); |
| 964 | } |
| 965 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 966 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 967 | * receive interrupt handler. |
| 968 | */ |
| 969 | static void |
| 970 | atmel_handle_receive(struct uart_port *port, unsigned int pending) |
| 971 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 972 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 973 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 974 | if (atmel_use_pdc_rx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 975 | /* |
| 976 | * PDC receive. Just schedule the tasklet and let it |
| 977 | * figure out the details. |
| 978 | * |
| 979 | * TODO: We're not handling error flags correctly at |
| 980 | * the moment. |
| 981 | */ |
| 982 | if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { |
| 983 | UART_PUT_IDR(port, (ATMEL_US_ENDRX |
| 984 | | ATMEL_US_TIMEOUT)); |
| 985 | tasklet_schedule(&atmel_port->tasklet); |
| 986 | } |
| 987 | |
| 988 | if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | |
| 989 | ATMEL_US_FRAME | ATMEL_US_PARE)) |
| 990 | atmel_pdc_rxerr(port, pending); |
| 991 | } |
| 992 | |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 993 | if (atmel_use_dma_rx(port)) { |
| 994 | if (pending & ATMEL_US_TIMEOUT) { |
| 995 | UART_PUT_IDR(port, ATMEL_US_TIMEOUT); |
| 996 | tasklet_schedule(&atmel_port->tasklet); |
| 997 | } |
| 998 | } |
| 999 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1000 | /* Interrupt receive */ |
| 1001 | if (pending & ATMEL_US_RXRDY) |
| 1002 | atmel_rx_chars(port); |
| 1003 | else if (pending & ATMEL_US_RXBRK) { |
| 1004 | /* |
| 1005 | * End of break detected. If it came along with a |
| 1006 | * character, atmel_rx_chars will handle it. |
| 1007 | */ |
| 1008 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 1009 | UART_PUT_IDR(port, ATMEL_US_RXBRK); |
| 1010 | atmel_port->break_active = 0; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /* |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1015 | * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1016 | */ |
| 1017 | static void |
| 1018 | atmel_handle_transmit(struct uart_port *port, unsigned int pending) |
| 1019 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1020 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1021 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1022 | if (pending & atmel_port->tx_done_mask) { |
| 1023 | /* Either PDC or interrupt transmission */ |
| 1024 | UART_PUT_IDR(port, atmel_port->tx_done_mask); |
| 1025 | tasklet_schedule(&atmel_port->tasklet); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1026 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * status flags interrupt handler. |
| 1031 | */ |
| 1032 | static void |
| 1033 | atmel_handle_status(struct uart_port *port, unsigned int pending, |
| 1034 | unsigned int status) |
| 1035 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1036 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1037 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1038 | if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1039 | | ATMEL_US_CTSIC)) { |
| 1040 | atmel_port->irq_status = status; |
| 1041 | tasklet_schedule(&atmel_port->tasklet); |
| 1042 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1046 | * Interrupt handler |
| 1047 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1048 | static irqreturn_t atmel_interrupt(int irq, void *dev_id) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1049 | { |
| 1050 | struct uart_port *port = dev_id; |
| 1051 | unsigned int status, pending, pass_counter = 0; |
| 1052 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1053 | do { |
| 1054 | status = UART_GET_CSR(port); |
| 1055 | pending = status & UART_GET_IMR(port); |
| 1056 | if (!pending) |
| 1057 | break; |
| 1058 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1059 | atmel_handle_receive(port, pending); |
| 1060 | atmel_handle_status(port, pending, status); |
| 1061 | atmel_handle_transmit(port, pending); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1062 | } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1063 | |
Haavard Skinnemoen | 0400b69 | 2008-02-23 15:23:36 -0800 | [diff] [blame] | 1064 | return pass_counter ? IRQ_HANDLED : IRQ_NONE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1067 | static void atmel_release_tx_pdc(struct uart_port *port) |
| 1068 | { |
| 1069 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1070 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 1071 | |
| 1072 | dma_unmap_single(port->dev, |
| 1073 | pdc->dma_addr, |
| 1074 | pdc->dma_size, |
| 1075 | DMA_TO_DEVICE); |
| 1076 | } |
| 1077 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1078 | /* |
| 1079 | * Called from tasklet with ENDTX and TXBUFE interrupts disabled. |
| 1080 | */ |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 1081 | static void atmel_tx_pdc(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1082 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1083 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1084 | struct circ_buf *xmit = &port->state->xmit; |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1085 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 1086 | int count; |
| 1087 | |
Michael Trimarchi | ba0657f | 2008-04-02 13:04:41 -0700 | [diff] [blame] | 1088 | /* nothing left to transmit? */ |
| 1089 | if (UART_GET_TCR(port)) |
| 1090 | return; |
| 1091 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1092 | xmit->tail += pdc->ofs; |
| 1093 | xmit->tail &= UART_XMIT_SIZE - 1; |
| 1094 | |
| 1095 | port->icount.tx += pdc->ofs; |
| 1096 | pdc->ofs = 0; |
| 1097 | |
Michael Trimarchi | ba0657f | 2008-04-02 13:04:41 -0700 | [diff] [blame] | 1098 | /* more to transmit - setup next transfer */ |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1099 | |
Michael Trimarchi | ba0657f | 2008-04-02 13:04:41 -0700 | [diff] [blame] | 1100 | /* disable PDC transmit */ |
| 1101 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
| 1102 | |
Itai Levi | 1f14081 | 2009-01-15 13:50:43 -0800 | [diff] [blame] | 1103 | if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1104 | dma_sync_single_for_device(port->dev, |
| 1105 | pdc->dma_addr, |
| 1106 | pdc->dma_size, |
| 1107 | DMA_TO_DEVICE); |
| 1108 | |
| 1109 | count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 1110 | pdc->ofs = count; |
| 1111 | |
| 1112 | UART_PUT_TPR(port, pdc->dma_addr + xmit->tail); |
| 1113 | UART_PUT_TCR(port, count); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1114 | /* re-enable PDC transmit */ |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1115 | UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1116 | /* Enable interrupts */ |
| 1117 | UART_PUT_IER(port, atmel_port->tx_done_mask); |
| 1118 | } else { |
Bernhard Roth | 83cac9f | 2011-08-24 09:48:23 +0200 | [diff] [blame] | 1119 | if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && |
| 1120 | !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) { |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1121 | /* DMA done, stop TX, start RX for RS485 */ |
| 1122 | atmel_start_rx(port); |
| 1123 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 1127 | uart_write_wakeup(port); |
| 1128 | } |
| 1129 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1130 | static int atmel_prepare_tx_pdc(struct uart_port *port) |
| 1131 | { |
| 1132 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1133 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; |
| 1134 | struct circ_buf *xmit = &port->state->xmit; |
| 1135 | |
| 1136 | pdc->buf = xmit->buf; |
| 1137 | pdc->dma_addr = dma_map_single(port->dev, |
| 1138 | pdc->buf, |
| 1139 | UART_XMIT_SIZE, |
| 1140 | DMA_TO_DEVICE); |
| 1141 | pdc->dma_size = UART_XMIT_SIZE; |
| 1142 | pdc->ofs = 0; |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1147 | static void atmel_rx_from_ring(struct uart_port *port) |
| 1148 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1149 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1150 | struct circ_buf *ring = &atmel_port->rx_ring; |
| 1151 | unsigned int flg; |
| 1152 | unsigned int status; |
| 1153 | |
| 1154 | while (ring->head != ring->tail) { |
| 1155 | struct atmel_uart_char c; |
| 1156 | |
| 1157 | /* Make sure c is loaded after head. */ |
| 1158 | smp_rmb(); |
| 1159 | |
| 1160 | c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; |
| 1161 | |
| 1162 | ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); |
| 1163 | |
| 1164 | port->icount.rx++; |
| 1165 | status = c.status; |
| 1166 | flg = TTY_NORMAL; |
| 1167 | |
| 1168 | /* |
| 1169 | * note that the error handling code is |
| 1170 | * out of the main execution path |
| 1171 | */ |
| 1172 | if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME |
| 1173 | | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { |
| 1174 | if (status & ATMEL_US_RXBRK) { |
| 1175 | /* ignore side-effect */ |
| 1176 | status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); |
| 1177 | |
| 1178 | port->icount.brk++; |
| 1179 | if (uart_handle_break(port)) |
| 1180 | continue; |
| 1181 | } |
| 1182 | if (status & ATMEL_US_PARE) |
| 1183 | port->icount.parity++; |
| 1184 | if (status & ATMEL_US_FRAME) |
| 1185 | port->icount.frame++; |
| 1186 | if (status & ATMEL_US_OVRE) |
| 1187 | port->icount.overrun++; |
| 1188 | |
| 1189 | status &= port->read_status_mask; |
| 1190 | |
| 1191 | if (status & ATMEL_US_RXBRK) |
| 1192 | flg = TTY_BREAK; |
| 1193 | else if (status & ATMEL_US_PARE) |
| 1194 | flg = TTY_PARITY; |
| 1195 | else if (status & ATMEL_US_FRAME) |
| 1196 | flg = TTY_FRAME; |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | if (uart_handle_sysrq_char(port, c.ch)) |
| 1201 | continue; |
| 1202 | |
| 1203 | uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Drop the lock here since it might end up calling |
| 1208 | * uart_start(), which takes the lock. |
| 1209 | */ |
| 1210 | spin_unlock(&port->lock); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 1211 | tty_flip_buffer_push(&port->state->port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1212 | spin_lock(&port->lock); |
| 1213 | } |
| 1214 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1215 | static void atmel_release_rx_pdc(struct uart_port *port) |
| 1216 | { |
| 1217 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1218 | int i; |
| 1219 | |
| 1220 | for (i = 0; i < 2; i++) { |
| 1221 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; |
| 1222 | |
| 1223 | dma_unmap_single(port->dev, |
| 1224 | pdc->dma_addr, |
| 1225 | pdc->dma_size, |
| 1226 | DMA_FROM_DEVICE); |
| 1227 | kfree(pdc->buf); |
| 1228 | } |
| 1229 | } |
| 1230 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 1231 | static void atmel_rx_from_pdc(struct uart_port *port) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1232 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1233 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 1234 | struct tty_port *tport = &port->state->port; |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1235 | struct atmel_dma_buffer *pdc; |
| 1236 | int rx_idx = atmel_port->pdc_rx_idx; |
| 1237 | unsigned int head; |
| 1238 | unsigned int tail; |
| 1239 | unsigned int count; |
| 1240 | |
| 1241 | do { |
| 1242 | /* Reset the UART timeout early so that we don't miss one */ |
| 1243 | UART_PUT_CR(port, ATMEL_US_STTTO); |
| 1244 | |
| 1245 | pdc = &atmel_port->pdc_rx[rx_idx]; |
| 1246 | head = UART_GET_RPR(port) - pdc->dma_addr; |
| 1247 | tail = pdc->ofs; |
| 1248 | |
| 1249 | /* If the PDC has switched buffers, RPR won't contain |
| 1250 | * any address within the current buffer. Since head |
| 1251 | * is unsigned, we just need a one-way comparison to |
| 1252 | * find out. |
| 1253 | * |
| 1254 | * In this case, we just need to consume the entire |
| 1255 | * buffer and resubmit it for DMA. This will clear the |
| 1256 | * ENDRX bit as well, so that we can safely re-enable |
| 1257 | * all interrupts below. |
| 1258 | */ |
| 1259 | head = min(head, pdc->dma_size); |
| 1260 | |
| 1261 | if (likely(head != tail)) { |
| 1262 | dma_sync_single_for_cpu(port->dev, pdc->dma_addr, |
| 1263 | pdc->dma_size, DMA_FROM_DEVICE); |
| 1264 | |
| 1265 | /* |
| 1266 | * head will only wrap around when we recycle |
| 1267 | * the DMA buffer, and when that happens, we |
| 1268 | * explicitly set tail to 0. So head will |
| 1269 | * always be greater than tail. |
| 1270 | */ |
| 1271 | count = head - tail; |
| 1272 | |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 1273 | tty_insert_flip_string(tport, pdc->buf + pdc->ofs, |
| 1274 | count); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1275 | |
| 1276 | dma_sync_single_for_device(port->dev, pdc->dma_addr, |
| 1277 | pdc->dma_size, DMA_FROM_DEVICE); |
| 1278 | |
| 1279 | port->icount.rx += count; |
| 1280 | pdc->ofs = head; |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * If the current buffer is full, we need to check if |
| 1285 | * the next one contains any additional data. |
| 1286 | */ |
| 1287 | if (head >= pdc->dma_size) { |
| 1288 | pdc->ofs = 0; |
| 1289 | UART_PUT_RNPR(port, pdc->dma_addr); |
| 1290 | UART_PUT_RNCR(port, pdc->dma_size); |
| 1291 | |
| 1292 | rx_idx = !rx_idx; |
| 1293 | atmel_port->pdc_rx_idx = rx_idx; |
| 1294 | } |
| 1295 | } while (head >= pdc->dma_size); |
| 1296 | |
| 1297 | /* |
| 1298 | * Drop the lock here since it might end up calling |
| 1299 | * uart_start(), which takes the lock. |
| 1300 | */ |
| 1301 | spin_unlock(&port->lock); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 1302 | tty_flip_buffer_push(tport); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1303 | spin_lock(&port->lock); |
| 1304 | |
| 1305 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
| 1306 | } |
| 1307 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1308 | static int atmel_prepare_rx_pdc(struct uart_port *port) |
| 1309 | { |
| 1310 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1311 | int i; |
| 1312 | |
| 1313 | for (i = 0; i < 2; i++) { |
| 1314 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; |
| 1315 | |
| 1316 | pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); |
| 1317 | if (pdc->buf == NULL) { |
| 1318 | if (i != 0) { |
| 1319 | dma_unmap_single(port->dev, |
| 1320 | atmel_port->pdc_rx[0].dma_addr, |
| 1321 | PDC_BUFFER_SIZE, |
| 1322 | DMA_FROM_DEVICE); |
| 1323 | kfree(atmel_port->pdc_rx[0].buf); |
| 1324 | } |
| 1325 | atmel_port->use_pdc_rx = 0; |
| 1326 | return -ENOMEM; |
| 1327 | } |
| 1328 | pdc->dma_addr = dma_map_single(port->dev, |
| 1329 | pdc->buf, |
| 1330 | PDC_BUFFER_SIZE, |
| 1331 | DMA_FROM_DEVICE); |
| 1332 | pdc->dma_size = PDC_BUFFER_SIZE; |
| 1333 | pdc->ofs = 0; |
| 1334 | } |
| 1335 | |
| 1336 | atmel_port->pdc_rx_idx = 0; |
| 1337 | |
| 1338 | UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); |
| 1339 | UART_PUT_RCR(port, PDC_BUFFER_SIZE); |
| 1340 | |
| 1341 | UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); |
| 1342 | UART_PUT_RNCR(port, PDC_BUFFER_SIZE); |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1347 | /* |
| 1348 | * tasklet handling tty stuff outside the interrupt handler. |
| 1349 | */ |
| 1350 | static void atmel_tasklet_func(unsigned long data) |
| 1351 | { |
| 1352 | struct uart_port *port = (struct uart_port *)data; |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1353 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1354 | unsigned int status; |
| 1355 | unsigned int status_change; |
| 1356 | |
| 1357 | /* The interrupt handler does not take the lock */ |
| 1358 | spin_lock(&port->lock); |
| 1359 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1360 | atmel_port->schedule_tx(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1361 | |
| 1362 | status = atmel_port->irq_status; |
| 1363 | status_change = status ^ atmel_port->irq_status_prev; |
| 1364 | |
| 1365 | if (status_change & (ATMEL_US_RI | ATMEL_US_DSR |
| 1366 | | ATMEL_US_DCD | ATMEL_US_CTS)) { |
| 1367 | /* TODO: All reads to CSR will clear these interrupts! */ |
| 1368 | if (status_change & ATMEL_US_RI) |
| 1369 | port->icount.rng++; |
| 1370 | if (status_change & ATMEL_US_DSR) |
| 1371 | port->icount.dsr++; |
| 1372 | if (status_change & ATMEL_US_DCD) |
| 1373 | uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); |
| 1374 | if (status_change & ATMEL_US_CTS) |
| 1375 | uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); |
| 1376 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1377 | wake_up_interruptible(&port->state->port.delta_msr_wait); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1378 | |
| 1379 | atmel_port->irq_status_prev = status; |
| 1380 | } |
| 1381 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1382 | atmel_port->schedule_rx(port); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 1383 | |
| 1384 | spin_unlock(&port->lock); |
| 1385 | } |
| 1386 | |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 1387 | static int atmel_init_property(struct atmel_uart_port *atmel_port, |
| 1388 | struct platform_device *pdev) |
| 1389 | { |
| 1390 | struct device_node *np = pdev->dev.of_node; |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1391 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 1392 | |
| 1393 | if (np) { |
| 1394 | /* DMA/PDC usage specification */ |
| 1395 | if (of_get_property(np, "atmel,use-dma-rx", NULL)) { |
| 1396 | if (of_get_property(np, "dmas", NULL)) { |
| 1397 | atmel_port->use_dma_rx = true; |
| 1398 | atmel_port->use_pdc_rx = false; |
| 1399 | } else { |
| 1400 | atmel_port->use_dma_rx = false; |
| 1401 | atmel_port->use_pdc_rx = true; |
| 1402 | } |
| 1403 | } else { |
| 1404 | atmel_port->use_dma_rx = false; |
| 1405 | atmel_port->use_pdc_rx = false; |
| 1406 | } |
| 1407 | |
| 1408 | if (of_get_property(np, "atmel,use-dma-tx", NULL)) { |
| 1409 | if (of_get_property(np, "dmas", NULL)) { |
| 1410 | atmel_port->use_dma_tx = true; |
| 1411 | atmel_port->use_pdc_tx = false; |
| 1412 | } else { |
| 1413 | atmel_port->use_dma_tx = false; |
| 1414 | atmel_port->use_pdc_tx = true; |
| 1415 | } |
| 1416 | } else { |
| 1417 | atmel_port->use_dma_tx = false; |
| 1418 | atmel_port->use_pdc_tx = false; |
| 1419 | } |
| 1420 | |
| 1421 | } else { |
| 1422 | atmel_port->use_pdc_rx = pdata->use_dma_rx; |
| 1423 | atmel_port->use_pdc_tx = pdata->use_dma_tx; |
| 1424 | atmel_port->use_dma_rx = false; |
| 1425 | atmel_port->use_dma_tx = false; |
| 1426 | } |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | static void atmel_init_rs485(struct atmel_uart_port *atmel_port, |
| 1432 | struct platform_device *pdev) |
| 1433 | { |
| 1434 | struct device_node *np = pdev->dev.of_node; |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1435 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 1436 | |
| 1437 | if (np) { |
| 1438 | u32 rs485_delay[2]; |
| 1439 | /* rs485 properties */ |
| 1440 | if (of_property_read_u32_array(np, "rs485-rts-delay", |
| 1441 | rs485_delay, 2) == 0) { |
| 1442 | struct serial_rs485 *rs485conf = &atmel_port->rs485; |
| 1443 | |
| 1444 | rs485conf->delay_rts_before_send = rs485_delay[0]; |
| 1445 | rs485conf->delay_rts_after_send = rs485_delay[1]; |
| 1446 | rs485conf->flags = 0; |
| 1447 | |
| 1448 | if (of_get_property(np, "rs485-rx-during-tx", NULL)) |
| 1449 | rs485conf->flags |= SER_RS485_RX_DURING_TX; |
| 1450 | |
| 1451 | if (of_get_property(np, "linux,rs485-enabled-at-boot-time", |
| 1452 | NULL)) |
| 1453 | rs485conf->flags |= SER_RS485_ENABLED; |
| 1454 | } |
| 1455 | } else { |
| 1456 | atmel_port->rs485 = pdata->rs485; |
| 1457 | } |
| 1458 | |
| 1459 | } |
| 1460 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1461 | static void atmel_set_ops(struct uart_port *port) |
| 1462 | { |
| 1463 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1464 | |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 1465 | if (atmel_use_dma_rx(port)) { |
| 1466 | atmel_port->prepare_rx = &atmel_prepare_rx_dma; |
| 1467 | atmel_port->schedule_rx = &atmel_rx_from_dma; |
| 1468 | atmel_port->release_rx = &atmel_release_rx_dma; |
| 1469 | } else if (atmel_use_pdc_rx(port)) { |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1470 | atmel_port->prepare_rx = &atmel_prepare_rx_pdc; |
| 1471 | atmel_port->schedule_rx = &atmel_rx_from_pdc; |
| 1472 | atmel_port->release_rx = &atmel_release_rx_pdc; |
| 1473 | } else { |
| 1474 | atmel_port->prepare_rx = NULL; |
| 1475 | atmel_port->schedule_rx = &atmel_rx_from_ring; |
| 1476 | atmel_port->release_rx = NULL; |
| 1477 | } |
| 1478 | |
Elen Song | 08f738b | 2013-07-22 16:30:26 +0800 | [diff] [blame] | 1479 | if (atmel_use_dma_tx(port)) { |
| 1480 | atmel_port->prepare_tx = &atmel_prepare_tx_dma; |
| 1481 | atmel_port->schedule_tx = &atmel_tx_dma; |
| 1482 | atmel_port->release_tx = &atmel_release_tx_dma; |
| 1483 | } else if (atmel_use_pdc_tx(port)) { |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1484 | atmel_port->prepare_tx = &atmel_prepare_tx_pdc; |
| 1485 | atmel_port->schedule_tx = &atmel_tx_pdc; |
| 1486 | atmel_port->release_tx = &atmel_release_tx_pdc; |
| 1487 | } else { |
| 1488 | atmel_port->prepare_tx = NULL; |
| 1489 | atmel_port->schedule_tx = &atmel_tx_chars; |
| 1490 | atmel_port->release_tx = NULL; |
| 1491 | } |
| 1492 | } |
| 1493 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1494 | /* |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 1495 | * Get ip name usart or uart |
| 1496 | */ |
Nicolas Ferre | 892db58 | 2013-10-17 17:37:11 +0200 | [diff] [blame] | 1497 | static void atmel_get_ip_name(struct uart_port *port) |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 1498 | { |
| 1499 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1500 | int name = UART_GET_IP_NAME(port); |
Nicolas Ferre | 731d9ca | 2013-10-17 17:37:12 +0200 | [diff] [blame] | 1501 | u32 version; |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 1502 | int usart, uart; |
| 1503 | /* usart and uart ascii */ |
| 1504 | usart = 0x55534152; |
| 1505 | uart = 0x44424755; |
| 1506 | |
| 1507 | atmel_port->is_usart = false; |
| 1508 | |
| 1509 | if (name == usart) { |
| 1510 | dev_dbg(port->dev, "This is usart\n"); |
| 1511 | atmel_port->is_usart = true; |
| 1512 | } else if (name == uart) { |
| 1513 | dev_dbg(port->dev, "This is uart\n"); |
| 1514 | atmel_port->is_usart = false; |
| 1515 | } else { |
Nicolas Ferre | 731d9ca | 2013-10-17 17:37:12 +0200 | [diff] [blame] | 1516 | /* fallback for older SoCs: use version field */ |
| 1517 | version = UART_GET_IP_VERSION(port); |
| 1518 | switch (version) { |
| 1519 | case 0x302: |
| 1520 | case 0x10213: |
| 1521 | dev_dbg(port->dev, "This version is usart\n"); |
| 1522 | atmel_port->is_usart = true; |
| 1523 | break; |
| 1524 | case 0x203: |
| 1525 | case 0x10202: |
| 1526 | dev_dbg(port->dev, "This version is uart\n"); |
| 1527 | atmel_port->is_usart = false; |
| 1528 | break; |
| 1529 | default: |
| 1530 | dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); |
| 1531 | } |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 1532 | } |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1536 | * Perform initialization and enable port for reception |
| 1537 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1538 | static int atmel_startup(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1539 | { |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 1540 | struct platform_device *pdev = to_platform_device(port->dev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1541 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1542 | struct tty_struct *tty = port->state->port.tty; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1543 | int retval; |
| 1544 | |
| 1545 | /* |
| 1546 | * Ensure that no interrupts are enabled otherwise when |
| 1547 | * request_irq() is called we could get stuck trying to |
| 1548 | * handle an unexpected interrupt |
| 1549 | */ |
| 1550 | UART_PUT_IDR(port, -1); |
| 1551 | |
| 1552 | /* |
| 1553 | * Allocate the IRQ |
| 1554 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1555 | retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, |
Haavard Skinnemoen | ae16106 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1556 | tty ? tty->name : "atmel_serial", port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1557 | if (retval) { |
Richard Genoud | ddaa603 | 2014-02-26 17:19:45 +0100 | [diff] [blame^] | 1558 | dev_err(port->dev, "atmel_startup - Can't get irq\n"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1559 | return retval; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1563 | * Initialize DMA (if necessary) |
| 1564 | */ |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 1565 | atmel_init_property(atmel_port, pdev); |
| 1566 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1567 | if (atmel_port->prepare_rx) { |
| 1568 | retval = atmel_port->prepare_rx(port); |
| 1569 | if (retval < 0) |
| 1570 | atmel_set_ops(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1573 | if (atmel_port->prepare_tx) { |
| 1574 | retval = atmel_port->prepare_tx(port); |
| 1575 | if (retval < 0) |
| 1576 | atmel_set_ops(port); |
| 1577 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1578 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1579 | * If there is a specific "open" function (to register |
| 1580 | * control line interrupts) |
| 1581 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 1582 | if (atmel_open_hook) { |
| 1583 | retval = atmel_open_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1584 | if (retval) { |
| 1585 | free_irq(port->irq, port); |
| 1586 | return retval; |
| 1587 | } |
| 1588 | } |
| 1589 | |
Atsushi Nemoto | 27c0c8e | 2009-02-18 14:48:28 -0800 | [diff] [blame] | 1590 | /* Save current CSR for comparison in atmel_tasklet_func() */ |
| 1591 | atmel_port->irq_status_prev = UART_GET_CSR(port); |
| 1592 | atmel_port->irq_status = atmel_port->irq_status_prev; |
| 1593 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1594 | /* |
| 1595 | * Finally, enable the serial port |
| 1596 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1597 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1598 | /* enable xmit & rcvr */ |
| 1599 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1600 | |
Marek Roszko | 8bc661b | 2014-01-10 10:33:11 +0100 | [diff] [blame] | 1601 | setup_timer(&atmel_port->uart_timer, |
| 1602 | atmel_uart_timer_callback, |
| 1603 | (unsigned long)port); |
| 1604 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 1605 | if (atmel_use_pdc_rx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1606 | /* set UART timeout */ |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1607 | if (!atmel_port->is_usart) { |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1608 | mod_timer(&atmel_port->uart_timer, |
| 1609 | jiffies + uart_poll_timeout(port)); |
| 1610 | /* set USART timeout */ |
| 1611 | } else { |
| 1612 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); |
| 1613 | UART_PUT_CR(port, ATMEL_US_STTTO); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1614 | |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1615 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
| 1616 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1617 | /* enable PDC controller */ |
| 1618 | UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 1619 | } else if (atmel_use_dma_rx(port)) { |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1620 | /* set UART timeout */ |
| 1621 | if (!atmel_port->is_usart) { |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1622 | mod_timer(&atmel_port->uart_timer, |
| 1623 | jiffies + uart_poll_timeout(port)); |
| 1624 | /* set USART timeout */ |
| 1625 | } else { |
| 1626 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); |
| 1627 | UART_PUT_CR(port, ATMEL_US_STTTO); |
Elen Song | 34df42f | 2013-07-22 16:30:27 +0800 | [diff] [blame] | 1628 | |
Elen Song | 2e68c22 | 2013-07-22 16:30:30 +0800 | [diff] [blame] | 1629 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); |
| 1630 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1631 | } else { |
| 1632 | /* enable receive only */ |
| 1633 | UART_PUT_IER(port, ATMEL_US_RXRDY); |
| 1634 | } |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1635 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * Disable the port |
| 1641 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1642 | static void atmel_shutdown(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1643 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1644 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Marek Roszko | 0cc7c6c | 2014-01-07 11:45:06 +0100 | [diff] [blame] | 1645 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1646 | /* |
Marek Roszko | 8bc661b | 2014-01-10 10:33:11 +0100 | [diff] [blame] | 1647 | * Prevent any tasklets being scheduled during |
| 1648 | * cleanup |
| 1649 | */ |
| 1650 | del_timer_sync(&atmel_port->uart_timer); |
| 1651 | |
| 1652 | /* |
Marek Roszko | 0cc7c6c | 2014-01-07 11:45:06 +0100 | [diff] [blame] | 1653 | * Clear out any scheduled tasklets before |
| 1654 | * we destroy the buffers |
| 1655 | */ |
| 1656 | tasklet_kill(&atmel_port->tasklet); |
| 1657 | |
| 1658 | /* |
| 1659 | * Ensure everything is stopped and |
| 1660 | * disable all interrupts, port and break condition. |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1661 | */ |
| 1662 | atmel_stop_rx(port); |
| 1663 | atmel_stop_tx(port); |
| 1664 | |
Marek Roszko | 0cc7c6c | 2014-01-07 11:45:06 +0100 | [diff] [blame] | 1665 | UART_PUT_CR(port, ATMEL_US_RSTSTA); |
| 1666 | UART_PUT_IDR(port, -1); |
| 1667 | |
| 1668 | |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1669 | /* |
| 1670 | * Shut-down the DMA. |
| 1671 | */ |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 1672 | if (atmel_port->release_rx) |
| 1673 | atmel_port->release_rx(port); |
| 1674 | if (atmel_port->release_tx) |
| 1675 | atmel_port->release_tx(port); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1676 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1677 | /* |
Mark Deneen | bb7e73c | 2014-01-07 11:45:09 +0100 | [diff] [blame] | 1678 | * Reset ring buffer pointers |
| 1679 | */ |
| 1680 | atmel_port->rx_ring.head = 0; |
| 1681 | atmel_port->rx_ring.tail = 0; |
| 1682 | |
| 1683 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1684 | * Free the interrupt |
| 1685 | */ |
| 1686 | free_irq(port->irq, port); |
| 1687 | |
| 1688 | /* |
| 1689 | * If there is a specific "close" function (to unregister |
| 1690 | * control line interrupts) |
| 1691 | */ |
Haavard Skinnemoen | 71f2e2b | 2006-10-04 16:02:07 +0200 | [diff] [blame] | 1692 | if (atmel_close_hook) |
| 1693 | atmel_close_hook(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /* |
Haavard Skinnemoen | 9afd561 | 2008-07-16 21:52:46 +0100 | [diff] [blame] | 1697 | * Flush any TX data submitted for DMA. Called when the TX circular |
| 1698 | * buffer is reset. |
| 1699 | */ |
| 1700 | static void atmel_flush_buffer(struct uart_port *port) |
| 1701 | { |
| 1702 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
| 1703 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 1704 | if (atmel_use_pdc_tx(port)) { |
Haavard Skinnemoen | 9afd561 | 2008-07-16 21:52:46 +0100 | [diff] [blame] | 1705 | UART_PUT_TCR(port, 0); |
| 1706 | atmel_port->pdc_tx.ofs = 0; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /* |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1711 | * Power / Clock management. |
| 1712 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1713 | static void atmel_serial_pm(struct uart_port *port, unsigned int state, |
| 1714 | unsigned int oldstate) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1715 | { |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 1716 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1717 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1718 | switch (state) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1719 | case 0: |
| 1720 | /* |
| 1721 | * Enable the peripheral clock for this serial port. |
| 1722 | * This is called on uart_open() or a resume event. |
| 1723 | */ |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 1724 | clk_prepare_enable(atmel_port->clk); |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 1725 | |
| 1726 | /* re-enable interrupts if we disabled some on suspend */ |
| 1727 | UART_PUT_IER(port, atmel_port->backup_imr); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1728 | break; |
| 1729 | case 3: |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 1730 | /* Back up the interrupt mask and disable all interrupts */ |
| 1731 | atmel_port->backup_imr = UART_GET_IMR(port); |
| 1732 | UART_PUT_IDR(port, -1); |
| 1733 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1734 | /* |
| 1735 | * Disable the peripheral clock for this serial port. |
| 1736 | * This is called on uart_close() or a suspend event. |
| 1737 | */ |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 1738 | clk_disable_unprepare(atmel_port->clk); |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1739 | break; |
| 1740 | default: |
Richard Genoud | ddaa603 | 2014-02-26 17:19:45 +0100 | [diff] [blame^] | 1741 | dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * Change the port parameters |
| 1747 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1748 | static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, |
| 1749 | struct ktermios *old) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1750 | { |
| 1751 | unsigned long flags; |
| 1752 | unsigned int mode, imr, quot, baud; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1753 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1754 | |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 1755 | /* Get current mode register */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1756 | mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL |
Peter Ma | 8e706c4 | 2009-03-31 10:31:02 -0700 | [diff] [blame] | 1757 | | ATMEL_US_NBSTOP | ATMEL_US_PAR |
| 1758 | | ATMEL_US_USMODE); |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 1759 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1760 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1761 | quot = uart_get_divisor(port, baud); |
| 1762 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1763 | if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ |
Andrew Victor | 03abeac | 2007-05-03 12:26:24 +0100 | [diff] [blame] | 1764 | quot /= 8; |
| 1765 | mode |= ATMEL_US_USCLKS_MCK_DIV8; |
| 1766 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1767 | |
| 1768 | /* byte size */ |
| 1769 | switch (termios->c_cflag & CSIZE) { |
| 1770 | case CS5: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1771 | mode |= ATMEL_US_CHRL_5; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1772 | break; |
| 1773 | case CS6: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1774 | mode |= ATMEL_US_CHRL_6; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1775 | break; |
| 1776 | case CS7: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1777 | mode |= ATMEL_US_CHRL_7; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | default: |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1780 | mode |= ATMEL_US_CHRL_8; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1781 | break; |
| 1782 | } |
| 1783 | |
| 1784 | /* stop bits */ |
| 1785 | if (termios->c_cflag & CSTOPB) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1786 | mode |= ATMEL_US_NBSTOP_2; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* parity */ |
| 1789 | if (termios->c_cflag & PARENB) { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1790 | /* Mark or Space parity */ |
| 1791 | if (termios->c_cflag & CMSPAR) { |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1792 | if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1793 | mode |= ATMEL_US_PAR_MARK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1794 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1795 | mode |= ATMEL_US_PAR_SPACE; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1796 | } else if (termios->c_cflag & PARODD) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1797 | mode |= ATMEL_US_PAR_ODD; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1798 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1799 | mode |= ATMEL_US_PAR_EVEN; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1800 | } else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1801 | mode |= ATMEL_US_PAR_NONE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1802 | |
Peter Ma | 8e706c4 | 2009-03-31 10:31:02 -0700 | [diff] [blame] | 1803 | /* hardware handshake (RTS/CTS) */ |
| 1804 | if (termios->c_cflag & CRTSCTS) |
| 1805 | mode |= ATMEL_US_USMODE_HWHS; |
| 1806 | else |
| 1807 | mode |= ATMEL_US_USMODE_NORMAL; |
| 1808 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1809 | spin_lock_irqsave(&port->lock, flags); |
| 1810 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1811 | port->read_status_mask = ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1812 | if (termios->c_iflag & INPCK) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1813 | port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1814 | if (termios->c_iflag & (BRKINT | PARMRK)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1815 | port->read_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1816 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 1817 | if (atmel_use_pdc_rx(port)) |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 1818 | /* need to enable error interrupts */ |
| 1819 | UART_PUT_IER(port, port->read_status_mask); |
| 1820 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1821 | /* |
| 1822 | * Characters to ignore |
| 1823 | */ |
| 1824 | port->ignore_status_mask = 0; |
| 1825 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1826 | port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1827 | if (termios->c_iflag & IGNBRK) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1828 | port->ignore_status_mask |= ATMEL_US_RXBRK; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1829 | /* |
| 1830 | * If we're ignoring parity and break indicators, |
| 1831 | * ignore overruns too (for real raw support). |
| 1832 | */ |
| 1833 | if (termios->c_iflag & IGNPAR) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1834 | port->ignore_status_mask |= ATMEL_US_OVRE; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1835 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1836 | /* TODO: Ignore all characters if CREAD is set.*/ |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1837 | |
| 1838 | /* update the per-port timeout */ |
| 1839 | uart_update_timeout(port, termios->c_cflag, baud); |
| 1840 | |
Haavard Skinnemoen | 0ccad87 | 2009-06-16 17:02:03 +0100 | [diff] [blame] | 1841 | /* |
| 1842 | * save/disable interrupts. The tty layer will ensure that the |
| 1843 | * transmitter is empty if requested by the caller, so there's |
| 1844 | * no need to wait for it here. |
| 1845 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 1846 | imr = UART_GET_IMR(port); |
| 1847 | UART_PUT_IDR(port, -1); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1848 | |
| 1849 | /* disable receiver and transmitter */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1850 | UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1851 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1852 | /* Resetting serial mode to RS232 (0x0) */ |
| 1853 | mode &= ~ATMEL_US_USMODE; |
| 1854 | |
| 1855 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) { |
Claudio Scordino | 93f3350 | 2011-11-09 15:51:49 +0100 | [diff] [blame] | 1856 | if ((atmel_port->rs485.delay_rts_after_send) > 0) |
Claudio Scordino | 1b63318 | 2010-07-20 15:26:47 -0700 | [diff] [blame] | 1857 | UART_PUT_TTGR(port, |
| 1858 | atmel_port->rs485.delay_rts_after_send); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1859 | mode |= ATMEL_US_USMODE_RS485; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1862 | /* set the parity, stop bits and data size */ |
| 1863 | UART_PUT_MR(port, mode); |
| 1864 | |
| 1865 | /* set the baud rate */ |
| 1866 | UART_PUT_BRGR(port, quot); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1867 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 1868 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1869 | |
| 1870 | /* restore interrupts */ |
| 1871 | UART_PUT_IER(port, imr); |
| 1872 | |
| 1873 | /* CTS flow-control and modem-status interrupts */ |
| 1874 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 1875 | port->ops->enable_ms(port); |
| 1876 | |
| 1877 | spin_unlock_irqrestore(&port->lock, flags); |
| 1878 | } |
| 1879 | |
Viktar Palstsiuk | 42bd7a4 | 2011-02-09 15:26:13 +0100 | [diff] [blame] | 1880 | static void atmel_set_ldisc(struct uart_port *port, int new) |
| 1881 | { |
Jiri Slaby | b54bf3b | 2011-11-09 21:33:44 +0100 | [diff] [blame] | 1882 | if (new == N_PPS) { |
Viktar Palstsiuk | 42bd7a4 | 2011-02-09 15:26:13 +0100 | [diff] [blame] | 1883 | port->flags |= UPF_HARDPPS_CD; |
| 1884 | atmel_enable_ms(port); |
| 1885 | } else { |
| 1886 | port->flags &= ~UPF_HARDPPS_CD; |
| 1887 | } |
| 1888 | } |
| 1889 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1890 | /* |
| 1891 | * Return string describing the specified port |
| 1892 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1893 | static const char *atmel_type(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1894 | { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1895 | return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * Release the memory region(s) being used by 'port'. |
| 1900 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1901 | static void atmel_release_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1902 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1903 | struct platform_device *pdev = to_platform_device(port->dev); |
| 1904 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
| 1905 | |
| 1906 | release_mem_region(port->mapbase, size); |
| 1907 | |
| 1908 | if (port->flags & UPF_IOREMAP) { |
| 1909 | iounmap(port->membase); |
| 1910 | port->membase = NULL; |
| 1911 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * Request the memory region(s) being used by 'port'. |
| 1916 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1917 | static int atmel_request_port(struct uart_port *port) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1918 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1919 | struct platform_device *pdev = to_platform_device(port->dev); |
| 1920 | int size = pdev->resource[0].end - pdev->resource[0].start + 1; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1921 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1922 | if (!request_mem_region(port->mapbase, size, "atmel_serial")) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 1923 | return -EBUSY; |
| 1924 | |
| 1925 | if (port->flags & UPF_IOREMAP) { |
| 1926 | port->membase = ioremap(port->mapbase, size); |
| 1927 | if (port->membase == NULL) { |
| 1928 | release_mem_region(port->mapbase, size); |
| 1929 | return -ENOMEM; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | return 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * Configure/autoconfigure the port. |
| 1938 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1939 | static void atmel_config_port(struct uart_port *port, int flags) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1940 | { |
| 1941 | if (flags & UART_CONFIG_TYPE) { |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1942 | port->type = PORT_ATMEL; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1943 | atmel_request_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * Verify the new serial_struct (for TIOCSSERIAL). |
| 1949 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 1950 | 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] | 1951 | { |
| 1952 | int ret = 0; |
Haavard Skinnemoen | 9ab4f88 | 2006-10-04 16:02:06 +0200 | [diff] [blame] | 1953 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 1954 | ret = -EINVAL; |
| 1955 | if (port->irq != ser->irq) |
| 1956 | ret = -EINVAL; |
| 1957 | if (ser->io_type != SERIAL_IO_MEM) |
| 1958 | ret = -EINVAL; |
| 1959 | if (port->uartclk / 16 != ser->baud_base) |
| 1960 | ret = -EINVAL; |
| 1961 | if ((void *)port->mapbase != ser->iomem_base) |
| 1962 | ret = -EINVAL; |
| 1963 | if (port->iobase != ser->port) |
| 1964 | ret = -EINVAL; |
| 1965 | if (ser->hub6 != 0) |
| 1966 | ret = -EINVAL; |
| 1967 | return ret; |
| 1968 | } |
| 1969 | |
Albin Tonnerre | 8fe2d54 | 2009-12-09 12:31:32 -0800 | [diff] [blame] | 1970 | #ifdef CONFIG_CONSOLE_POLL |
| 1971 | static int atmel_poll_get_char(struct uart_port *port) |
| 1972 | { |
| 1973 | while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY)) |
| 1974 | cpu_relax(); |
| 1975 | |
| 1976 | return UART_GET_CHAR(port); |
| 1977 | } |
| 1978 | |
| 1979 | static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) |
| 1980 | { |
| 1981 | while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) |
| 1982 | cpu_relax(); |
| 1983 | |
| 1984 | UART_PUT_CHAR(port, ch); |
| 1985 | } |
| 1986 | #endif |
| 1987 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 1988 | static int |
| 1989 | atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg) |
| 1990 | { |
| 1991 | struct serial_rs485 rs485conf; |
| 1992 | |
| 1993 | switch (cmd) { |
| 1994 | case TIOCSRS485: |
| 1995 | if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg, |
| 1996 | sizeof(rs485conf))) |
| 1997 | return -EFAULT; |
| 1998 | |
| 1999 | atmel_config_rs485(port, &rs485conf); |
| 2000 | break; |
| 2001 | |
| 2002 | case TIOCGRS485: |
| 2003 | if (copy_to_user((struct serial_rs485 *) arg, |
| 2004 | &(to_atmel_uart_port(port)->rs485), |
| 2005 | sizeof(rs485conf))) |
| 2006 | return -EFAULT; |
| 2007 | break; |
| 2008 | |
| 2009 | default: |
| 2010 | return -ENOIOCTLCMD; |
| 2011 | } |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2017 | static struct uart_ops atmel_pops = { |
| 2018 | .tx_empty = atmel_tx_empty, |
| 2019 | .set_mctrl = atmel_set_mctrl, |
| 2020 | .get_mctrl = atmel_get_mctrl, |
| 2021 | .stop_tx = atmel_stop_tx, |
| 2022 | .start_tx = atmel_start_tx, |
| 2023 | .stop_rx = atmel_stop_rx, |
| 2024 | .enable_ms = atmel_enable_ms, |
| 2025 | .break_ctl = atmel_break_ctl, |
| 2026 | .startup = atmel_startup, |
| 2027 | .shutdown = atmel_shutdown, |
Haavard Skinnemoen | 9afd561 | 2008-07-16 21:52:46 +0100 | [diff] [blame] | 2028 | .flush_buffer = atmel_flush_buffer, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2029 | .set_termios = atmel_set_termios, |
Viktar Palstsiuk | 42bd7a4 | 2011-02-09 15:26:13 +0100 | [diff] [blame] | 2030 | .set_ldisc = atmel_set_ldisc, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2031 | .type = atmel_type, |
| 2032 | .release_port = atmel_release_port, |
| 2033 | .request_port = atmel_request_port, |
| 2034 | .config_port = atmel_config_port, |
| 2035 | .verify_port = atmel_verify_port, |
| 2036 | .pm = atmel_serial_pm, |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2037 | .ioctl = atmel_ioctl, |
Albin Tonnerre | 8fe2d54 | 2009-12-09 12:31:32 -0800 | [diff] [blame] | 2038 | #ifdef CONFIG_CONSOLE_POLL |
| 2039 | .poll_get_char = atmel_poll_get_char, |
| 2040 | .poll_put_char = atmel_poll_put_char, |
| 2041 | #endif |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2042 | }; |
| 2043 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2044 | /* |
| 2045 | * Configure the port from the platform device resource info. |
| 2046 | */ |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2047 | static int atmel_init_port(struct atmel_uart_port *atmel_port, |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2048 | struct platform_device *pdev) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2049 | { |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2050 | int ret; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2051 | struct uart_port *port = &atmel_port->uart; |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2052 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2053 | |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 2054 | if (!atmel_init_property(atmel_port, pdev)) |
| 2055 | atmel_set_ops(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2056 | |
Elen Song | 33d64c4 | 2013-07-22 16:30:28 +0800 | [diff] [blame] | 2057 | atmel_init_rs485(atmel_port, pdev); |
Elen Song | a930e52 | 2013-07-22 16:30:25 +0800 | [diff] [blame] | 2058 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2059 | port->iotype = UPIO_MEM; |
| 2060 | port->flags = UPF_BOOT_AUTOCONF; |
| 2061 | port->ops = &atmel_pops; |
| 2062 | port->fifosize = 1; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2063 | port->dev = &pdev->dev; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2064 | port->mapbase = pdev->resource[0].start; |
| 2065 | port->irq = pdev->resource[1].start; |
| 2066 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2067 | tasklet_init(&atmel_port->tasklet, atmel_tasklet_func, |
| 2068 | (unsigned long)port); |
| 2069 | |
| 2070 | memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); |
| 2071 | |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 2072 | if (pdata && pdata->regs) { |
Haavard Skinnemoen | 75d3521 | 2006-10-04 16:02:08 +0200 | [diff] [blame] | 2073 | /* Already mapped by setup code */ |
Nicolas Ferre | 1acfc7e | 2011-10-12 18:06:57 +0200 | [diff] [blame] | 2074 | port->membase = pdata->regs; |
Nicolas Ferre | 588edbf | 2011-10-12 18:06:58 +0200 | [diff] [blame] | 2075 | } else { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2076 | port->flags |= UPF_IOREMAP; |
| 2077 | port->membase = NULL; |
| 2078 | } |
| 2079 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2080 | /* for console, the clock could already be configured */ |
| 2081 | if (!atmel_port->clk) { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2082 | atmel_port->clk = clk_get(&pdev->dev, "usart"); |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2083 | if (IS_ERR(atmel_port->clk)) { |
| 2084 | ret = PTR_ERR(atmel_port->clk); |
| 2085 | atmel_port->clk = NULL; |
| 2086 | return ret; |
| 2087 | } |
| 2088 | ret = clk_prepare_enable(atmel_port->clk); |
| 2089 | if (ret) { |
| 2090 | clk_put(atmel_port->clk); |
| 2091 | atmel_port->clk = NULL; |
| 2092 | return ret; |
| 2093 | } |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2094 | port->uartclk = clk_get_rate(atmel_port->clk); |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2095 | clk_disable_unprepare(atmel_port->clk); |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2096 | /* only enable clock when USART is in use */ |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2097 | } |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 2098 | |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2099 | /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ |
| 2100 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) |
| 2101 | atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 2102 | else if (atmel_use_pdc_tx(port)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 2103 | port->fifosize = PDC_BUFFER_SIZE; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2104 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; |
| 2105 | } else { |
| 2106 | atmel_port->tx_done_mask = ATMEL_US_TXRDY; |
| 2107 | } |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2108 | |
| 2109 | return 0; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Jean-Christophe PLAGNIOL-VILLARD | 69f6a27 | 2012-02-16 00:24:07 +0800 | [diff] [blame] | 2112 | struct platform_device *atmel_default_console_device; /* the serial console device */ |
| 2113 | |
Haavard Skinnemoen | 749c4e6 | 2006-10-04 16:02:02 +0200 | [diff] [blame] | 2114 | #ifdef CONFIG_SERIAL_ATMEL_CONSOLE |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2115 | static void atmel_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2116 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2117 | while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) |
Haavard Skinnemoen | 829dd81 | 2008-02-08 04:21:02 -0800 | [diff] [blame] | 2118 | cpu_relax(); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2119 | UART_PUT_CHAR(port, ch); |
| 2120 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* |
| 2123 | * Interrupts are disabled on entering |
| 2124 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2125 | 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] | 2126 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2127 | struct uart_port *port = &atmel_ports[co->index].uart; |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2128 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2129 | unsigned int status, imr; |
Marc Pignat | 39d4c92 | 2008-04-02 13:04:42 -0700 | [diff] [blame] | 2130 | unsigned int pdc_tx; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2131 | |
| 2132 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2133 | * First, save IMR and then disable interrupts |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2134 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2135 | imr = UART_GET_IMR(port); |
Claudio Scordino | e8faff7 | 2010-05-03 13:31:28 +0100 | [diff] [blame] | 2136 | UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2137 | |
Marc Pignat | 39d4c92 | 2008-04-02 13:04:42 -0700 | [diff] [blame] | 2138 | /* Store PDC transmit status and disable it */ |
| 2139 | pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN; |
| 2140 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
| 2141 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2142 | uart_console_write(port, s, count, atmel_console_putchar); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2143 | |
| 2144 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2145 | * Finally, wait for transmitter to become empty |
| 2146 | * and restore IMR |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2147 | */ |
| 2148 | do { |
| 2149 | status = UART_GET_CSR(port); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2150 | } while (!(status & ATMEL_US_TXRDY)); |
Marc Pignat | 39d4c92 | 2008-04-02 13:04:42 -0700 | [diff] [blame] | 2151 | |
| 2152 | /* Restore PDC transmit status */ |
| 2153 | if (pdc_tx) |
| 2154 | UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); |
| 2155 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2156 | /* set interrupts back the way they were */ |
| 2157 | UART_PUT_IER(port, imr); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /* |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2161 | * If the port was already initialised (eg, by a boot loader), |
| 2162 | * try to determine the current setup. |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2163 | */ |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2164 | static void __init atmel_console_get_options(struct uart_port *port, int *baud, |
| 2165 | int *parity, int *bits) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2166 | { |
| 2167 | unsigned int mr, quot; |
| 2168 | |
Haavard Skinnemoen | 1c0fd82 | 2008-02-08 04:21:03 -0800 | [diff] [blame] | 2169 | /* |
| 2170 | * If the baud rate generator isn't running, the port wasn't |
| 2171 | * initialized by the boot loader. |
| 2172 | */ |
Haavard Skinnemoen | 9c81c5c | 2008-06-05 22:46:39 -0700 | [diff] [blame] | 2173 | quot = UART_GET_BRGR(port) & ATMEL_US_CD; |
Haavard Skinnemoen | 1c0fd82 | 2008-02-08 04:21:03 -0800 | [diff] [blame] | 2174 | if (!quot) |
| 2175 | return; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2176 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2177 | mr = UART_GET_MR(port) & ATMEL_US_CHRL; |
| 2178 | if (mr == ATMEL_US_CHRL_8) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2179 | *bits = 8; |
| 2180 | else |
| 2181 | *bits = 7; |
| 2182 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2183 | mr = UART_GET_MR(port) & ATMEL_US_PAR; |
| 2184 | if (mr == ATMEL_US_PAR_EVEN) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2185 | *parity = 'e'; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2186 | else if (mr == ATMEL_US_PAR_ODD) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2187 | *parity = 'o'; |
| 2188 | |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 2189 | /* |
| 2190 | * The serial core only rounds down when matching this to a |
| 2191 | * supported baud rate. Make sure we don't end up slightly |
| 2192 | * lower than one of those, as it would make us fall through |
| 2193 | * to a much lower baud rate than we really want. |
| 2194 | */ |
Haavard Skinnemoen | 4d5e392 | 2006-10-04 16:02:11 +0200 | [diff] [blame] | 2195 | *baud = port->uartclk / (16 * (quot - 1)); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2198 | static int __init atmel_console_setup(struct console *co, char *options) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2199 | { |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2200 | int ret; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2201 | struct uart_port *port = &atmel_ports[co->index].uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2202 | int baud = 115200; |
| 2203 | int bits = 8; |
| 2204 | int parity = 'n'; |
| 2205 | int flow = 'n'; |
| 2206 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2207 | if (port->membase == NULL) { |
| 2208 | /* Port not initialized yet - delay setup */ |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2209 | return -ENODEV; |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2210 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2211 | |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2212 | ret = clk_prepare_enable(atmel_ports[co->index].clk); |
| 2213 | if (ret) |
| 2214 | return ret; |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2215 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2216 | UART_PUT_IDR(port, -1); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2217 | UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); |
| 2218 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (options) |
| 2221 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2222 | else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2223 | atmel_console_get_options(port, &baud, &parity, &bits); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2224 | |
| 2225 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 2226 | } |
| 2227 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2228 | static struct uart_driver atmel_uart; |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2229 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2230 | static struct console atmel_console = { |
| 2231 | .name = ATMEL_DEVICENAME, |
| 2232 | .write = atmel_console_write, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2233 | .device = uart_console_device, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2234 | .setup = atmel_console_setup, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2235 | .flags = CON_PRINTBUFFER, |
| 2236 | .index = -1, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2237 | .data = &atmel_uart, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2238 | }; |
| 2239 | |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2240 | #define ATMEL_CONSOLE_DEVICE (&atmel_console) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2241 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2242 | /* |
| 2243 | * Early console initialization (before VM subsystem initialized). |
| 2244 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2245 | static int __init atmel_console_init(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2246 | { |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2247 | int ret; |
Haavard Skinnemoen | 73e2798 | 2006-10-04 16:02:04 +0200 | [diff] [blame] | 2248 | if (atmel_default_console_device) { |
Voss, Nikolaus | 0d0a3cc | 2011-08-10 14:02:29 +0200 | [diff] [blame] | 2249 | struct atmel_uart_data *pdata = |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2250 | dev_get_platdata(&atmel_default_console_device->dev); |
Linus Torvalds | efb8d21 | 2011-10-26 15:11:09 +0200 | [diff] [blame] | 2251 | int id = pdata->num; |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2252 | struct atmel_uart_port *port = &atmel_ports[id]; |
Voss, Nikolaus | 0d0a3cc | 2011-08-10 14:02:29 +0200 | [diff] [blame] | 2253 | |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2254 | port->backup_imr = 0; |
| 2255 | port->uart.line = id; |
| 2256 | |
| 2257 | add_preferred_console(ATMEL_DEVICENAME, id, NULL); |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2258 | ret = atmel_init_port(port, atmel_default_console_device); |
| 2259 | if (ret) |
| 2260 | return ret; |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2261 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2262 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2263 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2266 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2267 | console_initcall(atmel_console_init); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2268 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2269 | /* |
| 2270 | * Late console initialization. |
| 2271 | */ |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2272 | static int __init atmel_late_console_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2273 | { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2274 | if (atmel_default_console_device |
| 2275 | && !(atmel_console.flags & CON_ENABLED)) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2276 | register_console(&atmel_console); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2277 | |
| 2278 | return 0; |
| 2279 | } |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2280 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2281 | core_initcall(atmel_late_console_init); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2282 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2283 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 2284 | { |
| 2285 | return port->cons && port->cons->index == port->line; |
| 2286 | } |
| 2287 | |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2288 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2289 | #define ATMEL_CONSOLE_DEVICE NULL |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2290 | |
| 2291 | static inline bool atmel_is_console_port(struct uart_port *port) |
| 2292 | { |
| 2293 | return false; |
| 2294 | } |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2295 | #endif |
| 2296 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2297 | static struct uart_driver atmel_uart = { |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2298 | .owner = THIS_MODULE, |
| 2299 | .driver_name = "atmel_serial", |
| 2300 | .dev_name = ATMEL_DEVICENAME, |
| 2301 | .major = SERIAL_ATMEL_MAJOR, |
| 2302 | .minor = MINOR_START, |
| 2303 | .nr = ATMEL_MAX_UART, |
| 2304 | .cons = ATMEL_CONSOLE_DEVICE, |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2305 | }; |
| 2306 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2307 | #ifdef CONFIG_PM |
Haavard Skinnemoen | f826caa | 2008-02-24 14:34:45 +0100 | [diff] [blame] | 2308 | static bool atmel_serial_clk_will_stop(void) |
| 2309 | { |
| 2310 | #ifdef CONFIG_ARCH_AT91 |
| 2311 | return at91_suspend_entering_slow_clock(); |
| 2312 | #else |
| 2313 | return false; |
| 2314 | #endif |
| 2315 | } |
| 2316 | |
Remy Bohmer | b843aa2 | 2008-02-08 04:21:01 -0800 | [diff] [blame] | 2317 | static int atmel_serial_suspend(struct platform_device *pdev, |
| 2318 | pm_message_t state) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2319 | { |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2320 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 2321 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2322 | |
Haavard Skinnemoen | e1c609e | 2008-03-14 14:54:13 +0100 | [diff] [blame] | 2323 | if (atmel_is_console_port(port) && console_suspend_enabled) { |
| 2324 | /* Drain the TX shifter */ |
| 2325 | while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) |
| 2326 | cpu_relax(); |
| 2327 | } |
| 2328 | |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 2329 | /* we can not wake up if we're running on slow clock */ |
| 2330 | atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); |
| 2331 | if (atmel_serial_clk_will_stop()) |
| 2332 | device_set_wakeup_enable(&pdev->dev, 0); |
| 2333 | |
| 2334 | uart_suspend_port(&atmel_uart, port); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2335 | |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2339 | static int atmel_serial_resume(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2340 | { |
| 2341 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 2342 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2343 | |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 2344 | uart_resume_port(&atmel_uart, port); |
| 2345 | device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2346 | |
| 2347 | return 0; |
| 2348 | } |
| 2349 | #else |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2350 | #define atmel_serial_suspend NULL |
| 2351 | #define atmel_serial_resume NULL |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2352 | #endif |
| 2353 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 2354 | static int atmel_serial_probe(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2355 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2356 | struct atmel_uart_port *port; |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 2357 | struct device_node *np = pdev->dev.of_node; |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2358 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2359 | void *data; |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2360 | int ret = -ENODEV; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2361 | |
Haavard Skinnemoen | 9d09daf | 2009-10-26 16:50:02 -0700 | [diff] [blame] | 2362 | BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2363 | |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 2364 | if (np) |
| 2365 | ret = of_alias_get_id(np, "serial"); |
| 2366 | else |
| 2367 | if (pdata) |
| 2368 | ret = pdata->num; |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2369 | |
| 2370 | if (ret < 0) |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 2371 | /* port id not found in platform data nor device-tree aliases: |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2372 | * auto-enumerate it */ |
Pawel Wieczorkiewicz | 503bded | 2013-02-20 17:26:20 +0100 | [diff] [blame] | 2373 | ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2374 | |
Pawel Wieczorkiewicz | 503bded | 2013-02-20 17:26:20 +0100 | [diff] [blame] | 2375 | if (ret >= ATMEL_MAX_UART) { |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2376 | ret = -ENODEV; |
| 2377 | goto err; |
| 2378 | } |
| 2379 | |
Pawel Wieczorkiewicz | 503bded | 2013-02-20 17:26:20 +0100 | [diff] [blame] | 2380 | if (test_and_set_bit(ret, atmel_ports_in_use)) { |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2381 | /* port already in use */ |
| 2382 | ret = -EBUSY; |
| 2383 | goto err; |
| 2384 | } |
| 2385 | |
| 2386 | port = &atmel_ports[ret]; |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 2387 | port->backup_imr = 0; |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2388 | port->uart.line = ret; |
Anti Sullin | f05596d | 2008-09-22 13:57:54 -0700 | [diff] [blame] | 2389 | |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2390 | ret = atmel_init_port(port, pdev); |
| 2391 | if (ret) |
| 2392 | goto err; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2393 | |
Elen Song | 64e22eb | 2013-07-22 16:30:24 +0800 | [diff] [blame] | 2394 | if (!atmel_use_pdc_rx(&port->uart)) { |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 2395 | ret = -ENOMEM; |
Haavard Skinnemoen | 6433471 | 2008-02-08 04:21:07 -0800 | [diff] [blame] | 2396 | data = kmalloc(sizeof(struct atmel_uart_char) |
| 2397 | * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); |
Chip Coldwell | a667061 | 2008-02-08 04:21:06 -0800 | [diff] [blame] | 2398 | if (!data) |
| 2399 | goto err_alloc_ring; |
| 2400 | port->rx_ring.buf = data; |
| 2401 | } |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2402 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2403 | ret = uart_add_one_port(&atmel_uart, &port->uart); |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2404 | if (ret) |
| 2405 | goto err_add_port; |
| 2406 | |
Albin Tonnerre | 8da14b5 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 2407 | #ifdef CONFIG_SERIAL_ATMEL_CONSOLE |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2408 | if (atmel_is_console_port(&port->uart) |
| 2409 | && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { |
| 2410 | /* |
| 2411 | * The serial core enabled the clock for us, so undo |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2412 | * the clk_prepare_enable() in atmel_console_setup() |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2413 | */ |
Boris BREZILLON | 91f8c2d | 2013-06-19 13:17:30 +0200 | [diff] [blame] | 2414 | clk_disable_unprepare(port->clk); |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2415 | } |
Albin Tonnerre | 8da14b5 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 2416 | #endif |
David Brownell | 06a7f05 | 2008-11-06 12:53:40 -0800 | [diff] [blame] | 2417 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2418 | device_init_wakeup(&pdev->dev, 1); |
| 2419 | platform_set_drvdata(pdev, port); |
| 2420 | |
Claudio Scordino | 5dfbd1d7 | 2011-01-13 15:45:39 -0800 | [diff] [blame] | 2421 | if (port->rs485.flags & SER_RS485_ENABLED) { |
| 2422 | UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL); |
| 2423 | UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); |
| 2424 | } |
| 2425 | |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 2426 | /* |
| 2427 | * Get port name of usart or uart |
| 2428 | */ |
Nicolas Ferre | 892db58 | 2013-10-17 17:37:11 +0200 | [diff] [blame] | 2429 | atmel_get_ip_name(&port->uart); |
Elen Song | 055560b | 2013-07-22 16:30:29 +0800 | [diff] [blame] | 2430 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2431 | return 0; |
| 2432 | |
| 2433 | err_add_port: |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2434 | kfree(port->rx_ring.buf); |
| 2435 | port->rx_ring.buf = NULL; |
| 2436 | err_alloc_ring: |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2437 | if (!atmel_is_console_port(&port->uart)) { |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2438 | clk_put(port->clk); |
| 2439 | port->clk = NULL; |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2440 | } |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2441 | err: |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2442 | return ret; |
| 2443 | } |
| 2444 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 2445 | static int atmel_serial_remove(struct platform_device *pdev) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2446 | { |
| 2447 | struct uart_port *port = platform_get_drvdata(pdev); |
Haavard Skinnemoen | c811ab8 | 2008-02-08 04:21:08 -0800 | [diff] [blame] | 2448 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2449 | int ret = 0; |
| 2450 | |
Marek Roszko | f50c995f | 2014-01-07 11:45:07 +0100 | [diff] [blame] | 2451 | tasklet_kill(&atmel_port->tasklet); |
| 2452 | |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2453 | device_init_wakeup(&pdev->dev, 0); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2454 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2455 | ret = uart_remove_one_port(&atmel_uart, port); |
| 2456 | |
Remy Bohmer | 1ecc26b | 2008-02-08 04:21:05 -0800 | [diff] [blame] | 2457 | kfree(atmel_port->rx_ring.buf); |
| 2458 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2459 | /* "port" is allocated statically, so we shouldn't free it */ |
| 2460 | |
Pawel Wieczorkiewicz | 503bded | 2013-02-20 17:26:20 +0100 | [diff] [blame] | 2461 | clear_bit(port->line, atmel_ports_in_use); |
Nicolas Ferre | 4cbf9f4 | 2011-10-12 18:06:59 +0200 | [diff] [blame] | 2462 | |
Haavard Skinnemoen | dfa7f34 | 2008-02-08 04:21:04 -0800 | [diff] [blame] | 2463 | clk_put(atmel_port->clk); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2464 | |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2468 | static struct platform_driver atmel_serial_driver = { |
| 2469 | .probe = atmel_serial_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 2470 | .remove = atmel_serial_remove, |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2471 | .suspend = atmel_serial_suspend, |
| 2472 | .resume = atmel_serial_resume, |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2473 | .driver = { |
Haavard Skinnemoen | 1e8ea80 | 2006-10-04 16:02:03 +0200 | [diff] [blame] | 2474 | .name = "atmel_usart", |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2475 | .owner = THIS_MODULE, |
Nicolas Ferre | 5fbe46b | 2011-10-12 18:07:00 +0200 | [diff] [blame] | 2476 | .of_match_table = of_match_ptr(atmel_serial_dt_ids), |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2477 | }, |
| 2478 | }; |
| 2479 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2480 | static int __init atmel_serial_init(void) |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2481 | { |
| 2482 | int ret; |
| 2483 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2484 | ret = uart_register_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2485 | if (ret) |
| 2486 | return ret; |
| 2487 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2488 | ret = platform_driver_register(&atmel_serial_driver); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2489 | if (ret) |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2490 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | afefc41 | 2006-06-19 19:53:19 +0100 | [diff] [blame] | 2491 | |
| 2492 | return ret; |
| 2493 | } |
| 2494 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2495 | static void __exit atmel_serial_exit(void) |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2496 | { |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2497 | platform_driver_unregister(&atmel_serial_driver); |
| 2498 | uart_unregister_driver(&atmel_uart); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2501 | module_init(atmel_serial_init); |
| 2502 | module_exit(atmel_serial_exit); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2503 | |
| 2504 | MODULE_AUTHOR("Rick Bronson"); |
Haavard Skinnemoen | 7192f92 | 2006-10-04 16:02:05 +0200 | [diff] [blame] | 2505 | MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); |
Andrew Victor | 1e6c9c2 | 2006-01-10 16:59:27 +0000 | [diff] [blame] | 2506 | MODULE_LICENSE("GPL"); |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 2507 | MODULE_ALIAS("platform:atmel_usart"); |