bcm-sf2-eth.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Broadcom Corporation.
  4. */
  5. #include <common.h>
  6. #include <log.h>
  7. #include <malloc.h>
  8. #include <net.h>
  9. #include <config.h>
  10. #include <linux/delay.h>
  11. #include <phy.h>
  12. #include <miiphy.h>
  13. #include <asm/io.h>
  14. #include <netdev.h>
  15. #include "bcm-sf2-eth.h"
  16. #if defined(CONFIG_BCM_SF2_ETH_GMAC)
  17. #include "bcm-sf2-eth-gmac.h"
  18. #else
  19. #error "bcm_sf2_eth: NEED to define a MAC!"
  20. #endif
  21. #define BCM_NET_MODULE_DESCRIPTION "Broadcom Starfighter2 Ethernet driver"
  22. #define BCM_NET_MODULE_VERSION "0.1"
  23. #define BCM_SF2_ETH_DEV_NAME "bcm_sf2"
  24. static const char banner[] =
  25. BCM_NET_MODULE_DESCRIPTION " " BCM_NET_MODULE_VERSION "\n";
  26. static int bcm_sf2_eth_init(struct eth_device *dev)
  27. {
  28. struct eth_info *eth = (struct eth_info *)(dev->priv);
  29. struct eth_dma *dma = &(eth->dma);
  30. struct phy_device *phydev;
  31. int rc = 0;
  32. int i;
  33. rc = eth->mac_init(dev);
  34. if (rc) {
  35. pr_err("%s: Couldn't cofigure MAC!\n", __func__);
  36. return rc;
  37. }
  38. /* disable DMA */
  39. dma->disable_dma(dma, MAC_DMA_RX);
  40. dma->disable_dma(dma, MAC_DMA_TX);
  41. eth->port_num = 0;
  42. debug("Connecting PHY 0...\n");
  43. phydev = phy_connect(miiphy_get_dev_by_name(dev->name),
  44. -1, dev, eth->phy_interface);
  45. if (phydev != NULL) {
  46. eth->port[0] = phydev;
  47. eth->port_num += 1;
  48. } else {
  49. debug("No PHY found for port 0\n");
  50. }
  51. for (i = 0; i < eth->port_num; i++)
  52. phy_config(eth->port[i]);
  53. return rc;
  54. }
  55. /*
  56. * u-boot net functions
  57. */
  58. static int bcm_sf2_eth_send(struct eth_device *dev, void *packet, int length)
  59. {
  60. struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
  61. uint8_t *buf = (uint8_t *)packet;
  62. int rc = 0;
  63. int i = 0;
  64. debug("%s enter\n", __func__);
  65. /* load buf and start transmit */
  66. rc = dma->tx_packet(dma, buf, length);
  67. if (rc) {
  68. debug("ERROR - Tx failed\n");
  69. return rc;
  70. }
  71. while (!(dma->check_tx_done(dma))) {
  72. udelay(100);
  73. debug(".");
  74. i++;
  75. if (i > 20) {
  76. pr_err("%s: Tx timeout: retried 20 times\n", __func__);
  77. rc = -1;
  78. break;
  79. }
  80. }
  81. debug("%s exit rc(0x%x)\n", __func__, rc);
  82. return rc;
  83. }
  84. static int bcm_sf2_eth_receive(struct eth_device *dev)
  85. {
  86. struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma);
  87. uint8_t *buf = (uint8_t *)net_rx_packets[0];
  88. int rcvlen;
  89. int rc = 0;
  90. int i = 0;
  91. while (1) {
  92. /* Poll Rx queue to get a packet */
  93. rcvlen = dma->check_rx_done(dma, buf);
  94. if (rcvlen < 0) {
  95. /* No packet received */
  96. rc = -1;
  97. debug("\nNO More Rx\n");
  98. break;
  99. } else if ((rcvlen == 0) || (rcvlen > RX_BUF_SIZE)) {
  100. pr_err("%s: Wrong Ethernet packet size (%d B), skip!\n",
  101. __func__, rcvlen);
  102. break;
  103. } else {
  104. debug("recieved\n");
  105. /* Forward received packet to uboot network handler */
  106. net_process_received_packet(buf, rcvlen);
  107. if (++i >= PKTBUFSRX)
  108. i = 0;
  109. buf = net_rx_packets[i];
  110. }
  111. }
  112. return rc;
  113. }
  114. static int bcm_sf2_eth_write_hwaddr(struct eth_device *dev)
  115. {
  116. struct eth_info *eth = (struct eth_info *)(dev->priv);
  117. printf(" ETH MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  118. dev->enetaddr[0], dev->enetaddr[1], dev->enetaddr[2],
  119. dev->enetaddr[3], dev->enetaddr[4], dev->enetaddr[5]);
  120. return eth->set_mac_addr(dev->enetaddr);
  121. }
  122. static int bcm_sf2_eth_open(struct eth_device *dev, struct bd_info *bt)
  123. {
  124. struct eth_info *eth = (struct eth_info *)(dev->priv);
  125. struct eth_dma *dma = &(eth->dma);
  126. int i;
  127. debug("Enabling BCM SF2 Ethernet.\n");
  128. eth->enable_mac();
  129. /* enable tx and rx DMA */
  130. dma->enable_dma(dma, MAC_DMA_RX);
  131. dma->enable_dma(dma, MAC_DMA_TX);
  132. /*
  133. * Need to start PHY here because link speed can change
  134. * before each ethernet operation
  135. */
  136. for (i = 0; i < eth->port_num; i++) {
  137. if (phy_startup(eth->port[i])) {
  138. pr_err("%s: PHY %d startup failed!\n", __func__, i);
  139. if (i == CONFIG_BCM_SF2_ETH_DEFAULT_PORT) {
  140. pr_err("%s: No default port %d!\n", __func__, i);
  141. return -1;
  142. }
  143. }
  144. }
  145. /* Set MAC speed using default port */
  146. i = CONFIG_BCM_SF2_ETH_DEFAULT_PORT;
  147. debug("PHY %d: speed:%d, duplex:%d, link:%d\n", i,
  148. eth->port[i]->speed, eth->port[i]->duplex, eth->port[i]->link);
  149. eth->set_mac_speed(eth->port[i]->speed, eth->port[i]->duplex);
  150. debug("Enable Ethernet Done.\n");
  151. return 0;
  152. }
  153. static void bcm_sf2_eth_close(struct eth_device *dev)
  154. {
  155. struct eth_info *eth = (struct eth_info *)(dev->priv);
  156. struct eth_dma *dma = &(eth->dma);
  157. /* disable DMA */
  158. dma->disable_dma(dma, MAC_DMA_RX);
  159. dma->disable_dma(dma, MAC_DMA_TX);
  160. eth->disable_mac();
  161. }
  162. int bcm_sf2_eth_register(struct bd_info *bis, u8 dev_num)
  163. {
  164. struct eth_device *dev;
  165. struct eth_info *eth;
  166. int rc;
  167. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  168. if (dev == NULL) {
  169. pr_err("%s: Not enough memory!\n", __func__);
  170. return -1;
  171. }
  172. eth = (struct eth_info *)malloc(sizeof(struct eth_info));
  173. if (eth == NULL) {
  174. pr_err("%s: Not enough memory!\n", __func__);
  175. return -1;
  176. }
  177. printf(banner);
  178. memset(dev, 0, sizeof(*dev));
  179. sprintf(dev->name, "%s_%s-%hu", BCM_SF2_ETH_DEV_NAME,
  180. BCM_SF2_ETH_MAC_NAME, dev_num);
  181. dev->priv = (void *)eth;
  182. dev->iobase = 0;
  183. dev->init = bcm_sf2_eth_open;
  184. dev->halt = bcm_sf2_eth_close;
  185. dev->send = bcm_sf2_eth_send;
  186. dev->recv = bcm_sf2_eth_receive;
  187. dev->write_hwaddr = bcm_sf2_eth_write_hwaddr;
  188. #ifdef CONFIG_BCM_SF2_ETH_GMAC
  189. if (gmac_add(dev)) {
  190. free(eth);
  191. free(dev);
  192. pr_err("%s: Adding GMAC failed!\n", __func__);
  193. return -1;
  194. }
  195. #else
  196. #error "bcm_sf2_eth: NEED to register a MAC!"
  197. #endif
  198. eth_register(dev);
  199. #ifdef CONFIG_CMD_MII
  200. int retval;
  201. struct mii_dev *mdiodev = mdio_alloc();
  202. if (!mdiodev)
  203. return -ENOMEM;
  204. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  205. mdiodev->read = eth->miiphy_read;
  206. mdiodev->write = eth->miiphy_write;
  207. retval = mdio_register(mdiodev);
  208. if (retval < 0)
  209. return retval;
  210. #endif
  211. /* Initialization */
  212. debug("Ethernet initialization ...");
  213. rc = bcm_sf2_eth_init(dev);
  214. if (rc != 0) {
  215. pr_err("%s: configuration failed!\n", __func__);
  216. return -1;
  217. }
  218. printf("Basic ethernet functionality initialized\n");
  219. return 0;
  220. }