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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /* Configure receive filters */
  173. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_RFE_CTL,
  174. ( LAN78XX_RFE_CTL_AB |
  175. LAN78XX_RFE_CTL_AM |
  176. LAN78XX_RFE_CTL_AU ) ) ) != 0 )
  177. goto err_rfe_ctl;
  178. /* Configure receive FIFO */
  179. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_RX_CTL,
  180. ( LAN78XX_FCT_RX_CTL_EN |
  181. LAN78XX_FCT_RX_CTL_BAD ) ) ) != 0 )
  182. goto err_fct_rx_ctl;
  183. /* Configure transmit FIFO */
  184. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_TX_CTL,
  185. LAN78XX_FCT_TX_CTL_EN ) ) != 0 )
  186. goto err_fct_tx_ctl;
  187. /* Configure receive datapath */
  188. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_RX,
  189. ( LAN78XX_MAC_RX_MAX_SIZE_DEFAULT |
  190. LAN78XX_MAC_RX_FCS |
  191. LAN78XX_MAC_RX_EN ) ) ) != 0 )
  192. goto err_mac_rx;
  193. /* Configure transmit datapath */
  194. if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_TX,
  195. LAN78XX_MAC_TX_EN ) ) != 0 )
  196. goto err_mac_tx;
  197. /* Set MAC address */
  198. if ( ( rc = smscusb_set_address ( smscusb,
  199. LAN78XX_RX_ADDR_BASE ) ) != 0 )
  200. goto err_set_address;
  201. /* Set MAC address perfect filter */
  202. if ( ( rc = smscusb_set_filter ( smscusb,
  203. LAN78XX_ADDR_FILT_BASE ) ) != 0 )
  204. goto err_set_filter;
  205. /* Enable PHY interrupts and update link status */
  206. if ( ( rc = smscusb_mii_open ( smscusb, LAN78XX_MII_PHY_INTR_MASK,
  207. ( LAN78XX_PHY_INTR_ENABLE |
  208. LAN78XX_PHY_INTR_LINK |
  209. LAN78XX_PHY_INTR_ANEG_ERR |
  210. LAN78XX_PHY_INTR_ANEG_DONE ) ) ) != 0 )
  211. goto err_mii_open;
  212. return 0;
  213. err_mii_open:
  214. err_set_filter:
  215. err_set_address:
  216. err_mac_tx:
  217. err_mac_rx:
  218. err_fct_tx_ctl:
  219. err_fct_rx_ctl:
  220. err_rfe_ctl:
  221. err_bulk_in_dly:
  222. err_int_ep_ctl:
  223. usbnet_close ( &smscusb->usbnet );
  224. err_open:
  225. err_usb_cfg0_write:
  226. err_usb_cfg0_read:
  227. lan78xx_reset ( smscusb );
  228. return rc;
  229. }
  230. /**
  231. * Close network device
  232. *
  233. * @v netdev Network device
  234. */
  235. static void lan78xx_close ( struct net_device *netdev ) {
  236. struct smscusb_device *smscusb = netdev->priv;
  237. /* Close USB network device */
  238. usbnet_close ( &smscusb->usbnet );
  239. /* Dump statistics (for debugging) */
  240. if ( DBG_LOG )
  241. smsc75xx_dump_statistics ( smscusb );
  242. /* Reset device */
  243. lan78xx_reset ( smscusb );
  244. }
  245. /** LAN78xx network device operations */
  246. static struct net_device_operations lan78xx_operations = {
  247. .open = lan78xx_open,
  248. .close = lan78xx_close,
  249. .transmit = smsc75xx_transmit,
  250. .poll = smsc75xx_poll,
  251. };
  252. /******************************************************************************
  253. *
  254. * USB interface
  255. *
  256. ******************************************************************************
  257. */
  258. /**
  259. * Probe device
  260. *
  261. * @v func USB function
  262. * @v config Configuration descriptor
  263. * @ret rc Return status code
  264. */
  265. static int lan78xx_probe ( struct usb_function *func,
  266. struct usb_configuration_descriptor *config ) {
  267. struct net_device *netdev;
  268. struct smscusb_device *smscusb;
  269. int rc;
  270. /* Allocate and initialise structure */
  271. netdev = alloc_etherdev ( sizeof ( *smscusb ) );
  272. if ( ! netdev ) {
  273. rc = -ENOMEM;
  274. goto err_alloc;
  275. }
  276. netdev_init ( netdev, &lan78xx_operations );
  277. netdev->dev = &func->dev;
  278. smscusb = netdev->priv;
  279. memset ( smscusb, 0, sizeof ( *smscusb ) );
  280. smscusb_init ( smscusb, netdev, func, &smsc75xx_in_operations );
  281. smscusb_mii_init ( smscusb, LAN78XX_MII_BASE,
  282. LAN78XX_MII_PHY_INTR_SOURCE );
  283. usb_refill_init ( &smscusb->usbnet.in, 0, SMSC75XX_IN_MTU,
  284. SMSC75XX_IN_MAX_FILL );
  285. DBGC ( smscusb, "LAN78XX %p on %s\n", smscusb, func->name );
  286. /* Describe USB network device */
  287. if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
  288. DBGC ( smscusb, "LAN78XX %p could not describe: %s\n",
  289. smscusb, strerror ( rc ) );
  290. goto err_describe;
  291. }
  292. /* Reset device */
  293. if ( ( rc = lan78xx_reset ( smscusb ) ) != 0 )
  294. goto err_reset;
  295. /* Read MAC address */
  296. if ( ( rc = lan78xx_fetch_mac ( smscusb ) ) != 0 )
  297. goto err_fetch_mac;
  298. /* Register network device */
  299. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  300. goto err_register;
  301. usb_func_set_drvdata ( func, netdev );
  302. return 0;
  303. unregister_netdev ( netdev );
  304. err_register:
  305. err_fetch_mac:
  306. err_reset:
  307. err_describe:
  308. netdev_nullify ( netdev );
  309. netdev_put ( netdev );
  310. err_alloc:
  311. return rc;
  312. }
  313. /**
  314. * Remove device
  315. *
  316. * @v func USB function
  317. */
  318. static void lan78xx_remove ( struct usb_function *func ) {
  319. struct net_device *netdev = usb_func_get_drvdata ( func );
  320. unregister_netdev ( netdev );
  321. netdev_nullify ( netdev );
  322. netdev_put ( netdev );
  323. }
  324. /** LAN78xx device IDs */
  325. static struct usb_device_id lan78xx_ids[] = {
  326. {
  327. .name = "lan7800",
  328. .vendor = 0x0424,
  329. .product = 0x7800,
  330. },
  331. {
  332. .name = "lan7850",
  333. .vendor = 0x0424,
  334. .product = 0x7850,
  335. },
  336. };
  337. /** LAN78xx driver */
  338. struct usb_driver lan78xx_driver __usb_driver = {
  339. .ids = lan78xx_ids,
  340. .id_count = ( sizeof ( lan78xx_ids ) / sizeof ( lan78xx_ids[0] ) ),
  341. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  342. .score = USB_SCORE_NORMAL,
  343. .probe = lan78xx_probe,
  344. .remove = lan78xx_remove,
  345. };