tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-2.6-block.git] / drivers / tty / serial / bfin_sport_uart.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Blackfin On-Chip Sport Emulated UART Driver
4  *
5  * Copyright 2006-2009 Analog Devices Inc.
6  *
7  * Enter bugs at http://blackfin.uclinux.org/
8  *
9  * Licensed under the GPL-2 or later.
10  */
11
12 /*
13  * This driver and the hardware supported are in term of EE-191 of ADI.
14  * http://www.analog.com/static/imported-files/application_notes/EE191.pdf 
15  * This application note describe how to implement a UART on a Sharc DSP,
16  * but this driver is implemented on Blackfin Processor.
17  * Transmit Frame Sync is not used by this driver to transfer data out.
18  */
19
20 /* #define DEBUG */
21
22 #define DRV_NAME "bfin-sport-uart"
23 #define DEVICE_NAME     "ttySS"
24 #define pr_fmt(fmt) DRV_NAME ": " fmt
25
26 #include <linux/module.h>
27 #include <linux/ioport.h>
28 #include <linux/io.h>
29 #include <linux/init.h>
30 #include <linux/console.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
33 #include <linux/platform_device.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/serial_core.h>
37 #include <linux/gpio.h>
38
39 #include <asm/bfin_sport.h>
40 #include <asm/delay.h>
41 #include <asm/portmux.h>
42
43 #include "bfin_sport_uart.h"
44
45 struct sport_uart_port {
46         struct uart_port        port;
47         int                     err_irq;
48         unsigned short          csize;
49         unsigned short          rxmask;
50         unsigned short          txmask1;
51         unsigned short          txmask2;
52         unsigned char           stopb;
53 /*      unsigned char           parib; */
54 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
55         int cts_pin;
56         int rts_pin;
57 #endif
58 };
59
60 static int sport_uart_tx_chars(struct sport_uart_port *up);
61 static void sport_stop_tx(struct uart_port *port);
62
63 static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
64 {
65         pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
66                 up->txmask1, up->txmask2);
67
68         /* Place Start and Stop bits */
69         __asm__ __volatile__ (
70                 "%[val] <<= 1;"
71                 "%[val] = %[val] & %[mask1];"
72                 "%[val] = %[val] | %[mask2];"
73                 : [val]"+d"(value)
74                 : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
75                 : "ASTAT"
76         );
77         pr_debug("%s value:%x\n", __func__, value);
78
79         SPORT_PUT_TX(up, value);
80 }
81
82 static inline unsigned char rx_one_byte(struct sport_uart_port *up)
83 {
84         unsigned int value;
85         unsigned char extract;
86         u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
87
88         if ((up->csize + up->stopb) > 7)
89                 value = SPORT_GET_RX32(up);
90         else
91                 value = SPORT_GET_RX(up);
92
93         pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
94                 up->csize, up->rxmask);
95
96         /* Extract data */
97         __asm__ __volatile__ (
98                 "%[extr] = 0;"
99                 "%[mask1] = %[rxmask];"
100                 "%[mask2] = 0x0200(Z);"
101                 "%[shift] = 0;"
102                 "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
103                 ".Lloop_s:"
104                 "%[tmp] = extract(%[val], %[mask1].L)(Z);"
105                 "%[tmp] <<= %[shift];"
106                 "%[extr] = %[extr] | %[tmp];"
107                 "%[mask1] = %[mask1] - %[mask2];"
108                 ".Lloop_e:"
109                 "%[shift] += 1;"
110                 : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
111                   [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
112                 : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
113                 : "ASTAT", "LB0", "LC0", "LT0"
114         );
115
116         pr_debug("      extract:%x\n", extract);
117         return extract;
118 }
119
120 static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
121 {
122         int tclkdiv, rclkdiv;
123         unsigned int sclk = get_sclk();
124
125         /* Set TCR1 and TCR2, TFSR is not enabled for uart */
126         SPORT_PUT_TCR1(up, (LATFS | ITFS | TFSR | TLSBIT | ITCLK));
127         SPORT_PUT_TCR2(up, size + 1);
128         pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
129
130         /* Set RCR1 and RCR2 */
131         SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
132         SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
133         pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
134
135         tclkdiv = sclk / (2 * baud_rate) - 1;
136         /* The actual uart baud rate of devices vary between +/-2%. The sport
137          * RX sample rate should be faster than the double of the worst case,
138          * otherwise, wrong data are received. So, set sport RX clock to be
139          * 3% faster.
140          */
141         rclkdiv = sclk / (2 * baud_rate * 2 * 97 / 100) - 1;
142         SPORT_PUT_TCLKDIV(up, tclkdiv);
143         SPORT_PUT_RCLKDIV(up, rclkdiv);
144         SSYNC();
145         pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
146                         __func__, sclk, baud_rate, tclkdiv, rclkdiv);
147
148         return 0;
149 }
150
151 static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
152 {
153         struct sport_uart_port *up = dev_id;
154         struct tty_port *port = &up->port.state->port;
155         unsigned int ch;
156
157         spin_lock(&up->port.lock);
158
159         while (SPORT_GET_STAT(up) & RXNE) {
160                 ch = rx_one_byte(up);
161                 up->port.icount.rx++;
162
163                 if (!uart_handle_sysrq_char(&up->port, ch))
164                         tty_insert_flip_char(port, ch, TTY_NORMAL);
165         }
166
167         spin_unlock(&up->port.lock);
168
169         /* XXX this won't deadlock with lowlat? */
170         tty_flip_buffer_push(port);
171
172         return IRQ_HANDLED;
173 }
174
175 static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
176 {
177         struct sport_uart_port *up = dev_id;
178
179         spin_lock(&up->port.lock);
180         sport_uart_tx_chars(up);
181         spin_unlock(&up->port.lock);
182
183         return IRQ_HANDLED;
184 }
185
186 static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
187 {
188         struct sport_uart_port *up = dev_id;
189         unsigned int stat = SPORT_GET_STAT(up);
190
191         spin_lock(&up->port.lock);
192
193         /* Overflow in RX FIFO */
194         if (stat & ROVF) {
195                 up->port.icount.overrun++;
196                 tty_insert_flip_char(&up->port.state->port, 0, TTY_OVERRUN);
197                 SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
198         }
199         /* These should not happen */
200         if (stat & (TOVF | TUVF | RUVF)) {
201                 pr_err("SPORT Error:%s %s %s\n",
202                        (stat & TOVF) ? "TX overflow" : "",
203                        (stat & TUVF) ? "TX underflow" : "",
204                        (stat & RUVF) ? "RX underflow" : "");
205                 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
206                 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
207         }
208         SSYNC();
209
210         spin_unlock(&up->port.lock);
211         /* XXX we don't push the overrun bit to TTY? */
212
213         return IRQ_HANDLED;
214 }
215
216 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
217 static unsigned int sport_get_mctrl(struct uart_port *port)
218 {
219         struct sport_uart_port *up = (struct sport_uart_port *)port;
220         if (up->cts_pin < 0)
221                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
222
223         /* CTS PIN is negative assertive. */
224         if (SPORT_UART_GET_CTS(up))
225                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
226         else
227                 return TIOCM_DSR | TIOCM_CAR;
228 }
229
230 static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
231 {
232         struct sport_uart_port *up = (struct sport_uart_port *)port;
233         if (up->rts_pin < 0)
234                 return;
235
236         /* RTS PIN is negative assertive. */
237         if (mctrl & TIOCM_RTS)
238                 SPORT_UART_ENABLE_RTS(up);
239         else
240                 SPORT_UART_DISABLE_RTS(up);
241 }
242
243 /*
244  * Handle any change of modem status signal.
245  */
246 static irqreturn_t sport_mctrl_cts_int(int irq, void *dev_id)
247 {
248         struct sport_uart_port *up = (struct sport_uart_port *)dev_id;
249         unsigned int status;
250
251         status = sport_get_mctrl(&up->port);
252         uart_handle_cts_change(&up->port, status & TIOCM_CTS);
253
254         return IRQ_HANDLED;
255 }
256 #else
257 static unsigned int sport_get_mctrl(struct uart_port *port)
258 {
259         pr_debug("%s enter\n", __func__);
260         return TIOCM_CTS | TIOCM_CD | TIOCM_DSR;
261 }
262
263 static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
264 {
265         pr_debug("%s enter\n", __func__);
266 }
267 #endif
268
269 /* Reqeust IRQ, Setup clock */
270 static int sport_startup(struct uart_port *port)
271 {
272         struct sport_uart_port *up = (struct sport_uart_port *)port;
273         int ret;
274
275         pr_debug("%s enter\n", __func__);
276         ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
277                 "SPORT_UART_RX", up);
278         if (ret) {
279                 dev_err(port->dev, "unable to request SPORT RX interrupt\n");
280                 return ret;
281         }
282
283         ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
284                 "SPORT_UART_TX", up);
285         if (ret) {
286                 dev_err(port->dev, "unable to request SPORT TX interrupt\n");
287                 goto fail1;
288         }
289
290         ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
291                 "SPORT_UART_STATUS", up);
292         if (ret) {
293                 dev_err(port->dev, "unable to request SPORT status interrupt\n");
294                 goto fail2;
295         }
296
297 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
298         if (up->cts_pin >= 0) {
299                 if (request_irq(gpio_to_irq(up->cts_pin),
300                         sport_mctrl_cts_int,
301                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
302                         0, "BFIN_SPORT_UART_CTS", up)) {
303                         up->cts_pin = -1;
304                         dev_info(port->dev, "Unable to attach BlackFin UART over SPORT CTS interrupt. So, disable it.\n");
305                 }
306         }
307         if (up->rts_pin >= 0) {
308                 if (gpio_request(up->rts_pin, DRV_NAME)) {
309                         dev_info(port->dev, "fail to request RTS PIN at GPIO_%d\n", up->rts_pin);
310                         up->rts_pin = -1;
311                 } else
312                         gpio_direction_output(up->rts_pin, 0);
313         }
314 #endif
315
316         return 0;
317  fail2:
318         free_irq(up->port.irq+1, up);
319  fail1:
320         free_irq(up->port.irq, up);
321
322         return ret;
323 }
324
325 /*
326  * sport_uart_tx_chars
327  *
328  * ret 1 means need to enable sport.
329  * ret 0 means do nothing.
330  */
331 static int sport_uart_tx_chars(struct sport_uart_port *up)
332 {
333         struct circ_buf *xmit = &up->port.state->xmit;
334
335         if (SPORT_GET_STAT(up) & TXF)
336                 return 0;
337
338         if (up->port.x_char) {
339                 tx_one_byte(up, up->port.x_char);
340                 up->port.icount.tx++;
341                 up->port.x_char = 0;
342                 return 1;
343         }
344
345         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
346                 /* The waiting loop to stop SPORT TX from TX interrupt is
347                  * too long. This may block SPORT RX interrupts and cause
348                  * RX FIFO overflow. So, do stop sport TX only after the last
349                  * char in TX FIFO is moved into the shift register.
350                  */
351                 if (SPORT_GET_STAT(up) & TXHRE)
352                         sport_stop_tx(&up->port);
353                 return 0;
354         }
355
356         while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
357                 tx_one_byte(up, xmit->buf[xmit->tail]);
358                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
359                 up->port.icount.tx++;
360         }
361
362         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
363                 uart_write_wakeup(&up->port);
364
365         return 1;
366 }
367
368 static unsigned int sport_tx_empty(struct uart_port *port)
369 {
370         struct sport_uart_port *up = (struct sport_uart_port *)port;
371         unsigned int stat;
372
373         stat = SPORT_GET_STAT(up);
374         pr_debug("%s stat:%04x\n", __func__, stat);
375         if (stat & TXHRE) {
376                 return TIOCSER_TEMT;
377         } else
378                 return 0;
379 }
380
381 static void sport_stop_tx(struct uart_port *port)
382 {
383         struct sport_uart_port *up = (struct sport_uart_port *)port;
384
385         pr_debug("%s enter\n", __func__);
386
387         if (!(SPORT_GET_TCR1(up) & TSPEN))
388                 return;
389
390         /* Although the hold register is empty, last byte is still in shift
391          * register and not sent out yet. So, put a dummy data into TX FIFO.
392          * Then, sport tx stops when last byte is shift out and the dummy
393          * data is moved into the shift register.
394          */
395         SPORT_PUT_TX(up, 0xffff);
396         while (!(SPORT_GET_STAT(up) & TXHRE))
397                 cpu_relax();
398
399         SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
400         SSYNC();
401
402         return;
403 }
404
405 static void sport_start_tx(struct uart_port *port)
406 {
407         struct sport_uart_port *up = (struct sport_uart_port *)port;
408
409         pr_debug("%s enter\n", __func__);
410
411         /* Write data into SPORT FIFO before enable SPROT to transmit */
412         if (sport_uart_tx_chars(up)) {
413                 /* Enable transmit, then an interrupt will generated */
414                 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
415                 SSYNC();
416         }
417
418         pr_debug("%s exit\n", __func__);
419 }
420
421 static void sport_stop_rx(struct uart_port *port)
422 {
423         struct sport_uart_port *up = (struct sport_uart_port *)port;
424
425         pr_debug("%s enter\n", __func__);
426         /* Disable sport to stop rx */
427         SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
428         SSYNC();
429 }
430
431 static void sport_break_ctl(struct uart_port *port, int break_state)
432 {
433         pr_debug("%s enter\n", __func__);
434 }
435
436 static void sport_shutdown(struct uart_port *port)
437 {
438         struct sport_uart_port *up = (struct sport_uart_port *)port;
439
440         dev_dbg(port->dev, "%s enter\n", __func__);
441
442         /* Disable sport */
443         SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
444         SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
445         SSYNC();
446
447         free_irq(up->port.irq, up);
448         free_irq(up->port.irq+1, up);
449         free_irq(up->err_irq, up);
450 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
451         if (up->cts_pin >= 0)
452                 free_irq(gpio_to_irq(up->cts_pin), up);
453         if (up->rts_pin >= 0)
454                 gpio_free(up->rts_pin);
455 #endif
456 }
457
458 static const char *sport_type(struct uart_port *port)
459 {
460         struct sport_uart_port *up = (struct sport_uart_port *)port;
461
462         pr_debug("%s enter\n", __func__);
463         return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
464 }
465
466 static void sport_release_port(struct uart_port *port)
467 {
468         pr_debug("%s enter\n", __func__);
469 }
470
471 static int sport_request_port(struct uart_port *port)
472 {
473         pr_debug("%s enter\n", __func__);
474         return 0;
475 }
476
477 static void sport_config_port(struct uart_port *port, int flags)
478 {
479         struct sport_uart_port *up = (struct sport_uart_port *)port;
480
481         pr_debug("%s enter\n", __func__);
482         up->port.type = PORT_BFIN_SPORT;
483 }
484
485 static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
486 {
487         pr_debug("%s enter\n", __func__);
488         return 0;
489 }
490
491 static void sport_set_termios(struct uart_port *port,
492                 struct ktermios *termios, struct ktermios *old)
493 {
494         struct sport_uart_port *up = (struct sport_uart_port *)port;
495         unsigned long flags;
496         int i;
497
498         pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
499
500 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
501         if (old == NULL && up->cts_pin != -1)
502                 termios->c_cflag |= CRTSCTS;
503         else if (up->cts_pin == -1)
504                 termios->c_cflag &= ~CRTSCTS;
505 #endif
506
507         switch (termios->c_cflag & CSIZE) {
508         case CS8:
509                 up->csize = 8;
510                 break;
511         case CS7:
512                 up->csize = 7;
513                 break;
514         case CS6:
515                 up->csize = 6;
516                 break;
517         case CS5:
518                 up->csize = 5;
519                 break;
520         default:
521                 pr_warn("requested word length not supported\n");
522                 break;
523         }
524
525         if (termios->c_cflag & CSTOPB) {
526                 up->stopb = 1;
527         }
528         if (termios->c_cflag & PARENB) {
529                 pr_warn("PAREN bit is not supported yet\n");
530                 /* up->parib = 1; */
531         }
532
533         spin_lock_irqsave(&up->port.lock, flags);
534
535         port->read_status_mask = 0;
536
537         /*
538          * Characters to ignore
539          */
540         port->ignore_status_mask = 0;
541
542         /* RX extract mask */
543         up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
544         /* TX masks, 8 bit data and 1 bit stop for example:
545          * mask1 = b#0111111110
546          * mask2 = b#1000000000
547          */
548         for (i = 0, up->txmask1 = 0; i < up->csize; i++)
549                 up->txmask1 |= (1<<i);
550         up->txmask2 = (1<<i);
551         if (up->stopb) {
552                 ++i;
553                 up->txmask2 |= (1<<i);
554         }
555         up->txmask1 <<= 1;
556         up->txmask2 <<= 1;
557         /* uart baud rate */
558         port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
559
560         /* Disable UART */
561         SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
562         SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
563
564         sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
565
566         /* driver TX line high after config, one dummy data is
567          * necessary to stop sport after shift one byte
568          */
569         SPORT_PUT_TX(up, 0xffff);
570         SPORT_PUT_TX(up, 0xffff);
571         SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
572         SSYNC();
573         while (!(SPORT_GET_STAT(up) & TXHRE))
574                 cpu_relax();
575         SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
576         SSYNC();
577
578         /* Port speed changed, update the per-port timeout. */
579         uart_update_timeout(port, termios->c_cflag, port->uartclk);
580
581         /* Enable sport rx */
582         SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
583         SSYNC();
584
585         spin_unlock_irqrestore(&up->port.lock, flags);
586 }
587
588 static const struct uart_ops sport_uart_ops = {
589         .tx_empty       = sport_tx_empty,
590         .set_mctrl      = sport_set_mctrl,
591         .get_mctrl      = sport_get_mctrl,
592         .stop_tx        = sport_stop_tx,
593         .start_tx       = sport_start_tx,
594         .stop_rx        = sport_stop_rx,
595         .break_ctl      = sport_break_ctl,
596         .startup        = sport_startup,
597         .shutdown       = sport_shutdown,
598         .set_termios    = sport_set_termios,
599         .type           = sport_type,
600         .release_port   = sport_release_port,
601         .request_port   = sport_request_port,
602         .config_port    = sport_config_port,
603         .verify_port    = sport_verify_port,
604 };
605
606 #define BFIN_SPORT_UART_MAX_PORTS 4
607
608 static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
609
610 #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
611 #define CLASS_BFIN_SPORT_CONSOLE        "bfin-sport-console"
612
613 static int __init
614 sport_uart_console_setup(struct console *co, char *options)
615 {
616         struct sport_uart_port *up;
617         int baud = 57600;
618         int bits = 8;
619         int parity = 'n';
620 # ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
621         int flow = 'r';
622 # else
623         int flow = 'n';
624 # endif
625
626         /* Check whether an invalid uart number has been specified */
627         if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
628                 return -ENODEV;
629
630         up = bfin_sport_uart_ports[co->index];
631         if (!up)
632                 return -ENODEV;
633
634         if (options)
635                 uart_parse_options(options, &baud, &parity, &bits, &flow);
636
637         return uart_set_options(&up->port, co, baud, parity, bits, flow);
638 }
639
640 static void sport_uart_console_putchar(struct uart_port *port, int ch)
641 {
642         struct sport_uart_port *up = (struct sport_uart_port *)port;
643
644         while (SPORT_GET_STAT(up) & TXF)
645                 barrier();
646
647         tx_one_byte(up, ch);
648 }
649
650 /*
651  * Interrupts are disabled on entering
652  */
653 static void
654 sport_uart_console_write(struct console *co, const char *s, unsigned int count)
655 {
656         struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
657         unsigned long flags;
658
659         spin_lock_irqsave(&up->port.lock, flags);
660
661         if (SPORT_GET_TCR1(up) & TSPEN)
662                 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
663         else {
664                 /* dummy data to start sport */
665                 while (SPORT_GET_STAT(up) & TXF)
666                         barrier();
667                 SPORT_PUT_TX(up, 0xffff);
668                 /* Enable transmit, then an interrupt will generated */
669                 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
670                 SSYNC();
671
672                 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
673
674                 /* Although the hold register is empty, last byte is still in shift
675                  * register and not sent out yet. So, put a dummy data into TX FIFO.
676                  * Then, sport tx stops when last byte is shift out and the dummy
677                  * data is moved into the shift register.
678                  */
679                 while (SPORT_GET_STAT(up) & TXF)
680                         barrier();
681                 SPORT_PUT_TX(up, 0xffff);
682                 while (!(SPORT_GET_STAT(up) & TXHRE))
683                         barrier();
684
685                 /* Stop sport tx transfer */
686                 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
687                 SSYNC();
688         }
689
690         spin_unlock_irqrestore(&up->port.lock, flags);
691 }
692
693 static struct uart_driver sport_uart_reg;
694
695 static struct console sport_uart_console = {
696         .name           = DEVICE_NAME,
697         .write          = sport_uart_console_write,
698         .device         = uart_console_device,
699         .setup          = sport_uart_console_setup,
700         .flags          = CON_PRINTBUFFER,
701         .index          = -1,
702         .data           = &sport_uart_reg,
703 };
704
705 #define SPORT_UART_CONSOLE      (&sport_uart_console)
706 #else
707 #define SPORT_UART_CONSOLE      NULL
708 #endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
709
710
711 static struct uart_driver sport_uart_reg = {
712         .owner          = THIS_MODULE,
713         .driver_name    = DRV_NAME,
714         .dev_name       = DEVICE_NAME,
715         .major          = 204,
716         .minor          = 84,
717         .nr             = BFIN_SPORT_UART_MAX_PORTS,
718         .cons           = SPORT_UART_CONSOLE,
719 };
720
721 #ifdef CONFIG_PM
722 static int sport_uart_suspend(struct device *dev)
723 {
724         struct sport_uart_port *sport = dev_get_drvdata(dev);
725
726         dev_dbg(dev, "%s enter\n", __func__);
727         if (sport)
728                 uart_suspend_port(&sport_uart_reg, &sport->port);
729
730         return 0;
731 }
732
733 static int sport_uart_resume(struct device *dev)
734 {
735         struct sport_uart_port *sport = dev_get_drvdata(dev);
736
737         dev_dbg(dev, "%s enter\n", __func__);
738         if (sport)
739                 uart_resume_port(&sport_uart_reg, &sport->port);
740
741         return 0;
742 }
743
744 static const struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
745         .suspend        = sport_uart_suspend,
746         .resume         = sport_uart_resume,
747 };
748 #endif
749
750 static int sport_uart_probe(struct platform_device *pdev)
751 {
752         struct resource *res;
753         struct sport_uart_port *sport;
754         int ret = 0;
755
756         dev_dbg(&pdev->dev, "%s enter\n", __func__);
757
758         if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
759                 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
760                 return -ENOENT;
761         }
762
763         if (bfin_sport_uart_ports[pdev->id] == NULL) {
764                 bfin_sport_uart_ports[pdev->id] =
765                         kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
766                 sport = bfin_sport_uart_ports[pdev->id];
767                 if (!sport) {
768                         dev_err(&pdev->dev,
769                                 "Fail to malloc sport_uart_port\n");
770                         return -ENOMEM;
771                 }
772
773                 ret = peripheral_request_list(dev_get_platdata(&pdev->dev),
774                                                 DRV_NAME);
775                 if (ret) {
776                         dev_err(&pdev->dev,
777                                 "Fail to request SPORT peripherals\n");
778                         goto out_error_free_mem;
779                 }
780
781                 spin_lock_init(&sport->port.lock);
782                 sport->port.fifosize  = SPORT_TX_FIFO_SIZE,
783                 sport->port.ops       = &sport_uart_ops;
784                 sport->port.line      = pdev->id;
785                 sport->port.iotype    = UPIO_MEM;
786                 sport->port.flags     = UPF_BOOT_AUTOCONF;
787
788                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
789                 if (res == NULL) {
790                         dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
791                         ret = -ENOENT;
792                         goto out_error_free_peripherals;
793                 }
794
795                 sport->port.membase = ioremap(res->start, resource_size(res));
796                 if (!sport->port.membase) {
797                         dev_err(&pdev->dev, "Cannot map sport IO\n");
798                         ret = -ENXIO;
799                         goto out_error_free_peripherals;
800                 }
801                 sport->port.mapbase = res->start;
802
803                 sport->port.irq = platform_get_irq(pdev, 0);
804                 if ((int)sport->port.irq < 0) {
805                         dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
806                         ret = -ENOENT;
807                         goto out_error_unmap;
808                 }
809
810                 sport->err_irq = platform_get_irq(pdev, 1);
811                 if (sport->err_irq < 0) {
812                         dev_err(&pdev->dev, "No sport status IRQ specified\n");
813                         ret = -ENOENT;
814                         goto out_error_unmap;
815                 }
816 #ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
817                 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
818                 if (res == NULL)
819                         sport->cts_pin = -1;
820                 else
821                         sport->cts_pin = res->start;
822
823                 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
824                 if (res == NULL)
825                         sport->rts_pin = -1;
826                 else
827                         sport->rts_pin = res->start;
828 #endif
829         }
830
831 #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
832         if (!is_early_platform_device(pdev)) {
833 #endif
834                 sport = bfin_sport_uart_ports[pdev->id];
835                 sport->port.dev = &pdev->dev;
836                 dev_set_drvdata(&pdev->dev, sport);
837                 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
838 #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
839         }
840 #endif
841         if (!ret)
842                 return 0;
843
844         if (sport) {
845 out_error_unmap:
846                 iounmap(sport->port.membase);
847 out_error_free_peripherals:
848                 peripheral_free_list(dev_get_platdata(&pdev->dev));
849 out_error_free_mem:
850                 kfree(sport);
851                 bfin_sport_uart_ports[pdev->id] = NULL;
852         }
853
854         return ret;
855 }
856
857 static int sport_uart_remove(struct platform_device *pdev)
858 {
859         struct sport_uart_port *sport = platform_get_drvdata(pdev);
860
861         dev_dbg(&pdev->dev, "%s enter\n", __func__);
862         dev_set_drvdata(&pdev->dev, NULL);
863
864         if (sport) {
865                 uart_remove_one_port(&sport_uart_reg, &sport->port);
866                 iounmap(sport->port.membase);
867                 peripheral_free_list(dev_get_platdata(&pdev->dev));
868                 kfree(sport);
869                 bfin_sport_uart_ports[pdev->id] = NULL;
870         }
871
872         return 0;
873 }
874
875 static struct platform_driver sport_uart_driver = {
876         .probe          = sport_uart_probe,
877         .remove         = sport_uart_remove,
878         .driver         = {
879                 .name   = DRV_NAME,
880 #ifdef CONFIG_PM
881                 .pm     = &bfin_sport_uart_dev_pm_ops,
882 #endif
883         },
884 };
885
886 #ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
887 static struct early_platform_driver early_sport_uart_driver __initdata = {
888         .class_str = CLASS_BFIN_SPORT_CONSOLE,
889         .pdrv = &sport_uart_driver,
890         .requested_id = EARLY_PLATFORM_ID_UNSET,
891 };
892
893 static int __init sport_uart_rs_console_init(void)
894 {
895         early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
896
897         early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
898                 BFIN_SPORT_UART_MAX_PORTS, 0);
899
900         register_console(&sport_uart_console);
901
902         return 0;
903 }
904 console_initcall(sport_uart_rs_console_init);
905 #endif
906
907 static int __init sport_uart_init(void)
908 {
909         int ret;
910
911         pr_info("Blackfin uart over sport driver\n");
912
913         ret = uart_register_driver(&sport_uart_reg);
914         if (ret) {
915                 pr_err("failed to register %s:%d\n",
916                                 sport_uart_reg.driver_name, ret);
917                 return ret;
918         }
919
920         ret = platform_driver_register(&sport_uart_driver);
921         if (ret) {
922                 pr_err("failed to register sport uart driver:%d\n", ret);
923                 uart_unregister_driver(&sport_uart_reg);
924         }
925
926         return ret;
927 }
928 module_init(sport_uart_init);
929
930 static void __exit sport_uart_exit(void)
931 {
932         platform_driver_unregister(&sport_uart_driver);
933         uart_unregister_driver(&sport_uart_reg);
934 }
935 module_exit(sport_uart_exit);
936
937 MODULE_AUTHOR("Sonic Zhang, Roy Huang");
938 MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
939 MODULE_LICENSE("GPL");