rs485.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * (C) Copyright 2003
  3. * Martin Krause, TQ-Systems GmbH, <martin.krause@tqs.de>
  4. *
  5. * Based on arch/arm/cpu/arm920t/serial.c, by Gary Jennejohn
  6. * (C) Copyright 2002 Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <asm/arch/s3c24x0_cpu.h>
  25. #include "rs485.h"
  26. static void rs485_setbrg (void);
  27. static void rs485_cfgio (void);
  28. static void set_rs485re(unsigned char rs485re_state);
  29. static void set_rs485de(unsigned char rs485de_state);
  30. static void rs485_setbrg (void);
  31. #ifdef NOT_USED
  32. static void trab_rs485_disable_tx(void);
  33. static void trab_rs485_disable_rx(void);
  34. #endif
  35. #define UART_NR S3C24X0_UART1
  36. /* CPLD-Register for controlling TRAB hardware functions */
  37. #define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
  38. static void rs485_setbrg (void)
  39. {
  40. struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
  41. int i;
  42. unsigned int reg = 0;
  43. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  44. /* reg = (33000000 / (16 * gd->baudrate)) - 1; */
  45. reg = (33000000 / (16 * 38400)) - 1;
  46. /* FIFO enable, Tx/Rx FIFO clear */
  47. uart->ufcon = 0x07;
  48. uart->umcon = 0x0;
  49. /* Normal,No parity,1 stop,8 bit */
  50. uart->ulcon = 0x3;
  51. /*
  52. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  53. * normal,interrupt or polling
  54. */
  55. uart->ucon = 0x245;
  56. uart->ubrdiv = reg;
  57. for (i = 0; i < 100; i++);
  58. }
  59. static void rs485_cfgio (void)
  60. {
  61. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  62. gpio->pfcon &= ~(0x3 << 2);
  63. gpio->pfcon |= (0x2 << 2); /* configure GPF1 as RXD1 */
  64. gpio->pfcon &= ~(0x3 << 6);
  65. gpio->pfcon |= (0x2 << 6); /* configure GPF3 as TXD1 */
  66. gpio->pfup |= (1 << 1); /* disable pullup on GPF1 */
  67. gpio->pfup |= (1 << 3); /* disable pullup on GPF3 */
  68. gpio->pacon &= ~(1 << 11); /* set GPA11 (RS485_DE) to output */
  69. }
  70. /*
  71. * Initialise the rs485 port with the given baudrate. The settings
  72. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  73. *
  74. */
  75. int rs485_init (void)
  76. {
  77. rs485_cfgio ();
  78. rs485_setbrg ();
  79. return (0);
  80. }
  81. /*
  82. * Read a single byte from the rs485 port. Returns 1 on success, 0
  83. * otherwise. When the function is succesfull, the character read is
  84. * written into its argument c.
  85. */
  86. int rs485_getc (void)
  87. {
  88. struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
  89. /* wait for character to arrive */
  90. while (!(uart->utrstat & 0x1))
  91. ;
  92. return uart->urxh & 0xff;
  93. }
  94. /*
  95. * Output a single byte to the rs485 port.
  96. */
  97. void rs485_putc (const char c)
  98. {
  99. struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
  100. /* wait for room in the tx FIFO */
  101. while (!(uart->utrstat & 0x2))
  102. ;
  103. uart->utxh = c;
  104. /* If \n, also do \r */
  105. if (c == '\n')
  106. rs485_putc ('\r');
  107. }
  108. /*
  109. * Test whether a character is in the RX buffer
  110. */
  111. int rs485_tstc (void)
  112. {
  113. struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
  114. return uart->utrstat & 0x1;
  115. }
  116. void rs485_puts (const char *s)
  117. {
  118. while (*s) {
  119. rs485_putc (*s++);
  120. }
  121. }
  122. /*
  123. * State table:
  124. * RE DE Result
  125. * 1 1 XMIT
  126. * 0 0 RCV
  127. * 1 0 Shutdown
  128. */
  129. /* function that controls the receiver enable for the rs485 */
  130. /* rs485re_state reflects the level (0/1) of the RE pin */
  131. static void set_rs485re(unsigned char rs485re_state)
  132. {
  133. if(rs485re_state)
  134. *CPLD_RS485_RE = 0x010000;
  135. else
  136. *CPLD_RS485_RE = 0x0;
  137. }
  138. /* function that controls the sender enable for the rs485 */
  139. /* rs485de_state reflects the level (0/1) of the DE pin */
  140. static void set_rs485de(unsigned char rs485de_state)
  141. {
  142. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  143. /* This is on PORT A bit 11 */
  144. if(rs485de_state)
  145. gpio->padat |= (1 << 11);
  146. else
  147. gpio->padat &= ~(1 << 11);
  148. }
  149. void trab_rs485_enable_tx(void)
  150. {
  151. set_rs485de(1);
  152. set_rs485re(1);
  153. }
  154. void trab_rs485_enable_rx(void)
  155. {
  156. set_rs485re(0);
  157. set_rs485de(0);
  158. }
  159. #ifdef NOT_USED
  160. static void trab_rs485_disable_tx(void)
  161. {
  162. set_rs485de(0);
  163. }
  164. static void trab_rs485_disable_rx(void)
  165. {
  166. set_rs485re(1);
  167. }
  168. #endif