arm_dcc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2004-2007 ARM Limited.
  4. * Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  5. * Copyright (C) 2015 - 2016 Xilinx, Inc, Michal Simek
  6. *
  7. * As a special exception, if other files instantiate templates or use macros
  8. * or inline functions from this file, or you compile this file and link it
  9. * with other works to produce a work based on this file, this file does not
  10. * by itself cause the resulting work to be covered by the GNU General Public
  11. * License. However the source code for this file must still be made available
  12. * in accordance with section (3) of the GNU General Public License.
  13. * This exception does not invalidate any other reasons why a work based on
  14. * this file might be covered by the GNU General Public License.
  15. */
  16. #include <common.h>
  17. #include <dm.h>
  18. #include <serial.h>
  19. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V7A) || defined(CONFIG_CPU_V7R)
  20. /*
  21. * ARMV6 & ARMV7
  22. */
  23. #define DCC_RBIT (1 << 30)
  24. #define DCC_WBIT (1 << 29)
  25. #define write_dcc(x) \
  26. __asm__ volatile ("mcr p14, 0, %0, c0, c5, 0\n" : : "r" (x))
  27. #define read_dcc(x) \
  28. __asm__ volatile ("mrc p14, 0, %0, c0, c5, 0\n" : "=r" (x))
  29. #define status_dcc(x) \
  30. __asm__ volatile ("mrc p14, 0, %0, c0, c1, 0\n" : "=r" (x))
  31. #elif defined(CONFIG_CPU_XSCALE)
  32. /*
  33. * XSCALE
  34. */
  35. #define DCC_RBIT (1 << 31)
  36. #define DCC_WBIT (1 << 28)
  37. #define write_dcc(x) \
  38. __asm__ volatile ("mcr p14, 0, %0, c8, c0, 0\n" : : "r" (x))
  39. #define read_dcc(x) \
  40. __asm__ volatile ("mrc p14, 0, %0, c9, c0, 0\n" : "=r" (x))
  41. #define status_dcc(x) \
  42. __asm__ volatile ("mrc p14, 0, %0, c14, c0, 0\n" : "=r" (x))
  43. #elif defined(CONFIG_CPU_ARMV8)
  44. /*
  45. * ARMV8
  46. */
  47. #define DCC_RBIT (1 << 30)
  48. #define DCC_WBIT (1 << 29)
  49. #define write_dcc(x) \
  50. __asm__ volatile ("msr dbgdtrtx_el0, %0\n" : : "r" (x))
  51. #define read_dcc(x) \
  52. __asm__ volatile ("mrs %0, dbgdtrrx_el0\n" : "=r" (x))
  53. #define status_dcc(x) \
  54. __asm__ volatile ("mrs %0, mdccsr_el0\n" : "=r" (x))
  55. #else
  56. #define DCC_RBIT (1 << 0)
  57. #define DCC_WBIT (1 << 1)
  58. #define write_dcc(x) \
  59. __asm__ volatile ("mcr p14, 0, %0, c1, c0, 0\n" : : "r" (x))
  60. #define read_dcc(x) \
  61. __asm__ volatile ("mrc p14, 0, %0, c1, c0, 0\n" : "=r" (x))
  62. #define status_dcc(x) \
  63. __asm__ volatile ("mrc p14, 0, %0, c0, c0, 0\n" : "=r" (x))
  64. #endif
  65. #define can_read_dcc(x) do { \
  66. status_dcc(x); \
  67. x &= DCC_RBIT; \
  68. } while (0);
  69. #define can_write_dcc(x) do { \
  70. status_dcc(x); \
  71. x &= DCC_WBIT; \
  72. x = (x == 0); \
  73. } while (0);
  74. #define TIMEOUT_COUNT 0x4000000
  75. static int arm_dcc_getc(struct udevice *dev)
  76. {
  77. int ch;
  78. register unsigned int reg;
  79. do {
  80. can_read_dcc(reg);
  81. } while (!reg);
  82. read_dcc(ch);
  83. return ch;
  84. }
  85. static int arm_dcc_putc(struct udevice *dev, char ch)
  86. {
  87. register unsigned int reg;
  88. unsigned int timeout_count = TIMEOUT_COUNT;
  89. while (--timeout_count) {
  90. can_write_dcc(reg);
  91. if (reg)
  92. break;
  93. }
  94. if (timeout_count == 0)
  95. return -EAGAIN;
  96. else
  97. write_dcc(ch);
  98. return 0;
  99. }
  100. static int arm_dcc_pending(struct udevice *dev, bool input)
  101. {
  102. register unsigned int reg;
  103. if (input) {
  104. can_read_dcc(reg);
  105. } else {
  106. can_write_dcc(reg);
  107. }
  108. return reg;
  109. }
  110. static const struct dm_serial_ops arm_dcc_ops = {
  111. .putc = arm_dcc_putc,
  112. .pending = arm_dcc_pending,
  113. .getc = arm_dcc_getc,
  114. };
  115. static const struct udevice_id arm_dcc_ids[] = {
  116. { .compatible = "arm,dcc", },
  117. { }
  118. };
  119. U_BOOT_DRIVER(serial_dcc) = {
  120. .name = "arm_dcc",
  121. .id = UCLASS_SERIAL,
  122. .of_match = arm_dcc_ids,
  123. .ops = &arm_dcc_ops,
  124. };
  125. #ifdef CONFIG_DEBUG_UART_ARM_DCC
  126. #include <debug_uart.h>
  127. static inline void _debug_uart_init(void)
  128. {
  129. }
  130. static inline void _debug_uart_putc(int ch)
  131. {
  132. arm_dcc_putc(NULL, ch);
  133. }
  134. DEBUG_UART_FUNCS
  135. #endif