tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-block.git] / drivers / tty / serial / pnx8xxx_uart.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * UART driver for PNX8XXX SoCs
4  *
5  * Author: Per Hallsmark per.hallsmark@mvista.com
6  * Ported to 2.6 kernel by EmbeddedAlley
7  * Reworked by Vitaly Wool <vitalywool@gmail.com>
8  *
9  * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
10  * Copyright (C) 2000 Deep Blue Solutions Ltd.
11  *
12  * This file is licensed under the terms of the GNU General Public License
13  * version 2. This program is licensed "as is" without any warranty of
14  * any kind, whether express or implied.
15  *
16  */
17
18 #if defined(CONFIG_SERIAL_PNX8XXX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19 #define SUPPORT_SYSRQ
20 #endif
21
22 #include <linux/module.h>
23 #include <linux/ioport.h>
24 #include <linux/init.h>
25 #include <linux/console.h>
26 #include <linux/sysrq.h>
27 #include <linux/device.h>
28 #include <linux/platform_device.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial_core.h>
32 #include <linux/serial.h>
33 #include <linux/serial_pnx8xxx.h>
34
35 #include <asm/io.h>
36 #include <asm/irq.h>
37
38 /* We'll be using StrongARM sa1100 serial port major/minor */
39 #define SERIAL_PNX8XXX_MAJOR    204
40 #define MINOR_START             5
41
42 #define NR_PORTS                2
43
44 #define PNX8XXX_ISR_PASS_LIMIT  256
45
46 /*
47  * Convert from ignore_status_mask or read_status_mask to FIFO
48  * and interrupt status bits
49  */
50 #define SM_TO_FIFO(x)   ((x) >> 10)
51 #define SM_TO_ISTAT(x)  ((x) & 0x000001ff)
52 #define FIFO_TO_SM(x)   ((x) << 10)
53 #define ISTAT_TO_SM(x)  ((x) & 0x000001ff)
54
55 /*
56  * This is the size of our serial port register set.
57  */
58 #define UART_PORT_SIZE  0x1000
59
60 /*
61  * This determines how often we check the modem status signals
62  * for any change.  They generally aren't connected to an IRQ
63  * so we have to poll them.  We also check immediately before
64  * filling the TX fifo incase CTS has been dropped.
65  */
66 #define MCTRL_TIMEOUT   (250*HZ/1000)
67
68 extern struct pnx8xxx_port pnx8xxx_ports[];
69
70 static inline int serial_in(struct pnx8xxx_port *sport, int offset)
71 {
72         return (__raw_readl(sport->port.membase + offset));
73 }
74
75 static inline void serial_out(struct pnx8xxx_port *sport, int offset, int value)
76 {
77         __raw_writel(value, sport->port.membase + offset);
78 }
79
80 /*
81  * Handle any change of modem status signal since we were last called.
82  */
83 static void pnx8xxx_mctrl_check(struct pnx8xxx_port *sport)
84 {
85         unsigned int status, changed;
86
87         status = sport->port.ops->get_mctrl(&sport->port);
88         changed = status ^ sport->old_status;
89
90         if (changed == 0)
91                 return;
92
93         sport->old_status = status;
94
95         if (changed & TIOCM_RI)
96                 sport->port.icount.rng++;
97         if (changed & TIOCM_DSR)
98                 sport->port.icount.dsr++;
99         if (changed & TIOCM_CAR)
100                 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
101         if (changed & TIOCM_CTS)
102                 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
103
104         wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
105 }
106
107 /*
108  * This is our per-port timeout handler, for checking the
109  * modem status signals.
110  */
111 static void pnx8xxx_timeout(unsigned long data)
112 {
113         struct pnx8xxx_port *sport = (struct pnx8xxx_port *)data;
114         unsigned long flags;
115
116         if (sport->port.state) {
117                 spin_lock_irqsave(&sport->port.lock, flags);
118                 pnx8xxx_mctrl_check(sport);
119                 spin_unlock_irqrestore(&sport->port.lock, flags);
120
121                 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
122         }
123 }
124
125 /*
126  * interrupts disabled on entry
127  */
128 static void pnx8xxx_stop_tx(struct uart_port *port)
129 {
130         struct pnx8xxx_port *sport =
131                 container_of(port, struct pnx8xxx_port, port);
132         u32 ien;
133
134         /* Disable TX intr */
135         ien = serial_in(sport, PNX8XXX_IEN);
136         serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLTX);
137
138         /* Clear all pending TX intr */
139         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
140 }
141
142 /*
143  * interrupts may not be disabled on entry
144  */
145 static void pnx8xxx_start_tx(struct uart_port *port)
146 {
147         struct pnx8xxx_port *sport =
148                 container_of(port, struct pnx8xxx_port, port);
149         u32 ien;
150
151         /* Clear all pending TX intr */
152         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
153
154         /* Enable TX intr */
155         ien = serial_in(sport, PNX8XXX_IEN);
156         serial_out(sport, PNX8XXX_IEN, ien | PNX8XXX_UART_INT_ALLTX);
157 }
158
159 /*
160  * Interrupts enabled
161  */
162 static void pnx8xxx_stop_rx(struct uart_port *port)
163 {
164         struct pnx8xxx_port *sport =
165                 container_of(port, struct pnx8xxx_port, port);
166         u32 ien;
167
168         /* Disable RX intr */
169         ien = serial_in(sport, PNX8XXX_IEN);
170         serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLRX);
171
172         /* Clear all pending RX intr */
173         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX);
174 }
175
176 /*
177  * Set the modem control timer to fire immediately.
178  */
179 static void pnx8xxx_enable_ms(struct uart_port *port)
180 {
181         struct pnx8xxx_port *sport =
182                 container_of(port, struct pnx8xxx_port, port);
183
184         mod_timer(&sport->timer, jiffies);
185 }
186
187 static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
188 {
189         unsigned int status, ch, flg;
190
191         status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
192                  ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
193         while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
194                 ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
195
196                 sport->port.icount.rx++;
197
198                 flg = TTY_NORMAL;
199
200                 /*
201                  * note that the error handling code is
202                  * out of the main execution path
203                  */
204                 if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
205                                         PNX8XXX_UART_FIFO_RXPAR |
206                                         PNX8XXX_UART_FIFO_RXBRK) |
207                               ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
208                         if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
209                                 status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
210                                         FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
211                                 sport->port.icount.brk++;
212                                 if (uart_handle_break(&sport->port))
213                                         goto ignore_char;
214                         } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
215                                 sport->port.icount.parity++;
216                         else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
217                                 sport->port.icount.frame++;
218                         if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
219                                 sport->port.icount.overrun++;
220
221                         status &= sport->port.read_status_mask;
222
223                         if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
224                                 flg = TTY_PARITY;
225                         else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
226                                 flg = TTY_FRAME;
227
228 #ifdef SUPPORT_SYSRQ
229                         sport->port.sysrq = 0;
230 #endif
231                 }
232
233                 if (uart_handle_sysrq_char(&sport->port, ch))
234                         goto ignore_char;
235
236                 uart_insert_char(&sport->port, status,
237                                 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
238
239         ignore_char:
240                 serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
241                                 PNX8XXX_UART_LCR_RX_NEXT);
242                 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
243                          ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
244         }
245
246         spin_unlock(&sport->port.lock);
247         tty_flip_buffer_push(&sport->port.state->port);
248         spin_lock(&sport->port.lock);
249 }
250
251 static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
252 {
253         struct circ_buf *xmit = &sport->port.state->xmit;
254
255         if (sport->port.x_char) {
256                 serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
257                 sport->port.icount.tx++;
258                 sport->port.x_char = 0;
259                 return;
260         }
261
262         /*
263          * Check the modem control lines before
264          * transmitting anything.
265          */
266         pnx8xxx_mctrl_check(sport);
267
268         if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
269                 pnx8xxx_stop_tx(&sport->port);
270                 return;
271         }
272
273         /*
274          * TX while bytes available
275          */
276         while (((serial_in(sport, PNX8XXX_FIFO) &
277                                         PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
278                 serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
279                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
280                 sport->port.icount.tx++;
281                 if (uart_circ_empty(xmit))
282                         break;
283         }
284
285         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
286                 uart_write_wakeup(&sport->port);
287
288         if (uart_circ_empty(xmit))
289                 pnx8xxx_stop_tx(&sport->port);
290 }
291
292 static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
293 {
294         struct pnx8xxx_port *sport = dev_id;
295         unsigned int status;
296
297         spin_lock(&sport->port.lock);
298         /* Get the interrupts */
299         status  = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
300
301         /* Byte or break signal received */
302         if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
303                 pnx8xxx_rx_chars(sport);
304
305         /* TX holding register empty - transmit a byte */
306         if (status & PNX8XXX_UART_INT_TX)
307                 pnx8xxx_tx_chars(sport);
308
309         /* Clear the ISTAT register */
310         serial_out(sport, PNX8XXX_ICLR, status);
311
312         spin_unlock(&sport->port.lock);
313         return IRQ_HANDLED;
314 }
315
316 /*
317  * Return TIOCSER_TEMT when transmitter is not busy.
318  */
319 static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
320 {
321         struct pnx8xxx_port *sport =
322                 container_of(port, struct pnx8xxx_port, port);
323
324         return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
325 }
326
327 static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
328 {
329         struct pnx8xxx_port *sport =
330                 container_of(port, struct pnx8xxx_port, port);
331         unsigned int mctrl = TIOCM_DSR;
332         unsigned int msr;
333
334         /* REVISIT */
335
336         msr = serial_in(sport, PNX8XXX_MCR);
337
338         mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
339         mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
340
341         return mctrl;
342 }
343
344 static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
345 {
346 #if     0       /* FIXME */
347         struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
348         unsigned int msr;
349 #endif
350 }
351
352 /*
353  * Interrupts always disabled.
354  */
355 static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
356 {
357         struct pnx8xxx_port *sport =
358                 container_of(port, struct pnx8xxx_port, port);
359         unsigned long flags;
360         unsigned int lcr;
361
362         spin_lock_irqsave(&sport->port.lock, flags);
363         lcr = serial_in(sport, PNX8XXX_LCR);
364         if (break_state == -1)
365                 lcr |= PNX8XXX_UART_LCR_TXBREAK;
366         else
367                 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
368         serial_out(sport, PNX8XXX_LCR, lcr);
369         spin_unlock_irqrestore(&sport->port.lock, flags);
370 }
371
372 static int pnx8xxx_startup(struct uart_port *port)
373 {
374         struct pnx8xxx_port *sport =
375                 container_of(port, struct pnx8xxx_port, port);
376         int retval;
377
378         /*
379          * Allocate the IRQ
380          */
381         retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
382                              "pnx8xxx-uart", sport);
383         if (retval)
384                 return retval;
385
386         /*
387          * Finally, clear and enable interrupts
388          */
389
390         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
391                              PNX8XXX_UART_INT_ALLTX);
392
393         serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
394                             PNX8XXX_UART_INT_ALLRX |
395                             PNX8XXX_UART_INT_ALLTX);
396
397         /*
398          * Enable modem status interrupts
399          */
400         spin_lock_irq(&sport->port.lock);
401         pnx8xxx_enable_ms(&sport->port);
402         spin_unlock_irq(&sport->port.lock);
403
404         return 0;
405 }
406
407 static void pnx8xxx_shutdown(struct uart_port *port)
408 {
409         struct pnx8xxx_port *sport =
410                 container_of(port, struct pnx8xxx_port, port);
411         int lcr;
412
413         /*
414          * Stop our timer.
415          */
416         del_timer_sync(&sport->timer);
417
418         /*
419          * Disable all interrupts
420          */
421         serial_out(sport, PNX8XXX_IEN, 0);
422
423         /*
424          * Reset the Tx and Rx FIFOS, disable the break condition
425          */
426         lcr = serial_in(sport, PNX8XXX_LCR);
427         lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
428         lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
429         serial_out(sport, PNX8XXX_LCR, lcr);
430
431         /*
432          * Clear all interrupts
433          */
434         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
435                              PNX8XXX_UART_INT_ALLTX);
436
437         /*
438          * Free the interrupt
439          */
440         free_irq(sport->port.irq, sport);
441 }
442
443 static void
444 pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
445                    struct ktermios *old)
446 {
447         struct pnx8xxx_port *sport =
448                 container_of(port, struct pnx8xxx_port, port);
449         unsigned long flags;
450         unsigned int lcr_fcr, old_ien, baud, quot;
451         unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
452
453         /*
454          * We only support CS7 and CS8.
455          */
456         while ((termios->c_cflag & CSIZE) != CS7 &&
457                (termios->c_cflag & CSIZE) != CS8) {
458                 termios->c_cflag &= ~CSIZE;
459                 termios->c_cflag |= old_csize;
460                 old_csize = CS8;
461         }
462
463         if ((termios->c_cflag & CSIZE) == CS8)
464                 lcr_fcr = PNX8XXX_UART_LCR_8BIT;
465         else
466                 lcr_fcr = 0;
467
468         if (termios->c_cflag & CSTOPB)
469                 lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
470         if (termios->c_cflag & PARENB) {
471                 lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
472                 if (!(termios->c_cflag & PARODD))
473                         lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
474         }
475
476         /*
477          * Ask the core to calculate the divisor for us.
478          */
479         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
480         quot = uart_get_divisor(port, baud);
481
482         spin_lock_irqsave(&sport->port.lock, flags);
483
484         sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
485                                 ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
486                                 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
487         if (termios->c_iflag & INPCK)
488                 sport->port.read_status_mask |=
489                         FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
490                         FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
491         if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
492                 sport->port.read_status_mask |=
493                         ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
494
495         /*
496          * Characters to ignore
497          */
498         sport->port.ignore_status_mask = 0;
499         if (termios->c_iflag & IGNPAR)
500                 sport->port.ignore_status_mask |=
501                         FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
502                         FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
503         if (termios->c_iflag & IGNBRK) {
504                 sport->port.ignore_status_mask |=
505                         ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
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                         sport->port.ignore_status_mask |=
512                                 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
513         }
514
515         /*
516          * ignore all characters if CREAD is not set
517          */
518         if ((termios->c_cflag & CREAD) == 0)
519                 sport->port.ignore_status_mask |=
520                         ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
521
522         del_timer_sync(&sport->timer);
523
524         /*
525          * Update the per-port timeout.
526          */
527         uart_update_timeout(port, termios->c_cflag, baud);
528
529         /*
530          * disable interrupts and drain transmitter
531          */
532         old_ien = serial_in(sport, PNX8XXX_IEN);
533         serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
534                                         PNX8XXX_UART_INT_ALLRX));
535
536         while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
537                 barrier();
538
539         /* then, disable everything */
540         serial_out(sport, PNX8XXX_IEN, 0);
541
542         /* Reset the Rx and Tx FIFOs too */
543         lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
544         lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
545
546         /* set the parity, stop bits and data size */
547         serial_out(sport, PNX8XXX_LCR, lcr_fcr);
548
549         /* set the baud rate */
550         quot -= 1;
551         serial_out(sport, PNX8XXX_BAUD, quot);
552
553         serial_out(sport, PNX8XXX_ICLR, -1);
554
555         serial_out(sport, PNX8XXX_IEN, old_ien);
556
557         if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
558                 pnx8xxx_enable_ms(&sport->port);
559
560         spin_unlock_irqrestore(&sport->port.lock, flags);
561 }
562
563 static const char *pnx8xxx_type(struct uart_port *port)
564 {
565         struct pnx8xxx_port *sport =
566                 container_of(port, struct pnx8xxx_port, port);
567
568         return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
569 }
570
571 /*
572  * Release the memory region(s) being used by 'port'.
573  */
574 static void pnx8xxx_release_port(struct uart_port *port)
575 {
576         struct pnx8xxx_port *sport =
577                 container_of(port, struct pnx8xxx_port, port);
578
579         release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
580 }
581
582 /*
583  * Request the memory region(s) being used by 'port'.
584  */
585 static int pnx8xxx_request_port(struct uart_port *port)
586 {
587         struct pnx8xxx_port *sport =
588                 container_of(port, struct pnx8xxx_port, port);
589         return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
590                         "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
591 }
592
593 /*
594  * Configure/autoconfigure the port.
595  */
596 static void pnx8xxx_config_port(struct uart_port *port, int flags)
597 {
598         struct pnx8xxx_port *sport =
599                 container_of(port, struct pnx8xxx_port, port);
600
601         if (flags & UART_CONFIG_TYPE &&
602             pnx8xxx_request_port(&sport->port) == 0)
603                 sport->port.type = PORT_PNX8XXX;
604 }
605
606 /*
607  * Verify the new serial_struct (for TIOCSSERIAL).
608  * The only change we allow are to the flags and type, and
609  * even then only between PORT_PNX8XXX and PORT_UNKNOWN
610  */
611 static int
612 pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
613 {
614         struct pnx8xxx_port *sport =
615                 container_of(port, struct pnx8xxx_port, port);
616         int ret = 0;
617
618         if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
619                 ret = -EINVAL;
620         if (sport->port.irq != ser->irq)
621                 ret = -EINVAL;
622         if (ser->io_type != SERIAL_IO_MEM)
623                 ret = -EINVAL;
624         if (sport->port.uartclk / 16 != ser->baud_base)
625                 ret = -EINVAL;
626         if ((void *)sport->port.mapbase != ser->iomem_base)
627                 ret = -EINVAL;
628         if (sport->port.iobase != ser->port)
629                 ret = -EINVAL;
630         if (ser->hub6 != 0)
631                 ret = -EINVAL;
632         return ret;
633 }
634
635 static const struct uart_ops pnx8xxx_pops = {
636         .tx_empty       = pnx8xxx_tx_empty,
637         .set_mctrl      = pnx8xxx_set_mctrl,
638         .get_mctrl      = pnx8xxx_get_mctrl,
639         .stop_tx        = pnx8xxx_stop_tx,
640         .start_tx       = pnx8xxx_start_tx,
641         .stop_rx        = pnx8xxx_stop_rx,
642         .enable_ms      = pnx8xxx_enable_ms,
643         .break_ctl      = pnx8xxx_break_ctl,
644         .startup        = pnx8xxx_startup,
645         .shutdown       = pnx8xxx_shutdown,
646         .set_termios    = pnx8xxx_set_termios,
647         .type           = pnx8xxx_type,
648         .release_port   = pnx8xxx_release_port,
649         .request_port   = pnx8xxx_request_port,
650         .config_port    = pnx8xxx_config_port,
651         .verify_port    = pnx8xxx_verify_port,
652 };
653
654
655 /*
656  * Setup the PNX8XXX serial ports.
657  *
658  * Note also that we support "console=ttySx" where "x" is either 0 or 1.
659  */
660 static void __init pnx8xxx_init_ports(void)
661 {
662         static int first = 1;
663         int i;
664
665         if (!first)
666                 return;
667         first = 0;
668
669         for (i = 0; i < NR_PORTS; i++) {
670                 setup_timer(&pnx8xxx_ports[i].timer, pnx8xxx_timeout,
671                             (unsigned long)&pnx8xxx_ports[i]);
672                 pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
673         }
674 }
675
676 #ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
677
678 static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
679 {
680         struct pnx8xxx_port *sport =
681                 container_of(port, struct pnx8xxx_port, port);
682         int status;
683
684         do {
685                 /* Wait for UART_TX register to empty */
686                 status = serial_in(sport, PNX8XXX_FIFO);
687         } while (status & PNX8XXX_UART_FIFO_TXFIFO);
688         serial_out(sport, PNX8XXX_FIFO, ch);
689 }
690
691 /*
692  * Interrupts are disabled on entering
693  */static void
694 pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
695 {
696         struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
697         unsigned int old_ien, status;
698
699         /*
700          *      First, save IEN and then disable interrupts
701          */
702         old_ien = serial_in(sport, PNX8XXX_IEN);
703         serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
704                                         PNX8XXX_UART_INT_ALLRX));
705
706         uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
707
708         /*
709          *      Finally, wait for transmitter to become empty
710          *      and restore IEN
711          */
712         do {
713                 /* Wait for UART_TX register to empty */
714                 status = serial_in(sport, PNX8XXX_FIFO);
715         } while (status & PNX8XXX_UART_FIFO_TXFIFO);
716
717         /* Clear TX and EMPTY interrupt */
718         serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
719                              PNX8XXX_UART_INT_EMPTY);
720
721         serial_out(sport, PNX8XXX_IEN, old_ien);
722 }
723
724 static int __init
725 pnx8xxx_console_setup(struct console *co, char *options)
726 {
727         struct pnx8xxx_port *sport;
728         int baud = 38400;
729         int bits = 8;
730         int parity = 'n';
731         int flow = 'n';
732
733         /*
734          * Check whether an invalid uart number has been specified, and
735          * if so, search for the first available port that does have
736          * console support.
737          */
738         if (co->index == -1 || co->index >= NR_PORTS)
739                 co->index = 0;
740         sport = &pnx8xxx_ports[co->index];
741
742         if (options)
743                 uart_parse_options(options, &baud, &parity, &bits, &flow);
744
745         return uart_set_options(&sport->port, co, baud, parity, bits, flow);
746 }
747
748 static struct uart_driver pnx8xxx_reg;
749 static struct console pnx8xxx_console = {
750         .name           = "ttyS",
751         .write          = pnx8xxx_console_write,
752         .device         = uart_console_device,
753         .setup          = pnx8xxx_console_setup,
754         .flags          = CON_PRINTBUFFER,
755         .index          = -1,
756         .data           = &pnx8xxx_reg,
757 };
758
759 static int __init pnx8xxx_rs_console_init(void)
760 {
761         pnx8xxx_init_ports();
762         register_console(&pnx8xxx_console);
763         return 0;
764 }
765 console_initcall(pnx8xxx_rs_console_init);
766
767 #define PNX8XXX_CONSOLE &pnx8xxx_console
768 #else
769 #define PNX8XXX_CONSOLE NULL
770 #endif
771
772 static struct uart_driver pnx8xxx_reg = {
773         .owner                  = THIS_MODULE,
774         .driver_name            = "ttyS",
775         .dev_name               = "ttyS",
776         .major                  = SERIAL_PNX8XXX_MAJOR,
777         .minor                  = MINOR_START,
778         .nr                     = NR_PORTS,
779         .cons                   = PNX8XXX_CONSOLE,
780 };
781
782 static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
783 {
784         struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
785
786         return uart_suspend_port(&pnx8xxx_reg, &sport->port);
787 }
788
789 static int pnx8xxx_serial_resume(struct platform_device *pdev)
790 {
791         struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
792
793         return uart_resume_port(&pnx8xxx_reg, &sport->port);
794 }
795
796 static int pnx8xxx_serial_probe(struct platform_device *pdev)
797 {
798         struct resource *res = pdev->resource;
799         int i;
800
801         for (i = 0; i < pdev->num_resources; i++, res++) {
802                 if (!(res->flags & IORESOURCE_MEM))
803                         continue;
804
805                 for (i = 0; i < NR_PORTS; i++) {
806                         if (pnx8xxx_ports[i].port.mapbase != res->start)
807                                 continue;
808
809                         pnx8xxx_ports[i].port.dev = &pdev->dev;
810                         uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
811                         platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
812                         break;
813                 }
814         }
815
816         return 0;
817 }
818
819 static int pnx8xxx_serial_remove(struct platform_device *pdev)
820 {
821         struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
822
823         if (sport)
824                 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
825
826         return 0;
827 }
828
829 static struct platform_driver pnx8xxx_serial_driver = {
830         .driver         = {
831                 .name   = "pnx8xxx-uart",
832         },
833         .probe          = pnx8xxx_serial_probe,
834         .remove         = pnx8xxx_serial_remove,
835         .suspend        = pnx8xxx_serial_suspend,
836         .resume         = pnx8xxx_serial_resume,
837 };
838
839 static int __init pnx8xxx_serial_init(void)
840 {
841         int ret;
842
843         printk(KERN_INFO "Serial: PNX8XXX driver\n");
844
845         pnx8xxx_init_ports();
846
847         ret = uart_register_driver(&pnx8xxx_reg);
848         if (ret == 0) {
849                 ret = platform_driver_register(&pnx8xxx_serial_driver);
850                 if (ret)
851                         uart_unregister_driver(&pnx8xxx_reg);
852         }
853         return ret;
854 }
855
856 static void __exit pnx8xxx_serial_exit(void)
857 {
858         platform_driver_unregister(&pnx8xxx_serial_driver);
859         uart_unregister_driver(&pnx8xxx_reg);
860 }
861
862 module_init(pnx8xxx_serial_init);
863 module_exit(pnx8xxx_serial_exit);
864
865 MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
866 MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
867 MODULE_LICENSE("GPL");
868 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
869 MODULE_ALIAS("platform:pnx8xxx-uart");