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