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