serial.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/ppc/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 defined(CONFIG_MGT5100)
  48. #error CONFIG_PSC_CONSOLE must be in 1, 2 or 3
  49. #elif CONFIG_PSC_CONSOLE == 4
  50. #define PSC_BASE MPC5XXX_PSC4
  51. #elif CONFIG_PSC_CONSOLE == 5
  52. #define PSC_BASE MPC5XXX_PSC5
  53. #elif CONFIG_PSC_CONSOLE == 6
  54. #define PSC_BASE MPC5XXX_PSC6
  55. #else
  56. #error CONFIG_PSC_CONSOLE must be in 1 ... 6
  57. #endif
  58. #if defined(CONFIG_SERIAL_MULTI) && !defined(CONFIG_PSC_CONSOLE2)
  59. #error you must define CONFIG_PSC_CONSOLE2 if CONFIG_SERIAL_MULTI is set
  60. #endif
  61. #if defined(CONFIG_SERIAL_MULTI)
  62. #if CONFIG_PSC_CONSOLE2 == 1
  63. #define PSC_BASE2 MPC5XXX_PSC1
  64. #elif CONFIG_PSC_CONSOLE2 == 2
  65. #define PSC_BASE2 MPC5XXX_PSC2
  66. #elif CONFIG_PSC_CONSOLE2 == 3
  67. #define PSC_BASE2 MPC5XXX_PSC3
  68. #elif defined(CONFIG_MGT5100)
  69. #error CONFIG_PSC_CONSOLE2 must be in 1, 2 or 3
  70. #elif CONFIG_PSC_CONSOLE2 == 4
  71. #define PSC_BASE2 MPC5XXX_PSC4
  72. #elif CONFIG_PSC_CONSOLE2 == 5
  73. #define PSC_BASE2 MPC5XXX_PSC5
  74. #elif CONFIG_PSC_CONSOLE2 == 6
  75. #define PSC_BASE2 MPC5XXX_PSC6
  76. #else
  77. #error CONFIG_PSC_CONSOLE2 must be in 1 ... 6
  78. #endif
  79. #endif /* CONFIG_SERIAL_MULTI */
  80. #if defined(CONFIG_SERIAL_MULTI)
  81. int serial_init_dev (unsigned long dev_base)
  82. #else
  83. int serial_init (void)
  84. #endif
  85. {
  86. #if defined(CONFIG_SERIAL_MULTI)
  87. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  88. #else
  89. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  90. #endif
  91. unsigned long baseclk;
  92. int div;
  93. /* reset PSC */
  94. psc->command = PSC_SEL_MODE_REG_1;
  95. /* select clock sources */
  96. #if defined(CONFIG_MGT5100)
  97. psc->psc_clock_select = 0xdd00;
  98. baseclk = (CONFIG_SYS_MPC5XXX_CLKIN + 16) / 32;
  99. #elif defined(CONFIG_MPC5200)
  100. psc->psc_clock_select = 0;
  101. baseclk = (gd->ipb_clk + 16) / 32;
  102. #endif
  103. /* switch to UART mode */
  104. psc->sicr = 0;
  105. /* configure parity, bit length and so on */
  106. #if defined(CONFIG_MGT5100)
  107. psc->mode = PSC_MODE_ERR | PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  108. #elif defined(CONFIG_MPC5200)
  109. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  110. #endif
  111. psc->mode = PSC_MODE_ONE_STOP;
  112. /* set up UART divisor */
  113. div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
  114. psc->ctur = (div >> 8) & 0xff;
  115. psc->ctlr = div & 0xff;
  116. /* disable all interrupts */
  117. psc->psc_imr = 0;
  118. /* reset and enable Rx/Tx */
  119. psc->command = PSC_RST_RX;
  120. psc->command = PSC_RST_TX;
  121. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  122. return (0);
  123. }
  124. #if defined(CONFIG_SERIAL_MULTI)
  125. void serial_putc_dev (unsigned long dev_base, const char c)
  126. #else
  127. void serial_putc(const char c)
  128. #endif
  129. {
  130. #if defined(CONFIG_SERIAL_MULTI)
  131. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  132. #else
  133. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  134. #endif
  135. if (c == '\n')
  136. #if defined(CONFIG_SERIAL_MULTI)
  137. serial_putc_dev (dev_base, '\r');
  138. #else
  139. serial_putc('\r');
  140. #endif
  141. /* Wait for last character to go. */
  142. while (!(psc->psc_status & PSC_SR_TXEMP))
  143. ;
  144. psc->psc_buffer_8 = c;
  145. }
  146. #if defined(CONFIG_SERIAL_MULTI)
  147. void serial_putc_raw_dev(unsigned long dev_base, const char c)
  148. #else
  149. void serial_putc_raw(const char c)
  150. #endif
  151. {
  152. #if defined(CONFIG_SERIAL_MULTI)
  153. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  154. #else
  155. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  156. #endif
  157. /* Wait for last character to go. */
  158. while (!(psc->psc_status & PSC_SR_TXEMP))
  159. ;
  160. psc->psc_buffer_8 = c;
  161. }
  162. #if defined(CONFIG_SERIAL_MULTI)
  163. void serial_puts_dev (unsigned long dev_base, const char *s)
  164. #else
  165. void serial_puts (const char *s)
  166. #endif
  167. {
  168. while (*s) {
  169. #if defined(CONFIG_SERIAL_MULTI)
  170. serial_putc_dev (dev_base, *s++);
  171. #else
  172. serial_putc (*s++);
  173. #endif
  174. }
  175. }
  176. #if defined(CONFIG_SERIAL_MULTI)
  177. int serial_getc_dev (unsigned long dev_base)
  178. #else
  179. int serial_getc(void)
  180. #endif
  181. {
  182. #if defined(CONFIG_SERIAL_MULTI)
  183. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  184. #else
  185. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  186. #endif
  187. /* Wait for a character to arrive. */
  188. while (!(psc->psc_status & PSC_SR_RXRDY))
  189. ;
  190. return psc->psc_buffer_8;
  191. }
  192. #if defined(CONFIG_SERIAL_MULTI)
  193. int serial_tstc_dev (unsigned long dev_base)
  194. #else
  195. int serial_tstc(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. return (psc->psc_status & PSC_SR_RXRDY);
  204. }
  205. #if defined(CONFIG_SERIAL_MULTI)
  206. void serial_setbrg_dev (unsigned long dev_base)
  207. #else
  208. void serial_setbrg(void)
  209. #endif
  210. {
  211. #if defined(CONFIG_SERIAL_MULTI)
  212. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  213. #else
  214. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  215. #endif
  216. unsigned long baseclk, div;
  217. #if defined(CONFIG_MGT5100)
  218. baseclk = (CONFIG_SYS_MPC5XXX_CLKIN + 16) / 32;
  219. #elif defined(CONFIG_MPC5200)
  220. baseclk = (gd->ipb_clk + 16) / 32;
  221. #endif
  222. /* set up UART divisor */
  223. div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
  224. psc->ctur = (div >> 8) & 0xFF;
  225. psc->ctlr = div & 0xff;
  226. }
  227. #if defined(CONFIG_SERIAL_MULTI)
  228. void serial_setrts_dev (unsigned long dev_base, int s)
  229. #else
  230. void serial_setrts(int s)
  231. #endif
  232. {
  233. #if defined(CONFIG_SERIAL_MULTI)
  234. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  235. #else
  236. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  237. #endif
  238. if (s) {
  239. /* Assert RTS (become LOW) */
  240. psc->op1 = 0x1;
  241. }
  242. else {
  243. /* Negate RTS (become HIGH) */
  244. psc->op0 = 0x1;
  245. }
  246. }
  247. #if defined(CONFIG_SERIAL_MULTI)
  248. int serial_getcts_dev (unsigned long dev_base)
  249. #else
  250. int serial_getcts(void)
  251. #endif
  252. {
  253. #if defined(CONFIG_SERIAL_MULTI)
  254. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
  255. #else
  256. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  257. #endif
  258. return (psc->ip & 0x1) ? 0 : 1;
  259. }
  260. #if defined(CONFIG_SERIAL_MULTI)
  261. int serial0_init(void)
  262. {
  263. return (serial_init_dev(PSC_BASE));
  264. }
  265. int serial1_init(void)
  266. {
  267. return (serial_init_dev(PSC_BASE2));
  268. }
  269. void serial0_setbrg (void)
  270. {
  271. serial_setbrg_dev(PSC_BASE);
  272. }
  273. void serial1_setbrg (void)
  274. {
  275. serial_setbrg_dev(PSC_BASE2);
  276. }
  277. void serial0_putc(const char c)
  278. {
  279. serial_putc_dev(PSC_BASE,c);
  280. }
  281. void serial1_putc(const char c)
  282. {
  283. serial_putc_dev(PSC_BASE2, c);
  284. }
  285. void serial0_puts(const char *s)
  286. {
  287. serial_puts_dev(PSC_BASE, s);
  288. }
  289. void serial1_puts(const char *s)
  290. {
  291. serial_puts_dev(PSC_BASE2, s);
  292. }
  293. int serial0_getc(void)
  294. {
  295. return(serial_getc_dev(PSC_BASE));
  296. }
  297. int serial1_getc(void)
  298. {
  299. return(serial_getc_dev(PSC_BASE2));
  300. }
  301. int serial0_tstc(void)
  302. {
  303. return (serial_tstc_dev(PSC_BASE));
  304. }
  305. int serial1_tstc(void)
  306. {
  307. return (serial_tstc_dev(PSC_BASE2));
  308. }
  309. struct serial_device serial0_device =
  310. {
  311. "serial0",
  312. "UART0",
  313. serial0_init,
  314. serial0_setbrg,
  315. serial0_getc,
  316. serial0_tstc,
  317. serial0_putc,
  318. serial0_puts,
  319. };
  320. struct serial_device serial1_device =
  321. {
  322. "serial1",
  323. "UART1",
  324. serial1_init,
  325. serial1_setbrg,
  326. serial1_getc,
  327. serial1_tstc,
  328. serial1_putc,
  329. serial1_puts,
  330. };
  331. #endif /* CONFIG_SERIAL_MULTI */
  332. #endif /* CONFIG_PSC_CONSOLE */