ucc_fast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <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%d Fast registers:", uccf->uf_info->ucc_num);
  30. printk(KERN_INFO "Base address: 0x%08x", (u32) uccf->uf_regs);
  31. printk(KERN_INFO "gumr : addr - 0x%08x, val - 0x%08x",
  32. (u32) & uccf->uf_regs->gumr, in_be32(&uccf->uf_regs->gumr));
  33. printk(KERN_INFO "upsmr : addr - 0x%08x, val - 0x%08x",
  34. (u32) & uccf->uf_regs->upsmr, in_be32(&uccf->uf_regs->upsmr));
  35. printk(KERN_INFO "utodr : addr - 0x%08x, val - 0x%04x",
  36. (u32) & uccf->uf_regs->utodr, in_be16(&uccf->uf_regs->utodr));
  37. printk(KERN_INFO "udsr : addr - 0x%08x, val - 0x%04x",
  38. (u32) & uccf->uf_regs->udsr, in_be16(&uccf->uf_regs->udsr));
  39. printk(KERN_INFO "ucce : addr - 0x%08x, val - 0x%08x",
  40. (u32) & uccf->uf_regs->ucce, in_be32(&uccf->uf_regs->ucce));
  41. printk(KERN_INFO "uccm : addr - 0x%08x, val - 0x%08x",
  42. (u32) & uccf->uf_regs->uccm, in_be32(&uccf->uf_regs->uccm));
  43. printk(KERN_INFO "uccs : addr - 0x%08x, val - 0x%02x",
  44. (u32) & uccf->uf_regs->uccs, uccf->uf_regs->uccs);
  45. printk(KERN_INFO "urfb : addr - 0x%08x, val - 0x%08x",
  46. (u32) & uccf->uf_regs->urfb, in_be32(&uccf->uf_regs->urfb));
  47. printk(KERN_INFO "urfs : addr - 0x%08x, val - 0x%04x",
  48. (u32) & uccf->uf_regs->urfs, in_be16(&uccf->uf_regs->urfs));
  49. printk(KERN_INFO "urfet : addr - 0x%08x, val - 0x%04x",
  50. (u32) & uccf->uf_regs->urfet, in_be16(&uccf->uf_regs->urfet));
  51. printk(KERN_INFO "urfset: addr - 0x%08x, val - 0x%04x",
  52. (u32) & uccf->uf_regs->urfset,
  53. in_be16(&uccf->uf_regs->urfset));
  54. printk(KERN_INFO "utfb : addr - 0x%08x, val - 0x%08x",
  55. (u32) & uccf->uf_regs->utfb, in_be32(&uccf->uf_regs->utfb));
  56. printk(KERN_INFO "utfs : addr - 0x%08x, val - 0x%04x",
  57. (u32) & uccf->uf_regs->utfs, in_be16(&uccf->uf_regs->utfs));
  58. printk(KERN_INFO "utfet : addr - 0x%08x, val - 0x%04x",
  59. (u32) & uccf->uf_regs->utfet, in_be16(&uccf->uf_regs->utfet));
  60. printk(KERN_INFO "utftt : addr - 0x%08x, val - 0x%04x",
  61. (u32) & uccf->uf_regs->utftt, in_be16(&uccf->uf_regs->utftt));
  62. printk(KERN_INFO "utpt : addr - 0x%08x, val - 0x%04x",
  63. (u32) & uccf->uf_regs->utpt, in_be16(&uccf->uf_regs->utpt));
  64. printk(KERN_INFO "urtry : addr - 0x%08x, val - 0x%08x",
  65. (u32) & uccf->uf_regs->urtry, in_be32(&uccf->uf_regs->urtry));
  66. printk(KERN_INFO "guemr : addr - 0x%08x, val - 0x%02x",
  67. (u32) & uccf->uf_regs->guemr, uccf->uf_regs->guemr);
  68. }
  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. void ucc_fast_transmit_on_demand(struct ucc_fast_private * uccf)
  84. {
  85. out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
  86. }
  87. void ucc_fast_enable(struct ucc_fast_private * uccf, enum comm_dir mode)
  88. {
  89. struct ucc_fast *uf_regs;
  90. u32 gumr;
  91. uf_regs = uccf->uf_regs;
  92. /* Enable reception and/or transmission on this UCC. */
  93. gumr = in_be32(&uf_regs->gumr);
  94. if (mode & COMM_DIR_TX) {
  95. gumr |= UCC_FAST_GUMR_ENT;
  96. uccf->enabled_tx = 1;
  97. }
  98. if (mode & COMM_DIR_RX) {
  99. gumr |= UCC_FAST_GUMR_ENR;
  100. uccf->enabled_rx = 1;
  101. }
  102. out_be32(&uf_regs->gumr, gumr);
  103. }
  104. void ucc_fast_disable(struct ucc_fast_private * uccf, enum comm_dir mode)
  105. {
  106. struct ucc_fast *uf_regs;
  107. u32 gumr;
  108. uf_regs = uccf->uf_regs;
  109. /* Disable reception and/or transmission on this UCC. */
  110. gumr = in_be32(&uf_regs->gumr);
  111. if (mode & COMM_DIR_TX) {
  112. gumr &= ~UCC_FAST_GUMR_ENT;
  113. uccf->enabled_tx = 0;
  114. }
  115. if (mode & COMM_DIR_RX) {
  116. gumr &= ~UCC_FAST_GUMR_ENR;
  117. uccf->enabled_rx = 0;
  118. }
  119. out_be32(&uf_regs->gumr, gumr);
  120. }
  121. int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** uccf_ret)
  122. {
  123. struct ucc_fast_private *uccf;
  124. struct ucc_fast *uf_regs;
  125. u32 gumr;
  126. int ret;
  127. if (!uf_info)
  128. return -EINVAL;
  129. /* check if the UCC port number is in range. */
  130. if ((uf_info->ucc_num < 0) || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
  131. printk(KERN_ERR "%s: illegal UCC number", __FUNCTION__);
  132. return -EINVAL;
  133. }
  134. /* Check that 'max_rx_buf_length' is properly aligned (4). */
  135. if (uf_info->max_rx_buf_length & (UCC_FAST_MRBLR_ALIGNMENT - 1)) {
  136. printk(KERN_ERR "%s: max_rx_buf_length not aligned", __FUNCTION__);
  137. return -EINVAL;
  138. }
  139. /* Validate Virtual Fifo register values */
  140. if (uf_info->urfs < UCC_FAST_URFS_MIN_VAL) {
  141. printk(KERN_ERR "%s: urfs is too small", __FUNCTION__);
  142. return -EINVAL;
  143. }
  144. if (uf_info->urfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  145. printk(KERN_ERR "%s: urfs is not aligned", __FUNCTION__);
  146. return -EINVAL;
  147. }
  148. if (uf_info->urfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  149. printk(KERN_ERR "%s: urfet is not aligned.", __FUNCTION__);
  150. return -EINVAL;
  151. }
  152. if (uf_info->urfset & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  153. printk(KERN_ERR "%s: urfset is not aligned", __FUNCTION__);
  154. return -EINVAL;
  155. }
  156. if (uf_info->utfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  157. printk(KERN_ERR "%s: utfs is not aligned", __FUNCTION__);
  158. return -EINVAL;
  159. }
  160. if (uf_info->utfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  161. printk(KERN_ERR "%s: utfet is not aligned", __FUNCTION__);
  162. return -EINVAL;
  163. }
  164. if (uf_info->utftt & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
  165. printk(KERN_ERR "%s: utftt is not aligned", __FUNCTION__);
  166. return -EINVAL;
  167. }
  168. uccf = kzalloc(sizeof(struct ucc_fast_private), GFP_KERNEL);
  169. if (!uccf) {
  170. printk(KERN_ERR "%s: Cannot allocate private data", __FUNCTION__);
  171. return -ENOMEM;
  172. }
  173. /* Fill fast UCC structure */
  174. uccf->uf_info = uf_info;
  175. /* Set the PHY base address */
  176. uccf->uf_regs = ioremap(uf_info->regs, sizeof(struct ucc_fast));
  177. if (uccf->uf_regs == NULL) {
  178. printk(KERN_ERR "%s: Cannot map UCC registers", __FUNCTION__);
  179. return -ENOMEM;
  180. }
  181. uccf->enabled_tx = 0;
  182. uccf->enabled_rx = 0;
  183. uccf->stopped_tx = 0;
  184. uccf->stopped_rx = 0;
  185. uf_regs = uccf->uf_regs;
  186. uccf->p_ucce = (u32 *) & (uf_regs->ucce);
  187. uccf->p_uccm = (u32 *) & (uf_regs->uccm);
  188. #ifdef CONFIG_UGETH_TX_ON_DEMAND
  189. uccf->p_utodr = (u16 *) & (uf_regs->utodr);
  190. #endif
  191. #ifdef STATISTICS
  192. uccf->tx_frames = 0;
  193. uccf->rx_frames = 0;
  194. uccf->rx_discarded = 0;
  195. #endif /* STATISTICS */
  196. /* Init Guemr register */
  197. if ((ret = ucc_init_guemr((struct ucc_common *) (uf_regs)))) {
  198. printk(KERN_ERR "%s: cannot init GUEMR", __FUNCTION__);
  199. ucc_fast_free(uccf);
  200. return ret;
  201. }
  202. /* Set UCC to fast type */
  203. if ((ret = ucc_set_type(uf_info->ucc_num,
  204. (struct ucc_common *) (uf_regs),
  205. UCC_SPEED_TYPE_FAST))) {
  206. printk(KERN_ERR "%s: cannot set UCC type", __FUNCTION__);
  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", __FUNCTION__);
  243. uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
  244. ucc_fast_free(uccf);
  245. return -ENOMEM;
  246. }
  247. /* Allocate memory for Rx Virtual Fifo */
  248. uccf->ucc_fast_rx_virtual_fifo_base_offset =
  249. qe_muram_alloc(uf_info->urfs +
  250. UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
  251. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  252. if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
  253. printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO", __FUNCTION__);
  254. uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
  255. ucc_fast_free(uccf);
  256. return -ENOMEM;
  257. }
  258. /* Set Virtual Fifo registers */
  259. out_be16(&uf_regs->urfs, uf_info->urfs);
  260. out_be16(&uf_regs->urfet, uf_info->urfet);
  261. out_be16(&uf_regs->urfset, uf_info->urfset);
  262. out_be16(&uf_regs->utfs, uf_info->utfs);
  263. out_be16(&uf_regs->utfet, uf_info->utfet);
  264. out_be16(&uf_regs->utftt, uf_info->utftt);
  265. /* utfb, urfb are offsets from MURAM base */
  266. out_be32(&uf_regs->utfb, uccf->ucc_fast_tx_virtual_fifo_base_offset);
  267. out_be32(&uf_regs->urfb, uccf->ucc_fast_rx_virtual_fifo_base_offset);
  268. /* Mux clocking */
  269. /* Grant Support */
  270. ucc_set_qe_mux_grant(uf_info->ucc_num, uf_info->grant_support);
  271. /* Breakpoint Support */
  272. ucc_set_qe_mux_bkpt(uf_info->ucc_num, uf_info->brkpt_support);
  273. /* Set Tsa or NMSI mode. */
  274. ucc_set_qe_mux_tsa(uf_info->ucc_num, uf_info->tsa);
  275. /* If NMSI (not Tsa), set Tx and Rx clock. */
  276. if (!uf_info->tsa) {
  277. /* Rx clock routing */
  278. if ((uf_info->rx_clock != QE_CLK_NONE) &&
  279. ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->rx_clock,
  280. COMM_DIR_RX)) {
  281. printk(KERN_ERR "%s: illegal value for RX clock",
  282. __FUNCTION__);
  283. ucc_fast_free(uccf);
  284. return -EINVAL;
  285. }
  286. /* Tx clock routing */
  287. if ((uf_info->tx_clock != QE_CLK_NONE) &&
  288. ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->tx_clock,
  289. COMM_DIR_TX)) {
  290. printk(KERN_ERR "%s: illegal value for TX clock",
  291. __FUNCTION__);
  292. ucc_fast_free(uccf);
  293. return -EINVAL;
  294. }
  295. }
  296. /* Set interrupt mask register at UCC level. */
  297. out_be32(&uf_regs->uccm, uf_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_be32(&uf_regs->ucce, 0xffffffff);
  303. *uccf_ret = uccf;
  304. return 0;
  305. }
  306. void ucc_fast_free(struct ucc_fast_private * uccf)
  307. {
  308. if (!uccf)
  309. return;
  310. if (uccf->ucc_fast_tx_virtual_fifo_base_offset)
  311. qe_muram_free(uccf->ucc_fast_tx_virtual_fifo_base_offset);
  312. if (uccf->ucc_fast_rx_virtual_fifo_base_offset)
  313. qe_muram_free(uccf->ucc_fast_rx_virtual_fifo_base_offset);
  314. kfree(uccf);
  315. }