[b77207e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 4 | * Copyright (c) 2013 Jakub Klama
|
---|
| 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /** @addtogroup genarch
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
| 36 | * @brief Gaisler GRLIB UART IP-Core driver.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
[e47ed05] | 39 | #include <genarch/drivers/grlib/uart.h>
|
---|
[b77207e] | 40 | #include <console/chardev.h>
|
---|
| 41 | #include <console/console.h>
|
---|
| 42 | #include <ddi/device.h>
|
---|
| 43 | #include <arch/asm.h>
|
---|
| 44 | #include <mm/slab.h>
|
---|
| 45 | #include <mm/page.h>
|
---|
| 46 | #include <mm/km.h>
|
---|
| 47 | #include <sysinfo/sysinfo.h>
|
---|
| 48 | #include <str.h>
|
---|
| 49 |
|
---|
| 50 | static void grlib_uart_sendb(outdev_t *dev, uint8_t byte)
|
---|
| 51 | {
|
---|
| 52 | grlib_uart_status_t *status;
|
---|
[e47ed05] | 53 | grlib_uart_t *uart = (grlib_uart_t *) dev->data;
|
---|
| 54 |
|
---|
[b77207e] | 55 | /* Wait for space becoming available in Tx FIFO. */
|
---|
| 56 | do {
|
---|
[e47ed05] | 57 | uint32_t reg = pio_read_32(&uart->io->status);
|
---|
| 58 | status = (grlib_uart_status_t *) ®
|
---|
[b77207e] | 59 | } while (status->tf != 0);
|
---|
[e47ed05] | 60 |
|
---|
[b77207e] | 61 | pio_write_32(&uart->io->data, byte);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | static void grlib_uart_putchar(outdev_t *dev, wchar_t ch)
|
---|
| 65 | {
|
---|
[e47ed05] | 66 | grlib_uart_t *uart = (grlib_uart_t *) dev->data;
|
---|
[b77207e] | 67 |
|
---|
| 68 | if ((!uart->parea.mapped) || (console_override)) {
|
---|
| 69 | if (!ascii_check(ch)) {
|
---|
| 70 | grlib_uart_sendb(dev, U_SPECIAL);
|
---|
| 71 | } else {
|
---|
| 72 | if (ch == '\n')
|
---|
| 73 | grlib_uart_sendb(dev, (uint8_t) '\r');
|
---|
[e47ed05] | 74 |
|
---|
[b77207e] | 75 | grlib_uart_sendb(dev, (uint8_t) ch);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | static irq_ownership_t grlib_uart_claim(irq_t *irq)
|
---|
| 81 | {
|
---|
| 82 | return IRQ_ACCEPT;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static void grlib_uart_irq_handler(irq_t *irq)
|
---|
| 86 | {
|
---|
| 87 | grlib_uart_t *uart = irq->instance;
|
---|
[e47ed05] | 88 |
|
---|
| 89 | uint32_t reg = pio_read_32(&uart->io->status);
|
---|
| 90 | grlib_uart_status_t *status = (grlib_uart_status_t *) ®
|
---|
| 91 |
|
---|
[b77207e] | 92 | while (status->dr != 0) {
|
---|
| 93 | uint32_t data = pio_read_32(&uart->io->data);
|
---|
[4d2dba7] | 94 | reg = pio_read_32(&uart->io->status);
|
---|
[e47ed05] | 95 | status = (grlib_uart_status_t *) ®
|
---|
[b77207e] | 96 | indev_push_character(uart->indev, data & 0xff);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | static outdev_operations_t grlib_uart_ops = {
|
---|
| 101 | .write = grlib_uart_putchar,
|
---|
| 102 | .redraw = NULL
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | outdev_t *grlib_uart_init(uintptr_t paddr, inr_t inr)
|
---|
| 106 | {
|
---|
| 107 | outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
---|
| 108 | if (!uart_dev)
|
---|
| 109 | return NULL;
|
---|
[e47ed05] | 110 |
|
---|
| 111 | grlib_uart_t *uart = malloc(sizeof(grlib_uart_t), FRAME_ATOMIC);
|
---|
[b77207e] | 112 | if (!uart) {
|
---|
| 113 | free(uart_dev);
|
---|
| 114 | return NULL;
|
---|
| 115 | }
|
---|
[e47ed05] | 116 |
|
---|
[b77207e] | 117 | outdev_initialize("grlib_uart_dev", uart_dev, &grlib_uart_ops);
|
---|
| 118 | uart_dev->data = uart;
|
---|
[e47ed05] | 119 |
|
---|
[b77207e] | 120 | uart->io = (grlib_uart_io_t *) km_map(paddr, PAGE_SIZE,
|
---|
| 121 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
---|
| 122 | uart->indev = NULL;
|
---|
[e47ed05] | 123 |
|
---|
[b77207e] | 124 | /* Initialize IRQ structure. */
|
---|
| 125 | irq_initialize(&uart->irq);
|
---|
| 126 | uart->irq.devno = device_assign_devno();
|
---|
| 127 | uart->irq.inr = inr;
|
---|
| 128 | uart->irq.claim = grlib_uart_claim;
|
---|
| 129 | uart->irq.handler = grlib_uart_irq_handler;
|
---|
| 130 | uart->irq.instance = uart;
|
---|
[e47ed05] | 131 |
|
---|
[b77207e] | 132 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
---|
[e47ed05] | 133 | grlib_uart_control_t control = {
|
---|
| 134 | .fa = 1,
|
---|
| 135 | .rf = 1,
|
---|
| 136 | .tf = 1,
|
---|
| 137 | .ri = 1,
|
---|
| 138 | .te = 1,
|
---|
| 139 | .re = 1
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | uint32_t *reg = (uint32_t *) &control;
|
---|
[4d2dba7] | 143 | pio_write_32(&uart->io->control, *reg);
|
---|
[e47ed05] | 144 |
|
---|
[b77207e] | 145 | link_initialize(&uart->parea.link);
|
---|
| 146 | uart->parea.pbase = paddr;
|
---|
| 147 | uart->parea.frames = 1;
|
---|
| 148 | uart->parea.unpriv = false;
|
---|
| 149 | uart->parea.mapped = false;
|
---|
| 150 | ddi_parea_register(&uart->parea);
|
---|
| 151 |
|
---|
| 152 | return uart_dev;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void grlib_uart_input_wire(grlib_uart_t *uart, indev_t *indev)
|
---|
| 156 | {
|
---|
| 157 | ASSERT(uart);
|
---|
| 158 | ASSERT(indev);
|
---|
[e47ed05] | 159 |
|
---|
[b77207e] | 160 | uart->indev = indev;
|
---|
| 161 | irq_register(&uart->irq);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /** @}
|
---|
| 165 | */
|
---|