2 * Copyright (C) 2006 Freescale Semiconductor, Inc.
4 * Dave Liu <daveliu@freescale.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include "asm/errno.h"
27 #include "asm/immap_qe.h"
33 #if defined(CONFIG_QE)
35 #ifdef CONFIG_UEC_ETH1
36 static uec_info_t eth1_uec_info = {
38 .ucc_num = CFG_UEC1_UCC_NUM,
39 .rx_clock = CFG_UEC1_RX_CLK,
40 .tx_clock = CFG_UEC1_TX_CLK,
41 .eth_type = CFG_UEC1_ETH_TYPE,
43 .num_threads_tx = UEC_NUM_OF_THREADS_4,
44 .num_threads_rx = UEC_NUM_OF_THREADS_4,
45 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
46 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
49 .phy_address = CFG_UEC1_PHY_ADDR,
50 .enet_interface = CFG_UEC1_INTERFACE_MODE,
53 #ifdef CONFIG_UEC_ETH2
54 static uec_info_t eth2_uec_info = {
56 .ucc_num = CFG_UEC2_UCC_NUM,
57 .rx_clock = CFG_UEC2_RX_CLK,
58 .tx_clock = CFG_UEC2_TX_CLK,
59 .eth_type = CFG_UEC2_ETH_TYPE,
61 .num_threads_tx = UEC_NUM_OF_THREADS_4,
62 .num_threads_rx = UEC_NUM_OF_THREADS_4,
63 .riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
64 .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
67 .phy_address = CFG_UEC2_PHY_ADDR,
68 .enet_interface = CFG_UEC2_INTERFACE_MODE,
72 static int uec_mac_enable(uec_private_t *uec, comm_dir_e mode)
78 printf("%s: uec not initial\n", __FUNCTION__);
81 uec_regs = uec->uec_regs;
83 maccfg1 = in_be32(&uec_regs->maccfg1);
85 if (mode & COMM_DIR_TX) {
86 maccfg1 |= MACCFG1_ENABLE_TX;
87 out_be32(&uec_regs->maccfg1, maccfg1);
88 uec->mac_tx_enabled = 1;
91 if (mode & COMM_DIR_RX) {
92 maccfg1 |= MACCFG1_ENABLE_RX;
93 out_be32(&uec_regs->maccfg1, maccfg1);
94 uec->mac_rx_enabled = 1;
100 static int uec_mac_disable(uec_private_t *uec, comm_dir_e mode)
106 printf("%s: uec not initial\n", __FUNCTION__);
109 uec_regs = uec->uec_regs;
111 maccfg1 = in_be32(&uec_regs->maccfg1);
113 if (mode & COMM_DIR_TX) {
114 maccfg1 &= ~MACCFG1_ENABLE_TX;
115 out_be32(&uec_regs->maccfg1, maccfg1);
116 uec->mac_tx_enabled = 0;
119 if (mode & COMM_DIR_RX) {
120 maccfg1 &= ~MACCFG1_ENABLE_RX;
121 out_be32(&uec_regs->maccfg1, maccfg1);
122 uec->mac_rx_enabled = 0;
128 static int uec_graceful_stop_tx(uec_private_t *uec)
134 if (!uec || !uec->uccf) {
135 printf("%s: No handle passed.\n", __FUNCTION__);
139 uf_regs = uec->uccf->uf_regs;
141 /* Clear the grace stop event */
142 out_be32(&uf_regs->ucce, UCCE_GRA);
144 /* Issue host command */
146 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
147 qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
148 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
150 /* Wait for command to complete */
152 ucce = in_be32(&uf_regs->ucce);
153 } while (! (ucce & UCCE_GRA));
155 uec->grace_stopped_tx = 1;
160 static int uec_graceful_stop_rx(uec_private_t *uec)
166 printf("%s: No handle passed.\n", __FUNCTION__);
170 if (!uec->p_rx_glbl_pram) {
171 printf("%s: No init rx global parameter\n", __FUNCTION__);
175 /* Clear acknowledge bit */
176 ack = uec->p_rx_glbl_pram->rxgstpack;
177 ack &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
178 uec->p_rx_glbl_pram->rxgstpack = ack;
180 /* Keep issuing cmd and checking ack bit until it is asserted */
182 /* Issue host command */
184 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
185 qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
186 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
187 ack = uec->p_rx_glbl_pram->rxgstpack;
188 } while (! (ack & GRACEFUL_STOP_ACKNOWLEDGE_RX ));
190 uec->grace_stopped_rx = 1;
195 static int uec_restart_tx(uec_private_t *uec)
199 if (!uec || !uec->uec_info) {
200 printf("%s: No handle passed.\n", __FUNCTION__);
205 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
206 qe_issue_cmd(QE_RESTART_TX, cecr_subblock,
207 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
209 uec->grace_stopped_tx = 0;
214 static int uec_restart_rx(uec_private_t *uec)
218 if (!uec || !uec->uec_info) {
219 printf("%s: No handle passed.\n", __FUNCTION__);
224 ucc_fast_get_qe_cr_subblock(uec->uec_info->uf_info.ucc_num);
225 qe_issue_cmd(QE_RESTART_RX, cecr_subblock,
226 (u8)QE_CR_PROTOCOL_ETHERNET, 0);
228 uec->grace_stopped_rx = 0;
233 static int uec_open(uec_private_t *uec, comm_dir_e mode)
235 ucc_fast_private_t *uccf;
237 if (!uec || !uec->uccf) {
238 printf("%s: No handle passed.\n", __FUNCTION__);
243 /* check if the UCC number is in range. */
244 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
245 printf("%s: ucc_num out of range.\n", __FUNCTION__);
250 uec_mac_enable(uec, mode);
252 /* Enable UCC fast */
253 ucc_fast_enable(uccf, mode);
255 /* RISC microcode start */
256 if ((mode & COMM_DIR_TX) && uec->grace_stopped_tx) {
259 if ((mode & COMM_DIR_RX) && uec->grace_stopped_rx) {
266 static int uec_stop(uec_private_t *uec, comm_dir_e mode)
268 ucc_fast_private_t *uccf;
270 if (!uec || !uec->uccf) {
271 printf("%s: No handle passed.\n", __FUNCTION__);
276 /* check if the UCC number is in range. */
277 if (uec->uec_info->uf_info.ucc_num >= UCC_MAX_NUM) {
278 printf("%s: ucc_num out of range.\n", __FUNCTION__);
281 /* Stop any transmissions */
282 if ((mode & COMM_DIR_TX) && !uec->grace_stopped_tx) {
283 uec_graceful_stop_tx(uec);
285 /* Stop any receptions */
286 if ((mode & COMM_DIR_RX) && !uec->grace_stopped_rx) {
287 uec_graceful_stop_rx(uec);
290 /* Disable the UCC fast */
291 ucc_fast_disable(uec->uccf, mode);
293 /* Disable the MAC */
294 uec_mac_disable(uec, mode);
299 static int uec_set_mac_duplex(uec_private_t *uec, int duplex)
305 printf("%s: uec not initial\n", __FUNCTION__);
308 uec_regs = uec->uec_regs;
310 if (duplex == DUPLEX_HALF) {
311 maccfg2 = in_be32(&uec_regs->maccfg2);
312 maccfg2 &= ~MACCFG2_FDX;
313 out_be32(&uec_regs->maccfg2, maccfg2);
316 if (duplex == DUPLEX_FULL) {
317 maccfg2 = in_be32(&uec_regs->maccfg2);
318 maccfg2 |= MACCFG2_FDX;
319 out_be32(&uec_regs->maccfg2, maccfg2);
325 static int uec_set_mac_if_mode(uec_private_t *uec, enet_interface_e if_mode)
327 enet_interface_e enet_if_mode;
328 uec_info_t *uec_info;
334 printf("%s: uec not initial\n", __FUNCTION__);
338 uec_info = uec->uec_info;
339 uec_regs = uec->uec_regs;
340 enet_if_mode = if_mode;
342 maccfg2 = in_be32(&uec_regs->maccfg2);
343 maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
345 upsmr = in_be32(&uec->uccf->uf_regs->upsmr);
346 upsmr &= ~(UPSMR_RPM | UPSMR_TBIM | UPSMR_R10M | UPSMR_RMM);
348 switch (enet_if_mode) {
351 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
354 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
357 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
361 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
362 upsmr |= (UPSMR_RPM | UPSMR_TBIM);
364 case ENET_1000_RGMII:
365 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
369 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
373 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
374 upsmr |= (UPSMR_RPM | UPSMR_R10M);
377 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
381 maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
382 upsmr |= (UPSMR_R10M | UPSMR_RMM);
388 out_be32(&uec_regs->maccfg2, maccfg2);
389 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
394 static int init_mii_management_configuration(uec_t *uec_regs)
396 uint timeout = 0x1000;
399 miimcfg = in_be32(&uec_regs->miimcfg);
400 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
401 out_be32(&uec_regs->miimcfg, miimcfg);
403 /* Wait until the bus is free */
404 while ((in_be32(&uec_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
406 printf("%s: The MII Bus is stuck!", __FUNCTION__);
413 static int init_phy(struct eth_device *dev)
417 struct uec_mii_info *mii_info;
418 struct phy_info *curphy;
421 uec = (uec_private_t *)dev->priv;
422 uec_regs = uec->uec_regs;
428 mii_info = malloc(sizeof(*mii_info));
430 printf("%s: Could not allocate mii_info", dev->name);
433 memset(mii_info, 0, sizeof(*mii_info));
435 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
436 mii_info->speed = SPEED_1000;
438 mii_info->speed = SPEED_100;
441 mii_info->duplex = DUPLEX_FULL;
445 mii_info->advertising = (ADVERTISED_10baseT_Half |
446 ADVERTISED_10baseT_Full |
447 ADVERTISED_100baseT_Half |
448 ADVERTISED_100baseT_Full |
449 ADVERTISED_1000baseT_Full);
450 mii_info->autoneg = 1;
451 mii_info->mii_id = uec->uec_info->phy_address;
454 mii_info->mdio_read = &read_phy_reg;
455 mii_info->mdio_write = &write_phy_reg;
457 uec->mii_info = mii_info;
459 if (init_mii_management_configuration(uec_regs)) {
460 printf("%s: The MII Bus is stuck!", dev->name);
465 /* get info for this PHY */
466 curphy = 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;
568 uec_regs = uec->uec_regs;
570 /* Delay 5s to give the PHY a chance to change the register state */
573 /* Update the link, speed, duplex */
574 result = uec->mii_info->phyinfo->read_status(uec->mii_info);
576 /* Adjust the interface according to speed */
577 if ((0 == result) || (uec->mii_info->link == 0)) {
582 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
589 printf("%s: uec not initial\n", __FUNCTION__);
593 uec_regs = uec->uec_regs;
595 /* if a station address of 0x12345678ABCD, perform a write to
596 MACSTNADDR1 of 0xCDAB7856,
597 MACSTNADDR2 of 0x34120000 */
599 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
600 (mac_addr[3] << 8) | (mac_addr[2]);
601 out_be32(&uec_regs->macstnaddr1, mac_addr1);
603 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
604 out_be32(&uec_regs->macstnaddr2, mac_addr2);
609 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
610 int *threads_num_ret)
612 int num_threads_numerica;
614 switch (threads_num) {
615 case UEC_NUM_OF_THREADS_1:
616 num_threads_numerica = 1;
618 case UEC_NUM_OF_THREADS_2:
619 num_threads_numerica = 2;
621 case UEC_NUM_OF_THREADS_4:
622 num_threads_numerica = 4;
624 case UEC_NUM_OF_THREADS_6:
625 num_threads_numerica = 6;
627 case UEC_NUM_OF_THREADS_8:
628 num_threads_numerica = 8;
631 printf("%s: Bad number of threads value.",
636 *threads_num_ret = num_threads_numerica;
641 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
643 uec_info_t *uec_info;
648 uec_info = uec->uec_info;
650 /* Alloc global Tx parameter RAM page */
651 uec->tx_glbl_pram_offset = qe_muram_alloc(
652 sizeof(uec_tx_global_pram_t),
653 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
654 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
655 qe_muram_addr(uec->tx_glbl_pram_offset);
657 /* Zero the global Tx prameter RAM */
658 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
660 /* Init global Tx parameter RAM */
662 /* TEMODER, RMON statistics disable, one Tx queue */
663 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
666 uec->send_q_mem_reg_offset = qe_muram_alloc(
667 sizeof(uec_send_queue_qd_t),
668 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
669 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
670 qe_muram_addr(uec->send_q_mem_reg_offset);
671 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
673 /* Setup the table with TxBDs ring */
674 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
676 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
677 (u32)(uec->p_tx_bd_ring));
678 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
681 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
682 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
684 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
685 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
687 /* TSTATE, global snooping, big endian, the CSB bus selected */
688 bmrx = BMR_INIT_VALUE;
689 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
692 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
693 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
697 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
698 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
702 uec->thread_dat_tx_offset = qe_muram_alloc(
703 num_threads_tx * sizeof(uec_thread_data_tx_t) +
704 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
706 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
707 qe_muram_addr(uec->thread_dat_tx_offset);
708 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
711 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
715 uec_82xx_address_filtering_pram_t *p_af_pram;
717 /* Allocate global Rx parameter RAM page */
718 uec->rx_glbl_pram_offset = qe_muram_alloc(
719 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
720 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
721 qe_muram_addr(uec->rx_glbl_pram_offset);
723 /* Zero Global Rx parameter RAM */
724 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
726 /* Init global Rx parameter RAM */
727 /* REMODER, Extended feature mode disable, VLAN disable,
728 LossLess flow control disable, Receive firmware statisic disable,
729 Extended address parsing mode disable, One Rx queues,
730 Dynamic maximum/minimum frame length disable, IP checksum check
731 disable, IP address alignment disable
733 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
736 uec->thread_dat_rx_offset = qe_muram_alloc(
737 num_threads_rx * sizeof(uec_thread_data_rx_t),
738 UEC_THREAD_DATA_ALIGNMENT);
739 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
740 qe_muram_addr(uec->thread_dat_rx_offset);
741 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
744 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
746 /* RxRMON base pointer, we don't need it */
747 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
749 /* IntCoalescingPTR, we don't need it, no interrupt */
750 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
752 /* RSTATE, global snooping, big endian, the CSB bus selected */
753 bmrx = BMR_INIT_VALUE;
754 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
757 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
760 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
761 sizeof(uec_rx_bd_queues_entry_t) + \
762 sizeof(uec_rx_prefetched_bds_t),
763 UEC_RX_BD_QUEUES_ALIGNMENT);
764 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
765 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
768 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
769 sizeof(uec_rx_prefetched_bds_t));
770 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
771 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
772 (u32)uec->p_rx_bd_ring);
775 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
777 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
779 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
781 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
783 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
785 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
787 for (i = 0; i < 8; i++) {
788 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
792 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
794 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
796 /* Clear PQ2 style address filtering hash table */
797 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
798 uec->p_rx_glbl_pram->addressfiltering;
800 p_af_pram->iaddr_h = 0;
801 p_af_pram->iaddr_l = 0;
802 p_af_pram->gaddr_h = 0;
803 p_af_pram->gaddr_l = 0;
806 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
807 int thread_tx, int thread_rx)
809 uec_init_cmd_pram_t *p_init_enet_param;
810 u32 init_enet_param_offset;
811 uec_info_t *uec_info;
814 u32 init_enet_offset;
819 uec_info = uec->uec_info;
821 /* Allocate init enet command parameter */
822 uec->init_enet_param_offset = qe_muram_alloc(
823 sizeof(uec_init_cmd_pram_t), 4);
824 init_enet_param_offset = uec->init_enet_param_offset;
825 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
826 qe_muram_addr(uec->init_enet_param_offset);
828 /* Zero init enet command struct */
829 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
831 /* Init the command struct */
832 p_init_enet_param = uec->p_init_enet_param;
833 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
834 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
835 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
836 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
837 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
838 p_init_enet_param->largestexternallookupkeysize = 0;
840 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
841 << ENET_INIT_PARAM_RGF_SHIFT;
842 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
843 << ENET_INIT_PARAM_TGF_SHIFT;
845 /* Init Rx global parameter pointer */
846 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
847 (u32)uec_info->riscRx;
849 /* Init Rx threads */
850 for (i = 0; i < (thread_rx + 1); i++) {
851 if ((snum = qe_get_snum()) < 0) {
852 printf("%s can not get snum\n", __FUNCTION__);
857 init_enet_offset = 0;
859 init_enet_offset = qe_muram_alloc(
860 sizeof(uec_thread_rx_pram_t),
861 UEC_THREAD_RX_PRAM_ALIGNMENT);
864 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
865 init_enet_offset | (u32)uec_info->riscRx;
866 p_init_enet_param->rxthread[i] = entry_val;
869 /* Init Tx global parameter pointer */
870 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
871 (u32)uec_info->riscTx;
873 /* Init Tx threads */
874 for (i = 0; i < thread_tx; i++) {
875 if ((snum = qe_get_snum()) < 0) {
876 printf("%s can not get snum\n", __FUNCTION__);
880 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
881 UEC_THREAD_TX_PRAM_ALIGNMENT);
883 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
884 init_enet_offset | (u32)uec_info->riscTx;
885 p_init_enet_param->txthread[i] = entry_val;
888 __asm__ __volatile__("sync");
890 /* Issue QE command */
891 command = QE_INIT_TX_RX;
892 cecr_subblock = ucc_fast_get_qe_cr_subblock(
893 uec->uec_info->uf_info.ucc_num);
894 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
895 init_enet_param_offset);
900 static int uec_startup(uec_private_t *uec)
902 uec_info_t *uec_info;
903 ucc_fast_info_t *uf_info;
904 ucc_fast_private_t *uccf;
910 enet_interface_e enet_interface;
917 if (!uec || !uec->uec_info) {
918 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
922 uec_info = uec->uec_info;
923 uf_info = &(uec_info->uf_info);
925 /* Check if Rx BD ring len is illegal */
926 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
927 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
928 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
933 /* Check if Tx BD ring len is illegal */
934 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
935 printf("%s: Tx BD ring length must not be smaller than 2.\n",
940 /* Check if MRBLR is illegal */
941 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
942 printf("%s: max rx buffer length must be mutliple of 128.\n",
947 /* Both Rx and Tx are stopped */
948 uec->grace_stopped_rx = 1;
949 uec->grace_stopped_tx = 1;
952 if (ucc_fast_init(uf_info, &uccf)) {
953 printf("%s: failed to init ucc fast\n", __FUNCTION__);
960 /* Convert the Tx threads number */
961 if (uec_convert_threads_num(uec_info->num_threads_tx,
966 /* Convert the Rx threads number */
967 if (uec_convert_threads_num(uec_info->num_threads_rx,
972 uf_regs = uccf->uf_regs;
974 /* UEC register is following UCC fast registers */
975 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
977 /* Save the UEC register pointer to UEC private struct */
978 uec->uec_regs = uec_regs;
980 /* Init UPSMR, enable hardware statistics (UCC) */
981 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
983 /* Init MACCFG1, flow control disable, disable Tx and Rx */
984 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
986 /* Init MACCFG2, length check, MAC PAD and CRC enable */
987 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
989 /* Setup MAC interface mode */
990 uec_set_mac_if_mode(uec, uec_info->enet_interface);
992 /* Setup MII master clock source */
993 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
996 utbipar = in_be32(&uec_regs->utbipar);
997 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
998 enet_interface = uec->uec_info->enet_interface;
999 if (enet_interface == ENET_1000_TBI ||
1000 enet_interface == ENET_1000_RTBI) {
1001 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1002 << UTBIPAR_PHY_ADDRESS_SHIFT;
1004 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1005 << UTBIPAR_PHY_ADDRESS_SHIFT;
1008 out_be32(&uec_regs->utbipar, utbipar);
1010 /* Allocate Tx BDs */
1011 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1012 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1013 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1014 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1015 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1016 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1019 align = UEC_TX_BD_RING_ALIGNMENT;
1020 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1021 if (uec->tx_bd_ring_offset != 0) {
1022 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1026 /* Zero all of Tx BDs */
1027 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1029 /* Allocate Rx BDs */
1030 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1031 align = UEC_RX_BD_RING_ALIGNMENT;
1032 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1033 if (uec->rx_bd_ring_offset != 0) {
1034 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1038 /* Zero all of Rx BDs */
1039 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1041 /* Allocate Rx buffer */
1042 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1043 align = UEC_RX_DATA_BUF_ALIGNMENT;
1044 uec->rx_buf_offset = (u32)malloc(length + align);
1045 if (uec->rx_buf_offset != 0) {
1046 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1050 /* Zero all of the Rx buffer */
1051 memset((void *)(uec->rx_buf_offset), 0, length + align);
1053 /* Init TxBD ring */
1054 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1057 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1059 BD_STATUS_SET(bd, 0);
1060 BD_LENGTH_SET(bd, 0);
1063 BD_STATUS_SET((--bd), TxBD_WRAP);
1065 /* Init RxBD ring */
1066 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1068 buf = uec->p_rx_buf;
1069 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1070 BD_DATA_SET(bd, buf);
1071 BD_LENGTH_SET(bd, 0);
1072 BD_STATUS_SET(bd, RxBD_EMPTY);
1073 buf += MAX_RXBUF_LEN;
1076 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1078 /* Init global Tx parameter RAM */
1079 uec_init_tx_parameter(uec, num_threads_tx);
1081 /* Init global Rx parameter RAM */
1082 uec_init_rx_parameter(uec, num_threads_rx);
1084 /* Init ethernet Tx and Rx parameter command */
1085 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1087 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1094 static int uec_init(struct eth_device* dev, bd_t *bd)
1099 uec = (uec_private_t *)dev->priv;
1101 if (uec->the_first_run == 0) {
1102 /* Set up the MAC address */
1103 if (dev->enetaddr[0] & 0x01) {
1104 printf("%s: MacAddress is multcast address\n",
1108 uec_set_mac_address(uec, dev->enetaddr);
1109 uec->the_first_run = 1;
1112 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1114 printf("%s: cannot enable UEC device\n", dev->name);
1121 static void uec_halt(struct eth_device* dev)
1123 uec_private_t *uec = (uec_private_t *)dev->priv;
1124 uec_stop(uec, COMM_DIR_RX_AND_TX);
1127 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1130 ucc_fast_private_t *uccf;
1131 volatile qe_bd_t *bd;
1136 uec = (uec_private_t *)dev->priv;
1140 /* Find an empty TxBD */
1141 for (i = 0; bd->status & TxBD_READY; i++) {
1143 printf("%s: tx buffer not ready\n", dev->name);
1149 BD_DATA_SET(bd, buf);
1150 BD_LENGTH_SET(bd, len);
1151 status = bd->status;
1153 status |= (TxBD_READY | TxBD_LAST);
1154 BD_STATUS_SET(bd, status);
1156 /* Tell UCC to transmit the buffer */
1157 ucc_fast_transmit_on_demand(uccf);
1159 /* Wait for buffer to be transmitted */
1160 for (i = 0; bd->status & TxBD_READY; i++) {
1162 printf("%s: tx error\n", dev->name);
1167 /* Ok, the buffer be transimitted */
1168 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1175 static int uec_recv(struct eth_device* dev)
1177 uec_private_t *uec = dev->priv;
1178 volatile qe_bd_t *bd;
1184 status = bd->status;
1186 while (!(status & RxBD_EMPTY)) {
1187 if (!(status & RxBD_ERROR)) {
1189 len = BD_LENGTH(bd);
1190 NetReceive(data, len);
1192 printf("%s: Rx error\n", dev->name);
1195 BD_LENGTH_SET(bd, 0);
1196 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1197 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1198 status = bd->status;
1205 int uec_initialize(int index)
1207 struct eth_device *dev;
1210 uec_info_t *uec_info;
1213 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1216 memset(dev, 0, sizeof(struct eth_device));
1218 /* Allocate the UEC private struct */
1219 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1223 memset(uec, 0, sizeof(uec_private_t));
1225 /* Init UEC private struct, they come from board.h */
1227 #ifdef CONFIG_UEC_ETH1
1228 uec_info = ð1_uec_info;
1230 } else if (index == 1) {
1231 #ifdef CONFIG_UEC_ETH2
1232 uec_info = ð2_uec_info;
1235 printf("%s: index is illegal.\n", __FUNCTION__);
1239 uec->uec_info = uec_info;
1241 sprintf(dev->name, "FSL UEC%d", index);
1243 dev->priv = (void *)uec;
1244 dev->init = uec_init;
1245 dev->halt = uec_halt;
1246 dev->send = uec_send;
1247 dev->recv = uec_recv;
1249 /* Clear the ethnet address */
1250 for (i = 0; i < 6; i++)
1251 dev->enetaddr[i] = 0;
1255 err = uec_startup(uec);
1257 printf("%s: Cannot configure net device, aborting.",dev->name);
1261 err = init_phy(dev);
1263 printf("%s: Cannot initialize PHY, aborting.\n", dev->name);
1271 #endif /* CONFIG_QE */