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