| 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 |
|
|---|
| 51 | #include "s3c24xx_uart.h"
|
|---|
| 52 |
|
|---|
| 53 | #define NAME "s3c24ser"
|
|---|
| 54 | #define NAMESPACE "char"
|
|---|
| 55 |
|
|---|
| 56 | /* Bits in UTRSTAT register */
|
|---|
| 57 | #define S3C24XX_UTRSTAT_TX_EMPTY 0x4
|
|---|
| 58 | #define S3C24XX_UTRSTAT_RDATA 0x1
|
|---|
| 59 |
|
|---|
| 60 | static irq_cmd_t uart_irq_cmds[] = {
|
|---|
| 61 | {
|
|---|
| 62 | .cmd = CMD_ACCEPT
|
|---|
| 63 | }
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | static irq_code_t uart_irq_code = {
|
|---|
| 67 | sizeof(uart_irq_cmds) / sizeof(irq_cmd_t),
|
|---|
| 68 | uart_irq_cmds
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /** S3C24xx UART instance structure */
|
|---|
| 72 | static s3c24xx_uart_t *uart;
|
|---|
| 73 |
|
|---|
| 74 | static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 75 | static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
|---|
| 76 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart);
|
|---|
| 77 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte);
|
|---|
| 78 |
|
|---|
| 79 | int main(int argc, char *argv[])
|
|---|
| 80 | {
|
|---|
| 81 | int rc;
|
|---|
| 82 |
|
|---|
| 83 | printf(NAME ": S3C24xx on-chip UART driver\n");
|
|---|
| 84 |
|
|---|
| 85 | rc = devmap_driver_register(NAME, s3c24xx_uart_connection);
|
|---|
| 86 | if (rc < 0) {
|
|---|
| 87 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 88 | return -1;
|
|---|
| 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 = devmap_device_register(NAMESPACE "/" NAME, &uart->dev_handle);
|
|---|
| 99 | if (rc != EOK) {
|
|---|
| 100 | devmap_hangup_phone(DEVMAP_DRIVER);
|
|---|
| 101 | printf(NAME ": Unable to register device %s.\n");
|
|---|
| 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_callid_t iid, ipc_call_t *icall)
|
|---|
| 117 | {
|
|---|
| 118 | ipc_callid_t callid;
|
|---|
| 119 | ipc_call_t call;
|
|---|
| 120 | ipcarg_t method;
|
|---|
| 121 | int retval;
|
|---|
| 122 |
|
|---|
| 123 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 124 | ipc_answer_0(iid, EOK);
|
|---|
| 125 |
|
|---|
| 126 | while (1) {
|
|---|
| 127 | callid = async_get_call(&call);
|
|---|
| 128 | method = IPC_GET_METHOD(call);
|
|---|
| 129 | switch (method) {
|
|---|
| 130 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 131 | /* The other side has hung up. */
|
|---|
| 132 | ipc_answer_0(callid, EOK);
|
|---|
| 133 | return;
|
|---|
| 134 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 135 | printf(NAME ": creating callback connection\n");
|
|---|
| 136 | uart->client_phone = IPC_GET_ARG5(call);
|
|---|
| 137 | retval = 0;
|
|---|
| 138 | break;
|
|---|
| 139 | case CHAR_WRITE_BYTE:
|
|---|
| 140 | printf(NAME ": write %d to device\n",
|
|---|
| 141 | IPC_GET_ARG1(call));
|
|---|
| 142 | s3c24xx_uart_sendb(uart, (uint8_t) IPC_GET_ARG1(call));
|
|---|
| 143 | retval = 0;
|
|---|
| 144 | break;
|
|---|
| 145 | default:
|
|---|
| 146 | retval = EINVAL;
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | ipc_answer_0(callid, retval);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 154 | {
|
|---|
| 155 | (void) iid; (void) call;
|
|---|
| 156 |
|
|---|
| 157 | if ((pio_read_32(&uart->io->utrstat) & S3C24XX_UTRSTAT_RDATA) != 0) {
|
|---|
| 158 | uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
|
|---|
| 159 |
|
|---|
| 160 | if (uart->client_phone != -1) {
|
|---|
| 161 | async_msg_1(uart->client_phone, CHAR_NOTIF_BYTE,
|
|---|
| 162 | data);
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** Initialize S3C24xx on-chip UART. */
|
|---|
| 168 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
|
|---|
| 169 | {
|
|---|
| 170 | void *vaddr;
|
|---|
| 171 | sysarg_t inr;
|
|---|
| 172 |
|
|---|
| 173 | if (sysinfo_get_value("s3c24xx_uart.address.physical",
|
|---|
| 174 | &uart->paddr) != EOK)
|
|---|
| 175 | return -1;
|
|---|
| 176 |
|
|---|
| 177 | if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
|
|---|
| 178 | &vaddr) != 0)
|
|---|
| 179 | return -1;
|
|---|
| 180 |
|
|---|
| 181 | if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
|
|---|
| 182 | return -1;
|
|---|
| 183 |
|
|---|
| 184 | uart->io = vaddr;
|
|---|
| 185 | uart->client_phone = -1;
|
|---|
| 186 |
|
|---|
| 187 | printf(NAME ": device at physical address 0x%x, inr %d.\n",
|
|---|
| 188 | uart->paddr, inr);
|
|---|
| 189 |
|
|---|
| 190 | async_set_interrupt_received(s3c24xx_uart_irq_handler);
|
|---|
| 191 |
|
|---|
| 192 | ipc_register_irq(inr, device_assign_devno(), 0, &uart_irq_code);
|
|---|
| 193 |
|
|---|
| 194 | /* Disable FIFO */
|
|---|
| 195 | pio_write_32(&uart->io->ufcon,
|
|---|
| 196 | pio_read_32(&uart->io->ufcon) & ~0x01);
|
|---|
| 197 |
|
|---|
| 198 | /* Set RX interrupt to pulse mode */
|
|---|
| 199 | pio_write_32(&uart->io->ucon,
|
|---|
| 200 | pio_read_32(&uart->io->ucon) & ~(1 << 8));
|
|---|
| 201 |
|
|---|
| 202 | return EOK;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Send a byte to the UART. */
|
|---|
| 206 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
|
|---|
| 207 | {
|
|---|
| 208 | /* Wait for transmitter to be empty. */
|
|---|
| 209 | while ((pio_read_32(&uart->io->utrstat) & S3C24XX_UTRSTAT_TX_EMPTY) == 0)
|
|---|
| 210 | ;
|
|---|
| 211 |
|
|---|
| 212 | pio_write_32(&uart->io->utxh, byte);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /** @}
|
|---|
| 216 | */
|
|---|