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