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 #ifdef CONFIG_UEC_ETH1
35 static uec_info_t eth1_uec_info = {
37 .ucc_num = CONFIG_SYS_UEC1_UCC_NUM,
38 .rx_clock = CONFIG_SYS_UEC1_RX_CLK,
39 .tx_clock = CONFIG_SYS_UEC1_TX_CLK,
40 .eth_type = CONFIG_SYS_UEC1_ETH_TYPE,
42 #if (CONFIG_SYS_UEC1_ETH_TYPE == FAST_ETH)
43 .num_threads_tx = UEC_NUM_OF_THREADS_1,
44 .num_threads_rx = UEC_NUM_OF_THREADS_1,
46 .num_threads_tx = UEC_NUM_OF_THREADS_4,
47 .num_threads_rx = UEC_NUM_OF_THREADS_4,
49 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
50 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
53 .phy_address = CONFIG_SYS_UEC1_PHY_ADDR,
54 .enet_interface = CONFIG_SYS_UEC1_INTERFACE_MODE,
57 #ifdef CONFIG_UEC_ETH2
58 static uec_info_t eth2_uec_info = {
60 .ucc_num = CONFIG_SYS_UEC2_UCC_NUM,
61 .rx_clock = CONFIG_SYS_UEC2_RX_CLK,
62 .tx_clock = CONFIG_SYS_UEC2_TX_CLK,
63 .eth_type = CONFIG_SYS_UEC2_ETH_TYPE,
65 #if (CONFIG_SYS_UEC2_ETH_TYPE == FAST_ETH)
66 .num_threads_tx = UEC_NUM_OF_THREADS_1,
67 .num_threads_rx = UEC_NUM_OF_THREADS_1,
69 .num_threads_tx = UEC_NUM_OF_THREADS_4,
70 .num_threads_rx = UEC_NUM_OF_THREADS_4,
72 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
73 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
76 .phy_address = CONFIG_SYS_UEC2_PHY_ADDR,
77 .enet_interface = CONFIG_SYS_UEC2_INTERFACE_MODE,
80 #ifdef CONFIG_UEC_ETH3
81 static uec_info_t eth3_uec_info = {
83 .ucc_num = CONFIG_SYS_UEC3_UCC_NUM,
84 .rx_clock = CONFIG_SYS_UEC3_RX_CLK,
85 .tx_clock = CONFIG_SYS_UEC3_TX_CLK,
86 .eth_type = CONFIG_SYS_UEC3_ETH_TYPE,
88 #if (CONFIG_SYS_UEC3_ETH_TYPE == FAST_ETH)
89 .num_threads_tx = UEC_NUM_OF_THREADS_1,
90 .num_threads_rx = UEC_NUM_OF_THREADS_1,
92 .num_threads_tx = UEC_NUM_OF_THREADS_4,
93 .num_threads_rx = UEC_NUM_OF_THREADS_4,
95 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
96 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
99 .phy_address = CONFIG_SYS_UEC3_PHY_ADDR,
100 .enet_interface = CONFIG_SYS_UEC3_INTERFACE_MODE,
103 #ifdef CONFIG_UEC_ETH4
104 static uec_info_t eth4_uec_info = {
106 .ucc_num = CONFIG_SYS_UEC4_UCC_NUM,
107 .rx_clock = CONFIG_SYS_UEC4_RX_CLK,
108 .tx_clock = CONFIG_SYS_UEC4_TX_CLK,
109 .eth_type = CONFIG_SYS_UEC4_ETH_TYPE,
111 #if (CONFIG_SYS_UEC4_ETH_TYPE == FAST_ETH)
112 .num_threads_tx = UEC_NUM_OF_THREADS_1,
113 .num_threads_rx = UEC_NUM_OF_THREADS_1,
115 .num_threads_tx = UEC_NUM_OF_THREADS_4,
116 .num_threads_rx = UEC_NUM_OF_THREADS_4,
118 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
119 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
120 .tx_bd_ring_len = 16,
121 .rx_bd_ring_len = 16,
122 .phy_address = CONFIG_SYS_UEC4_PHY_ADDR,
123 .enet_interface = CONFIG_SYS_UEC4_INTERFACE_MODE,
126 #ifdef CONFIG_UEC_ETH5
127 static uec_info_t eth5_uec_info = {
129 .ucc_num = CONFIG_SYS_UEC5_UCC_NUM,
130 .rx_clock = CONFIG_SYS_UEC5_RX_CLK,
131 .tx_clock = CONFIG_SYS_UEC5_TX_CLK,
132 .eth_type = CONFIG_SYS_UEC5_ETH_TYPE,
134 #if (CONFIG_SYS_UEC5_ETH_TYPE == FAST_ETH)
135 .num_threads_tx = UEC_NUM_OF_THREADS_1,
136 .num_threads_rx = UEC_NUM_OF_THREADS_1,
138 .num_threads_tx = UEC_NUM_OF_THREADS_4,
139 .num_threads_rx = UEC_NUM_OF_THREADS_4,
141 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
142 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
143 .tx_bd_ring_len = 16,
144 .rx_bd_ring_len = 16,
145 .phy_address = CONFIG_SYS_UEC5_PHY_ADDR,
146 .enet_interface = CONFIG_SYS_UEC5_INTERFACE_MODE,
149 #ifdef CONFIG_UEC_ETH6
150 static uec_info_t eth6_uec_info = {
152 .ucc_num = CONFIG_SYS_UEC6_UCC_NUM,
153 .rx_clock = CONFIG_SYS_UEC6_RX_CLK,
154 .tx_clock = CONFIG_SYS_UEC6_TX_CLK,
155 .eth_type = CONFIG_SYS_UEC6_ETH_TYPE,
157 #if (CONFIG_SYS_UEC6_ETH_TYPE == FAST_ETH)
158 .num_threads_tx = UEC_NUM_OF_THREADS_1,
159 .num_threads_rx = UEC_NUM_OF_THREADS_1,
161 .num_threads_tx = UEC_NUM_OF_THREADS_4,
162 .num_threads_rx = UEC_NUM_OF_THREADS_4,
164 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
165 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
166 .tx_bd_ring_len = 16,
167 .rx_bd_ring_len = 16,
168 .phy_address = CONFIG_SYS_UEC6_PHY_ADDR,
169 .enet_interface = CONFIG_SYS_UEC6_INTERFACE_MODE,
173 #define MAXCONTROLLERS (6)
175 static struct eth_device *devlist[MAXCONTROLLERS];
177 u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
178 void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
180 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
186 printf("%s: uec not initial\n", __FUNCTION__);
189 uec_regs = uec->uec_regs;
191 maccfg1 = in_be32(&uec_regs->maccfg1);
193 if (mode & COMM_DIR_TX) {
194 maccfg1 |= MACCFG1_ENABLE_TX;
195 out_be32(&uec_regs->maccfg1, maccfg1);
196 uec->mac_tx_enabled = 1;
199 if (mode & COMM_DIR_RX) {
200 maccfg1 |= MACCFG1_ENABLE_RX;
201 out_be32(&uec_regs->maccfg1, maccfg1);
202 uec->mac_rx_enabled = 1;
208 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
214 printf("%s: uec not initial\n", __FUNCTION__);
217 uec_regs = uec->uec_regs;
219 maccfg1 = in_be32(&uec_regs->maccfg1);
221 if (mode & COMM_DIR_TX) {
222 maccfg1 &= ~MACCFG1_ENABLE_TX;
223 out_be32(&uec_regs->maccfg1, maccfg1);
224 uec->mac_tx_enabled = 0;
227 if (mode & COMM_DIR_RX) {
228 maccfg1 &= ~MACCFG1_ENABLE_RX;
229 out_be32(&uec_regs->maccfg1, maccfg1);
230 uec->mac_rx_enabled = 0;
236 static int uec_graceful_stop_tx(uec_private_t *uec)
242 if (!uec || !uec->uccf) {
243 printf("%s: No handle passed.\n", __FUNCTION__);
247 uf_regs = uec->uccf->uf_regs;
249 /* Clear the grace stop event */
250 out_be32(&uf_regs->ucce, UCCE_GRA);
252 /* Issue host command */
254 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
255 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
256 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
258 /* Wait for command to complete */
260 ucce = in_be32(&uf_regs->ucce);
261 } while (! (ucce & UCCE_GRA));
263 uec->grace_stopped_tx = 1;
268 static int uec_graceful_stop_rx(uec_private_t *uec)
274 printf("%s: No handle passed.\n", __FUNCTION__);
278 if (!uec->p_rx_glbl_pram) {
279 printf("%s: No init rx global parameter\n", __FUNCTION__);
283 /* Clear acknowledge bit */
284 ack = uec->p_rx_glbl_pram->rxgstpack;
285 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
286 uec->p_rx_glbl_pram->rxgstpack = ack;
288 /* Keep issuing cmd and checking ack bit until it is asserted */
290 /* Issue host command */
292 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
293 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
294 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
295 ack = uec->p_rx_glbl_pram->rxgstpack;
296 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
298 uec->grace_stopped_rx = 1;
303 static int uec_restart_tx(uec_private_t *uec)
307 if (!uec || !uec->uec_info) {
308 printf("%s: No handle passed.\n", __FUNCTION__);
313 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
314 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
315 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
317 uec->grace_stopped_tx = 0;
322 static int uec_restart_rx(uec_private_t *uec)
326 if (!uec || !uec->uec_info) {
327 printf("%s: No handle passed.\n", __FUNCTION__);
332 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
333 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
334 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
336 uec->grace_stopped_rx = 0;
341 static int uec_open(uec_private_t *uec, comm_dir_e mode)
343 ucc_fast_private_t *uccf;
345 if (!uec || !uec->uccf) {
346 printf("%s: No handle passed.\n", __FUNCTION__);
351 /* check if the UCC number is in range. */
352 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
353 printf("%s: ucc_num out of range.\n", __FUNCTION__);
358 uec_mac_enable(uec, mode);
360 /* Enable UCC fast */
361 ucc_fast_enable(uccf, mode);
363 /* RISC microcode start */
364 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
367 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
374 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
376 ucc_fast_private_t *uccf;
378 if (!uec || !uec->uccf) {
379 printf("%s: No handle passed.\n", __FUNCTION__);
384 /* check if the UCC number is in range. */
385 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
386 printf("%s: ucc_num out of range.\n", __FUNCTION__);
389 /* Stop any transmissions */
390 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
391 uec_graceful_stop_tx(uec);
393 /* Stop any receptions */
394 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
395 uec_graceful_stop_rx(uec);
398 /* Disable the UCC fast */
399 ucc_fast_disable(uec->uccf, mode);
401 /* Disable the MAC */
402 uec_mac_disable(uec, mode);
407 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
413 printf("%s: uec not initial\n", __FUNCTION__);
416 uec_regs = uec->uec_regs;
418 if (duplex == DUPLEX_HALF) {
419 maccfg2 = in_be32(&uec_regs->maccfg2);
420 maccfg2 &= ~MACCFG2_FDX;
421 out_be32(&uec_regs->maccfg2, maccfg2);
424 if (duplex == DUPLEX_FULL) {
425 maccfg2 = in_be32(&uec_regs->maccfg2);
426 maccfg2 |= MACCFG2_FDX;
427 out_be32(&uec_regs->maccfg2, maccfg2);
433 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
435 enet_interface_e enet_if_mode;
436 uec_info_t *uec_info;
442 printf("%s: uec not initial\n", __FUNCTION__);
446 uec_info = uec->uec_info;
447 uec_regs = uec->uec_regs;
448 enet_if_mode = if_mode;
450 maccfg2 = in_be32(&uec_regs->maccfg2);
451 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
453 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
454 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
456 switch (enet_if_mode) {
459 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
462 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
465 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
469 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
470 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
472 case ENET_1000_RGMII_RXID:
473 case ENET_1000_RGMII_ID:
474 case ENET_1000_RGMII:
475 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
479 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
483 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
484 upsmr |= (UPSMR_RPM | UPSMR_R10M);
487 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
491 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
492 upsmr |= (UPSMR_R10M | UPSMR_RMM);
498 out_be32(&uec_regs->maccfg2, maccfg2);
499 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
504 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
506 uint timeout = 0x1000;
509 miimcfg = in_be32(&uec_mii_regs->miimcfg);
510 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
511 out_be32(&uec_mii_regs->miimcfg, miimcfg);
513 /* Wait until the bus is free */
514 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
516 printf("%s: The MII Bus is stuck!", __FUNCTION__);
523 static int init_phy(struct eth_device *dev)
526 uec_mii_t *umii_regs;
527 struct uec_mii_info *mii_info;
528 struct phy_info *curphy;
531 uec = (uec_private_t *)dev->priv;
532 umii_regs = uec->uec_mii_regs;
538 mii_info = malloc(sizeof(*mii_info));
540 printf("%s: Could not allocate mii_info", dev->name);
543 memset(mii_info, 0, sizeof(*mii_info));
545 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
546 mii_info->speed = SPEED_1000;
548 mii_info->speed = SPEED_100;
551 mii_info->duplex = DUPLEX_FULL;
555 mii_info->advertising = (ADVERTISED_10baseT_Half |
556 ADVERTISED_10baseT_Full |
557 ADVERTISED_100baseT_Half |
558 ADVERTISED_100baseT_Full |
559 ADVERTISED_1000baseT_Full);
560 mii_info->autoneg = 1;
561 mii_info->mii_id = uec->uec_info->phy_address;
564 mii_info->mdio_read = &uec_read_phy_reg;
565 mii_info->mdio_write = &uec_write_phy_reg;
567 uec->mii_info = mii_info;
569 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
571 if (init_mii_management_configuration(umii_regs)) {
572 printf("%s: The MII Bus is stuck!", dev->name);
577 /* get info for this PHY */
578 curphy = uec_get_phy_info(uec->mii_info);
580 printf("%s: No PHY found", dev->name);
585 mii_info->phyinfo = curphy;
587 /* Run the commands which initialize the PHY */
589 err = curphy->init(uec->mii_info);
603 static void adjust_link(struct eth_device *dev)
605 uec_private_t *uec = (uec_private_t *)dev->priv;
607 struct uec_mii_info *mii_info = uec->mii_info;
609 extern void change_phy_interface_mode(struct eth_device *dev,
610 enet_interface_e mode);
611 uec_regs = uec->uec_regs;
613 if (mii_info->link) {
614 /* Now we make sure that we can be in full duplex mode.
615 * If not, we operate in half-duplex mode. */
616 if (mii_info->duplex != uec->oldduplex) {
617 if (!(mii_info->duplex)) {
618 uec_set_mac_duplex(uec, DUPLEX_HALF);
619 printf("%s: Half Duplex\n", dev->name);
621 uec_set_mac_duplex(uec, DUPLEX_FULL);
622 printf("%s: Full Duplex\n", dev->name);
624 uec->oldduplex = mii_info->duplex;
627 if (mii_info->speed != uec->oldspeed) {
628 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
629 switch (mii_info->speed) {
633 printf ("switching to rgmii 100\n");
634 /* change phy to rgmii 100 */
635 change_phy_interface_mode(dev,
637 /* change the MAC interface mode */
638 uec_set_mac_if_mode(uec,ENET_100_RGMII);
641 printf ("switching to rgmii 10\n");
642 /* change phy to rgmii 10 */
643 change_phy_interface_mode(dev,
645 /* change the MAC interface mode */
646 uec_set_mac_if_mode(uec,ENET_10_RGMII);
649 printf("%s: Ack,Speed(%d)is illegal\n",
650 dev->name, mii_info->speed);
655 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
656 uec->oldspeed = mii_info->speed;
660 printf("%s: Link is up\n", dev->name);
664 } else { /* if (mii_info->link) */
666 printf("%s: Link is down\n", dev->name);
674 static void phy_change(struct eth_device *dev)
676 uec_private_t *uec = (uec_private_t *)dev->priv;
678 /* Update the link, speed, duplex */
679 uec->mii_info->phyinfo->read_status(uec->mii_info);
681 /* Adjust the interface according to speed */
685 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
686 && !defined(BITBANGMII)
689 * Find a device index from the devlist by name
692 * The index where the device is located, -1 on error
694 static int uec_miiphy_find_dev_by_name(char *devname)
698 for (i = 0; i < MAXCONTROLLERS; i++) {
699 if (strncmp(devname, devlist[i]->name, strlen(devname)) == 0) {
704 /* If device cannot be found, returns -1 */
705 if (i == MAXCONTROLLERS) {
706 debug ("%s: device %s not found in devlist\n", __FUNCTION__, devname);
714 * Read a MII PHY register.
719 static int uec_miiphy_read(char *devname, unsigned char addr,
720 unsigned char reg, unsigned short *value)
724 if (devname == NULL || value == NULL) {
725 debug("%s: NULL pointer given\n", __FUNCTION__);
727 devindex = uec_miiphy_find_dev_by_name(devname);
729 *value = uec_read_phy_reg(devlist[devindex], addr, reg);
736 * Write a MII PHY register.
741 static int uec_miiphy_write(char *devname, unsigned char addr,
742 unsigned char reg, unsigned short value)
746 if (devname == NULL) {
747 debug("%s: NULL pointer given\n", __FUNCTION__);
749 devindex = uec_miiphy_find_dev_by_name(devname);
751 uec_write_phy_reg(devlist[devindex], addr, reg, value);
758 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
765 printf("%s: uec not initial\n", __FUNCTION__);
769 uec_regs = uec->uec_regs;
771 /* if a station address of 0x12345678ABCD, perform a write to
772 MACSTNADDR1 of 0xCDAB7856,
773 MACSTNADDR2 of 0x34120000 */
775 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
776 (mac_addr[3] << 8) | (mac_addr[2]);
777 out_be32(&uec_regs->macstnaddr1, mac_addr1);
779 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
780 out_be32(&uec_regs->macstnaddr2, mac_addr2);
785 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
786 int *threads_num_ret)
788 int num_threads_numerica;
790 switch (threads_num) {
791 case UEC_NUM_OF_THREADS_1:
792 num_threads_numerica = 1;
794 case UEC_NUM_OF_THREADS_2:
795 num_threads_numerica = 2;
797 case UEC_NUM_OF_THREADS_4:
798 num_threads_numerica = 4;
800 case UEC_NUM_OF_THREADS_6:
801 num_threads_numerica = 6;
803 case UEC_NUM_OF_THREADS_8:
804 num_threads_numerica = 8;
807 printf("%s: Bad number of threads value.",
812 *threads_num_ret = num_threads_numerica;
817 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
819 uec_info_t *uec_info;
824 uec_info = uec->uec_info;
826 /* Alloc global Tx parameter RAM page */
827 uec->tx_glbl_pram_offset = qe_muram_alloc(
828 sizeof(uec_tx_global_pram_t),
829 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
830 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
831 qe_muram_addr(uec->tx_glbl_pram_offset);
833 /* Zero the global Tx prameter RAM */
834 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
836 /* Init global Tx parameter RAM */
838 /* TEMODER, RMON statistics disable, one Tx queue */
839 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
842 uec->send_q_mem_reg_offset = qe_muram_alloc(
843 sizeof(uec_send_queue_qd_t),
844 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
845 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
846 qe_muram_addr(uec->send_q_mem_reg_offset);
847 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
849 /* Setup the table with TxBDs ring */
850 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
852 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
853 (u32)(uec->p_tx_bd_ring));
854 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
857 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
858 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
860 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
861 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
863 /* TSTATE, global snooping, big endian, the CSB bus selected */
864 bmrx = BMR_INIT_VALUE;
865 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
868 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
869 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
873 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
874 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
878 uec->thread_dat_tx_offset = qe_muram_alloc(
879 num_threads_tx * sizeof(uec_thread_data_tx_t) +
880 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
882 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
883 qe_muram_addr(uec->thread_dat_tx_offset);
884 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
887 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
891 uec_82xx_address_filtering_pram_t *p_af_pram;
893 /* Allocate global Rx parameter RAM page */
894 uec->rx_glbl_pram_offset = qe_muram_alloc(
895 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
896 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
897 qe_muram_addr(uec->rx_glbl_pram_offset);
899 /* Zero Global Rx parameter RAM */
900 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
902 /* Init global Rx parameter RAM */
903 /* REMODER, Extended feature mode disable, VLAN disable,
904 LossLess flow control disable, Receive firmware statisic disable,
905 Extended address parsing mode disable, One Rx queues,
906 Dynamic maximum/minimum frame length disable, IP checksum check
907 disable, IP address alignment disable
909 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
912 uec->thread_dat_rx_offset = qe_muram_alloc(
913 num_threads_rx * sizeof(uec_thread_data_rx_t),
914 UEC_THREAD_DATA_ALIGNMENT);
915 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
916 qe_muram_addr(uec->thread_dat_rx_offset);
917 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
920 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
922 /* RxRMON base pointer, we don't need it */
923 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
925 /* IntCoalescingPTR, we don't need it, no interrupt */
926 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
928 /* RSTATE, global snooping, big endian, the CSB bus selected */
929 bmrx = BMR_INIT_VALUE;
930 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
933 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
936 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
937 sizeof(uec_rx_bd_queues_entry_t) + \
938 sizeof(uec_rx_prefetched_bds_t),
939 UEC_RX_BD_QUEUES_ALIGNMENT);
940 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
941 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
944 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
945 sizeof(uec_rx_prefetched_bds_t));
946 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
947 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
948 (u32)uec->p_rx_bd_ring);
951 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
953 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
955 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
957 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
959 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
961 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
963 for (i = 0; i < 8; i++) {
964 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
968 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
970 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
972 /* Clear PQ2 style address filtering hash table */
973 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
974 uec->p_rx_glbl_pram->addressfiltering;
976 p_af_pram->iaddr_h = 0;
977 p_af_pram->iaddr_l = 0;
978 p_af_pram->gaddr_h = 0;
979 p_af_pram->gaddr_l = 0;
982 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
983 int thread_tx, int thread_rx)
985 uec_init_cmd_pram_t *p_init_enet_param;
986 u32 init_enet_param_offset;
987 uec_info_t *uec_info;
990 u32 init_enet_offset;
995 uec_info = uec->uec_info;
997 /* Allocate init enet command parameter */
998 uec->init_enet_param_offset = qe_muram_alloc(
999 sizeof(uec_init_cmd_pram_t), 4);
1000 init_enet_param_offset = uec->init_enet_param_offset;
1001 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
1002 qe_muram_addr(uec->init_enet_param_offset);
1004 /* Zero init enet command struct */
1005 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
1007 /* Init the command struct */
1008 p_init_enet_param = uec->p_init_enet_param;
1009 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
1010 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
1011 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
1012 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
1013 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
1014 p_init_enet_param->largestexternallookupkeysize = 0;
1016 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
1017 << ENET_INIT_PARAM_RGF_SHIFT;
1018 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
1019 << ENET_INIT_PARAM_TGF_SHIFT;
1021 /* Init Rx global parameter pointer */
1022 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
1023 (u32)uec_info->riscRx;
1025 /* Init Rx threads */
1026 for (i = 0; i < (thread_rx + 1); i++) {
1027 if ((snum = qe_get_snum()) < 0) {
1028 printf("%s can not get snum\n", __FUNCTION__);
1033 init_enet_offset = 0;
1035 init_enet_offset = qe_muram_alloc(
1036 sizeof(uec_thread_rx_pram_t),
1037 UEC_THREAD_RX_PRAM_ALIGNMENT);
1040 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
1041 init_enet_offset | (u32)uec_info->riscRx;
1042 p_init_enet_param->rxthread[i] = entry_val;
1045 /* Init Tx global parameter pointer */
1046 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
1047 (u32)uec_info->riscTx;
1049 /* Init Tx threads */
1050 for (i = 0; i < thread_tx; i++) {
1051 if ((snum = qe_get_snum()) < 0) {
1052 printf("%s can not get snum\n", __FUNCTION__);
1056 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
1057 UEC_THREAD_TX_PRAM_ALIGNMENT);
1059 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
1060 init_enet_offset | (u32)uec_info->riscTx;
1061 p_init_enet_param->txthread[i] = entry_val;
1064 __asm__ __volatile__("sync");
1066 /* Issue QE command */
1067 command = QE_INIT_TX_RX;
1068 cecr_subblock = ucc_fast_get_qe_cr_subblock(
1069 uec->uec_info->uf_info.ucc_num);
1070 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
1071 init_enet_param_offset);
1076 static int uec_startup(uec_private_t *uec)
1078 uec_info_t *uec_info;
1079 ucc_fast_info_t *uf_info;
1080 ucc_fast_private_t *uccf;
1081 ucc_fast_t *uf_regs;
1086 enet_interface_e enet_interface;
1093 if (!uec || !uec->uec_info) {
1094 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
1098 uec_info = uec->uec_info;
1099 uf_info = &(uec_info->uf_info);
1101 /* Check if Rx BD ring len is illegal */
1102 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
1103 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
1104 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
1109 /* Check if Tx BD ring len is illegal */
1110 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
1111 printf("%s: Tx BD ring length must not be smaller than 2.\n",
1116 /* Check if MRBLR is illegal */
1117 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
1118 printf("%s: max rx buffer length must be mutliple of 128.\n",
1123 /* Both Rx and Tx are stopped */
1124 uec->grace_stopped_rx = 1;
1125 uec->grace_stopped_tx = 1;
1128 if (ucc_fast_init(uf_info, &uccf)) {
1129 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1136 /* Convert the Tx threads number */
1137 if (uec_convert_threads_num(uec_info->num_threads_tx,
1142 /* Convert the Rx threads number */
1143 if (uec_convert_threads_num(uec_info->num_threads_rx,
1148 uf_regs = uccf->uf_regs;
1150 /* UEC register is following UCC fast registers */
1151 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1153 /* Save the UEC register pointer to UEC private struct */
1154 uec->uec_regs = uec_regs;
1156 /* Init UPSMR, enable hardware statistics (UCC) */
1157 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1159 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1160 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1162 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1163 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1165 /* Setup MAC interface mode */
1166 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1168 /* Setup MII management base */
1169 #ifndef CONFIG_eTSEC_MDIO_BUS
1170 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1172 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1175 /* Setup MII master clock source */
1176 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1179 utbipar = in_be32(&uec_regs->utbipar);
1180 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1181 enet_interface = uec->uec_info->enet_interface;
1182 if (enet_interface == ENET_1000_TBI ||
1183 enet_interface == ENET_1000_RTBI) {
1184 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1185 << UTBIPAR_PHY_ADDRESS_SHIFT;
1187 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1188 << UTBIPAR_PHY_ADDRESS_SHIFT;
1191 out_be32(&uec_regs->utbipar, utbipar);
1193 /* Allocate Tx BDs */
1194 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1195 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1196 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1197 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1198 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1199 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1202 align = UEC_TX_BD_RING_ALIGNMENT;
1203 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1204 if (uec->tx_bd_ring_offset != 0) {
1205 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1209 /* Zero all of Tx BDs */
1210 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1212 /* Allocate Rx BDs */
1213 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1214 align = UEC_RX_BD_RING_ALIGNMENT;
1215 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1216 if (uec->rx_bd_ring_offset != 0) {
1217 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1221 /* Zero all of Rx BDs */
1222 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1224 /* Allocate Rx buffer */
1225 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1226 align = UEC_RX_DATA_BUF_ALIGNMENT;
1227 uec->rx_buf_offset = (u32)malloc(length + align);
1228 if (uec->rx_buf_offset != 0) {
1229 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1233 /* Zero all of the Rx buffer */
1234 memset((void *)(uec->rx_buf_offset), 0, length + align);
1236 /* Init TxBD ring */
1237 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1240 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1242 BD_STATUS_SET(bd, 0);
1243 BD_LENGTH_SET(bd, 0);
1246 BD_STATUS_SET((--bd), TxBD_WRAP);
1248 /* Init RxBD ring */
1249 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1251 buf = uec->p_rx_buf;
1252 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1253 BD_DATA_SET(bd, buf);
1254 BD_LENGTH_SET(bd, 0);
1255 BD_STATUS_SET(bd, RxBD_EMPTY);
1256 buf += MAX_RXBUF_LEN;
1259 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1261 /* Init global Tx parameter RAM */
1262 uec_init_tx_parameter(uec, num_threads_tx);
1264 /* Init global Rx parameter RAM */
1265 uec_init_rx_parameter(uec, num_threads_rx);
1267 /* Init ethernet Tx and Rx parameter command */
1268 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1270 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1277 static int uec_init(struct eth_device* dev, bd_t *bd)
1281 struct phy_info *curphy;
1283 uec = (uec_private_t *)dev->priv;
1285 if (uec->the_first_run == 0) {
1286 err = init_phy(dev);
1288 printf("%s: Cannot initialize PHY, aborting.\n",
1293 curphy = uec->mii_info->phyinfo;
1295 if (curphy->config_aneg) {
1296 err = curphy->config_aneg(uec->mii_info);
1298 printf("%s: Can't negotiate PHY\n", dev->name);
1303 /* Give PHYs up to 5 sec to report a link */
1306 err = curphy->read_status(uec->mii_info);
1308 } while (((i-- > 0) && !uec->mii_info->link) || err);
1311 printf("warning: %s: timeout on PHY link\n", dev->name);
1313 uec->the_first_run = 1;
1316 /* Set up the MAC address */
1317 if (dev->enetaddr[0] & 0x01) {
1318 printf("%s: MacAddress is multcast address\n",
1322 uec_set_mac_address(uec, dev->enetaddr);
1325 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1327 printf("%s: cannot enable UEC device\n", dev->name);
1333 return (uec->mii_info->link ? 0 : -1);
1336 static void uec_halt(struct eth_device* dev)
1338 uec_private_t *uec = (uec_private_t *)dev->priv;
1339 uec_stop(uec, COMM_DIR_RX_AND_TX);
1342 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1345 ucc_fast_private_t *uccf;
1346 volatile qe_bd_t *bd;
1351 uec = (uec_private_t *)dev->priv;
1355 /* Find an empty TxBD */
1356 for (i = 0; bd->status & TxBD_READY; i++) {
1358 printf("%s: tx buffer not ready\n", dev->name);
1364 BD_DATA_SET(bd, buf);
1365 BD_LENGTH_SET(bd, len);
1366 status = bd->status;
1368 status |= (TxBD_READY | TxBD_LAST);
1369 BD_STATUS_SET(bd, status);
1371 /* Tell UCC to transmit the buffer */
1372 ucc_fast_transmit_on_demand(uccf);
1374 /* Wait for buffer to be transmitted */
1375 for (i = 0; bd->status & TxBD_READY; i++) {
1377 printf("%s: tx error\n", dev->name);
1382 /* Ok, the buffer be transimitted */
1383 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1390 static int uec_recv(struct eth_device* dev)
1392 uec_private_t *uec = dev->priv;
1393 volatile qe_bd_t *bd;
1399 status = bd->status;
1401 while (!(status & RxBD_EMPTY)) {
1402 if (!(status & RxBD_ERROR)) {
1404 len = BD_LENGTH(bd);
1405 NetReceive(data, len);
1407 printf("%s: Rx error\n", dev->name);
1410 BD_LENGTH_SET(bd, 0);
1411 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1412 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1413 status = bd->status;
1420 int uec_initialize(int index)
1422 struct eth_device *dev;
1425 uec_info_t *uec_info;
1428 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1431 memset(dev, 0, sizeof(struct eth_device));
1433 /* Allocate the UEC private struct */
1434 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1438 memset(uec, 0, sizeof(uec_private_t));
1440 /* Init UEC private struct, they come from board.h */
1443 #ifdef CONFIG_UEC_ETH1
1444 uec_info = ð1_uec_info;
1446 } else if (index == 1) {
1447 #ifdef CONFIG_UEC_ETH2
1448 uec_info = ð2_uec_info;
1450 } else if (index == 2) {
1451 #ifdef CONFIG_UEC_ETH3
1452 uec_info = ð3_uec_info;
1454 } else if (index == 3) {
1455 #ifdef CONFIG_UEC_ETH4
1456 uec_info = ð4_uec_info;
1458 } else if (index == 4) {
1459 #ifdef CONFIG_UEC_ETH5
1460 uec_info = ð5_uec_info;
1462 } else if (index == 5) {
1463 #ifdef CONFIG_UEC_ETH6
1464 uec_info = ð6_uec_info;
1467 printf("%s: index is illegal.\n", __FUNCTION__);
1471 devlist[index] = dev;
1473 uec->uec_info = uec_info;
1475 sprintf(dev->name, "FSL UEC%d", index);
1477 dev->priv = (void *)uec;
1478 dev->init = uec_init;
1479 dev->halt = uec_halt;
1480 dev->send = uec_send;
1481 dev->recv = uec_recv;
1483 /* Clear the ethnet address */
1484 for (i = 0; i < 6; i++)
1485 dev->enetaddr[i] = 0;
1489 err = uec_startup(uec);
1491 printf("%s: Cannot configure net device, aborting.",dev->name);
1495 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1496 && !defined(BITBANGMII)
1497 miiphy_register(dev->name, uec_miiphy_read, uec_miiphy_write);