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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 mdio MII interface
  420. * @v phy PHY address
  421. * @v reg Register address
  422. * @ret value Data read, or negative error
  423. */
  424. static int smscusb_mii_read ( struct mii_interface *mdio,
  425. unsigned int phy __unused, unsigned int reg ) {
  426. struct smscusb_device *smscusb =
  427. container_of ( mdio, struct smscusb_device, mdio );
  428. unsigned int base = smscusb->mii_base;
  429. uint32_t mii_access;
  430. uint32_t mii_data;
  431. int rc;
  432. /* Wait for MII to become idle */
  433. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  434. return rc;
  435. /* Initiate read command */
  436. mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
  437. SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
  438. SMSCUSB_MII_ACCESS_MIIBZY );
  439. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
  440. mii_access ) ) != 0 )
  441. return rc;
  442. /* Wait for command to complete */
  443. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  444. return rc;
  445. /* Read MII data */
  446. if ( ( rc = smscusb_readl ( smscusb, ( base + SMSCUSB_MII_DATA ),
  447. &mii_data ) ) != 0 )
  448. return rc;
  449. return SMSCUSB_MII_DATA_GET ( mii_data );
  450. }
  451. /**
  452. * Write to MII register
  453. *
  454. * @v mdio MII interface
  455. * @v phy PHY address
  456. * @v reg Register address
  457. * @v data Data to write
  458. * @ret rc Return status code
  459. */
  460. static int smscusb_mii_write ( struct mii_interface *mdio,
  461. unsigned int phy __unused, unsigned int reg,
  462. unsigned int data ) {
  463. struct smscusb_device *smscusb =
  464. container_of ( mdio, struct smscusb_device, mdio );
  465. unsigned int base = smscusb->mii_base;
  466. uint32_t mii_access;
  467. uint32_t mii_data;
  468. int rc;
  469. /* Wait for MII to become idle */
  470. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  471. return rc;
  472. /* Write MII data */
  473. mii_data = SMSCUSB_MII_DATA_SET ( data );
  474. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_DATA ),
  475. mii_data ) ) != 0 )
  476. return rc;
  477. /* Initiate write command */
  478. mii_access = ( SMSCUSB_MII_ACCESS_PHY_ADDRESS |
  479. SMSCUSB_MII_ACCESS_MIIRINDA ( reg ) |
  480. SMSCUSB_MII_ACCESS_MIIWNR |
  481. SMSCUSB_MII_ACCESS_MIIBZY );
  482. if ( ( rc = smscusb_writel ( smscusb, ( base + SMSCUSB_MII_ACCESS ),
  483. mii_access ) ) != 0 )
  484. return rc;
  485. /* Wait for command to complete */
  486. if ( ( rc = smscusb_mii_wait ( smscusb ) ) != 0 )
  487. return rc;
  488. return 0;
  489. }
  490. /** MII operations */
  491. struct mii_operations smscusb_mii_operations = {
  492. .read = smscusb_mii_read,
  493. .write = smscusb_mii_write,
  494. };
  495. /**
  496. * Check link status
  497. *
  498. * @v smscusb SMSC USB device
  499. * @ret rc Return status code
  500. */
  501. int smscusb_mii_check_link ( struct smscusb_device *smscusb ) {
  502. struct net_device *netdev = smscusb->netdev;
  503. int intr;
  504. int rc;
  505. /* Read PHY interrupt source */
  506. intr = mii_read ( &smscusb->mii, smscusb->phy_source );
  507. if ( intr < 0 ) {
  508. rc = intr;
  509. DBGC ( smscusb, "SMSCUSB %p could not get PHY interrupt "
  510. "source: %s\n", smscusb, strerror ( rc ) );
  511. return rc;
  512. }
  513. /* Acknowledge PHY interrupt */
  514. if ( ( rc = mii_write ( &smscusb->mii, smscusb->phy_source,
  515. intr ) ) != 0 ) {
  516. DBGC ( smscusb, "SMSCUSB %p could not acknowledge PHY "
  517. "interrupt: %s\n", smscusb, strerror ( rc ) );
  518. return rc;
  519. }
  520. /* Check link status */
  521. if ( ( rc = mii_check_link ( &smscusb->mii, netdev ) ) != 0 ) {
  522. DBGC ( smscusb, "SMSCUSB %p could not check link: %s\n",
  523. smscusb, strerror ( rc ) );
  524. return rc;
  525. }
  526. DBGC ( smscusb, "SMSCUSB %p link %s (intr %#04x)\n",
  527. smscusb, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
  528. return 0;
  529. }
  530. /**
  531. * Enable PHY interrupts and update link status
  532. *
  533. * @v smscusb SMSC USB device
  534. * @v phy_mask PHY interrupt mask register
  535. * @v intrs PHY interrupts to enable
  536. * @ret rc Return status code
  537. */
  538. int smscusb_mii_open ( struct smscusb_device *smscusb,
  539. unsigned int phy_mask, unsigned int intrs ) {
  540. int rc;
  541. /* Enable PHY interrupts */
  542. if ( ( rc = mii_write ( &smscusb->mii, phy_mask, intrs ) ) != 0 ) {
  543. DBGC ( smscusb, "SMSCUSB %p could not set PHY interrupt "
  544. "mask: %s\n", smscusb, strerror ( rc ) );
  545. return rc;
  546. }
  547. /* Update link status */
  548. smscusb_mii_check_link ( smscusb );
  549. return 0;
  550. }
  551. /******************************************************************************
  552. *
  553. * Receive filtering
  554. *
  555. ******************************************************************************
  556. */
  557. /**
  558. * Set receive address
  559. *
  560. * @v smscusb SMSC USB device
  561. * @v addr_base Receive address register base
  562. * @ret rc Return status code
  563. */
  564. int smscusb_set_address ( struct smscusb_device *smscusb,
  565. unsigned int addr_base ) {
  566. struct net_device *netdev = smscusb->netdev;
  567. union smscusb_mac mac;
  568. int rc;
  569. /* Copy MAC address */
  570. memset ( &mac, 0, sizeof ( mac ) );
  571. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  572. /* Write MAC address high register */
  573. if ( ( rc = smscusb_raw_writel ( smscusb,
  574. ( addr_base + SMSCUSB_RX_ADDRH ),
  575. mac.addr.h ) ) != 0 )
  576. return rc;
  577. /* Write MAC address low register */
  578. if ( ( rc = smscusb_raw_writel ( smscusb,
  579. ( addr_base + SMSCUSB_RX_ADDRL ),
  580. mac.addr.l ) ) != 0 )
  581. return rc;
  582. return 0;
  583. }
  584. /**
  585. * Set receive filter
  586. *
  587. * @v smscusb SMSC USB device
  588. * @v filt_base Receive filter register base
  589. * @ret rc Return status code
  590. */
  591. int smscusb_set_filter ( struct smscusb_device *smscusb,
  592. unsigned int filt_base ) {
  593. struct net_device *netdev = smscusb->netdev;
  594. union smscusb_mac mac;
  595. int rc;
  596. /* Copy MAC address */
  597. memset ( &mac, 0, sizeof ( mac ) );
  598. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  599. mac.addr.h |= cpu_to_le32 ( SMSCUSB_ADDR_FILTH_VALID );
  600. /* Write MAC address perfect filter high register */
  601. if ( ( rc = smscusb_raw_writel ( smscusb,
  602. ( filt_base + SMSCUSB_ADDR_FILTH(0) ),
  603. mac.addr.h ) ) != 0 )
  604. return rc;
  605. /* Write MAC address perfect filter low register */
  606. if ( ( rc = smscusb_raw_writel ( smscusb,
  607. ( filt_base + SMSCUSB_ADDR_FILTL(0) ),
  608. mac.addr.l ) ) != 0 )
  609. return rc;
  610. return 0;
  611. }
  612. /******************************************************************************
  613. *
  614. * Endpoint operations
  615. *
  616. ******************************************************************************
  617. */
  618. /**
  619. * Complete interrupt transfer
  620. *
  621. * @v ep USB endpoint
  622. * @v iobuf I/O buffer
  623. * @v rc Completion status code
  624. */
  625. static void smscusb_intr_complete ( struct usb_endpoint *ep,
  626. struct io_buffer *iobuf, int rc ) {
  627. struct smscusb_device *smscusb =
  628. container_of ( ep, struct smscusb_device, usbnet.intr );
  629. struct net_device *netdev = smscusb->netdev;
  630. struct smscusb_interrupt *intr;
  631. /* Profile completions */
  632. profile_start ( &smscusb_intr_profiler );
  633. /* Ignore packets cancelled when the endpoint closes */
  634. if ( ! ep->open )
  635. goto done;
  636. /* Record USB errors against the network device */
  637. if ( rc != 0 ) {
  638. DBGC ( smscusb, "SMSCUSB %p interrupt failed: %s\n",
  639. smscusb, strerror ( rc ) );
  640. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  641. netdev_rx_err ( netdev, NULL, rc );
  642. goto done;
  643. }
  644. /* Extract interrupt data */
  645. if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
  646. DBGC ( smscusb, "SMSCUSB %p malformed interrupt\n",
  647. smscusb );
  648. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  649. netdev_rx_err ( netdev, NULL, rc );
  650. goto done;
  651. }
  652. intr = iobuf->data;
  653. /* Record interrupt status */
  654. smscusb->int_sts = le32_to_cpu ( intr->int_sts );
  655. profile_stop ( &smscusb_intr_profiler );
  656. done:
  657. /* Free I/O buffer */
  658. free_iob ( iobuf );
  659. }
  660. /** Interrupt endpoint operations */
  661. struct usb_endpoint_driver_operations smscusb_intr_operations = {
  662. .complete = smscusb_intr_complete,
  663. };
  664. /**
  665. * Complete bulk OUT transfer
  666. *
  667. * @v ep USB endpoint
  668. * @v iobuf I/O buffer
  669. * @v rc Completion status code
  670. */
  671. static void smscusb_out_complete ( struct usb_endpoint *ep,
  672. struct io_buffer *iobuf, int rc ) {
  673. struct smscusb_device *smscusb =
  674. container_of ( ep, struct smscusb_device, usbnet.out );
  675. struct net_device *netdev = smscusb->netdev;
  676. /* Report TX completion */
  677. netdev_tx_complete_err ( netdev, iobuf, rc );
  678. }
  679. /** Bulk OUT endpoint operations */
  680. struct usb_endpoint_driver_operations smscusb_out_operations = {
  681. .complete = smscusb_out_complete,
  682. };