ucc_fast.c 11 KB

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