serialGT.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * serialGT.c
  3. *
  4. * BRIEF MODULE DESCRIPTION
  5. * Low Level Serial Port control for use
  6. * with the Galileo EVB64120A MIPS eval board and
  7. * its on board two channel 16552 Uart.
  8. *
  9. * Copyright (C) 2000 RidgeRun, Inc.
  10. * Author: RidgeRun, Inc.
  11. * glonnon@ridgerun.com, skranz@ridgerun.com, stevej@ridgerun.com
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  24. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. */
  34. // Note:
  35. // Serial CHANNELS - 0 is the bottom connector of evb64120A.
  36. // (The one that maps to the "B" channel of the
  37. // board's uart)
  38. // 1 is the top connector of evb64120A.
  39. // (The one that maps to the "A" channel of the
  40. // board's uart)
  41. int DEBUG_CHANNEL = 0; // See Note Above
  42. int CONSOLE_CHANNEL = 1; // See Note Above
  43. #define DUART 0xBD000000 /* Base address of Uart. */
  44. #define CHANNELOFFSET 0x20 /* DUART+CHANNELOFFSET gets you to the ChanA
  45. register set of the 16552 Uart device.
  46. DUART+0 gets you to the ChanB register set.
  47. */
  48. #define DUART_DELTA 0x4
  49. #define FIFO_ENABLE 0x07
  50. #define INT_ENABLE 0x04 /* default interrupt mask */
  51. #define RBR 0x00
  52. #define THR 0x00
  53. #define DLL 0x00
  54. #define IER 0x01
  55. #define DLM 0x01
  56. #define IIR 0x02
  57. #define FCR 0x02
  58. #define LCR 0x03
  59. #define MCR 0x04
  60. #define LSR 0x05
  61. #define MSR 0x06
  62. #define SCR 0x07
  63. #define LCR_DLAB 0x80
  64. #define XTAL 1843200
  65. #define LSR_THRE 0x20
  66. #define LSR_BI 0x10
  67. #define LSR_DR 0x01
  68. #define MCR_LOOP 0x10
  69. #define ACCESS_DELAY 0x10000
  70. /******************************
  71. Routine:
  72. Description:
  73. ******************************/
  74. int inreg(int channel, int reg)
  75. {
  76. int val;
  77. val =
  78. *((volatile unsigned char *) DUART +
  79. (channel * CHANNELOFFSET) + (reg * DUART_DELTA));
  80. return val;
  81. }
  82. /******************************
  83. Routine:
  84. Description:
  85. ******************************/
  86. void outreg(int channel, int reg, unsigned char val)
  87. {
  88. *((volatile unsigned char *) DUART + (channel * CHANNELOFFSET)
  89. + (reg * DUART_DELTA)) = val;
  90. }
  91. /******************************
  92. Routine:
  93. Description:
  94. Initialize the device driver.
  95. ******************************/
  96. void serial_init(int channel)
  97. {
  98. /*
  99. * Configure active port, (CHANNELOFFSET already set.)
  100. *
  101. * Set 8 bits, 1 stop bit, no parity.
  102. *
  103. * LCR<7> 0 divisor latch access bit
  104. * LCR<6> 0 break control (1=send break)
  105. * LCR<5> 0 stick parity (0=space, 1=mark)
  106. * LCR<4> 0 parity even (0=odd, 1=even)
  107. * LCR<3> 0 parity enable (1=enabled)
  108. * LCR<2> 0 # stop bits (0=1, 1=1.5)
  109. * LCR<1:0> 11 bits per character(00=5, 01=6, 10=7, 11=8)
  110. */
  111. outreg(channel, LCR, 0x3);
  112. outreg(channel, FCR, FIFO_ENABLE); /* Enable the FIFO */
  113. outreg(channel, IER, INT_ENABLE); /* Enable appropriate interrupts */
  114. }
  115. /******************************
  116. Routine:
  117. Description:
  118. Set the baud rate.
  119. ******************************/
  120. void serial_set(int channel, unsigned long baud)
  121. {
  122. unsigned char sav_lcr;
  123. /*
  124. * Enable access to the divisor latches by setting DLAB in LCR.
  125. *
  126. */
  127. sav_lcr = inreg(channel, LCR);
  128. #if 0
  129. /*
  130. * Set baud rate
  131. */
  132. outreg(channel, LCR, LCR_DLAB | sav_lcr);
  133. // outreg(DLL,(XTAL/(16*2*(baud))-2));
  134. outreg(channel, DLL, XTAL / (16 * baud));
  135. // outreg(DLM,(XTAL/(16*2*(baud))-2)>>8);
  136. outreg(channel, DLM, (XTAL / (16 * baud)) >> 8);
  137. #else
  138. /*
  139. * Note: Set baud rate, hardcoded here for rate of 115200
  140. * since became unsure of above "buad rate" algorithm (??).
  141. */
  142. outreg(channel, LCR, 0x83);
  143. outreg(channel, DLM, 0x00); // See note above
  144. outreg(channel, DLL, 0x02); // See note above.
  145. outreg(channel, LCR, 0x03);
  146. #endif
  147. /*
  148. * Restore line control register
  149. */
  150. outreg(channel, LCR, sav_lcr);
  151. }
  152. /******************************
  153. Routine:
  154. Description:
  155. Transmit a character.
  156. ******************************/
  157. void serial_putc(int channel, int c)
  158. {
  159. while ((inreg(channel, LSR) & LSR_THRE) == 0);
  160. outreg(channel, THR, c);
  161. }
  162. /******************************
  163. Routine:
  164. Description:
  165. Read a received character if one is
  166. available. Return -1 otherwise.
  167. ******************************/
  168. int serial_getc(int channel)
  169. {
  170. if (inreg(channel, LSR) & LSR_DR) {
  171. return inreg(channel, RBR);
  172. }
  173. return -1;
  174. }
  175. /******************************
  176. Routine:
  177. Description:
  178. Used by embedded gdb client. (example; gdb-stub.c)
  179. ******************************/
  180. char getDebugChar()
  181. {
  182. int val;
  183. while ((val = serial_getc(DEBUG_CHANNEL)) == -1); // loop until we get a character in.
  184. return (char) val;
  185. }
  186. /******************************
  187. Routine:
  188. Description:
  189. Used by embedded gdb target. (example; gdb-stub.c)
  190. ******************************/
  191. void putDebugChar(char c)
  192. {
  193. serial_putc(DEBUG_CHANNEL, (int) c);
  194. }