2 * Copyright (C) 2006-2011 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 /* Default UTBIPAR SMI address */
35 #ifndef CONFIG_UTBIPAR_INIT_TBIPA
36 #define CONFIG_UTBIPAR_INIT_TBIPA 0x1F
39 static uec_info_t uec_info[] = {
40 #ifdef CONFIG_UEC_ETH1
41 STD_UEC_INFO(1), /* UEC1 */
43 #ifdef CONFIG_UEC_ETH2
44 STD_UEC_INFO(2), /* UEC2 */
46 #ifdef CONFIG_UEC_ETH3
47 STD_UEC_INFO(3), /* UEC3 */
49 #ifdef CONFIG_UEC_ETH4
50 STD_UEC_INFO(4), /* UEC4 */
52 #ifdef CONFIG_UEC_ETH5
53 STD_UEC_INFO(5), /* UEC5 */
55 #ifdef CONFIG_UEC_ETH6
56 STD_UEC_INFO(6), /* UEC6 */
58 #ifdef CONFIG_UEC_ETH7
59 STD_UEC_INFO(7), /* UEC7 */
61 #ifdef CONFIG_UEC_ETH8
62 STD_UEC_INFO(8), /* UEC8 */
66 #define MAXCONTROLLERS (8)
68 static struct eth_device *devlist[MAXCONTROLLERS];
70 u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
71 void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
73 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
79 printf("%s: uec not initial\n", __FUNCTION__);
82 uec_regs = uec->uec_regs;
84 maccfg1 = in_be32(&uec_regs->maccfg1);
86 if (mode & COMM_DIR_TX) {
87 maccfg1 |= MACCFG1_ENABLE_TX;
88 out_be32(&uec_regs->maccfg1, maccfg1);
89 uec->mac_tx_enabled = 1;
92 if (mode & COMM_DIR_RX) {
93 maccfg1 |= MACCFG1_ENABLE_RX;
94 out_be32(&uec_regs->maccfg1, maccfg1);
95 uec->mac_rx_enabled = 1;
101 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
107 printf("%s: uec not initial\n", __FUNCTION__);
110 uec_regs = uec->uec_regs;
112 maccfg1 = in_be32(&uec_regs->maccfg1);
114 if (mode & COMM_DIR_TX) {
115 maccfg1 &= ~MACCFG1_ENABLE_TX;
116 out_be32(&uec_regs->maccfg1, maccfg1);
117 uec->mac_tx_enabled = 0;
120 if (mode & COMM_DIR_RX) {
121 maccfg1 &= ~MACCFG1_ENABLE_RX;
122 out_be32(&uec_regs->maccfg1, maccfg1);
123 uec->mac_rx_enabled = 0;
129 static int uec_graceful_stop_tx(uec_private_t *uec)
135 if (!uec || !uec->uccf) {
136 printf("%s: No handle passed.\n", __FUNCTION__);
140 uf_regs = uec->uccf->uf_regs;
142 /* Clear the grace stop event */
143 out_be32(&uf_regs->ucce, UCCE_GRA);
145 /* Issue host command */
147 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
148 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
149 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
151 /* Wait for command to complete */
153 ucce = in_be32(&uf_regs->ucce);
154 } while (! (ucce & UCCE_GRA));
156 uec->grace_stopped_tx = 1;
161 static int uec_graceful_stop_rx(uec_private_t *uec)
167 printf("%s: No handle passed.\n", __FUNCTION__);
171 if (!uec->p_rx_glbl_pram) {
172 printf("%s: No init rx global parameter\n", __FUNCTION__);
176 /* Clear acknowledge bit */
177 ack = uec->p_rx_glbl_pram->rxgstpack;
178 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
179 uec->p_rx_glbl_pram->rxgstpack = ack;
181 /* Keep issuing cmd and checking ack bit until it is asserted */
183 /* Issue host command */
185 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
186 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
187 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
188 ack = uec->p_rx_glbl_pram->rxgstpack;
189 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
191 uec->grace_stopped_rx = 1;
196 static int uec_restart_tx(uec_private_t *uec)
200 if (!uec || !uec->uec_info) {
201 printf("%s: No handle passed.\n", __FUNCTION__);
206 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
207 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
208 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
210 uec->grace_stopped_tx = 0;
215 static int uec_restart_rx(uec_private_t *uec)
219 if (!uec || !uec->uec_info) {
220 printf("%s: No handle passed.\n", __FUNCTION__);
225 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
226 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
227 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
229 uec->grace_stopped_rx = 0;
234 static int uec_open(uec_private_t *uec, comm_dir_e mode)
236 ucc_fast_private_t *uccf;
238 if (!uec || !uec->uccf) {
239 printf("%s: No handle passed.\n", __FUNCTION__);
244 /* check if the UCC number is in range. */
245 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
246 printf("%s: ucc_num out of range.\n", __FUNCTION__);
251 uec_mac_enable(uec, mode);
253 /* Enable UCC fast */
254 ucc_fast_enable(uccf, mode);
256 /* RISC microcode start */
257 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
260 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
267 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
269 ucc_fast_private_t *uccf;
271 if (!uec || !uec->uccf) {
272 printf("%s: No handle passed.\n", __FUNCTION__);
277 /* check if the UCC number is in range. */
278 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
279 printf("%s: ucc_num out of range.\n", __FUNCTION__);
282 /* Stop any transmissions */
283 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
284 uec_graceful_stop_tx(uec);
286 /* Stop any receptions */
287 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
288 uec_graceful_stop_rx(uec);
291 /* Disable the UCC fast */
292 ucc_fast_disable(uec->uccf, mode);
294 /* Disable the MAC */
295 uec_mac_disable(uec, mode);
300 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
306 printf("%s: uec not initial\n", __FUNCTION__);
309 uec_regs = uec->uec_regs;
311 if (duplex == DUPLEX_HALF) {
312 maccfg2 = in_be32(&uec_regs->maccfg2);
313 maccfg2 &= ~MACCFG2_FDX;
314 out_be32(&uec_regs->maccfg2, maccfg2);
317 if (duplex == DUPLEX_FULL) {
318 maccfg2 = in_be32(&uec_regs->maccfg2);
319 maccfg2 |= MACCFG2_FDX;
320 out_be32(&uec_regs->maccfg2, maccfg2);
326 static int uec_set_mac_if_mode(uec_private_t *uec,
327 enum fsl_phy_enet_if if_mode, int speed)
329 enum fsl_phy_enet_if enet_if_mode;
330 uec_info_t *uec_info;
336 printf("%s: uec not initial\n", __FUNCTION__);
340 uec_info = uec->uec_info;
341 uec_regs = uec->uec_regs;
342 enet_if_mode = if_mode;
344 maccfg2 = in_be32(&uec_regs->maccfg2);
345 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
347 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
348 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
352 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
353 switch (enet_if_mode) {
357 upsmr |= (UPSMR_RPM | UPSMR_R10M);
360 upsmr |= (UPSMR_R10M | UPSMR_RMM);
368 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
369 switch (enet_if_mode) {
384 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
385 switch (enet_if_mode) {
392 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
412 out_be32(&uec_regs->maccfg2, maccfg2);
413 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
418 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
420 uint timeout = 0x1000;
423 miimcfg = in_be32(&uec_mii_regs->miimcfg);
424 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
425 out_be32(&uec_mii_regs->miimcfg, miimcfg);
427 /* Wait until the bus is free */
428 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
430 printf("%s: The MII Bus is stuck!", __FUNCTION__);
437 static int init_phy(struct eth_device *dev)
440 uec_mii_t *umii_regs;
441 struct uec_mii_info *mii_info;
442 struct phy_info *curphy;
445 uec = (uec_private_t *)dev->priv;
446 umii_regs = uec->uec_mii_regs;
452 mii_info = malloc(sizeof(*mii_info));
454 printf("%s: Could not allocate mii_info", dev->name);
457 memset(mii_info, 0, sizeof(*mii_info));
459 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
460 mii_info->speed = SPEED_1000;
462 mii_info->speed = SPEED_100;
465 mii_info->duplex = DUPLEX_FULL;
469 mii_info->advertising = (ADVERTISED_10baseT_Half |
470 ADVERTISED_10baseT_Full |
471 ADVERTISED_100baseT_Half |
472 ADVERTISED_100baseT_Full |
473 ADVERTISED_1000baseT_Full);
474 mii_info->autoneg = 1;
475 mii_info->mii_id = uec->uec_info->phy_address;
478 mii_info->mdio_read = &uec_read_phy_reg;
479 mii_info->mdio_write = &uec_write_phy_reg;
481 uec->mii_info = mii_info;
483 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
485 if (init_mii_management_configuration(umii_regs)) {
486 printf("%s: The MII Bus is stuck!", dev->name);
491 /* get info for this PHY */
492 curphy = uec_get_phy_info(uec->mii_info);
494 printf("%s: No PHY found", dev->name);
499 mii_info->phyinfo = curphy;
501 /* Run the commands which initialize the PHY */
503 err = curphy->init(uec->mii_info);
517 static void adjust_link(struct eth_device *dev)
519 uec_private_t *uec = (uec_private_t *)dev->priv;
521 struct uec_mii_info *mii_info = uec->mii_info;
523 extern void change_phy_interface_mode(struct eth_device *dev,
524 enum fsl_phy_enet_if mode, int speed);
525 uec_regs = uec->uec_regs;
527 if (mii_info->link) {
528 /* Now we make sure that we can be in full duplex mode.
529 * If not, we operate in half-duplex mode. */
530 if (mii_info->duplex != uec->oldduplex) {
531 if (!(mii_info->duplex)) {
532 uec_set_mac_duplex(uec, DUPLEX_HALF);
533 printf("%s: Half Duplex\n", dev->name);
535 uec_set_mac_duplex(uec, DUPLEX_FULL);
536 printf("%s: Full Duplex\n", dev->name);
538 uec->oldduplex = mii_info->duplex;
541 if (mii_info->speed != uec->oldspeed) {
542 enum fsl_phy_enet_if mode = \
543 uec->uec_info->enet_interface_type;
544 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
545 switch (mii_info->speed) {
549 printf ("switching to rgmii 100\n");
553 printf ("switching to rgmii 10\n");
557 printf("%s: Ack,Speed(%d)is illegal\n",
558 dev->name, mii_info->speed);
564 change_phy_interface_mode(dev, mode, mii_info->speed);
565 /* change the MAC interface mode */
566 uec_set_mac_if_mode(uec, mode, mii_info->speed);
568 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
569 uec->oldspeed = mii_info->speed;
573 printf("%s: Link is up\n", dev->name);
577 } else { /* if (mii_info->link) */
579 printf("%s: Link is down\n", dev->name);
587 static void phy_change(struct eth_device *dev)
589 uec_private_t *uec = (uec_private_t *)dev->priv;
591 #if defined(CONFIG_P1012) || defined(CONFIG_P1016) || \
592 defined(CONFIG_P1021) || defined(CONFIG_P1025)
593 ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
595 /* QE9 and QE12 need to be set for enabling QE MII managment signals */
596 setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE9);
597 setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE12);
600 /* Update the link, speed, duplex */
601 uec->mii_info->phyinfo->read_status(uec->mii_info);
603 #if defined(CONFIG_P1012) || defined(CONFIG_P1016) || \
604 defined(CONFIG_P1021) || defined(CONFIG_P1025)
606 * QE12 is muxed with LBCTL, it needs to be released for enabling
607 * LBCTL signal for LBC usage.
609 clrbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE12);
612 /* Adjust the interface according to speed */
616 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
619 * Find a device index from the devlist by name
622 * The index where the device is located, -1 on error
624 static int uec_miiphy_find_dev_by_name(const char *devname)
628 for (i = 0; i < MAXCONTROLLERS; i++) {
629 if (strncmp(devname, devlist[i]->name, strlen(devname)) == 0) {
634 /* If device cannot be found, returns -1 */
635 if (i == MAXCONTROLLERS) {
636 debug ("%s: device %s not found in devlist\n", __FUNCTION__, devname);
644 * Read a MII PHY register.
649 static int uec_miiphy_read(const char *devname, unsigned char addr,
650 unsigned char reg, unsigned short *value)
654 if (devname == NULL || value == NULL) {
655 debug("%s: NULL pointer given\n", __FUNCTION__);
657 devindex = uec_miiphy_find_dev_by_name(devname);
659 *value = uec_read_phy_reg(devlist[devindex], addr, reg);
666 * Write a MII PHY register.
671 static int uec_miiphy_write(const char *devname, unsigned char addr,
672 unsigned char reg, unsigned short value)
676 if (devname == NULL) {
677 debug("%s: NULL pointer given\n", __FUNCTION__);
679 devindex = uec_miiphy_find_dev_by_name(devname);
681 uec_write_phy_reg(devlist[devindex], addr, reg, value);
688 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
695 printf("%s: uec not initial\n", __FUNCTION__);
699 uec_regs = uec->uec_regs;
701 /* if a station address of 0x12345678ABCD, perform a write to
702 MACSTNADDR1 of 0xCDAB7856,
703 MACSTNADDR2 of 0x34120000 */
705 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
706 (mac_addr[3] << 8) | (mac_addr[2]);
707 out_be32(&uec_regs->macstnaddr1, mac_addr1);
709 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
710 out_be32(&uec_regs->macstnaddr2, mac_addr2);
715 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
716 int *threads_num_ret)
718 int num_threads_numerica;
720 switch (threads_num) {
721 case UEC_NUM_OF_THREADS_1:
722 num_threads_numerica = 1;
724 case UEC_NUM_OF_THREADS_2:
725 num_threads_numerica = 2;
727 case UEC_NUM_OF_THREADS_4:
728 num_threads_numerica = 4;
730 case UEC_NUM_OF_THREADS_6:
731 num_threads_numerica = 6;
733 case UEC_NUM_OF_THREADS_8:
734 num_threads_numerica = 8;
737 printf("%s: Bad number of threads value.",
742 *threads_num_ret = num_threads_numerica;
747 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
749 uec_info_t *uec_info;
754 uec_info = uec->uec_info;
756 /* Alloc global Tx parameter RAM page */
757 uec->tx_glbl_pram_offset = qe_muram_alloc(
758 sizeof(uec_tx_global_pram_t),
759 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
760 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
761 qe_muram_addr(uec->tx_glbl_pram_offset);
763 /* Zero the global Tx prameter RAM */
764 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
766 /* Init global Tx parameter RAM */
768 /* TEMODER, RMON statistics disable, one Tx queue */
769 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
772 uec->send_q_mem_reg_offset = qe_muram_alloc(
773 sizeof(uec_send_queue_qd_t),
774 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
775 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
776 qe_muram_addr(uec->send_q_mem_reg_offset);
777 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
779 /* Setup the table with TxBDs ring */
780 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
782 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
783 (u32)(uec->p_tx_bd_ring));
784 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
787 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
788 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
790 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
791 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
793 /* TSTATE, global snooping, big endian, the CSB bus selected */
794 bmrx = BMR_INIT_VALUE;
795 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
798 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
799 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
803 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
804 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
808 uec->thread_dat_tx_offset = qe_muram_alloc(
809 num_threads_tx * sizeof(uec_thread_data_tx_t) +
810 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
812 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
813 qe_muram_addr(uec->thread_dat_tx_offset);
814 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
817 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
821 uec_82xx_address_filtering_pram_t *p_af_pram;
823 /* Allocate global Rx parameter RAM page */
824 uec->rx_glbl_pram_offset = qe_muram_alloc(
825 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
826 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
827 qe_muram_addr(uec->rx_glbl_pram_offset);
829 /* Zero Global Rx parameter RAM */
830 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
832 /* Init global Rx parameter RAM */
833 /* REMODER, Extended feature mode disable, VLAN disable,
834 LossLess flow control disable, Receive firmware statisic disable,
835 Extended address parsing mode disable, One Rx queues,
836 Dynamic maximum/minimum frame length disable, IP checksum check
837 disable, IP address alignment disable
839 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
842 uec->thread_dat_rx_offset = qe_muram_alloc(
843 num_threads_rx * sizeof(uec_thread_data_rx_t),
844 UEC_THREAD_DATA_ALIGNMENT);
845 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
846 qe_muram_addr(uec->thread_dat_rx_offset);
847 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
850 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
852 /* RxRMON base pointer, we don't need it */
853 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
855 /* IntCoalescingPTR, we don't need it, no interrupt */
856 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
858 /* RSTATE, global snooping, big endian, the CSB bus selected */
859 bmrx = BMR_INIT_VALUE;
860 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
863 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
866 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
867 sizeof(uec_rx_bd_queues_entry_t) + \
868 sizeof(uec_rx_prefetched_bds_t),
869 UEC_RX_BD_QUEUES_ALIGNMENT);
870 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
871 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
874 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
875 sizeof(uec_rx_prefetched_bds_t));
876 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
877 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
878 (u32)uec->p_rx_bd_ring);
881 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
883 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
885 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
887 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
889 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
891 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
893 for (i = 0; i < 8; i++) {
894 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
898 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
900 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
902 /* Clear PQ2 style address filtering hash table */
903 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
904 uec->p_rx_glbl_pram->addressfiltering;
906 p_af_pram->iaddr_h = 0;
907 p_af_pram->iaddr_l = 0;
908 p_af_pram->gaddr_h = 0;
909 p_af_pram->gaddr_l = 0;
912 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
913 int thread_tx, int thread_rx)
915 uec_init_cmd_pram_t *p_init_enet_param;
916 u32 init_enet_param_offset;
917 uec_info_t *uec_info;
920 u32 init_enet_offset;
925 uec_info = uec->uec_info;
927 /* Allocate init enet command parameter */
928 uec->init_enet_param_offset = qe_muram_alloc(
929 sizeof(uec_init_cmd_pram_t), 4);
930 init_enet_param_offset = uec->init_enet_param_offset;
931 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
932 qe_muram_addr(uec->init_enet_param_offset);
934 /* Zero init enet command struct */
935 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
937 /* Init the command struct */
938 p_init_enet_param = uec->p_init_enet_param;
939 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
940 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
941 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
942 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
943 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
944 p_init_enet_param->largestexternallookupkeysize = 0;
946 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
947 << ENET_INIT_PARAM_RGF_SHIFT;
948 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
949 << ENET_INIT_PARAM_TGF_SHIFT;
951 /* Init Rx global parameter pointer */
952 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
953 (u32)uec_info->risc_rx;
955 /* Init Rx threads */
956 for (i = 0; i < (thread_rx + 1); i++) {
957 if ((snum = qe_get_snum()) < 0) {
958 printf("%s can not get snum\n", __FUNCTION__);
963 init_enet_offset = 0;
965 init_enet_offset = qe_muram_alloc(
966 sizeof(uec_thread_rx_pram_t),
967 UEC_THREAD_RX_PRAM_ALIGNMENT);
970 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
971 init_enet_offset | (u32)uec_info->risc_rx;
972 p_init_enet_param->rxthread[i] = entry_val;
975 /* Init Tx global parameter pointer */
976 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
977 (u32)uec_info->risc_tx;
979 /* Init Tx threads */
980 for (i = 0; i < thread_tx; i++) {
981 if ((snum = qe_get_snum()) < 0) {
982 printf("%s can not get snum\n", __FUNCTION__);
986 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
987 UEC_THREAD_TX_PRAM_ALIGNMENT);
989 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
990 init_enet_offset | (u32)uec_info->risc_tx;
991 p_init_enet_param->txthread[i] = entry_val;
994 __asm__ __volatile__("sync");
996 /* Issue QE command */
997 command = QE_INIT_TX_RX;
998 cecr_subblock = ucc_fast_get_qe_cr_subblock(
999 uec->uec_info->uf_info.ucc_num);
1000 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
1001 init_enet_param_offset);
1006 static int uec_startup(uec_private_t *uec)
1008 uec_info_t *uec_info;
1009 ucc_fast_info_t *uf_info;
1010 ucc_fast_private_t *uccf;
1011 ucc_fast_t *uf_regs;
1022 if (!uec || !uec->uec_info) {
1023 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
1027 uec_info = uec->uec_info;
1028 uf_info = &(uec_info->uf_info);
1030 /* Check if Rx BD ring len is illegal */
1031 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
1032 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
1033 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
1038 /* Check if Tx BD ring len is illegal */
1039 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
1040 printf("%s: Tx BD ring length must not be smaller than 2.\n",
1045 /* Check if MRBLR is illegal */
1046 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
1047 printf("%s: max rx buffer length must be mutliple of 128.\n",
1052 /* Both Rx and Tx are stopped */
1053 uec->grace_stopped_rx = 1;
1054 uec->grace_stopped_tx = 1;
1057 if (ucc_fast_init(uf_info, &uccf)) {
1058 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1065 /* Convert the Tx threads number */
1066 if (uec_convert_threads_num(uec_info->num_threads_tx,
1071 /* Convert the Rx threads number */
1072 if (uec_convert_threads_num(uec_info->num_threads_rx,
1077 uf_regs = uccf->uf_regs;
1079 /* UEC register is following UCC fast registers */
1080 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1082 /* Save the UEC register pointer to UEC private struct */
1083 uec->uec_regs = uec_regs;
1085 /* Init UPSMR, enable hardware statistics (UCC) */
1086 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1088 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1089 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1091 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1092 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1094 /* Setup MAC interface mode */
1095 uec_set_mac_if_mode(uec, uec_info->enet_interface_type, uec_info->speed);
1097 /* Setup MII management base */
1098 #ifndef CONFIG_eTSEC_MDIO_BUS
1099 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1101 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1104 /* Setup MII master clock source */
1105 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1108 utbipar = in_be32(&uec_regs->utbipar);
1109 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1111 /* Initialize UTBIPAR address to CONFIG_UTBIPAR_INIT_TBIPA for ALL UEC.
1112 * This frees up the remaining SMI addresses for use.
1114 utbipar |= CONFIG_UTBIPAR_INIT_TBIPA << UTBIPAR_PHY_ADDRESS_SHIFT;
1115 out_be32(&uec_regs->utbipar, utbipar);
1117 /* Configure the TBI for SGMII operation */
1118 if ((uec->uec_info->enet_interface_type == SGMII) &&
1119 (uec->uec_info->speed == 1000)) {
1120 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1121 ENET_TBI_MII_ANA, TBIANA_SETTINGS);
1123 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1124 ENET_TBI_MII_TBICON, TBICON_CLK_SELECT);
1126 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1127 ENET_TBI_MII_CR, TBICR_SETTINGS);
1130 /* Allocate Tx BDs */
1131 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1132 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1133 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1134 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1135 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1136 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1139 align = UEC_TX_BD_RING_ALIGNMENT;
1140 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1141 if (uec->tx_bd_ring_offset != 0) {
1142 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1146 /* Zero all of Tx BDs */
1147 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1149 /* Allocate Rx BDs */
1150 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1151 align = UEC_RX_BD_RING_ALIGNMENT;
1152 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1153 if (uec->rx_bd_ring_offset != 0) {
1154 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1158 /* Zero all of Rx BDs */
1159 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1161 /* Allocate Rx buffer */
1162 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1163 align = UEC_RX_DATA_BUF_ALIGNMENT;
1164 uec->rx_buf_offset = (u32)malloc(length + align);
1165 if (uec->rx_buf_offset != 0) {
1166 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1170 /* Zero all of the Rx buffer */
1171 memset((void *)(uec->rx_buf_offset), 0, length + align);
1173 /* Init TxBD ring */
1174 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1177 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1179 BD_STATUS_SET(bd, 0);
1180 BD_LENGTH_SET(bd, 0);
1183 BD_STATUS_SET((--bd), TxBD_WRAP);
1185 /* Init RxBD ring */
1186 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1188 buf = uec->p_rx_buf;
1189 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1190 BD_DATA_SET(bd, buf);
1191 BD_LENGTH_SET(bd, 0);
1192 BD_STATUS_SET(bd, RxBD_EMPTY);
1193 buf += MAX_RXBUF_LEN;
1196 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1198 /* Init global Tx parameter RAM */
1199 uec_init_tx_parameter(uec, num_threads_tx);
1201 /* Init global Rx parameter RAM */
1202 uec_init_rx_parameter(uec, num_threads_rx);
1204 /* Init ethernet Tx and Rx parameter command */
1205 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1207 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1214 static int uec_init(struct eth_device* dev, bd_t *bd)
1218 struct phy_info *curphy;
1219 #if defined(CONFIG_P1012) || defined(CONFIG_P1016) || \
1220 defined(CONFIG_P1021) || defined(CONFIG_P1025)
1221 ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
1224 uec = (uec_private_t *)dev->priv;
1226 if (uec->the_first_run == 0) {
1227 #if defined(CONFIG_P1012) || defined(CONFIG_P1016) || \
1228 defined(CONFIG_P1021) || defined(CONFIG_P1025)
1229 /* QE9 and QE12 need to be set for enabling QE MII managment signals */
1230 setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE9);
1231 setbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE12);
1234 err = init_phy(dev);
1236 printf("%s: Cannot initialize PHY, aborting.\n",
1241 curphy = uec->mii_info->phyinfo;
1243 if (curphy->config_aneg) {
1244 err = curphy->config_aneg(uec->mii_info);
1246 printf("%s: Can't negotiate PHY\n", dev->name);
1251 /* Give PHYs up to 5 sec to report a link */
1254 err = curphy->read_status(uec->mii_info);
1255 if (!(((i-- > 0) && !uec->mii_info->link) || err))
1260 #if defined(CONFIG_P1012) || defined(CONFIG_P1016) || \
1261 defined(CONFIG_P1021) || defined(CONFIG_P1025)
1262 /* QE12 needs to be released for enabling LBCTL signal*/
1263 clrbits_be32(&gur->pmuxcr, MPC85xx_PMUXCR_QE12);
1267 printf("warning: %s: timeout on PHY link\n", dev->name);
1270 uec->the_first_run = 1;
1273 /* Set up the MAC address */
1274 if (dev->enetaddr[0] & 0x01) {
1275 printf("%s: MacAddress is multcast address\n",
1279 uec_set_mac_address(uec, dev->enetaddr);
1282 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1284 printf("%s: cannot enable UEC device\n", dev->name);
1290 return (uec->mii_info->link ? 0 : -1);
1293 static void uec_halt(struct eth_device* dev)
1295 uec_private_t *uec = (uec_private_t *)dev->priv;
1296 uec_stop(uec, COMM_DIR_RX_AND_TX);
1299 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1302 ucc_fast_private_t *uccf;
1303 volatile qe_bd_t *bd;
1308 uec = (uec_private_t *)dev->priv;
1312 /* Find an empty TxBD */
1313 for (i = 0; bd->status & TxBD_READY; i++) {
1315 printf("%s: tx buffer not ready\n", dev->name);
1321 BD_DATA_SET(bd, buf);
1322 BD_LENGTH_SET(bd, len);
1323 status = bd->status;
1325 status |= (TxBD_READY | TxBD_LAST);
1326 BD_STATUS_SET(bd, status);
1328 /* Tell UCC to transmit the buffer */
1329 ucc_fast_transmit_on_demand(uccf);
1331 /* Wait for buffer to be transmitted */
1332 for (i = 0; bd->status & TxBD_READY; i++) {
1334 printf("%s: tx error\n", dev->name);
1339 /* Ok, the buffer be transimitted */
1340 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1347 static int uec_recv(struct eth_device* dev)
1349 uec_private_t *uec = dev->priv;
1350 volatile qe_bd_t *bd;
1356 status = bd->status;
1358 while (!(status & RxBD_EMPTY)) {
1359 if (!(status & RxBD_ERROR)) {
1361 len = BD_LENGTH(bd);
1362 NetReceive(data, len);
1364 printf("%s: Rx error\n", dev->name);
1367 BD_LENGTH_SET(bd, 0);
1368 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1369 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1370 status = bd->status;
1377 int uec_initialize(bd_t *bis, uec_info_t *uec_info)
1379 struct eth_device *dev;
1384 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1387 memset(dev, 0, sizeof(struct eth_device));
1389 /* Allocate the UEC private struct */
1390 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1394 memset(uec, 0, sizeof(uec_private_t));
1396 /* Adjust uec_info */
1397 #if (MAX_QE_RISC == 4)
1398 uec_info->risc_tx = QE_RISC_ALLOCATION_FOUR_RISCS;
1399 uec_info->risc_rx = QE_RISC_ALLOCATION_FOUR_RISCS;
1402 devlist[uec_info->uf_info.ucc_num] = dev;
1404 uec->uec_info = uec_info;
1407 sprintf(dev->name, "UEC%d", uec_info->uf_info.ucc_num);
1409 dev->priv = (void *)uec;
1410 dev->init = uec_init;
1411 dev->halt = uec_halt;
1412 dev->send = uec_send;
1413 dev->recv = uec_recv;
1415 /* Clear the ethnet address */
1416 for (i = 0; i < 6; i++)
1417 dev->enetaddr[i] = 0;
1421 err = uec_startup(uec);
1423 printf("%s: Cannot configure net device, aborting.",dev->name);
1427 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
1428 miiphy_register(dev->name, uec_miiphy_read, uec_miiphy_write);
1434 int uec_eth_init(bd_t *bis, uec_info_t *uecs, int num)
1438 for (i = 0; i < num; i++)
1439 uec_initialize(bis, &uecs[i]);
1444 int uec_standard_init(bd_t *bis)
1446 return uec_eth_init(bis, uec_info, ARRAY_SIZE(uec_info));