ucc_slow.c 9.8 KB

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