serial_mpc8xx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <commproc.h>
  9. #include <command.h>
  10. #include <serial.h>
  11. #include <watchdog.h>
  12. #include <linux/compiler.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
  15. #define SMC_INDEX 0
  16. #define PROFF_SMC PROFF_SMC1
  17. #define CPM_CR_CH_SMC CPM_CR_CH_SMC1
  18. #define IOPINS 0xc0
  19. #elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
  20. #define SMC_INDEX 1
  21. #define PROFF_SMC PROFF_SMC2
  22. #define CPM_CR_CH_SMC CPM_CR_CH_SMC2
  23. #define IOPINS 0xc00
  24. #endif /* CONFIG_8xx_CONS_SMCx */
  25. struct serialbuffer {
  26. cbd_t rxbd; /* Rx BD */
  27. cbd_t txbd; /* Tx BD */
  28. uint rxindex; /* index for next character to read */
  29. uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
  30. uchar txbuf; /* tx buffers */
  31. };
  32. static void serial_setdivisor(cpm8xx_t __iomem *cp)
  33. {
  34. int divisor = (gd->cpu_clk + 8 * gd->baudrate) / 16 / gd->baudrate;
  35. if (divisor / 16 > 0x1000) {
  36. /* bad divisor, assume 50MHz clock and 9600 baud */
  37. divisor = (50 * 1000 * 1000 + 8 * 9600) / 16 / 9600;
  38. }
  39. divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
  40. if (divisor <= 0x1000)
  41. out_be32(&cp->cp_brgc1, ((divisor - 1) << 1) | CPM_BRG_EN);
  42. else
  43. out_be32(&cp->cp_brgc1, ((divisor / 16 - 1) << 1) | CPM_BRG_EN |
  44. CPM_BRG_DIV16);
  45. }
  46. /*
  47. * Minimal serial functions needed to use one of the SMC ports
  48. * as serial console interface.
  49. */
  50. static void smc_setbrg(void)
  51. {
  52. immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
  53. cpm8xx_t __iomem *cp = &(im->im_cpm);
  54. /* Set up the baud rate generator.
  55. * See 8xx_io/commproc.c for details.
  56. *
  57. * Wire BRG1 to SMCx
  58. */
  59. out_be32(&cp->cp_simode, 0);
  60. serial_setdivisor(cp);
  61. }
  62. static int smc_init(void)
  63. {
  64. immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
  65. smc_t __iomem *sp;
  66. smc_uart_t __iomem *up;
  67. cpm8xx_t __iomem *cp = &(im->im_cpm);
  68. struct serialbuffer __iomem *rtx;
  69. /* initialize pointers to SMC */
  70. sp = cp->cp_smc + SMC_INDEX;
  71. up = (smc_uart_t __iomem *)&cp->cp_dparam[PROFF_SMC];
  72. /* Disable relocation */
  73. out_be16(&up->smc_rpbase, 0);
  74. /* Disable transmitter/receiver. */
  75. clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  76. /* Enable SDMA. */
  77. out_be32(&im->im_siu_conf.sc_sdcr, 1);
  78. /* clear error conditions */
  79. out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
  80. /* clear SDMA interrupt mask */
  81. out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
  82. /* Use Port B for SMCx instead of other functions. */
  83. setbits_be32(&cp->cp_pbpar, IOPINS);
  84. clrbits_be32(&cp->cp_pbdir, IOPINS);
  85. clrbits_be16(&cp->cp_pbodr, IOPINS);
  86. /* Set the physical address of the host memory buffers in
  87. * the buffer descriptors.
  88. */
  89. rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
  90. /* Allocate space for two buffer descriptors in the DP ram.
  91. * For now, this address seems OK, but it may have to
  92. * change with newer versions of the firmware.
  93. * damm: allocating space after the two buffers for rx/tx data
  94. */
  95. out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
  96. out_be16(&rtx->rxbd.cbd_sc, 0);
  97. out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
  98. out_be16(&rtx->txbd.cbd_sc, 0);
  99. /* Set up the uart parameters in the parameter ram. */
  100. out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
  101. out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
  102. out_8(&up->smc_rfcr, SMC_EB);
  103. out_8(&up->smc_tfcr, SMC_EB);
  104. /* Set UART mode, 8 bit, no parity, one stop.
  105. * Enable receive and transmit.
  106. */
  107. out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
  108. /* Mask all interrupts and remove anything pending.
  109. */
  110. out_8(&sp->smc_smcm, 0);
  111. out_8(&sp->smc_smce, 0xff);
  112. /* Set up the baud rate generator */
  113. smc_setbrg();
  114. /* Make the first buffer the only buffer. */
  115. setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
  116. setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
  117. /* single/multi character receive. */
  118. out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
  119. out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
  120. out_be32(&rtx->rxindex, 0);
  121. /* Initialize Tx/Rx parameters. */
  122. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
  123. ;
  124. out_be16(&cp->cp_cpcr,
  125. mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG);
  126. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG) /* wait if cp is busy */
  127. ;
  128. /* Enable transmitter/receiver. */
  129. setbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  130. return 0;
  131. }
  132. static void smc_putc(const char c)
  133. {
  134. immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
  135. cpm8xx_t __iomem *cpmp = &(im->im_cpm);
  136. struct serialbuffer __iomem *rtx;
  137. if (c == '\n')
  138. smc_putc('\r');
  139. rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
  140. /* Wait for last character to go. */
  141. out_8(&rtx->txbuf, c);
  142. out_be16(&rtx->txbd.cbd_datlen, 1);
  143. setbits_be16(&rtx->txbd.cbd_sc, BD_SC_READY);
  144. while (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
  145. WATCHDOG_RESET();
  146. }
  147. static void smc_puts(const char *s)
  148. {
  149. while (*s)
  150. smc_putc(*s++);
  151. }
  152. static int smc_getc(void)
  153. {
  154. immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
  155. cpm8xx_t __iomem *cpmp = &(im->im_cpm);
  156. struct serialbuffer __iomem *rtx;
  157. unsigned char c;
  158. uint rxindex;
  159. rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
  160. /* Wait for character to show up. */
  161. while (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
  162. WATCHDOG_RESET();
  163. /* the characters are read one by one,
  164. * use the rxindex to know the next char to deliver
  165. */
  166. rxindex = in_be32(&rtx->rxindex);
  167. c = in_8(rtx->rxbuf + rxindex);
  168. rxindex++;
  169. /* check if all char are readout, then make prepare for next receive */
  170. if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
  171. rxindex = 0;
  172. setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
  173. }
  174. out_be32(&rtx->rxindex, rxindex);
  175. return c;
  176. }
  177. static int smc_tstc(void)
  178. {
  179. immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
  180. cpm8xx_t __iomem *cpmp = &(im->im_cpm);
  181. struct serialbuffer __iomem *rtx;
  182. rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
  183. return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
  184. }
  185. struct serial_device serial_smc_device = {
  186. .name = "serial_smc",
  187. .start = smc_init,
  188. .stop = NULL,
  189. .setbrg = smc_setbrg,
  190. .getc = smc_getc,
  191. .tstc = smc_tstc,
  192. .putc = smc_putc,
  193. .puts = smc_puts,
  194. };
  195. __weak struct serial_device *default_serial_console(void)
  196. {
  197. return &serial_smc_device;
  198. }
  199. void mpc8xx_serial_initialize(void)
  200. {
  201. serial_register(&serial_smc_device);
  202. }