serial.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * (C) Copyright 2000
  3. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <ns16550.h>
  25. #ifdef CONFIG_NS87308
  26. #include <ns87308.h>
  27. #endif
  28. #if defined (CONFIG_SERIAL_MULTI)
  29. #include <serial.h>
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #if !defined(CONFIG_CONS_INDEX)
  33. #if defined (CONFIG_SERIAL_MULTI)
  34. /* with CONFIG_SERIAL_MULTI we might have no console
  35. * on these devices
  36. */
  37. #else
  38. #error "No console index specified."
  39. #endif /* CONFIG_SERIAL_MULTI */
  40. #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
  41. #error "Invalid console index value."
  42. #endif
  43. #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
  44. #error "Console port 1 defined but not configured."
  45. #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
  46. #error "Console port 2 defined but not configured."
  47. #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
  48. #error "Console port 3 defined but not configured."
  49. #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
  50. #error "Console port 4 defined but not configured."
  51. #endif
  52. /* Note: The port number specified in the functions is 1 based.
  53. * the array is 0 based.
  54. */
  55. static NS16550_t serial_ports[4] = {
  56. #ifdef CONFIG_SYS_NS16550_COM1
  57. (NS16550_t)CONFIG_SYS_NS16550_COM1,
  58. #else
  59. NULL,
  60. #endif
  61. #ifdef CONFIG_SYS_NS16550_COM2
  62. (NS16550_t)CONFIG_SYS_NS16550_COM2,
  63. #else
  64. NULL,
  65. #endif
  66. #ifdef CONFIG_SYS_NS16550_COM3
  67. (NS16550_t)CONFIG_SYS_NS16550_COM3,
  68. #else
  69. NULL,
  70. #endif
  71. #ifdef CONFIG_SYS_NS16550_COM4
  72. (NS16550_t)CONFIG_SYS_NS16550_COM4
  73. #else
  74. NULL
  75. #endif
  76. };
  77. #define PORT serial_ports[port-1]
  78. #if defined(CONFIG_CONS_INDEX)
  79. #define CONSOLE (serial_ports[CONFIG_CONS_INDEX-1])
  80. #endif
  81. #if defined(CONFIG_SERIAL_MULTI)
  82. /* Multi serial device functions */
  83. #define DECLARE_ESERIAL_FUNCTIONS(port) \
  84. int eserial##port##_init (void) {\
  85. int clock_divisor; \
  86. clock_divisor = calc_divisor(serial_ports[port-1]); \
  87. NS16550_init(serial_ports[port-1], clock_divisor); \
  88. return(0);}\
  89. void eserial##port##_setbrg (void) {\
  90. serial_setbrg_dev(port);}\
  91. int eserial##port##_getc (void) {\
  92. return serial_getc_dev(port);}\
  93. int eserial##port##_tstc (void) {\
  94. return serial_tstc_dev(port);}\
  95. void eserial##port##_putc (const char c) {\
  96. serial_putc_dev(port, c);}\
  97. void eserial##port##_puts (const char *s) {\
  98. serial_puts_dev(port, s);}
  99. /* Serial device descriptor */
  100. #define INIT_ESERIAL_STRUCTURE(port,name,bus) {\
  101. name,\
  102. bus,\
  103. eserial##port##_init,\
  104. eserial##port##_setbrg,\
  105. eserial##port##_getc,\
  106. eserial##port##_tstc,\
  107. eserial##port##_putc,\
  108. eserial##port##_puts, }
  109. #endif /* CONFIG_SERIAL_MULTI */
  110. static int calc_divisor (NS16550_t port)
  111. {
  112. #ifdef CONFIG_OMAP1510
  113. /* If can't cleanly clock 115200 set div to 1 */
  114. if ((CONFIG_SYS_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
  115. port->osc_12m_sel = OSC_12M_SEL; /* enable 6.5 * divisor */
  116. return (1); /* return 1 for base divisor */
  117. }
  118. port->osc_12m_sel = 0; /* clear if previsouly set */
  119. #endif
  120. #ifdef CONFIG_OMAP1610
  121. /* If can't cleanly clock 115200 set div to 1 */
  122. if ((CONFIG_SYS_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
  123. return (26); /* return 26 for base divisor */
  124. }
  125. #endif
  126. #ifdef CONFIG_APTIX
  127. #define MODE_X_DIV 13
  128. #else
  129. #define MODE_X_DIV 16
  130. #endif
  131. /* Compute divisor value. Normally, we should simply return:
  132. * CONFIG_SYS_NS16550_CLK) / MODE_X_DIV / gd->baudrate
  133. * but we need to round that value by adding 0.5.
  134. * Rounding is especially important at high baud rates.
  135. */
  136. return (CONFIG_SYS_NS16550_CLK + (gd->baudrate * (MODE_X_DIV / 2))) /
  137. (MODE_X_DIV * gd->baudrate);
  138. }
  139. #if !defined(CONFIG_SERIAL_MULTI)
  140. int serial_init (void)
  141. {
  142. int clock_divisor;
  143. #ifdef CONFIG_NS87308
  144. initialise_ns87308();
  145. #endif
  146. #ifdef CONFIG_SYS_NS16550_COM1
  147. clock_divisor = calc_divisor(serial_ports[0]);
  148. NS16550_init(serial_ports[0], clock_divisor);
  149. #endif
  150. #ifdef CONFIG_SYS_NS16550_COM2
  151. clock_divisor = calc_divisor(serial_ports[1]);
  152. NS16550_init(serial_ports[1], clock_divisor);
  153. #endif
  154. #ifdef CONFIG_SYS_NS16550_COM3
  155. clock_divisor = calc_divisor(serial_ports[2]);
  156. NS16550_init(serial_ports[2], clock_divisor);
  157. #endif
  158. #ifdef CONFIG_SYS_NS16550_COM4
  159. clock_divisor = calc_divisor(serial_ports[3]);
  160. NS16550_init(serial_ports[3], clock_divisor);
  161. #endif
  162. return (0);
  163. }
  164. #endif
  165. void
  166. _serial_putc(const char c,const int port)
  167. {
  168. if (c == '\n')
  169. NS16550_putc(PORT, '\r');
  170. NS16550_putc(PORT, c);
  171. }
  172. void
  173. _serial_putc_raw(const char c,const int port)
  174. {
  175. NS16550_putc(PORT, c);
  176. }
  177. void
  178. _serial_puts (const char *s,const int port)
  179. {
  180. while (*s) {
  181. _serial_putc (*s++,port);
  182. }
  183. }
  184. int
  185. _serial_getc(const int port)
  186. {
  187. return NS16550_getc(PORT);
  188. }
  189. int
  190. _serial_tstc(const int port)
  191. {
  192. return NS16550_tstc(PORT);
  193. }
  194. void
  195. _serial_setbrg (const int port)
  196. {
  197. int clock_divisor;
  198. clock_divisor = calc_divisor(PORT);
  199. NS16550_reinit(PORT, clock_divisor);
  200. }
  201. #if defined(CONFIG_SERIAL_MULTI)
  202. static inline void
  203. serial_putc_dev(unsigned int dev_index,const char c)
  204. {
  205. _serial_putc(c,dev_index);
  206. }
  207. #else
  208. void
  209. serial_putc(const char c)
  210. {
  211. _serial_putc(c,CONFIG_CONS_INDEX);
  212. }
  213. #endif
  214. #if defined(CONFIG_SERIAL_MULTI)
  215. static inline void
  216. serial_putc_raw_dev(unsigned int dev_index,const char c)
  217. {
  218. _serial_putc_raw(c,dev_index);
  219. }
  220. #else
  221. void
  222. serial_putc_raw(const char c)
  223. {
  224. _serial_putc_raw(c,CONFIG_CONS_INDEX);
  225. }
  226. #endif
  227. #if defined(CONFIG_SERIAL_MULTI)
  228. static inline void
  229. serial_puts_dev(unsigned int dev_index,const char *s)
  230. {
  231. _serial_puts(s,dev_index);
  232. }
  233. #else
  234. void
  235. serial_puts(const char *s)
  236. {
  237. _serial_puts(s,CONFIG_CONS_INDEX);
  238. }
  239. #endif
  240. #if defined(CONFIG_SERIAL_MULTI)
  241. static inline int
  242. serial_getc_dev(unsigned int dev_index)
  243. {
  244. return _serial_getc(dev_index);
  245. }
  246. #else
  247. int
  248. serial_getc(void)
  249. {
  250. return _serial_getc(CONFIG_CONS_INDEX);
  251. }
  252. #endif
  253. #if defined(CONFIG_SERIAL_MULTI)
  254. static inline int
  255. serial_tstc_dev(unsigned int dev_index)
  256. {
  257. return _serial_tstc(dev_index);
  258. }
  259. #else
  260. int
  261. serial_tstc(void)
  262. {
  263. return _serial_tstc(CONFIG_CONS_INDEX);
  264. }
  265. #endif
  266. #if defined(CONFIG_SERIAL_MULTI)
  267. static inline void
  268. serial_setbrg_dev(unsigned int dev_index)
  269. {
  270. _serial_setbrg(dev_index);
  271. }
  272. #else
  273. void
  274. serial_setbrg(void)
  275. {
  276. _serial_setbrg(CONFIG_CONS_INDEX);
  277. }
  278. #endif
  279. #if defined(CONFIG_SERIAL_MULTI)
  280. DECLARE_ESERIAL_FUNCTIONS(1);
  281. struct serial_device eserial1_device =
  282. INIT_ESERIAL_STRUCTURE(1,"eserial0","EUART1");
  283. DECLARE_ESERIAL_FUNCTIONS(2);
  284. struct serial_device eserial2_device =
  285. INIT_ESERIAL_STRUCTURE(2,"eserial1","EUART2");
  286. DECLARE_ESERIAL_FUNCTIONS(3);
  287. struct serial_device eserial3_device =
  288. INIT_ESERIAL_STRUCTURE(3,"eserial2","EUART3");
  289. DECLARE_ESERIAL_FUNCTIONS(4);
  290. struct serial_device eserial4_device =
  291. INIT_ESERIAL_STRUCTURE(4,"eserial3","EUART4");
  292. #endif /* CONFIG_SERIAL_MULTI */