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

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