1 // SPDX-License-Identifier: GPL-2.0+
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
8 * Ingo Assmus <ingo.assmus@keymile.com>
10 * based on - Driver for MV64360X ethernet ports
11 * Copyright (C) 2002 rabeeh@galileo.co.il
21 #include <linux/errno.h>
22 #include <asm/types.h>
23 #include <asm/system.h>
24 #include <asm/byteorder.h>
25 #include <asm/arch/cpu.h>
27 #if defined(CONFIG_KIRKWOOD)
28 #include <asm/arch/soc.h>
29 #elif defined(CONFIG_ORION5X)
30 #include <asm/arch/orion5x.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 #ifndef CONFIG_MVGBE_PORTS
38 # define CONFIG_MVGBE_PORTS {0, 0}
41 #define MV_PHY_ADR_REQUEST 0xee
42 #define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
44 #if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
45 static int smi_wait_ready(struct mvgbe_device *dmvgbe)
49 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
50 MVGBE_PHY_SMI_TIMEOUT_MS, false);
52 printf("Error: SMI busy timeout\n");
59 static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
60 int devad, int reg_ofs)
62 struct mvgbe_registers *regs = dmvgbe->regs;
67 /* Phyadr read request */
68 if (phy_adr == MV_PHY_ADR_REQUEST &&
69 reg_ofs == MV_PHY_ADR_REQUEST) {
71 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
74 /* check parameters */
75 if (phy_adr > PHYADR_MASK) {
76 printf("Err..(%s) Invalid PHY address %d\n",
80 if (reg_ofs > PHYREG_MASK) {
81 printf("Err..(%s) Invalid register offset %d\n",
86 /* wait till the SMI is not busy */
87 if (smi_wait_ready(dmvgbe) < 0)
90 /* fill the phy address and regiser offset and read opcode */
91 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
92 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
93 | MVGBE_PHY_SMI_OPCODE_READ;
95 /* write the smi register */
96 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
98 /*wait till read value is ready */
99 timeout = MVGBE_PHY_SMI_TIMEOUT;
102 /* read smi register */
103 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
104 if (timeout-- == 0) {
105 printf("Err..(%s) SMI read ready timeout\n",
109 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
111 /* Wait for the data to update in the SMI register */
112 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
115 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
117 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
124 * smi_reg_read - miiphy_read callback function.
126 * Returns 16bit phy register value, or -EFAULT on error
128 static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
132 struct mvgbe_device *dmvgbe = bus->priv;
134 struct eth_device *dev = eth_get_dev_by_name(bus->name);
135 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
138 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
141 static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
142 int devad, int reg_ofs, u16 data)
144 struct mvgbe_registers *regs = dmvgbe->regs;
147 /* Phyadr write request*/
148 if (phy_adr == MV_PHY_ADR_REQUEST &&
149 reg_ofs == MV_PHY_ADR_REQUEST) {
150 MVGBE_REG_WR(regs->phyadr, data);
154 /* check parameters */
155 if (phy_adr > PHYADR_MASK) {
156 printf("Err..(%s) Invalid phy address\n", __func__);
159 if (reg_ofs > PHYREG_MASK) {
160 printf("Err..(%s) Invalid register offset\n", __func__);
164 /* wait till the SMI is not busy */
165 if (smi_wait_ready(dmvgbe) < 0)
168 /* fill the phy addr and reg offset and write opcode and data */
169 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
170 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
171 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
172 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
174 /* write the smi register */
175 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
181 * smi_reg_write - miiphy_write callback function.
183 * Returns 0 if write succeed, -EFAULT on error
185 static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
186 int reg_ofs, u16 data)
189 struct mvgbe_device *dmvgbe = bus->priv;
191 struct eth_device *dev = eth_get_dev_by_name(bus->name);
192 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
195 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
199 /* Stop and checks all queues */
200 static void stop_queue(u32 * qreg)
204 reg_data = readl(qreg);
206 if (reg_data & 0xFF) {
207 /* Issue stop command for active channels only */
208 writel((reg_data << 8), qreg);
210 /* Wait for all queue activity to terminate. */
213 * Check port cause register that all queues
216 reg_data = readl(qreg);
218 while (reg_data & 0xFF);
223 * set_access_control - Config address decode parameters for Ethernet unit
225 * This function configures the address decode parameters for the Gigabit
226 * Ethernet Controller according the given parameters struct.
228 * @regs Register struct pointer.
229 * @param Address decode parameter struct.
231 static void set_access_control(struct mvgbe_registers *regs,
232 struct mvgbe_winparam *param)
236 /* Set access control register */
237 access_prot_reg = MVGBE_REG_RD(regs->epap);
238 /* clear window permission */
239 access_prot_reg &= (~(3 << (param->win * 2)));
240 access_prot_reg |= (param->access_ctrl << (param->win * 2));
241 MVGBE_REG_WR(regs->epap, access_prot_reg);
243 /* Set window Size reg (SR) */
244 MVGBE_REG_WR(regs->barsz[param->win].size,
245 (((param->size / 0x10000) - 1) << 16));
247 /* Set window Base address reg (BA) */
248 MVGBE_REG_WR(regs->barsz[param->win].bar,
249 (param->target | param->attrib | param->base_addr));
250 /* High address remap reg (HARR) */
252 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
254 /* Base address enable reg (BARER) */
255 if (param->enable == 1)
256 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
258 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
261 static void set_dram_access(struct mvgbe_registers *regs)
263 struct mvgbe_winparam win_param;
266 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
267 /* Set access parameters for DRAM bank i */
268 win_param.win = i; /* Use Ethernet window i */
269 /* Window target - DDR */
270 win_param.target = MVGBE_TARGET_DRAM;
271 /* Enable full access */
272 win_param.access_ctrl = EWIN_ACCESS_FULL;
273 win_param.high_addr = 0;
274 /* Get bank base and size */
275 win_param.base_addr = gd->bd->bi_dram[i].start;
276 win_param.size = gd->bd->bi_dram[i].size;
277 if (win_param.size == 0)
278 win_param.enable = 0;
280 win_param.enable = 1; /* Enable the access */
282 /* Enable DRAM bank */
285 win_param.attrib = EBAR_DRAM_CS0;
288 win_param.attrib = EBAR_DRAM_CS1;
291 win_param.attrib = EBAR_DRAM_CS2;
294 win_param.attrib = EBAR_DRAM_CS3;
297 /* invalid bank, disable access */
298 win_param.enable = 0;
299 win_param.attrib = 0;
302 /* Set the access control for address window(EPAPR) RD/WR */
303 set_access_control(regs, &win_param);
308 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
310 * Go through all the DA filter tables (Unicast, Special Multicast & Other
311 * Multicast) and set each entry to 0.
313 static void port_init_mac_tables(struct mvgbe_registers *regs)
317 /* Clear DA filter unicast table (Ex_dFUT) */
318 for (table_index = 0; table_index < 4; ++table_index)
319 MVGBE_REG_WR(regs->dfut[table_index], 0);
321 for (table_index = 0; table_index < 64; ++table_index) {
322 /* Clear DA filter special multicast table (Ex_dFSMT) */
323 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
324 /* Clear DA filter other multicast table (Ex_dFOMT) */
325 MVGBE_REG_WR(regs->dfomt[table_index], 0);
330 * port_uc_addr - This function Set the port unicast address table
332 * This function locates the proper entry in the Unicast table for the
333 * specified MAC nibble and sets its properties according to function
335 * This function add/removes MAC addresses from the port unicast address
338 * @uc_nibble Unicast MAC Address last nibble.
339 * @option 0 = Add, 1 = remove address.
341 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
343 static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
350 /* Locate the Unicast table entry */
351 uc_nibble = (0xf & uc_nibble);
352 /* Register offset from unicast table base */
353 tbl_offset = (uc_nibble / 4);
354 /* Entry offset within the above register */
355 reg_offset = uc_nibble % 4;
358 case REJECT_MAC_ADDR:
360 * Clear accepts frame bit at specified unicast
363 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
364 unicast_reg &= (0xFF << (8 * reg_offset));
365 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
367 case ACCEPT_MAC_ADDR:
368 /* Set accepts frame bit at unicast DA filter table entry */
369 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
370 unicast_reg &= (0xFF << (8 * reg_offset));
371 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
372 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
381 * port_uc_addr_set - This function Set the port Unicast address.
383 static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
385 struct mvgbe_registers *regs = dmvgbe->regs;
389 mac_l = (p_addr[4] << 8) | (p_addr[5]);
390 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
393 MVGBE_REG_WR(regs->macal, mac_l);
394 MVGBE_REG_WR(regs->macah, mac_h);
396 /* Accept frames of this address */
397 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
401 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
403 static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
405 struct mvgbe_rxdesc *p_rx_desc;
408 /* initialize the Rx descriptors ring */
409 p_rx_desc = dmvgbe->p_rxdesc;
410 for (i = 0; i < RINGSZ; i++) {
412 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
413 p_rx_desc->buf_size = PKTSIZE_ALIGN;
414 p_rx_desc->byte_cnt = 0;
415 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
416 if (i == (RINGSZ - 1))
417 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
419 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
420 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
421 p_rx_desc = p_rx_desc->nxtdesc_p;
424 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
427 static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
430 struct mvgbe_registers *regs = dmvgbe->regs;
431 #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
432 !defined(CONFIG_PHYLIB) && \
433 !defined(CONFIG_DM_ETH) && \
434 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
438 mvgbe_init_rx_desc_ring(dmvgbe);
440 /* Clear the ethernet port interrupts */
441 MVGBE_REG_WR(regs->ic, 0);
442 MVGBE_REG_WR(regs->ice, 0);
443 /* Unmask RX buffer and TX end interrupt */
444 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
445 /* Unmask phy and link status changes interrupts */
446 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
448 set_dram_access(regs);
449 port_init_mac_tables(regs);
450 port_uc_addr_set(dmvgbe, enetaddr);
452 /* Assign port configuration and command. */
453 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
454 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
455 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
457 /* Assign port SDMA configuration */
458 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
459 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
460 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
461 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
462 /* Turn off the port/RXUQ bandwidth limitation */
463 MVGBE_REG_WR(regs->pmtu, 0);
465 /* Set maximum receive buffer to 9700 bytes */
466 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
467 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
469 /* Enable port initially */
470 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
473 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
474 * disable the leaky bucket mechanism .
476 MVGBE_REG_WR(regs->pmtu, 0);
478 /* Assignment of Rx CRDB of given RXUQ */
479 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
480 /* ensure previous write is done before enabling Rx DMA */
482 /* Enable port Rx. */
483 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
485 #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
486 !defined(CONFIG_PHYLIB) && \
487 !defined(CONFIG_DM_ETH) && \
488 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
489 /* Wait up to 5s for the link status */
490 for (i = 0; i < 5; i++) {
493 miiphy_read(name, MV_PHY_ADR_REQUEST,
494 MV_PHY_ADR_REQUEST, &phyadr);
495 /* Return if we get link up */
496 if (miiphy_link(name, phyadr))
501 printf("No link on %s\n", name);
507 #ifndef CONFIG_DM_ETH
508 static int mvgbe_init(struct eth_device *dev)
510 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
512 return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name);
516 static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
518 struct mvgbe_registers *regs = dmvgbe->regs;
520 /* Disable all gigE address decoder */
521 MVGBE_REG_WR(regs->bare, 0x3f);
523 stop_queue(®s->tqc);
524 stop_queue(®s->rqc);
527 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
528 /* Set port is not reset */
529 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
530 #ifdef CONFIG_SYS_MII_MODE
531 /* Set MMI interface up */
532 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
534 /* Disable & mask ethernet port interrupts */
535 MVGBE_REG_WR(regs->ic, 0);
536 MVGBE_REG_WR(regs->ice, 0);
537 MVGBE_REG_WR(regs->pim, 0);
538 MVGBE_REG_WR(regs->peim, 0);
541 #ifndef CONFIG_DM_ETH
542 static int mvgbe_halt(struct eth_device *dev)
544 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
546 __mvgbe_halt(dmvgbe);
553 static int mvgbe_write_hwaddr(struct udevice *dev)
555 struct eth_pdata *pdata = dev_get_platdata(dev);
557 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
562 static int mvgbe_write_hwaddr(struct eth_device *dev)
564 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
566 /* Programs net device MAC address after initialization */
567 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
572 static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
575 struct mvgbe_registers *regs = dmvgbe->regs;
576 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
577 void *p = (void *)dataptr;
581 /* Copy buffer if it's misaligned */
582 if ((u32) dataptr & 0x07) {
583 if (datasize > PKTSIZE_ALIGN) {
584 printf("Non-aligned data too large (%d)\n",
589 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
590 p = dmvgbe->p_aligned_txbuf;
593 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
594 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
595 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
596 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
597 p_txdesc->buf_ptr = (u8 *) p;
598 p_txdesc->byte_cnt = datasize;
600 /* Set this tc desc as zeroth TXUQ */
601 txuq0_reg_addr = (u32)®s->tcqdp[TXUQ];
602 writel((u32) p_txdesc, txuq0_reg_addr);
604 /* ensure tx desc writes above are performed before we start Tx DMA */
607 /* Apply send command using zeroth TXUQ */
608 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
611 * wait for packet xmit completion
613 cmd_sts = readl(&p_txdesc->cmd_sts);
614 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
615 /* return fail if error is detected */
616 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
617 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
618 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
619 printf("Err..(%s) in xmit packet\n", __func__);
622 cmd_sts = readl(&p_txdesc->cmd_sts);
627 #ifndef CONFIG_DM_ETH
628 static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
630 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
632 return __mvgbe_send(dmvgbe, dataptr, datasize);
636 static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
638 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
641 u32 rxdesc_curr_addr;
647 /* wait untill rx packet available or timeout */
649 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
652 debug("%s time out...\n", __func__);
655 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
657 if (p_rxdesc_curr->byte_cnt != 0) {
658 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
659 __func__, (u32) p_rxdesc_curr->byte_cnt,
660 (u32) p_rxdesc_curr->buf_ptr,
661 (u32) p_rxdesc_curr->cmd_sts);
665 * In case received a packet without first/last bits on
666 * OR the error summary bit is on,
667 * the packets needs to be dropeed.
669 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
672 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
673 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
675 printf("Err..(%s) Dropping packet spread on"
676 " multiple descriptors\n", __func__);
678 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
680 printf("Err..(%s) Dropping packet with errors\n",
684 /* !!! call higher layer processing */
685 debug("%s: Sending Received packet to"
686 " upper layer (net_process_received_packet)\n",
689 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
690 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
696 * free these descriptors and point next in the ring
698 p_rxdesc_curr->cmd_sts =
699 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
700 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
701 p_rxdesc_curr->byte_cnt = 0;
703 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
704 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
709 #ifndef CONFIG_DM_ETH
710 static int mvgbe_recv(struct eth_device *dev)
712 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
716 ret = __mvgbe_recv(dmvgbe, &packet);
720 net_process_received_packet(packet, ret);
726 #if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH)
727 #if defined(CONFIG_DM_ETH)
728 static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
730 phy_interface_t phy_interface,
733 static struct phy_device *__mvgbe_phy_init(struct eth_device *dev,
735 phy_interface_t phy_interface,
739 struct phy_device *phydev;
741 /* Set phy address of the port */
742 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
745 phydev = phy_connect(bus, phyid, dev, phy_interface);
747 printf("phy_connect failed\n");
756 #endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */
758 #if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH)
759 int mvgbe_phylib_init(struct eth_device *dev, int phyid)
762 struct phy_device *phydev;
767 printf("mdio_alloc failed\n");
770 bus->read = smi_reg_read;
771 bus->write = smi_reg_write;
772 strcpy(bus->name, dev->name);
774 ret = mdio_register(bus);
776 printf("mdio_register failed\n");
781 phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid);
789 static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
791 dmvgbe->p_rxdesc = memalign(PKTALIGN,
792 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
793 if (!dmvgbe->p_rxdesc)
796 dmvgbe->p_rxbuf = memalign(PKTALIGN,
797 RINGSZ * PKTSIZE_ALIGN + 1);
798 if (!dmvgbe->p_rxbuf)
801 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
802 if (!dmvgbe->p_aligned_txbuf)
805 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
806 if (!dmvgbe->p_txdesc)
812 free(dmvgbe->p_aligned_txbuf);
814 free(dmvgbe->p_rxbuf);
816 free(dmvgbe->p_rxdesc);
821 #ifndef CONFIG_DM_ETH
822 int mvgbe_initialize(bd_t *bis)
824 struct mvgbe_device *dmvgbe;
825 struct eth_device *dev;
828 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
830 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
831 /*skip if port is configured not to use */
832 if (used_ports[devnum] == 0)
835 dmvgbe = malloc(sizeof(struct mvgbe_device));
839 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
840 ret = mvgbe_alloc_buffers(dmvgbe);
842 printf("Err.. %s Failed to allocate memory\n",
850 /* must be less than sizeof(dev->name) */
851 sprintf(dev->name, "egiga%d", devnum);
855 dmvgbe->regs = (void *)MVGBE0_BASE;
857 #if defined(MVGBE1_BASE)
859 dmvgbe->regs = (void *)MVGBE1_BASE;
862 default: /* this should never happen */
863 printf("Err..(%s) Invalid device number %d\n",
868 dev->init = (void *)mvgbe_init;
869 dev->halt = (void *)mvgbe_halt;
870 dev->send = (void *)mvgbe_send;
871 dev->recv = (void *)mvgbe_recv;
872 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
876 #if defined(CONFIG_PHYLIB)
877 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
878 #elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
880 struct mii_dev *mdiodev = mdio_alloc();
883 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
884 mdiodev->read = smi_reg_read;
885 mdiodev->write = smi_reg_write;
887 retval = mdio_register(mdiodev);
890 /* Set phy address of the port */
891 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
892 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
900 static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
902 return dmvgbe->phyaddr > PHY_MAX_ADDR;
905 static int mvgbe_start(struct udevice *dev)
907 struct eth_pdata *pdata = dev_get_platdata(dev);
908 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
911 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
915 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
916 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
917 dmvgbe->phy_interface,
926 static int mvgbe_send(struct udevice *dev, void *packet, int length)
928 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
930 return __mvgbe_send(dmvgbe, packet, length);
933 static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
935 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
937 return __mvgbe_recv(dmvgbe, packetp);
940 static void mvgbe_stop(struct udevice *dev)
942 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
944 __mvgbe_halt(dmvgbe);
947 static int mvgbe_probe(struct udevice *dev)
949 struct eth_pdata *pdata = dev_get_platdata(dev);
950 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
954 ret = mvgbe_alloc_buffers(dmvgbe);
958 dmvgbe->regs = (void __iomem *)pdata->iobase;
962 printf("Failed to allocate MDIO bus\n");
966 bus->read = smi_reg_read;
967 bus->write = smi_reg_write;
968 snprintf(bus->name, sizeof(bus->name), dev->name);
972 ret = mdio_register(bus);
979 static const struct eth_ops mvgbe_ops = {
980 .start = mvgbe_start,
984 .write_hwaddr = mvgbe_write_hwaddr,
987 static int mvgbe_ofdata_to_platdata(struct udevice *dev)
989 struct eth_pdata *pdata = dev_get_platdata(dev);
990 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
991 void *blob = (void *)gd->fdt_blob;
992 int node = dev_of_offset(dev);
993 const char *phy_mode;
998 pdata->iobase = devfdt_get_addr(dev);
999 pdata->phy_interface = -1;
1001 pnode = fdt_node_offset_by_compatible(blob, node,
1002 "marvell,kirkwood-eth-port");
1004 /* Get phy-mode / phy_interface from DT */
1005 phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL);
1007 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1009 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
1011 dmvgbe->phy_interface = pdata->phy_interface;
1013 /* fetch 'fixed-link' property */
1014 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
1015 if (fl_node != -FDT_ERR_NOTFOUND) {
1016 /* set phy_addr to invalid value for fixed link */
1017 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
1018 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1019 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1021 /* Now read phyaddr from DT */
1022 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
1024 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1030 static const struct udevice_id mvgbe_ids[] = {
1031 { .compatible = "marvell,kirkwood-eth" },
1035 U_BOOT_DRIVER(mvgbe) = {
1038 .of_match = mvgbe_ids,
1039 .ofdata_to_platdata = mvgbe_ofdata_to_platdata,
1040 .probe = mvgbe_probe,
1042 .priv_auto_alloc_size = sizeof(struct mvgbe_device),
1043 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1045 #endif /* CONFIG_DM_ETH */