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