uccf.c 11 KB

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