serial_xuartlite.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 const 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. #if !defined(CONFIG_SERIAL_MULTI)
  80. int serial_init(void)
  81. {
  82. /* FIXME: Nothing for now. We should initialize fifo, etc */
  83. return 0;
  84. }
  85. void serial_setbrg(void)
  86. {
  87. /* FIXME: what's this for? */
  88. }
  89. void serial_putc(const char c)
  90. {
  91. uartlite_serial_putc(c, 0);
  92. }
  93. void serial_puts(const char *s)
  94. {
  95. uartlite_serial_puts(s, 0);
  96. }
  97. int serial_getc(void)
  98. {
  99. return uartlite_serial_getc(0);
  100. }
  101. int serial_tstc(void)
  102. {
  103. return uartlite_serial_tstc(0);
  104. }
  105. #endif
  106. #if defined(CONFIG_SERIAL_MULTI)
  107. /* Multi serial device functions */
  108. #define DECLARE_ESERIAL_FUNCTIONS(port) \
  109. int userial##port##_init(void) \
  110. { return(0); } \
  111. void userial##port##_setbrg(void) {} \
  112. int userial##port##_getc(void) \
  113. { return uartlite_serial_getc(port); } \
  114. int userial##port##_tstc(void) \
  115. { return uartlite_serial_tstc(port); } \
  116. void userial##port##_putc(const char c) \
  117. { uartlite_serial_putc(c, port); } \
  118. void userial##port##_puts(const char *s) \
  119. { uartlite_serial_puts(s, port); }
  120. /* Serial device descriptor */
  121. #define INIT_ESERIAL_STRUCTURE(port, name) {\
  122. name,\
  123. userial##port##_init,\
  124. NULL,\
  125. userial##port##_setbrg,\
  126. userial##port##_getc,\
  127. userial##port##_tstc,\
  128. userial##port##_putc,\
  129. userial##port##_puts, }
  130. DECLARE_ESERIAL_FUNCTIONS(0);
  131. struct serial_device uartlite_serial0_device =
  132. INIT_ESERIAL_STRUCTURE(0, "ttyUL0");
  133. DECLARE_ESERIAL_FUNCTIONS(1);
  134. struct serial_device uartlite_serial1_device =
  135. INIT_ESERIAL_STRUCTURE(1, "ttyUL1");
  136. DECLARE_ESERIAL_FUNCTIONS(2);
  137. struct serial_device uartlite_serial2_device =
  138. INIT_ESERIAL_STRUCTURE(2, "ttyUL2");
  139. DECLARE_ESERIAL_FUNCTIONS(3);
  140. struct serial_device uartlite_serial3_device =
  141. INIT_ESERIAL_STRUCTURE(3, "ttyUL3");
  142. __weak struct serial_device *default_serial_console(void)
  143. {
  144. # ifdef XILINX_UARTLITE_BASEADDR
  145. return &uartlite_serial0_device;
  146. # endif /* XILINX_UARTLITE_BASEADDR */
  147. # ifdef XILINX_UARTLITE_BASEADDR1
  148. return &uartlite_serial1_device;
  149. # endif /* XILINX_UARTLITE_BASEADDR1 */
  150. # ifdef XILINX_UARTLITE_BASEADDR2
  151. return &uartlite_serial2_device;
  152. # endif /* XILINX_UARTLITE_BASEADDR2 */
  153. # ifdef XILINX_UARTLITE_BASEADDR3
  154. return &uartlite_serial3_device;
  155. # endif /* XILINX_UARTLITE_BASEADDR3 */
  156. }
  157. #endif /* CONFIG_SERIAL_MULTI */