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