serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * (C) Copyright 2000 - 2010
  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. * Based ont the MPC5200 PSC driver.
  24. * Adapted for MPC512x by Jan Wrobel <wrr@semihalf.com>
  25. */
  26. /*
  27. * Minimal serial functions needed to use one of the PSC ports
  28. * as serial console interface.
  29. */
  30. #include <common.h>
  31. #include <linux/compiler.h>
  32. #include <asm/io.h>
  33. #include <asm/processor.h>
  34. #include <serial.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #if defined(CONFIG_PSC_CONSOLE) || defined(CONFIG_SERIAL_MULTI)
  37. static void fifo_init (volatile psc512x_t *psc)
  38. {
  39. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  40. u32 tfsize, rfsize;
  41. /* reset Rx & Tx fifo slice */
  42. out_be32(&psc->rfcmd, PSC_FIFO_RESET_SLICE);
  43. out_be32(&psc->tfcmd, PSC_FIFO_RESET_SLICE);
  44. /* disable Tx & Rx FIFO interrupts */
  45. out_be32(&psc->rfintmask, 0);
  46. out_be32(&psc->tfintmask, 0);
  47. #if defined(CONFIG_SERIAL_MULTI)
  48. switch (((u32)psc & 0xf00) >> 8) {
  49. case 0:
  50. tfsize = FIFOC_PSC0_TX_SIZE | (FIFOC_PSC0_TX_ADDR << 16);
  51. rfsize = FIFOC_PSC0_RX_SIZE | (FIFOC_PSC0_RX_ADDR << 16);
  52. break;
  53. case 1:
  54. tfsize = FIFOC_PSC1_TX_SIZE | (FIFOC_PSC1_TX_ADDR << 16);
  55. rfsize = FIFOC_PSC1_RX_SIZE | (FIFOC_PSC1_RX_ADDR << 16);
  56. break;
  57. case 2:
  58. tfsize = FIFOC_PSC2_TX_SIZE | (FIFOC_PSC2_TX_ADDR << 16);
  59. rfsize = FIFOC_PSC2_RX_SIZE | (FIFOC_PSC2_RX_ADDR << 16);
  60. break;
  61. case 3:
  62. tfsize = FIFOC_PSC3_TX_SIZE | (FIFOC_PSC3_TX_ADDR << 16);
  63. rfsize = FIFOC_PSC3_RX_SIZE | (FIFOC_PSC3_RX_ADDR << 16);
  64. break;
  65. case 4:
  66. tfsize = FIFOC_PSC4_TX_SIZE | (FIFOC_PSC4_TX_ADDR << 16);
  67. rfsize = FIFOC_PSC4_RX_SIZE | (FIFOC_PSC4_RX_ADDR << 16);
  68. break;
  69. case 5:
  70. tfsize = FIFOC_PSC5_TX_SIZE | (FIFOC_PSC5_TX_ADDR << 16);
  71. rfsize = FIFOC_PSC5_RX_SIZE | (FIFOC_PSC5_RX_ADDR << 16);
  72. break;
  73. case 6:
  74. tfsize = FIFOC_PSC6_TX_SIZE | (FIFOC_PSC6_TX_ADDR << 16);
  75. rfsize = FIFOC_PSC6_RX_SIZE | (FIFOC_PSC6_RX_ADDR << 16);
  76. break;
  77. case 7:
  78. tfsize = FIFOC_PSC7_TX_SIZE | (FIFOC_PSC7_TX_ADDR << 16);
  79. rfsize = FIFOC_PSC7_RX_SIZE | (FIFOC_PSC7_RX_ADDR << 16);
  80. break;
  81. case 8:
  82. tfsize = FIFOC_PSC8_TX_SIZE | (FIFOC_PSC8_TX_ADDR << 16);
  83. rfsize = FIFOC_PSC8_RX_SIZE | (FIFOC_PSC8_RX_ADDR << 16);
  84. break;
  85. case 9:
  86. tfsize = FIFOC_PSC9_TX_SIZE | (FIFOC_PSC9_TX_ADDR << 16);
  87. rfsize = FIFOC_PSC9_RX_SIZE | (FIFOC_PSC9_RX_ADDR << 16);
  88. break;
  89. case 10:
  90. tfsize = FIFOC_PSC10_TX_SIZE | (FIFOC_PSC10_TX_ADDR << 16);
  91. rfsize = FIFOC_PSC10_RX_SIZE | (FIFOC_PSC10_RX_ADDR << 16);
  92. break;
  93. case 11:
  94. tfsize = FIFOC_PSC11_TX_SIZE | (FIFOC_PSC11_TX_ADDR << 16);
  95. rfsize = FIFOC_PSC11_RX_SIZE | (FIFOC_PSC11_RX_ADDR << 16);
  96. break;
  97. default:
  98. return;
  99. }
  100. #else
  101. tfsize = CONSOLE_FIFO_TX_SIZE | (CONSOLE_FIFO_TX_ADDR << 16);
  102. rfsize = CONSOLE_FIFO_RX_SIZE | (CONSOLE_FIFO_RX_ADDR << 16);
  103. #endif
  104. out_be32(&psc->tfsize, tfsize);
  105. out_be32(&psc->rfsize, rfsize);
  106. /* enable Tx & Rx FIFO slice */
  107. out_be32(&psc->rfcmd, PSC_FIFO_ENABLE_SLICE);
  108. out_be32(&psc->tfcmd, PSC_FIFO_ENABLE_SLICE);
  109. out_be32(&im->fifoc.fifoc_cmd, FIFOC_DISABLE_CLOCK_GATE);
  110. __asm__ volatile ("sync");
  111. }
  112. void serial_setbrg_dev(unsigned int idx)
  113. {
  114. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  115. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  116. unsigned long baseclk, div;
  117. unsigned long baudrate;
  118. char buf[16];
  119. char *br_env;
  120. baudrate = gd->baudrate;
  121. if (idx != CONFIG_PSC_CONSOLE) {
  122. /* Allows setting baudrate for other serial devices
  123. * on PSCx using environment. If not specified, use
  124. * the same baudrate as for console.
  125. */
  126. sprintf(buf, "psc%d_baudrate", idx);
  127. br_env = getenv(buf);
  128. if (br_env)
  129. baudrate = simple_strtoul(br_env, NULL, 10);
  130. debug("%s: idx %d, baudrate %d\n", __func__, idx, baudrate);
  131. }
  132. /* calculate divisor for setting PSC CTUR and CTLR registers */
  133. baseclk = (gd->ips_clk + 8) / 16;
  134. div = (baseclk + (baudrate / 2)) / baudrate;
  135. out_8(&psc->ctur, (div >> 8) & 0xff);
  136. out_8(&psc->ctlr, div & 0xff); /* set baudrate */
  137. }
  138. int serial_init_dev(unsigned int idx)
  139. {
  140. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  141. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  142. #if defined(CONFIG_SERIAL_MULTI)
  143. u32 reg;
  144. reg = in_be32(&im->clk.sccr[0]);
  145. out_be32(&im->clk.sccr[0], reg | CLOCK_SCCR1_PSC_EN(idx));
  146. #endif
  147. fifo_init (psc);
  148. /* set MR register to point to MR1 */
  149. out_8(&psc->command, PSC_SEL_MODE_REG_1);
  150. /* disable Tx/Rx */
  151. out_8(&psc->command, PSC_TX_DISABLE | PSC_RX_DISABLE);
  152. /* choose the prescaler by 16 for the Tx/Rx clock generation */
  153. out_be16(&psc->psc_clock_select, 0xdd00);
  154. /* switch to UART mode */
  155. out_be32(&psc->sicr, 0);
  156. /* mode register points to mr1 */
  157. /* configure parity, bit length and so on in mode register 1*/
  158. out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
  159. /* now, mode register points to mr2 */
  160. out_8(&psc->mode, PSC_MODE_1_STOPBIT);
  161. /* set baudrate */
  162. serial_setbrg_dev(idx);
  163. /* disable all interrupts */
  164. out_be16(&psc->psc_imr, 0);
  165. /* reset and enable Rx/Tx */
  166. out_8(&psc->command, PSC_RST_RX);
  167. out_8(&psc->command, PSC_RST_TX);
  168. out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
  169. return 0;
  170. }
  171. int serial_uninit_dev(unsigned int idx)
  172. {
  173. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  174. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  175. u32 reg;
  176. out_8(&psc->command, PSC_RX_DISABLE | PSC_TX_DISABLE);
  177. reg = in_be32(&im->clk.sccr[0]);
  178. reg &= ~CLOCK_SCCR1_PSC_EN(idx);
  179. out_be32(&im->clk.sccr[0], reg);
  180. return 0;
  181. }
  182. void serial_putc_dev(unsigned int idx, const char c)
  183. {
  184. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  185. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  186. if (c == '\n')
  187. serial_putc_dev(idx, '\r');
  188. /* Wait for last character to go. */
  189. while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
  190. ;
  191. out_8(&psc->tfdata_8, c);
  192. }
  193. void serial_putc_raw_dev(unsigned int idx, const char c)
  194. {
  195. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  196. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  197. /* Wait for last character to go. */
  198. while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
  199. ;
  200. out_8(&psc->tfdata_8, c);
  201. }
  202. void serial_puts_dev(unsigned int idx, const char *s)
  203. {
  204. while (*s)
  205. serial_putc_dev(idx, *s++);
  206. }
  207. int serial_getc_dev(unsigned int idx)
  208. {
  209. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  210. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  211. /* Wait for a character to arrive. */
  212. while (in_be32(&psc->rfstat) & PSC_FIFO_EMPTY)
  213. ;
  214. return in_8(&psc->rfdata_8);
  215. }
  216. int serial_tstc_dev(unsigned int idx)
  217. {
  218. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  219. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  220. return !(in_be32(&psc->rfstat) & PSC_FIFO_EMPTY);
  221. }
  222. void serial_setrts_dev(unsigned int idx, int s)
  223. {
  224. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  225. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  226. if (s) {
  227. /* Assert RTS (become LOW) */
  228. out_8(&psc->op1, 0x1);
  229. }
  230. else {
  231. /* Negate RTS (become HIGH) */
  232. out_8(&psc->op0, 0x1);
  233. }
  234. }
  235. int serial_getcts_dev(unsigned int idx)
  236. {
  237. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  238. volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx];
  239. return (in_8(&psc->ip) & 0x1) ? 0 : 1;
  240. }
  241. #endif /* CONFIG_PSC_CONSOLE || CONFIG_SERIAL_MULTI */
  242. #if defined(CONFIG_SERIAL_MULTI)
  243. #define DECLARE_PSC_SERIAL_FUNCTIONS(port) \
  244. int serial##port##_init(void) \
  245. { \
  246. return serial_init_dev(port); \
  247. } \
  248. int serial##port##_uninit(void) \
  249. { \
  250. return serial_uninit_dev(port); \
  251. } \
  252. void serial##port##_setbrg(void) \
  253. { \
  254. serial_setbrg_dev(port); \
  255. } \
  256. int serial##port##_getc(void) \
  257. { \
  258. return serial_getc_dev(port); \
  259. } \
  260. int serial##port##_tstc(void) \
  261. { \
  262. return serial_tstc_dev(port); \
  263. } \
  264. void serial##port##_putc(const char c) \
  265. { \
  266. serial_putc_dev(port, c); \
  267. } \
  268. void serial##port##_puts(const char *s) \
  269. { \
  270. serial_puts_dev(port, s); \
  271. }
  272. #define INIT_PSC_SERIAL_STRUCTURE(port, name, bus) { \
  273. name, \
  274. bus, \
  275. serial##port##_init, \
  276. serial##port##_uninit, \
  277. serial##port##_setbrg, \
  278. serial##port##_getc, \
  279. serial##port##_tstc, \
  280. serial##port##_putc, \
  281. serial##port##_puts, \
  282. }
  283. #if defined(CONFIG_SYS_PSC1)
  284. DECLARE_PSC_SERIAL_FUNCTIONS(1);
  285. struct serial_device serial1_device =
  286. INIT_PSC_SERIAL_STRUCTURE(1, "psc1", "UART1");
  287. #endif
  288. #if defined(CONFIG_SYS_PSC3)
  289. DECLARE_PSC_SERIAL_FUNCTIONS(3);
  290. struct serial_device serial3_device =
  291. INIT_PSC_SERIAL_STRUCTURE(3, "psc3", "UART3");
  292. #endif
  293. #if defined(CONFIG_SYS_PSC4)
  294. DECLARE_PSC_SERIAL_FUNCTIONS(4);
  295. struct serial_device serial4_device =
  296. INIT_PSC_SERIAL_STRUCTURE(4, "psc4", "UART4");
  297. #endif
  298. #if defined(CONFIG_SYS_PSC6)
  299. DECLARE_PSC_SERIAL_FUNCTIONS(6);
  300. struct serial_device serial6_device =
  301. INIT_PSC_SERIAL_STRUCTURE(6, "psc6", "UART6");
  302. #endif
  303. __weak struct serial_device *default_serial_console(void)
  304. {
  305. #if (CONFIG_PSC_CONSOLE == 3)
  306. return &serial3_device;
  307. #elif (CONFIG_PSC_CONSOLE == 6)
  308. return &serial6_device;
  309. #else
  310. #error "invalid CONFIG_PSC_CONSOLE"
  311. #endif
  312. }
  313. #else
  314. void serial_setbrg(void)
  315. {
  316. serial_setbrg_dev(CONFIG_PSC_CONSOLE);
  317. }
  318. int serial_init(void)
  319. {
  320. return serial_init_dev(CONFIG_PSC_CONSOLE);
  321. }
  322. void serial_putc(const char c)
  323. {
  324. serial_putc_dev(CONFIG_PSC_CONSOLE, c);
  325. }
  326. void serial_putc_raw(const char c)
  327. {
  328. serial_putc_raw_dev(CONFIG_PSC_CONSOLE, c);
  329. }
  330. void serial_puts(const char *s)
  331. {
  332. serial_puts_dev(CONFIG_PSC_CONSOLE, s);
  333. }
  334. int serial_getc(void)
  335. {
  336. return serial_getc_dev(CONFIG_PSC_CONSOLE);
  337. }
  338. int serial_tstc(void)
  339. {
  340. return serial_tstc_dev(CONFIG_PSC_CONSOLE);
  341. }
  342. void serial_setrts(int s)
  343. {
  344. return serial_setrts_dev(CONFIG_PSC_CONSOLE, s);
  345. }
  346. int serial_getcts(void)
  347. {
  348. return serial_getcts_dev(CONFIG_PSC_CONSOLE);
  349. }
  350. #endif /* CONFIG_PSC_CONSOLE */
  351. #if defined(CONFIG_SERIAL_MULTI)
  352. #include <stdio_dev.h>
  353. /*
  354. * Routines for communication with serial devices over PSC
  355. */
  356. /* Bitfield for initialized PSCs */
  357. static unsigned int initialized;
  358. struct stdio_dev *open_port(int num, int baudrate)
  359. {
  360. struct stdio_dev *port;
  361. char env_var[16];
  362. char env_val[10];
  363. char name[7];
  364. if (num < 0 || num > 11)
  365. return NULL;
  366. sprintf(name, "psc%d", num);
  367. port = stdio_get_by_name(name);
  368. if (!port)
  369. return NULL;
  370. if (!test_bit(num, &initialized)) {
  371. sprintf(env_var, "psc%d_baudrate", num);
  372. sprintf(env_val, "%d", baudrate);
  373. setenv(env_var, env_val);
  374. if (port->start())
  375. return NULL;
  376. set_bit(num, &initialized);
  377. }
  378. return port;
  379. }
  380. int close_port(int num)
  381. {
  382. struct stdio_dev *port;
  383. int ret;
  384. char name[7];
  385. if (num < 0 || num > 11)
  386. return -1;
  387. sprintf(name, "psc%d", num);
  388. port = stdio_get_by_name(name);
  389. if (!port)
  390. return -1;
  391. ret = port->stop();
  392. clear_bit(num, &initialized);
  393. return ret;
  394. }
  395. int write_port(struct stdio_dev *port, char *buf)
  396. {
  397. if (!port || !buf)
  398. return -1;
  399. port->puts(buf);
  400. return 0;
  401. }
  402. int read_port(struct stdio_dev *port, char *buf, int size)
  403. {
  404. int cnt = 0;
  405. if (!port || !buf)
  406. return -1;
  407. if (!size)
  408. return 0;
  409. while (port->tstc()) {
  410. buf[cnt++] = port->getc();
  411. if (cnt > size)
  412. break;
  413. }
  414. return cnt;
  415. }
  416. #endif /* CONFIG_SERIAL_MULTI */