| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Martin Decky
|
|---|
| 3 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 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 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup genarch
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Samsung S3C24xx on-chip UART driver.
|
|---|
| 36 | *
|
|---|
| 37 | * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <genarch/drivers/s3c24xx_uart/s3c24xx_uart.h>
|
|---|
| 41 | #include <console/chardev.h>
|
|---|
| 42 | #include <console/console.h>
|
|---|
| 43 | #include <ddi/device.h>
|
|---|
| 44 | #include <arch/asm.h>
|
|---|
| 45 | #include <mm/slab.h>
|
|---|
| 46 | #include <mm/page.h>
|
|---|
| 47 | #include <mm/km.h>
|
|---|
| 48 | #include <sysinfo/sysinfo.h>
|
|---|
| 49 | #include <str.h>
|
|---|
| 50 |
|
|---|
| 51 | static void s3c24xx_uart_sendb(outdev_t *dev, uint8_t byte)
|
|---|
| 52 | {
|
|---|
| 53 | s3c24xx_uart_t *uart =
|
|---|
| 54 | (s3c24xx_uart_t *) dev->data;
|
|---|
| 55 |
|
|---|
| 56 | /* Wait for space becoming available in Tx FIFO. */
|
|---|
| 57 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
|
|---|
| 58 | ;
|
|---|
| 59 |
|
|---|
| 60 | pio_write_32(&uart->io->utxh, byte);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | static void s3c24xx_uart_putchar(outdev_t *dev, wchar_t ch)
|
|---|
| 64 | {
|
|---|
| 65 | s3c24xx_uart_t *uart =
|
|---|
| 66 | (s3c24xx_uart_t *) dev->data;
|
|---|
| 67 |
|
|---|
| 68 | if ((!uart->parea.mapped) || (console_override)) {
|
|---|
| 69 | if (!ascii_check(ch)) {
|
|---|
| 70 | s3c24xx_uart_sendb(dev, U_SPECIAL);
|
|---|
| 71 | } else {
|
|---|
| 72 | if (ch == '\n')
|
|---|
| 73 | s3c24xx_uart_sendb(dev, (uint8_t) '\r');
|
|---|
| 74 | s3c24xx_uart_sendb(dev, (uint8_t) ch);
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | static irq_ownership_t s3c24xx_uart_claim(irq_t *irq)
|
|---|
| 80 | {
|
|---|
| 81 | return IRQ_ACCEPT;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static void s3c24xx_uart_irq_handler(irq_t *irq)
|
|---|
| 85 | {
|
|---|
| 86 | s3c24xx_uart_t *uart = irq->instance;
|
|---|
| 87 |
|
|---|
| 88 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
|
|---|
| 89 | uint32_t data = pio_read_32(&uart->io->urxh);
|
|---|
| 90 | pio_read_32(&uart->io->uerstat);
|
|---|
| 91 | indev_push_character(uart->indev, data & 0xff);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static outdev_operations_t s3c24xx_uart_ops = {
|
|---|
| 96 | .write = s3c24xx_uart_putchar,
|
|---|
| 97 | .redraw = NULL
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | outdev_t *s3c24xx_uart_init(uintptr_t paddr, inr_t inr)
|
|---|
| 101 | {
|
|---|
| 102 | outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
|---|
| 103 | if (!uart_dev)
|
|---|
| 104 | return NULL;
|
|---|
| 105 |
|
|---|
| 106 | s3c24xx_uart_t *uart =
|
|---|
| 107 | malloc(sizeof(s3c24xx_uart_t), FRAME_ATOMIC);
|
|---|
| 108 | if (!uart) {
|
|---|
| 109 | free(uart_dev);
|
|---|
| 110 | return NULL;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | outdev_initialize("s3c24xx_uart_dev", uart_dev, &s3c24xx_uart_ops);
|
|---|
| 114 | uart_dev->data = uart;
|
|---|
| 115 |
|
|---|
| 116 | uart->io = (s3c24xx_uart_io_t *) km_map(paddr, PAGE_SIZE,
|
|---|
| 117 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
|---|
| 118 | uart->indev = NULL;
|
|---|
| 119 |
|
|---|
| 120 | /* Initialize IRQ structure. */
|
|---|
| 121 | irq_initialize(&uart->irq);
|
|---|
| 122 | uart->irq.devno = device_assign_devno();
|
|---|
| 123 | uart->irq.inr = inr;
|
|---|
| 124 | uart->irq.claim = s3c24xx_uart_claim;
|
|---|
| 125 | uart->irq.handler = s3c24xx_uart_irq_handler;
|
|---|
| 126 | uart->irq.instance = uart;
|
|---|
| 127 |
|
|---|
| 128 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
|---|
| 129 | pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
|
|---|
| 130 | UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
|
|---|
| 131 |
|
|---|
| 132 | /* Set RX interrupt to pulse mode */
|
|---|
| 133 | pio_write_32(&uart->io->ucon,
|
|---|
| 134 | pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
|
|---|
| 135 |
|
|---|
| 136 | link_initialize(&uart->parea.link);
|
|---|
| 137 | uart->parea.pbase = paddr;
|
|---|
| 138 | uart->parea.frames = 1;
|
|---|
| 139 | uart->parea.unpriv = false;
|
|---|
| 140 | uart->parea.mapped = false;
|
|---|
| 141 | ddi_parea_register(&uart->parea);
|
|---|
| 142 |
|
|---|
| 143 | if (!fb_exported) {
|
|---|
| 144 | /*
|
|---|
| 145 | * This is the necessary evil until
|
|---|
| 146 | * the userspace driver is entirely
|
|---|
| 147 | * self-sufficient.
|
|---|
| 148 | */
|
|---|
| 149 | sysinfo_set_item_val("fb", NULL, true);
|
|---|
| 150 | sysinfo_set_item_val("fb.kind", NULL, 3);
|
|---|
| 151 | sysinfo_set_item_val("fb.address.physical", NULL, paddr);
|
|---|
| 152 |
|
|---|
| 153 | fb_exported = true;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | return uart_dev;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | void s3c24xx_uart_input_wire(s3c24xx_uart_t *uart, indev_t *indev)
|
|---|
| 160 | {
|
|---|
| 161 | ASSERT(uart);
|
|---|
| 162 | ASSERT(indev);
|
|---|
| 163 |
|
|---|
| 164 | uart->indev = indev;
|
|---|
| 165 | irq_register(&uart->irq);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /** @}
|
|---|
| 169 | */
|
|---|