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_callid_t, 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 int s3c24xx_uart_read(chardev_srv_t *, void *, size_t, size_t *);
|
---|
72 | static int s3c24xx_uart_write(chardev_srv_t *, const void *, size_t, size_t *);
|
---|
73 |
|
---|
74 | static chardev_ops_t s3c24xx_uart_chardev_ops = {
|
---|
75 | .read = s3c24xx_uart_read,
|
---|
76 | .write = s3c24xx_uart_write
|
---|
77 | };
|
---|
78 |
|
---|
79 | int main(int argc, char *argv[])
|
---|
80 | {
|
---|
81 | printf("%s: S3C24xx on-chip UART driver\n", NAME);
|
---|
82 |
|
---|
83 | async_set_fallback_port_handler(s3c24xx_uart_connection, uart);
|
---|
84 | int rc = loc_server_register(NAME);
|
---|
85 | if (rc != EOK) {
|
---|
86 | printf("%s: Unable to register server.\n", NAME);
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | uart = malloc(sizeof(s3c24xx_uart_t));
|
---|
91 | if (uart == NULL)
|
---|
92 | return -1;
|
---|
93 |
|
---|
94 | if (s3c24xx_uart_init(uart) != EOK)
|
---|
95 | return -1;
|
---|
96 |
|
---|
97 | rc = loc_service_register(NAMESPACE "/" NAME, &uart->service_id);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf(NAME ": Unable to register device %s.\n",
|
---|
100 | NAMESPACE "/" NAME);
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | printf(NAME ": Registered device %s.\n", NAMESPACE "/" NAME);
|
---|
105 |
|
---|
106 | printf(NAME ": Accepting connections\n");
|
---|
107 | task_retval(0);
|
---|
108 | async_manager();
|
---|
109 |
|
---|
110 | /* Not reached */
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Character device connection handler. */
|
---|
115 | static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
116 | void *arg)
|
---|
117 | {
|
---|
118 | s3c24xx_uart_t *uart = (s3c24xx_uart_t *) arg;
|
---|
119 |
|
---|
120 | chardev_conn(iid, icall, &uart->cds);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | static void s3c24xx_uart_irq_handler(ipc_call_t *call, void *arg)
|
---|
125 | {
|
---|
126 | int rc;
|
---|
127 |
|
---|
128 | (void) call;
|
---|
129 | (void) arg;
|
---|
130 |
|
---|
131 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
|
---|
132 | uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
|
---|
133 | uint32_t status = pio_read_32(&uart->io->uerstat);
|
---|
134 |
|
---|
135 | fibril_mutex_lock(&uart->buf_lock);
|
---|
136 |
|
---|
137 | rc = circ_buf_push(&uart->cbuf, &data);
|
---|
138 | if (rc != EOK)
|
---|
139 | printf(NAME ": Buffer overrun\n");
|
---|
140 |
|
---|
141 | fibril_mutex_unlock(&uart->buf_lock);
|
---|
142 | fibril_condvar_broadcast(&uart->buf_cv);
|
---|
143 |
|
---|
144 | if (status != 0)
|
---|
145 | printf(NAME ": Error status 0x%x\n", status);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Initialize S3C24xx on-chip UART. */
|
---|
150 | static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
|
---|
151 | {
|
---|
152 | void *vaddr;
|
---|
153 | sysarg_t inr;
|
---|
154 |
|
---|
155 | circ_buf_init(&uart->cbuf, uart->buf, s3c24xx_uart_buf_size, 1);
|
---|
156 | fibril_mutex_initialize(&uart->buf_lock);
|
---|
157 | fibril_condvar_initialize(&uart->buf_cv);
|
---|
158 |
|
---|
159 | if (sysinfo_get_value("s3c24xx_uart.address.physical",
|
---|
160 | &uart->paddr) != EOK)
|
---|
161 | return -1;
|
---|
162 |
|
---|
163 | if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
|
---|
164 | &vaddr) != 0)
|
---|
165 | return -1;
|
---|
166 |
|
---|
167 | if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
|
---|
168 | return -1;
|
---|
169 |
|
---|
170 | uart->io = vaddr;
|
---|
171 |
|
---|
172 | printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
|
---|
173 | (void *) uart->paddr, inr);
|
---|
174 |
|
---|
175 | async_irq_subscribe(inr, s3c24xx_uart_irq_handler, NULL, &uart_irq_code);
|
---|
176 |
|
---|
177 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
---|
178 | pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
|
---|
179 | UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
|
---|
180 |
|
---|
181 | /* Set RX interrupt to pulse mode */
|
---|
182 | pio_write_32(&uart->io->ucon,
|
---|
183 | pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
|
---|
184 |
|
---|
185 | chardev_srvs_init(&uart->cds);
|
---|
186 | uart->cds.ops = &s3c24xx_uart_chardev_ops;
|
---|
187 | uart->cds.sarg = uart;
|
---|
188 |
|
---|
189 | return EOK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Send a byte to the UART. */
|
---|
193 | static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
|
---|
194 | {
|
---|
195 | /* Wait for space becoming available in Tx FIFO. */
|
---|
196 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
|
---|
197 | ;
|
---|
198 |
|
---|
199 | pio_write_32(&uart->io->utxh, byte);
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int s3c24xx_uart_read(chardev_srv_t *srv, void *buf, size_t size,
|
---|
203 | size_t *nread)
|
---|
204 | {
|
---|
205 | s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg;
|
---|
206 | size_t p;
|
---|
207 | uint8_t *bp = (uint8_t *) buf;
|
---|
208 | int rc;
|
---|
209 |
|
---|
210 | fibril_mutex_lock(&uart->buf_lock);
|
---|
211 |
|
---|
212 | while (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 int 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 | /** @}
|
---|
245 | */
|
---|