[8f9d70b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[6404aca] | 29 | /** @addtogroup kernel_genarch
|
---|
[8f9d70b] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[c882505] | 34 | * @brief ARM PrimeCell PL011 UART driver.
|
---|
[8f9d70b] | 35 | */
|
---|
| 36 |
|
---|
[63e27ef] | 37 | #include <assert.h>
|
---|
[c882505] | 38 | #include <genarch/drivers/pl011/pl011.h>
|
---|
[8f9d70b] | 39 | #include <console/chardev.h>
|
---|
| 40 | #include <console/console.h>
|
---|
| 41 | #include <arch/asm.h>
|
---|
| 42 | #include <mm/slab.h>
|
---|
| 43 | #include <mm/page.h>
|
---|
| 44 | #include <mm/km.h>
|
---|
| 45 | #include <sysinfo/sysinfo.h>
|
---|
| 46 | #include <str.h>
|
---|
| 47 |
|
---|
| 48 | static void pl011_uart_sendb(pl011_uart_t *uart, uint8_t byte)
|
---|
| 49 | {
|
---|
| 50 | /* Wait for space becoming available in Tx FIFO. */
|
---|
| 51 | // TODO make pio_read accept consts pointers and remove the cast
|
---|
[1433ecda] | 52 | while ((pio_read_32((ioport32_t *)&uart->regs->flag) & PL011_UART_FLAG_TXFF_FLAG) != 0)
|
---|
[8f9d70b] | 53 | ;
|
---|
| 54 |
|
---|
| 55 | pio_write_32(&uart->regs->data, byte);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[39e1b9a] | 58 | static void pl011_uart_write(outdev_t *dev, const char *s, size_t n)
|
---|
[8f9d70b] | 59 | {
|
---|
| 60 | pl011_uart_t *uart = dev->data;
|
---|
| 61 |
|
---|
[84176f3] | 62 | /* If the userspace owns the console, do not output anything. */
|
---|
| 63 | if (uart->parea.mapped && !console_override)
|
---|
| 64 | return;
|
---|
| 65 |
|
---|
[39e1b9a] | 66 | const char *top = s + n;
|
---|
| 67 | assert(top >= s);
|
---|
| 68 |
|
---|
| 69 | for (; s < top; s++) {
|
---|
| 70 | if (*s == '\n')
|
---|
| 71 | pl011_uart_sendb(uart, '\r');
|
---|
| 72 |
|
---|
| 73 | pl011_uart_sendb(uart, (uint8_t) *s);
|
---|
[8f9d70b] | 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static outdev_operations_t pl011_uart_ops = {
|
---|
[39e1b9a] | 78 | .write = pl011_uart_write,
|
---|
[8f9d70b] | 79 | .redraw = NULL,
|
---|
[7ddc2c7] | 80 | .scroll_up = NULL,
|
---|
| 81 | .scroll_down = NULL
|
---|
[8f9d70b] | 82 | };
|
---|
| 83 |
|
---|
| 84 | static irq_ownership_t pl011_uart_claim(irq_t *irq)
|
---|
| 85 | {
|
---|
| 86 | return IRQ_ACCEPT;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static void pl011_uart_irq_handler(irq_t *irq)
|
---|
| 90 | {
|
---|
| 91 | pl011_uart_t *uart = irq->instance;
|
---|
| 92 |
|
---|
| 93 | // TODO make pio_read accept const pointers and remove the cast
|
---|
[1433ecda] | 94 | while ((pio_read_32((ioport32_t *)&uart->regs->flag) & PL011_UART_FLAG_RXFE_FLAG) == 0) {
|
---|
[8f9d70b] | 95 | /* We ignore all error flags here */
|
---|
| 96 | const uint8_t data = pio_read_32(&uart->regs->data);
|
---|
| 97 | if (uart->indev)
|
---|
| 98 | indev_push_character(uart->indev, data);
|
---|
| 99 | }
|
---|
| 100 | /* Ack interrupts */
|
---|
| 101 | pio_write_32(&uart->regs->interrupt_clear, PL011_UART_INTERRUPT_ALL);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[c882505] | 104 | bool pl011_uart_init(pl011_uart_t *uart, inr_t interrupt, uintptr_t addr)
|
---|
[8f9d70b] | 105 | {
|
---|
[63e27ef] | 106 | assert(uart);
|
---|
[1433ecda] | 107 | uart->regs = (void *)km_map(addr, sizeof(pl011_uart_regs_t),
|
---|
[84176f3] | 108 | KM_NATURAL_ALIGNMENT, PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
---|
[63e27ef] | 109 | assert(uart->regs);
|
---|
[8f9d70b] | 110 |
|
---|
[c882505] | 111 | /* Disable UART */
|
---|
[1433ecda] | 112 | uart->regs->control &= ~PL011_UART_CONTROL_UARTEN_FLAG;
|
---|
[8f9d70b] | 113 |
|
---|
[c882505] | 114 | /* Enable hw flow control */
|
---|
| 115 | uart->regs->control |=
|
---|
[1433ecda] | 116 | PL011_UART_CONTROL_RTSE_FLAG |
|
---|
| 117 | PL011_UART_CONTROL_CTSE_FLAG;
|
---|
[8f9d70b] | 118 |
|
---|
| 119 | /* Mask all interrupts */
|
---|
| 120 | uart->regs->interrupt_mask = 0;
|
---|
[c882505] | 121 | /* Clear interrupts */
|
---|
| 122 | uart->regs->interrupt_clear = PL011_UART_INTERRUPT_ALL;
|
---|
| 123 | /* Enable UART, TX and RX */
|
---|
| 124 | uart->regs->control |=
|
---|
[1433ecda] | 125 | PL011_UART_CONTROL_UARTEN_FLAG |
|
---|
| 126 | PL011_UART_CONTROL_TXE_FLAG |
|
---|
| 127 | PL011_UART_CONTROL_RXE_FLAG;
|
---|
[8f9d70b] | 128 |
|
---|
| 129 | outdev_initialize("pl011_uart_dev", &uart->outdev, &pl011_uart_ops);
|
---|
| 130 | uart->outdev.data = uart;
|
---|
| 131 |
|
---|
| 132 | /* Initialize IRQ */
|
---|
| 133 | irq_initialize(&uart->irq);
|
---|
| 134 | uart->irq.inr = interrupt;
|
---|
| 135 | uart->irq.claim = pl011_uart_claim;
|
---|
| 136 | uart->irq.handler = pl011_uart_irq_handler;
|
---|
| 137 | uart->irq.instance = uart;
|
---|
[c882505] | 138 |
|
---|
[84176f3] | 139 | ddi_parea_init(&uart->parea);
|
---|
| 140 | uart->parea.pbase = addr;
|
---|
| 141 | uart->parea.frames = 1;
|
---|
| 142 | uart->parea.unpriv = false;
|
---|
| 143 | uart->parea.mapped = false;
|
---|
| 144 | ddi_parea_register(&uart->parea);
|
---|
| 145 |
|
---|
[8f9d70b] | 146 | return true;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | void pl011_uart_input_wire(pl011_uart_t *uart, indev_t *indev)
|
---|
| 150 | {
|
---|
[63e27ef] | 151 | assert(uart);
|
---|
| 152 | assert(indev);
|
---|
[8f9d70b] | 153 |
|
---|
| 154 | uart->indev = indev;
|
---|
| 155 | irq_register(&uart->irq);
|
---|
[c882505] | 156 | /* Enable receive interrupts */
|
---|
| 157 | uart->regs->interrupt_mask |=
|
---|
[1433ecda] | 158 | PL011_UART_INTERRUPT_RX_FLAG |
|
---|
| 159 | PL011_UART_INTERRUPT_RT_FLAG;
|
---|
[8f9d70b] | 160 | }
|
---|
| 161 |
|
---|
| 162 | /** @}
|
---|
| 163 | */
|
---|