serial.c 7.2 KB

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