blob: be94246b6fcca1874161470035b9d9bd91237f88 [file] [log] [blame]
Sascha Hauer47d37d62011-01-11 15:54:54 +01001/*
Oleksij Rempel254da0d2016-03-16 14:05:52 +01002 * Application UART driver for:
3 * Freescale STMP37XX/STMP378X
4 * Alphascale ASM9260
Sascha Hauer47d37d62011-01-11 15:54:54 +01005 *
6 * Author: dmitry pervushin <dimka@embeddedalley.com>
7 *
Oleksij Rempel254da0d2016-03-16 14:05:52 +01008 * Copyright 2014 Oleksij Rempel <linux@rempel-privat.de>
9 * Provide Alphascale ASM9260 support.
Sascha Hauer47d37d62011-01-11 15:54:54 +010010 * Copyright 2008-2010 Freescale Semiconductor, Inc.
11 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
12 *
13 * The code contained herein is licensed under the GNU General Public
14 * License. You may obtain a copy of the GNU General Public License
15 * Version 2 or later at the following locations:
Sascha Hauer47d37d62011-01-11 15:54:54 +010016 */
17
Janusz Uzycki914d3b12014-10-10 13:13:28 +020018#if defined(CONFIG_SERIAL_MXS_AUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
Sascha Hauer47d37d62011-01-11 15:54:54 +010022#include <linux/kernel.h>
Sascha Hauer47d37d62011-01-11 15:54:54 +010023#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/console.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/slab.h>
29#include <linux/wait.h>
30#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/tty_flip.h>
33#include <linux/serial.h>
34#include <linux/serial_core.h>
35#include <linux/platform_device.h>
36#include <linux/device.h>
37#include <linux/clk.h>
38#include <linux/delay.h>
39#include <linux/io.h>
Fabio Estevam1ea66072012-06-18 10:06:09 -030040#include <linux/of_device.h>
Huang Shijiee8001632012-11-16 16:03:53 +080041#include <linux/dma-mapping.h>
Shawn Guobcc20f92013-02-26 13:47:41 +080042#include <linux/dmaengine.h>
Sascha Hauer47d37d62011-01-11 15:54:54 +010043
44#include <asm/cacheflush.h>
45
Janusz Uzyckif9e42392014-10-10 18:53:26 +020046#include <linux/gpio.h>
47#include <linux/gpio/consumer.h>
Janusz Uzycki7c573d72014-10-10 18:53:25 +020048#include <linux/err.h>
Janusz Uzyckif9e42392014-10-10 18:53:26 +020049#include <linux/irq.h>
Janusz Uzycki7c573d72014-10-10 18:53:25 +020050#include "serial_mctrl_gpio.h"
51
Sascha Hauer47d37d62011-01-11 15:54:54 +010052#define MXS_AUART_PORTS 5
Hector Palacios9987f762013-10-03 09:32:03 +020053#define MXS_AUART_FIFO_SIZE 16
Sascha Hauer47d37d62011-01-11 15:54:54 +010054
Oleksij Rempel254da0d2016-03-16 14:05:52 +010055#define SET_REG 0x4
56#define CLR_REG 0x8
57#define TOG_REG 0xc
58
Sascha Hauer47d37d62011-01-11 15:54:54 +010059#define AUART_CTRL0 0x00000000
Sascha Hauer47d37d62011-01-11 15:54:54 +010060#define AUART_CTRL1 0x00000010
Sascha Hauer47d37d62011-01-11 15:54:54 +010061#define AUART_CTRL2 0x00000020
Sascha Hauer47d37d62011-01-11 15:54:54 +010062#define AUART_LINECTRL 0x00000030
Sascha Hauer47d37d62011-01-11 15:54:54 +010063#define AUART_LINECTRL2 0x00000040
Sascha Hauer47d37d62011-01-11 15:54:54 +010064#define AUART_INTR 0x00000050
Sascha Hauer47d37d62011-01-11 15:54:54 +010065#define AUART_DATA 0x00000060
66#define AUART_STAT 0x00000070
67#define AUART_DEBUG 0x00000080
68#define AUART_VERSION 0x00000090
69#define AUART_AUTOBAUD 0x000000a0
70
71#define AUART_CTRL0_SFTRST (1 << 31)
72#define AUART_CTRL0_CLKGATE (1 << 30)
Huang Shijiee8001632012-11-16 16:03:53 +080073#define AUART_CTRL0_RXTO_ENABLE (1 << 27)
74#define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
75#define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
76
77#define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
78
79#define AUART_CTRL2_DMAONERR (1 << 26)
80#define AUART_CTRL2_TXDMAE (1 << 25)
81#define AUART_CTRL2_RXDMAE (1 << 24)
Sascha Hauer47d37d62011-01-11 15:54:54 +010082
83#define AUART_CTRL2_CTSEN (1 << 15)
Huang Shijie00592022012-08-08 10:37:59 +080084#define AUART_CTRL2_RTSEN (1 << 14)
Sascha Hauer47d37d62011-01-11 15:54:54 +010085#define AUART_CTRL2_RTS (1 << 11)
86#define AUART_CTRL2_RXE (1 << 9)
87#define AUART_CTRL2_TXE (1 << 8)
88#define AUART_CTRL2_UARTEN (1 << 0)
89
Stefan Wahrendf57cf62015-08-11 11:46:01 +000090#define AUART_LINECTRL_BAUD_DIV_MAX 0x003fffc0
91#define AUART_LINECTRL_BAUD_DIV_MIN 0x000000ec
Sascha Hauer47d37d62011-01-11 15:54:54 +010092#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
93#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
94#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
95#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
96#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
97#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
Wolfgang Ockerf87fa712016-12-12 08:21:01 +010098#define AUART_LINECTRL_SPS (1 << 7)
Sascha Hauer47d37d62011-01-11 15:54:54 +010099#define AUART_LINECTRL_WLEN_MASK 0x00000060
100#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
101#define AUART_LINECTRL_FEN (1 << 4)
102#define AUART_LINECTRL_STP2 (1 << 3)
103#define AUART_LINECTRL_EPS (1 << 2)
104#define AUART_LINECTRL_PEN (1 << 1)
105#define AUART_LINECTRL_BRK (1 << 0)
106
107#define AUART_INTR_RTIEN (1 << 22)
108#define AUART_INTR_TXIEN (1 << 21)
109#define AUART_INTR_RXIEN (1 << 20)
110#define AUART_INTR_CTSMIEN (1 << 17)
111#define AUART_INTR_RTIS (1 << 6)
112#define AUART_INTR_TXIS (1 << 5)
113#define AUART_INTR_RXIS (1 << 4)
114#define AUART_INTR_CTSMIS (1 << 1)
115
116#define AUART_STAT_BUSY (1 << 29)
117#define AUART_STAT_CTS (1 << 28)
118#define AUART_STAT_TXFE (1 << 27)
119#define AUART_STAT_TXFF (1 << 25)
120#define AUART_STAT_RXFE (1 << 24)
121#define AUART_STAT_OERR (1 << 19)
122#define AUART_STAT_BERR (1 << 18)
123#define AUART_STAT_PERR (1 << 17)
124#define AUART_STAT_FERR (1 << 16)
Huang Shijiee8001632012-11-16 16:03:53 +0800125#define AUART_STAT_RXCOUNT_MASK 0xffff
Sascha Hauer47d37d62011-01-11 15:54:54 +0100126
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100127/*
128 * Start of Alphascale asm9260 defines
129 * This list contains only differences of existing bits
130 * between imx2x and asm9260
131 */
132#define ASM9260_HW_CTRL0 0x0000
133/*
134 * RW. Tell the UART to execute the RX DMA Command. The
135 * UART will clear this bit at the end of receive execution.
136 */
137#define ASM9260_BM_CTRL0_RXDMA_RUN BIT(28)
138/* RW. 0 use FIFO for status register; 1 use DMA */
139#define ASM9260_BM_CTRL0_RXTO_SOURCE_STATUS BIT(25)
140/*
141 * RW. RX TIMEOUT Enable. Valid for FIFO and DMA.
142 * Warning: If this bit is set to 0, the RX timeout will not affect receive DMA
143 * operation. If this bit is set to 1, a receive timeout will cause the receive
144 * DMA logic to terminate by filling the remaining DMA bytes with garbage data.
145 */
146#define ASM9260_BM_CTRL0_RXTO_ENABLE BIT(24)
147/*
148 * RW. Receive Timeout Counter Value: number of 8-bit-time to wait before
149 * asserting timeout on the RX input. If the RXFIFO is not empty and the RX
150 * input is idle, then the watchdog counter will decrement each bit-time. Note
151 * 7-bit-time is added to the programmed value, so a value of zero will set
152 * the counter to 7-bit-time, a value of 0x1 gives 15-bit-time and so on. Also
153 * note that the counter is reloaded at the end of each frame, so if the frame
154 * is 10 bits long and the timeout counter value is zero, then timeout will
155 * occur (when FIFO is not empty) even if the RX input is not idle. The default
156 * value is 0x3 (31 bit-time).
157 */
158#define ASM9260_BM_CTRL0_RXTO_MASK (0xff << 16)
159/* TIMEOUT = (100*7+1)*(1/BAUD) */
160#define ASM9260_BM_CTRL0_DEFAULT_RXTIMEOUT (20 << 16)
161
162/* TX ctrl register */
163#define ASM9260_HW_CTRL1 0x0010
164/*
165 * RW. Tell the UART to execute the TX DMA Command. The
166 * UART will clear this bit at the end of transmit execution.
167 */
168#define ASM9260_BM_CTRL1_TXDMA_RUN BIT(28)
169
170#define ASM9260_HW_CTRL2 0x0020
171/*
172 * RW. Receive Interrupt FIFO Level Select.
173 * The trigger points for the receive interrupt are as follows:
174 * ONE_EIGHTHS = 0x0 Trigger on FIFO full to at least 2 of 16 entries.
175 * ONE_QUARTER = 0x1 Trigger on FIFO full to at least 4 of 16 entries.
176 * ONE_HALF = 0x2 Trigger on FIFO full to at least 8 of 16 entries.
177 * THREE_QUARTERS = 0x3 Trigger on FIFO full to at least 12 of 16 entries.
178 * SEVEN_EIGHTHS = 0x4 Trigger on FIFO full to at least 14 of 16 entries.
179 */
180#define ASM9260_BM_CTRL2_RXIFLSEL (7 << 20)
181#define ASM9260_BM_CTRL2_DEFAULT_RXIFLSEL (3 << 20)
182/* RW. Same as RXIFLSEL */
183#define ASM9260_BM_CTRL2_TXIFLSEL (7 << 16)
184#define ASM9260_BM_CTRL2_DEFAULT_TXIFLSEL (2 << 16)
185/* RW. Set DTR. When this bit is 1, the output is 0. */
186#define ASM9260_BM_CTRL2_DTR BIT(10)
187/* RW. Loop Back Enable */
188#define ASM9260_BM_CTRL2_LBE BIT(7)
189#define ASM9260_BM_CTRL2_PORT_ENABLE BIT(0)
190
191#define ASM9260_HW_LINECTRL 0x0030
192/*
193 * RW. Stick Parity Select. When bits 1, 2, and 7 of this register are set, the
194 * parity bit is transmitted and checked as a 0. When bits 1 and 7 are set,
195 * and bit 2 is 0, the parity bit is transmitted and checked as a 1. When this
196 * bit is cleared stick parity is disabled.
197 */
198#define ASM9260_BM_LCTRL_SPS BIT(7)
199/* RW. Word length */
200#define ASM9260_BM_LCTRL_WLEN (3 << 5)
201#define ASM9260_BM_LCTRL_CHRL_5 (0 << 5)
202#define ASM9260_BM_LCTRL_CHRL_6 (1 << 5)
203#define ASM9260_BM_LCTRL_CHRL_7 (2 << 5)
204#define ASM9260_BM_LCTRL_CHRL_8 (3 << 5)
205
206/*
207 * Interrupt register.
208 * contains the interrupt enables and the interrupt status bits
209 */
210#define ASM9260_HW_INTR 0x0040
211/* Tx FIFO EMPTY Raw Interrupt enable */
212#define ASM9260_BM_INTR_TFEIEN BIT(27)
213/* Overrun Error Interrupt Enable. */
214#define ASM9260_BM_INTR_OEIEN BIT(26)
215/* Break Error Interrupt Enable. */
216#define ASM9260_BM_INTR_BEIEN BIT(25)
217/* Parity Error Interrupt Enable. */
218#define ASM9260_BM_INTR_PEIEN BIT(24)
219/* Framing Error Interrupt Enable. */
220#define ASM9260_BM_INTR_FEIEN BIT(23)
221
222/* nUARTDSR Modem Interrupt Enable. */
223#define ASM9260_BM_INTR_DSRMIEN BIT(19)
224/* nUARTDCD Modem Interrupt Enable. */
225#define ASM9260_BM_INTR_DCDMIEN BIT(18)
226/* nUARTRI Modem Interrupt Enable. */
227#define ASM9260_BM_INTR_RIMIEN BIT(16)
228/* Auto-Boud Timeout */
229#define ASM9260_BM_INTR_ABTO BIT(13)
230#define ASM9260_BM_INTR_ABEO BIT(12)
231/* Tx FIFO EMPTY Raw Interrupt state */
232#define ASM9260_BM_INTR_TFEIS BIT(11)
233/* Overrun Error */
234#define ASM9260_BM_INTR_OEIS BIT(10)
235/* Break Error */
236#define ASM9260_BM_INTR_BEIS BIT(9)
237/* Parity Error */
238#define ASM9260_BM_INTR_PEIS BIT(8)
239/* Framing Error */
240#define ASM9260_BM_INTR_FEIS BIT(7)
241#define ASM9260_BM_INTR_DSRMIS BIT(3)
242#define ASM9260_BM_INTR_DCDMIS BIT(2)
243#define ASM9260_BM_INTR_RIMIS BIT(0)
244
245/*
246 * RW. In DMA mode, up to 4 Received/Transmit characters can be accessed at a
247 * time. In PIO mode, only one character can be accessed at a time. The status
248 * register contains the receive data flags and valid bits.
249 */
250#define ASM9260_HW_DATA 0x0050
251
252#define ASM9260_HW_STAT 0x0060
253/* RO. If 1, UARTAPP is present in this product. */
254#define ASM9260_BM_STAT_PRESENT BIT(31)
255/* RO. If 1, HISPEED is present in this product. */
256#define ASM9260_BM_STAT_HISPEED BIT(30)
257/* RO. Receive FIFO Full. */
258#define ASM9260_BM_STAT_RXFULL BIT(26)
259
260/* RO. The UART Debug Register contains the state of the DMA signals. */
261#define ASM9260_HW_DEBUG 0x0070
262/* DMA Command Run Status */
263#define ASM9260_BM_DEBUG_TXDMARUN BIT(5)
264#define ASM9260_BM_DEBUG_RXDMARUN BIT(4)
265/* DMA Command End Status */
266#define ASM9260_BM_DEBUG_TXCMDEND BIT(3)
267#define ASM9260_BM_DEBUG_RXCMDEND BIT(2)
268/* DMA Request Status */
269#define ASM9260_BM_DEBUG_TXDMARQ BIT(1)
270#define ASM9260_BM_DEBUG_RXDMARQ BIT(0)
271
272#define ASM9260_HW_ILPR 0x0080
273
274#define ASM9260_HW_RS485CTRL 0x0090
275/*
276 * RW. This bit reverses the polarity of the direction control signal on the RTS
277 * (or DTR) pin.
278 * If 0, The direction control pin will be driven to logic ‘0’ when the
279 * transmitter has data to be sent. It will be driven to logic ‘1’ after the
280 * last bit of data has been transmitted.
281 */
282#define ASM9260_BM_RS485CTRL_ONIV BIT(5)
283/* RW. Enable Auto Direction Control. */
284#define ASM9260_BM_RS485CTRL_DIR_CTRL BIT(4)
285/*
286 * RW. If 0 and DIR_CTRL = 1, pin RTS is used for direction control.
287 * If 1 and DIR_CTRL = 1, pin DTR is used for direction control.
288 */
289#define ASM9260_BM_RS485CTRL_PINSEL BIT(3)
290/* RW. Enable Auto Address Detect (AAD). */
291#define ASM9260_BM_RS485CTRL_AADEN BIT(2)
292/* RW. Disable receiver. */
293#define ASM9260_BM_RS485CTRL_RXDIS BIT(1)
294/* RW. Enable RS-485/EIA-485 Normal Multidrop Mode (NMM) */
295#define ASM9260_BM_RS485CTRL_RS485EN BIT(0)
296
297#define ASM9260_HW_RS485ADRMATCH 0x00a0
298/* Contains the address match value. */
299#define ASM9260_BM_RS485ADRMATCH_MASK (0xff << 0)
300
301#define ASM9260_HW_RS485DLY 0x00b0
302/*
303 * RW. Contains the direction control (RTS or DTR) delay value. This delay time
304 * is in periods of the baud clock.
305 */
306#define ASM9260_BM_RS485DLY_MASK (0xff << 0)
307
308#define ASM9260_HW_AUTOBAUD 0x00c0
309/* WO. Auto-baud time-out interrupt clear bit. */
310#define ASM9260_BM_AUTOBAUD_TO_INT_CLR BIT(9)
311/* WO. End of auto-baud interrupt clear bit. */
312#define ASM9260_BM_AUTOBAUD_EO_INT_CLR BIT(8)
313/* Restart in case of timeout (counter restarts at next UART Rx falling edge) */
314#define ASM9260_BM_AUTOBAUD_AUTORESTART BIT(2)
315/* Auto-baud mode select bit. 0 - Mode 0, 1 - Mode 1. */
316#define ASM9260_BM_AUTOBAUD_MODE BIT(1)
317/*
318 * Auto-baud start (auto-baud is running). Auto-baud run bit. This bit is
319 * automatically cleared after auto-baud completion.
320 */
321#define ASM9260_BM_AUTOBAUD_START BIT(0)
322
323#define ASM9260_HW_CTRL3 0x00d0
324#define ASM9260_BM_CTRL3_OUTCLK_DIV_MASK (0xffff << 16)
325/*
326 * RW. Provide clk over OUTCLK pin. In case of asm9260 it can be configured on
327 * pins 137 and 144.
328 */
329#define ASM9260_BM_CTRL3_MASTERMODE BIT(6)
330/* RW. Baud Rate Mode: 1 - Enable sync mode. 0 - async mode. */
331#define ASM9260_BM_CTRL3_SYNCMODE BIT(4)
332/* RW. 1 - MSB bit send frist; 0 - LSB bit frist. */
333#define ASM9260_BM_CTRL3_MSBF BIT(2)
334/* RW. 1 - sample rate = 8 x Baudrate; 0 - sample rate = 16 x Baudrate. */
335#define ASM9260_BM_CTRL3_BAUD8 BIT(1)
336/* RW. 1 - Set word length to 9bit. 0 - use ASM9260_BM_LCTRL_WLEN */
337#define ASM9260_BM_CTRL3_9BIT BIT(0)
338
339#define ASM9260_HW_ISO7816_CTRL 0x00e0
340/* RW. Enable High Speed mode. */
341#define ASM9260_BM_ISO7816CTRL_HS BIT(12)
342/* Disable Successive Receive NACK */
343#define ASM9260_BM_ISO7816CTRL_DS_NACK BIT(8)
344#define ASM9260_BM_ISO7816CTRL_MAX_ITER_MASK (0xff << 4)
345/* Receive NACK Inhibit */
346#define ASM9260_BM_ISO7816CTRL_INACK BIT(3)
347#define ASM9260_BM_ISO7816CTRL_NEG_DATA BIT(2)
348/* RW. 1 - ISO7816 mode; 0 - USART mode */
349#define ASM9260_BM_ISO7816CTRL_ENABLE BIT(0)
350
351#define ASM9260_HW_ISO7816_ERRCNT 0x00f0
352/* Parity error counter. Will be cleared after reading */
353#define ASM9260_BM_ISO7816_NB_ERRORS_MASK (0xff << 0)
354
355#define ASM9260_HW_ISO7816_STATUS 0x0100
356/* Max number of Repetitions Reached */
357#define ASM9260_BM_ISO7816_STAT_ITERATION BIT(0)
358
359/* End of Alphascale asm9260 defines */
360
Sascha Hauer47d37d62011-01-11 15:54:54 +0100361static struct uart_driver auart_driver;
362
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800363enum mxs_auart_type {
364 IMX23_AUART,
365 IMX28_AUART,
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100366 ASM9260_AUART,
367};
368
369struct vendor_data {
370 const u16 *reg_offset;
371};
372
373enum {
374 REG_CTRL0,
375 REG_CTRL1,
376 REG_CTRL2,
377 REG_LINECTRL,
378 REG_LINECTRL2,
379 REG_INTR,
380 REG_DATA,
381 REG_STAT,
382 REG_DEBUG,
383 REG_VERSION,
384 REG_AUTOBAUD,
385
386 /* The size of the array - must be last */
387 REG_ARRAY_SIZE,
388};
389
390static const u16 mxs_asm9260_offsets[REG_ARRAY_SIZE] = {
391 [REG_CTRL0] = ASM9260_HW_CTRL0,
392 [REG_CTRL1] = ASM9260_HW_CTRL1,
393 [REG_CTRL2] = ASM9260_HW_CTRL2,
394 [REG_LINECTRL] = ASM9260_HW_LINECTRL,
395 [REG_INTR] = ASM9260_HW_INTR,
396 [REG_DATA] = ASM9260_HW_DATA,
397 [REG_STAT] = ASM9260_HW_STAT,
398 [REG_DEBUG] = ASM9260_HW_DEBUG,
399 [REG_AUTOBAUD] = ASM9260_HW_AUTOBAUD,
400};
401
402static const u16 mxs_stmp37xx_offsets[REG_ARRAY_SIZE] = {
403 [REG_CTRL0] = AUART_CTRL0,
404 [REG_CTRL1] = AUART_CTRL1,
405 [REG_CTRL2] = AUART_CTRL2,
406 [REG_LINECTRL] = AUART_LINECTRL,
407 [REG_LINECTRL2] = AUART_LINECTRL2,
408 [REG_INTR] = AUART_INTR,
409 [REG_DATA] = AUART_DATA,
410 [REG_STAT] = AUART_STAT,
411 [REG_DEBUG] = AUART_DEBUG,
412 [REG_VERSION] = AUART_VERSION,
413 [REG_AUTOBAUD] = AUART_AUTOBAUD,
414};
415
416static const struct vendor_data vendor_alphascale_asm9260 = {
417 .reg_offset = mxs_asm9260_offsets,
418};
419
420static const struct vendor_data vendor_freescale_stmp37xx = {
421 .reg_offset = mxs_stmp37xx_offsets,
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800422};
423
Sascha Hauer47d37d62011-01-11 15:54:54 +0100424struct mxs_auart_port {
425 struct uart_port port;
426
Huang Shijiee8001632012-11-16 16:03:53 +0800427#define MXS_AUART_DMA_ENABLED 0x2
428#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
429#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
Huang Shijie8418e672013-08-03 10:09:14 -0400430#define MXS_AUART_RTSCTS 4 /* bit 4 */
Huang Shijiee8001632012-11-16 16:03:53 +0800431 unsigned long flags;
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200432 unsigned int mctrl_prev;
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800433 enum mxs_auart_type devtype;
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100434 const struct vendor_data *vendor;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100435
Sascha Hauer47d37d62011-01-11 15:54:54 +0100436 struct clk *clk;
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100437 struct clk *clk_ahb;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100438 struct device *dev;
Huang Shijiee8001632012-11-16 16:03:53 +0800439
440 /* for DMA */
Huang Shijiee8001632012-11-16 16:03:53 +0800441 struct scatterlist tx_sgl;
442 struct dma_chan *tx_dma_chan;
443 void *tx_dma_buf;
444
445 struct scatterlist rx_sgl;
446 struct dma_chan *rx_dma_chan;
447 void *rx_dma_buf;
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200448
449 struct mctrl_gpios *gpios;
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200450 int gpio_irq[UART_GPIO_MAX];
451 bool ms_irq_enabled;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100452};
453
Krzysztof Kozlowski0cd45212015-05-02 00:40:03 +0900454static const struct platform_device_id mxs_auart_devtype[] = {
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800455 { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
456 { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100457 { .name = "as-auart-asm9260", .driver_data = ASM9260_AUART },
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800458 { /* sentinel */ }
459};
460MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
461
Fabian Fredericked0bb232015-03-16 20:17:11 +0100462static const struct of_device_id mxs_auart_dt_ids[] = {
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800463 {
464 .compatible = "fsl,imx28-auart",
465 .data = &mxs_auart_devtype[IMX28_AUART]
466 }, {
467 .compatible = "fsl,imx23-auart",
468 .data = &mxs_auart_devtype[IMX23_AUART]
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100469 }, {
470 .compatible = "alphascale,asm9260-auart",
471 .data = &mxs_auart_devtype[ASM9260_AUART]
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800472 }, { /* sentinel */ }
473};
474MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
475
476static inline int is_imx28_auart(struct mxs_auart_port *s)
477{
478 return s->devtype == IMX28_AUART;
479}
480
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100481static inline int is_asm9260_auart(struct mxs_auart_port *s)
482{
483 return s->devtype == ASM9260_AUART;
484}
485
Huang Shijiee8001632012-11-16 16:03:53 +0800486static inline bool auart_dma_enabled(struct mxs_auart_port *s)
487{
488 return s->flags & MXS_AUART_DMA_ENABLED;
489}
490
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100491static unsigned int mxs_reg_to_offset(const struct mxs_auart_port *uap,
492 unsigned int reg)
493{
494 return uap->vendor->reg_offset[reg];
495}
496
497static unsigned int mxs_read(const struct mxs_auart_port *uap,
498 unsigned int reg)
499{
500 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
501
502 return readl_relaxed(addr);
503}
504
505static void mxs_write(unsigned int val, struct mxs_auart_port *uap,
506 unsigned int reg)
507{
508 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
509
510 writel_relaxed(val, addr);
511}
512
513static void mxs_set(unsigned int val, struct mxs_auart_port *uap,
514 unsigned int reg)
515{
516 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
517
518 writel_relaxed(val, addr + SET_REG);
519}
520
521static void mxs_clr(unsigned int val, struct mxs_auart_port *uap,
522 unsigned int reg)
523{
524 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
525
526 writel_relaxed(val, addr + CLR_REG);
527}
528
Sascha Hauer47d37d62011-01-11 15:54:54 +0100529static void mxs_auart_stop_tx(struct uart_port *u);
530
531#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
532
Huang Shijiee8001632012-11-16 16:03:53 +0800533static void mxs_auart_tx_chars(struct mxs_auart_port *s);
534
535static void dma_tx_callback(void *param)
536{
537 struct mxs_auart_port *s = param;
538 struct circ_buf *xmit = &s->port.state->xmit;
539
540 dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
541
542 /* clear the bit used to serialize the DMA tx. */
543 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100544 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800545
546 /* wake up the possible processes. */
547 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
548 uart_write_wakeup(&s->port);
549
550 mxs_auart_tx_chars(s);
551}
552
553static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
554{
555 struct dma_async_tx_descriptor *desc;
556 struct scatterlist *sgl = &s->tx_sgl;
557 struct dma_chan *channel = s->tx_dma_chan;
558 u32 pio;
559
560 /* [1] : send PIO. Note, the first pio word is CTRL1. */
561 pio = AUART_CTRL1_XFER_COUNT(size);
562 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
563 1, DMA_TRANS_NONE, 0);
564 if (!desc) {
565 dev_err(s->dev, "step 1 error\n");
566 return -EINVAL;
567 }
568
569 /* [2] : set DMA buffer. */
570 sg_init_one(sgl, s->tx_dma_buf, size);
571 dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
572 desc = dmaengine_prep_slave_sg(channel, sgl,
573 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
574 if (!desc) {
575 dev_err(s->dev, "step 2 error\n");
576 return -EINVAL;
577 }
578
579 /* [3] : submit the DMA */
580 desc->callback = dma_tx_callback;
581 desc->callback_param = s;
582 dmaengine_submit(desc);
583 dma_async_issue_pending(channel);
584 return 0;
585}
586
587static void mxs_auart_tx_chars(struct mxs_auart_port *s)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100588{
589 struct circ_buf *xmit = &s->port.state->xmit;
590
Huang Shijiee8001632012-11-16 16:03:53 +0800591 if (auart_dma_enabled(s)) {
fabio.estevam@freescale.com87b8bed2013-01-07 23:11:06 -0200592 u32 i = 0;
Huang Shijiee8001632012-11-16 16:03:53 +0800593 int size;
594 void *buffer = s->tx_dma_buf;
595
596 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
597 return;
598
599 while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
600 size = min_t(u32, UART_XMIT_SIZE - i,
601 CIRC_CNT_TO_END(xmit->head,
602 xmit->tail,
603 UART_XMIT_SIZE));
604 memcpy(buffer + i, xmit->buf + xmit->tail, size);
605 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
606
607 i += size;
608 if (i >= UART_XMIT_SIZE)
609 break;
610 }
611
612 if (uart_tx_stopped(&s->port))
613 mxs_auart_stop_tx(&s->port);
614
615 if (i) {
616 mxs_auart_dma_tx(s, i);
617 } else {
618 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100619 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800620 }
621 return;
622 }
623
624
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100625 while (!(mxs_read(s, REG_STAT) & AUART_STAT_TXFF)) {
Sascha Hauer47d37d62011-01-11 15:54:54 +0100626 if (s->port.x_char) {
627 s->port.icount.tx++;
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100628 mxs_write(s->port.x_char, s, REG_DATA);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100629 s->port.x_char = 0;
630 continue;
631 }
632 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
633 s->port.icount.tx++;
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100634 mxs_write(xmit->buf[xmit->tail], s, REG_DATA);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100635 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100636 } else
637 break;
638 }
Uwe Kleine-Königd0758a22011-11-22 14:22:56 +0100639 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
640 uart_write_wakeup(&s->port);
641
Sascha Hauer47d37d62011-01-11 15:54:54 +0100642 if (uart_circ_empty(&(s->port.state->xmit)))
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100643 mxs_clr(AUART_INTR_TXIEN, s, REG_INTR);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100644 else
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100645 mxs_set(AUART_INTR_TXIEN, s, REG_INTR);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100646
647 if (uart_tx_stopped(&s->port))
648 mxs_auart_stop_tx(&s->port);
649}
650
651static void mxs_auart_rx_char(struct mxs_auart_port *s)
652{
653 int flag;
654 u32 stat;
655 u8 c;
656
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100657 c = mxs_read(s, REG_DATA);
658 stat = mxs_read(s, REG_STAT);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100659
660 flag = TTY_NORMAL;
661 s->port.icount.rx++;
662
663 if (stat & AUART_STAT_BERR) {
664 s->port.icount.brk++;
665 if (uart_handle_break(&s->port))
666 goto out;
667 } else if (stat & AUART_STAT_PERR) {
668 s->port.icount.parity++;
669 } else if (stat & AUART_STAT_FERR) {
670 s->port.icount.frame++;
671 }
672
673 /*
674 * Mask off conditions which should be ingored.
675 */
676 stat &= s->port.read_status_mask;
677
678 if (stat & AUART_STAT_BERR) {
679 flag = TTY_BREAK;
680 } else if (stat & AUART_STAT_PERR)
681 flag = TTY_PARITY;
682 else if (stat & AUART_STAT_FERR)
683 flag = TTY_FRAME;
684
685 if (stat & AUART_STAT_OERR)
686 s->port.icount.overrun++;
687
688 if (uart_handle_sysrq_char(&s->port, c))
689 goto out;
690
691 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
692out:
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100693 mxs_write(stat, s, REG_STAT);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100694}
695
696static void mxs_auart_rx_chars(struct mxs_auart_port *s)
697{
Sascha Hauer47d37d62011-01-11 15:54:54 +0100698 u32 stat = 0;
699
700 for (;;) {
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100701 stat = mxs_read(s, REG_STAT);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100702 if (stat & AUART_STAT_RXFE)
703 break;
704 mxs_auart_rx_char(s);
705 }
706
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100707 mxs_write(stat, s, REG_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100708 tty_flip_buffer_push(&s->port.state->port);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100709}
710
711static int mxs_auart_request_port(struct uart_port *u)
712{
713 return 0;
714}
715
716static int mxs_auart_verify_port(struct uart_port *u,
717 struct serial_struct *ser)
718{
719 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
720 return -EINVAL;
721 return 0;
722}
723
724static void mxs_auart_config_port(struct uart_port *u, int flags)
725{
726}
727
728static const char *mxs_auart_type(struct uart_port *u)
729{
730 struct mxs_auart_port *s = to_auart_port(u);
731
732 return dev_name(s->dev);
733}
734
735static void mxs_auart_release_port(struct uart_port *u)
736{
737}
738
739static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
740{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200741 struct mxs_auart_port *s = to_auart_port(u);
742
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100743 u32 ctrl = mxs_read(s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100744
Steffen Trumtrara6833212012-12-13 14:27:43 +0100745 ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
Huang Shijie00592022012-08-08 10:37:59 +0800746 if (mctrl & TIOCM_RTS) {
Peter Hurley299245a2014-09-10 15:06:24 -0400747 if (uart_cts_enabled(u))
Huang Shijie00592022012-08-08 10:37:59 +0800748 ctrl |= AUART_CTRL2_RTSEN;
Steffen Trumtrara6833212012-12-13 14:27:43 +0100749 else
750 ctrl |= AUART_CTRL2_RTS;
Huang Shijie00592022012-08-08 10:37:59 +0800751 }
752
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100753 mxs_write(ctrl, s, REG_CTRL2);
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200754
755 mctrl_gpio_set(s->gpios, mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100756}
757
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200758#define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
759static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
760{
761 u32 mctrl_diff;
762
763 mctrl_diff = mctrl ^ s->mctrl_prev;
764 s->mctrl_prev = mctrl;
765 if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
766 s->port.state != NULL) {
767 if (mctrl_diff & TIOCM_RI)
768 s->port.icount.rng++;
769 if (mctrl_diff & TIOCM_DSR)
770 s->port.icount.dsr++;
771 if (mctrl_diff & TIOCM_CD)
772 uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
773 if (mctrl_diff & TIOCM_CTS)
774 uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
775
776 wake_up_interruptible(&s->port.state->port.delta_msr_wait);
777 }
778 return mctrl;
779}
780
Sascha Hauer47d37d62011-01-11 15:54:54 +0100781static u32 mxs_auart_get_mctrl(struct uart_port *u)
782{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200783 struct mxs_auart_port *s = to_auart_port(u);
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100784 u32 stat = mxs_read(s, REG_STAT);
Janusz Uzycki42b4eba2014-10-10 18:53:24 +0200785 u32 mctrl = 0;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100786
Sascha Hauer47d37d62011-01-11 15:54:54 +0100787 if (stat & AUART_STAT_CTS)
788 mctrl |= TIOCM_CTS;
789
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200790 return mctrl_gpio_get(s->gpios, &mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100791}
792
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200793/*
794 * Enable modem status interrupts
795 */
796static void mxs_auart_enable_ms(struct uart_port *port)
797{
798 struct mxs_auart_port *s = to_auart_port(port);
799
800 /*
801 * Interrupt should not be enabled twice
802 */
803 if (s->ms_irq_enabled)
804 return;
805
806 s->ms_irq_enabled = true;
807
808 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
809 enable_irq(s->gpio_irq[UART_GPIO_CTS]);
810 /* TODO: enable AUART_INTR_CTSMIEN otherwise */
811
812 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
813 enable_irq(s->gpio_irq[UART_GPIO_DSR]);
814
815 if (s->gpio_irq[UART_GPIO_RI] >= 0)
816 enable_irq(s->gpio_irq[UART_GPIO_RI]);
817
818 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
819 enable_irq(s->gpio_irq[UART_GPIO_DCD]);
820}
821
822/*
823 * Disable modem status interrupts
824 */
825static void mxs_auart_disable_ms(struct uart_port *port)
826{
827 struct mxs_auart_port *s = to_auart_port(port);
828
829 /*
830 * Interrupt should not be disabled twice
831 */
832 if (!s->ms_irq_enabled)
833 return;
834
835 s->ms_irq_enabled = false;
836
837 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
838 disable_irq(s->gpio_irq[UART_GPIO_CTS]);
839 /* TODO: disable AUART_INTR_CTSMIEN otherwise */
840
841 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
842 disable_irq(s->gpio_irq[UART_GPIO_DSR]);
843
844 if (s->gpio_irq[UART_GPIO_RI] >= 0)
845 disable_irq(s->gpio_irq[UART_GPIO_RI]);
846
847 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
848 disable_irq(s->gpio_irq[UART_GPIO_DCD]);
849}
850
Huang Shijiee8001632012-11-16 16:03:53 +0800851static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
852static void dma_rx_callback(void *arg)
853{
854 struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100855 struct tty_port *port = &s->port.state->port;
Huang Shijiee8001632012-11-16 16:03:53 +0800856 int count;
857 u32 stat;
858
Huang Shijied7ffb932012-11-22 15:06:30 +0800859 dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
860
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100861 stat = mxs_read(s, REG_STAT);
Huang Shijiee8001632012-11-16 16:03:53 +0800862 stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
863 AUART_STAT_PERR | AUART_STAT_FERR);
864
865 count = stat & AUART_STAT_RXCOUNT_MASK;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100866 tty_insert_flip_string(port, s->rx_dma_buf, count);
Huang Shijiee8001632012-11-16 16:03:53 +0800867
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100868 mxs_write(stat, s, REG_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100869 tty_flip_buffer_push(port);
Huang Shijiee8001632012-11-16 16:03:53 +0800870
871 /* start the next DMA for RX. */
872 mxs_auart_dma_prep_rx(s);
873}
874
875static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
876{
877 struct dma_async_tx_descriptor *desc;
878 struct scatterlist *sgl = &s->rx_sgl;
879 struct dma_chan *channel = s->rx_dma_chan;
880 u32 pio[1];
881
882 /* [1] : send PIO */
883 pio[0] = AUART_CTRL0_RXTO_ENABLE
884 | AUART_CTRL0_RXTIMEOUT(0x80)
885 | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
886 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
887 1, DMA_TRANS_NONE, 0);
888 if (!desc) {
889 dev_err(s->dev, "step 1 error\n");
890 return -EINVAL;
891 }
892
893 /* [2] : send DMA request */
894 sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
895 dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
896 desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
897 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
898 if (!desc) {
899 dev_err(s->dev, "step 2 error\n");
900 return -1;
901 }
902
903 /* [3] : submit the DMA, but do not issue it. */
904 desc->callback = dma_rx_callback;
905 desc->callback_param = s;
906 dmaengine_submit(desc);
907 dma_async_issue_pending(channel);
908 return 0;
909}
910
911static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
912{
913 if (s->tx_dma_chan) {
914 dma_release_channel(s->tx_dma_chan);
915 s->tx_dma_chan = NULL;
916 }
917 if (s->rx_dma_chan) {
918 dma_release_channel(s->rx_dma_chan);
919 s->rx_dma_chan = NULL;
920 }
921
922 kfree(s->tx_dma_buf);
923 kfree(s->rx_dma_buf);
924 s->tx_dma_buf = NULL;
925 s->rx_dma_buf = NULL;
926}
927
928static void mxs_auart_dma_exit(struct mxs_auart_port *s)
929{
930
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100931 mxs_clr(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
932 s, REG_CTRL2);
Huang Shijiee8001632012-11-16 16:03:53 +0800933
934 mxs_auart_dma_exit_channel(s);
935 s->flags &= ~MXS_AUART_DMA_ENABLED;
936 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
937 clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
938}
939
940static int mxs_auart_dma_init(struct mxs_auart_port *s)
941{
Huang Shijiee8001632012-11-16 16:03:53 +0800942 if (auart_dma_enabled(s))
943 return 0;
944
Huang Shijiee8001632012-11-16 16:03:53 +0800945 /* init for RX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800946 s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
Huang Shijiee8001632012-11-16 16:03:53 +0800947 if (!s->rx_dma_chan)
948 goto err_out;
949 s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
950 if (!s->rx_dma_buf)
951 goto err_out;
952
953 /* init for TX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800954 s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
Huang Shijiee8001632012-11-16 16:03:53 +0800955 if (!s->tx_dma_chan)
956 goto err_out;
957 s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
958 if (!s->tx_dma_buf)
959 goto err_out;
960
961 /* set the flags */
962 s->flags |= MXS_AUART_DMA_ENABLED;
963 dev_dbg(s->dev, "enabled the DMA support.");
964
Hector Palacios9987f762013-10-03 09:32:03 +0200965 /* The DMA buffer is now the FIFO the TTY subsystem can use */
966 s->port.fifosize = UART_XMIT_SIZE;
967
Huang Shijiee8001632012-11-16 16:03:53 +0800968 return 0;
969
970err_out:
971 mxs_auart_dma_exit_channel(s);
972 return -EINVAL;
973
974}
975
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200976#define RTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
977 UART_GPIO_RTS))
978#define CTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
979 UART_GPIO_CTS))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100980static void mxs_auart_settermios(struct uart_port *u,
981 struct ktermios *termios,
982 struct ktermios *old)
983{
Huang Shijiee8001632012-11-16 16:03:53 +0800984 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100985 u32 bm, ctrl, ctrl2, div;
Stefan Wahrendf57cf62015-08-11 11:46:01 +0000986 unsigned int cflag, baud, baud_min, baud_max;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100987
988 cflag = termios->c_cflag;
989
990 ctrl = AUART_LINECTRL_FEN;
Oleksij Rempel254da0d2016-03-16 14:05:52 +0100991 ctrl2 = mxs_read(s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100992
993 /* byte size */
994 switch (cflag & CSIZE) {
995 case CS5:
996 bm = 0;
997 break;
998 case CS6:
999 bm = 1;
1000 break;
1001 case CS7:
1002 bm = 2;
1003 break;
1004 case CS8:
1005 bm = 3;
1006 break;
1007 default:
1008 return;
1009 }
1010
1011 ctrl |= AUART_LINECTRL_WLEN(bm);
1012
1013 /* parity */
1014 if (cflag & PARENB) {
1015 ctrl |= AUART_LINECTRL_PEN;
1016 if ((cflag & PARODD) == 0)
1017 ctrl |= AUART_LINECTRL_EPS;
Wolfgang Ockerf87fa712016-12-12 08:21:01 +01001018 if (cflag & CMSPAR)
1019 ctrl |= AUART_LINECTRL_SPS;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001020 }
1021
Wolfgang Ockerb8106452016-11-16 12:37:45 +01001022 u->read_status_mask = AUART_STAT_OERR;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001023
1024 if (termios->c_iflag & INPCK)
1025 u->read_status_mask |= AUART_STAT_PERR;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04001026 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Sascha Hauer47d37d62011-01-11 15:54:54 +01001027 u->read_status_mask |= AUART_STAT_BERR;
1028
1029 /*
1030 * Characters to ignore
1031 */
1032 u->ignore_status_mask = 0;
1033 if (termios->c_iflag & IGNPAR)
1034 u->ignore_status_mask |= AUART_STAT_PERR;
1035 if (termios->c_iflag & IGNBRK) {
1036 u->ignore_status_mask |= AUART_STAT_BERR;
1037 /*
1038 * If we're ignoring parity and break indicators,
1039 * ignore overruns too (for real raw support).
1040 */
1041 if (termios->c_iflag & IGNPAR)
1042 u->ignore_status_mask |= AUART_STAT_OERR;
1043 }
1044
1045 /*
1046 * ignore all characters if CREAD is not set
1047 */
1048 if (cflag & CREAD)
1049 ctrl2 |= AUART_CTRL2_RXE;
1050 else
1051 ctrl2 &= ~AUART_CTRL2_RXE;
1052
1053 /* figure out the stop bits requested */
1054 if (cflag & CSTOPB)
1055 ctrl |= AUART_LINECTRL_STP2;
1056
1057 /* figure out the hardware flow control settings */
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001058 ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
Huang Shijiee8001632012-11-16 16:03:53 +08001059 if (cflag & CRTSCTS) {
1060 /*
1061 * The DMA has a bug(see errata:2836) in mx23.
1062 * So we can not implement the DMA for auart in mx23,
1063 * we can only implement the DMA support for auart
1064 * in mx28.
1065 */
Huang Shijieafab2202013-08-03 10:09:15 -04001066 if (is_imx28_auart(s)
Huang Shijie8418e672013-08-03 10:09:14 -04001067 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
Huang Shijiee8001632012-11-16 16:03:53 +08001068 if (!mxs_auart_dma_init(s))
1069 /* enable DMA tranfer */
1070 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
1071 | AUART_CTRL2_DMAONERR;
1072 }
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001073 /* Even if RTS is GPIO line RTSEN can be enabled because
1074 * the pinctrl configuration decides about RTS pin function */
1075 ctrl2 |= AUART_CTRL2_RTSEN;
1076 if (CTS_AT_AUART())
1077 ctrl2 |= AUART_CTRL2_CTSEN;
Huang Shijiee8001632012-11-16 16:03:53 +08001078 }
Sascha Hauer47d37d62011-01-11 15:54:54 +01001079
1080 /* set baud rate */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001081 if (is_asm9260_auart(s)) {
1082 baud = uart_get_baud_rate(u, termios, old,
1083 u->uartclk * 4 / 0x3FFFFF,
1084 u->uartclk / 16);
1085 div = u->uartclk * 4 / baud;
1086 } else {
1087 baud_min = DIV_ROUND_UP(u->uartclk * 32,
1088 AUART_LINECTRL_BAUD_DIV_MAX);
1089 baud_max = u->uartclk * 32 / AUART_LINECTRL_BAUD_DIV_MIN;
1090 baud = uart_get_baud_rate(u, termios, old, baud_min, baud_max);
Uwe Kleine-Königa6040bc2017-03-20 10:05:38 +01001091 div = DIV_ROUND_CLOSEST(u->uartclk * 32, baud);
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001092 }
1093
Sascha Hauer47d37d62011-01-11 15:54:54 +01001094 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
1095 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001096 mxs_write(ctrl, s, REG_LINECTRL);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001097
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001098 mxs_write(ctrl2, s, REG_CTRL2);
Lothar Waßmann8b979f72012-05-03 11:37:12 +02001099
1100 uart_update_timeout(u, termios->c_cflag, baud);
Huang Shijiee8001632012-11-16 16:03:53 +08001101
1102 /* prepare for the DMA RX. */
1103 if (auart_dma_enabled(s) &&
1104 !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
1105 if (!mxs_auart_dma_prep_rx(s)) {
1106 /* Disable the normal RX interrupt. */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001107 mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
1108 s, REG_INTR);
Huang Shijiee8001632012-11-16 16:03:53 +08001109 } else {
1110 mxs_auart_dma_exit(s);
1111 dev_err(s->dev, "We can not start up the DMA.\n");
1112 }
1113 }
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001114
1115 /* CTS flow-control and modem-status interrupts */
1116 if (UART_ENABLE_MS(u, termios->c_cflag))
1117 mxs_auart_enable_ms(u);
1118 else
1119 mxs_auart_disable_ms(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001120}
1121
Fabio Estevamf3006e42014-11-12 20:32:49 -02001122static void mxs_auart_set_ldisc(struct uart_port *port,
1123 struct ktermios *termios)
Janusz Uzycki36a26272014-10-10 18:53:27 +02001124{
Fabio Estevamf3006e42014-11-12 20:32:49 -02001125 if (termios->c_line == N_PPS) {
Janusz Uzycki36a26272014-10-10 18:53:27 +02001126 port->flags |= UPF_HARDPPS_CD;
1127 mxs_auart_enable_ms(port);
1128 } else {
1129 port->flags &= ~UPF_HARDPPS_CD;
1130 }
1131}
1132
Sascha Hauer47d37d62011-01-11 15:54:54 +01001133static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
1134{
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +02001135 u32 istat;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001136 struct mxs_auart_port *s = context;
Janusz Uzycki08f937f2014-11-14 23:24:33 +01001137 u32 mctrl_temp = s->mctrl_prev;
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001138 u32 stat = mxs_read(s, REG_STAT);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001139
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001140 istat = mxs_read(s, REG_INTR);
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +02001141
1142 /* ack irq */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001143 mxs_clr(istat & (AUART_INTR_RTIS | AUART_INTR_TXIS | AUART_INTR_RXIS
1144 | AUART_INTR_CTSMIS), s, REG_INTR);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001145
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001146 /*
1147 * Dealing with GPIO interrupt
1148 */
1149 if (irq == s->gpio_irq[UART_GPIO_CTS] ||
1150 irq == s->gpio_irq[UART_GPIO_DCD] ||
1151 irq == s->gpio_irq[UART_GPIO_DSR] ||
1152 irq == s->gpio_irq[UART_GPIO_RI])
1153 mxs_auart_modem_status(s,
Janusz Uzycki08f937f2014-11-14 23:24:33 +01001154 mctrl_gpio_get(s->gpios, &mctrl_temp));
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001155
Sascha Hauer47d37d62011-01-11 15:54:54 +01001156 if (istat & AUART_INTR_CTSMIS) {
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001157 if (CTS_AT_AUART() && s->ms_irq_enabled)
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001158 uart_handle_cts_change(&s->port,
1159 stat & AUART_STAT_CTS);
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001160 mxs_clr(AUART_INTR_CTSMIS, s, REG_INTR);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001161 istat &= ~AUART_INTR_CTSMIS;
1162 }
1163
1164 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
Huang Shijiea5919442012-11-22 15:06:29 +08001165 if (!auart_dma_enabled(s))
1166 mxs_auart_rx_chars(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001167 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
1168 }
1169
1170 if (istat & AUART_INTR_TXIS) {
1171 mxs_auart_tx_chars(s);
1172 istat &= ~AUART_INTR_TXIS;
1173 }
1174
Sascha Hauer47d37d62011-01-11 15:54:54 +01001175 return IRQ_HANDLED;
1176}
1177
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001178static void mxs_auart_reset_deassert(struct mxs_auart_port *s)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001179{
1180 int i;
1181 unsigned int reg;
1182
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001183 mxs_clr(AUART_CTRL0_SFTRST, s, REG_CTRL0);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001184
1185 for (i = 0; i < 10000; i++) {
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001186 reg = mxs_read(s, REG_CTRL0);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001187 if (!(reg & AUART_CTRL0_SFTRST))
1188 break;
1189 udelay(3);
1190 }
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001191 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001192}
1193
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001194static void mxs_auart_reset_assert(struct mxs_auart_port *s)
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001195{
1196 int i;
1197 u32 reg;
1198
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001199 reg = mxs_read(s, REG_CTRL0);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001200 /* if already in reset state, keep it untouched */
1201 if (reg & AUART_CTRL0_SFTRST)
1202 return;
1203
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001204 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1205 mxs_set(AUART_CTRL0_SFTRST, s, REG_CTRL0);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001206
1207 for (i = 0; i < 1000; i++) {
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001208 reg = mxs_read(s, REG_CTRL0);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001209 /* reset is finished when the clock is gated */
1210 if (reg & AUART_CTRL0_CLKGATE)
1211 return;
1212 udelay(10);
1213 }
1214
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001215 dev_err(s->dev, "Failed to reset the unit.");
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001216}
1217
Sascha Hauer47d37d62011-01-11 15:54:54 +01001218static int mxs_auart_startup(struct uart_port *u)
1219{
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -02001220 int ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001221 struct mxs_auart_port *s = to_auart_port(u);
1222
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -02001223 ret = clk_prepare_enable(s->clk);
1224 if (ret)
1225 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001226
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001227 if (uart_console(u)) {
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001228 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001229 } else {
1230 /* reset the unit to a well known state */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001231 mxs_auart_reset_assert(s);
1232 mxs_auart_reset_deassert(s);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001233 }
Sascha Hauer47d37d62011-01-11 15:54:54 +01001234
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001235 mxs_set(AUART_CTRL2_UARTEN, s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001236
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001237 mxs_write(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
1238 s, REG_INTR);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001239
Hector Palacios9987f762013-10-03 09:32:03 +02001240 /* Reset FIFO size (it could have changed if DMA was enabled) */
1241 u->fifosize = MXS_AUART_FIFO_SIZE;
1242
Sascha Hauer47d37d62011-01-11 15:54:54 +01001243 /*
1244 * Enable fifo so all four bytes of a DMA word are written to
1245 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
1246 */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001247 mxs_set(AUART_LINECTRL_FEN, s, REG_LINECTRL);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001248
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001249 /* get initial status of modem lines */
1250 mctrl_gpio_get(s->gpios, &s->mctrl_prev);
1251
1252 s->ms_irq_enabled = false;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001253 return 0;
1254}
1255
1256static void mxs_auart_shutdown(struct uart_port *u)
1257{
1258 struct mxs_auart_port *s = to_auart_port(u);
1259
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001260 mxs_auart_disable_ms(u);
1261
Huang Shijiee8001632012-11-16 16:03:53 +08001262 if (auart_dma_enabled(s))
1263 mxs_auart_dma_exit(s);
1264
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001265 if (uart_console(u)) {
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001266 mxs_clr(AUART_CTRL2_UARTEN, s, REG_CTRL2);
1267
1268 mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN |
1269 AUART_INTR_CTSMIEN, s, REG_INTR);
1270 mxs_set(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001271 } else {
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001272 mxs_auart_reset_assert(s);
Juergen Borleis17dc72c2015-08-07 12:47:04 +02001273 }
Huang Shijie851b7142012-09-06 22:38:40 -04001274
Shawn Guoa4813772011-12-20 14:10:29 +08001275 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001276}
1277
1278static unsigned int mxs_auart_tx_empty(struct uart_port *u)
1279{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001280 struct mxs_auart_port *s = to_auart_port(u);
1281
1282 if ((mxs_read(s, REG_STAT) &
Janusz Uzycki2b310ec2014-11-18 18:37:13 +01001283 (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001284 return TIOCSER_TEMT;
Janusz Uzycki2b310ec2014-11-18 18:37:13 +01001285
1286 return 0;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001287}
1288
1289static void mxs_auart_start_tx(struct uart_port *u)
1290{
1291 struct mxs_auart_port *s = to_auart_port(u);
1292
1293 /* enable transmitter */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001294 mxs_set(AUART_CTRL2_TXE, s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001295
1296 mxs_auart_tx_chars(s);
1297}
1298
1299static void mxs_auart_stop_tx(struct uart_port *u)
1300{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001301 struct mxs_auart_port *s = to_auart_port(u);
1302
1303 mxs_clr(AUART_CTRL2_TXE, s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001304}
1305
1306static void mxs_auart_stop_rx(struct uart_port *u)
1307{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001308 struct mxs_auart_port *s = to_auart_port(u);
1309
1310 mxs_clr(AUART_CTRL2_RXE, s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001311}
1312
1313static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
1314{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001315 struct mxs_auart_port *s = to_auart_port(u);
1316
Sascha Hauer47d37d62011-01-11 15:54:54 +01001317 if (ctl)
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001318 mxs_set(AUART_LINECTRL_BRK, s, REG_LINECTRL);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001319 else
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001320 mxs_clr(AUART_LINECTRL_BRK, s, REG_LINECTRL);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001321}
1322
Julia Lawall069a47e2016-09-01 19:51:35 +02001323static const struct uart_ops mxs_auart_ops = {
Sascha Hauer47d37d62011-01-11 15:54:54 +01001324 .tx_empty = mxs_auart_tx_empty,
1325 .start_tx = mxs_auart_start_tx,
1326 .stop_tx = mxs_auart_stop_tx,
1327 .stop_rx = mxs_auart_stop_rx,
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001328 .enable_ms = mxs_auart_enable_ms,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001329 .break_ctl = mxs_auart_break_ctl,
1330 .set_mctrl = mxs_auart_set_mctrl,
1331 .get_mctrl = mxs_auart_get_mctrl,
1332 .startup = mxs_auart_startup,
1333 .shutdown = mxs_auart_shutdown,
1334 .set_termios = mxs_auart_settermios,
Janusz Uzycki36a26272014-10-10 18:53:27 +02001335 .set_ldisc = mxs_auart_set_ldisc,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001336 .type = mxs_auart_type,
1337 .release_port = mxs_auart_release_port,
1338 .request_port = mxs_auart_request_port,
1339 .config_port = mxs_auart_config_port,
1340 .verify_port = mxs_auart_verify_port,
1341};
1342
1343static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
1344
1345#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1346static void mxs_auart_console_putchar(struct uart_port *port, int ch)
1347{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001348 struct mxs_auart_port *s = to_auart_port(port);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001349 unsigned int to = 1000;
1350
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001351 while (mxs_read(s, REG_STAT) & AUART_STAT_TXFF) {
Sascha Hauer47d37d62011-01-11 15:54:54 +01001352 if (!to--)
1353 break;
1354 udelay(1);
1355 }
1356
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001357 mxs_write(ch, s, REG_DATA);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001358}
1359
1360static void
1361auart_console_write(struct console *co, const char *str, unsigned int count)
1362{
1363 struct mxs_auart_port *s;
1364 struct uart_port *port;
1365 unsigned int old_ctrl0, old_ctrl2;
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001366 unsigned int to = 20000;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001367
Wolfram Sang4829e762013-04-19 21:12:17 +02001368 if (co->index >= MXS_AUART_PORTS || co->index < 0)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001369 return;
1370
1371 s = auart_port[co->index];
1372 port = &s->port;
1373
1374 clk_enable(s->clk);
1375
1376 /* First save the CR then disable the interrupts */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001377 old_ctrl2 = mxs_read(s, REG_CTRL2);
1378 old_ctrl0 = mxs_read(s, REG_CTRL0);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001379
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001380 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1381 mxs_set(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE, s, REG_CTRL2);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001382
1383 uart_console_write(port, str, count, mxs_auart_console_putchar);
1384
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001385 /* Finally, wait for transmitter to become empty ... */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001386 while (mxs_read(s, REG_STAT) & AUART_STAT_BUSY) {
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001387 udelay(1);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001388 if (!to--)
1389 break;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001390 }
1391
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001392 /*
1393 * ... and restore the TCR if we waited long enough for the transmitter
1394 * to be idle. This might keep the transmitter enabled although it is
1395 * unused, but that is better than to disable it while it is still
1396 * transmitting.
1397 */
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001398 if (!(mxs_read(s, REG_STAT) & AUART_STAT_BUSY)) {
1399 mxs_write(old_ctrl0, s, REG_CTRL0);
1400 mxs_write(old_ctrl2, s, REG_CTRL2);
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001401 }
Sascha Hauer47d37d62011-01-11 15:54:54 +01001402
1403 clk_disable(s->clk);
1404}
1405
1406static void __init
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001407auart_console_get_options(struct mxs_auart_port *s, int *baud,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001408 int *parity, int *bits)
1409{
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001410 struct uart_port *port = &s->port;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001411 unsigned int lcr_h, quot;
1412
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001413 if (!(mxs_read(s, REG_CTRL2) & AUART_CTRL2_UARTEN))
Sascha Hauer47d37d62011-01-11 15:54:54 +01001414 return;
1415
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001416 lcr_h = mxs_read(s, REG_LINECTRL);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001417
1418 *parity = 'n';
1419 if (lcr_h & AUART_LINECTRL_PEN) {
1420 if (lcr_h & AUART_LINECTRL_EPS)
1421 *parity = 'e';
1422 else
1423 *parity = 'o';
1424 }
1425
1426 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
1427 *bits = 7;
1428 else
1429 *bits = 8;
1430
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001431 quot = ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVINT_MASK))
1432 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
1433 quot |= ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
1434 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001435 if (quot == 0)
1436 quot = 1;
1437
1438 *baud = (port->uartclk << 2) / quot;
1439}
1440
1441static int __init
1442auart_console_setup(struct console *co, char *options)
1443{
1444 struct mxs_auart_port *s;
1445 int baud = 9600;
1446 int bits = 8;
1447 int parity = 'n';
1448 int flow = 'n';
1449 int ret;
1450
1451 /*
1452 * Check whether an invalid uart number has been specified, and
1453 * if so, search for the first available port that does have
1454 * console support.
1455 */
1456 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
1457 co->index = 0;
1458 s = auart_port[co->index];
1459 if (!s)
1460 return -ENODEV;
1461
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -02001462 ret = clk_prepare_enable(s->clk);
1463 if (ret)
1464 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001465
1466 if (options)
1467 uart_parse_options(options, &baud, &parity, &bits, &flow);
1468 else
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001469 auart_console_get_options(s, &baud, &parity, &bits);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001470
1471 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
1472
Shawn Guoa4813772011-12-20 14:10:29 +08001473 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001474
1475 return ret;
1476}
1477
1478static struct console auart_console = {
1479 .name = "ttyAPP",
1480 .write = auart_console_write,
1481 .device = uart_console_device,
1482 .setup = auart_console_setup,
1483 .flags = CON_PRINTBUFFER,
1484 .index = -1,
1485 .data = &auart_driver,
1486};
1487#endif
1488
1489static struct uart_driver auart_driver = {
1490 .owner = THIS_MODULE,
1491 .driver_name = "ttyAPP",
1492 .dev_name = "ttyAPP",
1493 .major = 0,
1494 .minor = 0,
1495 .nr = MXS_AUART_PORTS,
1496#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1497 .cons = &auart_console,
1498#endif
1499};
1500
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001501static void mxs_init_regs(struct mxs_auart_port *s)
1502{
1503 if (is_asm9260_auart(s))
1504 s->vendor = &vendor_alphascale_asm9260;
1505 else
1506 s->vendor = &vendor_freescale_stmp37xx;
1507}
1508
1509static int mxs_get_clks(struct mxs_auart_port *s,
1510 struct platform_device *pdev)
1511{
1512 int err;
1513
1514 if (!is_asm9260_auart(s)) {
1515 s->clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjun0d2665b2016-09-10 12:22:17 +00001516 return PTR_ERR_OR_ZERO(s->clk);
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001517 }
1518
1519 s->clk = devm_clk_get(s->dev, "mod");
1520 if (IS_ERR(s->clk)) {
1521 dev_err(s->dev, "Failed to get \"mod\" clk\n");
1522 return PTR_ERR(s->clk);
1523 }
1524
1525 s->clk_ahb = devm_clk_get(s->dev, "ahb");
1526 if (IS_ERR(s->clk_ahb)) {
1527 dev_err(s->dev, "Failed to get \"ahb\" clk\n");
1528 return PTR_ERR(s->clk_ahb);
1529 }
1530
1531 err = clk_prepare_enable(s->clk_ahb);
1532 if (err) {
1533 dev_err(s->dev, "Failed to enable ahb_clk!\n");
1534 return err;
1535 }
1536
1537 err = clk_set_rate(s->clk, clk_get_rate(s->clk_ahb));
1538 if (err) {
1539 dev_err(s->dev, "Failed to set rate!\n");
Wei Yongjun1664bc42016-09-17 01:13:46 +00001540 goto disable_clk_ahb;
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001541 }
1542
1543 err = clk_prepare_enable(s->clk);
1544 if (err) {
1545 dev_err(s->dev, "Failed to enable clk!\n");
Fabio Estevam5d7519d2016-09-09 08:31:33 -03001546 goto disable_clk_ahb;
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001547 }
1548
1549 return 0;
Fabio Estevam5d7519d2016-09-09 08:31:33 -03001550
1551disable_clk_ahb:
1552 clk_disable_unprepare(s->clk_ahb);
1553 return err;
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001554}
1555
Fabio Estevam1ea66072012-06-18 10:06:09 -03001556/*
1557 * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
1558 * could successfully get all information from dt or a negative errno.
1559 */
1560static int serial_mxs_probe_dt(struct mxs_auart_port *s,
1561 struct platform_device *pdev)
1562{
1563 struct device_node *np = pdev->dev.of_node;
1564 int ret;
1565
1566 if (!np)
1567 /* no device tree device */
1568 return 1;
1569
1570 ret = of_alias_get_id(np, "serial");
1571 if (ret < 0) {
1572 dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
1573 return ret;
1574 }
1575 s->port.line = ret;
1576
Geert Uytterhoeven182cdcb2016-04-22 17:22:22 +02001577 if (of_get_property(np, "uart-has-rtscts", NULL) ||
1578 of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
Huang Shijie8418e672013-08-03 10:09:14 -04001579 set_bit(MXS_AUART_RTSCTS, &s->flags);
1580
Fabio Estevam1ea66072012-06-18 10:06:09 -03001581 return 0;
1582}
1583
Uwe Kleine-König343fda92015-02-12 15:24:40 +01001584static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001585{
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001586 enum mctrl_gpio_idx i;
1587 struct gpio_desc *gpiod;
1588
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +02001589 s->gpios = mctrl_gpio_init_noauto(dev, 0);
Uwe Kleine-König343fda92015-02-12 15:24:40 +01001590 if (IS_ERR(s->gpios))
1591 return PTR_ERR(s->gpios);
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001592
1593 /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
1594 if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
1595 if (test_bit(MXS_AUART_RTSCTS, &s->flags))
1596 dev_warn(dev,
1597 "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
1598 clear_bit(MXS_AUART_RTSCTS, &s->flags);
1599 }
1600
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001601 for (i = 0; i < UART_GPIO_MAX; i++) {
1602 gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
1603 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
1604 s->gpio_irq[i] = gpiod_to_irq(gpiod);
1605 else
1606 s->gpio_irq[i] = -EINVAL;
1607 }
1608
Uwe Kleine-König343fda92015-02-12 15:24:40 +01001609 return 0;
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001610}
1611
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001612static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
1613{
1614 enum mctrl_gpio_idx i;
1615
1616 for (i = 0; i < UART_GPIO_MAX; i++)
1617 if (s->gpio_irq[i] >= 0)
1618 free_irq(s->gpio_irq[i], s);
1619}
1620
1621static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
1622{
1623 int *irq = s->gpio_irq;
1624 enum mctrl_gpio_idx i;
1625 int err = 0;
1626
1627 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1628 if (irq[i] < 0)
1629 continue;
1630
1631 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1632 err = request_irq(irq[i], mxs_auart_irq_handle,
1633 IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
1634 if (err)
1635 dev_err(s->dev, "%s - Can't get %d irq\n",
1636 __func__, irq[i]);
1637 }
1638
1639 /*
1640 * If something went wrong, rollback.
1641 */
1642 while (err && (--i >= 0))
1643 if (irq[i] >= 0)
1644 free_irq(irq[i], s);
1645
1646 return err;
1647}
1648
Bill Pemberton9671f092012-11-19 13:21:50 -05001649static int mxs_auart_probe(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001650{
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001651 const struct of_device_id *of_id =
1652 of_match_device(mxs_auart_dt_ids, &pdev->dev);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001653 struct mxs_auart_port *s;
1654 u32 version;
Fabio Estevam5f9ba5b2015-01-14 14:39:09 -02001655 int ret, irq;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001656 struct resource *r;
1657
Fabio Estevam46778bc2014-11-27 17:08:31 -02001658 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
Fabio Estevam11387b02014-11-27 17:08:30 -02001659 if (!s)
1660 return -ENOMEM;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001661
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001662 s->port.dev = &pdev->dev;
1663 s->dev = &pdev->dev;
1664
Fabio Estevam1ea66072012-06-18 10:06:09 -03001665 ret = serial_mxs_probe_dt(s, pdev);
1666 if (ret > 0)
1667 s->port.line = pdev->id < 0 ? 0 : pdev->id;
1668 else if (ret < 0)
Fabio Estevam46778bc2014-11-27 17:08:31 -02001669 return ret;
Fabio Estevam1ea66072012-06-18 10:06:09 -03001670
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001671 if (of_id) {
1672 pdev->id_entry = of_id->data;
1673 s->devtype = pdev->id_entry->driver_data;
1674 }
1675
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001676 ret = mxs_get_clks(s, pdev);
1677 if (ret)
1678 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001679
1680 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam75beb262014-11-27 17:08:32 -02001681 if (!r)
1682 return -ENXIO;
1683
Sascha Hauer47d37d62011-01-11 15:54:54 +01001684 s->port.mapbase = r->start;
1685 s->port.membase = ioremap(r->start, resource_size(r));
1686 s->port.ops = &mxs_auart_ops;
1687 s->port.iotype = UPIO_MEM;
Hector Palacios9987f762013-10-03 09:32:03 +02001688 s->port.fifosize = MXS_AUART_FIFO_SIZE;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001689 s->port.uartclk = clk_get_rate(s->clk);
1690 s->port.type = PORT_IMX;
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001691
1692 mxs_init_regs(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001693
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001694 s->mctrl_prev = 0;
1695
Fabio Estevam6960cd42015-01-14 14:39:07 -02001696 irq = platform_get_irq(pdev, 0);
Fabio Estevam99c932c2015-01-14 14:39:08 -02001697 if (irq < 0)
1698 return irq;
1699
Fabio Estevam6960cd42015-01-14 14:39:07 -02001700 s->port.irq = irq;
1701 ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
Fabio Estevam9e5df9f2014-11-27 17:08:33 -02001702 dev_name(&pdev->dev), s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001703 if (ret)
Fabio Estevam75beb262014-11-27 17:08:32 -02001704 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001705
1706 platform_set_drvdata(pdev, s);
1707
Uwe Kleine-König343fda92015-02-12 15:24:40 +01001708 ret = mxs_auart_init_gpios(s, &pdev->dev);
1709 if (ret) {
1710 dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
Fabio Estevamac25e8c2015-03-10 11:24:24 -03001711 return ret;
Uwe Kleine-König343fda92015-02-12 15:24:40 +01001712 }
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001713
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001714 /*
1715 * Get the GPIO lines IRQ
1716 */
1717 ret = mxs_auart_request_gpio_irq(s);
1718 if (ret)
Fabio Estevam9e5df9f2014-11-27 17:08:33 -02001719 return ret;
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001720
Fabio Estevam1ea66072012-06-18 10:06:09 -03001721 auart_port[s->port.line] = s;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001722
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001723 mxs_auart_reset_deassert(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001724
1725 ret = uart_add_one_port(&auart_driver, &s->port);
1726 if (ret)
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001727 goto out_free_gpio_irq;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001728
Oleksij Rempel254da0d2016-03-16 14:05:52 +01001729 /* ASM9260 don't have version reg */
1730 if (is_asm9260_auart(s)) {
1731 dev_info(&pdev->dev, "Found APPUART ASM9260\n");
1732 } else {
1733 version = mxs_read(s, REG_VERSION);
1734 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
1735 (version >> 24) & 0xff,
1736 (version >> 16) & 0xff, version & 0xffff);
1737 }
Sascha Hauer47d37d62011-01-11 15:54:54 +01001738
1739 return 0;
1740
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001741out_free_gpio_irq:
1742 mxs_auart_free_gpio_irq(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001743 auart_port[pdev->id] = NULL;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001744 return ret;
1745}
1746
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001747static int mxs_auart_remove(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001748{
1749 struct mxs_auart_port *s = platform_get_drvdata(pdev);
1750
1751 uart_remove_one_port(&auart_driver, &s->port);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001752 auart_port[pdev->id] = NULL;
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001753 mxs_auart_free_gpio_irq(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001754
1755 return 0;
1756}
1757
1758static struct platform_driver mxs_auart_driver = {
1759 .probe = mxs_auart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001760 .remove = mxs_auart_remove,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001761 .driver = {
1762 .name = "mxs-auart",
Fabio Estevam1ea66072012-06-18 10:06:09 -03001763 .of_match_table = mxs_auart_dt_ids,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001764 },
1765};
1766
1767static int __init mxs_auart_init(void)
1768{
1769 int r;
1770
1771 r = uart_register_driver(&auart_driver);
1772 if (r)
1773 goto out;
1774
1775 r = platform_driver_register(&mxs_auart_driver);
1776 if (r)
1777 goto out_err;
1778
1779 return 0;
1780out_err:
1781 uart_unregister_driver(&auart_driver);
1782out:
1783 return r;
1784}
1785
1786static void __exit mxs_auart_exit(void)
1787{
1788 platform_driver_unregister(&mxs_auart_driver);
1789 uart_unregister_driver(&auart_driver);
1790}
1791
1792module_init(mxs_auart_init);
1793module_exit(mxs_auart_exit);
1794MODULE_LICENSE("GPL");
1795MODULE_DESCRIPTION("Freescale MXS application uart driver");
Fabio Estevam1ea66072012-06-18 10:06:09 -03001796MODULE_ALIAS("platform:mxs-auart");