blob: 6c7300b9001801110b779da1c1f385b0d283dcf4 [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 -0800317
318static void msm_reset(struct uart_port *port)
319{
Stephen Boydf7e54d72014-01-14 12:34:55 -0800320 struct msm_port *msm_port = UART_TO_MSM(port);
321
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800322 /* reset everything */
323 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
324 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
325 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
326 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
327 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
328 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
Stephen Boydf7e54d72014-01-14 12:34:55 -0800329
330 /* Disable DM modes */
331 if (msm_port->is_uartdm)
332 msm_write(port, 0, UARTDM_DMEN);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800333}
334
Stephen Boydf8fb9522013-07-24 11:37:29 -0700335static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
Robert Love04896a72009-06-22 18:43:11 +0100336{
337 unsigned int mr;
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 +0100433
434static void msm_init_clock(struct uart_port *port)
435{
436 struct msm_port *msm_port = UART_TO_MSM(port);
437
Stephen Boydf98cf832013-06-17 10:43:08 -0700438 clk_prepare_enable(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700439 clk_prepare_enable(msm_port->pclk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700440 msm_serial_set_mnd_regs(port);
Robert Love04896a72009-06-22 18:43:11 +0100441}
442
443static int msm_startup(struct uart_port *port)
444{
445 struct msm_port *msm_port = UART_TO_MSM(port);
446 unsigned int data, rfr_level;
447 int ret;
448
449 snprintf(msm_port->name, sizeof(msm_port->name),
450 "msm_serial%d", port->line);
451
452 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
453 msm_port->name, port);
454 if (unlikely(ret))
455 return ret;
456
457 msm_init_clock(port);
458
459 if (likely(port->fifosize > 12))
460 rfr_level = port->fifosize - 12;
461 else
462 rfr_level = port->fifosize;
463
464 /* set automatic RFR level */
465 data = msm_read(port, UART_MR1);
466 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
467 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
468 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
469 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
470 msm_write(port, data, UART_MR1);
471
472 /* make sure that RXSTALE count is non-zero */
473 data = msm_read(port, UART_IPR);
474 if (unlikely(!data)) {
475 data |= UART_IPR_RXSTALE_LAST;
476 data |= UART_IPR_STALE_LSB;
477 msm_write(port, data, UART_IPR);
478 }
479
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800480 data = 0;
481 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
482 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
483 msm_reset(port);
484 data = UART_CR_TX_ENABLE;
485 }
Robert Love04896a72009-06-22 18:43:11 +0100486
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800487 data |= UART_CR_RX_ENABLE;
488 msm_write(port, data, UART_CR); /* enable TX & RX */
489
490 /* Make sure IPR is not 0 to start with*/
491 if (msm_port->is_uartdm)
492 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
Robert Love04896a72009-06-22 18:43:11 +0100493
494 /* turn on RX and CTS interrupts */
495 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
496 UART_IMR_CURRENT_CTS;
Robert Love04896a72009-06-22 18:43:11 +0100497
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800498 if (msm_port->is_uartdm) {
499 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
500 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
501 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
502 }
503
504 msm_write(port, msm_port->imr, UART_IMR);
Robert Love04896a72009-06-22 18:43:11 +0100505 return 0;
506}
507
508static void msm_shutdown(struct uart_port *port)
509{
510 struct msm_port *msm_port = UART_TO_MSM(port);
511
512 msm_port->imr = 0;
513 msm_write(port, 0, UART_IMR); /* disable interrupts */
514
Stephen Boydf98cf832013-06-17 10:43:08 -0700515 clk_disable_unprepare(msm_port->clk);
Robert Love04896a72009-06-22 18:43:11 +0100516
517 free_irq(port->irq, port);
518}
519
520static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
521 struct ktermios *old)
522{
523 unsigned long flags;
524 unsigned int baud, mr;
525
526 spin_lock_irqsave(&port->lock, flags);
527
528 /* calculate and set baud rate */
529 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
Alan Cox44da59e2009-06-22 18:43:18 +0100530 baud = msm_set_baud_rate(port, baud);
531 if (tty_termios_baud_rate(termios))
532 tty_termios_encode_baud_rate(termios, baud, baud);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800533
Robert Love04896a72009-06-22 18:43:11 +0100534 /* calculate parity */
535 mr = msm_read(port, UART_MR2);
536 mr &= ~UART_MR2_PARITY_MODE;
537 if (termios->c_cflag & PARENB) {
538 if (termios->c_cflag & PARODD)
539 mr |= UART_MR2_PARITY_MODE_ODD;
540 else if (termios->c_cflag & CMSPAR)
541 mr |= UART_MR2_PARITY_MODE_SPACE;
542 else
543 mr |= UART_MR2_PARITY_MODE_EVEN;
544 }
545
546 /* calculate bits per char */
547 mr &= ~UART_MR2_BITS_PER_CHAR;
548 switch (termios->c_cflag & CSIZE) {
549 case CS5:
550 mr |= UART_MR2_BITS_PER_CHAR_5;
551 break;
552 case CS6:
553 mr |= UART_MR2_BITS_PER_CHAR_6;
554 break;
555 case CS7:
556 mr |= UART_MR2_BITS_PER_CHAR_7;
557 break;
558 case CS8:
559 default:
560 mr |= UART_MR2_BITS_PER_CHAR_8;
561 break;
562 }
563
564 /* calculate stop bits */
565 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
566 if (termios->c_cflag & CSTOPB)
567 mr |= UART_MR2_STOP_BIT_LEN_TWO;
568 else
569 mr |= UART_MR2_STOP_BIT_LEN_ONE;
570
571 /* set parity, bits per char, and stop bit */
572 msm_write(port, mr, UART_MR2);
573
574 /* calculate and set hardware flow control */
575 mr = msm_read(port, UART_MR1);
576 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
577 if (termios->c_cflag & CRTSCTS) {
578 mr |= UART_MR1_CTS_CTL;
579 mr |= UART_MR1_RX_RDY_CTL;
580 }
581 msm_write(port, mr, UART_MR1);
582
583 /* Configure status bits to ignore based on termio flags. */
584 port->read_status_mask = 0;
585 if (termios->c_iflag & INPCK)
586 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400587 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Robert Love04896a72009-06-22 18:43:11 +0100588 port->read_status_mask |= UART_SR_RX_BREAK;
589
590 uart_update_timeout(port, termios->c_cflag, baud);
591
592 spin_unlock_irqrestore(&port->lock, flags);
593}
594
595static const char *msm_type(struct uart_port *port)
596{
597 return "MSM";
598}
599
600static void msm_release_port(struct uart_port *port)
601{
602 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800603 struct resource *uart_resource;
Robert Love04896a72009-06-22 18:43:11 +0100604 resource_size_t size;
605
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800606 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
607 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100608 return;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800609 size = resource_size(uart_resource);
Robert Love04896a72009-06-22 18:43:11 +0100610
611 release_mem_region(port->mapbase, size);
612 iounmap(port->membase);
613 port->membase = NULL;
614}
615
616static int msm_request_port(struct uart_port *port)
617{
618 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800619 struct resource *uart_resource;
Robert Love04896a72009-06-22 18:43:11 +0100620 resource_size_t size;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800621 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100622
David Brown886a4512011-08-02 09:02:49 -0700623 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800624 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100625 return -ENXIO;
Robert Love04896a72009-06-22 18:43:11 +0100626
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800627 size = resource_size(uart_resource);
628
629 if (!request_mem_region(port->mapbase, size, "msm_serial"))
Robert Love04896a72009-06-22 18:43:11 +0100630 return -EBUSY;
631
632 port->membase = ioremap(port->mapbase, size);
633 if (!port->membase) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800634 ret = -EBUSY;
635 goto fail_release_port;
636 }
637
Robert Love04896a72009-06-22 18:43:11 +0100638 return 0;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800639
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800640fail_release_port:
641 release_mem_region(port->mapbase, size);
642 return ret;
Robert Love04896a72009-06-22 18:43:11 +0100643}
644
645static void msm_config_port(struct uart_port *port, int flags)
646{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800647 int ret;
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:
680 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
681 }
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
Robert Love04896a72009-06-22 18:43:11 +0100960 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
961
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
1015 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
1016
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);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -07001040 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
1041
Robert Love04896a72009-06-22 18:43:11 +01001042
David Brown886a4512011-08-02 09:02:49 -07001043 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Robert Love04896a72009-06-22 18:43:11 +01001044 if (unlikely(!resource))
1045 return -ENXIO;
1046 port->mapbase = resource->start;
1047
Roel Kluin1e091752009-12-21 16:26:49 -08001048 irq = platform_get_irq(pdev, 0);
1049 if (unlikely(irq < 0))
Robert Love04896a72009-06-22 18:43:11 +01001050 return -ENXIO;
Roel Kluin1e091752009-12-21 16:26:49 -08001051 port->irq = irq;
Robert Love04896a72009-06-22 18:43:11 +01001052
1053 platform_set_drvdata(pdev, port);
1054
1055 return uart_add_one_port(&msm_uart_driver, port);
1056}
1057
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001058static int msm_serial_remove(struct platform_device *pdev)
Robert Love04896a72009-06-22 18:43:11 +01001059{
Stephen Boyd519b3712013-06-17 10:43:09 -07001060 struct uart_port *port = platform_get_drvdata(pdev);
Robert Love04896a72009-06-22 18:43:11 +01001061
Stephen Boyd519b3712013-06-17 10:43:09 -07001062 uart_remove_one_port(&msm_uart_driver, port);
Robert Love04896a72009-06-22 18:43:11 +01001063
1064 return 0;
1065}
1066
Kiran Padwalaf300532014-07-23 15:56:26 +05301067static const struct of_device_id msm_match_table[] = {
David Browncfdad2a2011-08-04 01:55:24 -07001068 { .compatible = "qcom,msm-uart" },
Stephen Boydc3b5d3b2013-08-20 23:48:04 -07001069 { .compatible = "qcom,msm-uartdm" },
David Browncfdad2a2011-08-04 01:55:24 -07001070 {}
1071};
1072
Robert Love04896a72009-06-22 18:43:11 +01001073static struct platform_driver msm_platform_driver = {
Robert Love04896a72009-06-22 18:43:11 +01001074 .remove = msm_serial_remove,
Andy Gross31964ff2014-04-24 11:31:22 -05001075 .probe = msm_serial_probe,
Robert Love04896a72009-06-22 18:43:11 +01001076 .driver = {
1077 .name = "msm_serial",
1078 .owner = THIS_MODULE,
David Browncfdad2a2011-08-04 01:55:24 -07001079 .of_match_table = msm_match_table,
Robert Love04896a72009-06-22 18:43:11 +01001080 },
1081};
1082
1083static int __init msm_serial_init(void)
1084{
1085 int ret;
1086
1087 ret = uart_register_driver(&msm_uart_driver);
1088 if (unlikely(ret))
1089 return ret;
1090
Andy Gross31964ff2014-04-24 11:31:22 -05001091 ret = platform_driver_register(&msm_platform_driver);
Robert Love04896a72009-06-22 18:43:11 +01001092 if (unlikely(ret))
1093 uart_unregister_driver(&msm_uart_driver);
1094
1095 printk(KERN_INFO "msm_serial: driver initialized\n");
1096
1097 return ret;
1098}
1099
1100static void __exit msm_serial_exit(void)
1101{
1102#ifdef CONFIG_SERIAL_MSM_CONSOLE
1103 unregister_console(&msm_console);
1104#endif
1105 platform_driver_unregister(&msm_platform_driver);
1106 uart_unregister_driver(&msm_uart_driver);
1107}
1108
1109module_init(msm_serial_init);
1110module_exit(msm_serial_exit);
1111
1112MODULE_AUTHOR("Robert Love <rlove@google.com>");
1113MODULE_DESCRIPTION("Driver for msm7x serial device");
1114MODULE_LICENSE("GPL");