]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mmc/core/sdio_uart.c
Merge tag 'platform-drivers-x86-v4.15-4' of git://git.infradead.org/linux-platform...
[mirror_ubuntu-bionic-kernel.git] / drivers / mmc / core / sdio_uart.c
CommitLineData
6e418a9d 1/*
f397c8d8 2 * SDIO UART/GPS driver
6e418a9d
NP
3 *
4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
5 * by Russell King.
6 *
7 * Author: Nicolas Pitre
8 * Created: June 15, 2007
9 * Copyright: MontaVista Software, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 */
16
17/*
18 * Note: Although this driver assumes a 16550A-like UART implementation,
19 * it is not possible to leverage the common 8250/16550 driver, nor the
20 * core UART infrastructure, as they assumes direct access to the hardware
21 * registers, often under a spinlock. This is not possible in the SDIO
22 * context as SDIO access functions must be able to sleep.
23 *
24 * Because we need to lock the SDIO host to ensure an exclusive access to
25 * the card, we simply rely on that lock to also prevent and serialize
26 * concurrent access to the same port.
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
4b3b49bb 32#include <linux/sched.h>
6e418a9d 33#include <linux/mutex.h>
201a50ba 34#include <linux/seq_file.h>
6e418a9d
NP
35#include <linux/serial_reg.h>
36#include <linux/circ_buf.h>
6e418a9d
NP
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
8b197a5c 39#include <linux/kfifo.h>
5a0e3ad6 40#include <linux/slab.h>
6e418a9d
NP
41
42#include <linux/mmc/core.h>
43#include <linux/mmc/card.h>
44#include <linux/mmc/sdio_func.h>
45#include <linux/mmc/sdio_ids.h>
46
47
48#define UART_NR 8 /* Number of UARTs this driver can handle */
49
50
8b197a5c 51#define FIFO_SIZE PAGE_SIZE
6e418a9d
NP
52#define WAKEUP_CHARS 256
53
6e418a9d
NP
54struct uart_icount {
55 __u32 cts;
56 __u32 dsr;
57 __u32 rng;
58 __u32 dcd;
59 __u32 rx;
60 __u32 tx;
61 __u32 frame;
62 __u32 overrun;
63 __u32 parity;
64 __u32 brk;
65};
66
67struct sdio_uart_port {
b5849b1a 68 struct tty_port port;
6e418a9d 69 unsigned int index;
6e418a9d
NP
70 struct sdio_func *func;
71 struct mutex func_lock;
15b82b46 72 struct task_struct *in_sdio_uart_irq;
6e418a9d 73 unsigned int regs_offset;
8b197a5c 74 struct kfifo xmit_fifo;
6e418a9d
NP
75 spinlock_t write_lock;
76 struct uart_icount icount;
77 unsigned int uartclk;
78 unsigned int mctrl;
4b3b49bb 79 unsigned int rx_mctrl;
6e418a9d
NP
80 unsigned int read_status_mask;
81 unsigned int ignore_status_mask;
82 unsigned char x_char;
83 unsigned char ier;
84 unsigned char lcr;
85};
86
87static struct sdio_uart_port *sdio_uart_table[UART_NR];
88static DEFINE_SPINLOCK(sdio_uart_table_lock);
89
90static int sdio_uart_add_port(struct sdio_uart_port *port)
91{
92 int index, ret = -EBUSY;
93
6e418a9d
NP
94 mutex_init(&port->func_lock);
95 spin_lock_init(&port->write_lock);
8b197a5c
AC
96 if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
97 return -ENOMEM;
6e418a9d
NP
98
99 spin_lock(&sdio_uart_table_lock);
100 for (index = 0; index < UART_NR; index++) {
101 if (!sdio_uart_table[index]) {
102 port->index = index;
103 sdio_uart_table[index] = port;
104 ret = 0;
105 break;
106 }
107 }
108 spin_unlock(&sdio_uart_table_lock);
109
110 return ret;
111}
112
113static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
114{
115 struct sdio_uart_port *port;
116
117 if (index >= UART_NR)
118 return NULL;
119
120 spin_lock(&sdio_uart_table_lock);
121 port = sdio_uart_table[index];
122 if (port)
e70c6771 123 tty_port_get(&port->port);
6e418a9d
NP
124 spin_unlock(&sdio_uart_table_lock);
125
126 return port;
127}
128
6e418a9d
NP
129static void sdio_uart_port_put(struct sdio_uart_port *port)
130{
e70c6771 131 tty_port_put(&port->port);
6e418a9d
NP
132}
133
134static void sdio_uart_port_remove(struct sdio_uart_port *port)
135{
136 struct sdio_func *func;
137
6e418a9d
NP
138 spin_lock(&sdio_uart_table_lock);
139 sdio_uart_table[port->index] = NULL;
140 spin_unlock(&sdio_uart_table_lock);
141
142 /*
143 * We're killing a port that potentially still is in use by
144 * the tty layer. Be careful to prevent any further access
145 * to the SDIO function and arrange for the tty layer to
146 * give up on that port ASAP.
147 * Beware: the lock ordering is critical.
148 */
0a68f64f 149 mutex_lock(&port->port.mutex);
6e418a9d
NP
150 mutex_lock(&port->func_lock);
151 func = port->func;
152 sdio_claim_host(func);
153 port->func = NULL;
154 mutex_unlock(&port->func_lock);
584abc37 155 /* tty_hangup is async so is this safe as is ?? */
aa27a094 156 tty_port_tty_hangup(&port->port, false);
0a68f64f 157 mutex_unlock(&port->port.mutex);
6e418a9d
NP
158 sdio_release_irq(func);
159 sdio_disable_func(func);
160 sdio_release_host(func);
161
162 sdio_uart_port_put(port);
163}
164
165static int sdio_uart_claim_func(struct sdio_uart_port *port)
166{
167 mutex_lock(&port->func_lock);
168 if (unlikely(!port->func)) {
169 mutex_unlock(&port->func_lock);
170 return -ENODEV;
171 }
15b82b46
NP
172 if (likely(port->in_sdio_uart_irq != current))
173 sdio_claim_host(port->func);
6e418a9d
NP
174 mutex_unlock(&port->func_lock);
175 return 0;
176}
177
178static inline void sdio_uart_release_func(struct sdio_uart_port *port)
179{
15b82b46
NP
180 if (likely(port->in_sdio_uart_irq != current))
181 sdio_release_host(port->func);
6e418a9d
NP
182}
183
184static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
185{
186 unsigned char c;
187 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
188 return c;
189}
190
191static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
192{
193 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
194}
195
196static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
197{
198 unsigned char status;
199 unsigned int ret;
200
4b3b49bb
AC
201 /* FIXME: What stops this losing the delta bits and breaking
202 sdio_uart_check_modem_status ? */
6e418a9d
NP
203 status = sdio_in(port, UART_MSR);
204
205 ret = 0;
206 if (status & UART_MSR_DCD)
207 ret |= TIOCM_CAR;
208 if (status & UART_MSR_RI)
209 ret |= TIOCM_RNG;
210 if (status & UART_MSR_DSR)
211 ret |= TIOCM_DSR;
212 if (status & UART_MSR_CTS)
213 ret |= TIOCM_CTS;
214 return ret;
215}
216
1e04b7ae
TLSC
217static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
218 unsigned int mctrl)
6e418a9d
NP
219{
220 unsigned char mcr = 0;
221
222 if (mctrl & TIOCM_RTS)
223 mcr |= UART_MCR_RTS;
224 if (mctrl & TIOCM_DTR)
225 mcr |= UART_MCR_DTR;
226 if (mctrl & TIOCM_OUT1)
227 mcr |= UART_MCR_OUT1;
228 if (mctrl & TIOCM_OUT2)
229 mcr |= UART_MCR_OUT2;
230 if (mctrl & TIOCM_LOOP)
231 mcr |= UART_MCR_LOOP;
232
233 sdio_out(port, UART_MCR, mcr);
234}
235
236static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
237 unsigned int set, unsigned int clear)
238{
239 unsigned int old;
240
241 old = port->mctrl;
242 port->mctrl = (old & ~clear) | set;
243 if (old != port->mctrl)
244 sdio_uart_write_mctrl(port, port->mctrl);
245}
246
247#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
248#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
249
250static void sdio_uart_change_speed(struct sdio_uart_port *port,
251 struct ktermios *termios,
252 struct ktermios *old)
253{
254 unsigned char cval, fcr = 0;
255 unsigned int baud, quot;
256
257 switch (termios->c_cflag & CSIZE) {
258 case CS5:
259 cval = UART_LCR_WLEN5;
260 break;
261 case CS6:
262 cval = UART_LCR_WLEN6;
263 break;
264 case CS7:
265 cval = UART_LCR_WLEN7;
266 break;
267 default:
268 case CS8:
269 cval = UART_LCR_WLEN8;
270 break;
271 }
272
273 if (termios->c_cflag & CSTOPB)
274 cval |= UART_LCR_STOP;
275 if (termios->c_cflag & PARENB)
276 cval |= UART_LCR_PARITY;
277 if (!(termios->c_cflag & PARODD))
278 cval |= UART_LCR_EPAR;
279
280 for (;;) {
281 baud = tty_termios_baud_rate(termios);
282 if (baud == 0)
283 baud = 9600; /* Special case: B0 rate. */
284 if (baud <= port->uartclk)
285 break;
286 /*
287 * Oops, the quotient was zero. Try again with the old
288 * baud rate if possible, otherwise default to 9600.
289 */
290 termios->c_cflag &= ~CBAUD;
291 if (old) {
292 termios->c_cflag |= old->c_cflag & CBAUD;
293 old = NULL;
294 } else
295 termios->c_cflag |= B9600;
296 }
297 quot = (2 * port->uartclk + baud) / (2 * baud);
298
299 if (baud < 2400)
300 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
301 else
302 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
303
304 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
305 if (termios->c_iflag & INPCK)
306 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
307 if (termios->c_iflag & (BRKINT | PARMRK))
308 port->read_status_mask |= UART_LSR_BI;
309
310 /*
311 * Characters to ignore
312 */
313 port->ignore_status_mask = 0;
314 if (termios->c_iflag & IGNPAR)
315 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
316 if (termios->c_iflag & IGNBRK) {
317 port->ignore_status_mask |= UART_LSR_BI;
318 /*
319 * If we're ignoring parity and break indicators,
320 * ignore overruns too (for real raw support).
321 */
322 if (termios->c_iflag & IGNPAR)
323 port->ignore_status_mask |= UART_LSR_OE;
324 }
325
326 /*
327 * ignore all characters if CREAD is not set
328 */
329 if ((termios->c_cflag & CREAD) == 0)
330 port->ignore_status_mask |= UART_LSR_DR;
331
332 /*
333 * CTS flow control flag and modem status interrupts
334 */
335 port->ier &= ~UART_IER_MSI;
336 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
337 port->ier |= UART_IER_MSI;
338
339 port->lcr = cval;
340
341 sdio_out(port, UART_IER, port->ier);
342 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
343 sdio_out(port, UART_DLL, quot & 0xff);
344 sdio_out(port, UART_DLM, quot >> 8);
345 sdio_out(port, UART_LCR, cval);
346 sdio_out(port, UART_FCR, fcr);
347
348 sdio_uart_write_mctrl(port, port->mctrl);
349}
350
351static void sdio_uart_start_tx(struct sdio_uart_port *port)
352{
353 if (!(port->ier & UART_IER_THRI)) {
354 port->ier |= UART_IER_THRI;
355 sdio_out(port, UART_IER, port->ier);
356 }
357}
358
359static void sdio_uart_stop_tx(struct sdio_uart_port *port)
360{
361 if (port->ier & UART_IER_THRI) {
362 port->ier &= ~UART_IER_THRI;
363 sdio_out(port, UART_IER, port->ier);
364 }
365}
366
367static void sdio_uart_stop_rx(struct sdio_uart_port *port)
368{
369 port->ier &= ~UART_IER_RLSI;
370 port->read_status_mask &= ~UART_LSR_DR;
371 sdio_out(port, UART_IER, port->ier);
372}
373
1e04b7ae
TLSC
374static void sdio_uart_receive_chars(struct sdio_uart_port *port,
375 unsigned int *status)
6e418a9d 376{
6e418a9d
NP
377 unsigned int ch, flag;
378 int max_count = 256;
379
380 do {
381 ch = sdio_in(port, UART_RX);
382 flag = TTY_NORMAL;
383 port->icount.rx++;
384
385 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
1e04b7ae 386 UART_LSR_FE | UART_LSR_OE))) {
6e418a9d
NP
387 /*
388 * For statistics only
389 */
390 if (*status & UART_LSR_BI) {
391 *status &= ~(UART_LSR_FE | UART_LSR_PE);
392 port->icount.brk++;
393 } else if (*status & UART_LSR_PE)
394 port->icount.parity++;
395 else if (*status & UART_LSR_FE)
396 port->icount.frame++;
397 if (*status & UART_LSR_OE)
398 port->icount.overrun++;
399
400 /*
401 * Mask off conditions which should be ignored.
402 */
403 *status &= port->read_status_mask;
1e04b7ae 404 if (*status & UART_LSR_BI)
6e418a9d 405 flag = TTY_BREAK;
1e04b7ae 406 else if (*status & UART_LSR_PE)
6e418a9d
NP
407 flag = TTY_PARITY;
408 else if (*status & UART_LSR_FE)
409 flag = TTY_FRAME;
410 }
411
412 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
2e124b4a 413 tty_insert_flip_char(&port->port, ch, flag);
6e418a9d
NP
414
415 /*
416 * Overrun is special. Since it's reported immediately,
417 * it doesn't affect the current character.
418 */
419 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
2e124b4a 420 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
6e418a9d
NP
421
422 *status = sdio_in(port, UART_LSR);
423 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
2e124b4a
JS
424
425 tty_flip_buffer_push(&port->port);
6e418a9d
NP
426}
427
428static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
429{
8b197a5c 430 struct kfifo *xmit = &port->xmit_fifo;
6e418a9d 431 int count;
530646f4 432 struct tty_struct *tty;
8b197a5c
AC
433 u8 iobuf[16];
434 int len;
6e418a9d
NP
435
436 if (port->x_char) {
437 sdio_out(port, UART_TX, port->x_char);
438 port->icount.tx++;
439 port->x_char = 0;
440 return;
441 }
530646f4
AC
442
443 tty = tty_port_tty_get(&port->port);
444
8b197a5c 445 if (tty == NULL || !kfifo_len(xmit) ||
c271cf37 446 tty->stopped || tty->hw_stopped) {
6e418a9d 447 sdio_uart_stop_tx(port);
530646f4 448 tty_kref_put(tty);
6e418a9d
NP
449 return;
450 }
451
8b197a5c
AC
452 len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
453 for (count = 0; count < len; count++) {
454 sdio_out(port, UART_TX, iobuf[count]);
6e418a9d 455 port->icount.tx++;
8b197a5c 456 }
6e418a9d 457
8b197a5c
AC
458 len = kfifo_len(xmit);
459 if (len < WAKEUP_CHARS) {
b5849b1a 460 tty_wakeup(tty);
8b197a5c
AC
461 if (len == 0)
462 sdio_uart_stop_tx(port);
463 }
530646f4 464 tty_kref_put(tty);
6e418a9d
NP
465}
466
467static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
468{
469 int status;
530646f4 470 struct tty_struct *tty;
6e418a9d
NP
471
472 status = sdio_in(port, UART_MSR);
473
474 if ((status & UART_MSR_ANY_DELTA) == 0)
475 return;
476
477 if (status & UART_MSR_TERI)
478 port->icount.rng++;
479 if (status & UART_MSR_DDSR)
480 port->icount.dsr++;
4b3b49bb 481 if (status & UART_MSR_DDCD) {
6e418a9d 482 port->icount.dcd++;
4b3b49bb
AC
483 /* DCD raise - wake for open */
484 if (status & UART_MSR_DCD)
485 wake_up_interruptible(&port->port.open_wait);
486 else {
487 /* DCD drop - hang up if tty attached */
aa27a094 488 tty_port_tty_hangup(&port->port, false);
4b3b49bb
AC
489 }
490 }
6e418a9d
NP
491 if (status & UART_MSR_DCTS) {
492 port->icount.cts++;
530646f4 493 tty = tty_port_tty_get(&port->port);
9db276f8 494 if (tty && C_CRTSCTS(tty)) {
6e418a9d 495 int cts = (status & UART_MSR_CTS);
b5849b1a 496 if (tty->hw_stopped) {
6e418a9d 497 if (cts) {
b5849b1a 498 tty->hw_stopped = 0;
6e418a9d 499 sdio_uart_start_tx(port);
b5849b1a 500 tty_wakeup(tty);
6e418a9d
NP
501 }
502 } else {
503 if (!cts) {
b5849b1a 504 tty->hw_stopped = 1;
6e418a9d
NP
505 sdio_uart_stop_tx(port);
506 }
507 }
508 }
530646f4 509 tty_kref_put(tty);
6e418a9d
NP
510 }
511}
512
513/*
514 * This handles the interrupt from one port.
515 */
516static void sdio_uart_irq(struct sdio_func *func)
517{
518 struct sdio_uart_port *port = sdio_get_drvdata(func);
519 unsigned int iir, lsr;
520
15b82b46
NP
521 /*
522 * In a few places sdio_uart_irq() is called directly instead of
523 * waiting for the actual interrupt to be raised and the SDIO IRQ
524 * thread scheduled in order to reduce latency. However, some
525 * interaction with the tty core may end up calling us back
526 * (serial echo, flow control, etc.) through those same places
527 * causing undesirable effects. Let's stop the recursion here.
528 */
529 if (unlikely(port->in_sdio_uart_irq == current))
530 return;
531
6e418a9d
NP
532 iir = sdio_in(port, UART_IIR);
533 if (iir & UART_IIR_NO_INT)
534 return;
15b82b46
NP
535
536 port->in_sdio_uart_irq = current;
6e418a9d
NP
537 lsr = sdio_in(port, UART_LSR);
538 if (lsr & UART_LSR_DR)
539 sdio_uart_receive_chars(port, &lsr);
540 sdio_uart_check_modem_status(port);
541 if (lsr & UART_LSR_THRE)
542 sdio_uart_transmit_chars(port);
15b82b46 543 port->in_sdio_uart_irq = NULL;
6e418a9d
NP
544}
545
4b3b49bb
AC
546static int uart_carrier_raised(struct tty_port *tport)
547{
548 struct sdio_uart_port *port =
549 container_of(tport, struct sdio_uart_port, port);
550 unsigned int ret = sdio_uart_claim_func(port);
c9404c9c 551 if (ret) /* Missing hardware shouldn't block for carrier */
4b3b49bb
AC
552 return 1;
553 ret = sdio_uart_get_mctrl(port);
554 sdio_uart_release_func(port);
555 if (ret & TIOCM_CAR)
556 return 1;
557 return 0;
558}
559
584abc37
AC
560/**
561 * uart_dtr_rts - port helper to set uart signals
562 * @tport: tty port to be updated
563 * @onoff: set to turn on DTR/RTS
564 *
565 * Called by the tty port helpers when the modem signals need to be
566 * adjusted during an open, close and hangup.
567 */
568
569static void uart_dtr_rts(struct tty_port *tport, int onoff)
6e418a9d 570{
584abc37
AC
571 struct sdio_uart_port *port =
572 container_of(tport, struct sdio_uart_port, port);
1f100b32
AC
573 int ret = sdio_uart_claim_func(port);
574 if (ret)
575 return;
584abc37
AC
576 if (onoff == 0)
577 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
578 else
579 sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1f100b32 580 sdio_uart_release_func(port);
584abc37 581}
530646f4 582
584abc37
AC
583/**
584 * sdio_uart_activate - start up hardware
585 * @tport: tty port to activate
586 * @tty: tty bound to this port
587 *
588 * Activate a tty port. The port locking guarantees us this will be
589 * run exactly once per set of opens, and if successful will see the
590 * shutdown method run exactly once to match. Start up and shutdown are
591 * protected from each other by the internal locking and will not run
592 * at the same time even during a hangup event.
593 *
594 * If we successfully start up the port we take an extra kref as we
595 * will keep it around until shutdown when the kref is dropped.
596 */
597
598static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
599{
600 struct sdio_uart_port *port =
601 container_of(tport, struct sdio_uart_port, port);
584abc37 602 int ret;
6e418a9d
NP
603
604 /*
605 * Set the TTY IO error marker - we will only clear this
606 * once we have successfully opened the port.
607 */
b5849b1a 608 set_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d 609
8b197a5c 610 kfifo_reset(&port->xmit_fifo);
6e418a9d
NP
611
612 ret = sdio_uart_claim_func(port);
613 if (ret)
8b197a5c 614 return ret;
6e418a9d
NP
615 ret = sdio_enable_func(port->func);
616 if (ret)
8b197a5c 617 goto err1;
6e418a9d
NP
618 ret = sdio_claim_irq(port->func, sdio_uart_irq);
619 if (ret)
8b197a5c 620 goto err2;
6e418a9d
NP
621
622 /*
623 * Clear the FIFO buffers and disable them.
624 * (they will be reenabled in sdio_change_speed())
625 */
626 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
627 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
1e04b7ae 628 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
6e418a9d
NP
629 sdio_out(port, UART_FCR, 0);
630
631 /*
632 * Clear the interrupt registers.
633 */
634 (void) sdio_in(port, UART_LSR);
635 (void) sdio_in(port, UART_RX);
636 (void) sdio_in(port, UART_IIR);
637 (void) sdio_in(port, UART_MSR);
638
639 /*
640 * Now, initialize the UART
641 */
642 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
643
c271cf37 644 port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
6e418a9d
NP
645 port->mctrl = TIOCM_OUT2;
646
adc8d746 647 sdio_uart_change_speed(port, &tty->termios, NULL);
6e418a9d 648
9db276f8 649 if (C_BAUD(tty))
6e418a9d
NP
650 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
651
9db276f8 652 if (C_CRTSCTS(tty))
6e418a9d 653 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
b5849b1a 654 tty->hw_stopped = 1;
6e418a9d 655
0395b48c 656 clear_bit(TTY_IO_ERROR, &tty->flags);
6e418a9d
NP
657
658 /* Kick the IRQ handler once while we're still holding the host lock */
659 sdio_uart_irq(port->func);
660
661 sdio_uart_release_func(port);
662 return 0;
663
6e418a9d 664err2:
8b197a5c 665 sdio_disable_func(port->func);
6e418a9d 666err1:
8b197a5c 667 sdio_uart_release_func(port);
6e418a9d
NP
668 return ret;
669}
670
584abc37
AC
671/**
672 * sdio_uart_shutdown - stop hardware
673 * @tport: tty port to shut down
674 *
675 * Deactivate a tty port. The port locking guarantees us this will be
676 * run only if a successful matching activate already ran. The two are
677 * protected from each other by the internal locking and will not run
678 * at the same time even during a hangup event.
679 */
680
681static void sdio_uart_shutdown(struct tty_port *tport)
6e418a9d 682{
584abc37
AC
683 struct sdio_uart_port *port =
684 container_of(tport, struct sdio_uart_port, port);
6e418a9d
NP
685 int ret;
686
687 ret = sdio_uart_claim_func(port);
688 if (ret)
8b197a5c 689 return;
6e418a9d
NP
690
691 sdio_uart_stop_rx(port);
692
1e04b7ae 693 /* Disable interrupts from this port */
6e418a9d
NP
694 sdio_release_irq(port->func);
695 port->ier = 0;
696 sdio_out(port, UART_IER, 0);
697
698 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
699
700 /* Disable break condition and FIFOs. */
701 port->lcr &= ~UART_LCR_SBC;
702 sdio_out(port, UART_LCR, port->lcr);
703 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
704 UART_FCR_CLEAR_RCVR |
705 UART_FCR_CLEAR_XMIT);
706 sdio_out(port, UART_FCR, 0);
707
708 sdio_disable_func(port->func);
709
710 sdio_uart_release_func(port);
6e418a9d
NP
711}
712
e70c6771
JS
713static void sdio_uart_port_destroy(struct tty_port *tport)
714{
715 struct sdio_uart_port *port =
716 container_of(tport, struct sdio_uart_port, port);
717 kfifo_free(&port->xmit_fifo);
718 kfree(port);
719}
720
584abc37
AC
721/**
722 * sdio_uart_install - install method
723 * @driver: the driver in use (sdio_uart in our case)
724 * @tty: the tty being bound
725 *
726 * Look up and bind the tty and the driver together. Initialize
727 * any needed private data (in our case the termios)
728 */
6e418a9d 729
584abc37
AC
730static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
731{
732 int idx = tty->index;
733 struct sdio_uart_port *port = sdio_uart_port_get(idx);
81f5835e 734 int ret = tty_standard_install(driver, tty);
584abc37 735
81f5835e 736 if (ret == 0)
584abc37
AC
737 /* This is the ref sdio_uart_port get provided */
738 tty->driver_data = port;
81f5835e 739 else
6e418a9d 740 sdio_uart_port_put(port);
584abc37 741 return ret;
6e418a9d
NP
742}
743
584abc37
AC
744/**
745 * sdio_uart_cleanup - called on the last tty kref drop
746 * @tty: the tty being destroyed
747 *
748 * Called asynchronously when the last reference to the tty is dropped.
749 * We cannot destroy the tty->driver_data port kref until this point
750 */
751
752static void sdio_uart_cleanup(struct tty_struct *tty)
6e418a9d
NP
753{
754 struct sdio_uart_port *port = tty->driver_data;
584abc37
AC
755 tty->driver_data = NULL; /* Bug trap */
756 sdio_uart_port_put(port);
757}
6e418a9d 758
584abc37
AC
759/*
760 * Open/close/hangup is now entirely boilerplate
761 */
6e418a9d 762
584abc37
AC
763static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
764{
765 struct sdio_uart_port *port = tty->driver_data;
766 return tty_port_open(&port->port, tty, filp);
767}
6e418a9d 768
584abc37
AC
769static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
770{
771 struct sdio_uart_port *port = tty->driver_data;
772 tty_port_close(&port->port, tty, filp);
773}
6e418a9d 774
584abc37
AC
775static void sdio_uart_hangup(struct tty_struct *tty)
776{
777 struct sdio_uart_port *port = tty->driver_data;
778 tty_port_hangup(&port->port);
6e418a9d
NP
779}
780
c271cf37 781static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
6e418a9d
NP
782 int count)
783{
784 struct sdio_uart_port *port = tty->driver_data;
8b197a5c 785 int ret;
6e418a9d
NP
786
787 if (!port->func)
788 return -ENODEV;
789
8b197a5c 790 ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
c271cf37 791 if (!(port->ier & UART_IER_THRI)) {
6e418a9d
NP
792 int err = sdio_uart_claim_func(port);
793 if (!err) {
794 sdio_uart_start_tx(port);
795 sdio_uart_irq(port->func);
796 sdio_uart_release_func(port);
797 } else
798 ret = err;
799 }
800
801 return ret;
802}
803
804static int sdio_uart_write_room(struct tty_struct *tty)
805{
806 struct sdio_uart_port *port = tty->driver_data;
8b197a5c 807 return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
6e418a9d
NP
808}
809
810static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
811{
812 struct sdio_uart_port *port = tty->driver_data;
8b197a5c 813 return kfifo_len(&port->xmit_fifo);
6e418a9d
NP
814}
815
816static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
817{
818 struct sdio_uart_port *port = tty->driver_data;
819
820 port->x_char = ch;
821 if (ch && !(port->ier & UART_IER_THRI)) {
822 if (sdio_uart_claim_func(port) != 0)
823 return;
824 sdio_uart_start_tx(port);
825 sdio_uart_irq(port->func);
826 sdio_uart_release_func(port);
827 }
828}
829
830static void sdio_uart_throttle(struct tty_struct *tty)
831{
832 struct sdio_uart_port *port = tty->driver_data;
833
9db276f8 834 if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
6e418a9d
NP
835 return;
836
837 if (sdio_uart_claim_func(port) != 0)
838 return;
839
840 if (I_IXOFF(tty)) {
841 port->x_char = STOP_CHAR(tty);
842 sdio_uart_start_tx(port);
843 }
844
9db276f8 845 if (C_CRTSCTS(tty))
6e418a9d
NP
846 sdio_uart_clear_mctrl(port, TIOCM_RTS);
847
848 sdio_uart_irq(port->func);
849 sdio_uart_release_func(port);
850}
851
852static void sdio_uart_unthrottle(struct tty_struct *tty)
853{
854 struct sdio_uart_port *port = tty->driver_data;
855
9db276f8 856 if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
6e418a9d
NP
857 return;
858
859 if (sdio_uart_claim_func(port) != 0)
860 return;
861
862 if (I_IXOFF(tty)) {
863 if (port->x_char) {
864 port->x_char = 0;
865 } else {
866 port->x_char = START_CHAR(tty);
867 sdio_uart_start_tx(port);
868 }
869 }
870
9db276f8 871 if (C_CRTSCTS(tty))
6e418a9d
NP
872 sdio_uart_set_mctrl(port, TIOCM_RTS);
873
874 sdio_uart_irq(port->func);
875 sdio_uart_release_func(port);
876}
877
c271cf37
AC
878static void sdio_uart_set_termios(struct tty_struct *tty,
879 struct ktermios *old_termios)
6e418a9d
NP
880{
881 struct sdio_uart_port *port = tty->driver_data;
adc8d746 882 unsigned int cflag = tty->termios.c_cflag;
6e418a9d 883
6e418a9d
NP
884 if (sdio_uart_claim_func(port) != 0)
885 return;
886
adc8d746 887 sdio_uart_change_speed(port, &tty->termios, old_termios);
6e418a9d
NP
888
889 /* Handle transition to B0 status */
890 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
891 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
892
893 /* Handle transition away from B0 status */
894 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
895 unsigned int mask = TIOCM_DTR;
97ef38b8 896 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
6e418a9d
NP
897 mask |= TIOCM_RTS;
898 sdio_uart_set_mctrl(port, mask);
899 }
900
901 /* Handle turning off CRTSCTS */
902 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
903 tty->hw_stopped = 0;
904 sdio_uart_start_tx(port);
905 }
906
907 /* Handle turning on CRTSCTS */
908 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
909 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
910 tty->hw_stopped = 1;
911 sdio_uart_stop_tx(port);
912 }
913 }
914
915 sdio_uart_release_func(port);
916}
917
c43d8636 918static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
6e418a9d
NP
919{
920 struct sdio_uart_port *port = tty->driver_data;
c43d8636 921 int result;
6e418a9d 922
c43d8636
DH
923 result = sdio_uart_claim_func(port);
924 if (result != 0)
925 return result;
6e418a9d
NP
926
927 if (break_state == -1)
928 port->lcr |= UART_LCR_SBC;
929 else
930 port->lcr &= ~UART_LCR_SBC;
931 sdio_out(port, UART_LCR, port->lcr);
932
933 sdio_uart_release_func(port);
c43d8636 934 return 0;
6e418a9d
NP
935}
936
60b33c13 937static int sdio_uart_tiocmget(struct tty_struct *tty)
6e418a9d
NP
938{
939 struct sdio_uart_port *port = tty->driver_data;
940 int result;
941
942 result = sdio_uart_claim_func(port);
943 if (!result) {
944 result = port->mctrl | sdio_uart_get_mctrl(port);
945 sdio_uart_release_func(port);
946 }
947
948 return result;
949}
950
20b9d177 951static int sdio_uart_tiocmset(struct tty_struct *tty,
6e418a9d
NP
952 unsigned int set, unsigned int clear)
953{
954 struct sdio_uart_port *port = tty->driver_data;
955 int result;
956
1e04b7ae 957 result = sdio_uart_claim_func(port);
c271cf37 958 if (!result) {
6e418a9d
NP
959 sdio_uart_update_mctrl(port, set, clear);
960 sdio_uart_release_func(port);
961 }
962
963 return result;
964}
965
201a50ba 966static int sdio_uart_proc_show(struct seq_file *m, void *v)
5ed334a1 967{
201a50ba 968 int i;
5ed334a1 969
201a50ba 970 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
5ed334a1 971 "", "", "");
201a50ba 972 for (i = 0; i < UART_NR; i++) {
5ed334a1
NP
973 struct sdio_uart_port *port = sdio_uart_port_get(i);
974 if (port) {
201a50ba 975 seq_printf(m, "%d: uart:SDIO", i);
c271cf37 976 if (capable(CAP_SYS_ADMIN)) {
201a50ba 977 seq_printf(m, " tx:%d rx:%d",
1e04b7ae 978 port->icount.tx, port->icount.rx);
5ed334a1 979 if (port->icount.frame)
201a50ba 980 seq_printf(m, " fe:%d",
1e04b7ae 981 port->icount.frame);
5ed334a1 982 if (port->icount.parity)
201a50ba 983 seq_printf(m, " pe:%d",
1e04b7ae 984 port->icount.parity);
5ed334a1 985 if (port->icount.brk)
201a50ba 986 seq_printf(m, " brk:%d",
1e04b7ae 987 port->icount.brk);
5ed334a1 988 if (port->icount.overrun)
201a50ba 989 seq_printf(m, " oe:%d",
1e04b7ae 990 port->icount.overrun);
5ed334a1 991 if (port->icount.cts)
201a50ba 992 seq_printf(m, " cts:%d",
1e04b7ae 993 port->icount.cts);
5ed334a1 994 if (port->icount.dsr)
201a50ba 995 seq_printf(m, " dsr:%d",
1e04b7ae 996 port->icount.dsr);
5ed334a1 997 if (port->icount.rng)
201a50ba 998 seq_printf(m, " rng:%d",
1e04b7ae 999 port->icount.rng);
5ed334a1 1000 if (port->icount.dcd)
201a50ba 1001 seq_printf(m, " dcd:%d",
1e04b7ae 1002 port->icount.dcd);
5ed334a1 1003 }
5ed334a1 1004 sdio_uart_port_put(port);
201a50ba 1005 seq_putc(m, '\n');
5ed334a1
NP
1006 }
1007 }
201a50ba
AD
1008 return 0;
1009}
5ed334a1 1010
201a50ba
AD
1011static int sdio_uart_proc_open(struct inode *inode, struct file *file)
1012{
1013 return single_open(file, sdio_uart_proc_show, NULL);
5ed334a1
NP
1014}
1015
201a50ba
AD
1016static const struct file_operations sdio_uart_proc_fops = {
1017 .owner = THIS_MODULE,
1018 .open = sdio_uart_proc_open,
1019 .read = seq_read,
1020 .llseek = seq_lseek,
1021 .release = single_release,
1022};
1023
584abc37
AC
1024static const struct tty_port_operations sdio_uart_port_ops = {
1025 .dtr_rts = uart_dtr_rts,
4b3b49bb 1026 .carrier_raised = uart_carrier_raised,
584abc37
AC
1027 .shutdown = sdio_uart_shutdown,
1028 .activate = sdio_uart_activate,
e70c6771 1029 .destruct = sdio_uart_port_destroy,
584abc37
AC
1030};
1031
6e418a9d
NP
1032static const struct tty_operations sdio_uart_ops = {
1033 .open = sdio_uart_open,
1034 .close = sdio_uart_close,
1035 .write = sdio_uart_write,
1036 .write_room = sdio_uart_write_room,
1037 .chars_in_buffer = sdio_uart_chars_in_buffer,
1038 .send_xchar = sdio_uart_send_xchar,
1039 .throttle = sdio_uart_throttle,
1040 .unthrottle = sdio_uart_unthrottle,
1041 .set_termios = sdio_uart_set_termios,
584abc37 1042 .hangup = sdio_uart_hangup,
6e418a9d
NP
1043 .break_ctl = sdio_uart_break_ctl,
1044 .tiocmget = sdio_uart_tiocmget,
1045 .tiocmset = sdio_uart_tiocmset,
584abc37
AC
1046 .install = sdio_uart_install,
1047 .cleanup = sdio_uart_cleanup,
201a50ba 1048 .proc_fops = &sdio_uart_proc_fops,
6e418a9d
NP
1049};
1050
1051static struct tty_driver *sdio_uart_tty_driver;
1052
1053static int sdio_uart_probe(struct sdio_func *func,
1054 const struct sdio_device_id *id)
1055{
1056 struct sdio_uart_port *port;
1057 int ret;
1058
1059 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1060 if (!port)
1061 return -ENOMEM;
1062
1063 if (func->class == SDIO_CLASS_UART) {
6606110d
JP
1064 pr_warn("%s: need info on UART class basic setup\n",
1065 sdio_func_id(func));
6e418a9d
NP
1066 kfree(port);
1067 return -ENOSYS;
1068 } else if (func->class == SDIO_CLASS_GPS) {
1069 /*
1070 * We need tuple 0x91. It contains SUBTPL_SIOREG
1071 * and SUBTPL_RCVCAPS.
1072 */
1073 struct sdio_func_tuple *tpl;
1074 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1075 if (tpl->code != 0x91)
1076 continue;
1077 if (tpl->size < 10)
1078 continue;
1079 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1080 break;
1081 }
1082 if (!tpl) {
6606110d
JP
1083 pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1084 sdio_func_id(func));
6e418a9d
NP
1085 kfree(port);
1086 return -EINVAL;
1087 }
a3c76eb9 1088 pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
6e418a9d
NP
1089 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1090 port->regs_offset = (tpl->data[4] << 0) |
1091 (tpl->data[5] << 8) |
1092 (tpl->data[6] << 16);
a3c76eb9 1093 pr_debug("%s: regs offset = 0x%x\n",
6e418a9d
NP
1094 sdio_func_id(func), port->regs_offset);
1095 port->uartclk = tpl->data[7] * 115200;
1096 if (port->uartclk == 0)
1097 port->uartclk = 115200;
a3c76eb9 1098 pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
6e418a9d
NP
1099 sdio_func_id(func), port->uartclk,
1100 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1101 } else {
1102 kfree(port);
1103 return -EINVAL;
1104 }
1105
1106 port->func = func;
1107 sdio_set_drvdata(func, port);
b5849b1a 1108 tty_port_init(&port->port);
584abc37 1109 port->port.ops = &sdio_uart_port_ops;
6e418a9d
NP
1110
1111 ret = sdio_uart_add_port(port);
1112 if (ret) {
1113 kfree(port);
1114 } else {
1115 struct device *dev;
734cc178
JS
1116 dev = tty_port_register_device(&port->port,
1117 sdio_uart_tty_driver, port->index, &func->dev);
6e418a9d
NP
1118 if (IS_ERR(dev)) {
1119 sdio_uart_port_remove(port);
1120 ret = PTR_ERR(dev);
1121 }
1122 }
1123
1124 return ret;
1125}
1126
1127static void sdio_uart_remove(struct sdio_func *func)
1128{
1129 struct sdio_uart_port *port = sdio_get_drvdata(func);
1130
1131 tty_unregister_device(sdio_uart_tty_driver, port->index);
1132 sdio_uart_port_remove(port);
1133}
1134
1135static const struct sdio_device_id sdio_uart_ids[] = {
1136 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1137 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1138 { /* end: all zeroes */ },
1139};
1140
1141MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1142
1143static struct sdio_driver sdio_uart_driver = {
1144 .probe = sdio_uart_probe,
1145 .remove = sdio_uart_remove,
1146 .name = "sdio_uart",
1147 .id_table = sdio_uart_ids,
1148};
1149
1150static int __init sdio_uart_init(void)
1151{
1152 int ret;
1153 struct tty_driver *tty_drv;
1154
1155 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1156 if (!tty_drv)
1157 return -ENOMEM;
1158
6e418a9d
NP
1159 tty_drv->driver_name = "sdio_uart";
1160 tty_drv->name = "ttySDIO";
1161 tty_drv->major = 0; /* dynamically allocated */
1162 tty_drv->minor_start = 0;
1163 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1164 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1165 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1166 tty_drv->init_termios = tty_std_termios;
1167 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
2ba30eed
NP
1168 tty_drv->init_termios.c_ispeed = 4800;
1169 tty_drv->init_termios.c_ospeed = 4800;
6e418a9d
NP
1170 tty_set_operations(tty_drv, &sdio_uart_ops);
1171
1172 ret = tty_register_driver(tty_drv);
1173 if (ret)
1174 goto err1;
1175
1176 ret = sdio_register_driver(&sdio_uart_driver);
1177 if (ret)
1178 goto err2;
1179
1180 return 0;
1181
1182err2:
1183 tty_unregister_driver(tty_drv);
1184err1:
1185 put_tty_driver(tty_drv);
1186 return ret;
1187}
1188
1189static void __exit sdio_uart_exit(void)
1190{
1191 sdio_unregister_driver(&sdio_uart_driver);
1192 tty_unregister_driver(sdio_uart_tty_driver);
1193 put_tty_driver(sdio_uart_tty_driver);
1194}
1195
1196module_init(sdio_uart_init);
1197module_exit(sdio_uart_exit);
1198
1199MODULE_AUTHOR("Nicolas Pitre");
1200MODULE_LICENSE("GPL");