blob: 7767902724542591f15c94ba29c077de17a4adb6 [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;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080048 unsigned int *gsbi_base;
49 int is_uartdm;
50 unsigned int old_snap_state;
Robert Love04896a72009-06-22 18:43:11 +010051};
52
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080053static inline void wait_for_xmitr(struct uart_port *port, int bits)
54{
55 if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY))
56 while ((msm_read(port, UART_ISR) & bits) != bits)
57 cpu_relax();
58}
59
Robert Love04896a72009-06-22 18:43:11 +010060static void msm_stop_tx(struct uart_port *port)
61{
62 struct msm_port *msm_port = UART_TO_MSM(port);
63
64 msm_port->imr &= ~UART_IMR_TXLEV;
65 msm_write(port, msm_port->imr, UART_IMR);
66}
67
68static void msm_start_tx(struct uart_port *port)
69{
70 struct msm_port *msm_port = UART_TO_MSM(port);
71
72 msm_port->imr |= UART_IMR_TXLEV;
73 msm_write(port, msm_port->imr, UART_IMR);
74}
75
76static void msm_stop_rx(struct uart_port *port)
77{
78 struct msm_port *msm_port = UART_TO_MSM(port);
79
80 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
81 msm_write(port, msm_port->imr, UART_IMR);
82}
83
84static void msm_enable_ms(struct uart_port *port)
85{
86 struct msm_port *msm_port = UART_TO_MSM(port);
87
88 msm_port->imr |= UART_IMR_DELTA_CTS;
89 msm_write(port, msm_port->imr, UART_IMR);
90}
91
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -080092static void handle_rx_dm(struct uart_port *port, unsigned int misr)
93{
94 struct tty_struct *tty = port->state->port.tty;
95 unsigned int sr;
96 int count = 0;
97 struct msm_port *msm_port = UART_TO_MSM(port);
98
99 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
100 port->icount.overrun++;
101 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
102 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
103 }
104
105 if (misr & UART_IMR_RXSTALE) {
106 count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
107 msm_port->old_snap_state;
108 msm_port->old_snap_state = 0;
109 } else {
110 count = 4 * (msm_read(port, UART_RFWR));
111 msm_port->old_snap_state += count;
112 }
113
114 /* TODO: Precise error reporting */
115
116 port->icount.rx += count;
117
118 while (count > 0) {
119 unsigned int c;
120
121 sr = msm_read(port, UART_SR);
122 if ((sr & UART_SR_RX_READY) == 0) {
123 msm_port->old_snap_state -= count;
124 break;
125 }
126 c = msm_read(port, UARTDM_RF);
127 if (sr & UART_SR_RX_BREAK) {
128 port->icount.brk++;
129 if (uart_handle_break(port))
130 continue;
131 } else if (sr & UART_SR_PAR_FRAME_ERR)
132 port->icount.frame++;
133
134 /* TODO: handle sysrq */
135 tty_insert_flip_string(tty, (char *) &c,
136 (count > 4) ? 4 : count);
137 count -= 4;
138 }
139
140 tty_flip_buffer_push(tty);
141 if (misr & (UART_IMR_RXSTALE))
142 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
143 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
144 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
145}
146
Robert Love04896a72009-06-22 18:43:11 +0100147static void handle_rx(struct uart_port *port)
148{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700149 struct tty_struct *tty = port->state->port.tty;
Robert Love04896a72009-06-22 18:43:11 +0100150 unsigned int sr;
151
152 /*
153 * Handle overrun. My understanding of the hardware is that overrun
154 * is not tied to the RX buffer, so we handle the case out of band.
155 */
156 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
157 port->icount.overrun++;
158 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
159 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
160 }
161
162 /* and now the main RX loop */
163 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
164 unsigned int c;
165 char flag = TTY_NORMAL;
166
167 c = msm_read(port, UART_RF);
168
169 if (sr & UART_SR_RX_BREAK) {
170 port->icount.brk++;
171 if (uart_handle_break(port))
172 continue;
173 } else if (sr & UART_SR_PAR_FRAME_ERR) {
174 port->icount.frame++;
175 } else {
176 port->icount.rx++;
177 }
178
179 /* Mask conditions we're ignorning. */
180 sr &= port->read_status_mask;
181
182 if (sr & UART_SR_RX_BREAK) {
183 flag = TTY_BREAK;
184 } else if (sr & UART_SR_PAR_FRAME_ERR) {
185 flag = TTY_FRAME;
186 }
187
188 if (!uart_handle_sysrq_char(port, c))
189 tty_insert_flip_char(tty, c, flag);
190 }
191
192 tty_flip_buffer_push(tty);
193}
194
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800195static void reset_dm_count(struct uart_port *port)
196{
197 wait_for_xmitr(port, UART_ISR_TX_READY);
198 msm_write(port, 1, UARTDM_NCF_TX);
199}
200
Robert Love04896a72009-06-22 18:43:11 +0100201static void handle_tx(struct uart_port *port)
202{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700203 struct circ_buf *xmit = &port->state->xmit;
Robert Love04896a72009-06-22 18:43:11 +0100204 struct msm_port *msm_port = UART_TO_MSM(port);
205 int sent_tx;
206
207 if (port->x_char) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800208 if (msm_port->is_uartdm)
209 reset_dm_count(port);
210
211 msm_write(port, port->x_char,
212 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
Robert Love04896a72009-06-22 18:43:11 +0100213 port->icount.tx++;
214 port->x_char = 0;
215 }
216
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800217 if (msm_port->is_uartdm)
218 reset_dm_count(port);
219
Robert Love04896a72009-06-22 18:43:11 +0100220 while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
221 if (uart_circ_empty(xmit)) {
222 /* disable tx interrupts */
223 msm_port->imr &= ~UART_IMR_TXLEV;
224 msm_write(port, msm_port->imr, UART_IMR);
225 break;
226 }
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800227 msm_write(port, xmit->buf[xmit->tail],
228 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
Robert Love04896a72009-06-22 18:43:11 +0100229
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800230 if (msm_port->is_uartdm)
231 reset_dm_count(port);
Robert Love04896a72009-06-22 18:43:11 +0100232
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 port->icount.tx++;
235 sent_tx = 1;
236 }
237
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port);
240}
241
242static void handle_delta_cts(struct uart_port *port)
243{
244 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
245 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700246 wake_up_interruptible(&port->state->port.delta_msr_wait);
Robert Love04896a72009-06-22 18:43:11 +0100247}
248
249static irqreturn_t msm_irq(int irq, void *dev_id)
250{
251 struct uart_port *port = dev_id;
252 struct msm_port *msm_port = UART_TO_MSM(port);
253 unsigned int misr;
254
255 spin_lock(&port->lock);
256 misr = msm_read(port, UART_MISR);
257 msm_write(port, 0, UART_IMR); /* disable interrupt */
258
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800259 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
260 if (msm_port->is_uartdm)
261 handle_rx_dm(port, misr);
262 else
263 handle_rx(port);
264 }
Robert Love04896a72009-06-22 18:43:11 +0100265 if (misr & UART_IMR_TXLEV)
266 handle_tx(port);
267 if (misr & UART_IMR_DELTA_CTS)
268 handle_delta_cts(port);
269
270 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
271 spin_unlock(&port->lock);
272
273 return IRQ_HANDLED;
274}
275
276static unsigned int msm_tx_empty(struct uart_port *port)
277{
278 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
279}
280
281static unsigned int msm_get_mctrl(struct uart_port *port)
282{
283 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
284}
285
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800286
287static void msm_reset(struct uart_port *port)
288{
289 /* reset everything */
290 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
291 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
292 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
293 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
294 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
295 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
296}
297
298void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
Robert Love04896a72009-06-22 18:43:11 +0100299{
300 unsigned int mr;
Robert Love04896a72009-06-22 18:43:11 +0100301 mr = msm_read(port, UART_MR1);
302
303 if (!(mctrl & TIOCM_RTS)) {
304 mr &= ~UART_MR1_RX_RDY_CTL;
305 msm_write(port, mr, UART_MR1);
306 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
307 } else {
308 mr |= UART_MR1_RX_RDY_CTL;
309 msm_write(port, mr, UART_MR1);
310 }
311}
312
313static void msm_break_ctl(struct uart_port *port, int break_ctl)
314{
315 if (break_ctl)
316 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
317 else
318 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
319}
320
Alan Cox44da59e2009-06-22 18:43:18 +0100321static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
Robert Love04896a72009-06-22 18:43:11 +0100322{
323 unsigned int baud_code, rxstale, watermark;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800324 struct msm_port *msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100325
326 switch (baud) {
327 case 300:
328 baud_code = UART_CSR_300;
329 rxstale = 1;
330 break;
331 case 600:
332 baud_code = UART_CSR_600;
333 rxstale = 1;
334 break;
335 case 1200:
336 baud_code = UART_CSR_1200;
337 rxstale = 1;
338 break;
339 case 2400:
340 baud_code = UART_CSR_2400;
341 rxstale = 1;
342 break;
343 case 4800:
344 baud_code = UART_CSR_4800;
345 rxstale = 1;
346 break;
347 case 9600:
348 baud_code = UART_CSR_9600;
349 rxstale = 2;
350 break;
351 case 14400:
352 baud_code = UART_CSR_14400;
353 rxstale = 3;
354 break;
355 case 19200:
356 baud_code = UART_CSR_19200;
357 rxstale = 4;
358 break;
359 case 28800:
360 baud_code = UART_CSR_28800;
361 rxstale = 6;
362 break;
363 case 38400:
364 baud_code = UART_CSR_38400;
365 rxstale = 8;
366 break;
367 case 57600:
368 baud_code = UART_CSR_57600;
369 rxstale = 16;
370 break;
371 case 115200:
372 default:
373 baud_code = UART_CSR_115200;
Alan Cox44da59e2009-06-22 18:43:18 +0100374 baud = 115200;
Robert Love04896a72009-06-22 18:43:11 +0100375 rxstale = 31;
376 break;
377 }
378
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800379 if (msm_port->is_uartdm)
380 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
381
Robert Love04896a72009-06-22 18:43:11 +0100382 msm_write(port, baud_code, UART_CSR);
383
384 /* RX stale watermark */
385 watermark = UART_IPR_STALE_LSB & rxstale;
386 watermark |= UART_IPR_RXSTALE_LAST;
387 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
388 msm_write(port, watermark, UART_IPR);
389
390 /* set RX watermark */
391 watermark = (port->fifosize * 3) / 4;
392 msm_write(port, watermark, UART_RFWR);
393
394 /* set TX watermark */
395 msm_write(port, 10, UART_TFWR);
Alan Cox44da59e2009-06-22 18:43:18 +0100396
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800397 if (msm_port->is_uartdm) {
398 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
399 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
400 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
401 }
402
Alan Cox44da59e2009-06-22 18:43:18 +0100403 return baud;
Robert Love04896a72009-06-22 18:43:11 +0100404}
405
Robert Love04896a72009-06-22 18:43:11 +0100406
407static void msm_init_clock(struct uart_port *port)
408{
409 struct msm_port *msm_port = UART_TO_MSM(port);
410
411 clk_enable(msm_port->clk);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800412 if (!IS_ERR(msm_port->pclk))
413 clk_enable(msm_port->pclk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700414 msm_serial_set_mnd_regs(port);
Robert Love04896a72009-06-22 18:43:11 +0100415}
416
417static int msm_startup(struct uart_port *port)
418{
419 struct msm_port *msm_port = UART_TO_MSM(port);
420 unsigned int data, rfr_level;
421 int ret;
422
423 snprintf(msm_port->name, sizeof(msm_port->name),
424 "msm_serial%d", port->line);
425
426 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
427 msm_port->name, port);
428 if (unlikely(ret))
429 return ret;
430
431 msm_init_clock(port);
432
433 if (likely(port->fifosize > 12))
434 rfr_level = port->fifosize - 12;
435 else
436 rfr_level = port->fifosize;
437
438 /* set automatic RFR level */
439 data = msm_read(port, UART_MR1);
440 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
441 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
442 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
443 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
444 msm_write(port, data, UART_MR1);
445
446 /* make sure that RXSTALE count is non-zero */
447 data = msm_read(port, UART_IPR);
448 if (unlikely(!data)) {
449 data |= UART_IPR_RXSTALE_LAST;
450 data |= UART_IPR_STALE_LSB;
451 msm_write(port, data, UART_IPR);
452 }
453
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800454 data = 0;
455 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
456 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
457 msm_reset(port);
458 data = UART_CR_TX_ENABLE;
459 }
Robert Love04896a72009-06-22 18:43:11 +0100460
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800461 data |= UART_CR_RX_ENABLE;
462 msm_write(port, data, UART_CR); /* enable TX & RX */
463
464 /* Make sure IPR is not 0 to start with*/
465 if (msm_port->is_uartdm)
466 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
Robert Love04896a72009-06-22 18:43:11 +0100467
468 /* turn on RX and CTS interrupts */
469 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
470 UART_IMR_CURRENT_CTS;
Robert Love04896a72009-06-22 18:43:11 +0100471
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800472 if (msm_port->is_uartdm) {
473 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
474 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
475 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
476 }
477
478 msm_write(port, msm_port->imr, UART_IMR);
Robert Love04896a72009-06-22 18:43:11 +0100479 return 0;
480}
481
482static void msm_shutdown(struct uart_port *port)
483{
484 struct msm_port *msm_port = UART_TO_MSM(port);
485
486 msm_port->imr = 0;
487 msm_write(port, 0, UART_IMR); /* disable interrupts */
488
489 clk_disable(msm_port->clk);
490
491 free_irq(port->irq, port);
492}
493
494static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
495 struct ktermios *old)
496{
497 unsigned long flags;
498 unsigned int baud, mr;
499
500 spin_lock_irqsave(&port->lock, flags);
501
502 /* calculate and set baud rate */
503 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
Alan Cox44da59e2009-06-22 18:43:18 +0100504 baud = msm_set_baud_rate(port, baud);
505 if (tty_termios_baud_rate(termios))
506 tty_termios_encode_baud_rate(termios, baud, baud);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800507
Robert Love04896a72009-06-22 18:43:11 +0100508 /* calculate parity */
509 mr = msm_read(port, UART_MR2);
510 mr &= ~UART_MR2_PARITY_MODE;
511 if (termios->c_cflag & PARENB) {
512 if (termios->c_cflag & PARODD)
513 mr |= UART_MR2_PARITY_MODE_ODD;
514 else if (termios->c_cflag & CMSPAR)
515 mr |= UART_MR2_PARITY_MODE_SPACE;
516 else
517 mr |= UART_MR2_PARITY_MODE_EVEN;
518 }
519
520 /* calculate bits per char */
521 mr &= ~UART_MR2_BITS_PER_CHAR;
522 switch (termios->c_cflag & CSIZE) {
523 case CS5:
524 mr |= UART_MR2_BITS_PER_CHAR_5;
525 break;
526 case CS6:
527 mr |= UART_MR2_BITS_PER_CHAR_6;
528 break;
529 case CS7:
530 mr |= UART_MR2_BITS_PER_CHAR_7;
531 break;
532 case CS8:
533 default:
534 mr |= UART_MR2_BITS_PER_CHAR_8;
535 break;
536 }
537
538 /* calculate stop bits */
539 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
540 if (termios->c_cflag & CSTOPB)
541 mr |= UART_MR2_STOP_BIT_LEN_TWO;
542 else
543 mr |= UART_MR2_STOP_BIT_LEN_ONE;
544
545 /* set parity, bits per char, and stop bit */
546 msm_write(port, mr, UART_MR2);
547
548 /* calculate and set hardware flow control */
549 mr = msm_read(port, UART_MR1);
550 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
551 if (termios->c_cflag & CRTSCTS) {
552 mr |= UART_MR1_CTS_CTL;
553 mr |= UART_MR1_RX_RDY_CTL;
554 }
555 msm_write(port, mr, UART_MR1);
556
557 /* Configure status bits to ignore based on termio flags. */
558 port->read_status_mask = 0;
559 if (termios->c_iflag & INPCK)
560 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
561 if (termios->c_iflag & (BRKINT | PARMRK))
562 port->read_status_mask |= UART_SR_RX_BREAK;
563
564 uart_update_timeout(port, termios->c_cflag, baud);
565
566 spin_unlock_irqrestore(&port->lock, flags);
567}
568
569static const char *msm_type(struct uart_port *port)
570{
571 return "MSM";
572}
573
574static void msm_release_port(struct uart_port *port)
575{
576 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800577 struct msm_port *msm_port = UART_TO_MSM(port);
578 struct resource *uart_resource;
579 struct resource *gsbi_resource;
Robert Love04896a72009-06-22 18:43:11 +0100580 resource_size_t size;
581
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800582 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
583 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100584 return;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800585 size = resource_size(uart_resource);
Robert Love04896a72009-06-22 18:43:11 +0100586
587 release_mem_region(port->mapbase, size);
588 iounmap(port->membase);
589 port->membase = NULL;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800590
591 if (msm_port->gsbi_base) {
592 iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base +
593 GSBI_CONTROL);
594
David Brown886a4512011-08-02 09:02:49 -0700595 gsbi_resource = platform_get_resource(pdev,
596 IORESOURCE_MEM, 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800597
598 if (unlikely(!gsbi_resource))
599 return;
600
601 size = resource_size(gsbi_resource);
602 release_mem_region(gsbi_resource->start, size);
603 iounmap(msm_port->gsbi_base);
604 msm_port->gsbi_base = NULL;
605 }
Robert Love04896a72009-06-22 18:43:11 +0100606}
607
608static int msm_request_port(struct uart_port *port)
609{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800610 struct msm_port *msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100611 struct platform_device *pdev = to_platform_device(port->dev);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800612 struct resource *uart_resource;
613 struct resource *gsbi_resource;
Robert Love04896a72009-06-22 18:43:11 +0100614 resource_size_t size;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800615 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100616
David Brown886a4512011-08-02 09:02:49 -0700617 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800618 if (unlikely(!uart_resource))
Robert Love04896a72009-06-22 18:43:11 +0100619 return -ENXIO;
Robert Love04896a72009-06-22 18:43:11 +0100620
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800621 size = resource_size(uart_resource);
622
623 if (!request_mem_region(port->mapbase, size, "msm_serial"))
Robert Love04896a72009-06-22 18:43:11 +0100624 return -EBUSY;
625
626 port->membase = ioremap(port->mapbase, size);
627 if (!port->membase) {
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800628 ret = -EBUSY;
629 goto fail_release_port;
630 }
631
David Brown886a4512011-08-02 09:02:49 -0700632 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800633 /* Is this a GSBI-based port? */
634 if (gsbi_resource) {
635 size = resource_size(gsbi_resource);
636
637 if (!request_mem_region(gsbi_resource->start, size,
638 "msm_serial")) {
639 ret = -EBUSY;
640 goto fail_release_port;
641 }
642
643 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
644 if (!msm_port->gsbi_base) {
645 ret = -EBUSY;
646 goto fail_release_gsbi;
647 }
Robert Love04896a72009-06-22 18:43:11 +0100648 }
649
650 return 0;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800651
652fail_release_gsbi:
653 release_mem_region(gsbi_resource->start, size);
654fail_release_port:
655 release_mem_region(port->mapbase, size);
656 return ret;
Robert Love04896a72009-06-22 18:43:11 +0100657}
658
659static void msm_config_port(struct uart_port *port, int flags)
660{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800661 struct msm_port *msm_port = UART_TO_MSM(port);
662 int ret;
Robert Love04896a72009-06-22 18:43:11 +0100663 if (flags & UART_CONFIG_TYPE) {
664 port->type = PORT_MSM;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800665 ret = msm_request_port(port);
666 if (ret)
667 return;
Robert Love04896a72009-06-22 18:43:11 +0100668 }
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800669
670 if (msm_port->is_uartdm)
671 iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base +
672 GSBI_CONTROL);
Robert Love04896a72009-06-22 18:43:11 +0100673}
674
675static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
676{
677 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
678 return -EINVAL;
679 if (unlikely(port->irq != ser->irq))
680 return -EINVAL;
681 return 0;
682}
683
684static void msm_power(struct uart_port *port, unsigned int state,
685 unsigned int oldstate)
686{
687 struct msm_port *msm_port = UART_TO_MSM(port);
688
689 switch (state) {
690 case 0:
691 clk_enable(msm_port->clk);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800692 if (!IS_ERR(msm_port->pclk))
693 clk_enable(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100694 break;
695 case 3:
696 clk_disable(msm_port->clk);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800697 if (!IS_ERR(msm_port->pclk))
698 clk_disable(msm_port->pclk);
Robert Love04896a72009-06-22 18:43:11 +0100699 break;
700 default:
701 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
702 }
703}
704
705static struct uart_ops msm_uart_pops = {
706 .tx_empty = msm_tx_empty,
707 .set_mctrl = msm_set_mctrl,
708 .get_mctrl = msm_get_mctrl,
709 .stop_tx = msm_stop_tx,
710 .start_tx = msm_start_tx,
711 .stop_rx = msm_stop_rx,
712 .enable_ms = msm_enable_ms,
713 .break_ctl = msm_break_ctl,
714 .startup = msm_startup,
715 .shutdown = msm_shutdown,
716 .set_termios = msm_set_termios,
717 .type = msm_type,
718 .release_port = msm_release_port,
719 .request_port = msm_request_port,
720 .config_port = msm_config_port,
721 .verify_port = msm_verify_port,
722 .pm = msm_power,
723};
724
725static struct msm_port msm_uart_ports[] = {
726 {
727 .uart = {
728 .iotype = UPIO_MEM,
729 .ops = &msm_uart_pops,
730 .flags = UPF_BOOT_AUTOCONF,
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800731 .fifosize = 64,
Robert Love04896a72009-06-22 18:43:11 +0100732 .line = 0,
733 },
734 },
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 = 1,
742 },
743 },
744 {
745 .uart = {
746 .iotype = UPIO_MEM,
747 .ops = &msm_uart_pops,
748 .flags = UPF_BOOT_AUTOCONF,
749 .fifosize = 64,
750 .line = 2,
751 },
752 },
753};
754
755#define UART_NR ARRAY_SIZE(msm_uart_ports)
756
757static inline struct uart_port *get_port_from_line(unsigned int line)
758{
759 return &msm_uart_ports[line].uart;
760}
761
762#ifdef CONFIG_SERIAL_MSM_CONSOLE
763
764static void msm_console_putchar(struct uart_port *port, int c)
765{
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800766 struct msm_port *msm_port = UART_TO_MSM(port);
767
768 if (msm_port->is_uartdm)
769 reset_dm_count(port);
770
Robert Love04896a72009-06-22 18:43:11 +0100771 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
772 ;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800773 msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
Robert Love04896a72009-06-22 18:43:11 +0100774}
775
776static void msm_console_write(struct console *co, const char *s,
777 unsigned int count)
778{
779 struct uart_port *port;
780 struct msm_port *msm_port;
781
782 BUG_ON(co->index < 0 || co->index >= UART_NR);
783
784 port = get_port_from_line(co->index);
785 msm_port = UART_TO_MSM(port);
786
787 spin_lock(&port->lock);
788 uart_console_write(port, s, count, msm_console_putchar);
789 spin_unlock(&port->lock);
790}
791
792static int __init msm_console_setup(struct console *co, char *options)
793{
794 struct uart_port *port;
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800795 struct msm_port *msm_port;
Robert Love04896a72009-06-22 18:43:11 +0100796 int baud, flow, bits, parity;
797
798 if (unlikely(co->index >= UART_NR || co->index < 0))
799 return -ENXIO;
800
801 port = get_port_from_line(co->index);
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800802 msm_port = UART_TO_MSM(port);
Robert Love04896a72009-06-22 18:43:11 +0100803
804 if (unlikely(!port->membase))
805 return -ENXIO;
806
807 port->cons = co;
808
809 msm_init_clock(port);
810
811 if (options)
812 uart_parse_options(options, &baud, &parity, &bits, &flow);
813
814 bits = 8;
815 parity = 'n';
816 flow = 'n';
817 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
818 UART_MR2); /* 8N1 */
819
820 if (baud < 300 || baud > 115200)
821 baud = 115200;
822 msm_set_baud_rate(port, baud);
823
824 msm_reset(port);
825
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800826 if (msm_port->is_uartdm) {
827 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
828 msm_write(port, UART_CR_TX_ENABLE, UART_CR);
829 }
830
Robert Love04896a72009-06-22 18:43:11 +0100831 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
832
833 return uart_set_options(port, co, baud, parity, bits, flow);
834}
835
836static struct uart_driver msm_uart_driver;
837
838static struct console msm_console = {
839 .name = "ttyMSM",
840 .write = msm_console_write,
841 .device = uart_console_device,
842 .setup = msm_console_setup,
843 .flags = CON_PRINTBUFFER,
844 .index = -1,
845 .data = &msm_uart_driver,
846};
847
848#define MSM_CONSOLE (&msm_console)
849
850#else
851#define MSM_CONSOLE NULL
852#endif
853
854static struct uart_driver msm_uart_driver = {
855 .owner = THIS_MODULE,
856 .driver_name = "msm_serial",
857 .dev_name = "ttyMSM",
858 .nr = UART_NR,
859 .cons = MSM_CONSOLE,
860};
861
David Browncfdad2a2011-08-04 01:55:24 -0700862static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
863
Robert Love04896a72009-06-22 18:43:11 +0100864static int __init msm_serial_probe(struct platform_device *pdev)
865{
866 struct msm_port *msm_port;
867 struct resource *resource;
868 struct uart_port *port;
Roel Kluin1e091752009-12-21 16:26:49 -0800869 int irq;
Robert Love04896a72009-06-22 18:43:11 +0100870
David Browncfdad2a2011-08-04 01:55:24 -0700871 if (pdev->id == -1)
872 pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;
873
Robert Love04896a72009-06-22 18:43:11 +0100874 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
875 return -ENXIO;
876
877 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
878
879 port = get_port_from_line(pdev->id);
880 port->dev = &pdev->dev;
881 msm_port = UART_TO_MSM(port);
882
David Brown886a4512011-08-02 09:02:49 -0700883 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
Stepan Moskovchenkoec8f29e2010-12-21 12:38:05 -0800884 msm_port->is_uartdm = 1;
885 else
886 msm_port->is_uartdm = 0;
887
888 if (msm_port->is_uartdm) {
889 msm_port->clk = clk_get(&pdev->dev, "gsbi_uart_clk");
890 msm_port->pclk = clk_get(&pdev->dev, "gsbi_pclk");
891 } else {
892 msm_port->clk = clk_get(&pdev->dev, "uart_clk");
893 msm_port->pclk = ERR_PTR(-ENOENT);
894 }
895
896 if (unlikely(IS_ERR(msm_port->clk) || (IS_ERR(msm_port->pclk) &&
897 msm_port->is_uartdm)))
898 return PTR_ERR(msm_port->clk);
899
900 if (msm_port->is_uartdm)
901 clk_set_rate(msm_port->clk, 7372800);
902
Robert Love04896a72009-06-22 18:43:11 +0100903 port->uartclk = clk_get_rate(msm_port->clk);
Abhijeet Dharmapurikar18c79d72010-05-20 15:20:23 -0700904 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
905
Robert Love04896a72009-06-22 18:43:11 +0100906
David Brown886a4512011-08-02 09:02:49 -0700907 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Robert Love04896a72009-06-22 18:43:11 +0100908 if (unlikely(!resource))
909 return -ENXIO;
910 port->mapbase = resource->start;
911
Roel Kluin1e091752009-12-21 16:26:49 -0800912 irq = platform_get_irq(pdev, 0);
913 if (unlikely(irq < 0))
Robert Love04896a72009-06-22 18:43:11 +0100914 return -ENXIO;
Roel Kluin1e091752009-12-21 16:26:49 -0800915 port->irq = irq;
Robert Love04896a72009-06-22 18:43:11 +0100916
917 platform_set_drvdata(pdev, port);
918
919 return uart_add_one_port(&msm_uart_driver, port);
920}
921
922static int __devexit msm_serial_remove(struct platform_device *pdev)
923{
924 struct msm_port *msm_port = platform_get_drvdata(pdev);
925
926 clk_put(msm_port->clk);
927
928 return 0;
929}
930
David Browncfdad2a2011-08-04 01:55:24 -0700931static struct of_device_id msm_match_table[] = {
932 { .compatible = "qcom,msm-uart" },
933 {}
934};
935
Robert Love04896a72009-06-22 18:43:11 +0100936static struct platform_driver msm_platform_driver = {
Robert Love04896a72009-06-22 18:43:11 +0100937 .remove = msm_serial_remove,
938 .driver = {
939 .name = "msm_serial",
940 .owner = THIS_MODULE,
David Browncfdad2a2011-08-04 01:55:24 -0700941 .of_match_table = msm_match_table,
Robert Love04896a72009-06-22 18:43:11 +0100942 },
943};
944
945static int __init msm_serial_init(void)
946{
947 int ret;
948
949 ret = uart_register_driver(&msm_uart_driver);
950 if (unlikely(ret))
951 return ret;
952
953 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
954 if (unlikely(ret))
955 uart_unregister_driver(&msm_uart_driver);
956
957 printk(KERN_INFO "msm_serial: driver initialized\n");
958
959 return ret;
960}
961
962static void __exit msm_serial_exit(void)
963{
964#ifdef CONFIG_SERIAL_MSM_CONSOLE
965 unregister_console(&msm_console);
966#endif
967 platform_driver_unregister(&msm_platform_driver);
968 uart_unregister_driver(&msm_uart_driver);
969}
970
971module_init(msm_serial_init);
972module_exit(msm_serial_exit);
973
974MODULE_AUTHOR("Robert Love <rlove@google.com>");
975MODULE_DESCRIPTION("Driver for msm7x serial device");
976MODULE_LICENSE("GPL");