source: mainline/kernel/genarch/src/drivers/ns16550/ns16550.c@ 448e093

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

Fix function names, other minor tweaks.

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