serial.c 7.9 KB

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