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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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 rcr;
  432. int rc;
  433. /* Create transmit descriptor ring */
  434. if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
  435. goto err_create_tx;
  436. /* Create receive descriptor ring */
  437. if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
  438. goto err_create_rx;
  439. /* Create receive buffer */
  440. if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
  441. goto err_create_buffer;
  442. /* Accept all packets */
  443. writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
  444. writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
  445. /* Enable transmitter and receiver. RTL8139 requires that
  446. * this happens before writing to RCR.
  447. */
  448. writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
  449. /* Configure receiver */
  450. rcr = readl ( rtl->regs + RTL_RCR );
  451. rcr &= ~( RTL_RCR_RXFTH_MASK | RTL_RCR_RBLEN_MASK |
  452. RTL_RCR_MXDMA_MASK );
  453. rcr |= ( RTL_RCR_RXFTH_DEFAULT | RTL_RCR_RBLEN_DEFAULT |
  454. RTL_RCR_MXDMA_DEFAULT | RTL_RCR_WRAP | RTL_RCR_AB |
  455. RTL_RCR_AM | RTL_RCR_APM | RTL_RCR_AAP );
  456. writel ( rcr, rtl->regs + RTL_RCR );
  457. /* Fill receive ring */
  458. realtek_refill_rx ( rtl );
  459. /* Update link state */
  460. realtek_check_link ( netdev );
  461. return 0;
  462. realtek_destroy_buffer ( rtl );
  463. err_create_buffer:
  464. realtek_destroy_ring ( rtl, &rtl->rx );
  465. err_create_rx:
  466. realtek_destroy_ring ( rtl, &rtl->tx );
  467. err_create_tx:
  468. return rc;
  469. }
  470. /**
  471. * Close network device
  472. *
  473. * @v netdev Network device
  474. */
  475. static void realtek_close ( struct net_device *netdev ) {
  476. struct realtek_nic *rtl = netdev->priv;
  477. unsigned int i;
  478. /* Disable receiver and transmitter */
  479. writeb ( 0, rtl->regs + RTL_CR );
  480. /* Destroy receive buffer */
  481. realtek_destroy_buffer ( rtl );
  482. /* Destroy receive descriptor ring */
  483. realtek_destroy_ring ( rtl, &rtl->rx );
  484. /* Discard any unused receive buffers */
  485. for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
  486. if ( rtl->rx_iobuf[i] )
  487. free_iob ( rtl->rx_iobuf[i] );
  488. rtl->rx_iobuf[i] = NULL;
  489. }
  490. /* Destroy transmit descriptor ring */
  491. realtek_destroy_ring ( rtl, &rtl->tx );
  492. }
  493. /**
  494. * Transmit packet
  495. *
  496. * @v netdev Network device
  497. * @v iobuf I/O buffer
  498. * @ret rc Return status code
  499. */
  500. static int realtek_transmit ( struct net_device *netdev,
  501. struct io_buffer *iobuf ) {
  502. struct realtek_nic *rtl = netdev->priv;
  503. struct realtek_descriptor *tx;
  504. unsigned int tx_idx;
  505. physaddr_t address;
  506. int is_last;
  507. /* Get next transmit descriptor */
  508. if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
  509. DBGC ( rtl, "REALTEK %p out of transmit descriptors\n", rtl );
  510. return -ENOBUFS;
  511. }
  512. tx_idx = ( rtl->tx.prod++ % RTL_NUM_TX_DESC );
  513. /* Transmit packet */
  514. if ( rtl->legacy ) {
  515. /* Pad and align packet */
  516. iob_pad ( iobuf, ETH_ZLEN );
  517. address = virt_to_bus ( iobuf->data );
  518. /* Check that card can support address */
  519. if ( address & ~0xffffffffULL ) {
  520. DBGC ( rtl, "REALTEK %p cannot support 64-bit TX "
  521. "buffer address\n", rtl );
  522. return -ENOTSUP;
  523. }
  524. /* Add to transmit ring */
  525. writel ( address, rtl->regs + RTL_TSAD ( tx_idx ) );
  526. writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
  527. rtl->regs + RTL_TSD ( tx_idx ) );
  528. } else {
  529. /* Populate transmit descriptor */
  530. address = virt_to_bus ( iobuf->data );
  531. is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
  532. tx = &rtl->tx.desc[tx_idx];
  533. tx->address = cpu_to_le64 ( address );
  534. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  535. wmb();
  536. tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
  537. RTL_DESC_LS ) |
  538. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  539. wmb();
  540. /* Notify card that there are packets ready to transmit */
  541. writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
  542. }
  543. DBGC2 ( rtl, "REALTEK %p TX %d is [%llx,%llx)\n", rtl, tx_idx,
  544. ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ),
  545. ( ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ) +
  546. iob_len ( iobuf ) ) );
  547. return 0;
  548. }
  549. /**
  550. * Poll for completed packets
  551. *
  552. * @v netdev Network device
  553. */
  554. static void realtek_poll_tx ( struct net_device *netdev ) {
  555. struct realtek_nic *rtl = netdev->priv;
  556. struct realtek_descriptor *tx;
  557. unsigned int tx_idx;
  558. /* Check for completed packets */
  559. while ( rtl->tx.cons != rtl->tx.prod ) {
  560. /* Get next transmit descriptor */
  561. tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
  562. /* Stop if descriptor is still in use */
  563. if ( rtl->legacy ) {
  564. /* Check ownership bit in transmit status register */
  565. if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
  566. RTL_TSD_OWN ) )
  567. return;
  568. } else {
  569. /* Check ownership bit in descriptor */
  570. tx = &rtl->tx.desc[tx_idx];
  571. if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  572. return;
  573. }
  574. DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
  575. /* Complete TX descriptor */
  576. netdev_tx_complete_next ( netdev );
  577. rtl->tx.cons++;
  578. }
  579. }
  580. /**
  581. * Poll for received packets (legacy mode)
  582. *
  583. * @v netdev Network device
  584. */
  585. static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
  586. struct realtek_nic *rtl = netdev->priv;
  587. struct realtek_legacy_header *rx;
  588. struct io_buffer *iobuf;
  589. size_t len;
  590. /* Check for received packets */
  591. while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
  592. /* Extract packet from receive buffer */
  593. rx = ( rtl->rx_buffer + rtl->rx_offset );
  594. len = le16_to_cpu ( rx->length );
  595. if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
  596. DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
  597. rtl, rtl->rx_offset, len );
  598. /* Allocate I/O buffer */
  599. iobuf = alloc_iob ( len );
  600. if ( ! iobuf ) {
  601. netdev_rx_err ( netdev, NULL, -ENOMEM );
  602. /* Leave packet for next poll */
  603. break;
  604. }
  605. /* Copy data to I/O buffer */
  606. memcpy ( iob_put ( iobuf, len ), rx->data, len );
  607. iob_unput ( iobuf, 4 /* strip CRC */ );
  608. /* Hand off to network stack */
  609. netdev_rx ( netdev, iobuf );
  610. } else {
  611. DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
  612. rtl, rtl->rx_offset, len,
  613. le16_to_cpu ( rx->status ) );
  614. netdev_rx_err ( netdev, NULL, -EIO );
  615. }
  616. /* Update buffer offset */
  617. rtl->rx_offset = ( rtl->rx_offset + sizeof ( *rx ) + len );
  618. rtl->rx_offset = ( ( rtl->rx_offset + 3 ) & ~3 );
  619. rtl->rx_offset = ( rtl->rx_offset % RTL_RXBUF_LEN );
  620. writew ( ( rtl->rx_offset - 16 ), rtl->regs + RTL_CAPR );
  621. }
  622. }
  623. /**
  624. * Poll for received packets
  625. *
  626. * @v netdev Network device
  627. */
  628. static void realtek_poll_rx ( struct net_device *netdev ) {
  629. struct realtek_nic *rtl = netdev->priv;
  630. struct realtek_descriptor *rx;
  631. struct io_buffer *iobuf;
  632. unsigned int rx_idx;
  633. size_t len;
  634. /* Poll receive buffer if in legacy mode */
  635. if ( rtl->legacy ) {
  636. realtek_legacy_poll_rx ( netdev );
  637. return;
  638. }
  639. /* Check for received packets */
  640. while ( rtl->rx.cons != rtl->rx.prod ) {
  641. /* Get next receive descriptor */
  642. rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
  643. rx = &rtl->rx.desc[rx_idx];
  644. /* Stop if descriptor is still in use */
  645. if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  646. return;
  647. /* Populate I/O buffer */
  648. iobuf = rtl->rx_iobuf[rx_idx];
  649. rtl->rx_iobuf[rx_idx] = NULL;
  650. len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
  651. iob_put ( iobuf, ( len - 4 /* strip CRC */ ) );
  652. DBGC2 ( rtl, "REALTEK %p RX %d complete (length %zd)\n",
  653. rtl, rx_idx, len );
  654. /* Hand off to network stack */
  655. if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
  656. netdev_rx_err ( netdev, iobuf, -EIO );
  657. } else {
  658. netdev_rx ( netdev, iobuf );
  659. }
  660. rtl->rx.cons++;
  661. }
  662. }
  663. /**
  664. * Poll for completed and received packets
  665. *
  666. * @v netdev Network device
  667. */
  668. static void realtek_poll ( struct net_device *netdev ) {
  669. struct realtek_nic *rtl = netdev->priv;
  670. uint16_t isr;
  671. /* Check for and acknowledge interrupts */
  672. isr = readw ( rtl->regs + RTL_ISR );
  673. if ( ! isr )
  674. return;
  675. writew ( isr, rtl->regs + RTL_ISR );
  676. /* Poll for TX completions, if applicable */
  677. if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
  678. realtek_poll_tx ( netdev );
  679. /* Poll for RX completionsm, if applicable */
  680. if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
  681. realtek_poll_rx ( netdev );
  682. /* Check link state, if applicable */
  683. if ( isr & RTL_IRQ_PUN_LINKCHG )
  684. realtek_check_link ( netdev );
  685. /* Refill RX ring */
  686. realtek_refill_rx ( rtl );
  687. }
  688. /**
  689. * Enable or disable interrupts
  690. *
  691. * @v netdev Network device
  692. * @v enable Interrupts should be enabled
  693. */
  694. static void realtek_irq ( struct net_device *netdev, int enable ) {
  695. struct realtek_nic *rtl = netdev->priv;
  696. uint16_t imr;
  697. /* Set interrupt mask */
  698. imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
  699. RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
  700. writew ( imr, rtl->regs + RTL_IMR );
  701. }
  702. /** Realtek network device operations */
  703. static struct net_device_operations realtek_operations = {
  704. .open = realtek_open,
  705. .close = realtek_close,
  706. .transmit = realtek_transmit,
  707. .poll = realtek_poll,
  708. .irq = realtek_irq,
  709. };
  710. /******************************************************************************
  711. *
  712. * PCI interface
  713. *
  714. ******************************************************************************
  715. */
  716. /**
  717. * Detect device type
  718. *
  719. * @v rtl Realtek device
  720. */
  721. static void realtek_detect ( struct realtek_nic *rtl ) {
  722. uint16_t rms;
  723. uint16_t check_rms;
  724. uint16_t cpcr;
  725. uint16_t check_cpcr;
  726. /* The RX Packet Maximum Size register is present only on
  727. * 8169. Try to set to our intended MTU.
  728. */
  729. rms = RTL_RX_MAX_LEN;
  730. writew ( rms, rtl->regs + RTL_RMS );
  731. check_rms = readw ( rtl->regs + RTL_RMS );
  732. /* The C+ Command register is present only on 8169 and 8139C+.
  733. * Try to enable C+ mode and PCI Dual Address Cycle (for
  734. * 64-bit systems), if supported.
  735. */
  736. cpcr = ( RTL_CPCR_DAC | RTL_CPCR_MULRW | RTL_CPCR_CPRX |
  737. RTL_CPCR_CPTX );
  738. writew ( cpcr, rtl->regs + RTL_CPCR );
  739. check_cpcr = readw ( rtl->regs + RTL_CPCR );
  740. /* Detect device type */
  741. if ( check_rms == rms ) {
  742. DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
  743. rtl->have_phy_regs = 1;
  744. rtl->tppoll = RTL_TPPOLL_8169;
  745. } else {
  746. if ( check_cpcr == cpcr ) {
  747. DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
  748. rtl );
  749. rtl->tppoll = RTL_TPPOLL_8139CP;
  750. } else {
  751. DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
  752. rtl );
  753. rtl->legacy = 1;
  754. }
  755. }
  756. }
  757. /**
  758. * Probe PCI device
  759. *
  760. * @v pci PCI device
  761. * @ret rc Return status code
  762. */
  763. static int realtek_probe ( struct pci_device *pci ) {
  764. struct net_device *netdev;
  765. struct realtek_nic *rtl;
  766. unsigned int i;
  767. int rc;
  768. /* Allocate and initialise net device */
  769. netdev = alloc_etherdev ( sizeof ( *rtl ) );
  770. if ( ! netdev ) {
  771. rc = -ENOMEM;
  772. goto err_alloc;
  773. }
  774. netdev_init ( netdev, &realtek_operations );
  775. rtl = netdev->priv;
  776. pci_set_drvdata ( pci, netdev );
  777. netdev->dev = &pci->dev;
  778. memset ( rtl, 0, sizeof ( *rtl ) );
  779. realtek_init_ring ( &rtl->tx, RTL_NUM_TX_DESC, RTL_TNPDS );
  780. realtek_init_ring ( &rtl->rx, RTL_NUM_RX_DESC, RTL_RDSAR );
  781. /* Fix up PCI device */
  782. adjust_pci_device ( pci );
  783. /* Map registers */
  784. rtl->regs = ioremap ( pci->membase, RTL_BAR_SIZE );
  785. /* Reset the NIC */
  786. if ( ( rc = realtek_reset ( rtl ) ) != 0 )
  787. goto err_reset;
  788. /* Detect device type */
  789. realtek_detect ( rtl );
  790. /* Initialise EEPROM */
  791. realtek_init_eeprom ( netdev );
  792. /* Read MAC address from EEPROM */
  793. if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
  794. netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
  795. DBGC ( rtl, "REALTEK %p could not read MAC address: %s\n",
  796. rtl, strerror ( rc ) );
  797. goto err_nvs_read;
  798. }
  799. /* The EEPROM may not be present for onboard NICs. Fall back
  800. * to reading the current ID register value, which will
  801. * hopefully have been programmed by the platform firmware.
  802. */
  803. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  804. DBGC ( rtl, "REALTEK %p seems to have no EEPROM\n", rtl );
  805. for ( i = 0 ; i < ETH_ALEN ; i++ )
  806. netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
  807. }
  808. /* Initialise and reset MII interface */
  809. mii_init ( &rtl->mii, &realtek_mii_operations );
  810. if ( rtl->have_phy_regs &&
  811. ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) ) {
  812. DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
  813. rtl, strerror ( rc ) );
  814. goto err_mii_reset;
  815. }
  816. /* Register network device */
  817. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  818. goto err_register_netdev;
  819. /* Set initial link state */
  820. realtek_check_link ( netdev );
  821. /* Register non-volatile options, if applicable */
  822. if ( rtl->nvo.nvs ) {
  823. if ( ( rc = register_nvo ( &rtl->nvo,
  824. netdev_settings ( netdev ) ) ) != 0)
  825. goto err_register_nvo;
  826. }
  827. return 0;
  828. err_register_nvo:
  829. unregister_netdev ( netdev );
  830. err_register_netdev:
  831. err_mii_reset:
  832. err_nvs_read:
  833. realtek_reset ( rtl );
  834. err_reset:
  835. iounmap ( rtl->regs );
  836. netdev_nullify ( netdev );
  837. netdev_put ( netdev );
  838. err_alloc:
  839. return rc;
  840. }
  841. /**
  842. * Remove PCI device
  843. *
  844. * @v pci PCI device
  845. */
  846. static void realtek_remove ( struct pci_device *pci ) {
  847. struct net_device *netdev = pci_get_drvdata ( pci );
  848. struct realtek_nic *rtl = netdev->priv;
  849. /* Unregister non-volatile options, if applicable */
  850. if ( rtl->nvo.nvs )
  851. unregister_nvo ( &rtl->nvo );
  852. /* Unregister network device */
  853. unregister_netdev ( netdev );
  854. /* Reset card */
  855. realtek_reset ( rtl );
  856. /* Free network device */
  857. iounmap ( rtl->regs );
  858. netdev_nullify ( netdev );
  859. netdev_put ( netdev );
  860. }
  861. /** Realtek PCI device IDs */
  862. static struct pci_device_id realtek_nics[] = {
  863. PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
  864. PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
  865. PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
  866. PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
  867. PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
  868. PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
  869. PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
  870. PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
  871. PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
  872. PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
  873. PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
  874. PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
  875. PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
  876. PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
  877. PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
  878. PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
  879. PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
  880. PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
  881. PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
  882. PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
  883. PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
  884. PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
  885. PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
  886. PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
  887. PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
  888. PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
  889. PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
  890. PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
  891. PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
  892. PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
  893. PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
  894. };
  895. /** Realtek PCI driver */
  896. struct pci_driver realtek_driver __pci_driver = {
  897. .ids = realtek_nics,
  898. .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
  899. .probe = realtek_probe,
  900. .remove = realtek_remove,
  901. };