serial_s3c44b0.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * (C) Copyright 2004
  3. * DAVE Srl
  4. * http://www.dave-tech.it
  5. * http://www.wawnet.biz
  6. * mailto:info@wawnet.biz
  7. *
  8. * (C) Copyright 2002-2004
  9. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Marius Groeger <mgroeger@sysgo.de>
  14. *
  15. * (C) Copyright 2002
  16. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  17. * Alex Zuepke <azu@sysgo.de>
  18. *
  19. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34. *
  35. */
  36. #include <common.h>
  37. #include <asm/hardware.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. /* flush serial input queue. returns 0 on success or negative error
  40. * number otherwise
  41. */
  42. static int serial_flush_input(void)
  43. {
  44. volatile u32 tmp;
  45. /* keep on reading as long as the receiver is not empty */
  46. while(UTRSTAT0&0x01) {
  47. tmp = REGB(URXH0);
  48. }
  49. return 0;
  50. }
  51. /* flush output queue. returns 0 on success or negative error number
  52. * otherwise
  53. */
  54. static int serial_flush_output(void)
  55. {
  56. /* wait until the transmitter is no longer busy */
  57. while(!(UTRSTAT0 & 0x02)) {
  58. }
  59. return 0;
  60. }
  61. void serial_setbrg (void)
  62. {
  63. u32 divisor = 0;
  64. /* get correct divisor */
  65. switch(gd->baudrate) {
  66. case 1200:
  67. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  68. divisor = 3124;
  69. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  70. divisor = 3905;
  71. #else
  72. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  73. #endif
  74. break;
  75. case 9600:
  76. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  77. divisor = 390;
  78. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  79. divisor = 487;
  80. #else
  81. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  82. #endif
  83. break;
  84. case 19200:
  85. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  86. divisor = 194;
  87. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  88. divisor = 243;
  89. #else
  90. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  91. #endif
  92. break;
  93. case 38400:
  94. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  95. divisor = 97;
  96. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  97. divisor = 121;
  98. #else
  99. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  100. #endif /* break; */
  101. case 57600:
  102. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  103. divisor = 64;
  104. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  105. divisor = 80;
  106. #else
  107. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  108. #endif /* break; */
  109. case 115200:
  110. #if CONFIG_S3C44B0_CLOCK_SPEED==66
  111. divisor = 32;
  112. #elif CONFIG_S3C44B0_CLOCK_SPEED==75
  113. divisor = 40;
  114. #else
  115. # error CONFIG_S3C44B0_CLOCK_SPEED undefined
  116. #endif /* break; */
  117. }
  118. serial_flush_output();
  119. serial_flush_input();
  120. UFCON0 = 0x0;
  121. ULCON0 = 0x03;
  122. UCON0 = 0x05;
  123. UBRDIV0 = divisor;
  124. UFCON1 = 0x0;
  125. ULCON1 = 0x03;
  126. UCON1 = 0x05;
  127. UBRDIV1 = divisor;
  128. for(divisor=0; divisor<100; divisor++) {
  129. /* NOP */
  130. }
  131. }
  132. /*
  133. * Initialise the serial port with the given baudrate. The settings
  134. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  135. *
  136. */
  137. int serial_init (void)
  138. {
  139. serial_setbrg ();
  140. return (0);
  141. }
  142. /*
  143. * Output a single byte to the serial port.
  144. */
  145. void serial_putc (const char c)
  146. {
  147. /* wait for room in the transmit FIFO */
  148. while(!(UTRSTAT0 & 0x02));
  149. UTXH0 = (unsigned char)c;
  150. /*
  151. to be polite with serial console add a line feed
  152. to the carriage return character
  153. */
  154. if (c=='\n')
  155. serial_putc('\r');
  156. }
  157. /*
  158. * Read a single byte from the serial port. Returns 1 on success, 0
  159. * otherwise. When the function is succesfull, the character read is
  160. * written into its argument c.
  161. */
  162. int serial_tstc (void)
  163. {
  164. return (UTRSTAT0 & 0x01);
  165. }
  166. /*
  167. * Read a single byte from the serial port. Returns 1 on success, 0
  168. * otherwise. When the function is succesfull, the character read is
  169. * written into its argument c.
  170. */
  171. int serial_getc (void)
  172. {
  173. int rv;
  174. for(;;) {
  175. rv = serial_tstc();
  176. if(rv > 0)
  177. return URXH0;
  178. }
  179. }
  180. void
  181. serial_puts (const char *s)
  182. {
  183. while (*s) {
  184. serial_putc (*s++);
  185. }
  186. }