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"
43 struct uart_port uart
;
48 unsigned int *gsbi_base
;
50 unsigned int old_snap_state
;
53 static inline void wait_for_xmitr(struct uart_port
*port
, int bits
)
55 if (!(msm_read(port
, UART_SR
) & UART_SR_TX_EMPTY
))
56 while ((msm_read(port
, UART_ISR
) & bits
) != bits
)
60 static void msm_stop_tx(struct uart_port
*port
)
62 struct msm_port
*msm_port
= UART_TO_MSM(port
);
64 msm_port
->imr
&= ~UART_IMR_TXLEV
;
65 msm_write(port
, msm_port
->imr
, UART_IMR
);
68 static void msm_start_tx(struct uart_port
*port
)
70 struct msm_port
*msm_port
= UART_TO_MSM(port
);
72 msm_port
->imr
|= UART_IMR_TXLEV
;
73 msm_write(port
, msm_port
->imr
, UART_IMR
);
76 static void msm_stop_rx(struct uart_port
*port
)
78 struct msm_port
*msm_port
= UART_TO_MSM(port
);
80 msm_port
->imr
&= ~(UART_IMR_RXLEV
| UART_IMR_RXSTALE
);
81 msm_write(port
, msm_port
->imr
, UART_IMR
);
84 static void msm_enable_ms(struct uart_port
*port
)
86 struct msm_port
*msm_port
= UART_TO_MSM(port
);
88 msm_port
->imr
|= UART_IMR_DELTA_CTS
;
89 msm_write(port
, msm_port
->imr
, UART_IMR
);
92 static void handle_rx_dm(struct uart_port
*port
, unsigned int misr
)
94 struct tty_port
*tport
= &port
->state
->port
;
97 struct msm_port
*msm_port
= UART_TO_MSM(port
);
99 if ((msm_read(port
, UART_SR
) & UART_SR_OVERRUN
)) {
100 port
->icount
.overrun
++;
101 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
102 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
105 if (misr
& UART_IMR_RXSTALE
) {
106 count
= msm_read(port
, UARTDM_RX_TOTAL_SNAP
) -
107 msm_port
->old_snap_state
;
108 msm_port
->old_snap_state
= 0;
110 count
= 4 * (msm_read(port
, UART_RFWR
));
111 msm_port
->old_snap_state
+= count
;
114 /* TODO: Precise error reporting */
116 port
->icount
.rx
+= count
;
121 sr
= msm_read(port
, UART_SR
);
122 if ((sr
& UART_SR_RX_READY
) == 0) {
123 msm_port
->old_snap_state
-= count
;
126 c
= msm_read(port
, UARTDM_RF
);
127 if (sr
& UART_SR_RX_BREAK
) {
129 if (uart_handle_break(port
))
131 } else if (sr
& UART_SR_PAR_FRAME_ERR
)
132 port
->icount
.frame
++;
134 /* TODO: handle sysrq */
135 tty_insert_flip_string(tport
, (char *)&c
,
136 (count
> 4) ? 4 : count
);
140 tty_flip_buffer_push(tport
);
141 if (misr
& (UART_IMR_RXSTALE
))
142 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
143 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
144 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
, UART_CR
);
147 static void handle_rx(struct uart_port
*port
)
149 struct tty_port
*tport
= &port
->state
->port
;
153 * Handle overrun. My understanding of the hardware is that overrun
154 * is not tied to the RX buffer, so we handle the case out of band.
156 if ((msm_read(port
, UART_SR
) & UART_SR_OVERRUN
)) {
157 port
->icount
.overrun
++;
158 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
159 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
162 /* and now the main RX loop */
163 while ((sr
= msm_read(port
, UART_SR
)) & UART_SR_RX_READY
) {
165 char flag
= TTY_NORMAL
;
167 c
= msm_read(port
, UART_RF
);
169 if (sr
& UART_SR_RX_BREAK
) {
171 if (uart_handle_break(port
))
173 } else if (sr
& UART_SR_PAR_FRAME_ERR
) {
174 port
->icount
.frame
++;
179 /* Mask conditions we're ignorning. */
180 sr
&= port
->read_status_mask
;
182 if (sr
& UART_SR_RX_BREAK
) {
184 } else if (sr
& UART_SR_PAR_FRAME_ERR
) {
188 if (!uart_handle_sysrq_char(port
, c
))
189 tty_insert_flip_char(tport
, c
, flag
);
192 tty_flip_buffer_push(tport
);
195 static void reset_dm_count(struct uart_port
*port
)
197 wait_for_xmitr(port
, UART_ISR_TX_READY
);
198 msm_write(port
, 1, UARTDM_NCF_TX
);
201 static void handle_tx(struct uart_port
*port
)
203 struct circ_buf
*xmit
= &port
->state
->xmit
;
204 struct msm_port
*msm_port
= UART_TO_MSM(port
);
208 if (msm_port
->is_uartdm
)
209 reset_dm_count(port
);
211 msm_write(port
, port
->x_char
,
212 msm_port
->is_uartdm
? UARTDM_TF
: UART_TF
);
217 if (msm_port
->is_uartdm
)
218 reset_dm_count(port
);
220 while (msm_read(port
, UART_SR
) & UART_SR_TX_READY
) {
221 if (uart_circ_empty(xmit
)) {
222 /* disable tx interrupts */
223 msm_port
->imr
&= ~UART_IMR_TXLEV
;
224 msm_write(port
, msm_port
->imr
, UART_IMR
);
227 msm_write(port
, xmit
->buf
[xmit
->tail
],
228 msm_port
->is_uartdm
? UARTDM_TF
: UART_TF
);
230 if (msm_port
->is_uartdm
)
231 reset_dm_count(port
);
233 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
238 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
239 uart_write_wakeup(port
);
242 static void handle_delta_cts(struct uart_port
*port
)
244 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
246 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
249 static irqreturn_t
msm_irq(int irq
, void *dev_id
)
251 struct uart_port
*port
= dev_id
;
252 struct msm_port
*msm_port
= UART_TO_MSM(port
);
255 spin_lock(&port
->lock
);
256 misr
= msm_read(port
, UART_MISR
);
257 msm_write(port
, 0, UART_IMR
); /* disable interrupt */
259 if (misr
& (UART_IMR_RXLEV
| UART_IMR_RXSTALE
)) {
260 if (msm_port
->is_uartdm
)
261 handle_rx_dm(port
, misr
);
265 if (misr
& UART_IMR_TXLEV
)
267 if (misr
& UART_IMR_DELTA_CTS
)
268 handle_delta_cts(port
);
270 msm_write(port
, msm_port
->imr
, UART_IMR
); /* restore interrupt */
271 spin_unlock(&port
->lock
);
276 static unsigned int msm_tx_empty(struct uart_port
*port
)
278 return (msm_read(port
, UART_SR
) & UART_SR_TX_EMPTY
) ? TIOCSER_TEMT
: 0;
281 static unsigned int msm_get_mctrl(struct uart_port
*port
)
283 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
| TIOCM_RTS
;
287 static void msm_reset(struct uart_port
*port
)
289 /* reset everything */
290 msm_write(port
, UART_CR_CMD_RESET_RX
, UART_CR
);
291 msm_write(port
, UART_CR_CMD_RESET_TX
, UART_CR
);
292 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
293 msm_write(port
, UART_CR_CMD_RESET_BREAK_INT
, UART_CR
);
294 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
295 msm_write(port
, UART_CR_CMD_SET_RFR
, UART_CR
);
298 void msm_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
301 mr
= msm_read(port
, UART_MR1
);
303 if (!(mctrl
& TIOCM_RTS
)) {
304 mr
&= ~UART_MR1_RX_RDY_CTL
;
305 msm_write(port
, mr
, UART_MR1
);
306 msm_write(port
, UART_CR_CMD_RESET_RFR
, UART_CR
);
308 mr
|= UART_MR1_RX_RDY_CTL
;
309 msm_write(port
, mr
, UART_MR1
);
313 static void msm_break_ctl(struct uart_port
*port
, int break_ctl
)
316 msm_write(port
, UART_CR_CMD_START_BREAK
, UART_CR
);
318 msm_write(port
, UART_CR_CMD_STOP_BREAK
, UART_CR
);
321 static int msm_set_baud_rate(struct uart_port
*port
, unsigned int baud
)
323 unsigned int baud_code
, rxstale
, watermark
;
324 struct msm_port
*msm_port
= UART_TO_MSM(port
);
328 baud_code
= UART_CSR_300
;
332 baud_code
= UART_CSR_600
;
336 baud_code
= UART_CSR_1200
;
340 baud_code
= UART_CSR_2400
;
344 baud_code
= UART_CSR_4800
;
348 baud_code
= UART_CSR_9600
;
352 baud_code
= UART_CSR_14400
;
356 baud_code
= UART_CSR_19200
;
360 baud_code
= UART_CSR_28800
;
364 baud_code
= UART_CSR_38400
;
368 baud_code
= UART_CSR_57600
;
373 baud_code
= UART_CSR_115200
;
379 if (msm_port
->is_uartdm
)
380 msm_write(port
, UART_CR_CMD_RESET_RX
, UART_CR
);
382 msm_write(port
, baud_code
, UART_CSR
);
384 /* RX stale watermark */
385 watermark
= UART_IPR_STALE_LSB
& rxstale
;
386 watermark
|= UART_IPR_RXSTALE_LAST
;
387 watermark
|= UART_IPR_STALE_TIMEOUT_MSB
& (rxstale
<< 2);
388 msm_write(port
, watermark
, UART_IPR
);
390 /* set RX watermark */
391 watermark
= (port
->fifosize
* 3) / 4;
392 msm_write(port
, watermark
, UART_RFWR
);
394 /* set TX watermark */
395 msm_write(port
, 10, UART_TFWR
);
397 if (msm_port
->is_uartdm
) {
398 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
399 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
400 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
, UART_CR
);
407 static void msm_init_clock(struct uart_port
*port
)
409 struct msm_port
*msm_port
= UART_TO_MSM(port
);
411 clk_prepare_enable(msm_port
->clk
);
412 if (!IS_ERR(msm_port
->pclk
))
413 clk_prepare_enable(msm_port
->pclk
);
414 msm_serial_set_mnd_regs(port
);
417 static int msm_startup(struct uart_port
*port
)
419 struct msm_port
*msm_port
= UART_TO_MSM(port
);
420 unsigned int data
, rfr_level
;
423 snprintf(msm_port
->name
, sizeof(msm_port
->name
),
424 "msm_serial%d", port
->line
);
426 ret
= request_irq(port
->irq
, msm_irq
, IRQF_TRIGGER_HIGH
,
427 msm_port
->name
, port
);
431 msm_init_clock(port
);
433 if (likely(port
->fifosize
> 12))
434 rfr_level
= port
->fifosize
- 12;
436 rfr_level
= port
->fifosize
;
438 /* set automatic RFR level */
439 data
= msm_read(port
, UART_MR1
);
440 data
&= ~UART_MR1_AUTO_RFR_LEVEL1
;
441 data
&= ~UART_MR1_AUTO_RFR_LEVEL0
;
442 data
|= UART_MR1_AUTO_RFR_LEVEL1
& (rfr_level
<< 2);
443 data
|= UART_MR1_AUTO_RFR_LEVEL0
& rfr_level
;
444 msm_write(port
, data
, UART_MR1
);
446 /* make sure that RXSTALE count is non-zero */
447 data
= msm_read(port
, UART_IPR
);
448 if (unlikely(!data
)) {
449 data
|= UART_IPR_RXSTALE_LAST
;
450 data
|= UART_IPR_STALE_LSB
;
451 msm_write(port
, data
, UART_IPR
);
455 if (!port
->cons
|| (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))) {
456 msm_write(port
, UART_CR_CMD_PROTECTION_EN
, UART_CR
);
458 data
= UART_CR_TX_ENABLE
;
461 data
|= UART_CR_RX_ENABLE
;
462 msm_write(port
, data
, UART_CR
); /* enable TX & RX */
464 /* Make sure IPR is not 0 to start with*/
465 if (msm_port
->is_uartdm
)
466 msm_write(port
, UART_IPR_STALE_LSB
, UART_IPR
);
468 /* turn on RX and CTS interrupts */
469 msm_port
->imr
= UART_IMR_RXLEV
| UART_IMR_RXSTALE
|
470 UART_IMR_CURRENT_CTS
;
472 if (msm_port
->is_uartdm
) {
473 msm_write(port
, 0xFFFFFF, UARTDM_DMRX
);
474 msm_write(port
, UART_CR_CMD_RESET_STALE_INT
, UART_CR
);
475 msm_write(port
, UART_CR_CMD_STALE_EVENT_ENABLE
, UART_CR
);
478 msm_write(port
, msm_port
->imr
, UART_IMR
);
482 static void msm_shutdown(struct uart_port
*port
)
484 struct msm_port
*msm_port
= UART_TO_MSM(port
);
487 msm_write(port
, 0, UART_IMR
); /* disable interrupts */
489 clk_disable_unprepare(msm_port
->clk
);
491 free_irq(port
->irq
, port
);
494 static void msm_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
495 struct ktermios
*old
)
498 unsigned int baud
, mr
;
500 spin_lock_irqsave(&port
->lock
, flags
);
502 /* calculate and set baud rate */
503 baud
= uart_get_baud_rate(port
, termios
, old
, 300, 115200);
504 baud
= msm_set_baud_rate(port
, baud
);
505 if (tty_termios_baud_rate(termios
))
506 tty_termios_encode_baud_rate(termios
, baud
, baud
);
508 /* calculate parity */
509 mr
= msm_read(port
, UART_MR2
);
510 mr
&= ~UART_MR2_PARITY_MODE
;
511 if (termios
->c_cflag
& PARENB
) {
512 if (termios
->c_cflag
& PARODD
)
513 mr
|= UART_MR2_PARITY_MODE_ODD
;
514 else if (termios
->c_cflag
& CMSPAR
)
515 mr
|= UART_MR2_PARITY_MODE_SPACE
;
517 mr
|= UART_MR2_PARITY_MODE_EVEN
;
520 /* calculate bits per char */
521 mr
&= ~UART_MR2_BITS_PER_CHAR
;
522 switch (termios
->c_cflag
& CSIZE
) {
524 mr
|= UART_MR2_BITS_PER_CHAR_5
;
527 mr
|= UART_MR2_BITS_PER_CHAR_6
;
530 mr
|= UART_MR2_BITS_PER_CHAR_7
;
534 mr
|= UART_MR2_BITS_PER_CHAR_8
;
538 /* calculate stop bits */
539 mr
&= ~(UART_MR2_STOP_BIT_LEN_ONE
| UART_MR2_STOP_BIT_LEN_TWO
);
540 if (termios
->c_cflag
& CSTOPB
)
541 mr
|= UART_MR2_STOP_BIT_LEN_TWO
;
543 mr
|= UART_MR2_STOP_BIT_LEN_ONE
;
545 /* set parity, bits per char, and stop bit */
546 msm_write(port
, mr
, UART_MR2
);
548 /* calculate and set hardware flow control */
549 mr
= msm_read(port
, UART_MR1
);
550 mr
&= ~(UART_MR1_CTS_CTL
| UART_MR1_RX_RDY_CTL
);
551 if (termios
->c_cflag
& CRTSCTS
) {
552 mr
|= UART_MR1_CTS_CTL
;
553 mr
|= UART_MR1_RX_RDY_CTL
;
555 msm_write(port
, mr
, UART_MR1
);
557 /* Configure status bits to ignore based on termio flags. */
558 port
->read_status_mask
= 0;
559 if (termios
->c_iflag
& INPCK
)
560 port
->read_status_mask
|= UART_SR_PAR_FRAME_ERR
;
561 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
562 port
->read_status_mask
|= UART_SR_RX_BREAK
;
564 uart_update_timeout(port
, termios
->c_cflag
, baud
);
566 spin_unlock_irqrestore(&port
->lock
, flags
);
569 static const char *msm_type(struct uart_port
*port
)
574 static void msm_release_port(struct uart_port
*port
)
576 struct platform_device
*pdev
= to_platform_device(port
->dev
);
577 struct msm_port
*msm_port
= UART_TO_MSM(port
);
578 struct resource
*uart_resource
;
579 struct resource
*gsbi_resource
;
580 resource_size_t size
;
582 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
583 if (unlikely(!uart_resource
))
585 size
= resource_size(uart_resource
);
587 release_mem_region(port
->mapbase
, size
);
588 iounmap(port
->membase
);
589 port
->membase
= NULL
;
591 if (msm_port
->gsbi_base
) {
592 iowrite32(GSBI_PROTOCOL_IDLE
, msm_port
->gsbi_base
+
595 gsbi_resource
= platform_get_resource(pdev
,
598 if (unlikely(!gsbi_resource
))
601 size
= resource_size(gsbi_resource
);
602 release_mem_region(gsbi_resource
->start
, size
);
603 iounmap(msm_port
->gsbi_base
);
604 msm_port
->gsbi_base
= NULL
;
608 static int msm_request_port(struct uart_port
*port
)
610 struct msm_port
*msm_port
= UART_TO_MSM(port
);
611 struct platform_device
*pdev
= to_platform_device(port
->dev
);
612 struct resource
*uart_resource
;
613 struct resource
*gsbi_resource
;
614 resource_size_t size
;
617 uart_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
618 if (unlikely(!uart_resource
))
621 size
= resource_size(uart_resource
);
623 if (!request_mem_region(port
->mapbase
, size
, "msm_serial"))
626 port
->membase
= ioremap(port
->mapbase
, size
);
627 if (!port
->membase
) {
629 goto fail_release_port
;
632 gsbi_resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
633 /* Is this a GSBI-based port? */
635 size
= resource_size(gsbi_resource
);
637 if (!request_mem_region(gsbi_resource
->start
, size
,
640 goto fail_release_port
;
643 msm_port
->gsbi_base
= ioremap(gsbi_resource
->start
, size
);
644 if (!msm_port
->gsbi_base
) {
646 goto fail_release_gsbi
;
653 release_mem_region(gsbi_resource
->start
, size
);
655 release_mem_region(port
->mapbase
, size
);
659 static void msm_config_port(struct uart_port
*port
, int flags
)
661 struct msm_port
*msm_port
= UART_TO_MSM(port
);
663 if (flags
& UART_CONFIG_TYPE
) {
664 port
->type
= PORT_MSM
;
665 ret
= msm_request_port(port
);
670 if (msm_port
->is_uartdm
)
671 iowrite32(GSBI_PROTOCOL_UART
, msm_port
->gsbi_base
+
675 static int msm_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
677 if (unlikely(ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_MSM
))
679 if (unlikely(port
->irq
!= ser
->irq
))
684 static void msm_power(struct uart_port
*port
, unsigned int state
,
685 unsigned int oldstate
)
687 struct msm_port
*msm_port
= UART_TO_MSM(port
);
691 clk_prepare_enable(msm_port
->clk
);
692 if (!IS_ERR(msm_port
->pclk
))
693 clk_prepare_enable(msm_port
->pclk
);
696 clk_disable_unprepare(msm_port
->clk
);
697 if (!IS_ERR(msm_port
->pclk
))
698 clk_disable_unprepare(msm_port
->pclk
);
701 printk(KERN_ERR
"msm_serial: Unknown PM state %d\n", state
);
705 static struct uart_ops msm_uart_pops
= {
706 .tx_empty
= msm_tx_empty
,
707 .set_mctrl
= msm_set_mctrl
,
708 .get_mctrl
= msm_get_mctrl
,
709 .stop_tx
= msm_stop_tx
,
710 .start_tx
= msm_start_tx
,
711 .stop_rx
= msm_stop_rx
,
712 .enable_ms
= msm_enable_ms
,
713 .break_ctl
= msm_break_ctl
,
714 .startup
= msm_startup
,
715 .shutdown
= msm_shutdown
,
716 .set_termios
= msm_set_termios
,
718 .release_port
= msm_release_port
,
719 .request_port
= msm_request_port
,
720 .config_port
= msm_config_port
,
721 .verify_port
= msm_verify_port
,
725 static struct msm_port msm_uart_ports
[] = {
729 .ops
= &msm_uart_pops
,
730 .flags
= UPF_BOOT_AUTOCONF
,
738 .ops
= &msm_uart_pops
,
739 .flags
= UPF_BOOT_AUTOCONF
,
747 .ops
= &msm_uart_pops
,
748 .flags
= UPF_BOOT_AUTOCONF
,
755 #define UART_NR ARRAY_SIZE(msm_uart_ports)
757 static inline struct uart_port
*get_port_from_line(unsigned int line
)
759 return &msm_uart_ports
[line
].uart
;
762 #ifdef CONFIG_SERIAL_MSM_CONSOLE
764 static void msm_console_putchar(struct uart_port
*port
, int c
)
766 struct msm_port
*msm_port
= UART_TO_MSM(port
);
768 if (msm_port
->is_uartdm
)
769 reset_dm_count(port
);
771 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
773 msm_write(port
, c
, msm_port
->is_uartdm
? UARTDM_TF
: UART_TF
);
776 static void msm_console_write(struct console
*co
, const char *s
,
779 struct uart_port
*port
;
780 struct msm_port
*msm_port
;
782 BUG_ON(co
->index
< 0 || co
->index
>= UART_NR
);
784 port
= get_port_from_line(co
->index
);
785 msm_port
= UART_TO_MSM(port
);
787 spin_lock(&port
->lock
);
788 uart_console_write(port
, s
, count
, msm_console_putchar
);
789 spin_unlock(&port
->lock
);
792 static int __init
msm_console_setup(struct console
*co
, char *options
)
794 struct uart_port
*port
;
795 struct msm_port
*msm_port
;
796 int baud
, flow
, bits
, parity
;
798 if (unlikely(co
->index
>= UART_NR
|| co
->index
< 0))
801 port
= get_port_from_line(co
->index
);
802 msm_port
= UART_TO_MSM(port
);
804 if (unlikely(!port
->membase
))
807 msm_init_clock(port
);
810 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
815 msm_write(port
, UART_MR2_BITS_PER_CHAR_8
| UART_MR2_STOP_BIT_LEN_ONE
,
818 if (baud
< 300 || baud
> 115200)
820 msm_set_baud_rate(port
, baud
);
824 if (msm_port
->is_uartdm
) {
825 msm_write(port
, UART_CR_CMD_PROTECTION_EN
, UART_CR
);
826 msm_write(port
, UART_CR_TX_ENABLE
, UART_CR
);
829 printk(KERN_INFO
"msm_serial: console setup on port #%d\n", port
->line
);
831 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
834 static struct uart_driver msm_uart_driver
;
836 static struct console msm_console
= {
838 .write
= msm_console_write
,
839 .device
= uart_console_device
,
840 .setup
= msm_console_setup
,
841 .flags
= CON_PRINTBUFFER
,
843 .data
= &msm_uart_driver
,
846 #define MSM_CONSOLE (&msm_console)
849 #define MSM_CONSOLE NULL
852 static struct uart_driver msm_uart_driver
= {
853 .owner
= THIS_MODULE
,
854 .driver_name
= "msm_serial",
855 .dev_name
= "ttyMSM",
860 static atomic_t msm_uart_next_id
= ATOMIC_INIT(0);
862 static int __init
msm_serial_probe(struct platform_device
*pdev
)
864 struct msm_port
*msm_port
;
865 struct resource
*resource
;
866 struct uart_port
*port
;
870 pdev
->id
= atomic_inc_return(&msm_uart_next_id
) - 1;
872 if (unlikely(pdev
->id
< 0 || pdev
->id
>= UART_NR
))
875 printk(KERN_INFO
"msm_serial: detected port #%d\n", pdev
->id
);
877 port
= get_port_from_line(pdev
->id
);
878 port
->dev
= &pdev
->dev
;
879 msm_port
= UART_TO_MSM(port
);
881 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
882 msm_port
->is_uartdm
= 1;
884 msm_port
->is_uartdm
= 0;
886 if (msm_port
->is_uartdm
) {
887 msm_port
->clk
= devm_clk_get(&pdev
->dev
, "gsbi_uart_clk");
888 msm_port
->pclk
= devm_clk_get(&pdev
->dev
, "gsbi_pclk");
890 msm_port
->clk
= devm_clk_get(&pdev
->dev
, "uart_clk");
891 msm_port
->pclk
= ERR_PTR(-ENOENT
);
894 if (IS_ERR(msm_port
->clk
))
895 return PTR_ERR(msm_port
->clk
);
897 if (msm_port
->is_uartdm
) {
898 if (IS_ERR(msm_port
->pclk
))
899 return PTR_ERR(msm_port
->pclk
);
901 clk_set_rate(msm_port
->clk
, 1843200);
904 port
->uartclk
= clk_get_rate(msm_port
->clk
);
905 printk(KERN_INFO
"uartclk = %d\n", port
->uartclk
);
908 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
909 if (unlikely(!resource
))
911 port
->mapbase
= resource
->start
;
913 irq
= platform_get_irq(pdev
, 0);
914 if (unlikely(irq
< 0))
918 platform_set_drvdata(pdev
, port
);
920 return uart_add_one_port(&msm_uart_driver
, port
);
923 static int msm_serial_remove(struct platform_device
*pdev
)
925 struct uart_port
*port
= platform_get_drvdata(pdev
);
927 uart_remove_one_port(&msm_uart_driver
, port
);
932 static struct of_device_id msm_match_table
[] = {
933 { .compatible
= "qcom,msm-uart" },
937 static struct platform_driver msm_platform_driver
= {
938 .remove
= msm_serial_remove
,
940 .name
= "msm_serial",
941 .owner
= THIS_MODULE
,
942 .of_match_table
= msm_match_table
,
946 static int __init
msm_serial_init(void)
950 ret
= uart_register_driver(&msm_uart_driver
);
954 ret
= platform_driver_probe(&msm_platform_driver
, msm_serial_probe
);
956 uart_unregister_driver(&msm_uart_driver
);
958 printk(KERN_INFO
"msm_serial: driver initialized\n");
963 static void __exit
msm_serial_exit(void)
965 #ifdef CONFIG_SERIAL_MSM_CONSOLE
966 unregister_console(&msm_console
);
968 platform_driver_unregister(&msm_platform_driver
);
969 uart_unregister_driver(&msm_uart_driver
);
972 module_init(msm_serial_init
);
973 module_exit(msm_serial_exit
);
975 MODULE_AUTHOR("Robert Love <rlove@google.com>");
976 MODULE_DESCRIPTION("Driver for msm7x serial device");
977 MODULE_LICENSE("GPL");