serial_mpc8xx.c 6.3 KB

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