ucc_fast.c 11 KB

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