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