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 "plat_internal.h"
14#include <sel4/sel4.h>
15#include <platsupport/chardev.h>
16#include <sel4platsupport/gen_config.h>
17#include <stddef.h>
18#include <vka/object.h>
19
20static ssize_t debug_write(ps_chardevice_t *device UNUSED, const void *vdata, size_t count,
21                           chardev_callback_t cb UNUSED, void *token UNUSED)
22{
23#ifdef CONFIG_PRINTING
24    size_t sent = 0;
25    const char *data = (const char *)vdata;
26    for (sent = 0; sent < count; sent++) {
27        seL4_DebugPutChar(*data++);
28    }
29#else
30    /* /dev/null */
31    (void)vdata;
32#endif
33    return count;
34}
35
36static struct ps_chardevice console_device = {
37    .write = &debug_write
38};
39static struct ps_chardevice *console = &console_device;
40
41void register_console(struct ps_chardevice *user_console)
42{
43    console = user_console;
44}
45
46int __plat_serial_init(ps_io_ops_t *io_ops)
47{
48    struct ps_chardevice temp_device;
49    if (ps_cdev_init(PS_SERIAL_DEFAULT, io_ops, &temp_device)) {
50        /* Apply the changes */
51#if defined(CONFIG_LIB_SEL4_PLAT_SUPPORT_USE_SEL4_DEBUG_PUTCHAR)
52        temp_device.write = &debug_write;
53#endif
54        console_device = temp_device;
55        console = &console_device;
56        return 0;
57    } else {
58        return -1;
59    }
60}
61
62void __plat_putchar(int c)
63{
64    if (console) {
65        ps_cdev_putchar(console, c);
66    }
67}
68
69int __plat_getchar(void)
70{
71    if (console) {
72        return ps_cdev_getchar(console);
73    } else {
74        return EOF;
75    }
76}
77