serial.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * (C) Copyright 2000 - 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with
  24. * changes based on the file arch/powerpc/mbxboot/m8260_tty.c from the
  25. * Linux/PPC sources (m8260_tty.c had no copyright info in it).
  26. *
  27. * Martin Krause, 8 Jun 2006
  28. * Added CONFIG_SERIAL_MULTI support
  29. */
  30. /*
  31. * Minimal serial functions needed to use one of the PSC ports
  32. * as serial console interface.
  33. */
  34. #include <common.h>
  35. #include <mpc5xxx.h>
  36. #if defined (CONFIG_SERIAL_MULTI)
  37. #include <serial.h>
  38. #endif
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #if defined(CONFIG_PSC_CONSOLE)
  41. #if CONFIG_PSC_CONSOLE == 1
  42. #define PSC_BASE MPC5XXX_PSC1
  43. #elif CONFIG_PSC_CONSOLE == 2
  44. #define PSC_BASE MPC5XXX_PSC2
  45. #elif CONFIG_PSC_CONSOLE == 3
  46. #define PSC_BASE MPC5XXX_PSC3
  47. #elif CONFIG_PSC_CONSOLE == 4
  48. #define PSC_BASE MPC5XXX_PSC4
  49. #elif CONFIG_PSC_CONSOLE == 5
  50. #define PSC_BASE MPC5XXX_PSC5
  51. #elif CONFIG_PSC_CONSOLE == 6
  52. #define PSC_BASE MPC5XXX_PSC6
  53. #else
  54. #error CONFIG_PSC_CONSOLE must be in 1 ... 6
  55. #endif
  56. #if defined(CONFIG_SERIAL_MULTI) && !defined(CONFIG_PSC_CONSOLE2)
  57. #error you must define CONFIG_PSC_CONSOLE2 if CONFIG_SERIAL_MULTI is set
  58. #endif
  59. #if defined(CONFIG_SERIAL_MULTI)
  60. #if CONFIG_PSC_CONSOLE2 == 1
  61. #define PSC_BASE2 MPC5XXX_PSC1
  62. #elif CONFIG_PSC_CONSOLE2 == 2
  63. #define PSC_BASE2 MPC5XXX_PSC2
  64. #elif CONFIG_PSC_CONSOLE2 == 3
  65. #define PSC_BASE2 MPC5XXX_PSC3
  66. #elif CONFIG_PSC_CONSOLE2 == 4
  67. #define PSC_BASE2 MPC5XXX_PSC4
  68. #elif CONFIG_PSC_CONSOLE2 == 5
  69. #define PSC_BASE2 MPC5XXX_PSC5
  70. #elif CONFIG_PSC_CONSOLE2 == 6
  71. #define PSC_BASE2 MPC5XXX_PSC6
  72. #else
  73. #error CONFIG_PSC_CONSOLE2 must be in 1 ... 6
  74. #endif
  75. #endif /* CONFIG_SERIAL_MULTI */
  76. #if defined(CONFIG_SERIAL_MULTI)
  77. int serial_init_dev (unsigned long dev_base)
  78. #else
  79. int serial_init (void)
  80. #endif
  81. {
  82. #if defined(CONFIG_SERIAL_MULTI)
  83. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  84. #else
  85. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  86. #endif
  87. unsigned long baseclk;
  88. int div;
  89. /* reset PSC */
  90. psc->command = PSC_SEL_MODE_REG_1;
  91. /* select clock sources */
  92. psc->psc_clock_select = 0;
  93. baseclk = (gd->ipb_clk + 16) / 32;
  94. /* switch to UART mode */
  95. psc->sicr = 0;
  96. /* configure parity, bit length and so on */
  97. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  98. psc->mode = PSC_MODE_ONE_STOP;
  99. /* set up UART divisor */
  100. div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
  101. psc->ctur = (div >> 8) & 0xff;
  102. psc->ctlr = div & 0xff;
  103. /* disable all interrupts */
  104. psc->psc_imr = 0;
  105. /* reset and enable Rx/Tx */
  106. psc->command = PSC_RST_RX;
  107. psc->command = PSC_RST_TX;
  108. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  109. return (0);
  110. }
  111. #if defined(CONFIG_SERIAL_MULTI)
  112. void serial_putc_dev (unsigned long dev_base, const char c)
  113. #else
  114. void serial_putc(const char c)
  115. #endif
  116. {
  117. #if defined(CONFIG_SERIAL_MULTI)
  118. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  119. #else
  120. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  121. #endif
  122. if (c == '\n')
  123. #if defined(CONFIG_SERIAL_MULTI)
  124. serial_putc_dev (dev_base, '\r');
  125. #else
  126. serial_putc('\r');
  127. #endif
  128. /* Wait for last character to go. */
  129. while (!(psc->psc_status & PSC_SR_TXEMP))
  130. ;
  131. psc->psc_buffer_8 = c;
  132. }
  133. #if defined(CONFIG_SERIAL_MULTI)
  134. void serial_putc_raw_dev(unsigned long dev_base, const char c)
  135. #else
  136. void serial_putc_raw(const char c)
  137. #endif
  138. {
  139. #if defined(CONFIG_SERIAL_MULTI)
  140. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  141. #else
  142. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  143. #endif
  144. /* Wait for last character to go. */
  145. while (!(psc->psc_status & PSC_SR_TXEMP))
  146. ;
  147. psc->psc_buffer_8 = c;
  148. }
  149. #if defined(CONFIG_SERIAL_MULTI)
  150. void serial_puts_dev (unsigned long dev_base, const char *s)
  151. #else
  152. void serial_puts (const char *s)
  153. #endif
  154. {
  155. while (*s) {
  156. #if defined(CONFIG_SERIAL_MULTI)
  157. serial_putc_dev (dev_base, *s++);
  158. #else
  159. serial_putc (*s++);
  160. #endif
  161. }
  162. }
  163. #if defined(CONFIG_SERIAL_MULTI)
  164. int serial_getc_dev (unsigned long dev_base)
  165. #else
  166. int serial_getc(void)
  167. #endif
  168. {
  169. #if defined(CONFIG_SERIAL_MULTI)
  170. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  171. #else
  172. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  173. #endif
  174. /* Wait for a character to arrive. */
  175. while (!(psc->psc_status & PSC_SR_RXRDY))
  176. ;
  177. return psc->psc_buffer_8;
  178. }
  179. #if defined(CONFIG_SERIAL_MULTI)
  180. int serial_tstc_dev (unsigned long dev_base)
  181. #else
  182. int serial_tstc(void)
  183. #endif
  184. {
  185. #if defined(CONFIG_SERIAL_MULTI)
  186. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  187. #else
  188. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  189. #endif
  190. return (psc->psc_status & PSC_SR_RXRDY);
  191. }
  192. #if defined(CONFIG_SERIAL_MULTI)
  193. void serial_setbrg_dev (unsigned long dev_base)
  194. #else
  195. void serial_setbrg(void)
  196. #endif
  197. {
  198. #if defined(CONFIG_SERIAL_MULTI)
  199. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  200. #else
  201. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  202. #endif
  203. unsigned long baseclk, div;
  204. baseclk = (gd->ipb_clk + 16) / 32;
  205. /* set up UART divisor */
  206. div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
  207. psc->ctur = (div >> 8) & 0xFF;
  208. psc->ctlr = div & 0xff;
  209. }
  210. #if defined(CONFIG_SERIAL_MULTI)
  211. void serial_setrts_dev (unsigned long dev_base, int s)
  212. #else
  213. void serial_setrts(int s)
  214. #endif
  215. {
  216. #if defined(CONFIG_SERIAL_MULTI)
  217. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  218. #else
  219. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  220. #endif
  221. if (s) {
  222. /* Assert RTS (become LOW) */
  223. psc->op1 = 0x1;
  224. }
  225. else {
  226. /* Negate RTS (become HIGH) */
  227. psc->op0 = 0x1;
  228. }
  229. }
  230. #if defined(CONFIG_SERIAL_MULTI)
  231. int serial_getcts_dev (unsigned long dev_base)
  232. #else
  233. int serial_getcts(void)
  234. #endif
  235. {
  236. #if defined(CONFIG_SERIAL_MULTI)
  237. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  238. #else
  239. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  240. #endif
  241. return (psc->ip & 0x1) ? 0 : 1;
  242. }
  243. #if defined(CONFIG_SERIAL_MULTI)
  244. int serial0_init(void)
  245. {
  246. return (serial_init_dev(PSC_BASE));
  247. }
  248. int serial1_init(void)
  249. {
  250. return (serial_init_dev(PSC_BASE2));
  251. }
  252. void serial0_setbrg (void)
  253. {
  254. serial_setbrg_dev(PSC_BASE);
  255. }
  256. void serial1_setbrg (void)
  257. {
  258. serial_setbrg_dev(PSC_BASE2);
  259. }
  260. void serial0_putc(const char c)
  261. {
  262. serial_putc_dev(PSC_BASE,c);
  263. }
  264. void serial1_putc(const char c)
  265. {
  266. serial_putc_dev(PSC_BASE2, c);
  267. }
  268. void serial0_puts(const char *s)
  269. {
  270. serial_puts_dev(PSC_BASE, s);
  271. }
  272. void serial1_puts(const char *s)
  273. {
  274. serial_puts_dev(PSC_BASE2, s);
  275. }
  276. int serial0_getc(void)
  277. {
  278. return(serial_getc_dev(PSC_BASE));
  279. }
  280. int serial1_getc(void)
  281. {
  282. return(serial_getc_dev(PSC_BASE2));
  283. }
  284. int serial0_tstc(void)
  285. {
  286. return (serial_tstc_dev(PSC_BASE));
  287. }
  288. int serial1_tstc(void)
  289. {
  290. return (serial_tstc_dev(PSC_BASE2));
  291. }
  292. struct serial_device serial0_device =
  293. {
  294. "serial0",
  295. "UART0",
  296. serial0_init,
  297. NULL,
  298. serial0_setbrg,
  299. serial0_getc,
  300. serial0_tstc,
  301. serial0_putc,
  302. serial0_puts,
  303. };
  304. struct serial_device serial1_device =
  305. {
  306. "serial1",
  307. "UART1",
  308. serial1_init,
  309. NULL,
  310. serial1_setbrg,
  311. serial1_getc,
  312. serial1_tstc,
  313. serial1_putc,
  314. serial1_puts,
  315. };
  316. #endif /* CONFIG_SERIAL_MULTI */
  317. #endif /* CONFIG_PSC_CONSOLE */