1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2006 Freescale Semiconductor, Inc.
5 * Dave Liu <daveliu@freescale.com>
6 * based on source code of Shlomi Gridish
11 #include <linux/errno.h>
13 #include <linux/immap_qe.h>
17 #if !defined(CONFIG_DM_ETH)
18 void ucc_fast_transmit_on_demand(struct ucc_fast_priv *uccf)
20 out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
23 u32 ucc_fast_get_qe_cr_subblock(int ucc_num)
27 return QE_CR_SUBBLOCK_UCCFAST1;
29 return QE_CR_SUBBLOCK_UCCFAST2;
31 return QE_CR_SUBBLOCK_UCCFAST3;
33 return QE_CR_SUBBLOCK_UCCFAST4;
35 return QE_CR_SUBBLOCK_UCCFAST5;
37 return QE_CR_SUBBLOCK_UCCFAST6;
39 return QE_CR_SUBBLOCK_UCCFAST7;
41 return QE_CR_SUBBLOCK_UCCFAST8;
43 return QE_CR_SUBBLOCK_INVALID;
47 static void ucc_get_cmxucr_reg(int ucc_num, u32 **p_cmxucr,
48 u8 *reg_num, u8 *shift)
52 *p_cmxucr = &qe_immr->qmx.cmxucr1;
57 *p_cmxucr = &qe_immr->qmx.cmxucr1;
62 *p_cmxucr = &qe_immr->qmx.cmxucr2;
67 *p_cmxucr = &qe_immr->qmx.cmxucr2;
72 *p_cmxucr = &qe_immr->qmx.cmxucr3;
77 *p_cmxucr = &qe_immr->qmx.cmxucr3;
82 *p_cmxucr = &qe_immr->qmx.cmxucr4;
87 *p_cmxucr = &qe_immr->qmx.cmxucr4;
96 static int ucc_set_clk_src(int ucc_num, qe_clock_e clock, comm_dir_e mode)
105 /* check if the UCC number is in range. */
106 if ((ucc_num > UCC_MAX_NUM - 1) || ucc_num < 0)
109 if (!(mode == COMM_DIR_RX || mode == COMM_DIR_TX)) {
110 printf("%s: bad comm mode type passed\n", __func__);
114 ucc_get_cmxucr_reg(ucc_num, &p_cmxucr, ®_num, &shift);
277 printf("%s: Bad combination of clock and UCC\n", __func__);
281 clk_bits = (u32)source;
282 clk_mask = QE_CMXUCR_TX_CLK_SRC_MASK;
283 if (mode == COMM_DIR_RX) {
284 clk_bits <<= 4; /* Rx field is 4 bits to left of Tx field */
285 clk_mask <<= 4; /* Rx field is 4 bits to left of Tx field */
290 out_be32(p_cmxucr, (in_be32(p_cmxucr) & ~clk_mask) | clk_bits);
295 static uint ucc_get_reg_baseaddr(int ucc_num)
299 /* check if the UCC number is in range */
300 if ((ucc_num > UCC_MAX_NUM - 1) || ucc_num < 0) {
301 printf("%s: the UCC num not in ranges\n", __func__);
334 base = (uint)qe_immr + base;
338 void ucc_fast_enable(struct ucc_fast_priv *uccf, comm_dir_e mode)
343 uf_regs = uccf->uf_regs;
345 /* Enable reception and/or transmission on this UCC. */
346 gumr = in_be32(&uf_regs->gumr);
347 if (mode & COMM_DIR_TX) {
348 gumr |= UCC_FAST_GUMR_ENT;
349 uccf->enabled_tx = 1;
351 if (mode & COMM_DIR_RX) {
352 gumr |= UCC_FAST_GUMR_ENR;
353 uccf->enabled_rx = 1;
355 out_be32(&uf_regs->gumr, gumr);
358 void ucc_fast_disable(struct ucc_fast_priv *uccf, comm_dir_e mode)
363 uf_regs = uccf->uf_regs;
365 /* Disable reception and/or transmission on this UCC. */
366 gumr = in_be32(&uf_regs->gumr);
367 if (mode & COMM_DIR_TX) {
368 gumr &= ~UCC_FAST_GUMR_ENT;
369 uccf->enabled_tx = 0;
371 if (mode & COMM_DIR_RX) {
372 gumr &= ~UCC_FAST_GUMR_ENR;
373 uccf->enabled_rx = 0;
375 out_be32(&uf_regs->gumr, gumr);
378 int ucc_fast_init(struct ucc_fast_inf *uf_info,
379 struct ucc_fast_priv **uccf_ret)
381 struct ucc_fast_priv *uccf;
387 if (uf_info->ucc_num < 0 || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
388 printf("%s: Illagal UCC number!\n", __func__);
392 uccf = (struct ucc_fast_priv *)malloc(sizeof(struct ucc_fast_priv));
394 printf("%s: No memory for UCC fast data structure!\n",
398 memset(uccf, 0, sizeof(struct ucc_fast_priv));
400 /* Save fast UCC structure */
401 uccf->uf_info = uf_info;
402 uccf->uf_regs = (ucc_fast_t *)ucc_get_reg_baseaddr(uf_info->ucc_num);
404 if (!uccf->uf_regs) {
405 printf("%s: No memory map for UCC fast controller!\n",
410 uccf->enabled_tx = 0;
411 uccf->enabled_rx = 0;
413 uf_regs = uccf->uf_regs;
414 uccf->p_ucce = (u32 *)&uf_regs->ucce;
415 uccf->p_uccm = (u32 *)&uf_regs->uccm;
417 /* Init GUEMR register, UCC both Rx and Tx is Fast protocol */
418 out_8(&uf_regs->guemr, UCC_GUEMR_SET_RESERVED3 | UCC_GUEMR_MODE_FAST_RX
419 | UCC_GUEMR_MODE_FAST_TX);
421 /* Set GUMR, disable UCC both Rx and Tx, Ethernet protocol */
422 out_be32(&uf_regs->gumr, UCC_FAST_GUMR_ETH);
424 /* Set the Giga ethernet VFIFO stuff */
425 if (uf_info->eth_type == GIGA_ETH) {
426 /* Allocate memory for Tx Virtual Fifo */
427 uccf->ucc_fast_tx_virtual_fifo_base_offset =
428 qe_muram_alloc(UCC_GETH_UTFS_GIGA_INIT,
429 UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
431 /* Allocate memory for Rx Virtual Fifo */
432 uccf->ucc_fast_rx_virtual_fifo_base_offset =
433 qe_muram_alloc(UCC_GETH_URFS_GIGA_INIT +
434 UCC_FAST_RX_VIRTUAL_FIFO_SIZE_PAD,
435 UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
437 /* utfb, urfb are offsets from MURAM base */
438 out_be32(&uf_regs->utfb,
439 uccf->ucc_fast_tx_virtual_fifo_base_offset);
440 out_be32(&uf_regs->urfb,
441 uccf->ucc_fast_rx_virtual_fifo_base_offset);
443 /* Set Virtual Fifo registers */
444 out_be16(&uf_regs->urfs, UCC_GETH_URFS_GIGA_INIT);
445 out_be16(&uf_regs->urfet, UCC_GETH_URFET_GIGA_INIT);
446 out_be16(&uf_regs->urfset, UCC_GETH_URFSET_GIGA_INIT);
447 out_be16(&uf_regs->utfs, UCC_GETH_UTFS_GIGA_INIT);
448 out_be16(&uf_regs->utfet, UCC_GETH_UTFET_GIGA_INIT);
449 out_be16(&uf_regs->utftt, UCC_GETH_UTFTT_GIGA_INIT);
452 /* Set the Fast ethernet VFIFO stuff */
453 if (uf_info->eth_type == FAST_ETH) {
454 /* Allocate memory for Tx Virtual Fifo */
455 uccf->ucc_fast_tx_virtual_fifo_base_offset =
456 qe_muram_alloc(UCC_GETH_UTFS_INIT,
457 UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
459 /* Allocate memory for Rx Virtual Fifo */
460 uccf->ucc_fast_rx_virtual_fifo_base_offset =
461 qe_muram_alloc(UCC_GETH_URFS_INIT +
462 UCC_FAST_RX_VIRTUAL_FIFO_SIZE_PAD,
463 UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
465 /* utfb, urfb are offsets from MURAM base */
466 out_be32(&uf_regs->utfb,
467 uccf->ucc_fast_tx_virtual_fifo_base_offset);
468 out_be32(&uf_regs->urfb,
469 uccf->ucc_fast_rx_virtual_fifo_base_offset);
471 /* Set Virtual Fifo registers */
472 out_be16(&uf_regs->urfs, UCC_GETH_URFS_INIT);
473 out_be16(&uf_regs->urfet, UCC_GETH_URFET_INIT);
474 out_be16(&uf_regs->urfset, UCC_GETH_URFSET_INIT);
475 out_be16(&uf_regs->utfs, UCC_GETH_UTFS_INIT);
476 out_be16(&uf_regs->utfet, UCC_GETH_UTFET_INIT);
477 out_be16(&uf_regs->utftt, UCC_GETH_UTFTT_INIT);
480 /* Rx clock routing */
481 if (uf_info->rx_clock != QE_CLK_NONE) {
482 if (ucc_set_clk_src(uf_info->ucc_num,
483 uf_info->rx_clock, COMM_DIR_RX)) {
484 printf("%s: Illegal value for parameter 'RxClock'.\n",
490 /* Tx clock routing */
491 if (uf_info->tx_clock != QE_CLK_NONE) {
492 if (ucc_set_clk_src(uf_info->ucc_num,
493 uf_info->tx_clock, COMM_DIR_TX)) {
494 printf("%s: Illegal value for parameter 'TxClock'.\n",
500 /* Clear interrupt mask register to disable all of interrupts */
501 out_be32(&uf_regs->uccm, 0x0);
503 /* Writing '1' to clear all of envents */
504 out_be32(&uf_regs->ucce, 0xffffffff);