| 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 | /** @addtogroup driver_serial
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Samsung S3C24xx on-chip UART driver.
|
|---|
| 35 | *
|
|---|
| 36 | * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <ddi.h>
|
|---|
| 40 | #include <libarch/ddi.h>
|
|---|
| 41 | #include <devmap.h>
|
|---|
| 42 | #include <ipc/ipc.h>
|
|---|
| 43 | #include <ipc/char.h>
|
|---|
| 44 | #include <async.h>
|
|---|
| 45 | #include <unistd.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 | #include <stdlib.h>
|
|---|
| 48 | #include <sysinfo.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <inttypes.h>
|
|---|
| 51 |
|
|---|
| 52 | #include "s3c24xx_uart.h"
|
|---|
| 53 |
|
|---|
| 54 | #define NAME "s3c24ser"
|
|---|
| 55 | #define NAMESPACE "char"
|
|---|
| 56 |
|
|---|
| 57 | static irq_cmd_t uart_irq_cmds[] = {
|
|---|
| 58 | {
|
|---|
| 59 | .cmd = CMD_ACCEPT
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | static irq_code_t uart_irq_code = {
|
|---|
| 64 | sizeof(uart_irq_cmds) / sizeof(irq_cmd_t),
|
|---|
| 65 | uart_irq_cmds
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | /** S3C24xx UART instance structure */
|
|---|
| 69 | static s3c24xx_uart_t *uart;
|
|---|
| 70 |
|
|---|
| 71 | static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 72 | static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
|---|
| 73 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart);
|
|---|
| 74 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte);
|
|---|
| 75 |
|
|---|
| 76 | int main(int argc, char *argv[])
|
|---|
| 77 | {
|
|---|
| 78 | int rc;
|
|---|
| 79 |
|
|---|
| 80 | printf(NAME ": S3C24xx on-chip UART driver\n");
|
|---|
| 81 |
|
|---|
| 82 | rc = devmap_driver_register(NAME, s3c24xx_uart_connection);
|
|---|
| 83 | if (rc < 0) {
|
|---|
| 84 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 85 | return -1;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | uart = malloc(sizeof(s3c24xx_uart_t));
|
|---|
| 89 | if (uart == NULL)
|
|---|
| 90 | return -1;
|
|---|
| 91 |
|
|---|
| 92 | if (s3c24xx_uart_init(uart) != EOK)
|
|---|
| 93 | return -1;
|
|---|
| 94 |
|
|---|
| 95 | rc = devmap_device_register(NAMESPACE "/" NAME, &uart->devmap_handle);
|
|---|
| 96 | if (rc != EOK) {
|
|---|
| 97 | devmap_hangup_phone(DEVMAP_DRIVER);
|
|---|
| 98 | printf(NAME ": Unable to register device %s.\n",
|
|---|
| 99 | NAMESPACE "/" NAME);
|
|---|
| 100 | return -1;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | printf(NAME ": Registered device %s.\n", NAMESPACE "/" NAME);
|
|---|
| 104 |
|
|---|
| 105 | printf(NAME ": Accepting connections\n");
|
|---|
| 106 | task_retval(0);
|
|---|
| 107 | async_manager();
|
|---|
| 108 |
|
|---|
| 109 | /* Not reached */
|
|---|
| 110 | return 0;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Character device connection handler. */
|
|---|
| 114 | static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 115 | {
|
|---|
| 116 | ipc_callid_t callid;
|
|---|
| 117 | ipc_call_t call;
|
|---|
| 118 | sysarg_t method;
|
|---|
| 119 | int retval;
|
|---|
| 120 |
|
|---|
| 121 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 122 | ipc_answer_0(iid, EOK);
|
|---|
| 123 |
|
|---|
| 124 | while (1) {
|
|---|
| 125 | callid = async_get_call(&call);
|
|---|
| 126 | method = IPC_GET_IMETHOD(call);
|
|---|
| 127 | switch (method) {
|
|---|
| 128 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 129 | /* The other side has hung up. */
|
|---|
| 130 | ipc_answer_0(callid, EOK);
|
|---|
| 131 | return;
|
|---|
| 132 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 133 | printf(NAME ": creating callback connection\n");
|
|---|
| 134 | uart->client_phone = IPC_GET_ARG5(call);
|
|---|
| 135 | retval = 0;
|
|---|
| 136 | break;
|
|---|
| 137 | case CHAR_WRITE_BYTE:
|
|---|
| 138 | printf(NAME ": write %" PRIun " to device\n",
|
|---|
| 139 | IPC_GET_ARG1(call));
|
|---|
| 140 | s3c24xx_uart_sendb(uart, (uint8_t) IPC_GET_ARG1(call));
|
|---|
| 141 | retval = 0;
|
|---|
| 142 | break;
|
|---|
| 143 | default:
|
|---|
| 144 | retval = EINVAL;
|
|---|
| 145 | break;
|
|---|
| 146 | }
|
|---|
| 147 | ipc_answer_0(callid, retval);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 152 | {
|
|---|
| 153 | (void) iid; (void) call;
|
|---|
| 154 |
|
|---|
| 155 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
|
|---|
| 156 | uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
|
|---|
| 157 | uint32_t status = pio_read_32(&uart->io->uerstat);
|
|---|
| 158 |
|
|---|
| 159 | if (uart->client_phone != -1) {
|
|---|
| 160 | async_msg_1(uart->client_phone, CHAR_NOTIF_BYTE,
|
|---|
| 161 | data);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (status != 0)
|
|---|
| 165 | printf(NAME ": Error status 0x%x\n", status);
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Initialize S3C24xx on-chip UART. */
|
|---|
| 170 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
|
|---|
| 171 | {
|
|---|
| 172 | void *vaddr;
|
|---|
| 173 | sysarg_t inr;
|
|---|
| 174 |
|
|---|
| 175 | if (sysinfo_get_value("s3c24xx_uart.address.physical",
|
|---|
| 176 | &uart->paddr) != EOK)
|
|---|
| 177 | return -1;
|
|---|
| 178 |
|
|---|
| 179 | if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
|
|---|
| 180 | &vaddr) != 0)
|
|---|
| 181 | return -1;
|
|---|
| 182 |
|
|---|
| 183 | if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
|
|---|
| 184 | return -1;
|
|---|
| 185 |
|
|---|
| 186 | uart->io = vaddr;
|
|---|
| 187 | uart->client_phone = -1;
|
|---|
| 188 |
|
|---|
| 189 | printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
|
|---|
| 190 | (void *) uart->paddr, inr);
|
|---|
| 191 |
|
|---|
| 192 | async_set_interrupt_received(s3c24xx_uart_irq_handler);
|
|---|
| 193 |
|
|---|
| 194 | ipc_register_irq(inr, device_assign_devno(), 0, &uart_irq_code);
|
|---|
| 195 |
|
|---|
| 196 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
|---|
| 197 | pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
|
|---|
| 198 | UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
|
|---|
| 199 |
|
|---|
| 200 | /* Set RX interrupt to pulse mode */
|
|---|
| 201 | pio_write_32(&uart->io->ucon,
|
|---|
| 202 | pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
|
|---|
| 203 |
|
|---|
| 204 | return EOK;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /** Send a byte to the UART. */
|
|---|
| 208 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
|
|---|
| 209 | {
|
|---|
| 210 | /* Wait for space becoming available in Tx FIFO. */
|
|---|
| 211 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
|
|---|
| 212 | ;
|
|---|
| 213 |
|
|---|
| 214 | pio_write_32(&uart->io->utxh, byte);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** @}
|
|---|
| 218 | */
|
|---|