fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / tty / serial / msm_serial.c
blobb5d779cd3c2b182743c728e71198ce75a60a9692
1 /*
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
20 #endif
22 #include <linux/atomic.h>
23 #include <linux/hrtimer.h>
24 #include <linux/module.h>
25 #include <linux/io.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>
37 #include <linux/of.h>
38 #include <linux/of_device.h>
40 #include "msm_serial.h"
42 struct msm_port {
43 struct uart_port uart;
44 char name[16];
45 struct clk *clk;
46 struct clk *pclk;
47 unsigned int imr;
48 void __iomem *gsbi_base;
49 int is_uartdm;
50 unsigned int old_snap_state;
53 static inline void wait_for_xmitr(struct uart_port *port)
55 while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
56 if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
57 break;
58 udelay(1);
60 msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
63 static void msm_stop_tx(struct uart_port *port)
65 struct msm_port *msm_port = UART_TO_MSM(port);
67 msm_port->imr &= ~UART_IMR_TXLEV;
68 msm_write(port, msm_port->imr, UART_IMR);
71 static void msm_start_tx(struct uart_port *port)
73 struct msm_port *msm_port = UART_TO_MSM(port);
75 msm_port->imr |= UART_IMR_TXLEV;
76 msm_write(port, msm_port->imr, UART_IMR);
79 static void msm_stop_rx(struct uart_port *port)
81 struct msm_port *msm_port = UART_TO_MSM(port);
83 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
84 msm_write(port, msm_port->imr, UART_IMR);
87 static void msm_enable_ms(struct uart_port *port)
89 struct msm_port *msm_port = UART_TO_MSM(port);
91 msm_port->imr |= UART_IMR_DELTA_CTS;
92 msm_write(port, msm_port->imr, UART_IMR);
95 static void handle_rx_dm(struct uart_port *port, unsigned int misr)
97 struct tty_port *tport = &port->state->port;
98 unsigned int sr;
99 int count = 0;
100 struct msm_port *msm_port = UART_TO_MSM(port);
102 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
103 port->icount.overrun++;
104 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
105 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
108 if (misr & UART_IMR_RXSTALE) {
109 count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
110 msm_port->old_snap_state;
111 msm_port->old_snap_state = 0;
112 } else {
113 count = 4 * (msm_read(port, UART_RFWR));
114 msm_port->old_snap_state += count;
117 /* TODO: Precise error reporting */
119 port->icount.rx += count;
121 while (count > 0) {
122 unsigned int c;
124 sr = msm_read(port, UART_SR);
125 if ((sr & UART_SR_RX_READY) == 0) {
126 msm_port->old_snap_state -= count;
127 break;
129 c = msm_read(port, UARTDM_RF);
130 if (sr & UART_SR_RX_BREAK) {
131 port->icount.brk++;
132 if (uart_handle_break(port))
133 continue;
134 } else if (sr & UART_SR_PAR_FRAME_ERR)
135 port->icount.frame++;
137 /* TODO: handle sysrq */
138 tty_insert_flip_string(tport, (char *)&c,
139 (count > 4) ? 4 : count);
140 count -= 4;
143 spin_unlock(&port->lock);
144 tty_flip_buffer_push(tport);
145 spin_lock(&port->lock);
147 if (misr & (UART_IMR_RXSTALE))
148 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
149 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
150 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
153 static void handle_rx(struct uart_port *port)
155 struct tty_port *tport = &port->state->port;
156 unsigned int sr;
159 * Handle overrun. My understanding of the hardware is that overrun
160 * is not tied to the RX buffer, so we handle the case out of band.
162 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
163 port->icount.overrun++;
164 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
165 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
168 /* and now the main RX loop */
169 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
170 unsigned int c;
171 char flag = TTY_NORMAL;
173 c = msm_read(port, UART_RF);
175 if (sr & UART_SR_RX_BREAK) {
176 port->icount.brk++;
177 if (uart_handle_break(port))
178 continue;
179 } else if (sr & UART_SR_PAR_FRAME_ERR) {
180 port->icount.frame++;
181 } else {
182 port->icount.rx++;
185 /* Mask conditions we're ignorning. */
186 sr &= port->read_status_mask;
188 if (sr & UART_SR_RX_BREAK) {
189 flag = TTY_BREAK;
190 } else if (sr & UART_SR_PAR_FRAME_ERR) {
191 flag = TTY_FRAME;
194 if (!uart_handle_sysrq_char(port, c))
195 tty_insert_flip_char(tport, c, flag);
198 spin_unlock(&port->lock);
199 tty_flip_buffer_push(tport);
200 spin_lock(&port->lock);
203 static void reset_dm_count(struct uart_port *port, int count)
205 wait_for_xmitr(port);
206 msm_write(port, count, UARTDM_NCF_TX);
207 msm_read(port, UARTDM_NCF_TX);
210 static void handle_tx(struct uart_port *port)
212 struct circ_buf *xmit = &port->state->xmit;
213 struct msm_port *msm_port = UART_TO_MSM(port);
214 unsigned int tx_count, num_chars;
215 unsigned int tf_pointer = 0;
217 tx_count = uart_circ_chars_pending(xmit);
218 tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
219 port->fifosize);
221 if (port->x_char) {
222 if (msm_port->is_uartdm)
223 reset_dm_count(port, tx_count + 1);
225 msm_write(port, port->x_char,
226 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
227 port->icount.tx++;
228 port->x_char = 0;
229 } else if (tx_count && msm_port->is_uartdm) {
230 reset_dm_count(port, tx_count);
233 while (tf_pointer < tx_count) {
234 int i;
235 char buf[4] = { 0 };
236 unsigned int *bf = (unsigned int *)&buf;
238 if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
239 break;
241 if (msm_port->is_uartdm)
242 num_chars = min(tx_count - tf_pointer,
243 (unsigned int)sizeof(buf));
244 else
245 num_chars = 1;
247 for (i = 0; i < num_chars; i++) {
248 buf[i] = xmit->buf[xmit->tail + i];
249 port->icount.tx++;
252 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
253 xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
254 tf_pointer += num_chars;
257 /* disable tx interrupts if nothing more to send */
258 if (uart_circ_empty(xmit))
259 msm_stop_tx(port);
261 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
262 uart_write_wakeup(port);
265 static void handle_delta_cts(struct uart_port *port)
267 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
268 port->icount.cts++;
269 wake_up_interruptible(&port->state->port.delta_msr_wait);
272 static irqreturn_t msm_irq(int irq, void *dev_id)
274 struct uart_port *port = dev_id;
275 struct msm_port *msm_port = UART_TO_MSM(port);
276 unsigned int misr;
278 spin_lock(&port->lock);
279 misr = msm_read(port, UART_MISR);
280 msm_write(port, 0, UART_IMR); /* disable interrupt */
282 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
283 if (msm_port->is_uartdm)
284 handle_rx_dm(port, misr);
285 else
286 handle_rx(port);
288 if (misr & UART_IMR_TXLEV)
289 handle_tx(port);
290 if (misr & UART_IMR_DELTA_CTS)
291 handle_delta_cts(port);
293 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
294 spin_unlock(&port->lock);
296 return IRQ_HANDLED;
299 static unsigned int msm_tx_empty(struct uart_port *port)
301 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
304 static unsigned int msm_get_mctrl(struct uart_port *port)
306 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
310 static void msm_reset(struct uart_port *port)
312 /* reset everything */
313 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
314 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
315 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
316 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
317 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
318 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
321 static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
323 unsigned int mr;
324 mr = msm_read(port, UART_MR1);
326 if (!(mctrl & TIOCM_RTS)) {
327 mr &= ~UART_MR1_RX_RDY_CTL;
328 msm_write(port, mr, UART_MR1);
329 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
330 } else {
331 mr |= UART_MR1_RX_RDY_CTL;
332 msm_write(port, mr, UART_MR1);
336 static void msm_break_ctl(struct uart_port *port, int break_ctl)
338 if (break_ctl)
339 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
340 else
341 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
344 struct msm_baud_map {
345 u16 divisor;
346 u8 code;
347 u8 rxstale;
350 static const struct msm_baud_map *
351 msm_find_best_baud(struct uart_port *port, unsigned int baud)
353 unsigned int i, divisor;
354 const struct msm_baud_map *entry;
355 static const struct msm_baud_map table[] = {
356 { 1536, 0x00, 1 },
357 { 768, 0x11, 1 },
358 { 384, 0x22, 1 },
359 { 192, 0x33, 1 },
360 { 96, 0x44, 1 },
361 { 48, 0x55, 1 },
362 { 32, 0x66, 1 },
363 { 24, 0x77, 1 },
364 { 16, 0x88, 1 },
365 { 12, 0x99, 6 },
366 { 8, 0xaa, 6 },
367 { 6, 0xbb, 6 },
368 { 4, 0xcc, 6 },
369 { 3, 0xdd, 8 },
370 { 2, 0xee, 16 },
371 { 1, 0xff, 31 },
374 divisor = uart_get_divisor(port, baud);
376 for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
377 if (entry->divisor <= divisor)
378 break;
380 return entry; /* Default to smallest divider */
383 static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
385 unsigned int rxstale, watermark;
386 struct msm_port *msm_port = UART_TO_MSM(port);
387 const struct msm_baud_map *entry;
389 entry = msm_find_best_baud(port, baud);
391 if (msm_port->is_uartdm)
392 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
394 msm_write(port, entry->code, UART_CSR);
396 /* RX stale watermark */
397 rxstale = entry->rxstale;
398 watermark = UART_IPR_STALE_LSB & rxstale;
399 watermark |= UART_IPR_RXSTALE_LAST;
400 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
401 msm_write(port, watermark, UART_IPR);
403 /* set RX watermark */
404 watermark = (port->fifosize * 3) / 4;
405 msm_write(port, watermark, UART_RFWR);
407 /* set TX watermark */
408 msm_write(port, 10, UART_TFWR);
410 if (msm_port->is_uartdm) {
411 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
412 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
413 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
416 return baud;
420 static void msm_init_clock(struct uart_port *port)
422 struct msm_port *msm_port = UART_TO_MSM(port);
424 clk_prepare_enable(msm_port->clk);
425 clk_prepare_enable(msm_port->pclk);
426 msm_serial_set_mnd_regs(port);
429 static int msm_startup(struct uart_port *port)
431 struct msm_port *msm_port = UART_TO_MSM(port);
432 unsigned int data, rfr_level;
433 int ret;
435 snprintf(msm_port->name, sizeof(msm_port->name),
436 "msm_serial%d", port->line);
438 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
439 msm_port->name, port);
440 if (unlikely(ret))
441 return ret;
443 msm_init_clock(port);
445 if (likely(port->fifosize > 12))
446 rfr_level = port->fifosize - 12;
447 else
448 rfr_level = port->fifosize;
450 /* set automatic RFR level */
451 data = msm_read(port, UART_MR1);
452 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
453 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
454 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
455 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
456 msm_write(port, data, UART_MR1);
458 /* make sure that RXSTALE count is non-zero */
459 data = msm_read(port, UART_IPR);
460 if (unlikely(!data)) {
461 data |= UART_IPR_RXSTALE_LAST;
462 data |= UART_IPR_STALE_LSB;
463 msm_write(port, data, UART_IPR);
466 data = 0;
467 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
468 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
469 msm_reset(port);
470 data = UART_CR_TX_ENABLE;
473 data |= UART_CR_RX_ENABLE;
474 msm_write(port, data, UART_CR); /* enable TX & RX */
476 /* Make sure IPR is not 0 to start with*/
477 if (msm_port->is_uartdm)
478 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
480 /* turn on RX and CTS interrupts */
481 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
482 UART_IMR_CURRENT_CTS;
484 if (msm_port->is_uartdm) {
485 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
486 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
487 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
490 msm_write(port, msm_port->imr, UART_IMR);
491 return 0;
494 static void msm_shutdown(struct uart_port *port)
496 struct msm_port *msm_port = UART_TO_MSM(port);
498 msm_port->imr = 0;
499 msm_write(port, 0, UART_IMR); /* disable interrupts */
501 clk_disable_unprepare(msm_port->clk);
503 free_irq(port->irq, port);
506 static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
507 struct ktermios *old)
509 unsigned long flags;
510 unsigned int baud, mr;
512 spin_lock_irqsave(&port->lock, flags);
514 /* calculate and set baud rate */
515 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
516 baud = msm_set_baud_rate(port, baud);
517 if (tty_termios_baud_rate(termios))
518 tty_termios_encode_baud_rate(termios, baud, baud);
520 /* calculate parity */
521 mr = msm_read(port, UART_MR2);
522 mr &= ~UART_MR2_PARITY_MODE;
523 if (termios->c_cflag & PARENB) {
524 if (termios->c_cflag & PARODD)
525 mr |= UART_MR2_PARITY_MODE_ODD;
526 else if (termios->c_cflag & CMSPAR)
527 mr |= UART_MR2_PARITY_MODE_SPACE;
528 else
529 mr |= UART_MR2_PARITY_MODE_EVEN;
532 /* calculate bits per char */
533 mr &= ~UART_MR2_BITS_PER_CHAR;
534 switch (termios->c_cflag & CSIZE) {
535 case CS5:
536 mr |= UART_MR2_BITS_PER_CHAR_5;
537 break;
538 case CS6:
539 mr |= UART_MR2_BITS_PER_CHAR_6;
540 break;
541 case CS7:
542 mr |= UART_MR2_BITS_PER_CHAR_7;
543 break;
544 case CS8:
545 default:
546 mr |= UART_MR2_BITS_PER_CHAR_8;
547 break;
550 /* calculate stop bits */
551 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
552 if (termios->c_cflag & CSTOPB)
553 mr |= UART_MR2_STOP_BIT_LEN_TWO;
554 else
555 mr |= UART_MR2_STOP_BIT_LEN_ONE;
557 /* set parity, bits per char, and stop bit */
558 msm_write(port, mr, UART_MR2);
560 /* calculate and set hardware flow control */
561 mr = msm_read(port, UART_MR1);
562 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
563 if (termios->c_cflag & CRTSCTS) {
564 mr |= UART_MR1_CTS_CTL;
565 mr |= UART_MR1_RX_RDY_CTL;
567 msm_write(port, mr, UART_MR1);
569 /* Configure status bits to ignore based on termio flags. */
570 port->read_status_mask = 0;
571 if (termios->c_iflag & INPCK)
572 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
573 if (termios->c_iflag & (BRKINT | PARMRK))
574 port->read_status_mask |= UART_SR_RX_BREAK;
576 uart_update_timeout(port, termios->c_cflag, baud);
578 spin_unlock_irqrestore(&port->lock, flags);
581 static const char *msm_type(struct uart_port *port)
583 return "MSM";
586 static void msm_release_port(struct uart_port *port)
588 struct platform_device *pdev = to_platform_device(port->dev);
589 struct msm_port *msm_port = UART_TO_MSM(port);
590 struct resource *uart_resource;
591 struct resource *gsbi_resource;
592 resource_size_t size;
594 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
595 if (unlikely(!uart_resource))
596 return;
597 size = resource_size(uart_resource);
599 release_mem_region(port->mapbase, size);
600 iounmap(port->membase);
601 port->membase = NULL;
603 if (msm_port->gsbi_base) {
604 writel_relaxed(GSBI_PROTOCOL_IDLE,
605 msm_port->gsbi_base + GSBI_CONTROL);
607 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
608 if (unlikely(!gsbi_resource))
609 return;
611 size = resource_size(gsbi_resource);
612 release_mem_region(gsbi_resource->start, size);
613 iounmap(msm_port->gsbi_base);
614 msm_port->gsbi_base = NULL;
618 static int msm_request_port(struct uart_port *port)
620 struct msm_port *msm_port = UART_TO_MSM(port);
621 struct platform_device *pdev = to_platform_device(port->dev);
622 struct resource *uart_resource;
623 struct resource *gsbi_resource;
624 resource_size_t size;
625 int ret;
627 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
628 if (unlikely(!uart_resource))
629 return -ENXIO;
631 size = resource_size(uart_resource);
633 if (!request_mem_region(port->mapbase, size, "msm_serial"))
634 return -EBUSY;
636 port->membase = ioremap(port->mapbase, size);
637 if (!port->membase) {
638 ret = -EBUSY;
639 goto fail_release_port;
642 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
643 /* Is this a GSBI-based port? */
644 if (gsbi_resource) {
645 size = resource_size(gsbi_resource);
647 if (!request_mem_region(gsbi_resource->start, size,
648 "msm_serial")) {
649 ret = -EBUSY;
650 goto fail_release_port_membase;
653 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
654 if (!msm_port->gsbi_base) {
655 ret = -EBUSY;
656 goto fail_release_gsbi;
660 return 0;
662 fail_release_gsbi:
663 release_mem_region(gsbi_resource->start, size);
664 fail_release_port_membase:
665 iounmap(port->membase);
666 fail_release_port:
667 release_mem_region(port->mapbase, size);
668 return ret;
671 static void msm_config_port(struct uart_port *port, int flags)
673 struct msm_port *msm_port = UART_TO_MSM(port);
674 int ret;
675 if (flags & UART_CONFIG_TYPE) {
676 port->type = PORT_MSM;
677 ret = msm_request_port(port);
678 if (ret)
679 return;
681 if (msm_port->gsbi_base)
682 writel_relaxed(GSBI_PROTOCOL_UART,
683 msm_port->gsbi_base + GSBI_CONTROL);
686 static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
688 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
689 return -EINVAL;
690 if (unlikely(port->irq != ser->irq))
691 return -EINVAL;
692 return 0;
695 static void msm_power(struct uart_port *port, unsigned int state,
696 unsigned int oldstate)
698 struct msm_port *msm_port = UART_TO_MSM(port);
700 switch (state) {
701 case 0:
702 clk_prepare_enable(msm_port->clk);
703 clk_prepare_enable(msm_port->pclk);
704 break;
705 case 3:
706 clk_disable_unprepare(msm_port->clk);
707 clk_disable_unprepare(msm_port->pclk);
708 break;
709 default:
710 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
714 static struct uart_ops msm_uart_pops = {
715 .tx_empty = msm_tx_empty,
716 .set_mctrl = msm_set_mctrl,
717 .get_mctrl = msm_get_mctrl,
718 .stop_tx = msm_stop_tx,
719 .start_tx = msm_start_tx,
720 .stop_rx = msm_stop_rx,
721 .enable_ms = msm_enable_ms,
722 .break_ctl = msm_break_ctl,
723 .startup = msm_startup,
724 .shutdown = msm_shutdown,
725 .set_termios = msm_set_termios,
726 .type = msm_type,
727 .release_port = msm_release_port,
728 .request_port = msm_request_port,
729 .config_port = msm_config_port,
730 .verify_port = msm_verify_port,
731 .pm = msm_power,
734 static struct msm_port msm_uart_ports[] = {
736 .uart = {
737 .iotype = UPIO_MEM,
738 .ops = &msm_uart_pops,
739 .flags = UPF_BOOT_AUTOCONF,
740 .fifosize = 64,
741 .line = 0,
745 .uart = {
746 .iotype = UPIO_MEM,
747 .ops = &msm_uart_pops,
748 .flags = UPF_BOOT_AUTOCONF,
749 .fifosize = 64,
750 .line = 1,
754 .uart = {
755 .iotype = UPIO_MEM,
756 .ops = &msm_uart_pops,
757 .flags = UPF_BOOT_AUTOCONF,
758 .fifosize = 64,
759 .line = 2,
764 #define UART_NR ARRAY_SIZE(msm_uart_ports)
766 static inline struct uart_port *get_port_from_line(unsigned int line)
768 return &msm_uart_ports[line].uart;
771 #ifdef CONFIG_SERIAL_MSM_CONSOLE
772 static void msm_console_write(struct console *co, const char *s,
773 unsigned int count)
775 int i;
776 struct uart_port *port;
777 struct msm_port *msm_port;
778 int num_newlines = 0;
779 bool replaced = false;
781 BUG_ON(co->index < 0 || co->index >= UART_NR);
783 port = get_port_from_line(co->index);
784 msm_port = UART_TO_MSM(port);
786 /* Account for newlines that will get a carriage return added */
787 for (i = 0; i < count; i++)
788 if (s[i] == '\n')
789 num_newlines++;
790 count += num_newlines;
792 spin_lock(&port->lock);
793 if (msm_port->is_uartdm)
794 reset_dm_count(port, count);
796 i = 0;
797 while (i < count) {
798 int j;
799 unsigned int num_chars;
800 char buf[4] = { 0 };
801 unsigned int *bf = (unsigned int *)&buf;
803 if (msm_port->is_uartdm)
804 num_chars = min(count - i, (unsigned int)sizeof(buf));
805 else
806 num_chars = 1;
808 for (j = 0; j < num_chars; j++) {
809 char c = *s;
811 if (c == '\n' && !replaced) {
812 buf[j] = '\r';
813 j++;
814 replaced = true;
816 if (j < num_chars) {
817 buf[j] = c;
818 s++;
819 replaced = false;
823 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
824 cpu_relax();
826 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
827 i += num_chars;
829 spin_unlock(&port->lock);
832 static int __init msm_console_setup(struct console *co, char *options)
834 struct uart_port *port;
835 struct msm_port *msm_port;
836 int baud, flow, bits, parity;
838 if (unlikely(co->index >= UART_NR || co->index < 0))
839 return -ENXIO;
841 port = get_port_from_line(co->index);
842 msm_port = UART_TO_MSM(port);
844 if (unlikely(!port->membase))
845 return -ENXIO;
847 msm_init_clock(port);
849 if (options)
850 uart_parse_options(options, &baud, &parity, &bits, &flow);
852 bits = 8;
853 parity = 'n';
854 flow = 'n';
855 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
856 UART_MR2); /* 8N1 */
858 if (baud < 300 || baud > 115200)
859 baud = 115200;
860 msm_set_baud_rate(port, baud);
862 msm_reset(port);
864 if (msm_port->is_uartdm) {
865 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
866 msm_write(port, UART_CR_TX_ENABLE, UART_CR);
869 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
871 return uart_set_options(port, co, baud, parity, bits, flow);
874 static struct uart_driver msm_uart_driver;
876 static struct console msm_console = {
877 .name = "ttyMSM",
878 .write = msm_console_write,
879 .device = uart_console_device,
880 .setup = msm_console_setup,
881 .flags = CON_PRINTBUFFER,
882 .index = -1,
883 .data = &msm_uart_driver,
886 #define MSM_CONSOLE (&msm_console)
888 #else
889 #define MSM_CONSOLE NULL
890 #endif
892 static struct uart_driver msm_uart_driver = {
893 .owner = THIS_MODULE,
894 .driver_name = "msm_serial",
895 .dev_name = "ttyMSM",
896 .nr = UART_NR,
897 .cons = MSM_CONSOLE,
900 static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
902 static const struct of_device_id msm_uartdm_table[] = {
903 { .compatible = "qcom,msm-uartdm" },
907 static int __init msm_serial_probe(struct platform_device *pdev)
909 struct msm_port *msm_port;
910 struct resource *resource;
911 struct uart_port *port;
912 int irq;
914 if (pdev->id == -1)
915 pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;
917 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
918 return -ENXIO;
920 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
922 port = get_port_from_line(pdev->id);
923 port->dev = &pdev->dev;
924 msm_port = UART_TO_MSM(port);
926 if (of_match_device(msm_uartdm_table, &pdev->dev))
927 msm_port->is_uartdm = 1;
928 else
929 msm_port->is_uartdm = 0;
931 msm_port->clk = devm_clk_get(&pdev->dev, "core");
932 if (IS_ERR(msm_port->clk))
933 return PTR_ERR(msm_port->clk);
935 if (msm_port->is_uartdm) {
936 msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
937 if (IS_ERR(msm_port->pclk))
938 return PTR_ERR(msm_port->pclk);
940 clk_set_rate(msm_port->clk, 1843200);
943 port->uartclk = clk_get_rate(msm_port->clk);
944 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
947 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
948 if (unlikely(!resource))
949 return -ENXIO;
950 port->mapbase = resource->start;
952 irq = platform_get_irq(pdev, 0);
953 if (unlikely(irq < 0))
954 return -ENXIO;
955 port->irq = irq;
957 platform_set_drvdata(pdev, port);
959 return uart_add_one_port(&msm_uart_driver, port);
962 static int msm_serial_remove(struct platform_device *pdev)
964 struct uart_port *port = platform_get_drvdata(pdev);
966 uart_remove_one_port(&msm_uart_driver, port);
968 return 0;
971 static struct of_device_id msm_match_table[] = {
972 { .compatible = "qcom,msm-uart" },
973 { .compatible = "qcom,msm-uartdm" },
977 static struct platform_driver msm_platform_driver = {
978 .remove = msm_serial_remove,
979 .driver = {
980 .name = "msm_serial",
981 .owner = THIS_MODULE,
982 .of_match_table = msm_match_table,
986 static int __init msm_serial_init(void)
988 int ret;
990 ret = uart_register_driver(&msm_uart_driver);
991 if (unlikely(ret))
992 return ret;
994 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
995 if (unlikely(ret))
996 uart_unregister_driver(&msm_uart_driver);
998 printk(KERN_INFO "msm_serial: driver initialized\n");
1000 return ret;
1003 static void __exit msm_serial_exit(void)
1005 #ifdef CONFIG_SERIAL_MSM_CONSOLE
1006 unregister_console(&msm_console);
1007 #endif
1008 platform_driver_unregister(&msm_platform_driver);
1009 uart_unregister_driver(&msm_uart_driver);
1012 module_init(msm_serial_init);
1013 module_exit(msm_serial_exit);
1015 MODULE_AUTHOR("Robert Love <rlove@google.com>");
1016 MODULE_DESCRIPTION("Driver for msm7x serial device");
1017 MODULE_LICENSE("GPL");