serial_xuartlite.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * (C) Copyright 2008-2011 Michal Simek <monstr@monstr.eu>
  3. * Clean driver and add xilinx constant from header file
  4. *
  5. * (C) Copyright 2004 Atmark Techno, Inc.
  6. * Yasushi SHOJI <yashi@atmark-techno.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <config.h>
  27. #include <common.h>
  28. #include <asm/io.h>
  29. #include <linux/compiler.h>
  30. #include <serial.h>
  31. #define SR_TX_FIFO_FULL 0x08 /* transmit FIFO full */
  32. #define SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */
  33. #define SR_RX_FIFO_FULL 0x02 /* receive FIFO full */
  34. struct uartlite {
  35. unsigned int rx_fifo;
  36. unsigned int tx_fifo;
  37. unsigned int status;
  38. };
  39. static struct uartlite *userial_ports[4] = {
  40. #ifdef XILINX_UARTLITE_BASEADDR
  41. [0] = (struct uartlite *)XILINX_UARTLITE_BASEADDR,
  42. #endif
  43. #ifdef XILINX_UARTLITE_BASEADDR1
  44. [1] = (struct uartlite *)XILINX_UARTLITE_BASEADDR1,
  45. #endif
  46. #ifdef XILINX_UARTLITE_BASEADDR2
  47. [2] = (struct uartlite *)XILINX_UARTLITE_BASEADDR2,
  48. #endif
  49. #ifdef XILINX_UARTLITE_BASEADDR3
  50. [3] = (struct uartlite *)XILINX_UARTLITE_BASEADDR3
  51. #endif
  52. };
  53. void uartlite_serial_putc(const char c, const int port)
  54. {
  55. struct uartlite *regs = userial_ports[port];
  56. if (c == '\n')
  57. uartlite_serial_putc('\r', port);
  58. while (in_be32(&regs->status) & SR_TX_FIFO_FULL)
  59. ;
  60. out_be32(&regs->tx_fifo, c & 0xff);
  61. }
  62. void uartlite_serial_puts(const char *s, const int port)
  63. {
  64. while (*s)
  65. uartlite_serial_putc(*s++, port);
  66. }
  67. int uartlite_serial_getc(const int port)
  68. {
  69. struct uartlite *regs = userial_ports[port];
  70. while (!(in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA))
  71. ;
  72. return in_be32(&regs->rx_fifo) & 0xff;
  73. }
  74. int uartlite_serial_tstc(const int port)
  75. {
  76. struct uartlite *regs = userial_ports[port];
  77. return in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA;
  78. }
  79. static int uartlite_serial_init(const int port)
  80. {
  81. if (userial_ports[port])
  82. return 0;
  83. return -1;
  84. }
  85. /* Multi serial device functions */
  86. #define DECLARE_ESERIAL_FUNCTIONS(port) \
  87. int userial##port##_init(void) \
  88. { return uartlite_serial_init(port); } \
  89. void userial##port##_setbrg(void) {} \
  90. int userial##port##_getc(void) \
  91. { return uartlite_serial_getc(port); } \
  92. int userial##port##_tstc(void) \
  93. { return uartlite_serial_tstc(port); } \
  94. void userial##port##_putc(const char c) \
  95. { uartlite_serial_putc(c, port); } \
  96. void userial##port##_puts(const char *s) \
  97. { uartlite_serial_puts(s, port); }
  98. /* Serial device descriptor */
  99. #define INIT_ESERIAL_STRUCTURE(port, __name) { \
  100. .name = __name, \
  101. .start = userial##port##_init, \
  102. .stop = NULL, \
  103. .setbrg = userial##port##_setbrg, \
  104. .getc = userial##port##_getc, \
  105. .tstc = userial##port##_tstc, \
  106. .putc = userial##port##_putc, \
  107. .puts = userial##port##_puts, \
  108. }
  109. DECLARE_ESERIAL_FUNCTIONS(0);
  110. struct serial_device uartlite_serial0_device =
  111. INIT_ESERIAL_STRUCTURE(0, "ttyUL0");
  112. DECLARE_ESERIAL_FUNCTIONS(1);
  113. struct serial_device uartlite_serial1_device =
  114. INIT_ESERIAL_STRUCTURE(1, "ttyUL1");
  115. DECLARE_ESERIAL_FUNCTIONS(2);
  116. struct serial_device uartlite_serial2_device =
  117. INIT_ESERIAL_STRUCTURE(2, "ttyUL2");
  118. DECLARE_ESERIAL_FUNCTIONS(3);
  119. struct serial_device uartlite_serial3_device =
  120. INIT_ESERIAL_STRUCTURE(3, "ttyUL3");
  121. __weak struct serial_device *default_serial_console(void)
  122. {
  123. if (userial_ports[0])
  124. return &uartlite_serial0_device;
  125. if (userial_ports[1])
  126. return &uartlite_serial1_device;
  127. if (userial_ports[2])
  128. return &uartlite_serial2_device;
  129. if (userial_ports[3])
  130. return &uartlite_serial3_device;
  131. return NULL;
  132. }
  133. void uartlite_serial_initialize(void)
  134. {
  135. #ifdef XILINX_UARTLITE_BASEADDR
  136. serial_register(&uartlite_serial0_device);
  137. #endif /* XILINX_UARTLITE_BASEADDR */
  138. #ifdef XILINX_UARTLITE_BASEADDR1
  139. serial_register(&uartlite_serial1_device);
  140. #endif /* XILINX_UARTLITE_BASEADDR1 */
  141. #ifdef XILINX_UARTLITE_BASEADDR2
  142. serial_register(&uartlite_serial2_device);
  143. #endif /* XILINX_UARTLITE_BASEADDR2 */
  144. #ifdef XILINX_UARTLITE_BASEADDR3
  145. serial_register(&uartlite_serial3_device);
  146. #endif /* XILINX_UARTLITE_BASEADDR3 */
  147. }