[ubicom32]: move new files out from platform support patch
[openwrt.git] / target / linux / ubicom32 / files / drivers / serial / ubi32_mailbox.c
1 /*
2  * drivers/serial/ubi32_mailbox.c
3  *   Ubicom32 On-Chip Mailbox Driver
4  *
5  * (C) Copyright 2009, Ubicom, Inc.
6  *
7  * This file is part of the Ubicom32 Linux Kernel Port.
8  *
9  * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10  * it and/or modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation, either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  * the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with the Ubicom32 Linux Kernel Port.  If not,
21  * see <http://www.gnu.org/licenses/>.
22  *
23  * Ubicom32 implementation derived from (with many thanks):
24  *   arch/m68knommu
25  *   arch/blackfin
26  *   arch/parisc
27  */
28 #include <linux/module.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/console.h>
32 #include <linux/sysrq.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
38 #include <asm/ip5000.h>
39
40 #define SERIAL_UBICOM_BAUDRATE  115200
41 #define SERIAL_UBICOM_DATA_BIT  8       /* Fixed parameter - do not change */
42 #define SERIAL_UBICOM_PAR_BIT   0       /* Fixed parameter - do not change */
43 #define SERIAL_UBICOM_STOP_BIT  1       /* Fixed parameter - do not change */
44
45 /* UART name and device definitions */
46 #define UBI32_MAILBOX_NAME      "ttyUM" // XXX
47 #define UBI32_MAILBOX_MAJOR     207 // XXX
48 #define UBI32_MAILBOX_MINOR     64
49
50 #define PORT_UBI32_MAILBOX      1235
51 #define NR_PORTS 1
52
53 #define get_sclk() 0
54
55 struct ubi32_mailbox_port {
56         struct uart_port port;
57         /*
58          * NOTE (rkeller):
59          * the uart port is wrapped in another structure in case we need to hold more state than
60          * what we can hold in the uart_port.
61          * Not sure if we need this, I took over the concept from the blackfin driver.
62          */
63 } ubi32_mailbox_ports[NR_PORTS];
64
65 struct ubi32_mailbox_resource {
66         int uart_base_addr;
67         int uart_irq;
68 } ubi32_mailbox_resource[NR_PORTS] = {
69         /*
70          * uart_base_addr has to be non-NULL because it is put in the uart_port membase.
71          * If membase if null the kernel skips the configuration and our port_type never gets set.
72          */
73         {ISD_MAILBOX_BASE, ISD_MAILBOX_INT}
74 };
75
76 static volatile struct ubicom32_isd_mailbox {
77         volatile u32_t in;
78         volatile u32_t out;
79         volatile u32_t status;
80 } *ubi32_mailbox = (struct ubicom32_isd_mailbox *)ISD_MAILBOX_BASE;
81
82 static void ubi32_mailbox_tx_chars(struct ubi32_mailbox_port *uart);
83
84 static void ubi32_mailbox_mctrl_check(struct ubi32_mailbox_port *uart);
85
86 #define TRUE 1
87 #define FALSE 0
88
89 static int mailbox_console_flg = TRUE;
90 static int num_timeouts = 0;
91
92 /*
93  * dummy functions and defined to be able to compile the Blackfin code
94  */
95 #define UART_GET_LSR(port) (1)
96 #define UART_PUT_LSR(port, bits)
97 #define UART_CLEAR_LSR(port) (1)
98 #define TEMT 1
99 #define TFI 1
100 #define BI 1
101 #define PE 1
102 #define OE 1
103 #define FE 1
104 #define THRE 1
105 #define DR 1
106 #define UART_GET_LCR(port) (1)
107 #define UART_PUT_LCR(port, bits)
108 #define SB 1
109 #define STB 1
110 #define PEN 1
111 #define EPS 1
112 #define STP 1
113 #define WLS(n) 0
114 #define UART_GET_IER(port) (1)
115 #define UART_SET_IER(port, bits)
116 #define UART_CLEAR_IER(port, bits)
117 #define ETBEI 0
118 #define ERBFI 0
119 #define UART_GET_CHAR(port) ubi32_mailbox_get_char()
120 #define UART_PUT_CHAR(port, ch) ubi32_mailbox_put_char(ch)
121 #define SSYNC()
122 #define UART_GET_DLL(port) 0
123 #define UART_PUT_DLL(port, ch)
124 #define UART_GET_DLH(port) 0
125 #define UART_PUT_DLH(port, ch)
126 #define UART_GET_GCTL(port) (0)
127 #define UART_PUT_GCTL(port, ch)
128 #define UCEN 1
129
130 /*
131  * ubi32_mailbox_get_char_avail()
132  */
133 static int ubi32_mailbox_get_char_avail(void)
134 {
135         return !(ubi32_mailbox->status & ISD_MAILBOX_STATUS_IN_EMPTY);
136 }
137
138 /*
139  * ubi32_mailbox_get_char()
140  */
141 static u32_t ubi32_mailbox_get_char(void)
142 {
143         if (mailbox_console_flg == TRUE) {
144                 /*
145                  * Mailbox console is connected.
146                  */
147                 while (ubi32_mailbox->status & ISD_MAILBOX_STATUS_IN_EMPTY);
148                 return ubi32_mailbox->in & 0xff;
149         }
150
151         /*
152          * Mailbox console was not connected.
153          */
154         if (ubi32_mailbox->status & ISD_MAILBOX_STATUS_IN_EMPTY) {
155                 return 0xff;
156         }
157
158         /*
159          * Mailbox console is connecting.
160          */
161         mailbox_console_flg = TRUE;
162         num_timeouts = 0;
163         return ubi32_mailbox->in & 0xff;
164 }
165
166 #define MAILBOX_MAX_ATTEMPTS 1000000
167 #define MAILBOX_MAX_TIMEOUTS 5
168 /*
169  * ubi32_mailbox_put_char()
170  */
171 static void ubi32_mailbox_put_char(u32_t v)
172 {
173         /*
174          * Wait to be able to output.
175          */
176         u32_t num_attempts = 0;
177
178         if(mailbox_console_flg == TRUE) {
179                 while(num_attempts++ < MAILBOX_MAX_ATTEMPTS) {
180                         if(ubi32_mailbox->status & ISD_MAILBOX_STATUS_OUT_EMPTY) {
181                                 break;
182                         }
183                 }
184
185                 /*
186                  * If timed out more than 5 times on send, mailbox console is disconnected now.
187                  */
188                 if (num_attempts > MAILBOX_MAX_ATTEMPTS) {
189                         if (num_timeouts++ > MAILBOX_MAX_TIMEOUTS) {
190                                 mailbox_console_flg = FALSE;
191                         }
192                 }
193         }
194
195         asm volatile(
196                 "pipe_flush 0   \n\t"
197                 "pipe_flush 0   \n\t"
198                 "pipe_flush 0   \n\t"
199                 "pipe_flush 0   \n\t"
200                 "pipe_flush 0   \n\t"
201                 "pipe_flush 0   \n\t"
202                 "pipe_flush 0   \n\t"
203         );
204
205         ubi32_mailbox->out = v & 0xff;
206 }
207
208 static void ubi32_mailbox_hw_init(struct ubi32_mailbox_port *uart)
209 {
210 // NOTE: It does not do any good to do these here because we are running on the linux hardware thread,
211 //      and these have to be called on the ldsr thread.
212 //      ubicom32_clear_interrupt(ISD_MAILBOX_INT);
213 //      ubicom32_enable_interrupt(ISD_MAILBOX_INT);
214 }
215
216 /*
217  * interrupts are disabled on entry
218  */
219 static void ubi32_mailbox_stop_tx(struct uart_port *port)
220 {
221 //      struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
222 //      struct circ_buf *xmit = &uart->port.info->xmit;
223
224         while (!(UART_GET_LSR(uart) & TEMT))
225                 cpu_relax();
226
227         /* Clear TFI bit */
228         UART_PUT_LSR(uart, TFI);
229         UART_CLEAR_IER(uart, ETBEI);
230 }
231
232 /*
233  * port is locked and interrupts are disabled
234  */
235 static void ubi32_mailbox_start_tx(struct uart_port *port)
236 {
237         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
238
239         UART_SET_IER(uart, ETBEI);
240
241         ubi32_mailbox_tx_chars(uart);
242 }
243
244 /*
245  * Interrupts are enabled
246  */
247 static void ubi32_mailbox_stop_rx(struct uart_port *port)
248 {
249 //      struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
250         UART_CLEAR_IER(uart, ERBFI);
251 }
252
253 /*
254  * Set the modem control timer to fire immediately.
255  */
256 static void ubi32_mailbox_enable_ms(struct uart_port *port)
257 {
258 }
259
260 static void ubi32_mailbox_rx_chars(struct ubi32_mailbox_port *uart)
261 {
262         struct uart_info *info = uart->port.info;
263         struct tty_struct *tty = info->port.tty;
264         unsigned int status, ch, flg;
265
266         status = 0; // XXX? UART_GET_LSR(uart);
267         UART_CLEAR_LSR(uart);
268
269         ch = UART_GET_CHAR(uart);
270
271         if(ch == 0xff)
272                 return;
273
274         uart->port.icount.rx++;
275
276         if (status & BI) {
277                 uart->port.icount.brk++;
278                 if (uart_handle_break(&uart->port))
279                         goto ignore_char;
280                 status &= ~(PE | FE);
281         }
282         if (status & PE)
283                 uart->port.icount.parity++;
284         if (status & OE)
285                 uart->port.icount.overrun++;
286         if (status & FE)
287                 uart->port.icount.frame++;
288
289         status &= uart->port.read_status_mask;
290
291         if (status & BI)
292                 flg = TTY_BREAK;
293         else if (status & PE)
294                 flg = TTY_PARITY;
295         else if (status & FE)
296                 flg = TTY_FRAME;
297         else
298                 flg = TTY_NORMAL;
299
300         if (uart_handle_sysrq_char(&uart->port, ch))
301                 goto ignore_char;
302
303         uart_insert_char(&uart->port, status, OE, ch, flg);
304
305  ignore_char:
306         tty_flip_buffer_push(tty);
307 }
308
309 static void ubi32_mailbox_tx_chars(struct ubi32_mailbox_port *uart)
310 {
311         struct circ_buf *xmit = &uart->port.info->xmit;
312
313         if (uart->port.x_char) {
314                 UART_PUT_CHAR(uart, uart->port.x_char);
315                 uart->port.icount.tx++;
316                 uart->port.x_char = 0;
317         }
318         /*
319          * Check the modem control lines before
320          * transmitting anything.
321          */
322         ubi32_mailbox_mctrl_check(uart);
323
324         if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
325                 ubi32_mailbox_stop_tx(&uart->port);
326                 return;
327         }
328
329         while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
330                 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
331                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
332                 uart->port.icount.tx++;
333                 SSYNC();
334         }
335
336         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
337                 uart_write_wakeup(&uart->port);
338
339         if (uart_circ_empty(xmit))
340                 ubi32_mailbox_stop_tx(&uart->port);
341 }
342
343 static irqreturn_t ubi32_mailbox_isr(int irq, void *dev_id)
344 {
345         struct ubi32_mailbox_port *uart = dev_id;
346
347         spin_lock(&uart->port.lock);
348
349         //XXX?while (UART_GET_LSR(uart) & DR)
350
351         /*
352          * RX process
353          */
354         while (ubi32_mailbox_get_char_avail()) {
355                 ubi32_mailbox_rx_chars(uart);
356         }
357
358 #if 0
359         /*
360          * TX process
361          */
362         if (this_uart.tx_in == this_uart.tx_out) {
363                 UBICOM32_IO_PORT(SERIAL_UBICOM_PORT)->int_mask &= ~IO_PORTX_INT_SERDES_TXBE;
364         } else if (UBICOM32_IO_PORT(SERIAL_UBICOM_PORT)->int_status & IO_PORTX_INT_SERDES_TXBE) {
365                 uart_ubicom32_send(this_uart.tx_buf[this_uart.tx_out & (SERIAL_UBICOM_BUF_SIZE - 1)]);
366                 this_uart.tx_out++;
367                 UBICOM32_IO_PORT(SERIAL_UBICOM_PORT)->int_mask |= IO_PORTX_INT_SERDES_TXBE;
368         }
369 #endif
370
371         spin_unlock(&uart->port.lock);
372
373         return IRQ_HANDLED;
374 }
375 #if 0
376 static irqreturn_t ubi32_mailbox_tx_int(int irq, void *dev_id)
377 {
378         struct ubi32_mailbox_port *uart = dev_id;
379
380         spin_lock(&uart->port.lock);
381         if (UART_GET_LSR(uart) & THRE)
382                 ubi32_mailbox_tx_chars(uart);
383         spin_unlock(&uart->port.lock);
384
385         return IRQ_HANDLED;
386 }
387 #endif
388
389 /*
390  * Return TIOCSER_TEMT when transmitter is not busy.
391  */
392 static unsigned int ubi32_mailbox_tx_empty(struct uart_port *port)
393 {
394 //      struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
395         unsigned short lsr;
396
397         lsr = UART_GET_LSR(uart);
398         if (lsr & TEMT)
399                 return TIOCSER_TEMT;
400         else
401                 return 0;
402 }
403
404 static unsigned int ubi32_mailbox_get_mctrl(struct uart_port *port)
405 {
406                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
407 }
408
409 static void ubi32_mailbox_set_mctrl(struct uart_port *port, unsigned int mctrl)
410 {
411 }
412
413 /*
414  * Handle any change of modem status signal since we were last called.
415  */
416 static void ubi32_mailbox_mctrl_check(struct ubi32_mailbox_port *uart)
417 {
418 }
419
420 /*
421  * Interrupts are always disabled.
422  */
423 static void ubi32_mailbox_break_ctl(struct uart_port *port, int break_state)
424 {
425 //      struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
426         u16 lcr = UART_GET_LCR(uart);
427         if (break_state)
428                 lcr |= SB;
429         else
430                 lcr &= ~SB;
431         UART_PUT_LCR(uart, lcr);
432         SSYNC();
433 }
434
435 static int ubi32_mailbox_startup(struct uart_port *port)
436 {
437         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
438
439         if (request_irq(uart->port.irq, ubi32_mailbox_isr, IRQF_DISABLED,
440              "UBI32_MAILBOX", uart)) {
441                 printk(KERN_NOTICE "Unable to attach Ubicom32 SERDES interrupt\n");
442                 return -EBUSY;
443         }
444
445         UART_SET_IER(uart, ERBFI);
446         return 0;
447 }
448
449 static void ubi32_mailbox_shutdown(struct uart_port *port)
450 {
451         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
452
453         free_irq(uart->port.irq, uart);
454 }
455
456 static void
457 ubi32_mailbox_set_termios(struct uart_port *port, struct ktermios *termios,
458                    struct ktermios *old)
459 {
460         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
461         unsigned long flags;
462         unsigned int baud, quot;
463         unsigned short val, ier, lsr, lcr = 0;
464
465         switch (termios->c_cflag & CSIZE) {
466         case CS8:
467                 lcr = WLS(8);
468                 break;
469         case CS7:
470                 lcr = WLS(7);
471                 break;
472         case CS6:
473                 lcr = WLS(6);
474                 break;
475         case CS5:
476                 lcr = WLS(5);
477                 break;
478         default:
479                 printk(KERN_ERR "%s: word lengh not supported\n",
480                         __FUNCTION__);
481         }
482
483         if (termios->c_cflag & CSTOPB)
484                 lcr |= STB;
485         if (termios->c_cflag & PARENB)
486                 lcr |= PEN;
487         if (!(termios->c_cflag & PARODD))
488                 lcr |= EPS;
489         if (termios->c_cflag & CMSPAR)
490                 lcr |= STP;
491
492         port->read_status_mask = OE;
493         if (termios->c_iflag & INPCK)
494                 port->read_status_mask |= (FE | PE);
495         if (termios->c_iflag & (BRKINT | PARMRK))
496                 port->read_status_mask |= BI;
497
498         /*
499          * Characters to ignore
500          */
501         port->ignore_status_mask = 0;
502         if (termios->c_iflag & IGNPAR)
503                 port->ignore_status_mask |= FE | PE;
504         if (termios->c_iflag & IGNBRK) {
505                 port->ignore_status_mask |= BI;
506                 /*
507                  * If we're ignoring parity and break indicators,
508                  * ignore overruns too (for real raw support).
509                  */
510                 if (termios->c_iflag & IGNPAR)
511                         port->ignore_status_mask |= OE;
512         }
513
514         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
515         quot = uart_get_divisor(port, baud);
516         spin_lock_irqsave(&uart->port.lock, flags);
517
518         do {
519                 lsr = UART_GET_LSR(uart);
520         } while (!(lsr & TEMT));
521
522         /* Disable UART */
523         ier = UART_GET_IER(uart);
524         UART_CLEAR_IER(uart, 0xF);
525
526         UART_PUT_DLL(uart, quot & 0xFF);
527         SSYNC();
528         UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
529         SSYNC();
530
531         UART_PUT_LCR(uart, lcr);
532
533         /* Enable UART */
534         UART_SET_IER(uart, ier);
535
536         val = UART_GET_GCTL(uart);
537         val |= UCEN;
538         UART_PUT_GCTL(uart, val);
539
540         spin_unlock_irqrestore(&uart->port.lock, flags);
541 }
542
543 static const char *ubi32_mailbox_type(struct uart_port *port)
544 {
545         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
546
547         return uart->port.type == PORT_UBI32_MAILBOX ? "UBI32_MAILBOX" : NULL;
548 }
549
550 /*
551  * Release the memory region(s) being used by 'port'.
552  */
553 static void ubi32_mailbox_release_port(struct uart_port *port)
554 {
555 }
556
557 /*
558  * Request the memory region(s) being used by 'port'.
559  */
560 static int ubi32_mailbox_request_port(struct uart_port *port)
561 {
562         return 0;
563 }
564
565 /*
566  * Configure/autoconfigure the port.
567  */
568 static void ubi32_mailbox_config_port(struct uart_port *port, int flags)
569 {
570         struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
571
572         if (flags & UART_CONFIG_TYPE && ubi32_mailbox_request_port(&uart->port) == 0)
573                 uart->port.type = PORT_UBI32_MAILBOX;
574 }
575
576 /*
577  * Verify the new serial_struct (for TIOCSSERIAL).
578  * The only change we allow are to the flags and type, and
579  * even then only between PORT_UBI32_MAILBOX and PORT_UNKNOWN
580  */
581 static int
582 ubi32_mailbox_verify_port(struct uart_port *port, struct serial_struct *ser)
583 {
584         return 0;
585 }
586
587 static struct uart_ops ubi32_mailbox_pops = {
588         .tx_empty       = ubi32_mailbox_tx_empty,
589         .set_mctrl      = ubi32_mailbox_set_mctrl,
590         .get_mctrl      = ubi32_mailbox_get_mctrl,
591         .stop_tx        = ubi32_mailbox_stop_tx,
592         .start_tx       = ubi32_mailbox_start_tx,
593         .stop_rx        = ubi32_mailbox_stop_rx,
594         .enable_ms      = ubi32_mailbox_enable_ms,
595         .break_ctl      = ubi32_mailbox_break_ctl,
596         .startup        = ubi32_mailbox_startup,
597         .shutdown       = ubi32_mailbox_shutdown,
598         .set_termios    = ubi32_mailbox_set_termios,
599         .type           = ubi32_mailbox_type,
600         .release_port   = ubi32_mailbox_release_port,
601         .request_port   = ubi32_mailbox_request_port,
602         .config_port    = ubi32_mailbox_config_port,
603         .verify_port    = ubi32_mailbox_verify_port,
604 };
605
606 static void __init ubi32_mailbox_init_ports(void)
607 {
608         static int first = 1;
609         int i;
610
611         if (!first)
612                 return;
613         first = 0;
614
615         for (i = 0; i < NR_PORTS; i++) {
616                 ubi32_mailbox_ports[i].port.uartclk   = get_sclk();
617                 ubi32_mailbox_ports[i].port.ops       = &ubi32_mailbox_pops;
618                 ubi32_mailbox_ports[i].port.line      = i;
619                 ubi32_mailbox_ports[i].port.iotype    = UPIO_MEM;
620                 ubi32_mailbox_ports[i].port.membase   =
621                         (void __iomem *)ubi32_mailbox_resource[i].uart_base_addr;
622                 ubi32_mailbox_ports[i].port.mapbase   =
623                         ubi32_mailbox_resource[i].uart_base_addr;
624                 ubi32_mailbox_ports[i].port.irq       =
625                         ubi32_mailbox_resource[i].uart_irq;
626                 ubi32_mailbox_ports[i].port.flags     = UPF_BOOT_AUTOCONF;
627                 spin_lock_init(&ubi32_mailbox_ports[i].port.lock);
628
629                 ubi32_mailbox_hw_init(&ubi32_mailbox_ports[i]);
630         }
631
632 }
633
634 #ifdef CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE
635 /*
636  * If the port was already initialised (eg, by a boot loader),
637  * try to determine the current setup.
638  */
639 static void __init
640 ubi32_mailbox_console_get_options(struct ubi32_mailbox_port *uart, int *baud,
641                            int *parity, int *bits)
642 {
643         unsigned short status;
644
645         status = UART_GET_IER(uart) & (ERBFI | ETBEI);
646         if (status == (ERBFI | ETBEI)) {
647                 /* ok, the port was enabled */
648                 unsigned short lcr;
649                 unsigned short dlh, dll;
650
651                 lcr = UART_GET_LCR(uart);
652
653                 *parity = 'n';
654                 if (lcr & PEN) {
655                         if (lcr & EPS)
656                                 *parity = 'e';
657                         else
658                                 *parity = 'o';
659                 }
660                 switch (lcr & 0x03) {
661                         case 0: *bits = 5; break;
662                         case 1: *bits = 6; break;
663                         case 2: *bits = 7; break;
664                         case 3: *bits = 8; break;
665                 }
666
667                 dll = UART_GET_DLL(uart);
668                 dlh = UART_GET_DLH(uart);
669
670                 *baud = get_sclk() / (16*(dll | dlh << 8));
671         }
672         pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
673 }
674 #endif
675
676 #if defined(CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
677 static struct uart_driver ubi32_mailbox_reg;
678
679 static int __init
680 ubi32_mailbox_console_setup(struct console *co, char *options)
681 {
682         struct ubi32_mailbox_port *uart;
683 # ifdef CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE
684         int baud = SERIAL_UBICOM_BAUDRATE;
685         int bits = 8;
686         int parity = 'n';
687         int flow = 'n';
688 # endif
689
690         /*
691          * Check whether an invalid uart number has been specified, and
692          * if so, search for the first available port that does have
693          * console support.
694          */
695         if (co->index == -1 || co->index >= NR_PORTS)
696                 co->index = 0;
697         uart = &ubi32_mailbox_ports[co->index];
698
699 # ifdef CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE
700         if (options)
701                 uart_parse_options(options, &baud, &parity, &bits, &flow);
702         else
703                 ubi32_mailbox_console_get_options(uart, &baud, &parity, &bits);
704
705         //JB return uart_set_options(&uart->port, co, baud, parity, bits, flow);
706         return 0;
707 # else
708         return 0;
709 # endif
710 }
711 #endif /* defined (CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE) ||
712                                  defined (CONFIG_EARLY_PRINTK) */
713
714 #ifdef CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE
715 static void ubi32_mailbox_console_putchar(struct uart_port *port, int ch)
716 {
717 //      struct ubi32_mailbox_port *uart = (struct ubi32_mailbox_port *)port;
718         while (!(UART_GET_LSR(uart) & THRE))
719                 barrier();
720         UART_PUT_CHAR(uart, ch);
721         SSYNC();
722 }
723
724 /*
725  * Interrupts are disabled on entering
726  */
727 static void
728 ubi32_mailbox_console_write(struct console *co, const char *s, unsigned int count)
729 {
730         struct ubi32_mailbox_port *uart = &ubi32_mailbox_ports[co->index];
731         unsigned long flags = 0;
732
733         spin_lock_irqsave(&uart->port.lock, flags);
734         uart_console_write(&uart->port, s, count, ubi32_mailbox_console_putchar);
735         spin_unlock_irqrestore(&uart->port.lock, flags);
736
737 }
738
739 static struct console ubi32_mailbox_console = {
740         .name           = UBI32_MAILBOX_NAME,
741         .write          = ubi32_mailbox_console_write,
742         .device         = uart_console_device,
743         .setup          = ubi32_mailbox_console_setup,
744         .flags          = CON_PRINTBUFFER,
745         .index          = -1,
746         .data           = &ubi32_mailbox_reg,
747 };
748
749 static int __init ubi32_mailbox_console_init(void)
750 {
751         ubi32_mailbox_init_ports();
752         register_console(&ubi32_mailbox_console);
753         return 0;
754 }
755 console_initcall(ubi32_mailbox_console_init);
756
757 #define UBI32_MAILBOX_CONSOLE   &ubi32_mailbox_console
758 #else
759 #define UBI32_MAILBOX_CONSOLE   NULL
760 #endif /* CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE */
761
762
763 #ifdef CONFIG_EARLY_PRINTK
764 static __init void ubi32_mailbox_early_putc(struct uart_port *port, int ch)
765 {
766         UART_PUT_CHAR(uart, ch);
767 }
768
769 static __init void ubi32_mailbox_early_write(struct console *con, const char *s,
770                                         unsigned int n)
771 {
772         struct ubi32_mailbox_port *uart = &ubi32_mailbox_ports[con->index];
773         unsigned int i;
774
775         for (i = 0; i < n; i++, s++) {
776                 if (*s == '\n')
777                         ubi32_mailbox_early_putc(&uart->port, '\r');
778                 ubi32_mailbox_early_putc(&uart->port, *s);
779         }
780 }
781
782 static struct __init console ubi32_mailbox_early_console = {
783         .name = "early_UM",
784         .write = ubi32_mailbox_early_write,
785         .device = uart_console_device,
786         .flags = CON_PRINTBUFFER,
787         .setup = ubi32_mailbox_console_setup,
788         .index = -1,
789         .data  = &ubi32_mailbox_reg,
790 };
791
792 /*
793  * XXX Unused in our driver. Need to find out what the termios initialization is good/needed for.
794  */
795 struct console __init *ubi32_mailbox_early_init(unsigned int port,
796                                                 unsigned int cflag)
797 {
798         struct ubi32_mailbox_port *uart;
799         struct ktermios t;
800
801         if (port == -1 || port >= NR_PORTS)
802                 port = 0;
803         ubi32_mailbox_init_ports();
804         ubi32_mailbox_early_console.index = port;
805         uart = &ubi32_mailbox_ports[port];
806         t.c_cflag = cflag;
807         t.c_iflag = 0;
808         t.c_oflag = 0;
809         t.c_lflag = ICANON;
810         t.c_line = port;
811         ubi32_mailbox_set_termios(&uart->port, &t, &t);
812         return &ubi32_mailbox_early_console;
813 }
814
815 #endif /* CONFIG_SERIAL_UBI32_MAILBOX_CONSOLE */
816
817 static struct uart_driver ubi32_mailbox_reg = {
818         .owner                  = THIS_MODULE,
819         .driver_name            = "ubi32_mailbox",
820         .dev_name               = UBI32_MAILBOX_NAME,
821         .major                  = UBI32_MAILBOX_MAJOR,
822         .minor                  = UBI32_MAILBOX_MINOR,
823         .nr                     = NR_PORTS,
824         .cons                   = UBI32_MAILBOX_CONSOLE,
825 };
826
827 static int ubi32_mailbox_suspend(struct platform_device *dev, pm_message_t state)
828 {
829         struct ubi32_mailbox_port *uart = platform_get_drvdata(dev);
830
831         if (uart)
832                 uart_suspend_port(&ubi32_mailbox_reg, &uart->port);
833
834         return 0;
835 }
836
837 static int ubi32_mailbox_resume(struct platform_device *dev)
838 {
839         struct ubi32_mailbox_port *uart = platform_get_drvdata(dev);
840
841         if (uart)
842                 uart_resume_port(&ubi32_mailbox_reg, &uart->port);
843
844         return 0;
845 }
846
847 static int ubi32_mailbox_probe(struct platform_device *dev)
848 {
849         struct resource *res = dev->resource;
850         int i;
851
852         for (i = 0; i < dev->num_resources; i++, res++)
853                 if (res->flags & IORESOURCE_MEM)
854                         break;
855
856         if (i < dev->num_resources) {
857                 for (i = 0; i < NR_PORTS; i++, res++) {
858                         if (ubi32_mailbox_ports[i].port.mapbase != res->start)
859                                 continue;
860                         ubi32_mailbox_ports[i].port.dev = &dev->dev;
861                         uart_add_one_port(&ubi32_mailbox_reg, &ubi32_mailbox_ports[i].port);
862                         platform_set_drvdata(dev, &ubi32_mailbox_ports[i]);
863                 }
864         }
865
866         return 0;
867 }
868
869 static int ubi32_mailbox_remove(struct platform_device *pdev)
870 {
871         struct ubi32_mailbox_port *uart = platform_get_drvdata(pdev);
872
873         platform_set_drvdata(pdev, NULL);
874
875         if (uart)
876                 uart_remove_one_port(&ubi32_mailbox_reg, &uart->port);
877
878         return 0;
879 }
880
881 static struct platform_driver ubi32_mailbox_driver = {
882         .probe          = ubi32_mailbox_probe,
883         .remove         = ubi32_mailbox_remove,
884         .suspend        = ubi32_mailbox_suspend,
885         .resume         = ubi32_mailbox_resume,
886         .driver         = {
887                 .name   = "ubi32-mbox",
888                 .owner  = THIS_MODULE,
889         },
890 };
891
892 static int __init ubi32_mailbox_init(void)
893 {
894         int ret;
895
896         pr_info("Serial: Ubicom32 mailbox serial driver.\n");
897
898         mailbox_console_flg = TRUE;
899         num_timeouts = 0;
900         ubi32_mailbox_init_ports();
901
902         ret = uart_register_driver(&ubi32_mailbox_reg);
903         if (ret == 0) {
904                 ret = platform_driver_register(&ubi32_mailbox_driver);
905                 if (ret) {
906                         pr_debug("uart register failed\n");
907                         uart_unregister_driver(&ubi32_mailbox_reg);
908                 }
909         }
910
911         /*
912          * XXX HACK: currently probe does not get called, but the port needs to be added to work.
913          */
914         uart_add_one_port(&ubi32_mailbox_reg, &ubi32_mailbox_ports[0].port);
915         return ret;
916 }
917
918 static void __exit ubi32_mailbox_exit(void)
919 {
920         platform_driver_unregister(&ubi32_mailbox_driver);
921         uart_unregister_driver(&ubi32_mailbox_reg);
922 }
923
924 module_init(ubi32_mailbox_init);
925 module_exit(ubi32_mailbox_exit);
926
927 MODULE_ALIAS_CHARDEV_MAJOR(UBI32_MAILBOX_MAJOR);
928 MODULE_ALIAS("platform:ubi32_mailbox");