| 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 |
|
|---|
| 39 | #include <genarch/drivers/grlib/uart.h>
|
|---|
| 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;
|
|---|
| 53 | grlib_uart_t *uart = (grlib_uart_t *) dev->data;
|
|---|
| 54 | uint32_t reg;
|
|---|
| 55 |
|
|---|
| 56 | /* Wait for space becoming available in Tx FIFO. */
|
|---|
| 57 | do {
|
|---|
| 58 | reg = pio_read_32(&uart->io->status);
|
|---|
| 59 | status = (grlib_uart_status_t *) ®
|
|---|
| 60 | } while (status->tf != 0);
|
|---|
| 61 |
|
|---|
| 62 | pio_write_32(&uart->io->data, byte);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | static void grlib_uart_putchar(outdev_t *dev, wchar_t ch)
|
|---|
| 66 | {
|
|---|
| 67 | grlib_uart_t *uart = (grlib_uart_t *) dev->data;
|
|---|
| 68 |
|
|---|
| 69 | if ((!uart->parea.mapped) || (console_override)) {
|
|---|
| 70 | if (!ascii_check(ch)) {
|
|---|
| 71 | grlib_uart_sendb(dev, U_SPECIAL);
|
|---|
| 72 | } else {
|
|---|
| 73 | if (ch == '\n')
|
|---|
| 74 | grlib_uart_sendb(dev, (uint8_t) '\r');
|
|---|
| 75 |
|
|---|
| 76 | grlib_uart_sendb(dev, (uint8_t) ch);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static irq_ownership_t grlib_uart_claim(irq_t *irq)
|
|---|
| 82 | {
|
|---|
| 83 | return IRQ_ACCEPT;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | static void grlib_uart_irq_handler(irq_t *irq)
|
|---|
| 87 | {
|
|---|
| 88 | grlib_uart_t *uart = irq->instance;
|
|---|
| 89 |
|
|---|
| 90 | uint32_t reg = pio_read_32(&uart->io->status);
|
|---|
| 91 | grlib_uart_status_t *status = (grlib_uart_status_t *) ®
|
|---|
| 92 |
|
|---|
| 93 | while (status->dr != 0) {
|
|---|
| 94 | uint32_t data = pio_read_32(&uart->io->data);
|
|---|
| 95 | reg = pio_read_32(&uart->io->status);
|
|---|
| 96 | status = (grlib_uart_status_t *) ®
|
|---|
| 97 | indev_push_character(uart->indev, data & 0xff);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static outdev_operations_t grlib_uart_ops = {
|
|---|
| 102 | .write = grlib_uart_putchar,
|
|---|
| 103 | .redraw = NULL,
|
|---|
| 104 | .scroll_up = NULL,
|
|---|
| 105 | .scroll_down = NULL
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | outdev_t *grlib_uart_init(uintptr_t paddr, inr_t inr)
|
|---|
| 109 | {
|
|---|
| 110 | outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
|---|
| 111 | if (!uart_dev)
|
|---|
| 112 | return NULL;
|
|---|
| 113 |
|
|---|
| 114 | grlib_uart_t *uart = malloc(sizeof(grlib_uart_t), FRAME_ATOMIC);
|
|---|
| 115 | if (!uart) {
|
|---|
| 116 | free(uart_dev);
|
|---|
| 117 | return NULL;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | outdev_initialize("grlib_uart_dev", uart_dev, &grlib_uart_ops);
|
|---|
| 121 | uart_dev->data = uart;
|
|---|
| 122 |
|
|---|
| 123 | uart->io = (grlib_uart_io_t *) km_map(paddr, PAGE_SIZE,
|
|---|
| 124 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
|---|
| 125 | uart->indev = NULL;
|
|---|
| 126 |
|
|---|
| 127 | /* Initialize IRQ structure. */
|
|---|
| 128 | irq_initialize(&uart->irq);
|
|---|
| 129 | uart->irq.devno = device_assign_devno();
|
|---|
| 130 | uart->irq.inr = inr;
|
|---|
| 131 | uart->irq.claim = grlib_uart_claim;
|
|---|
| 132 | uart->irq.handler = grlib_uart_irq_handler;
|
|---|
| 133 | uart->irq.instance = uart;
|
|---|
| 134 |
|
|---|
| 135 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
|---|
| 136 | grlib_uart_control_t control = {
|
|---|
| 137 | .fa = 1,
|
|---|
| 138 | .rf = 1,
|
|---|
| 139 | .tf = 1,
|
|---|
| 140 | .ri = 1,
|
|---|
| 141 | .te = 1,
|
|---|
| 142 | .re = 1
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | uint32_t *reg = (uint32_t *) &control;
|
|---|
| 146 | pio_write_32(&uart->io->control, *reg);
|
|---|
| 147 |
|
|---|
| 148 | link_initialize(&uart->parea.link);
|
|---|
| 149 | uart->parea.pbase = paddr;
|
|---|
| 150 | uart->parea.frames = 1;
|
|---|
| 151 | uart->parea.unpriv = false;
|
|---|
| 152 | uart->parea.mapped = false;
|
|---|
| 153 | ddi_parea_register(&uart->parea);
|
|---|
| 154 |
|
|---|
| 155 | return uart_dev;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | void grlib_uart_input_wire(grlib_uart_t *uart, indev_t *indev)
|
|---|
| 159 | {
|
|---|
| 160 | ASSERT(uart);
|
|---|
| 161 | ASSERT(indev);
|
|---|
| 162 |
|
|---|
| 163 | uart->indev = indev;
|
|---|
| 164 | irq_register(&uart->irq);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** @}
|
|---|
| 168 | */
|
|---|