ucc_slow.c 9.7 KB

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