1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
11 #include <linux/delay.h>
19 #include "bcm-sf2-eth.h"
21 #if defined(CONFIG_BCM_SF2_ETH_GMAC)
22 #include "bcm-sf2-eth-gmac.h"
24 #error "bcm_sf2_eth: NEED to define a MAC!"
27 #define BCM_NET_MODULE_DESCRIPTION "Broadcom Starfighter2 Ethernet driver"
28 #define BCM_NET_MODULE_VERSION "0.1"
29 #define BCM_SF2_ETH_DEV_NAME "bcm_sf2"
31 static const char banner[] =
32 BCM_NET_MODULE_DESCRIPTION " " BCM_NET_MODULE_VERSION "\n";
34 static int bcm_sf2_eth_init(struct eth_device *dev)
36 struct eth_info *eth = (struct eth_info *)(dev->priv);
37 struct eth_dma *dma = &(eth->dma);
38 struct phy_device *phydev;
42 rc = eth->mac_init(dev);
44 pr_err("%s: Couldn't cofigure MAC!\n", __func__);
49 dma->disable_dma(dma, MAC_DMA_RX);
50 dma->disable_dma(dma, MAC_DMA_TX);
53 debug("Connecting PHY 0...\n");
54 phydev = phy_connect(miiphy_get_dev_by_name(dev->name),
55 -1, dev, eth->phy_interface);
57 eth->port[0] = phydev;
60 debug("No PHY found for port 0\n");
63 for (i = 0; i < eth->port_num; i++)
64 phy_config(eth->port[i]);
70 * u-boot net functions
73 static int bcm_sf2_eth_send(struct eth_device *dev, void *packet, int length)
75 struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
76 uint8_t *buf = (uint8_t *)packet;
80 debug("%s enter\n", __func__);
82 /* load buf and start transmit */
83 rc = dma->tx_packet(dma, buf, length);
85 debug("ERROR - Tx failed\n");
89 while (!(dma->check_tx_done(dma))) {
94 pr_err("%s: Tx timeout: retried 20 times\n", __func__);
100 debug("%s exit rc(0x%x)\n", __func__, rc);
104 static int bcm_sf2_eth_receive(struct eth_device *dev)
106 struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
107 uint8_t *buf = (uint8_t *)net_rx_packets[0];
113 /* Poll Rx queue to get a packet */
114 rcvlen = dma->check_rx_done(dma, buf);
116 /* No packet received */
118 debug("\nNO More Rx\n");
120 } else if ((rcvlen == 0) || (rcvlen > RX_BUF_SIZE)) {
121 pr_err("%s: Wrong Ethernet packet size (%d B), skip!\n",
127 /* Forward received packet to uboot network handler */
128 net_process_received_packet(buf, rcvlen);
130 if (++i >= PKTBUFSRX)
132 buf = net_rx_packets[i];
139 static int bcm_sf2_eth_write_hwaddr(struct eth_device *dev)
141 struct eth_info *eth = (struct eth_info *)(dev->priv);
143 printf(" ETH MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
144 dev->enetaddr[0], dev->enetaddr[1], dev->enetaddr[2],
145 dev->enetaddr[3], dev->enetaddr[4], dev->enetaddr[5]);
147 return eth->set_mac_addr(dev->enetaddr);
150 static int bcm_sf2_eth_open(struct eth_device *dev, bd_t *bt)
152 struct eth_info *eth = (struct eth_info *)(dev->priv);
153 struct eth_dma *dma = &(eth->dma);
156 debug("Enabling BCM SF2 Ethernet.\n");
160 /* enable tx and rx DMA */
161 dma->enable_dma(dma, MAC_DMA_RX);
162 dma->enable_dma(dma, MAC_DMA_TX);
165 * Need to start PHY here because link speed can change
166 * before each ethernet operation
168 for (i = 0; i < eth->port_num; i++) {
169 if (phy_startup(eth->port[i])) {
170 pr_err("%s: PHY %d startup failed!\n", __func__, i);
171 if (i == CONFIG_BCM_SF2_ETH_DEFAULT_PORT) {
172 pr_err("%s: No default port %d!\n", __func__, i);
178 /* Set MAC speed using default port */
179 i = CONFIG_BCM_SF2_ETH_DEFAULT_PORT;
180 debug("PHY %d: speed:%d, duplex:%d, link:%d\n", i,
181 eth->port[i]->speed, eth->port[i]->duplex, eth->port[i]->link);
182 eth->set_mac_speed(eth->port[i]->speed, eth->port[i]->duplex);
184 debug("Enable Ethernet Done.\n");
189 static void bcm_sf2_eth_close(struct eth_device *dev)
191 struct eth_info *eth = (struct eth_info *)(dev->priv);
192 struct eth_dma *dma = &(eth->dma);
195 dma->disable_dma(dma, MAC_DMA_RX);
196 dma->disable_dma(dma, MAC_DMA_TX);
201 int bcm_sf2_eth_register(bd_t *bis, u8 dev_num)
203 struct eth_device *dev;
204 struct eth_info *eth;
207 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
209 pr_err("%s: Not enough memory!\n", __func__);
213 eth = (struct eth_info *)malloc(sizeof(struct eth_info));
215 pr_err("%s: Not enough memory!\n", __func__);
221 memset(dev, 0, sizeof(*dev));
222 sprintf(dev->name, "%s_%s-%hu", BCM_SF2_ETH_DEV_NAME,
223 BCM_SF2_ETH_MAC_NAME, dev_num);
225 dev->priv = (void *)eth;
228 dev->init = bcm_sf2_eth_open;
229 dev->halt = bcm_sf2_eth_close;
230 dev->send = bcm_sf2_eth_send;
231 dev->recv = bcm_sf2_eth_receive;
232 dev->write_hwaddr = bcm_sf2_eth_write_hwaddr;
234 #ifdef CONFIG_BCM_SF2_ETH_GMAC
238 pr_err("%s: Adding GMAC failed!\n", __func__);
242 #error "bcm_sf2_eth: NEED to register a MAC!"
247 #ifdef CONFIG_CMD_MII
249 struct mii_dev *mdiodev = mdio_alloc();
253 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
254 mdiodev->read = eth->miiphy_read;
255 mdiodev->write = eth->miiphy_write;
257 retval = mdio_register(mdiodev);
263 debug("Ethernet initialization ...");
265 rc = bcm_sf2_eth_init(dev);
267 pr_err("%s: configuration failed!\n", __func__);
271 printf("Basic ethernet functionality initialized\n");