serial.c 7.5 KB

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