2 * Copyright (C) 2006-2009 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 static uec_info_t uec_info[] = {
35 #ifdef CONFIG_UEC_ETH1
36 STD_UEC_INFO(1), /* UEC1 */
38 #ifdef CONFIG_UEC_ETH2
39 STD_UEC_INFO(2), /* UEC2 */
41 #ifdef CONFIG_UEC_ETH3
42 STD_UEC_INFO(3), /* UEC3 */
44 #ifdef CONFIG_UEC_ETH4
45 STD_UEC_INFO(4), /* UEC4 */
47 #ifdef CONFIG_UEC_ETH5
48 STD_UEC_INFO(5), /* UEC5 */
50 #ifdef CONFIG_UEC_ETH6
51 STD_UEC_INFO(6), /* UEC6 */
53 #ifdef CONFIG_UEC_ETH7
54 STD_UEC_INFO(7), /* UEC7 */
56 #ifdef CONFIG_UEC_ETH8
57 STD_UEC_INFO(8), /* UEC8 */
61 #define MAXCONTROLLERS (8)
63 static struct eth_device *devlist[MAXCONTROLLERS];
65 u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
66 void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
68 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
74 printf("%s: uec not initial\n", __FUNCTION__);
77 uec_regs = uec->uec_regs;
79 maccfg1 = in_be32(&uec_regs->maccfg1);
81 if (mode & COMM_DIR_TX) {
82 maccfg1 |= MACCFG1_ENABLE_TX;
83 out_be32(&uec_regs->maccfg1, maccfg1);
84 uec->mac_tx_enabled = 1;
87 if (mode & COMM_DIR_RX) {
88 maccfg1 |= MACCFG1_ENABLE_RX;
89 out_be32(&uec_regs->maccfg1, maccfg1);
90 uec->mac_rx_enabled = 1;
96 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
102 printf("%s: uec not initial\n", __FUNCTION__);
105 uec_regs = uec->uec_regs;
107 maccfg1 = in_be32(&uec_regs->maccfg1);
109 if (mode & COMM_DIR_TX) {
110 maccfg1 &= ~MACCFG1_ENABLE_TX;
111 out_be32(&uec_regs->maccfg1, maccfg1);
112 uec->mac_tx_enabled = 0;
115 if (mode & COMM_DIR_RX) {
116 maccfg1 &= ~MACCFG1_ENABLE_RX;
117 out_be32(&uec_regs->maccfg1, maccfg1);
118 uec->mac_rx_enabled = 0;
124 static int uec_graceful_stop_tx(uec_private_t *uec)
130 if (!uec || !uec->uccf) {
131 printf("%s: No handle passed.\n", __FUNCTION__);
135 uf_regs = uec->uccf->uf_regs;
137 /* Clear the grace stop event */
138 out_be32(&uf_regs->ucce, UCCE_GRA);
140 /* Issue host command */
142 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
143 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
144 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
146 /* Wait for command to complete */
148 ucce = in_be32(&uf_regs->ucce);
149 } while (! (ucce & UCCE_GRA));
151 uec->grace_stopped_tx = 1;
156 static int uec_graceful_stop_rx(uec_private_t *uec)
162 printf("%s: No handle passed.\n", __FUNCTION__);
166 if (!uec->p_rx_glbl_pram) {
167 printf("%s: No init rx global parameter\n", __FUNCTION__);
171 /* Clear acknowledge bit */
172 ack = uec->p_rx_glbl_pram->rxgstpack;
173 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
174 uec->p_rx_glbl_pram->rxgstpack = ack;
176 /* Keep issuing cmd and checking ack bit until it is asserted */
178 /* Issue host command */
180 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
181 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
182 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
183 ack = uec->p_rx_glbl_pram->rxgstpack;
184 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
186 uec->grace_stopped_rx = 1;
191 static int uec_restart_tx(uec_private_t *uec)
195 if (!uec || !uec->uec_info) {
196 printf("%s: No handle passed.\n", __FUNCTION__);
201 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
202 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
203 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
205 uec->grace_stopped_tx = 0;
210 static int uec_restart_rx(uec_private_t *uec)
214 if (!uec || !uec->uec_info) {
215 printf("%s: No handle passed.\n", __FUNCTION__);
220 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
221 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
222 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
224 uec->grace_stopped_rx = 0;
229 static int uec_open(uec_private_t *uec, comm_dir_e mode)
231 ucc_fast_private_t *uccf;
233 if (!uec || !uec->uccf) {
234 printf("%s: No handle passed.\n", __FUNCTION__);
239 /* check if the UCC number is in range. */
240 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
241 printf("%s: ucc_num out of range.\n", __FUNCTION__);
246 uec_mac_enable(uec, mode);
248 /* Enable UCC fast */
249 ucc_fast_enable(uccf, mode);
251 /* RISC microcode start */
252 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
255 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
262 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
264 ucc_fast_private_t *uccf;
266 if (!uec || !uec->uccf) {
267 printf("%s: No handle passed.\n", __FUNCTION__);
272 /* check if the UCC number is in range. */
273 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
274 printf("%s: ucc_num out of range.\n", __FUNCTION__);
277 /* Stop any transmissions */
278 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
279 uec_graceful_stop_tx(uec);
281 /* Stop any receptions */
282 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
283 uec_graceful_stop_rx(uec);
286 /* Disable the UCC fast */
287 ucc_fast_disable(uec->uccf, mode);
289 /* Disable the MAC */
290 uec_mac_disable(uec, mode);
295 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
301 printf("%s: uec not initial\n", __FUNCTION__);
304 uec_regs = uec->uec_regs;
306 if (duplex == DUPLEX_HALF) {
307 maccfg2 = in_be32(&uec_regs->maccfg2);
308 maccfg2 &= ~MACCFG2_FDX;
309 out_be32(&uec_regs->maccfg2, maccfg2);
312 if (duplex == DUPLEX_FULL) {
313 maccfg2 = in_be32(&uec_regs->maccfg2);
314 maccfg2 |= MACCFG2_FDX;
315 out_be32(&uec_regs->maccfg2, maccfg2);
321 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
323 enet_interface_e enet_if_mode;
324 uec_info_t *uec_info;
330 printf("%s: uec not initial\n", __FUNCTION__);
334 uec_info = uec->uec_info;
335 uec_regs = uec->uec_regs;
336 enet_if_mode = if_mode;
338 maccfg2 = in_be32(&uec_regs->maccfg2);
339 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
341 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
342 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
344 switch (enet_if_mode) {
347 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
350 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
353 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
357 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
358 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
360 case ENET_1000_RGMII_RXID:
361 case ENET_1000_RGMII_ID:
362 case ENET_1000_RGMII:
363 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
367 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
371 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
372 upsmr |= (UPSMR_RPM | UPSMR_R10M);
375 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
379 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
380 upsmr |= (UPSMR_R10M | UPSMR_RMM);
386 out_be32(&uec_regs->maccfg2, maccfg2);
387 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
392 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
394 uint timeout = 0x1000;
397 miimcfg = in_be32(&uec_mii_regs->miimcfg);
398 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
399 out_be32(&uec_mii_regs->miimcfg, miimcfg);
401 /* Wait until the bus is free */
402 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
404 printf("%s: The MII Bus is stuck!", __FUNCTION__);
411 static int init_phy(struct eth_device *dev)
414 uec_mii_t *umii_regs;
415 struct uec_mii_info *mii_info;
416 struct phy_info *curphy;
419 uec = (uec_private_t *)dev->priv;
420 umii_regs = uec->uec_mii_regs;
426 mii_info = malloc(sizeof(*mii_info));
428 printf("%s: Could not allocate mii_info", dev->name);
431 memset(mii_info, 0, sizeof(*mii_info));
433 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
434 mii_info->speed = SPEED_1000;
436 mii_info->speed = SPEED_100;
439 mii_info->duplex = DUPLEX_FULL;
443 mii_info->advertising = (ADVERTISED_10baseT_Half |
444 ADVERTISED_10baseT_Full |
445 ADVERTISED_100baseT_Half |
446 ADVERTISED_100baseT_Full |
447 ADVERTISED_1000baseT_Full);
448 mii_info->autoneg = 1;
449 mii_info->mii_id = uec->uec_info->phy_address;
452 mii_info->mdio_read = &uec_read_phy_reg;
453 mii_info->mdio_write = &uec_write_phy_reg;
455 uec->mii_info = mii_info;
457 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
459 if (init_mii_management_configuration(umii_regs)) {
460 printf("%s: The MII Bus is stuck!", dev->name);
465 /* get info for this PHY */
466 curphy = uec_get_phy_info(uec->mii_info);
468 printf("%s: No PHY found", dev->name);
473 mii_info->phyinfo = curphy;
475 /* Run the commands which initialize the PHY */
477 err = curphy->init(uec->mii_info);
491 static void adjust_link(struct eth_device *dev)
493 uec_private_t *uec = (uec_private_t *)dev->priv;
495 struct uec_mii_info *mii_info = uec->mii_info;
497 extern void change_phy_interface_mode(struct eth_device *dev,
498 enet_interface_e mode);
499 uec_regs = uec->uec_regs;
501 if (mii_info->link) {
502 /* Now we make sure that we can be in full duplex mode.
503 * If not, we operate in half-duplex mode. */
504 if (mii_info->duplex != uec->oldduplex) {
505 if (!(mii_info->duplex)) {
506 uec_set_mac_duplex(uec, DUPLEX_HALF);
507 printf("%s: Half Duplex\n", dev->name);
509 uec_set_mac_duplex(uec, DUPLEX_FULL);
510 printf("%s: Full Duplex\n", dev->name);
512 uec->oldduplex = mii_info->duplex;
515 if (mii_info->speed != uec->oldspeed) {
516 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
517 switch (mii_info->speed) {
521 printf ("switching to rgmii 100\n");
522 /* change phy to rgmii 100 */
523 change_phy_interface_mode(dev,
525 /* change the MAC interface mode */
526 uec_set_mac_if_mode(uec,ENET_100_RGMII);
529 printf ("switching to rgmii 10\n");
530 /* change phy to rgmii 10 */
531 change_phy_interface_mode(dev,
533 /* change the MAC interface mode */
534 uec_set_mac_if_mode(uec,ENET_10_RGMII);
537 printf("%s: Ack,Speed(%d)is illegal\n",
538 dev->name, mii_info->speed);
543 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
544 uec->oldspeed = mii_info->speed;
548 printf("%s: Link is up\n", dev->name);
552 } else { /* if (mii_info->link) */
554 printf("%s: Link is down\n", dev->name);
562 static void phy_change(struct eth_device *dev)
564 uec_private_t *uec = (uec_private_t *)dev->priv;
566 /* Update the link, speed, duplex */
567 uec->mii_info->phyinfo->read_status(uec->mii_info);
569 /* Adjust the interface according to speed */
573 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
574 && !defined(BITBANGMII)
577 * Find a device index from the devlist by name
580 * The index where the device is located, -1 on error
582 static int uec_miiphy_find_dev_by_name(char *devname)
586 for (i = 0; i < MAXCONTROLLERS; i++) {
587 if (strncmp(devname, devlist[i]->name, strlen(devname)) == 0) {
592 /* If device cannot be found, returns -1 */
593 if (i == MAXCONTROLLERS) {
594 debug ("%s: device %s not found in devlist\n", __FUNCTION__, devname);
602 * Read a MII PHY register.
607 static int uec_miiphy_read(char *devname, unsigned char addr,
608 unsigned char reg, unsigned short *value)
612 if (devname == NULL || value == NULL) {
613 debug("%s: NULL pointer given\n", __FUNCTION__);
615 devindex = uec_miiphy_find_dev_by_name(devname);
617 *value = uec_read_phy_reg(devlist[devindex], addr, reg);
624 * Write a MII PHY register.
629 static int uec_miiphy_write(char *devname, unsigned char addr,
630 unsigned char reg, unsigned short value)
634 if (devname == NULL) {
635 debug("%s: NULL pointer given\n", __FUNCTION__);
637 devindex = uec_miiphy_find_dev_by_name(devname);
639 uec_write_phy_reg(devlist[devindex], addr, reg, value);
646 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
653 printf("%s: uec not initial\n", __FUNCTION__);
657 uec_regs = uec->uec_regs;
659 /* if a station address of 0x12345678ABCD, perform a write to
660 MACSTNADDR1 of 0xCDAB7856,
661 MACSTNADDR2 of 0x34120000 */
663 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
664 (mac_addr[3] << 8) | (mac_addr[2]);
665 out_be32(&uec_regs->macstnaddr1, mac_addr1);
667 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
668 out_be32(&uec_regs->macstnaddr2, mac_addr2);
673 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
674 int *threads_num_ret)
676 int num_threads_numerica;
678 switch (threads_num) {
679 case UEC_NUM_OF_THREADS_1:
680 num_threads_numerica = 1;
682 case UEC_NUM_OF_THREADS_2:
683 num_threads_numerica = 2;
685 case UEC_NUM_OF_THREADS_4:
686 num_threads_numerica = 4;
688 case UEC_NUM_OF_THREADS_6:
689 num_threads_numerica = 6;
691 case UEC_NUM_OF_THREADS_8:
692 num_threads_numerica = 8;
695 printf("%s: Bad number of threads value.",
700 *threads_num_ret = num_threads_numerica;
705 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
707 uec_info_t *uec_info;
712 uec_info = uec->uec_info;
714 /* Alloc global Tx parameter RAM page */
715 uec->tx_glbl_pram_offset = qe_muram_alloc(
716 sizeof(uec_tx_global_pram_t),
717 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
718 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
719 qe_muram_addr(uec->tx_glbl_pram_offset);
721 /* Zero the global Tx prameter RAM */
722 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
724 /* Init global Tx parameter RAM */
726 /* TEMODER, RMON statistics disable, one Tx queue */
727 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
730 uec->send_q_mem_reg_offset = qe_muram_alloc(
731 sizeof(uec_send_queue_qd_t),
732 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
733 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
734 qe_muram_addr(uec->send_q_mem_reg_offset);
735 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
737 /* Setup the table with TxBDs ring */
738 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
740 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
741 (u32)(uec->p_tx_bd_ring));
742 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
745 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
746 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
748 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
749 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
751 /* TSTATE, global snooping, big endian, the CSB bus selected */
752 bmrx = BMR_INIT_VALUE;
753 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
756 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
757 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
761 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
762 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
766 uec->thread_dat_tx_offset = qe_muram_alloc(
767 num_threads_tx * sizeof(uec_thread_data_tx_t) +
768 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
770 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
771 qe_muram_addr(uec->thread_dat_tx_offset);
772 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
775 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
779 uec_82xx_address_filtering_pram_t *p_af_pram;
781 /* Allocate global Rx parameter RAM page */
782 uec->rx_glbl_pram_offset = qe_muram_alloc(
783 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
784 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
785 qe_muram_addr(uec->rx_glbl_pram_offset);
787 /* Zero Global Rx parameter RAM */
788 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
790 /* Init global Rx parameter RAM */
791 /* REMODER, Extended feature mode disable, VLAN disable,
792 LossLess flow control disable, Receive firmware statisic disable,
793 Extended address parsing mode disable, One Rx queues,
794 Dynamic maximum/minimum frame length disable, IP checksum check
795 disable, IP address alignment disable
797 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
800 uec->thread_dat_rx_offset = qe_muram_alloc(
801 num_threads_rx * sizeof(uec_thread_data_rx_t),
802 UEC_THREAD_DATA_ALIGNMENT);
803 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
804 qe_muram_addr(uec->thread_dat_rx_offset);
805 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
808 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
810 /* RxRMON base pointer, we don't need it */
811 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
813 /* IntCoalescingPTR, we don't need it, no interrupt */
814 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
816 /* RSTATE, global snooping, big endian, the CSB bus selected */
817 bmrx = BMR_INIT_VALUE;
818 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
821 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
824 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
825 sizeof(uec_rx_bd_queues_entry_t) + \
826 sizeof(uec_rx_prefetched_bds_t),
827 UEC_RX_BD_QUEUES_ALIGNMENT);
828 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
829 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
832 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
833 sizeof(uec_rx_prefetched_bds_t));
834 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
835 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
836 (u32)uec->p_rx_bd_ring);
839 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
841 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
843 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
845 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
847 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
849 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
851 for (i = 0; i < 8; i++) {
852 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
856 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
858 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
860 /* Clear PQ2 style address filtering hash table */
861 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
862 uec->p_rx_glbl_pram->addressfiltering;
864 p_af_pram->iaddr_h = 0;
865 p_af_pram->iaddr_l = 0;
866 p_af_pram->gaddr_h = 0;
867 p_af_pram->gaddr_l = 0;
870 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
871 int thread_tx, int thread_rx)
873 uec_init_cmd_pram_t *p_init_enet_param;
874 u32 init_enet_param_offset;
875 uec_info_t *uec_info;
878 u32 init_enet_offset;
883 uec_info = uec->uec_info;
885 /* Allocate init enet command parameter */
886 uec->init_enet_param_offset = qe_muram_alloc(
887 sizeof(uec_init_cmd_pram_t), 4);
888 init_enet_param_offset = uec->init_enet_param_offset;
889 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
890 qe_muram_addr(uec->init_enet_param_offset);
892 /* Zero init enet command struct */
893 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
895 /* Init the command struct */
896 p_init_enet_param = uec->p_init_enet_param;
897 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
898 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
899 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
900 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
901 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
902 p_init_enet_param->largestexternallookupkeysize = 0;
904 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
905 << ENET_INIT_PARAM_RGF_SHIFT;
906 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
907 << ENET_INIT_PARAM_TGF_SHIFT;
909 /* Init Rx global parameter pointer */
910 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
911 (u32)uec_info->risc_rx;
913 /* Init Rx threads */
914 for (i = 0; i < (thread_rx + 1); i++) {
915 if ((snum = qe_get_snum()) < 0) {
916 printf("%s can not get snum\n", __FUNCTION__);
921 init_enet_offset = 0;
923 init_enet_offset = qe_muram_alloc(
924 sizeof(uec_thread_rx_pram_t),
925 UEC_THREAD_RX_PRAM_ALIGNMENT);
928 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
929 init_enet_offset | (u32)uec_info->risc_rx;
930 p_init_enet_param->rxthread[i] = entry_val;
933 /* Init Tx global parameter pointer */
934 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
935 (u32)uec_info->risc_tx;
937 /* Init Tx threads */
938 for (i = 0; i < thread_tx; i++) {
939 if ((snum = qe_get_snum()) < 0) {
940 printf("%s can not get snum\n", __FUNCTION__);
944 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
945 UEC_THREAD_TX_PRAM_ALIGNMENT);
947 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
948 init_enet_offset | (u32)uec_info->risc_tx;
949 p_init_enet_param->txthread[i] = entry_val;
952 __asm__ __volatile__("sync");
954 /* Issue QE command */
955 command = QE_INIT_TX_RX;
956 cecr_subblock = ucc_fast_get_qe_cr_subblock(
957 uec->uec_info->uf_info.ucc_num);
958 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
959 init_enet_param_offset);
964 static int uec_startup(uec_private_t *uec)
966 uec_info_t *uec_info;
967 ucc_fast_info_t *uf_info;
968 ucc_fast_private_t *uccf;
974 enet_interface_e enet_interface;
981 if (!uec || !uec->uec_info) {
982 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
986 uec_info = uec->uec_info;
987 uf_info = &(uec_info->uf_info);
989 /* Check if Rx BD ring len is illegal */
990 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
991 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
992 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
997 /* Check if Tx BD ring len is illegal */
998 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
999 printf("%s: Tx BD ring length must not be smaller than 2.\n",
1004 /* Check if MRBLR is illegal */
1005 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
1006 printf("%s: max rx buffer length must be mutliple of 128.\n",
1011 /* Both Rx and Tx are stopped */
1012 uec->grace_stopped_rx = 1;
1013 uec->grace_stopped_tx = 1;
1016 if (ucc_fast_init(uf_info, &uccf)) {
1017 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1024 /* Convert the Tx threads number */
1025 if (uec_convert_threads_num(uec_info->num_threads_tx,
1030 /* Convert the Rx threads number */
1031 if (uec_convert_threads_num(uec_info->num_threads_rx,
1036 uf_regs = uccf->uf_regs;
1038 /* UEC register is following UCC fast registers */
1039 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1041 /* Save the UEC register pointer to UEC private struct */
1042 uec->uec_regs = uec_regs;
1044 /* Init UPSMR, enable hardware statistics (UCC) */
1045 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1047 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1048 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1050 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1051 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1053 /* Setup MAC interface mode */
1054 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1056 /* Setup MII management base */
1057 #ifndef CONFIG_eTSEC_MDIO_BUS
1058 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1060 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1063 /* Setup MII master clock source */
1064 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1067 utbipar = in_be32(&uec_regs->utbipar);
1068 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1069 enet_interface = uec->uec_info->enet_interface;
1070 if (enet_interface == ENET_1000_TBI ||
1071 enet_interface == ENET_1000_RTBI) {
1072 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1073 << UTBIPAR_PHY_ADDRESS_SHIFT;
1075 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1076 << UTBIPAR_PHY_ADDRESS_SHIFT;
1079 out_be32(&uec_regs->utbipar, utbipar);
1081 /* Allocate Tx BDs */
1082 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1083 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1084 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1085 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1086 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1087 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1090 align = UEC_TX_BD_RING_ALIGNMENT;
1091 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1092 if (uec->tx_bd_ring_offset != 0) {
1093 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1097 /* Zero all of Tx BDs */
1098 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1100 /* Allocate Rx BDs */
1101 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1102 align = UEC_RX_BD_RING_ALIGNMENT;
1103 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1104 if (uec->rx_bd_ring_offset != 0) {
1105 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1109 /* Zero all of Rx BDs */
1110 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1112 /* Allocate Rx buffer */
1113 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1114 align = UEC_RX_DATA_BUF_ALIGNMENT;
1115 uec->rx_buf_offset = (u32)malloc(length + align);
1116 if (uec->rx_buf_offset != 0) {
1117 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1121 /* Zero all of the Rx buffer */
1122 memset((void *)(uec->rx_buf_offset), 0, length + align);
1124 /* Init TxBD ring */
1125 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1128 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1130 BD_STATUS_SET(bd, 0);
1131 BD_LENGTH_SET(bd, 0);
1134 BD_STATUS_SET((--bd), TxBD_WRAP);
1136 /* Init RxBD ring */
1137 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1139 buf = uec->p_rx_buf;
1140 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1141 BD_DATA_SET(bd, buf);
1142 BD_LENGTH_SET(bd, 0);
1143 BD_STATUS_SET(bd, RxBD_EMPTY);
1144 buf += MAX_RXBUF_LEN;
1147 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1149 /* Init global Tx parameter RAM */
1150 uec_init_tx_parameter(uec, num_threads_tx);
1152 /* Init global Rx parameter RAM */
1153 uec_init_rx_parameter(uec, num_threads_rx);
1155 /* Init ethernet Tx and Rx parameter command */
1156 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1158 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1165 static int uec_init(struct eth_device* dev, bd_t *bd)
1169 struct phy_info *curphy;
1171 uec = (uec_private_t *)dev->priv;
1173 if (uec->the_first_run == 0) {
1174 err = init_phy(dev);
1176 printf("%s: Cannot initialize PHY, aborting.\n",
1181 curphy = uec->mii_info->phyinfo;
1183 if (curphy->config_aneg) {
1184 err = curphy->config_aneg(uec->mii_info);
1186 printf("%s: Can't negotiate PHY\n", dev->name);
1191 /* Give PHYs up to 5 sec to report a link */
1194 err = curphy->read_status(uec->mii_info);
1196 } while (((i-- > 0) && !uec->mii_info->link) || err);
1199 printf("warning: %s: timeout on PHY link\n", dev->name);
1201 uec->the_first_run = 1;
1204 /* Set up the MAC address */
1205 if (dev->enetaddr[0] & 0x01) {
1206 printf("%s: MacAddress is multcast address\n",
1210 uec_set_mac_address(uec, dev->enetaddr);
1213 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1215 printf("%s: cannot enable UEC device\n", dev->name);
1221 return (uec->mii_info->link ? 0 : -1);
1224 static void uec_halt(struct eth_device* dev)
1226 uec_private_t *uec = (uec_private_t *)dev->priv;
1227 uec_stop(uec, COMM_DIR_RX_AND_TX);
1230 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1233 ucc_fast_private_t *uccf;
1234 volatile qe_bd_t *bd;
1239 uec = (uec_private_t *)dev->priv;
1243 /* Find an empty TxBD */
1244 for (i = 0; bd->status & TxBD_READY; i++) {
1246 printf("%s: tx buffer not ready\n", dev->name);
1252 BD_DATA_SET(bd, buf);
1253 BD_LENGTH_SET(bd, len);
1254 status = bd->status;
1256 status |= (TxBD_READY | TxBD_LAST);
1257 BD_STATUS_SET(bd, status);
1259 /* Tell UCC to transmit the buffer */
1260 ucc_fast_transmit_on_demand(uccf);
1262 /* Wait for buffer to be transmitted */
1263 for (i = 0; bd->status & TxBD_READY; i++) {
1265 printf("%s: tx error\n", dev->name);
1270 /* Ok, the buffer be transimitted */
1271 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1278 static int uec_recv(struct eth_device* dev)
1280 uec_private_t *uec = dev->priv;
1281 volatile qe_bd_t *bd;
1287 status = bd->status;
1289 while (!(status & RxBD_EMPTY)) {
1290 if (!(status & RxBD_ERROR)) {
1292 len = BD_LENGTH(bd);
1293 NetReceive(data, len);
1295 printf("%s: Rx error\n", dev->name);
1298 BD_LENGTH_SET(bd, 0);
1299 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1300 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1301 status = bd->status;
1308 int uec_initialize(bd_t *bis, uec_info_t *uec_info)
1310 struct eth_device *dev;
1315 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1318 memset(dev, 0, sizeof(struct eth_device));
1320 /* Allocate the UEC private struct */
1321 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1325 memset(uec, 0, sizeof(uec_private_t));
1327 /* Adjust uec_info */
1328 #if (MAX_QE_RISC == 4)
1329 uec_info->risc_tx = QE_RISC_ALLOCATION_FOUR_RISCS;
1330 uec_info->risc_rx = QE_RISC_ALLOCATION_FOUR_RISCS;
1333 devlist[uec_info->uf_info.ucc_num] = dev;
1335 uec->uec_info = uec_info;
1337 sprintf(dev->name, "FSL UEC%d", uec_info->uf_info.ucc_num);
1339 dev->priv = (void *)uec;
1340 dev->init = uec_init;
1341 dev->halt = uec_halt;
1342 dev->send = uec_send;
1343 dev->recv = uec_recv;
1345 /* Clear the ethnet address */
1346 for (i = 0; i < 6; i++)
1347 dev->enetaddr[i] = 0;
1351 err = uec_startup(uec);
1353 printf("%s: Cannot configure net device, aborting.",dev->name);
1357 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1358 && !defined(BITBANGMII)
1359 miiphy_register(dev->name, uec_miiphy_read, uec_miiphy_write);
1365 int uec_eth_init(bd_t *bis, uec_info_t *uecs, int num)
1369 for (i = 0; i < num; i++)
1370 uec_initialize(bis, &uecs[i]);
1375 int uec_standard_init(bd_t *bis)
1377 return uec_eth_init(bis, uec_info, ARRAY_SIZE(uec_info));