jtag-console.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * jtag-console.c - console driver over Blackfin JTAG
  3. *
  4. * Copyright (c) 2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <devices.h>
  10. #include <asm/blackfin.h>
  11. #ifndef CONFIG_JTAG_CONSOLE_TIMEOUT
  12. # define CONFIG_JTAG_CONSOLE_TIMEOUT 100
  13. #endif
  14. /* The Blackfin tends to be much much faster than the JTAG hardware. */
  15. static void jtag_write_emudat(uint32_t emudat)
  16. {
  17. static bool overflowed = false;
  18. ulong timeout = get_timer(0) + CONFIG_JTAG_CONSOLE_TIMEOUT;
  19. while (bfin_read_DBGSTAT() & 0x1) {
  20. if (overflowed)
  21. return;
  22. if (timeout < get_timer(0))
  23. overflowed = true;
  24. }
  25. overflowed = false;
  26. __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
  27. }
  28. /* Transmit a buffer. The format is:
  29. * [32bit length][actual data]
  30. */
  31. static void jtag_send(const char *c, uint32_t len)
  32. {
  33. uint32_t i;
  34. if (len == 0)
  35. return;
  36. /* First send the length */
  37. jtag_write_emudat(len);
  38. /* Then send the data */
  39. for (i = 0; i < len; i += 4)
  40. jtag_write_emudat((c[i] << 0) | (c[i+1] << 8) | (c[i+2] << 16) | (c[i+3] << 24));
  41. }
  42. static void jtag_putc(const char c)
  43. {
  44. jtag_send(&c, 1);
  45. }
  46. static void jtag_puts(const char *s)
  47. {
  48. jtag_send(s, strlen(s));
  49. }
  50. static int jtag_tstc(void)
  51. {
  52. return (bfin_read_DBGSTAT() & 0x2);
  53. }
  54. /* Receive a buffer. The format is:
  55. * [32bit length][actual data]
  56. */
  57. static size_t inbound_len;
  58. static int leftovers_len;
  59. static uint32_t leftovers;
  60. static int jtag_getc(void)
  61. {
  62. int ret;
  63. uint32_t emudat;
  64. /* see if any data is left over */
  65. if (leftovers_len) {
  66. --leftovers_len;
  67. ret = leftovers & 0xff;
  68. leftovers >>= 8;
  69. return ret;
  70. }
  71. /* wait for new data ! */
  72. while (!jtag_tstc())
  73. continue;
  74. __asm__("%0 = emudat;" : "=d"(emudat));
  75. if (inbound_len == 0) {
  76. /* grab the length */
  77. inbound_len = emudat;
  78. } else {
  79. /* store the bytes */
  80. leftovers_len = min(4, inbound_len);
  81. inbound_len -= leftovers_len;
  82. leftovers = emudat;
  83. }
  84. return jtag_getc();
  85. }
  86. int drv_jtag_console_init(void)
  87. {
  88. device_t dev;
  89. int ret;
  90. memset(&dev, 0x00, sizeof(dev));
  91. strcpy(dev.name, "jtag");
  92. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  93. dev.putc = jtag_putc;
  94. dev.puts = jtag_puts;
  95. dev.tstc = jtag_tstc;
  96. dev.getc = jtag_getc;
  97. ret = device_register(&dev);
  98. return (ret == 0 ? 1 : ret);
  99. }
  100. #ifdef CONFIG_UART_CONSOLE_IS_JTAG
  101. /* Since the JTAG is always available (at power on), allow it to fake a UART */
  102. void serial_set_baud(uint32_t baud) {}
  103. void serial_setbrg(void) {}
  104. int serial_init(void) { return 0; }
  105. void serial_putc(const char c) __attribute__((alias("jtag_putc")));
  106. void serial_puts(const char *s) __attribute__((alias("jtag_puts")));
  107. int serial_tstc(void) __attribute__((alias("jtag_tstc")));
  108. int serial_getc(void) __attribute__((alias("jtag_getc")));
  109. #endif