You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

realtek.c 30KB

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