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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* Otherwise, generate a random MAC address */
  88. eth_random_addr ( netdev->hw_addr );
  89. DBGC ( smscusb, "LAN78XX %p using random MAC %s\n",
  90. smscusb, eth_ntoa ( netdev->hw_addr ) );
  91. return 0;
  92. }
  93. /******************************************************************************
  94. *
  95. * Device reset
  96. *
  97. ******************************************************************************
  98. */
  99. /**
  100. * Reset device
  101. *
  102. * @v smscusb SMSC USB device
  103. * @ret rc Return status code
  104. */
  105. static int lan78xx_reset ( struct smscusb_device *smscusb ) {
  106. uint32_t hw_cfg;
  107. unsigned int i;
  108. int rc;
  109. /* Reset device */
  110. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_HW_CFG,
  111. LAN78XX_HW_CFG_LRST ) ) != 0 )
  112. return rc;
  113. /* Wait for reset to complete */
  114. for ( i = 0 ; i < LAN78XX_RESET_MAX_WAIT_MS ; i++ ) {
  115. /* Check if reset has completed */
  116. if ( ( rc = smscusb_readl ( smscusb, LAN78XX_HW_CFG,
  117. &hw_cfg ) ) != 0 )
  118. return rc;
  119. if ( ! ( hw_cfg & LAN78XX_HW_CFG_LRST ) )
  120. return 0;
  121. /* Delay */
  122. mdelay ( 1 );
  123. }
  124. DBGC ( smscusb, "LAN78XX %p timed out waiting for reset\n",
  125. smscusb );
  126. return -ETIMEDOUT;
  127. }
  128. /******************************************************************************
  129. *
  130. * Network device interface
  131. *
  132. ******************************************************************************
  133. */
  134. /**
  135. * Open network device
  136. *
  137. * @v netdev Network device
  138. * @ret rc Return status code
  139. */
  140. static int lan78xx_open ( struct net_device *netdev ) {
  141. struct smscusb_device *smscusb = netdev->priv;
  142. uint32_t usb_cfg0;
  143. int rc;
  144. /* Clear stored interrupt status */
  145. smscusb->int_sts = 0;
  146. /* Configure bulk IN empty response */
  147. if ( ( rc = smscusb_readl ( smscusb, LAN78XX_USB_CFG0,
  148. &usb_cfg0 ) ) != 0 )
  149. goto err_usb_cfg0_read;
  150. usb_cfg0 |= LAN78XX_USB_CFG0_BIR;
  151. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_USB_CFG0,
  152. usb_cfg0 ) ) != 0 )
  153. goto err_usb_cfg0_write;
  154. /* Open USB network device */
  155. if ( ( rc = usbnet_open ( &smscusb->usbnet ) ) != 0 ) {
  156. DBGC ( smscusb, "LAN78XX %p could not open: %s\n",
  157. smscusb, strerror ( rc ) );
  158. goto err_open;
  159. }
  160. /* Configure interrupt endpoint */
  161. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_INT_EP_CTL,
  162. ( LAN78XX_INT_EP_CTL_RDFO_EN |
  163. LAN78XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  164. goto err_int_ep_ctl;
  165. /* Configure bulk IN delay */
  166. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_BULK_IN_DLY,
  167. LAN78XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  168. goto err_bulk_in_dly;
  169. /* Configure receive filters */
  170. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_RFE_CTL,
  171. ( LAN78XX_RFE_CTL_AB |
  172. LAN78XX_RFE_CTL_AM |
  173. LAN78XX_RFE_CTL_AU ) ) ) != 0 )
  174. goto err_rfe_ctl;
  175. /* Configure receive FIFO */
  176. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_RX_CTL,
  177. ( LAN78XX_FCT_RX_CTL_EN |
  178. LAN78XX_FCT_RX_CTL_BAD ) ) ) != 0 )
  179. goto err_fct_rx_ctl;
  180. /* Configure transmit FIFO */
  181. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_TX_CTL,
  182. LAN78XX_FCT_TX_CTL_EN ) ) != 0 )
  183. goto err_fct_tx_ctl;
  184. /* Configure receive datapath */
  185. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_RX,
  186. ( LAN78XX_MAC_RX_MAX_SIZE_DEFAULT |
  187. LAN78XX_MAC_RX_FCS |
  188. LAN78XX_MAC_RX_EN ) ) ) != 0 )
  189. goto err_mac_rx;
  190. /* Configure transmit datapath */
  191. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_TX,
  192. LAN78XX_MAC_TX_EN ) ) != 0 )
  193. goto err_mac_tx;
  194. /* Set MAC address */
  195. if ( ( rc = smscusb_set_address ( smscusb,
  196. LAN78XX_RX_ADDR_BASE ) ) != 0 )
  197. goto err_set_address;
  198. /* Set MAC address perfect filter */
  199. if ( ( rc = smscusb_set_filter ( smscusb,
  200. LAN78XX_ADDR_FILT_BASE ) ) != 0 )
  201. goto err_set_filter;
  202. /* Enable PHY interrupts and update link status */
  203. if ( ( rc = smscusb_mii_open ( smscusb, LAN78XX_MII_PHY_INTR_MASK,
  204. ( LAN78XX_PHY_INTR_ENABLE |
  205. LAN78XX_PHY_INTR_LINK |
  206. LAN78XX_PHY_INTR_ANEG_ERR |
  207. LAN78XX_PHY_INTR_ANEG_DONE ) ) ) != 0 )
  208. goto err_mii_open;
  209. return 0;
  210. err_mii_open:
  211. err_set_filter:
  212. err_set_address:
  213. err_mac_tx:
  214. err_mac_rx:
  215. err_fct_tx_ctl:
  216. err_fct_rx_ctl:
  217. err_rfe_ctl:
  218. err_bulk_in_dly:
  219. err_int_ep_ctl:
  220. usbnet_close ( &smscusb->usbnet );
  221. err_open:
  222. err_usb_cfg0_write:
  223. err_usb_cfg0_read:
  224. lan78xx_reset ( smscusb );
  225. return rc;
  226. }
  227. /**
  228. * Close network device
  229. *
  230. * @v netdev Network device
  231. */
  232. static void lan78xx_close ( struct net_device *netdev ) {
  233. struct smscusb_device *smscusb = netdev->priv;
  234. /* Close USB network device */
  235. usbnet_close ( &smscusb->usbnet );
  236. /* Dump statistics (for debugging) */
  237. if ( DBG_LOG )
  238. smsc75xx_dump_statistics ( smscusb );
  239. /* Reset device */
  240. lan78xx_reset ( smscusb );
  241. }
  242. /** LAN78xx network device operations */
  243. static struct net_device_operations lan78xx_operations = {
  244. .open = lan78xx_open,
  245. .close = lan78xx_close,
  246. .transmit = smsc75xx_transmit,
  247. .poll = smsc75xx_poll,
  248. };
  249. /******************************************************************************
  250. *
  251. * USB interface
  252. *
  253. ******************************************************************************
  254. */
  255. /**
  256. * Probe device
  257. *
  258. * @v func USB function
  259. * @v config Configuration descriptor
  260. * @ret rc Return status code
  261. */
  262. static int lan78xx_probe ( struct usb_function *func,
  263. struct usb_configuration_descriptor *config ) {
  264. struct net_device *netdev;
  265. struct smscusb_device *smscusb;
  266. int rc;
  267. /* Allocate and initialise structure */
  268. netdev = alloc_etherdev ( sizeof ( *smscusb ) );
  269. if ( ! netdev ) {
  270. rc = -ENOMEM;
  271. goto err_alloc;
  272. }
  273. netdev_init ( netdev, &lan78xx_operations );
  274. netdev->dev = &func->dev;
  275. smscusb = netdev->priv;
  276. memset ( smscusb, 0, sizeof ( *smscusb ) );
  277. smscusb_init ( smscusb, netdev, func, &smsc75xx_in_operations );
  278. smscusb_mii_init ( smscusb, LAN78XX_MII_BASE,
  279. LAN78XX_MII_PHY_INTR_SOURCE );
  280. usb_refill_init ( &smscusb->usbnet.in, 0, SMSC75XX_IN_MTU,
  281. SMSC75XX_IN_MAX_FILL );
  282. DBGC ( smscusb, "LAN78XX %p on %s\n", smscusb, func->name );
  283. /* Describe USB network device */
  284. if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
  285. DBGC ( smscusb, "LAN78XX %p could not describe: %s\n",
  286. smscusb, strerror ( rc ) );
  287. goto err_describe;
  288. }
  289. /* Reset device */
  290. if ( ( rc = lan78xx_reset ( smscusb ) ) != 0 )
  291. goto err_reset;
  292. /* Read MAC address */
  293. if ( ( rc = lan78xx_fetch_mac ( smscusb ) ) != 0 )
  294. goto err_fetch_mac;
  295. /* Register network device */
  296. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  297. goto err_register;
  298. usb_func_set_drvdata ( func, netdev );
  299. return 0;
  300. unregister_netdev ( netdev );
  301. err_register:
  302. err_fetch_mac:
  303. err_reset:
  304. err_describe:
  305. netdev_nullify ( netdev );
  306. netdev_put ( netdev );
  307. err_alloc:
  308. return rc;
  309. }
  310. /**
  311. * Remove device
  312. *
  313. * @v func USB function
  314. */
  315. static void lan78xx_remove ( struct usb_function *func ) {
  316. struct net_device *netdev = usb_func_get_drvdata ( func );
  317. unregister_netdev ( netdev );
  318. netdev_nullify ( netdev );
  319. netdev_put ( netdev );
  320. }
  321. /** LAN78xx device IDs */
  322. static struct usb_device_id lan78xx_ids[] = {
  323. {
  324. .name = "lan7800",
  325. .vendor = 0x0424,
  326. .product = 0x7800,
  327. },
  328. {
  329. .name = "lan7850",
  330. .vendor = 0x0424,
  331. .product = 0x7850,
  332. },
  333. };
  334. /** LAN78xx driver */
  335. struct usb_driver lan78xx_driver __usb_driver = {
  336. .ids = lan78xx_ids,
  337. .id_count = ( sizeof ( lan78xx_ids ) / sizeof ( lan78xx_ids[0] ) ),
  338. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  339. .score = USB_SCORE_NORMAL,
  340. .probe = lan78xx_probe,
  341. .remove = lan78xx_remove,
  342. };