serial_smc.c 12 KB

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