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