uart.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
  3. * reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the NetLogic
  9. * license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY NETLOGIC ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  31. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  32. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef __XLP_HAL_UART_H__
  35. #define __XLP_HAL_UART_H__
  36. /* UART Specific registers */
  37. #define UART_RX_DATA 0x00
  38. #define UART_TX_DATA 0x00
  39. #define UART_INT_EN 0x01
  40. #define UART_INT_ID 0x02
  41. #define UART_FIFO_CTL 0x02
  42. #define UART_LINE_CTL 0x03
  43. #define UART_MODEM_CTL 0x04
  44. #define UART_LINE_STS 0x05
  45. #define UART_MODEM_STS 0x06
  46. #define UART_DIVISOR0 0x00
  47. #define UART_DIVISOR1 0x01
  48. #define BASE_BAUD (XLP_IO_CLK/16)
  49. #define BAUD_DIVISOR(baud) (BASE_BAUD / baud)
  50. /* LCR mask values */
  51. #define LCR_5BITS 0x00
  52. #define LCR_6BITS 0x01
  53. #define LCR_7BITS 0x02
  54. #define LCR_8BITS 0x03
  55. #define LCR_STOPB 0x04
  56. #define LCR_PENAB 0x08
  57. #define LCR_PODD 0x00
  58. #define LCR_PEVEN 0x10
  59. #define LCR_PONE 0x20
  60. #define LCR_PZERO 0x30
  61. #define LCR_SBREAK 0x40
  62. #define LCR_EFR_ENABLE 0xbf
  63. #define LCR_DLAB 0x80
  64. /* MCR mask values */
  65. #define MCR_DTR 0x01
  66. #define MCR_RTS 0x02
  67. #define MCR_DRS 0x04
  68. #define MCR_IE 0x08
  69. #define MCR_LOOPBACK 0x10
  70. /* FCR mask values */
  71. #define FCR_RCV_RST 0x02
  72. #define FCR_XMT_RST 0x04
  73. #define FCR_RX_LOW 0x00
  74. #define FCR_RX_MEDL 0x40
  75. #define FCR_RX_MEDH 0x80
  76. #define FCR_RX_HIGH 0xc0
  77. /* IER mask values */
  78. #define IER_ERXRDY 0x1
  79. #define IER_ETXRDY 0x2
  80. #define IER_ERLS 0x4
  81. #define IER_EMSC 0x8
  82. #if !defined(LOCORE) && !defined(__ASSEMBLY__)
  83. #define nlm_read_uart_reg(b, r) nlm_read_reg(b, r)
  84. #define nlm_write_uart_reg(b, r, v) nlm_write_reg(b, r, v)
  85. #define nlm_get_uart_pcibase(node, inst) \
  86. nlm_pcicfg_base(XLP_IO_UART_OFFSET(node, inst))
  87. #define nlm_get_uart_regbase(node, inst) \
  88. (nlm_get_uart_pcibase(node, inst) + XLP_IO_PCI_HDRSZ)
  89. static inline void
  90. nlm_uart_set_baudrate(uint64_t base, int baud)
  91. {
  92. uint32_t lcr;
  93. lcr = nlm_read_uart_reg(base, UART_LINE_CTL);
  94. /* enable divisor register, and write baud values */
  95. nlm_write_uart_reg(base, UART_LINE_CTL, lcr | (1 << 7));
  96. nlm_write_uart_reg(base, UART_DIVISOR0,
  97. (BAUD_DIVISOR(baud) & 0xff));
  98. nlm_write_uart_reg(base, UART_DIVISOR1,
  99. ((BAUD_DIVISOR(baud) >> 8) & 0xff));
  100. /* restore default lcr */
  101. nlm_write_uart_reg(base, UART_LINE_CTL, lcr);
  102. }
  103. static inline void
  104. nlm_uart_outbyte(uint64_t base, char c)
  105. {
  106. uint32_t lsr;
  107. for (;;) {
  108. lsr = nlm_read_uart_reg(base, UART_LINE_STS);
  109. if (lsr & 0x20)
  110. break;
  111. }
  112. nlm_write_uart_reg(base, UART_TX_DATA, (int)c);
  113. }
  114. static inline char
  115. nlm_uart_inbyte(uint64_t base)
  116. {
  117. int data, lsr;
  118. for (;;) {
  119. lsr = nlm_read_uart_reg(base, UART_LINE_STS);
  120. if (lsr & 0x80) { /* parity/frame/break-error - push a zero */
  121. data = 0;
  122. break;
  123. }
  124. if (lsr & 0x01) { /* Rx data */
  125. data = nlm_read_uart_reg(base, UART_RX_DATA);
  126. break;
  127. }
  128. }
  129. return (char)data;
  130. }
  131. static inline int
  132. nlm_uart_init(uint64_t base, int baud, int databits, int stopbits,
  133. int parity, int int_en, int loopback)
  134. {
  135. uint32_t lcr;
  136. lcr = 0;
  137. if (databits >= 8)
  138. lcr |= LCR_8BITS;
  139. else if (databits == 7)
  140. lcr |= LCR_7BITS;
  141. else if (databits == 6)
  142. lcr |= LCR_6BITS;
  143. else
  144. lcr |= LCR_5BITS;
  145. if (stopbits > 1)
  146. lcr |= LCR_STOPB;
  147. lcr |= parity << 3;
  148. /* setup default lcr */
  149. nlm_write_uart_reg(base, UART_LINE_CTL, lcr);
  150. /* Reset the FIFOs */
  151. nlm_write_uart_reg(base, UART_LINE_CTL, FCR_RCV_RST | FCR_XMT_RST);
  152. nlm_uart_set_baudrate(base, baud);
  153. if (loopback)
  154. nlm_write_uart_reg(base, UART_MODEM_CTL, 0x1f);
  155. if (int_en)
  156. nlm_write_uart_reg(base, UART_INT_EN, IER_ERXRDY | IER_ETXRDY);
  157. return 0;
  158. }
  159. #endif /* !LOCORE && !__ASSEMBLY__ */
  160. #endif /* __XLP_HAL_UART_H__ */