uccf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (C) 2006 Freescale Semiconductor, Inc.
  3. *
  4. * Dave Liu <daveliu@freescale.com>
  5. * based on source code of Shlomi Gridish
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include "common.h"
  23. #include "malloc.h"
  24. #include "asm/errno.h"
  25. #include "asm/io.h"
  26. #include "asm/immap_qe.h"
  27. #include "qe.h"
  28. #include "uccf.h"
  29. void ucc_fast_transmit_on_demand(ucc_fast_private_t *uccf)
  30. {
  31. out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
  32. }
  33. u32 ucc_fast_get_qe_cr_subblock(int ucc_num)
  34. {
  35. switch (ucc_num) {
  36. case 0: return QE_CR_SUBBLOCK_UCCFAST1;
  37. case 1: return QE_CR_SUBBLOCK_UCCFAST2;
  38. case 2: return QE_CR_SUBBLOCK_UCCFAST3;
  39. case 3: return QE_CR_SUBBLOCK_UCCFAST4;
  40. case 4: return QE_CR_SUBBLOCK_UCCFAST5;
  41. case 5: return QE_CR_SUBBLOCK_UCCFAST6;
  42. case 6: return QE_CR_SUBBLOCK_UCCFAST7;
  43. case 7: return QE_CR_SUBBLOCK_UCCFAST8;
  44. default: return QE_CR_SUBBLOCK_INVALID;
  45. }
  46. }
  47. static void ucc_get_cmxucr_reg(int ucc_num, volatile u32 **p_cmxucr,
  48. u8 *reg_num, u8 *shift)
  49. {
  50. switch (ucc_num) {
  51. case 0: /* UCC1 */
  52. *p_cmxucr = &(qe_immr->qmx.cmxucr1);
  53. *reg_num = 1;
  54. *shift = 16;
  55. break;
  56. case 2: /* UCC3 */
  57. *p_cmxucr = &(qe_immr->qmx.cmxucr1);
  58. *reg_num = 1;
  59. *shift = 0;
  60. break;
  61. case 4: /* UCC5 */
  62. *p_cmxucr = &(qe_immr->qmx.cmxucr2);
  63. *reg_num = 2;
  64. *shift = 16;
  65. break;
  66. case 6: /* UCC7 */
  67. *p_cmxucr = &(qe_immr->qmx.cmxucr2);
  68. *reg_num = 2;
  69. *shift = 0;
  70. break;
  71. case 1: /* UCC2 */
  72. *p_cmxucr = &(qe_immr->qmx.cmxucr3);
  73. *reg_num = 3;
  74. *shift = 16;
  75. break;
  76. case 3: /* UCC4 */
  77. *p_cmxucr = &(qe_immr->qmx.cmxucr3);
  78. *reg_num = 3;
  79. *shift = 0;
  80. break;
  81. case 5: /* UCC6 */
  82. *p_cmxucr = &(qe_immr->qmx.cmxucr4);
  83. *reg_num = 4;
  84. *shift = 16;
  85. break;
  86. case 7: /* UCC8 */
  87. *p_cmxucr = &(qe_immr->qmx.cmxucr4);
  88. *reg_num = 4;
  89. *shift = 0;
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. static int ucc_set_clk_src(int ucc_num, qe_clock_e clock, comm_dir_e mode)
  96. {
  97. volatile u32 *p_cmxucr = NULL;
  98. u8 reg_num = 0;
  99. u8 shift = 0;
  100. u32 clockBits;
  101. u32 clockMask;
  102. int source = -1;
  103. /* check if the UCC number is in range. */
  104. if ((ucc_num > UCC_MAX_NUM - 1) || (ucc_num < 0))
  105. return -EINVAL;
  106. if (! ((mode == COMM_DIR_RX) || (mode == COMM_DIR_TX))) {
  107. printf("%s: bad comm mode type passed\n", __FUNCTION__);
  108. return -EINVAL;
  109. }
  110. ucc_get_cmxucr_reg(ucc_num, &p_cmxucr, &reg_num, &shift);
  111. switch (reg_num) {
  112. case 1:
  113. switch (clock) {
  114. case QE_BRG1: source = 1; break;
  115. case QE_BRG2: source = 2; break;
  116. case QE_BRG7: source = 3; break;
  117. case QE_BRG8: source = 4; break;
  118. case QE_CLK9: source = 5; break;
  119. case QE_CLK10: source = 6; break;
  120. case QE_CLK11: source = 7; break;
  121. case QE_CLK12: source = 8; break;
  122. case QE_CLK15: source = 9; break;
  123. case QE_CLK16: source = 10; break;
  124. default: source = -1; break;
  125. }
  126. break;
  127. case 2:
  128. switch (clock) {
  129. case QE_BRG5: source = 1; break;
  130. case QE_BRG6: source = 2; break;
  131. case QE_BRG7: source = 3; break;
  132. case QE_BRG8: source = 4; break;
  133. case QE_CLK13: source = 5; break;
  134. case QE_CLK14: source = 6; break;
  135. case QE_CLK19: source = 7; break;
  136. case QE_CLK20: source = 8; break;
  137. case QE_CLK15: source = 9; break;
  138. case QE_CLK16: source = 10; break;
  139. default: source = -1; break;
  140. }
  141. break;
  142. case 3:
  143. switch (clock) {
  144. case QE_BRG9: source = 1; break;
  145. case QE_BRG10: source = 2; break;
  146. case QE_BRG15: source = 3; break;
  147. case QE_BRG16: source = 4; break;
  148. case QE_CLK3: source = 5; break;
  149. case QE_CLK4: source = 6; break;
  150. case QE_CLK17: source = 7; break;
  151. case QE_CLK18: source = 8; break;
  152. case QE_CLK7: source = 9; break;
  153. case QE_CLK8: source = 10; break;
  154. case QE_CLK16: source = 11; break;
  155. default: source = -1; break;
  156. }
  157. break;
  158. case 4:
  159. switch (clock) {
  160. case QE_BRG13: source = 1; break;
  161. case QE_BRG14: source = 2; break;
  162. case QE_BRG15: source = 3; break;
  163. case QE_BRG16: source = 4; break;
  164. case QE_CLK5: source = 5; break;
  165. case QE_CLK6: source = 6; break;
  166. case QE_CLK21: source = 7; break;
  167. case QE_CLK22: source = 8; break;
  168. case QE_CLK7: source = 9; break;
  169. case QE_CLK8: source = 10; break;
  170. case QE_CLK16: source = 11; break;
  171. default: source = -1; break;
  172. }
  173. break;
  174. default:
  175. source = -1;
  176. break;
  177. }
  178. if (source == -1) {
  179. printf("%s: Bad combination of clock and UCC\n", __FUNCTION__);
  180. return -ENOENT;
  181. }
  182. clockBits = (u32) source;
  183. clockMask = QE_CMXUCR_TX_CLK_SRC_MASK;
  184. if (mode == COMM_DIR_RX) {
  185. clockBits <<= 4; /* Rx field is 4 bits to left of Tx field */
  186. clockMask <<= 4; /* Rx field is 4 bits to left of Tx field */
  187. }
  188. clockBits <<= shift;
  189. clockMask <<= shift;
  190. out_be32(p_cmxucr, (in_be32(p_cmxucr) & ~clockMask) | clockBits);
  191. return 0;
  192. }
  193. static uint ucc_get_reg_baseaddr(int ucc_num)
  194. {
  195. uint base = 0;
  196. /* check if the UCC number is in range */
  197. if ((ucc_num > UCC_MAX_NUM - 1) || (ucc_num < 0)) {
  198. printf("%s: the UCC num not in ranges\n", __FUNCTION__);
  199. return 0;
  200. }
  201. switch (ucc_num) {
  202. case 0: base = 0x00002000; break;
  203. case 1: base = 0x00003000; break;
  204. case 2: base = 0x00002200; break;
  205. case 3: base = 0x00003200; break;
  206. case 4: base = 0x00002400; break;
  207. case 5: base = 0x00003400; break;
  208. case 6: base = 0x00002600; break;
  209. case 7: base = 0x00003600; break;
  210. default: break;
  211. }
  212. base = (uint)qe_immr + base;
  213. return base;
  214. }
  215. void ucc_fast_enable(ucc_fast_private_t *uccf, comm_dir_e mode)
  216. {
  217. ucc_fast_t *uf_regs;
  218. u32 gumr;
  219. uf_regs = uccf->uf_regs;
  220. /* Enable reception and/or transmission on this UCC. */
  221. gumr = in_be32(&uf_regs->gumr);
  222. if (mode & COMM_DIR_TX) {
  223. gumr |= UCC_FAST_GUMR_ENT;
  224. uccf->enabled_tx = 1;
  225. }
  226. if (mode & COMM_DIR_RX) {
  227. gumr |= UCC_FAST_GUMR_ENR;
  228. uccf->enabled_rx = 1;
  229. }
  230. out_be32(&uf_regs->gumr, gumr);
  231. }
  232. void ucc_fast_disable(ucc_fast_private_t *uccf, comm_dir_e mode)
  233. {
  234. ucc_fast_t *uf_regs;
  235. u32 gumr;
  236. uf_regs = uccf->uf_regs;
  237. /* Disable reception and/or transmission on this UCC. */
  238. gumr = in_be32(&uf_regs->gumr);
  239. if (mode & COMM_DIR_TX) {
  240. gumr &= ~UCC_FAST_GUMR_ENT;
  241. uccf->enabled_tx = 0;
  242. }
  243. if (mode & COMM_DIR_RX) {
  244. gumr &= ~UCC_FAST_GUMR_ENR;
  245. uccf->enabled_rx = 0;
  246. }
  247. out_be32(&uf_regs->gumr, gumr);
  248. }
  249. int ucc_fast_init(ucc_fast_info_t *uf_info, ucc_fast_private_t **uccf_ret)
  250. {
  251. ucc_fast_private_t *uccf;
  252. ucc_fast_t *uf_regs;
  253. if (!uf_info)
  254. return -EINVAL;
  255. if ((uf_info->ucc_num < 0) || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
  256. printf("%s: Illagal UCC number!\n", __FUNCTION__);
  257. return -EINVAL;
  258. }
  259. uccf = (ucc_fast_private_t *)malloc(sizeof(ucc_fast_private_t));
  260. if (!uccf) {
  261. printf("%s: No memory for UCC fast data structure!\n",
  262. __FUNCTION__);
  263. return -ENOMEM;
  264. }
  265. memset(uccf, 0, sizeof(ucc_fast_private_t));
  266. /* Save fast UCC structure */
  267. uccf->uf_info = uf_info;
  268. uccf->uf_regs = (ucc_fast_t *)ucc_get_reg_baseaddr(uf_info->ucc_num);
  269. if (uccf->uf_regs == NULL) {
  270. printf("%s: No memory map for UCC fast controller!\n",
  271. __FUNCTION__);
  272. return -ENOMEM;
  273. }
  274. uccf->enabled_tx = 0;
  275. uccf->enabled_rx = 0;
  276. uf_regs = uccf->uf_regs;
  277. uccf->p_ucce = (u32 *) &(uf_regs->ucce);
  278. uccf->p_uccm = (u32 *) &(uf_regs->uccm);
  279. /* Init GUEMR register, UCC both Rx and Tx is Fast protocol */
  280. out_8(&uf_regs->guemr, UCC_GUEMR_SET_RESERVED3 | UCC_GUEMR_MODE_FAST_RX
  281. | UCC_GUEMR_MODE_FAST_TX);
  282. /* Set GUMR, disable UCC both Rx and Tx, Ethernet protocol */
  283. out_be32(&uf_regs->gumr, UCC_FAST_GUMR_ETH);
  284. /* Set the Giga ethernet VFIFO stuff */
  285. if (uf_info->eth_type == GIGA_ETH) {
  286. /* Allocate memory for Tx Virtual Fifo */
  287. uccf->ucc_fast_tx_virtual_fifo_base_offset =
  288. qe_muram_alloc(UCC_GETH_UTFS_GIGA_INIT,
  289. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  290. /* Allocate memory for Rx Virtual Fifo */
  291. uccf->ucc_fast_rx_virtual_fifo_base_offset =
  292. qe_muram_alloc(UCC_GETH_URFS_GIGA_INIT +
  293. UCC_FAST_RX_VIRTUAL_FIFO_SIZE_PAD,
  294. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  295. /* utfb, urfb are offsets from MURAM base */
  296. out_be32(&uf_regs->utfb,
  297. uccf->ucc_fast_tx_virtual_fifo_base_offset);
  298. out_be32(&uf_regs->urfb,
  299. uccf->ucc_fast_rx_virtual_fifo_base_offset);
  300. /* Set Virtual Fifo registers */
  301. out_be16(&uf_regs->urfs, UCC_GETH_URFS_GIGA_INIT);
  302. out_be16(&uf_regs->urfet, UCC_GETH_URFET_GIGA_INIT);
  303. out_be16(&uf_regs->urfset, UCC_GETH_URFSET_GIGA_INIT);
  304. out_be16(&uf_regs->utfs, UCC_GETH_UTFS_GIGA_INIT);
  305. out_be16(&uf_regs->utfet, UCC_GETH_UTFET_GIGA_INIT);
  306. out_be16(&uf_regs->utftt, UCC_GETH_UTFTT_GIGA_INIT);
  307. }
  308. /* Set the Fast ethernet VFIFO stuff */
  309. if (uf_info->eth_type == FAST_ETH) {
  310. /* Allocate memory for Tx Virtual Fifo */
  311. uccf->ucc_fast_tx_virtual_fifo_base_offset =
  312. qe_muram_alloc(UCC_GETH_UTFS_INIT,
  313. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  314. /* Allocate memory for Rx Virtual Fifo */
  315. uccf->ucc_fast_rx_virtual_fifo_base_offset =
  316. qe_muram_alloc(UCC_GETH_URFS_INIT +
  317. UCC_FAST_RX_VIRTUAL_FIFO_SIZE_PAD,
  318. UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
  319. /* utfb, urfb are offsets from MURAM base */
  320. out_be32(&uf_regs->utfb,
  321. uccf->ucc_fast_tx_virtual_fifo_base_offset);
  322. out_be32(&uf_regs->urfb,
  323. uccf->ucc_fast_rx_virtual_fifo_base_offset);
  324. /* Set Virtual Fifo registers */
  325. out_be16(&uf_regs->urfs, UCC_GETH_URFS_INIT);
  326. out_be16(&uf_regs->urfet, UCC_GETH_URFET_INIT);
  327. out_be16(&uf_regs->urfset, UCC_GETH_URFSET_INIT);
  328. out_be16(&uf_regs->utfs, UCC_GETH_UTFS_INIT);
  329. out_be16(&uf_regs->utfet, UCC_GETH_UTFET_INIT);
  330. out_be16(&uf_regs->utftt, UCC_GETH_UTFTT_INIT);
  331. }
  332. /* Rx clock routing */
  333. if (uf_info->rx_clock != QE_CLK_NONE) {
  334. if (ucc_set_clk_src(uf_info->ucc_num,
  335. uf_info->rx_clock, COMM_DIR_RX)) {
  336. printf("%s: Illegal value for parameter 'RxClock'.\n",
  337. __FUNCTION__);
  338. return -EINVAL;
  339. }
  340. }
  341. /* Tx clock routing */
  342. if (uf_info->tx_clock != QE_CLK_NONE) {
  343. if (ucc_set_clk_src(uf_info->ucc_num,
  344. uf_info->tx_clock, COMM_DIR_TX)) {
  345. printf("%s: Illegal value for parameter 'TxClock'.\n",
  346. __FUNCTION__);
  347. return -EINVAL;
  348. }
  349. }
  350. /* Clear interrupt mask register to disable all of interrupts */
  351. out_be32(&uf_regs->uccm, 0x0);
  352. /* Writing '1' to clear all of envents */
  353. out_be32(&uf_regs->ucce, 0xffffffff);
  354. *uccf_ret = uccf;
  355. return 0;
  356. }