Root/target/linux/ubicom32/files/drivers/serial/ubi32_uarttio.c

1/*
2 * drivers/serial/ubi32_uarttio.c
3 * Ubicom32 Serial Virtual Peripherial 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
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/platform_device.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33
34#include <asm/ip5000.h>
35#include <asm/gpio.h>
36#include <asm/thread.h>
37#include <asm/uart_tio.h>
38
39#define DRIVER_NAME "ubi32_uarttio"
40
41/*
42 * For storing the module parameters.
43 */
44#define UBI32_UARTTIO_MAX_PARAM_LEN 80
45static char utio_ports_param[UBI32_UARTTIO_MAX_PARAM_LEN];
46
47/*
48 * UART name and device definitions
49 */
50#define UBI32_UARTTIO_NAME "ttyUV" // XXX
51#define UBI32_UARTTIO_MAJOR 206 // XXX
52#define UBI32_UARTTIO_MINOR 64 // XXX
53
54/*
55 * The following structures are allocated statically because the
56 * memory allocation subsystem is not initialized this early on
57 */
58
59/*
60 * Per port structure
61 */
62struct ubi32_uarttio_port {
63    struct uarttio_uart *uart;
64    unsigned int tx_pin;
65    unsigned int rx_pin;
66
67    struct uart_port port;
68
69    u8_t added;
70
71    /*
72     * If this value is set, the port has had its direction set already
73     */
74    u8_t port_init;
75};
76static struct ubi32_uarttio_port uarttio_ports[CONFIG_SERIAL_UBI32_UARTTIO_NR_UARTS];
77
78/*
79 * Number of ports currently initialized
80 */
81static int uarttio_nports;
82
83/*
84 * Per device structure
85 */
86struct ubi32_uarttio_instance {
87    struct uarttio_regs *regs;
88    struct ubi32_uarttio_port *ports;
89
90    u8_t irq_requested;
91    u8_t driver_registered;
92    u8_t irq;
93};
94static struct ubi32_uarttio_instance uarttio_inst;
95
96#ifdef CONFIG_SERIAL_UBI32_UARTTIO_CONSOLE
97static struct console ubi32_uarttio_console;
98#define UBI32_UARTTIO_CONSOLE &ubi32_uarttio_console
99#else
100#define UBI32_UARTTIO_CONSOLE NULL
101#endif
102
103static struct uart_driver ubi32_uarttio_uart_driver = {
104    .owner = THIS_MODULE,
105    .driver_name = DRIVER_NAME,
106    .dev_name = UBI32_UARTTIO_NAME,
107    .major = UBI32_UARTTIO_MAJOR,
108    .minor = UBI32_UARTTIO_MINOR,
109    .cons = UBI32_UARTTIO_CONSOLE,
110};
111
112#ifdef UBI32_UARTTIO_UNUSED
113/*
114 * ubi32_uarttio_get_send_space
115 */
116static int ubi32_uarttio_get_send_space(struct uarttio_uart *uart)
117{
118    int count = uart->tx_fifo_head - uart->tx_fifo_tail;
119    if (count < 0) {
120        count += uart->tx_fifo_size;
121    }
122    return uart->tx_fifo_size - count;
123}
124#endif
125
126/*
127 * ubi32_uarttio_get_recv_ready
128 */
129static int ubi32_uarttio_get_recv_ready(struct uarttio_uart *uart)
130{
131    int count = uart->rx_fifo_head - uart->rx_fifo_tail;
132    if (count < 0) {
133        count += uart->rx_fifo_size;
134    }
135    return count;
136}
137
138/*
139 * ubi32_uarttio_get_char()
140 */
141static u8_t ubi32_uarttio_get_char(struct uarttio_uart *uart)
142{
143    /*
144     * Retrieve byte
145     */
146    u32_t tail = uart->rx_fifo_tail;
147    u8_t data = uart->rx_fifo[tail];
148
149    if (++tail == uart->rx_fifo_size) {
150        tail = 0;
151    }
152    uart->rx_fifo_tail = tail;
153
154    return data;
155}
156
157/*
158 * ubi32_uarttio_put_char()
159 */
160static int ubi32_uarttio_put_char(struct uarttio_uart *uart, u8_t c)
161{
162    u32_t head = uart->tx_fifo_head;
163    u32_t prev = head;
164
165    /*
166     * Wrap
167     */
168    if (++head == uart->tx_fifo_size) {
169        head = 0;
170    }
171
172    /*
173     * If there isn't any space, return EBUSY
174     */
175    if (head == uart->tx_fifo_tail) {
176        return -EBUSY;
177    }
178
179    /*
180     * Put the character in the queue
181     */
182    uart->tx_fifo[prev] = c;
183    uart->tx_fifo_head = head;
184
185    return 0;
186}
187
188/*
189 * ubi32_uarttio_set_baud
190 */
191static int ubi32_uarttio_set_baud(struct ubi32_uarttio_port *uup, unsigned int baud)
192{
193    if (uup->uart->current_baud_rate == baud) {
194        return 0;
195    }
196
197    uup->uart->baud_rate = baud;
198    uup->uart->flags |= UARTTIO_UART_FLAG_SET_RATE;
199    while (uup->uart->flags & UARTTIO_UART_FLAG_SET_RATE) {
200        cpu_relax();
201    }
202
203    if (uup->uart->current_baud_rate != baud) {
204        /*
205         * Failed to set baud rate
206         */
207        printk(KERN_WARNING "Invalid baud rate %u, running at %u\n", baud, uup->uart->current_baud_rate);
208        return -EINVAL;
209    }
210
211    return 0;
212}
213
214/*
215 * ubi32_uarttio_handle_receive
216 */
217static void ubi32_uarttio_handle_receive(struct ubi32_uarttio_port *uup, int stat)
218{
219    struct uarttio_uart *uart = uup->uart;
220    struct uart_port *port = &uup->port;
221    struct tty_struct *tty = port->info->port.tty;
222    unsigned char ch = 0;
223    char flag = TTY_NORMAL;
224    int count;
225
226    if ((stat & (UARTTIO_UART_INT_RX | UARTTIO_UART_INT_RXFRAME | UARTTIO_UART_INT_RXOVF)) == 0) {
227        return;
228    }
229
230    if (stat & UARTTIO_UART_INT_RX) {
231        count = ubi32_uarttio_get_recv_ready(uart);
232        port->icount.rx += count;
233    }
234
235    if (stat & UARTTIO_UART_INT_RXOVF) {
236        port->icount.overrun++;
237    }
238
239    if (stat & UARTTIO_UART_INT_RXFRAME) {
240        port->icount.frame++;
241    }
242
243    stat &= ~port->ignore_status_mask;
244
245    if (stat & UARTTIO_UART_INT_RX) {
246        int i;
247        for (i = 0; i < count; i++) {
248            ch = ubi32_uarttio_get_char(uart);
249            tty_insert_flip_char(tty, ch, flag);
250        }
251    }
252
253    if (stat & UARTTIO_UART_INT_RXFRAME) {
254        tty_insert_flip_char(tty, 0, TTY_FRAME);
255    }
256
257    if (stat & UARTTIO_UART_INT_RXOVF) {
258        tty_insert_flip_char(tty, 0, TTY_OVERRUN);
259    }
260}
261
262/*
263 * ubi32_uarttio_stop_tx
264 * interrupts are disabled on entry
265 */
266static void ubi32_uarttio_stop_tx(struct uart_port *port)
267{
268    struct ubi32_uarttio_port *uup = port->private_data;
269
270    uup->uart->int_mask &= ~UARTTIO_UART_INT_TXBE;
271}
272
273/*
274 * ubi32_uarttio_handle_transmit
275 */
276static void ubi32_uarttio_handle_transmit(struct ubi32_uarttio_port *uup, int stat)
277{
278    struct uarttio_uart *uart = uup->uart;
279    struct uart_port *port = &uup->port;
280    struct circ_buf *xmit = &port->info->xmit;
281
282    if (!(stat & UARTTIO_UART_INT_TXBE)) {
283        return;
284    }
285
286    if (port->x_char) {
287        if (ubi32_uarttio_put_char(uart, port->x_char)) {
288            return;
289        }
290        port->x_char = 0;
291        port->icount.tx++;
292        return;
293    }
294
295    if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
296        ubi32_uarttio_stop_tx(port);
297        return;
298    }
299
300    /*
301     * Send as many characters as we can
302     */
303    while (ubi32_uarttio_put_char(uart, xmit->buf[xmit->tail]) == 0) {
304        xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
305        port->icount.tx++;
306        if (uart_circ_empty(xmit)) {
307            break;
308        }
309    }
310
311    /* wake up */
312    if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
313        uart_write_wakeup(port);
314    }
315
316    if (uart_circ_empty(xmit)) {
317        ubi32_uarttio_stop_tx(port);
318    }
319}
320
321/*
322 * ubi32_uarttio_start_tx
323 * port is locked and interrupts are disabled
324 */
325static void ubi32_uarttio_start_tx(struct uart_port *port)
326{
327    struct ubi32_uarttio_port *uup = port->private_data;
328    struct uarttio_uart *uart = uup->uart;
329
330    uart->int_mask |= UARTTIO_UART_INT_TXBE;
331}
332
333/*
334 * ubi32_uarttio_stop_rx
335 * Interrupts are enabled
336 */
337static void ubi32_uarttio_stop_rx(struct uart_port *port)
338{
339    struct ubi32_uarttio_port *uup = port->private_data;
340    struct uarttio_uart *uart = uup->uart;
341
342    /*
343     * don't forward any more data (like !CREAD)
344     */
345    uart->int_mask &= ~UARTTIO_UART_INT_RX;
346    port->ignore_status_mask = UARTTIO_UART_INT_RX;
347}
348
349/*
350 * ubi32_uarttio_enable_ms
351 * Set the modem control timer to fire immediately.
352 */
353static void ubi32_uarttio_enable_ms(struct uart_port *port)
354{
355    /* N/A */
356}
357
358/*
359 * ubi32_uarttio_isr
360 */
361static irqreturn_t ubi32_uarttio_isr(int irq, void *appdata)
362{
363    struct ubi32_uarttio_port *uup = uarttio_ports;
364    int i;
365
366    /*
367     * Service all of the ports
368     */
369    for (i = 0; i < uarttio_nports; i++) {
370        unsigned int flags;
371
372        if (!(uup->uart->flags & UARTTIO_UART_FLAG_ENABLED)) {
373            uup++;
374            continue;
375        }
376
377        spin_lock(&uup->port.lock);
378
379        flags = uup->uart->int_flags;
380
381        uup->uart->int_flags = 0;
382
383        ubi32_uarttio_handle_receive(uup, flags);
384        ubi32_uarttio_handle_transmit(uup, flags);
385
386        tty_flip_buffer_push(uup->port.info->port.tty);
387
388        spin_unlock(&uup->port.lock);
389
390        uup++;
391    }
392
393    return IRQ_HANDLED;
394}
395
396/*
397 * ubi32_uarttio_tx_empty
398 * Return TIOCSER_TEMT when transmitter is not busy.
399 */
400static unsigned int ubi32_uarttio_tx_empty(struct uart_port *port)
401{
402    struct ubi32_uarttio_port *uup = port->private_data;
403
404    if (uup->uart->tx_fifo_head == uup->uart->tx_fifo_tail) {
405        return TIOCSER_TEMT;
406    }
407
408    return 0;
409}
410
411/*
412 * ubi32_uarttio_get_mctrl
413 */
414static unsigned int ubi32_uarttio_get_mctrl(struct uart_port *port)
415{
416    return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
417}
418
419/*
420 * ubi32_uarttio_set_mctrl
421 */
422static void ubi32_uarttio_set_mctrl(struct uart_port *port, unsigned int mctrl)
423{
424    /* N/A */
425}
426
427/*
428 * ubi32_uarttio_break_ctl
429 */
430static void ubi32_uarttio_break_ctl(struct uart_port *port, int break_state)
431{
432    /* N/A */
433}
434
435/*
436 * ubi32_uarttio_startup
437 */
438static int ubi32_uarttio_startup(struct uart_port *port)
439{
440    struct ubi32_uarttio_port *uup = port->private_data;
441    struct uarttio_uart *uart = uup->uart;
442
443    uart->flags |= UARTTIO_UART_FLAG_ENABLED;
444
445    uart->int_mask |= UARTTIO_UART_INT_TXBE | UARTTIO_UART_INT_RX;
446
447    return 0;
448}
449
450/*
451 * ubi32_uarttio_shutdown
452 */
453static void ubi32_uarttio_shutdown(struct uart_port *port)
454{
455    struct ubi32_uarttio_port *uup = port->private_data;
456    struct uarttio_uart *uart = uup->uart;
457
458    uart->int_mask = 0;
459    uart->flags &= ~UARTTIO_UART_FLAG_ENABLED;
460}
461
462/*
463 * ubi32_uarttio_set_termios
464 */
465static void ubi32_uarttio_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
466{
467    struct ubi32_uarttio_port *uup = port->private_data;
468    unsigned long flags;
469    unsigned int baud;
470
471    spin_lock_irqsave(&port->lock, flags);
472
473#if 0
474    port->read_status_mask = UBI32_UARTTIO_RX | UBI32_UARTTIO_RXOVF | UBI32_UARTTIO_TXOVF;
475
476    if (termios->c_iflag & INPCK) {
477        port->read_status_mask |= UBI32_UARTTIO_RXFRAME;
478    }
479#endif
480
481    port->ignore_status_mask = 0;
482    if (termios->c_iflag & IGNPAR) {
483        port->ignore_status_mask |= UARTTIO_UART_INT_RXFRAME |
484                        UARTTIO_UART_INT_RXOVF;
485    }
486
487    /*
488     * ignore all characters if CREAD is not set
489     */
490    if ((termios->c_cflag & CREAD) == 0) {
491        port->ignore_status_mask |= UARTTIO_UART_INT_RX |
492                        UARTTIO_UART_INT_RXFRAME |
493                        UARTTIO_UART_INT_RXOVF;
494    }
495
496    /* update timeout */
497    baud = uart_get_baud_rate(port, termios, old, 0, 460800);
498    uart_update_timeout(port, termios->c_cflag, baud);
499
500    ubi32_uarttio_set_baud(uup, baud);
501    spin_unlock_irqrestore(&port->lock, flags);
502}
503
504/*
505 * ubi32_uarttio_type
506 */
507static const char *ubi32_uarttio_type(struct uart_port *port)
508{
509    return (port->type == PORT_UBI32_UARTTIO) ? "UBI32_UARTTIO" : NULL;
510}
511
512/*
513 * ubi32_uarttio_release_port
514 * Release the memory region(s) being used by 'port'.
515 */
516static void ubi32_uarttio_release_port(struct uart_port *port)
517{
518}
519
520/*
521 * ubi32_uarttio_request_port
522 * Request the memory region(s) being used by 'port'.
523 */
524static int ubi32_uarttio_request_port(struct uart_port *port)
525{
526    return 0;
527}
528
529/*
530 * ubi32_uarttio_config_port
531 * Configure/autoconfigure the port.
532 */
533static void ubi32_uarttio_config_port(struct uart_port *port, int flags)
534{
535    if ((flags & UART_CONFIG_TYPE) && (ubi32_uarttio_request_port(port) == 0)) {
536        port->type = PORT_UBI32_UARTTIO;
537    }
538}
539
540/*
541 * ubi32_uarttio_verify_port
542 * Verify the new serial_struct (for TIOCSSERIAL).
543 *
544 * The only change we allow are to the flags and type, and
545 * even then only between PORT_UBI32_UARTTIO and PORT_UNKNOWN
546 */
547static int ubi32_uarttio_verify_port(struct uart_port *port, struct serial_struct *ser)
548{
549    return 0;
550}
551
552static struct uart_ops ubi32_uarttio_pops = {
553    .tx_empty = ubi32_uarttio_tx_empty,
554    .set_mctrl = ubi32_uarttio_set_mctrl,
555    .get_mctrl = ubi32_uarttio_get_mctrl,
556    .stop_tx = ubi32_uarttio_stop_tx,
557    .start_tx = ubi32_uarttio_start_tx,
558    .stop_rx = ubi32_uarttio_stop_rx,
559    .enable_ms = ubi32_uarttio_enable_ms,
560    .break_ctl = ubi32_uarttio_break_ctl,
561    .startup = ubi32_uarttio_startup,
562    .shutdown = ubi32_uarttio_shutdown,
563    .set_termios = ubi32_uarttio_set_termios,
564    .type = ubi32_uarttio_type,
565    .release_port = ubi32_uarttio_release_port,
566    .request_port = ubi32_uarttio_request_port,
567    .config_port = ubi32_uarttio_config_port,
568    .verify_port = ubi32_uarttio_verify_port,
569};
570
571/*
572 * ubi32_uarttio_add_ports
573 */
574static int __init ubi32_uarttio_add_ports(void)
575{
576    int res = 0;
577    struct ubi32_uarttio_port *uup = uarttio_ports;
578    int i = 0;
579
580    for (i = 0; i < uarttio_nports; i++) {
581        /*
582         * Setup the GPIOs
583         */
584        res = gpio_request(uup->tx_pin, "ubi32_uarttio_tx");
585        if (res) {
586            printk(KERN_WARNING "Failed to request GPIO %d\n", uup->tx_pin);
587            res = -EBUSY;
588            goto next;
589        }
590
591        res = gpio_request(uup->rx_pin, "ubi32_uarttio_rx");
592        if (res) {
593            gpio_free(uup->tx_pin);
594            printk(KERN_WARNING "Failed to request GPIO %d\n", uup->rx_pin);
595            res = -EBUSY;
596            goto next;
597        }
598
599        res = uart_add_one_port(&ubi32_uarttio_uart_driver, &uup->port);
600        if (res) {
601            gpio_free(uup->rx_pin);
602            gpio_free(uup->tx_pin);
603            res = -ENODEV;
604            printk(KERN_WARNING "Failed to add port %d,%d\n", uup->tx_pin, uup->rx_pin);
605            goto next;
606        }
607        uup->added = 1;
608
609        /*
610         * Set the direction of the ports now, after we're sure that everything is ok
611         */
612        if (!uup->port_init) {
613            gpio_direction_output(uup->tx_pin, 1);
614            gpio_direction_input(uup->rx_pin);
615        }
616
617next:
618        uup++;
619    }
620    return res;
621}
622
623/*
624 * ubi32_uarttio_cleanup
625 */
626static void ubi32_uarttio_cleanup(void)
627{
628    struct ubi32_uarttio_port *uup;
629    int i;
630
631    /*
632     * Stop the hardware thread
633     */
634    if (uarttio_inst.regs) {
635        thread_disable(uarttio_inst.regs->thread);
636    }
637    if (uarttio_inst.irq_requested) {
638        free_irq(uarttio_inst.irq, NULL);
639    }
640
641    /*
642     * Get rid of the ports
643     */
644    uup = uarttio_inst.ports;
645    for (i = 0; i < uarttio_nports; i++) {
646        gpio_free(uup->tx_pin);
647        gpio_free(uup->rx_pin);
648        if (uup->added) {
649            uart_remove_one_port(&ubi32_uarttio_uart_driver, &uup->port);
650        }
651        uup++;
652    }
653
654    if (uarttio_inst.driver_registered) {
655        uart_unregister_driver(&ubi32_uarttio_uart_driver);
656    }
657}
658
659/*
660 * ubi32_uarttio_setup_port
661 * Setup a port in the TIO registers
662 */
663static int ubi32_uarttio_setup_port(int index,
664                    struct uarttio_uart *uart,
665                    unsigned int baud, unsigned int tx_pin,
666                    unsigned int rx_pin)
667{
668    struct ubi32_uarttio_port *uup = &uarttio_ports[index];
669    void *tx_port = ubi_gpio_get_port(tx_pin);
670    void *rx_port = ubi_gpio_get_port(rx_pin);
671
672    /*
673     * Verify the ports are on chip
674     */
675    if (!tx_port || !rx_port) {
676        printk(KERN_WARNING "Invalid port(s) specified: %u or %u\n", tx_pin, rx_pin);
677        return -EINVAL;
678    }
679
680    uup->tx_pin = tx_pin;
681    uup->rx_pin = rx_pin;
682    uup->uart = uart;
683
684    /*
685     * Setup the port structure
686     */
687    uup->port.ops = &ubi32_uarttio_pops;
688    uup->port.line = index;
689    uup->port.iotype = UPIO_MEM;
690    uup->port.flags = UPF_BOOT_AUTOCONF;
691    uup->port.fifosize = uup->uart->tx_fifo_size;
692    uup->port.private_data = uup;
693
694    /*
695     * We share this IRQ across all ports
696     */
697    uup->port.irq = uarttio_inst.irq;
698
699    /*
700     * We really don't have a mem/map base but without these variables
701     * set, the serial_core won't startup.
702     */
703    uup->port.membase = (void __iomem *)uup;
704    uup->port.mapbase = (resource_size_t)uup;
705    spin_lock_init(&uup->port.lock);
706
707    /*
708     * Set up the hardware
709     */
710    uart->flags = UARTTIO_UART_FLAG_SET_RATE | UARTTIO_UART_FLAG_RESET;
711
712    uart->tx_port = (unsigned int)tx_port;
713    uart->tx_pin = gpio_pin_index(tx_pin);
714    uart->tx_bits = 8;
715    uart->tx_stop_bits = 1;
716
717    uart->rx_port = (unsigned int)rx_port;
718    uart->rx_pin = gpio_pin_index(rx_pin);
719    uart->rx_bits = 8;
720    uart->rx_stop_bits = 1;
721
722    uart->baud_rate = baud;
723
724    return 0;
725}
726
727enum ubi32_uarttio_parse_states {
728    UBI32_UARTTIO_PARSE_STATE_BAUD,
729    UBI32_UARTTIO_PARSE_STATE_TX_PIN,
730    UBI32_UARTTIO_PARSE_STATE_RX_PIN,
731    UBI32_UARTTIO_PARSE_STATE_HS,
732    UBI32_UARTTIO_PARSE_STATE_CTS_PIN,
733    UBI32_UARTTIO_PARSE_STATE_RTS_PIN,
734};
735
736/*
737 * ubi32_uarttio_parse_param
738 */
739static int ubi32_uarttio_parse_param(char *str)
740{
741    int res;
742    int i;
743    int baud = 0;
744    int tx_pin = 0;
745    int rx_pin = 0;
746    int hs = 0;
747    int cts_pin = 0;
748    int rts_pin = 0;
749    int nfound = 0;
750    enum ubi32_uarttio_parse_states state = UBI32_UARTTIO_PARSE_STATE_BAUD;
751    struct uarttio_uart *uart = uarttio_inst.regs->uarts;
752
753    /*
754     * Run though the options and generate the proper structures
755     */
756    res = get_option(&str, &i);
757    while ((res == 2) || (res == 1)) {
758        switch (state) {
759        case UBI32_UARTTIO_PARSE_STATE_BAUD:
760            /*
761             * If we are here and nfound > 0 then create the port
762             * based on the previous input
763             */
764            if (nfound) {
765                /*
766                 * Create the port
767                 */
768                if (ubi32_uarttio_setup_port(nfound - 1, uart, baud, tx_pin, rx_pin)) {
769                    /*
770                     * Port was invalid
771                     */
772                    goto fail;
773                } else {
774                    printk(KERN_INFO "Serial port %d: tx=%d:rx=%d @ %d\n", nfound, tx_pin, rx_pin, baud);
775                    uart++;
776                }
777            }
778
779            /*
780             * Reset the variables and go to the next state
781             */
782            hs = 0;
783            baud = i;
784            state = UBI32_UARTTIO_PARSE_STATE_TX_PIN;
785            break;
786
787        case UBI32_UARTTIO_PARSE_STATE_TX_PIN:
788            tx_pin = i;
789            state = UBI32_UARTTIO_PARSE_STATE_RX_PIN;
790            break;
791
792        case UBI32_UARTTIO_PARSE_STATE_RX_PIN:
793            rx_pin = i;
794            state = UBI32_UARTTIO_PARSE_STATE_HS;
795            break;
796
797        case UBI32_UARTTIO_PARSE_STATE_HS:
798            hs = i;
799            if (hs) {
800                state = UBI32_UARTTIO_PARSE_STATE_CTS_PIN;
801                break;
802            }
803
804            if (nfound == uarttio_inst.regs->max_uarts) {
805                printk(KERN_WARNING "Maximum number of serial ports reached\n");
806                goto done;
807            }
808            nfound++;
809            state = UBI32_UARTTIO_PARSE_STATE_BAUD;
810            break;
811
812        case UBI32_UARTTIO_PARSE_STATE_CTS_PIN:
813            cts_pin = i;
814            state = UBI32_UARTTIO_PARSE_STATE_RTS_PIN;
815            break;
816
817        case UBI32_UARTTIO_PARSE_STATE_RTS_PIN:
818            rts_pin = i;
819
820            if (nfound == uarttio_inst.regs->max_uarts) {
821                printk(KERN_WARNING "Maximum number of serial ports reached\n");
822                goto done;
823            }
824            nfound++;
825            state = UBI32_UARTTIO_PARSE_STATE_BAUD;
826            break;
827        }
828        res = get_option(&str, &i);
829    }
830
831    if ((res > 2) || state != UBI32_UARTTIO_PARSE_STATE_BAUD) {
832        printk(KERN_WARNING "Parameter syntax error.\n");
833        res = -EINVAL;
834        goto fail;
835    }
836
837    /*
838     * Create the final port
839     */
840    if (ubi32_uarttio_setup_port(nfound - 1, uart, baud, tx_pin, rx_pin)) {
841        goto fail;
842    }
843    printk(KERN_INFO "Serial port %d: tx=%d:rx=%d @ %d\n", nfound, tx_pin, rx_pin, baud);
844
845done:
846    uarttio_nports = nfound;
847
848    return nfound ? 0 : -ENODEV;
849
850fail:
851    /*
852     * Reset the ports
853     */
854    uart = uarttio_inst.regs->uarts;
855    for (i = 0; i < uarttio_inst.regs->max_uarts; i++) {
856        uart->flags = 0;
857        uart++;
858    }
859
860    return res;
861}
862
863/*
864 * ubi32_uarttio_probe
865 */
866static int ubi32_uarttio_probe(void)
867{
868    int ret;
869    struct uarttio_node *uart_node;
870    char *str = utio_ports_param;
871    static int probed;
872    static int probe_result;
873
874    /*
875     * We only want to be probed once, we could be probed twice
876     * for example if we are used as a console
877     */
878    if (probed) {
879        return probe_result;
880    }
881    probed = 1;
882
883    /*
884     * Extract the TIO name from the setup string
885     */
886    while (*str) {
887        if (*str == ',') {
888            *str++ = 0;
889            break;
890        }
891        str++;
892    }
893
894    if (!*str) {
895        probe_result = -EINVAL;
896        return -EINVAL;
897    }
898
899    uart_node = (struct uarttio_node *)devtree_find_node(utio_ports_param);
900    if (!uart_node) {
901        probe_result = -ENODEV;
902        return -ENODEV;
903    }
904
905    uarttio_inst.irq = uart_node->dn.recvirq;
906    uarttio_inst.regs = uart_node->regs;
907
908    /*
909     * Parse module parameters.
910     */
911    ret = ubi32_uarttio_parse_param(str);
912    if (ret != 0) {
913        ubi32_uarttio_cleanup();
914        probe_result = ret;
915        return ret;
916    }
917
918    ubi32_uarttio_uart_driver.nr = uarttio_nports;
919
920    return 0;
921}
922
923#if defined(CONFIG_SERIAL_UBI32_UARTTIO_CONSOLE)
924/*
925 * ubi32_uarttio_console_setup
926 */
927static int __init ubi32_uarttio_console_setup(struct console *co, char *options)
928{
929    int baud;
930    int bits = 8;
931    int parity = 'n';
932    int flow = 'n';
933    struct ubi32_uarttio_port *uup;
934
935    /*
936     * Check whether an invalid uart number has been specified, and
937     * if so, search for the first available port that does have
938     * console support.
939     */
940    if (co->index == -1 || co->index >= uarttio_nports) {
941        co->index = 0;
942    }
943    uup = &uarttio_ports[co->index];
944    baud = uup->uart->baud_rate;
945    uup->uart->flags |= UARTTIO_UART_FLAG_ENABLED;
946
947    /*
948     * Setup the GPIOs
949     * We have to use the direct interface because the gpio
950     * subsystem is not available at this point.
951     */
952    uup->port_init = 1;
953    UBICOM32_GPIO_SET_PIN_HIGH(uup->tx_pin);
954    UBICOM32_GPIO_SET_PIN_OUTPUT(uup->tx_pin);
955    UBICOM32_GPIO_SET_PIN_INPUT(uup->rx_pin);
956
957    /*
958     * Start the thread
959     */
960    thread_enable(uarttio_inst.regs->thread);
961
962    /*
963     * Process options
964     */
965    if (options) {
966        uart_parse_options(options, &baud, &parity, &bits, &flow);
967        if (ubi32_uarttio_set_baud(uup, baud)) {
968            baud = uup->uart->current_baud_rate;
969        }
970    }
971
972    return uart_set_options(&uup->port, co, baud, 'n', 8, 'n');
973}
974
975/*
976 * ubi32_uarttio_console_putchar
977 */
978static void ubi32_uarttio_console_putchar(struct uart_port *port, int ch)
979{
980    struct ubi32_uarttio_port *uup = port->private_data;
981
982    while (ubi32_uarttio_put_char(uup->uart, ch)) {
983        cpu_relax();
984    }
985}
986
987/*
988 * ubi32_uarttio_console_write
989 * Interrupts are disabled on entering
990 */
991static void ubi32_uarttio_console_write(struct console *co, const char *s, unsigned int count)
992{
993    struct uart_port *port = &(uarttio_ports[co->index].port);
994    unsigned long flags = 0;
995
996    spin_lock_irqsave(&port->lock, flags);
997    uart_console_write(port, s, count, ubi32_uarttio_console_putchar);
998    spin_unlock_irqrestore(&port->lock, flags);
999}
1000
1001static struct console ubi32_uarttio_console = {
1002    .name = UBI32_UARTTIO_NAME,
1003    .write = ubi32_uarttio_console_write,
1004    .device = uart_console_device,
1005    .setup = ubi32_uarttio_console_setup,
1006    .flags = CON_PRINTBUFFER,
1007    .index = -1,
1008    .data = &ubi32_uarttio_uart_driver,
1009};
1010
1011static int __init ubi32_uarttio_console_init(void)
1012{
1013    int res;
1014
1015    res = ubi32_uarttio_probe();
1016    if (res) {
1017        return res;
1018    }
1019
1020    register_console(&ubi32_uarttio_console);
1021    return 0;
1022}
1023console_initcall(ubi32_uarttio_console_init);
1024#endif /* CONFIG_SERIAL_UBI32_UARTTIO_CONSOLE */
1025
1026/*
1027 * ubi32_serial_suspend
1028 */
1029static int ubi32_uarttio_suspend(struct platform_device *pdev, pm_message_t state)
1030{
1031    int i;
1032    for (i = 0; i < uarttio_nports; i++) {
1033        uart_suspend_port(&ubi32_uarttio_uart_driver, &uarttio_ports[i].port);
1034    }
1035
1036    return 0;
1037}
1038
1039/*
1040 * ubi32_serial_resume
1041 */
1042static int ubi32_uarttio_resume(struct platform_device *pdev)
1043{
1044    int i;
1045    for (i = 0; i < uarttio_nports; i++) {
1046        uart_resume_port(&ubi32_uarttio_uart_driver, &uarttio_ports[i].port);
1047    }
1048
1049    return 0;
1050}
1051
1052/*
1053 * ubi32_uarttio_remove
1054 */
1055static int __devexit ubi32_uarttio_remove(struct platform_device *pdev)
1056{
1057    ubi32_uarttio_cleanup();
1058
1059    uart_unregister_driver(&ubi32_uarttio_uart_driver);
1060
1061    return 0;
1062}
1063
1064static struct platform_driver ubi32_uarttio_platform_driver = {
1065    .remove = __devexit_p(ubi32_uarttio_remove),
1066    .suspend = ubi32_uarttio_suspend,
1067    .resume = ubi32_uarttio_resume,
1068    .driver = {
1069        .name = DRIVER_NAME,
1070        .owner = THIS_MODULE,
1071    },
1072};
1073
1074#ifndef MODULE
1075/*
1076 * Called at boot time.
1077 *
1078 * uarttio=TIONAME,(baud,tx_pin,rx_pin,handshake[,cts_pin,rts_pin],...)
1079 * TIONAME is the name of the devtree node which describes the UARTTIO
1080 * pin is the index of the pin, i.e. PA4 is 5 [(port * 32) + pin]
1081 * handshake = 1 to enable handshaking, provide cts_pin, rts_pin (UNSUPPORTED)
1082 * handshake = 0 to disable handshaking, do not provide cts_pin, rts_pin
1083 * Ex: uarttio=UARTTIO,57600,7,6,0,9600,8,9,0
1084 */
1085static int __init ubi32_uarttio_setup(char *str)
1086{
1087    strncpy(utio_ports_param, str, UBI32_UARTTIO_MAX_PARAM_LEN);
1088    utio_ports_param[UBI32_UARTTIO_MAX_PARAM_LEN - 1] = 0;
1089    return 1;
1090}
1091__setup("uarttio=", ubi32_uarttio_setup);
1092#endif
1093
1094/*
1095 * ubi32_uarttio_init
1096 */
1097static int __init ubi32_uarttio_init(void)
1098{
1099    int ret;
1100    int i;
1101
1102    ret = ubi32_uarttio_probe();
1103    if (ret) {
1104        return ret;
1105    }
1106
1107    /*
1108     * Request the IRQ (do it here since many ports share the same IRQ)
1109     */
1110    ret = request_irq(uarttio_inst.irq, ubi32_uarttio_isr, IRQF_DISABLED, DRIVER_NAME, NULL);
1111    if (ret != 0) {
1112        printk(KERN_WARNING "Could not request IRQ %d\n", uarttio_inst.irq);
1113        goto fail;
1114    }
1115    uarttio_inst.irq_requested = 1;
1116
1117    /*
1118     * Register the UART driver and add the ports
1119     */
1120    ret = uart_register_driver(&ubi32_uarttio_uart_driver);
1121    if (ret != 0) {
1122        goto fail;
1123    }
1124    uarttio_inst.driver_registered = 1;
1125
1126    ret = ubi32_uarttio_add_ports();
1127    if (ret != 0) {
1128        ubi32_uarttio_cleanup();
1129        return ret;
1130    }
1131
1132    /*
1133     * Start the thread
1134     */
1135    thread_enable(uarttio_inst.regs->thread);
1136
1137    for (i = 0; i < uarttio_nports; i++) {
1138        pr_info("Serial: Ubicom32 uarttio #%d: tx:%d rx:%d baud:%d\n",
1139            i, uarttio_ports[i].tx_pin, uarttio_ports[i].rx_pin,
1140            uarttio_ports[i].uart->current_baud_rate);
1141    }
1142    pr_info("Serial: Ubicom32 uarttio started on thread:%d irq:%d\n", uarttio_inst.regs->thread, uarttio_inst.irq);
1143
1144    return ret;
1145
1146fail:
1147    ubi32_uarttio_cleanup();
1148    return ret;
1149}
1150module_init(ubi32_uarttio_init);
1151
1152/*
1153 * ubi32_uarttio_exit
1154 */
1155static void __exit ubi32_uarttio_exit(void)
1156{
1157    platform_driver_unregister(&ubi32_uarttio_platform_driver);
1158}
1159module_exit(ubi32_uarttio_exit);
1160
1161module_param_string(ports, utio_ports_param, sizeof(utio_ports_param), 0444);
1162MODULE_PARM_DESC(ports, "Sets the ports to allocate: ports=TIONAME,(baud,txpin,rxpin,handshake[,ctspin,rtspin],...)\n"
1163            " TIONAME is the name of the devtree node which describes the UARTTIO\n"
1164            " pin is the index of the pin, i.e. PA4 is 5 [(port * 32) + pin]\n"
1165            " handshake = 1 to enable handshaking, provide ctspin, rtspin (UNSUPPORTED)\n"
1166            " handshake = 0 to disable handshaking, do not provide ctspin, rtspin\n"
1167            " Ex: ports=UARTTIO,57600,7,6,0,9600,8,9,0\n");
1168MODULE_AUTHOR("Patrick Tjin <pat.tjin@ubicom.com>");
1169MODULE_DESCRIPTION("Ubicom serial virtual peripherial driver");
1170MODULE_LICENSE("GPL");
1171MODULE_ALIAS_CHARDEV_MAJOR(UBI32_UARTTIO_MAJOR);
1172MODULE_ALIAS("platform:" DRIVER_NAME);
1173

Archive Download this file



interactive