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