uart.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the coreboot project.
  3. *
  4. * Copyright 2014 Google Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "u.h"
  16. #include "../port/lib.h"
  17. #include "mem.h"
  18. #include "dat.h"
  19. #include "fns.h"
  20. void lowrisc_uart_init(int idx);
  21. void lowrisc_putchar(int idx, int data);
  22. unsigned char lowrisc_getchar(int idx);
  23. void lowrisc_flush(int idx);
  24. // riscv has no standard uart. Further, if we use a bad address,
  25. // the hardware explodes. So, the uart, if 0, is ignored.
  26. // At some point it will be ready.
  27. extern uint8_t *uart;
  28. extern uintptr_t uartpa;
  29. static void spike_putchar(uint8_t c)
  30. {
  31. *uart = c;
  32. }
  33. // Get a 7-bit char. < 0 means err.
  34. static int spike_getchar(void)
  35. {
  36. if (uart[5] & 1) {
  37. int c = uart[0];
  38. if (0) print("getchar: got 0x%x\n", c);
  39. return c;
  40. }
  41. return -1;
  42. }
  43. void uart_init(void)
  44. {
  45. switch(uartpa){
  46. case 0x42000000:
  47. lowrisc_uart_init(0);
  48. default:
  49. break;
  50. }
  51. }
  52. void putchar(uint8_t c)
  53. {
  54. switch(uartpa){
  55. case 0x40001000:
  56. spike_putchar(c);
  57. break;
  58. case 0x42000000:
  59. //lowrisc_flush(0);
  60. lowrisc_putchar(0, c);
  61. *uart = c;
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. // Get a 7-bit char. < 0 means err.
  68. int getchar(void)
  69. {
  70. int c;
  71. switch(uartpa){
  72. case 0x40001000:
  73. return spike_getchar();
  74. break;
  75. case 0x42000000:
  76. c = lowrisc_getchar(0);
  77. // bug in lowrisc hardware
  78. if (c == 0)
  79. return -1;
  80. return c;
  81. break;
  82. default:
  83. break;
  84. }
  85. return -1;
  86. }