arm_dcc.c 3.6 KB

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