2 * Driver for msm7k serial device and console
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
6 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19 # define SUPPORT_SYSRQ
22 #include <linux/atomic.h>
23 #include <linux/hrtimer.h>
24 #include <linux/module.h>
26 #include <linux/ioport.h>
27 #include <linux/irq.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/serial_core.h>
33 #include <linux/serial.h>
34 #include <linux/clk.h>
35 #include <linux/platform_device.h>
36 #include <linux/delay.h>
38 #include <linux/of_device.h>
40 #include "msm_serial.h"
50 struct uart_port uart
;
56 unsigned int old_snap_state
;
60 static inline void wait_for_xmitr(struct uart_port
*port
)
62 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_EMPTY
)) {
63 if (msm_read(port
, UART_ISR
) & UART_ISR_TX_READY
)
67 msm_write(port
, UART_CR_CMD_RESET_TX_READY
, UART_CR
);
70 static void msm_stop_tx(struct uart_port
*port
)
72 struct msm_port
*msm_port
= UART_TO_MSM(port
);
74 msm_port
->imr
&= ~UART_IMR_TXLEV
;
75 msm_write(port
, msm_port
->imr
, UART_IMR
);
78 static void msm_start_tx(struct uart_port
*port
)
80 struct msm_port
*msm_port
= UART_TO_MSM(port
);
82 msm_port
->imr
|= UART_IMR_TXLEV
;
83 msm_write(port
, msm_port
->imr
, UART_IMR
);
86 static void msm_stop_rx(struct uart_port
*port
)
88 struct msm_port
*msm_port
= UART_TO_MSM(port
);
90 msm_port
->imr
&= ~(UART_IMR_RXLEV
| UART_IMR_RXSTALE
);
91 msm_write(port
, msm_port
->imr
, UART_IMR
);
94 static void msm_enable_ms(struct uart_port
*port
)
96 struct msm_port
*msm_port
= UART_TO_MSM(port
);
98 msm_port
->imr
|= UART_IMR_DELTA_CTS
;
99 msm_write(port
, msm_port
->imr
, UART_IMR
);
102 static void handle_rx_dm(struct uart_port
*port
, unsigned int misr
)
104 struct tty_port
*tport
= &port
->state
->port
;
107 struct msm_port
*msm_port
= UART_TO_MSM(port
);
109 if ((msm_read(port
, UART_SR
) & UART_SR_OVERRUN
)) {
110 port
->icount
.overrun
++;
111 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
112 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
115 if (misr
& UART_IMR_RXSTALE
) {
116 count
= msm_read(port
, UARTDM_RX_TOTAL_SNAP
) -
117 msm_port
->old_snap_state
;
118 msm_port
->old_snap_state
= 0;
120 count
= 4 * (msm_read(port
, UART_RFWR
));
121 msm_port
->old_snap_state
+= count
;
124 /* TODO: Precise error reporting */
126 port
->icount
.rx
+= count
;
129 unsigned char buf
[4];
130 int sysrq
, r_count
, i
;
132 sr
= msm_read(port
, UART_SR
);
133 if ((sr
& UART_SR_RX_READY
) == 0) {
134 msm_port
->old_snap_state
-= count
;
138 ioread32_rep(port
->membase
+ UARTDM_RF
, buf
, 1);
139 r_count
= min_t(int, count
, sizeof(buf
));
141 for (i
= 0; i
< r_count
; i
++) {
142 char flag
= TTY_NORMAL
;
144 if (msm_port
->break_detected
&& buf
[i
] == 0) {
147 msm_port
->break_detected
= false;
148 if (uart_handle_break(port
))
152 if (!(port
->read_status_mask
& UART_SR_RX_BREAK
))
155 spin_unlock(&port
->lock
);
156 sysrq
= uart_handle_sysrq_char(port
, buf
[i
]);
157 spin_lock(&port
->lock
);
159 tty_insert_flip_char(tport
, buf
[i
], flag
);
164 spin_unlock(&port
->lock
);
165 tty_flip_buffer_push(tport
);
166 spin_lock(&port
->lock
);
168 if (misr
& (UART_IMR_RXSTALE
))
169 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
170 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
171 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
, UART_CR
);
174 static void handle_rx(struct uart_port
*port
)
176 struct tty_port
*tport
= &port
->state
->port
;
180 * Handle overrun. My understanding of the hardware is that overrun
181 * is not tied to the RX buffer, so we handle the case out of band.
183 if ((msm_read(port
, UART_SR
) & UART_SR_OVERRUN
)) {
184 port
->icount
.overrun
++;
185 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
186 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
189 /* and now the main RX loop */
190 while ((sr
= msm_read(port
, UART_SR
)) & UART_SR_RX_READY
) {
192 char flag
= TTY_NORMAL
;
195 c
= msm_read(port
, UART_RF
);
197 if (sr
& UART_SR_RX_BREAK
) {
199 if (uart_handle_break(port
))
201 } else if (sr
& UART_SR_PAR_FRAME_ERR
) {
202 port
->icount
.frame
++;
207 /* Mask conditions we're ignorning. */
208 sr
&= port
->read_status_mask
;
210 if (sr
& UART_SR_RX_BREAK
)
212 else if (sr
& UART_SR_PAR_FRAME_ERR
)
215 spin_unlock(&port
->lock
);
216 sysrq
= uart_handle_sysrq_char(port
, c
);
217 spin_lock(&port
->lock
);
219 tty_insert_flip_char(tport
, c
, flag
);
222 spin_unlock(&port
->lock
);
223 tty_flip_buffer_push(tport
);
224 spin_lock(&port
->lock
);
227 static void reset_dm_count(struct uart_port
*port
, int count
)
229 wait_for_xmitr(port
);
230 msm_write(port
, count
, UARTDM_NCF_TX
);
231 msm_read(port
, UARTDM_NCF_TX
);
234 static void handle_tx(struct uart_port
*port
)
236 struct circ_buf
*xmit
= &port
->state
->xmit
;
237 struct msm_port
*msm_port
= UART_TO_MSM(port
);
238 unsigned int tx_count
, num_chars
;
239 unsigned int tf_pointer
= 0;
242 if (msm_port
->is_uartdm
)
243 tf
= port
->membase
+ UARTDM_TF
;
245 tf
= port
->membase
+ UART_TF
;
247 tx_count
= uart_circ_chars_pending(xmit
);
248 tx_count
= min3(tx_count
, (unsigned int)UART_XMIT_SIZE
- xmit
->tail
,
252 if (msm_port
->is_uartdm
)
253 reset_dm_count(port
, tx_count
+ 1);
255 iowrite8_rep(tf
, &port
->x_char
, 1);
258 } else if (tx_count
&& msm_port
->is_uartdm
) {
259 reset_dm_count(port
, tx_count
);
262 while (tf_pointer
< tx_count
) {
266 if (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
269 if (msm_port
->is_uartdm
)
270 num_chars
= min(tx_count
- tf_pointer
,
271 (unsigned int)sizeof(buf
));
275 for (i
= 0; i
< num_chars
; i
++) {
276 buf
[i
] = xmit
->buf
[xmit
->tail
+ i
];
280 iowrite32_rep(tf
, buf
, 1);
281 xmit
->tail
= (xmit
->tail
+ num_chars
) & (UART_XMIT_SIZE
- 1);
282 tf_pointer
+= num_chars
;
285 /* disable tx interrupts if nothing more to send */
286 if (uart_circ_empty(xmit
))
289 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
290 uart_write_wakeup(port
);
293 static void handle_delta_cts(struct uart_port
*port
)
295 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
297 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
300 static irqreturn_t
msm_irq(int irq
, void *dev_id
)
302 struct uart_port
*port
= dev_id
;
303 struct msm_port
*msm_port
= UART_TO_MSM(port
);
306 spin_lock(&port
->lock
);
307 misr
= msm_read(port
, UART_MISR
);
308 msm_write(port
, 0, UART_IMR
); /* disable interrupt */
310 if (misr
& UART_IMR_RXBREAK_START
) {
311 msm_port
->break_detected
= true;
312 msm_write(port
, UART_CR_CMD_RESET_RXBREAK_START
, UART_CR
);
315 if (misr
& (UART_IMR_RXLEV
| UART_IMR_RXSTALE
)) {
316 if (msm_port
->is_uartdm
)
317 handle_rx_dm(port
, misr
);
321 if (misr
& UART_IMR_TXLEV
)
323 if (misr
& UART_IMR_DELTA_CTS
)
324 handle_delta_cts(port
);
326 msm_write(port
, msm_port
->imr
, UART_IMR
); /* restore interrupt */
327 spin_unlock(&port
->lock
);
332 static unsigned int msm_tx_empty(struct uart_port
*port
)
334 return (msm_read(port
, UART_SR
) & UART_SR_TX_EMPTY
) ? TIOCSER_TEMT
: 0;
337 static unsigned int msm_get_mctrl(struct uart_port
*port
)
339 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
| TIOCM_RTS
;
342 static void msm_reset(struct uart_port
*port
)
344 struct msm_port
*msm_port
= UART_TO_MSM(port
);
346 /* reset everything */
347 msm_write(port
, UART_CR_CMD_RESET_RX
, UART_CR
);
348 msm_write(port
, UART_CR_CMD_RESET_TX
, UART_CR
);
349 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
350 msm_write(port
, UART_CR_CMD_RESET_BREAK_INT
, UART_CR
);
351 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
352 msm_write(port
, UART_CR_CMD_SET_RFR
, UART_CR
);
354 /* Disable DM modes */
355 if (msm_port
->is_uartdm
)
356 msm_write(port
, 0, UARTDM_DMEN
);
359 static void msm_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
363 mr
= msm_read(port
, UART_MR1
);
365 if (!(mctrl
& TIOCM_RTS
)) {
366 mr
&= ~UART_MR1_RX_RDY_CTL
;
367 msm_write(port
, mr
, UART_MR1
);
368 msm_write(port
, UART_CR_CMD_RESET_RFR
, UART_CR
);
370 mr
|= UART_MR1_RX_RDY_CTL
;
371 msm_write(port
, mr
, UART_MR1
);
375 static void msm_break_ctl(struct uart_port
*port
, int break_ctl
)
378 msm_write(port
, UART_CR_CMD_START_BREAK
, UART_CR
);
380 msm_write(port
, UART_CR_CMD_STOP_BREAK
, UART_CR
);
383 struct msm_baud_map
{
389 static const struct msm_baud_map
*
390 msm_find_best_baud(struct uart_port
*port
, unsigned int baud
)
392 unsigned int i
, divisor
;
393 const struct msm_baud_map
*entry
;
394 static const struct msm_baud_map table
[] = {
413 divisor
= uart_get_divisor(port
, baud
);
415 for (i
= 0, entry
= table
; i
< ARRAY_SIZE(table
); i
++, entry
++)
416 if (entry
->divisor
<= divisor
)
419 return entry
; /* Default to smallest divider */
422 static int msm_set_baud_rate(struct uart_port
*port
, unsigned int baud
)
424 unsigned int rxstale
, watermark
;
425 struct msm_port
*msm_port
= UART_TO_MSM(port
);
426 const struct msm_baud_map
*entry
;
428 entry
= msm_find_best_baud(port
, baud
);
430 msm_write(port
, entry
->code
, UART_CSR
);
432 /* RX stale watermark */
433 rxstale
= entry
->rxstale
;
434 watermark
= UART_IPR_STALE_LSB
& rxstale
;
435 watermark
|= UART_IPR_RXSTALE_LAST
;
436 watermark
|= UART_IPR_STALE_TIMEOUT_MSB
& (rxstale
<< 2);
437 msm_write(port
, watermark
, UART_IPR
);
439 /* set RX watermark */
440 watermark
= (port
->fifosize
* 3) / 4;
441 msm_write(port
, watermark
, UART_RFWR
);
443 /* set TX watermark */
444 msm_write(port
, 10, UART_TFWR
);
446 msm_write(port
, UART_CR_CMD_PROTECTION_EN
, UART_CR
);
449 /* Enable RX and TX */
450 msm_write(port
, UART_CR_TX_ENABLE
| UART_CR_RX_ENABLE
, UART_CR
);
452 /* turn on RX and CTS interrupts */
453 msm_port
->imr
= UART_IMR_RXLEV
| UART_IMR_RXSTALE
|
454 UART_IMR_CURRENT_CTS
| UART_IMR_RXBREAK_START
;
456 msm_write(port
, msm_port
->imr
, UART_IMR
);
458 if (msm_port
->is_uartdm
) {
459 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
460 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
461 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
, UART_CR
);
467 static void msm_init_clock(struct uart_port
*port
)
469 struct msm_port
*msm_port
= UART_TO_MSM(port
);
471 clk_prepare_enable(msm_port
->clk
);
472 clk_prepare_enable(msm_port
->pclk
);
473 msm_serial_set_mnd_regs(port
);
476 static int msm_startup(struct uart_port
*port
)
478 struct msm_port
*msm_port
= UART_TO_MSM(port
);
479 unsigned int data
, rfr_level
;
482 snprintf(msm_port
->name
, sizeof(msm_port
->name
),
483 "msm_serial%d", port
->line
);
485 ret
= request_irq(port
->irq
, msm_irq
, IRQF_TRIGGER_HIGH
,
486 msm_port
->name
, port
);
490 msm_init_clock(port
);
492 if (likely(port
->fifosize
> 12))
493 rfr_level
= port
->fifosize
- 12;
495 rfr_level
= port
->fifosize
;
497 /* set automatic RFR level */
498 data
= msm_read(port
, UART_MR1
);
499 data
&= ~UART_MR1_AUTO_RFR_LEVEL1
;
500 data
&= ~UART_MR1_AUTO_RFR_LEVEL0
;
501 data
|= UART_MR1_AUTO_RFR_LEVEL1
& (rfr_level
<< 2);
502 data
|= UART_MR1_AUTO_RFR_LEVEL0
& rfr_level
;
503 msm_write(port
, data
, UART_MR1
);
507 static void msm_shutdown(struct uart_port
*port
)
509 struct msm_port
*msm_port
= UART_TO_MSM(port
);
512 msm_write(port
, 0, UART_IMR
); /* disable interrupts */
514 clk_disable_unprepare(msm_port
->clk
);
516 free_irq(port
->irq
, port
);
519 static void msm_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
520 struct ktermios
*old
)
523 unsigned int baud
, mr
;
525 spin_lock_irqsave(&port
->lock
, flags
);
527 /* calculate and set baud rate */
528 baud
= uart_get_baud_rate(port
, termios
, old
, 300, 115200);
529 baud
= msm_set_baud_rate(port
, baud
);
530 if (tty_termios_baud_rate(termios
))
531 tty_termios_encode_baud_rate(termios
, baud
, baud
);
533 /* calculate parity */
534 mr
= msm_read(port
, UART_MR2
);
535 mr
&= ~UART_MR2_PARITY_MODE
;
536 if (termios
->c_cflag
& PARENB
) {
537 if (termios
->c_cflag
& PARODD
)
538 mr
|= UART_MR2_PARITY_MODE_ODD
;
539 else if (termios
->c_cflag
& CMSPAR
)
540 mr
|= UART_MR2_PARITY_MODE_SPACE
;
542 mr
|= UART_MR2_PARITY_MODE_EVEN
;
545 /* calculate bits per char */
546 mr
&= ~UART_MR2_BITS_PER_CHAR
;
547 switch (termios
->c_cflag
& CSIZE
) {
549 mr
|= UART_MR2_BITS_PER_CHAR_5
;
552 mr
|= UART_MR2_BITS_PER_CHAR_6
;
555 mr
|= UART_MR2_BITS_PER_CHAR_7
;
559 mr
|= UART_MR2_BITS_PER_CHAR_8
;
563 /* calculate stop bits */
564 mr
&= ~(UART_MR2_STOP_BIT_LEN_ONE
| UART_MR2_STOP_BIT_LEN_TWO
);
565 if (termios
->c_cflag
& CSTOPB
)
566 mr
|= UART_MR2_STOP_BIT_LEN_TWO
;
568 mr
|= UART_MR2_STOP_BIT_LEN_ONE
;
570 /* set parity, bits per char, and stop bit */
571 msm_write(port
, mr
, UART_MR2
);
573 /* calculate and set hardware flow control */
574 mr
= msm_read(port
, UART_MR1
);
575 mr
&= ~(UART_MR1_CTS_CTL
| UART_MR1_RX_RDY_CTL
);
576 if (termios
->c_cflag
& CRTSCTS
) {
577 mr
|= UART_MR1_CTS_CTL
;
578 mr
|= UART_MR1_RX_RDY_CTL
;
580 msm_write(port
, mr
, UART_MR1
);
582 /* Configure status bits to ignore based on termio flags. */
583 port
->read_status_mask
= 0;
584 if (termios
->c_iflag
& INPCK
)
585 port
->read_status_mask
|= UART_SR_PAR_FRAME_ERR
;
586 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
587 port
->read_status_mask
|= UART_SR_RX_BREAK
;
589 uart_update_timeout(port
, termios
->c_cflag
, baud
);
591 spin_unlock_irqrestore(&port
->lock
, flags
);
594 static const char *msm_type(struct uart_port
*port
)
599 static void msm_release_port(struct uart_port
*port
)
601 struct platform_device
*pdev
= to_platform_device(port
->dev
);
602 struct resource
*uart_resource
;
603 resource_size_t size
;
605 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
606 if (unlikely(!uart_resource
))
608 size
= resource_size(uart_resource
);
610 release_mem_region(port
->mapbase
, size
);
611 iounmap(port
->membase
);
612 port
->membase
= NULL
;
615 static int msm_request_port(struct uart_port
*port
)
617 struct platform_device
*pdev
= to_platform_device(port
->dev
);
618 struct resource
*uart_resource
;
619 resource_size_t size
;
622 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
623 if (unlikely(!uart_resource
))
626 size
= resource_size(uart_resource
);
628 if (!request_mem_region(port
->mapbase
, size
, "msm_serial"))
631 port
->membase
= ioremap(port
->mapbase
, size
);
632 if (!port
->membase
) {
634 goto fail_release_port
;
640 release_mem_region(port
->mapbase
, size
);
644 static void msm_config_port(struct uart_port
*port
, int flags
)
648 if (flags
& UART_CONFIG_TYPE
) {
649 port
->type
= PORT_MSM
;
650 ret
= msm_request_port(port
);
656 static int msm_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
658 if (unlikely(ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_MSM
))
660 if (unlikely(port
->irq
!= ser
->irq
))
665 static void msm_power(struct uart_port
*port
, unsigned int state
,
666 unsigned int oldstate
)
668 struct msm_port
*msm_port
= UART_TO_MSM(port
);
672 clk_prepare_enable(msm_port
->clk
);
673 clk_prepare_enable(msm_port
->pclk
);
676 clk_disable_unprepare(msm_port
->clk
);
677 clk_disable_unprepare(msm_port
->pclk
);
680 pr_err("msm_serial: Unknown PM state %d\n", state
);
684 #ifdef CONFIG_CONSOLE_POLL
685 static int msm_poll_get_char_single(struct uart_port
*port
)
687 struct msm_port
*msm_port
= UART_TO_MSM(port
);
688 unsigned int rf_reg
= msm_port
->is_uartdm
? UARTDM_RF
: UART_RF
;
690 if (!(msm_read(port
, UART_SR
) & UART_SR_RX_READY
))
693 return msm_read(port
, rf_reg
) & 0xff;
696 static int msm_poll_get_char_dm(struct uart_port
*port
)
701 unsigned char *sp
= (unsigned char *)&slop
;
703 /* Check if a previous read had more than one char */
705 c
= sp
[sizeof(slop
) - count
];
707 /* Or if FIFO is empty */
708 } else if (!(msm_read(port
, UART_SR
) & UART_SR_RX_READY
)) {
710 * If RX packing buffer has less than a word, force stale to
711 * push contents into RX FIFO
713 count
= msm_read(port
, UARTDM_RXFS
);
714 count
= (count
>> UARTDM_RXFS_BUF_SHIFT
) & UARTDM_RXFS_BUF_MASK
;
716 msm_write(port
, UART_CR_CMD_FORCE_STALE
, UART_CR
);
717 slop
= msm_read(port
, UARTDM_RF
);
720 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
721 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
722 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
,
727 /* FIFO has a word */
729 slop
= msm_read(port
, UARTDM_RF
);
731 count
= sizeof(slop
) - 1;
737 static int msm_poll_get_char(struct uart_port
*port
)
741 struct msm_port
*msm_port
= UART_TO_MSM(port
);
743 /* Disable all interrupts */
744 imr
= msm_read(port
, UART_IMR
);
745 msm_write(port
, 0, UART_IMR
);
747 if (msm_port
->is_uartdm
)
748 c
= msm_poll_get_char_dm(port
);
750 c
= msm_poll_get_char_single(port
);
752 /* Enable interrupts */
753 msm_write(port
, imr
, UART_IMR
);
758 static void msm_poll_put_char(struct uart_port
*port
, unsigned char c
)
761 struct msm_port
*msm_port
= UART_TO_MSM(port
);
763 /* Disable all interrupts */
764 imr
= msm_read(port
, UART_IMR
);
765 msm_write(port
, 0, UART_IMR
);
767 if (msm_port
->is_uartdm
)
768 reset_dm_count(port
, 1);
770 /* Wait until FIFO is empty */
771 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
774 /* Write a character */
775 msm_write(port
, c
, msm_port
->is_uartdm
? UARTDM_TF
: UART_TF
);
777 /* Wait until FIFO is empty */
778 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
781 /* Enable interrupts */
782 msm_write(port
, imr
, UART_IMR
);
786 static struct uart_ops msm_uart_pops
= {
787 .tx_empty
= msm_tx_empty
,
788 .set_mctrl
= msm_set_mctrl
,
789 .get_mctrl
= msm_get_mctrl
,
790 .stop_tx
= msm_stop_tx
,
791 .start_tx
= msm_start_tx
,
792 .stop_rx
= msm_stop_rx
,
793 .enable_ms
= msm_enable_ms
,
794 .break_ctl
= msm_break_ctl
,
795 .startup
= msm_startup
,
796 .shutdown
= msm_shutdown
,
797 .set_termios
= msm_set_termios
,
799 .release_port
= msm_release_port
,
800 .request_port
= msm_request_port
,
801 .config_port
= msm_config_port
,
802 .verify_port
= msm_verify_port
,
804 #ifdef CONFIG_CONSOLE_POLL
805 .poll_get_char
= msm_poll_get_char
,
806 .poll_put_char
= msm_poll_put_char
,
810 static struct msm_port msm_uart_ports
[] = {
814 .ops
= &msm_uart_pops
,
815 .flags
= UPF_BOOT_AUTOCONF
,
823 .ops
= &msm_uart_pops
,
824 .flags
= UPF_BOOT_AUTOCONF
,
832 .ops
= &msm_uart_pops
,
833 .flags
= UPF_BOOT_AUTOCONF
,
840 #define UART_NR ARRAY_SIZE(msm_uart_ports)
842 static inline struct uart_port
*get_port_from_line(unsigned int line
)
844 return &msm_uart_ports
[line
].uart
;
847 #ifdef CONFIG_SERIAL_MSM_CONSOLE
848 static void __msm_console_write(struct uart_port
*port
, const char *s
,
849 unsigned int count
, bool is_uartdm
)
852 int num_newlines
= 0;
853 bool replaced
= false;
857 tf
= port
->membase
+ UARTDM_TF
;
859 tf
= port
->membase
+ UART_TF
;
861 /* Account for newlines that will get a carriage return added */
862 for (i
= 0; i
< count
; i
++)
865 count
+= num_newlines
;
867 spin_lock(&port
->lock
);
869 reset_dm_count(port
, count
);
874 unsigned int num_chars
;
878 num_chars
= min(count
- i
, (unsigned int)sizeof(buf
));
882 for (j
= 0; j
< num_chars
; j
++) {
885 if (c
== '\n' && !replaced
) {
897 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
900 iowrite32_rep(tf
, buf
, 1);
903 spin_unlock(&port
->lock
);
906 static void msm_console_write(struct console
*co
, const char *s
,
909 struct uart_port
*port
;
910 struct msm_port
*msm_port
;
912 BUG_ON(co
->index
< 0 || co
->index
>= UART_NR
);
914 port
= get_port_from_line(co
->index
);
915 msm_port
= UART_TO_MSM(port
);
917 __msm_console_write(port
, s
, count
, msm_port
->is_uartdm
);
920 static int __init
msm_console_setup(struct console
*co
, char *options
)
922 struct uart_port
*port
;
928 if (unlikely(co
->index
>= UART_NR
|| co
->index
< 0))
931 port
= get_port_from_line(co
->index
);
933 if (unlikely(!port
->membase
))
936 msm_init_clock(port
);
939 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
941 pr_info("msm_serial: console setup on port #%d\n", port
->line
);
943 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
947 msm_serial_early_write(struct console
*con
, const char *s
, unsigned n
)
949 struct earlycon_device
*dev
= con
->data
;
951 __msm_console_write(&dev
->port
, s
, n
, false);
955 msm_serial_early_console_setup(struct earlycon_device
*device
, const char *opt
)
957 if (!device
->port
.membase
)
960 device
->con
->write
= msm_serial_early_write
;
963 EARLYCON_DECLARE(msm_serial
, msm_serial_early_console_setup
);
964 OF_EARLYCON_DECLARE(msm_serial
, "qcom,msm-uart",
965 msm_serial_early_console_setup
);
968 msm_serial_early_write_dm(struct console
*con
, const char *s
, unsigned n
)
970 struct earlycon_device
*dev
= con
->data
;
972 __msm_console_write(&dev
->port
, s
, n
, true);
976 msm_serial_early_console_setup_dm(struct earlycon_device
*device
,
979 if (!device
->port
.membase
)
982 device
->con
->write
= msm_serial_early_write_dm
;
985 EARLYCON_DECLARE(msm_serial_dm
, msm_serial_early_console_setup_dm
);
986 OF_EARLYCON_DECLARE(msm_serial_dm
, "qcom,msm-uartdm",
987 msm_serial_early_console_setup_dm
);
989 static struct uart_driver msm_uart_driver
;
991 static struct console msm_console
= {
993 .write
= msm_console_write
,
994 .device
= uart_console_device
,
995 .setup
= msm_console_setup
,
996 .flags
= CON_PRINTBUFFER
,
998 .data
= &msm_uart_driver
,
1001 #define MSM_CONSOLE (&msm_console)
1004 #define MSM_CONSOLE NULL
1007 static struct uart_driver msm_uart_driver
= {
1008 .owner
= THIS_MODULE
,
1009 .driver_name
= "msm_serial",
1010 .dev_name
= "ttyMSM",
1012 .cons
= MSM_CONSOLE
,
1015 static atomic_t msm_uart_next_id
= ATOMIC_INIT(0);
1017 static const struct of_device_id msm_uartdm_table
[] = {
1018 { .compatible
= "qcom,msm-uartdm-v1.1", .data
= (void *)UARTDM_1P1
},
1019 { .compatible
= "qcom,msm-uartdm-v1.2", .data
= (void *)UARTDM_1P2
},
1020 { .compatible
= "qcom,msm-uartdm-v1.3", .data
= (void *)UARTDM_1P3
},
1021 { .compatible
= "qcom,msm-uartdm-v1.4", .data
= (void *)UARTDM_1P4
},
1025 static int msm_serial_probe(struct platform_device
*pdev
)
1027 struct msm_port
*msm_port
;
1028 struct resource
*resource
;
1029 struct uart_port
*port
;
1030 const struct of_device_id
*id
;
1033 if (pdev
->dev
.of_node
)
1034 line
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
1039 line
= atomic_inc_return(&msm_uart_next_id
) - 1;
1041 if (unlikely(line
< 0 || line
>= UART_NR
))
1044 dev_info(&pdev
->dev
, "msm_serial: detected port #%d\n", line
);
1046 port
= get_port_from_line(line
);
1047 port
->dev
= &pdev
->dev
;
1048 msm_port
= UART_TO_MSM(port
);
1050 id
= of_match_device(msm_uartdm_table
, &pdev
->dev
);
1052 msm_port
->is_uartdm
= (unsigned long)id
->data
;
1054 msm_port
->is_uartdm
= 0;
1056 msm_port
->clk
= devm_clk_get(&pdev
->dev
, "core");
1057 if (IS_ERR(msm_port
->clk
))
1058 return PTR_ERR(msm_port
->clk
);
1060 if (msm_port
->is_uartdm
) {
1061 msm_port
->pclk
= devm_clk_get(&pdev
->dev
, "iface");
1062 if (IS_ERR(msm_port
->pclk
))
1063 return PTR_ERR(msm_port
->pclk
);
1065 clk_set_rate(msm_port
->clk
, 1843200);
1068 port
->uartclk
= clk_get_rate(msm_port
->clk
);
1069 dev_info(&pdev
->dev
, "uartclk = %d\n", port
->uartclk
);
1071 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1072 if (unlikely(!resource
))
1074 port
->mapbase
= resource
->start
;
1076 irq
= platform_get_irq(pdev
, 0);
1077 if (unlikely(irq
< 0))
1081 platform_set_drvdata(pdev
, port
);
1083 return uart_add_one_port(&msm_uart_driver
, port
);
1086 static int msm_serial_remove(struct platform_device
*pdev
)
1088 struct uart_port
*port
= platform_get_drvdata(pdev
);
1090 uart_remove_one_port(&msm_uart_driver
, port
);
1095 static const struct of_device_id msm_match_table
[] = {
1096 { .compatible
= "qcom,msm-uart" },
1097 { .compatible
= "qcom,msm-uartdm" },
1101 static struct platform_driver msm_platform_driver
= {
1102 .remove
= msm_serial_remove
,
1103 .probe
= msm_serial_probe
,
1105 .name
= "msm_serial",
1106 .of_match_table
= msm_match_table
,
1110 static int __init
msm_serial_init(void)
1114 ret
= uart_register_driver(&msm_uart_driver
);
1118 ret
= platform_driver_register(&msm_platform_driver
);
1120 uart_unregister_driver(&msm_uart_driver
);
1122 pr_info("msm_serial: driver initialized\n");
1127 static void __exit
msm_serial_exit(void)
1129 platform_driver_unregister(&msm_platform_driver
);
1130 uart_unregister_driver(&msm_uart_driver
);
1133 module_init(msm_serial_init
);
1134 module_exit(msm_serial_exit
);
1136 MODULE_AUTHOR("Robert Love <rlove@google.com>");
1137 MODULE_DESCRIPTION("Driver for msm7x serial device");
1138 MODULE_LICENSE("GPL");