tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-block.git] / drivers / tty / serial / vr41xx_siu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Driver for NEC VR4100 series Serial Interface Unit.
4  *
5  *  Copyright (C) 2004-2008  Yoichi Yuasa <yuasa@linux-mips.org>
6  *
7  *  Based on drivers/serial/8250.c, by Russell King.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25 #define SUPPORT_SYSRQ
26 #endif
27
28 #include <linux/console.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/ioport.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/serial.h>
36 #include <linux/serial_core.h>
37 #include <linux/serial_reg.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40
41 #include <asm/io.h>
42 #include <asm/vr41xx/siu.h>
43 #include <asm/vr41xx/vr41xx.h>
44
45 #define SIU_BAUD_BASE   1152000
46 #define SIU_MAJOR       204
47 #define SIU_MINOR_BASE  82
48
49 #define RX_MAX_COUNT    256
50 #define TX_MAX_COUNT    15
51
52 #define SIUIRSEL        0x08
53  #define TMICMODE       0x20
54  #define TMICTX         0x10
55  #define IRMSEL         0x0c
56  #define IRMSEL_HP      0x08
57  #define IRMSEL_TEMIC   0x04
58  #define IRMSEL_SHARP   0x00
59  #define IRUSESEL       0x02
60  #define SIRSEL         0x01
61
62 static struct uart_port siu_uart_ports[SIU_PORTS_MAX] = {
63         [0 ... SIU_PORTS_MAX-1] = {
64                 .lock   = __SPIN_LOCK_UNLOCKED(siu_uart_ports->lock),
65                 .irq    = 0,
66         },
67 };
68
69 #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
70 static uint8_t lsr_break_flag[SIU_PORTS_MAX];
71 #endif
72
73 #define siu_read(port, offset)          readb((port)->membase + (offset))
74 #define siu_write(port, offset, value)  writeb((value), (port)->membase + (offset))
75
76 void vr41xx_select_siu_interface(siu_interface_t interface)
77 {
78         struct uart_port *port;
79         unsigned long flags;
80         uint8_t irsel;
81
82         port = &siu_uart_ports[0];
83
84         spin_lock_irqsave(&port->lock, flags);
85
86         irsel = siu_read(port, SIUIRSEL);
87         if (interface == SIU_INTERFACE_IRDA)
88                 irsel |= SIRSEL;
89         else
90                 irsel &= ~SIRSEL;
91         siu_write(port, SIUIRSEL, irsel);
92
93         spin_unlock_irqrestore(&port->lock, flags);
94 }
95 EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
96
97 void vr41xx_use_irda(irda_use_t use)
98 {
99         struct uart_port *port;
100         unsigned long flags;
101         uint8_t irsel;
102
103         port = &siu_uart_ports[0];
104
105         spin_lock_irqsave(&port->lock, flags);
106
107         irsel = siu_read(port, SIUIRSEL);
108         if (use == FIR_USE_IRDA)
109                 irsel |= IRUSESEL;
110         else
111                 irsel &= ~IRUSESEL;
112         siu_write(port, SIUIRSEL, irsel);
113
114         spin_unlock_irqrestore(&port->lock, flags);
115 }
116 EXPORT_SYMBOL_GPL(vr41xx_use_irda);
117
118 void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
119 {
120         struct uart_port *port;
121         unsigned long flags;
122         uint8_t irsel;
123
124         port = &siu_uart_ports[0];
125
126         spin_lock_irqsave(&port->lock, flags);
127
128         irsel = siu_read(port, SIUIRSEL);
129         irsel &= ~(IRMSEL | TMICTX | TMICMODE);
130         switch (module) {
131         case SHARP_IRDA:
132                 irsel |= IRMSEL_SHARP;
133                 break;
134         case TEMIC_IRDA:
135                 irsel |= IRMSEL_TEMIC | TMICMODE;
136                 if (speed == IRDA_TX_4MBPS)
137                         irsel |= TMICTX;
138                 break;
139         case HP_IRDA:
140                 irsel |= IRMSEL_HP;
141                 break;
142         default:
143                 break;
144         }
145         siu_write(port, SIUIRSEL, irsel);
146
147         spin_unlock_irqrestore(&port->lock, flags);
148 }
149 EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
150
151 static inline void siu_clear_fifo(struct uart_port *port)
152 {
153         siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
154         siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
155                                   UART_FCR_CLEAR_XMIT);
156         siu_write(port, UART_FCR, 0);
157 }
158
159 static inline unsigned long siu_port_size(struct uart_port *port)
160 {
161         switch (port->type) {
162         case PORT_VR41XX_SIU:
163                 return 11UL;
164         case PORT_VR41XX_DSIU:
165                 return 8UL;
166         }
167
168         return 0;
169 }
170
171 static inline unsigned int siu_check_type(struct uart_port *port)
172 {
173         if (port->line == 0)
174                 return PORT_VR41XX_SIU;
175         if (port->line == 1 && port->irq)
176                 return PORT_VR41XX_DSIU;
177
178         return PORT_UNKNOWN;
179 }
180
181 static inline const char *siu_type_name(struct uart_port *port)
182 {
183         switch (port->type) {
184         case PORT_VR41XX_SIU:
185                 return "SIU";
186         case PORT_VR41XX_DSIU:
187                 return "DSIU";
188         }
189
190         return NULL;
191 }
192
193 static unsigned int siu_tx_empty(struct uart_port *port)
194 {
195         uint8_t lsr;
196
197         lsr = siu_read(port, UART_LSR);
198         if (lsr & UART_LSR_TEMT)
199                 return TIOCSER_TEMT;
200
201         return 0;
202 }
203
204 static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
205 {
206         uint8_t mcr = 0;
207
208         if (mctrl & TIOCM_DTR)
209                 mcr |= UART_MCR_DTR;
210         if (mctrl & TIOCM_RTS)
211                 mcr |= UART_MCR_RTS;
212         if (mctrl & TIOCM_OUT1)
213                 mcr |= UART_MCR_OUT1;
214         if (mctrl & TIOCM_OUT2)
215                 mcr |= UART_MCR_OUT2;
216         if (mctrl & TIOCM_LOOP)
217                 mcr |= UART_MCR_LOOP;
218
219         siu_write(port, UART_MCR, mcr);
220 }
221
222 static unsigned int siu_get_mctrl(struct uart_port *port)
223 {
224         uint8_t msr;
225         unsigned int mctrl = 0;
226
227         msr = siu_read(port, UART_MSR);
228         if (msr & UART_MSR_DCD)
229                 mctrl |= TIOCM_CAR;
230         if (msr & UART_MSR_RI)
231                 mctrl |= TIOCM_RNG;
232         if (msr & UART_MSR_DSR)
233                 mctrl |= TIOCM_DSR;
234         if (msr & UART_MSR_CTS)
235                 mctrl |= TIOCM_CTS;
236
237         return mctrl;
238 }
239
240 static void siu_stop_tx(struct uart_port *port)
241 {
242         unsigned long flags;
243         uint8_t ier;
244
245         spin_lock_irqsave(&port->lock, flags);
246
247         ier = siu_read(port, UART_IER);
248         ier &= ~UART_IER_THRI;
249         siu_write(port, UART_IER, ier);
250
251         spin_unlock_irqrestore(&port->lock, flags);
252 }
253
254 static void siu_start_tx(struct uart_port *port)
255 {
256         unsigned long flags;
257         uint8_t ier;
258
259         spin_lock_irqsave(&port->lock, flags);
260
261         ier = siu_read(port, UART_IER);
262         ier |= UART_IER_THRI;
263         siu_write(port, UART_IER, ier);
264
265         spin_unlock_irqrestore(&port->lock, flags);
266 }
267
268 static void siu_stop_rx(struct uart_port *port)
269 {
270         unsigned long flags;
271         uint8_t ier;
272
273         spin_lock_irqsave(&port->lock, flags);
274
275         ier = siu_read(port, UART_IER);
276         ier &= ~UART_IER_RLSI;
277         siu_write(port, UART_IER, ier);
278
279         port->read_status_mask &= ~UART_LSR_DR;
280
281         spin_unlock_irqrestore(&port->lock, flags);
282 }
283
284 static void siu_enable_ms(struct uart_port *port)
285 {
286         unsigned long flags;
287         uint8_t ier;
288
289         spin_lock_irqsave(&port->lock, flags);
290
291         ier = siu_read(port, UART_IER);
292         ier |= UART_IER_MSI;
293         siu_write(port, UART_IER, ier);
294
295         spin_unlock_irqrestore(&port->lock, flags);
296 }
297
298 static void siu_break_ctl(struct uart_port *port, int ctl)
299 {
300         unsigned long flags;
301         uint8_t lcr;
302
303         spin_lock_irqsave(&port->lock, flags);
304
305         lcr = siu_read(port, UART_LCR);
306         if (ctl == -1)
307                 lcr |= UART_LCR_SBC;
308         else
309                 lcr &= ~UART_LCR_SBC;
310         siu_write(port, UART_LCR, lcr);
311
312         spin_unlock_irqrestore(&port->lock, flags);
313 }
314
315 static inline void receive_chars(struct uart_port *port, uint8_t *status)
316 {
317         uint8_t lsr, ch;
318         char flag;
319         int max_count = RX_MAX_COUNT;
320
321         lsr = *status;
322
323         do {
324                 ch = siu_read(port, UART_RX);
325                 port->icount.rx++;
326                 flag = TTY_NORMAL;
327
328 #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
329                 lsr |= lsr_break_flag[port->line];
330                 lsr_break_flag[port->line] = 0;
331 #endif
332                 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
333                                     UART_LSR_PE | UART_LSR_OE))) {
334                         if (lsr & UART_LSR_BI) {
335                                 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
336                                 port->icount.brk++;
337
338                                 if (uart_handle_break(port))
339                                         goto ignore_char;
340                         }
341
342                         if (lsr & UART_LSR_FE)
343                                 port->icount.frame++;
344                         if (lsr & UART_LSR_PE)
345                                 port->icount.parity++;
346                         if (lsr & UART_LSR_OE)
347                                 port->icount.overrun++;
348
349                         lsr &= port->read_status_mask;
350                         if (lsr & UART_LSR_BI)
351                                 flag = TTY_BREAK;
352                         if (lsr & UART_LSR_FE)
353                                 flag = TTY_FRAME;
354                         if (lsr & UART_LSR_PE)
355                                 flag = TTY_PARITY;
356                 }
357
358                 if (uart_handle_sysrq_char(port, ch))
359                         goto ignore_char;
360
361                 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
362
363         ignore_char:
364                 lsr = siu_read(port, UART_LSR);
365         } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
366
367         tty_flip_buffer_push(&port->state->port);
368
369         *status = lsr;
370 }
371
372 static inline void check_modem_status(struct uart_port *port)
373 {
374         uint8_t msr;
375
376         msr = siu_read(port, UART_MSR);
377         if ((msr & UART_MSR_ANY_DELTA) == 0)
378                 return;
379         if (msr & UART_MSR_DDCD)
380                 uart_handle_dcd_change(port, msr & UART_MSR_DCD);
381         if (msr & UART_MSR_TERI)
382                 port->icount.rng++;
383         if (msr & UART_MSR_DDSR)
384                 port->icount.dsr++;
385         if (msr & UART_MSR_DCTS)
386                 uart_handle_cts_change(port, msr & UART_MSR_CTS);
387
388         wake_up_interruptible(&port->state->port.delta_msr_wait);
389 }
390
391 static inline void transmit_chars(struct uart_port *port)
392 {
393         struct circ_buf *xmit;
394         int max_count = TX_MAX_COUNT;
395
396         xmit = &port->state->xmit;
397
398         if (port->x_char) {
399                 siu_write(port, UART_TX, port->x_char);
400                 port->icount.tx++;
401                 port->x_char = 0;
402                 return;
403         }
404
405         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
406                 siu_stop_tx(port);
407                 return;
408         }
409
410         do {
411                 siu_write(port, UART_TX, xmit->buf[xmit->tail]);
412                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
413                 port->icount.tx++;
414                 if (uart_circ_empty(xmit))
415                         break;
416         } while (max_count-- > 0);
417
418         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
419                 uart_write_wakeup(port);
420
421         if (uart_circ_empty(xmit))
422                 siu_stop_tx(port);
423 }
424
425 static irqreturn_t siu_interrupt(int irq, void *dev_id)
426 {
427         struct uart_port *port;
428         uint8_t iir, lsr;
429
430         port = (struct uart_port *)dev_id;
431
432         iir = siu_read(port, UART_IIR);
433         if (iir & UART_IIR_NO_INT)
434                 return IRQ_NONE;
435
436         lsr = siu_read(port, UART_LSR);
437         if (lsr & UART_LSR_DR)
438                 receive_chars(port, &lsr);
439
440         check_modem_status(port);
441
442         if (lsr & UART_LSR_THRE)
443                 transmit_chars(port);
444
445         return IRQ_HANDLED;
446 }
447
448 static int siu_startup(struct uart_port *port)
449 {
450         int retval;
451
452         if (port->membase == NULL)
453                 return -ENODEV;
454
455         siu_clear_fifo(port);
456
457         (void)siu_read(port, UART_LSR);
458         (void)siu_read(port, UART_RX);
459         (void)siu_read(port, UART_IIR);
460         (void)siu_read(port, UART_MSR);
461
462         if (siu_read(port, UART_LSR) == 0xff)
463                 return -ENODEV;
464
465         retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
466         if (retval)
467                 return retval;
468
469         if (port->type == PORT_VR41XX_DSIU)
470                 vr41xx_enable_dsiuint(DSIUINT_ALL);
471
472         siu_write(port, UART_LCR, UART_LCR_WLEN8);
473
474         spin_lock_irq(&port->lock);
475         siu_set_mctrl(port, port->mctrl);
476         spin_unlock_irq(&port->lock);
477
478         siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
479
480         (void)siu_read(port, UART_LSR);
481         (void)siu_read(port, UART_RX);
482         (void)siu_read(port, UART_IIR);
483         (void)siu_read(port, UART_MSR);
484
485         return 0;
486 }
487
488 static void siu_shutdown(struct uart_port *port)
489 {
490         unsigned long flags;
491         uint8_t lcr;
492
493         siu_write(port, UART_IER, 0);
494
495         spin_lock_irqsave(&port->lock, flags);
496
497         port->mctrl &= ~TIOCM_OUT2;
498         siu_set_mctrl(port, port->mctrl);
499
500         spin_unlock_irqrestore(&port->lock, flags);
501
502         lcr = siu_read(port, UART_LCR);
503         lcr &= ~UART_LCR_SBC;
504         siu_write(port, UART_LCR, lcr);
505
506         siu_clear_fifo(port);
507
508         (void)siu_read(port, UART_RX);
509
510         if (port->type == PORT_VR41XX_DSIU)
511                 vr41xx_disable_dsiuint(DSIUINT_ALL);
512
513         free_irq(port->irq, port);
514 }
515
516 static void siu_set_termios(struct uart_port *port, struct ktermios *new,
517                             struct ktermios *old)
518 {
519         tcflag_t c_cflag, c_iflag;
520         uint8_t lcr, fcr, ier;
521         unsigned int baud, quot;
522         unsigned long flags;
523
524         c_cflag = new->c_cflag;
525         switch (c_cflag & CSIZE) {
526         case CS5:
527                 lcr = UART_LCR_WLEN5;
528                 break;
529         case CS6:
530                 lcr = UART_LCR_WLEN6;
531                 break;
532         case CS7:
533                 lcr = UART_LCR_WLEN7;
534                 break;
535         default:
536                 lcr = UART_LCR_WLEN8;
537                 break;
538         }
539
540         if (c_cflag & CSTOPB)
541                 lcr |= UART_LCR_STOP;
542         if (c_cflag & PARENB)
543                 lcr |= UART_LCR_PARITY;
544         if ((c_cflag & PARODD) != PARODD)
545                 lcr |= UART_LCR_EPAR;
546         if (c_cflag & CMSPAR)
547                 lcr |= UART_LCR_SPAR;
548
549         baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
550         quot = uart_get_divisor(port, baud);
551
552         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
553
554         spin_lock_irqsave(&port->lock, flags);
555
556         uart_update_timeout(port, c_cflag, baud);
557
558         c_iflag = new->c_iflag;
559
560         port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
561         if (c_iflag & INPCK)
562                 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
563         if (c_iflag & (IGNBRK | BRKINT | PARMRK))
564                 port->read_status_mask |= UART_LSR_BI;
565
566         port->ignore_status_mask = 0;
567         if (c_iflag & IGNPAR)
568                 port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
569         if (c_iflag & IGNBRK) {
570                 port->ignore_status_mask |= UART_LSR_BI;
571                 if (c_iflag & IGNPAR)
572                         port->ignore_status_mask |= UART_LSR_OE;
573         }
574
575         if ((c_cflag & CREAD) == 0)
576                 port->ignore_status_mask |= UART_LSR_DR;
577
578         ier = siu_read(port, UART_IER);
579         ier &= ~UART_IER_MSI;
580         if (UART_ENABLE_MS(port, c_cflag))
581                 ier |= UART_IER_MSI;
582         siu_write(port, UART_IER, ier);
583
584         siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
585
586         siu_write(port, UART_DLL, (uint8_t)quot);
587         siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
588
589         siu_write(port, UART_LCR, lcr);
590
591         siu_write(port, UART_FCR, fcr);
592
593         siu_set_mctrl(port, port->mctrl);
594
595         spin_unlock_irqrestore(&port->lock, flags);
596 }
597
598 static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
599 {
600         switch (state) {
601         case 0:
602                 switch (port->type) {
603                 case PORT_VR41XX_SIU:
604                         vr41xx_supply_clock(SIU_CLOCK);
605                         break;
606                 case PORT_VR41XX_DSIU:
607                         vr41xx_supply_clock(DSIU_CLOCK);
608                         break;
609                 }
610                 break;
611         case 3:
612                 switch (port->type) {
613                 case PORT_VR41XX_SIU:
614                         vr41xx_mask_clock(SIU_CLOCK);
615                         break;
616                 case PORT_VR41XX_DSIU:
617                         vr41xx_mask_clock(DSIU_CLOCK);
618                         break;
619                 }
620                 break;
621         }
622 }
623
624 static const char *siu_type(struct uart_port *port)
625 {
626         return siu_type_name(port);
627 }
628
629 static void siu_release_port(struct uart_port *port)
630 {
631         unsigned long size;
632
633         if (port->flags & UPF_IOREMAP) {
634                 iounmap(port->membase);
635                 port->membase = NULL;
636         }
637
638         size = siu_port_size(port);
639         release_mem_region(port->mapbase, size);
640 }
641
642 static int siu_request_port(struct uart_port *port)
643 {
644         unsigned long size;
645         struct resource *res;
646
647         size = siu_port_size(port);
648         res = request_mem_region(port->mapbase, size, siu_type_name(port));
649         if (res == NULL)
650                 return -EBUSY;
651
652         if (port->flags & UPF_IOREMAP) {
653                 port->membase = ioremap(port->mapbase, size);
654                 if (port->membase == NULL) {
655                         release_resource(res);
656                         return -ENOMEM;
657                 }
658         }
659
660         return 0;
661 }
662
663 static void siu_config_port(struct uart_port *port, int flags)
664 {
665         if (flags & UART_CONFIG_TYPE) {
666                 port->type = siu_check_type(port);
667                 (void)siu_request_port(port);
668         }
669 }
670
671 static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
672 {
673         if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
674                 return -EINVAL;
675         if (port->irq != serial->irq)
676                 return -EINVAL;
677         if (port->iotype != serial->io_type)
678                 return -EINVAL;
679         if (port->mapbase != (unsigned long)serial->iomem_base)
680                 return -EINVAL;
681
682         return 0;
683 }
684
685 static const struct uart_ops siu_uart_ops = {
686         .tx_empty       = siu_tx_empty,
687         .set_mctrl      = siu_set_mctrl,
688         .get_mctrl      = siu_get_mctrl,
689         .stop_tx        = siu_stop_tx,
690         .start_tx       = siu_start_tx,
691         .stop_rx        = siu_stop_rx,
692         .enable_ms      = siu_enable_ms,
693         .break_ctl      = siu_break_ctl,
694         .startup        = siu_startup,
695         .shutdown       = siu_shutdown,
696         .set_termios    = siu_set_termios,
697         .pm             = siu_pm,
698         .type           = siu_type,
699         .release_port   = siu_release_port,
700         .request_port   = siu_request_port,
701         .config_port    = siu_config_port,
702         .verify_port    = siu_verify_port,
703 };
704
705 static int siu_init_ports(struct platform_device *pdev)
706 {
707         struct uart_port *port;
708         struct resource *res;
709         int *type = dev_get_platdata(&pdev->dev);
710         int i;
711
712         if (!type)
713                 return 0;
714
715         port = siu_uart_ports;
716         for (i = 0; i < SIU_PORTS_MAX; i++) {
717                 port->type = type[i];
718                 if (port->type == PORT_UNKNOWN)
719                         continue;
720                 port->irq = platform_get_irq(pdev, i);
721                 port->uartclk = SIU_BAUD_BASE * 16;
722                 port->fifosize = 16;
723                 port->regshift = 0;
724                 port->iotype = UPIO_MEM;
725                 port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
726                 port->line = i;
727                 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
728                 port->mapbase = res->start;
729                 port++;
730         }
731
732         return i;
733 }
734
735 #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
736
737 #define BOTH_EMPTY      (UART_LSR_TEMT | UART_LSR_THRE)
738
739 static void wait_for_xmitr(struct uart_port *port)
740 {
741         int timeout = 10000;
742         uint8_t lsr, msr;
743
744         do {
745                 lsr = siu_read(port, UART_LSR);
746                 if (lsr & UART_LSR_BI)
747                         lsr_break_flag[port->line] = UART_LSR_BI;
748
749                 if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
750                         break;
751         } while (timeout-- > 0);
752
753         if (port->flags & UPF_CONS_FLOW) {
754                 timeout = 1000000;
755
756                 do {
757                         msr = siu_read(port, UART_MSR);
758                         if ((msr & UART_MSR_CTS) != 0)
759                                 break;
760                 } while (timeout-- > 0);
761         }
762 }
763
764 static void siu_console_putchar(struct uart_port *port, int ch)
765 {
766         wait_for_xmitr(port);
767         siu_write(port, UART_TX, ch);
768 }
769
770 static void siu_console_write(struct console *con, const char *s, unsigned count)
771 {
772         struct uart_port *port;
773         uint8_t ier;
774
775         port = &siu_uart_ports[con->index];
776
777         ier = siu_read(port, UART_IER);
778         siu_write(port, UART_IER, 0);
779
780         uart_console_write(port, s, count, siu_console_putchar);
781
782         wait_for_xmitr(port);
783         siu_write(port, UART_IER, ier);
784 }
785
786 static int __init siu_console_setup(struct console *con, char *options)
787 {
788         struct uart_port *port;
789         int baud = 9600;
790         int parity = 'n';
791         int bits = 8;
792         int flow = 'n';
793
794         if (con->index >= SIU_PORTS_MAX)
795                 con->index = 0;
796
797         port = &siu_uart_ports[con->index];
798         if (port->membase == NULL) {
799                 if (port->mapbase == 0)
800                         return -ENODEV;
801                 port->membase = ioremap(port->mapbase, siu_port_size(port));
802         }
803
804         if (port->type == PORT_VR41XX_SIU)
805                 vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
806
807         if (options != NULL)
808                 uart_parse_options(options, &baud, &parity, &bits, &flow);
809
810         return uart_set_options(port, con, baud, parity, bits, flow);
811 }
812
813 static struct uart_driver siu_uart_driver;
814
815 static struct console siu_console = {
816         .name   = "ttyVR",
817         .write  = siu_console_write,
818         .device = uart_console_device,
819         .setup  = siu_console_setup,
820         .flags  = CON_PRINTBUFFER,
821         .index  = -1,
822         .data   = &siu_uart_driver,
823 };
824
825 static int siu_console_init(void)
826 {
827         struct uart_port *port;
828         int i;
829
830         for (i = 0; i < SIU_PORTS_MAX; i++) {
831                 port = &siu_uart_ports[i];
832                 port->ops = &siu_uart_ops;
833         }
834
835         register_console(&siu_console);
836
837         return 0;
838 }
839
840 console_initcall(siu_console_init);
841
842 void __init vr41xx_siu_early_setup(struct uart_port *port)
843 {
844         if (port->type == PORT_UNKNOWN)
845                 return;
846
847         siu_uart_ports[port->line].line = port->line;
848         siu_uart_ports[port->line].type = port->type;
849         siu_uart_ports[port->line].uartclk = SIU_BAUD_BASE * 16;
850         siu_uart_ports[port->line].mapbase = port->mapbase;
851         siu_uart_ports[port->line].ops = &siu_uart_ops;
852 }
853
854 #define SERIAL_VR41XX_CONSOLE   &siu_console
855 #else
856 #define SERIAL_VR41XX_CONSOLE   NULL
857 #endif
858
859 static struct uart_driver siu_uart_driver = {
860         .owner          = THIS_MODULE,
861         .driver_name    = "SIU",
862         .dev_name       = "ttyVR",
863         .major          = SIU_MAJOR,
864         .minor          = SIU_MINOR_BASE,
865         .cons           = SERIAL_VR41XX_CONSOLE,
866 };
867
868 static int siu_probe(struct platform_device *dev)
869 {
870         struct uart_port *port;
871         int num, i, retval;
872
873         num = siu_init_ports(dev);
874         if (num <= 0)
875                 return -ENODEV;
876
877         siu_uart_driver.nr = num;
878         retval = uart_register_driver(&siu_uart_driver);
879         if (retval)
880                 return retval;
881
882         for (i = 0; i < num; i++) {
883                 port = &siu_uart_ports[i];
884                 port->ops = &siu_uart_ops;
885                 port->dev = &dev->dev;
886
887                 retval = uart_add_one_port(&siu_uart_driver, port);
888                 if (retval < 0) {
889                         port->dev = NULL;
890                         break;
891                 }
892         }
893
894         if (i == 0 && retval < 0) {
895                 uart_unregister_driver(&siu_uart_driver);
896                 return retval;
897         }
898
899         return 0;
900 }
901
902 static int siu_remove(struct platform_device *dev)
903 {
904         struct uart_port *port;
905         int i;
906
907         for (i = 0; i < siu_uart_driver.nr; i++) {
908                 port = &siu_uart_ports[i];
909                 if (port->dev == &dev->dev) {
910                         uart_remove_one_port(&siu_uart_driver, port);
911                         port->dev = NULL;
912                 }
913         }
914
915         uart_unregister_driver(&siu_uart_driver);
916
917         return 0;
918 }
919
920 static int siu_suspend(struct platform_device *dev, pm_message_t state)
921 {
922         struct uart_port *port;
923         int i;
924
925         for (i = 0; i < siu_uart_driver.nr; i++) {
926                 port = &siu_uart_ports[i];
927                 if ((port->type == PORT_VR41XX_SIU ||
928                      port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
929                         uart_suspend_port(&siu_uart_driver, port);
930
931         }
932
933         return 0;
934 }
935
936 static int siu_resume(struct platform_device *dev)
937 {
938         struct uart_port *port;
939         int i;
940
941         for (i = 0; i < siu_uart_driver.nr; i++) {
942                 port = &siu_uart_ports[i];
943                 if ((port->type == PORT_VR41XX_SIU ||
944                      port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
945                         uart_resume_port(&siu_uart_driver, port);
946         }
947
948         return 0;
949 }
950
951 static struct platform_driver siu_device_driver = {
952         .probe          = siu_probe,
953         .remove         = siu_remove,
954         .suspend        = siu_suspend,
955         .resume         = siu_resume,
956         .driver         = {
957                 .name   = "SIU",
958         },
959 };
960
961 module_platform_driver(siu_device_driver);
962
963 MODULE_LICENSE("GPL");
964 MODULE_ALIAS("platform:SIU");