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 #if (CFG_UEC1_ETH_TYPE == FAST_ETH)
44 .num_threads_tx = UEC_NUM_OF_THREADS_1,
45 .num_threads_rx = UEC_NUM_OF_THREADS_1,
47 .num_threads_tx = UEC_NUM_OF_THREADS_4,
48 .num_threads_rx = UEC_NUM_OF_THREADS_4,
50 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
51 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
54 .phy_address = CFG_UEC1_PHY_ADDR,
55 .enet_interface = CFG_UEC1_INTERFACE_MODE,
58 #ifdef CONFIG_UEC_ETH2
59 static uec_info_t eth2_uec_info = {
61 .ucc_num = CFG_UEC2_UCC_NUM,
62 .rx_clock = CFG_UEC2_RX_CLK,
63 .tx_clock = CFG_UEC2_TX_CLK,
64 .eth_type = CFG_UEC2_ETH_TYPE,
66 #if (CFG_UEC2_ETH_TYPE == FAST_ETH)
67 .num_threads_tx = UEC_NUM_OF_THREADS_1,
68 .num_threads_rx = UEC_NUM_OF_THREADS_1,
70 .num_threads_tx = UEC_NUM_OF_THREADS_4,
71 .num_threads_rx = UEC_NUM_OF_THREADS_4,
73 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
74 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
77 .phy_address = CFG_UEC2_PHY_ADDR,
78 .enet_interface = CFG_UEC2_INTERFACE_MODE,
81 #ifdef CONFIG_UEC_ETH3
82 static uec_info_t eth3_uec_info = {
84 .ucc_num = CFG_UEC3_UCC_NUM,
85 .rx_clock = CFG_UEC3_RX_CLK,
86 .tx_clock = CFG_UEC3_TX_CLK,
87 .eth_type = CFG_UEC3_ETH_TYPE,
89 #if (CFG_UEC3_ETH_TYPE == FAST_ETH)
90 .num_threads_tx = UEC_NUM_OF_THREADS_1,
91 .num_threads_rx = UEC_NUM_OF_THREADS_1,
93 .num_threads_tx = UEC_NUM_OF_THREADS_4,
94 .num_threads_rx = UEC_NUM_OF_THREADS_4,
96 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
97 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
100 .phy_address = CFG_UEC3_PHY_ADDR,
101 .enet_interface = CFG_UEC3_INTERFACE_MODE,
104 #ifdef CONFIG_UEC_ETH4
105 static uec_info_t eth4_uec_info = {
107 .ucc_num = CFG_UEC4_UCC_NUM,
108 .rx_clock = CFG_UEC4_RX_CLK,
109 .tx_clock = CFG_UEC4_TX_CLK,
110 .eth_type = CFG_UEC4_ETH_TYPE,
112 #if (CFG_UEC4_ETH_TYPE == FAST_ETH)
113 .num_threads_tx = UEC_NUM_OF_THREADS_1,
114 .num_threads_rx = UEC_NUM_OF_THREADS_1,
116 .num_threads_tx = UEC_NUM_OF_THREADS_4,
117 .num_threads_rx = UEC_NUM_OF_THREADS_4,
119 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
120 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
121 .tx_bd_ring_len = 16,
122 .rx_bd_ring_len = 16,
123 .phy_address = CFG_UEC4_PHY_ADDR,
124 .enet_interface = CFG_UEC4_INTERFACE_MODE,
128 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
134 printf("%s: uec not initial\n", __FUNCTION__);
137 uec_regs = uec->uec_regs;
139 maccfg1 = in_be32(&uec_regs->maccfg1);
141 if (mode & COMM_DIR_TX) {
142 maccfg1 |= MACCFG1_ENABLE_TX;
143 out_be32(&uec_regs->maccfg1, maccfg1);
144 uec->mac_tx_enabled = 1;
147 if (mode & COMM_DIR_RX) {
148 maccfg1 |= MACCFG1_ENABLE_RX;
149 out_be32(&uec_regs->maccfg1, maccfg1);
150 uec->mac_rx_enabled = 1;
156 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
162 printf("%s: uec not initial\n", __FUNCTION__);
165 uec_regs = uec->uec_regs;
167 maccfg1 = in_be32(&uec_regs->maccfg1);
169 if (mode & COMM_DIR_TX) {
170 maccfg1 &= ~MACCFG1_ENABLE_TX;
171 out_be32(&uec_regs->maccfg1, maccfg1);
172 uec->mac_tx_enabled = 0;
175 if (mode & COMM_DIR_RX) {
176 maccfg1 &= ~MACCFG1_ENABLE_RX;
177 out_be32(&uec_regs->maccfg1, maccfg1);
178 uec->mac_rx_enabled = 0;
184 static int uec_graceful_stop_tx(uec_private_t *uec)
190 if (!uec || !uec->uccf) {
191 printf("%s: No handle passed.\n", __FUNCTION__);
195 uf_regs = uec->uccf->uf_regs;
197 /* Clear the grace stop event */
198 out_be32(&uf_regs->ucce, UCCE_GRA);
200 /* Issue host command */
202 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
203 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
204 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
206 /* Wait for command to complete */
208 ucce = in_be32(&uf_regs->ucce);
209 } while (! (ucce & UCCE_GRA));
211 uec->grace_stopped_tx = 1;
216 static int uec_graceful_stop_rx(uec_private_t *uec)
222 printf("%s: No handle passed.\n", __FUNCTION__);
226 if (!uec->p_rx_glbl_pram) {
227 printf("%s: No init rx global parameter\n", __FUNCTION__);
231 /* Clear acknowledge bit */
232 ack = uec->p_rx_glbl_pram->rxgstpack;
233 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
234 uec->p_rx_glbl_pram->rxgstpack = ack;
236 /* Keep issuing cmd and checking ack bit until it is asserted */
238 /* Issue host command */
240 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
241 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
242 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
243 ack = uec->p_rx_glbl_pram->rxgstpack;
244 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
246 uec->grace_stopped_rx = 1;
251 static int uec_restart_tx(uec_private_t *uec)
255 if (!uec || !uec->uec_info) {
256 printf("%s: No handle passed.\n", __FUNCTION__);
261 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
262 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
263 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
265 uec->grace_stopped_tx = 0;
270 static int uec_restart_rx(uec_private_t *uec)
274 if (!uec || !uec->uec_info) {
275 printf("%s: No handle passed.\n", __FUNCTION__);
280 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
281 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
282 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
284 uec->grace_stopped_rx = 0;
289 static int uec_open(uec_private_t *uec, comm_dir_e mode)
291 ucc_fast_private_t *uccf;
293 if (!uec || !uec->uccf) {
294 printf("%s: No handle passed.\n", __FUNCTION__);
299 /* check if the UCC number is in range. */
300 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
301 printf("%s: ucc_num out of range.\n", __FUNCTION__);
306 uec_mac_enable(uec, mode);
308 /* Enable UCC fast */
309 ucc_fast_enable(uccf, mode);
311 /* RISC microcode start */
312 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
315 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
322 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
324 ucc_fast_private_t *uccf;
326 if (!uec || !uec->uccf) {
327 printf("%s: No handle passed.\n", __FUNCTION__);
332 /* check if the UCC number is in range. */
333 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
334 printf("%s: ucc_num out of range.\n", __FUNCTION__);
337 /* Stop any transmissions */
338 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
339 uec_graceful_stop_tx(uec);
341 /* Stop any receptions */
342 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
343 uec_graceful_stop_rx(uec);
346 /* Disable the UCC fast */
347 ucc_fast_disable(uec->uccf, mode);
349 /* Disable the MAC */
350 uec_mac_disable(uec, mode);
355 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
361 printf("%s: uec not initial\n", __FUNCTION__);
364 uec_regs = uec->uec_regs;
366 if (duplex == DUPLEX_HALF) {
367 maccfg2 = in_be32(&uec_regs->maccfg2);
368 maccfg2 &= ~MACCFG2_FDX;
369 out_be32(&uec_regs->maccfg2, maccfg2);
372 if (duplex == DUPLEX_FULL) {
373 maccfg2 = in_be32(&uec_regs->maccfg2);
374 maccfg2 |= MACCFG2_FDX;
375 out_be32(&uec_regs->maccfg2, maccfg2);
381 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
383 enet_interface_e enet_if_mode;
384 uec_info_t *uec_info;
390 printf("%s: uec not initial\n", __FUNCTION__);
394 uec_info = uec->uec_info;
395 uec_regs = uec->uec_regs;
396 enet_if_mode = if_mode;
398 maccfg2 = in_be32(&uec_regs->maccfg2);
399 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
401 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
402 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
404 switch (enet_if_mode) {
407 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
410 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
413 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
417 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
418 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
420 case ENET_1000_RGMII:
421 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
425 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
429 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
430 upsmr |= (UPSMR_RPM | UPSMR_R10M);
433 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
437 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
438 upsmr |= (UPSMR_R10M | UPSMR_RMM);
444 out_be32(&uec_regs->maccfg2, maccfg2);
445 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
450 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
452 uint timeout = 0x1000;
455 miimcfg = in_be32(&uec_mii_regs->miimcfg);
456 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
457 out_be32(&uec_mii_regs->miimcfg, miimcfg);
459 /* Wait until the bus is free */
460 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
462 printf("%s: The MII Bus is stuck!", __FUNCTION__);
469 static int init_phy(struct eth_device *dev)
472 uec_mii_t *umii_regs;
473 struct uec_mii_info *mii_info;
474 struct phy_info *curphy;
477 uec = (uec_private_t *)dev->priv;
478 umii_regs = uec->uec_mii_regs;
484 mii_info = malloc(sizeof(*mii_info));
486 printf("%s: Could not allocate mii_info", dev->name);
489 memset(mii_info, 0, sizeof(*mii_info));
491 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
492 mii_info->speed = SPEED_1000;
494 mii_info->speed = SPEED_100;
497 mii_info->duplex = DUPLEX_FULL;
501 mii_info->advertising = (ADVERTISED_10baseT_Half |
502 ADVERTISED_10baseT_Full |
503 ADVERTISED_100baseT_Half |
504 ADVERTISED_100baseT_Full |
505 ADVERTISED_1000baseT_Full);
506 mii_info->autoneg = 1;
507 mii_info->mii_id = uec->uec_info->phy_address;
510 mii_info->mdio_read = &uec_read_phy_reg;
511 mii_info->mdio_write = &uec_write_phy_reg;
513 uec->mii_info = mii_info;
515 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
517 if (init_mii_management_configuration(umii_regs)) {
518 printf("%s: The MII Bus is stuck!", dev->name);
523 /* get info for this PHY */
524 curphy = uec_get_phy_info(uec->mii_info);
526 printf("%s: No PHY found", dev->name);
531 mii_info->phyinfo = curphy;
533 /* Run the commands which initialize the PHY */
535 err = curphy->init(uec->mii_info);
549 static void adjust_link(struct eth_device *dev)
551 uec_private_t *uec = (uec_private_t *)dev->priv;
553 struct uec_mii_info *mii_info = uec->mii_info;
555 extern void change_phy_interface_mode(struct eth_device *dev,
556 enet_interface_e mode);
557 uec_regs = uec->uec_regs;
559 if (mii_info->link) {
560 /* Now we make sure that we can be in full duplex mode.
561 * If not, we operate in half-duplex mode. */
562 if (mii_info->duplex != uec->oldduplex) {
563 if (!(mii_info->duplex)) {
564 uec_set_mac_duplex(uec, DUPLEX_HALF);
565 printf("%s: Half Duplex\n", dev->name);
567 uec_set_mac_duplex(uec, DUPLEX_FULL);
568 printf("%s: Full Duplex\n", dev->name);
570 uec->oldduplex = mii_info->duplex;
573 if (mii_info->speed != uec->oldspeed) {
574 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
575 switch (mii_info->speed) {
579 printf ("switching to rgmii 100\n");
580 /* change phy to rgmii 100 */
581 change_phy_interface_mode(dev,
583 /* change the MAC interface mode */
584 uec_set_mac_if_mode(uec,ENET_100_RGMII);
587 printf ("switching to rgmii 10\n");
588 /* change phy to rgmii 10 */
589 change_phy_interface_mode(dev,
591 /* change the MAC interface mode */
592 uec_set_mac_if_mode(uec,ENET_10_RGMII);
595 printf("%s: Ack,Speed(%d)is illegal\n",
596 dev->name, mii_info->speed);
601 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
602 uec->oldspeed = mii_info->speed;
606 printf("%s: Link is up\n", dev->name);
610 } else { /* if (mii_info->link) */
612 printf("%s: Link is down\n", dev->name);
620 static void phy_change(struct eth_device *dev)
622 uec_private_t *uec = (uec_private_t *)dev->priv;
624 /* Update the link, speed, duplex */
625 uec->mii_info->phyinfo->read_status(uec->mii_info);
627 /* Adjust the interface according to speed */
631 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
638 printf("%s: uec not initial\n", __FUNCTION__);
642 uec_regs = uec->uec_regs;
644 /* if a station address of 0x12345678ABCD, perform a write to
645 MACSTNADDR1 of 0xCDAB7856,
646 MACSTNADDR2 of 0x34120000 */
648 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
649 (mac_addr[3] << 8) | (mac_addr[2]);
650 out_be32(&uec_regs->macstnaddr1, mac_addr1);
652 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
653 out_be32(&uec_regs->macstnaddr2, mac_addr2);
658 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
659 int *threads_num_ret)
661 int num_threads_numerica;
663 switch (threads_num) {
664 case UEC_NUM_OF_THREADS_1:
665 num_threads_numerica = 1;
667 case UEC_NUM_OF_THREADS_2:
668 num_threads_numerica = 2;
670 case UEC_NUM_OF_THREADS_4:
671 num_threads_numerica = 4;
673 case UEC_NUM_OF_THREADS_6:
674 num_threads_numerica = 6;
676 case UEC_NUM_OF_THREADS_8:
677 num_threads_numerica = 8;
680 printf("%s: Bad number of threads value.",
685 *threads_num_ret = num_threads_numerica;
690 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
692 uec_info_t *uec_info;
697 uec_info = uec->uec_info;
699 /* Alloc global Tx parameter RAM page */
700 uec->tx_glbl_pram_offset = qe_muram_alloc(
701 sizeof(uec_tx_global_pram_t),
702 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
703 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
704 qe_muram_addr(uec->tx_glbl_pram_offset);
706 /* Zero the global Tx prameter RAM */
707 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
709 /* Init global Tx parameter RAM */
711 /* TEMODER, RMON statistics disable, one Tx queue */
712 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
715 uec->send_q_mem_reg_offset = qe_muram_alloc(
716 sizeof(uec_send_queue_qd_t),
717 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
718 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
719 qe_muram_addr(uec->send_q_mem_reg_offset);
720 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
722 /* Setup the table with TxBDs ring */
723 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
725 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
726 (u32)(uec->p_tx_bd_ring));
727 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
730 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
731 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
733 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
734 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
736 /* TSTATE, global snooping, big endian, the CSB bus selected */
737 bmrx = BMR_INIT_VALUE;
738 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
741 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
742 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
746 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
747 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
751 uec->thread_dat_tx_offset = qe_muram_alloc(
752 num_threads_tx * sizeof(uec_thread_data_tx_t) +
753 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
755 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
756 qe_muram_addr(uec->thread_dat_tx_offset);
757 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
760 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
764 uec_82xx_address_filtering_pram_t *p_af_pram;
766 /* Allocate global Rx parameter RAM page */
767 uec->rx_glbl_pram_offset = qe_muram_alloc(
768 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
769 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
770 qe_muram_addr(uec->rx_glbl_pram_offset);
772 /* Zero Global Rx parameter RAM */
773 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
775 /* Init global Rx parameter RAM */
776 /* REMODER, Extended feature mode disable, VLAN disable,
777 LossLess flow control disable, Receive firmware statisic disable,
778 Extended address parsing mode disable, One Rx queues,
779 Dynamic maximum/minimum frame length disable, IP checksum check
780 disable, IP address alignment disable
782 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
785 uec->thread_dat_rx_offset = qe_muram_alloc(
786 num_threads_rx * sizeof(uec_thread_data_rx_t),
787 UEC_THREAD_DATA_ALIGNMENT);
788 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
789 qe_muram_addr(uec->thread_dat_rx_offset);
790 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
793 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
795 /* RxRMON base pointer, we don't need it */
796 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
798 /* IntCoalescingPTR, we don't need it, no interrupt */
799 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
801 /* RSTATE, global snooping, big endian, the CSB bus selected */
802 bmrx = BMR_INIT_VALUE;
803 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
806 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
809 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
810 sizeof(uec_rx_bd_queues_entry_t) + \
811 sizeof(uec_rx_prefetched_bds_t),
812 UEC_RX_BD_QUEUES_ALIGNMENT);
813 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
814 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
817 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
818 sizeof(uec_rx_prefetched_bds_t));
819 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
820 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
821 (u32)uec->p_rx_bd_ring);
824 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
826 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
828 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
830 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
832 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
834 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
836 for (i = 0; i < 8; i++) {
837 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
841 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
843 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
845 /* Clear PQ2 style address filtering hash table */
846 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
847 uec->p_rx_glbl_pram->addressfiltering;
849 p_af_pram->iaddr_h = 0;
850 p_af_pram->iaddr_l = 0;
851 p_af_pram->gaddr_h = 0;
852 p_af_pram->gaddr_l = 0;
855 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
856 int thread_tx, int thread_rx)
858 uec_init_cmd_pram_t *p_init_enet_param;
859 u32 init_enet_param_offset;
860 uec_info_t *uec_info;
863 u32 init_enet_offset;
868 uec_info = uec->uec_info;
870 /* Allocate init enet command parameter */
871 uec->init_enet_param_offset = qe_muram_alloc(
872 sizeof(uec_init_cmd_pram_t), 4);
873 init_enet_param_offset = uec->init_enet_param_offset;
874 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
875 qe_muram_addr(uec->init_enet_param_offset);
877 /* Zero init enet command struct */
878 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
880 /* Init the command struct */
881 p_init_enet_param = uec->p_init_enet_param;
882 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
883 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
884 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
885 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
886 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
887 p_init_enet_param->largestexternallookupkeysize = 0;
889 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
890 << ENET_INIT_PARAM_RGF_SHIFT;
891 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
892 << ENET_INIT_PARAM_TGF_SHIFT;
894 /* Init Rx global parameter pointer */
895 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
896 (u32)uec_info->riscRx;
898 /* Init Rx threads */
899 for (i = 0; i < (thread_rx + 1); i++) {
900 if ((snum = qe_get_snum()) < 0) {
901 printf("%s can not get snum\n", __FUNCTION__);
906 init_enet_offset = 0;
908 init_enet_offset = qe_muram_alloc(
909 sizeof(uec_thread_rx_pram_t),
910 UEC_THREAD_RX_PRAM_ALIGNMENT);
913 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
914 init_enet_offset | (u32)uec_info->riscRx;
915 p_init_enet_param->rxthread[i] = entry_val;
918 /* Init Tx global parameter pointer */
919 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
920 (u32)uec_info->riscTx;
922 /* Init Tx threads */
923 for (i = 0; i < thread_tx; i++) {
924 if ((snum = qe_get_snum()) < 0) {
925 printf("%s can not get snum\n", __FUNCTION__);
929 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
930 UEC_THREAD_TX_PRAM_ALIGNMENT);
932 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
933 init_enet_offset | (u32)uec_info->riscTx;
934 p_init_enet_param->txthread[i] = entry_val;
937 __asm__ __volatile__("sync");
939 /* Issue QE command */
940 command = QE_INIT_TX_RX;
941 cecr_subblock = ucc_fast_get_qe_cr_subblock(
942 uec->uec_info->uf_info.ucc_num);
943 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
944 init_enet_param_offset);
949 static int uec_startup(uec_private_t *uec)
951 uec_info_t *uec_info;
952 ucc_fast_info_t *uf_info;
953 ucc_fast_private_t *uccf;
959 enet_interface_e enet_interface;
966 if (!uec || !uec->uec_info) {
967 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
971 uec_info = uec->uec_info;
972 uf_info = &(uec_info->uf_info);
974 /* Check if Rx BD ring len is illegal */
975 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
976 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
977 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
982 /* Check if Tx BD ring len is illegal */
983 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
984 printf("%s: Tx BD ring length must not be smaller than 2.\n",
989 /* Check if MRBLR is illegal */
990 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
991 printf("%s: max rx buffer length must be mutliple of 128.\n",
996 /* Both Rx and Tx are stopped */
997 uec->grace_stopped_rx = 1;
998 uec->grace_stopped_tx = 1;
1001 if (ucc_fast_init(uf_info, &uccf)) {
1002 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1009 /* Convert the Tx threads number */
1010 if (uec_convert_threads_num(uec_info->num_threads_tx,
1015 /* Convert the Rx threads number */
1016 if (uec_convert_threads_num(uec_info->num_threads_rx,
1021 uf_regs = uccf->uf_regs;
1023 /* UEC register is following UCC fast registers */
1024 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1026 /* Save the UEC register pointer to UEC private struct */
1027 uec->uec_regs = uec_regs;
1029 /* Init UPSMR, enable hardware statistics (UCC) */
1030 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1032 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1033 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1035 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1036 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1038 /* Setup MAC interface mode */
1039 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1041 /* Setup MII management base */
1042 #ifndef CONFIG_eTSEC_MDIO_BUS
1043 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1045 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1048 /* Setup MII master clock source */
1049 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1052 utbipar = in_be32(&uec_regs->utbipar);
1053 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1054 enet_interface = uec->uec_info->enet_interface;
1055 if (enet_interface == ENET_1000_TBI ||
1056 enet_interface == ENET_1000_RTBI) {
1057 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1058 << UTBIPAR_PHY_ADDRESS_SHIFT;
1060 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1061 << UTBIPAR_PHY_ADDRESS_SHIFT;
1064 out_be32(&uec_regs->utbipar, utbipar);
1066 /* Allocate Tx BDs */
1067 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1068 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1069 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1070 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1071 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1072 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1075 align = UEC_TX_BD_RING_ALIGNMENT;
1076 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1077 if (uec->tx_bd_ring_offset != 0) {
1078 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1082 /* Zero all of Tx BDs */
1083 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1085 /* Allocate Rx BDs */
1086 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1087 align = UEC_RX_BD_RING_ALIGNMENT;
1088 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1089 if (uec->rx_bd_ring_offset != 0) {
1090 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1094 /* Zero all of Rx BDs */
1095 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1097 /* Allocate Rx buffer */
1098 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1099 align = UEC_RX_DATA_BUF_ALIGNMENT;
1100 uec->rx_buf_offset = (u32)malloc(length + align);
1101 if (uec->rx_buf_offset != 0) {
1102 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1106 /* Zero all of the Rx buffer */
1107 memset((void *)(uec->rx_buf_offset), 0, length + align);
1109 /* Init TxBD ring */
1110 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1113 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1115 BD_STATUS_SET(bd, 0);
1116 BD_LENGTH_SET(bd, 0);
1119 BD_STATUS_SET((--bd), TxBD_WRAP);
1121 /* Init RxBD ring */
1122 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1124 buf = uec->p_rx_buf;
1125 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1126 BD_DATA_SET(bd, buf);
1127 BD_LENGTH_SET(bd, 0);
1128 BD_STATUS_SET(bd, RxBD_EMPTY);
1129 buf += MAX_RXBUF_LEN;
1132 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1134 /* Init global Tx parameter RAM */
1135 uec_init_tx_parameter(uec, num_threads_tx);
1137 /* Init global Rx parameter RAM */
1138 uec_init_rx_parameter(uec, num_threads_rx);
1140 /* Init ethernet Tx and Rx parameter command */
1141 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1143 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1150 static int uec_init(struct eth_device* dev, bd_t *bd)
1154 struct phy_info *curphy;
1156 uec = (uec_private_t *)dev->priv;
1158 if (uec->the_first_run == 0) {
1159 err = init_phy(dev);
1161 printf("%s: Cannot initialize PHY, aborting.\n",
1166 curphy = uec->mii_info->phyinfo;
1168 if (curphy->config_aneg) {
1169 err = curphy->config_aneg(uec->mii_info);
1171 printf("%s: Can't negotiate PHY\n", dev->name);
1176 /* Give PHYs up to 5 sec to report a link */
1179 err = curphy->read_status(uec->mii_info);
1181 } while (((i-- > 0) && !uec->mii_info->link) || err);
1184 printf("warning: %s: timeout on PHY link\n", dev->name);
1186 uec->the_first_run = 1;
1189 /* Set up the MAC address */
1190 if (dev->enetaddr[0] & 0x01) {
1191 printf("%s: MacAddress is multcast address\n",
1195 uec_set_mac_address(uec, dev->enetaddr);
1198 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1200 printf("%s: cannot enable UEC device\n", dev->name);
1206 return (uec->mii_info->link ? 0 : -1);
1209 static void uec_halt(struct eth_device* dev)
1211 uec_private_t *uec = (uec_private_t *)dev->priv;
1212 uec_stop(uec, COMM_DIR_RX_AND_TX);
1215 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1218 ucc_fast_private_t *uccf;
1219 volatile qe_bd_t *bd;
1224 uec = (uec_private_t *)dev->priv;
1228 /* Find an empty TxBD */
1229 for (i = 0; bd->status & TxBD_READY; i++) {
1231 printf("%s: tx buffer not ready\n", dev->name);
1237 BD_DATA_SET(bd, buf);
1238 BD_LENGTH_SET(bd, len);
1239 status = bd->status;
1241 status |= (TxBD_READY | TxBD_LAST);
1242 BD_STATUS_SET(bd, status);
1244 /* Tell UCC to transmit the buffer */
1245 ucc_fast_transmit_on_demand(uccf);
1247 /* Wait for buffer to be transmitted */
1248 for (i = 0; bd->status & TxBD_READY; i++) {
1250 printf("%s: tx error\n", dev->name);
1255 /* Ok, the buffer be transimitted */
1256 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1263 static int uec_recv(struct eth_device* dev)
1265 uec_private_t *uec = dev->priv;
1266 volatile qe_bd_t *bd;
1272 status = bd->status;
1274 while (!(status & RxBD_EMPTY)) {
1275 if (!(status & RxBD_ERROR)) {
1277 len = BD_LENGTH(bd);
1278 NetReceive(data, len);
1280 printf("%s: Rx error\n", dev->name);
1283 BD_LENGTH_SET(bd, 0);
1284 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1285 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1286 status = bd->status;
1293 int uec_initialize(int index)
1295 struct eth_device *dev;
1298 uec_info_t *uec_info;
1301 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1304 memset(dev, 0, sizeof(struct eth_device));
1306 /* Allocate the UEC private struct */
1307 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1311 memset(uec, 0, sizeof(uec_private_t));
1313 /* Init UEC private struct, they come from board.h */
1316 #ifdef CONFIG_UEC_ETH1
1317 uec_info = ð1_uec_info;
1319 } else if (index == 1) {
1320 #ifdef CONFIG_UEC_ETH2
1321 uec_info = ð2_uec_info;
1323 } else if (index == 2) {
1324 #ifdef CONFIG_UEC_ETH3
1325 uec_info = ð3_uec_info;
1327 } else if (index == 3) {
1328 #ifdef CONFIG_UEC_ETH4
1329 uec_info = ð4_uec_info;
1332 printf("%s: index is illegal.\n", __FUNCTION__);
1336 uec->uec_info = uec_info;
1338 sprintf(dev->name, "FSL UEC%d", index);
1340 dev->priv = (void *)uec;
1341 dev->init = uec_init;
1342 dev->halt = uec_halt;
1343 dev->send = uec_send;
1344 dev->recv = uec_recv;
1346 /* Clear the ethnet address */
1347 for (i = 0; i < 6; i++)
1348 dev->enetaddr[i] = 0;
1352 err = uec_startup(uec);
1354 printf("%s: Cannot configure net device, aborting.",dev->name);
1360 #endif /* CONFIG_QE */