1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for msm7k serial device and console
5 * Copyright (C) 2007 Google, Inc.
6 * Author: Robert Love <rlove@google.com>
7 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 #include <linux/kernel.h>
11 #include <linux/atomic.h>
12 #include <linux/dma/qcom_adm.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 #include <linux/module.h>
17 #include <linux/ioport.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/console.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial_core.h>
24 #include <linux/slab.h>
25 #include <linux/clk.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_opp.h>
28 #include <linux/delay.h>
30 #include <linux/of_device.h>
31 #include <linux/wait.h>
33 #define MSM_UART_MR1 0x0000
35 #define MSM_UART_MR1_AUTO_RFR_LEVEL0 0x3F
36 #define MSM_UART_MR1_AUTO_RFR_LEVEL1 0x3FF00
37 #define MSM_UART_DM_MR1_AUTO_RFR_LEVEL1 0xFFFFFF00
38 #define MSM_UART_MR1_RX_RDY_CTL BIT(7)
39 #define MSM_UART_MR1_CTS_CTL BIT(6)
41 #define MSM_UART_MR2 0x0004
42 #define MSM_UART_MR2_ERROR_MODE BIT(6)
43 #define MSM_UART_MR2_BITS_PER_CHAR 0x30
44 #define MSM_UART_MR2_BITS_PER_CHAR_5 (0x0 << 4)
45 #define MSM_UART_MR2_BITS_PER_CHAR_6 (0x1 << 4)
46 #define MSM_UART_MR2_BITS_PER_CHAR_7 (0x2 << 4)
47 #define MSM_UART_MR2_BITS_PER_CHAR_8 (0x3 << 4)
48 #define MSM_UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2)
49 #define MSM_UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2)
50 #define MSM_UART_MR2_PARITY_MODE_NONE 0x0
51 #define MSM_UART_MR2_PARITY_MODE_ODD 0x1
52 #define MSM_UART_MR2_PARITY_MODE_EVEN 0x2
53 #define MSM_UART_MR2_PARITY_MODE_SPACE 0x3
54 #define MSM_UART_MR2_PARITY_MODE 0x3
56 #define MSM_UART_CSR 0x0008
58 #define MSM_UART_TF 0x000C
59 #define UARTDM_TF 0x0070
61 #define MSM_UART_CR 0x0010
62 #define MSM_UART_CR_CMD_NULL (0 << 4)
63 #define MSM_UART_CR_CMD_RESET_RX (1 << 4)
64 #define MSM_UART_CR_CMD_RESET_TX (2 << 4)
65 #define MSM_UART_CR_CMD_RESET_ERR (3 << 4)
66 #define MSM_UART_CR_CMD_RESET_BREAK_INT (4 << 4)
67 #define MSM_UART_CR_CMD_START_BREAK (5 << 4)
68 #define MSM_UART_CR_CMD_STOP_BREAK (6 << 4)
69 #define MSM_UART_CR_CMD_RESET_CTS (7 << 4)
70 #define MSM_UART_CR_CMD_RESET_STALE_INT (8 << 4)
71 #define MSM_UART_CR_CMD_PACKET_MODE (9 << 4)
72 #define MSM_UART_CR_CMD_MODE_RESET (12 << 4)
73 #define MSM_UART_CR_CMD_SET_RFR (13 << 4)
74 #define MSM_UART_CR_CMD_RESET_RFR (14 << 4)
75 #define MSM_UART_CR_CMD_PROTECTION_EN (16 << 4)
76 #define MSM_UART_CR_CMD_STALE_EVENT_DISABLE (6 << 8)
77 #define MSM_UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
78 #define MSM_UART_CR_CMD_FORCE_STALE (4 << 8)
79 #define MSM_UART_CR_CMD_RESET_TX_READY (3 << 8)
80 #define MSM_UART_CR_TX_DISABLE BIT(3)
81 #define MSM_UART_CR_TX_ENABLE BIT(2)
82 #define MSM_UART_CR_RX_DISABLE BIT(1)
83 #define MSM_UART_CR_RX_ENABLE BIT(0)
84 #define MSM_UART_CR_CMD_RESET_RXBREAK_START ((1 << 11) | (2 << 4))
86 #define MSM_UART_IMR 0x0014
87 #define MSM_UART_IMR_TXLEV BIT(0)
88 #define MSM_UART_IMR_RXSTALE BIT(3)
89 #define MSM_UART_IMR_RXLEV BIT(4)
90 #define MSM_UART_IMR_DELTA_CTS BIT(5)
91 #define MSM_UART_IMR_CURRENT_CTS BIT(6)
92 #define MSM_UART_IMR_RXBREAK_START BIT(10)
94 #define MSM_UART_IPR_RXSTALE_LAST 0x20
95 #define MSM_UART_IPR_STALE_LSB 0x1F
96 #define MSM_UART_IPR_STALE_TIMEOUT_MSB 0x3FF80
97 #define MSM_UART_DM_IPR_STALE_TIMEOUT_MSB 0xFFFFFF80
99 #define MSM_UART_IPR 0x0018
100 #define MSM_UART_TFWR 0x001C
101 #define MSM_UART_RFWR 0x0020
102 #define MSM_UART_HCR 0x0024
104 #define MSM_UART_MREG 0x0028
105 #define MSM_UART_NREG 0x002C
106 #define MSM_UART_DREG 0x0030
107 #define MSM_UART_MNDREG 0x0034
108 #define MSM_UART_IRDA 0x0038
109 #define MSM_UART_MISR_MODE 0x0040
110 #define MSM_UART_MISR_RESET 0x0044
111 #define MSM_UART_MISR_EXPORT 0x0048
112 #define MSM_UART_MISR_VAL 0x004C
113 #define MSM_UART_TEST_CTRL 0x0050
115 #define MSM_UART_SR 0x0008
116 #define MSM_UART_SR_HUNT_CHAR BIT(7)
117 #define MSM_UART_SR_RX_BREAK BIT(6)
118 #define MSM_UART_SR_PAR_FRAME_ERR BIT(5)
119 #define MSM_UART_SR_OVERRUN BIT(4)
120 #define MSM_UART_SR_TX_EMPTY BIT(3)
121 #define MSM_UART_SR_TX_READY BIT(2)
122 #define MSM_UART_SR_RX_FULL BIT(1)
123 #define MSM_UART_SR_RX_READY BIT(0)
125 #define MSM_UART_RF 0x000C
126 #define UARTDM_RF 0x0070
127 #define MSM_UART_MISR 0x0010
128 #define MSM_UART_ISR 0x0014
129 #define MSM_UART_ISR_TX_READY BIT(7)
131 #define UARTDM_RXFS 0x50
132 #define UARTDM_RXFS_BUF_SHIFT 0x7
133 #define UARTDM_RXFS_BUF_MASK 0x7
135 #define UARTDM_DMEN 0x3C
136 #define UARTDM_DMEN_RX_SC_ENABLE BIT(5)
137 #define UARTDM_DMEN_TX_SC_ENABLE BIT(4)
139 #define UARTDM_DMEN_TX_BAM_ENABLE BIT(2) /* UARTDM_1P4 */
140 #define UARTDM_DMEN_TX_DM_ENABLE BIT(0) /* < UARTDM_1P4 */
142 #define UARTDM_DMEN_RX_BAM_ENABLE BIT(3) /* UARTDM_1P4 */
143 #define UARTDM_DMEN_RX_DM_ENABLE BIT(1) /* < UARTDM_1P4 */
145 #define UARTDM_DMRX 0x34
146 #define UARTDM_NCF_TX 0x40
147 #define UARTDM_RX_TOTAL_SNAP 0x38
149 #define UARTDM_BURST_SIZE 16 /* in bytes */
150 #define UARTDM_TX_AIGN(x) ((x) & ~0x3) /* valid for > 1p3 */
151 #define UARTDM_TX_MAX 256 /* in bytes, valid for <= 1p3 */
152 #define UARTDM_RX_SIZE (UART_XMIT_SIZE / 4)
162 struct dma_chan
*chan
;
163 enum dma_data_direction dir
;
170 struct scatterlist tx_sg
;
174 struct dma_async_tx_descriptor
*desc
;
178 struct uart_port uart
;
184 unsigned int old_snap_state
;
186 struct msm_dma tx_dma
;
187 struct msm_dma rx_dma
;
190 static inline struct msm_port
*to_msm_port(struct uart_port
*up
)
192 return container_of(up
, struct msm_port
, uart
);
196 void msm_write(struct uart_port
*port
, unsigned int val
, unsigned int off
)
198 writel_relaxed(val
, port
->membase
+ off
);
202 unsigned int msm_read(struct uart_port
*port
, unsigned int off
)
204 return readl_relaxed(port
->membase
+ off
);
208 * Setup the MND registers to use the TCXO clock.
210 static void msm_serial_set_mnd_regs_tcxo(struct uart_port
*port
)
212 msm_write(port
, 0x06, MSM_UART_MREG
);
213 msm_write(port
, 0xF1, MSM_UART_NREG
);
214 msm_write(port
, 0x0F, MSM_UART_DREG
);
215 msm_write(port
, 0x1A, MSM_UART_MNDREG
);
216 port
->uartclk
= 1843200;
220 * Setup the MND registers to use the TCXO clock divided by 4.
222 static void msm_serial_set_mnd_regs_tcxoby4(struct uart_port
*port
)
224 msm_write(port
, 0x18, MSM_UART_MREG
);
225 msm_write(port
, 0xF6, MSM_UART_NREG
);
226 msm_write(port
, 0x0F, MSM_UART_DREG
);
227 msm_write(port
, 0x0A, MSM_UART_MNDREG
);
228 port
->uartclk
= 1843200;
231 static void msm_serial_set_mnd_regs(struct uart_port
*port
)
233 struct msm_port
*msm_port
= to_msm_port(port
);
236 * These registers don't exist so we change the clk input rate
237 * on uartdm hardware instead
239 if (msm_port
->is_uartdm
)
242 if (port
->uartclk
== 19200000)
243 msm_serial_set_mnd_regs_tcxo(port
);
244 else if (port
->uartclk
== 4800000)
245 msm_serial_set_mnd_regs_tcxoby4(port
);
248 static void msm_handle_tx(struct uart_port
*port
);
249 static void msm_start_rx_dma(struct msm_port
*msm_port
);
251 static void msm_stop_dma(struct uart_port
*port
, struct msm_dma
*dma
)
253 struct device
*dev
= port
->dev
;
257 if (dma
->dir
== DMA_TO_DEVICE
) {
258 mapped
= sg_dma_len(&dma
->tx_sg
);
260 mapped
= dma
->rx
.count
;
264 dmaengine_terminate_all(dma
->chan
);
267 * DMA Stall happens if enqueue and flush command happens concurrently.
268 * For example before changing the baud rate/protocol configuration and
269 * sending flush command to ADM, disable the channel of UARTDM.
270 * Note: should not reset the receiver here immediately as it is not
271 * suggested to do disable/reset or reset/disable at the same time.
273 val
= msm_read(port
, UARTDM_DMEN
);
274 val
&= ~dma
->enable_bit
;
275 msm_write(port
, val
, UARTDM_DMEN
);
278 if (dma
->dir
== DMA_TO_DEVICE
) {
279 dma_unmap_sg(dev
, &dma
->tx_sg
, 1, dma
->dir
);
280 sg_init_table(&dma
->tx_sg
, 1);
282 dma_unmap_single(dev
, dma
->rx
.phys
, mapped
, dma
->dir
);
286 static void msm_release_dma(struct msm_port
*msm_port
)
290 dma
= &msm_port
->tx_dma
;
292 msm_stop_dma(&msm_port
->uart
, dma
);
293 dma_release_channel(dma
->chan
);
296 memset(dma
, 0, sizeof(*dma
));
298 dma
= &msm_port
->rx_dma
;
300 msm_stop_dma(&msm_port
->uart
, dma
);
301 dma_release_channel(dma
->chan
);
305 memset(dma
, 0, sizeof(*dma
));
308 static void msm_request_tx_dma(struct msm_port
*msm_port
, resource_size_t base
)
310 struct device
*dev
= msm_port
->uart
.dev
;
311 struct dma_slave_config conf
;
312 struct qcom_adm_peripheral_config periph_conf
= {};
317 dma
= &msm_port
->tx_dma
;
319 /* allocate DMA resources, if available */
320 dma
->chan
= dma_request_chan(dev
, "tx");
321 if (IS_ERR(dma
->chan
))
324 of_property_read_u32(dev
->of_node
, "qcom,tx-crci", &crci
);
326 memset(&conf
, 0, sizeof(conf
));
327 conf
.direction
= DMA_MEM_TO_DEV
;
328 conf
.device_fc
= true;
329 conf
.dst_addr
= base
+ UARTDM_TF
;
330 conf
.dst_maxburst
= UARTDM_BURST_SIZE
;
332 conf
.peripheral_config
= &periph_conf
;
333 conf
.peripheral_size
= sizeof(periph_conf
);
334 periph_conf
.crci
= crci
;
337 ret
= dmaengine_slave_config(dma
->chan
, &conf
);
341 dma
->dir
= DMA_TO_DEVICE
;
343 if (msm_port
->is_uartdm
< UARTDM_1P4
)
344 dma
->enable_bit
= UARTDM_DMEN_TX_DM_ENABLE
;
346 dma
->enable_bit
= UARTDM_DMEN_TX_BAM_ENABLE
;
351 dma_release_channel(dma
->chan
);
353 memset(dma
, 0, sizeof(*dma
));
356 static void msm_request_rx_dma(struct msm_port
*msm_port
, resource_size_t base
)
358 struct device
*dev
= msm_port
->uart
.dev
;
359 struct dma_slave_config conf
;
360 struct qcom_adm_peripheral_config periph_conf
= {};
365 dma
= &msm_port
->rx_dma
;
367 /* allocate DMA resources, if available */
368 dma
->chan
= dma_request_chan(dev
, "rx");
369 if (IS_ERR(dma
->chan
))
372 of_property_read_u32(dev
->of_node
, "qcom,rx-crci", &crci
);
374 dma
->rx
.virt
= kzalloc(UARTDM_RX_SIZE
, GFP_KERNEL
);
378 memset(&conf
, 0, sizeof(conf
));
379 conf
.direction
= DMA_DEV_TO_MEM
;
380 conf
.device_fc
= true;
381 conf
.src_addr
= base
+ UARTDM_RF
;
382 conf
.src_maxburst
= UARTDM_BURST_SIZE
;
384 conf
.peripheral_config
= &periph_conf
;
385 conf
.peripheral_size
= sizeof(periph_conf
);
386 periph_conf
.crci
= crci
;
389 ret
= dmaengine_slave_config(dma
->chan
, &conf
);
393 dma
->dir
= DMA_FROM_DEVICE
;
395 if (msm_port
->is_uartdm
< UARTDM_1P4
)
396 dma
->enable_bit
= UARTDM_DMEN_RX_DM_ENABLE
;
398 dma
->enable_bit
= UARTDM_DMEN_RX_BAM_ENABLE
;
404 dma_release_channel(dma
->chan
);
406 memset(dma
, 0, sizeof(*dma
));
409 static inline void msm_wait_for_xmitr(struct uart_port
*port
)
411 unsigned int timeout
= 500000;
413 while (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_EMPTY
)) {
414 if (msm_read(port
, MSM_UART_ISR
) & MSM_UART_ISR_TX_READY
)
420 msm_write(port
, MSM_UART_CR_CMD_RESET_TX_READY
, MSM_UART_CR
);
423 static void msm_stop_tx(struct uart_port
*port
)
425 struct msm_port
*msm_port
= to_msm_port(port
);
427 msm_port
->imr
&= ~MSM_UART_IMR_TXLEV
;
428 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
431 static void msm_start_tx(struct uart_port
*port
)
433 struct msm_port
*msm_port
= to_msm_port(port
);
434 struct msm_dma
*dma
= &msm_port
->tx_dma
;
436 /* Already started in DMA mode */
437 if (sg_dma_len(&dma
->tx_sg
))
440 msm_port
->imr
|= MSM_UART_IMR_TXLEV
;
441 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
444 static void msm_reset_dm_count(struct uart_port
*port
, int count
)
446 msm_wait_for_xmitr(port
);
447 msm_write(port
, count
, UARTDM_NCF_TX
);
448 msm_read(port
, UARTDM_NCF_TX
);
451 static void msm_complete_tx_dma(void *args
)
453 struct msm_port
*msm_port
= args
;
454 struct uart_port
*port
= &msm_port
->uart
;
455 struct tty_port
*tport
= &port
->state
->port
;
456 struct msm_dma
*dma
= &msm_port
->tx_dma
;
457 struct dma_tx_state state
;
462 uart_port_lock_irqsave(port
, &flags
);
464 /* Already stopped */
465 if (!sg_dma_len(&dma
->tx_sg
))
468 dmaengine_tx_status(dma
->chan
, dma
->cookie
, &state
);
470 dma_unmap_sg(port
->dev
, &dma
->tx_sg
, 1, dma
->dir
);
472 val
= msm_read(port
, UARTDM_DMEN
);
473 val
&= ~dma
->enable_bit
;
474 msm_write(port
, val
, UARTDM_DMEN
);
476 if (msm_port
->is_uartdm
> UARTDM_1P3
) {
477 msm_write(port
, MSM_UART_CR_CMD_RESET_TX
, MSM_UART_CR
);
478 msm_write(port
, MSM_UART_CR_TX_ENABLE
, MSM_UART_CR
);
481 count
= sg_dma_len(&dma
->tx_sg
) - state
.residue
;
482 uart_xmit_advance(port
, count
);
483 sg_init_table(&dma
->tx_sg
, 1);
485 /* Restore "Tx FIFO below watermark" interrupt */
486 msm_port
->imr
|= MSM_UART_IMR_TXLEV
;
487 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
489 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
490 uart_write_wakeup(port
);
494 uart_port_unlock_irqrestore(port
, flags
);
497 static int msm_handle_tx_dma(struct msm_port
*msm_port
, unsigned int count
)
499 struct uart_port
*port
= &msm_port
->uart
;
500 struct tty_port
*tport
= &port
->state
->port
;
501 struct msm_dma
*dma
= &msm_port
->tx_dma
;
506 sg_init_table(&dma
->tx_sg
, 1);
507 kfifo_dma_out_prepare(&tport
->xmit_fifo
, &dma
->tx_sg
, 1, count
);
509 mapped
= dma_map_sg(port
->dev
, &dma
->tx_sg
, 1, dma
->dir
);
515 dma
->desc
= dmaengine_prep_slave_sg(dma
->chan
, &dma
->tx_sg
, 1,
524 dma
->desc
->callback
= msm_complete_tx_dma
;
525 dma
->desc
->callback_param
= msm_port
;
527 dma
->cookie
= dmaengine_submit(dma
->desc
);
528 ret
= dma_submit_error(dma
->cookie
);
533 * Using DMA complete for Tx FIFO reload, no need for
534 * "Tx FIFO below watermark" one, disable it
536 msm_port
->imr
&= ~MSM_UART_IMR_TXLEV
;
537 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
539 val
= msm_read(port
, UARTDM_DMEN
);
540 val
|= dma
->enable_bit
;
542 if (msm_port
->is_uartdm
< UARTDM_1P4
)
543 msm_write(port
, val
, UARTDM_DMEN
);
545 msm_reset_dm_count(port
, count
);
547 if (msm_port
->is_uartdm
> UARTDM_1P3
)
548 msm_write(port
, val
, UARTDM_DMEN
);
550 dma_async_issue_pending(dma
->chan
);
553 dma_unmap_sg(port
->dev
, &dma
->tx_sg
, 1, dma
->dir
);
555 sg_init_table(&dma
->tx_sg
, 1);
559 static void msm_complete_rx_dma(void *args
)
561 struct msm_port
*msm_port
= args
;
562 struct uart_port
*port
= &msm_port
->uart
;
563 struct tty_port
*tport
= &port
->state
->port
;
564 struct msm_dma
*dma
= &msm_port
->rx_dma
;
565 int count
= 0, i
, sysrq
;
569 uart_port_lock_irqsave(port
, &flags
);
571 /* Already stopped */
575 val
= msm_read(port
, UARTDM_DMEN
);
576 val
&= ~dma
->enable_bit
;
577 msm_write(port
, val
, UARTDM_DMEN
);
579 if (msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_OVERRUN
) {
580 port
->icount
.overrun
++;
581 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
582 msm_write(port
, MSM_UART_CR_CMD_RESET_ERR
, MSM_UART_CR
);
585 count
= msm_read(port
, UARTDM_RX_TOTAL_SNAP
);
587 port
->icount
.rx
+= count
;
591 dma_unmap_single(port
->dev
, dma
->rx
.phys
, UARTDM_RX_SIZE
, dma
->dir
);
593 for (i
= 0; i
< count
; i
++) {
594 char flag
= TTY_NORMAL
;
596 if (msm_port
->break_detected
&& dma
->rx
.virt
[i
] == 0) {
599 msm_port
->break_detected
= false;
600 if (uart_handle_break(port
))
604 if (!(port
->read_status_mask
& MSM_UART_SR_RX_BREAK
))
607 sysrq
= uart_prepare_sysrq_char(port
, dma
->rx
.virt
[i
]);
609 tty_insert_flip_char(tport
, dma
->rx
.virt
[i
], flag
);
612 msm_start_rx_dma(msm_port
);
614 uart_unlock_and_check_sysrq_irqrestore(port
, flags
);
617 tty_flip_buffer_push(tport
);
620 static void msm_start_rx_dma(struct msm_port
*msm_port
)
622 struct msm_dma
*dma
= &msm_port
->rx_dma
;
623 struct uart_port
*uart
= &msm_port
->uart
;
627 if (IS_ENABLED(CONFIG_CONSOLE_POLL
))
633 dma
->rx
.phys
= dma_map_single(uart
->dev
, dma
->rx
.virt
,
634 UARTDM_RX_SIZE
, dma
->dir
);
635 ret
= dma_mapping_error(uart
->dev
, dma
->rx
.phys
);
639 dma
->desc
= dmaengine_prep_slave_single(dma
->chan
, dma
->rx
.phys
,
640 UARTDM_RX_SIZE
, DMA_DEV_TO_MEM
,
645 dma
->desc
->callback
= msm_complete_rx_dma
;
646 dma
->desc
->callback_param
= msm_port
;
648 dma
->cookie
= dmaengine_submit(dma
->desc
);
649 ret
= dma_submit_error(dma
->cookie
);
653 * Using DMA for FIFO off-load, no need for "Rx FIFO over
654 * watermark" or "stale" interrupts, disable them
656 msm_port
->imr
&= ~(MSM_UART_IMR_RXLEV
| MSM_UART_IMR_RXSTALE
);
659 * Well, when DMA is ADM3 engine(implied by <= UARTDM v1.3),
660 * we need RXSTALE to flush input DMA fifo to memory
662 if (msm_port
->is_uartdm
< UARTDM_1P4
)
663 msm_port
->imr
|= MSM_UART_IMR_RXSTALE
;
665 msm_write(uart
, msm_port
->imr
, MSM_UART_IMR
);
667 dma
->rx
.count
= UARTDM_RX_SIZE
;
669 dma_async_issue_pending(dma
->chan
);
671 msm_write(uart
, MSM_UART_CR_CMD_RESET_STALE_INT
, MSM_UART_CR
);
672 msm_write(uart
, MSM_UART_CR_CMD_STALE_EVENT_ENABLE
, MSM_UART_CR
);
674 val
= msm_read(uart
, UARTDM_DMEN
);
675 val
|= dma
->enable_bit
;
677 if (msm_port
->is_uartdm
< UARTDM_1P4
)
678 msm_write(uart
, val
, UARTDM_DMEN
);
680 msm_write(uart
, UARTDM_RX_SIZE
, UARTDM_DMRX
);
682 if (msm_port
->is_uartdm
> UARTDM_1P3
)
683 msm_write(uart
, val
, UARTDM_DMEN
);
687 dma_unmap_single(uart
->dev
, dma
->rx
.phys
, UARTDM_RX_SIZE
, dma
->dir
);
691 * Switch from DMA to SW/FIFO mode. After clearing Rx BAM (UARTDM_DMEN),
692 * receiver must be reset.
694 msm_write(uart
, MSM_UART_CR_CMD_RESET_RX
, MSM_UART_CR
);
695 msm_write(uart
, MSM_UART_CR_RX_ENABLE
, MSM_UART_CR
);
697 msm_write(uart
, MSM_UART_CR_CMD_RESET_STALE_INT
, MSM_UART_CR
);
698 msm_write(uart
, 0xFFFFFF, UARTDM_DMRX
);
699 msm_write(uart
, MSM_UART_CR_CMD_STALE_EVENT_ENABLE
, MSM_UART_CR
);
701 /* Re-enable RX interrupts */
702 msm_port
->imr
|= MSM_UART_IMR_RXLEV
| MSM_UART_IMR_RXSTALE
;
703 msm_write(uart
, msm_port
->imr
, MSM_UART_IMR
);
706 static void msm_stop_rx(struct uart_port
*port
)
708 struct msm_port
*msm_port
= to_msm_port(port
);
709 struct msm_dma
*dma
= &msm_port
->rx_dma
;
711 msm_port
->imr
&= ~(MSM_UART_IMR_RXLEV
| MSM_UART_IMR_RXSTALE
);
712 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
715 msm_stop_dma(port
, dma
);
718 static void msm_enable_ms(struct uart_port
*port
)
720 struct msm_port
*msm_port
= to_msm_port(port
);
722 msm_port
->imr
|= MSM_UART_IMR_DELTA_CTS
;
723 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
726 static void msm_handle_rx_dm(struct uart_port
*port
, unsigned int misr
)
727 __must_hold(&port
->lock
)
729 struct tty_port
*tport
= &port
->state
->port
;
732 struct msm_port
*msm_port
= to_msm_port(port
);
734 if ((msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_OVERRUN
)) {
735 port
->icount
.overrun
++;
736 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
737 msm_write(port
, MSM_UART_CR_CMD_RESET_ERR
, MSM_UART_CR
);
740 if (misr
& MSM_UART_IMR_RXSTALE
) {
741 count
= msm_read(port
, UARTDM_RX_TOTAL_SNAP
) -
742 msm_port
->old_snap_state
;
743 msm_port
->old_snap_state
= 0;
745 count
= 4 * (msm_read(port
, MSM_UART_RFWR
));
746 msm_port
->old_snap_state
+= count
;
749 /* TODO: Precise error reporting */
751 port
->icount
.rx
+= count
;
754 unsigned char buf
[4];
755 int sysrq
, r_count
, i
;
757 sr
= msm_read(port
, MSM_UART_SR
);
758 if ((sr
& MSM_UART_SR_RX_READY
) == 0) {
759 msm_port
->old_snap_state
-= count
;
763 ioread32_rep(port
->membase
+ UARTDM_RF
, buf
, 1);
764 r_count
= min_t(int, count
, sizeof(buf
));
766 for (i
= 0; i
< r_count
; i
++) {
767 char flag
= TTY_NORMAL
;
769 if (msm_port
->break_detected
&& buf
[i
] == 0) {
772 msm_port
->break_detected
= false;
773 if (uart_handle_break(port
))
777 if (!(port
->read_status_mask
& MSM_UART_SR_RX_BREAK
))
780 sysrq
= uart_prepare_sysrq_char(port
, buf
[i
]);
782 tty_insert_flip_char(tport
, buf
[i
], flag
);
787 tty_flip_buffer_push(tport
);
789 if (misr
& (MSM_UART_IMR_RXSTALE
))
790 msm_write(port
, MSM_UART_CR_CMD_RESET_STALE_INT
, MSM_UART_CR
);
791 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
792 msm_write(port
, MSM_UART_CR_CMD_STALE_EVENT_ENABLE
, MSM_UART_CR
);
795 msm_start_rx_dma(msm_port
);
798 static void msm_handle_rx(struct uart_port
*port
)
799 __must_hold(&port
->lock
)
801 struct tty_port
*tport
= &port
->state
->port
;
805 * Handle overrun. My understanding of the hardware is that overrun
806 * is not tied to the RX buffer, so we handle the case out of band.
808 if ((msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_OVERRUN
)) {
809 port
->icount
.overrun
++;
810 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
811 msm_write(port
, MSM_UART_CR_CMD_RESET_ERR
, MSM_UART_CR
);
814 /* and now the main RX loop */
815 while ((sr
= msm_read(port
, MSM_UART_SR
)) & MSM_UART_SR_RX_READY
) {
817 char flag
= TTY_NORMAL
;
820 c
= msm_read(port
, MSM_UART_RF
);
822 if (sr
& MSM_UART_SR_RX_BREAK
) {
824 if (uart_handle_break(port
))
826 } else if (sr
& MSM_UART_SR_PAR_FRAME_ERR
) {
827 port
->icount
.frame
++;
832 /* Mask conditions we're ignoring. */
833 sr
&= port
->read_status_mask
;
835 if (sr
& MSM_UART_SR_RX_BREAK
)
837 else if (sr
& MSM_UART_SR_PAR_FRAME_ERR
)
840 sysrq
= uart_prepare_sysrq_char(port
, c
);
842 tty_insert_flip_char(tport
, c
, flag
);
845 tty_flip_buffer_push(tport
);
848 static void msm_handle_tx_pio(struct uart_port
*port
, unsigned int tx_count
)
850 struct msm_port
*msm_port
= to_msm_port(port
);
851 struct tty_port
*tport
= &port
->state
->port
;
852 unsigned int num_chars
;
853 unsigned int tf_pointer
= 0;
856 if (msm_port
->is_uartdm
)
857 tf
= port
->membase
+ UARTDM_TF
;
859 tf
= port
->membase
+ MSM_UART_TF
;
861 if (tx_count
&& msm_port
->is_uartdm
)
862 msm_reset_dm_count(port
, tx_count
);
864 while (tf_pointer
< tx_count
) {
865 unsigned char buf
[4] = { 0 };
867 if (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_READY
))
870 if (msm_port
->is_uartdm
)
871 num_chars
= min(tx_count
- tf_pointer
,
872 (unsigned int)sizeof(buf
));
876 num_chars
= uart_fifo_out(port
, buf
, num_chars
);
877 iowrite32_rep(tf
, buf
, 1);
878 tf_pointer
+= num_chars
;
881 /* disable tx interrupts if nothing more to send */
882 if (kfifo_is_empty(&tport
->xmit_fifo
))
885 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
886 uart_write_wakeup(port
);
889 static void msm_handle_tx(struct uart_port
*port
)
891 struct msm_port
*msm_port
= to_msm_port(port
);
892 struct tty_port
*tport
= &port
->state
->port
;
893 struct msm_dma
*dma
= &msm_port
->tx_dma
;
894 unsigned int pio_count
, dma_count
, dma_min
;
900 if (msm_port
->is_uartdm
)
901 tf
= port
->membase
+ UARTDM_TF
;
903 tf
= port
->membase
+ MSM_UART_TF
;
905 buf
[0] = port
->x_char
;
907 if (msm_port
->is_uartdm
)
908 msm_reset_dm_count(port
, 1);
910 iowrite32_rep(tf
, buf
, 1);
916 if (kfifo_is_empty(&tport
->xmit_fifo
) || uart_tx_stopped(port
)) {
921 dma_count
= pio_count
= kfifo_out_linear(&tport
->xmit_fifo
, NULL
,
924 dma_min
= 1; /* Always DMA */
925 if (msm_port
->is_uartdm
> UARTDM_1P3
) {
926 dma_count
= UARTDM_TX_AIGN(dma_count
);
927 dma_min
= UARTDM_BURST_SIZE
;
929 if (dma_count
> UARTDM_TX_MAX
)
930 dma_count
= UARTDM_TX_MAX
;
933 if (pio_count
> port
->fifosize
)
934 pio_count
= port
->fifosize
;
936 if (!dma
->chan
|| dma_count
< dma_min
)
937 msm_handle_tx_pio(port
, pio_count
);
939 err
= msm_handle_tx_dma(msm_port
, dma_count
);
941 if (err
) /* fall back to PIO mode */
942 msm_handle_tx_pio(port
, pio_count
);
945 static void msm_handle_delta_cts(struct uart_port
*port
)
947 msm_write(port
, MSM_UART_CR_CMD_RESET_CTS
, MSM_UART_CR
);
949 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
952 static irqreturn_t
msm_uart_irq(int irq
, void *dev_id
)
954 struct uart_port
*port
= dev_id
;
955 struct msm_port
*msm_port
= to_msm_port(port
);
956 struct msm_dma
*dma
= &msm_port
->rx_dma
;
960 uart_port_lock(port
);
961 misr
= msm_read(port
, MSM_UART_MISR
);
962 msm_write(port
, 0, MSM_UART_IMR
); /* disable interrupt */
964 if (misr
& MSM_UART_IMR_RXBREAK_START
) {
965 msm_port
->break_detected
= true;
966 msm_write(port
, MSM_UART_CR_CMD_RESET_RXBREAK_START
, MSM_UART_CR
);
969 if (misr
& (MSM_UART_IMR_RXLEV
| MSM_UART_IMR_RXSTALE
)) {
971 val
= MSM_UART_CR_CMD_STALE_EVENT_DISABLE
;
972 msm_write(port
, val
, MSM_UART_CR
);
973 val
= MSM_UART_CR_CMD_RESET_STALE_INT
;
974 msm_write(port
, val
, MSM_UART_CR
);
976 * Flush DMA input fifo to memory, this will also
977 * trigger DMA RX completion
979 dmaengine_terminate_all(dma
->chan
);
980 } else if (msm_port
->is_uartdm
) {
981 msm_handle_rx_dm(port
, misr
);
986 if (misr
& MSM_UART_IMR_TXLEV
)
988 if (misr
& MSM_UART_IMR_DELTA_CTS
)
989 msm_handle_delta_cts(port
);
991 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
); /* restore interrupt */
992 uart_unlock_and_check_sysrq(port
);
997 static unsigned int msm_tx_empty(struct uart_port
*port
)
999 return (msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_EMPTY
) ? TIOCSER_TEMT
: 0;
1002 static unsigned int msm_get_mctrl(struct uart_port
*port
)
1004 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
| TIOCM_RTS
;
1007 static void msm_reset(struct uart_port
*port
)
1009 struct msm_port
*msm_port
= to_msm_port(port
);
1012 /* reset everything */
1013 msm_write(port
, MSM_UART_CR_CMD_RESET_RX
, MSM_UART_CR
);
1014 msm_write(port
, MSM_UART_CR_CMD_RESET_TX
, MSM_UART_CR
);
1015 msm_write(port
, MSM_UART_CR_CMD_RESET_ERR
, MSM_UART_CR
);
1016 msm_write(port
, MSM_UART_CR_CMD_RESET_BREAK_INT
, MSM_UART_CR
);
1017 msm_write(port
, MSM_UART_CR_CMD_RESET_CTS
, MSM_UART_CR
);
1018 msm_write(port
, MSM_UART_CR_CMD_RESET_RFR
, MSM_UART_CR
);
1019 mr
= msm_read(port
, MSM_UART_MR1
);
1020 mr
&= ~MSM_UART_MR1_RX_RDY_CTL
;
1021 msm_write(port
, mr
, MSM_UART_MR1
);
1023 /* Disable DM modes */
1024 if (msm_port
->is_uartdm
)
1025 msm_write(port
, 0, UARTDM_DMEN
);
1028 static void msm_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1032 mr
= msm_read(port
, MSM_UART_MR1
);
1034 if (!(mctrl
& TIOCM_RTS
)) {
1035 mr
&= ~MSM_UART_MR1_RX_RDY_CTL
;
1036 msm_write(port
, mr
, MSM_UART_MR1
);
1037 msm_write(port
, MSM_UART_CR_CMD_RESET_RFR
, MSM_UART_CR
);
1039 mr
|= MSM_UART_MR1_RX_RDY_CTL
;
1040 msm_write(port
, mr
, MSM_UART_MR1
);
1044 static void msm_break_ctl(struct uart_port
*port
, int break_ctl
)
1047 msm_write(port
, MSM_UART_CR_CMD_START_BREAK
, MSM_UART_CR
);
1049 msm_write(port
, MSM_UART_CR_CMD_STOP_BREAK
, MSM_UART_CR
);
1052 struct msm_baud_map
{
1058 static const struct msm_baud_map
*
1059 msm_find_best_baud(struct uart_port
*port
, unsigned int baud
,
1060 unsigned long *rate
)
1062 struct msm_port
*msm_port
= to_msm_port(port
);
1063 unsigned int divisor
, result
;
1064 unsigned long target
, old
, best_rate
= 0, diff
, best_diff
= ULONG_MAX
;
1065 const struct msm_baud_map
*entry
, *end
, *best
;
1066 static const struct msm_baud_map table
[] = {
1085 best
= table
; /* Default to smallest divider */
1086 target
= clk_round_rate(msm_port
->clk
, 16 * baud
);
1087 divisor
= DIV_ROUND_CLOSEST(target
, 16 * baud
);
1089 end
= table
+ ARRAY_SIZE(table
);
1091 while (entry
< end
) {
1092 if (entry
->divisor
<= divisor
) {
1093 result
= target
/ entry
->divisor
/ 16;
1094 diff
= abs(result
- baud
);
1096 /* Keep track of best entry */
1097 if (diff
< best_diff
) {
1105 } else if (entry
->divisor
> divisor
) {
1107 target
= clk_round_rate(msm_port
->clk
, old
+ 1);
1109 * The rate didn't get any faster so we can't do
1110 * better at dividing it down
1115 /* Start the divisor search over at this new rate */
1117 divisor
= DIV_ROUND_CLOSEST(target
, 16 * baud
);
1127 static int msm_set_baud_rate(struct uart_port
*port
, unsigned int baud
,
1128 unsigned long *saved_flags
)
1129 __must_hold(&port
->lock
)
1131 unsigned int rxstale
, watermark
, mask
;
1132 struct msm_port
*msm_port
= to_msm_port(port
);
1133 const struct msm_baud_map
*entry
;
1134 unsigned long flags
, rate
;
1136 flags
= *saved_flags
;
1137 uart_port_unlock_irqrestore(port
, flags
);
1139 entry
= msm_find_best_baud(port
, baud
, &rate
);
1140 dev_pm_opp_set_rate(port
->dev
, rate
);
1141 baud
= rate
/ 16 / entry
->divisor
;
1143 uart_port_lock_irqsave(port
, &flags
);
1144 *saved_flags
= flags
;
1145 port
->uartclk
= rate
;
1147 msm_write(port
, entry
->code
, MSM_UART_CSR
);
1149 /* RX stale watermark */
1150 rxstale
= entry
->rxstale
;
1151 watermark
= MSM_UART_IPR_STALE_LSB
& rxstale
;
1152 if (msm_port
->is_uartdm
) {
1153 mask
= MSM_UART_DM_IPR_STALE_TIMEOUT_MSB
;
1155 watermark
|= MSM_UART_IPR_RXSTALE_LAST
;
1156 mask
= MSM_UART_IPR_STALE_TIMEOUT_MSB
;
1159 watermark
|= mask
& (rxstale
<< 2);
1161 msm_write(port
, watermark
, MSM_UART_IPR
);
1163 /* set RX watermark */
1164 watermark
= (port
->fifosize
* 3) / 4;
1165 msm_write(port
, watermark
, MSM_UART_RFWR
);
1167 /* set TX watermark */
1168 msm_write(port
, 10, MSM_UART_TFWR
);
1170 msm_write(port
, MSM_UART_CR_CMD_PROTECTION_EN
, MSM_UART_CR
);
1173 /* Enable RX and TX */
1174 msm_write(port
, MSM_UART_CR_TX_ENABLE
| MSM_UART_CR_RX_ENABLE
, MSM_UART_CR
);
1176 /* turn on RX and CTS interrupts */
1177 msm_port
->imr
= MSM_UART_IMR_RXLEV
| MSM_UART_IMR_RXSTALE
|
1178 MSM_UART_IMR_CURRENT_CTS
| MSM_UART_IMR_RXBREAK_START
;
1180 msm_write(port
, msm_port
->imr
, MSM_UART_IMR
);
1182 if (msm_port
->is_uartdm
) {
1183 msm_write(port
, MSM_UART_CR_CMD_RESET_STALE_INT
, MSM_UART_CR
);
1184 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
1185 msm_write(port
, MSM_UART_CR_CMD_STALE_EVENT_ENABLE
, MSM_UART_CR
);
1191 static void msm_init_clock(struct uart_port
*port
)
1193 struct msm_port
*msm_port
= to_msm_port(port
);
1195 dev_pm_opp_set_rate(port
->dev
, port
->uartclk
);
1196 clk_prepare_enable(msm_port
->clk
);
1197 clk_prepare_enable(msm_port
->pclk
);
1198 msm_serial_set_mnd_regs(port
);
1201 static int msm_startup(struct uart_port
*port
)
1203 struct msm_port
*msm_port
= to_msm_port(port
);
1204 unsigned int data
, rfr_level
, mask
;
1207 snprintf(msm_port
->name
, sizeof(msm_port
->name
),
1208 "msm_serial%d", port
->line
);
1210 msm_init_clock(port
);
1212 if (likely(port
->fifosize
> 12))
1213 rfr_level
= port
->fifosize
- 12;
1215 rfr_level
= port
->fifosize
;
1217 /* set automatic RFR level */
1218 data
= msm_read(port
, MSM_UART_MR1
);
1220 if (msm_port
->is_uartdm
)
1221 mask
= MSM_UART_DM_MR1_AUTO_RFR_LEVEL1
;
1223 mask
= MSM_UART_MR1_AUTO_RFR_LEVEL1
;
1226 data
&= ~MSM_UART_MR1_AUTO_RFR_LEVEL0
;
1227 data
|= mask
& (rfr_level
<< 2);
1228 data
|= MSM_UART_MR1_AUTO_RFR_LEVEL0
& rfr_level
;
1229 msm_write(port
, data
, MSM_UART_MR1
);
1231 if (msm_port
->is_uartdm
) {
1232 msm_request_tx_dma(msm_port
, msm_port
->uart
.mapbase
);
1233 msm_request_rx_dma(msm_port
, msm_port
->uart
.mapbase
);
1236 ret
= request_irq(port
->irq
, msm_uart_irq
, IRQF_TRIGGER_HIGH
,
1237 msm_port
->name
, port
);
1244 if (msm_port
->is_uartdm
)
1245 msm_release_dma(msm_port
);
1247 clk_disable_unprepare(msm_port
->pclk
);
1248 clk_disable_unprepare(msm_port
->clk
);
1249 dev_pm_opp_set_rate(port
->dev
, 0);
1254 static void msm_shutdown(struct uart_port
*port
)
1256 struct msm_port
*msm_port
= to_msm_port(port
);
1259 msm_write(port
, 0, MSM_UART_IMR
); /* disable interrupts */
1261 if (msm_port
->is_uartdm
)
1262 msm_release_dma(msm_port
);
1264 clk_disable_unprepare(msm_port
->clk
);
1265 dev_pm_opp_set_rate(port
->dev
, 0);
1267 free_irq(port
->irq
, port
);
1270 static void msm_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1271 const struct ktermios
*old
)
1273 struct msm_port
*msm_port
= to_msm_port(port
);
1274 struct msm_dma
*dma
= &msm_port
->rx_dma
;
1275 unsigned long flags
;
1276 unsigned int baud
, mr
;
1278 uart_port_lock_irqsave(port
, &flags
);
1280 if (dma
->chan
) /* Terminate if any */
1281 msm_stop_dma(port
, dma
);
1283 /* calculate and set baud rate */
1284 baud
= uart_get_baud_rate(port
, termios
, old
, 300, 4000000);
1285 baud
= msm_set_baud_rate(port
, baud
, &flags
);
1286 if (tty_termios_baud_rate(termios
))
1287 tty_termios_encode_baud_rate(termios
, baud
, baud
);
1289 /* calculate parity */
1290 mr
= msm_read(port
, MSM_UART_MR2
);
1291 mr
&= ~MSM_UART_MR2_PARITY_MODE
;
1292 if (termios
->c_cflag
& PARENB
) {
1293 if (termios
->c_cflag
& PARODD
)
1294 mr
|= MSM_UART_MR2_PARITY_MODE_ODD
;
1295 else if (termios
->c_cflag
& CMSPAR
)
1296 mr
|= MSM_UART_MR2_PARITY_MODE_SPACE
;
1298 mr
|= MSM_UART_MR2_PARITY_MODE_EVEN
;
1301 /* calculate bits per char */
1302 mr
&= ~MSM_UART_MR2_BITS_PER_CHAR
;
1303 switch (termios
->c_cflag
& CSIZE
) {
1305 mr
|= MSM_UART_MR2_BITS_PER_CHAR_5
;
1308 mr
|= MSM_UART_MR2_BITS_PER_CHAR_6
;
1311 mr
|= MSM_UART_MR2_BITS_PER_CHAR_7
;
1315 mr
|= MSM_UART_MR2_BITS_PER_CHAR_8
;
1319 /* calculate stop bits */
1320 mr
&= ~(MSM_UART_MR2_STOP_BIT_LEN_ONE
| MSM_UART_MR2_STOP_BIT_LEN_TWO
);
1321 if (termios
->c_cflag
& CSTOPB
)
1322 mr
|= MSM_UART_MR2_STOP_BIT_LEN_TWO
;
1324 mr
|= MSM_UART_MR2_STOP_BIT_LEN_ONE
;
1326 /* set parity, bits per char, and stop bit */
1327 msm_write(port
, mr
, MSM_UART_MR2
);
1329 /* calculate and set hardware flow control */
1330 mr
= msm_read(port
, MSM_UART_MR1
);
1331 mr
&= ~(MSM_UART_MR1_CTS_CTL
| MSM_UART_MR1_RX_RDY_CTL
);
1332 if (termios
->c_cflag
& CRTSCTS
) {
1333 mr
|= MSM_UART_MR1_CTS_CTL
;
1334 mr
|= MSM_UART_MR1_RX_RDY_CTL
;
1336 msm_write(port
, mr
, MSM_UART_MR1
);
1338 /* Configure status bits to ignore based on termio flags. */
1339 port
->read_status_mask
= 0;
1340 if (termios
->c_iflag
& INPCK
)
1341 port
->read_status_mask
|= MSM_UART_SR_PAR_FRAME_ERR
;
1342 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1343 port
->read_status_mask
|= MSM_UART_SR_RX_BREAK
;
1345 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1347 /* Try to use DMA */
1348 msm_start_rx_dma(msm_port
);
1350 uart_port_unlock_irqrestore(port
, flags
);
1353 static const char *msm_type(struct uart_port
*port
)
1358 static void msm_release_port(struct uart_port
*port
)
1360 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1361 struct resource
*uart_resource
;
1362 resource_size_t size
;
1364 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1365 if (unlikely(!uart_resource
))
1367 size
= resource_size(uart_resource
);
1369 release_mem_region(port
->mapbase
, size
);
1370 iounmap(port
->membase
);
1371 port
->membase
= NULL
;
1374 static int msm_request_port(struct uart_port
*port
)
1376 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1377 struct resource
*uart_resource
;
1378 resource_size_t size
;
1381 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1382 if (unlikely(!uart_resource
))
1385 size
= resource_size(uart_resource
);
1387 if (!request_mem_region(port
->mapbase
, size
, "msm_serial"))
1390 port
->membase
= ioremap(port
->mapbase
, size
);
1391 if (!port
->membase
) {
1393 goto fail_release_port
;
1399 release_mem_region(port
->mapbase
, size
);
1403 static void msm_config_port(struct uart_port
*port
, int flags
)
1407 if (flags
& UART_CONFIG_TYPE
) {
1408 port
->type
= PORT_MSM
;
1409 ret
= msm_request_port(port
);
1415 static int msm_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1417 if (unlikely(ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_MSM
))
1419 if (unlikely(port
->irq
!= ser
->irq
))
1424 static void msm_power(struct uart_port
*port
, unsigned int state
,
1425 unsigned int oldstate
)
1427 struct msm_port
*msm_port
= to_msm_port(port
);
1431 dev_pm_opp_set_rate(port
->dev
, port
->uartclk
);
1432 clk_prepare_enable(msm_port
->clk
);
1433 clk_prepare_enable(msm_port
->pclk
);
1436 clk_disable_unprepare(msm_port
->clk
);
1437 dev_pm_opp_set_rate(port
->dev
, 0);
1438 clk_disable_unprepare(msm_port
->pclk
);
1441 pr_err("msm_serial: Unknown PM state %d\n", state
);
1445 #ifdef CONFIG_CONSOLE_POLL
1446 static int msm_poll_get_char_single(struct uart_port
*port
)
1448 struct msm_port
*msm_port
= to_msm_port(port
);
1449 unsigned int rf_reg
= msm_port
->is_uartdm
? UARTDM_RF
: MSM_UART_RF
;
1451 if (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_RX_READY
))
1452 return NO_POLL_CHAR
;
1454 return msm_read(port
, rf_reg
) & 0xff;
1457 static int msm_poll_get_char_dm(struct uart_port
*port
)
1462 unsigned char *sp
= (unsigned char *)&slop
;
1464 /* Check if a previous read had more than one char */
1466 c
= sp
[sizeof(slop
) - count
];
1468 /* Or if FIFO is empty */
1469 } else if (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_RX_READY
)) {
1471 * If RX packing buffer has less than a word, force stale to
1472 * push contents into RX FIFO
1474 count
= msm_read(port
, UARTDM_RXFS
);
1475 count
= (count
>> UARTDM_RXFS_BUF_SHIFT
) & UARTDM_RXFS_BUF_MASK
;
1477 msm_write(port
, MSM_UART_CR_CMD_FORCE_STALE
, MSM_UART_CR
);
1478 slop
= msm_read(port
, UARTDM_RF
);
1481 msm_write(port
, MSM_UART_CR_CMD_RESET_STALE_INT
, MSM_UART_CR
);
1482 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
1483 msm_write(port
, MSM_UART_CR_CMD_STALE_EVENT_ENABLE
, MSM_UART_CR
);
1487 /* FIFO has a word */
1489 slop
= msm_read(port
, UARTDM_RF
);
1491 count
= sizeof(slop
) - 1;
1497 static int msm_poll_get_char(struct uart_port
*port
)
1501 struct msm_port
*msm_port
= to_msm_port(port
);
1503 /* Disable all interrupts */
1504 imr
= msm_read(port
, MSM_UART_IMR
);
1505 msm_write(port
, 0, MSM_UART_IMR
);
1507 if (msm_port
->is_uartdm
)
1508 c
= msm_poll_get_char_dm(port
);
1510 c
= msm_poll_get_char_single(port
);
1512 /* Enable interrupts */
1513 msm_write(port
, imr
, MSM_UART_IMR
);
1518 static void msm_poll_put_char(struct uart_port
*port
, unsigned char c
)
1521 struct msm_port
*msm_port
= to_msm_port(port
);
1523 /* Disable all interrupts */
1524 imr
= msm_read(port
, MSM_UART_IMR
);
1525 msm_write(port
, 0, MSM_UART_IMR
);
1527 if (msm_port
->is_uartdm
)
1528 msm_reset_dm_count(port
, 1);
1530 /* Wait until FIFO is empty */
1531 while (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_READY
))
1534 /* Write a character */
1535 msm_write(port
, c
, msm_port
->is_uartdm
? UARTDM_TF
: MSM_UART_TF
);
1537 /* Wait until FIFO is empty */
1538 while (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_READY
))
1541 /* Enable interrupts */
1542 msm_write(port
, imr
, MSM_UART_IMR
);
1546 static const struct uart_ops msm_uart_pops
= {
1547 .tx_empty
= msm_tx_empty
,
1548 .set_mctrl
= msm_set_mctrl
,
1549 .get_mctrl
= msm_get_mctrl
,
1550 .stop_tx
= msm_stop_tx
,
1551 .start_tx
= msm_start_tx
,
1552 .stop_rx
= msm_stop_rx
,
1553 .enable_ms
= msm_enable_ms
,
1554 .break_ctl
= msm_break_ctl
,
1555 .startup
= msm_startup
,
1556 .shutdown
= msm_shutdown
,
1557 .set_termios
= msm_set_termios
,
1559 .release_port
= msm_release_port
,
1560 .request_port
= msm_request_port
,
1561 .config_port
= msm_config_port
,
1562 .verify_port
= msm_verify_port
,
1564 #ifdef CONFIG_CONSOLE_POLL
1565 .poll_get_char
= msm_poll_get_char
,
1566 .poll_put_char
= msm_poll_put_char
,
1570 static struct msm_port msm_uart_ports
[] = {
1574 .ops
= &msm_uart_pops
,
1575 .flags
= UPF_BOOT_AUTOCONF
,
1583 .ops
= &msm_uart_pops
,
1584 .flags
= UPF_BOOT_AUTOCONF
,
1592 .ops
= &msm_uart_pops
,
1593 .flags
= UPF_BOOT_AUTOCONF
,
1600 #define MSM_UART_NR ARRAY_SIZE(msm_uart_ports)
1602 static inline struct uart_port
*msm_get_port_from_line(unsigned int line
)
1604 return &msm_uart_ports
[line
].uart
;
1607 #ifdef CONFIG_SERIAL_MSM_CONSOLE
1608 static void __msm_console_write(struct uart_port
*port
, const char *s
,
1609 unsigned int count
, bool is_uartdm
)
1611 unsigned long flags
;
1613 int num_newlines
= 0;
1614 bool replaced
= false;
1619 tf
= port
->membase
+ UARTDM_TF
;
1621 tf
= port
->membase
+ MSM_UART_TF
;
1623 /* Account for newlines that will get a carriage return added */
1624 for (i
= 0; i
< count
; i
++)
1627 count
+= num_newlines
;
1629 if (oops_in_progress
)
1630 locked
= uart_port_trylock_irqsave(port
, &flags
);
1632 uart_port_lock_irqsave(port
, &flags
);
1635 msm_reset_dm_count(port
, count
);
1640 unsigned int num_chars
;
1641 char buf
[4] = { 0 };
1644 num_chars
= min(count
- i
, (unsigned int)sizeof(buf
));
1648 for (j
= 0; j
< num_chars
; j
++) {
1651 if (c
== '\n' && !replaced
) {
1656 if (j
< num_chars
) {
1663 while (!(msm_read(port
, MSM_UART_SR
) & MSM_UART_SR_TX_READY
))
1666 iowrite32_rep(tf
, buf
, 1);
1671 uart_port_unlock_irqrestore(port
, flags
);
1674 static void msm_console_write(struct console
*co
, const char *s
,
1677 struct uart_port
*port
;
1678 struct msm_port
*msm_port
;
1680 BUG_ON(co
->index
< 0 || co
->index
>= MSM_UART_NR
);
1682 port
= msm_get_port_from_line(co
->index
);
1683 msm_port
= to_msm_port(port
);
1685 __msm_console_write(port
, s
, count
, msm_port
->is_uartdm
);
1688 static int msm_console_setup(struct console
*co
, char *options
)
1690 struct uart_port
*port
;
1696 if (unlikely(co
->index
>= MSM_UART_NR
|| co
->index
< 0))
1699 port
= msm_get_port_from_line(co
->index
);
1701 if (unlikely(!port
->membase
))
1704 msm_init_clock(port
);
1707 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1709 pr_info("msm_serial: console setup on port #%d\n", port
->line
);
1711 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1715 msm_serial_early_write(struct console
*con
, const char *s
, unsigned n
)
1717 struct earlycon_device
*dev
= con
->data
;
1719 __msm_console_write(&dev
->port
, s
, n
, false);
1723 msm_serial_early_console_setup(struct earlycon_device
*device
, const char *opt
)
1725 if (!device
->port
.membase
)
1728 device
->con
->write
= msm_serial_early_write
;
1731 OF_EARLYCON_DECLARE(msm_serial
, "qcom,msm-uart",
1732 msm_serial_early_console_setup
);
1735 msm_serial_early_write_dm(struct console
*con
, const char *s
, unsigned n
)
1737 struct earlycon_device
*dev
= con
->data
;
1739 __msm_console_write(&dev
->port
, s
, n
, true);
1743 msm_serial_early_console_setup_dm(struct earlycon_device
*device
,
1746 if (!device
->port
.membase
)
1749 device
->con
->write
= msm_serial_early_write_dm
;
1752 OF_EARLYCON_DECLARE(msm_serial_dm
, "qcom,msm-uartdm",
1753 msm_serial_early_console_setup_dm
);
1755 static struct uart_driver msm_uart_driver
;
1757 static struct console msm_console
= {
1759 .write
= msm_console_write
,
1760 .device
= uart_console_device
,
1761 .setup
= msm_console_setup
,
1762 .flags
= CON_PRINTBUFFER
,
1764 .data
= &msm_uart_driver
,
1767 #define MSM_CONSOLE (&msm_console)
1770 #define MSM_CONSOLE NULL
1773 static struct uart_driver msm_uart_driver
= {
1774 .owner
= THIS_MODULE
,
1775 .driver_name
= "msm_serial",
1776 .dev_name
= "ttyMSM",
1778 .cons
= MSM_CONSOLE
,
1781 static atomic_t msm_uart_next_id
= ATOMIC_INIT(0);
1783 static const struct of_device_id msm_uartdm_table
[] = {
1784 { .compatible
= "qcom,msm-uartdm-v1.1", .data
= (void *)UARTDM_1P1
},
1785 { .compatible
= "qcom,msm-uartdm-v1.2", .data
= (void *)UARTDM_1P2
},
1786 { .compatible
= "qcom,msm-uartdm-v1.3", .data
= (void *)UARTDM_1P3
},
1787 { .compatible
= "qcom,msm-uartdm-v1.4", .data
= (void *)UARTDM_1P4
},
1791 static int msm_serial_probe(struct platform_device
*pdev
)
1793 struct msm_port
*msm_port
;
1794 struct resource
*resource
;
1795 struct uart_port
*port
;
1796 const struct of_device_id
*id
;
1799 if (pdev
->dev
.of_node
)
1800 line
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
1805 line
= atomic_inc_return(&msm_uart_next_id
) - 1;
1807 if (unlikely(line
< 0 || line
>= MSM_UART_NR
))
1810 dev_info(&pdev
->dev
, "msm_serial: detected port #%d\n", line
);
1812 port
= msm_get_port_from_line(line
);
1813 port
->dev
= &pdev
->dev
;
1814 msm_port
= to_msm_port(port
);
1816 id
= of_match_device(msm_uartdm_table
, &pdev
->dev
);
1818 msm_port
->is_uartdm
= (unsigned long)id
->data
;
1820 msm_port
->is_uartdm
= 0;
1822 msm_port
->clk
= devm_clk_get(&pdev
->dev
, "core");
1823 if (IS_ERR(msm_port
->clk
))
1824 return PTR_ERR(msm_port
->clk
);
1826 if (msm_port
->is_uartdm
) {
1827 msm_port
->pclk
= devm_clk_get(&pdev
->dev
, "iface");
1828 if (IS_ERR(msm_port
->pclk
))
1829 return PTR_ERR(msm_port
->pclk
);
1832 ret
= devm_pm_opp_set_clkname(&pdev
->dev
, "core");
1836 /* OPP table is optional */
1837 ret
= devm_pm_opp_of_add_table(&pdev
->dev
);
1838 if (ret
&& ret
!= -ENODEV
)
1839 return dev_err_probe(&pdev
->dev
, ret
, "invalid OPP table\n");
1841 port
->uartclk
= clk_get_rate(msm_port
->clk
);
1842 dev_info(&pdev
->dev
, "uartclk = %d\n", port
->uartclk
);
1844 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1845 if (unlikely(!resource
))
1847 port
->mapbase
= resource
->start
;
1849 irq
= platform_get_irq(pdev
, 0);
1850 if (unlikely(irq
< 0))
1853 port
->has_sysrq
= IS_ENABLED(CONFIG_SERIAL_MSM_CONSOLE
);
1855 platform_set_drvdata(pdev
, port
);
1857 return uart_add_one_port(&msm_uart_driver
, port
);
1860 static void msm_serial_remove(struct platform_device
*pdev
)
1862 struct uart_port
*port
= platform_get_drvdata(pdev
);
1864 uart_remove_one_port(&msm_uart_driver
, port
);
1867 static const struct of_device_id msm_match_table
[] = {
1868 { .compatible
= "qcom,msm-uart" },
1869 { .compatible
= "qcom,msm-uartdm" },
1872 MODULE_DEVICE_TABLE(of
, msm_match_table
);
1874 static int __maybe_unused
msm_serial_suspend(struct device
*dev
)
1876 struct msm_port
*port
= dev_get_drvdata(dev
);
1878 uart_suspend_port(&msm_uart_driver
, &port
->uart
);
1883 static int __maybe_unused
msm_serial_resume(struct device
*dev
)
1885 struct msm_port
*port
= dev_get_drvdata(dev
);
1887 uart_resume_port(&msm_uart_driver
, &port
->uart
);
1892 static const struct dev_pm_ops msm_serial_dev_pm_ops
= {
1893 SET_SYSTEM_SLEEP_PM_OPS(msm_serial_suspend
, msm_serial_resume
)
1896 static struct platform_driver msm_platform_driver
= {
1897 .remove
= msm_serial_remove
,
1898 .probe
= msm_serial_probe
,
1900 .name
= "msm_serial",
1901 .pm
= &msm_serial_dev_pm_ops
,
1902 .of_match_table
= msm_match_table
,
1906 static int __init
msm_serial_init(void)
1910 ret
= uart_register_driver(&msm_uart_driver
);
1914 ret
= platform_driver_register(&msm_platform_driver
);
1916 uart_unregister_driver(&msm_uart_driver
);
1918 pr_info("msm_serial: driver initialized\n");
1923 static void __exit
msm_serial_exit(void)
1925 platform_driver_unregister(&msm_platform_driver
);
1926 uart_unregister_driver(&msm_uart_driver
);
1929 module_init(msm_serial_init
);
1930 module_exit(msm_serial_exit
);
1932 MODULE_AUTHOR("Robert Love <rlove@google.com>");
1933 MODULE_DESCRIPTION("Driver for msm7x serial device");
1934 MODULE_LICENSE("GPL");