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