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 21KB

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