s3c4510b_uart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2004 Cucy Systems (http://www.cucy.com)
  3. * Curt Brune <curt@cucy.com>
  4. *
  5. * (C) Copyright 2004
  6. * DAVE Srl
  7. * http://www.dave-tech.it
  8. * http://www.wawnet.biz
  9. * mailto:info@wawnet.biz
  10. *
  11. * (C) Copyright 2002-2004
  12. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  13. *
  14. * (C) Copyright 2002
  15. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  16. * Marius Groeger <mgroeger@sysgo.de>
  17. *
  18. * (C) Copyright 2002
  19. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  20. * Alex Zuepke <azu@sysgo.de>
  21. *
  22. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  37. *
  38. * MODULE: $Id:$
  39. * Description: UART/Serial interface for Samsung S3C4510B SoC
  40. * Runtime Env: ARM7TDMI
  41. * Change History:
  42. * 03-02-04 Create (Curt Brune) curt@cucy.com
  43. *
  44. */
  45. #include <common.h>
  46. #ifdef CONFIG_DRIVER_S3C4510_UART
  47. #include <asm/hardware.h>
  48. #include "s3c4510b_uart.h"
  49. DECLARE_GLOBAL_DATA_PTR;
  50. static UART *uart;
  51. /* flush serial input queue. returns 0 on success or negative error
  52. * number otherwise
  53. */
  54. static int serial_flush_input(void)
  55. {
  56. volatile u32 tmp;
  57. /* keep on reading as long as the receiver is not empty */
  58. while( uart->m_stat.bf.rxReady) {
  59. tmp = uart->m_rx;
  60. }
  61. return 0;
  62. }
  63. /* flush output queue. returns 0 on success or negative error number
  64. * otherwise
  65. */
  66. static int serial_flush_output(void)
  67. {
  68. /* wait until the transmitter is no longer busy */
  69. while( !uart->m_stat.bf.txBufEmpty);
  70. return 0;
  71. }
  72. void serial_setbrg (void)
  73. {
  74. UART_LINE_CTRL ulctrl;
  75. UART_CTRL uctrl;
  76. UART_BAUD_DIV ubd;
  77. serial_flush_output();
  78. serial_flush_input();
  79. /* control register */
  80. uctrl.ui = 0x0;
  81. uctrl.bf.rxMode = 0x1;
  82. uctrl.bf.rxIrq = 0x0;
  83. uctrl.bf.txMode = 0x1;
  84. uctrl.bf.DSR = 0x0;
  85. uctrl.bf.sendBreak = 0x0;
  86. uctrl.bf.loopBack = 0x0;
  87. uart->m_ctrl.ui = uctrl.ui;
  88. /* line control register */
  89. ulctrl.ui = 0x0;
  90. ulctrl.bf.wordLen = 0x3; /* 8 bit data */
  91. ulctrl.bf.nStop = 0x0; /* 1 stop bit */
  92. ulctrl.bf.parity = 0x0; /* no parity */
  93. ulctrl.bf.clk = 0x0; /* internal clock */
  94. ulctrl.bf.infra_red = 0x0; /* no infra_red */
  95. uart->m_lineCtrl.ui = ulctrl.ui;
  96. ubd.ui = 0x0;
  97. /* see table on page 10-15 in SAMSUNG S3C4510B manual */
  98. /* get correct divisor */
  99. switch(gd->baudrate) {
  100. case 1200: ubd.bf.cnt0 = 1301; break;
  101. case 2400: ubd.bf.cnt0 = 650; break;
  102. case 4800: ubd.bf.cnt0 = 324; break;
  103. case 9600: ubd.bf.cnt0 = 162; break;
  104. case 19200: ubd.bf.cnt0 = 80; break;
  105. case 38400: ubd.bf.cnt0 = 40; break;
  106. case 57600: ubd.bf.cnt0 = 26; break;
  107. case 115200: ubd.bf.cnt0 = 13; break;
  108. }
  109. uart->m_baudDiv.ui = ubd.ui;
  110. uart->m_baudCnt = 0x0;
  111. uart->m_baudClk = 0x0;
  112. }
  113. /*
  114. * Initialise the serial port with the given baudrate. The settings
  115. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  116. *
  117. */
  118. int serial_init (void)
  119. {
  120. #if CONFIG_SERIAL1 == 1
  121. uart = (UART *)UART0_BASE;
  122. #elif CONFIG_SERIAL1 == 2
  123. uart = (UART *)UART1_BASE;
  124. #else
  125. #error CONFIG_SERIAL1 not equal to 1 or 2
  126. #endif
  127. serial_setbrg ();
  128. return (0);
  129. }
  130. /*
  131. * Output a single byte to the serial port.
  132. */
  133. void serial_putc (const char c)
  134. {
  135. /* wait for room in the transmit FIFO */
  136. while( !uart->m_stat.bf.txBufEmpty);
  137. uart->m_tx = c;
  138. /*
  139. to be polite with serial console add a line feed
  140. to the carriage return character
  141. */
  142. if (c=='\n')
  143. serial_putc('\r');
  144. }
  145. /*
  146. * Test if an input byte is ready from the serial port. Returns non-zero on
  147. * success, 0 otherwise.
  148. */
  149. int serial_tstc (void)
  150. {
  151. return uart->m_stat.bf.rxReady;
  152. }
  153. /*
  154. * Read a single byte from the serial port. Returns 1 on success, 0
  155. * otherwise. When the function is succesfull, the character read is
  156. * written into its argument c.
  157. */
  158. int serial_getc (void)
  159. {
  160. int rv;
  161. for(;;) {
  162. rv = serial_tstc();
  163. if (rv) {
  164. return uart->m_rx & 0xFF;
  165. }
  166. }
  167. }
  168. void serial_puts (const char *s)
  169. {
  170. while (*s) {
  171. serial_putc (*s++);
  172. }
  173. /* busy wait for tx complete */
  174. while ( !uart->m_stat.bf.txComplete);
  175. /* clear break */
  176. uart->m_ctrl.bf.sendBreak = 0;
  177. }
  178. #endif