1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
17 #include "bcm-sf2-eth.h"
19 #if defined(CONFIG_BCM_SF2_ETH_GMAC)
20 #include "bcm-sf2-eth-gmac.h"
22 #error "bcm_sf2_eth: NEED to define a MAC!"
25 #define BCM_NET_MODULE_DESCRIPTION "Broadcom Starfighter2 Ethernet driver"
26 #define BCM_NET_MODULE_VERSION "0.1"
27 #define BCM_SF2_ETH_DEV_NAME "bcm_sf2"
29 static const char banner[] =
30 BCM_NET_MODULE_DESCRIPTION " " BCM_NET_MODULE_VERSION "\n";
32 static int bcm_sf2_eth_init(struct eth_device *dev)
34 struct eth_info *eth = (struct eth_info *)(dev->priv);
35 struct eth_dma *dma = &(eth->dma);
36 struct phy_device *phydev;
40 rc = eth->mac_init(dev);
42 pr_err("%s: Couldn't cofigure MAC!\n", __func__);
47 dma->disable_dma(dma, MAC_DMA_RX);
48 dma->disable_dma(dma, MAC_DMA_TX);
51 debug("Connecting PHY 0...\n");
52 phydev = phy_connect(miiphy_get_dev_by_name(dev->name),
53 0, dev, eth->phy_interface);
55 eth->port[0] = phydev;
58 debug("No PHY found for port 0\n");
61 for (i = 0; i < eth->port_num; i++)
62 phy_config(eth->port[i]);
68 * u-boot net functions
71 static int bcm_sf2_eth_send(struct eth_device *dev, void *packet, int length)
73 struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
74 uint8_t *buf = (uint8_t *)packet;
78 debug("%s enter\n", __func__);
80 /* load buf and start transmit */
81 rc = dma->tx_packet(dma, buf, length);
83 debug("ERROR - Tx failed\n");
87 while (!(dma->check_tx_done(dma))) {
92 pr_err("%s: Tx timeout: retried 20 times\n", __func__);
98 debug("%s exit rc(0x%x)\n", __func__, rc);
102 static int bcm_sf2_eth_receive(struct eth_device *dev)
104 struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
105 uint8_t *buf = (uint8_t *)net_rx_packets[0];
111 /* Poll Rx queue to get a packet */
112 rcvlen = dma->check_rx_done(dma, buf);
114 /* No packet received */
116 debug("\nNO More Rx\n");
118 } else if ((rcvlen == 0) || (rcvlen > RX_BUF_SIZE)) {
119 pr_err("%s: Wrong Ethernet packet size (%d B), skip!\n",
125 /* Forward received packet to uboot network handler */
126 net_process_received_packet(buf, rcvlen);
128 if (++i >= PKTBUFSRX)
130 buf = net_rx_packets[i];
137 static int bcm_sf2_eth_write_hwaddr(struct eth_device *dev)
139 struct eth_info *eth = (struct eth_info *)(dev->priv);
141 printf(" ETH MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
142 dev->enetaddr[0], dev->enetaddr[1], dev->enetaddr[2],
143 dev->enetaddr[3], dev->enetaddr[4], dev->enetaddr[5]);
145 return eth->set_mac_addr(dev->enetaddr);
148 static int bcm_sf2_eth_open(struct eth_device *dev, bd_t *bt)
150 struct eth_info *eth = (struct eth_info *)(dev->priv);
151 struct eth_dma *dma = &(eth->dma);
154 debug("Enabling BCM SF2 Ethernet.\n");
158 /* enable tx and rx DMA */
159 dma->enable_dma(dma, MAC_DMA_RX);
160 dma->enable_dma(dma, MAC_DMA_TX);
163 * Need to start PHY here because link speed can change
164 * before each ethernet operation
166 for (i = 0; i < eth->port_num; i++) {
167 if (phy_startup(eth->port[i])) {
168 pr_err("%s: PHY %d startup failed!\n", __func__, i);
169 if (i == CONFIG_BCM_SF2_ETH_DEFAULT_PORT) {
170 pr_err("%s: No default port %d!\n", __func__, i);
176 /* Set MAC speed using default port */
177 i = CONFIG_BCM_SF2_ETH_DEFAULT_PORT;
178 debug("PHY %d: speed:%d, duplex:%d, link:%d\n", i,
179 eth->port[i]->speed, eth->port[i]->duplex, eth->port[i]->link);
180 eth->set_mac_speed(eth->port[i]->speed, eth->port[i]->duplex);
182 debug("Enable Ethernet Done.\n");
187 static void bcm_sf2_eth_close(struct eth_device *dev)
189 struct eth_info *eth = (struct eth_info *)(dev->priv);
190 struct eth_dma *dma = &(eth->dma);
193 dma->disable_dma(dma, MAC_DMA_RX);
194 dma->disable_dma(dma, MAC_DMA_TX);
199 int bcm_sf2_eth_register(bd_t *bis, u8 dev_num)
201 struct eth_device *dev;
202 struct eth_info *eth;
205 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
207 pr_err("%s: Not enough memory!\n", __func__);
211 eth = (struct eth_info *)malloc(sizeof(struct eth_info));
213 pr_err("%s: Not enough memory!\n", __func__);
219 memset(dev, 0, sizeof(*dev));
220 sprintf(dev->name, "%s_%s-%hu", BCM_SF2_ETH_DEV_NAME,
221 BCM_SF2_ETH_MAC_NAME, dev_num);
223 dev->priv = (void *)eth;
226 dev->init = bcm_sf2_eth_open;
227 dev->halt = bcm_sf2_eth_close;
228 dev->send = bcm_sf2_eth_send;
229 dev->recv = bcm_sf2_eth_receive;
230 dev->write_hwaddr = bcm_sf2_eth_write_hwaddr;
232 #ifdef CONFIG_BCM_SF2_ETH_GMAC
236 pr_err("%s: Adding GMAC failed!\n", __func__);
240 #error "bcm_sf2_eth: NEED to register a MAC!"
245 #ifdef CONFIG_CMD_MII
247 struct mii_dev *mdiodev = mdio_alloc();
251 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
252 mdiodev->read = eth->miiphy_read;
253 mdiodev->write = eth->miiphy_write;
255 retval = mdio_register(mdiodev);
261 debug("Ethernet initialization ...");
263 rc = bcm_sf2_eth_init(dev);
265 pr_err("%s: configuration failed!\n", __func__);
269 printf("Basic ethernet functionality initialized\n");