HelenOS sources
This source file includes following definitions.
- ns16550_reg_read
- ns16550_reg_write
- ns16550_claim
- ns16550_irq_handler
- ns16550_clear_buffer
- ns16550_sendb
- ns16550_putuchar
- ns16550_format_set
- ns16550_init
- ns16550_wire
#include <assert.h>
#include <genarch/drivers/ns16550/ns16550.h>
#include <ddi/irq.h>
#include <ddi/ddi.h>
#include <arch/asm.h>
#include <console/chardev.h>
#include <stdlib.h>
#include <align.h>
#include <str.h>
#define LSR_DATA_READY 0x01
#define LSR_TH_READY 0x20
#define RETRY_CNT 100000
#define NOTHING \
do { \
} while(0)
#define WHILE_CNT_AND_COND_DO(cnt, expr, statement) \
do { \
for (volatile unsigned i = 0; i < (cnt); i++) \
if ((expr)) \
statement; \
else \
break; \
} while (0)
static uint8_t ns16550_reg_read(ns16550_instance_t *inst, ns16550_reg_t reg)
{
return pio_read_8(&inst->ns16550[reg << inst->reg_shift]);
}
static void ns16550_reg_write(ns16550_instance_t *inst, ns16550_reg_t reg,
uint8_t val)
{
pio_write_8(&inst->ns16550[reg << inst->reg_shift], val);
}
static irq_ownership_t ns16550_claim(irq_t *irq)
{
ns16550_instance_t *instance = irq->instance;
if (ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY)
return IRQ_ACCEPT;
else
return IRQ_DECLINE;
}
static void ns16550_irq_handler(irq_t *irq)
{
ns16550_instance_t *instance = irq->instance;
while (ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY) {
uint8_t data = ns16550_reg_read(instance, NS16550_REG_RBR);
indev_push_character(instance->input, data);
}
}
static void ns16550_clear_buffer(ns16550_instance_t *instance)
{
WHILE_CNT_AND_COND_DO(RETRY_CNT,
ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_DATA_READY,
(void) ns16550_reg_read(instance, NS16550_REG_RBR));
}
static void ns16550_sendb(ns16550_instance_t *instance, uint8_t byte)
{
WHILE_CNT_AND_COND_DO(RETRY_CNT,
!(ns16550_reg_read(instance, NS16550_REG_LSR) & LSR_TH_READY),
NOTHING);
ns16550_reg_write(instance, NS16550_REG_THR, byte);
}
static void ns16550_putuchar(outdev_t *dev, char32_t ch)
{
ns16550_instance_t *instance = (ns16550_instance_t *) dev->data;
if ((!instance->parea.mapped) || (console_override)) {
if (ch == '\n')
ns16550_sendb(instance, '\r');
if (ascii_check(ch))
ns16550_sendb(instance, (uint8_t) ch);
else
ns16550_sendb(instance, U_SPECIAL);
}
}
static outdev_operations_t ns16550_ops = {
.write = ns16550_putuchar,
.redraw = NULL
};
void ns16550_format_set(ns16550_instance_t *instance,
unsigned baud_rate, uint8_t lcr_format)
{
uint16_t divisor;
divisor = (uint16_t)(NS156440_CLOCK / baud_rate);
if (divisor == 0)
divisor = 1;
ns16550_reg_write(instance, NS16550_REG_LCR, LCR_DLAB);
ns16550_reg_write(instance, NS16550_REG_DLL, divisor & 0xFF);
ns16550_reg_write(instance, NS16550_REG_DLH, (divisor >> 8) & 0xFF);
ns16550_reg_write(instance, NS16550_REG_LCR, lcr_format & ~LCR_DLAB);
}
ns16550_instance_t *ns16550_init(ioport8_t *dev_phys, unsigned reg_shift,
inr_t inr, cir_t cir, void *cir_arg, outdev_t **output)
{
size_t size = 6 * (1U << reg_shift);
ioport8_t *dev = pio_map((void *) dev_phys, size);
ns16550_instance_t *instance = malloc(sizeof(ns16550_instance_t));
if (instance) {
instance->ns16550 = dev;
instance->reg_shift = reg_shift;
instance->input = NULL;
instance->output = NULL;
if (output) {
instance->output = malloc(sizeof(outdev_t));
if (!instance->output) {
free(instance);
pio_unmap((void *) dev_phys, (void *) dev,
size);
return NULL;
}
outdev_initialize("ns16550", instance->output,
&ns16550_ops);
instance->output->data = instance;
*output = instance->output;
}
irq_initialize(&instance->irq);
instance->irq.inr = inr;
instance->irq.claim = ns16550_claim;
instance->irq.handler = ns16550_irq_handler;
instance->irq.instance = instance;
instance->irq.cir = cir;
instance->irq.cir_arg = cir_arg;
ddi_parea_init(&instance->parea);
instance->parea.pbase = ALIGN_DOWN((uintptr_t) dev_phys,
PAGE_SIZE);
instance->parea.frames = ALIGN_UP(size, PAGE_SIZE);
instance->parea.unpriv = false;
instance->parea.mapped = false;
ddi_parea_register(&instance->parea);
}
return instance;
}
void ns16550_wire(ns16550_instance_t *instance, indev_t *input)
{
assert(instance);
assert(input);
instance->input = input;
irq_register(&instance->irq);
ns16550_clear_buffer(instance);
ns16550_reg_write(instance, NS16550_REG_IER, IER_ERBFI);
ns16550_reg_write(instance, NS16550_REG_MCR, MCR_OUT2);
}
HelenOS homepage, sources at GitHub