source: rtems/bsps/riscv/riscv_generic/console/console-io.c @ d7d66d7

5
Last change on this file since d7d66d7 was d7d66d7, checked in by Sebastian Huber <sebastian.huber@…>, on 04/19/18 at 04:28:01

bsps: Move console drivers to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2015 University of York.
3 * Hesham Almatary <hesham@alumni.york.ac.uk>
4 *
5 * Copyright (c) 2013, The Regents of the University of California (Regents).
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <bsp.h>
31#include <bsp/console-polled.h>
32#include <rtems/libio.h>
33#include <stdlib.h>
34#include <assert.h>
35#include <stdio.h>
36
37/* Most of the code below is copied from riscv-pk project */
38# define TOHOST_CMD(dev, cmd, payload) \
39  (((uint64_t)(dev) << 56) | ((uint64_t)(cmd) << 48) | (uint64_t)(payload))
40
41#define FROMHOST_DEV(fromhost_value) ((uint64_t)(fromhost_value) >> 56)
42#define FROMHOST_CMD(fromhost_value) ((uint64_t)(fromhost_value) << 8 >> 56)
43#define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16)
44
45volatile uint64_t tohost __attribute__((section(".htif")));
46volatile uint64_t fromhost __attribute__((section(".htif")));
47volatile int htif_console_buf;
48
49static void __check_fromhost()
50{
51  uint64_t fh = fromhost;
52  if (!fh) {
53    return;
54  }
55  fromhost = 0;
56
57  // this should be from the console
58  assert(FROMHOST_DEV(fh) == 1);
59  switch (FROMHOST_CMD(fh)) {
60  case 0:
61    htif_console_buf = 1 + (uint8_t)FROMHOST_DATA(fh);
62    break;
63  case 1:
64    break;
65  default:
66    assert(0);
67  }
68}
69
70static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
71{
72  while (tohost) {
73    __check_fromhost();
74  }
75  tohost = TOHOST_CMD(dev, cmd, data);
76}
77
78int htif_console_getchar()
79{
80  __check_fromhost();
81  int ch = htif_console_buf;
82  if (ch >= 0) {
83    htif_console_buf = -1;
84    __set_tohost(1, 0, 0);
85  }
86
87  return ch - 1;
88}
89
90static void do_tohost_fromhost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
91{
92  __set_tohost(dev, cmd, data);
93
94  while (1) {
95    uint64_t fh = fromhost;
96    if (fh) {
97      if (FROMHOST_DEV(fh) == dev && FROMHOST_CMD(fh) == cmd) {
98        fromhost = 0;
99        break;
100      }
101      __check_fromhost();
102    }
103  }
104}
105
106void htif_console_putchar(uint8_t ch)
107{
108  __set_tohost(1, 1, ch);
109}
110
111void htif_poweroff()
112{
113  while (1) {
114    fromhost = 0;
115    tohost = 1;
116  }
117}
118
119void console_initialize_hardware(void)
120{
121  /* Do nothing */
122}
123
124static void outbyte_console(char ch)
125{
126  htif_console_putchar(ch);
127}
128
129static char inbyte_console(void)
130{
131  return htif_console_getchar();
132}
133
134/*
135 *  console_outbyte_polled
136 *
137 *  This routine transmits a character using polling.
138 */
139void console_outbyte_polled(
140  int  port,
141  char ch
142)
143{
144  outbyte_console( ch );
145}
146
147/*
148 *  console_inbyte_nonblocking
149 *
150 *  This routine polls for a character.
151 */
152
153int console_inbyte_nonblocking(int port)
154{
155  char c;
156
157  c = inbyte_console();
158  if (!c) {
159    return -1;
160  }
161  return (int) c;
162}
163
164/*
165 *  To support printk
166 */
167
168#include <rtems/bspIo.h>
169
170static void RISCV_output_char(char c)
171{
172  console_outbyte_polled( 0, c );
173}
174
175BSP_output_char_function_type BSP_output_char = RISCV_output_char;
176BSP_polling_getchar_function_type BSP_poll_char =
177  (void *)console_inbyte_nonblocking;
Note: See TracBrowser for help on using the repository browser.