tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-2.6-block.git] / drivers / tty / serial / tilegx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2013 Tilera Corporation. All Rights Reserved.
4  *
5  *   This program is free software; you can redistribute it and/or
6  *   modify it under the terms of the GNU General Public License
7  *   as published by the Free Software Foundation, version 2.
8  *
9  *   This program is distributed in the hope that it will be useful, but
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  *   NON INFRINGEMENT.  See the GNU General Public License for
13  *   more details.
14  *
15  * TILEGx UART driver.
16  */
17
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/irq.h>
23 #include <linux/module.h>
24 #include <linux/serial_core.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27
28 #include <gxio/common.h>
29 #include <gxio/iorpc_globals.h>
30 #include <gxio/iorpc_uart.h>
31 #include <gxio/kiorpc.h>
32
33 #include <hv/drv_uart_intf.h>
34
35 /*
36  * Use device name ttyS, major 4, minor 64-65.
37  * This is the usual serial port name, 8250 conventional range.
38  */
39 #define TILEGX_UART_MAJOR       TTY_MAJOR
40 #define TILEGX_UART_MINOR       64
41 #define TILEGX_UART_NAME        "ttyS"
42 #define DRIVER_NAME_STRING      "TILEGx_Serial"
43 #define TILEGX_UART_REF_CLK     125000000; /* REF_CLK is always 125 MHz. */
44
45 struct tile_uart_port {
46         /* UART port. */
47         struct uart_port        uart;
48
49         /* GXIO device context. */
50         gxio_uart_context_t     context;
51
52         /* UART access mutex. */
53         struct mutex            mutex;
54
55         /* CPU receiving interrupts. */
56         int                     irq_cpu;
57 };
58
59 static struct tile_uart_port tile_uart_ports[TILEGX_UART_NR];
60 static struct uart_driver tilegx_uart_driver;
61
62
63 /*
64  * Read UART rx fifo, and insert the chars into tty buffer.
65  */
66 static void receive_chars(struct tile_uart_port *tile_uart,
67                           struct tty_struct *tty)
68 {
69         int i;
70         char c;
71         UART_FIFO_COUNT_t count;
72         gxio_uart_context_t *context = &tile_uart->context;
73         struct tty_port *port = tty->port;
74
75         count.word = gxio_uart_read(context, UART_FIFO_COUNT);
76         for (i = 0; i < count.rfifo_count; i++) {
77                 c = (char)gxio_uart_read(context, UART_RECEIVE_DATA);
78                 tty_insert_flip_char(port, c, TTY_NORMAL);
79         }
80 }
81
82
83 /*
84  * Drain the Rx FIFO, called by interrupt handler.
85  */
86 static void handle_receive(struct tile_uart_port *tile_uart)
87 {
88         struct tty_port *port = &tile_uart->uart.state->port;
89         struct tty_struct *tty = tty_port_tty_get(port);
90         gxio_uart_context_t *context = &tile_uart->context;
91
92         if (!tty)
93                 return;
94
95         /* First read UART rx fifo. */
96         receive_chars(tile_uart, tty);
97
98         /* Reset RFIFO_WE interrupt. */
99         gxio_uart_write(context, UART_INTERRUPT_STATUS,
100                         UART_INTERRUPT_MASK__RFIFO_WE_MASK);
101
102         /* Final read, if any chars comes between the first read and
103          * the interrupt reset.
104          */
105         receive_chars(tile_uart, tty);
106
107         spin_unlock(&tile_uart->uart.lock);
108         tty_flip_buffer_push(port);
109         spin_lock(&tile_uart->uart.lock);
110         tty_kref_put(tty);
111 }
112
113
114 /*
115  * Push one char to UART Write FIFO.
116  * Return 0 on success, -1 if write filo is full.
117  */
118 static int tilegx_putchar(gxio_uart_context_t *context, char c)
119 {
120         UART_FLAG_t flag;
121         flag.word = gxio_uart_read(context, UART_FLAG);
122         if (flag.wfifo_full)
123                 return -1;
124
125         gxio_uart_write(context, UART_TRANSMIT_DATA, (unsigned long)c);
126         return 0;
127 }
128
129
130 /*
131  * Send chars to UART Write FIFO; called by interrupt handler.
132  */
133 static void handle_transmit(struct tile_uart_port *tile_uart)
134 {
135         unsigned char ch;
136         struct uart_port *port;
137         struct circ_buf *xmit;
138         gxio_uart_context_t *context = &tile_uart->context;
139
140         /* First reset WFIFO_RE interrupt. */
141         gxio_uart_write(context, UART_INTERRUPT_STATUS,
142                         UART_INTERRUPT_MASK__WFIFO_RE_MASK);
143
144         port = &tile_uart->uart;
145         xmit = &port->state->xmit;
146         if (port->x_char) {
147                 if (tilegx_putchar(context, port->x_char))
148                         return;
149                 port->x_char = 0;
150                 port->icount.tx++;
151         }
152
153         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
154                 return;
155
156         while (!uart_circ_empty(xmit)) {
157                 ch = xmit->buf[xmit->tail];
158                 if (tilegx_putchar(context, ch))
159                         break;
160                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
161                 port->icount.tx++;
162         }
163
164         /* Reset WFIFO_RE interrupt. */
165         gxio_uart_write(context, UART_INTERRUPT_STATUS,
166                         UART_INTERRUPT_MASK__WFIFO_RE_MASK);
167
168         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
169                 uart_write_wakeup(port);
170 }
171
172
173 /*
174  * UART Interrupt handler.
175  */
176 static irqreturn_t tilegx_interrupt(int irq, void *dev_id)
177 {
178         unsigned long flags;
179         UART_INTERRUPT_STATUS_t intr_stat;
180         struct tile_uart_port *tile_uart;
181         gxio_uart_context_t *context;
182         struct uart_port *port = dev_id;
183         irqreturn_t ret = IRQ_NONE;
184
185         spin_lock_irqsave(&port->lock, flags);
186
187         tile_uart = container_of(port, struct tile_uart_port, uart);
188         context = &tile_uart->context;
189         intr_stat.word = gxio_uart_read(context, UART_INTERRUPT_STATUS);
190
191         if (intr_stat.rfifo_we) {
192                 handle_receive(tile_uart);
193                 ret = IRQ_HANDLED;
194         }
195         if (intr_stat.wfifo_re) {
196                 handle_transmit(tile_uart);
197                 ret = IRQ_HANDLED;
198         }
199
200         spin_unlock_irqrestore(&port->lock, flags);
201         return ret;
202 }
203
204
205 /*
206  * Return TIOCSER_TEMT when transmitter FIFO is empty.
207  */
208 static u_int tilegx_tx_empty(struct uart_port *port)
209 {
210         int ret;
211         UART_FLAG_t flag;
212         struct tile_uart_port *tile_uart;
213         gxio_uart_context_t *context;
214
215         tile_uart = container_of(port, struct tile_uart_port, uart);
216         if (!mutex_trylock(&tile_uart->mutex))
217                 return 0;
218         context = &tile_uart->context;
219
220         flag.word = gxio_uart_read(context, UART_FLAG);
221         ret = (flag.wfifo_empty) ? TIOCSER_TEMT : 0;
222         mutex_unlock(&tile_uart->mutex);
223
224         return ret;
225 }
226
227
228 /*
229  * Set state of the modem control output lines.
230  */
231 static void tilegx_set_mctrl(struct uart_port *port, u_int mctrl)
232 {
233         /* N/A */
234 }
235
236
237 /*
238  * Get state of the modem control input lines.
239  */
240 static u_int tilegx_get_mctrl(struct uart_port *port)
241 {
242         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
243 }
244
245
246 /*
247  * Stop transmitting.
248  */
249 static void tilegx_stop_tx(struct uart_port *port)
250 {
251         /* N/A */
252 }
253
254
255 /*
256  * Start transmitting.
257  */
258 static void tilegx_start_tx(struct uart_port *port)
259 {
260         unsigned char ch;
261         struct circ_buf *xmit;
262         struct tile_uart_port *tile_uart;
263         gxio_uart_context_t *context;
264
265         tile_uart = container_of(port, struct tile_uart_port, uart);
266         if (!mutex_trylock(&tile_uart->mutex))
267                 return;
268         context = &tile_uart->context;
269         xmit = &port->state->xmit;
270         if (port->x_char) {
271                 if (tilegx_putchar(context, port->x_char))
272                         return;
273                 port->x_char = 0;
274                 port->icount.tx++;
275         }
276
277         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
278                 mutex_unlock(&tile_uart->mutex);
279                 return;
280         }
281
282         while (!uart_circ_empty(xmit)) {
283                 ch = xmit->buf[xmit->tail];
284                 if (tilegx_putchar(context, ch))
285                         break;
286                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
287                 port->icount.tx++;
288         }
289
290         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
291                 uart_write_wakeup(port);
292
293         mutex_unlock(&tile_uart->mutex);
294 }
295
296
297 /*
298  * Stop receiving - port is in process of being closed.
299  */
300 static void tilegx_stop_rx(struct uart_port *port)
301 {
302         int err;
303         struct tile_uart_port *tile_uart;
304         gxio_uart_context_t *context;
305         int cpu;
306
307         tile_uart = container_of(port, struct tile_uart_port, uart);
308         if (!mutex_trylock(&tile_uart->mutex))
309                 return;
310
311         context = &tile_uart->context;
312         cpu = tile_uart->irq_cpu;
313         err = gxio_uart_cfg_interrupt(context, cpu_x(cpu), cpu_y(cpu),
314                                       KERNEL_PL, -1);
315         mutex_unlock(&tile_uart->mutex);
316 }
317
318 /*
319  * Control the transmission of a break signal.
320  */
321 static void tilegx_break_ctl(struct uart_port *port, int break_state)
322 {
323         /* N/A */
324 }
325
326
327 /*
328  * Perform initialization and enable port for reception.
329  */
330 static int tilegx_startup(struct uart_port *port)
331 {
332         struct tile_uart_port *tile_uart;
333         gxio_uart_context_t *context;
334         int ret = 0;
335         int cpu = raw_smp_processor_id();  /* pick an arbitrary cpu */
336
337         tile_uart = container_of(port, struct tile_uart_port, uart);
338         if (mutex_lock_interruptible(&tile_uart->mutex))
339                 return -EBUSY;
340         context = &tile_uart->context;
341
342         /* Now open the hypervisor device if we haven't already. */
343         if (context->fd < 0) {
344                 UART_INTERRUPT_MASK_t intr_mask;
345
346                 /* Initialize UART device. */
347                 ret = gxio_uart_init(context, port->line);
348                 if (ret) {
349                         ret = -ENXIO;
350                         goto err;
351                 }
352
353                 /* Create our IRQs. */
354                 port->irq = irq_alloc_hwirq(-1);
355                 if (!port->irq)
356                         goto err_uart_dest;
357                 tile_irq_activate(port->irq, TILE_IRQ_PERCPU);
358
359                 /* Register our IRQs. */
360                 ret = request_irq(port->irq, tilegx_interrupt, 0,
361                                   tilegx_uart_driver.driver_name, port);
362                 if (ret)
363                         goto err_dest_irq;
364
365                 /* Request that the hardware start sending us interrupts. */
366                 tile_uart->irq_cpu = cpu;
367                 ret = gxio_uart_cfg_interrupt(context, cpu_x(cpu), cpu_y(cpu),
368                                               KERNEL_PL, port->irq);
369                 if (ret)
370                         goto err_free_irq;
371
372                 /* Enable UART Tx/Rx Interrupt. */
373                 intr_mask.word = gxio_uart_read(context, UART_INTERRUPT_MASK);
374                 intr_mask.wfifo_re = 0;
375                 intr_mask.rfifo_we = 0;
376                 gxio_uart_write(context, UART_INTERRUPT_MASK, intr_mask.word);
377
378                 /* Reset the Tx/Rx interrupt in case it's set. */
379                 gxio_uart_write(context, UART_INTERRUPT_STATUS,
380                                 UART_INTERRUPT_MASK__WFIFO_RE_MASK |
381                                 UART_INTERRUPT_MASK__RFIFO_WE_MASK);
382         }
383
384         mutex_unlock(&tile_uart->mutex);
385         return ret;
386
387 err_free_irq:
388         free_irq(port->irq, port);
389 err_dest_irq:
390         irq_free_hwirq(port->irq);
391 err_uart_dest:
392         gxio_uart_destroy(context);
393         ret = -ENXIO;
394 err:
395         mutex_unlock(&tile_uart->mutex);
396         return ret;
397 }
398
399
400 /*
401  * Release kernel resources if it is the last close, disable the port,
402  * free IRQ and close the port.
403  */
404 static void tilegx_shutdown(struct uart_port *port)
405 {
406         int err;
407         UART_INTERRUPT_MASK_t intr_mask;
408         struct tile_uart_port *tile_uart;
409         gxio_uart_context_t *context;
410         int cpu;
411
412         tile_uart = container_of(port, struct tile_uart_port, uart);
413         if (mutex_lock_interruptible(&tile_uart->mutex))
414                 return;
415         context = &tile_uart->context;
416
417         /* Disable UART Tx/Rx Interrupt. */
418         intr_mask.word = gxio_uart_read(context, UART_INTERRUPT_MASK);
419         intr_mask.wfifo_re = 1;
420         intr_mask.rfifo_we = 1;
421         gxio_uart_write(context, UART_INTERRUPT_MASK, intr_mask.word);
422
423         /* Request that the hardware stop sending us interrupts. */
424         cpu = tile_uart->irq_cpu;
425         err = gxio_uart_cfg_interrupt(context, cpu_x(cpu), cpu_y(cpu),
426                                       KERNEL_PL, -1);
427
428         if (port->irq > 0) {
429                 free_irq(port->irq, port);
430                 irq_free_hwirq(port->irq);
431                 port->irq = 0;
432         }
433
434         gxio_uart_destroy(context);
435
436         mutex_unlock(&tile_uart->mutex);
437 }
438
439
440 /*
441  * Flush the buffer.
442  */
443 static void tilegx_flush_buffer(struct uart_port *port)
444 {
445         /* N/A */
446 }
447
448
449 /*
450  * Change the port parameters.
451  */
452 static void tilegx_set_termios(struct uart_port *port,
453                                struct ktermios *termios, struct ktermios *old)
454 {
455         int err;
456         UART_DIVISOR_t divisor;
457         UART_TYPE_t type;
458         unsigned int baud;
459         struct tile_uart_port *tile_uart;
460         gxio_uart_context_t *context;
461
462         tile_uart = container_of(port, struct tile_uart_port, uart);
463         if (!mutex_trylock(&tile_uart->mutex))
464                 return;
465         context = &tile_uart->context;
466
467         /* Open the hypervisor device if we haven't already. */
468         if (context->fd < 0) {
469                 err = gxio_uart_init(context, port->line);
470                 if (err) {
471                         mutex_unlock(&tile_uart->mutex);
472                         return;
473                 }
474         }
475
476         divisor.word = gxio_uart_read(context, UART_DIVISOR);
477         type.word = gxio_uart_read(context, UART_TYPE);
478
479         /* Divisor. */
480         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
481         divisor.divisor = uart_get_divisor(port, baud);
482
483         /* Byte size. */
484         if ((termios->c_cflag & CSIZE) == CS7)
485                 type.dbits = UART_TYPE__DBITS_VAL_SEVEN_DBITS;
486         else
487                 type.dbits = UART_TYPE__DBITS_VAL_EIGHT_DBITS;
488
489         /* Parity. */
490         if (termios->c_cflag & PARENB) {
491                 /* Mark or Space parity. */
492                 if (termios->c_cflag & CMSPAR)
493                         if (termios->c_cflag & PARODD)
494                                 type.ptype = UART_TYPE__PTYPE_VAL_MARK;
495                         else
496                                 type.ptype = UART_TYPE__PTYPE_VAL_SPACE;
497                 else if (termios->c_cflag & PARODD)
498                         type.ptype = UART_TYPE__PTYPE_VAL_ODD;
499                 else
500                         type.ptype = UART_TYPE__PTYPE_VAL_EVEN;
501         } else
502                 type.ptype = UART_TYPE__PTYPE_VAL_NONE;
503
504         /* Stop bits. */
505         if (termios->c_cflag & CSTOPB)
506                 type.sbits = UART_TYPE__SBITS_VAL_TWO_SBITS;
507         else
508                 type.sbits = UART_TYPE__SBITS_VAL_ONE_SBITS;
509
510         /* Set the uart paramters. */
511         gxio_uart_write(context, UART_DIVISOR, divisor.word);
512         gxio_uart_write(context, UART_TYPE, type.word);
513
514         mutex_unlock(&tile_uart->mutex);
515 }
516
517
518 /*
519  * Return string describing the specified port.
520  */
521 static const char *tilegx_type(struct uart_port *port)
522 {
523         return port->type == PORT_TILEGX ? DRIVER_NAME_STRING : NULL;
524 }
525
526
527 /*
528  * Release the resources being used by 'port'.
529  */
530 static void tilegx_release_port(struct uart_port *port)
531 {
532         /* Nothing to release. */
533 }
534
535
536 /*
537  * Request the resources being used by 'port'.
538  */
539 static int tilegx_request_port(struct uart_port *port)
540 {
541         /* Always present. */
542         return 0;
543 }
544
545
546 /*
547  * Configure/autoconfigure the port.
548  */
549 static void tilegx_config_port(struct uart_port *port, int flags)
550 {
551         if (flags & UART_CONFIG_TYPE)
552                 port->type = PORT_TILEGX;
553 }
554
555
556 /*
557  * Verify the new serial_struct (for TIOCSSERIAL).
558  */
559 static int tilegx_verify_port(struct uart_port *port,
560                               struct serial_struct *ser)
561 {
562         if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_TILEGX))
563                 return -EINVAL;
564
565         return 0;
566 }
567
568 #ifdef CONFIG_CONSOLE_POLL
569
570 /*
571  * Console polling routines for writing and reading from the uart while
572  * in an interrupt or debug context.
573  */
574
575 static int tilegx_poll_get_char(struct uart_port *port)
576 {
577         UART_FIFO_COUNT_t count;
578         gxio_uart_context_t *context;
579         struct tile_uart_port *tile_uart;
580
581         tile_uart = container_of(port, struct tile_uart_port, uart);
582         context = &tile_uart->context;
583         count.word = gxio_uart_read(context, UART_FIFO_COUNT);
584         if (count.rfifo_count == 0)
585                 return NO_POLL_CHAR;
586         return (char)gxio_uart_read(context, UART_RECEIVE_DATA);
587 }
588
589 static void tilegx_poll_put_char(struct uart_port *port, unsigned char c)
590 {
591         gxio_uart_context_t *context;
592         struct tile_uart_port *tile_uart;
593
594         tile_uart = container_of(port, struct tile_uart_port, uart);
595         context = &tile_uart->context;
596         gxio_uart_write(context, UART_TRANSMIT_DATA, (unsigned long)c);
597 }
598
599 #endif /* CONFIG_CONSOLE_POLL */
600
601
602 static const struct uart_ops tilegx_ops = {
603         .tx_empty       = tilegx_tx_empty,
604         .set_mctrl      = tilegx_set_mctrl,
605         .get_mctrl      = tilegx_get_mctrl,
606         .stop_tx        = tilegx_stop_tx,
607         .start_tx       = tilegx_start_tx,
608         .stop_rx        = tilegx_stop_rx,
609         .break_ctl      = tilegx_break_ctl,
610         .startup        = tilegx_startup,
611         .shutdown       = tilegx_shutdown,
612         .flush_buffer   = tilegx_flush_buffer,
613         .set_termios    = tilegx_set_termios,
614         .type           = tilegx_type,
615         .release_port   = tilegx_release_port,
616         .request_port   = tilegx_request_port,
617         .config_port    = tilegx_config_port,
618         .verify_port    = tilegx_verify_port,
619 #ifdef CONFIG_CONSOLE_POLL
620         .poll_get_char  = tilegx_poll_get_char,
621         .poll_put_char  = tilegx_poll_put_char,
622 #endif
623 };
624
625
626 static void tilegx_init_ports(void)
627 {
628         int i;
629         struct uart_port *port;
630
631         for (i = 0; i < TILEGX_UART_NR; i++) {
632                 port = &tile_uart_ports[i].uart;
633                 port->ops = &tilegx_ops;
634                 port->line = i;
635                 port->type = PORT_TILEGX;
636                 port->uartclk = TILEGX_UART_REF_CLK;
637                 port->flags = UPF_BOOT_AUTOCONF;
638
639                 tile_uart_ports[i].context.fd = -1;
640                 mutex_init(&tile_uart_ports[i].mutex);
641         }
642 }
643
644
645 static struct uart_driver tilegx_uart_driver = {
646         .owner          = THIS_MODULE,
647         .driver_name    = DRIVER_NAME_STRING,
648         .dev_name       = TILEGX_UART_NAME,
649         .major          = TILEGX_UART_MAJOR,
650         .minor          = TILEGX_UART_MINOR,
651         .nr             = TILEGX_UART_NR,
652 };
653
654
655 static int __init tilegx_init(void)
656 {
657         int i;
658         int ret;
659         struct tty_driver *tty_drv;
660
661         ret = uart_register_driver(&tilegx_uart_driver);
662         if (ret)
663                 return ret;
664         tty_drv = tilegx_uart_driver.tty_driver;
665         tty_drv->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
666         tty_drv->init_termios.c_ispeed = 115200;
667         tty_drv->init_termios.c_ospeed = 115200;
668
669         tilegx_init_ports();
670
671         for (i = 0; i < TILEGX_UART_NR; i++) {
672                 struct uart_port *port = &tile_uart_ports[i].uart;
673                 ret = uart_add_one_port(&tilegx_uart_driver, port);
674         }
675
676         return 0;
677 }
678
679
680 static void __exit tilegx_exit(void)
681 {
682         int i;
683         struct uart_port *port;
684
685         for (i = 0; i < TILEGX_UART_NR; i++) {
686                 port = &tile_uart_ports[i].uart;
687                 uart_remove_one_port(&tilegx_uart_driver, port);
688         }
689
690         uart_unregister_driver(&tilegx_uart_driver);
691 }
692
693
694 module_init(tilegx_init);
695 module_exit(tilegx_exit);
696
697 MODULE_AUTHOR("Tilera Corporation");
698 MODULE_DESCRIPTION("TILEGx serial port driver");
699 MODULE_LICENSE("GPL");