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.

smscusb.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 <errno.h>
  26. #include <unistd.h>
  27. #include <ipxe/usb.h>
  28. #include <ipxe/usbnet.h>
  29. #include <ipxe/ethernet.h>
  30. #include <ipxe/profile.h>
  31. #include "smscusb.h"
  32. /** @file
  33. *
  34. * SMSC USB Ethernet drivers
  35. *
  36. */
  37. /** Interrupt completion profiler */
  38. static struct profiler smscusb_intr_profiler __profiler =
  39. { .name = "smscusb.intr" };
  40. /******************************************************************************
  41. *
  42. * Register access
  43. *
  44. ******************************************************************************
  45. */
  46. /**
  47. * Write register (without byte-swapping)
  48. *
  49. * @v smscusb Smscusb device
  50. * @v address Register address
  51. * @v value Register value
  52. * @ret rc Return status code
  53. */
  54. int smscusb_raw_writel ( struct smscusb_device *smscusb, unsigned int address,
  55. uint32_t value ) {
  56. int rc;
  57. /* Write register */
  58. DBGCIO ( smscusb, "SMSCUSB %p [%03x] <= %08x\n",
  59. smscusb, address, le32_to_cpu ( value ) );
  60. if ( ( rc = usb_control ( smscusb->usb, SMSCUSB_REGISTER_WRITE, 0,
  61. address, &value, sizeof ( value ) ) ) != 0 ) {
  62. DBGC ( smscusb, "SMSCUSB %p could not write %03x: %s\n",
  63. smscusb, address, strerror ( rc ) );
  64. return rc;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * Read register (without byte-swapping)
  70. *
  71. * @v smscusb SMSC USB device
  72. * @v address Register address
  73. * @ret value Register value
  74. * @ret rc Return status code
  75. */
  76. int smscusb_raw_readl ( struct smscusb_device *smscusb, unsigned int address,
  77. uint32_t *value ) {
  78. int rc;
  79. /* Read register */
  80. if ( ( rc = usb_control ( smscusb->usb, SMSCUSB_REGISTER_READ, 0,
  81. address, value, sizeof ( *value ) ) ) != 0 ) {
  82. DBGC ( smscusb, "SMSCUSB %p could not read %03x: %s\n",
  83. smscusb, address, strerror ( rc ) );
  84. return rc;
  85. }
  86. DBGCIO ( smscusb, "SMSCUSB %p [%03x] => %08x\n",
  87. smscusb, address, le32_to_cpu ( *value ) );
  88. return 0;
  89. }
  90. /******************************************************************************
  91. *
  92. * EEPROM access
  93. *
  94. ******************************************************************************
  95. */
  96. /**
  97. * Wait for EEPROM to become idle
  98. *
  99. * @v smscusb SMSC USB device
  100. * @v e2p_base E2P register base
  101. * @ret rc Return status code
  102. */
  103. static int smscusb_eeprom_wait ( struct smscusb_device *smscusb,
  104. unsigned int e2p_base ) {
  105. uint32_t e2p_cmd;
  106. unsigned int i;
  107. int rc;
  108. /* Wait for EPC_BSY to become clear */
  109. for ( i = 0 ; i < SMSCUSB_EEPROM_MAX_WAIT_MS ; i++ ) {
  110. /* Read E2P_CMD and check EPC_BSY */
  111. if ( ( rc = smscusb_readl ( smscusb,
  112. ( e2p_base + SMSCUSB_E2P_CMD ),
  113. &e2p_cmd ) ) != 0 )
  114. return rc;
  115. if ( ! ( e2p_cmd & SMSCUSB_E2P_CMD_EPC_BSY ) )
  116. return 0;
  117. /* Delay */
  118. mdelay ( 1 );
  119. }
  120. DBGC ( smscusb, "SMSCUSB %p timed out waiting for EEPROM\n",
  121. smscusb );
  122. return -ETIMEDOUT;
  123. }
  124. /**
  125. * Read byte from EEPROM
  126. *
  127. * @v smscusb SMSC USB device
  128. * @v e2p_base E2P register base
  129. * @v address EEPROM address
  130. * @ret byte Byte read, or negative error
  131. */
  132. static int smscusb_eeprom_read_byte ( struct smscusb_device *smscusb,
  133. unsigned int e2p_base,
  134. unsigned int address ) {
  135. uint32_t e2p_cmd;
  136. uint32_t e2p_data;
  137. int rc;
  138. /* Wait for EEPROM to become idle */
  139. if ( ( rc = smscusb_eeprom_wait ( smscusb, e2p_base ) ) != 0 )
  140. return rc;
  141. /* Initiate read command */
  142. e2p_cmd = ( SMSCUSB_E2P_CMD_EPC_BSY | SMSCUSB_E2P_CMD_EPC_CMD_READ |
  143. SMSCUSB_E2P_CMD_EPC_ADDR ( address ) );
  144. if ( ( rc = smscusb_writel ( smscusb, ( e2p_base + SMSCUSB_E2P_CMD ),
  145. e2p_cmd ) ) != 0 )
  146. return rc;
  147. /* Wait for command to complete */
  148. if ( ( rc = smscusb_eeprom_wait ( smscusb, e2p_base ) ) != 0 )
  149. return rc;
  150. /* Read EEPROM data */
  151. if ( ( rc = smscusb_readl ( smscusb, ( e2p_base + SMSCUSB_E2P_DATA ),
  152. &e2p_data ) ) != 0 )
  153. return rc;
  154. return SMSCUSB_E2P_DATA_GET ( e2p_data );
  155. }
  156. /**
  157. * Read data from EEPROM
  158. *
  159. * @v smscusb SMSC USB device
  160. * @v e2p_base E2P register base
  161. * @v address EEPROM address
  162. * @v data Data buffer
  163. * @v len Length of data
  164. * @ret rc Return status code
  165. */
  166. static int smscusb_eeprom_read ( struct smscusb_device *smscusb,
  167. unsigned int e2p_base, unsigned int address,
  168. void *data, size_t len ) {
  169. uint8_t *bytes;
  170. int byte;
  171. /* Read bytes */
  172. for ( bytes = data ; len-- ; address++, bytes++ ) {
  173. byte = smscusb_eeprom_read_byte ( smscusb, e2p_base, address );
  174. if ( byte < 0 )
  175. return byte;
  176. *bytes = byte;
  177. }
  178. return 0;
  179. }
  180. /**
  181. * Fetch MAC address from EEPROM
  182. *
  183. * @v smscusb SMSC USB device
  184. * @v e2p_base E2P register base
  185. * @ret rc Return status code
  186. */
  187. int smscusb_eeprom_fetch_mac ( struct smscusb_device *smscusb,
  188. unsigned int e2p_base ) {
  189. struct net_device *netdev = smscusb->netdev;
  190. int rc;
  191. /* Read MAC address from EEPROM */
  192. if ( ( rc = smscusb_eeprom_read ( smscusb, e2p_base, SMSCUSB_EEPROM_MAC,
  193. netdev->hw_addr, ETH_ALEN ) ) != 0 )
  194. return rc;
  195. /* Check that EEPROM is physically present */
  196. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  197. DBGC ( smscusb, "SMSCUSB %p has no EEPROM MAC (%s)\n",
  198. smscusb, eth_ntoa ( netdev->hw_addr ) );
  199. return -ENODEV;
  200. }
  201. DBGC ( smscusb, "SMSCUSB %p using EEPROM MAC %s\n",
  202. smscusb, eth_ntoa ( netdev->hw_addr ) );
  203. return 0;
  204. }
  205. /******************************************************************************
  206. *
  207. * OTP access
  208. *
  209. ******************************************************************************
  210. */
  211. /**
  212. * Power up OTP
  213. *
  214. * @v smscusb SMSC USB device
  215. * @v otp_base OTP register base
  216. * @ret rc Return status code
  217. */
  218. static int smscusb_otp_power_up ( struct smscusb_device *smscusb,
  219. unsigned int otp_base ) {
  220. uint32_t otp_power;
  221. unsigned int i;
  222. int rc;
  223. /* Power up OTP */
  224. if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_POWER ),
  225. 0 ) ) != 0 )
  226. return rc;
  227. /* Wait for OTP_POWER_DOWN to become clear */
  228. for ( i = 0 ; i < SMSCUSB_OTP_MAX_WAIT_MS ; i++ ) {
  229. /* Read OTP_POWER and check OTP_POWER_DOWN */
  230. if ( ( rc = smscusb_readl ( smscusb,
  231. ( otp_base + SMSCUSB_OTP_POWER ),
  232. &otp_power ) ) != 0 )
  233. return rc;
  234. if ( ! ( otp_power & SMSCUSB_OTP_POWER_DOWN ) )
  235. return 0;
  236. /* Delay */
  237. mdelay ( 1 );
  238. }
  239. DBGC ( smscusb, "SMSCUSB %p timed out waiting for OTP power up\n",
  240. smscusb );
  241. return -ETIMEDOUT;
  242. }
  243. /**
  244. * Wait for OTP to become idle
  245. *
  246. * @v smscusb SMSC USB device
  247. * @v otp_base OTP register base
  248. * @ret rc Return status code
  249. */
  250. static int smscusb_otp_wait ( struct smscusb_device *smscusb,
  251. unsigned int otp_base ) {
  252. uint32_t otp_status;
  253. unsigned int i;
  254. int rc;
  255. /* Wait for OTP_STATUS_BUSY to become clear */
  256. for ( i = 0 ; i < SMSCUSB_OTP_MAX_WAIT_MS ; i++ ) {
  257. /* Read OTP_STATUS and check OTP_STATUS_BUSY */
  258. if ( ( rc = smscusb_readl ( smscusb,
  259. ( otp_base + SMSCUSB_OTP_STATUS ),
  260. &otp_status ) ) != 0 )
  261. return rc;
  262. if ( ! ( otp_status & SMSCUSB_OTP_STATUS_BUSY ) )
  263. return 0;
  264. /* Delay */
  265. mdelay ( 1 );
  266. }
  267. DBGC ( smscusb, "SMSCUSB %p timed out waiting for OTP\n",
  268. smscusb );
  269. return -ETIMEDOUT;
  270. }
  271. /**
  272. * Read byte from OTP
  273. *
  274. * @v smscusb SMSC USB device
  275. * @v otp_base OTP register base
  276. * @v address OTP address
  277. * @ret byte Byte read, or negative error
  278. */
  279. static int smscusb_otp_read_byte ( struct smscusb_device *smscusb,
  280. unsigned int otp_base,
  281. unsigned int address ) {
  282. uint8_t addrh = ( address >> 8 );
  283. uint8_t addrl = ( address >> 0 );
  284. uint32_t otp_data;
  285. int rc;
  286. /* Wait for OTP to become idle */
  287. if ( ( rc = smscusb_otp_wait ( smscusb, otp_base ) ) != 0 )
  288. return rc;
  289. /* Initiate read command */
  290. if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_ADDRH ),
  291. addrh ) ) != 0 )
  292. return rc;
  293. if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_ADDRL ),
  294. addrl ) ) != 0 )
  295. return rc;
  296. if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_CMD ),
  297. SMSCUSB_OTP_CMD_READ ) ) != 0 )
  298. return rc;
  299. if ( ( rc = smscusb_writel ( smscusb, ( otp_base + SMSCUSB_OTP_GO ),
  300. SMSCUSB_OTP_GO_GO ) ) != 0 )
  301. return rc;
  302. /* Wait for command to complete */
  303. if ( ( rc = smscusb_otp_wait ( smscusb, otp_base ) ) != 0 )
  304. return rc;
  305. /* Read OTP data */
  306. if ( ( rc = smscusb_readl ( smscusb, ( otp_base + SMSCUSB_OTP_DATA ),
  307. &otp_data ) ) != 0 )
  308. return rc;
  309. return SMSCUSB_OTP_DATA_GET ( otp_data );
  310. }
  311. /**
  312. * Read data from OTP
  313. *
  314. * @v smscusb SMSC USB device
  315. * @v otp_base OTP register base
  316. * @v address OTP address
  317. * @v data Data buffer
  318. * @v len Length of data
  319. * @ret rc Return status code
  320. */
  321. static int smscusb_otp_read ( struct smscusb_device *smscusb,
  322. unsigned int otp_base, unsigned int address,
  323. void *data, size_t len ) {
  324. uint8_t *bytes;
  325. int byte;
  326. int rc;
  327. /* Power up OTP */
  328. if ( ( rc = smscusb_otp_power_up ( smscusb, otp_base ) ) != 0 )
  329. return rc;
  330. /* Read bytes */
  331. for ( bytes = data ; len-- ; address++, bytes++ ) {
  332. byte = smscusb_otp_read_byte ( smscusb, otp_base, address );
  333. if ( byte < 0 )
  334. return byte;
  335. *bytes = byte;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * Fetch MAC address from OTP
  341. *
  342. * @v smscusb SMSC USB device
  343. * @v otp_base OTP register base
  344. * @ret rc Return status code
  345. */
  346. int smscusb_otp_fetch_mac ( struct smscusb_device *smscusb,
  347. unsigned int otp_base ) {
  348. struct net_device *netdev = smscusb->netdev;
  349. uint8_t signature;
  350. unsigned int address;
  351. int rc;
  352. /* Read OTP signature byte */
  353. if ( ( rc = smscusb_otp_read ( smscusb, otp_base, 0, &signature,
  354. sizeof ( signature ) ) ) != 0 )
  355. return rc;
  356. /* Determine location of MAC address */
  357. switch ( signature ) {
  358. case SMSCUSB_OTP_1_SIG:
  359. address = SMSCUSB_OTP_1_MAC;
  360. break;
  361. case SMSCUSB_OTP_2_SIG:
  362. address = SMSCUSB_OTP_2_MAC;
  363. break;
  364. default:
  365. DBGC ( smscusb, "SMSCUSB %p unknown OTP signature %#02x\n",
  366. smscusb, signature );
  367. return -ENOTSUP;
  368. }
  369. /* Read MAC address from OTP */
  370. if ( ( rc = smscusb_otp_read ( smscusb, otp_base, address,
  371. netdev->hw_addr, ETH_ALEN ) ) != 0 )
  372. return rc;
  373. /* Check that OTP is valid */
  374. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  375. DBGC ( smscusb, "SMSCUSB %p has no layout %#02x OTP MAC (%s)\n",
  376. smscusb, signature, eth_ntoa ( netdev->hw_addr ) );
  377. return -ENODEV;
  378. }
  379. DBGC ( smscusb, "SMSCUSB %p using layout %#02x OTP MAC %s\n",
  380. smscusb, signature, eth_ntoa ( netdev->hw_addr ) );
  381. return 0;
  382. }
  383. /******************************************************************************
  384. *
  385. * MII access
  386. *
  387. ******************************************************************************
  388. */
  389. /**
  390. * Wait for MII to become idle
  391. *
  392. * @v smscusb SMSC USB device
  393. * @ret rc Return status code
  394. */
  395. static int smscusb_mii_wait ( struct smscusb_device *smscusb ) {
  396. unsigned int base = smscusb->mii_base;
  397. uint32_t mii_access;
  398. unsigned int i;
  399. int rc;
  400. /* Wait for MIIBZY to become clear */
  401. for ( i = 0 ; i < SMSCUSB_MII_MAX_WAIT_MS ; i++ ) {
  402. /* Read MII_ACCESS and check MIIBZY */
  403. if ( ( rc = smscusb_readl ( smscusb,
  404. ( base + SMSCUSB_MII_ACCESS ),
  405. &mii_access ) ) != 0 )
  406. return rc;
  407. if ( ! ( mii_access & SMSCUSB_MII_ACCESS_MIIBZY ) )
  408. return 0;
  409. /* Delay */
  410. mdelay ( 1 );
  411. }
  412. DBGC ( smscusb, "SMSCUSB %p timed out waiting for MII\n",
  413. smscusb );
  414. return -ETIMEDOUT;
  415. }
  416. /**
  417. * Read from MII register
  418. *
  419. * @v mii MII interface
  420. * @v reg Register address
  421. * @ret value Data read, or negative error
  422. */
  423. static int smscusb_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  424. struct smscusb_device *smscusb =
  425. container_of ( mii, struct smscusb_device, mii );
  426. unsigned int base = smscusb->mii_base;
  427. uint32_t mii_access;
  428. uint32_t mii_data;
  429. int rc;
  430. /* Wait for MII to become idle */
  431. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  432. return rc;
  433. /* Initiate read command */
  434. mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
  435. SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
  436. SMSCUSB_MII_ACCESS_MIIBZY );
  437. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
  438. mii_access ) ) != 0 )
  439. return rc;
  440. /* Wait for command to complete */
  441. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  442. return rc;
  443. /* Read MII data */
  444. if ( ( rc = smscusb_readl ( smscusb, ( base + SMSCUSB_MII_DATA ),
  445. &mii_data ) ) != 0 )
  446. return rc;
  447. return SMSCUSB_MII_DATA_GET ( mii_data );
  448. }
  449. /**
  450. * Write to MII register
  451. *
  452. * @v mii MII interface
  453. * @v reg Register address
  454. * @v data Data to write
  455. * @ret rc Return status code
  456. */
  457. static int smscusb_mii_write ( struct mii_interface *mii, unsigned int reg,
  458. unsigned int data ) {
  459. struct smscusb_device *smscusb =
  460. container_of ( mii, struct smscusb_device, mii );
  461. unsigned int base = smscusb->mii_base;
  462. uint32_t mii_access;
  463. uint32_t mii_data;
  464. int rc;
  465. /* Wait for MII to become idle */
  466. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  467. return rc;
  468. /* Write MII data */
  469. mii_data = SMSCUSB_MII_DATA_SET ( data );
  470. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_DATA ),
  471. mii_data ) ) != 0 )
  472. return rc;
  473. /* Initiate write command */
  474. mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
  475. SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
  476. SMSCUSB_MII_ACCESS_MIIWNR |
  477. SMSCUSB_MII_ACCESS_MIIBZY );
  478. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
  479. mii_access ) ) != 0 )
  480. return rc;
  481. /* Wait for command to complete */
  482. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  483. return rc;
  484. return 0;
  485. }
  486. /** MII operations */
  487. struct mii_operations smscusb_mii_operations = {
  488. .read = smscusb_mii_read,
  489. .write = smscusb_mii_write,
  490. };
  491. /**
  492. * Check link status
  493. *
  494. * @v smscusb SMSC USB device
  495. * @ret rc Return status code
  496. */
  497. int smscusb_mii_check_link ( struct smscusb_device *smscusb ) {
  498. struct net_device *netdev = smscusb->netdev;
  499. int intr;
  500. int rc;
  501. /* Read PHY interrupt source */
  502. intr = mii_read ( &smscusb->mii, smscusb->phy_source );
  503. if ( intr < 0 ) {
  504. rc = intr;
  505. DBGC ( smscusb, "SMSCUSB %p could not get PHY interrupt "
  506. "source: %s\n", smscusb, strerror ( rc ) );
  507. return rc;
  508. }
  509. /* Acknowledge PHY interrupt */
  510. if ( ( rc = mii_write ( &smscusb->mii, smscusb->phy_source,
  511. intr ) ) != 0 ) {
  512. DBGC ( smscusb, "SMSCUSB %p could not acknowledge PHY "
  513. "interrupt: %s\n", smscusb, strerror ( rc ) );
  514. return rc;
  515. }
  516. /* Check link status */
  517. if ( ( rc = mii_check_link ( &smscusb->mii, netdev ) ) != 0 ) {
  518. DBGC ( smscusb, "SMSCUSB %p could not check link: %s\n",
  519. smscusb, strerror ( rc ) );
  520. return rc;
  521. }
  522. DBGC ( smscusb, "SMSCUSB %p link %s (intr %#04x)\n",
  523. smscusb, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
  524. return 0;
  525. }
  526. /**
  527. * Enable PHY interrupts and update link status
  528. *
  529. * @v smscusb SMSC USB device
  530. * @v phy_mask PHY interrupt mask register
  531. * @v intrs PHY interrupts to enable
  532. * @ret rc Return status code
  533. */
  534. int smscusb_mii_open ( struct smscusb_device *smscusb,
  535. unsigned int phy_mask, unsigned int intrs ) {
  536. int rc;
  537. /* Enable PHY interrupts */
  538. if ( ( rc = mii_write ( &smscusb->mii, phy_mask, intrs ) ) != 0 ) {
  539. DBGC ( smscusb, "SMSCUSB %p could not set PHY interrupt "
  540. "mask: %s\n", smscusb, strerror ( rc ) );
  541. return rc;
  542. }
  543. /* Update link status */
  544. smscusb_mii_check_link ( smscusb );
  545. return 0;
  546. }
  547. /******************************************************************************
  548. *
  549. * Receive filtering
  550. *
  551. ******************************************************************************
  552. */
  553. /**
  554. * Set receive address
  555. *
  556. * @v smscusb SMSC USB device
  557. * @v addr_base Receive address register base
  558. * @ret rc Return status code
  559. */
  560. int smscusb_set_address ( struct smscusb_device *smscusb,
  561. unsigned int addr_base ) {
  562. struct net_device *netdev = smscusb->netdev;
  563. union smscusb_mac mac;
  564. int rc;
  565. /* Copy MAC address */
  566. memset ( &mac, 0, sizeof ( mac ) );
  567. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  568. /* Write MAC address high register */
  569. if ( ( rc = smscusb_raw_writel ( smscusb,
  570. ( addr_base + SMSCUSB_RX_ADDRH ),
  571. mac.addr.h ) ) != 0 )
  572. return rc;
  573. /* Write MAC address low register */
  574. if ( ( rc = smscusb_raw_writel ( smscusb,
  575. ( addr_base + SMSCUSB_RX_ADDRL ),
  576. mac.addr.l ) ) != 0 )
  577. return rc;
  578. return 0;
  579. }
  580. /**
  581. * Set receive filter
  582. *
  583. * @v smscusb SMSC USB device
  584. * @v filt_base Receive filter register base
  585. * @ret rc Return status code
  586. */
  587. int smscusb_set_filter ( struct smscusb_device *smscusb,
  588. unsigned int filt_base ) {
  589. struct net_device *netdev = smscusb->netdev;
  590. union smscusb_mac mac;
  591. int rc;
  592. /* Copy MAC address */
  593. memset ( &mac, 0, sizeof ( mac ) );
  594. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  595. mac.addr.h |= cpu_to_le32 ( SMSCUSB_ADDR_FILTH_VALID );
  596. /* Write MAC address perfect filter high register */
  597. if ( ( rc = smscusb_raw_writel ( smscusb,
  598. ( filt_base + SMSCUSB_ADDR_FILTH(0) ),
  599. mac.addr.h ) ) != 0 )
  600. return rc;
  601. /* Write MAC address perfect filter low register */
  602. if ( ( rc = smscusb_raw_writel ( smscusb,
  603. ( filt_base + SMSCUSB_ADDR_FILTL(0) ),
  604. mac.addr.l ) ) != 0 )
  605. return rc;
  606. return 0;
  607. }
  608. /******************************************************************************
  609. *
  610. * Endpoint operations
  611. *
  612. ******************************************************************************
  613. */
  614. /**
  615. * Complete interrupt transfer
  616. *
  617. * @v ep USB endpoint
  618. * @v iobuf I/O buffer
  619. * @v rc Completion status code
  620. */
  621. static void smscusb_intr_complete ( struct usb_endpoint *ep,
  622. struct io_buffer *iobuf, int rc ) {
  623. struct smscusb_device *smscusb =
  624. container_of ( ep, struct smscusb_device, usbnet.intr );
  625. struct net_device *netdev = smscusb->netdev;
  626. struct smscusb_interrupt *intr;
  627. /* Profile completions */
  628. profile_start ( &smscusb_intr_profiler );
  629. /* Ignore packets cancelled when the endpoint closes */
  630. if ( ! ep->open )
  631. goto done;
  632. /* Record USB errors against the network device */
  633. if ( rc != 0 ) {
  634. DBGC ( smscusb, "SMSCUSB %p interrupt failed: %s\n",
  635. smscusb, strerror ( rc ) );
  636. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  637. netdev_rx_err ( netdev, NULL, rc );
  638. goto done;
  639. }
  640. /* Extract interrupt data */
  641. if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
  642. DBGC ( smscusb, "SMSCUSB %p malformed interrupt\n",
  643. smscusb );
  644. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  645. netdev_rx_err ( netdev, NULL, rc );
  646. goto done;
  647. }
  648. intr = iobuf->data;
  649. /* Record interrupt status */
  650. smscusb->int_sts = le32_to_cpu ( intr->int_sts );
  651. profile_stop ( &smscusb_intr_profiler );
  652. done:
  653. /* Free I/O buffer */
  654. free_iob ( iobuf );
  655. }
  656. /** Interrupt endpoint operations */
  657. struct usb_endpoint_driver_operations smscusb_intr_operations = {
  658. .complete = smscusb_intr_complete,
  659. };
  660. /**
  661. * Complete bulk OUT transfer
  662. *
  663. * @v ep USB endpoint
  664. * @v iobuf I/O buffer
  665. * @v rc Completion status code
  666. */
  667. static void smscusb_out_complete ( struct usb_endpoint *ep,
  668. struct io_buffer *iobuf, int rc ) {
  669. struct smscusb_device *smscusb =
  670. container_of ( ep, struct smscusb_device, usbnet.out );
  671. struct net_device *netdev = smscusb->netdev;
  672. /* Report TX completion */
  673. netdev_tx_complete_err ( netdev, iobuf, rc );
  674. }
  675. /** Bulk OUT endpoint operations */
  676. struct usb_endpoint_driver_operations smscusb_out_operations = {
  677. .complete = smscusb_out_complete,
  678. };