source: mainline/kernel/genarch/src/drivers/ns16550/ns16550.c@ 1b7b7af

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b7b7af was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 * Copyright (c) 2009 Jakub Jermar
3 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
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 kernel_genarch
31 * @{
32 */
33/**
34 * @file
35 * @brief NS 16550 serial controller driver.
36 */
37
38#include <assert.h>
39#include <genarch/drivers/ns16550/ns16550.h>
40#include <ddi/irq.h>
41#include <ddi/ddi.h>
42#include <arch/asm.h>
43#include <console/chardev.h>
44#include <stdlib.h>
45#include <align.h>
46#include <str.h>
47
48#define LSR_DATA_READY 0x01
49#define LSR_TH_READY 0x20
50
51#define RETRY_CNT 100000
52
53#define NOTHING \
54 do { \
55 } while(0)
56
57#define WHILE_CNT_AND_COND_DO(cnt, expr, statement) \
58 do { \
59 for (volatile unsigned i = 0; i < (cnt); i++) \
60 if ((expr)) \
61 statement; \
62 else \
63 break; \
64 } while (0)
65
66static uint8_t ns16550_reg_read(ns16550_instance_t *inst, ns16550_reg_t reg)
67{
68 return pio_read_8(&inst->ns16550[reg << inst->reg_shift]);
69}
70
71static void ns16550_reg_write(ns16550_instance_t *inst, ns16550_reg_t reg,
72 uint8_t val)
73{
74 pio_write_8(&inst->ns16550[reg << inst->reg_shift], val);
75}
76
77static irq_ownership_t ns16550_claim(irq_t *irq)
78{
79 ns16550_instance_t *instance = irq->instance;
80
81 if (ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY)
82 return IRQ_ACCEPT;
83 else
84 return IRQ_DECLINE;
85}
86
87static void ns16550_irq_handler(irq_t *irq)
88{
89 ns16550_instance_t *instance = irq->instance;
90
91 while (ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY) {
92 uint8_t data = ns16550_reg_read(instance, NS16550_REG_RBR);
93 indev_push_character(instance->input, data);
94 }
95}
96
97/** Clear input buffer. */
98static void ns16550_clear_buffer(ns16550_instance_t *instance)
99{
100 WHILE_CNT_AND_COND_DO(RETRY_CNT,
101 ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY,
102 (void) ns16550_reg_read(instance, NS16550_REG_RBR));
103}
104
105static void ns16550_sendb(ns16550_instance_t *instance, uint8_t byte)
106{
107 WHILE_CNT_AND_COND_DO(RETRY_CNT,
108 !(ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_TH_READY),
109 NOTHING);
110
111 ns16550_reg_write(instance, NS16550_REG_THR, byte);
112}
113
114static void ns16550_putuchar(outdev_t *dev, char32_t ch)
115{
116 ns16550_instance_t *instance = (ns16550_instance_t *) dev->data;
117
118 if ((!instance->parea.mapped) || (console_override)) {
119 if (ch == '\n')
120 ns16550_sendb(instance, '\r');
121
122 if (ascii_check(ch))
123 ns16550_sendb(instance, (uint8_t) ch);
124 else
125 ns16550_sendb(instance, U_SPECIAL);
126 }
127}
128
129static outdev_operations_t ns16550_ops = {
130 .write = ns16550_putuchar,
131 .redraw = NULL
132};
133
134/** Initialize ns16550.
135 *
136 * @param dev Address of the beginning of the device in I/O space.
137 * @param reg_shift Spacing between individual register addresses, in log2.
138 * The individual register location is calculated as
139 * `base + (register offset << reg_shift)`.
140 * @param inr Interrupt number.
141 * @param cir Clear interrupt function.
142 * @param cir_arg First argument to cir.
143 * @param output Where to store pointer to the output device
144 * or NULL if the caller is not interested in
145 * writing to the serial port.
146 *
147 * @return Keyboard instance or NULL on failure.
148 *
149 */
150ns16550_instance_t *ns16550_init(ioport8_t *dev_phys, unsigned reg_shift,
151 inr_t inr, cir_t cir, void *cir_arg, outdev_t **output)
152{
153 size_t size = 6 * (1U << reg_shift);
154 ioport8_t *dev = pio_map((void *) dev_phys, size);
155
156 ns16550_instance_t *instance = malloc(sizeof(ns16550_instance_t));
157 if (instance) {
158 instance->ns16550 = dev;
159 instance->reg_shift = reg_shift;
160 instance->input = NULL;
161 instance->output = NULL;
162
163 if (output) {
164 instance->output = malloc(sizeof(outdev_t));
165 if (!instance->output) {
166 free(instance);
167 pio_unmap((void *) dev_phys, (void *) dev,
168 size);
169 return NULL;
170 }
171
172 outdev_initialize("ns16550", instance->output,
173 &ns16550_ops);
174 instance->output->data = instance;
175 *output = instance->output;
176 }
177
178 irq_initialize(&instance->irq);
179 instance->irq.inr = inr;
180 instance->irq.claim = ns16550_claim;
181 instance->irq.handler = ns16550_irq_handler;
182 instance->irq.instance = instance;
183 instance->irq.cir = cir;
184 instance->irq.cir_arg = cir_arg;
185
186 ddi_parea_init(&instance->parea);
187 instance->parea.pbase = ALIGN_DOWN((uintptr_t) dev_phys,
188 PAGE_SIZE);
189 instance->parea.frames = ALIGN_UP(size, PAGE_SIZE);
190 instance->parea.unpriv = false;
191 instance->parea.mapped = false;
192 ddi_parea_register(&instance->parea);
193 }
194
195 return instance;
196}
197
198void ns16550_wire(ns16550_instance_t *instance, indev_t *input)
199{
200 assert(instance);
201 assert(input);
202
203 instance->input = input;
204 irq_register(&instance->irq);
205
206 ns16550_clear_buffer(instance);
207
208 /* Enable interrupts */
209 ns16550_reg_write(instance, NS16550_REG_IER, IER_ERBFI);
210 ns16550_reg_write(instance, NS16550_REG_MCR, MCR_OUT2);
211}
212
213/** @}
214 */
Note: See TracBrowser for help on using the repository browser.