| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 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 |
|
|---|
| 29 | /**
|
|---|
| 30 | * @file
|
|---|
| 31 | * @brief Samsung S3C24xx on-chip UART driver.
|
|---|
| 32 | *
|
|---|
| 33 | * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <async.h>
|
|---|
| 37 | #include <ddi.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <inttypes.h>
|
|---|
| 40 | #include <io/chardev_srv.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <sysinfo.h>
|
|---|
| 45 | #include "s3c24xx_uart.h"
|
|---|
| 46 |
|
|---|
| 47 | #define NAME "s3c24ser"
|
|---|
| 48 | #define NAMESPACE "char"
|
|---|
| 49 |
|
|---|
| 50 | static irq_cmd_t uart_irq_cmds[] = {
|
|---|
| 51 | {
|
|---|
| 52 | .cmd = CMD_ACCEPT
|
|---|
| 53 | }
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static irq_code_t uart_irq_code = {
|
|---|
| 57 | 0,
|
|---|
| 58 | NULL,
|
|---|
| 59 | sizeof(uart_irq_cmds) / sizeof(irq_cmd_t),
|
|---|
| 60 | uart_irq_cmds
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /** S3C24xx UART instance structure */
|
|---|
| 64 | static s3c24xx_uart_t *uart;
|
|---|
| 65 |
|
|---|
| 66 | static void s3c24xx_uart_connection(ipc_call_t *, void *);
|
|---|
| 67 | static void s3c24xx_uart_irq_handler(ipc_call_t *, void *);
|
|---|
| 68 | static int s3c24xx_uart_init(s3c24xx_uart_t *);
|
|---|
| 69 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *, uint8_t);
|
|---|
| 70 |
|
|---|
| 71 | static errno_t s3c24xx_uart_read(chardev_srv_t *, void *, size_t, size_t *,
|
|---|
| 72 | chardev_flags_t);
|
|---|
| 73 | static errno_t s3c24xx_uart_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 74 |
|
|---|
| 75 | static chardev_ops_t s3c24xx_uart_chardev_ops = {
|
|---|
| 76 | .read = s3c24xx_uart_read,
|
|---|
| 77 | .write = s3c24xx_uart_write
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | int main(int argc, char *argv[])
|
|---|
| 81 | {
|
|---|
| 82 | printf("%s: S3C24xx on-chip UART driver\n", NAME);
|
|---|
| 83 |
|
|---|
| 84 | async_set_fallback_port_handler(s3c24xx_uart_connection, uart);
|
|---|
| 85 | errno_t rc = loc_server_register(NAME);
|
|---|
| 86 | if (rc != EOK) {
|
|---|
| 87 | printf("%s: Unable to register server.\n", NAME);
|
|---|
| 88 | return rc;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | uart = malloc(sizeof(s3c24xx_uart_t));
|
|---|
| 92 | if (uart == NULL)
|
|---|
| 93 | return -1;
|
|---|
| 94 |
|
|---|
| 95 | if (s3c24xx_uart_init(uart) != EOK)
|
|---|
| 96 | return -1;
|
|---|
| 97 |
|
|---|
| 98 | rc = loc_service_register(NAMESPACE "/" NAME, &uart->service_id);
|
|---|
| 99 | if (rc != EOK) {
|
|---|
| 100 | printf(NAME ": Unable to register device %s.\n",
|
|---|
| 101 | NAMESPACE "/" NAME);
|
|---|
| 102 | return -1;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | printf(NAME ": Registered device %s.\n", NAMESPACE "/" NAME);
|
|---|
| 106 |
|
|---|
| 107 | printf(NAME ": Accepting connections\n");
|
|---|
| 108 | task_retval(0);
|
|---|
| 109 | async_manager();
|
|---|
| 110 |
|
|---|
| 111 | /* Not reached */
|
|---|
| 112 | return 0;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Character device connection handler. */
|
|---|
| 116 | static void s3c24xx_uart_connection(ipc_call_t *icall, void *arg)
|
|---|
| 117 | {
|
|---|
| 118 | s3c24xx_uart_t *uart = (s3c24xx_uart_t *) arg;
|
|---|
| 119 |
|
|---|
| 120 | chardev_conn(icall, &uart->cds);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | static void s3c24xx_uart_irq_handler(ipc_call_t *call, void *arg)
|
|---|
| 124 | {
|
|---|
| 125 | errno_t rc;
|
|---|
| 126 |
|
|---|
| 127 | (void) call;
|
|---|
| 128 | (void) arg;
|
|---|
| 129 |
|
|---|
| 130 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
|
|---|
| 131 | uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
|
|---|
| 132 | uint32_t status = pio_read_32(&uart->io->uerstat);
|
|---|
| 133 |
|
|---|
| 134 | fibril_mutex_lock(&uart->buf_lock);
|
|---|
| 135 |
|
|---|
| 136 | rc = circ_buf_push(&uart->cbuf, &data);
|
|---|
| 137 | if (rc != EOK)
|
|---|
| 138 | printf(NAME ": Buffer overrun\n");
|
|---|
| 139 |
|
|---|
| 140 | fibril_mutex_unlock(&uart->buf_lock);
|
|---|
| 141 | fibril_condvar_broadcast(&uart->buf_cv);
|
|---|
| 142 |
|
|---|
| 143 | if (status != 0)
|
|---|
| 144 | printf(NAME ": Error status 0x%x\n", status);
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /** Initialize S3C24xx on-chip UART. */
|
|---|
| 149 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
|
|---|
| 150 | {
|
|---|
| 151 | void *vaddr;
|
|---|
| 152 | sysarg_t inr;
|
|---|
| 153 |
|
|---|
| 154 | circ_buf_init(&uart->cbuf, uart->buf, s3c24xx_uart_buf_size, 1);
|
|---|
| 155 | fibril_mutex_initialize(&uart->buf_lock);
|
|---|
| 156 | fibril_condvar_initialize(&uart->buf_cv);
|
|---|
| 157 |
|
|---|
| 158 | if (sysinfo_get_value("s3c24xx_uart.address.physical",
|
|---|
| 159 | &uart->paddr) != EOK)
|
|---|
| 160 | return -1;
|
|---|
| 161 |
|
|---|
| 162 | if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
|
|---|
| 163 | &vaddr) != 0)
|
|---|
| 164 | return -1;
|
|---|
| 165 |
|
|---|
| 166 | if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
|
|---|
| 167 | return -1;
|
|---|
| 168 |
|
|---|
| 169 | uart->io = vaddr;
|
|---|
| 170 |
|
|---|
| 171 | printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
|
|---|
| 172 | (void *) uart->paddr, inr);
|
|---|
| 173 |
|
|---|
| 174 | async_irq_subscribe(inr, s3c24xx_uart_irq_handler, NULL, &uart_irq_code, NULL);
|
|---|
| 175 |
|
|---|
| 176 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
|---|
| 177 | pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
|
|---|
| 178 | UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
|
|---|
| 179 |
|
|---|
| 180 | /* Set RX interrupt to pulse mode */
|
|---|
| 181 | pio_write_32(&uart->io->ucon,
|
|---|
| 182 | pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
|
|---|
| 183 |
|
|---|
| 184 | chardev_srvs_init(&uart->cds);
|
|---|
| 185 | uart->cds.ops = &s3c24xx_uart_chardev_ops;
|
|---|
| 186 | uart->cds.sarg = uart;
|
|---|
| 187 |
|
|---|
| 188 | return EOK;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** Send a byte to the UART. */
|
|---|
| 192 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
|
|---|
| 193 | {
|
|---|
| 194 | /* Wait for space becoming available in Tx FIFO. */
|
|---|
| 195 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
|
|---|
| 196 | ;
|
|---|
| 197 |
|
|---|
| 198 | pio_write_32(&uart->io->utxh, byte);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | static errno_t s3c24xx_uart_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 202 | size_t *nread, chardev_flags_t flags)
|
|---|
| 203 | {
|
|---|
| 204 | s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg;
|
|---|
| 205 | size_t p;
|
|---|
| 206 | uint8_t *bp = (uint8_t *) buf;
|
|---|
| 207 | errno_t rc;
|
|---|
| 208 |
|
|---|
| 209 | fibril_mutex_lock(&uart->buf_lock);
|
|---|
| 210 |
|
|---|
| 211 | while ((flags & chardev_f_nonblock) == 0 &&
|
|---|
| 212 | circ_buf_nused(&uart->cbuf) == 0)
|
|---|
| 213 | fibril_condvar_wait(&uart->buf_cv, &uart->buf_lock);
|
|---|
| 214 |
|
|---|
| 215 | p = 0;
|
|---|
| 216 | while (p < size) {
|
|---|
| 217 | rc = circ_buf_pop(&uart->cbuf, &bp[p]);
|
|---|
| 218 | if (rc != EOK)
|
|---|
| 219 | break;
|
|---|
| 220 | ++p;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | fibril_mutex_unlock(&uart->buf_lock);
|
|---|
| 224 |
|
|---|
| 225 | *nread = p;
|
|---|
| 226 | return EOK;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | static errno_t s3c24xx_uart_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 230 | size_t *nwr)
|
|---|
| 231 | {
|
|---|
| 232 | s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg;
|
|---|
| 233 | size_t i;
|
|---|
| 234 | uint8_t *dp = (uint8_t *) data;
|
|---|
| 235 |
|
|---|
| 236 | for (i = 0; i < size; i++)
|
|---|
| 237 | s3c24xx_uart_sendb(uart, dp[i]);
|
|---|
| 238 |
|
|---|
| 239 | *nwr = size;
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** @}
|
|---|
| 244 | */
|
|---|