serial_xuartlite.c 4.6 KB

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