ucc_fast.c 11 KB

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