serial.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* GRLIB APBUART Serial controller driver
  2. *
  3. * (C) Copyright 2007
  4. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <common.h>
  26. #include <asm/processor.h>
  27. #include <asm/leon.h>
  28. #include <ambapp.h>
  29. #include <serial.h>
  30. #include <linux/compiler.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* Force cache miss each time a serial controller reg is read */
  33. #define CACHE_BYPASS 1
  34. #ifdef CACHE_BYPASS
  35. #define READ_BYTE(var) SPARC_NOCACHE_READ_BYTE((unsigned int)&(var))
  36. #define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var))
  37. #define READ_WORD(var) SPARC_NOCACHE_READ((unsigned int)&(var))
  38. #define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var))
  39. #endif
  40. ambapp_dev_apbuart *leon3_apbuart = NULL;
  41. static int leon3_serial_init(void)
  42. {
  43. ambapp_apbdev apbdev;
  44. unsigned int tmp;
  45. /* find UART */
  46. if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) {
  47. leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address;
  48. /* found apbuart, let's init...
  49. *
  50. * Set scaler / baud rate
  51. *
  52. * Receiver & transmitter enable
  53. */
  54. leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER;
  55. /* Let bit 11 be unchanged (debug bit for GRMON) */
  56. tmp = READ_WORD(leon3_apbuart->ctrl);
  57. leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) |
  58. LEON_REG_UART_CTRL_RE |
  59. LEON_REG_UART_CTRL_TE);
  60. return 0;
  61. }
  62. return -1; /* didn't find hardware */
  63. }
  64. static void leon3_serial_putc_raw(const char c)
  65. {
  66. if (!leon3_apbuart)
  67. return;
  68. /* Wait for last character to go. */
  69. while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ;
  70. /* Send data */
  71. leon3_apbuart->data = c;
  72. #ifdef LEON_DEBUG
  73. /* Wait for data to be sent */
  74. while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ;
  75. #endif
  76. }
  77. static void leon3_serial_putc(const char c)
  78. {
  79. if (c == '\n')
  80. leon3_serial_putc_raw('\r');
  81. leon3_serial_putc_raw(c);
  82. }
  83. static int leon3_serial_getc(void)
  84. {
  85. if (!leon3_apbuart)
  86. return 0;
  87. /* Wait for a character to arrive. */
  88. while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ;
  89. /* read data */
  90. return READ_WORD(leon3_apbuart->data);
  91. }
  92. static int leon3_serial_tstc(void)
  93. {
  94. if (leon3_apbuart)
  95. return (READ_WORD(leon3_apbuart->status) &
  96. LEON_REG_UART_STATUS_DR);
  97. return 0;
  98. }
  99. /* set baud rate for uart */
  100. static void leon3_serial_setbrg(void)
  101. {
  102. /* update baud rate settings, read it from gd->baudrate */
  103. unsigned int scaler;
  104. if (leon3_apbuart && (gd->baudrate > 0)) {
  105. scaler =
  106. (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
  107. 5) / 10;
  108. leon3_apbuart->scaler = scaler;
  109. }
  110. return;
  111. }
  112. static struct serial_device leon3_serial_drv = {
  113. .name = "leon3_serial",
  114. .start = leon3_serial_init,
  115. .stop = NULL,
  116. .setbrg = leon3_serial_setbrg,
  117. .putc = leon3_serial_putc,
  118. .puts = default_serial_puts,
  119. .getc = leon3_serial_getc,
  120. .tstc = leon3_serial_tstc,
  121. };
  122. void leon3_serial_initialize(void)
  123. {
  124. serial_register(&leon3_serial_drv);
  125. }
  126. __weak struct serial_device *default_serial_console(void)
  127. {
  128. return &leon3_serial_drv;
  129. }