You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lan78xx.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <ipxe/ethernet.h>
  28. #include <ipxe/usb.h>
  29. #include <ipxe/usbnet.h>
  30. #include "lan78xx.h"
  31. /** @file
  32. *
  33. * Microchip LAN78xx USB Ethernet driver
  34. *
  35. */
  36. /******************************************************************************
  37. *
  38. * MAC address
  39. *
  40. ******************************************************************************
  41. */
  42. /**
  43. * Fetch MAC address from EEPROM
  44. *
  45. * @v smscusb SMSC USB device
  46. * @ret rc Return status code
  47. */
  48. static int lan78xx_eeprom_fetch_mac ( struct smscusb_device *smscusb ) {
  49. uint32_t hw_cfg;
  50. uint32_t orig_hw_cfg;
  51. int rc;
  52. /* Read original HW_CFG value */
  53. if ( ( rc = smscusb_readl ( smscusb, LAN78XX_HW_CFG, &hw_cfg ) ) != 0 )
  54. goto err_read_hw_cfg;
  55. orig_hw_cfg = hw_cfg;
  56. /* Temporarily disable LED0 and LED1 (which share physical
  57. * pins with EEDO and EECLK respectively).
  58. */
  59. hw_cfg &= ~( LAN78XX_HW_CFG_LED0_EN | LAN78XX_HW_CFG_LED1_EN );
  60. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_HW_CFG, hw_cfg ) ) != 0 )
  61. goto err_write_hw_cfg;
  62. /* Fetch MAC address from EEPROM */
  63. if ( ( rc = smscusb_eeprom_fetch_mac ( smscusb,
  64. LAN78XX_E2P_BASE ) ) != 0 )
  65. goto err_fetch_mac;
  66. err_fetch_mac:
  67. smscusb_writel ( smscusb, LAN78XX_HW_CFG, orig_hw_cfg );
  68. err_write_hw_cfg:
  69. err_read_hw_cfg:
  70. return rc;
  71. }
  72. /**
  73. * Fetch MAC address
  74. *
  75. * @v smscusb SMSC USB device
  76. * @ret rc Return status code
  77. */
  78. static int lan78xx_fetch_mac ( struct smscusb_device *smscusb ) {
  79. struct net_device *netdev = smscusb->netdev;
  80. int rc;
  81. /* Read MAC address from EEPROM, if present */
  82. if ( ( rc = lan78xx_eeprom_fetch_mac ( smscusb ) ) == 0 )
  83. return 0;
  84. /* Read MAC address from OTP, if present */
  85. if ( ( rc = smscusb_otp_fetch_mac ( smscusb, LAN78XX_OTP_BASE ) ) == 0 )
  86. return 0;
  87. /* Read MAC address from device tree, if present */
  88. if ( ( rc = smscusb_fdt_fetch_mac ( smscusb ) ) == 0 )
  89. return 0;
  90. /* Otherwise, generate a random MAC address */
  91. eth_random_addr ( netdev->hw_addr );
  92. DBGC ( smscusb, "LAN78XX %p using random MAC %s\n",
  93. smscusb, eth_ntoa ( netdev->hw_addr ) );
  94. return 0;
  95. }
  96. /******************************************************************************
  97. *
  98. * Device reset
  99. *
  100. ******************************************************************************
  101. */
  102. /**
  103. * Reset device
  104. *
  105. * @v smscusb SMSC USB device
  106. * @ret rc Return status code
  107. */
  108. static int lan78xx_reset ( struct smscusb_device *smscusb ) {
  109. uint32_t hw_cfg;
  110. unsigned int i;
  111. int rc;
  112. /* Reset device */
  113. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_HW_CFG,
  114. LAN78XX_HW_CFG_LRST ) ) != 0 )
  115. return rc;
  116. /* Wait for reset to complete */
  117. for ( i = 0 ; i < LAN78XX_RESET_MAX_WAIT_MS ; i++ ) {
  118. /* Check if reset has completed */
  119. if ( ( rc = smscusb_readl ( smscusb, LAN78XX_HW_CFG,
  120. &hw_cfg ) ) != 0 )
  121. return rc;
  122. if ( ! ( hw_cfg & LAN78XX_HW_CFG_LRST ) )
  123. return 0;
  124. /* Delay */
  125. mdelay ( 1 );
  126. }
  127. DBGC ( smscusb, "LAN78XX %p timed out waiting for reset\n",
  128. smscusb );
  129. return -ETIMEDOUT;
  130. }
  131. /******************************************************************************
  132. *
  133. * Network device interface
  134. *
  135. ******************************************************************************
  136. */
  137. /**
  138. * Open network device
  139. *
  140. * @v netdev Network device
  141. * @ret rc Return status code
  142. */
  143. static int lan78xx_open ( struct net_device *netdev ) {
  144. struct smscusb_device *smscusb = netdev->priv;
  145. uint32_t usb_cfg0;
  146. int rc;
  147. /* Clear stored interrupt status */
  148. smscusb->int_sts = 0;
  149. /* Configure bulk IN empty response */
  150. if ( ( rc = smscusb_readl ( smscusb, LAN78XX_USB_CFG0,
  151. &usb_cfg0 ) ) != 0 )
  152. goto err_usb_cfg0_read;
  153. usb_cfg0 |= LAN78XX_USB_CFG0_BIR;
  154. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_USB_CFG0,
  155. usb_cfg0 ) ) != 0 )
  156. goto err_usb_cfg0_write;
  157. /* Open USB network device */
  158. if ( ( rc = usbnet_open ( &smscusb->usbnet ) ) != 0 ) {
  159. DBGC ( smscusb, "LAN78XX %p could not open: %s\n",
  160. smscusb, strerror ( rc ) );
  161. goto err_open;
  162. }
  163. /* Configure interrupt endpoint */
  164. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_INT_EP_CTL,
  165. ( LAN78XX_INT_EP_CTL_RDFO_EN |
  166. LAN78XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  167. goto err_int_ep_ctl;
  168. /* Configure bulk IN delay */
  169. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_BULK_IN_DLY,
  170. LAN78XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  171. goto err_bulk_in_dly;
  172. /* Enable automatic speed and duplex detection */
  173. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_CR,
  174. ( LAN78XX_MAC_CR_ADP |
  175. LAN78XX_MAC_CR_ADD |
  176. LAN78XX_MAC_CR_ASD ) ) ) != 0 )
  177. goto err_mac_cr;
  178. /* Configure receive filters */
  179. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_RFE_CTL,
  180. ( LAN78XX_RFE_CTL_AB |
  181. LAN78XX_RFE_CTL_AM |
  182. LAN78XX_RFE_CTL_AU ) ) ) != 0 )
  183. goto err_rfe_ctl;
  184. /* Configure receive FIFO */
  185. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_RX_CTL,
  186. ( LAN78XX_FCT_RX_CTL_EN |
  187. LAN78XX_FCT_RX_CTL_BAD ) ) ) != 0 )
  188. goto err_fct_rx_ctl;
  189. /* Configure transmit FIFO */
  190. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_TX_CTL,
  191. LAN78XX_FCT_TX_CTL_EN ) ) != 0 )
  192. goto err_fct_tx_ctl;
  193. /* Configure receive datapath */
  194. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_RX,
  195. ( LAN78XX_MAC_RX_MAX_SIZE_DEFAULT |
  196. LAN78XX_MAC_RX_FCS |
  197. LAN78XX_MAC_RX_EN ) ) ) != 0 )
  198. goto err_mac_rx;
  199. /* Configure transmit datapath */
  200. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_TX,
  201. LAN78XX_MAC_TX_EN ) ) != 0 )
  202. goto err_mac_tx;
  203. /* Set MAC address */
  204. if ( ( rc = smscusb_set_address ( smscusb,
  205. LAN78XX_RX_ADDR_BASE ) ) != 0 )
  206. goto err_set_address;
  207. /* Set MAC address perfect filter */
  208. if ( ( rc = smscusb_set_filter ( smscusb,
  209. LAN78XX_ADDR_FILT_BASE ) ) != 0 )
  210. goto err_set_filter;
  211. /* Enable PHY interrupts and update link status */
  212. if ( ( rc = smscusb_mii_open ( smscusb, LAN78XX_MII_PHY_INTR_MASK,
  213. ( LAN78XX_PHY_INTR_ENABLE |
  214. LAN78XX_PHY_INTR_LINK |
  215. LAN78XX_PHY_INTR_ANEG_ERR |
  216. LAN78XX_PHY_INTR_ANEG_DONE ) ) ) != 0 )
  217. goto err_mii_open;
  218. return 0;
  219. err_mii_open:
  220. err_set_filter:
  221. err_set_address:
  222. err_mac_tx:
  223. err_mac_rx:
  224. err_fct_tx_ctl:
  225. err_fct_rx_ctl:
  226. err_rfe_ctl:
  227. err_mac_cr:
  228. err_bulk_in_dly:
  229. err_int_ep_ctl:
  230. usbnet_close ( &smscusb->usbnet );
  231. err_open:
  232. err_usb_cfg0_write:
  233. err_usb_cfg0_read:
  234. lan78xx_reset ( smscusb );
  235. return rc;
  236. }
  237. /**
  238. * Close network device
  239. *
  240. * @v netdev Network device
  241. */
  242. static void lan78xx_close ( struct net_device *netdev ) {
  243. struct smscusb_device *smscusb = netdev->priv;
  244. /* Close USB network device */
  245. usbnet_close ( &smscusb->usbnet );
  246. /* Dump statistics (for debugging) */
  247. if ( DBG_LOG )
  248. smsc75xx_dump_statistics ( smscusb );
  249. /* Reset device */
  250. lan78xx_reset ( smscusb );
  251. }
  252. /** LAN78xx network device operations */
  253. static struct net_device_operations lan78xx_operations = {
  254. .open = lan78xx_open,
  255. .close = lan78xx_close,
  256. .transmit = smsc75xx_transmit,
  257. .poll = smsc75xx_poll,
  258. };
  259. /******************************************************************************
  260. *
  261. * USB interface
  262. *
  263. ******************************************************************************
  264. */
  265. /**
  266. * Probe device
  267. *
  268. * @v func USB function
  269. * @v config Configuration descriptor
  270. * @ret rc Return status code
  271. */
  272. static int lan78xx_probe ( struct usb_function *func,
  273. struct usb_configuration_descriptor *config ) {
  274. struct net_device *netdev;
  275. struct smscusb_device *smscusb;
  276. int rc;
  277. /* Allocate and initialise structure */
  278. netdev = alloc_etherdev ( sizeof ( *smscusb ) );
  279. if ( ! netdev ) {
  280. rc = -ENOMEM;
  281. goto err_alloc;
  282. }
  283. netdev_init ( netdev, &lan78xx_operations );
  284. netdev->dev = &func->dev;
  285. smscusb = netdev->priv;
  286. memset ( smscusb, 0, sizeof ( *smscusb ) );
  287. smscusb_init ( smscusb, netdev, func, &smsc75xx_in_operations );
  288. smscusb_mii_init ( smscusb, LAN78XX_MII_BASE,
  289. LAN78XX_MII_PHY_INTR_SOURCE );
  290. usb_refill_init ( &smscusb->usbnet.in, 0, SMSC75XX_IN_MTU,
  291. SMSC75XX_IN_MAX_FILL );
  292. DBGC ( smscusb, "LAN78XX %p on %s\n", smscusb, func->name );
  293. /* Describe USB network device */
  294. if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
  295. DBGC ( smscusb, "LAN78XX %p could not describe: %s\n",
  296. smscusb, strerror ( rc ) );
  297. goto err_describe;
  298. }
  299. /* Reset device */
  300. if ( ( rc = lan78xx_reset ( smscusb ) ) != 0 )
  301. goto err_reset;
  302. /* Read MAC address */
  303. if ( ( rc = lan78xx_fetch_mac ( smscusb ) ) != 0 )
  304. goto err_fetch_mac;
  305. /* Register network device */
  306. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  307. goto err_register;
  308. usb_func_set_drvdata ( func, netdev );
  309. return 0;
  310. unregister_netdev ( netdev );
  311. err_register:
  312. err_fetch_mac:
  313. err_reset:
  314. err_describe:
  315. netdev_nullify ( netdev );
  316. netdev_put ( netdev );
  317. err_alloc:
  318. return rc;
  319. }
  320. /**
  321. * Remove device
  322. *
  323. * @v func USB function
  324. */
  325. static void lan78xx_remove ( struct usb_function *func ) {
  326. struct net_device *netdev = usb_func_get_drvdata ( func );
  327. unregister_netdev ( netdev );
  328. netdev_nullify ( netdev );
  329. netdev_put ( netdev );
  330. }
  331. /** LAN78xx device IDs */
  332. static struct usb_device_id lan78xx_ids[] = {
  333. {
  334. .name = "lan7800",
  335. .vendor = 0x0424,
  336. .product = 0x7800,
  337. },
  338. {
  339. .name = "lan7850",
  340. .vendor = 0x0424,
  341. .product = 0x7850,
  342. },
  343. };
  344. /** LAN78xx driver */
  345. struct usb_driver lan78xx_driver __usb_driver = {
  346. .ids = lan78xx_ids,
  347. .id_count = ( sizeof ( lan78xx_ids ) / sizeof ( lan78xx_ids[0] ) ),
  348. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  349. .score = USB_SCORE_NORMAL,
  350. .probe = lan78xx_probe,
  351. .remove = lan78xx_remove,
  352. };