serial_smc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * (C) Copyright 2000, 2001, 2002
  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. /*
  28. * Minimal serial functions needed to use one of the SMC ports
  29. * as serial console interface.
  30. */
  31. #include <common.h>
  32. #include <mpc8260.h>
  33. #include <asm/cpm_8260.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if defined(CONFIG_CONS_ON_SMC)
  36. #if CONFIG_CONS_INDEX == 1 /* Console on SMC1 */
  37. #define SMC_INDEX 0
  38. #define PROFF_SMC_BASE PROFF_SMC1_BASE
  39. #define PROFF_SMC PROFF_SMC1
  40. #define CPM_CR_SMC_PAGE CPM_CR_SMC1_PAGE
  41. #define CPM_CR_SMC_SBLOCK CPM_CR_SMC1_SBLOCK
  42. #define CMXSMR_MASK (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
  43. #define CMXSMR_VALUE CMXSMR_SMC1CS_BRG7
  44. #elif CONFIG_CONS_INDEX == 2 /* Console on SMC2 */
  45. #define SMC_INDEX 1
  46. #define PROFF_SMC_BASE PROFF_SMC2_BASE
  47. #define PROFF_SMC PROFF_SMC2
  48. #define CPM_CR_SMC_PAGE CPM_CR_SMC2_PAGE
  49. #define CPM_CR_SMC_SBLOCK CPM_CR_SMC2_SBLOCK
  50. #define CMXSMR_MASK (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
  51. #define CMXSMR_VALUE CMXSMR_SMC2CS_BRG8
  52. #else
  53. #error "console not correctly defined"
  54. #endif
  55. #if !defined(CONFIG_SYS_SMC_RXBUFLEN)
  56. #define CONFIG_SYS_SMC_RXBUFLEN 1
  57. #define CONFIG_SYS_MAXIDLE 0
  58. #else
  59. #if !defined(CONFIG_SYS_MAXIDLE)
  60. #error "you must define CONFIG_SYS_MAXIDLE"
  61. #endif
  62. #endif
  63. typedef volatile struct serialbuffer {
  64. cbd_t rxbd; /* Rx BD */
  65. cbd_t txbd; /* Tx BD */
  66. uint rxindex; /* index for next character to read */
  67. volatile uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
  68. volatile uchar txbuf; /* tx buffers */
  69. } serialbuffer_t;
  70. /* map rs_table index to baud rate generator index */
  71. static unsigned char brg_map[] = {
  72. 6, /* BRG7 for SMC1 */
  73. 7, /* BRG8 for SMC2 */
  74. 0, /* BRG1 for SCC1 */
  75. 1, /* BRG1 for SCC2 */
  76. 2, /* BRG1 for SCC3 */
  77. 3, /* BRG1 for SCC4 */
  78. };
  79. int serial_init (void)
  80. {
  81. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  82. volatile smc_t *sp;
  83. volatile smc_uart_t *up;
  84. volatile cpm8260_t *cp = &(im->im_cpm);
  85. uint dpaddr;
  86. volatile serialbuffer_t *rtx;
  87. /* initialize pointers to SMC */
  88. sp = (smc_t *) &(im->im_smc[SMC_INDEX]);
  89. *(ushort *)(&im->im_dprambase[PROFF_SMC_BASE]) = PROFF_SMC;
  90. up = (smc_uart_t *)&im->im_dprambase[PROFF_SMC];
  91. /* Disable transmitter/receiver. */
  92. sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  93. /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
  94. /* Allocate space for two buffer descriptors in the DP ram.
  95. * damm: allocating space after the two buffers for rx/tx data
  96. */
  97. /* allocate size of struct serialbuffer with bd rx/tx,
  98. * buffer rx/tx and rx index
  99. */
  100. dpaddr = m8260_cpm_dpalloc((sizeof(serialbuffer_t)), 16);
  101. rtx = (serialbuffer_t *)&im->im_dprambase[dpaddr];
  102. /* Set the physical address of the host memory buffers in
  103. * the buffer descriptors.
  104. */
  105. rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
  106. rtx->rxbd.cbd_sc = 0;
  107. rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
  108. rtx->txbd.cbd_sc = 0;
  109. /* Set up the uart parameters in the parameter ram. */
  110. up->smc_rbase = dpaddr;
  111. up->smc_tbase = dpaddr+sizeof(cbd_t);
  112. up->smc_rfcr = CPMFCR_EB;
  113. up->smc_tfcr = CPMFCR_EB;
  114. up->smc_brklen = 0;
  115. up->smc_brkec = 0;
  116. up->smc_brkcr = 0;
  117. /* Set UART mode, 8 bit, no parity, one stop.
  118. * Enable receive and transmit.
  119. */
  120. sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
  121. /* Mask all interrupts and remove anything pending. */
  122. sp->smc_smcm = 0;
  123. sp->smc_smce = 0xff;
  124. /* put the SMC channel into NMSI (non multiplexd serial interface)
  125. * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
  126. */
  127. im->im_cpmux.cmx_smr = (im->im_cpmux.cmx_smr&~CMXSMR_MASK)|CMXSMR_VALUE;
  128. /* Set up the baud rate generator. */
  129. serial_setbrg ();
  130. /* Make the first buffer the only buffer. */
  131. rtx->txbd.cbd_sc |= BD_SC_WRAP;
  132. rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  133. /* single/multi character receive. */
  134. up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
  135. up->smc_maxidl = CONFIG_SYS_MAXIDLE;
  136. rtx->rxindex = 0;
  137. /* Initialize Tx/Rx parameters. */
  138. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  139. ;
  140. cp->cp_cpcr = mk_cr_cmd(CPM_CR_SMC_PAGE, CPM_CR_SMC_SBLOCK,
  141. 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  142. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  143. ;
  144. /* Enable transmitter/receiver. */
  145. sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
  146. return (0);
  147. }
  148. void
  149. serial_setbrg (void)
  150. {
  151. #if defined(CONFIG_CONS_USE_EXTC)
  152. m8260_cpm_extcbrg(brg_map[SMC_INDEX], gd->baudrate,
  153. CONFIG_CONS_EXTC_RATE, CONFIG_CONS_EXTC_PINSEL);
  154. #else
  155. m8260_cpm_setbrg(brg_map[SMC_INDEX], gd->baudrate);
  156. #endif
  157. }
  158. void
  159. serial_putc(const char c)
  160. {
  161. volatile smc_uart_t *up;
  162. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  163. volatile serialbuffer_t *rtx;
  164. if (c == '\n')
  165. serial_putc ('\r');
  166. up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
  167. rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
  168. /* Wait for last character to go. */
  169. while (rtx->txbd.cbd_sc & BD_SC_READY & BD_SC_READY)
  170. ;
  171. rtx->txbuf = c;
  172. rtx->txbd.cbd_datlen = 1;
  173. rtx->txbd.cbd_sc |= BD_SC_READY;
  174. }
  175. void
  176. serial_puts (const char *s)
  177. {
  178. while (*s) {
  179. serial_putc (*s++);
  180. }
  181. }
  182. int
  183. serial_getc(void)
  184. {
  185. volatile smc_uart_t *up;
  186. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  187. volatile serialbuffer_t *rtx;
  188. unsigned char c;
  189. up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
  190. rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
  191. /* Wait for character to show up. */
  192. while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
  193. ;
  194. /* the characters are read one by one,
  195. * use the rxindex to know the next char to deliver
  196. */
  197. c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr + rtx->rxindex);
  198. rtx->rxindex++;
  199. /* check if all char are readout, then make prepare for next receive */
  200. if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
  201. rtx->rxindex = 0;
  202. rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
  203. }
  204. return(c);
  205. }
  206. int
  207. serial_tstc()
  208. {
  209. volatile smc_uart_t *up;
  210. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  211. volatile serialbuffer_t *rtx;
  212. up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
  213. rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
  214. return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
  215. }
  216. #endif /* CONFIG_CONS_ON_SMC */
  217. #if defined(CONFIG_KGDB_ON_SMC)
  218. #if defined(CONFIG_CONS_ON_SMC) && CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX
  219. #error Whoops! serial console and kgdb are on the same smc serial port
  220. #endif
  221. #if CONFIG_KGDB_INDEX == 1 /* KGDB Port on SMC1 */
  222. #define KGDB_SMC_INDEX 0
  223. #define KGDB_PROFF_SMC_BASE PROFF_SMC1_BASE
  224. #define KGDB_PROFF_SMC PROFF_SMC1
  225. #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC1_PAGE
  226. #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC1_SBLOCK
  227. #define KGDB_CMXSMR_MASK (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
  228. #define KGDB_CMXSMR_VALUE CMXSMR_SMC1CS_BRG7
  229. #elif CONFIG_KGDB_INDEX == 2 /* KGDB Port on SMC2 */
  230. #define KGDB_SMC_INDEX 1
  231. #define KGDB_PROFF_SMC_BASE PROFF_SMC2_BASE
  232. #define KGDB_PROFF_SMC PROFF_SMC2
  233. #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC2_PAGE
  234. #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC2_SBLOCK
  235. #define KGDB_CMXSMR_MASK (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
  236. #define KGDB_CMXSMR_VALUE CMXSMR_SMC2CS_BRG8
  237. #else
  238. #error "console not correctly defined"
  239. #endif
  240. void
  241. kgdb_serial_init (void)
  242. {
  243. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  244. volatile smc_t *sp;
  245. volatile smc_uart_t *up;
  246. volatile cbd_t *tbdf, *rbdf;
  247. volatile cpm8260_t *cp = &(im->im_cpm);
  248. uint dpaddr, speed = CONFIG_KGDB_BAUDRATE;
  249. char *s, *e;
  250. if ((s = getenv("kgdbrate")) != NULL && *s != '\0') {
  251. ulong rate = simple_strtoul(s, &e, 10);
  252. if (e > s && *e == '\0')
  253. speed = rate;
  254. }
  255. /* initialize pointers to SMC */
  256. sp = (smc_t *) &(im->im_smc[KGDB_SMC_INDEX]);
  257. *(ushort *)(&im->im_dprambase[KGDB_PROFF_SMC_BASE]) = KGDB_PROFF_SMC;
  258. up = (smc_uart_t *)&im->im_dprambase[KGDB_PROFF_SMC];
  259. /* Disable transmitter/receiver. */
  260. sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  261. /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
  262. /* Allocate space for two buffer descriptors in the DP ram.
  263. * damm: allocating space after the two buffers for rx/tx data
  264. */
  265. dpaddr = m8260_cpm_dpalloc((2 * sizeof (cbd_t)) + 2, 16);
  266. /* Set the physical address of the host memory buffers in
  267. * the buffer descriptors.
  268. */
  269. rbdf = (cbd_t *)&im->im_dprambase[dpaddr];
  270. rbdf->cbd_bufaddr = (uint) (rbdf+2);
  271. rbdf->cbd_sc = 0;
  272. tbdf = rbdf + 1;
  273. tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
  274. tbdf->cbd_sc = 0;
  275. /* Set up the uart parameters in the parameter ram. */
  276. up->smc_rbase = dpaddr;
  277. up->smc_tbase = dpaddr+sizeof(cbd_t);
  278. up->smc_rfcr = CPMFCR_EB;
  279. up->smc_tfcr = CPMFCR_EB;
  280. up->smc_brklen = 0;
  281. up->smc_brkec = 0;
  282. up->smc_brkcr = 0;
  283. /* Set UART mode, 8 bit, no parity, one stop.
  284. * Enable receive and transmit.
  285. */
  286. sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
  287. /* Mask all interrupts and remove anything pending. */
  288. sp->smc_smcm = 0;
  289. sp->smc_smce = 0xff;
  290. /* put the SMC channel into NMSI (non multiplexd serial interface)
  291. * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
  292. */
  293. im->im_cpmux.cmx_smr =
  294. (im->im_cpmux.cmx_smr & ~KGDB_CMXSMR_MASK) | KGDB_CMXSMR_VALUE;
  295. /* Set up the baud rate generator. */
  296. #if defined(CONFIG_KGDB_USE_EXTC)
  297. m8260_cpm_extcbrg(brg_map[KGDB_SMC_INDEX], speed,
  298. CONFIG_KGDB_EXTC_RATE, CONFIG_KGDB_EXTC_PINSEL);
  299. #else
  300. m8260_cpm_setbrg(brg_map[KGDB_SMC_INDEX], speed);
  301. #endif
  302. /* Make the first buffer the only buffer. */
  303. tbdf->cbd_sc |= BD_SC_WRAP;
  304. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  305. /* Single character receive. */
  306. up->smc_mrblr = 1;
  307. up->smc_maxidl = 0;
  308. /* Initialize Tx/Rx parameters. */
  309. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  310. ;
  311. cp->cp_cpcr = mk_cr_cmd(KGDB_CPM_CR_SMC_PAGE, KGDB_CPM_CR_SMC_SBLOCK,
  312. 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  313. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  314. ;
  315. /* Enable transmitter/receiver. */
  316. sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
  317. printf("SMC%d at %dbps ", CONFIG_KGDB_INDEX, speed);
  318. }
  319. void
  320. putDebugChar(const char c)
  321. {
  322. volatile cbd_t *tbdf;
  323. volatile char *buf;
  324. volatile smc_uart_t *up;
  325. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  326. if (c == '\n')
  327. putDebugChar ('\r');
  328. up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
  329. tbdf = (cbd_t *)&im->im_dprambase[up->smc_tbase];
  330. /* Wait for last character to go. */
  331. buf = (char *)tbdf->cbd_bufaddr;
  332. while (tbdf->cbd_sc & BD_SC_READY)
  333. ;
  334. *buf = c;
  335. tbdf->cbd_datlen = 1;
  336. tbdf->cbd_sc |= BD_SC_READY;
  337. }
  338. void
  339. putDebugStr (const char *s)
  340. {
  341. while (*s) {
  342. putDebugChar (*s++);
  343. }
  344. }
  345. int
  346. getDebugChar(void)
  347. {
  348. volatile cbd_t *rbdf;
  349. volatile unsigned char *buf;
  350. volatile smc_uart_t *up;
  351. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  352. unsigned char c;
  353. up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
  354. rbdf = (cbd_t *)&im->im_dprambase[up->smc_rbase];
  355. /* Wait for character to show up. */
  356. buf = (unsigned char *)rbdf->cbd_bufaddr;
  357. while (rbdf->cbd_sc & BD_SC_EMPTY)
  358. ;
  359. c = *buf;
  360. rbdf->cbd_sc |= BD_SC_EMPTY;
  361. return(c);
  362. }
  363. void
  364. kgdb_interruptible(int yes)
  365. {
  366. return;
  367. }
  368. #endif /* CONFIG_KGDB_ON_SMC */