cpm-serial.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * CPM serial console support.
  3. *
  4. * Copyright 2007 Freescale Semiconductor, Inc.
  5. * Author: Scott Wood <scottwood@freescale.com>
  6. *
  7. * It is assumed that the firmware (or the platform file) has already set
  8. * up the port.
  9. */
  10. #include "types.h"
  11. #include "io.h"
  12. #include "ops.h"
  13. #include "page.h"
  14. struct cpm_scc {
  15. u32 gsmrl;
  16. u32 gsmrh;
  17. u16 psmr;
  18. u8 res1[2];
  19. u16 todr;
  20. u16 dsr;
  21. u16 scce;
  22. u8 res2[2];
  23. u16 sccm;
  24. u8 res3;
  25. u8 sccs;
  26. u8 res4[8];
  27. };
  28. struct cpm_smc {
  29. u8 res1[2];
  30. u16 smcmr;
  31. u8 res2[2];
  32. u8 smce;
  33. u8 res3[3];
  34. u8 smcm;
  35. u8 res4[5];
  36. };
  37. struct cpm_param {
  38. u16 rbase;
  39. u16 tbase;
  40. u8 rfcr;
  41. u8 tfcr;
  42. u16 mrblr;
  43. u32 rstate;
  44. u8 res1[4];
  45. u16 rbptr;
  46. u8 res2[6];
  47. u32 tstate;
  48. u8 res3[4];
  49. u16 tbptr;
  50. u8 res4[6];
  51. u16 maxidl;
  52. u16 idlc;
  53. u16 brkln;
  54. u16 brkec;
  55. u16 brkcr;
  56. u16 rmask;
  57. u8 res5[4];
  58. };
  59. struct cpm_bd {
  60. u16 sc; /* Status and Control */
  61. u16 len; /* Data length in buffer */
  62. u8 *addr; /* Buffer address in host memory */
  63. };
  64. static void *cpcr;
  65. static struct cpm_param *param;
  66. static struct cpm_smc *smc;
  67. static struct cpm_scc *scc;
  68. static struct cpm_bd *tbdf, *rbdf;
  69. static u32 cpm_cmd;
  70. static void *cbd_addr;
  71. static u32 cbd_offset;
  72. static void (*do_cmd)(int op);
  73. static void (*enable_port)(void);
  74. static void (*disable_port)(void);
  75. #define CPM_CMD_STOP_TX 4
  76. #define CPM_CMD_RESTART_TX 6
  77. #define CPM_CMD_INIT_RX_TX 0
  78. static void cpm1_cmd(int op)
  79. {
  80. while (in_be16(cpcr) & 1)
  81. ;
  82. out_be16(cpcr, (op << 8) | cpm_cmd | 1);
  83. while (in_be16(cpcr) & 1)
  84. ;
  85. }
  86. static void cpm2_cmd(int op)
  87. {
  88. while (in_be32(cpcr) & 0x10000)
  89. ;
  90. out_be32(cpcr, op | cpm_cmd | 0x10000);
  91. while (in_be32(cpcr) & 0x10000)
  92. ;
  93. }
  94. static void smc_disable_port(void)
  95. {
  96. do_cmd(CPM_CMD_STOP_TX);
  97. out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3);
  98. }
  99. static void scc_disable_port(void)
  100. {
  101. do_cmd(CPM_CMD_STOP_TX);
  102. out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30);
  103. }
  104. static void smc_enable_port(void)
  105. {
  106. out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3);
  107. do_cmd(CPM_CMD_RESTART_TX);
  108. }
  109. static void scc_enable_port(void)
  110. {
  111. out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30);
  112. do_cmd(CPM_CMD_RESTART_TX);
  113. }
  114. static int cpm_serial_open(void)
  115. {
  116. disable_port();
  117. out_8(&param->rfcr, 0x10);
  118. out_8(&param->tfcr, 0x10);
  119. out_be16(&param->mrblr, 1);
  120. out_be16(&param->maxidl, 0);
  121. out_be16(&param->brkec, 0);
  122. out_be16(&param->brkln, 0);
  123. out_be16(&param->brkcr, 0);
  124. rbdf = cbd_addr;
  125. rbdf->addr = (u8 *)rbdf - 1;
  126. rbdf->sc = 0xa000;
  127. rbdf->len = 1;
  128. tbdf = rbdf + 1;
  129. tbdf->addr = (u8 *)rbdf - 2;
  130. tbdf->sc = 0x2000;
  131. tbdf->len = 1;
  132. sync();
  133. out_be16(&param->rbase, cbd_offset);
  134. out_be16(&param->tbase, cbd_offset + sizeof(struct cpm_bd));
  135. do_cmd(CPM_CMD_INIT_RX_TX);
  136. enable_port();
  137. return 0;
  138. }
  139. static void cpm_serial_putc(unsigned char c)
  140. {
  141. while (tbdf->sc & 0x8000)
  142. barrier();
  143. sync();
  144. tbdf->addr[0] = c;
  145. eieio();
  146. tbdf->sc |= 0x8000;
  147. }
  148. static unsigned char cpm_serial_tstc(void)
  149. {
  150. barrier();
  151. return !(rbdf->sc & 0x8000);
  152. }
  153. static unsigned char cpm_serial_getc(void)
  154. {
  155. unsigned char c;
  156. while (!cpm_serial_tstc())
  157. ;
  158. sync();
  159. c = rbdf->addr[0];
  160. eieio();
  161. rbdf->sc |= 0x8000;
  162. return c;
  163. }
  164. int cpm_console_init(void *devp, struct serial_console_data *scdp)
  165. {
  166. void *vreg[2];
  167. u32 reg[2];
  168. int is_smc = 0, is_cpm2 = 0;
  169. void *parent, *muram;
  170. void *muram_addr;
  171. unsigned long muram_offset, muram_size;
  172. if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) {
  173. is_smc = 1;
  174. } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) {
  175. is_cpm2 = 1;
  176. } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) {
  177. is_cpm2 = 1;
  178. is_smc = 1;
  179. }
  180. if (is_smc) {
  181. enable_port = smc_enable_port;
  182. disable_port = smc_disable_port;
  183. } else {
  184. enable_port = scc_enable_port;
  185. disable_port = scc_disable_port;
  186. }
  187. if (is_cpm2)
  188. do_cmd = cpm2_cmd;
  189. else
  190. do_cmd = cpm1_cmd;
  191. if (getprop(devp, "fsl,cpm-command", &cpm_cmd, 4) < 4)
  192. return -1;
  193. if (dt_get_virtual_reg(devp, vreg, 2) < 2)
  194. return -1;
  195. if (is_smc)
  196. smc = vreg[0];
  197. else
  198. scc = vreg[0];
  199. param = vreg[1];
  200. parent = get_parent(devp);
  201. if (!parent)
  202. return -1;
  203. if (dt_get_virtual_reg(parent, &cpcr, 1) < 1)
  204. return -1;
  205. muram = finddevice("/soc/cpm/muram/data");
  206. if (!muram)
  207. return -1;
  208. /* For bootwrapper-compatible device trees, we assume that the first
  209. * entry has at least 128 bytes, and that #address-cells/#data-cells
  210. * is one for both parent and child.
  211. */
  212. if (dt_get_virtual_reg(muram, &muram_addr, 1) < 1)
  213. return -1;
  214. if (getprop(muram, "reg", reg, 8) < 8)
  215. return -1;
  216. muram_offset = reg[0];
  217. muram_size = reg[1];
  218. /* Store the buffer descriptors at the end of the first muram chunk.
  219. * For SMC ports on CPM2-based platforms, relocate the parameter RAM
  220. * just before the buffer descriptors.
  221. */
  222. cbd_offset = muram_offset + muram_size - 2 * sizeof(struct cpm_bd);
  223. if (is_cpm2 && is_smc) {
  224. u16 *smc_base = (u16 *)param;
  225. u16 pram_offset;
  226. pram_offset = cbd_offset - 64;
  227. pram_offset = _ALIGN_DOWN(pram_offset, 64);
  228. disable_port();
  229. out_be16(smc_base, pram_offset);
  230. param = muram_addr - muram_offset + pram_offset;
  231. }
  232. cbd_addr = muram_addr - muram_offset + cbd_offset;
  233. scdp->open = cpm_serial_open;
  234. scdp->putc = cpm_serial_putc;
  235. scdp->getc = cpm_serial_getc;
  236. scdp->tstc = cpm_serial_tstc;
  237. return 0;
  238. }