1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <stdlib.h>
14#include <platsupport/serial.h>
15#include <platsupport/plat/serial.h>
16#include <string.h>
17
18#include "../../chardev.h"
19
20#define USR       0x0008 /* Status register */
21#define UCR       0x0010 /* Control register */
22#define UTF       0x0070 /* TX fifo */
23#define UNTX      0x0040 /* Number of bytes to send */
24
25#define USR_TXRDY             (1U << 2)
26#define USR_TXEMP             (1U << 3)
27
28#define CMD_TXRDY_RESET       (3U << 8)
29
30#define UART_REG(base, x)    ((volatile uint32_t *)((uintptr_t)(base) + (x)))
31
32static void
33uart_handle_irq(ps_chardevice_t* d UNUSED)
34{
35}
36
37int uart_putchar(ps_chardevice_t* d, int c)
38{
39    while (!(*UART_REG(d->vaddr, USR) & USR_TXEMP));
40
41    *UART_REG(d->vaddr, UNTX) = 1;
42    *UART_REG(d->vaddr, UTF) = c & 0xff;
43    if (c == '\n' && (d->flags & SERIAL_AUTO_CR)) {
44        uart_putchar(d, '\r');
45    }
46    return 0;
47}
48
49int uart_getchar(ps_chardevice_t* d UNUSED)
50{
51    return EOF;
52}
53
54int uart_init(const struct dev_defn* defn,
55              const ps_io_ops_t* ops,
56              ps_chardevice_t* dev)
57{
58    /* Attempt to map the virtual address, assure this works */
59    void* vaddr = chardev_map(defn, ops);
60    memset(dev, 0, sizeof(*dev));
61    if (vaddr == NULL) {
62        return -1;
63    }
64
65    /* Set up all the  device properties. */
66    dev->id         = defn->id;
67    dev->vaddr      = (void*)vaddr;
68    dev->read       = &uart_read;
69    dev->write      = &uart_write;
70    dev->handle_irq = &uart_handle_irq;
71    dev->irqs       = defn->irqs;
72    dev->ioops      = *ops;
73    dev->flags      = SERIAL_AUTO_CR;
74
75    return 0;
76}
77