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.

smsc95xx.c 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * Copyright (C) 2015 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 <byteswap.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/usb.h>
  30. #include <ipxe/usbnet.h>
  31. #include <ipxe/profile.h>
  32. #include "smsc95xx.h"
  33. /** @file
  34. *
  35. * SMSC LAN95xx USB Ethernet driver
  36. *
  37. */
  38. /** Interrupt completion profiler */
  39. static struct profiler smsc95xx_intr_profiler __profiler =
  40. { .name = "smsc95xx.intr" };
  41. /** Bulk IN completion profiler */
  42. static struct profiler smsc95xx_in_profiler __profiler =
  43. { .name = "smsc95xx.in" };
  44. /** Bulk OUT profiler */
  45. static struct profiler smsc95xx_out_profiler __profiler =
  46. { .name = "smsc95xx.out" };
  47. /******************************************************************************
  48. *
  49. * Register access
  50. *
  51. ******************************************************************************
  52. */
  53. /**
  54. * Write register (without byte-swapping)
  55. *
  56. * @v smsc95xx SMSC95xx device
  57. * @v address Register address
  58. * @v value Register value
  59. * @ret rc Return status code
  60. */
  61. static int smsc95xx_raw_writel ( struct smsc95xx_device *smsc95xx,
  62. unsigned int address, uint32_t value ) {
  63. int rc;
  64. /* Write register */
  65. DBGCIO ( smsc95xx, "SMSC95XX %p [%03x] <= %08x\n",
  66. smsc95xx, address, le32_to_cpu ( value ) );
  67. if ( ( rc = usb_control ( smsc95xx->usb, SMSC95XX_REGISTER_WRITE, 0,
  68. address, &value, sizeof ( value ) ) ) != 0 ) {
  69. DBGC ( smsc95xx, "SMSC95XX %p could not write %03x: %s\n",
  70. smsc95xx, address, strerror ( rc ) );
  71. return rc;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * Write register
  77. *
  78. * @v smsc95xx SMSC95xx device
  79. * @v address Register address
  80. * @v value Register value
  81. * @ret rc Return status code
  82. */
  83. static inline __attribute__ (( always_inline )) int
  84. smsc95xx_writel ( struct smsc95xx_device *smsc95xx, unsigned int address,
  85. uint32_t value ) {
  86. int rc;
  87. /* Write register */
  88. if ( ( rc = smsc95xx_raw_writel ( smsc95xx, address,
  89. cpu_to_le32 ( value ) ) ) != 0 )
  90. return rc;
  91. return 0;
  92. }
  93. /**
  94. * Read register (without byte-swapping)
  95. *
  96. * @v smsc95xx SMSC95xx device
  97. * @v address Register address
  98. * @ret value Register value
  99. * @ret rc Return status code
  100. */
  101. static int smsc95xx_raw_readl ( struct smsc95xx_device *smsc95xx,
  102. unsigned int address, uint32_t *value ) {
  103. int rc;
  104. /* Read register */
  105. if ( ( rc = usb_control ( smsc95xx->usb, SMSC95XX_REGISTER_READ, 0,
  106. address, value, sizeof ( *value ) ) ) != 0 ) {
  107. DBGC ( smsc95xx, "SMSC95XX %p could not read %03x: %s\n",
  108. smsc95xx, address, strerror ( rc ) );
  109. return rc;
  110. }
  111. DBGCIO ( smsc95xx, "SMSC95XX %p [%03x] => %08x\n",
  112. smsc95xx, address, le32_to_cpu ( *value ) );
  113. return 0;
  114. }
  115. /**
  116. * Read register
  117. *
  118. * @v smsc95xx SMSC95xx device
  119. * @v address Register address
  120. * @ret value Register value
  121. * @ret rc Return status code
  122. */
  123. static inline __attribute__ (( always_inline )) int
  124. smsc95xx_readl ( struct smsc95xx_device *smsc95xx, unsigned int address,
  125. uint32_t *value ) {
  126. int rc;
  127. /* Read register */
  128. if ( ( rc = smsc95xx_raw_readl ( smsc95xx, address, value ) ) != 0 )
  129. return rc;
  130. le32_to_cpus ( value );
  131. return 0;
  132. }
  133. /******************************************************************************
  134. *
  135. * EEPROM access
  136. *
  137. ******************************************************************************
  138. */
  139. /**
  140. * Wait for EEPROM to become idle
  141. *
  142. * @v smsc95xx SMSC95xx device
  143. * @ret rc Return status code
  144. */
  145. static int smsc95xx_eeprom_wait ( struct smsc95xx_device *smsc95xx ) {
  146. uint32_t e2p_cmd;
  147. unsigned int i;
  148. int rc;
  149. /* Wait for EPC_BSY to become clear */
  150. for ( i = 0 ; i < SMSC95XX_EEPROM_MAX_WAIT_MS ; i++ ) {
  151. /* Read E2P_CMD and check EPC_BSY */
  152. if ( ( rc = smsc95xx_readl ( smsc95xx, SMSC95XX_E2P_CMD,
  153. &e2p_cmd ) ) != 0 )
  154. return rc;
  155. if ( ! ( e2p_cmd & SMSC95XX_E2P_CMD_EPC_BSY ) )
  156. return 0;
  157. /* Delay */
  158. mdelay ( 1 );
  159. }
  160. DBGC ( smsc95xx, "SMSC95XX %p timed out waiting for EEPROM\n",
  161. smsc95xx );
  162. return -ETIMEDOUT;
  163. }
  164. /**
  165. * Read byte from EEPROM
  166. *
  167. * @v smsc95xx SMSC95xx device
  168. * @v address EEPROM address
  169. * @ret byte Byte read, or negative error
  170. */
  171. static int smsc95xx_eeprom_read_byte ( struct smsc95xx_device *smsc95xx,
  172. unsigned int address ) {
  173. uint32_t e2p_cmd;
  174. uint32_t e2p_data;
  175. int rc;
  176. /* Wait for EEPROM to become idle */
  177. if ( ( rc = smsc95xx_eeprom_wait ( smsc95xx ) ) != 0 )
  178. return rc;
  179. /* Initiate read command */
  180. e2p_cmd = ( SMSC95XX_E2P_CMD_EPC_BSY | SMSC95XX_E2P_CMD_EPC_CMD_READ |
  181. SMSC95XX_E2P_CMD_EPC_ADDR ( address ) );
  182. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_E2P_CMD,
  183. e2p_cmd ) ) != 0 )
  184. return rc;
  185. /* Wait for command to complete */
  186. if ( ( rc = smsc95xx_eeprom_wait ( smsc95xx ) ) != 0 )
  187. return rc;
  188. /* Read EEPROM data */
  189. if ( ( rc = smsc95xx_readl ( smsc95xx, SMSC95XX_E2P_DATA,
  190. &e2p_data ) ) != 0 )
  191. return rc;
  192. return SMSC95XX_E2P_DATA_GET ( e2p_data );
  193. }
  194. /**
  195. * Read data from EEPROM
  196. *
  197. * @v smsc95xx SMSC95xx device
  198. * @v address EEPROM address
  199. * @v data Data buffer
  200. * @v len Length of data
  201. * @ret rc Return status code
  202. */
  203. static int smsc95xx_eeprom_read ( struct smsc95xx_device *smsc95xx,
  204. unsigned int address, void *data,
  205. size_t len ) {
  206. uint8_t *bytes;
  207. int byte;
  208. /* Read bytes */
  209. for ( bytes = data ; len-- ; address++, bytes++ ) {
  210. byte = smsc95xx_eeprom_read_byte ( smsc95xx, address );
  211. if ( byte < 0 )
  212. return byte;
  213. *bytes = byte;
  214. }
  215. return 0;
  216. }
  217. /******************************************************************************
  218. *
  219. * MAC address
  220. *
  221. ******************************************************************************
  222. */
  223. /**
  224. * Fetch MAC address from EEPROM
  225. *
  226. * @v smsc95xx SMSC95xx device
  227. * @v hw_addr Hardware address to fill in
  228. * @ret rc Return status code
  229. */
  230. static int smsc95xx_fetch_mac_eeprom ( struct smsc95xx_device *smsc95xx,
  231. uint8_t *hw_addr ) {
  232. int rc;
  233. /* Read MAC address from EEPROM */
  234. if ( ( rc = smsc95xx_eeprom_read ( smsc95xx, SMSC95XX_EEPROM_MAC,
  235. hw_addr, ETH_ALEN ) ) != 0 )
  236. return rc;
  237. /* Check that EEPROM is physically present */
  238. if ( ! is_valid_ether_addr ( hw_addr ) ) {
  239. DBGC ( smsc95xx, "SMSC95XX %p has no EEPROM (%s)\n",
  240. smsc95xx, eth_ntoa ( hw_addr ) );
  241. return -ENODEV;
  242. }
  243. return 0;
  244. }
  245. /**
  246. * Fetch MAC address
  247. *
  248. * @v smsc95xx SMSC95xx device
  249. * @v hw_addr Hardware address to fill in
  250. * @ret rc Return status code
  251. */
  252. static int smsc95xx_fetch_mac ( struct smsc95xx_device *smsc95xx,
  253. uint8_t *hw_addr ) {
  254. int rc;
  255. /* Read MAC address from EEPROM, if present */
  256. if ( ( rc = smsc95xx_fetch_mac_eeprom ( smsc95xx, hw_addr ) ) == 0 )
  257. return 0;
  258. /* Otherwise, generate a random MAC address */
  259. eth_random_addr ( hw_addr );
  260. return 0;
  261. }
  262. /******************************************************************************
  263. *
  264. * MII access
  265. *
  266. ******************************************************************************
  267. */
  268. /**
  269. * Wait for MII to become idle
  270. *
  271. * @v smsc95xx SMSC95xx device
  272. * @ret rc Return status code
  273. */
  274. static int smsc95xx_mii_wait ( struct smsc95xx_device *smsc95xx ) {
  275. uint32_t mii_access;
  276. unsigned int i;
  277. int rc;
  278. /* Wait for MIIBZY to become clear */
  279. for ( i = 0 ; i < SMSC95XX_MII_MAX_WAIT_MS ; i++ ) {
  280. /* Read MII_ACCESS and check MIIBZY */
  281. if ( ( rc = smsc95xx_readl ( smsc95xx, SMSC95XX_MII_ACCESS,
  282. &mii_access ) ) != 0 )
  283. return rc;
  284. if ( ! ( mii_access & SMSC95XX_MII_ACCESS_MIIBZY ) )
  285. return 0;
  286. /* Delay */
  287. mdelay ( 1 );
  288. }
  289. DBGC ( smsc95xx, "SMSC95XX %p timed out waiting for MII\n",
  290. smsc95xx );
  291. return -ETIMEDOUT;
  292. }
  293. /**
  294. * Read from MII register
  295. *
  296. * @v mii MII interface
  297. * @v reg Register address
  298. * @ret value Data read, or negative error
  299. */
  300. static int smsc95xx_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  301. struct smsc95xx_device *smsc95xx =
  302. container_of ( mii, struct smsc95xx_device, mii );
  303. uint32_t mii_access;
  304. uint32_t mii_data;
  305. int rc;
  306. /* Wait for MII to become idle */
  307. if ( ( rc = smsc95xx_mii_wait ( smsc95xx ) ) != 0 )
  308. return rc;
  309. /* Initiate read command */
  310. mii_access = ( SMSC95XX_MII_ACCESS_PHY_ADDRESS |
  311. SMSC95XX_MII_ACCESS_MIIRINDA ( reg ) |
  312. SMSC95XX_MII_ACCESS_MIIBZY );
  313. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_MII_ACCESS,
  314. mii_access ) ) != 0 )
  315. return rc;
  316. /* Wait for command to complete */
  317. if ( ( rc = smsc95xx_mii_wait ( smsc95xx ) ) != 0 )
  318. return rc;
  319. /* Read MII data */
  320. if ( ( rc = smsc95xx_readl ( smsc95xx, SMSC95XX_MII_DATA,
  321. &mii_data ) ) != 0 )
  322. return rc;
  323. return SMSC95XX_MII_DATA_GET ( mii_data );
  324. }
  325. /**
  326. * Write to MII register
  327. *
  328. * @v mii MII interface
  329. * @v reg Register address
  330. * @v data Data to write
  331. * @ret rc Return status code
  332. */
  333. static int smsc95xx_mii_write ( struct mii_interface *mii, unsigned int reg,
  334. unsigned int data ) {
  335. struct smsc95xx_device *smsc95xx =
  336. container_of ( mii, struct smsc95xx_device, mii );
  337. uint32_t mii_access;
  338. uint32_t mii_data;
  339. int rc;
  340. /* Wait for MII to become idle */
  341. if ( ( rc = smsc95xx_mii_wait ( smsc95xx ) ) != 0 )
  342. return rc;
  343. /* Write MII data */
  344. mii_data = SMSC95XX_MII_DATA_SET ( data );
  345. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_MII_DATA,
  346. mii_data ) ) != 0 )
  347. return rc;
  348. /* Initiate write command */
  349. mii_access = ( SMSC95XX_MII_ACCESS_PHY_ADDRESS |
  350. SMSC95XX_MII_ACCESS_MIIRINDA ( reg ) |
  351. SMSC95XX_MII_ACCESS_MIIWNR |
  352. SMSC95XX_MII_ACCESS_MIIBZY );
  353. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_MII_ACCESS,
  354. mii_access ) ) != 0 )
  355. return rc;
  356. /* Wait for command to complete */
  357. if ( ( rc = smsc95xx_mii_wait ( smsc95xx ) ) != 0 )
  358. return rc;
  359. return 0;
  360. }
  361. /** MII operations */
  362. static struct mii_operations smsc95xx_mii_operations = {
  363. .read = smsc95xx_mii_read,
  364. .write = smsc95xx_mii_write,
  365. };
  366. /**
  367. * Check link status
  368. *
  369. * @v smsc95xx SMSC95xx device
  370. * @ret rc Return status code
  371. */
  372. static int smsc95xx_check_link ( struct smsc95xx_device *smsc95xx ) {
  373. struct net_device *netdev = smsc95xx->netdev;
  374. int intr;
  375. int rc;
  376. /* Read PHY interrupt source */
  377. intr = mii_read ( &smsc95xx->mii, SMSC95XX_MII_PHY_INTR_SOURCE );
  378. if ( intr < 0 ) {
  379. rc = intr;
  380. DBGC ( smsc95xx, "SMSC95XX %p could not get PHY interrupt "
  381. "source: %s\n", smsc95xx, strerror ( rc ) );
  382. return rc;
  383. }
  384. /* Acknowledge PHY interrupt */
  385. if ( ( rc = mii_write ( &smsc95xx->mii, SMSC95XX_MII_PHY_INTR_SOURCE,
  386. intr ) ) != 0 ) {
  387. DBGC ( smsc95xx, "SMSC95XX %p could not acknowledge PHY "
  388. "interrupt: %s\n", smsc95xx, strerror ( rc ) );
  389. return rc;
  390. }
  391. /* Check link status */
  392. if ( ( rc = mii_check_link ( &smsc95xx->mii, netdev ) ) != 0 ) {
  393. DBGC ( smsc95xx, "SMSC95XX %p could not check link: %s\n",
  394. smsc95xx, strerror ( rc ) );
  395. return rc;
  396. }
  397. DBGC ( smsc95xx, "SMSC95XX %p link %s (intr %#04x)\n",
  398. smsc95xx, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
  399. return 0;
  400. }
  401. /******************************************************************************
  402. *
  403. * Statistics (for debugging)
  404. *
  405. ******************************************************************************
  406. */
  407. /**
  408. * Get RX statistics
  409. *
  410. * @v smsc95xx SMSC95xx device
  411. * @v stats Statistics to fill in
  412. * @ret rc Return status code
  413. */
  414. static int smsc95xx_get_rx_statistics ( struct smsc95xx_device *smsc95xx,
  415. struct smsc95xx_rx_statistics *stats ) {
  416. int rc;
  417. /* Get statistics */
  418. if ( ( rc = usb_control ( smsc95xx->usb, SMSC95XX_GET_STATISTICS, 0,
  419. SMSC95XX_RX_STATISTICS, stats,
  420. sizeof ( *stats ) ) ) != 0 ) {
  421. DBGC ( smsc95xx, "SMSC95XX %p could not get RX statistics: "
  422. "%s\n", smsc95xx, strerror ( rc ) );
  423. return rc;
  424. }
  425. return 0;
  426. }
  427. /**
  428. * Get TX statistics
  429. *
  430. * @v smsc95xx SMSC95xx device
  431. * @v stats Statistics to fill in
  432. * @ret rc Return status code
  433. */
  434. static int smsc95xx_get_tx_statistics ( struct smsc95xx_device *smsc95xx,
  435. struct smsc95xx_tx_statistics *stats ) {
  436. int rc;
  437. /* Get statistics */
  438. if ( ( rc = usb_control ( smsc95xx->usb, SMSC95XX_GET_STATISTICS, 0,
  439. SMSC95XX_TX_STATISTICS, stats,
  440. sizeof ( *stats ) ) ) != 0 ) {
  441. DBGC ( smsc95xx, "SMSC95XX %p could not get TX statistics: "
  442. "%s\n", smsc95xx, strerror ( rc ) );
  443. return rc;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * Dump statistics (for debugging)
  449. *
  450. * @v smsc95xx SMSC95xx device
  451. * @ret rc Return status code
  452. */
  453. static int smsc95xx_dump_statistics ( struct smsc95xx_device *smsc95xx ) {
  454. struct smsc95xx_rx_statistics rx;
  455. struct smsc95xx_tx_statistics tx;
  456. int rc;
  457. /* Do nothing unless debugging is enabled */
  458. if ( ! DBG_LOG )
  459. return 0;
  460. /* Get RX statistics */
  461. if ( ( rc = smsc95xx_get_rx_statistics ( smsc95xx, &rx ) ) != 0 )
  462. return rc;
  463. /* Get TX statistics */
  464. if ( ( rc = smsc95xx_get_tx_statistics ( smsc95xx, &tx ) ) != 0 )
  465. return rc;
  466. /* Dump statistics */
  467. DBGC ( smsc95xx, "SMSC95XX %p RX good %d bad %d crc %d und %d aln %d "
  468. "ovr %d lat %d drp %d\n", smsc95xx, le32_to_cpu ( rx.good ),
  469. le32_to_cpu ( rx.bad ), le32_to_cpu ( rx.crc ),
  470. le32_to_cpu ( rx.undersize ), le32_to_cpu ( rx.alignment ),
  471. le32_to_cpu ( rx.oversize ), le32_to_cpu ( rx.late ),
  472. le32_to_cpu ( rx.dropped ) );
  473. DBGC ( smsc95xx, "SMSC95XX %p TX good %d bad %d pau %d sgl %d mul %d "
  474. "exc %d lat %d und %d def %d car %d\n", smsc95xx,
  475. le32_to_cpu ( tx.good ), le32_to_cpu ( tx.bad ),
  476. le32_to_cpu ( tx.pause ), le32_to_cpu ( tx.single ),
  477. le32_to_cpu ( tx.multiple ), le32_to_cpu ( tx.excessive ),
  478. le32_to_cpu ( tx.late ), le32_to_cpu ( tx.underrun ),
  479. le32_to_cpu ( tx.deferred ), le32_to_cpu ( tx.carrier ) );
  480. return 0;
  481. }
  482. /******************************************************************************
  483. *
  484. * Device reset
  485. *
  486. ******************************************************************************
  487. */
  488. /**
  489. * Reset device
  490. *
  491. * @v smsc95xx SMSC95xx device
  492. * @ret rc Return status code
  493. */
  494. static int smsc95xx_reset ( struct smsc95xx_device *smsc95xx ) {
  495. uint32_t hw_cfg;
  496. int rc;
  497. /* Reset device */
  498. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_HW_CFG,
  499. SMSC95XX_HW_CFG_LRST ) ) != 0 )
  500. return rc;
  501. /* Wait for reset to complete */
  502. udelay ( SMSC95XX_RESET_DELAY_US );
  503. /* Check that reset has completed */
  504. if ( ( rc = smsc95xx_readl ( smsc95xx, SMSC95XX_HW_CFG,
  505. &hw_cfg ) ) != 0 )
  506. return rc;
  507. if ( hw_cfg & SMSC95XX_HW_CFG_LRST ) {
  508. DBGC ( smsc95xx, "SMSC95XX %p failed to reset\n", smsc95xx );
  509. return -ETIMEDOUT;
  510. }
  511. return 0;
  512. }
  513. /******************************************************************************
  514. *
  515. * Endpoint operations
  516. *
  517. ******************************************************************************
  518. */
  519. /**
  520. * Complete interrupt transfer
  521. *
  522. * @v ep USB endpoint
  523. * @v iobuf I/O buffer
  524. * @v rc Completion status code
  525. */
  526. static void smsc95xx_intr_complete ( struct usb_endpoint *ep,
  527. struct io_buffer *iobuf, int rc ) {
  528. struct smsc95xx_device *smsc95xx =
  529. container_of ( ep, struct smsc95xx_device, usbnet.intr );
  530. struct net_device *netdev = smsc95xx->netdev;
  531. struct smsc95xx_interrupt *intr;
  532. /* Profile completions */
  533. profile_start ( &smsc95xx_intr_profiler );
  534. /* Ignore packets cancelled when the endpoint closes */
  535. if ( ! ep->open )
  536. goto done;
  537. /* Record USB errors against the network device */
  538. if ( rc != 0 ) {
  539. DBGC ( smsc95xx, "SMSC95XX %p interrupt failed: %s\n",
  540. smsc95xx, strerror ( rc ) );
  541. DBGC_HDA ( smsc95xx, 0, iobuf->data, iob_len ( iobuf ) );
  542. netdev_rx_err ( netdev, NULL, rc );
  543. goto done;
  544. }
  545. /* Extract interrupt data */
  546. if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
  547. DBGC ( smsc95xx, "SMSC95XX %p malformed interrupt\n",
  548. smsc95xx );
  549. DBGC_HDA ( smsc95xx, 0, iobuf->data, iob_len ( iobuf ) );
  550. netdev_rx_err ( netdev, NULL, rc );
  551. goto done;
  552. }
  553. intr = iobuf->data;
  554. /* Record interrupt status */
  555. smsc95xx->int_sts = le32_to_cpu ( intr->int_sts );
  556. profile_stop ( &smsc95xx_intr_profiler );
  557. done:
  558. /* Free I/O buffer */
  559. free_iob ( iobuf );
  560. }
  561. /** Interrupt endpoint operations */
  562. static struct usb_endpoint_driver_operations smsc95xx_intr_operations = {
  563. .complete = smsc95xx_intr_complete,
  564. };
  565. /**
  566. * Complete bulk IN transfer
  567. *
  568. * @v ep USB endpoint
  569. * @v iobuf I/O buffer
  570. * @v rc Completion status code
  571. */
  572. static void smsc95xx_in_complete ( struct usb_endpoint *ep,
  573. struct io_buffer *iobuf, int rc ) {
  574. struct smsc95xx_device *smsc95xx =
  575. container_of ( ep, struct smsc95xx_device, usbnet.in );
  576. struct net_device *netdev = smsc95xx->netdev;
  577. struct smsc95xx_rx_header *header;
  578. /* Profile completions */
  579. profile_start ( &smsc95xx_in_profiler );
  580. /* Ignore packets cancelled when the endpoint closes */
  581. if ( ! ep->open ) {
  582. free_iob ( iobuf );
  583. return;
  584. }
  585. /* Record USB errors against the network device */
  586. if ( rc != 0 ) {
  587. DBGC ( smsc95xx, "SMSC95XX %p bulk IN failed: %s\n",
  588. smsc95xx, strerror ( rc ) );
  589. goto err;
  590. }
  591. /* Sanity check */
  592. if ( iob_len ( iobuf ) < ( sizeof ( *header ) + 4 /* CRC */ ) ) {
  593. DBGC ( smsc95xx, "SMSC95XX %p underlength bulk IN\n",
  594. smsc95xx );
  595. DBGC_HDA ( smsc95xx, 0, iobuf->data, iob_len ( iobuf ) );
  596. rc = -EINVAL;
  597. goto err;
  598. }
  599. /* Strip header and CRC */
  600. header = iobuf->data;
  601. iob_pull ( iobuf, sizeof ( *header ) );
  602. iob_unput ( iobuf, 4 /* CRC */ );
  603. /* Check for errors */
  604. if ( header->command & cpu_to_le32 ( SMSC95XX_RX_RUNT |
  605. SMSC95XX_RX_LATE |
  606. SMSC95XX_RX_CRC ) ) {
  607. DBGC ( smsc95xx, "SMSC95XX %p receive error (%08x):\n",
  608. smsc95xx, le32_to_cpu ( header->command ) );
  609. DBGC_HDA ( smsc95xx, 0, iobuf->data, iob_len ( iobuf ) );
  610. rc = -EIO;
  611. goto err;
  612. }
  613. /* Hand off to network stack */
  614. netdev_rx ( netdev, iob_disown ( iobuf ) );
  615. profile_stop ( &smsc95xx_in_profiler );
  616. return;
  617. err:
  618. /* Hand off to network stack */
  619. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  620. }
  621. /** Bulk IN endpoint operations */
  622. static struct usb_endpoint_driver_operations smsc95xx_in_operations = {
  623. .complete = smsc95xx_in_complete,
  624. };
  625. /**
  626. * Transmit packet
  627. *
  628. * @v smsc95xx SMSC95xx device
  629. * @v iobuf I/O buffer
  630. * @ret rc Return status code
  631. */
  632. static int smsc95xx_out_transmit ( struct smsc95xx_device *smsc95xx,
  633. struct io_buffer *iobuf ) {
  634. struct smsc95xx_tx_header *header;
  635. size_t len = iob_len ( iobuf );
  636. int rc;
  637. /* Profile transmissions */
  638. profile_start ( &smsc95xx_out_profiler );
  639. /* Prepend header */
  640. if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
  641. return rc;
  642. header = iob_push ( iobuf, sizeof ( *header ) );
  643. header->command = cpu_to_le32 ( SMSC95XX_TX_FIRST | SMSC95XX_TX_LAST |
  644. SMSC95XX_TX_LEN ( len ) );
  645. header->len = cpu_to_le32 ( len );
  646. /* Enqueue I/O buffer */
  647. if ( ( rc = usb_stream ( &smsc95xx->usbnet.out, iobuf, 0 ) ) != 0 )
  648. return rc;
  649. profile_stop ( &smsc95xx_out_profiler );
  650. return 0;
  651. }
  652. /**
  653. * Complete bulk OUT transfer
  654. *
  655. * @v ep USB endpoint
  656. * @v iobuf I/O buffer
  657. * @v rc Completion status code
  658. */
  659. static void smsc95xx_out_complete ( struct usb_endpoint *ep,
  660. struct io_buffer *iobuf, int rc ) {
  661. struct smsc95xx_device *smsc95xx =
  662. container_of ( ep, struct smsc95xx_device, usbnet.out );
  663. struct net_device *netdev = smsc95xx->netdev;
  664. /* Report TX completion */
  665. netdev_tx_complete_err ( netdev, iobuf, rc );
  666. }
  667. /** Bulk OUT endpoint operations */
  668. static struct usb_endpoint_driver_operations smsc95xx_out_operations = {
  669. .complete = smsc95xx_out_complete,
  670. };
  671. /******************************************************************************
  672. *
  673. * Network device interface
  674. *
  675. ******************************************************************************
  676. */
  677. /**
  678. * Open network device
  679. *
  680. * @v netdev Network device
  681. * @ret rc Return status code
  682. */
  683. static int smsc95xx_open ( struct net_device *netdev ) {
  684. struct smsc95xx_device *smsc95xx = netdev->priv;
  685. union smsc95xx_mac mac;
  686. int rc;
  687. /* Clear stored interrupt status */
  688. smsc95xx->int_sts = 0;
  689. /* Copy MAC address */
  690. memset ( &mac, 0, sizeof ( mac ) );
  691. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  692. /* Configure bulk IN empty response */
  693. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_HW_CFG,
  694. SMSC95XX_HW_CFG_BIR ) ) != 0 )
  695. goto err_hw_cfg;
  696. /* Open USB network device */
  697. if ( ( rc = usbnet_open ( &smsc95xx->usbnet ) ) != 0 ) {
  698. DBGC ( smsc95xx, "SMSC95XX %p could not open: %s\n",
  699. smsc95xx, strerror ( rc ) );
  700. goto err_open;
  701. }
  702. /* Configure interrupt endpoint */
  703. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_INT_EP_CTL,
  704. ( SMSC95XX_INT_EP_CTL_RXDF_EN |
  705. SMSC95XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  706. goto err_int_ep_ctl;
  707. /* Configure bulk IN delay */
  708. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_BULK_IN_DLY,
  709. SMSC95XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  710. goto err_bulk_in_dly;
  711. /* Configure MAC */
  712. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_MAC_CR,
  713. ( SMSC95XX_MAC_CR_RXALL |
  714. SMSC95XX_MAC_CR_FDPX |
  715. SMSC95XX_MAC_CR_MCPAS |
  716. SMSC95XX_MAC_CR_PRMS |
  717. SMSC95XX_MAC_CR_PASSBAD |
  718. SMSC95XX_MAC_CR_TXEN |
  719. SMSC95XX_MAC_CR_RXEN ) ) ) != 0 )
  720. goto err_mac_cr;
  721. /* Configure transmit datapath */
  722. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_TX_CFG,
  723. SMSC95XX_TX_CFG_ON ) ) != 0 )
  724. goto err_tx_cfg;
  725. /* Write MAC address high register */
  726. if ( ( rc = smsc95xx_raw_writel ( smsc95xx, SMSC95XX_ADDRH,
  727. mac.addr.h ) ) != 0 )
  728. goto err_addrh;
  729. /* Write MAC address low register */
  730. if ( ( rc = smsc95xx_raw_writel ( smsc95xx, SMSC95XX_ADDRL,
  731. mac.addr.l ) ) != 0 )
  732. goto err_addrl;
  733. /* Enable PHY interrupts */
  734. if ( ( rc = mii_write ( &smsc95xx->mii, SMSC95XX_MII_PHY_INTR_MASK,
  735. ( SMSC95XX_PHY_INTR_ANEG_DONE |
  736. SMSC95XX_PHY_INTR_LINK_DOWN ) ) ) != 0 ) {
  737. DBGC ( smsc95xx, "SMSC95XX %p could not set PHY interrupt "
  738. "mask: %s\n", smsc95xx, strerror ( rc ) );
  739. goto err_phy_intr_mask;
  740. }
  741. /* Update link status */
  742. smsc95xx_check_link ( smsc95xx );
  743. return 0;
  744. err_phy_intr_mask:
  745. err_addrl:
  746. err_addrh:
  747. err_tx_cfg:
  748. err_mac_cr:
  749. err_bulk_in_dly:
  750. err_int_ep_ctl:
  751. usbnet_close ( &smsc95xx->usbnet );
  752. err_open:
  753. err_hw_cfg:
  754. smsc95xx_reset ( smsc95xx );
  755. return rc;
  756. }
  757. /**
  758. * Close network device
  759. *
  760. * @v netdev Network device
  761. */
  762. static void smsc95xx_close ( struct net_device *netdev ) {
  763. struct smsc95xx_device *smsc95xx = netdev->priv;
  764. /* Close USB network device */
  765. usbnet_close ( &smsc95xx->usbnet );
  766. /* Dump statistics (for debugging) */
  767. smsc95xx_dump_statistics ( smsc95xx );
  768. /* Reset device */
  769. smsc95xx_reset ( smsc95xx );
  770. }
  771. /**
  772. * Transmit packet
  773. *
  774. * @v netdev Network device
  775. * @v iobuf I/O buffer
  776. * @ret rc Return status code
  777. */
  778. static int smsc95xx_transmit ( struct net_device *netdev,
  779. struct io_buffer *iobuf ) {
  780. struct smsc95xx_device *smsc95xx = netdev->priv;
  781. int rc;
  782. /* Transmit packet */
  783. if ( ( rc = smsc95xx_out_transmit ( smsc95xx, iobuf ) ) != 0 )
  784. return rc;
  785. return 0;
  786. }
  787. /**
  788. * Poll for completed and received packets
  789. *
  790. * @v netdev Network device
  791. */
  792. static void smsc95xx_poll ( struct net_device *netdev ) {
  793. struct smsc95xx_device *smsc95xx = netdev->priv;
  794. uint32_t int_sts;
  795. int rc;
  796. /* Poll USB bus */
  797. usb_poll ( smsc95xx->bus );
  798. /* Refill endpoints */
  799. if ( ( rc = usbnet_refill ( &smsc95xx->usbnet ) ) != 0 )
  800. netdev_rx_err ( netdev, NULL, rc );
  801. /* Do nothing more unless there are interrupts to handle */
  802. int_sts = smsc95xx->int_sts;
  803. if ( ! int_sts )
  804. return;
  805. /* Check link status if applicable */
  806. if ( int_sts & SMSC95XX_INT_STS_PHY_INT ) {
  807. smsc95xx_check_link ( smsc95xx );
  808. int_sts &= ~SMSC95XX_INT_STS_PHY_INT;
  809. }
  810. /* Record RX FIFO overflow if applicable */
  811. if ( int_sts & SMSC95XX_INT_STS_RXDF_INT ) {
  812. DBGC2 ( smsc95xx, "SMSC95XX %p RX FIFO overflowed\n",
  813. smsc95xx );
  814. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  815. int_sts &= ~SMSC95XX_INT_STS_RXDF_INT;
  816. }
  817. /* Check for unexpected interrupts */
  818. if ( int_sts ) {
  819. DBGC ( smsc95xx, "SMSC95XX %p unexpected interrupt %#08x\n",
  820. smsc95xx, int_sts );
  821. netdev_rx_err ( netdev, NULL, -ENOTTY );
  822. }
  823. /* Clear interrupts */
  824. if ( ( rc = smsc95xx_writel ( smsc95xx, SMSC95XX_INT_STS,
  825. smsc95xx->int_sts ) ) != 0 )
  826. netdev_rx_err ( netdev, NULL, rc );
  827. smsc95xx->int_sts = 0;
  828. }
  829. /** SMSC95xx network device operations */
  830. static struct net_device_operations smsc95xx_operations = {
  831. .open = smsc95xx_open,
  832. .close = smsc95xx_close,
  833. .transmit = smsc95xx_transmit,
  834. .poll = smsc95xx_poll,
  835. };
  836. /******************************************************************************
  837. *
  838. * USB interface
  839. *
  840. ******************************************************************************
  841. */
  842. /**
  843. * Probe device
  844. *
  845. * @v func USB function
  846. * @v config Configuration descriptor
  847. * @ret rc Return status code
  848. */
  849. static int smsc95xx_probe ( struct usb_function *func,
  850. struct usb_configuration_descriptor *config ) {
  851. struct usb_device *usb = func->usb;
  852. struct net_device *netdev;
  853. struct smsc95xx_device *smsc95xx;
  854. int rc;
  855. /* Allocate and initialise structure */
  856. netdev = alloc_etherdev ( sizeof ( *smsc95xx ) );
  857. if ( ! netdev ) {
  858. rc = -ENOMEM;
  859. goto err_alloc;
  860. }
  861. netdev_init ( netdev, &smsc95xx_operations );
  862. netdev->dev = &func->dev;
  863. smsc95xx = netdev->priv;
  864. memset ( smsc95xx, 0, sizeof ( *smsc95xx ) );
  865. smsc95xx->usb = usb;
  866. smsc95xx->bus = usb->port->hub->bus;
  867. smsc95xx->netdev = netdev;
  868. usbnet_init ( &smsc95xx->usbnet, func, &smsc95xx_intr_operations,
  869. &smsc95xx_in_operations, &smsc95xx_out_operations );
  870. usb_refill_init ( &smsc95xx->usbnet.intr, 0, SMSC95XX_INTR_MAX_FILL );
  871. usb_refill_init ( &smsc95xx->usbnet.in, SMSC95XX_IN_MTU,
  872. SMSC95XX_IN_MAX_FILL );
  873. mii_init ( &smsc95xx->mii, &smsc95xx_mii_operations );
  874. DBGC ( smsc95xx, "SMSC95XX %p on %s\n", smsc95xx, func->name );
  875. /* Describe USB network device */
  876. if ( ( rc = usbnet_describe ( &smsc95xx->usbnet, config ) ) != 0 ) {
  877. DBGC ( smsc95xx, "SMSC95XX %p could not describe: %s\n",
  878. smsc95xx, strerror ( rc ) );
  879. goto err_describe;
  880. }
  881. /* Reset device */
  882. if ( ( rc = smsc95xx_reset ( smsc95xx ) ) != 0 )
  883. goto err_reset;
  884. /* Read MAC address */
  885. if ( ( rc = smsc95xx_fetch_mac ( smsc95xx, netdev->hw_addr ) ) != 0 )
  886. goto err_fetch_mac;
  887. /* Register network device */
  888. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  889. goto err_register;
  890. usb_func_set_drvdata ( func, netdev );
  891. return 0;
  892. unregister_netdev ( netdev );
  893. err_register:
  894. err_fetch_mac:
  895. err_reset:
  896. err_describe:
  897. netdev_nullify ( netdev );
  898. netdev_put ( netdev );
  899. err_alloc:
  900. return rc;
  901. }
  902. /**
  903. * Remove device
  904. *
  905. * @v func USB function
  906. */
  907. static void smsc95xx_remove ( struct usb_function *func ) {
  908. struct net_device *netdev = usb_func_get_drvdata ( func );
  909. unregister_netdev ( netdev );
  910. netdev_nullify ( netdev );
  911. netdev_put ( netdev );
  912. }
  913. /** SMSC95xx device IDs */
  914. static struct usb_device_id smsc95xx_ids[] = {
  915. {
  916. .name = "smsc9500",
  917. .vendor = 0x0424,
  918. .product = 0x9500,
  919. },
  920. {
  921. .name = "smsc9505",
  922. .vendor = 0x0424,
  923. .product = 0x9505,
  924. },
  925. {
  926. .name = "smsc9500a",
  927. .vendor = 0x0424,
  928. .product = 0x9e00,
  929. },
  930. {
  931. .name = "smsc9505a",
  932. .vendor = 0x0424,
  933. .product = 0x9e01,
  934. },
  935. {
  936. .name = "smsc9514",
  937. .vendor = 0x0424,
  938. .product = 0xec00,
  939. },
  940. {
  941. .name = "smsc9500-s",
  942. .vendor = 0x0424,
  943. .product = 0x9900,
  944. },
  945. {
  946. .name = "smsc9505-s",
  947. .vendor = 0x0424,
  948. .product = 0x9901,
  949. },
  950. {
  951. .name = "smsc9500a-s",
  952. .vendor = 0x0424,
  953. .product = 0x9902,
  954. },
  955. {
  956. .name = "smsc9505a-s",
  957. .vendor = 0x0424,
  958. .product = 0x9903,
  959. },
  960. {
  961. .name = "smsc9514-s",
  962. .vendor = 0x0424,
  963. .product = 0x9904,
  964. },
  965. {
  966. .name = "smsc9500a-h",
  967. .vendor = 0x0424,
  968. .product = 0x9905,
  969. },
  970. {
  971. .name = "smsc9505a-h",
  972. .vendor = 0x0424,
  973. .product = 0x9906,
  974. },
  975. {
  976. .name = "smsc9500-2",
  977. .vendor = 0x0424,
  978. .product = 0x9907,
  979. },
  980. {
  981. .name = "smsc9500a-2",
  982. .vendor = 0x0424,
  983. .product = 0x9908,
  984. },
  985. {
  986. .name = "smsc9514-2",
  987. .vendor = 0x0424,
  988. .product = 0x9909,
  989. },
  990. {
  991. .name = "smsc9530",
  992. .vendor = 0x0424,
  993. .product = 0x9530,
  994. },
  995. {
  996. .name = "smsc9730",
  997. .vendor = 0x0424,
  998. .product = 0x9730,
  999. },
  1000. {
  1001. .name = "smsc89530",
  1002. .vendor = 0x0424,
  1003. .product = 0x9e08,
  1004. },
  1005. };
  1006. /** SMSC LAN95xx driver */
  1007. struct usb_driver smsc95xx_driver __usb_driver = {
  1008. .ids = smsc95xx_ids,
  1009. .id_count = ( sizeof ( smsc95xx_ids ) / sizeof ( smsc95xx_ids[0] ) ),
  1010. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  1011. .score = USB_SCORE_NORMAL,
  1012. .probe = smsc95xx_probe,
  1013. .remove = smsc95xx_remove,
  1014. };