blob: a53899c47e600083c93df1ab97e53cfc9911741f [file] [log] [blame]
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301/*
2 * serial_tegra.c
3 *
4 * High-speed serial driver for NVIDIA Tegra SoCs
5 *
6 * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
7 *
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/clk.h>
24#include <linux/debugfs.h>
25#include <linux/delay.h>
26#include <linux/dmaengine.h>
27#include <linux/dma-mapping.h>
28#include <linux/dmapool.h>
Sachin Kamat84e81922013-03-04 09:59:00 +053029#include <linux/err.h>
Laxman Dewangane9ea0962013-01-08 16:27:44 +053030#include <linux/io.h>
31#include <linux/irq.h>
32#include <linux/module.h>
33#include <linux/of.h>
34#include <linux/of_device.h>
35#include <linux/pagemap.h>
36#include <linux/platform_device.h>
Stephen Warrend3d654e2013-11-06 16:50:44 -070037#include <linux/reset.h>
Laxman Dewangane9ea0962013-01-08 16:27:44 +053038#include <linux/serial.h>
39#include <linux/serial_8250.h>
40#include <linux/serial_core.h>
41#include <linux/serial_reg.h>
42#include <linux/slab.h>
43#include <linux/string.h>
44#include <linux/termios.h>
45#include <linux/tty.h>
46#include <linux/tty_flip.h>
47
Laxman Dewangane9ea0962013-01-08 16:27:44 +053048#define TEGRA_UART_TYPE "TEGRA_UART"
49#define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
50#define BYTES_TO_ALIGN(x) ((unsigned long)(x) & 0x3)
51
52#define TEGRA_UART_RX_DMA_BUFFER_SIZE 4096
53#define TEGRA_UART_LSR_TXFIFO_FULL 0x100
54#define TEGRA_UART_IER_EORD 0x20
55#define TEGRA_UART_MCR_RTS_EN 0x40
56#define TEGRA_UART_MCR_CTS_EN 0x20
57#define TEGRA_UART_LSR_ANY (UART_LSR_OE | UART_LSR_BI | \
58 UART_LSR_PE | UART_LSR_FE)
59#define TEGRA_UART_IRDA_CSR 0x08
60#define TEGRA_UART_SIR_ENABLED 0x80
61
62#define TEGRA_UART_TX_PIO 1
63#define TEGRA_UART_TX_DMA 2
64#define TEGRA_UART_MIN_DMA 16
65#define TEGRA_UART_FIFO_SIZE 32
66
67/*
68 * Tx fifo trigger level setting in tegra uart is in
69 * reverse way then conventional uart.
70 */
71#define TEGRA_UART_TX_TRIG_16B 0x00
72#define TEGRA_UART_TX_TRIG_8B 0x10
73#define TEGRA_UART_TX_TRIG_4B 0x20
74#define TEGRA_UART_TX_TRIG_1B 0x30
75
76#define TEGRA_UART_MAXIMUM 5
77
78/* Default UART setting when started: 115200 no parity, stop, 8 data bits */
79#define TEGRA_UART_DEFAULT_BAUD 115200
80#define TEGRA_UART_DEFAULT_LSR UART_LCR_WLEN8
81
82/* Tx transfer mode */
83#define TEGRA_TX_PIO 1
84#define TEGRA_TX_DMA 2
85
86/**
87 * tegra_uart_chip_data: SOC specific data.
88 *
89 * @tx_fifo_full_status: Status flag available for checking tx fifo full.
90 * @allow_txfifo_reset_fifo_mode: allow_tx fifo reset with fifo mode or not.
91 * Tegra30 does not allow this.
92 * @support_clk_src_div: Clock source support the clock divider.
93 */
94struct tegra_uart_chip_data {
95 bool tx_fifo_full_status;
96 bool allow_txfifo_reset_fifo_mode;
97 bool support_clk_src_div;
98};
99
100struct tegra_uart_port {
101 struct uart_port uport;
102 const struct tegra_uart_chip_data *cdata;
103
104 struct clk *uart_clk;
Stephen Warrend3d654e2013-11-06 16:50:44 -0700105 struct reset_control *rst;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530106 unsigned int current_baud;
107
108 /* Register shadow */
109 unsigned long fcr_shadow;
110 unsigned long mcr_shadow;
111 unsigned long lcr_shadow;
112 unsigned long ier_shadow;
113 bool rts_active;
114
115 int tx_in_progress;
116 unsigned int tx_bytes;
117
118 bool enable_modem_interrupt;
119
120 bool rx_timeout;
121 int rx_in_progress;
122 int symb_bit;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530123
124 struct dma_chan *rx_dma_chan;
125 struct dma_chan *tx_dma_chan;
126 dma_addr_t rx_dma_buf_phys;
127 dma_addr_t tx_dma_buf_phys;
128 unsigned char *rx_dma_buf_virt;
129 unsigned char *tx_dma_buf_virt;
130 struct dma_async_tx_descriptor *tx_dma_desc;
131 struct dma_async_tx_descriptor *rx_dma_desc;
132 dma_cookie_t tx_cookie;
133 dma_cookie_t rx_cookie;
134 int tx_bytes_requested;
135 int rx_bytes_requested;
136};
137
138static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
139static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
140
141static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
142 unsigned long reg)
143{
144 return readl(tup->uport.membase + (reg << tup->uport.regshift));
145}
146
147static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
148 unsigned long reg)
149{
150 writel(val, tup->uport.membase + (reg << tup->uport.regshift));
151}
152
153static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
154{
155 return container_of(u, struct tegra_uart_port, uport);
156}
157
158static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
159{
160 struct tegra_uart_port *tup = to_tegra_uport(u);
161
162 /*
163 * RI - Ring detector is active
164 * CD/DCD/CAR - Carrier detect is always active. For some reason
165 * linux has different names for carrier detect.
166 * DSR - Data Set ready is active as the hardware doesn't support it.
167 * Don't know if the linux support this yet?
168 * CTS - Clear to send. Always set to active, as the hardware handles
169 * CTS automatically.
170 */
171 if (tup->enable_modem_interrupt)
172 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
173 return TIOCM_CTS;
174}
175
176static void set_rts(struct tegra_uart_port *tup, bool active)
177{
178 unsigned long mcr;
179
180 mcr = tup->mcr_shadow;
181 if (active)
182 mcr |= TEGRA_UART_MCR_RTS_EN;
183 else
184 mcr &= ~TEGRA_UART_MCR_RTS_EN;
185 if (mcr != tup->mcr_shadow) {
186 tegra_uart_write(tup, mcr, UART_MCR);
187 tup->mcr_shadow = mcr;
188 }
189 return;
190}
191
192static void set_dtr(struct tegra_uart_port *tup, bool active)
193{
194 unsigned long mcr;
195
196 mcr = tup->mcr_shadow;
197 if (active)
198 mcr |= UART_MCR_DTR;
199 else
200 mcr &= ~UART_MCR_DTR;
201 if (mcr != tup->mcr_shadow) {
202 tegra_uart_write(tup, mcr, UART_MCR);
203 tup->mcr_shadow = mcr;
204 }
205 return;
206}
207
208static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
209{
210 struct tegra_uart_port *tup = to_tegra_uport(u);
211 unsigned long mcr;
212 int dtr_enable;
213
214 mcr = tup->mcr_shadow;
215 tup->rts_active = !!(mctrl & TIOCM_RTS);
216 set_rts(tup, tup->rts_active);
217
218 dtr_enable = !!(mctrl & TIOCM_DTR);
219 set_dtr(tup, dtr_enable);
220 return;
221}
222
223static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
224{
225 struct tegra_uart_port *tup = to_tegra_uport(u);
226 unsigned long lcr;
227
228 lcr = tup->lcr_shadow;
229 if (break_ctl)
230 lcr |= UART_LCR_SBC;
231 else
232 lcr &= ~UART_LCR_SBC;
233 tegra_uart_write(tup, lcr, UART_LCR);
234 tup->lcr_shadow = lcr;
235}
236
Jon Hunter245c0272015-05-05 15:17:52 +0100237/**
238 * tegra_uart_wait_cycle_time: Wait for N UART clock periods
239 *
240 * @tup: Tegra serial port data structure.
241 * @cycles: Number of clock periods to wait.
242 *
243 * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
244 * clock speed is 16X the current baud rate.
245 */
246static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
247 unsigned int cycles)
248{
249 if (tup->current_baud)
250 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
251}
252
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530253/* Wait for a symbol-time. */
254static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
255 unsigned int syms)
256{
257 if (tup->current_baud)
258 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
259 tup->current_baud));
260}
261
262static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
263{
264 unsigned long fcr = tup->fcr_shadow;
265
266 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
267 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
268 tegra_uart_write(tup, fcr, UART_FCR);
269 } else {
270 fcr &= ~UART_FCR_ENABLE_FIFO;
271 tegra_uart_write(tup, fcr, UART_FCR);
272 udelay(60);
273 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
274 tegra_uart_write(tup, fcr, UART_FCR);
275 fcr |= UART_FCR_ENABLE_FIFO;
276 tegra_uart_write(tup, fcr, UART_FCR);
277 }
278
279 /* Dummy read to ensure the write is posted */
280 tegra_uart_read(tup, UART_SCR);
281
Jon Hunter245c0272015-05-05 15:17:52 +0100282 /*
283 * For all tegra devices (up to t210), there is a hardware issue that
284 * requires software to wait for 32 UART clock periods for the flush
285 * to propagate, otherwise data could be lost.
286 */
287 tegra_uart_wait_cycle_time(tup, 32);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530288}
289
290static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
291{
292 unsigned long rate;
293 unsigned int divisor;
294 unsigned long lcr;
295 int ret;
296
297 if (tup->current_baud == baud)
298 return 0;
299
300 if (tup->cdata->support_clk_src_div) {
301 rate = baud * 16;
302 ret = clk_set_rate(tup->uart_clk, rate);
303 if (ret < 0) {
304 dev_err(tup->uport.dev,
305 "clk_set_rate() failed for rate %lu\n", rate);
306 return ret;
307 }
308 divisor = 1;
309 } else {
310 rate = clk_get_rate(tup->uart_clk);
311 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
312 }
313
314 lcr = tup->lcr_shadow;
315 lcr |= UART_LCR_DLAB;
316 tegra_uart_write(tup, lcr, UART_LCR);
317
318 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
319 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
320
321 lcr &= ~UART_LCR_DLAB;
322 tegra_uart_write(tup, lcr, UART_LCR);
323
324 /* Dummy read to ensure the write is posted */
325 tegra_uart_read(tup, UART_SCR);
326
327 tup->current_baud = baud;
328
329 /* wait two character intervals at new rate */
330 tegra_uart_wait_sym_time(tup, 2);
331 return 0;
332}
333
334static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
335 unsigned long lsr)
336{
337 char flag = TTY_NORMAL;
338
339 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
340 if (lsr & UART_LSR_OE) {
341 /* Overrrun error */
Johan Hovoldf0c1e462014-11-18 11:18:02 +0100342 flag = TTY_OVERRUN;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530343 tup->uport.icount.overrun++;
344 dev_err(tup->uport.dev, "Got overrun errors\n");
345 } else if (lsr & UART_LSR_PE) {
346 /* Parity error */
Johan Hovoldf0c1e462014-11-18 11:18:02 +0100347 flag = TTY_PARITY;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530348 tup->uport.icount.parity++;
349 dev_err(tup->uport.dev, "Got Parity errors\n");
350 } else if (lsr & UART_LSR_FE) {
Johan Hovoldf0c1e462014-11-18 11:18:02 +0100351 flag = TTY_FRAME;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530352 tup->uport.icount.frame++;
353 dev_err(tup->uport.dev, "Got frame errors\n");
354 } else if (lsr & UART_LSR_BI) {
355 dev_err(tup->uport.dev, "Got Break\n");
356 tup->uport.icount.brk++;
357 /* If FIFO read error without any data, reset Rx FIFO */
358 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
359 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
360 }
361 }
362 return flag;
363}
364
365static int tegra_uart_request_port(struct uart_port *u)
366{
367 return 0;
368}
369
370static void tegra_uart_release_port(struct uart_port *u)
371{
372 /* Nothing to do here */
373}
374
375static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
376{
377 struct circ_buf *xmit = &tup->uport.state->xmit;
378 int i;
379
380 for (i = 0; i < max_bytes; i++) {
381 BUG_ON(uart_circ_empty(xmit));
382 if (tup->cdata->tx_fifo_full_status) {
383 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
384 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
385 break;
386 }
387 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
388 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
389 tup->uport.icount.tx++;
390 }
391}
392
393static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
394 unsigned int bytes)
395{
396 if (bytes > TEGRA_UART_MIN_DMA)
397 bytes = TEGRA_UART_MIN_DMA;
398
399 tup->tx_in_progress = TEGRA_UART_TX_PIO;
400 tup->tx_bytes = bytes;
401 tup->ier_shadow |= UART_IER_THRI;
402 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
403}
404
405static void tegra_uart_tx_dma_complete(void *args)
406{
407 struct tegra_uart_port *tup = args;
408 struct circ_buf *xmit = &tup->uport.state->xmit;
409 struct dma_tx_state state;
410 unsigned long flags;
411 int count;
412
413 dmaengine_tx_status(tup->tx_dma_chan, tup->rx_cookie, &state);
414 count = tup->tx_bytes_requested - state.residue;
415 async_tx_ack(tup->tx_dma_desc);
416 spin_lock_irqsave(&tup->uport.lock, flags);
417 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
418 tup->tx_in_progress = 0;
419 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
420 uart_write_wakeup(&tup->uport);
421 tegra_uart_start_next_tx(tup);
422 spin_unlock_irqrestore(&tup->uport.lock, flags);
423}
424
425static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
426 unsigned long count)
427{
428 struct circ_buf *xmit = &tup->uport.state->xmit;
429 dma_addr_t tx_phys_addr;
430
431 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
432 UART_XMIT_SIZE, DMA_TO_DEVICE);
433
434 tup->tx_bytes = count & ~(0xF);
435 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
436 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
437 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
438 DMA_PREP_INTERRUPT);
439 if (!tup->tx_dma_desc) {
440 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
441 return -EIO;
442 }
443
444 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
445 tup->tx_dma_desc->callback_param = tup;
446 tup->tx_in_progress = TEGRA_UART_TX_DMA;
447 tup->tx_bytes_requested = tup->tx_bytes;
448 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
449 dma_async_issue_pending(tup->tx_dma_chan);
450 return 0;
451}
452
453static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
454{
455 unsigned long tail;
456 unsigned long count;
457 struct circ_buf *xmit = &tup->uport.state->xmit;
458
459 tail = (unsigned long)&xmit->buf[xmit->tail];
460 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
461 if (!count)
462 return;
463
464 if (count < TEGRA_UART_MIN_DMA)
465 tegra_uart_start_pio_tx(tup, count);
466 else if (BYTES_TO_ALIGN(tail) > 0)
467 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
468 else
469 tegra_uart_start_tx_dma(tup, count);
470}
471
472/* Called by serial core driver with u->lock taken. */
473static void tegra_uart_start_tx(struct uart_port *u)
474{
475 struct tegra_uart_port *tup = to_tegra_uport(u);
476 struct circ_buf *xmit = &u->state->xmit;
477
478 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
479 tegra_uart_start_next_tx(tup);
480}
481
482static unsigned int tegra_uart_tx_empty(struct uart_port *u)
483{
484 struct tegra_uart_port *tup = to_tegra_uport(u);
485 unsigned int ret = 0;
486 unsigned long flags;
487
488 spin_lock_irqsave(&u->lock, flags);
489 if (!tup->tx_in_progress) {
490 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
491 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
492 ret = TIOCSER_TEMT;
493 }
494 spin_unlock_irqrestore(&u->lock, flags);
495 return ret;
496}
497
498static void tegra_uart_stop_tx(struct uart_port *u)
499{
500 struct tegra_uart_port *tup = to_tegra_uport(u);
501 struct circ_buf *xmit = &tup->uport.state->xmit;
502 struct dma_tx_state state;
503 int count;
504
Pradeep Goudagunta5e3dbfc2014-06-06 16:48:08 +0530505 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
506 return;
507
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530508 dmaengine_terminate_all(tup->tx_dma_chan);
509 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
510 count = tup->tx_bytes_requested - state.residue;
511 async_tx_ack(tup->tx_dma_desc);
512 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
513 tup->tx_in_progress = 0;
514 return;
515}
516
517static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
518{
519 struct circ_buf *xmit = &tup->uport.state->xmit;
520
521 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
522 tup->tx_in_progress = 0;
523 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
524 uart_write_wakeup(&tup->uport);
525 tegra_uart_start_next_tx(tup);
526 return;
527}
528
529static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
Thierry Reding962963e2013-01-17 14:31:45 +0100530 struct tty_port *tty)
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530531{
532 do {
533 char flag = TTY_NORMAL;
534 unsigned long lsr = 0;
535 unsigned char ch;
536
537 lsr = tegra_uart_read(tup, UART_LSR);
538 if (!(lsr & UART_LSR_DR))
539 break;
540
541 flag = tegra_uart_decode_rx_error(tup, lsr);
542 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
543 tup->uport.icount.rx++;
544
545 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
546 tty_insert_flip_char(tty, ch, flag);
547 } while (1);
548
549 return;
550}
551
552static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
Thierry Reding962963e2013-01-17 14:31:45 +0100553 struct tty_port *tty, int count)
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530554{
555 int copied;
556
Shardar Shariff Mddb8e7842015-05-05 15:17:54 +0100557 /* If count is zero, then there is no data to be copied */
558 if (!count)
559 return;
560
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530561 tup->uport.icount.rx += count;
562 if (!tty) {
563 dev_err(tup->uport.dev, "No tty port\n");
564 return;
565 }
566 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
567 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
568 copied = tty_insert_flip_string(tty,
569 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
570 if (copied != count) {
571 WARN_ON(1);
572 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
573 }
574 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
575 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
576}
577
578static void tegra_uart_rx_dma_complete(void *args)
579{
580 struct tegra_uart_port *tup = args;
581 struct uart_port *u = &tup->uport;
582 int count = tup->rx_bytes_requested;
583 struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
Thierry Reding962963e2013-01-17 14:31:45 +0100584 struct tty_port *port = &u->state->port;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530585 unsigned long flags;
586
587 async_tx_ack(tup->rx_dma_desc);
588 spin_lock_irqsave(&u->lock, flags);
589
590 /* Deactivate flow control to stop sender */
591 if (tup->rts_active)
592 set_rts(tup, false);
593
594 /* If we are here, DMA is stopped */
Shardar Shariff Mddb8e7842015-05-05 15:17:54 +0100595 tegra_uart_copy_rx_to_tty(tup, port, count);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530596
Thierry Reding962963e2013-01-17 14:31:45 +0100597 tegra_uart_handle_rx_pio(tup, port);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530598 if (tty) {
Viresh Kumar9b887482013-08-19 20:14:27 +0530599 spin_unlock_irqrestore(&u->lock, flags);
Thierry Reding962963e2013-01-17 14:31:45 +0100600 tty_flip_buffer_push(port);
Viresh Kumar9b887482013-08-19 20:14:27 +0530601 spin_lock_irqsave(&u->lock, flags);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530602 tty_kref_put(tty);
603 }
604 tegra_uart_start_rx_dma(tup);
605
606 /* Activate flow control to start transfer */
607 if (tup->rts_active)
608 set_rts(tup, true);
609
610 spin_unlock_irqrestore(&u->lock, flags);
611}
612
Viresh Kumar9b887482013-08-19 20:14:27 +0530613static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup,
614 unsigned long *flags)
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530615{
616 struct dma_tx_state state;
617 struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
Thierry Reding962963e2013-01-17 14:31:45 +0100618 struct tty_port *port = &tup->uport.state->port;
Viresh Kumar9b887482013-08-19 20:14:27 +0530619 struct uart_port *u = &tup->uport;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530620 int count;
621
622 /* Deactivate flow control to stop sender */
623 if (tup->rts_active)
624 set_rts(tup, false);
625
626 dmaengine_terminate_all(tup->rx_dma_chan);
627 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
Pradeep Goudaguntab31245b2014-06-06 16:48:09 +0530628 async_tx_ack(tup->rx_dma_desc);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530629 count = tup->rx_bytes_requested - state.residue;
630
631 /* If we are here, DMA is stopped */
Shardar Shariff Mddb8e7842015-05-05 15:17:54 +0100632 tegra_uart_copy_rx_to_tty(tup, port, count);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530633
Thierry Reding962963e2013-01-17 14:31:45 +0100634 tegra_uart_handle_rx_pio(tup, port);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530635 if (tty) {
Viresh Kumar9b887482013-08-19 20:14:27 +0530636 spin_unlock_irqrestore(&u->lock, *flags);
Thierry Reding962963e2013-01-17 14:31:45 +0100637 tty_flip_buffer_push(port);
Viresh Kumar9b887482013-08-19 20:14:27 +0530638 spin_lock_irqsave(&u->lock, *flags);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530639 tty_kref_put(tty);
640 }
641 tegra_uart_start_rx_dma(tup);
642
643 if (tup->rts_active)
644 set_rts(tup, true);
645}
646
647static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
648{
649 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
650
651 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
652 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
653 DMA_PREP_INTERRUPT);
654 if (!tup->rx_dma_desc) {
655 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
656 return -EIO;
657 }
658
659 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
660 tup->rx_dma_desc->callback_param = tup;
661 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
662 count, DMA_TO_DEVICE);
663 tup->rx_bytes_requested = count;
664 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
665 dma_async_issue_pending(tup->rx_dma_chan);
666 return 0;
667}
668
669static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
670{
671 struct tegra_uart_port *tup = to_tegra_uport(u);
672 unsigned long msr;
673
674 msr = tegra_uart_read(tup, UART_MSR);
675 if (!(msr & UART_MSR_ANY_DELTA))
676 return;
677
678 if (msr & UART_MSR_TERI)
679 tup->uport.icount.rng++;
680 if (msr & UART_MSR_DDSR)
681 tup->uport.icount.dsr++;
682 /* We may only get DDCD when HW init and reset */
683 if (msr & UART_MSR_DDCD)
684 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
685 /* Will start/stop_tx accordingly */
686 if (msr & UART_MSR_DCTS)
687 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
688 return;
689}
690
691static irqreturn_t tegra_uart_isr(int irq, void *data)
692{
693 struct tegra_uart_port *tup = data;
694 struct uart_port *u = &tup->uport;
695 unsigned long iir;
696 unsigned long ier;
697 bool is_rx_int = false;
698 unsigned long flags;
699
700 spin_lock_irqsave(&u->lock, flags);
701 while (1) {
702 iir = tegra_uart_read(tup, UART_IIR);
703 if (iir & UART_IIR_NO_INT) {
704 if (is_rx_int) {
Viresh Kumar9b887482013-08-19 20:14:27 +0530705 tegra_uart_handle_rx_dma(tup, &flags);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530706 if (tup->rx_in_progress) {
707 ier = tup->ier_shadow;
708 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
709 TEGRA_UART_IER_EORD);
710 tup->ier_shadow = ier;
711 tegra_uart_write(tup, ier, UART_IER);
712 }
713 }
714 spin_unlock_irqrestore(&u->lock, flags);
715 return IRQ_HANDLED;
716 }
717
718 switch ((iir >> 1) & 0x7) {
719 case 0: /* Modem signal change interrupt */
720 tegra_uart_handle_modem_signal_change(u);
721 break;
722
723 case 1: /* Transmit interrupt only triggered when using PIO */
724 tup->ier_shadow &= ~UART_IER_THRI;
725 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
726 tegra_uart_handle_tx_pio(tup);
727 break;
728
729 case 4: /* End of data */
730 case 6: /* Rx timeout */
731 case 2: /* Receive */
732 if (!is_rx_int) {
733 is_rx_int = true;
734 /* Disable Rx interrupts */
735 ier = tup->ier_shadow;
736 ier |= UART_IER_RDI;
737 tegra_uart_write(tup, ier, UART_IER);
738 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
739 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
740 tup->ier_shadow = ier;
741 tegra_uart_write(tup, ier, UART_IER);
742 }
743 break;
744
745 case 3: /* Receive error */
746 tegra_uart_decode_rx_error(tup,
747 tegra_uart_read(tup, UART_LSR));
748 break;
749
750 case 5: /* break nothing to handle */
751 case 7: /* break nothing to handle */
752 break;
753 }
754 }
755}
756
757static void tegra_uart_stop_rx(struct uart_port *u)
758{
759 struct tegra_uart_port *tup = to_tegra_uport(u);
Johan Hovoldcfd29aa2013-09-10 12:50:48 +0200760 struct tty_struct *tty;
Thierry Reding962963e2013-01-17 14:31:45 +0100761 struct tty_port *port = &u->state->port;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530762 struct dma_tx_state state;
763 unsigned long ier;
764 int count;
765
766 if (tup->rts_active)
767 set_rts(tup, false);
768
769 if (!tup->rx_in_progress)
770 return;
771
Johan Hovoldcfd29aa2013-09-10 12:50:48 +0200772 tty = tty_port_tty_get(&tup->uport.state->port);
773
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530774 tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
775
776 ier = tup->ier_shadow;
777 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
778 TEGRA_UART_IER_EORD);
779 tup->ier_shadow = ier;
780 tegra_uart_write(tup, ier, UART_IER);
781 tup->rx_in_progress = 0;
782 if (tup->rx_dma_chan) {
783 dmaengine_terminate_all(tup->rx_dma_chan);
784 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
785 async_tx_ack(tup->rx_dma_desc);
786 count = tup->rx_bytes_requested - state.residue;
Thierry Reding962963e2013-01-17 14:31:45 +0100787 tegra_uart_copy_rx_to_tty(tup, port, count);
788 tegra_uart_handle_rx_pio(tup, port);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530789 } else {
Thierry Reding962963e2013-01-17 14:31:45 +0100790 tegra_uart_handle_rx_pio(tup, port);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530791 }
792 if (tty) {
Thierry Reding962963e2013-01-17 14:31:45 +0100793 tty_flip_buffer_push(port);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530794 tty_kref_put(tty);
795 }
796 return;
797}
798
799static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
800{
801 unsigned long flags;
802 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
803 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
804 unsigned long wait_time;
805 unsigned long lsr;
806 unsigned long msr;
807 unsigned long mcr;
808
809 /* Disable interrupts */
810 tegra_uart_write(tup, 0, UART_IER);
811
812 lsr = tegra_uart_read(tup, UART_LSR);
813 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
814 msr = tegra_uart_read(tup, UART_MSR);
815 mcr = tegra_uart_read(tup, UART_MCR);
816 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
817 dev_err(tup->uport.dev,
818 "Tx Fifo not empty, CTS disabled, waiting\n");
819
820 /* Wait for Tx fifo to be empty */
821 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
822 wait_time = min(fifo_empty_time, 100lu);
823 udelay(wait_time);
824 fifo_empty_time -= wait_time;
825 if (!fifo_empty_time) {
826 msr = tegra_uart_read(tup, UART_MSR);
827 mcr = tegra_uart_read(tup, UART_MCR);
828 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
829 (msr & UART_MSR_CTS))
830 dev_err(tup->uport.dev,
831 "Slave not ready\n");
832 break;
833 }
834 lsr = tegra_uart_read(tup, UART_LSR);
835 }
836 }
837
838 spin_lock_irqsave(&tup->uport.lock, flags);
839 /* Reset the Rx and Tx FIFOs */
840 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
841 tup->current_baud = 0;
842 spin_unlock_irqrestore(&tup->uport.lock, flags);
843
844 clk_disable_unprepare(tup->uart_clk);
845}
846
847static int tegra_uart_hw_init(struct tegra_uart_port *tup)
848{
849 int ret;
850
851 tup->fcr_shadow = 0;
852 tup->mcr_shadow = 0;
853 tup->lcr_shadow = 0;
854 tup->ier_shadow = 0;
855 tup->current_baud = 0;
856
857 clk_prepare_enable(tup->uart_clk);
858
859 /* Reset the UART controller to clear all previous status.*/
Stephen Warrend3d654e2013-11-06 16:50:44 -0700860 reset_control_assert(tup->rst);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530861 udelay(10);
Stephen Warrend3d654e2013-11-06 16:50:44 -0700862 reset_control_deassert(tup->rst);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530863
864 tup->rx_in_progress = 0;
865 tup->tx_in_progress = 0;
866
867 /*
868 * Set the trigger level
869 *
870 * For PIO mode:
871 *
872 * For receive, this will interrupt the CPU after that many number of
873 * bytes are received, for the remaining bytes the receive timeout
874 * interrupt is received. Rx high watermark is set to 4.
875 *
876 * For transmit, if the trasnmit interrupt is enabled, this will
877 * interrupt the CPU when the number of entries in the FIFO reaches the
878 * low watermark. Tx low watermark is set to 16 bytes.
879 *
880 * For DMA mode:
881 *
882 * Set the Tx trigger to 16. This should match the DMA burst size that
883 * programmed in the DMA registers.
884 */
885 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
886 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
887 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
888 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
889
Jon Hunter11e71002015-05-05 15:17:53 +0100890 /* Dummy read to ensure the write is posted */
891 tegra_uart_read(tup, UART_SCR);
892
893 /*
894 * For all tegra devices (up to t210), there is a hardware issue that
895 * requires software to wait for 3 UART clock periods after enabling
896 * the TX fifo, otherwise data could be lost.
897 */
898 tegra_uart_wait_cycle_time(tup, 3);
899
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530900 /*
901 * Initialize the UART with default configuration
902 * (115200, N, 8, 1) so that the receive DMA buffer may be
903 * enqueued
904 */
905 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
906 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
907 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
908 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
909
910 ret = tegra_uart_start_rx_dma(tup);
911 if (ret < 0) {
912 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
913 return ret;
914 }
915 tup->rx_in_progress = 1;
916
917 /*
918 * Enable IE_RXS for the receive status interrupts like line errros.
919 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
920 *
921 * If using DMA mode, enable EORD instead of receive interrupt which
922 * will interrupt after the UART is done with the receive instead of
923 * the interrupt when the FIFO "threshold" is reached.
924 *
925 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
926 * the DATA is sitting in the FIFO and couldn't be transferred to the
927 * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
928 * triggered when there is a pause of the incomming data stream for 4
929 * characters long.
930 *
931 * For pauses in the data which is not aligned to 4 bytes, we get
932 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
933 * then the EORD.
934 */
935 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
936 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
937 return 0;
938}
939
940static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
941 bool dma_to_memory)
942{
943 struct dma_chan *dma_chan;
944 unsigned char *dma_buf;
945 dma_addr_t dma_phys;
946 int ret;
947 struct dma_slave_config dma_sconfig;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530948
Stephen Warrenc2b329f2013-11-11 14:16:38 -0700949 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
950 dma_to_memory ? "rx" : "tx");
951 if (IS_ERR(dma_chan)) {
952 ret = PTR_ERR(dma_chan);
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530953 dev_err(tup->uport.dev,
Stephen Warrenc2b329f2013-11-11 14:16:38 -0700954 "DMA channel alloc failed: %d\n", ret);
955 return ret;
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530956 }
957
958 if (dma_to_memory) {
959 dma_buf = dma_alloc_coherent(tup->uport.dev,
960 TEGRA_UART_RX_DMA_BUFFER_SIZE,
961 &dma_phys, GFP_KERNEL);
962 if (!dma_buf) {
963 dev_err(tup->uport.dev,
964 "Not able to allocate the dma buffer\n");
965 dma_release_channel(dma_chan);
966 return -ENOMEM;
967 }
968 } else {
969 dma_phys = dma_map_single(tup->uport.dev,
970 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
971 DMA_TO_DEVICE);
972 dma_buf = tup->uport.state->xmit.buf;
973 }
974
Laxman Dewangane9ea0962013-01-08 16:27:44 +0530975 if (dma_to_memory) {
976 dma_sconfig.src_addr = tup->uport.mapbase;
977 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
978 dma_sconfig.src_maxburst = 4;
979 } else {
980 dma_sconfig.dst_addr = tup->uport.mapbase;
981 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
982 dma_sconfig.dst_maxburst = 16;
983 }
984
985 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
986 if (ret < 0) {
987 dev_err(tup->uport.dev,
988 "Dma slave config failed, err = %d\n", ret);
989 goto scrub;
990 }
991
992 if (dma_to_memory) {
993 tup->rx_dma_chan = dma_chan;
994 tup->rx_dma_buf_virt = dma_buf;
995 tup->rx_dma_buf_phys = dma_phys;
996 } else {
997 tup->tx_dma_chan = dma_chan;
998 tup->tx_dma_buf_virt = dma_buf;
999 tup->tx_dma_buf_phys = dma_phys;
1000 }
1001 return 0;
1002
1003scrub:
1004 dma_release_channel(dma_chan);
1005 return ret;
1006}
1007
1008static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
1009 bool dma_to_memory)
1010{
1011 struct dma_chan *dma_chan;
1012
1013 if (dma_to_memory) {
1014 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
1015 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
1016 dma_chan = tup->rx_dma_chan;
1017 tup->rx_dma_chan = NULL;
1018 tup->rx_dma_buf_phys = 0;
1019 tup->rx_dma_buf_virt = NULL;
1020 } else {
1021 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
1022 UART_XMIT_SIZE, DMA_TO_DEVICE);
1023 dma_chan = tup->tx_dma_chan;
1024 tup->tx_dma_chan = NULL;
1025 tup->tx_dma_buf_phys = 0;
1026 tup->tx_dma_buf_virt = NULL;
1027 }
1028 dma_release_channel(dma_chan);
1029}
1030
1031static int tegra_uart_startup(struct uart_port *u)
1032{
1033 struct tegra_uart_port *tup = to_tegra_uport(u);
1034 int ret;
1035
1036 ret = tegra_uart_dma_channel_allocate(tup, false);
1037 if (ret < 0) {
1038 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1039 return ret;
1040 }
1041
1042 ret = tegra_uart_dma_channel_allocate(tup, true);
1043 if (ret < 0) {
1044 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1045 goto fail_rx_dma;
1046 }
1047
1048 ret = tegra_uart_hw_init(tup);
1049 if (ret < 0) {
1050 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1051 goto fail_hw_init;
1052 }
1053
Michael Opdenackercf81e052013-10-06 08:30:17 +02001054 ret = request_irq(u->irq, tegra_uart_isr, 0,
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301055 dev_name(u->dev), tup);
1056 if (ret < 0) {
1057 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1058 goto fail_hw_init;
1059 }
1060 return 0;
1061
1062fail_hw_init:
1063 tegra_uart_dma_channel_free(tup, true);
1064fail_rx_dma:
1065 tegra_uart_dma_channel_free(tup, false);
1066 return ret;
1067}
1068
Peter Hurley479e9b92014-10-16 16:54:18 -04001069/*
1070 * Flush any TX data submitted for DMA and PIO. Called when the
1071 * TX circular buffer is reset.
1072 */
1073static void tegra_uart_flush_buffer(struct uart_port *u)
1074{
1075 struct tegra_uart_port *tup = to_tegra_uport(u);
1076
1077 tup->tx_bytes = 0;
1078 if (tup->tx_dma_chan)
1079 dmaengine_terminate_all(tup->tx_dma_chan);
1080 return;
1081}
1082
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301083static void tegra_uart_shutdown(struct uart_port *u)
1084{
1085 struct tegra_uart_port *tup = to_tegra_uport(u);
1086
1087 tegra_uart_hw_deinit(tup);
1088
1089 tup->rx_in_progress = 0;
1090 tup->tx_in_progress = 0;
1091
1092 tegra_uart_dma_channel_free(tup, true);
1093 tegra_uart_dma_channel_free(tup, false);
1094 free_irq(u->irq, tup);
Peter Hurley479e9b92014-10-16 16:54:18 -04001095
1096 tegra_uart_flush_buffer(u);
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301097}
1098
1099static void tegra_uart_enable_ms(struct uart_port *u)
1100{
1101 struct tegra_uart_port *tup = to_tegra_uport(u);
1102
1103 if (tup->enable_modem_interrupt) {
1104 tup->ier_shadow |= UART_IER_MSI;
1105 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1106 }
1107}
1108
1109static void tegra_uart_set_termios(struct uart_port *u,
1110 struct ktermios *termios, struct ktermios *oldtermios)
1111{
1112 struct tegra_uart_port *tup = to_tegra_uport(u);
1113 unsigned int baud;
1114 unsigned long flags;
1115 unsigned int lcr;
1116 int symb_bit = 1;
1117 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1118 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1119 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1120
1121 max_divider *= 16;
1122 spin_lock_irqsave(&u->lock, flags);
1123
1124 /* Changing configuration, it is safe to stop any rx now */
1125 if (tup->rts_active)
1126 set_rts(tup, false);
1127
1128 /* Clear all interrupts as configuration is going to be change */
1129 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1130 tegra_uart_read(tup, UART_IER);
1131 tegra_uart_write(tup, 0, UART_IER);
1132 tegra_uart_read(tup, UART_IER);
1133
1134 /* Parity */
1135 lcr = tup->lcr_shadow;
1136 lcr &= ~UART_LCR_PARITY;
1137
1138 /* CMSPAR isn't supported by this driver */
1139 termios->c_cflag &= ~CMSPAR;
1140
1141 if ((termios->c_cflag & PARENB) == PARENB) {
1142 symb_bit++;
1143 if (termios->c_cflag & PARODD) {
1144 lcr |= UART_LCR_PARITY;
1145 lcr &= ~UART_LCR_EPAR;
1146 lcr &= ~UART_LCR_SPAR;
1147 } else {
1148 lcr |= UART_LCR_PARITY;
1149 lcr |= UART_LCR_EPAR;
1150 lcr &= ~UART_LCR_SPAR;
1151 }
1152 }
1153
1154 lcr &= ~UART_LCR_WLEN8;
1155 switch (termios->c_cflag & CSIZE) {
1156 case CS5:
1157 lcr |= UART_LCR_WLEN5;
1158 symb_bit += 5;
1159 break;
1160 case CS6:
1161 lcr |= UART_LCR_WLEN6;
1162 symb_bit += 6;
1163 break;
1164 case CS7:
1165 lcr |= UART_LCR_WLEN7;
1166 symb_bit += 7;
1167 break;
1168 default:
1169 lcr |= UART_LCR_WLEN8;
1170 symb_bit += 8;
1171 break;
1172 }
1173
1174 /* Stop bits */
1175 if (termios->c_cflag & CSTOPB) {
1176 lcr |= UART_LCR_STOP;
1177 symb_bit += 2;
1178 } else {
1179 lcr &= ~UART_LCR_STOP;
1180 symb_bit++;
1181 }
1182
1183 tegra_uart_write(tup, lcr, UART_LCR);
1184 tup->lcr_shadow = lcr;
1185 tup->symb_bit = symb_bit;
1186
1187 /* Baud rate. */
1188 baud = uart_get_baud_rate(u, termios, oldtermios,
1189 parent_clk_rate/max_divider,
1190 parent_clk_rate/16);
1191 spin_unlock_irqrestore(&u->lock, flags);
1192 tegra_set_baudrate(tup, baud);
1193 if (tty_termios_baud_rate(termios))
1194 tty_termios_encode_baud_rate(termios, baud, baud);
1195 spin_lock_irqsave(&u->lock, flags);
1196
1197 /* Flow control */
1198 if (termios->c_cflag & CRTSCTS) {
1199 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1200 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1201 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1202 /* if top layer has asked to set rts active then do so here */
1203 if (tup->rts_active)
1204 set_rts(tup, true);
1205 } else {
1206 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1207 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1208 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1209 }
1210
1211 /* update the port timeout based on new settings */
1212 uart_update_timeout(u, termios->c_cflag, baud);
1213
1214 /* Make sure all write has completed */
1215 tegra_uart_read(tup, UART_IER);
1216
1217 /* Reenable interrupt */
1218 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1219 tegra_uart_read(tup, UART_IER);
1220
1221 spin_unlock_irqrestore(&u->lock, flags);
1222 return;
1223}
1224
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301225static const char *tegra_uart_type(struct uart_port *u)
1226{
1227 return TEGRA_UART_TYPE;
1228}
1229
1230static struct uart_ops tegra_uart_ops = {
1231 .tx_empty = tegra_uart_tx_empty,
1232 .set_mctrl = tegra_uart_set_mctrl,
1233 .get_mctrl = tegra_uart_get_mctrl,
1234 .stop_tx = tegra_uart_stop_tx,
1235 .start_tx = tegra_uart_start_tx,
1236 .stop_rx = tegra_uart_stop_rx,
1237 .flush_buffer = tegra_uart_flush_buffer,
1238 .enable_ms = tegra_uart_enable_ms,
1239 .break_ctl = tegra_uart_break_ctl,
1240 .startup = tegra_uart_startup,
1241 .shutdown = tegra_uart_shutdown,
1242 .set_termios = tegra_uart_set_termios,
1243 .type = tegra_uart_type,
1244 .request_port = tegra_uart_request_port,
1245 .release_port = tegra_uart_release_port,
1246};
1247
1248static struct uart_driver tegra_uart_driver = {
1249 .owner = THIS_MODULE,
1250 .driver_name = "tegra_hsuart",
1251 .dev_name = "ttyTHS",
Jingoo Hanfbf3ab22013-08-08 17:40:55 +09001252 .cons = NULL,
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301253 .nr = TEGRA_UART_MAXIMUM,
1254};
1255
1256static int tegra_uart_parse_dt(struct platform_device *pdev,
1257 struct tegra_uart_port *tup)
1258{
1259 struct device_node *np = pdev->dev.of_node;
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301260 int port;
1261
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301262 port = of_alias_get_id(np, "serial");
1263 if (port < 0) {
1264 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1265 return port;
1266 }
1267 tup->uport.line = port;
1268
1269 tup->enable_modem_interrupt = of_property_read_bool(np,
1270 "nvidia,enable-modem-interrupt");
1271 return 0;
1272}
1273
Jingoo Hanfbf3ab22013-08-08 17:40:55 +09001274static struct tegra_uart_chip_data tegra20_uart_chip_data = {
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301275 .tx_fifo_full_status = false,
1276 .allow_txfifo_reset_fifo_mode = true,
1277 .support_clk_src_div = false,
1278};
1279
Jingoo Hanfbf3ab22013-08-08 17:40:55 +09001280static struct tegra_uart_chip_data tegra30_uart_chip_data = {
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301281 .tx_fifo_full_status = true,
1282 .allow_txfifo_reset_fifo_mode = false,
1283 .support_clk_src_div = true,
1284};
1285
Fabian Fredericked0bb232015-03-16 20:17:11 +01001286static const struct of_device_id tegra_uart_of_match[] = {
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301287 {
1288 .compatible = "nvidia,tegra30-hsuart",
1289 .data = &tegra30_uart_chip_data,
1290 }, {
1291 .compatible = "nvidia,tegra20-hsuart",
1292 .data = &tegra20_uart_chip_data,
1293 }, {
1294 },
1295};
1296MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1297
1298static int tegra_uart_probe(struct platform_device *pdev)
1299{
1300 struct tegra_uart_port *tup;
1301 struct uart_port *u;
1302 struct resource *resource;
1303 int ret;
1304 const struct tegra_uart_chip_data *cdata;
1305 const struct of_device_id *match;
1306
Stephen Warrenc3e1bec2013-02-15 15:04:45 -07001307 match = of_match_device(tegra_uart_of_match, &pdev->dev);
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301308 if (!match) {
1309 dev_err(&pdev->dev, "Error: No device match found\n");
1310 return -ENODEV;
1311 }
1312 cdata = match->data;
1313
1314 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1315 if (!tup) {
1316 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1317 return -ENOMEM;
1318 }
1319
1320 ret = tegra_uart_parse_dt(pdev, tup);
1321 if (ret < 0)
1322 return ret;
1323
1324 u = &tup->uport;
1325 u->dev = &pdev->dev;
1326 u->ops = &tegra_uart_ops;
1327 u->type = PORT_TEGRA;
1328 u->fifosize = 32;
1329 tup->cdata = cdata;
1330
1331 platform_set_drvdata(pdev, tup);
1332 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1333 if (!resource) {
1334 dev_err(&pdev->dev, "No IO memory resource\n");
1335 return -ENODEV;
1336 }
1337
1338 u->mapbase = resource->start;
Sachin Kamat84e81922013-03-04 09:59:00 +05301339 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1340 if (IS_ERR(u->membase))
1341 return PTR_ERR(u->membase);
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301342
1343 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1344 if (IS_ERR(tup->uart_clk)) {
1345 dev_err(&pdev->dev, "Couldn't get the clock\n");
1346 return PTR_ERR(tup->uart_clk);
1347 }
1348
Stephen Warrend3d654e2013-11-06 16:50:44 -07001349 tup->rst = devm_reset_control_get(&pdev->dev, "serial");
1350 if (IS_ERR(tup->rst)) {
1351 dev_err(&pdev->dev, "Couldn't get the reset\n");
1352 return PTR_ERR(tup->rst);
1353 }
1354
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301355 u->iotype = UPIO_MEM32;
1356 u->irq = platform_get_irq(pdev, 0);
1357 u->regshift = 2;
1358 ret = uart_add_one_port(&tegra_uart_driver, u);
1359 if (ret < 0) {
1360 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1361 return ret;
1362 }
1363 return ret;
1364}
1365
1366static int tegra_uart_remove(struct platform_device *pdev)
1367{
1368 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1369 struct uart_port *u = &tup->uport;
1370
1371 uart_remove_one_port(&tegra_uart_driver, u);
1372 return 0;
1373}
1374
1375#ifdef CONFIG_PM_SLEEP
1376static int tegra_uart_suspend(struct device *dev)
1377{
1378 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1379 struct uart_port *u = &tup->uport;
1380
1381 return uart_suspend_port(&tegra_uart_driver, u);
1382}
1383
1384static int tegra_uart_resume(struct device *dev)
1385{
1386 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1387 struct uart_port *u = &tup->uport;
1388
1389 return uart_resume_port(&tegra_uart_driver, u);
1390}
1391#endif
1392
1393static const struct dev_pm_ops tegra_uart_pm_ops = {
1394 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1395};
1396
1397static struct platform_driver tegra_uart_platform_driver = {
1398 .probe = tegra_uart_probe,
1399 .remove = tegra_uart_remove,
1400 .driver = {
1401 .name = "serial-tegra",
Stephen Warrenc3e1bec2013-02-15 15:04:45 -07001402 .of_match_table = tegra_uart_of_match,
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301403 .pm = &tegra_uart_pm_ops,
1404 },
1405};
1406
1407static int __init tegra_uart_init(void)
1408{
1409 int ret;
1410
1411 ret = uart_register_driver(&tegra_uart_driver);
1412 if (ret < 0) {
1413 pr_err("Could not register %s driver\n",
1414 tegra_uart_driver.driver_name);
1415 return ret;
1416 }
1417
1418 ret = platform_driver_register(&tegra_uart_platform_driver);
1419 if (ret < 0) {
Masanari Iida8b513d02013-05-21 23:13:12 +09001420 pr_err("Uart platform driver register failed, e = %d\n", ret);
Laxman Dewangane9ea0962013-01-08 16:27:44 +05301421 uart_unregister_driver(&tegra_uart_driver);
1422 return ret;
1423 }
1424 return 0;
1425}
1426
1427static void __exit tegra_uart_exit(void)
1428{
1429 pr_info("Unloading tegra uart driver\n");
1430 platform_driver_unregister(&tegra_uart_platform_driver);
1431 uart_unregister_driver(&tegra_uart_driver);
1432}
1433
1434module_init(tegra_uart_init);
1435module_exit(tegra_uart_exit);
1436
1437MODULE_ALIAS("platform:serial-tegra");
1438MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1439MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1440MODULE_LICENSE("GPL v2");