source: mainline/uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c@ 03bf4fc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 03bf4fc was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 6.3 KB
Line 
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
50static irq_cmd_t uart_irq_cmds[] = {
51 {
52 .cmd = CMD_ACCEPT
53 }
54};
55
56static 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 */
64static s3c24xx_uart_t *uart;
65
66static void s3c24xx_uart_connection(ipc_call_t *, void *);
67static void s3c24xx_uart_irq_handler(ipc_call_t *, void *);
68static int s3c24xx_uart_init(s3c24xx_uart_t *);
69static void s3c24xx_uart_sendb(s3c24xx_uart_t *, uint8_t);
70
71static errno_t s3c24xx_uart_read(chardev_srv_t *, void *, size_t, size_t *);
72static errno_t s3c24xx_uart_write(chardev_srv_t *, const void *, size_t, size_t *);
73
74static chardev_ops_t s3c24xx_uart_chardev_ops = {
75 .read = s3c24xx_uart_read,
76 .write = s3c24xx_uart_write
77};
78
79int 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 errno_t 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. */
115static void s3c24xx_uart_connection(ipc_call_t *icall, void *arg)
116{
117 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) arg;
118
119 chardev_conn(icall, &uart->cds);
120}
121
122static void s3c24xx_uart_irq_handler(ipc_call_t *call, void *arg)
123{
124 errno_t rc;
125
126 (void) call;
127 (void) arg;
128
129 while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
130 uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
131 uint32_t status = pio_read_32(&uart->io->uerstat);
132
133 fibril_mutex_lock(&uart->buf_lock);
134
135 rc = circ_buf_push(&uart->cbuf, &data);
136 if (rc != EOK)
137 printf(NAME ": Buffer overrun\n");
138
139 fibril_mutex_unlock(&uart->buf_lock);
140 fibril_condvar_broadcast(&uart->buf_cv);
141
142 if (status != 0)
143 printf(NAME ": Error status 0x%x\n", status);
144 }
145}
146
147/** Initialize S3C24xx on-chip UART. */
148static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
149{
150 void *vaddr;
151 sysarg_t inr;
152
153 circ_buf_init(&uart->cbuf, uart->buf, s3c24xx_uart_buf_size, 1);
154 fibril_mutex_initialize(&uart->buf_lock);
155 fibril_condvar_initialize(&uart->buf_cv);
156
157 if (sysinfo_get_value("s3c24xx_uart.address.physical",
158 &uart->paddr) != EOK)
159 return -1;
160
161 if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
162 &vaddr) != 0)
163 return -1;
164
165 if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
166 return -1;
167
168 uart->io = vaddr;
169
170 printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
171 (void *) uart->paddr, inr);
172
173 async_irq_subscribe(inr, s3c24xx_uart_irq_handler, NULL, &uart_irq_code, NULL);
174
175 /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
176 pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
177 UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
178
179 /* Set RX interrupt to pulse mode */
180 pio_write_32(&uart->io->ucon,
181 pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
182
183 chardev_srvs_init(&uart->cds);
184 uart->cds.ops = &s3c24xx_uart_chardev_ops;
185 uart->cds.sarg = uart;
186
187 return EOK;
188}
189
190/** Send a byte to the UART. */
191static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
192{
193 /* Wait for space becoming available in Tx FIFO. */
194 while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
195 ;
196
197 pio_write_32(&uart->io->utxh, byte);
198}
199
200static errno_t s3c24xx_uart_read(chardev_srv_t *srv, void *buf, size_t size,
201 size_t *nread)
202{
203 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg;
204 size_t p;
205 uint8_t *bp = (uint8_t *) buf;
206 errno_t rc;
207
208 fibril_mutex_lock(&uart->buf_lock);
209
210 while (circ_buf_nused(&uart->cbuf) == 0)
211 fibril_condvar_wait(&uart->buf_cv, &uart->buf_lock);
212
213 p = 0;
214 while (p < size) {
215 rc = circ_buf_pop(&uart->cbuf, &bp[p]);
216 if (rc != EOK)
217 break;
218 ++p;
219 }
220
221 fibril_mutex_unlock(&uart->buf_lock);
222
223 *nread = p;
224 return EOK;
225}
226
227static errno_t s3c24xx_uart_write(chardev_srv_t *srv, const void *data, size_t size,
228 size_t *nwr)
229{
230 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg;
231 size_t i;
232 uint8_t *dp = (uint8_t *) data;
233
234 for (i = 0; i < size; i++)
235 s3c24xx_uart_sendb(uart, dp[i]);
236
237 *nwr = size;
238 return EOK;
239}
240
241/** @}
242 */
Note: See TracBrowser for help on using the repository browser.