uartlite.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Xilinx UARTLITE bootloader driver
  3. *
  4. * Copyright (C) 2007 Secret Lab Technologies Ltd.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <stdarg.h>
  11. #include <stddef.h>
  12. #include "types.h"
  13. #include "string.h"
  14. #include "stdio.h"
  15. #include "io.h"
  16. #include "ops.h"
  17. #define ULITE_RX 0x00
  18. #define ULITE_TX 0x04
  19. #define ULITE_STATUS 0x08
  20. #define ULITE_CONTROL 0x0c
  21. #define ULITE_STATUS_RXVALID 0x01
  22. #define ULITE_STATUS_TXFULL 0x08
  23. #define ULITE_CONTROL_RST_RX 0x02
  24. static void * reg_base;
  25. static int uartlite_open(void)
  26. {
  27. /* Clear the RX FIFO */
  28. out_be32(reg_base + ULITE_CONTROL, ULITE_CONTROL_RST_RX);
  29. return 0;
  30. }
  31. static void uartlite_putc(unsigned char c)
  32. {
  33. u32 reg = ULITE_STATUS_TXFULL;
  34. while (reg & ULITE_STATUS_TXFULL) /* spin on TXFULL bit */
  35. reg = in_be32(reg_base + ULITE_STATUS);
  36. out_be32(reg_base + ULITE_TX, c);
  37. }
  38. static unsigned char uartlite_getc(void)
  39. {
  40. u32 reg = 0;
  41. while (!(reg & ULITE_STATUS_RXVALID)) /* spin waiting for RXVALID bit */
  42. reg = in_be32(reg_base + ULITE_STATUS);
  43. return in_be32(reg_base + ULITE_RX);
  44. }
  45. static u8 uartlite_tstc(void)
  46. {
  47. u32 reg = in_be32(reg_base + ULITE_STATUS);
  48. return reg & ULITE_STATUS_RXVALID;
  49. }
  50. int uartlite_console_init(void *devp, struct serial_console_data *scdp)
  51. {
  52. int n;
  53. unsigned long reg_phys;
  54. n = getprop(devp, "virtual-reg", &reg_base, sizeof(reg_base));
  55. if (n != sizeof(reg_base)) {
  56. if (!dt_xlate_reg(devp, 0, &reg_phys, NULL))
  57. return -1;
  58. reg_base = (void *)reg_phys;
  59. }
  60. scdp->open = uartlite_open;
  61. scdp->putc = uartlite_putc;
  62. scdp->getc = uartlite_getc;
  63. scdp->tstc = uartlite_tstc;
  64. scdp->close = NULL;
  65. return 0;
  66. }