blob: b5d779cd3c2b182743c728e71198ce75a60a9692 [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
42struct msm_port {
43 struct uart_port uart;
44 char name[16];
45 struct clk *clk;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080046 struct clk *pclk;
Robert Love04896a72009-06-22 18:43:11 +010047 unsigned int imr;
Stephen Boydf8fb9522013-07-24 11:37:29 -070048 void __iomem *gsbi_base;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080049 int is_uartdm;
50 unsigned int old_snap_state;
Robert Love04896a72009-06-22 18:43:11 +010051};
52
Stephen Boyd4a5662d2013-07-24 11:37:28 -070053static inline void wait_for_xmitr(struct uart_port *port)
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080054{
Stephen Boyd4a5662d2013-07-24 11:37:28 -070055 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);
59 }
60 msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080061}
62
Robert Love04896a72009-06-22 18:43:11 +010063static void msm_stop_tx(struct uart_port *port)
64{
65 struct msm_port *msm_port = UART_TO_MSM(port);
66
67 msm_port->imr &= ~UART_IMR_TXLEV;
68 msm_write(port, msm_port->imr, UART_IMR);
69}
70
71static void msm_start_tx(struct uart_port *port)
72{
73 struct msm_port *msm_port = UART_TO_MSM(port);
74
75 msm_port->imr |= UART_IMR_TXLEV;
76 msm_write(port, msm_port->imr, UART_IMR);
77}
78
79static void msm_stop_rx(struct uart_port *port)
80{
81 struct msm_port *msm_port = UART_TO_MSM(port);
82
83 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
84 msm_write(port, msm_port->imr, UART_IMR);
85}
86
87static void msm_enable_ms(struct uart_port *port)
88{
89 struct msm_port *msm_port = UART_TO_MSM(port);
90
91 msm_port->imr |= UART_IMR_DELTA_CTS;
92 msm_write(port, msm_port->imr, UART_IMR);
93}
94
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080095static void handle_rx_dm(struct uart_port *port, unsigned int misr)
96{
Jiri Slaby92a19f92013-01-03 15:53:03 +010097 struct tty_port *tport = &port->state->port;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080098 unsigned int sr;
99 int count = 0;
100 struct msm_port *msm_port = UART_TO_MSM(port);
101
102 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
103 port->icount.overrun++;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100104 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800105 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
106 }
107
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;
115 }
116
117 /* TODO: Precise error reporting */
118
119 port->icount.rx += count;
120
121 while (count > 0) {
122 unsigned int c;
123
124 sr = msm_read(port, UART_SR);
125 if ((sr & UART_SR_RX_READY) == 0) {
126 msm_port->old_snap_state -= count;
127 break;
128 }
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++;
136
137 /* TODO: handle sysrq */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100138 tty_insert_flip_string(tport, (char *)&c,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800139 (count > 4) ? 4 : count);
140 count -= 4;
141 }
142
Viresh Kumarf77232d2013-08-19 20:14:20 +0530143 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100144 tty_flip_buffer_push(tport);
Viresh Kumarf77232d2013-08-19 20:14:20 +0530145 spin_lock(&port->lock);
146
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800147 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);
151}
152
Robert Love04896a72009-06-22 18:43:11 +0100153static void handle_rx(struct uart_port *port)
154{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100155 struct tty_port *tport = &port->state->port;
Robert Love04896a72009-06-22 18:43:11 +0100156 unsigned int sr;
157
158 /*
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.
161 */
162 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
163 port->icount.overrun++;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100164 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Robert Love04896a72009-06-22 18:43:11 +0100165 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
166 }
167
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;
172
173 c = msm_read(port, UART_RF);
174
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++;
183 }
184
185 /* Mask conditions we're ignorning. */
186 sr &= port->read_status_mask;
187
188 if (sr & UART_SR_RX_BREAK) {
189 flag = TTY_BREAK;
190 } else if (sr & UART_SR_PAR_FRAME_ERR) {
191 flag = TTY_FRAME;
192 }
193
194 if (!uart_handle_sysrq_char(port, c))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100195 tty_insert_flip_char(tport, c, flag);
Robert Love04896a72009-06-22 18:43:11 +0100196 }
197
Viresh Kumarf77232d2013-08-19 20:14:20 +0530198 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100199 tty_flip_buffer_push(tport);
Viresh Kumarf77232d2013-08-19 20:14:20 +0530200 spin_lock(&port->lock);
Robert Love04896a72009-06-22 18:43:11 +0100201}
202
Stephen Boyd17fae282013-07-24 11:37:31 -0700203static void reset_dm_count(struct uart_port *port, int count)
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800204{
Stephen Boyd4a5662d2013-07-24 11:37:28 -0700205 wait_for_xmitr(port);
Stephen Boyd17fae282013-07-24 11:37:31 -0700206 msm_write(port, count, UARTDM_NCF_TX);
Stephen Boyd4a5662d2013-07-24 11:37:28 -0700207 msm_read(port, UARTDM_NCF_TX);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800208}
209
Robert Love04896a72009-06-22 18:43:11 +0100210static void handle_tx(struct uart_port *port)
211{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700212 struct circ_buf *xmit = &port->state->xmit;
Robert Love04896a72009-06-22 18:43:11 +0100213 struct msm_port *msm_port = UART_TO_MSM(port);
Stephen Boyd17fae282013-07-24 11:37:31 -0700214 unsigned int tx_count, num_chars;
215 unsigned int tf_pointer = 0;
216
217 tx_count = uart_circ_chars_pending(xmit);
218 tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
219 port->fifosize);
Robert Love04896a72009-06-22 18:43:11 +0100220
221 if (port->x_char) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800222 if (msm_port->is_uartdm)
Stephen Boyd17fae282013-07-24 11:37:31 -0700223 reset_dm_count(port, tx_count + 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800224
225 msm_write(port, port->x_char,
226 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
Robert Love04896a72009-06-22 18:43:11 +0100227 port->icount.tx++;
228 port->x_char = 0;
Stephen Boyd17fae282013-07-24 11:37:31 -0700229 } else if (tx_count && msm_port->is_uartdm) {
230 reset_dm_count(port, tx_count);
Robert Love04896a72009-06-22 18:43:11 +0100231 }
232
Stephen Boyd17fae282013-07-24 11:37:31 -0700233 while (tf_pointer < tx_count) {
234 int i;
235 char buf[4] = { 0 };
236 unsigned int *bf = (unsigned int *)&buf;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800237
Stephen Boyd17fae282013-07-24 11:37:31 -0700238 if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
Robert Love04896a72009-06-22 18:43:11 +0100239 break;
Robert Love04896a72009-06-22 18:43:11 +0100240
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800241 if (msm_port->is_uartdm)
Jingoo Han4f749f22013-08-08 17:38:20 +0900242 num_chars = min(tx_count - tf_pointer,
243 (unsigned int)sizeof(buf));
Stephen Boyd17fae282013-07-24 11:37:31 -0700244 else
245 num_chars = 1;
Robert Love04896a72009-06-22 18:43:11 +0100246
Stephen Boyd17fae282013-07-24 11:37:31 -0700247 for (i = 0; i < num_chars; i++) {
248 buf[i] = xmit->buf[xmit->tail + i];
249 port->icount.tx++;
250 }
251
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;
Robert Love04896a72009-06-22 18:43:11 +0100255 }
256
Stephen Boyd17fae282013-07-24 11:37:31 -0700257 /* disable tx interrupts if nothing more to send */
258 if (uart_circ_empty(xmit))
259 msm_stop_tx(port);
260
Robert Love04896a72009-06-22 18:43:11 +0100261 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
262 uart_write_wakeup(port);
263}
264
265static void handle_delta_cts(struct uart_port *port)
266{
267 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
268 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700269 wake_up_interruptible(&port->state->port.delta_msr_wait);
Robert Love04896a72009-06-22 18:43:11 +0100270}
271
272static irqreturn_t msm_irq(int irq, void *dev_id)
273{
274 struct uart_port *port = dev_id;
275 struct msm_port *msm_port = UART_TO_MSM(port);
276 unsigned int misr;
277
278 spin_lock(&port->lock);
279 misr = msm_read(port, UART_MISR);
280 msm_write(port, 0, UART_IMR); /* disable interrupt */
281
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800282 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);
287 }
Robert Love04896a72009-06-22 18:43:11 +0100288 if (misr & UART_IMR_TXLEV)
289 handle_tx(port);
290 if (misr & UART_IMR_DELTA_CTS)
291 handle_delta_cts(port);
292
293 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
294 spin_unlock(&port->lock);
295
296 return IRQ_HANDLED;
297}
298
299static unsigned int msm_tx_empty(struct uart_port *port)
300{
301 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
302}
303
304static unsigned int msm_get_mctrl(struct uart_port *port)
305{
306 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
307}
308
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800309
310static void msm_reset(struct uart_port *port)
311{
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);
319}
320
Stephen Boydf8fb9522013-07-24 11:37:29 -0700321static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
Robert Love04896a72009-06-22 18:43:11 +0100322{
323 unsigned int mr;
Robert Love04896a72009-06-22 18:43:11 +0100324 mr = msm_read(port, UART_MR1);
325
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);
333 }
334}
335
336static void msm_break_ctl(struct uart_port *port, int break_ctl)
337{
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);
342}
343
Stephen Boyd6909dad2013-07-24 11:37:30 -0700344struct msm_baud_map {
345 u16 divisor;
346 u8 code;
347 u8 rxstale;
348};
349
350static const struct msm_baud_map *
351msm_find_best_baud(struct uart_port *port, unsigned int baud)
352{
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 },
372 };
373
374 divisor = uart_get_divisor(port, baud);
375
376 for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
377 if (entry->divisor <= divisor)
378 break;
379
380 return entry; /* Default to smallest divider */
381}
382
Alan Cox44da59e2009-06-22 18:43:18 +0100383static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
Robert Love04896a72009-06-22 18:43:11 +0100384{
Stephen Boyd6909dad2013-07-24 11:37:30 -0700385 unsigned int rxstale, watermark;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800386 struct msm_port *msm_port = UART_TO_MSM(port);
Stephen Boyd6909dad2013-07-24 11:37:30 -0700387 const struct msm_baud_map *entry;
Robert Love04896a72009-06-22 18:43:11 +0100388
Stephen Boyd6909dad2013-07-24 11:37:30 -0700389 entry = msm_find_best_baud(port, baud);
Robert Love04896a72009-06-22 18:43:11 +0100390
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800391 if (msm_port->is_uartdm)
392 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
393
Stephen Boyd6909dad2013-07-24 11:37:30 -0700394 msm_write(port, entry->code, UART_CSR);
Robert Love04896a72009-06-22 18:43:11 +0100395
396 /* RX stale watermark */
Stephen Boyd6909dad2013-07-24 11:37:30 -0700397 rxstale = entry->rxstale;
Robert Love04896a72009-06-22 18:43:11 +0100398 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);
402
403 /* set RX watermark */
404 watermark = (port->fifosize * 3) / 4;
405 msm_write(port, watermark, UART_RFWR);
406
407 /* set TX watermark */
408 msm_write(port, 10, UART_TFWR);
Alan Cox44da59e2009-06-22 18:43:18 +0100409
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800410 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);
414 }
415
Alan Cox44da59e2009-06-22 18:43:18 +0100416 return baud;
Robert Love04896a72009-06-22 18:43:11 +0100417}
418
Robert Love04896a72009-06-22 18:43:11 +0100419
420static void msm_init_clock(struct uart_port *port)
421{
422 struct msm_port *msm_port = UART_TO_MSM(port);
423
Stephen Boydf98cf832013-06-17 10:43:08 -0700424 clk_prepare_enable(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700425 clk_prepare_enable(msm_port->pclk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700426 msm_serial_set_mnd_regs(port);
Robert Love04896a72009-06-22 18:43:11 +0100427}
428
429static int msm_startup(struct uart_port *port)
430{
431 struct msm_port *msm_port = UART_TO_MSM(port);
432 unsigned int data, rfr_level;
433 int ret;
434
435 snprintf(msm_port->name, sizeof(msm_port->name),
436 "msm_serial%d", port->line);
437
438 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
439 msm_port->name, port);
440 if (unlikely(ret))
441 return ret;
442
443 msm_init_clock(port);
444
445 if (likely(port->fifosize > 12))
446 rfr_level = port->fifosize - 12;
447 else
448 rfr_level = port->fifosize;
449
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);
457
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);
464 }
465
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800466 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;
471 }
Robert Love04896a72009-06-22 18:43:11 +0100472
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800473 data |= UART_CR_RX_ENABLE;
474 msm_write(port, data, UART_CR); /* enable TX & RX */
475
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);
Robert Love04896a72009-06-22 18:43:11 +0100479
480 /* turn on RX and CTS interrupts */
481 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
482 UART_IMR_CURRENT_CTS;
Robert Love04896a72009-06-22 18:43:11 +0100483
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800484 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);
488 }
489
490 msm_write(port, msm_port->imr, UART_IMR);
Robert Love04896a72009-06-22 18:43:11 +0100491 return 0;
492}
493
494static void msm_shutdown(struct uart_port *port)
495{
496 struct msm_port *msm_port = UART_TO_MSM(port);
497
498 msm_port->imr = 0;
499 msm_write(port, 0, UART_IMR); /* disable interrupts */
500
Stephen Boydf98cf832013-06-17 10:43:08 -0700501 clk_disable_unprepare(msm_port->clk);
Robert Love04896a72009-06-22 18:43:11 +0100502
503 free_irq(port->irq, port);
504}
505
506static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
507 struct ktermios *old)
508{
509 unsigned long flags;
510 unsigned int baud, mr;
511
512 spin_lock_irqsave(&port->lock, flags);
513
514 /* calculate and set baud rate */
515 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
Alan Cox44da59e2009-06-22 18:43:18 +0100516 baud = msm_set_baud_rate(port, baud);
517 if (tty_termios_baud_rate(termios))
518 tty_termios_encode_baud_rate(termios, baud, baud);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800519
Robert Love04896a72009-06-22 18:43:11 +0100520 /* 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;
530 }
531
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;
548 }
549
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;
556
557 /* set parity, bits per char, and stop bit */
558 msm_write(port, mr, UART_MR2);
559
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;
566 }
567 msm_write(port, mr, UART_MR1);
568
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;
575
576 uart_update_timeout(port, termios->c_cflag, baud);
577
578 spin_unlock_irqrestore(&port->lock, flags);
579}
580
581static const char *msm_type(struct uart_port *port)
582{
583 return "MSM";
584}
585
586static void msm_release_port(struct uart_port *port)
587{
588 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800589 struct msm_port *msm_port = UART_TO_MSM(port);
590 struct resource *uart_resource;
591 struct resource *gsbi_resource;
Robert Love04896a72009-06-22 18:43:11 +0100592 resource_size_t size;
593
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800594 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
595 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100596 return;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800597 size = resource_size(uart_resource);
Robert Love04896a72009-06-22 18:43:11 +0100598
599 release_mem_region(port->mapbase, size);
600 iounmap(port->membase);
601 port->membase = NULL;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800602
603 if (msm_port->gsbi_base) {
Stephen Boydf8fb9522013-07-24 11:37:29 -0700604 writel_relaxed(GSBI_PROTOCOL_IDLE,
605 msm_port->gsbi_base + GSBI_CONTROL);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800606
Stephen Boydf8fb9522013-07-24 11:37:29 -0700607 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800608 if (unlikely(!gsbi_resource))
609 return;
610
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;
615 }
Robert Love04896a72009-06-22 18:43:11 +0100616}
617
618static int msm_request_port(struct uart_port *port)
619{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800620 struct msm_port *msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100621 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800622 struct resource *uart_resource;
623 struct resource *gsbi_resource;
Robert Love04896a72009-06-22 18:43:11 +0100624 resource_size_t size;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800625 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100626
David Brown886a4512011-08-02 09:02:49 -0700627 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800628 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100629 return -ENXIO;
Robert Love04896a72009-06-22 18:43:11 +0100630
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800631 size = resource_size(uart_resource);
632
633 if (!request_mem_region(port->mapbase, size, "msm_serial"))
Robert Love04896a72009-06-22 18:43:11 +0100634 return -EBUSY;
635
636 port->membase = ioremap(port->mapbase, size);
637 if (!port->membase) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800638 ret = -EBUSY;
639 goto fail_release_port;
640 }
641
David Brown886a4512011-08-02 09:02:49 -0700642 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800643 /* Is this a GSBI-based port? */
644 if (gsbi_resource) {
645 size = resource_size(gsbi_resource);
646
647 if (!request_mem_region(gsbi_resource->start, size,
648 "msm_serial")) {
649 ret = -EBUSY;
Wei Yongjun195be842013-07-17 13:34:09 +0800650 goto fail_release_port_membase;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800651 }
652
653 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
654 if (!msm_port->gsbi_base) {
655 ret = -EBUSY;
656 goto fail_release_gsbi;
657 }
Robert Love04896a72009-06-22 18:43:11 +0100658 }
659
660 return 0;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800661
662fail_release_gsbi:
663 release_mem_region(gsbi_resource->start, size);
Wei Yongjun195be842013-07-17 13:34:09 +0800664fail_release_port_membase:
665 iounmap(port->membase);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800666fail_release_port:
667 release_mem_region(port->mapbase, size);
668 return ret;
Robert Love04896a72009-06-22 18:43:11 +0100669}
670
671static void msm_config_port(struct uart_port *port, int flags)
672{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800673 struct msm_port *msm_port = UART_TO_MSM(port);
674 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100675 if (flags & UART_CONFIG_TYPE) {
676 port->type = PORT_MSM;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800677 ret = msm_request_port(port);
678 if (ret)
679 return;
Robert Love04896a72009-06-22 18:43:11 +0100680 }
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700681 if (msm_port->gsbi_base)
Stephen Boydf8fb9522013-07-24 11:37:29 -0700682 writel_relaxed(GSBI_PROTOCOL_UART,
683 msm_port->gsbi_base + GSBI_CONTROL);
Robert Love04896a72009-06-22 18:43:11 +0100684}
685
686static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
687{
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;
693}
694
695static void msm_power(struct uart_port *port, unsigned int state,
696 unsigned int oldstate)
697{
698 struct msm_port *msm_port = UART_TO_MSM(port);
699
700 switch (state) {
701 case 0:
Stephen Boydf98cf832013-06-17 10:43:08 -0700702 clk_prepare_enable(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700703 clk_prepare_enable(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100704 break;
705 case 3:
Stephen Boydf98cf832013-06-17 10:43:08 -0700706 clk_disable_unprepare(msm_port->clk);
Stephen Boydbfaddb72013-08-20 23:48:02 -0700707 clk_disable_unprepare(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100708 break;
709 default:
710 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
711 }
712}
713
714static 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,
732};
733
734static struct msm_port msm_uart_ports[] = {
735 {
736 .uart = {
737 .iotype = UPIO_MEM,
738 .ops = &msm_uart_pops,
739 .flags = UPF_BOOT_AUTOCONF,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800740 .fifosize = 64,
Robert Love04896a72009-06-22 18:43:11 +0100741 .line = 0,
742 },
743 },
744 {
745 .uart = {
746 .iotype = UPIO_MEM,
747 .ops = &msm_uart_pops,
748 .flags = UPF_BOOT_AUTOCONF,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800749 .fifosize = 64,
Robert Love04896a72009-06-22 18:43:11 +0100750 .line = 1,
751 },
752 },
753 {
754 .uart = {
755 .iotype = UPIO_MEM,
756 .ops = &msm_uart_pops,
757 .flags = UPF_BOOT_AUTOCONF,
758 .fifosize = 64,
759 .line = 2,
760 },
761 },
762};
763
764#define UART_NR ARRAY_SIZE(msm_uart_ports)
765
766static inline struct uart_port *get_port_from_line(unsigned int line)
767{
768 return &msm_uart_ports[line].uart;
769}
770
771#ifdef CONFIG_SERIAL_MSM_CONSOLE
Robert Love04896a72009-06-22 18:43:11 +0100772static void msm_console_write(struct console *co, const char *s,
773 unsigned int count)
774{
Stephen Boyda3957e82013-08-20 23:48:06 -0700775 int i;
Robert Love04896a72009-06-22 18:43:11 +0100776 struct uart_port *port;
777 struct msm_port *msm_port;
Stephen Boyda3957e82013-08-20 23:48:06 -0700778 int num_newlines = 0;
779 bool replaced = false;
Robert Love04896a72009-06-22 18:43:11 +0100780
781 BUG_ON(co->index < 0 || co->index >= UART_NR);
782
783 port = get_port_from_line(co->index);
784 msm_port = UART_TO_MSM(port);
785
Stephen Boyda3957e82013-08-20 23:48:06 -0700786 /* 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;
791
Robert Love04896a72009-06-22 18:43:11 +0100792 spin_lock(&port->lock);
Stephen Boyda3957e82013-08-20 23:48:06 -0700793 if (msm_port->is_uartdm)
794 reset_dm_count(port, count);
795
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;
802
803 if (msm_port->is_uartdm)
804 num_chars = min(count - i, (unsigned int)sizeof(buf));
805 else
806 num_chars = 1;
807
808 for (j = 0; j < num_chars; j++) {
809 char c = *s;
810
811 if (c == '\n' && !replaced) {
812 buf[j] = '\r';
813 j++;
814 replaced = true;
815 }
816 if (j < num_chars) {
817 buf[j] = c;
818 s++;
819 replaced = false;
820 }
821 }
822
823 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
824 cpu_relax();
825
826 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
827 i += num_chars;
828 }
Robert Love04896a72009-06-22 18:43:11 +0100829 spin_unlock(&port->lock);
830}
831
832static int __init msm_console_setup(struct console *co, char *options)
833{
834 struct uart_port *port;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800835 struct msm_port *msm_port;
Robert Love04896a72009-06-22 18:43:11 +0100836 int baud, flow, bits, parity;
837
838 if (unlikely(co->index >= UART_NR || co->index < 0))
839 return -ENXIO;
840
841 port = get_port_from_line(co->index);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800842 msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100843
844 if (unlikely(!port->membase))
845 return -ENXIO;
846
Robert Love04896a72009-06-22 18:43:11 +0100847 msm_init_clock(port);
848
849 if (options)
850 uart_parse_options(options, &baud, &parity, &bits, &flow);
851
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 */
857
858 if (baud < 300 || baud > 115200)
859 baud = 115200;
860 msm_set_baud_rate(port, baud);
861
862 msm_reset(port);
863
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800864 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);
867 }
868
Robert Love04896a72009-06-22 18:43:11 +0100869 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
870
871 return uart_set_options(port, co, baud, parity, bits, flow);
872}
873
874static struct uart_driver msm_uart_driver;
875
876static 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,
884};
885
886#define MSM_CONSOLE (&msm_console)
887
888#else
889#define MSM_CONSOLE NULL
890#endif
891
892static 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,
898};
899
David Browncfdad2a2011-08-04 01:55:24 -0700900static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
901
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700902static const struct of_device_id msm_uartdm_table[] = {
903 { .compatible = "qcom,msm-uartdm" },
904 { }
905};
906
Robert Love04896a72009-06-22 18:43:11 +0100907static int __init msm_serial_probe(struct platform_device *pdev)
908{
909 struct msm_port *msm_port;
910 struct resource *resource;
911 struct uart_port *port;
Roel Kluin1e091752009-12-21 16:26:49 -0800912 int irq;
Robert Love04896a72009-06-22 18:43:11 +0100913
David Browncfdad2a2011-08-04 01:55:24 -0700914 if (pdev->id == -1)
915 pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;
916
Robert Love04896a72009-06-22 18:43:11 +0100917 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
918 return -ENXIO;
919
920 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
921
922 port = get_port_from_line(pdev->id);
923 port->dev = &pdev->dev;
924 msm_port = UART_TO_MSM(port);
925
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700926 if (of_match_device(msm_uartdm_table, &pdev->dev))
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800927 msm_port->is_uartdm = 1;
928 else
929 msm_port->is_uartdm = 0;
930
Stephen Boydbfaddb72013-08-20 23:48:02 -0700931 msm_port->clk = devm_clk_get(&pdev->dev, "core");
Stephen Boyd519b3712013-06-17 10:43:09 -0700932 if (IS_ERR(msm_port->clk))
933 return PTR_ERR(msm_port->clk);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800934
Stephen Boyd519b3712013-06-17 10:43:09 -0700935 if (msm_port->is_uartdm) {
Stephen Boydbfaddb72013-08-20 23:48:02 -0700936 msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
Stephen Boyd519b3712013-06-17 10:43:09 -0700937 if (IS_ERR(msm_port->pclk))
938 return PTR_ERR(msm_port->pclk);
939
David Brown7b6031a2012-09-07 14:45:03 -0700940 clk_set_rate(msm_port->clk, 1843200);
Stephen Boyd519b3712013-06-17 10:43:09 -0700941 }
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800942
Robert Love04896a72009-06-22 18:43:11 +0100943 port->uartclk = clk_get_rate(msm_port->clk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700944 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
945
Robert Love04896a72009-06-22 18:43:11 +0100946
David Brown886a4512011-08-02 09:02:49 -0700947 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Robert Love04896a72009-06-22 18:43:11 +0100948 if (unlikely(!resource))
949 return -ENXIO;
950 port->mapbase = resource->start;
951
Roel Kluin1e091752009-12-21 16:26:49 -0800952 irq = platform_get_irq(pdev, 0);
953 if (unlikely(irq < 0))
Robert Love04896a72009-06-22 18:43:11 +0100954 return -ENXIO;
Roel Kluin1e091752009-12-21 16:26:49 -0800955 port->irq = irq;
Robert Love04896a72009-06-22 18:43:11 +0100956
957 platform_set_drvdata(pdev, port);
958
959 return uart_add_one_port(&msm_uart_driver, port);
960}
961
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500962static int msm_serial_remove(struct platform_device *pdev)
Robert Love04896a72009-06-22 18:43:11 +0100963{
Stephen Boyd519b3712013-06-17 10:43:09 -0700964 struct uart_port *port = platform_get_drvdata(pdev);
Robert Love04896a72009-06-22 18:43:11 +0100965
Stephen Boyd519b3712013-06-17 10:43:09 -0700966 uart_remove_one_port(&msm_uart_driver, port);
Robert Love04896a72009-06-22 18:43:11 +0100967
968 return 0;
969}
970
David Browncfdad2a2011-08-04 01:55:24 -0700971static struct of_device_id msm_match_table[] = {
972 { .compatible = "qcom,msm-uart" },
Stephen Boydc3b5d3b2013-08-20 23:48:04 -0700973 { .compatible = "qcom,msm-uartdm" },
David Browncfdad2a2011-08-04 01:55:24 -0700974 {}
975};
976
Robert Love04896a72009-06-22 18:43:11 +0100977static struct platform_driver msm_platform_driver = {
Robert Love04896a72009-06-22 18:43:11 +0100978 .remove = msm_serial_remove,
979 .driver = {
980 .name = "msm_serial",
981 .owner = THIS_MODULE,
David Browncfdad2a2011-08-04 01:55:24 -0700982 .of_match_table = msm_match_table,
Robert Love04896a72009-06-22 18:43:11 +0100983 },
984};
985
986static int __init msm_serial_init(void)
987{
988 int ret;
989
990 ret = uart_register_driver(&msm_uart_driver);
991 if (unlikely(ret))
992 return ret;
993
994 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
995 if (unlikely(ret))
996 uart_unregister_driver(&msm_uart_driver);
997
998 printk(KERN_INFO "msm_serial: driver initialized\n");
999
1000 return ret;
1001}
1002
1003static void __exit msm_serial_exit(void)
1004{
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);
1010}
1011
1012module_init(msm_serial_init);
1013module_exit(msm_serial_exit);
1014
1015MODULE_AUTHOR("Robert Love <rlove@google.com>");
1016MODULE_DESCRIPTION("Driver for msm7x serial device");
1017MODULE_LICENSE("GPL");