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