serial.c 11 KB

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