serial_s3c24x0.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <linux/compiler.h>
  22. #include <asm/arch/s3c24x0_cpu.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifdef CONFIG_SERIAL1
  25. #define UART_NR S3C24X0_UART0
  26. #elif defined(CONFIG_SERIAL2)
  27. #define UART_NR S3C24X0_UART1
  28. #elif defined(CONFIG_SERIAL3)
  29. #define UART_NR S3C24X0_UART2
  30. #else
  31. #error "Bad: you didn't configure serial ..."
  32. #endif
  33. #include <asm/io.h>
  34. #if defined(CONFIG_SERIAL_MULTI)
  35. #include <serial.h>
  36. /* Multi serial device functions */
  37. #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
  38. int s3serial##port##_init(void) \
  39. { \
  40. return serial_init_dev(port); \
  41. } \
  42. void s3serial##port##_setbrg(void) \
  43. { \
  44. serial_setbrg_dev(port); \
  45. } \
  46. int s3serial##port##_getc(void) \
  47. { \
  48. return serial_getc_dev(port); \
  49. } \
  50. int s3serial##port##_tstc(void) \
  51. { \
  52. return serial_tstc_dev(port); \
  53. } \
  54. void s3serial##port##_putc(const char c) \
  55. { \
  56. serial_putc_dev(port, c); \
  57. } \
  58. void s3serial##port##_puts(const char *s) \
  59. { \
  60. serial_puts_dev(port, s); \
  61. }
  62. #define INIT_S3C_SERIAL_STRUCTURE(port, name) { \
  63. name, \
  64. s3serial##port##_init, \
  65. NULL,\
  66. s3serial##port##_setbrg, \
  67. s3serial##port##_getc, \
  68. s3serial##port##_tstc, \
  69. s3serial##port##_putc, \
  70. s3serial##port##_puts, \
  71. }
  72. #endif /* CONFIG_SERIAL_MULTI */
  73. #ifdef CONFIG_HWFLOW
  74. static int hwflow;
  75. #endif
  76. void _serial_setbrg(const int dev_index)
  77. {
  78. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  79. unsigned int reg = 0;
  80. int i;
  81. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  82. reg = get_PCLK() / (16 * gd->baudrate) - 1;
  83. writel(reg, &uart->ubrdiv);
  84. for (i = 0; i < 100; i++)
  85. /* Delay */ ;
  86. }
  87. #if defined(CONFIG_SERIAL_MULTI)
  88. static inline void serial_setbrg_dev(unsigned int dev_index)
  89. {
  90. _serial_setbrg(dev_index);
  91. }
  92. #else
  93. void serial_setbrg(void)
  94. {
  95. _serial_setbrg(UART_NR);
  96. }
  97. #endif
  98. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  99. * 1 stop bit, no start bits.
  100. */
  101. static int serial_init_dev(const int dev_index)
  102. {
  103. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  104. #ifdef CONFIG_HWFLOW
  105. hwflow = 0; /* turned off by default */
  106. #endif
  107. /* FIFO enable, Tx/Rx FIFO clear */
  108. writel(0x07, &uart->ufcon);
  109. writel(0x0, &uart->umcon);
  110. /* Normal,No parity,1 stop,8 bit */
  111. writel(0x3, &uart->ulcon);
  112. /*
  113. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  114. * normal,interrupt or polling
  115. */
  116. writel(0x245, &uart->ucon);
  117. #ifdef CONFIG_HWFLOW
  118. writel(0x1, &uart->umcon); /* rts up */
  119. #endif
  120. /* FIXME: This is sooooooooooooooooooo ugly */
  121. #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
  122. /* we need auto hw flow control on the gsm and gps port */
  123. if (dev_index == 0 || dev_index == 1)
  124. writel(0x10, &uart->umcon);
  125. #endif
  126. _serial_setbrg(dev_index);
  127. return (0);
  128. }
  129. #if !defined(CONFIG_SERIAL_MULTI)
  130. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  131. * 1 stop bit, no start bits.
  132. */
  133. int serial_init(void)
  134. {
  135. return serial_init_dev(UART_NR);
  136. }
  137. #endif
  138. /*
  139. * Read a single byte from the serial port. Returns 1 on success, 0
  140. * otherwise. When the function is succesfull, the character read is
  141. * written into its argument c.
  142. */
  143. int _serial_getc(const int dev_index)
  144. {
  145. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  146. while (!(readl(&uart->utrstat) & 0x1))
  147. /* wait for character to arrive */ ;
  148. return readb(&uart->urxh) & 0xff;
  149. }
  150. #if defined(CONFIG_SERIAL_MULTI)
  151. static inline int serial_getc_dev(unsigned int dev_index)
  152. {
  153. return _serial_getc(dev_index);
  154. }
  155. #else
  156. int serial_getc(void)
  157. {
  158. return _serial_getc(UART_NR);
  159. }
  160. #endif
  161. #ifdef CONFIG_HWFLOW
  162. int hwflow_onoff(int on)
  163. {
  164. switch (on) {
  165. case 0:
  166. default:
  167. break; /* return current */
  168. case 1:
  169. hwflow = 1; /* turn on */
  170. break;
  171. case -1:
  172. hwflow = 0; /* turn off */
  173. break;
  174. }
  175. return hwflow;
  176. }
  177. #endif
  178. #ifdef CONFIG_MODEM_SUPPORT
  179. static int be_quiet = 0;
  180. void disable_putc(void)
  181. {
  182. be_quiet = 1;
  183. }
  184. void enable_putc(void)
  185. {
  186. be_quiet = 0;
  187. }
  188. #endif
  189. /*
  190. * Output a single byte to the serial port.
  191. */
  192. void _serial_putc(const char c, const int dev_index)
  193. {
  194. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  195. #ifdef CONFIG_MODEM_SUPPORT
  196. if (be_quiet)
  197. return;
  198. #endif
  199. while (!(readl(&uart->utrstat) & 0x2))
  200. /* wait for room in the tx FIFO */ ;
  201. #ifdef CONFIG_HWFLOW
  202. while (hwflow && !(readl(&uart->umstat) & 0x1))
  203. /* Wait for CTS up */ ;
  204. #endif
  205. writeb(c, &uart->utxh);
  206. /* If \n, also do \r */
  207. if (c == '\n')
  208. serial_putc('\r');
  209. }
  210. #if defined(CONFIG_SERIAL_MULTI)
  211. static inline void serial_putc_dev(unsigned int dev_index, const char c)
  212. {
  213. _serial_putc(c, dev_index);
  214. }
  215. #else
  216. void serial_putc(const char c)
  217. {
  218. _serial_putc(c, UART_NR);
  219. }
  220. #endif
  221. /*
  222. * Test whether a character is in the RX buffer
  223. */
  224. int _serial_tstc(const int dev_index)
  225. {
  226. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  227. return readl(&uart->utrstat) & 0x1;
  228. }
  229. #if defined(CONFIG_SERIAL_MULTI)
  230. static inline int serial_tstc_dev(unsigned int dev_index)
  231. {
  232. return _serial_tstc(dev_index);
  233. }
  234. #else
  235. int serial_tstc(void)
  236. {
  237. return _serial_tstc(UART_NR);
  238. }
  239. #endif
  240. void _serial_puts(const char *s, const int dev_index)
  241. {
  242. while (*s) {
  243. _serial_putc(*s++, dev_index);
  244. }
  245. }
  246. #if defined(CONFIG_SERIAL_MULTI)
  247. static inline void serial_puts_dev(int dev_index, const char *s)
  248. {
  249. _serial_puts(s, dev_index);
  250. }
  251. #else
  252. void serial_puts(const char *s)
  253. {
  254. _serial_puts(s, UART_NR);
  255. }
  256. #endif
  257. #if defined(CONFIG_SERIAL_MULTI)
  258. DECLARE_S3C_SERIAL_FUNCTIONS(0);
  259. struct serial_device s3c24xx_serial0_device =
  260. INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
  261. DECLARE_S3C_SERIAL_FUNCTIONS(1);
  262. struct serial_device s3c24xx_serial1_device =
  263. INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
  264. DECLARE_S3C_SERIAL_FUNCTIONS(2);
  265. struct serial_device s3c24xx_serial2_device =
  266. INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
  267. __weak struct serial_device *default_serial_console(void)
  268. {
  269. #if defined(CONFIG_SERIAL1)
  270. return &s3c24xx_serial0_device;
  271. #elif defined(CONFIG_SERIAL2)
  272. return &s3c24xx_serial1_device;
  273. #elif defined(CONFIG_SERIAL3)
  274. return &s3c24xx_serial2_device;
  275. #else
  276. #error "CONFIG_SERIAL? missing."
  277. #endif
  278. }
  279. #endif /* CONFIG_SERIAL_MULTI */