ucc_fast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 Fast API Set - UCC Fast 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 <asm/immap_qe.h>
  24. #include <asm/qe.h>
  25. #include <asm/ucc.h>
  26. #include <asm/ucc_fast.h>
  27. void ucc_fast_dump_regs(struct ucc_fast_private * uccf)
  28. {
  29. printk(KERN_INFO "UCC%u Fast registers:\n", uccf->uf_info->ucc_num);
  30. printk(KERN_INFO "Base address: 0x%p\n", uccf->uf_regs);
  31. printk(KERN_INFO "gumr : addr=0x%p, val=0x%08x\n",
  32. &uccf->uf_regs->gumr, in_be32(&uccf->uf_regs->gumr));
  33. printk(KERN_INFO "upsmr : addr=0x%p, val=0x%08x\n",
  34. &uccf->uf_regs->upsmr, in_be32(&uccf->uf_regs->upsmr));
  35. printk(KERN_INFO "utodr : addr=0x%p, val=0x%04x\n",
  36. &uccf->uf_regs->utodr, in_be16(&uccf->uf_regs->utodr));
  37. printk(KERN_INFO "udsr : addr=0x%p, val=0x%04x\n",
  38. &uccf->uf_regs->udsr, in_be16(&uccf->uf_regs->udsr));
  39. printk(KERN_INFO "ucce : addr=0x%p, val=0x%08x\n",
  40. &uccf->uf_regs->ucce, in_be32(&uccf->uf_regs->ucce));
  41. printk(KERN_INFO "uccm : addr=0x%p, val=0x%08x\n",
  42. &uccf->uf_regs->uccm, in_be32(&uccf->uf_regs->uccm));
  43. printk(KERN_INFO "uccs : addr=0x%p, val=0x%02x\n",
  44. &uccf->uf_regs->uccs, in_8(&uccf->uf_regs->uccs));
  45. printk(KERN_INFO "urfb : addr=0x%p, val=0x%08x\n",
  46. &uccf->uf_regs->urfb, in_be32(&uccf->uf_regs->urfb));
  47. printk(KERN_INFO "urfs : addr=0x%p, val=0x%04x\n",
  48. &uccf->uf_regs->urfs, in_be16(&uccf->uf_regs->urfs));
  49. printk(KERN_INFO "urfet : addr=0x%p, val=0x%04x\n",
  50. &uccf->uf_regs->urfet, in_be16(&uccf->uf_regs->urfet));
  51. printk(KERN_INFO "urfset: addr=0x%p, val=0x%04x\n",
  52. &uccf->uf_regs->urfset, in_be16(&uccf->uf_regs->urfset));
  53. printk(KERN_INFO "utfb : addr=0x%p, val=0x%08x\n",
  54. &uccf->uf_regs->utfb, in_be32(&uccf->uf_regs->utfb));
  55. printk(KERN_INFO "utfs : addr=0x%p, val=0x%04x\n",
  56. &uccf->uf_regs->utfs, in_be16(&uccf->uf_regs->utfs));
  57. printk(KERN_INFO "utfet : addr=0x%p, val=0x%04x\n",
  58. &uccf->uf_regs->utfet, in_be16(&uccf->uf_regs->utfet));
  59. printk(KERN_INFO "utftt : addr=0x%p, val=0x%04x\n",
  60. &uccf->uf_regs->utftt, in_be16(&uccf->uf_regs->utftt));
  61. printk(KERN_INFO "utpt : addr=0x%p, val=0x%04x\n",
  62. &uccf->uf_regs->utpt, in_be16(&uccf->uf_regs->utpt));
  63. printk(KERN_INFO "urtry : addr=0x%p, val=0x%08x\n",
  64. &uccf->uf_regs->urtry, in_be32(&uccf->uf_regs->urtry));
  65. printk(KERN_INFO "guemr : addr=0x%p, val=0x%02x\n",
  66. &uccf->uf_regs->guemr, in_8(&uccf->uf_regs->guemr));
  67. }
  68. EXPORT_SYMBOL(ucc_fast_dump_regs);
  69. u32 ucc_fast_get_qe_cr_subblock(int uccf_num)
  70. {
  71. switch (uccf_num) {
  72. case 0: return QE_CR_SUBBLOCK_UCCFAST1;
  73. case 1: return QE_CR_SUBBLOCK_UCCFAST2;
  74. case 2: return QE_CR_SUBBLOCK_UCCFAST3;
  75. case 3: return QE_CR_SUBBLOCK_UCCFAST4;
  76. case 4: return QE_CR_SUBBLOCK_UCCFAST5;
  77. case 5: return QE_CR_SUBBLOCK_UCCFAST6;
  78. case 6: return QE_CR_SUBBLOCK_UCCFAST7;
  79. case 7: return QE_CR_SUBBLOCK_UCCFAST8;
  80. default: return QE_CR_SUBBLOCK_INVALID;
  81. }
  82. }
  83. EXPORT_SYMBOL(ucc_fast_get_qe_cr_subblock);
  84. void ucc_fast_transmit_on_demand(struct ucc_fast_private * uccf)
  85. {
  86. out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
  87. }
  88. EXPORT_SYMBOL(ucc_fast_transmit_on_demand);
  89. void ucc_fast_enable(struct ucc_fast_private * uccf, enum comm_dir mode)
  90. {
  91. struct ucc_fast __iomem *uf_regs;
  92. u32 gumr;
  93. uf_regs = uccf->uf_regs;
  94. /* Enable reception and/or transmission on this UCC. */
  95. gumr = in_be32(&uf_regs->gumr);
  96. if (mode & COMM_DIR_TX) {
  97. gumr |= UCC_FAST_GUMR_ENT;
  98. uccf->enabled_tx = 1;
  99. }
  100. if (mode & COMM_DIR_RX) {
  101. gumr |= UCC_FAST_GUMR_ENR;
  102. uccf->enabled_rx = 1;
  103. }
  104. out_be32(&uf_regs->gumr, gumr);
  105. }
  106. EXPORT_SYMBOL(ucc_fast_enable);
  107. void ucc_fast_disable(struct ucc_fast_private * uccf, enum comm_dir mode)
  108. {
  109. struct ucc_fast __iomem *uf_regs;
  110. u32 gumr;
  111. uf_regs = uccf->uf_regs;
  112. /* Disable reception and/or transmission on this UCC. */
  113. gumr = in_be32(&uf_regs->gumr);
  114. if (mode & COMM_DIR_TX) {
  115. gumr &= ~UCC_FAST_GUMR_ENT;
  116. uccf->enabled_tx = 0;
  117. }
  118. if (mode & COMM_DIR_RX) {
  119. gumr &= ~UCC_FAST_GUMR_ENR;
  120. uccf->enabled_rx = 0;
  121. }
  122. out_be32(&uf_regs->gumr, gumr);
  123. }
  124. EXPORT_SYMBOL(ucc_fast_disable);
  125. int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** uccf_ret)
  126. {
  127. struct ucc_fast_private *uccf;
  128. struct ucc_fast __iomem *uf_regs;
  129. u32 gumr;
  130. int ret;
  131. if (!uf_info)
  132. return -EINVAL;
  133. /* check if the UCC port number is in range. */
  134. if ((uf_info->ucc_num < 0) || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
  135. printk(KERN_ERR "%s: illegal UCC number\n", __func__);
  136. return -EINVAL;
  137. }
  138. /* Check that 'max_rx_buf_length' is properly aligned (4). */
  139. if (uf_info->max_rx_buf_length & (UCC_FAST_MRBLR_ALIGNMENT - 1)) {
  140. printk(KERN_ERR "%s: max_rx_buf_length not aligned\n",
  141. __func__);
  142. return -EINVAL;
  143. }
  144. /* Validate Virtual Fifo register values */
  145. if (uf_info->urfs < UCC_FAST_URFS_MIN_VAL) {
  146. printk(KERN_ERR "%s: urfs is too small\n", __func__);
  147. return -EINVAL;
  148. }
  149. if (uf_info->urfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  150. printk(KERN_ERR "%s: urfs is not aligned\n", __func__);
  151. return -EINVAL;
  152. }
  153. if (uf_info->urfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  154. printk(KERN_ERR "%s: urfet is not aligned.\n", __func__);
  155. return -EINVAL;
  156. }
  157. if (uf_info->urfset & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  158. printk(KERN_ERR "%s: urfset is not aligned\n", __func__);
  159. return -EINVAL;
  160. }
  161. if (uf_info->utfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  162. printk(KERN_ERR "%s: utfs is not aligned\n", __func__);
  163. return -EINVAL;
  164. }
  165. if (uf_info->utfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  166. printk(KERN_ERR "%s: utfet is not aligned\n", __func__);
  167. return -EINVAL;
  168. }
  169. if (uf_info->utftt & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  170. printk(KERN_ERR "%s: utftt is not aligned\n", __func__);
  171. return -EINVAL;
  172. }
  173. uccf = kzalloc(sizeof(struct ucc_fast_private), GFP_KERNEL);
  174. if (!uccf) {
  175. printk(KERN_ERR "%s: Cannot allocate private data\n",
  176. __func__);
  177. return -ENOMEM;
  178. }
  179. /* Fill fast UCC structure */
  180. uccf->uf_info = uf_info;
  181. /* Set the PHY base address */
  182. uccf->uf_regs = ioremap(uf_info->regs, sizeof(struct ucc_fast));
  183. if (uccf->uf_regs == NULL) {
  184. printk(KERN_ERR "%s: Cannot map UCC registers\n", __func__);
  185. kfree(uccf);
  186. return -ENOMEM;
  187. }
  188. uccf->enabled_tx = 0;
  189. uccf->enabled_rx = 0;
  190. uccf->stopped_tx = 0;
  191. uccf->stopped_rx = 0;
  192. uf_regs = uccf->uf_regs;
  193. uccf->p_ucce = &uf_regs->ucce;
  194. uccf->p_uccm = &uf_regs->uccm;
  195. #ifdef CONFIG_UGETH_TX_ON_DEMAND
  196. uccf->p_utodr = &uf_regs->utodr;
  197. #endif
  198. #ifdef STATISTICS
  199. uccf->tx_frames = 0;
  200. uccf->rx_frames = 0;
  201. uccf->rx_discarded = 0;
  202. #endif /* STATISTICS */
  203. /* Set UCC to fast type */
  204. ret = ucc_set_type(uf_info->ucc_num, UCC_SPEED_TYPE_FAST);
  205. if (ret) {
  206. printk(KERN_ERR "%s: cannot set UCC type\n", __func__);
  207. ucc_fast_free(uccf);
  208. return ret;
  209. }
  210. uccf->mrblr = uf_info->max_rx_buf_length;
  211. /* Set GUMR */
  212. /* For more details see the hardware spec. */
  213. gumr = uf_info->ttx_trx;
  214. if (uf_info->tci)
  215. gumr |= UCC_FAST_GUMR_TCI;
  216. if (uf_info->cdp)
  217. gumr |= UCC_FAST_GUMR_CDP;
  218. if (uf_info->ctsp)
  219. gumr |= UCC_FAST_GUMR_CTSP;
  220. if (uf_info->cds)
  221. gumr |= UCC_FAST_GUMR_CDS;
  222. if (uf_info->ctss)
  223. gumr |= UCC_FAST_GUMR_CTSS;
  224. if (uf_info->txsy)
  225. gumr |= UCC_FAST_GUMR_TXSY;
  226. if (uf_info->rsyn)
  227. gumr |= UCC_FAST_GUMR_RSYN;
  228. gumr |= uf_info->synl;
  229. if (uf_info->rtsm)
  230. gumr |= UCC_FAST_GUMR_RTSM;
  231. gumr |= uf_info->renc;
  232. if (uf_info->revd)
  233. gumr |= UCC_FAST_GUMR_REVD;
  234. gumr |= uf_info->tenc;
  235. gumr |= uf_info->tcrc;
  236. gumr |= uf_info->mode;
  237. out_be32(&uf_regs->gumr, gumr);
  238. /* Allocate memory for Tx Virtual Fifo */
  239. uccf->ucc_fast_tx_virtual_fifo_base_offset =
  240. qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  241. if (IS_ERR_VALUE(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
  242. printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO\n",
  243. __func__);
  244. uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
  245. ucc_fast_free(uccf);
  246. return -ENOMEM;
  247. }
  248. /* Allocate memory for Rx Virtual Fifo */
  249. uccf->ucc_fast_rx_virtual_fifo_base_offset =
  250. qe_muram_alloc(uf_info->urfs +
  251. UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
  252. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  253. if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
  254. printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO\n",
  255. __func__);
  256. uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
  257. ucc_fast_free(uccf);
  258. return -ENOMEM;
  259. }
  260. /* Set Virtual Fifo registers */
  261. out_be16(&uf_regs->urfs, uf_info->urfs);
  262. out_be16(&uf_regs->urfet, uf_info->urfet);
  263. out_be16(&uf_regs->urfset, uf_info->urfset);
  264. out_be16(&uf_regs->utfs, uf_info->utfs);
  265. out_be16(&uf_regs->utfet, uf_info->utfet);
  266. out_be16(&uf_regs->utftt, uf_info->utftt);
  267. /* utfb, urfb are offsets from MURAM base */
  268. out_be32(&uf_regs->utfb, uccf->ucc_fast_tx_virtual_fifo_base_offset);
  269. out_be32(&uf_regs->urfb, uccf->ucc_fast_rx_virtual_fifo_base_offset);
  270. /* Mux clocking */
  271. /* Grant Support */
  272. ucc_set_qe_mux_grant(uf_info->ucc_num, uf_info->grant_support);
  273. /* Breakpoint Support */
  274. ucc_set_qe_mux_bkpt(uf_info->ucc_num, uf_info->brkpt_support);
  275. /* Set Tsa or NMSI mode. */
  276. ucc_set_qe_mux_tsa(uf_info->ucc_num, uf_info->tsa);
  277. /* If NMSI (not Tsa), set Tx and Rx clock. */
  278. if (!uf_info->tsa) {
  279. /* Rx clock routing */
  280. if ((uf_info->rx_clock != QE_CLK_NONE) &&
  281. ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->rx_clock,
  282. COMM_DIR_RX)) {
  283. printk(KERN_ERR "%s: illegal value for RX clock\n",
  284. __func__);
  285. ucc_fast_free(uccf);
  286. return -EINVAL;
  287. }
  288. /* Tx clock routing */
  289. if ((uf_info->tx_clock != QE_CLK_NONE) &&
  290. ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->tx_clock,
  291. COMM_DIR_TX)) {
  292. printk(KERN_ERR "%s: illegal value for TX clock\n",
  293. __func__);
  294. ucc_fast_free(uccf);
  295. return -EINVAL;
  296. }
  297. }
  298. /* Set interrupt mask register at UCC level. */
  299. out_be32(&uf_regs->uccm, uf_info->uccm_mask);
  300. /* First, clear anything pending at UCC level,
  301. * otherwise, old garbage may come through
  302. * as soon as the dam is opened. */
  303. /* Writing '1' clears */
  304. out_be32(&uf_regs->ucce, 0xffffffff);
  305. *uccf_ret = uccf;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(ucc_fast_init);
  309. void ucc_fast_free(struct ucc_fast_private * uccf)
  310. {
  311. if (!uccf)
  312. return;
  313. if (uccf->ucc_fast_tx_virtual_fifo_base_offset)
  314. qe_muram_free(uccf->ucc_fast_tx_virtual_fifo_base_offset);
  315. if (uccf->ucc_fast_rx_virtual_fifo_base_offset)
  316. qe_muram_free(uccf->ucc_fast_rx_virtual_fifo_base_offset);
  317. if (uccf->uf_regs)
  318. iounmap(uccf->uf_regs);
  319. kfree(uccf);
  320. }
  321. EXPORT_SYMBOL(ucc_fast_free);