ucc_slow.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2006 Freescale Semiconductor, Inc. All rights reserved.
  4. *
  5. * Authors: Shlomi Gridish <[email protected]>
  6. * Li Yang <[email protected]>
  7. *
  8. * Description:
  9. * QE UCC Slow API Set - UCC Slow specific routines implementations.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/stddef.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/err.h>
  17. #include <linux/export.h>
  18. #include <asm/io.h>
  19. #include <soc/fsl/qe/immap_qe.h>
  20. #include <soc/fsl/qe/qe.h>
  21. #include <soc/fsl/qe/ucc.h>
  22. #include <soc/fsl/qe/ucc_slow.h>
  23. u32 ucc_slow_get_qe_cr_subblock(int uccs_num)
  24. {
  25. switch (uccs_num) {
  26. case 0: return QE_CR_SUBBLOCK_UCCSLOW1;
  27. case 1: return QE_CR_SUBBLOCK_UCCSLOW2;
  28. case 2: return QE_CR_SUBBLOCK_UCCSLOW3;
  29. case 3: return QE_CR_SUBBLOCK_UCCSLOW4;
  30. case 4: return QE_CR_SUBBLOCK_UCCSLOW5;
  31. case 5: return QE_CR_SUBBLOCK_UCCSLOW6;
  32. case 6: return QE_CR_SUBBLOCK_UCCSLOW7;
  33. case 7: return QE_CR_SUBBLOCK_UCCSLOW8;
  34. default: return QE_CR_SUBBLOCK_INVALID;
  35. }
  36. }
  37. EXPORT_SYMBOL(ucc_slow_get_qe_cr_subblock);
  38. void ucc_slow_graceful_stop_tx(struct ucc_slow_private * uccs)
  39. {
  40. struct ucc_slow_info *us_info = uccs->us_info;
  41. u32 id;
  42. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  43. qe_issue_cmd(QE_GRACEFUL_STOP_TX, id,
  44. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  45. }
  46. EXPORT_SYMBOL(ucc_slow_graceful_stop_tx);
  47. void ucc_slow_stop_tx(struct ucc_slow_private * uccs)
  48. {
  49. struct ucc_slow_info *us_info = uccs->us_info;
  50. u32 id;
  51. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  52. qe_issue_cmd(QE_STOP_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
  53. }
  54. EXPORT_SYMBOL(ucc_slow_stop_tx);
  55. void ucc_slow_restart_tx(struct ucc_slow_private * uccs)
  56. {
  57. struct ucc_slow_info *us_info = uccs->us_info;
  58. u32 id;
  59. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  60. qe_issue_cmd(QE_RESTART_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
  61. }
  62. EXPORT_SYMBOL(ucc_slow_restart_tx);
  63. void ucc_slow_enable(struct ucc_slow_private * uccs, enum comm_dir mode)
  64. {
  65. struct ucc_slow __iomem *us_regs;
  66. u32 gumr_l;
  67. us_regs = uccs->us_regs;
  68. /* Enable reception and/or transmission on this UCC. */
  69. gumr_l = ioread32be(&us_regs->gumr_l);
  70. if (mode & COMM_DIR_TX) {
  71. gumr_l |= UCC_SLOW_GUMR_L_ENT;
  72. uccs->enabled_tx = 1;
  73. }
  74. if (mode & COMM_DIR_RX) {
  75. gumr_l |= UCC_SLOW_GUMR_L_ENR;
  76. uccs->enabled_rx = 1;
  77. }
  78. iowrite32be(gumr_l, &us_regs->gumr_l);
  79. }
  80. EXPORT_SYMBOL(ucc_slow_enable);
  81. void ucc_slow_disable(struct ucc_slow_private * uccs, enum comm_dir mode)
  82. {
  83. struct ucc_slow __iomem *us_regs;
  84. u32 gumr_l;
  85. us_regs = uccs->us_regs;
  86. /* Disable reception and/or transmission on this UCC. */
  87. gumr_l = ioread32be(&us_regs->gumr_l);
  88. if (mode & COMM_DIR_TX) {
  89. gumr_l &= ~UCC_SLOW_GUMR_L_ENT;
  90. uccs->enabled_tx = 0;
  91. }
  92. if (mode & COMM_DIR_RX) {
  93. gumr_l &= ~UCC_SLOW_GUMR_L_ENR;
  94. uccs->enabled_rx = 0;
  95. }
  96. iowrite32be(gumr_l, &us_regs->gumr_l);
  97. }
  98. EXPORT_SYMBOL(ucc_slow_disable);
  99. /* Initialize the UCC for Slow operations
  100. *
  101. * The caller should initialize the following us_info
  102. */
  103. int ucc_slow_init(struct ucc_slow_info * us_info, struct ucc_slow_private ** uccs_ret)
  104. {
  105. struct ucc_slow_private *uccs;
  106. u32 i;
  107. struct ucc_slow __iomem *us_regs;
  108. u32 gumr;
  109. struct qe_bd __iomem *bd;
  110. u32 id;
  111. u32 command;
  112. int ret = 0;
  113. if (!us_info)
  114. return -EINVAL;
  115. /* check if the UCC port number is in range. */
  116. if ((us_info->ucc_num < 0) || (us_info->ucc_num > UCC_MAX_NUM - 1)) {
  117. printk(KERN_ERR "%s: illegal UCC number\n", __func__);
  118. return -EINVAL;
  119. }
  120. /*
  121. * Set mrblr
  122. * Check that 'max_rx_buf_length' is properly aligned (4), unless
  123. * rfw is 1, meaning that QE accepts one byte at a time, unlike normal
  124. * case when QE accepts 32 bits at a time.
  125. */
  126. if ((!us_info->rfw) &&
  127. (us_info->max_rx_buf_length & (UCC_SLOW_MRBLR_ALIGNMENT - 1))) {
  128. printk(KERN_ERR "max_rx_buf_length not aligned.\n");
  129. return -EINVAL;
  130. }
  131. uccs = kzalloc(sizeof(struct ucc_slow_private), GFP_KERNEL);
  132. if (!uccs) {
  133. printk(KERN_ERR "%s: Cannot allocate private data\n",
  134. __func__);
  135. return -ENOMEM;
  136. }
  137. uccs->rx_base_offset = -1;
  138. uccs->tx_base_offset = -1;
  139. uccs->us_pram_offset = -1;
  140. /* Fill slow UCC structure */
  141. uccs->us_info = us_info;
  142. /* Set the PHY base address */
  143. uccs->us_regs = ioremap(us_info->regs, sizeof(struct ucc_slow));
  144. if (uccs->us_regs == NULL) {
  145. printk(KERN_ERR "%s: Cannot map UCC registers\n", __func__);
  146. kfree(uccs);
  147. return -ENOMEM;
  148. }
  149. us_regs = uccs->us_regs;
  150. uccs->p_ucce = &us_regs->ucce;
  151. uccs->p_uccm = &us_regs->uccm;
  152. /* Get PRAM base */
  153. uccs->us_pram_offset =
  154. qe_muram_alloc(UCC_SLOW_PRAM_SIZE, ALIGNMENT_OF_UCC_SLOW_PRAM);
  155. if (uccs->us_pram_offset < 0) {
  156. printk(KERN_ERR "%s: cannot allocate MURAM for PRAM", __func__);
  157. ucc_slow_free(uccs);
  158. return -ENOMEM;
  159. }
  160. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  161. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, id, us_info->protocol,
  162. uccs->us_pram_offset);
  163. uccs->us_pram = qe_muram_addr(uccs->us_pram_offset);
  164. /* Set UCC to slow type */
  165. ret = ucc_set_type(us_info->ucc_num, UCC_SPEED_TYPE_SLOW);
  166. if (ret) {
  167. printk(KERN_ERR "%s: cannot set UCC type", __func__);
  168. ucc_slow_free(uccs);
  169. return ret;
  170. }
  171. iowrite16be(us_info->max_rx_buf_length, &uccs->us_pram->mrblr);
  172. INIT_LIST_HEAD(&uccs->confQ);
  173. /* Allocate BDs. */
  174. uccs->rx_base_offset =
  175. qe_muram_alloc(us_info->rx_bd_ring_len * sizeof(struct qe_bd),
  176. QE_ALIGNMENT_OF_BD);
  177. if (uccs->rx_base_offset < 0) {
  178. printk(KERN_ERR "%s: cannot allocate %u RX BDs\n", __func__,
  179. us_info->rx_bd_ring_len);
  180. ucc_slow_free(uccs);
  181. return -ENOMEM;
  182. }
  183. uccs->tx_base_offset =
  184. qe_muram_alloc(us_info->tx_bd_ring_len * sizeof(struct qe_bd),
  185. QE_ALIGNMENT_OF_BD);
  186. if (uccs->tx_base_offset < 0) {
  187. printk(KERN_ERR "%s: cannot allocate TX BDs", __func__);
  188. ucc_slow_free(uccs);
  189. return -ENOMEM;
  190. }
  191. /* Init Tx bds */
  192. bd = uccs->confBd = uccs->tx_bd = qe_muram_addr(uccs->tx_base_offset);
  193. for (i = 0; i < us_info->tx_bd_ring_len - 1; i++) {
  194. /* clear bd buffer */
  195. iowrite32be(0, &bd->buf);
  196. /* set bd status and length */
  197. iowrite32be(0, (u32 __iomem *)bd);
  198. bd++;
  199. }
  200. /* for last BD set Wrap bit */
  201. iowrite32be(0, &bd->buf);
  202. iowrite32be(T_W, (u32 __iomem *)bd);
  203. /* Init Rx bds */
  204. bd = uccs->rx_bd = qe_muram_addr(uccs->rx_base_offset);
  205. for (i = 0; i < us_info->rx_bd_ring_len - 1; i++) {
  206. /* set bd status and length */
  207. iowrite32be(0, (u32 __iomem *)bd);
  208. /* clear bd buffer */
  209. iowrite32be(0, &bd->buf);
  210. bd++;
  211. }
  212. /* for last BD set Wrap bit */
  213. iowrite32be(R_W, (u32 __iomem *)bd);
  214. iowrite32be(0, &bd->buf);
  215. /* Set GUMR (For more details see the hardware spec.). */
  216. /* gumr_h */
  217. gumr = us_info->tcrc;
  218. if (us_info->cdp)
  219. gumr |= UCC_SLOW_GUMR_H_CDP;
  220. if (us_info->ctsp)
  221. gumr |= UCC_SLOW_GUMR_H_CTSP;
  222. if (us_info->cds)
  223. gumr |= UCC_SLOW_GUMR_H_CDS;
  224. if (us_info->ctss)
  225. gumr |= UCC_SLOW_GUMR_H_CTSS;
  226. if (us_info->tfl)
  227. gumr |= UCC_SLOW_GUMR_H_TFL;
  228. if (us_info->rfw)
  229. gumr |= UCC_SLOW_GUMR_H_RFW;
  230. if (us_info->txsy)
  231. gumr |= UCC_SLOW_GUMR_H_TXSY;
  232. if (us_info->rtsm)
  233. gumr |= UCC_SLOW_GUMR_H_RTSM;
  234. iowrite32be(gumr, &us_regs->gumr_h);
  235. /* gumr_l */
  236. gumr = (u32)us_info->tdcr | (u32)us_info->rdcr | (u32)us_info->tenc |
  237. (u32)us_info->renc | (u32)us_info->diag | (u32)us_info->mode;
  238. if (us_info->tci)
  239. gumr |= UCC_SLOW_GUMR_L_TCI;
  240. if (us_info->rinv)
  241. gumr |= UCC_SLOW_GUMR_L_RINV;
  242. if (us_info->tinv)
  243. gumr |= UCC_SLOW_GUMR_L_TINV;
  244. if (us_info->tend)
  245. gumr |= UCC_SLOW_GUMR_L_TEND;
  246. iowrite32be(gumr, &us_regs->gumr_l);
  247. /* Function code registers */
  248. /* if the data is in cachable memory, the 'global' */
  249. /* in the function code should be set. */
  250. iowrite8(UCC_BMR_BO_BE, &uccs->us_pram->tbmr);
  251. iowrite8(UCC_BMR_BO_BE, &uccs->us_pram->rbmr);
  252. /* rbase, tbase are offsets from MURAM base */
  253. iowrite16be(uccs->rx_base_offset, &uccs->us_pram->rbase);
  254. iowrite16be(uccs->tx_base_offset, &uccs->us_pram->tbase);
  255. /* Mux clocking */
  256. /* Grant Support */
  257. ucc_set_qe_mux_grant(us_info->ucc_num, us_info->grant_support);
  258. /* Breakpoint Support */
  259. ucc_set_qe_mux_bkpt(us_info->ucc_num, us_info->brkpt_support);
  260. /* Set Tsa or NMSI mode. */
  261. ucc_set_qe_mux_tsa(us_info->ucc_num, us_info->tsa);
  262. /* If NMSI (not Tsa), set Tx and Rx clock. */
  263. if (!us_info->tsa) {
  264. /* Rx clock routing */
  265. if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->rx_clock,
  266. COMM_DIR_RX)) {
  267. printk(KERN_ERR "%s: illegal value for RX clock\n",
  268. __func__);
  269. ucc_slow_free(uccs);
  270. return -EINVAL;
  271. }
  272. /* Tx clock routing */
  273. if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->tx_clock,
  274. COMM_DIR_TX)) {
  275. printk(KERN_ERR "%s: illegal value for TX clock\n",
  276. __func__);
  277. ucc_slow_free(uccs);
  278. return -EINVAL;
  279. }
  280. }
  281. /* Set interrupt mask register at UCC level. */
  282. iowrite16be(us_info->uccm_mask, &us_regs->uccm);
  283. /* First, clear anything pending at UCC level,
  284. * otherwise, old garbage may come through
  285. * as soon as the dam is opened. */
  286. /* Writing '1' clears */
  287. iowrite16be(0xffff, &us_regs->ucce);
  288. /* Issue QE Init command */
  289. if (us_info->init_tx && us_info->init_rx)
  290. command = QE_INIT_TX_RX;
  291. else if (us_info->init_tx)
  292. command = QE_INIT_TX;
  293. else
  294. command = QE_INIT_RX; /* We know at least one is TRUE */
  295. qe_issue_cmd(command, id, us_info->protocol, 0);
  296. *uccs_ret = uccs;
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(ucc_slow_init);
  300. void ucc_slow_free(struct ucc_slow_private * uccs)
  301. {
  302. if (!uccs)
  303. return;
  304. qe_muram_free(uccs->rx_base_offset);
  305. qe_muram_free(uccs->tx_base_offset);
  306. qe_muram_free(uccs->us_pram_offset);
  307. if (uccs->us_regs)
  308. iounmap(uccs->us_regs);
  309. kfree(uccs);
  310. }
  311. EXPORT_SYMBOL(ucc_slow_free);