選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

realtek.c 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. * Configure PHY for Gigabit operation
  239. *
  240. * @v rtl Realtek device
  241. * @ret rc Return status code
  242. */
  243. static int realtek_phy_speed ( struct realtek_nic *rtl ) {
  244. int ctrl1000;
  245. int rc;
  246. /* Read CTRL1000 register */
  247. ctrl1000 = mii_read ( &rtl->mii, MII_CTRL1000 );
  248. if ( ctrl1000 < 0 ) {
  249. rc = ctrl1000;
  250. DBGC ( rtl, "REALTEK %p could not read CTRL1000: %s\n",
  251. rtl, strerror ( rc ) );
  252. return rc;
  253. }
  254. /* Advertise 1000Mbps speeds */
  255. ctrl1000 |= ( ADVERTISE_1000FULL | ADVERTISE_1000HALF );
  256. if ( ( rc = mii_write ( &rtl->mii, MII_CTRL1000, ctrl1000 ) ) != 0 ) {
  257. DBGC ( rtl, "REALTEK %p could not write CTRL1000: %s\n",
  258. rtl, strerror ( rc ) );
  259. return rc;
  260. }
  261. return 0;
  262. }
  263. /**
  264. * Reset PHY
  265. *
  266. * @v rtl Realtek device
  267. * @ret rc Return status code
  268. */
  269. static int realtek_phy_reset ( struct realtek_nic *rtl ) {
  270. int rc;
  271. /* Do nothing if we have no separate PHY register access */
  272. if ( ! rtl->have_phy_regs )
  273. return 0;
  274. /* Perform MII reset */
  275. if ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) {
  276. DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
  277. rtl, strerror ( rc ) );
  278. return rc;
  279. }
  280. /* Some cards (e.g. RTL8169SC) do not advertise Gigabit by
  281. * default. Try to enable advertisement of Gigabit speeds.
  282. */
  283. if ( ( rc = realtek_phy_speed ( rtl ) ) != 0 ) {
  284. /* Ignore failures, since the register may not be
  285. * present on non-Gigabit PHYs (e.g. RTL8101).
  286. */
  287. }
  288. /* Restart autonegotiation */
  289. if ( ( rc = mii_restart ( &rtl->mii ) ) != 0 ) {
  290. DBGC ( rtl, "REALTEK %p could not restart MII: %s\n",
  291. rtl, strerror ( rc ) );
  292. return rc;
  293. }
  294. return 0;
  295. }
  296. /******************************************************************************
  297. *
  298. * Link state
  299. *
  300. ******************************************************************************
  301. */
  302. /**
  303. * Check link state
  304. *
  305. * @v netdev Network device
  306. */
  307. static void realtek_check_link ( struct net_device *netdev ) {
  308. struct realtek_nic *rtl = netdev->priv;
  309. uint8_t phystatus;
  310. uint8_t msr;
  311. int link_up;
  312. /* Determine link state */
  313. if ( rtl->have_phy_regs ) {
  314. phystatus = readb ( rtl->regs + RTL_PHYSTATUS );
  315. link_up = ( phystatus & RTL_PHYSTATUS_LINKSTS );
  316. DBGC ( rtl, "REALTEK %p PHY status is %02x\n", rtl, phystatus );
  317. } else {
  318. msr = readb ( rtl->regs + RTL_MSR );
  319. link_up = ( ! ( msr & RTL_MSR_LINKB ) );
  320. DBGC ( rtl, "REALTEK %p media status is %02x\n", rtl, msr );
  321. }
  322. /* Report link state */
  323. if ( link_up ) {
  324. netdev_link_up ( netdev );
  325. } else {
  326. netdev_link_down ( netdev );
  327. }
  328. }
  329. /******************************************************************************
  330. *
  331. * Network device interface
  332. *
  333. ******************************************************************************
  334. */
  335. /**
  336. * Create receive buffer (legacy mode)
  337. *
  338. * @v rtl Realtek device
  339. * @ret rc Return status code
  340. */
  341. static int realtek_create_buffer ( struct realtek_nic *rtl ) {
  342. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  343. physaddr_t address;
  344. int rc;
  345. /* Do nothing unless in legacy mode */
  346. if ( ! rtl->legacy )
  347. return 0;
  348. /* Allocate buffer */
  349. rtl->rx_buffer = malloc_dma ( len, RTL_RXBUF_ALIGN );
  350. if ( ! rtl->rx_buffer ) {
  351. rc = -ENOMEM;
  352. goto err_alloc;
  353. }
  354. address = virt_to_bus ( rtl->rx_buffer );
  355. /* Check that card can support address */
  356. if ( address & ~0xffffffffULL ) {
  357. DBGC ( rtl, "REALTEK %p cannot support 64-bit RX buffer "
  358. "address\n", rtl );
  359. rc = -ENOTSUP;
  360. goto err_64bit;
  361. }
  362. /* Program buffer address */
  363. writel ( address, rtl->regs + RTL_RBSTART );
  364. DBGC ( rtl, "REALTEK %p receive buffer is at [%08llx,%08llx,%08llx)\n",
  365. rtl, ( ( unsigned long long ) address ),
  366. ( ( unsigned long long ) address + RTL_RXBUF_LEN ),
  367. ( ( unsigned long long ) address + len ) );
  368. return 0;
  369. err_64bit:
  370. free_dma ( rtl->rx_buffer, len );
  371. rtl->rx_buffer = NULL;
  372. err_alloc:
  373. return rc;
  374. }
  375. /**
  376. * Destroy receive buffer (legacy mode)
  377. *
  378. * @v rtl Realtek device
  379. */
  380. static void realtek_destroy_buffer ( struct realtek_nic *rtl ) {
  381. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  382. /* Do nothing unless in legacy mode */
  383. if ( ! rtl->legacy )
  384. return;
  385. /* Clear buffer address */
  386. writel ( 0, rtl->regs + RTL_RBSTART );
  387. /* Free buffer */
  388. free_dma ( rtl->rx_buffer, len );
  389. rtl->rx_buffer = NULL;
  390. rtl->rx_offset = 0;
  391. }
  392. /**
  393. * Create descriptor ring
  394. *
  395. * @v rtl Realtek device
  396. * @v ring Descriptor ring
  397. * @ret rc Return status code
  398. */
  399. static int realtek_create_ring ( struct realtek_nic *rtl,
  400. struct realtek_ring *ring ) {
  401. physaddr_t address;
  402. /* Do nothing in legacy mode */
  403. if ( rtl->legacy )
  404. return 0;
  405. /* Allocate descriptor ring */
  406. ring->desc = malloc_dma ( ring->len, RTL_RING_ALIGN );
  407. if ( ! ring->desc )
  408. return -ENOMEM;
  409. /* Initialise descriptor ring */
  410. memset ( ring->desc, 0, ring->len );
  411. /* Program ring address */
  412. address = virt_to_bus ( ring->desc );
  413. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  414. rtl->regs + ring->reg + 4 );
  415. writel ( ( address & 0xffffffffUL ), rtl->regs + ring->reg );
  416. DBGC ( rtl, "REALTEK %p ring %02x is at [%08llx,%08llx)\n",
  417. rtl, ring->reg, ( ( unsigned long long ) address ),
  418. ( ( unsigned long long ) address + ring->len ) );
  419. return 0;
  420. }
  421. /**
  422. * Destroy descriptor ring
  423. *
  424. * @v rtl Realtek device
  425. * @v ring Descriptor ring
  426. */
  427. static void realtek_destroy_ring ( struct realtek_nic *rtl,
  428. struct realtek_ring *ring ) {
  429. /* Do nothing in legacy mode */
  430. if ( rtl->legacy )
  431. return;
  432. /* Clear ring address */
  433. writel ( 0, rtl->regs + ring->reg );
  434. writel ( 0, rtl->regs + ring->reg + 4 );
  435. /* Free descriptor ring */
  436. free_dma ( ring->desc, ring->len );
  437. ring->desc = NULL;
  438. ring->prod = 0;
  439. ring->cons = 0;
  440. }
  441. /**
  442. * Refill receive descriptor ring
  443. *
  444. * @v rtl Realtek device
  445. */
  446. static void realtek_refill_rx ( struct realtek_nic *rtl ) {
  447. struct realtek_descriptor *rx;
  448. struct io_buffer *iobuf;
  449. unsigned int rx_idx;
  450. physaddr_t address;
  451. int is_last;
  452. /* Do nothing in legacy mode */
  453. if ( rtl->legacy )
  454. return;
  455. while ( ( rtl->rx.prod - rtl->rx.cons ) < RTL_NUM_RX_DESC ) {
  456. /* Allocate I/O buffer */
  457. iobuf = alloc_iob ( RTL_RX_MAX_LEN );
  458. if ( ! iobuf ) {
  459. /* Wait for next refill */
  460. return;
  461. }
  462. /* Get next receive descriptor */
  463. rx_idx = ( rtl->rx.prod++ % RTL_NUM_RX_DESC );
  464. is_last = ( rx_idx == ( RTL_NUM_RX_DESC - 1 ) );
  465. rx = &rtl->rx.desc[rx_idx];
  466. /* Populate receive descriptor */
  467. address = virt_to_bus ( iobuf->data );
  468. rx->address = cpu_to_le64 ( address );
  469. rx->length = cpu_to_le16 ( RTL_RX_MAX_LEN );
  470. wmb();
  471. rx->flags = ( cpu_to_le16 ( RTL_DESC_OWN ) |
  472. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  473. wmb();
  474. /* Record I/O buffer */
  475. assert ( rtl->rx_iobuf[rx_idx] == NULL );
  476. rtl->rx_iobuf[rx_idx] = iobuf;
  477. DBGC2 ( rtl, "REALTEK %p RX %d is [%llx,%llx)\n", rtl, rx_idx,
  478. ( ( unsigned long long ) address ),
  479. ( ( unsigned long long ) address + RTL_RX_MAX_LEN ) );
  480. }
  481. }
  482. /**
  483. * Open network device
  484. *
  485. * @v netdev Network device
  486. * @ret rc Return status code
  487. */
  488. static int realtek_open ( struct net_device *netdev ) {
  489. struct realtek_nic *rtl = netdev->priv;
  490. uint32_t tcr;
  491. uint32_t rcr;
  492. int rc;
  493. /* Create transmit descriptor ring */
  494. if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
  495. goto err_create_tx;
  496. /* Create receive descriptor ring */
  497. if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
  498. goto err_create_rx;
  499. /* Create receive buffer */
  500. if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
  501. goto err_create_buffer;
  502. /* Accept all packets */
  503. writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
  504. writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
  505. /* Enable transmitter and receiver. RTL8139 requires that
  506. * this happens before writing to RCR.
  507. */
  508. writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
  509. /* Configure transmitter */
  510. tcr = readl ( rtl->regs + RTL_TCR );
  511. tcr &= ~RTL_TCR_MXDMA_MASK;
  512. tcr |= RTL_TCR_MXDMA_DEFAULT;
  513. writel ( tcr, rtl->regs + RTL_TCR );
  514. /* Configure receiver */
  515. rcr = readl ( rtl->regs + RTL_RCR );
  516. rcr &= ~( RTL_RCR_RXFTH_MASK | RTL_RCR_RBLEN_MASK |
  517. RTL_RCR_MXDMA_MASK );
  518. rcr |= ( RTL_RCR_RXFTH_DEFAULT | RTL_RCR_RBLEN_DEFAULT |
  519. RTL_RCR_MXDMA_DEFAULT | RTL_RCR_WRAP | RTL_RCR_AB |
  520. RTL_RCR_AM | RTL_RCR_APM | RTL_RCR_AAP );
  521. writel ( rcr, rtl->regs + RTL_RCR );
  522. /* Fill receive ring */
  523. realtek_refill_rx ( rtl );
  524. /* Update link state */
  525. realtek_check_link ( netdev );
  526. return 0;
  527. realtek_destroy_buffer ( rtl );
  528. err_create_buffer:
  529. realtek_destroy_ring ( rtl, &rtl->rx );
  530. err_create_rx:
  531. realtek_destroy_ring ( rtl, &rtl->tx );
  532. err_create_tx:
  533. return rc;
  534. }
  535. /**
  536. * Close network device
  537. *
  538. * @v netdev Network device
  539. */
  540. static void realtek_close ( struct net_device *netdev ) {
  541. struct realtek_nic *rtl = netdev->priv;
  542. unsigned int i;
  543. /* Disable receiver and transmitter */
  544. writeb ( 0, rtl->regs + RTL_CR );
  545. /* Destroy receive buffer */
  546. realtek_destroy_buffer ( rtl );
  547. /* Destroy receive descriptor ring */
  548. realtek_destroy_ring ( rtl, &rtl->rx );
  549. /* Discard any unused receive buffers */
  550. for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
  551. if ( rtl->rx_iobuf[i] )
  552. free_iob ( rtl->rx_iobuf[i] );
  553. rtl->rx_iobuf[i] = NULL;
  554. }
  555. /* Destroy transmit descriptor ring */
  556. realtek_destroy_ring ( rtl, &rtl->tx );
  557. }
  558. /**
  559. * Transmit packet
  560. *
  561. * @v netdev Network device
  562. * @v iobuf I/O buffer
  563. * @ret rc Return status code
  564. */
  565. static int realtek_transmit ( struct net_device *netdev,
  566. struct io_buffer *iobuf ) {
  567. struct realtek_nic *rtl = netdev->priv;
  568. struct realtek_descriptor *tx;
  569. unsigned int tx_idx;
  570. physaddr_t address;
  571. int is_last;
  572. /* Get next transmit descriptor */
  573. if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
  574. DBGC ( rtl, "REALTEK %p out of transmit descriptors\n", rtl );
  575. return -ENOBUFS;
  576. }
  577. tx_idx = ( rtl->tx.prod++ % RTL_NUM_TX_DESC );
  578. /* Transmit packet */
  579. if ( rtl->legacy ) {
  580. /* Pad and align packet */
  581. iob_pad ( iobuf, ETH_ZLEN );
  582. address = virt_to_bus ( iobuf->data );
  583. /* Check that card can support address */
  584. if ( address & ~0xffffffffULL ) {
  585. DBGC ( rtl, "REALTEK %p cannot support 64-bit TX "
  586. "buffer address\n", rtl );
  587. return -ENOTSUP;
  588. }
  589. /* Add to transmit ring */
  590. writel ( address, rtl->regs + RTL_TSAD ( tx_idx ) );
  591. writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
  592. rtl->regs + RTL_TSD ( tx_idx ) );
  593. } else {
  594. /* Populate transmit descriptor */
  595. address = virt_to_bus ( iobuf->data );
  596. is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
  597. tx = &rtl->tx.desc[tx_idx];
  598. tx->address = cpu_to_le64 ( address );
  599. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  600. wmb();
  601. tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
  602. RTL_DESC_LS ) |
  603. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  604. wmb();
  605. /* Notify card that there are packets ready to transmit */
  606. writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
  607. }
  608. DBGC2 ( rtl, "REALTEK %p TX %d is [%llx,%llx)\n", rtl, tx_idx,
  609. ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ),
  610. ( ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ) +
  611. iob_len ( iobuf ) ) );
  612. return 0;
  613. }
  614. /**
  615. * Poll for completed packets
  616. *
  617. * @v netdev Network device
  618. */
  619. static void realtek_poll_tx ( struct net_device *netdev ) {
  620. struct realtek_nic *rtl = netdev->priv;
  621. struct realtek_descriptor *tx;
  622. unsigned int tx_idx;
  623. /* Check for completed packets */
  624. while ( rtl->tx.cons != rtl->tx.prod ) {
  625. /* Get next transmit descriptor */
  626. tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
  627. /* Stop if descriptor is still in use */
  628. if ( rtl->legacy ) {
  629. /* Check ownership bit in transmit status register */
  630. if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
  631. RTL_TSD_OWN ) )
  632. return;
  633. } else {
  634. /* Check ownership bit in descriptor */
  635. tx = &rtl->tx.desc[tx_idx];
  636. if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  637. return;
  638. }
  639. DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
  640. /* Complete TX descriptor */
  641. netdev_tx_complete_next ( netdev );
  642. rtl->tx.cons++;
  643. }
  644. }
  645. /**
  646. * Poll for received packets (legacy mode)
  647. *
  648. * @v netdev Network device
  649. */
  650. static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
  651. struct realtek_nic *rtl = netdev->priv;
  652. struct realtek_legacy_header *rx;
  653. struct io_buffer *iobuf;
  654. size_t len;
  655. /* Check for received packets */
  656. while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
  657. /* Extract packet from receive buffer */
  658. rx = ( rtl->rx_buffer + rtl->rx_offset );
  659. len = le16_to_cpu ( rx->length );
  660. if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
  661. DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
  662. rtl, rtl->rx_offset, len );
  663. /* Allocate I/O buffer */
  664. iobuf = alloc_iob ( len );
  665. if ( ! iobuf ) {
  666. netdev_rx_err ( netdev, NULL, -ENOMEM );
  667. /* Leave packet for next poll */
  668. break;
  669. }
  670. /* Copy data to I/O buffer */
  671. memcpy ( iob_put ( iobuf, len ), rx->data, len );
  672. iob_unput ( iobuf, 4 /* strip CRC */ );
  673. /* Hand off to network stack */
  674. netdev_rx ( netdev, iobuf );
  675. } else {
  676. DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
  677. rtl, rtl->rx_offset, len,
  678. le16_to_cpu ( rx->status ) );
  679. netdev_rx_err ( netdev, NULL, -EIO );
  680. }
  681. /* Update buffer offset */
  682. rtl->rx_offset = ( rtl->rx_offset + sizeof ( *rx ) + len );
  683. rtl->rx_offset = ( ( rtl->rx_offset + 3 ) & ~3 );
  684. rtl->rx_offset = ( rtl->rx_offset % RTL_RXBUF_LEN );
  685. writew ( ( rtl->rx_offset - 16 ), rtl->regs + RTL_CAPR );
  686. }
  687. }
  688. /**
  689. * Poll for received packets
  690. *
  691. * @v netdev Network device
  692. */
  693. static void realtek_poll_rx ( struct net_device *netdev ) {
  694. struct realtek_nic *rtl = netdev->priv;
  695. struct realtek_descriptor *rx;
  696. struct io_buffer *iobuf;
  697. unsigned int rx_idx;
  698. size_t len;
  699. /* Poll receive buffer if in legacy mode */
  700. if ( rtl->legacy ) {
  701. realtek_legacy_poll_rx ( netdev );
  702. return;
  703. }
  704. /* Check for received packets */
  705. while ( rtl->rx.cons != rtl->rx.prod ) {
  706. /* Get next receive descriptor */
  707. rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
  708. rx = &rtl->rx.desc[rx_idx];
  709. /* Stop if descriptor is still in use */
  710. if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  711. return;
  712. /* Populate I/O buffer */
  713. iobuf = rtl->rx_iobuf[rx_idx];
  714. rtl->rx_iobuf[rx_idx] = NULL;
  715. len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
  716. iob_put ( iobuf, ( len - 4 /* strip CRC */ ) );
  717. DBGC2 ( rtl, "REALTEK %p RX %d complete (length %zd)\n",
  718. rtl, rx_idx, len );
  719. /* Hand off to network stack */
  720. if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
  721. netdev_rx_err ( netdev, iobuf, -EIO );
  722. } else {
  723. netdev_rx ( netdev, iobuf );
  724. }
  725. rtl->rx.cons++;
  726. }
  727. }
  728. /**
  729. * Poll for completed and received packets
  730. *
  731. * @v netdev Network device
  732. */
  733. static void realtek_poll ( struct net_device *netdev ) {
  734. struct realtek_nic *rtl = netdev->priv;
  735. uint16_t isr;
  736. /* Check for and acknowledge interrupts */
  737. isr = readw ( rtl->regs + RTL_ISR );
  738. if ( ! isr )
  739. return;
  740. writew ( isr, rtl->regs + RTL_ISR );
  741. /* Poll for TX completions, if applicable */
  742. if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
  743. realtek_poll_tx ( netdev );
  744. /* Poll for RX completionsm, if applicable */
  745. if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
  746. realtek_poll_rx ( netdev );
  747. /* Check link state, if applicable */
  748. if ( isr & RTL_IRQ_PUN_LINKCHG )
  749. realtek_check_link ( netdev );
  750. /* Refill RX ring */
  751. realtek_refill_rx ( rtl );
  752. }
  753. /**
  754. * Enable or disable interrupts
  755. *
  756. * @v netdev Network device
  757. * @v enable Interrupts should be enabled
  758. */
  759. static void realtek_irq ( struct net_device *netdev, int enable ) {
  760. struct realtek_nic *rtl = netdev->priv;
  761. uint16_t imr;
  762. /* Set interrupt mask */
  763. imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
  764. RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
  765. writew ( imr, rtl->regs + RTL_IMR );
  766. }
  767. /** Realtek network device operations */
  768. static struct net_device_operations realtek_operations = {
  769. .open = realtek_open,
  770. .close = realtek_close,
  771. .transmit = realtek_transmit,
  772. .poll = realtek_poll,
  773. .irq = realtek_irq,
  774. };
  775. /******************************************************************************
  776. *
  777. * PCI interface
  778. *
  779. ******************************************************************************
  780. */
  781. /**
  782. * Detect device type
  783. *
  784. * @v rtl Realtek device
  785. */
  786. static void realtek_detect ( struct realtek_nic *rtl ) {
  787. uint16_t rms;
  788. uint16_t check_rms;
  789. uint16_t cpcr;
  790. uint16_t check_cpcr;
  791. /* The RX Packet Maximum Size register is present only on
  792. * 8169. Try to set to our intended MTU.
  793. */
  794. rms = RTL_RX_MAX_LEN;
  795. writew ( rms, rtl->regs + RTL_RMS );
  796. check_rms = readw ( rtl->regs + RTL_RMS );
  797. /* The C+ Command register is present only on 8169 and 8139C+.
  798. * Try to enable C+ mode and PCI Dual Address Cycle (for
  799. * 64-bit systems), if supported.
  800. *
  801. * Note that enabling DAC seems to cause bizarre behaviour
  802. * (lockups, garbage data on the wire) on some systems, even
  803. * if only 32-bit addresses are used.
  804. */
  805. cpcr = readw ( rtl->regs + RTL_CPCR );
  806. cpcr |= ( RTL_CPCR_MULRW | RTL_CPCR_CPRX | RTL_CPCR_CPTX );
  807. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
  808. cpcr |= RTL_CPCR_DAC;
  809. writew ( cpcr, rtl->regs + RTL_CPCR );
  810. check_cpcr = readw ( rtl->regs + RTL_CPCR );
  811. /* Detect device type */
  812. if ( check_rms == rms ) {
  813. DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
  814. rtl->have_phy_regs = 1;
  815. rtl->tppoll = RTL_TPPOLL_8169;
  816. } else {
  817. if ( ( check_cpcr == cpcr ) && ( cpcr != 0xffff ) ) {
  818. DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
  819. rtl );
  820. rtl->tppoll = RTL_TPPOLL_8139CP;
  821. } else {
  822. DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
  823. rtl );
  824. rtl->legacy = 1;
  825. }
  826. }
  827. }
  828. /**
  829. * Probe PCI device
  830. *
  831. * @v pci PCI device
  832. * @ret rc Return status code
  833. */
  834. static int realtek_probe ( struct pci_device *pci ) {
  835. struct net_device *netdev;
  836. struct realtek_nic *rtl;
  837. unsigned int i;
  838. int rc;
  839. /* Allocate and initialise net device */
  840. netdev = alloc_etherdev ( sizeof ( *rtl ) );
  841. if ( ! netdev ) {
  842. rc = -ENOMEM;
  843. goto err_alloc;
  844. }
  845. netdev_init ( netdev, &realtek_operations );
  846. rtl = netdev->priv;
  847. pci_set_drvdata ( pci, netdev );
  848. netdev->dev = &pci->dev;
  849. memset ( rtl, 0, sizeof ( *rtl ) );
  850. realtek_init_ring ( &rtl->tx, RTL_NUM_TX_DESC, RTL_TNPDS );
  851. realtek_init_ring ( &rtl->rx, RTL_NUM_RX_DESC, RTL_RDSAR );
  852. /* Fix up PCI device */
  853. adjust_pci_device ( pci );
  854. /* Map registers */
  855. rtl->regs = ioremap ( pci->membase, RTL_BAR_SIZE );
  856. /* Reset the NIC */
  857. if ( ( rc = realtek_reset ( rtl ) ) != 0 )
  858. goto err_reset;
  859. /* Detect device type */
  860. realtek_detect ( rtl );
  861. /* Initialise EEPROM */
  862. realtek_init_eeprom ( netdev );
  863. /* Read MAC address from EEPROM */
  864. if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
  865. netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
  866. DBGC ( rtl, "REALTEK %p could not read MAC address: %s\n",
  867. rtl, strerror ( rc ) );
  868. goto err_nvs_read;
  869. }
  870. /* The EEPROM may not be present for onboard NICs. Fall back
  871. * to reading the current ID register value, which will
  872. * hopefully have been programmed by the platform firmware.
  873. */
  874. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  875. DBGC ( rtl, "REALTEK %p seems to have no EEPROM\n", rtl );
  876. for ( i = 0 ; i < ETH_ALEN ; i++ )
  877. netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
  878. }
  879. /* Initialise and reset MII interface */
  880. mii_init ( &rtl->mii, &realtek_mii_operations );
  881. if ( ( rc = realtek_phy_reset ( rtl ) ) != 0 )
  882. goto err_phy_reset;
  883. /* Register network device */
  884. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  885. goto err_register_netdev;
  886. /* Set initial link state */
  887. realtek_check_link ( netdev );
  888. /* Register non-volatile options, if applicable */
  889. if ( rtl->nvo.nvs ) {
  890. if ( ( rc = register_nvo ( &rtl->nvo,
  891. netdev_settings ( netdev ) ) ) != 0)
  892. goto err_register_nvo;
  893. }
  894. return 0;
  895. err_register_nvo:
  896. unregister_netdev ( netdev );
  897. err_register_netdev:
  898. err_phy_reset:
  899. err_nvs_read:
  900. realtek_reset ( rtl );
  901. err_reset:
  902. iounmap ( rtl->regs );
  903. netdev_nullify ( netdev );
  904. netdev_put ( netdev );
  905. err_alloc:
  906. return rc;
  907. }
  908. /**
  909. * Remove PCI device
  910. *
  911. * @v pci PCI device
  912. */
  913. static void realtek_remove ( struct pci_device *pci ) {
  914. struct net_device *netdev = pci_get_drvdata ( pci );
  915. struct realtek_nic *rtl = netdev->priv;
  916. /* Unregister non-volatile options, if applicable */
  917. if ( rtl->nvo.nvs )
  918. unregister_nvo ( &rtl->nvo );
  919. /* Unregister network device */
  920. unregister_netdev ( netdev );
  921. /* Reset card */
  922. realtek_reset ( rtl );
  923. /* Free network device */
  924. iounmap ( rtl->regs );
  925. netdev_nullify ( netdev );
  926. netdev_put ( netdev );
  927. }
  928. /** Realtek PCI device IDs */
  929. static struct pci_device_id realtek_nics[] = {
  930. PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
  931. PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
  932. PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
  933. PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
  934. PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
  935. PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
  936. PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
  937. PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
  938. PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
  939. PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
  940. PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
  941. PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
  942. PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
  943. PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
  944. PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
  945. PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
  946. PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
  947. PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
  948. PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
  949. PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
  950. PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
  951. PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
  952. PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
  953. PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
  954. PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
  955. PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
  956. PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
  957. PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
  958. PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
  959. PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
  960. PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
  961. };
  962. /** Realtek PCI driver */
  963. struct pci_driver realtek_driver __pci_driver = {
  964. .ids = realtek_nics,
  965. .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
  966. .probe = realtek_probe,
  967. .remove = realtek_remove,
  968. };