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);
382 case ENET_1000_SGMII:
383 maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
390 out_be32(&uec_regs->maccfg2, maccfg2);
391 out_be32(&uec->uccf->uf_regs->upsmr, upsmr);
396 static int init_mii_management_configuration(uec_mii_t *uec_mii_regs)
398 uint timeout = 0x1000;
401 miimcfg = in_be32(&uec_mii_regs->miimcfg);
402 miimcfg |= MIIMCFG_MNGMNT_CLC_DIV_INIT_VALUE;
403 out_be32(&uec_mii_regs->miimcfg, miimcfg);
405 /* Wait until the bus is free */
406 while ((in_be32(&uec_mii_regs->miimcfg) & MIIMIND_BUSY) && timeout--);
408 printf("%s: The MII Bus is stuck!", __FUNCTION__);
415 static int init_phy(struct eth_device *dev)
418 uec_mii_t *umii_regs;
419 struct uec_mii_info *mii_info;
420 struct phy_info *curphy;
423 uec = (uec_private_t *)dev->priv;
424 umii_regs = uec->uec_mii_regs;
430 mii_info = malloc(sizeof(*mii_info));
432 printf("%s: Could not allocate mii_info", dev->name);
435 memset(mii_info, 0, sizeof(*mii_info));
437 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
438 mii_info->speed = SPEED_1000;
440 mii_info->speed = SPEED_100;
443 mii_info->duplex = DUPLEX_FULL;
447 mii_info->advertising = (ADVERTISED_10baseT_Half |
448 ADVERTISED_10baseT_Full |
449 ADVERTISED_100baseT_Half |
450 ADVERTISED_100baseT_Full |
451 ADVERTISED_1000baseT_Full);
452 mii_info->autoneg = 1;
453 mii_info->mii_id = uec->uec_info->phy_address;
456 mii_info->mdio_read = &uec_read_phy_reg;
457 mii_info->mdio_write = &uec_write_phy_reg;
459 uec->mii_info = mii_info;
461 qe_set_mii_clk_src(uec->uec_info->uf_info.ucc_num);
463 if (init_mii_management_configuration(umii_regs)) {
464 printf("%s: The MII Bus is stuck!", dev->name);
469 /* get info for this PHY */
470 curphy = uec_get_phy_info(uec->mii_info);
472 printf("%s: No PHY found", dev->name);
477 mii_info->phyinfo = curphy;
479 /* Run the commands which initialize the PHY */
481 err = curphy->init(uec->mii_info);
495 static void adjust_link(struct eth_device *dev)
497 uec_private_t *uec = (uec_private_t *)dev->priv;
499 struct uec_mii_info *mii_info = uec->mii_info;
501 extern void change_phy_interface_mode(struct eth_device *dev,
502 enet_interface_e mode);
503 uec_regs = uec->uec_regs;
505 if (mii_info->link) {
506 /* Now we make sure that we can be in full duplex mode.
507 * If not, we operate in half-duplex mode. */
508 if (mii_info->duplex != uec->oldduplex) {
509 if (!(mii_info->duplex)) {
510 uec_set_mac_duplex(uec, DUPLEX_HALF);
511 printf("%s: Half Duplex\n", dev->name);
513 uec_set_mac_duplex(uec, DUPLEX_FULL);
514 printf("%s: Full Duplex\n", dev->name);
516 uec->oldduplex = mii_info->duplex;
519 if (mii_info->speed != uec->oldspeed) {
520 if (uec->uec_info->uf_info.eth_type == GIGA_ETH) {
521 switch (mii_info->speed) {
525 printf ("switching to rgmii 100\n");
526 /* change phy to rgmii 100 */
527 change_phy_interface_mode(dev,
529 /* change the MAC interface mode */
530 uec_set_mac_if_mode(uec,ENET_100_RGMII);
533 printf ("switching to rgmii 10\n");
534 /* change phy to rgmii 10 */
535 change_phy_interface_mode(dev,
537 /* change the MAC interface mode */
538 uec_set_mac_if_mode(uec,ENET_10_RGMII);
541 printf("%s: Ack,Speed(%d)is illegal\n",
542 dev->name, mii_info->speed);
547 printf("%s: Speed %dBT\n", dev->name, mii_info->speed);
548 uec->oldspeed = mii_info->speed;
552 printf("%s: Link is up\n", dev->name);
556 } else { /* if (mii_info->link) */
558 printf("%s: Link is down\n", dev->name);
566 static void phy_change(struct eth_device *dev)
568 uec_private_t *uec = (uec_private_t *)dev->priv;
570 /* Update the link, speed, duplex */
571 uec->mii_info->phyinfo->read_status(uec->mii_info);
573 /* Adjust the interface according to speed */
577 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
578 && !defined(BITBANGMII)
581 * Find a device index from the devlist by name
584 * The index where the device is located, -1 on error
586 static int uec_miiphy_find_dev_by_name(char *devname)
590 for (i = 0; i < MAXCONTROLLERS; i++) {
591 if (strncmp(devname, devlist[i]->name, strlen(devname)) == 0) {
596 /* If device cannot be found, returns -1 */
597 if (i == MAXCONTROLLERS) {
598 debug ("%s: device %s not found in devlist\n", __FUNCTION__, devname);
606 * Read a MII PHY register.
611 static int uec_miiphy_read(char *devname, unsigned char addr,
612 unsigned char reg, unsigned short *value)
616 if (devname == NULL || value == NULL) {
617 debug("%s: NULL pointer given\n", __FUNCTION__);
619 devindex = uec_miiphy_find_dev_by_name(devname);
621 *value = uec_read_phy_reg(devlist[devindex], addr, reg);
628 * Write a MII PHY register.
633 static int uec_miiphy_write(char *devname, unsigned char addr,
634 unsigned char reg, unsigned short value)
638 if (devname == NULL) {
639 debug("%s: NULL pointer given\n", __FUNCTION__);
641 devindex = uec_miiphy_find_dev_by_name(devname);
643 uec_write_phy_reg(devlist[devindex], addr, reg, value);
650 static int uec_set_mac_address(uec_private_t *uec, u8 *mac_addr)
657 printf("%s: uec not initial\n", __FUNCTION__);
661 uec_regs = uec->uec_regs;
663 /* if a station address of 0x12345678ABCD, perform a write to
664 MACSTNADDR1 of 0xCDAB7856,
665 MACSTNADDR2 of 0x34120000 */
667 mac_addr1 = (mac_addr[5] << 24) | (mac_addr[4] << 16) | \
668 (mac_addr[3] << 8) | (mac_addr[2]);
669 out_be32(&uec_regs->macstnaddr1, mac_addr1);
671 mac_addr2 = ((mac_addr[1] << 24) | (mac_addr[0] << 16)) & 0xffff0000;
672 out_be32(&uec_regs->macstnaddr2, mac_addr2);
677 static int uec_convert_threads_num(uec_num_of_threads_e threads_num,
678 int *threads_num_ret)
680 int num_threads_numerica;
682 switch (threads_num) {
683 case UEC_NUM_OF_THREADS_1:
684 num_threads_numerica = 1;
686 case UEC_NUM_OF_THREADS_2:
687 num_threads_numerica = 2;
689 case UEC_NUM_OF_THREADS_4:
690 num_threads_numerica = 4;
692 case UEC_NUM_OF_THREADS_6:
693 num_threads_numerica = 6;
695 case UEC_NUM_OF_THREADS_8:
696 num_threads_numerica = 8;
699 printf("%s: Bad number of threads value.",
704 *threads_num_ret = num_threads_numerica;
709 static void uec_init_tx_parameter(uec_private_t *uec, int num_threads_tx)
711 uec_info_t *uec_info;
716 uec_info = uec->uec_info;
718 /* Alloc global Tx parameter RAM page */
719 uec->tx_glbl_pram_offset = qe_muram_alloc(
720 sizeof(uec_tx_global_pram_t),
721 UEC_TX_GLOBAL_PRAM_ALIGNMENT);
722 uec->p_tx_glbl_pram = (uec_tx_global_pram_t *)
723 qe_muram_addr(uec->tx_glbl_pram_offset);
725 /* Zero the global Tx prameter RAM */
726 memset(uec->p_tx_glbl_pram, 0, sizeof(uec_tx_global_pram_t));
728 /* Init global Tx parameter RAM */
730 /* TEMODER, RMON statistics disable, one Tx queue */
731 out_be16(&uec->p_tx_glbl_pram->temoder, TEMODER_INIT_VALUE);
734 uec->send_q_mem_reg_offset = qe_muram_alloc(
735 sizeof(uec_send_queue_qd_t),
736 UEC_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
737 uec->p_send_q_mem_reg = (uec_send_queue_mem_region_t *)
738 qe_muram_addr(uec->send_q_mem_reg_offset);
739 out_be32(&uec->p_tx_glbl_pram->sqptr, uec->send_q_mem_reg_offset);
741 /* Setup the table with TxBDs ring */
742 end_bd = (u32)uec->p_tx_bd_ring + (uec_info->tx_bd_ring_len - 1)
744 out_be32(&uec->p_send_q_mem_reg->sqqd[0].bd_ring_base,
745 (u32)(uec->p_tx_bd_ring));
746 out_be32(&uec->p_send_q_mem_reg->sqqd[0].last_bd_completed_address,
749 /* Scheduler Base Pointer, we have only one Tx queue, no need it */
750 out_be32(&uec->p_tx_glbl_pram->schedulerbasepointer, 0);
752 /* TxRMON Base Pointer, TxRMON disable, we don't need it */
753 out_be32(&uec->p_tx_glbl_pram->txrmonbaseptr, 0);
755 /* TSTATE, global snooping, big endian, the CSB bus selected */
756 bmrx = BMR_INIT_VALUE;
757 out_be32(&uec->p_tx_glbl_pram->tstate, ((u32)(bmrx) << BMR_SHIFT));
760 for (i = 0; i < MAX_IPH_OFFSET_ENTRY; i++) {
761 out_8(&uec->p_tx_glbl_pram->iphoffset[i], 0);
765 for (i = 0; i < UEC_TX_VTAG_TABLE_ENTRY_MAX; i++) {
766 out_be32(&uec->p_tx_glbl_pram->vtagtable[i], 0);
770 uec->thread_dat_tx_offset = qe_muram_alloc(
771 num_threads_tx * sizeof(uec_thread_data_tx_t) +
772 32 *(num_threads_tx == 1), UEC_THREAD_DATA_ALIGNMENT);
774 uec->p_thread_data_tx = (uec_thread_data_tx_t *)
775 qe_muram_addr(uec->thread_dat_tx_offset);
776 out_be32(&uec->p_tx_glbl_pram->tqptr, uec->thread_dat_tx_offset);
779 static void uec_init_rx_parameter(uec_private_t *uec, int num_threads_rx)
783 uec_82xx_address_filtering_pram_t *p_af_pram;
785 /* Allocate global Rx parameter RAM page */
786 uec->rx_glbl_pram_offset = qe_muram_alloc(
787 sizeof(uec_rx_global_pram_t), UEC_RX_GLOBAL_PRAM_ALIGNMENT);
788 uec->p_rx_glbl_pram = (uec_rx_global_pram_t *)
789 qe_muram_addr(uec->rx_glbl_pram_offset);
791 /* Zero Global Rx parameter RAM */
792 memset(uec->p_rx_glbl_pram, 0, sizeof(uec_rx_global_pram_t));
794 /* Init global Rx parameter RAM */
795 /* REMODER, Extended feature mode disable, VLAN disable,
796 LossLess flow control disable, Receive firmware statisic disable,
797 Extended address parsing mode disable, One Rx queues,
798 Dynamic maximum/minimum frame length disable, IP checksum check
799 disable, IP address alignment disable
801 out_be32(&uec->p_rx_glbl_pram->remoder, REMODER_INIT_VALUE);
804 uec->thread_dat_rx_offset = qe_muram_alloc(
805 num_threads_rx * sizeof(uec_thread_data_rx_t),
806 UEC_THREAD_DATA_ALIGNMENT);
807 uec->p_thread_data_rx = (uec_thread_data_rx_t *)
808 qe_muram_addr(uec->thread_dat_rx_offset);
809 out_be32(&uec->p_rx_glbl_pram->rqptr, uec->thread_dat_rx_offset);
812 out_be16(&uec->p_rx_glbl_pram->typeorlen, 3072);
814 /* RxRMON base pointer, we don't need it */
815 out_be32(&uec->p_rx_glbl_pram->rxrmonbaseptr, 0);
817 /* IntCoalescingPTR, we don't need it, no interrupt */
818 out_be32(&uec->p_rx_glbl_pram->intcoalescingptr, 0);
820 /* RSTATE, global snooping, big endian, the CSB bus selected */
821 bmrx = BMR_INIT_VALUE;
822 out_8(&uec->p_rx_glbl_pram->rstate, bmrx);
825 out_be16(&uec->p_rx_glbl_pram->mrblr, MAX_RXBUF_LEN);
828 uec->rx_bd_qs_tbl_offset = qe_muram_alloc(
829 sizeof(uec_rx_bd_queues_entry_t) + \
830 sizeof(uec_rx_prefetched_bds_t),
831 UEC_RX_BD_QUEUES_ALIGNMENT);
832 uec->p_rx_bd_qs_tbl = (uec_rx_bd_queues_entry_t *)
833 qe_muram_addr(uec->rx_bd_qs_tbl_offset);
836 memset(uec->p_rx_bd_qs_tbl, 0, sizeof(uec_rx_bd_queues_entry_t) + \
837 sizeof(uec_rx_prefetched_bds_t));
838 out_be32(&uec->p_rx_glbl_pram->rbdqptr, uec->rx_bd_qs_tbl_offset);
839 out_be32(&uec->p_rx_bd_qs_tbl->externalbdbaseptr,
840 (u32)uec->p_rx_bd_ring);
843 out_be16(&uec->p_rx_glbl_pram->mflr, MAX_FRAME_LEN);
845 out_be16(&uec->p_rx_glbl_pram->minflr, MIN_FRAME_LEN);
847 out_be16(&uec->p_rx_glbl_pram->maxd1, MAX_DMA1_LEN);
849 out_be16(&uec->p_rx_glbl_pram->maxd2, MAX_DMA2_LEN);
851 out_be32(&uec->p_rx_glbl_pram->ecamptr, 0);
853 out_be32(&uec->p_rx_glbl_pram->l2qt, 0);
855 for (i = 0; i < 8; i++) {
856 out_be32(&uec->p_rx_glbl_pram->l3qt[i], 0);
860 out_be16(&uec->p_rx_glbl_pram->vlantype, 0x8100);
862 out_be16(&uec->p_rx_glbl_pram->vlantci, 0);
864 /* Clear PQ2 style address filtering hash table */
865 p_af_pram = (uec_82xx_address_filtering_pram_t *) \
866 uec->p_rx_glbl_pram->addressfiltering;
868 p_af_pram->iaddr_h = 0;
869 p_af_pram->iaddr_l = 0;
870 p_af_pram->gaddr_h = 0;
871 p_af_pram->gaddr_l = 0;
874 static int uec_issue_init_enet_rxtx_cmd(uec_private_t *uec,
875 int thread_tx, int thread_rx)
877 uec_init_cmd_pram_t *p_init_enet_param;
878 u32 init_enet_param_offset;
879 uec_info_t *uec_info;
882 u32 init_enet_offset;
887 uec_info = uec->uec_info;
889 /* Allocate init enet command parameter */
890 uec->init_enet_param_offset = qe_muram_alloc(
891 sizeof(uec_init_cmd_pram_t), 4);
892 init_enet_param_offset = uec->init_enet_param_offset;
893 uec->p_init_enet_param = (uec_init_cmd_pram_t *)
894 qe_muram_addr(uec->init_enet_param_offset);
896 /* Zero init enet command struct */
897 memset((void *)uec->p_init_enet_param, 0, sizeof(uec_init_cmd_pram_t));
899 /* Init the command struct */
900 p_init_enet_param = uec->p_init_enet_param;
901 p_init_enet_param->resinit0 = ENET_INIT_PARAM_MAGIC_RES_INIT0;
902 p_init_enet_param->resinit1 = ENET_INIT_PARAM_MAGIC_RES_INIT1;
903 p_init_enet_param->resinit2 = ENET_INIT_PARAM_MAGIC_RES_INIT2;
904 p_init_enet_param->resinit3 = ENET_INIT_PARAM_MAGIC_RES_INIT3;
905 p_init_enet_param->resinit4 = ENET_INIT_PARAM_MAGIC_RES_INIT4;
906 p_init_enet_param->largestexternallookupkeysize = 0;
908 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_rx)
909 << ENET_INIT_PARAM_RGF_SHIFT;
910 p_init_enet_param->rgftgfrxglobal |= ((u32)uec_info->num_threads_tx)
911 << ENET_INIT_PARAM_TGF_SHIFT;
913 /* Init Rx global parameter pointer */
914 p_init_enet_param->rgftgfrxglobal |= uec->rx_glbl_pram_offset |
915 (u32)uec_info->risc_rx;
917 /* Init Rx threads */
918 for (i = 0; i < (thread_rx + 1); i++) {
919 if ((snum = qe_get_snum()) < 0) {
920 printf("%s can not get snum\n", __FUNCTION__);
925 init_enet_offset = 0;
927 init_enet_offset = qe_muram_alloc(
928 sizeof(uec_thread_rx_pram_t),
929 UEC_THREAD_RX_PRAM_ALIGNMENT);
932 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
933 init_enet_offset | (u32)uec_info->risc_rx;
934 p_init_enet_param->rxthread[i] = entry_val;
937 /* Init Tx global parameter pointer */
938 p_init_enet_param->txglobal = uec->tx_glbl_pram_offset |
939 (u32)uec_info->risc_tx;
941 /* Init Tx threads */
942 for (i = 0; i < thread_tx; i++) {
943 if ((snum = qe_get_snum()) < 0) {
944 printf("%s can not get snum\n", __FUNCTION__);
948 init_enet_offset = qe_muram_alloc(sizeof(uec_thread_tx_pram_t),
949 UEC_THREAD_TX_PRAM_ALIGNMENT);
951 entry_val = ((u32)snum << ENET_INIT_PARAM_SNUM_SHIFT) |
952 init_enet_offset | (u32)uec_info->risc_tx;
953 p_init_enet_param->txthread[i] = entry_val;
956 __asm__ __volatile__("sync");
958 /* Issue QE command */
959 command = QE_INIT_TX_RX;
960 cecr_subblock = ucc_fast_get_qe_cr_subblock(
961 uec->uec_info->uf_info.ucc_num);
962 qe_issue_cmd(command, cecr_subblock, (u8) QE_CR_PROTOCOL_ETHERNET,
963 init_enet_param_offset);
968 static int uec_startup(uec_private_t *uec)
970 uec_info_t *uec_info;
971 ucc_fast_info_t *uf_info;
972 ucc_fast_private_t *uccf;
978 enet_interface_e enet_interface;
985 if (!uec || !uec->uec_info) {
986 printf("%s: uec or uec_info not initial\n", __FUNCTION__);
990 uec_info = uec->uec_info;
991 uf_info = &(uec_info->uf_info);
993 /* Check if Rx BD ring len is illegal */
994 if ((uec_info->rx_bd_ring_len < UEC_RX_BD_RING_SIZE_MIN) || \
995 (uec_info->rx_bd_ring_len % UEC_RX_BD_RING_SIZE_ALIGNMENT)) {
996 printf("%s: Rx BD ring len must be multiple of 4, and > 8.\n",
1001 /* Check if Tx BD ring len is illegal */
1002 if (uec_info->tx_bd_ring_len < UEC_TX_BD_RING_SIZE_MIN) {
1003 printf("%s: Tx BD ring length must not be smaller than 2.\n",
1008 /* Check if MRBLR is illegal */
1009 if ((MAX_RXBUF_LEN == 0) || (MAX_RXBUF_LEN % UEC_MRBLR_ALIGNMENT)) {
1010 printf("%s: max rx buffer length must be mutliple of 128.\n",
1015 /* Both Rx and Tx are stopped */
1016 uec->grace_stopped_rx = 1;
1017 uec->grace_stopped_tx = 1;
1020 if (ucc_fast_init(uf_info, &uccf)) {
1021 printf("%s: failed to init ucc fast\n", __FUNCTION__);
1028 /* Convert the Tx threads number */
1029 if (uec_convert_threads_num(uec_info->num_threads_tx,
1034 /* Convert the Rx threads number */
1035 if (uec_convert_threads_num(uec_info->num_threads_rx,
1040 uf_regs = uccf->uf_regs;
1042 /* UEC register is following UCC fast registers */
1043 uec_regs = (uec_t *)(&uf_regs->ucc_eth);
1045 /* Save the UEC register pointer to UEC private struct */
1046 uec->uec_regs = uec_regs;
1048 /* Init UPSMR, enable hardware statistics (UCC) */
1049 out_be32(&uec->uccf->uf_regs->upsmr, UPSMR_INIT_VALUE);
1051 /* Init MACCFG1, flow control disable, disable Tx and Rx */
1052 out_be32(&uec_regs->maccfg1, MACCFG1_INIT_VALUE);
1054 /* Init MACCFG2, length check, MAC PAD and CRC enable */
1055 out_be32(&uec_regs->maccfg2, MACCFG2_INIT_VALUE);
1057 /* Setup MAC interface mode */
1058 uec_set_mac_if_mode(uec, uec_info->enet_interface);
1060 /* Setup MII management base */
1061 #ifndef CONFIG_eTSEC_MDIO_BUS
1062 uec->uec_mii_regs = (uec_mii_t *)(&uec_regs->miimcfg);
1064 uec->uec_mii_regs = (uec_mii_t *) CONFIG_MIIM_ADDRESS;
1067 /* Setup MII master clock source */
1068 qe_set_mii_clk_src(uec_info->uf_info.ucc_num);
1071 utbipar = in_be32(&uec_regs->utbipar);
1072 utbipar &= ~UTBIPAR_PHY_ADDRESS_MASK;
1073 enet_interface = uec->uec_info->enet_interface;
1074 if (enet_interface == ENET_1000_TBI ||
1075 enet_interface == ENET_1000_RTBI) {
1076 utbipar |= (uec_info->phy_address + uec_info->uf_info.ucc_num)
1077 << UTBIPAR_PHY_ADDRESS_SHIFT;
1079 utbipar |= (0x10 + uec_info->uf_info.ucc_num)
1080 << UTBIPAR_PHY_ADDRESS_SHIFT;
1083 out_be32(&uec_regs->utbipar, utbipar);
1085 /* Configure the TBI for SGMII operation */
1086 if (uec->uec_info->enet_interface == ENET_1000_SGMII) {
1087 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1088 ENET_TBI_MII_ANA, TBIANA_SETTINGS);
1090 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1091 ENET_TBI_MII_TBICON, TBICON_CLK_SELECT);
1093 uec_write_phy_reg(uec->dev, uec_regs->utbipar,
1094 ENET_TBI_MII_CR, TBICR_SETTINGS);
1097 /* Allocate Tx BDs */
1098 length = ((uec_info->tx_bd_ring_len * SIZEOFBD) /
1099 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) *
1100 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1101 if ((uec_info->tx_bd_ring_len * SIZEOFBD) %
1102 UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT) {
1103 length += UEC_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
1106 align = UEC_TX_BD_RING_ALIGNMENT;
1107 uec->tx_bd_ring_offset = (u32)malloc((u32)(length + align));
1108 if (uec->tx_bd_ring_offset != 0) {
1109 uec->p_tx_bd_ring = (u8 *)((uec->tx_bd_ring_offset + align)
1113 /* Zero all of Tx BDs */
1114 memset((void *)(uec->tx_bd_ring_offset), 0, length + align);
1116 /* Allocate Rx BDs */
1117 length = uec_info->rx_bd_ring_len * SIZEOFBD;
1118 align = UEC_RX_BD_RING_ALIGNMENT;
1119 uec->rx_bd_ring_offset = (u32)(malloc((u32)(length + align)));
1120 if (uec->rx_bd_ring_offset != 0) {
1121 uec->p_rx_bd_ring = (u8 *)((uec->rx_bd_ring_offset + align)
1125 /* Zero all of Rx BDs */
1126 memset((void *)(uec->rx_bd_ring_offset), 0, length + align);
1128 /* Allocate Rx buffer */
1129 length = uec_info->rx_bd_ring_len * MAX_RXBUF_LEN;
1130 align = UEC_RX_DATA_BUF_ALIGNMENT;
1131 uec->rx_buf_offset = (u32)malloc(length + align);
1132 if (uec->rx_buf_offset != 0) {
1133 uec->p_rx_buf = (u8 *)((uec->rx_buf_offset + align)
1137 /* Zero all of the Rx buffer */
1138 memset((void *)(uec->rx_buf_offset), 0, length + align);
1140 /* Init TxBD ring */
1141 bd = (qe_bd_t *)uec->p_tx_bd_ring;
1144 for (i = 0; i < uec_info->tx_bd_ring_len; i++) {
1146 BD_STATUS_SET(bd, 0);
1147 BD_LENGTH_SET(bd, 0);
1150 BD_STATUS_SET((--bd), TxBD_WRAP);
1152 /* Init RxBD ring */
1153 bd = (qe_bd_t *)uec->p_rx_bd_ring;
1155 buf = uec->p_rx_buf;
1156 for (i = 0; i < uec_info->rx_bd_ring_len; i++) {
1157 BD_DATA_SET(bd, buf);
1158 BD_LENGTH_SET(bd, 0);
1159 BD_STATUS_SET(bd, RxBD_EMPTY);
1160 buf += MAX_RXBUF_LEN;
1163 BD_STATUS_SET((--bd), RxBD_WRAP | RxBD_EMPTY);
1165 /* Init global Tx parameter RAM */
1166 uec_init_tx_parameter(uec, num_threads_tx);
1168 /* Init global Rx parameter RAM */
1169 uec_init_rx_parameter(uec, num_threads_rx);
1171 /* Init ethernet Tx and Rx parameter command */
1172 if (uec_issue_init_enet_rxtx_cmd(uec, num_threads_tx,
1174 printf("%s issue init enet cmd failed\n", __FUNCTION__);
1181 static int uec_init(struct eth_device* dev, bd_t *bd)
1185 struct phy_info *curphy;
1187 uec = (uec_private_t *)dev->priv;
1189 if (uec->the_first_run == 0) {
1190 err = init_phy(dev);
1192 printf("%s: Cannot initialize PHY, aborting.\n",
1197 curphy = uec->mii_info->phyinfo;
1199 if (curphy->config_aneg) {
1200 err = curphy->config_aneg(uec->mii_info);
1202 printf("%s: Can't negotiate PHY\n", dev->name);
1207 /* Give PHYs up to 5 sec to report a link */
1210 err = curphy->read_status(uec->mii_info);
1212 } while (((i-- > 0) && !uec->mii_info->link) || err);
1215 printf("warning: %s: timeout on PHY link\n", dev->name);
1217 uec->the_first_run = 1;
1220 /* Set up the MAC address */
1221 if (dev->enetaddr[0] & 0x01) {
1222 printf("%s: MacAddress is multcast address\n",
1226 uec_set_mac_address(uec, dev->enetaddr);
1229 err = uec_open(uec, COMM_DIR_RX_AND_TX);
1231 printf("%s: cannot enable UEC device\n", dev->name);
1237 return (uec->mii_info->link ? 0 : -1);
1240 static void uec_halt(struct eth_device* dev)
1242 uec_private_t *uec = (uec_private_t *)dev->priv;
1243 uec_stop(uec, COMM_DIR_RX_AND_TX);
1246 static int uec_send(struct eth_device* dev, volatile void *buf, int len)
1249 ucc_fast_private_t *uccf;
1250 volatile qe_bd_t *bd;
1255 uec = (uec_private_t *)dev->priv;
1259 /* Find an empty TxBD */
1260 for (i = 0; bd->status & TxBD_READY; i++) {
1262 printf("%s: tx buffer not ready\n", dev->name);
1268 BD_DATA_SET(bd, buf);
1269 BD_LENGTH_SET(bd, len);
1270 status = bd->status;
1272 status |= (TxBD_READY | TxBD_LAST);
1273 BD_STATUS_SET(bd, status);
1275 /* Tell UCC to transmit the buffer */
1276 ucc_fast_transmit_on_demand(uccf);
1278 /* Wait for buffer to be transmitted */
1279 for (i = 0; bd->status & TxBD_READY; i++) {
1281 printf("%s: tx error\n", dev->name);
1286 /* Ok, the buffer be transimitted */
1287 BD_ADVANCE(bd, status, uec->p_tx_bd_ring);
1294 static int uec_recv(struct eth_device* dev)
1296 uec_private_t *uec = dev->priv;
1297 volatile qe_bd_t *bd;
1303 status = bd->status;
1305 while (!(status & RxBD_EMPTY)) {
1306 if (!(status & RxBD_ERROR)) {
1308 len = BD_LENGTH(bd);
1309 NetReceive(data, len);
1311 printf("%s: Rx error\n", dev->name);
1314 BD_LENGTH_SET(bd, 0);
1315 BD_STATUS_SET(bd, status | RxBD_EMPTY);
1316 BD_ADVANCE(bd, status, uec->p_rx_bd_ring);
1317 status = bd->status;
1324 int uec_initialize(bd_t *bis, uec_info_t *uec_info)
1326 struct eth_device *dev;
1331 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1334 memset(dev, 0, sizeof(struct eth_device));
1336 /* Allocate the UEC private struct */
1337 uec = (uec_private_t *)malloc(sizeof(uec_private_t));
1341 memset(uec, 0, sizeof(uec_private_t));
1343 /* Adjust uec_info */
1344 #if (MAX_QE_RISC == 4)
1345 uec_info->risc_tx = QE_RISC_ALLOCATION_FOUR_RISCS;
1346 uec_info->risc_rx = QE_RISC_ALLOCATION_FOUR_RISCS;
1349 devlist[uec_info->uf_info.ucc_num] = dev;
1351 uec->uec_info = uec_info;
1354 sprintf(dev->name, "FSL UEC%d", uec_info->uf_info.ucc_num);
1356 dev->priv = (void *)uec;
1357 dev->init = uec_init;
1358 dev->halt = uec_halt;
1359 dev->send = uec_send;
1360 dev->recv = uec_recv;
1362 /* Clear the ethnet address */
1363 for (i = 0; i < 6; i++)
1364 dev->enetaddr[i] = 0;
1368 err = uec_startup(uec);
1370 printf("%s: Cannot configure net device, aborting.",dev->name);
1374 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
1375 && !defined(BITBANGMII)
1376 miiphy_register(dev->name, uec_miiphy_read, uec_miiphy_write);
1382 int uec_eth_init(bd_t *bis, uec_info_t *uecs, int num)
1386 for (i = 0; i < num; i++)
1387 uec_initialize(bis, &uecs[i]);
1392 int uec_standard_init(bd_t *bis)
1394 return uec_eth_init(bis, uec_info, ARRAY_SIZE(uec_info));