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