serial.c 7.5 KB

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