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.

realtek.c 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * (EEPROM code originally implemented for rtl8139.c)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. FILE_LICENCE ( GPL2_OR_LATER );
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/malloc.h>
  32. #include <ipxe/pci.h>
  33. #include <ipxe/nvs.h>
  34. #include <ipxe/threewire.h>
  35. #include <ipxe/bitbash.h>
  36. #include <ipxe/mii.h>
  37. #include "realtek.h"
  38. /** @file
  39. *
  40. * Realtek 10/100/1000 network card driver
  41. *
  42. * Based on the following datasheets:
  43. *
  44. * http://www.datasheetarchive.com/dl/Datasheets-8/DSA-153536.pdf
  45. * http://www.datasheetarchive.com/indexdl/Datasheet-028/DSA00494723.pdf
  46. */
  47. /******************************************************************************
  48. *
  49. * EEPROM interface
  50. *
  51. ******************************************************************************
  52. */
  53. /** Pin mapping for SPI bit-bashing interface */
  54. static const uint8_t realtek_eeprom_bits[] = {
  55. [SPI_BIT_SCLK] = RTL_9346CR_EESK,
  56. [SPI_BIT_MOSI] = RTL_9346CR_EEDI,
  57. [SPI_BIT_MISO] = RTL_9346CR_EEDO,
  58. [SPI_BIT_SS(0)] = RTL_9346CR_EECS,
  59. };
  60. /**
  61. * Open bit-bashing interface
  62. *
  63. * @v basher Bit-bashing interface
  64. */
  65. static void realtek_spi_open_bit ( struct bit_basher *basher ) {
  66. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  67. spibit.basher );
  68. /* Enable EEPROM access */
  69. writeb ( RTL_9346CR_EEM_EEPROM, rtl->regs + RTL_9346CR );
  70. }
  71. /**
  72. * Close bit-bashing interface
  73. *
  74. * @v basher Bit-bashing interface
  75. */
  76. static void realtek_spi_close_bit ( struct bit_basher *basher ) {
  77. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  78. spibit.basher );
  79. /* Disable EEPROM access */
  80. writeb ( RTL_9346CR_EEM_NORMAL, rtl->regs + RTL_9346CR );
  81. }
  82. /**
  83. * Read input bit
  84. *
  85. * @v basher Bit-bashing interface
  86. * @v bit_id Bit number
  87. * @ret zero Input is a logic 0
  88. * @ret non-zero Input is a logic 1
  89. */
  90. static int realtek_spi_read_bit ( struct bit_basher *basher,
  91. unsigned int bit_id ) {
  92. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  93. spibit.basher );
  94. uint8_t mask = realtek_eeprom_bits[bit_id];
  95. uint8_t reg;
  96. DBG_DISABLE ( DBGLVL_IO );
  97. reg = readb ( rtl->regs + RTL_9346CR );
  98. DBG_ENABLE ( DBGLVL_IO );
  99. return ( reg & mask );
  100. }
  101. /**
  102. * Set/clear output bit
  103. *
  104. * @v basher Bit-bashing interface
  105. * @v bit_id Bit number
  106. * @v data Value to write
  107. */
  108. static void realtek_spi_write_bit ( struct bit_basher *basher,
  109. unsigned int bit_id, unsigned long data ) {
  110. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  111. spibit.basher );
  112. uint8_t mask = realtek_eeprom_bits[bit_id];
  113. uint8_t reg;
  114. DBG_DISABLE ( DBGLVL_IO );
  115. reg = readb ( rtl->regs + RTL_9346CR );
  116. reg &= ~mask;
  117. reg |= ( data & mask );
  118. writeb ( reg, rtl->regs + RTL_9346CR );
  119. DBG_ENABLE ( DBGLVL_IO );
  120. }
  121. /** SPI bit-bashing interface */
  122. static struct bit_basher_operations realtek_basher_ops = {
  123. .open = realtek_spi_open_bit,
  124. .close = realtek_spi_close_bit,
  125. .read = realtek_spi_read_bit,
  126. .write = realtek_spi_write_bit,
  127. };
  128. /**
  129. * Initialise EEPROM
  130. *
  131. * @v netdev Network device
  132. */
  133. static void realtek_init_eeprom ( struct net_device *netdev ) {
  134. struct realtek_nic *rtl = netdev->priv;
  135. /* Initialise SPI bit-bashing interface */
  136. rtl->spibit.basher.op = &realtek_basher_ops;
  137. rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
  138. init_spi_bit_basher ( &rtl->spibit );
  139. /* Detect EEPROM type and initialise three-wire device */
  140. if ( readl ( rtl->regs + RTL_RCR ) & RTL_RCR_9356SEL ) {
  141. DBGC ( rtl, "REALTEK %p EEPROM is a 93C56\n", rtl );
  142. init_at93c56 ( &rtl->eeprom, 16 );
  143. } else {
  144. DBGC ( rtl, "REALTEK %p EEPROM is a 93C46\n", rtl );
  145. init_at93c46 ( &rtl->eeprom, 16 );
  146. }
  147. rtl->eeprom.bus = &rtl->spibit.bus;
  148. /* Initialise space for non-volatile options, if available
  149. *
  150. * We use offset 0x40 (i.e. address 0x20), length 0x40. This
  151. * block is marked as VPD in the Realtek datasheets, so we use
  152. * it only if we detect that the card is not supporting VPD.
  153. */
  154. if ( readb ( rtl->regs + RTL_CONFIG1 ) & RTL_CONFIG1_VPD ) {
  155. DBGC ( rtl, "REALTEK %p EEPROM in use for VPD; cannot use "
  156. "for options\n", rtl );
  157. } else {
  158. nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, RTL_EEPROM_VPD,
  159. RTL_EEPROM_VPD_LEN, NULL, &netdev->refcnt );
  160. }
  161. }
  162. /******************************************************************************
  163. *
  164. * MII interface
  165. *
  166. ******************************************************************************
  167. */
  168. /**
  169. * Read from MII register
  170. *
  171. * @v mii MII interface
  172. * @v reg Register address
  173. * @ret value Data read, or negative error
  174. */
  175. static int realtek_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  176. struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
  177. unsigned int i;
  178. uint32_t value;
  179. /* Fail if PHYAR register is not present */
  180. if ( ! rtl->have_phy_regs )
  181. return -ENOTSUP;
  182. /* Initiate read */
  183. writel ( RTL_PHYAR_VALUE ( 0, reg, 0 ), rtl->regs + RTL_PHYAR );
  184. /* Wait for read to complete */
  185. for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
  186. /* If read is not complete, delay 1us and retry */
  187. value = readl ( rtl->regs + RTL_PHYAR );
  188. if ( ! ( value & RTL_PHYAR_FLAG ) ) {
  189. udelay ( 1 );
  190. continue;
  191. }
  192. /* Return register value */
  193. return ( RTL_PHYAR_DATA ( value ) );
  194. }
  195. DBGC ( rtl, "REALTEK %p timed out waiting for MII read\n", rtl );
  196. return -ETIMEDOUT;
  197. }
  198. /**
  199. * Write to MII register
  200. *
  201. * @v mii MII interface
  202. * @v reg Register address
  203. * @v data Data to write
  204. * @ret rc Return status code
  205. */
  206. static int realtek_mii_write ( struct mii_interface *mii, unsigned int reg,
  207. unsigned int data) {
  208. struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
  209. unsigned int i;
  210. /* Fail if PHYAR register is not present */
  211. if ( ! rtl->have_phy_regs )
  212. return -ENOTSUP;
  213. /* Initiate write */
  214. writel ( RTL_PHYAR_VALUE ( RTL_PHYAR_FLAG, reg, data ),
  215. rtl->regs + RTL_PHYAR );
  216. /* Wait for write to complete */
  217. for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
  218. /* If write is not complete, delay 1us and retry */
  219. if ( readl ( rtl->regs + RTL_PHYAR ) & RTL_PHYAR_FLAG ) {
  220. udelay ( 1 );
  221. continue;
  222. }
  223. return 0;
  224. }
  225. DBGC ( rtl, "REALTEK %p timed out waiting for MII write\n", rtl );
  226. return -ETIMEDOUT;
  227. }
  228. /** Realtek MII operations */
  229. static struct mii_operations realtek_mii_operations = {
  230. .read = realtek_mii_read,
  231. .write = realtek_mii_write,
  232. };
  233. /******************************************************************************
  234. *
  235. * Device reset
  236. *
  237. ******************************************************************************
  238. */
  239. /**
  240. * Reset hardware
  241. *
  242. * @v rtl Realtek device
  243. * @ret rc Return status code
  244. */
  245. static int realtek_reset ( struct realtek_nic *rtl ) {
  246. unsigned int i;
  247. /* Issue reset */
  248. writeb ( RTL_CR_RST, rtl->regs + RTL_CR );
  249. /* Wait for reset to complete */
  250. for ( i = 0 ; i < RTL_RESET_MAX_WAIT_MS ; i++ ) {
  251. /* If reset is not complete, delay 1ms and retry */
  252. if ( readb ( rtl->regs + RTL_CR ) & RTL_CR_RST ) {
  253. mdelay ( 1 );
  254. continue;
  255. }
  256. return 0;
  257. }
  258. DBGC ( rtl, "REALTEK %p timed out waiting for reset\n", rtl );
  259. return -ETIMEDOUT;
  260. }
  261. /**
  262. * Configure PHY for Gigabit operation
  263. *
  264. * @v rtl Realtek device
  265. * @ret rc Return status code
  266. */
  267. static int realtek_phy_speed ( struct realtek_nic *rtl ) {
  268. int ctrl1000;
  269. int rc;
  270. /* Read CTRL1000 register */
  271. ctrl1000 = mii_read ( &rtl->mii, MII_CTRL1000 );
  272. if ( ctrl1000 < 0 ) {
  273. rc = ctrl1000;
  274. DBGC ( rtl, "REALTEK %p could not read CTRL1000: %s\n",
  275. rtl, strerror ( rc ) );
  276. return rc;
  277. }
  278. /* Advertise 1000Mbps speeds */
  279. ctrl1000 |= ( ADVERTISE_1000FULL | ADVERTISE_1000HALF );
  280. if ( ( rc = mii_write ( &rtl->mii, MII_CTRL1000, ctrl1000 ) ) != 0 ) {
  281. DBGC ( rtl, "REALTEK %p could not write CTRL1000: %s\n",
  282. rtl, strerror ( rc ) );
  283. return rc;
  284. }
  285. return 0;
  286. }
  287. /**
  288. * Reset PHY
  289. *
  290. * @v rtl Realtek device
  291. * @ret rc Return status code
  292. */
  293. static int realtek_phy_reset ( struct realtek_nic *rtl ) {
  294. int rc;
  295. /* Do nothing if we have no separate PHY register access */
  296. if ( ! rtl->have_phy_regs )
  297. return 0;
  298. /* Perform MII reset */
  299. if ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) {
  300. DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
  301. rtl, strerror ( rc ) );
  302. return rc;
  303. }
  304. /* Some cards (e.g. RTL8169SC) do not advertise Gigabit by
  305. * default. Try to enable advertisement of Gigabit speeds.
  306. */
  307. if ( ( rc = realtek_phy_speed ( rtl ) ) != 0 ) {
  308. /* Ignore failures, since the register may not be
  309. * present on non-Gigabit PHYs (e.g. RTL8101).
  310. */
  311. }
  312. /* Restart autonegotiation */
  313. if ( ( rc = mii_restart ( &rtl->mii ) ) != 0 ) {
  314. DBGC ( rtl, "REALTEK %p could not restart MII: %s\n",
  315. rtl, strerror ( rc ) );
  316. return rc;
  317. }
  318. return 0;
  319. }
  320. /******************************************************************************
  321. *
  322. * Link state
  323. *
  324. ******************************************************************************
  325. */
  326. /**
  327. * Check link state
  328. *
  329. * @v netdev Network device
  330. */
  331. static void realtek_check_link ( struct net_device *netdev ) {
  332. struct realtek_nic *rtl = netdev->priv;
  333. uint8_t phystatus;
  334. uint8_t msr;
  335. int link_up;
  336. /* Determine link state */
  337. if ( rtl->have_phy_regs ) {
  338. phystatus = readb ( rtl->regs + RTL_PHYSTATUS );
  339. link_up = ( phystatus & RTL_PHYSTATUS_LINKSTS );
  340. DBGC ( rtl, "REALTEK %p PHY status is %02x\n", rtl, phystatus );
  341. } else {
  342. msr = readb ( rtl->regs + RTL_MSR );
  343. link_up = ( ! ( msr & RTL_MSR_LINKB ) );
  344. DBGC ( rtl, "REALTEK %p media status is %02x\n", rtl, msr );
  345. }
  346. /* Report link state */
  347. if ( link_up ) {
  348. netdev_link_up ( netdev );
  349. } else {
  350. netdev_link_down ( netdev );
  351. }
  352. }
  353. /******************************************************************************
  354. *
  355. * Network device interface
  356. *
  357. ******************************************************************************
  358. */
  359. /**
  360. * Create receive buffer (legacy mode)
  361. *
  362. * @v rtl Realtek device
  363. * @ret rc Return status code
  364. */
  365. static int realtek_create_buffer ( struct realtek_nic *rtl ) {
  366. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  367. physaddr_t address;
  368. int rc;
  369. /* Do nothing unless in legacy mode */
  370. if ( ! rtl->legacy )
  371. return 0;
  372. /* Allocate buffer */
  373. rtl->rx_buffer = malloc_dma ( len, RTL_RXBUF_ALIGN );
  374. if ( ! rtl->rx_buffer ) {
  375. rc = -ENOMEM;
  376. goto err_alloc;
  377. }
  378. address = virt_to_bus ( rtl->rx_buffer );
  379. /* Check that card can support address */
  380. if ( address & ~0xffffffffULL ) {
  381. DBGC ( rtl, "REALTEK %p cannot support 64-bit RX buffer "
  382. "address\n", rtl );
  383. rc = -ENOTSUP;
  384. goto err_64bit;
  385. }
  386. /* Program buffer address */
  387. writel ( address, rtl->regs + RTL_RBSTART );
  388. DBGC ( rtl, "REALTEK %p receive buffer is at [%08llx,%08llx,%08llx)\n",
  389. rtl, ( ( unsigned long long ) address ),
  390. ( ( unsigned long long ) address + RTL_RXBUF_LEN ),
  391. ( ( unsigned long long ) address + len ) );
  392. return 0;
  393. err_64bit:
  394. free_dma ( rtl->rx_buffer, len );
  395. rtl->rx_buffer = NULL;
  396. err_alloc:
  397. return rc;
  398. }
  399. /**
  400. * Destroy receive buffer (legacy mode)
  401. *
  402. * @v rtl Realtek device
  403. */
  404. static void realtek_destroy_buffer ( struct realtek_nic *rtl ) {
  405. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  406. /* Do nothing unless in legacy mode */
  407. if ( ! rtl->legacy )
  408. return;
  409. /* Clear buffer address */
  410. writel ( 0, rtl->regs + RTL_RBSTART );
  411. /* Free buffer */
  412. free_dma ( rtl->rx_buffer, len );
  413. rtl->rx_buffer = NULL;
  414. rtl->rx_offset = 0;
  415. }
  416. /**
  417. * Create descriptor ring
  418. *
  419. * @v rtl Realtek device
  420. * @v ring Descriptor ring
  421. * @ret rc Return status code
  422. */
  423. static int realtek_create_ring ( struct realtek_nic *rtl,
  424. struct realtek_ring *ring ) {
  425. physaddr_t address;
  426. /* Do nothing in legacy mode */
  427. if ( rtl->legacy )
  428. return 0;
  429. /* Allocate descriptor ring */
  430. ring->desc = malloc_dma ( ring->len, RTL_RING_ALIGN );
  431. if ( ! ring->desc )
  432. return -ENOMEM;
  433. /* Initialise descriptor ring */
  434. memset ( ring->desc, 0, ring->len );
  435. /* Program ring address */
  436. address = virt_to_bus ( ring->desc );
  437. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  438. rtl->regs + ring->reg + 4 );
  439. writel ( ( address & 0xffffffffUL ), rtl->regs + ring->reg );
  440. DBGC ( rtl, "REALTEK %p ring %02x is at [%08llx,%08llx)\n",
  441. rtl, ring->reg, ( ( unsigned long long ) address ),
  442. ( ( unsigned long long ) address + ring->len ) );
  443. return 0;
  444. }
  445. /**
  446. * Destroy descriptor ring
  447. *
  448. * @v rtl Realtek device
  449. * @v ring Descriptor ring
  450. */
  451. static void realtek_destroy_ring ( struct realtek_nic *rtl,
  452. struct realtek_ring *ring ) {
  453. /* Do nothing in legacy mode */
  454. if ( rtl->legacy )
  455. return;
  456. /* Clear ring address */
  457. writel ( 0, rtl->regs + ring->reg );
  458. writel ( 0, rtl->regs + ring->reg + 4 );
  459. /* Free descriptor ring */
  460. free_dma ( ring->desc, ring->len );
  461. ring->desc = NULL;
  462. ring->prod = 0;
  463. ring->cons = 0;
  464. }
  465. /**
  466. * Refill receive descriptor ring
  467. *
  468. * @v rtl Realtek device
  469. */
  470. static void realtek_refill_rx ( struct realtek_nic *rtl ) {
  471. struct realtek_descriptor *rx;
  472. struct io_buffer *iobuf;
  473. unsigned int rx_idx;
  474. physaddr_t address;
  475. int is_last;
  476. /* Do nothing in legacy mode */
  477. if ( rtl->legacy )
  478. return;
  479. while ( ( rtl->rx.prod - rtl->rx.cons ) < RTL_NUM_RX_DESC ) {
  480. /* Allocate I/O buffer */
  481. iobuf = alloc_iob ( RTL_RX_MAX_LEN );
  482. if ( ! iobuf ) {
  483. /* Wait for next refill */
  484. return;
  485. }
  486. /* Get next receive descriptor */
  487. rx_idx = ( rtl->rx.prod++ % RTL_NUM_RX_DESC );
  488. is_last = ( rx_idx == ( RTL_NUM_RX_DESC - 1 ) );
  489. rx = &rtl->rx.desc[rx_idx];
  490. /* Populate receive descriptor */
  491. address = virt_to_bus ( iobuf->data );
  492. rx->address = cpu_to_le64 ( address );
  493. rx->length = cpu_to_le16 ( RTL_RX_MAX_LEN );
  494. wmb();
  495. rx->flags = ( cpu_to_le16 ( RTL_DESC_OWN ) |
  496. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  497. wmb();
  498. /* Record I/O buffer */
  499. assert ( rtl->rx_iobuf[rx_idx] == NULL );
  500. rtl->rx_iobuf[rx_idx] = iobuf;
  501. DBGC2 ( rtl, "REALTEK %p RX %d is [%llx,%llx)\n", rtl, rx_idx,
  502. ( ( unsigned long long ) address ),
  503. ( ( unsigned long long ) address + RTL_RX_MAX_LEN ) );
  504. }
  505. }
  506. /**
  507. * Open network device
  508. *
  509. * @v netdev Network device
  510. * @ret rc Return status code
  511. */
  512. static int realtek_open ( struct net_device *netdev ) {
  513. struct realtek_nic *rtl = netdev->priv;
  514. uint32_t tcr;
  515. uint32_t rcr;
  516. int rc;
  517. /* Create transmit descriptor ring */
  518. if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
  519. goto err_create_tx;
  520. /* Create receive descriptor ring */
  521. if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
  522. goto err_create_rx;
  523. /* Create receive buffer */
  524. if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
  525. goto err_create_buffer;
  526. /* Accept all packets */
  527. writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
  528. writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
  529. /* Enable transmitter and receiver. RTL8139 requires that
  530. * this happens before writing to RCR.
  531. */
  532. writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
  533. /* Configure transmitter */
  534. tcr = readl ( rtl->regs + RTL_TCR );
  535. tcr &= ~RTL_TCR_MXDMA_MASK;
  536. tcr |= RTL_TCR_MXDMA_DEFAULT;
  537. writel ( tcr, rtl->regs + RTL_TCR );
  538. /* Configure receiver */
  539. rcr = readl ( rtl->regs + RTL_RCR );
  540. rcr &= ~( RTL_RCR_RXFTH_MASK | RTL_RCR_RBLEN_MASK |
  541. RTL_RCR_MXDMA_MASK );
  542. rcr |= ( RTL_RCR_RXFTH_DEFAULT | RTL_RCR_RBLEN_DEFAULT |
  543. RTL_RCR_MXDMA_DEFAULT | RTL_RCR_WRAP | RTL_RCR_AB |
  544. RTL_RCR_AM | RTL_RCR_APM | RTL_RCR_AAP );
  545. writel ( rcr, rtl->regs + RTL_RCR );
  546. /* Fill receive ring */
  547. realtek_refill_rx ( rtl );
  548. /* Update link state */
  549. realtek_check_link ( netdev );
  550. return 0;
  551. realtek_destroy_buffer ( rtl );
  552. err_create_buffer:
  553. realtek_destroy_ring ( rtl, &rtl->rx );
  554. err_create_rx:
  555. realtek_destroy_ring ( rtl, &rtl->tx );
  556. err_create_tx:
  557. return rc;
  558. }
  559. /**
  560. * Close network device
  561. *
  562. * @v netdev Network device
  563. */
  564. static void realtek_close ( struct net_device *netdev ) {
  565. struct realtek_nic *rtl = netdev->priv;
  566. unsigned int i;
  567. /* Disable receiver and transmitter */
  568. writeb ( 0, rtl->regs + RTL_CR );
  569. /* Destroy receive buffer */
  570. realtek_destroy_buffer ( rtl );
  571. /* Destroy receive descriptor ring */
  572. realtek_destroy_ring ( rtl, &rtl->rx );
  573. /* Discard any unused receive buffers */
  574. for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
  575. if ( rtl->rx_iobuf[i] )
  576. free_iob ( rtl->rx_iobuf[i] );
  577. rtl->rx_iobuf[i] = NULL;
  578. }
  579. /* Destroy transmit descriptor ring */
  580. realtek_destroy_ring ( rtl, &rtl->tx );
  581. }
  582. /**
  583. * Transmit packet
  584. *
  585. * @v netdev Network device
  586. * @v iobuf I/O buffer
  587. * @ret rc Return status code
  588. */
  589. static int realtek_transmit ( struct net_device *netdev,
  590. struct io_buffer *iobuf ) {
  591. struct realtek_nic *rtl = netdev->priv;
  592. struct realtek_descriptor *tx;
  593. unsigned int tx_idx;
  594. physaddr_t address;
  595. int is_last;
  596. /* Get next transmit descriptor */
  597. if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
  598. DBGC ( rtl, "REALTEK %p out of transmit descriptors\n", rtl );
  599. return -ENOBUFS;
  600. }
  601. tx_idx = ( rtl->tx.prod++ % RTL_NUM_TX_DESC );
  602. /* Transmit packet */
  603. if ( rtl->legacy ) {
  604. /* Pad and align packet */
  605. iob_pad ( iobuf, ETH_ZLEN );
  606. address = virt_to_bus ( iobuf->data );
  607. /* Check that card can support address */
  608. if ( address & ~0xffffffffULL ) {
  609. DBGC ( rtl, "REALTEK %p cannot support 64-bit TX "
  610. "buffer address\n", rtl );
  611. return -ENOTSUP;
  612. }
  613. /* Add to transmit ring */
  614. writel ( address, rtl->regs + RTL_TSAD ( tx_idx ) );
  615. writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
  616. rtl->regs + RTL_TSD ( tx_idx ) );
  617. } else {
  618. /* Populate transmit descriptor */
  619. address = virt_to_bus ( iobuf->data );
  620. is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
  621. tx = &rtl->tx.desc[tx_idx];
  622. tx->address = cpu_to_le64 ( address );
  623. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  624. wmb();
  625. tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
  626. RTL_DESC_LS ) |
  627. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  628. wmb();
  629. /* Notify card that there are packets ready to transmit */
  630. writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
  631. }
  632. DBGC2 ( rtl, "REALTEK %p TX %d is [%llx,%llx)\n", rtl, tx_idx,
  633. ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ),
  634. ( ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ) +
  635. iob_len ( iobuf ) ) );
  636. return 0;
  637. }
  638. /**
  639. * Poll for completed packets
  640. *
  641. * @v netdev Network device
  642. */
  643. static void realtek_poll_tx ( struct net_device *netdev ) {
  644. struct realtek_nic *rtl = netdev->priv;
  645. struct realtek_descriptor *tx;
  646. unsigned int tx_idx;
  647. /* Check for completed packets */
  648. while ( rtl->tx.cons != rtl->tx.prod ) {
  649. /* Get next transmit descriptor */
  650. tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
  651. /* Stop if descriptor is still in use */
  652. if ( rtl->legacy ) {
  653. /* Check ownership bit in transmit status register */
  654. if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
  655. RTL_TSD_OWN ) )
  656. return;
  657. } else {
  658. /* Check ownership bit in descriptor */
  659. tx = &rtl->tx.desc[tx_idx];
  660. if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  661. return;
  662. }
  663. DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
  664. /* Complete TX descriptor */
  665. netdev_tx_complete_next ( netdev );
  666. rtl->tx.cons++;
  667. }
  668. }
  669. /**
  670. * Poll for received packets (legacy mode)
  671. *
  672. * @v netdev Network device
  673. */
  674. static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
  675. struct realtek_nic *rtl = netdev->priv;
  676. struct realtek_legacy_header *rx;
  677. struct io_buffer *iobuf;
  678. size_t len;
  679. /* Check for received packets */
  680. while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
  681. /* Extract packet from receive buffer */
  682. rx = ( rtl->rx_buffer + rtl->rx_offset );
  683. len = le16_to_cpu ( rx->length );
  684. if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
  685. DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
  686. rtl, rtl->rx_offset, len );
  687. /* Allocate I/O buffer */
  688. iobuf = alloc_iob ( len );
  689. if ( ! iobuf ) {
  690. netdev_rx_err ( netdev, NULL, -ENOMEM );
  691. /* Leave packet for next poll */
  692. break;
  693. }
  694. /* Copy data to I/O buffer */
  695. memcpy ( iob_put ( iobuf, len ), rx->data, len );
  696. iob_unput ( iobuf, 4 /* strip CRC */ );
  697. /* Hand off to network stack */
  698. netdev_rx ( netdev, iobuf );
  699. } else {
  700. DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
  701. rtl, rtl->rx_offset, len,
  702. le16_to_cpu ( rx->status ) );
  703. netdev_rx_err ( netdev, NULL, -EIO );
  704. }
  705. /* Update buffer offset */
  706. rtl->rx_offset = ( rtl->rx_offset + sizeof ( *rx ) + len );
  707. rtl->rx_offset = ( ( rtl->rx_offset + 3 ) & ~3 );
  708. rtl->rx_offset = ( rtl->rx_offset % RTL_RXBUF_LEN );
  709. writew ( ( rtl->rx_offset - 16 ), rtl->regs + RTL_CAPR );
  710. }
  711. }
  712. /**
  713. * Poll for received packets
  714. *
  715. * @v netdev Network device
  716. */
  717. static void realtek_poll_rx ( struct net_device *netdev ) {
  718. struct realtek_nic *rtl = netdev->priv;
  719. struct realtek_descriptor *rx;
  720. struct io_buffer *iobuf;
  721. unsigned int rx_idx;
  722. size_t len;
  723. /* Poll receive buffer if in legacy mode */
  724. if ( rtl->legacy ) {
  725. realtek_legacy_poll_rx ( netdev );
  726. return;
  727. }
  728. /* Check for received packets */
  729. while ( rtl->rx.cons != rtl->rx.prod ) {
  730. /* Get next receive descriptor */
  731. rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
  732. rx = &rtl->rx.desc[rx_idx];
  733. /* Stop if descriptor is still in use */
  734. if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  735. return;
  736. /* Populate I/O buffer */
  737. iobuf = rtl->rx_iobuf[rx_idx];
  738. rtl->rx_iobuf[rx_idx] = NULL;
  739. len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
  740. iob_put ( iobuf, ( len - 4 /* strip CRC */ ) );
  741. DBGC2 ( rtl, "REALTEK %p RX %d complete (length %zd)\n",
  742. rtl, rx_idx, len );
  743. /* Hand off to network stack */
  744. if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
  745. netdev_rx_err ( netdev, iobuf, -EIO );
  746. } else {
  747. netdev_rx ( netdev, iobuf );
  748. }
  749. rtl->rx.cons++;
  750. }
  751. }
  752. /**
  753. * Poll for completed and received packets
  754. *
  755. * @v netdev Network device
  756. */
  757. static void realtek_poll ( struct net_device *netdev ) {
  758. struct realtek_nic *rtl = netdev->priv;
  759. uint16_t isr;
  760. /* Check for and acknowledge interrupts */
  761. isr = readw ( rtl->regs + RTL_ISR );
  762. if ( ! isr )
  763. return;
  764. writew ( isr, rtl->regs + RTL_ISR );
  765. /* Poll for TX completions, if applicable */
  766. if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
  767. realtek_poll_tx ( netdev );
  768. /* Poll for RX completionsm, if applicable */
  769. if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
  770. realtek_poll_rx ( netdev );
  771. /* Check link state, if applicable */
  772. if ( isr & RTL_IRQ_PUN_LINKCHG )
  773. realtek_check_link ( netdev );
  774. /* Refill RX ring */
  775. realtek_refill_rx ( rtl );
  776. }
  777. /**
  778. * Enable or disable interrupts
  779. *
  780. * @v netdev Network device
  781. * @v enable Interrupts should be enabled
  782. */
  783. static void realtek_irq ( struct net_device *netdev, int enable ) {
  784. struct realtek_nic *rtl = netdev->priv;
  785. uint16_t imr;
  786. /* Set interrupt mask */
  787. imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
  788. RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
  789. writew ( imr, rtl->regs + RTL_IMR );
  790. }
  791. /** Realtek network device operations */
  792. static struct net_device_operations realtek_operations = {
  793. .open = realtek_open,
  794. .close = realtek_close,
  795. .transmit = realtek_transmit,
  796. .poll = realtek_poll,
  797. .irq = realtek_irq,
  798. };
  799. /******************************************************************************
  800. *
  801. * PCI interface
  802. *
  803. ******************************************************************************
  804. */
  805. /**
  806. * Detect device type
  807. *
  808. * @v rtl Realtek device
  809. */
  810. static void realtek_detect ( struct realtek_nic *rtl ) {
  811. uint16_t rms;
  812. uint16_t check_rms;
  813. uint16_t cpcr;
  814. uint16_t check_cpcr;
  815. /* The RX Packet Maximum Size register is present only on
  816. * 8169. Try to set to our intended MTU.
  817. */
  818. rms = RTL_RX_MAX_LEN;
  819. writew ( rms, rtl->regs + RTL_RMS );
  820. check_rms = readw ( rtl->regs + RTL_RMS );
  821. /* The C+ Command register is present only on 8169 and 8139C+.
  822. * Try to enable C+ mode and PCI Dual Address Cycle (for
  823. * 64-bit systems), if supported.
  824. *
  825. * Note that enabling DAC seems to cause bizarre behaviour
  826. * (lockups, garbage data on the wire) on some systems, even
  827. * if only 32-bit addresses are used.
  828. */
  829. cpcr = readw ( rtl->regs + RTL_CPCR );
  830. cpcr |= ( RTL_CPCR_MULRW | RTL_CPCR_CPRX | RTL_CPCR_CPTX );
  831. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
  832. cpcr |= RTL_CPCR_DAC;
  833. writew ( cpcr, rtl->regs + RTL_CPCR );
  834. check_cpcr = readw ( rtl->regs + RTL_CPCR );
  835. /* Detect device type */
  836. if ( check_rms == rms ) {
  837. DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
  838. rtl->have_phy_regs = 1;
  839. rtl->tppoll = RTL_TPPOLL_8169;
  840. } else {
  841. if ( ( check_cpcr == cpcr ) && ( cpcr != 0xffff ) ) {
  842. DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
  843. rtl );
  844. rtl->tppoll = RTL_TPPOLL_8139CP;
  845. } else {
  846. DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
  847. rtl );
  848. rtl->legacy = 1;
  849. }
  850. }
  851. }
  852. /**
  853. * Probe PCI device
  854. *
  855. * @v pci PCI device
  856. * @ret rc Return status code
  857. */
  858. static int realtek_probe ( struct pci_device *pci ) {
  859. struct net_device *netdev;
  860. struct realtek_nic *rtl;
  861. unsigned int i;
  862. int rc;
  863. /* Allocate and initialise net device */
  864. netdev = alloc_etherdev ( sizeof ( *rtl ) );
  865. if ( ! netdev ) {
  866. rc = -ENOMEM;
  867. goto err_alloc;
  868. }
  869. netdev_init ( netdev, &realtek_operations );
  870. rtl = netdev->priv;
  871. pci_set_drvdata ( pci, netdev );
  872. netdev->dev = &pci->dev;
  873. memset ( rtl, 0, sizeof ( *rtl ) );
  874. realtek_init_ring ( &rtl->tx, RTL_NUM_TX_DESC, RTL_TNPDS );
  875. realtek_init_ring ( &rtl->rx, RTL_NUM_RX_DESC, RTL_RDSAR );
  876. /* Fix up PCI device */
  877. adjust_pci_device ( pci );
  878. /* Map registers */
  879. rtl->regs = ioremap ( pci->membase, RTL_BAR_SIZE );
  880. /* Reset the NIC */
  881. if ( ( rc = realtek_reset ( rtl ) ) != 0 )
  882. goto err_reset;
  883. /* Detect device type */
  884. realtek_detect ( rtl );
  885. /* Initialise EEPROM */
  886. realtek_init_eeprom ( netdev );
  887. /* Read MAC address from EEPROM */
  888. if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
  889. netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
  890. DBGC ( rtl, "REALTEK %p could not read MAC address: %s\n",
  891. rtl, strerror ( rc ) );
  892. goto err_nvs_read;
  893. }
  894. /* The EEPROM may not be present for onboard NICs. Fall back
  895. * to reading the current ID register value, which will
  896. * hopefully have been programmed by the platform firmware.
  897. */
  898. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  899. DBGC ( rtl, "REALTEK %p seems to have no EEPROM (MAC %s)\n",
  900. rtl, eth_ntoa ( netdev->hw_addr ) );
  901. for ( i = 0 ; i < ETH_ALEN ; i++ )
  902. netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
  903. }
  904. /* Initialise and reset MII interface */
  905. mii_init ( &rtl->mii, &realtek_mii_operations );
  906. if ( ( rc = realtek_phy_reset ( rtl ) ) != 0 )
  907. goto err_phy_reset;
  908. /* Register network device */
  909. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  910. goto err_register_netdev;
  911. /* Set initial link state */
  912. realtek_check_link ( netdev );
  913. /* Register non-volatile options, if applicable */
  914. if ( rtl->nvo.nvs ) {
  915. if ( ( rc = register_nvo ( &rtl->nvo,
  916. netdev_settings ( netdev ) ) ) != 0)
  917. goto err_register_nvo;
  918. }
  919. return 0;
  920. err_register_nvo:
  921. unregister_netdev ( netdev );
  922. err_register_netdev:
  923. err_phy_reset:
  924. err_nvs_read:
  925. realtek_reset ( rtl );
  926. err_reset:
  927. iounmap ( rtl->regs );
  928. netdev_nullify ( netdev );
  929. netdev_put ( netdev );
  930. err_alloc:
  931. return rc;
  932. }
  933. /**
  934. * Remove PCI device
  935. *
  936. * @v pci PCI device
  937. */
  938. static void realtek_remove ( struct pci_device *pci ) {
  939. struct net_device *netdev = pci_get_drvdata ( pci );
  940. struct realtek_nic *rtl = netdev->priv;
  941. /* Unregister non-volatile options, if applicable */
  942. if ( rtl->nvo.nvs )
  943. unregister_nvo ( &rtl->nvo );
  944. /* Unregister network device */
  945. unregister_netdev ( netdev );
  946. /* Reset card */
  947. realtek_reset ( rtl );
  948. /* Free network device */
  949. iounmap ( rtl->regs );
  950. netdev_nullify ( netdev );
  951. netdev_put ( netdev );
  952. }
  953. /** Realtek PCI device IDs */
  954. static struct pci_device_id realtek_nics[] = {
  955. PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
  956. PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
  957. PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
  958. PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
  959. PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
  960. PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
  961. PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
  962. PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
  963. PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
  964. PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
  965. PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
  966. PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
  967. PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
  968. PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
  969. PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
  970. PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
  971. PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
  972. PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
  973. PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
  974. PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
  975. PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
  976. PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
  977. PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
  978. PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
  979. PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
  980. PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
  981. PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
  982. PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
  983. PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
  984. PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
  985. PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
  986. };
  987. /** Realtek PCI driver */
  988. struct pci_driver realtek_driver __pci_driver = {
  989. .ids = realtek_nics,
  990. .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
  991. .probe = realtek_probe,
  992. .remove = realtek_remove,
  993. };