[b38c079] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
---|
| 3 | * Copyright (c) 2013 Maurizio Lombardi
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
[6404aca] | 29 | /** @addtogroup kernel_genarch
|
---|
[b38c079] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Texas Instruments OMAP on-chip uart serial line driver.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[63e27ef] | 37 | #include <assert.h>
|
---|
[b38c079] | 38 | #include <genarch/drivers/omap/uart.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <mm/km.h>
|
---|
| 41 |
|
---|
| 42 | static void omap_uart_txb(omap_uart_t *uart, uint8_t b)
|
---|
| 43 | {
|
---|
| 44 | /* Wait for buffer */
|
---|
[1433ecda] | 45 | while (uart->regs->ssr & OMAP_UART_SSR_TX_FIFO_FULL_FLAG)
|
---|
| 46 | ;
|
---|
[b38c079] | 47 | /* Write to the outgoing fifo */
|
---|
| 48 | uart->regs->thr = b;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[e9bc927] | 51 | static void omap_uart_write(outdev_t *dev, const char *s, size_t n)
|
---|
[b38c079] | 52 | {
|
---|
| 53 | omap_uart_t *uart = dev->data;
|
---|
[e9bc927] | 54 |
|
---|
| 55 | const char *top = s + n;
|
---|
| 56 | assert(top >= s);
|
---|
| 57 |
|
---|
| 58 | for (; s < top; s++) {
|
---|
| 59 | if (*s == '\n')
|
---|
[b38c079] | 60 | omap_uart_txb(uart, '\r');
|
---|
[e9bc927] | 61 |
|
---|
| 62 | omap_uart_txb(uart, (uint8_t) *s);
|
---|
[b38c079] | 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | static outdev_operations_t omap_uart_ops = {
|
---|
[e9bc927] | 67 | .write = omap_uart_write,
|
---|
[7ddc2c7] | 68 | .redraw = NULL,
|
---|
| 69 | .scroll_up = NULL,
|
---|
| 70 | .scroll_down = NULL
|
---|
[b38c079] | 71 | };
|
---|
| 72 |
|
---|
| 73 | static irq_ownership_t omap_uart_claim(irq_t *irq)
|
---|
| 74 | {
|
---|
| 75 | return IRQ_ACCEPT;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static void omap_uart_handler(irq_t *irq)
|
---|
| 79 | {
|
---|
| 80 | omap_uart_t *uart = irq->instance;
|
---|
| 81 | while ((uart->regs->rx_fifo_lvl)) {
|
---|
| 82 | const uint8_t val = uart->regs->rhr;
|
---|
| 83 | if (uart->indev && val) {
|
---|
| 84 | indev_push_character(uart->indev, val);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | bool omap_uart_init(
|
---|
| 90 | omap_uart_t *uart, inr_t interrupt, uintptr_t addr, size_t size)
|
---|
| 91 | {
|
---|
[63e27ef] | 92 | assert(uart);
|
---|
[a1b9f63] | 93 | uart->regs = (void *)km_map(addr, size, KM_NATURAL_ALIGNMENT,
|
---|
| 94 | PAGE_NOT_CACHEABLE);
|
---|
[b38c079] | 95 |
|
---|
[63e27ef] | 96 | assert(uart->regs);
|
---|
[b38c079] | 97 |
|
---|
| 98 | /* Soft reset the port */
|
---|
| 99 | uart->regs->sysc = OMAP_UART_SYSC_SOFTRESET_FLAG;
|
---|
[1433ecda] | 100 | while (!(uart->regs->syss & OMAP_UART_SYSS_RESETDONE_FLAG))
|
---|
| 101 | ;
|
---|
[b38c079] | 102 |
|
---|
| 103 | /* Disable the UART module */
|
---|
| 104 | uart->regs->mdr1 |= OMAP_UART_MDR_MS_DISABLE;
|
---|
| 105 |
|
---|
| 106 | /* Enable access to EFR register */
|
---|
| 107 | uart->regs->lcr = 0xbf; /* Sets config mode B */
|
---|
| 108 |
|
---|
| 109 | /* Enable access to TCL_TLR register */
|
---|
| 110 | const bool enhanced = uart->regs->efr & OMAP_UART_EFR_ENH_FLAG;
|
---|
| 111 | uart->regs->efr |= OMAP_UART_EFR_ENH_FLAG; /* Turn on enh. */
|
---|
| 112 | uart->regs->lcr = 0x80; /* Config mode A */
|
---|
| 113 |
|
---|
| 114 | /* Set default (val 0) triggers, disable DMA enable FIFOs */
|
---|
| 115 | const bool tcl_tlr = uart->regs->mcr & OMAP_UART_MCR_TCR_TLR_FLAG;
|
---|
| 116 | /* Enable access to tcr and tlr registers */
|
---|
| 117 | uart->regs->mcr |= OMAP_UART_MCR_TCR_TLR_FLAG;
|
---|
| 118 |
|
---|
| 119 | /* Enable FIFOs */
|
---|
| 120 | uart->regs->fcr = OMAP_UART_FCR_FIFO_EN_FLAG;
|
---|
| 121 |
|
---|
[7c3fb9b] | 122 | /*
|
---|
| 123 | * Enable fine granularity for RX FIFO and set trigger level to 1,
|
---|
| 124 | * TX FIFO, trigger level is irrelevant
|
---|
| 125 | */
|
---|
[b38c079] | 126 | uart->regs->lcr = 0xBF; /* Sets config mode B */
|
---|
| 127 | uart->regs->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_FLAG;
|
---|
| 128 | uart->regs->tlr = 1 << OMAP_UART_TLR_RX_FIFO_TRIG_SHIFT;
|
---|
| 129 |
|
---|
| 130 | /* Sets config mode A */
|
---|
| 131 | uart->regs->lcr = 0x80;
|
---|
| 132 | /* Restore tcl_tlr access flag */
|
---|
| 133 | if (!tcl_tlr)
|
---|
| 134 | uart->regs->mcr &= ~OMAP_UART_MCR_TCR_TLR_FLAG;
|
---|
| 135 | /* Sets config mode B */
|
---|
| 136 | uart->regs->lcr = 0xBF;
|
---|
| 137 |
|
---|
| 138 | /* Set the divisor value to get a baud rate of 115200 bps */
|
---|
| 139 | uart->regs->dll = 0x1A;
|
---|
| 140 | uart->regs->dlh = 0x00;
|
---|
| 141 |
|
---|
| 142 | /* Restore enhanced */
|
---|
| 143 | if (!enhanced)
|
---|
| 144 | uart->regs->efr &= ~OMAP_UART_EFR_ENH_FLAG;
|
---|
| 145 |
|
---|
| 146 | /* Set the DIV_EN bit to 0 */
|
---|
| 147 | uart->regs->lcr &= ~OMAP_UART_LCR_DIV_EN_FLAG;
|
---|
| 148 | /* Set the BREAK_EN bit to 0 */
|
---|
| 149 | uart->regs->lcr &= ~OMAP_UART_LCR_BREAK_EN_FLAG;
|
---|
| 150 | /* No parity */
|
---|
| 151 | uart->regs->lcr &= ~OMAP_UART_LCR_PARITY_EN_FLAG;
|
---|
| 152 | /* Stop = 1 bit */
|
---|
| 153 | uart->regs->lcr &= ~OMAP_UART_LCR_NB_STOP_FLAG;
|
---|
| 154 | /* Char length = 8 bits */
|
---|
| 155 | uart->regs->lcr |= OMAP_UART_LCR_CHAR_LENGTH_8BITS;
|
---|
| 156 |
|
---|
| 157 | /* Enable the UART module */
|
---|
| 158 | uart->regs->mdr1 &= (OMAP_UART_MDR_MS_UART16 &
|
---|
| 159 | ~OMAP_UART_MDR_MS_MASK);
|
---|
| 160 |
|
---|
| 161 | /* Disable interrupts */
|
---|
| 162 | uart->regs->ier = 0;
|
---|
| 163 |
|
---|
| 164 | /* Setup outdev */
|
---|
| 165 | outdev_initialize("omap_uart_dev", &uart->outdev, &omap_uart_ops);
|
---|
| 166 | uart->outdev.data = uart;
|
---|
| 167 |
|
---|
| 168 | /* Initialize IRQ */
|
---|
| 169 | irq_initialize(&uart->irq);
|
---|
| 170 | uart->irq.inr = interrupt;
|
---|
| 171 | uart->irq.claim = omap_uart_claim;
|
---|
| 172 | uart->irq.handler = omap_uart_handler;
|
---|
| 173 | uart->irq.instance = uart;
|
---|
| 174 |
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void omap_uart_input_wire(omap_uart_t *uart, indev_t *indev)
|
---|
| 179 | {
|
---|
[63e27ef] | 180 | assert(uart);
|
---|
[b38c079] | 181 | /* Set indev */
|
---|
| 182 | uart->indev = indev;
|
---|
| 183 | /* Register interrupt. */
|
---|
| 184 | irq_register(&uart->irq);
|
---|
| 185 | /* Enable interrupt on receive */
|
---|
| 186 | uart->regs->ier |= OMAP_UART_IER_RHR_IRQ_FLAG;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * @}
|
---|
| 191 | */
|
---|