serial.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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) {\
  111. name,\
  112. eserial##port##_init,\
  113. NULL,\
  114. eserial##port##_setbrg,\
  115. eserial##port##_getc,\
  116. eserial##port##_tstc,\
  117. eserial##port##_putc,\
  118. eserial##port##_puts, }
  119. #endif /* CONFIG_SERIAL_MULTI */
  120. static int calc_divisor (NS16550_t port)
  121. {
  122. #ifdef CONFIG_OMAP1510
  123. /* If can't cleanly clock 115200 set div to 1 */
  124. if ((CONFIG_SYS_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
  125. port->osc_12m_sel = OSC_12M_SEL; /* enable 6.5 * divisor */
  126. return (1); /* return 1 for base divisor */
  127. }
  128. port->osc_12m_sel = 0; /* clear if previsouly set */
  129. #endif
  130. #ifdef CONFIG_OMAP1610
  131. /* If can't cleanly clock 115200 set div to 1 */
  132. if ((CONFIG_SYS_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
  133. return (26); /* return 26 for base divisor */
  134. }
  135. #endif
  136. #ifdef CONFIG_APTIX
  137. #define MODE_X_DIV 13
  138. #else
  139. #define MODE_X_DIV 16
  140. #endif
  141. /* Compute divisor value. Normally, we should simply return:
  142. * CONFIG_SYS_NS16550_CLK) / MODE_X_DIV / gd->baudrate
  143. * but we need to round that value by adding 0.5.
  144. * Rounding is especially important at high baud rates.
  145. */
  146. return (CONFIG_SYS_NS16550_CLK + (gd->baudrate * (MODE_X_DIV / 2))) /
  147. (MODE_X_DIV * gd->baudrate);
  148. }
  149. #if !defined(CONFIG_SERIAL_MULTI)
  150. int serial_init (void)
  151. {
  152. int clock_divisor;
  153. #ifdef CONFIG_NS87308
  154. initialise_ns87308();
  155. #endif
  156. #ifdef CONFIG_SYS_NS16550_COM1
  157. clock_divisor = calc_divisor(serial_ports[0]);
  158. NS16550_init(serial_ports[0], clock_divisor);
  159. #endif
  160. #ifdef CONFIG_SYS_NS16550_COM2
  161. clock_divisor = calc_divisor(serial_ports[1]);
  162. NS16550_init(serial_ports[1], clock_divisor);
  163. #endif
  164. #ifdef CONFIG_SYS_NS16550_COM3
  165. clock_divisor = calc_divisor(serial_ports[2]);
  166. NS16550_init(serial_ports[2], clock_divisor);
  167. #endif
  168. #ifdef CONFIG_SYS_NS16550_COM4
  169. clock_divisor = calc_divisor(serial_ports[3]);
  170. NS16550_init(serial_ports[3], clock_divisor);
  171. #endif
  172. return (0);
  173. }
  174. #endif
  175. void
  176. _serial_putc(const char c,const int port)
  177. {
  178. if (c == '\n')
  179. NS16550_putc(PORT, '\r');
  180. NS16550_putc(PORT, c);
  181. }
  182. void
  183. _serial_putc_raw(const char c,const int port)
  184. {
  185. NS16550_putc(PORT, c);
  186. }
  187. void
  188. _serial_puts (const char *s,const int port)
  189. {
  190. while (*s) {
  191. _serial_putc (*s++,port);
  192. }
  193. }
  194. int
  195. _serial_getc(const int port)
  196. {
  197. return NS16550_getc(PORT);
  198. }
  199. int
  200. _serial_tstc(const int port)
  201. {
  202. return NS16550_tstc(PORT);
  203. }
  204. void
  205. _serial_setbrg (const int port)
  206. {
  207. int clock_divisor;
  208. clock_divisor = calc_divisor(PORT);
  209. NS16550_reinit(PORT, clock_divisor);
  210. }
  211. #if defined(CONFIG_SERIAL_MULTI)
  212. static inline void
  213. serial_putc_dev(unsigned int dev_index,const char c)
  214. {
  215. _serial_putc(c,dev_index);
  216. }
  217. #else
  218. void
  219. serial_putc(const char c)
  220. {
  221. _serial_putc(c,CONFIG_CONS_INDEX);
  222. }
  223. #endif
  224. #if defined(CONFIG_SERIAL_MULTI)
  225. static inline void
  226. serial_putc_raw_dev(unsigned int dev_index,const char c)
  227. {
  228. _serial_putc_raw(c,dev_index);
  229. }
  230. #else
  231. void
  232. serial_putc_raw(const char c)
  233. {
  234. _serial_putc_raw(c,CONFIG_CONS_INDEX);
  235. }
  236. #endif
  237. #if defined(CONFIG_SERIAL_MULTI)
  238. static inline void
  239. serial_puts_dev(unsigned int dev_index,const char *s)
  240. {
  241. _serial_puts(s,dev_index);
  242. }
  243. #else
  244. void
  245. serial_puts(const char *s)
  246. {
  247. _serial_puts(s,CONFIG_CONS_INDEX);
  248. }
  249. #endif
  250. #if defined(CONFIG_SERIAL_MULTI)
  251. static inline int
  252. serial_getc_dev(unsigned int dev_index)
  253. {
  254. return _serial_getc(dev_index);
  255. }
  256. #else
  257. int
  258. serial_getc(void)
  259. {
  260. return _serial_getc(CONFIG_CONS_INDEX);
  261. }
  262. #endif
  263. #if defined(CONFIG_SERIAL_MULTI)
  264. static inline int
  265. serial_tstc_dev(unsigned int dev_index)
  266. {
  267. return _serial_tstc(dev_index);
  268. }
  269. #else
  270. int
  271. serial_tstc(void)
  272. {
  273. return _serial_tstc(CONFIG_CONS_INDEX);
  274. }
  275. #endif
  276. #if defined(CONFIG_SERIAL_MULTI)
  277. static inline void
  278. serial_setbrg_dev(unsigned int dev_index)
  279. {
  280. _serial_setbrg(dev_index);
  281. }
  282. #else
  283. void
  284. serial_setbrg(void)
  285. {
  286. _serial_setbrg(CONFIG_CONS_INDEX);
  287. }
  288. #endif
  289. #if defined(CONFIG_SERIAL_MULTI)
  290. DECLARE_ESERIAL_FUNCTIONS(1);
  291. struct serial_device eserial1_device =
  292. INIT_ESERIAL_STRUCTURE(1, "eserial0");
  293. DECLARE_ESERIAL_FUNCTIONS(2);
  294. struct serial_device eserial2_device =
  295. INIT_ESERIAL_STRUCTURE(2, "eserial1");
  296. DECLARE_ESERIAL_FUNCTIONS(3);
  297. struct serial_device eserial3_device =
  298. INIT_ESERIAL_STRUCTURE(3, "eserial2");
  299. DECLARE_ESERIAL_FUNCTIONS(4);
  300. struct serial_device eserial4_device =
  301. INIT_ESERIAL_STRUCTURE(4, "eserial3");
  302. __weak struct serial_device *default_serial_console(void)
  303. {
  304. #if CONFIG_CONS_INDEX == 1
  305. return &eserial1_device;
  306. #elif CONFIG_CONS_INDEX == 2
  307. return &eserial2_device;
  308. #elif CONFIG_CONS_INDEX == 3
  309. return &eserial3_device;
  310. #elif CONFIG_CONS_INDEX == 4
  311. return &eserial4_device;
  312. #else
  313. #error "Bad CONFIG_CONS_INDEX."
  314. #endif
  315. }
  316. #endif /* CONFIG_SERIAL_MULTI */