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 <assert.h>
|
---|
41 | #include <genarch/drivers/s3c24xx/uart.h>
|
---|
42 | #include <console/chardev.h>
|
---|
43 | #include <console/console.h>
|
---|
44 | #include <ddi/device.h>
|
---|
45 | #include <arch/asm.h>
|
---|
46 | #include <mm/slab.h>
|
---|
47 | #include <mm/page.h>
|
---|
48 | #include <mm/km.h>
|
---|
49 | #include <sysinfo/sysinfo.h>
|
---|
50 | #include <str.h>
|
---|
51 |
|
---|
52 | static void s3c24xx_uart_sendb(outdev_t *dev, uint8_t byte)
|
---|
53 | {
|
---|
54 | s3c24xx_uart_t *uart =
|
---|
55 | (s3c24xx_uart_t *) dev->data;
|
---|
56 |
|
---|
57 | /* Wait for space becoming available in Tx FIFO. */
|
---|
58 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
|
---|
59 | ;
|
---|
60 |
|
---|
61 | pio_write_32(&uart->io->utxh, byte);
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void s3c24xx_uart_putchar(outdev_t *dev, wchar_t ch)
|
---|
65 | {
|
---|
66 | s3c24xx_uart_t *uart =
|
---|
67 | (s3c24xx_uart_t *) dev->data;
|
---|
68 |
|
---|
69 | if ((!uart->parea.mapped) || (console_override)) {
|
---|
70 | if (!ascii_check(ch)) {
|
---|
71 | s3c24xx_uart_sendb(dev, U_SPECIAL);
|
---|
72 | } else {
|
---|
73 | if (ch == '\n')
|
---|
74 | s3c24xx_uart_sendb(dev, (uint8_t) '\r');
|
---|
75 | s3c24xx_uart_sendb(dev, (uint8_t) ch);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | static irq_ownership_t s3c24xx_uart_claim(irq_t *irq)
|
---|
81 | {
|
---|
82 | return IRQ_ACCEPT;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static void s3c24xx_uart_irq_handler(irq_t *irq)
|
---|
86 | {
|
---|
87 | s3c24xx_uart_t *uart = irq->instance;
|
---|
88 |
|
---|
89 | while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
|
---|
90 | uint32_t data = pio_read_32(&uart->io->urxh);
|
---|
91 | pio_read_32(&uart->io->uerstat);
|
---|
92 | indev_push_character(uart->indev, data & 0xff);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | static outdev_operations_t s3c24xx_uart_ops = {
|
---|
97 | .write = s3c24xx_uart_putchar,
|
---|
98 | .redraw = NULL,
|
---|
99 | .scroll_up = NULL,
|
---|
100 | .scroll_down = NULL
|
---|
101 | };
|
---|
102 |
|
---|
103 | outdev_t *s3c24xx_uart_init(uintptr_t paddr, inr_t inr)
|
---|
104 | {
|
---|
105 | outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
---|
106 | if (!uart_dev)
|
---|
107 | return NULL;
|
---|
108 |
|
---|
109 | s3c24xx_uart_t *uart =
|
---|
110 | malloc(sizeof(s3c24xx_uart_t), FRAME_ATOMIC);
|
---|
111 | if (!uart) {
|
---|
112 | free(uart_dev);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | outdev_initialize("s3c24xx_uart_dev", uart_dev, &s3c24xx_uart_ops);
|
---|
117 | uart_dev->data = uart;
|
---|
118 |
|
---|
119 | uart->io = (s3c24xx_uart_io_t *) km_map(paddr, PAGE_SIZE,
|
---|
120 | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
---|
121 | uart->indev = NULL;
|
---|
122 |
|
---|
123 | /* Initialize IRQ structure. */
|
---|
124 | irq_initialize(&uart->irq);
|
---|
125 | uart->irq.devno = device_assign_devno();
|
---|
126 | uart->irq.inr = inr;
|
---|
127 | uart->irq.claim = s3c24xx_uart_claim;
|
---|
128 | uart->irq.handler = s3c24xx_uart_irq_handler;
|
---|
129 | uart->irq.instance = uart;
|
---|
130 |
|
---|
131 | /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
|
---|
132 | pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
|
---|
133 | UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
|
---|
134 |
|
---|
135 | /* Set RX interrupt to pulse mode */
|
---|
136 | pio_write_32(&uart->io->ucon,
|
---|
137 | pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
|
---|
138 |
|
---|
139 | link_initialize(&uart->parea.link);
|
---|
140 | uart->parea.pbase = paddr;
|
---|
141 | uart->parea.frames = 1;
|
---|
142 | uart->parea.unpriv = false;
|
---|
143 | uart->parea.mapped = false;
|
---|
144 | ddi_parea_register(&uart->parea);
|
---|
145 |
|
---|
146 | if (!fb_exported) {
|
---|
147 | /*
|
---|
148 | * This is the necessary evil until
|
---|
149 | * the userspace driver is entirely
|
---|
150 | * self-sufficient.
|
---|
151 | */
|
---|
152 | sysinfo_set_item_val("fb", NULL, true);
|
---|
153 | sysinfo_set_item_val("fb.kind", NULL, 3);
|
---|
154 | sysinfo_set_item_val("fb.address.physical", NULL, paddr);
|
---|
155 |
|
---|
156 | fb_exported = true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return uart_dev;
|
---|
160 | }
|
---|
161 |
|
---|
162 | void s3c24xx_uart_input_wire(s3c24xx_uart_t *uart, indev_t *indev)
|
---|
163 | {
|
---|
164 | assert(uart);
|
---|
165 | assert(indev);
|
---|
166 |
|
---|
167 | uart->indev = indev;
|
---|
168 | irq_register(&uart->irq);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** @}
|
---|
172 | */
|
---|