ucc_slow.c 10 KB

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