blob: e70d4e80d4a88fcb73d36c749484fb013120ba32 [file] [log] [blame]
Robert Love04896a72009-06-22 18:43:11 +01001/*
Jovi Zhang99edb3d2011-03-30 05:30:41 -04002 * Driver for msm7k serial device and console
Robert Love04896a72009-06-22 18:43:11 +01003 *
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -08006 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Robert Love04896a72009-06-22 18:43:11 +01007 *
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.
11 *
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.
16 */
17
18#if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
David Browncfdad2a2011-08-04 01:55:24 -070022#include <linux/atomic.h>
Robert Love04896a72009-06-22 18:43:11 +010023#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>
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080036#include <linux/delay.h>
David Browncfdad2a2011-08-04 01:55:24 -070037#include <linux/of.h>
38#include <linux/of_device.h>
Robert Love04896a72009-06-22 18:43:11 +010039
40#include "msm_serial.h"
41
Stephen Boydf7e54d72014-01-14 12:34:55 -080042enum {
43 UARTDM_1P1 = 1,
44 UARTDM_1P2,
45 UARTDM_1P3,
46 UARTDM_1P4,
47};
48
Robert Love04896a72009-06-22 18:43:11 +010049struct msm_port {
50 struct uart_port uart;
51 char name[16];
52 struct clk *clk;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080053 struct clk *pclk;
Robert Love04896a72009-06-22 18:43:11 +010054 unsigned int imr;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080055 int is_uartdm;
56 unsigned int old_snap_state;
Robert Love04896a72009-06-22 18:43:11 +010057};
58
Stephen Boyd4a5662d2013-07-24 11:37:28 -070059static inline void wait_for_xmitr(struct uart_port *port)
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080060{
Stephen Boyd4a5662d2013-07-24 11:37:28 -070061 while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
62 if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
63 break;
64 udelay(1);
65 }
66 msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080067}
68
Robert Love04896a72009-06-22 18:43:11 +010069static void msm_stop_tx(struct uart_port *port)
70{
71 struct msm_port *msm_port = UART_TO_MSM(port);
72
73 msm_port->imr &= ~UART_IMR_TXLEV;
74 msm_write(port, msm_port->imr, UART_IMR);
75}
76
77static void msm_start_tx(struct uart_port *port)
78{
79 struct msm_port *msm_port = UART_TO_MSM(port);
80
81 msm_port->imr |= UART_IMR_TXLEV;
82 msm_write(port, msm_port->imr, UART_IMR);
83}
84
85static void msm_stop_rx(struct uart_port *port)
86{
87 struct msm_port *msm_port = UART_TO_MSM(port);
88
89 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
90 msm_write(port, msm_port->imr, UART_IMR);
91}
92
93static void msm_enable_ms(struct uart_port *port)
94{
95 struct msm_port *msm_port = UART_TO_MSM(port);
96
97 msm_port->imr |= UART_IMR_DELTA_CTS;
98 msm_write(port, msm_port->imr, UART_IMR);
99}
100
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800101static void handle_rx_dm(struct uart_port *port, unsigned int misr)
102{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100103 struct tty_port *tport = &port->state->port;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800104 unsigned int sr;
105 int count = 0;
106 struct msm_port *msm_port = UART_TO_MSM(port);
107
108 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
109 port->icount.overrun++;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100110 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800111 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
112 }
113
114 if (misr & UART_IMR_RXSTALE) {
115 count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
116 msm_port->old_snap_state;
117 msm_port->old_snap_state = 0;
118 } else {
119 count = 4 * (msm_read(port, UART_RFWR));
120 msm_port->old_snap_state += count;
121 }
122
123 /* TODO: Precise error reporting */
124
125 port->icount.rx += count;
126
127 while (count > 0) {
Stephen Boyd68252422014-06-30 14:54:01 -0700128 unsigned char buf[4];
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800129
130 sr = msm_read(port, UART_SR);
131 if ((sr & UART_SR_RX_READY) == 0) {
132 msm_port->old_snap_state -= count;
133 break;
134 }
Stephen Boyd68252422014-06-30 14:54:01 -0700135 ioread32_rep(port->membase + UARTDM_RF, buf, 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800136 if (sr & UART_SR_RX_BREAK) {
137 port->icount.brk++;
138 if (uart_handle_break(port))
139 continue;
140 } else if (sr & UART_SR_PAR_FRAME_ERR)
141 port->icount.frame++;
142
143 /* TODO: handle sysrq */
Stephen Boyd68252422014-06-30 14:54:01 -0700144 tty_insert_flip_string(tport, buf, min(count, 4));
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800145 count -= 4;
146 }
147
Viresh Kumarf77232d2013-08-19 20:14:20 +0530148 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100149 tty_flip_buffer_push(tport);
Viresh Kumarf77232d2013-08-19 20:14:20 +0530150 spin_lock(&port->lock);
151
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800152 if (misr & (UART_IMR_RXSTALE))
153 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
154 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
155 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
156}
157
Robert Love04896a72009-06-22 18:43:11 +0100158static void handle_rx(struct uart_port *port)
159{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100160 struct tty_port *tport = &port->state->port;
Robert Love04896a72009-06-22 18:43:11 +0100161 unsigned int sr;
162
163 /*
164 * Handle overrun. My understanding of the hardware is that overrun
165 * is not tied to the RX buffer, so we handle the case out of band.
166 */
167 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
168 port->icount.overrun++;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100169 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Robert Love04896a72009-06-22 18:43:11 +0100170 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
171 }
172
173 /* and now the main RX loop */
174 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
175 unsigned int c;
176 char flag = TTY_NORMAL;
177
178 c = msm_read(port, UART_RF);
179
180 if (sr & UART_SR_RX_BREAK) {
181 port->icount.brk++;
182 if (uart_handle_break(port))
183 continue;
184 } else if (sr & UART_SR_PAR_FRAME_ERR) {
185 port->icount.frame++;
186 } else {
187 port->icount.rx++;
188 }
189
190 /* Mask conditions we're ignorning. */
191 sr &= port->read_status_mask;
192
Kiran Padwalddea3922014-08-05 13:21:59 +0530193 if (sr & UART_SR_RX_BREAK)
Robert Love04896a72009-06-22 18:43:11 +0100194 flag = TTY_BREAK;
Kiran Padwalddea3922014-08-05 13:21:59 +0530195 else if (sr & UART_SR_PAR_FRAME_ERR)
Robert Love04896a72009-06-22 18:43:11 +0100196 flag = TTY_FRAME;
Robert Love04896a72009-06-22 18:43:11 +0100197
198 if (!uart_handle_sysrq_char(port, c))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100199 tty_insert_flip_char(tport, c, flag);
Robert Love04896a72009-06-22 18:43:11 +0100200 }
201
Viresh Kumarf77232d2013-08-19 20:14:20 +0530202 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100203 tty_flip_buffer_push(tport);
Viresh Kumarf77232d2013-08-19 20:14:20 +0530204 spin_lock(&port->lock);
Robert Love04896a72009-06-22 18:43:11 +0100205}
206
Stephen Boyd17fae282013-07-24 11:37:31 -0700207static void reset_dm_count(struct uart_port *port, int count)
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800208{
Stephen Boyd4a5662d2013-07-24 11:37:28 -0700209 wait_for_xmitr(port);
Stephen Boyd17fae282013-07-24 11:37:31 -0700210 msm_write(port, count, UARTDM_NCF_TX);
Stephen Boyd4a5662d2013-07-24 11:37:28 -0700211 msm_read(port, UARTDM_NCF_TX);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800212}
213
Robert Love04896a72009-06-22 18:43:11 +0100214static void handle_tx(struct uart_port *port)
215{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700216 struct circ_buf *xmit = &port->state->xmit;
Robert Love04896a72009-06-22 18:43:11 +0100217 struct msm_port *msm_port = UART_TO_MSM(port);
Stephen Boyd17fae282013-07-24 11:37:31 -0700218 unsigned int tx_count, num_chars;
219 unsigned int tf_pointer = 0;
Stephen Boyd68252422014-06-30 14:54:01 -0700220 void __iomem *tf;
221
222 if (msm_port->is_uartdm)
223 tf = port->membase + UARTDM_TF;
224 else
225 tf = port->membase + UART_TF;
Stephen Boyd17fae282013-07-24 11:37:31 -0700226
227 tx_count = uart_circ_chars_pending(xmit);
228 tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
229 port->fifosize);
Robert Love04896a72009-06-22 18:43:11 +0100230
231 if (port->x_char) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800232 if (msm_port->is_uartdm)
Stephen Boyd17fae282013-07-24 11:37:31 -0700233 reset_dm_count(port, tx_count + 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800234
Stephen Boyd68252422014-06-30 14:54:01 -0700235 iowrite8_rep(tf, &port->x_char, 1);
Robert Love04896a72009-06-22 18:43:11 +0100236 port->icount.tx++;
237 port->x_char = 0;
Stephen Boyd17fae282013-07-24 11:37:31 -0700238 } else if (tx_count && msm_port->is_uartdm) {
239 reset_dm_count(port, tx_count);
Robert Love04896a72009-06-22 18:43:11 +0100240 }
241
Stephen Boyd17fae282013-07-24 11:37:31 -0700242 while (tf_pointer < tx_count) {
243 int i;
244 char buf[4] = { 0 };
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800245
Stephen Boyd17fae282013-07-24 11:37:31 -0700246 if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
Robert Love04896a72009-06-22 18:43:11 +0100247 break;
Robert Love04896a72009-06-22 18:43:11 +0100248
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800249 if (msm_port->is_uartdm)
Jingoo Han4f749f22013-08-08 17:38:20 +0900250 num_chars = min(tx_count - tf_pointer,
251 (unsigned int)sizeof(buf));
Stephen Boyd17fae282013-07-24 11:37:31 -0700252 else
253 num_chars = 1;
Robert Love04896a72009-06-22 18:43:11 +0100254
Stephen Boyd17fae282013-07-24 11:37:31 -0700255 for (i = 0; i < num_chars; i++) {
256 buf[i] = xmit->buf[xmit->tail + i];
257 port->icount.tx++;
258 }
259
Stephen Boyd68252422014-06-30 14:54:01 -0700260 iowrite32_rep(tf, buf, 1);
Stephen Boyd17fae282013-07-24 11:37:31 -0700261 xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
262 tf_pointer += num_chars;
Robert Love04896a72009-06-22 18:43:11 +0100263 }
264
Stephen Boyd17fae282013-07-24 11:37:31 -0700265 /* disable tx interrupts if nothing more to send */
266 if (uart_circ_empty(xmit))
267 msm_stop_tx(port);
268
Robert Love04896a72009-06-22 18:43:11 +0100269 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
270 uart_write_wakeup(port);
271}
272
273static void handle_delta_cts(struct uart_port *port)
274{
275 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
276 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700277 wake_up_interruptible(&port->state->port.delta_msr_wait);
Robert Love04896a72009-06-22 18:43:11 +0100278}
279
280static irqreturn_t msm_irq(int irq, void *dev_id)
281{
282 struct uart_port *port = dev_id;
283 struct msm_port *msm_port = UART_TO_MSM(port);
284 unsigned int misr;
285
286 spin_lock(&port->lock);
287 misr = msm_read(port, UART_MISR);
288 msm_write(port, 0, UART_IMR); /* disable interrupt */
289
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800290 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
291 if (msm_port->is_uartdm)
292 handle_rx_dm(port, misr);
293 else
294 handle_rx(port);
295 }
Robert Love04896a72009-06-22 18:43:11 +0100296 if (misr & UART_IMR_TXLEV)
297 handle_tx(port);
298 if (misr & UART_IMR_DELTA_CTS)
299 handle_delta_cts(port);
300
301 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
302 spin_unlock(&port->lock);
303
304 return IRQ_HANDLED;
305}
306
307static unsigned int msm_tx_empty(struct uart_port *port)
308{
309 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
310}
311
312static unsigned int msm_get_mctrl(struct uart_port *port)
313{
314 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
315}
316
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800317static void msm_reset(struct uart_port *port)
318{
Stephen Boydf7e54d72014-01-14 12:34:55 -0800319 struct msm_port *msm_port = UART_TO_MSM(port);
320
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800321 /* reset everything */
322 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
323 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
324 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
325 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
326 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
327 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
Stephen Boydf7e54d72014-01-14 12:34:55 -0800328
329 /* Disable DM modes */
330 if (msm_port->is_uartdm)
331 msm_write(port, 0, UARTDM_DMEN);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800332}
333
Stephen Boydf8fb9522013-07-24 11:37:29 -0700334static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
Robert Love04896a72009-06-22 18:43:11 +0100335{
336 unsigned int mr;
Kiran Padwale919cef2014-08-05 13:22:00 +0530337
Robert Love04896a72009-06-22 18:43:11 +0100338 mr = msm_read(port, UART_MR1);
339
340 if (!(mctrl & TIOCM_RTS)) {
341 mr &= ~UART_MR1_RX_RDY_CTL;
342 msm_write(port, mr, UART_MR1);
343 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
344 } else {
345 mr |= UART_MR1_RX_RDY_CTL;
346 msm_write(port, mr, UART_MR1);
347 }
348}
349
350static void msm_break_ctl(struct uart_port *port, int break_ctl)
351{
352 if (break_ctl)
353 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
354 else
355 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
356}
357
Stephen Boyd6909dad2013-07-24 11:37:30 -0700358struct msm_baud_map {
359 u16 divisor;
360 u8 code;
361 u8 rxstale;
362};
363
364static const struct msm_baud_map *
365msm_find_best_baud(struct uart_port *port, unsigned int baud)
366{
367 unsigned int i, divisor;
368 const struct msm_baud_map *entry;
369 static const struct msm_baud_map table[] = {
370 { 1536, 0x00, 1 },
371 { 768, 0x11, 1 },
372 { 384, 0x22, 1 },
373 { 192, 0x33, 1 },
374 { 96, 0x44, 1 },
375 { 48, 0x55, 1 },
376 { 32, 0x66, 1 },
377 { 24, 0x77, 1 },
378 { 16, 0x88, 1 },
379 { 12, 0x99, 6 },
380 { 8, 0xaa, 6 },
381 { 6, 0xbb, 6 },
382 { 4, 0xcc, 6 },
383 { 3, 0xdd, 8 },
384 { 2, 0xee, 16 },
385 { 1, 0xff, 31 },
386 };
387
388 divisor = uart_get_divisor(port, baud);
389
390 for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
391 if (entry->divisor <= divisor)
392 break;
393
394 return entry; /* Default to smallest divider */
395}
396
Alan Cox44da59e2009-06-22 18:43:18 +0100397static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
Robert Love04896a72009-06-22 18:43:11 +0100398{
Stephen Boyd6909dad2013-07-24 11:37:30 -0700399 unsigned int rxstale, watermark;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800400 struct msm_port *msm_port = UART_TO_MSM(port);
Stephen Boyd6909dad2013-07-24 11:37:30 -0700401 const struct msm_baud_map *entry;
Robert Love04896a72009-06-22 18:43:11 +0100402
Stephen Boyd6909dad2013-07-24 11:37:30 -0700403 entry = msm_find_best_baud(port, baud);
Robert Love04896a72009-06-22 18:43:11 +0100404
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800405 if (msm_port->is_uartdm)
406 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
407
Stephen Boyd6909dad2013-07-24 11:37:30 -0700408 msm_write(port, entry->code, UART_CSR);
Robert Love04896a72009-06-22 18:43:11 +0100409
410 /* RX stale watermark */
Stephen Boyd6909dad2013-07-24 11:37:30 -0700411 rxstale = entry->rxstale;
Robert Love04896a72009-06-22 18:43:11 +0100412 watermark = UART_IPR_STALE_LSB & rxstale;
413 watermark |= UART_IPR_RXSTALE_LAST;
414 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
415 msm_write(port, watermark, UART_IPR);
416
417 /* set RX watermark */
418 watermark = (port->fifosize * 3) / 4;
419 msm_write(port, watermark, UART_RFWR);
420
421 /* set TX watermark */
422 msm_write(port, 10, UART_TFWR);
Alan Cox44da59e2009-06-22 18:43:18 +0100423
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800424 if (msm_port->is_uartdm) {
425 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
426 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
427 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
428 }
429
Alan Cox44da59e2009-06-22 18:43:18 +0100430 return baud;
Robert Love04896a72009-06-22 18:43:11 +0100431}
432
Robert Love04896a72009-06-22 18:43:11 +0100433static void msm_init_clock(struct uart_port *port)
434{
435 struct msm_port *msm_port = UART_TO_MSM(port);
436
Stephen Boydf98cf832013-06-17 10:43:08 -0700437 clk_prepare_enable(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700438 clk_prepare_enable(msm_port->pclk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700439 msm_serial_set_mnd_regs(port);
Robert Love04896a72009-06-22 18:43:11 +0100440}
441
442static int msm_startup(struct uart_port *port)
443{
444 struct msm_port *msm_port = UART_TO_MSM(port);
445 unsigned int data, rfr_level;
446 int ret;
447
448 snprintf(msm_port->name, sizeof(msm_port->name),
449 "msm_serial%d", port->line);
450
451 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
452 msm_port->name, port);
453 if (unlikely(ret))
454 return ret;
455
456 msm_init_clock(port);
457
458 if (likely(port->fifosize > 12))
459 rfr_level = port->fifosize - 12;
460 else
461 rfr_level = port->fifosize;
462
463 /* set automatic RFR level */
464 data = msm_read(port, UART_MR1);
465 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
466 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
467 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
468 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
469 msm_write(port, data, UART_MR1);
470
471 /* make sure that RXSTALE count is non-zero */
472 data = msm_read(port, UART_IPR);
473 if (unlikely(!data)) {
474 data |= UART_IPR_RXSTALE_LAST;
475 data |= UART_IPR_STALE_LSB;
476 msm_write(port, data, UART_IPR);
477 }
478
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800479 data = 0;
480 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
481 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
482 msm_reset(port);
483 data = UART_CR_TX_ENABLE;
484 }
Robert Love04896a72009-06-22 18:43:11 +0100485
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800486 data |= UART_CR_RX_ENABLE;
487 msm_write(port, data, UART_CR); /* enable TX & RX */
488
489 /* Make sure IPR is not 0 to start with*/
490 if (msm_port->is_uartdm)
491 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
Robert Love04896a72009-06-22 18:43:11 +0100492
493 /* turn on RX and CTS interrupts */
494 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
495 UART_IMR_CURRENT_CTS;
Robert Love04896a72009-06-22 18:43:11 +0100496
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800497 if (msm_port->is_uartdm) {
498 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
499 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
500 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
501 }
502
503 msm_write(port, msm_port->imr, UART_IMR);
Robert Love04896a72009-06-22 18:43:11 +0100504 return 0;
505}
506
507static void msm_shutdown(struct uart_port *port)
508{
509 struct msm_port *msm_port = UART_TO_MSM(port);
510
511 msm_port->imr = 0;
512 msm_write(port, 0, UART_IMR); /* disable interrupts */
513
Stephen Boydf98cf832013-06-17 10:43:08 -0700514 clk_disable_unprepare(msm_port->clk);
Robert Love04896a72009-06-22 18:43:11 +0100515
516 free_irq(port->irq, port);
517}
518
519static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
520 struct ktermios *old)
521{
522 unsigned long flags;
523 unsigned int baud, mr;
524
525 spin_lock_irqsave(&port->lock, flags);
526
527 /* calculate and set baud rate */
528 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
Alan Cox44da59e2009-06-22 18:43:18 +0100529 baud = msm_set_baud_rate(port, baud);
530 if (tty_termios_baud_rate(termios))
531 tty_termios_encode_baud_rate(termios, baud, baud);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800532
Robert Love04896a72009-06-22 18:43:11 +0100533 /* 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;
541 else
542 mr |= UART_MR2_PARITY_MODE_EVEN;
543 }
544
545 /* calculate bits per char */
546 mr &= ~UART_MR2_BITS_PER_CHAR;
547 switch (termios->c_cflag & CSIZE) {
548 case CS5:
549 mr |= UART_MR2_BITS_PER_CHAR_5;
550 break;
551 case CS6:
552 mr |= UART_MR2_BITS_PER_CHAR_6;
553 break;
554 case CS7:
555 mr |= UART_MR2_BITS_PER_CHAR_7;
556 break;
557 case CS8:
558 default:
559 mr |= UART_MR2_BITS_PER_CHAR_8;
560 break;
561 }
562
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;
567 else
568 mr |= UART_MR2_STOP_BIT_LEN_ONE;
569
570 /* set parity, bits per char, and stop bit */
571 msm_write(port, mr, UART_MR2);
572
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;
579 }
580 msm_write(port, mr, UART_MR1);
581
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;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400586 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Robert Love04896a72009-06-22 18:43:11 +0100587 port->read_status_mask |= UART_SR_RX_BREAK;
588
589 uart_update_timeout(port, termios->c_cflag, baud);
590
591 spin_unlock_irqrestore(&port->lock, flags);
592}
593
594static const char *msm_type(struct uart_port *port)
595{
596 return "MSM";
597}
598
599static void msm_release_port(struct uart_port *port)
600{
601 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800602 struct resource *uart_resource;
Robert Love04896a72009-06-22 18:43:11 +0100603 resource_size_t size;
604
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800605 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
606 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100607 return;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800608 size = resource_size(uart_resource);
Robert Love04896a72009-06-22 18:43:11 +0100609
610 release_mem_region(port->mapbase, size);
611 iounmap(port->membase);
612 port->membase = NULL;
613}
614
615static int msm_request_port(struct uart_port *port)
616{
617 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800618 struct resource *uart_resource;
Robert Love04896a72009-06-22 18:43:11 +0100619 resource_size_t size;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800620 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100621
David Brown886a4512011-08-02 09:02:49 -0700622 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800623 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100624 return -ENXIO;
Robert Love04896a72009-06-22 18:43:11 +0100625
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800626 size = resource_size(uart_resource);
627
628 if (!request_mem_region(port->mapbase, size, "msm_serial"))
Robert Love04896a72009-06-22 18:43:11 +0100629 return -EBUSY;
630
631 port->membase = ioremap(port->mapbase, size);
632 if (!port->membase) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800633 ret = -EBUSY;
634 goto fail_release_port;
635 }
636
Robert Love04896a72009-06-22 18:43:11 +0100637 return 0;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800638
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800639fail_release_port:
640 release_mem_region(port->mapbase, size);
641 return ret;
Robert Love04896a72009-06-22 18:43:11 +0100642}
643
644static void msm_config_port(struct uart_port *port, int flags)
645{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800646 int ret;
Kiran Padwale919cef2014-08-05 13:22:00 +0530647
Robert Love04896a72009-06-22 18:43:11 +0100648 if (flags & UART_CONFIG_TYPE) {
649 port->type = PORT_MSM;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800650 ret = msm_request_port(port);
651 if (ret)
652 return;
Robert Love04896a72009-06-22 18:43:11 +0100653 }
654}
655
656static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
657{
658 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
659 return -EINVAL;
660 if (unlikely(port->irq != ser->irq))
661 return -EINVAL;
662 return 0;
663}
664
665static void msm_power(struct uart_port *port, unsigned int state,
666 unsigned int oldstate)
667{
668 struct msm_port *msm_port = UART_TO_MSM(port);
669
670 switch (state) {
671 case 0:
Stephen Boydf98cf832013-06-17 10:43:08 -0700672 clk_prepare_enable(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700673 clk_prepare_enable(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100674 break;
675 case 3:
Stephen Boydf98cf832013-06-17 10:43:08 -0700676 clk_disable_unprepare(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700677 clk_disable_unprepare(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100678 break;
679 default:
Kiran Padwal6a7cfe42014-08-05 13:22:01 +0530680 pr_err("msm_serial: Unknown PM state %d\n", state);
Robert Love04896a72009-06-22 18:43:11 +0100681 }
682}
683
Stephen Boydf7e54d72014-01-14 12:34:55 -0800684#ifdef CONFIG_CONSOLE_POLL
685static int msm_poll_init(struct uart_port *port)
686{
687 struct msm_port *msm_port = UART_TO_MSM(port);
688
689 /* Enable single character mode on RX FIFO */
690 if (msm_port->is_uartdm >= UARTDM_1P4)
691 msm_write(port, UARTDM_DMEN_RX_SC_ENABLE, UARTDM_DMEN);
692
693 return 0;
694}
695
696static int msm_poll_get_char_single(struct uart_port *port)
697{
698 struct msm_port *msm_port = UART_TO_MSM(port);
699 unsigned int rf_reg = msm_port->is_uartdm ? UARTDM_RF : UART_RF;
700
701 if (!(msm_read(port, UART_SR) & UART_SR_RX_READY))
702 return NO_POLL_CHAR;
703 else
704 return msm_read(port, rf_reg) & 0xff;
705}
706
707static int msm_poll_get_char_dm_1p3(struct uart_port *port)
708{
709 int c;
710 static u32 slop;
711 static int count;
712 unsigned char *sp = (unsigned char *)&slop;
713
714 /* Check if a previous read had more than one char */
715 if (count) {
716 c = sp[sizeof(slop) - count];
717 count--;
718 /* Or if FIFO is empty */
719 } else if (!(msm_read(port, UART_SR) & UART_SR_RX_READY)) {
720 /*
721 * If RX packing buffer has less than a word, force stale to
722 * push contents into RX FIFO
723 */
724 count = msm_read(port, UARTDM_RXFS);
725 count = (count >> UARTDM_RXFS_BUF_SHIFT) & UARTDM_RXFS_BUF_MASK;
726 if (count) {
727 msm_write(port, UART_CR_CMD_FORCE_STALE, UART_CR);
728 slop = msm_read(port, UARTDM_RF);
729 c = sp[0];
730 count--;
731 } else {
732 c = NO_POLL_CHAR;
733 }
734 /* FIFO has a word */
735 } else {
736 slop = msm_read(port, UARTDM_RF);
737 c = sp[0];
738 count = sizeof(slop) - 1;
739 }
740
741 return c;
742}
743
744static int msm_poll_get_char(struct uart_port *port)
745{
746 u32 imr;
747 int c;
748 struct msm_port *msm_port = UART_TO_MSM(port);
749
750 /* Disable all interrupts */
751 imr = msm_read(port, UART_IMR);
752 msm_write(port, 0, UART_IMR);
753
754 if (msm_port->is_uartdm == UARTDM_1P3)
755 c = msm_poll_get_char_dm_1p3(port);
756 else
757 c = msm_poll_get_char_single(port);
758
759 /* Enable interrupts */
760 msm_write(port, imr, UART_IMR);
761
762 return c;
763}
764
765static void msm_poll_put_char(struct uart_port *port, unsigned char c)
766{
767 u32 imr;
768 struct msm_port *msm_port = UART_TO_MSM(port);
769
770 /* Disable all interrupts */
771 imr = msm_read(port, UART_IMR);
772 msm_write(port, 0, UART_IMR);
773
774 if (msm_port->is_uartdm)
775 reset_dm_count(port, 1);
776
777 /* Wait until FIFO is empty */
778 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
779 cpu_relax();
780
781 /* Write a character */
782 msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
783
784 /* Wait until FIFO is empty */
785 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
786 cpu_relax();
787
788 /* Enable interrupts */
789 msm_write(port, imr, UART_IMR);
790
791 return;
792}
793#endif
794
Robert Love04896a72009-06-22 18:43:11 +0100795static struct uart_ops msm_uart_pops = {
796 .tx_empty = msm_tx_empty,
797 .set_mctrl = msm_set_mctrl,
798 .get_mctrl = msm_get_mctrl,
799 .stop_tx = msm_stop_tx,
800 .start_tx = msm_start_tx,
801 .stop_rx = msm_stop_rx,
802 .enable_ms = msm_enable_ms,
803 .break_ctl = msm_break_ctl,
804 .startup = msm_startup,
805 .shutdown = msm_shutdown,
806 .set_termios = msm_set_termios,
807 .type = msm_type,
808 .release_port = msm_release_port,
809 .request_port = msm_request_port,
810 .config_port = msm_config_port,
811 .verify_port = msm_verify_port,
812 .pm = msm_power,
Stephen Boydf7e54d72014-01-14 12:34:55 -0800813#ifdef CONFIG_CONSOLE_POLL
814 .poll_init = msm_poll_init,
815 .poll_get_char = msm_poll_get_char,
816 .poll_put_char = msm_poll_put_char,
817#endif
Robert Love04896a72009-06-22 18:43:11 +0100818};
819
820static struct msm_port msm_uart_ports[] = {
821 {
822 .uart = {
823 .iotype = UPIO_MEM,
824 .ops = &msm_uart_pops,
825 .flags = UPF_BOOT_AUTOCONF,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800826 .fifosize = 64,
Robert Love04896a72009-06-22 18:43:11 +0100827 .line = 0,
828 },
829 },
830 {
831 .uart = {
832 .iotype = UPIO_MEM,
833 .ops = &msm_uart_pops,
834 .flags = UPF_BOOT_AUTOCONF,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800835 .fifosize = 64,
Robert Love04896a72009-06-22 18:43:11 +0100836 .line = 1,
837 },
838 },
839 {
840 .uart = {
841 .iotype = UPIO_MEM,
842 .ops = &msm_uart_pops,
843 .flags = UPF_BOOT_AUTOCONF,
844 .fifosize = 64,
845 .line = 2,
846 },
847 },
848};
849
850#define UART_NR ARRAY_SIZE(msm_uart_ports)
851
852static inline struct uart_port *get_port_from_line(unsigned int line)
853{
854 return &msm_uart_ports[line].uart;
855}
856
857#ifdef CONFIG_SERIAL_MSM_CONSOLE
Robert Love04896a72009-06-22 18:43:11 +0100858static void msm_console_write(struct console *co, const char *s,
859 unsigned int count)
860{
Stephen Boyda3957e82013-08-20 23:48:06 -0700861 int i;
Robert Love04896a72009-06-22 18:43:11 +0100862 struct uart_port *port;
863 struct msm_port *msm_port;
Stephen Boyda3957e82013-08-20 23:48:06 -0700864 int num_newlines = 0;
865 bool replaced = false;
Stephen Boyd68252422014-06-30 14:54:01 -0700866 void __iomem *tf;
Robert Love04896a72009-06-22 18:43:11 +0100867
868 BUG_ON(co->index < 0 || co->index >= UART_NR);
869
870 port = get_port_from_line(co->index);
871 msm_port = UART_TO_MSM(port);
872
Stephen Boyd68252422014-06-30 14:54:01 -0700873 if (msm_port->is_uartdm)
874 tf = port->membase + UARTDM_TF;
875 else
876 tf = port->membase + UART_TF;
877
Stephen Boyda3957e82013-08-20 23:48:06 -0700878 /* Account for newlines that will get a carriage return added */
879 for (i = 0; i < count; i++)
880 if (s[i] == '\n')
881 num_newlines++;
882 count += num_newlines;
883
Robert Love04896a72009-06-22 18:43:11 +0100884 spin_lock(&port->lock);
Stephen Boyda3957e82013-08-20 23:48:06 -0700885 if (msm_port->is_uartdm)
886 reset_dm_count(port, count);
887
888 i = 0;
889 while (i < count) {
890 int j;
891 unsigned int num_chars;
892 char buf[4] = { 0 };
Stephen Boyda3957e82013-08-20 23:48:06 -0700893
894 if (msm_port->is_uartdm)
895 num_chars = min(count - i, (unsigned int)sizeof(buf));
896 else
897 num_chars = 1;
898
899 for (j = 0; j < num_chars; j++) {
900 char c = *s;
901
902 if (c == '\n' && !replaced) {
903 buf[j] = '\r';
904 j++;
905 replaced = true;
906 }
907 if (j < num_chars) {
908 buf[j] = c;
909 s++;
910 replaced = false;
911 }
912 }
913
914 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
915 cpu_relax();
916
Stephen Boyd68252422014-06-30 14:54:01 -0700917 iowrite32_rep(tf, buf, 1);
Stephen Boyda3957e82013-08-20 23:48:06 -0700918 i += num_chars;
919 }
Robert Love04896a72009-06-22 18:43:11 +0100920 spin_unlock(&port->lock);
921}
922
923static int __init msm_console_setup(struct console *co, char *options)
924{
925 struct uart_port *port;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800926 struct msm_port *msm_port;
Rickard Strandqvist2b844ad2014-06-01 15:38:24 +0200927 int baud = 0, flow, bits, parity;
Robert Love04896a72009-06-22 18:43:11 +0100928
929 if (unlikely(co->index >= UART_NR || co->index < 0))
930 return -ENXIO;
931
932 port = get_port_from_line(co->index);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800933 msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100934
935 if (unlikely(!port->membase))
936 return -ENXIO;
937
Robert Love04896a72009-06-22 18:43:11 +0100938 msm_init_clock(port);
939
940 if (options)
941 uart_parse_options(options, &baud, &parity, &bits, &flow);
942
943 bits = 8;
944 parity = 'n';
945 flow = 'n';
946 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
947 UART_MR2); /* 8N1 */
948
949 if (baud < 300 || baud > 115200)
950 baud = 115200;
951 msm_set_baud_rate(port, baud);
952
953 msm_reset(port);
954
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800955 if (msm_port->is_uartdm) {
956 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
957 msm_write(port, UART_CR_TX_ENABLE, UART_CR);
958 }
959
Kiran Padwal6a7cfe42014-08-05 13:22:01 +0530960 pr_info("msm_serial: console setup on port #%d\n", port->line);
Robert Love04896a72009-06-22 18:43:11 +0100961
962 return uart_set_options(port, co, baud, parity, bits, flow);
963}
964
965static struct uart_driver msm_uart_driver;
966
967static struct console msm_console = {
968 .name = "ttyMSM",
969 .write = msm_console_write,
970 .device = uart_console_device,
971 .setup = msm_console_setup,
972 .flags = CON_PRINTBUFFER,
973 .index = -1,
974 .data = &msm_uart_driver,
975};
976
977#define MSM_CONSOLE (&msm_console)
978
979#else
980#define MSM_CONSOLE NULL
981#endif
982
983static struct uart_driver msm_uart_driver = {
984 .owner = THIS_MODULE,
985 .driver_name = "msm_serial",
986 .dev_name = "ttyMSM",
987 .nr = UART_NR,
988 .cons = MSM_CONSOLE,
989};
990
David Browncfdad2a2011-08-04 01:55:24 -0700991static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
992
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700993static const struct of_device_id msm_uartdm_table[] = {
Stephen Boydf7e54d72014-01-14 12:34:55 -0800994 { .compatible = "qcom,msm-uartdm-v1.1", .data = (void *)UARTDM_1P1 },
995 { .compatible = "qcom,msm-uartdm-v1.2", .data = (void *)UARTDM_1P2 },
996 { .compatible = "qcom,msm-uartdm-v1.3", .data = (void *)UARTDM_1P3 },
997 { .compatible = "qcom,msm-uartdm-v1.4", .data = (void *)UARTDM_1P4 },
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700998 { }
999};
1000
Kumar Gala4cc29462014-06-03 15:13:22 -05001001static int msm_serial_probe(struct platform_device *pdev)
Robert Love04896a72009-06-22 18:43:11 +01001002{
1003 struct msm_port *msm_port;
1004 struct resource *resource;
1005 struct uart_port *port;
Stephen Boydf7e54d72014-01-14 12:34:55 -08001006 const struct of_device_id *id;
Roel Kluin1e091752009-12-21 16:26:49 -08001007 int irq;
Robert Love04896a72009-06-22 18:43:11 +01001008
David Browncfdad2a2011-08-04 01:55:24 -07001009 if (pdev->id == -1)
1010 pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;
1011
Robert Love04896a72009-06-22 18:43:11 +01001012 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
1013 return -ENXIO;
1014
Kiran Padwal6a7cfe42014-08-05 13:22:01 +05301015 dev_info(&pdev->dev, "msm_serial: detected port #%d\n", pdev->id);
Robert Love04896a72009-06-22 18:43:11 +01001016
1017 port = get_port_from_line(pdev->id);
1018 port->dev = &pdev->dev;
1019 msm_port = UART_TO_MSM(port);
1020
Stephen Boydf7e54d72014-01-14 12:34:55 -08001021 id = of_match_device(msm_uartdm_table, &pdev->dev);
1022 if (id)
1023 msm_port->is_uartdm = (unsigned long)id->data;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -08001024 else
1025 msm_port->is_uartdm = 0;
1026
Stephen Boydbfaddb72013-08-20 23:48:02 -07001027 msm_port->clk = devm_clk_get(&pdev->dev, "core");
Stephen Boyd519b3712013-06-17 10:43:09 -07001028 if (IS_ERR(msm_port->clk))
1029 return PTR_ERR(msm_port->clk);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -08001030
Stephen Boyd519b3712013-06-17 10:43:09 -07001031 if (msm_port->is_uartdm) {
Stephen Boydbfaddb72013-08-20 23:48:02 -07001032 msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
Stephen Boyd519b3712013-06-17 10:43:09 -07001033 if (IS_ERR(msm_port->pclk))
1034 return PTR_ERR(msm_port->pclk);
1035
David Brown7b6031a2012-09-07 14:45:03 -07001036 clk_set_rate(msm_port->clk, 1843200);
Stephen Boyd519b3712013-06-17 10:43:09 -07001037 }
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -08001038
Robert Love04896a72009-06-22 18:43:11 +01001039 port->uartclk = clk_get_rate(msm_port->clk);
Kiran Padwal6a7cfe42014-08-05 13:22:01 +05301040 dev_info(&pdev->dev, "uartclk = %d\n", port->uartclk);
Robert Love04896a72009-06-22 18:43:11 +01001041
David Brown886a4512011-08-02 09:02:49 -07001042 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Robert Love04896a72009-06-22 18:43:11 +01001043 if (unlikely(!resource))
1044 return -ENXIO;
1045 port->mapbase = resource->start;
1046
Roel Kluin1e091752009-12-21 16:26:49 -08001047 irq = platform_get_irq(pdev, 0);
1048 if (unlikely(irq < 0))
Robert Love04896a72009-06-22 18:43:11 +01001049 return -ENXIO;
Roel Kluin1e091752009-12-21 16:26:49 -08001050 port->irq = irq;
Robert Love04896a72009-06-22 18:43:11 +01001051
1052 platform_set_drvdata(pdev, port);
1053
1054 return uart_add_one_port(&msm_uart_driver, port);
1055}
1056
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001057static int msm_serial_remove(struct platform_device *pdev)
Robert Love04896a72009-06-22 18:43:11 +01001058{
Stephen Boyd519b3712013-06-17 10:43:09 -07001059 struct uart_port *port = platform_get_drvdata(pdev);
Robert Love04896a72009-06-22 18:43:11 +01001060
Stephen Boyd519b3712013-06-17 10:43:09 -07001061 uart_remove_one_port(&msm_uart_driver, port);
Robert Love04896a72009-06-22 18:43:11 +01001062
1063 return 0;
1064}
1065
Kiran Padwalaf300532014-07-23 15:56:26 +05301066static const struct of_device_id msm_match_table[] = {
David Browncfdad2a2011-08-04 01:55:24 -07001067 { .compatible = "qcom,msm-uart" },
Stephen Boydc3b5d3b2013-08-20 23:48:04 -07001068 { .compatible = "qcom,msm-uartdm" },
David Browncfdad2a2011-08-04 01:55:24 -07001069 {}
1070};
1071
Robert Love04896a72009-06-22 18:43:11 +01001072static struct platform_driver msm_platform_driver = {
Robert Love04896a72009-06-22 18:43:11 +01001073 .remove = msm_serial_remove,
Andy Gross31964ff2014-04-24 11:31:22 -05001074 .probe = msm_serial_probe,
Robert Love04896a72009-06-22 18:43:11 +01001075 .driver = {
1076 .name = "msm_serial",
1077 .owner = THIS_MODULE,
David Browncfdad2a2011-08-04 01:55:24 -07001078 .of_match_table = msm_match_table,
Robert Love04896a72009-06-22 18:43:11 +01001079 },
1080};
1081
1082static int __init msm_serial_init(void)
1083{
1084 int ret;
1085
1086 ret = uart_register_driver(&msm_uart_driver);
1087 if (unlikely(ret))
1088 return ret;
1089
Andy Gross31964ff2014-04-24 11:31:22 -05001090 ret = platform_driver_register(&msm_platform_driver);
Robert Love04896a72009-06-22 18:43:11 +01001091 if (unlikely(ret))
1092 uart_unregister_driver(&msm_uart_driver);
1093
Kiran Padwal6a7cfe42014-08-05 13:22:01 +05301094 pr_info("msm_serial: driver initialized\n");
Robert Love04896a72009-06-22 18:43:11 +01001095
1096 return ret;
1097}
1098
1099static void __exit msm_serial_exit(void)
1100{
1101#ifdef CONFIG_SERIAL_MSM_CONSOLE
1102 unregister_console(&msm_console);
1103#endif
1104 platform_driver_unregister(&msm_platform_driver);
1105 uart_unregister_driver(&msm_uart_driver);
1106}
1107
1108module_init(msm_serial_init);
1109module_exit(msm_serial_exit);
1110
1111MODULE_AUTHOR("Robert Love <rlove@google.com>");
1112MODULE_DESCRIPTION("Driver for msm7x serial device");
1113MODULE_LICENSE("GPL");