serial.c 7.9 KB

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