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"
34 #if defined(CONFIG_QE)
36 #ifdef CONFIG_UEC_ETH1
37 static uec_info_t eth1_uec_info = {
39 .ucc_num = CFG_UEC1_UCC_NUM,
40 .rx_clock = CFG_UEC1_RX_CLK,
41 .tx_clock = CFG_UEC1_TX_CLK,
42 .eth_type = CFG_UEC1_ETH_TYPE,
44 #if (CFG_UEC1_ETH_TYPE == FAST_ETH)
45 .num_threads_tx = UEC_NUM_OF_THREADS_1,
46 .num_threads_rx = UEC_NUM_OF_THREADS_1,
48 .num_threads_tx = UEC_NUM_OF_THREADS_4,
49 .num_threads_rx = UEC_NUM_OF_THREADS_4,
51 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
52 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
55 .phy_address = CFG_UEC1_PHY_ADDR,
56 .enet_interface = CFG_UEC1_INTERFACE_MODE,
59 #ifdef CONFIG_UEC_ETH2
60 static uec_info_t eth2_uec_info = {
62 .ucc_num = CFG_UEC2_UCC_NUM,
63 .rx_clock = CFG_UEC2_RX_CLK,
64 .tx_clock = CFG_UEC2_TX_CLK,
65 .eth_type = CFG_UEC2_ETH_TYPE,
67 #if (CFG_UEC2_ETH_TYPE == FAST_ETH)
68 .num_threads_tx = UEC_NUM_OF_THREADS_1,
69 .num_threads_rx = UEC_NUM_OF_THREADS_1,
71 .num_threads_tx = UEC_NUM_OF_THREADS_4,
72 .num_threads_rx = UEC_NUM_OF_THREADS_4,
74 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
75 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
78 .phy_address = CFG_UEC2_PHY_ADDR,
79 .enet_interface = CFG_UEC2_INTERFACE_MODE,
82 #ifdef CONFIG_UEC_ETH3
83 static uec_info_t eth3_uec_info = {
85 .ucc_num = CFG_UEC3_UCC_NUM,
86 .rx_clock = CFG_UEC3_RX_CLK,
87 .tx_clock = CFG_UEC3_TX_CLK,
88 .eth_type = CFG_UEC3_ETH_TYPE,
90 #if (CFG_UEC3_ETH_TYPE == FAST_ETH)
91 .num_threads_tx = UEC_NUM_OF_THREADS_1,
92 .num_threads_rx = UEC_NUM_OF_THREADS_1,
94 .num_threads_tx = UEC_NUM_OF_THREADS_4,
95 .num_threads_rx = UEC_NUM_OF_THREADS_4,
97 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
98 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
100 .rx_bd_ring_len = 16,
101 .phy_address = CFG_UEC3_PHY_ADDR,
102 .enet_interface = CFG_UEC3_INTERFACE_MODE,
105 #ifdef CONFIG_UEC_ETH4
106 static uec_info_t eth4_uec_info = {
108 .ucc_num = CFG_UEC4_UCC_NUM,
109 .rx_clock = CFG_UEC4_RX_CLK,
110 .tx_clock = CFG_UEC4_TX_CLK,
111 .eth_type = CFG_UEC4_ETH_TYPE,
113 #if (CFG_UEC4_ETH_TYPE == FAST_ETH)
114 .num_threads_tx = UEC_NUM_OF_THREADS_1,
115 .num_threads_rx = UEC_NUM_OF_THREADS_1,
117 .num_threads_tx = UEC_NUM_OF_THREADS_4,
118 .num_threads_rx = UEC_NUM_OF_THREADS_4,
120 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
121 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
122 .tx_bd_ring_len = 16,
123 .rx_bd_ring_len = 16,
124 .phy_address = CFG_UEC4_PHY_ADDR,
125 .enet_interface = CFG_UEC4_INTERFACE_MODE,
129 #define MAXCONTROLLERS (4)
131 static struct eth_device *devlist[MAXCONTROLLERS];
133 static int uec_miiphy_read(char *devname, unsigned char addr,
134 unsigned char reg, unsigned short *value);
135 static int uec_miiphy_write(char *devname, unsigned char addr,
136 unsigned char reg, unsigned short value);
137 u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
138 void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
140 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
146 printf("%s: uec not initial\n", __FUNCTION__);
149 uec_regs = uec->uec_regs;
151 maccfg1 = in_be32(&uec_regs->maccfg1);
153 if (mode & COMM_DIR_TX) {
154 maccfg1 |= MACCFG1_ENABLE_TX;
155 out_be32(&uec_regs->maccfg1, maccfg1);
156 uec->mac_tx_enabled = 1;
159 if (mode & COMM_DIR_RX) {
160 maccfg1 |= MACCFG1_ENABLE_RX;
161 out_be32(&uec_regs->maccfg1, maccfg1);
162 uec->mac_rx_enabled = 1;
168 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
174 printf("%s: uec not initial\n", __FUNCTION__);
177 uec_regs = uec->uec_regs;
179 maccfg1 = in_be32(&uec_regs->maccfg1);
181 if (mode & COMM_DIR_TX) {
182 maccfg1 &= ~MACCFG1_ENABLE_TX;
183 out_be32(&uec_regs->maccfg1, maccfg1);
184 uec->mac_tx_enabled = 0;
187 if (mode & COMM_DIR_RX) {
188 maccfg1 &= ~MACCFG1_ENABLE_RX;
189 out_be32(&uec_regs->maccfg1, maccfg1);
190 uec->mac_rx_enabled = 0;
196 static int uec_graceful_stop_tx(uec_private_t *uec)
202 if (!uec || !uec->uccf) {
203 printf("%s: No handle passed.\n", __FUNCTION__);
207 uf_regs = uec->uccf->uf_regs;
209 /* Clear the grace stop event */
210 out_be32(&uf_regs->ucce, UCCE_GRA);
212 /* Issue host command */
214 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
215 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
216 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
218 /* Wait for command to complete */
220 ucce = in_be32(&uf_regs->ucce);
221 } while (! (ucce & UCCE_GRA));
223 uec->grace_stopped_tx = 1;
228 static int uec_graceful_stop_rx(uec_private_t *uec)
234 printf("%s: No handle passed.\n", __FUNCTION__);
238 if (!uec->p_rx_glbl_pram) {
239 printf("%s: No init rx global parameter\n", __FUNCTION__);
243 /* Clear acknowledge bit */
244 ack = uec->p_rx_glbl_pram->rxgstpack;
245 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
246 uec->p_rx_glbl_pram->rxgstpack = ack;
248 /* Keep issuing cmd and checking ack bit until it is asserted */
250 /* Issue host command */
252 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
253 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
254 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
255 ack = uec->p_rx_glbl_pram->rxgstpack;
256 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
258 uec->grace_stopped_rx = 1;
263 static int uec_restart_tx(uec_private_t *uec)
267 if (!uec || !uec->uec_info) {
268 printf("%s: No handle passed.\n", __FUNCTION__);
273 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
274 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
275 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
277 uec->grace_stopped_tx = 0;
282 static int uec_restart_rx(uec_private_t *uec)
286 if (!uec || !uec->uec_info) {
287 printf("%s: No handle passed.\n", __FUNCTION__);
292 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
293 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
294 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
296 uec->grace_stopped_rx = 0;
301 static int uec_open(uec_private_t *uec, comm_dir_e mode)
303 ucc_fast_private_t *uccf;
305 if (!uec || !uec->uccf) {
306 printf("%s: No handle passed.\n", __FUNCTION__);
311 /* check if the UCC number is in range. */
312 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
313 printf("%s: ucc_num out of range.\n", __FUNCTION__);
318 uec_mac_enable(uec, mode);
320 /* Enable UCC fast */
321 ucc_fast_enable(uccf, mode);
323 /* RISC microcode start */
324 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
327 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
334 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
336 ucc_fast_private_t *uccf;
338 if (!uec || !uec->uccf) {
339 printf("%s: No handle passed.\n", __FUNCTION__);
344 /* check if the UCC number is in range. */
345 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
346 printf("%s: ucc_num out of range.\n", __FUNCTION__);
349 /* Stop any transmissions */
350 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
351 uec_graceful_stop_tx(uec);
353 /* Stop any receptions */
354 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
355 uec_graceful_stop_rx(uec);
358 /* Disable the UCC fast */
359 ucc_fast_disable(uec->uccf, mode);
361 /* Disable the MAC */
362 uec_mac_disable(uec, mode);
367 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
373 printf("%s: uec not initial\n", __FUNCTION__);
376 uec_regs = uec->uec_regs;
378 if (duplex == DUPLEX_HALF) {
379 maccfg2 = in_be32(&uec_regs->maccfg2);
380 maccfg2 &= ~MACCFG2_FDX;
381 out_be32(&uec_regs->maccfg2, maccfg2);
384 if (duplex == DUPLEX_FULL) {
385 maccfg2 = in_be32(&uec_regs->maccfg2);
386 maccfg2 |= MACCFG2_FDX;
387 out_be32(&uec_regs->maccfg2, maccfg2);
393 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
395 enet_interface_e enet_if_mode;
396 uec_info_t *uec_info;
402 printf("%s: uec not initial\n", __FUNCTION__);
406 uec_info = uec->uec_info;
407 uec_regs = uec->uec_regs;
408 enet_if_mode = if_mode;
410 maccfg2 = in_be32(&uec_regs->maccfg2);
411 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
413 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
414 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
416 switch (enet_if_mode) {
419 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
422 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
425 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
429 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
430 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
432 case ENET_1000_RGMII_RXID:
433 case ENET_1000_RGMII:
434 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
438 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
442 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
443 upsmr |= (UPSMR_RPM | UPSMR_R10M);
446 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
450 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
451 upsmr |= (UPSMR_R10M | UPSMR_RMM);
457 out_be32(&uec_regs->maccfg2, maccfg2);
458 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
463 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
465 uint timeout = 0x1000;
468 miimcfg = in_be32(&uec_mii_regs->miimcfg);
469 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
470 out_be32(&uec_mii_regs->miimcfg, miimcfg);
472 /* Wait until the bus is free */
473 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
475 printf("%s: The MII Bus is stuck!", __FUNCTION__);
482 static int init_phy(struct eth_device *dev)
485 uec_mii_t *umii_regs;
486 struct uec_mii_info *mii_info;
487 struct phy_info *curphy;
490 uec = (uec_private_t *)dev->priv;
491 umii_regs = uec->uec_mii_regs;
497 mii_info = malloc(sizeof(*mii_info));
499 printf("%s: Could not allocate mii_info", dev->name);
502 memset(mii_info, 0, sizeof(*mii_info));
504 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
505 mii_info->speed = SPEED_1000;
507 mii_info->speed = SPEED_100;
510 mii_info->duplex = DUPLEX_FULL;
514 mii_info->advertising = (ADVERTISED_10baseT_Half |
515 ADVERTISED_10baseT_Full |
516 ADVERTISED_100baseT_Half |
517 ADVERTISED_100baseT_Full |
518 ADVERTISED_1000baseT_Full);
519 mii_info->autoneg = 1;
520 mii_info->mii_id = uec->uec_info->phy_address;
523 mii_info->mdio_read = &uec_read_phy_reg;
524 mii_info->mdio_write = &uec_write_phy_reg;
526 uec->mii_info = mii_info;
528 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
530 if (init_mii_management_configuration(umii_regs)) {
531 printf("%s: The MII Bus is stuck!", dev->name);
536 /* get info for this PHY */
537 curphy = uec_get_phy_info(uec->mii_info);
539 printf("%s: No PHY found", dev->name);
544 mii_info->phyinfo = curphy;
546 /* Run the commands which initialize the PHY */
548 err = curphy->init(uec->mii_info);
562 static void adjust_link(struct eth_device *dev)
564 uec_private_t *uec = (uec_private_t *)dev->priv;
566 struct uec_mii_info *mii_info = uec->mii_info;
568 extern void change_phy_interface_mode(struct eth_device *dev,
569 enet_interface_e mode);
570 uec_regs = uec->uec_regs;
572 if (mii_info->link) {
573 /* Now we make sure that we can be in full duplex mode.
574 * If not, we operate in half-duplex mode. */
575 if (mii_info->duplex != uec->oldduplex) {
576 if (!(mii_info->duplex)) {
577 uec_set_mac_duplex(uec, DUPLEX_HALF);
578 printf("%s: Half Duplex\n", dev->name);
580 uec_set_mac_duplex(uec, DUPLEX_FULL);
581 printf("%s: Full Duplex\n", dev->name);
583 uec->oldduplex = mii_info->duplex;
586 if (mii_info->speed != uec->oldspeed) {
587 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
588 switch (mii_info->speed) {
592 printf ("switching to rgmii 100\n");
593 /* change phy to rgmii 100 */
594 change_phy_interface_mode(dev,
596 /* change the MAC interface mode */
597 uec_set_mac_if_mode(uec,ENET_100_RGMII);
600 printf ("switching to rgmii 10\n");
601 /* change phy to rgmii 10 */
602 change_phy_interface_mode(dev,
604 /* change the MAC interface mode */
605 uec_set_mac_if_mode(uec,ENET_10_RGMII);
608 printf("%s: Ack,Speed(%d)is illegal\n",
609 dev->name, mii_info->speed);
614 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
615 uec->oldspeed = mii_info->speed;
619 printf("%s: Link is up\n", dev->name);
623 } else { /* if (mii_info->link) */
625 printf("%s: Link is down\n", dev->name);
633 static void phy_change(struct eth_device *dev)
635 uec_private_t *uec = (uec_private_t *)dev->priv;
637 /* Update the link, speed, duplex */
638 uec->mii_info->phyinfo->read_status(uec->mii_info);
640 /* Adjust the interface according to speed */
644 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
651 printf("%s: uec not initial\n", __FUNCTION__);
655 uec_regs = uec->uec_regs;
657 /* if a station address of 0x12345678ABCD, perform a write to
658 MACSTNADDR1 of 0xCDAB7856,
659 MACSTNADDR2 of 0x34120000 */
661 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
662 (mac_addr[3] << 8) | (mac_addr[2]);
663 out_be32(&uec_regs->macstnaddr1, mac_addr1);
665 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
666 out_be32(&uec_regs->macstnaddr2, mac_addr2);
671 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
672 int *threads_num_ret)
674 int num_threads_numerica;
676 switch (threads_num) {
677 case UEC_NUM_OF_THREADS_1:
678 num_threads_numerica = 1;
680 case UEC_NUM_OF_THREADS_2:
681 num_threads_numerica = 2;
683 case UEC_NUM_OF_THREADS_4:
684 num_threads_numerica = 4;
686 case UEC_NUM_OF_THREADS_6:
687 num_threads_numerica = 6;
689 case UEC_NUM_OF_THREADS_8:
690 num_threads_numerica = 8;
693 printf("%s: Bad number of threads value.",
698 *threads_num_ret = num_threads_numerica;
703 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
705 uec_info_t *uec_info;
710 uec_info = uec->uec_info;
712 /* Alloc global Tx parameter RAM page */
713 uec->tx_glbl_pram_offset = qe_muram_alloc(
714 sizeof(uec_tx_global_pram_t),
715 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
716 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
717 qe_muram_addr(uec->tx_glbl_pram_offset);
719 /* Zero the global Tx prameter RAM */
720 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
722 /* Init global Tx parameter RAM */
724 /* TEMODER, RMON statistics disable, one Tx queue */
725 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
728 uec->send_q_mem_reg_offset = qe_muram_alloc(
729 sizeof(uec_send_queue_qd_t),
730 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
731 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
732 qe_muram_addr(uec->send_q_mem_reg_offset);
733 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
735 /* Setup the table with TxBDs ring */
736 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
738 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
739 (u32)(uec->p_tx_bd_ring));
740 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
743 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
744 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
746 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
747 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
749 /* TSTATE, global snooping, big endian, the CSB bus selected */
750 bmrx = BMR_INIT_VALUE;
751 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
754 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
755 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
759 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
760 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
764 uec->thread_dat_tx_offset = qe_muram_alloc(
765 num_threads_tx * sizeof(uec_thread_data_tx_t) +
766 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
768 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
769 qe_muram_addr(uec->thread_dat_tx_offset);
770 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
773 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
777 uec_82xx_address_filtering_pram_t *p_af_pram;
779 /* Allocate global Rx parameter RAM page */
780 uec->rx_glbl_pram_offset = qe_muram_alloc(
781 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
782 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
783 qe_muram_addr(uec->rx_glbl_pram_offset);
785 /* Zero Global Rx parameter RAM */
786 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
788 /* Init global Rx parameter RAM */
789 /* REMODER, Extended feature mode disable, VLAN disable,
790 LossLess flow control disable, Receive firmware statisic disable,
791 Extended address parsing mode disable, One Rx queues,
792 Dynamic maximum/minimum frame length disable, IP checksum check
793 disable, IP address alignment disable
795 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
798 uec->thread_dat_rx_offset = qe_muram_alloc(
799 num_threads_rx * sizeof(uec_thread_data_rx_t),
800 UEC_THREAD_DATA_ALIGNMENT);
801 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
802 qe_muram_addr(uec->thread_dat_rx_offset);
803 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
806 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
808 /* RxRMON base pointer, we don't need it */
809 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
811 /* IntCoalescingPTR, we don't need it, no interrupt */
812 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
814 /* RSTATE, global snooping, big endian, the CSB bus selected */
815 bmrx = BMR_INIT_VALUE;
816 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
819 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
822 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
823 sizeof(uec_rx_bd_queues_entry_t) + \
824 sizeof(uec_rx_prefetched_bds_t),
825 UEC_RX_BD_QUEUES_ALIGNMENT);
826 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
827 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
830 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
831 sizeof(uec_rx_prefetched_bds_t));
832 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
833 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
834 (u32)uec->p_rx_bd_ring);
837 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
839 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
841 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
843 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
845 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
847 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
849 for (i = 0; i < 8; i++) {
850 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
854 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
856 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
858 /* Clear PQ2 style address filtering hash table */
859 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
860 uec->p_rx_glbl_pram->addressfiltering;
862 p_af_pram->iaddr_h = 0;
863 p_af_pram->iaddr_l = 0;
864 p_af_pram->gaddr_h = 0;
865 p_af_pram->gaddr_l = 0;
868 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
869 int thread_tx, int thread_rx)
871 uec_init_cmd_pram_t *p_init_enet_param;
872 u32 init_enet_param_offset;
873 uec_info_t *uec_info;
876 u32 init_enet_offset;
881 uec_info = uec->uec_info;
883 /* Allocate init enet command parameter */
884 uec->init_enet_param_offset = qe_muram_alloc(
885 sizeof(uec_init_cmd_pram_t), 4);
886 init_enet_param_offset = uec->init_enet_param_offset;
887 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
888 qe_muram_addr(uec->init_enet_param_offset);
890 /* Zero init enet command struct */
891 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
893 /* Init the command struct */
894 p_init_enet_param = uec->p_init_enet_param;
895 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
896 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
897 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
898 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
899 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
900 p_init_enet_param->largestexternallookupkeysize = 0;
902 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
903 << ENET_INIT_PARAM_RGF_SHIFT;
904 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
905 << ENET_INIT_PARAM_TGF_SHIFT;
907 /* Init Rx global parameter pointer */
908 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
909 (u32)uec_info->riscRx;
911 /* Init Rx threads */
912 for (i = 0; i < (thread_rx + 1); i++) {
913 if ((snum = qe_get_snum()) < 0) {
914 printf("%s can not get snum\n", __FUNCTION__);
919 init_enet_offset = 0;
921 init_enet_offset = qe_muram_alloc(
922 sizeof(uec_thread_rx_pram_t),
923 UEC_THREAD_RX_PRAM_ALIGNMENT);
926 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
927 init_enet_offset | (u32)uec_info->riscRx;
928 p_init_enet_param->rxthread[i] = entry_val;
931 /* Init Tx global parameter pointer */
932 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
933 (u32)uec_info->riscTx;
935 /* Init Tx threads */
936 for (i = 0; i < thread_tx; i++) {
937 if ((snum = qe_get_snum()) < 0) {
938 printf("%s can not get snum\n", __FUNCTION__);
942 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
943 UEC_THREAD_TX_PRAM_ALIGNMENT);
945 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
946 init_enet_offset | (u32)uec_info->riscTx;
947 p_init_enet_param->txthread[i] = entry_val;
950 __asm__ __volatile__("sync");
952 /* Issue QE command */
953 command = QE_INIT_TX_RX;
954 cecr_subblock = ucc_fast_get_qe_cr_subblock(
955 uec->uec_info->uf_info.ucc_num);
956 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
957 init_enet_param_offset);
962 static int uec_startup(uec_private_t *uec)
964 uec_info_t *uec_info;
965 ucc_fast_info_t *uf_info;
966 ucc_fast_private_t *uccf;
972 enet_interface_e enet_interface;
979 if (!uec || !uec->uec_info) {
980 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
984 uec_info = uec->uec_info;
985 uf_info = &(uec_info->uf_info);
987 /* Check if Rx BD ring len is illegal */
988 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
989 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
990 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
995 /* Check if Tx BD ring len is illegal */
996 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
997 printf("%s: Tx BD ring length must not be smaller than 2.\n",
1002 /* Check if MRBLR is illegal */
1003 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
1004 printf("%s: max rx buffer length must be mutliple of 128.\n",
1009 /* Both Rx and Tx are stopped */
1010 uec->grace_stopped_rx = 1;
1011 uec->grace_stopped_tx = 1;
1014 if (ucc_fast_init(uf_info, &uccf)) {
1015 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1022 /* Convert the Tx threads number */
1023 if (uec_convert_threads_num(uec_info->num_threads_tx,
1028 /* Convert the Rx threads number */
1029 if (uec_convert_threads_num(uec_info->num_threads_rx,
1034 uf_regs = uccf->uf_regs;
1036 /* UEC register is following UCC fast registers */
1037 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1039 /* Save the UEC register pointer to UEC private struct */
1040 uec->uec_regs = uec_regs;
1042 /* Init UPSMR, enable hardware statistics (UCC) */
1043 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1045 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1046 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1048 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1049 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1051 /* Setup MAC interface mode */
1052 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1054 /* Setup MII management base */
1055 #ifndef CONFIG_eTSEC_MDIO_BUS
1056 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1058 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1061 /* Setup MII master clock source */
1062 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1065 utbipar = in_be32(&uec_regs->utbipar);
1066 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1067 enet_interface = uec->uec_info->enet_interface;
1068 if (enet_interface == ENET_1000_TBI ||
1069 enet_interface == ENET_1000_RTBI) {
1070 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1071 << UTBIPAR_PHY_ADDRESS_SHIFT;
1073 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1074 << UTBIPAR_PHY_ADDRESS_SHIFT;
1077 out_be32(&uec_regs->utbipar, utbipar);
1079 /* Allocate Tx BDs */
1080 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1081 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1082 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1083 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1084 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1085 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1088 align = UEC_TX_BD_RING_ALIGNMENT;
1089 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1090 if (uec->tx_bd_ring_offset != 0) {
1091 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1095 /* Zero all of Tx BDs */
1096 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1098 /* Allocate Rx BDs */
1099 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1100 align = UEC_RX_BD_RING_ALIGNMENT;
1101 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1102 if (uec->rx_bd_ring_offset != 0) {
1103 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1107 /* Zero all of Rx BDs */
1108 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1110 /* Allocate Rx buffer */
1111 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1112 align = UEC_RX_DATA_BUF_ALIGNMENT;
1113 uec->rx_buf_offset = (u32)malloc(length + align);
1114 if (uec->rx_buf_offset != 0) {
1115 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1119 /* Zero all of the Rx buffer */
1120 memset((void *)(uec->rx_buf_offset), 0, length + align);
1122 /* Init TxBD ring */
1123 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1126 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1128 BD_STATUS_SET(bd, 0);
1129 BD_LENGTH_SET(bd, 0);
1132 BD_STATUS_SET((--bd), TxBD_WRAP);
1134 /* Init RxBD ring */
1135 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1137 buf = uec->p_rx_buf;
1138 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1139 BD_DATA_SET(bd, buf);
1140 BD_LENGTH_SET(bd, 0);
1141 BD_STATUS_SET(bd, RxBD_EMPTY);
1142 buf += MAX_RXBUF_LEN;
1145 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1147 /* Init global Tx parameter RAM */
1148 uec_init_tx_parameter(uec, num_threads_tx);
1150 /* Init global Rx parameter RAM */
1151 uec_init_rx_parameter(uec, num_threads_rx);
1153 /* Init ethernet Tx and Rx parameter command */
1154 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1156 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1163 static int uec_init(struct eth_device* dev, bd_t *bd)
1167 struct phy_info *curphy;
1169 uec = (uec_private_t *)dev->priv;
1171 if (uec->the_first_run == 0) {
1172 err = init_phy(dev);
1174 printf("%s: Cannot initialize PHY, aborting.\n",
1179 curphy = uec->mii_info->phyinfo;
1181 if (curphy->config_aneg) {
1182 err = curphy->config_aneg(uec->mii_info);
1184 printf("%s: Can't negotiate PHY\n", dev->name);
1189 /* Give PHYs up to 5 sec to report a link */
1192 err = curphy->read_status(uec->mii_info);
1194 } while (((i-- > 0) && !uec->mii_info->link) || err);
1197 printf("warning: %s: timeout on PHY link\n", dev->name);
1199 uec->the_first_run = 1;
1202 /* Set up the MAC address */
1203 if (dev->enetaddr[0] & 0x01) {
1204 printf("%s: MacAddress is multcast address\n",
1208 uec_set_mac_address(uec, dev->enetaddr);
1211 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1213 printf("%s: cannot enable UEC device\n", dev->name);
1219 return (uec->mii_info->link ? 0 : -1);
1222 static void uec_halt(struct eth_device* dev)
1224 uec_private_t *uec = (uec_private_t *)dev->priv;
1225 uec_stop(uec, COMM_DIR_RX_AND_TX);
1228 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1231 ucc_fast_private_t *uccf;
1232 volatile qe_bd_t *bd;
1237 uec = (uec_private_t *)dev->priv;
1241 /* Find an empty TxBD */
1242 for (i = 0; bd->status & TxBD_READY; i++) {
1244 printf("%s: tx buffer not ready\n", dev->name);
1250 BD_DATA_SET(bd, buf);
1251 BD_LENGTH_SET(bd, len);
1252 status = bd->status;
1254 status |= (TxBD_READY | TxBD_LAST);
1255 BD_STATUS_SET(bd, status);
1257 /* Tell UCC to transmit the buffer */
1258 ucc_fast_transmit_on_demand(uccf);
1260 /* Wait for buffer to be transmitted */
1261 for (i = 0; bd->status & TxBD_READY; i++) {
1263 printf("%s: tx error\n", dev->name);
1268 /* Ok, the buffer be transimitted */
1269 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1276 static int uec_recv(struct eth_device* dev)
1278 uec_private_t *uec = dev->priv;
1279 volatile qe_bd_t *bd;
1285 status = bd->status;
1287 while (!(status & RxBD_EMPTY)) {
1288 if (!(status & RxBD_ERROR)) {
1290 len = BD_LENGTH(bd);
1291 NetReceive(data, len);
1293 printf("%s: Rx error\n", dev->name);
1296 BD_LENGTH_SET(bd, 0);
1297 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1298 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1299 status = bd->status;
1306 int uec_initialize(int index)
1308 struct eth_device *dev;
1311 uec_info_t *uec_info;
1314 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1317 memset(dev, 0, sizeof(struct eth_device));
1319 /* Allocate the UEC private struct */
1320 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1324 memset(uec, 0, sizeof(uec_private_t));
1326 /* Init UEC private struct, they come from board.h */
1329 #ifdef CONFIG_UEC_ETH1
1330 uec_info = ð1_uec_info;
1332 } else if (index == 1) {
1333 #ifdef CONFIG_UEC_ETH2
1334 uec_info = ð2_uec_info;
1336 } else if (index == 2) {
1337 #ifdef CONFIG_UEC_ETH3
1338 uec_info = ð3_uec_info;
1340 } else if (index == 3) {
1341 #ifdef CONFIG_UEC_ETH4
1342 uec_info = ð4_uec_info;
1345 printf("%s: index is illegal.\n", __FUNCTION__);
1349 devlist[index] = dev;
1351 uec->uec_info = uec_info;
1353 sprintf(dev->name, "FSL UEC%d", index);
1355 dev->priv = (void *)uec;
1356 dev->init = uec_init;
1357 dev->halt = uec_halt;
1358 dev->send = uec_send;
1359 dev->recv = uec_recv;
1361 /* Clear the ethnet address */
1362 for (i = 0; i < 6; i++)
1363 dev->enetaddr[i] = 0;
1367 err = uec_startup(uec);
1369 printf("%s: Cannot configure net device, aborting.",dev->name);
1373 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1374 && !defined(BITBANGMII)
1375 miiphy_register(dev->name, uec_miiphy_read, uec_miiphy_write);
1381 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1382 && !defined(BITBANGMII)
1385 * Read a MII PHY register.
1390 static int uec_miiphy_read(char *devname, unsigned char addr,
1391 unsigned char reg, unsigned short *value)
1393 *value = uec_read_phy_reg(devlist[0], addr, reg);
1399 * Write a MII PHY register.
1404 static int uec_miiphy_write(char *devname, unsigned char addr,
1405 unsigned char reg, unsigned short value)
1407 uec_write_phy_reg(devlist[0], addr, reg, value);
1414 #endif /* CONFIG_QE */