ucc_slow.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 Slow API Set - UCC Slow 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_slow.h>
  27. u32 ucc_slow_get_qe_cr_subblock(int uccs_num)
  28. {
  29. switch (uccs_num) {
  30. case 0: return QE_CR_SUBBLOCK_UCCSLOW1;
  31. case 1: return QE_CR_SUBBLOCK_UCCSLOW2;
  32. case 2: return QE_CR_SUBBLOCK_UCCSLOW3;
  33. case 3: return QE_CR_SUBBLOCK_UCCSLOW4;
  34. case 4: return QE_CR_SUBBLOCK_UCCSLOW5;
  35. case 5: return QE_CR_SUBBLOCK_UCCSLOW6;
  36. case 6: return QE_CR_SUBBLOCK_UCCSLOW7;
  37. case 7: return QE_CR_SUBBLOCK_UCCSLOW8;
  38. default: return QE_CR_SUBBLOCK_INVALID;
  39. }
  40. }
  41. void ucc_slow_poll_transmitter_now(struct ucc_slow_private * uccs)
  42. {
  43. out_be16(&uccs->us_regs->utodr, UCC_SLOW_TOD);
  44. }
  45. void ucc_slow_graceful_stop_tx(struct ucc_slow_private * uccs)
  46. {
  47. struct ucc_slow_info *us_info = uccs->us_info;
  48. u32 id;
  49. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  50. qe_issue_cmd(QE_GRACEFUL_STOP_TX, id,
  51. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  52. }
  53. void ucc_slow_stop_tx(struct ucc_slow_private * uccs)
  54. {
  55. struct ucc_slow_info *us_info = uccs->us_info;
  56. u32 id;
  57. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  58. qe_issue_cmd(QE_STOP_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
  59. }
  60. void ucc_slow_restart_tx(struct ucc_slow_private * uccs)
  61. {
  62. struct ucc_slow_info *us_info = uccs->us_info;
  63. u32 id;
  64. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  65. qe_issue_cmd(QE_RESTART_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
  66. }
  67. void ucc_slow_enable(struct ucc_slow_private * uccs, enum comm_dir mode)
  68. {
  69. struct ucc_slow *us_regs;
  70. u32 gumr_l;
  71. us_regs = uccs->us_regs;
  72. /* Enable reception and/or transmission on this UCC. */
  73. gumr_l = in_be32(&us_regs->gumr_l);
  74. if (mode & COMM_DIR_TX) {
  75. gumr_l |= UCC_SLOW_GUMR_L_ENT;
  76. uccs->enabled_tx = 1;
  77. }
  78. if (mode & COMM_DIR_RX) {
  79. gumr_l |= UCC_SLOW_GUMR_L_ENR;
  80. uccs->enabled_rx = 1;
  81. }
  82. out_be32(&us_regs->gumr_l, gumr_l);
  83. }
  84. void ucc_slow_disable(struct ucc_slow_private * uccs, enum comm_dir mode)
  85. {
  86. struct ucc_slow *us_regs;
  87. u32 gumr_l;
  88. us_regs = uccs->us_regs;
  89. /* Disable reception and/or transmission on this UCC. */
  90. gumr_l = in_be32(&us_regs->gumr_l);
  91. if (mode & COMM_DIR_TX) {
  92. gumr_l &= ~UCC_SLOW_GUMR_L_ENT;
  93. uccs->enabled_tx = 0;
  94. }
  95. if (mode & COMM_DIR_RX) {
  96. gumr_l &= ~UCC_SLOW_GUMR_L_ENR;
  97. uccs->enabled_rx = 0;
  98. }
  99. out_be32(&us_regs->gumr_l, gumr_l);
  100. }
  101. int ucc_slow_init(struct ucc_slow_info * us_info, struct ucc_slow_private ** uccs_ret)
  102. {
  103. struct ucc_slow_private *uccs;
  104. u32 i;
  105. struct ucc_slow *us_regs;
  106. u32 gumr;
  107. struct qe_bd *bd;
  108. u32 id;
  109. u32 command;
  110. int ret = 0;
  111. if (!us_info)
  112. return -EINVAL;
  113. /* check if the UCC port number is in range. */
  114. if ((us_info->ucc_num < 0) || (us_info->ucc_num > UCC_MAX_NUM - 1)) {
  115. printk(KERN_ERR "%s: illegal UCC number", __FUNCTION__);
  116. return -EINVAL;
  117. }
  118. /*
  119. * Set mrblr
  120. * Check that 'max_rx_buf_length' is properly aligned (4), unless
  121. * rfw is 1, meaning that QE accepts one byte at a time, unlike normal
  122. * case when QE accepts 32 bits at a time.
  123. */
  124. if ((!us_info->rfw) &&
  125. (us_info->max_rx_buf_length & (UCC_SLOW_MRBLR_ALIGNMENT - 1))) {
  126. printk(KERN_ERR "max_rx_buf_length not aligned.");
  127. return -EINVAL;
  128. }
  129. uccs = kzalloc(sizeof(struct ucc_slow_private), GFP_KERNEL);
  130. if (!uccs) {
  131. printk(KERN_ERR "%s: Cannot allocate private data", __FUNCTION__);
  132. return -ENOMEM;
  133. }
  134. /* Fill slow UCC structure */
  135. uccs->us_info = us_info;
  136. /* Set the PHY base address */
  137. uccs->us_regs = ioremap(us_info->regs, sizeof(struct ucc_slow));
  138. if (uccs->us_regs == NULL) {
  139. printk(KERN_ERR "%s: Cannot map UCC registers", __FUNCTION__);
  140. return -ENOMEM;
  141. }
  142. uccs->saved_uccm = 0;
  143. uccs->p_rx_frame = 0;
  144. us_regs = uccs->us_regs;
  145. uccs->p_ucce = (u16 *) & (us_regs->ucce);
  146. uccs->p_uccm = (u16 *) & (us_regs->uccm);
  147. #ifdef STATISTICS
  148. uccs->rx_frames = 0;
  149. uccs->tx_frames = 0;
  150. uccs->rx_discarded = 0;
  151. #endif /* STATISTICS */
  152. /* Get PRAM base */
  153. uccs->us_pram_offset =
  154. qe_muram_alloc(UCC_SLOW_PRAM_SIZE, ALIGNMENT_OF_UCC_SLOW_PRAM);
  155. if (IS_ERR_VALUE(uccs->us_pram_offset)) {
  156. printk(KERN_ERR "%s: cannot allocate MURAM for PRAM", __FUNCTION__);
  157. ucc_slow_free(uccs);
  158. return -ENOMEM;
  159. }
  160. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  161. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, id, QE_CR_PROTOCOL_UNSPECIFIED,
  162. uccs->us_pram_offset);
  163. uccs->us_pram = qe_muram_addr(uccs->us_pram_offset);
  164. /* Init Guemr register */
  165. if ((ret = ucc_init_guemr((struct ucc_common *) us_regs))) {
  166. printk(KERN_ERR "%s: cannot init GUEMR", __FUNCTION__);
  167. ucc_slow_free(uccs);
  168. return ret;
  169. }
  170. /* Set UCC to slow type */
  171. if ((ret = ucc_set_type(us_info->ucc_num,
  172. (struct ucc_common *) us_regs,
  173. UCC_SPEED_TYPE_SLOW))) {
  174. printk(KERN_ERR "%s: cannot set UCC type", __FUNCTION__);
  175. ucc_slow_free(uccs);
  176. return ret;
  177. }
  178. out_be16(&uccs->us_pram->mrblr, us_info->max_rx_buf_length);
  179. INIT_LIST_HEAD(&uccs->confQ);
  180. /* Allocate BDs. */
  181. uccs->rx_base_offset =
  182. qe_muram_alloc(us_info->rx_bd_ring_len * sizeof(struct qe_bd),
  183. QE_ALIGNMENT_OF_BD);
  184. if (IS_ERR_VALUE(uccs->rx_base_offset)) {
  185. printk(KERN_ERR "%s: cannot allocate RX BDs", __FUNCTION__);
  186. uccs->rx_base_offset = 0;
  187. ucc_slow_free(uccs);
  188. return -ENOMEM;
  189. }
  190. uccs->tx_base_offset =
  191. qe_muram_alloc(us_info->tx_bd_ring_len * sizeof(struct qe_bd),
  192. QE_ALIGNMENT_OF_BD);
  193. if (IS_ERR_VALUE(uccs->tx_base_offset)) {
  194. printk(KERN_ERR "%s: cannot allocate TX BDs", __FUNCTION__);
  195. uccs->tx_base_offset = 0;
  196. ucc_slow_free(uccs);
  197. return -ENOMEM;
  198. }
  199. /* Init Tx bds */
  200. bd = uccs->confBd = uccs->tx_bd = qe_muram_addr(uccs->tx_base_offset);
  201. for (i = 0; i < us_info->tx_bd_ring_len - 1; i++) {
  202. /* clear bd buffer */
  203. out_be32(&bd->buf, 0);
  204. /* set bd status and length */
  205. out_be32((u32 *) bd, 0);
  206. bd++;
  207. }
  208. /* for last BD set Wrap bit */
  209. out_be32(&bd->buf, 0);
  210. out_be32((u32 *) bd, cpu_to_be32(T_W));
  211. /* Init Rx bds */
  212. bd = uccs->rx_bd = qe_muram_addr(uccs->rx_base_offset);
  213. for (i = 0; i < us_info->rx_bd_ring_len - 1; i++) {
  214. /* set bd status and length */
  215. out_be32((u32*)bd, 0);
  216. /* clear bd buffer */
  217. out_be32(&bd->buf, 0);
  218. bd++;
  219. }
  220. /* for last BD set Wrap bit */
  221. out_be32((u32*)bd, cpu_to_be32(R_W));
  222. out_be32(&bd->buf, 0);
  223. /* Set GUMR (For more details see the hardware spec.). */
  224. /* gumr_h */
  225. gumr = us_info->tcrc;
  226. if (us_info->cdp)
  227. gumr |= UCC_SLOW_GUMR_H_CDP;
  228. if (us_info->ctsp)
  229. gumr |= UCC_SLOW_GUMR_H_CTSP;
  230. if (us_info->cds)
  231. gumr |= UCC_SLOW_GUMR_H_CDS;
  232. if (us_info->ctss)
  233. gumr |= UCC_SLOW_GUMR_H_CTSS;
  234. if (us_info->tfl)
  235. gumr |= UCC_SLOW_GUMR_H_TFL;
  236. if (us_info->rfw)
  237. gumr |= UCC_SLOW_GUMR_H_RFW;
  238. if (us_info->txsy)
  239. gumr |= UCC_SLOW_GUMR_H_TXSY;
  240. if (us_info->rtsm)
  241. gumr |= UCC_SLOW_GUMR_H_RTSM;
  242. out_be32(&us_regs->gumr_h, gumr);
  243. /* gumr_l */
  244. gumr = us_info->tdcr | us_info->rdcr | us_info->tenc | us_info->renc |
  245. us_info->diag | us_info->mode;
  246. if (us_info->tci)
  247. gumr |= UCC_SLOW_GUMR_L_TCI;
  248. if (us_info->rinv)
  249. gumr |= UCC_SLOW_GUMR_L_RINV;
  250. if (us_info->tinv)
  251. gumr |= UCC_SLOW_GUMR_L_TINV;
  252. if (us_info->tend)
  253. gumr |= UCC_SLOW_GUMR_L_TEND;
  254. out_be32(&us_regs->gumr_l, gumr);
  255. /* Function code registers */
  256. /* if the data is in cachable memory, the 'global' */
  257. /* in the function code should be set. */
  258. uccs->us_pram->tfcr = uccs->us_pram->rfcr =
  259. us_info->data_mem_part | QE_BMR_BYTE_ORDER_BO_MOT;
  260. /* rbase, tbase are offsets from MURAM base */
  261. out_be16(&uccs->us_pram->rbase, uccs->us_pram_offset);
  262. out_be16(&uccs->us_pram->tbase, uccs->us_pram_offset);
  263. /* Mux clocking */
  264. /* Grant Support */
  265. ucc_set_qe_mux_grant(us_info->ucc_num, us_info->grant_support);
  266. /* Breakpoint Support */
  267. ucc_set_qe_mux_bkpt(us_info->ucc_num, us_info->brkpt_support);
  268. /* Set Tsa or NMSI mode. */
  269. ucc_set_qe_mux_tsa(us_info->ucc_num, us_info->tsa);
  270. /* If NMSI (not Tsa), set Tx and Rx clock. */
  271. if (!us_info->tsa) {
  272. /* Rx clock routing */
  273. if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->rx_clock,
  274. COMM_DIR_RX)) {
  275. printk(KERN_ERR "%s: illegal value for RX clock",
  276. __FUNCTION__);
  277. ucc_slow_free(uccs);
  278. return -EINVAL;
  279. }
  280. /* Tx clock routing */
  281. if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->tx_clock,
  282. COMM_DIR_TX)) {
  283. printk(KERN_ERR "%s: illegal value for TX clock",
  284. __FUNCTION__);
  285. ucc_slow_free(uccs);
  286. return -EINVAL;
  287. }
  288. }
  289. /* Set interrupt mask register at UCC level. */
  290. out_be16(&us_regs->uccm, us_info->uccm_mask);
  291. /* First, clear anything pending at UCC level,
  292. * otherwise, old garbage may come through
  293. * as soon as the dam is opened. */
  294. /* Writing '1' clears */
  295. out_be16(&us_regs->ucce, 0xffff);
  296. /* Issue QE Init command */
  297. if (us_info->init_tx && us_info->init_rx)
  298. command = QE_INIT_TX_RX;
  299. else if (us_info->init_tx)
  300. command = QE_INIT_TX;
  301. else
  302. command = QE_INIT_RX; /* We know at least one is TRUE */
  303. id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
  304. qe_issue_cmd(command, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
  305. *uccs_ret = uccs;
  306. return 0;
  307. }
  308. void ucc_slow_free(struct ucc_slow_private * uccs)
  309. {
  310. if (!uccs)
  311. return;
  312. if (uccs->rx_base_offset)
  313. qe_muram_free(uccs->rx_base_offset);
  314. if (uccs->tx_base_offset)
  315. qe_muram_free(uccs->tx_base_offset);
  316. if (uccs->us_pram) {
  317. qe_muram_free(uccs->us_pram_offset);
  318. uccs->us_pram = NULL;
  319. }
  320. kfree(uccs);
  321. }