source: mainline/kernel/genarch/src/drivers/s3c24xx_uart/s3c24xx_uart.c@ 77429d3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 77429d3 was 07c66cf, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Avoid magic numbers.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
3 * Copyright (c) 2010 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup genarch
31 * @{
32 */
33/**
34 * @file
35 * @brief Samsung S3C24xx on-chip UART driver.
36 *
37 * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
38 */
39
40#include <genarch/drivers/s3c24xx_uart/s3c24xx_uart.h>
41#include <console/chardev.h>
42#include <console/console.h>
43#include <ddi/device.h>
44#include <arch/asm.h>
45#include <mm/slab.h>
46#include <sysinfo/sysinfo.h>
47#include <str.h>
48
49static void s3c24xx_uart_sendb(outdev_t *dev, uint8_t byte)
50{
51 s3c24xx_uart_t *uart =
52 (s3c24xx_uart_t *) dev->data;
53
54 /* Wait for space becoming available in Tx FIFO. */
55 while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
56 ;
57
58 pio_write_32(&uart->io->utxh, byte);
59}
60
61static void s3c24xx_uart_putchar(outdev_t *dev, wchar_t ch, bool silent)
62{
63 if (!silent) {
64 if (!ascii_check(ch)) {
65 s3c24xx_uart_sendb(dev, U_SPECIAL);
66 } else {
67 if (ch == '\n')
68 s3c24xx_uart_sendb(dev, (uint8_t) '\r');
69 s3c24xx_uart_sendb(dev, (uint8_t) ch);
70 }
71 }
72}
73
74static irq_ownership_t s3c24xx_uart_claim(irq_t *irq)
75{
76 return IRQ_ACCEPT;
77}
78
79static void s3c24xx_uart_irq_handler(irq_t *irq)
80{
81 s3c24xx_uart_t *uart = irq->instance;
82
83 while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
84 uint32_t data = pio_read_32(&uart->io->urxh);
85 pio_read_32(&uart->io->uerstat);
86 indev_push_character(uart->indev, data & 0xff);
87 }
88}
89
90static outdev_operations_t s3c24xx_uart_ops = {
91 .write = s3c24xx_uart_putchar,
92 .redraw = NULL
93};
94
95outdev_t *s3c24xx_uart_init(s3c24xx_uart_io_t *io, inr_t inr)
96{
97 outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
98 if (!uart_dev)
99 return NULL;
100
101 s3c24xx_uart_t *uart =
102 malloc(sizeof(s3c24xx_uart_t), FRAME_ATOMIC);
103 if (!uart) {
104 free(uart_dev);
105 return NULL;
106 }
107
108 outdev_initialize("s3c24xx_uart_dev", uart_dev, &s3c24xx_uart_ops);
109 uart_dev->data = uart;
110
111 uart->io = io;
112 uart->indev = NULL;
113
114 /* Initialize IRQ structure. */
115 irq_initialize(&uart->irq);
116 uart->irq.devno = device_assign_devno();
117 uart->irq.inr = inr;
118 uart->irq.claim = s3c24xx_uart_claim;
119 uart->irq.handler = s3c24xx_uart_irq_handler;
120 uart->irq.instance = uart;
121
122 /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
123 pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
124 UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
125
126 /* Set RX interrupt to pulse mode */
127 pio_write_32(&uart->io->ucon,
128 pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
129
130 if (!fb_exported) {
131 /*
132 * This is the necessary evil until the userspace driver is entirely
133 * self-sufficient.
134 */
135 sysinfo_set_item_val("fb", NULL, true);
136 sysinfo_set_item_val("fb.kind", NULL, 3);
137 sysinfo_set_item_val("fb.address.physical", NULL, KA2PA(io));
138
139 fb_exported = true;
140 }
141
142 return uart_dev;
143}
144
145void s3c24xx_uart_input_wire(s3c24xx_uart_t *uart, indev_t *indev)
146{
147 ASSERT(uart);
148 ASSERT(indev);
149
150 uart->indev = indev;
151 irq_register(&uart->irq);
152}
153
154/** @}
155 */
Note: See TracBrowser for help on using the repository browser.