2 * Copyright (C) 2006 Freescale Semiconductor, Inc.
4 * Dave Liu <daveliu@freescale.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include "asm/errno.h"
27 #include "asm/immap_qe.h"
33 #if defined(CONFIG_QE)
35 #ifdef CONFIG_UEC_ETH1
36 static uec_info_t eth1_uec_info = {
38 .ucc_num = CFG_UEC1_UCC_NUM,
39 .rx_clock = CFG_UEC1_RX_CLK,
40 .tx_clock = CFG_UEC1_TX_CLK,
41 .eth_type = CFG_UEC1_ETH_TYPE,
43 .num_threads_tx = UEC_NUM_OF_THREADS_4,
44 .num_threads_rx = UEC_NUM_OF_THREADS_4,
45 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
46 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
49 .phy_address = CFG_UEC1_PHY_ADDR,
50 .enet_interface = CFG_UEC1_INTERFACE_MODE,
53 #ifdef CONFIG_UEC_ETH2
54 static uec_info_t eth2_uec_info = {
56 .ucc_num = CFG_UEC2_UCC_NUM,
57 .rx_clock = CFG_UEC2_RX_CLK,
58 .tx_clock = CFG_UEC2_TX_CLK,
59 .eth_type = CFG_UEC2_ETH_TYPE,
61 .num_threads_tx = UEC_NUM_OF_THREADS_4,
62 .num_threads_rx = UEC_NUM_OF_THREADS_4,
63 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
64 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
67 .phy_address = CFG_UEC2_PHY_ADDR,
68 .enet_interface = CFG_UEC2_INTERFACE_MODE,
72 #ifdef CONFIG_UEC_ETH3
73 static uec_info_t eth3_uec_info = {
75 .ucc_num = CFG_UEC3_UCC_NUM,
76 .rx_clock = CFG_UEC3_RX_CLK,
77 .tx_clock = CFG_UEC3_TX_CLK,
78 .eth_type = CFG_UEC3_ETH_TYPE,
80 .num_threads_tx = UEC_NUM_OF_THREADS_4,
81 .num_threads_rx = UEC_NUM_OF_THREADS_4,
82 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
83 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
86 .phy_address = CFG_UEC3_PHY_ADDR,
87 .enet_interface = CFG_UEC3_INTERFACE_MODE,
91 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
97 printf("%s: uec not initial\n", __FUNCTION__);
100 uec_regs = uec->uec_regs;
102 maccfg1 = in_be32(&uec_regs->maccfg1);
104 if (mode & COMM_DIR_TX) {
105 maccfg1 |= MACCFG1_ENABLE_TX;
106 out_be32(&uec_regs->maccfg1, maccfg1);
107 uec->mac_tx_enabled = 1;
110 if (mode & COMM_DIR_RX) {
111 maccfg1 |= MACCFG1_ENABLE_RX;
112 out_be32(&uec_regs->maccfg1, maccfg1);
113 uec->mac_rx_enabled = 1;
119 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
125 printf("%s: uec not initial\n", __FUNCTION__);
128 uec_regs = uec->uec_regs;
130 maccfg1 = in_be32(&uec_regs->maccfg1);
132 if (mode & COMM_DIR_TX) {
133 maccfg1 &= ~MACCFG1_ENABLE_TX;
134 out_be32(&uec_regs->maccfg1, maccfg1);
135 uec->mac_tx_enabled = 0;
138 if (mode & COMM_DIR_RX) {
139 maccfg1 &= ~MACCFG1_ENABLE_RX;
140 out_be32(&uec_regs->maccfg1, maccfg1);
141 uec->mac_rx_enabled = 0;
147 static int uec_graceful_stop_tx(uec_private_t *uec)
153 if (!uec || !uec->uccf) {
154 printf("%s: No handle passed.\n", __FUNCTION__);
158 uf_regs = uec->uccf->uf_regs;
160 /* Clear the grace stop event */
161 out_be32(&uf_regs->ucce, UCCE_GRA);
163 /* Issue host command */
165 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
166 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
167 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
169 /* Wait for command to complete */
171 ucce = in_be32(&uf_regs->ucce);
172 } while (! (ucce & UCCE_GRA));
174 uec->grace_stopped_tx = 1;
179 static int uec_graceful_stop_rx(uec_private_t *uec)
185 printf("%s: No handle passed.\n", __FUNCTION__);
189 if (!uec->p_rx_glbl_pram) {
190 printf("%s: No init rx global parameter\n", __FUNCTION__);
194 /* Clear acknowledge bit */
195 ack = uec->p_rx_glbl_pram->rxgstpack;
196 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
197 uec->p_rx_glbl_pram->rxgstpack = ack;
199 /* Keep issuing cmd and checking ack bit until it is asserted */
201 /* Issue host command */
203 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
204 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
205 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
206 ack = uec->p_rx_glbl_pram->rxgstpack;
207 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
209 uec->grace_stopped_rx = 1;
214 static int uec_restart_tx(uec_private_t *uec)
218 if (!uec || !uec->uec_info) {
219 printf("%s: No handle passed.\n", __FUNCTION__);
224 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
225 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
226 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
228 uec->grace_stopped_tx = 0;
233 static int uec_restart_rx(uec_private_t *uec)
237 if (!uec || !uec->uec_info) {
238 printf("%s: No handle passed.\n", __FUNCTION__);
243 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
244 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
245 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
247 uec->grace_stopped_rx = 0;
252 static int uec_open(uec_private_t *uec, comm_dir_e mode)
254 ucc_fast_private_t *uccf;
256 if (!uec || !uec->uccf) {
257 printf("%s: No handle passed.\n", __FUNCTION__);
262 /* check if the UCC number is in range. */
263 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
264 printf("%s: ucc_num out of range.\n", __FUNCTION__);
269 uec_mac_enable(uec, mode);
271 /* Enable UCC fast */
272 ucc_fast_enable(uccf, mode);
274 /* RISC microcode start */
275 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
278 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
285 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
287 ucc_fast_private_t *uccf;
289 if (!uec || !uec->uccf) {
290 printf("%s: No handle passed.\n", __FUNCTION__);
295 /* check if the UCC number is in range. */
296 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
297 printf("%s: ucc_num out of range.\n", __FUNCTION__);
300 /* Stop any transmissions */
301 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
302 uec_graceful_stop_tx(uec);
304 /* Stop any receptions */
305 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
306 uec_graceful_stop_rx(uec);
309 /* Disable the UCC fast */
310 ucc_fast_disable(uec->uccf, mode);
312 /* Disable the MAC */
313 uec_mac_disable(uec, mode);
318 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
324 printf("%s: uec not initial\n", __FUNCTION__);
327 uec_regs = uec->uec_regs;
329 if (duplex == DUPLEX_HALF) {
330 maccfg2 = in_be32(&uec_regs->maccfg2);
331 maccfg2 &= ~MACCFG2_FDX;
332 out_be32(&uec_regs->maccfg2, maccfg2);
335 if (duplex == DUPLEX_FULL) {
336 maccfg2 = in_be32(&uec_regs->maccfg2);
337 maccfg2 |= MACCFG2_FDX;
338 out_be32(&uec_regs->maccfg2, maccfg2);
344 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
346 enet_interface_e enet_if_mode;
347 uec_info_t *uec_info;
353 printf("%s: uec not initial\n", __FUNCTION__);
357 uec_info = uec->uec_info;
358 uec_regs = uec->uec_regs;
359 enet_if_mode = if_mode;
361 maccfg2 = in_be32(&uec_regs->maccfg2);
362 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
364 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
365 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
367 switch (enet_if_mode) {
370 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
373 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
376 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
380 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
381 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
383 case ENET_1000_RGMII:
384 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
388 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
392 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
393 upsmr |= (UPSMR_RPM | UPSMR_R10M);
396 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
400 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
401 upsmr |= (UPSMR_R10M | UPSMR_RMM);
407 out_be32(&uec_regs->maccfg2, maccfg2);
408 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
413 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
415 uint timeout = 0x1000;
418 miimcfg = in_be32(&uec_mii_regs->miimcfg);
419 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
420 out_be32(&uec_mii_regs->miimcfg, miimcfg);
422 /* Wait until the bus is free */
423 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
425 printf("%s: The MII Bus is stuck!", __FUNCTION__);
432 static int init_phy(struct eth_device *dev)
435 uec_mii_t *umii_regs;
436 struct uec_mii_info *mii_info;
437 struct phy_info *curphy;
440 uec = (uec_private_t *)dev->priv;
441 umii_regs = uec->uec_mii_regs;
447 mii_info = malloc(sizeof(*mii_info));
449 printf("%s: Could not allocate mii_info", dev->name);
452 memset(mii_info, 0, sizeof(*mii_info));
454 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
455 mii_info->speed = SPEED_1000;
457 mii_info->speed = SPEED_100;
460 mii_info->duplex = DUPLEX_FULL;
464 mii_info->advertising = (ADVERTISED_10baseT_Half |
465 ADVERTISED_10baseT_Full |
466 ADVERTISED_100baseT_Half |
467 ADVERTISED_100baseT_Full |
468 ADVERTISED_1000baseT_Full);
469 mii_info->autoneg = 1;
470 mii_info->mii_id = uec->uec_info->phy_address;
473 mii_info->mdio_read = &uec_read_phy_reg;
474 mii_info->mdio_write = &uec_write_phy_reg;
476 uec->mii_info = mii_info;
478 if (init_mii_management_configuration(umii_regs)) {
479 printf("%s: The MII Bus is stuck!", dev->name);
484 /* get info for this PHY */
485 curphy = uec_get_phy_info(uec->mii_info);
487 printf("%s: No PHY found", dev->name);
492 mii_info->phyinfo = curphy;
494 /* Run the commands which initialize the PHY */
496 err = curphy->init(uec->mii_info);
510 static void adjust_link(struct eth_device *dev)
512 uec_private_t *uec = (uec_private_t *)dev->priv;
514 struct uec_mii_info *mii_info = uec->mii_info;
516 extern void change_phy_interface_mode(struct eth_device *dev,
517 enet_interface_e mode);
518 uec_regs = uec->uec_regs;
520 if (mii_info->link) {
521 /* Now we make sure that we can be in full duplex mode.
522 * If not, we operate in half-duplex mode. */
523 if (mii_info->duplex != uec->oldduplex) {
524 if (!(mii_info->duplex)) {
525 uec_set_mac_duplex(uec, DUPLEX_HALF);
526 printf("%s: Half Duplex\n", dev->name);
528 uec_set_mac_duplex(uec, DUPLEX_FULL);
529 printf("%s: Full Duplex\n", dev->name);
531 uec->oldduplex = mii_info->duplex;
534 if (mii_info->speed != uec->oldspeed) {
535 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
536 switch (mii_info->speed) {
540 printf ("switching to rgmii 100\n");
541 /* change phy to rgmii 100 */
542 change_phy_interface_mode(dev,
544 /* change the MAC interface mode */
545 uec_set_mac_if_mode(uec,ENET_100_RGMII);
548 printf ("switching to rgmii 10\n");
549 /* change phy to rgmii 10 */
550 change_phy_interface_mode(dev,
552 /* change the MAC interface mode */
553 uec_set_mac_if_mode(uec,ENET_10_RGMII);
556 printf("%s: Ack,Speed(%d)is illegal\n",
557 dev->name, mii_info->speed);
562 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
563 uec->oldspeed = mii_info->speed;
567 printf("%s: Link is up\n", dev->name);
571 } else { /* if (mii_info->link) */
573 printf("%s: Link is down\n", dev->name);
581 static void phy_change(struct eth_device *dev)
583 uec_private_t *uec = (uec_private_t *)dev->priv;
587 uec_regs = uec->uec_regs;
589 /* Delay 5s to give the PHY a chance to change the register state */
592 /* Update the link, speed, duplex */
593 result = uec->mii_info->phyinfo->read_status(uec->mii_info);
595 /* Adjust the interface according to speed */
596 if ((0 == result) || (uec->mii_info->link == 0)) {
601 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
608 printf("%s: uec not initial\n", __FUNCTION__);
612 uec_regs = uec->uec_regs;
614 /* if a station address of 0x12345678ABCD, perform a write to
615 MACSTNADDR1 of 0xCDAB7856,
616 MACSTNADDR2 of 0x34120000 */
618 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
619 (mac_addr[3] << 8) | (mac_addr[2]);
620 out_be32(&uec_regs->macstnaddr1, mac_addr1);
622 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
623 out_be32(&uec_regs->macstnaddr2, mac_addr2);
628 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
629 int *threads_num_ret)
631 int num_threads_numerica;
633 switch (threads_num) {
634 case UEC_NUM_OF_THREADS_1:
635 num_threads_numerica = 1;
637 case UEC_NUM_OF_THREADS_2:
638 num_threads_numerica = 2;
640 case UEC_NUM_OF_THREADS_4:
641 num_threads_numerica = 4;
643 case UEC_NUM_OF_THREADS_6:
644 num_threads_numerica = 6;
646 case UEC_NUM_OF_THREADS_8:
647 num_threads_numerica = 8;
650 printf("%s: Bad number of threads value.",
655 *threads_num_ret = num_threads_numerica;
660 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
662 uec_info_t *uec_info;
667 uec_info = uec->uec_info;
669 /* Alloc global Tx parameter RAM page */
670 uec->tx_glbl_pram_offset = qe_muram_alloc(
671 sizeof(uec_tx_global_pram_t),
672 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
673 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
674 qe_muram_addr(uec->tx_glbl_pram_offset);
676 /* Zero the global Tx prameter RAM */
677 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
679 /* Init global Tx parameter RAM */
681 /* TEMODER, RMON statistics disable, one Tx queue */
682 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
685 uec->send_q_mem_reg_offset = qe_muram_alloc(
686 sizeof(uec_send_queue_qd_t),
687 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
688 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
689 qe_muram_addr(uec->send_q_mem_reg_offset);
690 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
692 /* Setup the table with TxBDs ring */
693 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
695 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
696 (u32)(uec->p_tx_bd_ring));
697 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
700 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
701 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
703 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
704 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
706 /* TSTATE, global snooping, big endian, the CSB bus selected */
707 bmrx = BMR_INIT_VALUE;
708 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
711 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
712 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
716 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
717 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
721 uec->thread_dat_tx_offset = qe_muram_alloc(
722 num_threads_tx * sizeof(uec_thread_data_tx_t) +
723 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
725 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
726 qe_muram_addr(uec->thread_dat_tx_offset);
727 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
730 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
734 uec_82xx_address_filtering_pram_t *p_af_pram;
736 /* Allocate global Rx parameter RAM page */
737 uec->rx_glbl_pram_offset = qe_muram_alloc(
738 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
739 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
740 qe_muram_addr(uec->rx_glbl_pram_offset);
742 /* Zero Global Rx parameter RAM */
743 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
745 /* Init global Rx parameter RAM */
746 /* REMODER, Extended feature mode disable, VLAN disable,
747 LossLess flow control disable, Receive firmware statisic disable,
748 Extended address parsing mode disable, One Rx queues,
749 Dynamic maximum/minimum frame length disable, IP checksum check
750 disable, IP address alignment disable
752 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
755 uec->thread_dat_rx_offset = qe_muram_alloc(
756 num_threads_rx * sizeof(uec_thread_data_rx_t),
757 UEC_THREAD_DATA_ALIGNMENT);
758 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
759 qe_muram_addr(uec->thread_dat_rx_offset);
760 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
763 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
765 /* RxRMON base pointer, we don't need it */
766 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
768 /* IntCoalescingPTR, we don't need it, no interrupt */
769 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
771 /* RSTATE, global snooping, big endian, the CSB bus selected */
772 bmrx = BMR_INIT_VALUE;
773 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
776 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
779 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
780 sizeof(uec_rx_bd_queues_entry_t) + \
781 sizeof(uec_rx_prefetched_bds_t),
782 UEC_RX_BD_QUEUES_ALIGNMENT);
783 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
784 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
787 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
788 sizeof(uec_rx_prefetched_bds_t));
789 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
790 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
791 (u32)uec->p_rx_bd_ring);
794 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
796 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
798 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
800 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
802 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
804 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
806 for (i = 0; i < 8; i++) {
807 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
811 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
813 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
815 /* Clear PQ2 style address filtering hash table */
816 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
817 uec->p_rx_glbl_pram->addressfiltering;
819 p_af_pram->iaddr_h = 0;
820 p_af_pram->iaddr_l = 0;
821 p_af_pram->gaddr_h = 0;
822 p_af_pram->gaddr_l = 0;
825 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
826 int thread_tx, int thread_rx)
828 uec_init_cmd_pram_t *p_init_enet_param;
829 u32 init_enet_param_offset;
830 uec_info_t *uec_info;
833 u32 init_enet_offset;
838 uec_info = uec->uec_info;
840 /* Allocate init enet command parameter */
841 uec->init_enet_param_offset = qe_muram_alloc(
842 sizeof(uec_init_cmd_pram_t), 4);
843 init_enet_param_offset = uec->init_enet_param_offset;
844 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
845 qe_muram_addr(uec->init_enet_param_offset);
847 /* Zero init enet command struct */
848 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
850 /* Init the command struct */
851 p_init_enet_param = uec->p_init_enet_param;
852 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
853 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
854 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
855 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
856 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
857 p_init_enet_param->largestexternallookupkeysize = 0;
859 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
860 << ENET_INIT_PARAM_RGF_SHIFT;
861 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
862 << ENET_INIT_PARAM_TGF_SHIFT;
864 /* Init Rx global parameter pointer */
865 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
866 (u32)uec_info->riscRx;
868 /* Init Rx threads */
869 for (i = 0; i < (thread_rx + 1); i++) {
870 if ((snum = qe_get_snum()) < 0) {
871 printf("%s can not get snum\n", __FUNCTION__);
876 init_enet_offset = 0;
878 init_enet_offset = qe_muram_alloc(
879 sizeof(uec_thread_rx_pram_t),
880 UEC_THREAD_RX_PRAM_ALIGNMENT);
883 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
884 init_enet_offset | (u32)uec_info->riscRx;
885 p_init_enet_param->rxthread[i] = entry_val;
888 /* Init Tx global parameter pointer */
889 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
890 (u32)uec_info->riscTx;
892 /* Init Tx threads */
893 for (i = 0; i < thread_tx; i++) {
894 if ((snum = qe_get_snum()) < 0) {
895 printf("%s can not get snum\n", __FUNCTION__);
899 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
900 UEC_THREAD_TX_PRAM_ALIGNMENT);
902 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
903 init_enet_offset | (u32)uec_info->riscTx;
904 p_init_enet_param->txthread[i] = entry_val;
907 __asm__ __volatile__("sync");
909 /* Issue QE command */
910 command = QE_INIT_TX_RX;
911 cecr_subblock = ucc_fast_get_qe_cr_subblock(
912 uec->uec_info->uf_info.ucc_num);
913 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
914 init_enet_param_offset);
919 static int uec_startup(uec_private_t *uec)
921 uec_info_t *uec_info;
922 ucc_fast_info_t *uf_info;
923 ucc_fast_private_t *uccf;
929 enet_interface_e enet_interface;
936 if (!uec || !uec->uec_info) {
937 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
941 uec_info = uec->uec_info;
942 uf_info = &(uec_info->uf_info);
944 /* Check if Rx BD ring len is illegal */
945 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
946 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
947 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
952 /* Check if Tx BD ring len is illegal */
953 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
954 printf("%s: Tx BD ring length must not be smaller than 2.\n",
959 /* Check if MRBLR is illegal */
960 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
961 printf("%s: max rx buffer length must be mutliple of 128.\n",
966 /* Both Rx and Tx are stopped */
967 uec->grace_stopped_rx = 1;
968 uec->grace_stopped_tx = 1;
971 if (ucc_fast_init(uf_info, &uccf)) {
972 printf("%s: failed to init ucc fast\n", __FUNCTION__);
979 /* Convert the Tx threads number */
980 if (uec_convert_threads_num(uec_info->num_threads_tx,
985 /* Convert the Rx threads number */
986 if (uec_convert_threads_num(uec_info->num_threads_rx,
991 uf_regs = uccf->uf_regs;
993 /* UEC register is following UCC fast registers */
994 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
996 /* Save the UEC register pointer to UEC private struct */
997 uec->uec_regs = uec_regs;
999 /* Init UPSMR, enable hardware statistics (UCC) */
1000 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1002 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1003 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1005 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1006 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1008 /* Setup MAC interface mode */
1009 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1011 /* Setup MII management base */
1012 #ifndef CONFIG_eTSEC_MDIO_BUS
1013 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1015 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1018 /* Setup MII master clock source */
1019 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1022 utbipar = in_be32(&uec_regs->utbipar);
1023 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1024 enet_interface = uec->uec_info->enet_interface;
1025 if (enet_interface == ENET_1000_TBI ||
1026 enet_interface == ENET_1000_RTBI) {
1027 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1028 << UTBIPAR_PHY_ADDRESS_SHIFT;
1030 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1031 << UTBIPAR_PHY_ADDRESS_SHIFT;
1034 out_be32(&uec_regs->utbipar, utbipar);
1036 /* Allocate Tx BDs */
1037 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1038 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1039 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1040 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1041 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1042 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1045 align = UEC_TX_BD_RING_ALIGNMENT;
1046 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1047 if (uec->tx_bd_ring_offset != 0) {
1048 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1052 /* Zero all of Tx BDs */
1053 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1055 /* Allocate Rx BDs */
1056 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1057 align = UEC_RX_BD_RING_ALIGNMENT;
1058 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1059 if (uec->rx_bd_ring_offset != 0) {
1060 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1064 /* Zero all of Rx BDs */
1065 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1067 /* Allocate Rx buffer */
1068 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1069 align = UEC_RX_DATA_BUF_ALIGNMENT;
1070 uec->rx_buf_offset = (u32)malloc(length + align);
1071 if (uec->rx_buf_offset != 0) {
1072 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1076 /* Zero all of the Rx buffer */
1077 memset((void *)(uec->rx_buf_offset), 0, length + align);
1079 /* Init TxBD ring */
1080 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1083 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1085 BD_STATUS_SET(bd, 0);
1086 BD_LENGTH_SET(bd, 0);
1089 BD_STATUS_SET((--bd), TxBD_WRAP);
1091 /* Init RxBD ring */
1092 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1094 buf = uec->p_rx_buf;
1095 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1096 BD_DATA_SET(bd, buf);
1097 BD_LENGTH_SET(bd, 0);
1098 BD_STATUS_SET(bd, RxBD_EMPTY);
1099 buf += MAX_RXBUF_LEN;
1102 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1104 /* Init global Tx parameter RAM */
1105 uec_init_tx_parameter(uec, num_threads_tx);
1107 /* Init global Rx parameter RAM */
1108 uec_init_rx_parameter(uec, num_threads_rx);
1110 /* Init ethernet Tx and Rx parameter command */
1111 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1113 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1120 static int uec_init(struct eth_device* dev, bd_t *bd)
1125 uec = (uec_private_t *)dev->priv;
1127 if (uec->the_first_run == 0) {
1128 /* Set up the MAC address */
1129 if (dev->enetaddr[0] & 0x01) {
1130 printf("%s: MacAddress is multcast address\n",
1134 uec_set_mac_address(uec, dev->enetaddr);
1135 uec->the_first_run = 1;
1138 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1140 printf("%s: cannot enable UEC device\n", dev->name);
1144 return uec->mii_info->link;
1147 static void uec_halt(struct eth_device* dev)
1149 uec_private_t *uec = (uec_private_t *)dev->priv;
1150 uec_stop(uec, COMM_DIR_RX_AND_TX);
1153 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1156 ucc_fast_private_t *uccf;
1157 volatile qe_bd_t *bd;
1162 uec = (uec_private_t *)dev->priv;
1166 /* Find an empty TxBD */
1167 for (i = 0; bd->status & TxBD_READY; i++) {
1169 printf("%s: tx buffer not ready\n", dev->name);
1175 BD_DATA_SET(bd, buf);
1176 BD_LENGTH_SET(bd, len);
1177 status = bd->status;
1179 status |= (TxBD_READY | TxBD_LAST);
1180 BD_STATUS_SET(bd, status);
1182 /* Tell UCC to transmit the buffer */
1183 ucc_fast_transmit_on_demand(uccf);
1185 /* Wait for buffer to be transmitted */
1186 for (i = 0; bd->status & TxBD_READY; i++) {
1188 printf("%s: tx error\n", dev->name);
1193 /* Ok, the buffer be transimitted */
1194 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1201 static int uec_recv(struct eth_device* dev)
1203 uec_private_t *uec = dev->priv;
1204 volatile qe_bd_t *bd;
1210 status = bd->status;
1212 while (!(status & RxBD_EMPTY)) {
1213 if (!(status & RxBD_ERROR)) {
1215 len = BD_LENGTH(bd);
1216 NetReceive(data, len);
1218 printf("%s: Rx error\n", dev->name);
1221 BD_LENGTH_SET(bd, 0);
1222 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1223 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1224 status = bd->status;
1231 int uec_initialize(int index)
1233 struct eth_device *dev;
1236 uec_info_t *uec_info;
1239 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1242 memset(dev, 0, sizeof(struct eth_device));
1244 /* Allocate the UEC private struct */
1245 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1249 memset(uec, 0, sizeof(uec_private_t));
1251 /* Init UEC private struct, they come from board.h */
1253 #ifdef CONFIG_UEC_ETH1
1254 uec_info = ð1_uec_info;
1256 } else if (index == 1) {
1257 #ifdef CONFIG_UEC_ETH2
1258 uec_info = ð2_uec_info;
1260 } else if (index == 2) {
1261 #ifdef CONFIG_UEC_ETH3
1262 uec_info = ð3_uec_info;
1265 printf("%s: index is illegal.\n", __FUNCTION__);
1269 uec->uec_info = uec_info;
1271 sprintf(dev->name, "FSL UEC%d", index);
1273 dev->priv = (void *)uec;
1274 dev->init = uec_init;
1275 dev->halt = uec_halt;
1276 dev->send = uec_send;
1277 dev->recv = uec_recv;
1279 /* Clear the ethnet address */
1280 for (i = 0; i < 6; i++)
1281 dev->enetaddr[i] = 0;
1285 err = uec_startup(uec);
1287 printf("%s: Cannot configure net device, aborting.",dev->name);
1291 err = init_phy(dev);
1293 printf("%s: Cannot initialize PHY, aborting.\n", dev->name);
1301 #endif /* CONFIG_QE */