forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/tty/serial/serial-tegra.c
....@@ -4,7 +4,7 @@
44 *
55 * High-speed serial driver for NVIDIA Tegra SoCs
66 *
7
- * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
7
+ * Copyright (c) 2012-2019, NVIDIA CORPORATION. All rights reserved.
88 *
99 * Author: Laxman Dewangan <ldewangan@nvidia.com>
1010 */
....@@ -62,7 +62,7 @@
6262 #define TEGRA_UART_TX_TRIG_4B 0x20
6363 #define TEGRA_UART_TX_TRIG_1B 0x30
6464
65
-#define TEGRA_UART_MAXIMUM 5
65
+#define TEGRA_UART_MAXIMUM 8
6666
6767 /* Default UART setting when started: 115200 no parity, stop, 8 data bits */
6868 #define TEGRA_UART_DEFAULT_BAUD 115200
....@@ -71,6 +71,8 @@
7171 /* Tx transfer mode */
7272 #define TEGRA_TX_PIO 1
7373 #define TEGRA_TX_DMA 2
74
+
75
+#define TEGRA_UART_FCR_IIR_FIFO_EN 0x40
7476
7577 /**
7678 * tegra_uart_chip_data: SOC specific data.
....@@ -84,6 +86,17 @@
8486 bool tx_fifo_full_status;
8587 bool allow_txfifo_reset_fifo_mode;
8688 bool support_clk_src_div;
89
+ bool fifo_mode_enable_status;
90
+ int uart_max_port;
91
+ int max_dma_burst_bytes;
92
+ int error_tolerance_low_range;
93
+ int error_tolerance_high_range;
94
+};
95
+
96
+struct tegra_baud_tolerance {
97
+ u32 lower_range_baud;
98
+ u32 upper_range_baud;
99
+ s32 tolerance;
87100 };
88101
89102 struct tegra_uart_port {
....@@ -122,10 +135,19 @@
122135 dma_cookie_t rx_cookie;
123136 unsigned int tx_bytes_requested;
124137 unsigned int rx_bytes_requested;
138
+ struct tegra_baud_tolerance *baud_tolerance;
139
+ int n_adjustable_baud_rates;
140
+ int required_rate;
141
+ int configured_rate;
142
+ bool use_rx_pio;
143
+ bool use_tx_pio;
144
+ bool rx_dma_active;
125145 };
126146
127147 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
128148 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
149
+static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
150
+ bool dma_to_memory);
129151
130152 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
131153 unsigned long reg)
....@@ -192,16 +214,34 @@
192214 }
193215 }
194216
217
+static void set_loopbk(struct tegra_uart_port *tup, bool active)
218
+{
219
+ unsigned long mcr = tup->mcr_shadow;
220
+
221
+ if (active)
222
+ mcr |= UART_MCR_LOOP;
223
+ else
224
+ mcr &= ~UART_MCR_LOOP;
225
+
226
+ if (mcr != tup->mcr_shadow) {
227
+ tegra_uart_write(tup, mcr, UART_MCR);
228
+ tup->mcr_shadow = mcr;
229
+ }
230
+}
231
+
195232 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
196233 {
197234 struct tegra_uart_port *tup = to_tegra_uport(u);
198
- int dtr_enable;
235
+ int enable;
199236
200237 tup->rts_active = !!(mctrl & TIOCM_RTS);
201238 set_rts(tup, tup->rts_active);
202239
203
- dtr_enable = !!(mctrl & TIOCM_DTR);
204
- set_dtr(tup, dtr_enable);
240
+ enable = !!(mctrl & TIOCM_DTR);
241
+ set_dtr(tup, enable);
242
+
243
+ enable = !!(mctrl & TIOCM_LOOP);
244
+ set_loopbk(tup, enable);
205245 }
206246
207247 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
....@@ -243,9 +283,28 @@
243283 tup->current_baud));
244284 }
245285
286
+static int tegra_uart_wait_fifo_mode_enabled(struct tegra_uart_port *tup)
287
+{
288
+ unsigned long iir;
289
+ unsigned int tmout = 100;
290
+
291
+ do {
292
+ iir = tegra_uart_read(tup, UART_IIR);
293
+ if (iir & TEGRA_UART_FCR_IIR_FIFO_EN)
294
+ return 0;
295
+ udelay(1);
296
+ } while (--tmout);
297
+
298
+ return -ETIMEDOUT;
299
+}
300
+
246301 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
247302 {
248303 unsigned long fcr = tup->fcr_shadow;
304
+ unsigned int lsr, tmout = 10000;
305
+
306
+ if (tup->rts_active)
307
+ set_rts(tup, false);
249308
250309 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
251310 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
....@@ -258,6 +317,8 @@
258317 tegra_uart_write(tup, fcr, UART_FCR);
259318 fcr |= UART_FCR_ENABLE_FIFO;
260319 tegra_uart_write(tup, fcr, UART_FCR);
320
+ if (tup->cdata->fifo_mode_enable_status)
321
+ tegra_uart_wait_fifo_mode_enabled(tup);
261322 }
262323
263324 /* Dummy read to ensure the write is posted */
....@@ -269,6 +330,47 @@
269330 * to propagate, otherwise data could be lost.
270331 */
271332 tegra_uart_wait_cycle_time(tup, 32);
333
+
334
+ do {
335
+ lsr = tegra_uart_read(tup, UART_LSR);
336
+ if ((lsr & UART_LSR_TEMT) && !(lsr & UART_LSR_DR))
337
+ break;
338
+ udelay(1);
339
+ } while (--tmout);
340
+
341
+ if (tup->rts_active)
342
+ set_rts(tup, true);
343
+}
344
+
345
+static long tegra_get_tolerance_rate(struct tegra_uart_port *tup,
346
+ unsigned int baud, long rate)
347
+{
348
+ int i;
349
+
350
+ for (i = 0; i < tup->n_adjustable_baud_rates; ++i) {
351
+ if (baud >= tup->baud_tolerance[i].lower_range_baud &&
352
+ baud <= tup->baud_tolerance[i].upper_range_baud)
353
+ return (rate + (rate *
354
+ tup->baud_tolerance[i].tolerance) / 10000);
355
+ }
356
+
357
+ return rate;
358
+}
359
+
360
+static int tegra_check_rate_in_range(struct tegra_uart_port *tup)
361
+{
362
+ long diff;
363
+
364
+ diff = ((long)(tup->configured_rate - tup->required_rate) * 10000)
365
+ / tup->required_rate;
366
+ if (diff < (tup->cdata->error_tolerance_low_range * 100) ||
367
+ diff > (tup->cdata->error_tolerance_high_range * 100)) {
368
+ dev_err(tup->uport.dev,
369
+ "configured baud rate is out of range by %ld", diff);
370
+ return -EIO;
371
+ }
372
+
373
+ return 0;
272374 }
273375
274376 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
....@@ -276,6 +378,7 @@
276378 unsigned long rate;
277379 unsigned int divisor;
278380 unsigned long lcr;
381
+ unsigned long flags;
279382 int ret;
280383
281384 if (tup->current_baud == baud)
....@@ -283,18 +386,28 @@
283386
284387 if (tup->cdata->support_clk_src_div) {
285388 rate = baud * 16;
389
+ tup->required_rate = rate;
390
+
391
+ if (tup->n_adjustable_baud_rates)
392
+ rate = tegra_get_tolerance_rate(tup, baud, rate);
393
+
286394 ret = clk_set_rate(tup->uart_clk, rate);
287395 if (ret < 0) {
288396 dev_err(tup->uport.dev,
289397 "clk_set_rate() failed for rate %lu\n", rate);
290398 return ret;
291399 }
400
+ tup->configured_rate = clk_get_rate(tup->uart_clk);
292401 divisor = 1;
402
+ ret = tegra_check_rate_in_range(tup);
403
+ if (ret < 0)
404
+ return ret;
293405 } else {
294406 rate = clk_get_rate(tup->uart_clk);
295407 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
296408 }
297409
410
+ spin_lock_irqsave(&tup->uport.lock, flags);
298411 lcr = tup->lcr_shadow;
299412 lcr |= UART_LCR_DLAB;
300413 tegra_uart_write(tup, lcr, UART_LCR);
....@@ -307,6 +420,7 @@
307420
308421 /* Dummy read to ensure the write is posted */
309422 tegra_uart_read(tup, UART_SCR);
423
+ spin_unlock_irqrestore(&tup->uport.lock, flags);
310424
311425 tup->current_baud = baud;
312426
....@@ -325,24 +439,32 @@
325439 /* Overrrun error */
326440 flag = TTY_OVERRUN;
327441 tup->uport.icount.overrun++;
328
- dev_err(tup->uport.dev, "Got overrun errors\n");
442
+ dev_dbg(tup->uport.dev, "Got overrun errors\n");
329443 } else if (lsr & UART_LSR_PE) {
330444 /* Parity error */
331445 flag = TTY_PARITY;
332446 tup->uport.icount.parity++;
333
- dev_err(tup->uport.dev, "Got Parity errors\n");
447
+ dev_dbg(tup->uport.dev, "Got Parity errors\n");
334448 } else if (lsr & UART_LSR_FE) {
335449 flag = TTY_FRAME;
336450 tup->uport.icount.frame++;
337
- dev_err(tup->uport.dev, "Got frame errors\n");
451
+ dev_dbg(tup->uport.dev, "Got frame errors\n");
338452 } else if (lsr & UART_LSR_BI) {
339
- dev_err(tup->uport.dev, "Got Break\n");
340
- tup->uport.icount.brk++;
341
- /* If FIFO read error without any data, reset Rx FIFO */
453
+ /*
454
+ * Break error
455
+ * If FIFO read error without any data, reset Rx FIFO
456
+ */
342457 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
343458 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
459
+ if (tup->uport.ignore_status_mask & UART_LSR_BI)
460
+ return TTY_BREAK;
461
+ flag = TTY_BREAK;
462
+ tup->uport.icount.brk++;
463
+ dev_dbg(tup->uport.dev, "Got Break\n");
344464 }
465
+ uart_insert_char(&tup->uport, lsr, UART_LSR_OE, 0, flag);
345466 }
467
+
346468 return flag;
347469 }
348470
....@@ -398,7 +520,7 @@
398520 count = tup->tx_bytes_requested - state.residue;
399521 async_tx_ack(tup->tx_dma_desc);
400522 spin_lock_irqsave(&tup->uport.lock, flags);
401
- xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
523
+ uart_xmit_advance(&tup->uport, count);
402524 tup->tx_in_progress = 0;
403525 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
404526 uart_write_wakeup(&tup->uport);
....@@ -412,11 +534,12 @@
412534 struct circ_buf *xmit = &tup->uport.state->xmit;
413535 dma_addr_t tx_phys_addr;
414536
415
- dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
416
- UART_XMIT_SIZE, DMA_TO_DEVICE);
417
-
418537 tup->tx_bytes = count & ~(0xF);
419538 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
539
+
540
+ dma_sync_single_for_device(tup->uport.dev, tx_phys_addr,
541
+ tup->tx_bytes, DMA_TO_DEVICE);
542
+
420543 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
421544 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
422545 DMA_PREP_INTERRUPT);
....@@ -440,12 +563,15 @@
440563 unsigned long count;
441564 struct circ_buf *xmit = &tup->uport.state->xmit;
442565
566
+ if (!tup->current_baud)
567
+ return;
568
+
443569 tail = (unsigned long)&xmit->buf[xmit->tail];
444570 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
445571 if (!count)
446572 return;
447573
448
- if (count < TEGRA_UART_MIN_DMA)
574
+ if (tup->use_tx_pio || count < TEGRA_UART_MIN_DMA)
449575 tegra_uart_start_pio_tx(tup, count);
450576 else if (BYTES_TO_ALIGN(tail) > 0)
451577 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
....@@ -482,7 +608,6 @@
482608 static void tegra_uart_stop_tx(struct uart_port *u)
483609 {
484610 struct tegra_uart_port *tup = to_tegra_uport(u);
485
- struct circ_buf *xmit = &tup->uport.state->xmit;
486611 struct dma_tx_state state;
487612 unsigned int count;
488613
....@@ -493,7 +618,7 @@
493618 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
494619 count = tup->tx_bytes_requested - state.residue;
495620 async_tx_ack(tup->tx_dma_desc);
496
- xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
621
+ uart_xmit_advance(&tup->uport, count);
497622 tup->tx_in_progress = 0;
498623 }
499624
....@@ -509,7 +634,7 @@
509634 }
510635
511636 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
512
- struct tty_port *tty)
637
+ struct tty_port *port)
513638 {
514639 do {
515640 char flag = TTY_NORMAL;
....@@ -521,16 +646,24 @@
521646 break;
522647
523648 flag = tegra_uart_decode_rx_error(tup, lsr);
649
+ if (flag != TTY_NORMAL)
650
+ continue;
651
+
524652 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
525653 tup->uport.icount.rx++;
526654
527
- if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
528
- tty_insert_flip_char(tty, ch, flag);
655
+ if (uart_handle_sysrq_char(&tup->uport, ch))
656
+ continue;
657
+
658
+ if (tup->uport.ignore_status_mask & UART_LSR_DR)
659
+ continue;
660
+
661
+ tty_insert_flip_char(port, ch, flag);
529662 } while (1);
530663 }
531664
532665 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
533
- struct tty_port *tty,
666
+ struct tty_port *port,
534667 unsigned int count)
535668 {
536669 int copied;
....@@ -540,27 +673,38 @@
540673 return;
541674
542675 tup->uport.icount.rx += count;
543
- if (!tty) {
544
- dev_err(tup->uport.dev, "No tty port\n");
676
+
677
+ if (tup->uport.ignore_status_mask & UART_LSR_DR)
545678 return;
546
- }
679
+
547680 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
548
- TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
549
- copied = tty_insert_flip_string(tty,
681
+ count, DMA_FROM_DEVICE);
682
+ copied = tty_insert_flip_string(port,
550683 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
551684 if (copied != count) {
552685 WARN_ON(1);
553686 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
554687 }
555688 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
556
- TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
689
+ count, DMA_TO_DEVICE);
690
+}
691
+
692
+static void do_handle_rx_pio(struct tegra_uart_port *tup)
693
+{
694
+ struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
695
+ struct tty_port *port = &tup->uport.state->port;
696
+
697
+ tegra_uart_handle_rx_pio(tup, port);
698
+ if (tty) {
699
+ tty_flip_buffer_push(port);
700
+ tty_kref_put(tty);
701
+ }
557702 }
558703
559704 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
560705 unsigned int residue)
561706 {
562707 struct tty_port *port = &tup->uport.state->port;
563
- struct tty_struct *tty = tty_port_tty_get(port);
564708 unsigned int count;
565709
566710 async_tx_ack(tup->rx_dma_desc);
....@@ -569,11 +713,7 @@
569713 /* If we are here, DMA is stopped */
570714 tegra_uart_copy_rx_to_tty(tup, port, count);
571715
572
- tegra_uart_handle_rx_pio(tup, port);
573
- if (tty) {
574
- tty_flip_buffer_push(port);
575
- tty_kref_put(tty);
576
- }
716
+ do_handle_rx_pio(tup);
577717 }
578718
579719 static void tegra_uart_rx_dma_complete(void *args)
....@@ -597,6 +737,7 @@
597737 if (tup->rts_active)
598738 set_rts(tup, false);
599739
740
+ tup->rx_dma_active = false;
600741 tegra_uart_rx_buffer_push(tup, 0);
601742 tegra_uart_start_rx_dma(tup);
602743
....@@ -608,18 +749,29 @@
608749 spin_unlock_irqrestore(&u->lock, flags);
609750 }
610751
611
-static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
752
+static void tegra_uart_terminate_rx_dma(struct tegra_uart_port *tup)
612753 {
613754 struct dma_tx_state state;
614755
756
+ if (!tup->rx_dma_active) {
757
+ do_handle_rx_pio(tup);
758
+ return;
759
+ }
760
+
761
+ dmaengine_terminate_all(tup->rx_dma_chan);
762
+ dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
763
+
764
+ tegra_uart_rx_buffer_push(tup, state.residue);
765
+ tup->rx_dma_active = false;
766
+}
767
+
768
+static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
769
+{
615770 /* Deactivate flow control to stop sender */
616771 if (tup->rts_active)
617772 set_rts(tup, false);
618773
619
- dmaengine_terminate_all(tup->rx_dma_chan);
620
- dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
621
- tegra_uart_rx_buffer_push(tup, state.residue);
622
- tegra_uart_start_rx_dma(tup);
774
+ tegra_uart_terminate_rx_dma(tup);
623775
624776 if (tup->rts_active)
625777 set_rts(tup, true);
....@@ -629,6 +781,9 @@
629781 {
630782 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
631783
784
+ if (tup->rx_dma_active)
785
+ return 0;
786
+
632787 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
633788 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
634789 DMA_PREP_INTERRUPT);
....@@ -637,10 +792,9 @@
637792 return -EIO;
638793 }
639794
795
+ tup->rx_dma_active = true;
640796 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
641797 tup->rx_dma_desc->callback_param = tup;
642
- dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
643
- count, DMA_TO_DEVICE);
644798 tup->rx_bytes_requested = count;
645799 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
646800 dma_async_issue_pending(tup->rx_dma_chan);
....@@ -674,6 +828,7 @@
674828 struct uart_port *u = &tup->uport;
675829 unsigned long iir;
676830 unsigned long ier;
831
+ bool is_rx_start = false;
677832 bool is_rx_int = false;
678833 unsigned long flags;
679834
....@@ -681,15 +836,17 @@
681836 while (1) {
682837 iir = tegra_uart_read(tup, UART_IIR);
683838 if (iir & UART_IIR_NO_INT) {
684
- if (is_rx_int) {
839
+ if (!tup->use_rx_pio && is_rx_int) {
685840 tegra_uart_handle_rx_dma(tup);
686841 if (tup->rx_in_progress) {
687842 ier = tup->ier_shadow;
688843 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
689
- TEGRA_UART_IER_EORD);
844
+ TEGRA_UART_IER_EORD | UART_IER_RDI);
690845 tup->ier_shadow = ier;
691846 tegra_uart_write(tup, ier, UART_IER);
692847 }
848
+ } else if (is_rx_start) {
849
+ tegra_uart_start_rx_dma(tup);
693850 }
694851 spin_unlock_irqrestore(&u->lock, flags);
695852 return IRQ_HANDLED;
....@@ -708,17 +865,25 @@
708865
709866 case 4: /* End of data */
710867 case 6: /* Rx timeout */
711
- case 2: /* Receive */
712
- if (!is_rx_int) {
713
- is_rx_int = true;
868
+ if (!tup->use_rx_pio) {
869
+ is_rx_int = tup->rx_in_progress;
714870 /* Disable Rx interrupts */
715871 ier = tup->ier_shadow;
716
- ier |= UART_IER_RDI;
717
- tegra_uart_write(tup, ier, UART_IER);
718872 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
719873 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
720874 tup->ier_shadow = ier;
721875 tegra_uart_write(tup, ier, UART_IER);
876
+ break;
877
+ }
878
+ fallthrough;
879
+ case 2: /* Receive */
880
+ if (!tup->use_rx_pio) {
881
+ is_rx_start = tup->rx_in_progress;
882
+ tup->ier_shadow &= ~UART_IER_RDI;
883
+ tegra_uart_write(tup, tup->ier_shadow,
884
+ UART_IER);
885
+ } else {
886
+ do_handle_rx_pio(tup);
722887 }
723888 break;
724889
....@@ -737,7 +902,7 @@
737902 static void tegra_uart_stop_rx(struct uart_port *u)
738903 {
739904 struct tegra_uart_port *tup = to_tegra_uport(u);
740
- struct dma_tx_state state;
905
+ struct tty_port *port = &tup->uport.state->port;
741906 unsigned long ier;
742907
743908 if (tup->rts_active)
....@@ -746,7 +911,7 @@
746911 if (!tup->rx_in_progress)
747912 return;
748913
749
- tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
914
+ tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
750915
751916 ier = tup->ier_shadow;
752917 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
....@@ -754,9 +919,11 @@
754919 tup->ier_shadow = ier;
755920 tegra_uart_write(tup, ier, UART_IER);
756921 tup->rx_in_progress = 0;
757
- dmaengine_terminate_all(tup->rx_dma_chan);
758
- dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
759
- tegra_uart_rx_buffer_push(tup, state.residue);
922
+
923
+ if (!tup->use_rx_pio)
924
+ tegra_uart_terminate_rx_dma(tup);
925
+ else
926
+ tegra_uart_handle_rx_pio(tup, port);
760927 }
761928
762929 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
....@@ -804,6 +971,14 @@
804971 tup->current_baud = 0;
805972 spin_unlock_irqrestore(&tup->uport.lock, flags);
806973
974
+ tup->rx_in_progress = 0;
975
+ tup->tx_in_progress = 0;
976
+
977
+ if (!tup->use_rx_pio)
978
+ tegra_uart_dma_channel_free(tup, true);
979
+ if (!tup->use_tx_pio)
980
+ tegra_uart_dma_channel_free(tup, false);
981
+
807982 clk_disable_unprepare(tup->uart_clk);
808983 }
809984
....@@ -846,34 +1021,55 @@
8461021 * programmed in the DMA registers.
8471022 */
8481023 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
849
- tup->fcr_shadow |= UART_FCR_R_TRIG_01;
1024
+
1025
+ if (tup->use_rx_pio) {
1026
+ tup->fcr_shadow |= UART_FCR_R_TRIG_11;
1027
+ } else {
1028
+ if (tup->cdata->max_dma_burst_bytes == 8)
1029
+ tup->fcr_shadow |= UART_FCR_R_TRIG_10;
1030
+ else
1031
+ tup->fcr_shadow |= UART_FCR_R_TRIG_01;
1032
+ }
1033
+
8501034 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
8511035 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
8521036
8531037 /* Dummy read to ensure the write is posted */
8541038 tegra_uart_read(tup, UART_SCR);
8551039
856
- /*
857
- * For all tegra devices (up to t210), there is a hardware issue that
858
- * requires software to wait for 3 UART clock periods after enabling
859
- * the TX fifo, otherwise data could be lost.
860
- */
861
- tegra_uart_wait_cycle_time(tup, 3);
1040
+ if (tup->cdata->fifo_mode_enable_status) {
1041
+ ret = tegra_uart_wait_fifo_mode_enabled(tup);
1042
+ if (ret < 0) {
1043
+ dev_err(tup->uport.dev,
1044
+ "Failed to enable FIFO mode: %d\n", ret);
1045
+ return ret;
1046
+ }
1047
+ } else {
1048
+ /*
1049
+ * For all tegra devices (up to t210), there is a hardware
1050
+ * issue that requires software to wait for 3 UART clock
1051
+ * periods after enabling the TX fifo, otherwise data could
1052
+ * be lost.
1053
+ */
1054
+ tegra_uart_wait_cycle_time(tup, 3);
1055
+ }
8621056
8631057 /*
8641058 * Initialize the UART with default configuration
8651059 * (115200, N, 8, 1) so that the receive DMA buffer may be
8661060 * enqueued
8671061 */
868
- tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
869
- tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
870
- tup->fcr_shadow |= UART_FCR_DMA_SELECT;
871
- tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
872
-
873
- ret = tegra_uart_start_rx_dma(tup);
1062
+ ret = tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
8741063 if (ret < 0) {
875
- dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
1064
+ dev_err(tup->uport.dev, "Failed to set baud rate\n");
8761065 return ret;
1066
+ }
1067
+ if (!tup->use_rx_pio) {
1068
+ tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
1069
+ tup->fcr_shadow |= UART_FCR_DMA_SELECT;
1070
+ tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
1071
+ } else {
1072
+ tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
8771073 }
8781074 tup->rx_in_progress = 1;
8791075
....@@ -881,13 +1077,9 @@
8811077 * Enable IE_RXS for the receive status interrupts like line errros.
8821078 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
8831079 *
884
- * If using DMA mode, enable EORD instead of receive interrupt which
885
- * will interrupt after the UART is done with the receive instead of
886
- * the interrupt when the FIFO "threshold" is reached.
887
- *
8881080 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
8891081 * the DATA is sitting in the FIFO and couldn't be transferred to the
890
- * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
1082
+ * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
8911083 * triggered when there is a pause of the incomming data stream for 4
8921084 * characters long.
8931085 *
....@@ -895,7 +1087,15 @@
8951087 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
8961088 * then the EORD.
8971089 */
898
- tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
1090
+ tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | UART_IER_RDI;
1091
+
1092
+ /*
1093
+ * If using DMA mode, enable EORD interrupt to notify about RX
1094
+ * completion.
1095
+ */
1096
+ if (!tup->use_rx_pio)
1097
+ tup->ier_shadow |= TEGRA_UART_IER_EORD;
1098
+
8991099 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
9001100 return 0;
9011101 }
....@@ -931,8 +1131,7 @@
9311131 int ret;
9321132 struct dma_slave_config dma_sconfig;
9331133
934
- dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
935
- dma_to_memory ? "rx" : "tx");
1134
+ dma_chan = dma_request_chan(tup->uport.dev, dma_to_memory ? "rx" : "tx");
9361135 if (IS_ERR(dma_chan)) {
9371136 ret = PTR_ERR(dma_chan);
9381137 dev_err(tup->uport.dev,
....@@ -950,9 +1149,12 @@
9501149 dma_release_channel(dma_chan);
9511150 return -ENOMEM;
9521151 }
1152
+ dma_sync_single_for_device(tup->uport.dev, dma_phys,
1153
+ TEGRA_UART_RX_DMA_BUFFER_SIZE,
1154
+ DMA_TO_DEVICE);
9531155 dma_sconfig.src_addr = tup->uport.mapbase;
9541156 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
955
- dma_sconfig.src_maxburst = 4;
1157
+ dma_sconfig.src_maxburst = tup->cdata->max_dma_burst_bytes;
9561158 tup->rx_dma_chan = dma_chan;
9571159 tup->rx_dma_buf_virt = dma_buf;
9581160 tup->rx_dma_buf_phys = dma_phys;
....@@ -990,16 +1192,22 @@
9901192 struct tegra_uart_port *tup = to_tegra_uport(u);
9911193 int ret;
9921194
993
- ret = tegra_uart_dma_channel_allocate(tup, false);
994
- if (ret < 0) {
995
- dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
996
- return ret;
1195
+ if (!tup->use_tx_pio) {
1196
+ ret = tegra_uart_dma_channel_allocate(tup, false);
1197
+ if (ret < 0) {
1198
+ dev_err(u->dev, "Tx Dma allocation failed, err = %d\n",
1199
+ ret);
1200
+ return ret;
1201
+ }
9971202 }
9981203
999
- ret = tegra_uart_dma_channel_allocate(tup, true);
1000
- if (ret < 0) {
1001
- dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1002
- goto fail_rx_dma;
1204
+ if (!tup->use_rx_pio) {
1205
+ ret = tegra_uart_dma_channel_allocate(tup, true);
1206
+ if (ret < 0) {
1207
+ dev_err(u->dev, "Rx Dma allocation failed, err = %d\n",
1208
+ ret);
1209
+ goto fail_rx_dma;
1210
+ }
10031211 }
10041212
10051213 ret = tegra_uart_hw_init(tup);
....@@ -1017,9 +1225,11 @@
10171225 return 0;
10181226
10191227 fail_hw_init:
1020
- tegra_uart_dma_channel_free(tup, true);
1228
+ if (!tup->use_rx_pio)
1229
+ tegra_uart_dma_channel_free(tup, true);
10211230 fail_rx_dma:
1022
- tegra_uart_dma_channel_free(tup, false);
1231
+ if (!tup->use_tx_pio)
1232
+ tegra_uart_dma_channel_free(tup, false);
10231233 return ret;
10241234 }
10251235
....@@ -1041,12 +1251,6 @@
10411251 struct tegra_uart_port *tup = to_tegra_uport(u);
10421252
10431253 tegra_uart_hw_deinit(tup);
1044
-
1045
- tup->rx_in_progress = 0;
1046
- tup->tx_in_progress = 0;
1047
-
1048
- tegra_uart_dma_channel_free(tup, true);
1049
- tegra_uart_dma_channel_free(tup, false);
10501254 free_irq(u->irq, tup);
10511255 }
10521256
....@@ -1071,6 +1275,7 @@
10711275 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
10721276 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
10731277 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1278
+ int ret;
10741279
10751280 max_divider *= 16;
10761281 spin_lock_irqsave(&u->lock, flags);
....@@ -1079,7 +1284,7 @@
10791284 if (tup->rts_active)
10801285 set_rts(tup, false);
10811286
1082
- /* Clear all interrupts as configuration is going to be change */
1287
+ /* Clear all interrupts as configuration is going to be changed */
10831288 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
10841289 tegra_uart_read(tup, UART_IER);
10851290 tegra_uart_write(tup, 0, UART_IER);
....@@ -1143,7 +1348,11 @@
11431348 parent_clk_rate/max_divider,
11441349 parent_clk_rate/16);
11451350 spin_unlock_irqrestore(&u->lock, flags);
1146
- tegra_set_baudrate(tup, baud);
1351
+ ret = tegra_set_baudrate(tup, baud);
1352
+ if (ret < 0) {
1353
+ dev_err(tup->uport.dev, "Failed to set baud rate\n");
1354
+ return;
1355
+ }
11471356 if (tty_termios_baud_rate(termios))
11481357 tty_termios_encode_baud_rate(termios, baud, baud);
11491358 spin_lock_irqsave(&u->lock, flags);
....@@ -1165,12 +1374,19 @@
11651374 /* update the port timeout based on new settings */
11661375 uart_update_timeout(u, termios->c_cflag, baud);
11671376
1168
- /* Make sure all write has completed */
1377
+ /* Make sure all writes have completed */
11691378 tegra_uart_read(tup, UART_IER);
11701379
1171
- /* Reenable interrupt */
1380
+ /* Re-enable interrupt */
11721381 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
11731382 tegra_uart_read(tup, UART_IER);
1383
+
1384
+ tup->uport.ignore_status_mask = 0;
1385
+ /* Ignore all characters if CREAD is not set */
1386
+ if ((termios->c_cflag & CREAD) == 0)
1387
+ tup->uport.ignore_status_mask |= UART_LSR_DR;
1388
+ if (termios->c_iflag & IGNBRK)
1389
+ tup->uport.ignore_status_mask |= UART_LSR_BI;
11741390
11751391 spin_unlock_irqrestore(&u->lock, flags);
11761392 }
....@@ -1211,6 +1427,11 @@
12111427 {
12121428 struct device_node *np = pdev->dev.of_node;
12131429 int port;
1430
+ int ret;
1431
+ int index;
1432
+ u32 pval;
1433
+ int count;
1434
+ int n_entries;
12141435
12151436 port = of_alias_get_id(np, "serial");
12161437 if (port < 0) {
....@@ -1221,6 +1442,54 @@
12211442
12221443 tup->enable_modem_interrupt = of_property_read_bool(np,
12231444 "nvidia,enable-modem-interrupt");
1445
+
1446
+ index = of_property_match_string(np, "dma-names", "rx");
1447
+ if (index < 0) {
1448
+ tup->use_rx_pio = true;
1449
+ dev_info(&pdev->dev, "RX in PIO mode\n");
1450
+ }
1451
+ index = of_property_match_string(np, "dma-names", "tx");
1452
+ if (index < 0) {
1453
+ tup->use_tx_pio = true;
1454
+ dev_info(&pdev->dev, "TX in PIO mode\n");
1455
+ }
1456
+
1457
+ n_entries = of_property_count_u32_elems(np, "nvidia,adjust-baud-rates");
1458
+ if (n_entries > 0) {
1459
+ tup->n_adjustable_baud_rates = n_entries / 3;
1460
+ tup->baud_tolerance =
1461
+ devm_kzalloc(&pdev->dev, (tup->n_adjustable_baud_rates) *
1462
+ sizeof(*tup->baud_tolerance), GFP_KERNEL);
1463
+ if (!tup->baud_tolerance)
1464
+ return -ENOMEM;
1465
+ for (count = 0, index = 0; count < n_entries; count += 3,
1466
+ index++) {
1467
+ ret =
1468
+ of_property_read_u32_index(np,
1469
+ "nvidia,adjust-baud-rates",
1470
+ count, &pval);
1471
+ if (!ret)
1472
+ tup->baud_tolerance[index].lower_range_baud =
1473
+ pval;
1474
+ ret =
1475
+ of_property_read_u32_index(np,
1476
+ "nvidia,adjust-baud-rates",
1477
+ count + 1, &pval);
1478
+ if (!ret)
1479
+ tup->baud_tolerance[index].upper_range_baud =
1480
+ pval;
1481
+ ret =
1482
+ of_property_read_u32_index(np,
1483
+ "nvidia,adjust-baud-rates",
1484
+ count + 2, &pval);
1485
+ if (!ret)
1486
+ tup->baud_tolerance[index].tolerance =
1487
+ (s32)pval;
1488
+ }
1489
+ } else {
1490
+ tup->n_adjustable_baud_rates = 0;
1491
+ }
1492
+
12241493 return 0;
12251494 }
12261495
....@@ -1228,12 +1497,44 @@
12281497 .tx_fifo_full_status = false,
12291498 .allow_txfifo_reset_fifo_mode = true,
12301499 .support_clk_src_div = false,
1500
+ .fifo_mode_enable_status = false,
1501
+ .uart_max_port = 5,
1502
+ .max_dma_burst_bytes = 4,
1503
+ .error_tolerance_low_range = -4,
1504
+ .error_tolerance_high_range = 4,
12311505 };
12321506
12331507 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
12341508 .tx_fifo_full_status = true,
12351509 .allow_txfifo_reset_fifo_mode = false,
12361510 .support_clk_src_div = true,
1511
+ .fifo_mode_enable_status = false,
1512
+ .uart_max_port = 5,
1513
+ .max_dma_burst_bytes = 4,
1514
+ .error_tolerance_low_range = -4,
1515
+ .error_tolerance_high_range = 4,
1516
+};
1517
+
1518
+static struct tegra_uart_chip_data tegra186_uart_chip_data = {
1519
+ .tx_fifo_full_status = true,
1520
+ .allow_txfifo_reset_fifo_mode = false,
1521
+ .support_clk_src_div = true,
1522
+ .fifo_mode_enable_status = true,
1523
+ .uart_max_port = 8,
1524
+ .max_dma_burst_bytes = 8,
1525
+ .error_tolerance_low_range = 0,
1526
+ .error_tolerance_high_range = 4,
1527
+};
1528
+
1529
+static struct tegra_uart_chip_data tegra194_uart_chip_data = {
1530
+ .tx_fifo_full_status = true,
1531
+ .allow_txfifo_reset_fifo_mode = false,
1532
+ .support_clk_src_div = true,
1533
+ .fifo_mode_enable_status = true,
1534
+ .uart_max_port = 8,
1535
+ .max_dma_burst_bytes = 8,
1536
+ .error_tolerance_low_range = -2,
1537
+ .error_tolerance_high_range = 2,
12371538 };
12381539
12391540 static const struct of_device_id tegra_uart_of_match[] = {
....@@ -1243,6 +1544,12 @@
12431544 }, {
12441545 .compatible = "nvidia,tegra20-hsuart",
12451546 .data = &tegra20_uart_chip_data,
1547
+ }, {
1548
+ .compatible = "nvidia,tegra186-hsuart",
1549
+ .data = &tegra186_uart_chip_data,
1550
+ }, {
1551
+ .compatible = "nvidia,tegra194-hsuart",
1552
+ .data = &tegra194_uart_chip_data,
12461553 }, {
12471554 },
12481555 };
....@@ -1307,10 +1614,8 @@
13071614
13081615 u->iotype = UPIO_MEM32;
13091616 ret = platform_get_irq(pdev, 0);
1310
- if (ret < 0) {
1311
- dev_err(&pdev->dev, "Couldn't get IRQ\n");
1617
+ if (ret < 0)
13121618 return ret;
1313
- }
13141619 u->irq = ret;
13151620 u->regshift = 2;
13161621 ret = uart_add_one_port(&tegra_uart_driver, u);
....@@ -1365,11 +1670,22 @@
13651670 static int __init tegra_uart_init(void)
13661671 {
13671672 int ret;
1673
+ struct device_node *node;
1674
+ const struct of_device_id *match = NULL;
1675
+ const struct tegra_uart_chip_data *cdata = NULL;
1676
+
1677
+ node = of_find_matching_node(NULL, tegra_uart_of_match);
1678
+ if (node)
1679
+ match = of_match_node(tegra_uart_of_match, node);
1680
+ if (match)
1681
+ cdata = match->data;
1682
+ if (cdata)
1683
+ tegra_uart_driver.nr = cdata->uart_max_port;
13681684
13691685 ret = uart_register_driver(&tegra_uart_driver);
13701686 if (ret < 0) {
13711687 pr_err("Could not register %s driver\n",
1372
- tegra_uart_driver.driver_name);
1688
+ tegra_uart_driver.driver_name);
13731689 return ret;
13741690 }
13751691