serial_smc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. *(ushort *)(&im->im_dprambase[PROFF_SMC_BASE]) = 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 void mpc8260_smc_serial_puts(const char *s)
  176. {
  177. while (*s) {
  178. serial_putc (*s++);
  179. }
  180. }
  181. static int mpc8260_smc_serial_getc(void)
  182. {
  183. volatile smc_uart_t *up;
  184. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  185. volatile serialbuffer_t *rtx;
  186. unsigned char c;
  187. up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
  188. rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
  189. /* Wait for character to show up. */
  190. while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
  191. ;
  192. /* the characters are read one by one,
  193. * use the rxindex to know the next char to deliver
  194. */
  195. c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr + rtx->rxindex);
  196. rtx->rxindex++;
  197. /* check if all char are readout, then make prepare for next receive */
  198. if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
  199. rtx->rxindex = 0;
  200. rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
  201. }
  202. return(c);
  203. }
  204. static int mpc8260_smc_serial_tstc(void)
  205. {
  206. volatile smc_uart_t *up;
  207. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  208. volatile serialbuffer_t *rtx;
  209. up = (smc_uart_t *)&(im->im_dprambase[PROFF_SMC]);
  210. rtx = (serialbuffer_t *)&im->im_dprambase[up->smc_rbase];
  211. return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
  212. }
  213. static struct serial_device mpc8260_smc_serial_drv = {
  214. .name = "mpc8260_smc_uart",
  215. .start = mpc8260_smc_serial_init,
  216. .stop = NULL,
  217. .setbrg = mpc8260_smc_serial_setbrg,
  218. .putc = mpc8260_smc_serial_putc,
  219. .puts = mpc8260_smc_serial_puts,
  220. .getc = mpc8260_smc_serial_getc,
  221. .tstc = mpc8260_smc_serial_tstc,
  222. };
  223. void mpc8260_smc_serial_initialize(void)
  224. {
  225. serial_register(&mpc8260_smc_serial_drv);
  226. }
  227. __weak struct serial_device *default_serial_console(void)
  228. {
  229. return &mpc8260_smc_serial_drv;
  230. }
  231. #endif /* CONFIG_CONS_ON_SMC */
  232. #if defined(CONFIG_KGDB_ON_SMC)
  233. #if defined(CONFIG_CONS_ON_SMC) && CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX
  234. #error Whoops! serial console and kgdb are on the same smc serial port
  235. #endif
  236. #if CONFIG_KGDB_INDEX == 1 /* KGDB Port on SMC1 */
  237. #define KGDB_SMC_INDEX 0
  238. #define KGDB_PROFF_SMC_BASE PROFF_SMC1_BASE
  239. #define KGDB_PROFF_SMC PROFF_SMC1
  240. #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC1_PAGE
  241. #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC1_SBLOCK
  242. #define KGDB_CMXSMR_MASK (CMXSMR_SMC1|CMXSMR_SMC1CS_MSK)
  243. #define KGDB_CMXSMR_VALUE CMXSMR_SMC1CS_BRG7
  244. #elif CONFIG_KGDB_INDEX == 2 /* KGDB Port on SMC2 */
  245. #define KGDB_SMC_INDEX 1
  246. #define KGDB_PROFF_SMC_BASE PROFF_SMC2_BASE
  247. #define KGDB_PROFF_SMC PROFF_SMC2
  248. #define KGDB_CPM_CR_SMC_PAGE CPM_CR_SMC2_PAGE
  249. #define KGDB_CPM_CR_SMC_SBLOCK CPM_CR_SMC2_SBLOCK
  250. #define KGDB_CMXSMR_MASK (CMXSMR_SMC2|CMXSMR_SMC2CS_MSK)
  251. #define KGDB_CMXSMR_VALUE CMXSMR_SMC2CS_BRG8
  252. #else
  253. #error "console not correctly defined"
  254. #endif
  255. void
  256. kgdb_serial_init (void)
  257. {
  258. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  259. volatile smc_t *sp;
  260. volatile smc_uart_t *up;
  261. volatile cbd_t *tbdf, *rbdf;
  262. volatile cpm8260_t *cp = &(im->im_cpm);
  263. uint dpaddr, speed = CONFIG_KGDB_BAUDRATE;
  264. char *s, *e;
  265. if ((s = getenv("kgdbrate")) != NULL && *s != '\0') {
  266. ulong rate = simple_strtoul(s, &e, 10);
  267. if (e > s && *e == '\0')
  268. speed = rate;
  269. }
  270. /* initialize pointers to SMC */
  271. sp = (smc_t *) &(im->im_smc[KGDB_SMC_INDEX]);
  272. *(ushort *)(&im->im_dprambase[KGDB_PROFF_SMC_BASE]) = KGDB_PROFF_SMC;
  273. up = (smc_uart_t *)&im->im_dprambase[KGDB_PROFF_SMC];
  274. /* Disable transmitter/receiver. */
  275. sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  276. /* NOTE: I/O port pins are set up via the iop_conf_tab[] table */
  277. /* Allocate space for two buffer descriptors in the DP ram.
  278. * damm: allocating space after the two buffers for rx/tx data
  279. */
  280. dpaddr = m8260_cpm_dpalloc((2 * sizeof (cbd_t)) + 2, 16);
  281. /* Set the physical address of the host memory buffers in
  282. * the buffer descriptors.
  283. */
  284. rbdf = (cbd_t *)&im->im_dprambase[dpaddr];
  285. rbdf->cbd_bufaddr = (uint) (rbdf+2);
  286. rbdf->cbd_sc = 0;
  287. tbdf = rbdf + 1;
  288. tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
  289. tbdf->cbd_sc = 0;
  290. /* Set up the uart parameters in the parameter ram. */
  291. up->smc_rbase = dpaddr;
  292. up->smc_tbase = dpaddr+sizeof(cbd_t);
  293. up->smc_rfcr = CPMFCR_EB;
  294. up->smc_tfcr = CPMFCR_EB;
  295. up->smc_brklen = 0;
  296. up->smc_brkec = 0;
  297. up->smc_brkcr = 0;
  298. /* Set UART mode, 8 bit, no parity, one stop.
  299. * Enable receive and transmit.
  300. */
  301. sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
  302. /* Mask all interrupts and remove anything pending. */
  303. sp->smc_smcm = 0;
  304. sp->smc_smce = 0xff;
  305. /* put the SMC channel into NMSI (non multiplexd serial interface)
  306. * mode and wire either BRG7 to SMC1 or BRG8 to SMC2 (15-17).
  307. */
  308. im->im_cpmux.cmx_smr =
  309. (im->im_cpmux.cmx_smr & ~KGDB_CMXSMR_MASK) | KGDB_CMXSMR_VALUE;
  310. /* Set up the baud rate generator. */
  311. #if defined(CONFIG_KGDB_USE_EXTC)
  312. m8260_cpm_extcbrg(brg_map[KGDB_SMC_INDEX], speed,
  313. CONFIG_KGDB_EXTC_RATE, CONFIG_KGDB_EXTC_PINSEL);
  314. #else
  315. m8260_cpm_setbrg(brg_map[KGDB_SMC_INDEX], speed);
  316. #endif
  317. /* Make the first buffer the only buffer. */
  318. tbdf->cbd_sc |= BD_SC_WRAP;
  319. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  320. /* Single character receive. */
  321. up->smc_mrblr = 1;
  322. up->smc_maxidl = 0;
  323. /* Initialize Tx/Rx parameters. */
  324. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  325. ;
  326. cp->cp_cpcr = mk_cr_cmd(KGDB_CPM_CR_SMC_PAGE, KGDB_CPM_CR_SMC_SBLOCK,
  327. 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  328. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  329. ;
  330. /* Enable transmitter/receiver. */
  331. sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
  332. printf("SMC%d at %dbps ", CONFIG_KGDB_INDEX, speed);
  333. }
  334. void
  335. putDebugChar(const char c)
  336. {
  337. volatile cbd_t *tbdf;
  338. volatile char *buf;
  339. volatile smc_uart_t *up;
  340. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  341. if (c == '\n')
  342. putDebugChar ('\r');
  343. up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
  344. tbdf = (cbd_t *)&im->im_dprambase[up->smc_tbase];
  345. /* Wait for last character to go. */
  346. buf = (char *)tbdf->cbd_bufaddr;
  347. while (tbdf->cbd_sc & BD_SC_READY)
  348. ;
  349. *buf = c;
  350. tbdf->cbd_datlen = 1;
  351. tbdf->cbd_sc |= BD_SC_READY;
  352. }
  353. void
  354. putDebugStr (const char *s)
  355. {
  356. while (*s) {
  357. putDebugChar (*s++);
  358. }
  359. }
  360. int
  361. getDebugChar(void)
  362. {
  363. volatile cbd_t *rbdf;
  364. volatile unsigned char *buf;
  365. volatile smc_uart_t *up;
  366. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  367. unsigned char c;
  368. up = (smc_uart_t *)&(im->im_dprambase[KGDB_PROFF_SMC]);
  369. rbdf = (cbd_t *)&im->im_dprambase[up->smc_rbase];
  370. /* Wait for character to show up. */
  371. buf = (unsigned char *)rbdf->cbd_bufaddr;
  372. while (rbdf->cbd_sc & BD_SC_EMPTY)
  373. ;
  374. c = *buf;
  375. rbdf->cbd_sc |= BD_SC_EMPTY;
  376. return(c);
  377. }
  378. void
  379. kgdb_interruptible(int yes)
  380. {
  381. return;
  382. }
  383. #endif /* CONFIG_KGDB_ON_SMC */