serial_arc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <common.h>
  10. #include <serial.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. struct arc_serial_regs {
  13. unsigned int id0;
  14. unsigned int id1;
  15. unsigned int id2;
  16. unsigned int id3;
  17. unsigned int data;
  18. unsigned int status;
  19. unsigned int baudl;
  20. unsigned int baudh;
  21. };
  22. /* Bit definitions of STATUS register */
  23. #define UART_RXEMPTY (1 << 5)
  24. #define UART_OVERFLOW_ERR (1 << 1)
  25. #define UART_TXEMPTY (1 << 7)
  26. struct arc_serial_regs *regs;
  27. static void arc_serial_setbrg(void)
  28. {
  29. int arc_console_baud;
  30. if (!gd->baudrate)
  31. gd->baudrate = CONFIG_BAUDRATE;
  32. arc_console_baud = gd->cpu_clk / (gd->baudrate * 4) - 1;
  33. writeb(arc_console_baud & 0xff, &regs->baudl);
  34. #ifdef CONFIG_ARC
  35. /*
  36. * UART ISS(Instruction Set simulator) emulation has a subtle bug:
  37. * A existing value of Baudh = 0 is used as a indication to startup
  38. * it's internal state machine.
  39. * Thus if baudh is set to 0, 2 times, it chokes.
  40. * This happens with BAUD=115200 and the formaula above
  41. * Until that is fixed, when running on ISS, we will set baudh to !0
  42. */
  43. if (gd->arch.running_on_hw)
  44. writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
  45. else
  46. writeb(1, &regs->baudh);
  47. #else
  48. writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
  49. #endif
  50. }
  51. static int arc_serial_init(void)
  52. {
  53. regs = (struct arc_serial_regs *)CONFIG_ARC_UART_BASE;
  54. serial_setbrg();
  55. return 0;
  56. }
  57. static void arc_serial_putc(const char c)
  58. {
  59. if (c == '\n')
  60. arc_serial_putc('\r');
  61. while (!(readb(&regs->status) & UART_TXEMPTY))
  62. ;
  63. writeb(c, &regs->data);
  64. }
  65. static int arc_serial_tstc(void)
  66. {
  67. return !(readb(&regs->status) & UART_RXEMPTY);
  68. }
  69. static int arc_serial_getc(void)
  70. {
  71. while (!arc_serial_tstc())
  72. ;
  73. /* Check for overflow errors */
  74. if (readb(&regs->status) & UART_OVERFLOW_ERR)
  75. return 0;
  76. return readb(&regs->data) & 0xFF;
  77. }
  78. static struct serial_device arc_serial_drv = {
  79. .name = "arc_serial",
  80. .start = arc_serial_init,
  81. .stop = NULL,
  82. .setbrg = arc_serial_setbrg,
  83. .putc = arc_serial_putc,
  84. .puts = default_serial_puts,
  85. .getc = arc_serial_getc,
  86. .tstc = arc_serial_tstc,
  87. };
  88. void arc_serial_initialize(void)
  89. {
  90. serial_register(&arc_serial_drv);
  91. }
  92. __weak struct serial_device *default_serial_console(void)
  93. {
  94. return &arc_serial_drv;
  95. }