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