serial_ns16550.c 8.0 KB

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