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