2 * drivers/serial/msm_serial.c - driver for msm7k serial device and console
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18 # define SUPPORT_SYSRQ
21 #include <linux/hrtimer.h>
22 #include <linux/module.h>
24 #include <linux/ioport.h>
25 #include <linux/irq.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/clk.h>
33 #include <linux/platform_device.h>
35 #include "msm_serial.h"
38 struct uart_port uart
;
44 #define UART_TO_MSM(uart_port) ((struct msm_port *) uart_port)
46 static inline void msm_write(struct uart_port
*port
, unsigned int val
,
49 __raw_writel(val
, port
->membase
+ off
);
52 static inline unsigned int msm_read(struct uart_port
*port
, unsigned int off
)
54 return __raw_readl(port
->membase
+ off
);
57 static void msm_stop_tx(struct uart_port
*port
)
59 struct msm_port
*msm_port
= UART_TO_MSM(port
);
61 msm_port
->imr
&= ~UART_IMR_TXLEV
;
62 msm_write(port
, msm_port
->imr
, UART_IMR
);
65 static void msm_start_tx(struct uart_port
*port
)
67 struct msm_port
*msm_port
= UART_TO_MSM(port
);
69 msm_port
->imr
|= UART_IMR_TXLEV
;
70 msm_write(port
, msm_port
->imr
, UART_IMR
);
73 static void msm_stop_rx(struct uart_port
*port
)
75 struct msm_port
*msm_port
= UART_TO_MSM(port
);
77 msm_port
->imr
&= ~(UART_IMR_RXLEV
| UART_IMR_RXSTALE
);
78 msm_write(port
, msm_port
->imr
, UART_IMR
);
81 static void msm_enable_ms(struct uart_port
*port
)
83 struct msm_port
*msm_port
= UART_TO_MSM(port
);
85 msm_port
->imr
|= UART_IMR_DELTA_CTS
;
86 msm_write(port
, msm_port
->imr
, UART_IMR
);
89 static void handle_rx(struct uart_port
*port
)
91 struct tty_struct
*tty
= port
->state
->port
.tty
;
95 * Handle overrun. My understanding of the hardware is that overrun
96 * is not tied to the RX buffer, so we handle the case out of band.
98 if ((msm_read(port
, UART_SR
) & UART_SR_OVERRUN
)) {
99 port
->icount
.overrun
++;
100 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
101 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
104 /* and now the main RX loop */
105 while ((sr
= msm_read(port
, UART_SR
)) & UART_SR_RX_READY
) {
107 char flag
= TTY_NORMAL
;
109 c
= msm_read(port
, UART_RF
);
111 if (sr
& UART_SR_RX_BREAK
) {
113 if (uart_handle_break(port
))
115 } else if (sr
& UART_SR_PAR_FRAME_ERR
) {
116 port
->icount
.frame
++;
121 /* Mask conditions we're ignorning. */
122 sr
&= port
->read_status_mask
;
124 if (sr
& UART_SR_RX_BREAK
) {
126 } else if (sr
& UART_SR_PAR_FRAME_ERR
) {
130 if (!uart_handle_sysrq_char(port
, c
))
131 tty_insert_flip_char(tty
, c
, flag
);
134 tty_flip_buffer_push(tty
);
137 static void handle_tx(struct uart_port
*port
)
139 struct circ_buf
*xmit
= &port
->state
->xmit
;
140 struct msm_port
*msm_port
= UART_TO_MSM(port
);
144 msm_write(port
, port
->x_char
, UART_TF
);
149 while (msm_read(port
, UART_SR
) & UART_SR_TX_READY
) {
150 if (uart_circ_empty(xmit
)) {
151 /* disable tx interrupts */
152 msm_port
->imr
&= ~UART_IMR_TXLEV
;
153 msm_write(port
, msm_port
->imr
, UART_IMR
);
157 msm_write(port
, xmit
->buf
[xmit
->tail
], UART_TF
);
159 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
164 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
165 uart_write_wakeup(port
);
168 static void handle_delta_cts(struct uart_port
*port
)
170 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
172 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
175 static irqreturn_t
msm_irq(int irq
, void *dev_id
)
177 struct uart_port
*port
= dev_id
;
178 struct msm_port
*msm_port
= UART_TO_MSM(port
);
181 spin_lock(&port
->lock
);
182 misr
= msm_read(port
, UART_MISR
);
183 msm_write(port
, 0, UART_IMR
); /* disable interrupt */
185 if (misr
& (UART_IMR_RXLEV
| UART_IMR_RXSTALE
))
187 if (misr
& UART_IMR_TXLEV
)
189 if (misr
& UART_IMR_DELTA_CTS
)
190 handle_delta_cts(port
);
192 msm_write(port
, msm_port
->imr
, UART_IMR
); /* restore interrupt */
193 spin_unlock(&port
->lock
);
198 static unsigned int msm_tx_empty(struct uart_port
*port
)
200 return (msm_read(port
, UART_SR
) & UART_SR_TX_EMPTY
) ? TIOCSER_TEMT
: 0;
203 static unsigned int msm_get_mctrl(struct uart_port
*port
)
205 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
| TIOCM_RTS
;
208 static void msm_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
212 mr
= msm_read(port
, UART_MR1
);
214 if (!(mctrl
& TIOCM_RTS
)) {
215 mr
&= ~UART_MR1_RX_RDY_CTL
;
216 msm_write(port
, mr
, UART_MR1
);
217 msm_write(port
, UART_CR_CMD_RESET_RFR
, UART_CR
);
219 mr
|= UART_MR1_RX_RDY_CTL
;
220 msm_write(port
, mr
, UART_MR1
);
224 static void msm_break_ctl(struct uart_port
*port
, int break_ctl
)
227 msm_write(port
, UART_CR_CMD_START_BREAK
, UART_CR
);
229 msm_write(port
, UART_CR_CMD_STOP_BREAK
, UART_CR
);
232 static int msm_set_baud_rate(struct uart_port
*port
, unsigned int baud
)
234 unsigned int baud_code
, rxstale
, watermark
;
238 baud_code
= UART_CSR_300
;
242 baud_code
= UART_CSR_600
;
246 baud_code
= UART_CSR_1200
;
250 baud_code
= UART_CSR_2400
;
254 baud_code
= UART_CSR_4800
;
258 baud_code
= UART_CSR_9600
;
262 baud_code
= UART_CSR_14400
;
266 baud_code
= UART_CSR_19200
;
270 baud_code
= UART_CSR_28800
;
274 baud_code
= UART_CSR_38400
;
278 baud_code
= UART_CSR_57600
;
283 baud_code
= UART_CSR_115200
;
289 msm_write(port
, baud_code
, UART_CSR
);
291 /* RX stale watermark */
292 watermark
= UART_IPR_STALE_LSB
& rxstale
;
293 watermark
|= UART_IPR_RXSTALE_LAST
;
294 watermark
|= UART_IPR_STALE_TIMEOUT_MSB
& (rxstale
<< 2);
295 msm_write(port
, watermark
, UART_IPR
);
297 /* set RX watermark */
298 watermark
= (port
->fifosize
* 3) / 4;
299 msm_write(port
, watermark
, UART_RFWR
);
301 /* set TX watermark */
302 msm_write(port
, 10, UART_TFWR
);
307 static void msm_reset(struct uart_port
*port
)
309 /* reset everything */
310 msm_write(port
, UART_CR_CMD_RESET_RX
, UART_CR
);
311 msm_write(port
, UART_CR_CMD_RESET_TX
, UART_CR
);
312 msm_write(port
, UART_CR_CMD_RESET_ERR
, UART_CR
);
313 msm_write(port
, UART_CR_CMD_RESET_BREAK_INT
, UART_CR
);
314 msm_write(port
, UART_CR_CMD_RESET_CTS
, UART_CR
);
315 msm_write(port
, UART_CR_CMD_SET_RFR
, UART_CR
);
318 static void msm_init_clock(struct uart_port
*port
)
320 struct msm_port
*msm_port
= UART_TO_MSM(port
);
322 clk_enable(msm_port
->clk
);
324 msm_write(port
, 0xC0, UART_MREG
);
325 msm_write(port
, 0xB2, UART_NREG
);
326 msm_write(port
, 0x7D, UART_DREG
);
327 msm_write(port
, 0x1C, UART_MNDREG
);
330 static int msm_startup(struct uart_port
*port
)
332 struct msm_port
*msm_port
= UART_TO_MSM(port
);
333 unsigned int data
, rfr_level
;
336 snprintf(msm_port
->name
, sizeof(msm_port
->name
),
337 "msm_serial%d", port
->line
);
339 ret
= request_irq(port
->irq
, msm_irq
, IRQF_TRIGGER_HIGH
,
340 msm_port
->name
, port
);
344 msm_init_clock(port
);
346 if (likely(port
->fifosize
> 12))
347 rfr_level
= port
->fifosize
- 12;
349 rfr_level
= port
->fifosize
;
351 /* set automatic RFR level */
352 data
= msm_read(port
, UART_MR1
);
353 data
&= ~UART_MR1_AUTO_RFR_LEVEL1
;
354 data
&= ~UART_MR1_AUTO_RFR_LEVEL0
;
355 data
|= UART_MR1_AUTO_RFR_LEVEL1
& (rfr_level
<< 2);
356 data
|= UART_MR1_AUTO_RFR_LEVEL0
& rfr_level
;
357 msm_write(port
, data
, UART_MR1
);
359 /* make sure that RXSTALE count is non-zero */
360 data
= msm_read(port
, UART_IPR
);
361 if (unlikely(!data
)) {
362 data
|= UART_IPR_RXSTALE_LAST
;
363 data
|= UART_IPR_STALE_LSB
;
364 msm_write(port
, data
, UART_IPR
);
369 msm_write(port
, 0x05, UART_CR
); /* enable TX & RX */
371 /* turn on RX and CTS interrupts */
372 msm_port
->imr
= UART_IMR_RXLEV
| UART_IMR_RXSTALE
|
373 UART_IMR_CURRENT_CTS
;
374 msm_write(port
, msm_port
->imr
, UART_IMR
);
379 static void msm_shutdown(struct uart_port
*port
)
381 struct msm_port
*msm_port
= UART_TO_MSM(port
);
384 msm_write(port
, 0, UART_IMR
); /* disable interrupts */
386 clk_disable(msm_port
->clk
);
388 free_irq(port
->irq
, port
);
391 static void msm_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
392 struct ktermios
*old
)
395 unsigned int baud
, mr
;
397 spin_lock_irqsave(&port
->lock
, flags
);
399 /* calculate and set baud rate */
400 baud
= uart_get_baud_rate(port
, termios
, old
, 300, 115200);
401 baud
= msm_set_baud_rate(port
, baud
);
402 if (tty_termios_baud_rate(termios
))
403 tty_termios_encode_baud_rate(termios
, baud
, baud
);
405 /* calculate parity */
406 mr
= msm_read(port
, UART_MR2
);
407 mr
&= ~UART_MR2_PARITY_MODE
;
408 if (termios
->c_cflag
& PARENB
) {
409 if (termios
->c_cflag
& PARODD
)
410 mr
|= UART_MR2_PARITY_MODE_ODD
;
411 else if (termios
->c_cflag
& CMSPAR
)
412 mr
|= UART_MR2_PARITY_MODE_SPACE
;
414 mr
|= UART_MR2_PARITY_MODE_EVEN
;
417 /* calculate bits per char */
418 mr
&= ~UART_MR2_BITS_PER_CHAR
;
419 switch (termios
->c_cflag
& CSIZE
) {
421 mr
|= UART_MR2_BITS_PER_CHAR_5
;
424 mr
|= UART_MR2_BITS_PER_CHAR_6
;
427 mr
|= UART_MR2_BITS_PER_CHAR_7
;
431 mr
|= UART_MR2_BITS_PER_CHAR_8
;
435 /* calculate stop bits */
436 mr
&= ~(UART_MR2_STOP_BIT_LEN_ONE
| UART_MR2_STOP_BIT_LEN_TWO
);
437 if (termios
->c_cflag
& CSTOPB
)
438 mr
|= UART_MR2_STOP_BIT_LEN_TWO
;
440 mr
|= UART_MR2_STOP_BIT_LEN_ONE
;
442 /* set parity, bits per char, and stop bit */
443 msm_write(port
, mr
, UART_MR2
);
445 /* calculate and set hardware flow control */
446 mr
= msm_read(port
, UART_MR1
);
447 mr
&= ~(UART_MR1_CTS_CTL
| UART_MR1_RX_RDY_CTL
);
448 if (termios
->c_cflag
& CRTSCTS
) {
449 mr
|= UART_MR1_CTS_CTL
;
450 mr
|= UART_MR1_RX_RDY_CTL
;
452 msm_write(port
, mr
, UART_MR1
);
454 /* Configure status bits to ignore based on termio flags. */
455 port
->read_status_mask
= 0;
456 if (termios
->c_iflag
& INPCK
)
457 port
->read_status_mask
|= UART_SR_PAR_FRAME_ERR
;
458 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
459 port
->read_status_mask
|= UART_SR_RX_BREAK
;
461 uart_update_timeout(port
, termios
->c_cflag
, baud
);
463 spin_unlock_irqrestore(&port
->lock
, flags
);
466 static const char *msm_type(struct uart_port
*port
)
471 static void msm_release_port(struct uart_port
*port
)
473 struct platform_device
*pdev
= to_platform_device(port
->dev
);
474 struct resource
*resource
;
475 resource_size_t size
;
477 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
478 if (unlikely(!resource
))
480 size
= resource
->end
- resource
->start
+ 1;
482 release_mem_region(port
->mapbase
, size
);
483 iounmap(port
->membase
);
484 port
->membase
= NULL
;
487 static int msm_request_port(struct uart_port
*port
)
489 struct platform_device
*pdev
= to_platform_device(port
->dev
);
490 struct resource
*resource
;
491 resource_size_t size
;
493 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
494 if (unlikely(!resource
))
496 size
= resource
->end
- resource
->start
+ 1;
498 if (unlikely(!request_mem_region(port
->mapbase
, size
, "msm_serial")))
501 port
->membase
= ioremap(port
->mapbase
, size
);
502 if (!port
->membase
) {
503 release_mem_region(port
->mapbase
, size
);
510 static void msm_config_port(struct uart_port
*port
, int flags
)
512 if (flags
& UART_CONFIG_TYPE
) {
513 port
->type
= PORT_MSM
;
514 msm_request_port(port
);
518 static int msm_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
520 if (unlikely(ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_MSM
))
522 if (unlikely(port
->irq
!= ser
->irq
))
527 static void msm_power(struct uart_port
*port
, unsigned int state
,
528 unsigned int oldstate
)
530 struct msm_port
*msm_port
= UART_TO_MSM(port
);
534 clk_enable(msm_port
->clk
);
537 clk_disable(msm_port
->clk
);
540 printk(KERN_ERR
"msm_serial: Unknown PM state %d\n", state
);
544 static struct uart_ops msm_uart_pops
= {
545 .tx_empty
= msm_tx_empty
,
546 .set_mctrl
= msm_set_mctrl
,
547 .get_mctrl
= msm_get_mctrl
,
548 .stop_tx
= msm_stop_tx
,
549 .start_tx
= msm_start_tx
,
550 .stop_rx
= msm_stop_rx
,
551 .enable_ms
= msm_enable_ms
,
552 .break_ctl
= msm_break_ctl
,
553 .startup
= msm_startup
,
554 .shutdown
= msm_shutdown
,
555 .set_termios
= msm_set_termios
,
557 .release_port
= msm_release_port
,
558 .request_port
= msm_request_port
,
559 .config_port
= msm_config_port
,
560 .verify_port
= msm_verify_port
,
564 static struct msm_port msm_uart_ports
[] = {
568 .ops
= &msm_uart_pops
,
569 .flags
= UPF_BOOT_AUTOCONF
,
577 .ops
= &msm_uart_pops
,
578 .flags
= UPF_BOOT_AUTOCONF
,
586 .ops
= &msm_uart_pops
,
587 .flags
= UPF_BOOT_AUTOCONF
,
594 #define UART_NR ARRAY_SIZE(msm_uart_ports)
596 static inline struct uart_port
*get_port_from_line(unsigned int line
)
598 return &msm_uart_ports
[line
].uart
;
601 #ifdef CONFIG_SERIAL_MSM_CONSOLE
603 static void msm_console_putchar(struct uart_port
*port
, int c
)
605 while (!(msm_read(port
, UART_SR
) & UART_SR_TX_READY
))
607 msm_write(port
, c
, UART_TF
);
610 static void msm_console_write(struct console
*co
, const char *s
,
613 struct uart_port
*port
;
614 struct msm_port
*msm_port
;
616 BUG_ON(co
->index
< 0 || co
->index
>= UART_NR
);
618 port
= get_port_from_line(co
->index
);
619 msm_port
= UART_TO_MSM(port
);
621 spin_lock(&port
->lock
);
622 uart_console_write(port
, s
, count
, msm_console_putchar
);
623 spin_unlock(&port
->lock
);
626 static int __init
msm_console_setup(struct console
*co
, char *options
)
628 struct uart_port
*port
;
629 int baud
, flow
, bits
, parity
;
631 if (unlikely(co
->index
>= UART_NR
|| co
->index
< 0))
634 port
= get_port_from_line(co
->index
);
636 if (unlikely(!port
->membase
))
641 msm_init_clock(port
);
644 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
649 msm_write(port
, UART_MR2_BITS_PER_CHAR_8
| UART_MR2_STOP_BIT_LEN_ONE
,
652 if (baud
< 300 || baud
> 115200)
654 msm_set_baud_rate(port
, baud
);
658 printk(KERN_INFO
"msm_serial: console setup on port #%d\n", port
->line
);
660 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
663 static struct uart_driver msm_uart_driver
;
665 static struct console msm_console
= {
667 .write
= msm_console_write
,
668 .device
= uart_console_device
,
669 .setup
= msm_console_setup
,
670 .flags
= CON_PRINTBUFFER
,
672 .data
= &msm_uart_driver
,
675 #define MSM_CONSOLE (&msm_console)
678 #define MSM_CONSOLE NULL
681 static struct uart_driver msm_uart_driver
= {
682 .owner
= THIS_MODULE
,
683 .driver_name
= "msm_serial",
684 .dev_name
= "ttyMSM",
689 static int __init
msm_serial_probe(struct platform_device
*pdev
)
691 struct msm_port
*msm_port
;
692 struct resource
*resource
;
693 struct uart_port
*port
;
696 if (unlikely(pdev
->id
< 0 || pdev
->id
>= UART_NR
))
699 printk(KERN_INFO
"msm_serial: detected port #%d\n", pdev
->id
);
701 port
= get_port_from_line(pdev
->id
);
702 port
->dev
= &pdev
->dev
;
703 msm_port
= UART_TO_MSM(port
);
705 msm_port
->clk
= clk_get(&pdev
->dev
, "uart_clk");
706 if (unlikely(IS_ERR(msm_port
->clk
)))
707 return PTR_ERR(msm_port
->clk
);
708 port
->uartclk
= clk_get_rate(msm_port
->clk
);
710 resource
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
711 if (unlikely(!resource
))
713 port
->mapbase
= resource
->start
;
715 irq
= platform_get_irq(pdev
, 0);
716 if (unlikely(irq
< 0))
720 platform_set_drvdata(pdev
, port
);
722 return uart_add_one_port(&msm_uart_driver
, port
);
725 static int __devexit
msm_serial_remove(struct platform_device
*pdev
)
727 struct msm_port
*msm_port
= platform_get_drvdata(pdev
);
729 clk_put(msm_port
->clk
);
734 static struct platform_driver msm_platform_driver
= {
735 .remove
= msm_serial_remove
,
737 .name
= "msm_serial",
738 .owner
= THIS_MODULE
,
742 static int __init
msm_serial_init(void)
746 ret
= uart_register_driver(&msm_uart_driver
);
750 ret
= platform_driver_probe(&msm_platform_driver
, msm_serial_probe
);
752 uart_unregister_driver(&msm_uart_driver
);
754 printk(KERN_INFO
"msm_serial: driver initialized\n");
759 static void __exit
msm_serial_exit(void)
761 #ifdef CONFIG_SERIAL_MSM_CONSOLE
762 unregister_console(&msm_console
);
764 platform_driver_unregister(&msm_platform_driver
);
765 uart_unregister_driver(&msm_uart_driver
);
768 module_init(msm_serial_init
);
769 module_exit(msm_serial_exit
);
771 MODULE_AUTHOR("Robert Love <rlove@google.com>");
772 MODULE_DESCRIPTION("Driver for msm7x serial device");
773 MODULE_LICENSE("GPL");