qe_tdm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2015 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Authors: Zhao Qiang <qiang.zhao@nxp.com>
  5. *
  6. * Description:
  7. * QE TDM API Set - TDM specific routines implementations.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.h>
  19. #include <soc/fsl/qe/qe_tdm.h>
  20. static int set_tdm_framer(const char *tdm_framer_type)
  21. {
  22. if (strcmp(tdm_framer_type, "e1") == 0)
  23. return TDM_FRAMER_E1;
  24. else if (strcmp(tdm_framer_type, "t1") == 0)
  25. return TDM_FRAMER_T1;
  26. else
  27. return -EINVAL;
  28. }
  29. static void set_si_param(struct ucc_tdm *utdm, struct ucc_tdm_info *ut_info)
  30. {
  31. struct si_mode_info *si_info = &ut_info->si_info;
  32. if (utdm->tdm_mode == TDM_INTERNAL_LOOPBACK) {
  33. si_info->simr_crt = 1;
  34. si_info->simr_rfsd = 0;
  35. }
  36. }
  37. int ucc_of_parse_tdm(struct device_node *np, struct ucc_tdm *utdm,
  38. struct ucc_tdm_info *ut_info)
  39. {
  40. const char *sprop;
  41. int ret = 0;
  42. u32 val;
  43. struct resource *res;
  44. struct device_node *np2;
  45. static int siram_init_flag;
  46. struct platform_device *pdev;
  47. sprop = of_get_property(np, "fsl,rx-sync-clock", NULL);
  48. if (sprop) {
  49. ut_info->uf_info.rx_sync = qe_clock_source(sprop);
  50. if ((ut_info->uf_info.rx_sync < QE_CLK_NONE) ||
  51. (ut_info->uf_info.rx_sync > QE_RSYNC_PIN)) {
  52. pr_err("QE-TDM: Invalid rx-sync-clock property\n");
  53. return -EINVAL;
  54. }
  55. } else {
  56. pr_err("QE-TDM: Invalid rx-sync-clock property\n");
  57. return -EINVAL;
  58. }
  59. sprop = of_get_property(np, "fsl,tx-sync-clock", NULL);
  60. if (sprop) {
  61. ut_info->uf_info.tx_sync = qe_clock_source(sprop);
  62. if ((ut_info->uf_info.tx_sync < QE_CLK_NONE) ||
  63. (ut_info->uf_info.tx_sync > QE_TSYNC_PIN)) {
  64. pr_err("QE-TDM: Invalid tx-sync-clock property\n");
  65. return -EINVAL;
  66. }
  67. } else {
  68. pr_err("QE-TDM: Invalid tx-sync-clock property\n");
  69. return -EINVAL;
  70. }
  71. ret = of_property_read_u32_index(np, "fsl,tx-timeslot-mask", 0, &val);
  72. if (ret) {
  73. pr_err("QE-TDM: Invalid tx-timeslot-mask property\n");
  74. return -EINVAL;
  75. }
  76. utdm->tx_ts_mask = val;
  77. ret = of_property_read_u32_index(np, "fsl,rx-timeslot-mask", 0, &val);
  78. if (ret) {
  79. ret = -EINVAL;
  80. pr_err("QE-TDM: Invalid rx-timeslot-mask property\n");
  81. return ret;
  82. }
  83. utdm->rx_ts_mask = val;
  84. ret = of_property_read_u32_index(np, "fsl,tdm-id", 0, &val);
  85. if (ret) {
  86. ret = -EINVAL;
  87. pr_err("QE-TDM: No fsl,tdm-id property for this UCC\n");
  88. return ret;
  89. }
  90. utdm->tdm_port = val;
  91. ut_info->uf_info.tdm_num = utdm->tdm_port;
  92. if (of_property_read_bool(np, "fsl,tdm-internal-loopback"))
  93. utdm->tdm_mode = TDM_INTERNAL_LOOPBACK;
  94. else
  95. utdm->tdm_mode = TDM_NORMAL;
  96. sprop = of_get_property(np, "fsl,tdm-framer-type", NULL);
  97. if (!sprop) {
  98. ret = -EINVAL;
  99. pr_err("QE-TDM: No tdm-framer-type property for UCC\n");
  100. return ret;
  101. }
  102. ret = set_tdm_framer(sprop);
  103. if (ret < 0)
  104. return -EINVAL;
  105. utdm->tdm_framer_type = ret;
  106. ret = of_property_read_u32_index(np, "fsl,siram-entry-id", 0, &val);
  107. if (ret) {
  108. ret = -EINVAL;
  109. pr_err("QE-TDM: No siram entry id for UCC\n");
  110. return ret;
  111. }
  112. utdm->siram_entry_id = val;
  113. set_si_param(utdm, ut_info);
  114. np2 = of_find_compatible_node(NULL, NULL, "fsl,t1040-qe-si");
  115. if (!np2)
  116. return -EINVAL;
  117. pdev = of_find_device_by_node(np2);
  118. if (!pdev) {
  119. pr_err("%s: failed to lookup pdev\n", np2->name);
  120. of_node_put(np2);
  121. return -EINVAL;
  122. }
  123. of_node_put(np2);
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. utdm->si_regs = devm_ioremap_resource(&pdev->dev, res);
  126. if (IS_ERR(utdm->si_regs)) {
  127. ret = PTR_ERR(utdm->si_regs);
  128. goto err_miss_siram_property;
  129. }
  130. np2 = of_find_compatible_node(NULL, NULL, "fsl,t1040-qe-siram");
  131. if (!np2) {
  132. ret = -EINVAL;
  133. goto err_miss_siram_property;
  134. }
  135. pdev = of_find_device_by_node(np2);
  136. if (!pdev) {
  137. ret = -EINVAL;
  138. pr_err("%s: failed to lookup pdev\n", np2->name);
  139. of_node_put(np2);
  140. goto err_miss_siram_property;
  141. }
  142. of_node_put(np2);
  143. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  144. utdm->siram = devm_ioremap_resource(&pdev->dev, res);
  145. if (IS_ERR(utdm->siram)) {
  146. ret = PTR_ERR(utdm->siram);
  147. goto err_miss_siram_property;
  148. }
  149. if (siram_init_flag == 0) {
  150. memset_io(utdm->siram, 0, resource_size(res));
  151. siram_init_flag = 1;
  152. }
  153. return ret;
  154. err_miss_siram_property:
  155. devm_iounmap(&pdev->dev, utdm->si_regs);
  156. return ret;
  157. }
  158. EXPORT_SYMBOL(ucc_of_parse_tdm);
  159. void ucc_tdm_init(struct ucc_tdm *utdm, struct ucc_tdm_info *ut_info)
  160. {
  161. struct si1 __iomem *si_regs;
  162. u16 __iomem *siram;
  163. u16 siram_entry_valid;
  164. u16 siram_entry_closed;
  165. u16 ucc_num;
  166. u8 csel;
  167. u16 sixmr;
  168. u16 tdm_port;
  169. u32 siram_entry_id;
  170. u32 mask;
  171. int i;
  172. si_regs = utdm->si_regs;
  173. siram = utdm->siram;
  174. ucc_num = ut_info->uf_info.ucc_num;
  175. tdm_port = utdm->tdm_port;
  176. siram_entry_id = utdm->siram_entry_id;
  177. if (utdm->tdm_framer_type == TDM_FRAMER_T1)
  178. utdm->num_of_ts = 24;
  179. if (utdm->tdm_framer_type == TDM_FRAMER_E1)
  180. utdm->num_of_ts = 32;
  181. /* set siram table */
  182. csel = (ucc_num < 4) ? ucc_num + 9 : ucc_num - 3;
  183. siram_entry_valid = SIR_CSEL(csel) | SIR_BYTE | SIR_CNT(0);
  184. siram_entry_closed = SIR_IDLE | SIR_BYTE | SIR_CNT(0);
  185. for (i = 0; i < utdm->num_of_ts; i++) {
  186. mask = 0x01 << i;
  187. if (utdm->tx_ts_mask & mask)
  188. iowrite16be(siram_entry_valid,
  189. &siram[siram_entry_id * 32 + i]);
  190. else
  191. iowrite16be(siram_entry_closed,
  192. &siram[siram_entry_id * 32 + i]);
  193. if (utdm->rx_ts_mask & mask)
  194. iowrite16be(siram_entry_valid,
  195. &siram[siram_entry_id * 32 + 0x200 + i]);
  196. else
  197. iowrite16be(siram_entry_closed,
  198. &siram[siram_entry_id * 32 + 0x200 + i]);
  199. }
  200. setbits16(&siram[(siram_entry_id * 32) + (utdm->num_of_ts - 1)],
  201. SIR_LAST);
  202. setbits16(&siram[(siram_entry_id * 32) + 0x200 + (utdm->num_of_ts - 1)],
  203. SIR_LAST);
  204. /* Set SIxMR register */
  205. sixmr = SIMR_SAD(siram_entry_id);
  206. sixmr &= ~SIMR_SDM_MASK;
  207. if (utdm->tdm_mode == TDM_INTERNAL_LOOPBACK)
  208. sixmr |= SIMR_SDM_INTERNAL_LOOPBACK;
  209. else
  210. sixmr |= SIMR_SDM_NORMAL;
  211. sixmr |= SIMR_RFSD(ut_info->si_info.simr_rfsd) |
  212. SIMR_TFSD(ut_info->si_info.simr_tfsd);
  213. if (ut_info->si_info.simr_crt)
  214. sixmr |= SIMR_CRT;
  215. if (ut_info->si_info.simr_sl)
  216. sixmr |= SIMR_SL;
  217. if (ut_info->si_info.simr_ce)
  218. sixmr |= SIMR_CE;
  219. if (ut_info->si_info.simr_fe)
  220. sixmr |= SIMR_FE;
  221. if (ut_info->si_info.simr_gm)
  222. sixmr |= SIMR_GM;
  223. switch (tdm_port) {
  224. case 0:
  225. iowrite16be(sixmr, &si_regs->sixmr1[0]);
  226. break;
  227. case 1:
  228. iowrite16be(sixmr, &si_regs->sixmr1[1]);
  229. break;
  230. case 2:
  231. iowrite16be(sixmr, &si_regs->sixmr1[2]);
  232. break;
  233. case 3:
  234. iowrite16be(sixmr, &si_regs->sixmr1[3]);
  235. break;
  236. default:
  237. pr_err("QE-TDM: can not find tdm sixmr reg\n");
  238. break;
  239. }
  240. }
  241. EXPORT_SYMBOL(ucc_tdm_init);