1/*
2 * Copyright 2018, 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#include <stdio.h>
13#include <stdint.h>
14
15#include <camkes.h>
16#include <camkes/io.h>
17#include <sel4/sel4.h>
18#include <utils/attribute.h>
19#include <utils/ansi.h>
20
21#include "plat.h"
22
23struct ps_chardevice serial_device;
24struct ps_chardevice *serial = NULL;
25
26ssize_t plat_serial_write(void *buf, size_t buf_size, chardev_callback_t cb, void *token)
27{
28    ssize_t res = ps_cdev_write(serial, buf, buf_size, cb, token);
29    return res;
30}
31
32ssize_t plat_serial_read(void *buf, size_t buf_size, chardev_callback_t cb, void *token)
33{
34    ssize_t res = ps_cdev_read(serial, buf, buf_size, cb, token);
35    return res;
36}
37
38void plat_serial_interrupt(handle_char_fn handle_char)
39{
40    if (serial) {
41        int data = 0;
42        ps_cdev_handle_irq(serial, 0);
43        while (data !=  EOF) {
44            data = ps_cdev_getchar(serial);
45            if (data != EOF) {
46                handle_char((uint8_t)data);
47            }
48        }
49    }
50}
51
52void plat_serial_putchar(int c)
53{
54    if (serial) {
55        ps_cdev_putchar(serial, c);
56    }
57}
58
59void plat_pre_init(ps_io_ops_t *io_ops)
60{
61    ZF_LOGF_IF(io_ops == NULL, "Was passed an empty IO ops struct");
62
63#ifdef CONFIG_PLAT_EXYNOS5
64    io_ops->clock_sys.priv = NULL;
65    io_ops->mux_sys.priv = NULL;
66#endif
67
68    serial = ps_cdev_init(PS_SERIAL_DEFAULT, io_ops, &serial_device);
69    if (serial == NULL) {
70        ZF_LOGE("Failed to initialise character device");
71    }
72}
73