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.

r8169.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /**************************************************************************
  2. * r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
  3. * Written 2003 by Timothy Legge <tlegge@rogers.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Portions of this code based on:
  20. * r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
  21. * for Linux kernel 2.4.x.
  22. *
  23. * Written 2002 ShuChen <shuchen@realtek.com.tw>
  24. * See Linux Driver for full information
  25. *
  26. * Linux Driver Version 1.27a, 10.02.2002
  27. *
  28. * Thanks to:
  29. * Jean Chen of RealTek Semiconductor Corp. for
  30. * providing the evaluation NIC used to develop
  31. * this driver. RealTek's support for Etherboot
  32. * is appreciated.
  33. *
  34. * REVISION HISTORY:
  35. * ================
  36. *
  37. * v1.0 11-26-2003 timlegge Initial port of Linux driver
  38. * v1.5 01-17-2004 timlegge Initial driver output cleanup
  39. * v1.6 03-27-2004 timlegge Additional Cleanup
  40. *
  41. * Indent Options: indent -kr -i8
  42. ***************************************************************************/
  43. /* to get some global routines like printf */
  44. #include "etherboot.h"
  45. /* to get the interface to the body of the program */
  46. #include "nic.h"
  47. /* to get the PCI support functions, if this is a PCI NIC */
  48. #include "pci.h"
  49. #include "timer.h"
  50. #define drv_version "v1.6"
  51. #define drv_date "03-27-2004"
  52. typedef unsigned char u8;
  53. typedef signed char s8;
  54. typedef unsigned short u16;
  55. typedef signed short s16;
  56. typedef unsigned int u32;
  57. typedef signed int s32;
  58. #define HZ 1000
  59. static u32 ioaddr;
  60. #ifdef EDEBUG
  61. #define dprintf(x) printf x
  62. #else
  63. #define dprintf(x)
  64. #endif
  65. /* Condensed operations for readability. */
  66. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  67. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  68. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  69. /* media options
  70. _10_Half = 0x01,
  71. _10_Full = 0x02,
  72. _100_Half = 0x04,
  73. _100_Full = 0x08,
  74. _1000_Full = 0x10,
  75. */
  76. static int media = -1;
  77. #if 0
  78. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  79. static int max_interrupt_work = 20;
  80. #endif
  81. #if 0
  82. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  83. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  84. static int multicast_filter_limit = 32;
  85. #endif
  86. /* MAC address length*/
  87. #define MAC_ADDR_LEN 6
  88. /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
  89. #define MAX_ETH_FRAME_SIZE 1536
  90. #define TX_FIFO_THRESH 256 /* In bytes */
  91. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  92. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  93. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  94. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  95. #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
  96. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  97. #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
  98. #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
  99. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  100. #define RTL_MIN_IO_SIZE 0x80
  101. #define TX_TIMEOUT (6*HZ)
  102. /* write/read MMIO register */
  103. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  104. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  105. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  106. #define RTL_R8(reg) readb (ioaddr + (reg))
  107. #define RTL_R16(reg) readw (ioaddr + (reg))
  108. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  109. enum RTL8169_registers {
  110. MAC0 = 0, /* Ethernet hardware address. */
  111. MAR0 = 8, /* Multicast filter. */
  112. TxDescStartAddr = 0x20,
  113. TxHDescStartAddr = 0x28,
  114. FLASH = 0x30,
  115. ERSR = 0x36,
  116. ChipCmd = 0x37,
  117. TxPoll = 0x38,
  118. IntrMask = 0x3C,
  119. IntrStatus = 0x3E,
  120. TxConfig = 0x40,
  121. RxConfig = 0x44,
  122. RxMissed = 0x4C,
  123. Cfg9346 = 0x50,
  124. Config0 = 0x51,
  125. Config1 = 0x52,
  126. Config2 = 0x53,
  127. Config3 = 0x54,
  128. Config4 = 0x55,
  129. Config5 = 0x56,
  130. MultiIntr = 0x5C,
  131. PHYAR = 0x60,
  132. TBICSR = 0x64,
  133. TBI_ANAR = 0x68,
  134. TBI_LPAR = 0x6A,
  135. PHYstatus = 0x6C,
  136. RxMaxSize = 0xDA,
  137. CPlusCmd = 0xE0,
  138. RxDescStartAddr = 0xE4,
  139. EarlyTxThres = 0xEC,
  140. FuncEvent = 0xF0,
  141. FuncEventMask = 0xF4,
  142. FuncPresetState = 0xF8,
  143. FuncForceEvent = 0xFC,
  144. };
  145. enum RTL8169_register_content {
  146. /*InterruptStatusBits */
  147. SYSErr = 0x8000,
  148. PCSTimeout = 0x4000,
  149. SWInt = 0x0100,
  150. TxDescUnavail = 0x80,
  151. RxFIFOOver = 0x40,
  152. RxUnderrun = 0x20,
  153. RxOverflow = 0x10,
  154. TxErr = 0x08,
  155. TxOK = 0x04,
  156. RxErr = 0x02,
  157. RxOK = 0x01,
  158. /*RxStatusDesc */
  159. RxRES = 0x00200000,
  160. RxCRC = 0x00080000,
  161. RxRUNT = 0x00100000,
  162. RxRWT = 0x00400000,
  163. /*ChipCmdBits */
  164. CmdReset = 0x10,
  165. CmdRxEnb = 0x08,
  166. CmdTxEnb = 0x04,
  167. RxBufEmpty = 0x01,
  168. /*Cfg9346Bits */
  169. Cfg9346_Lock = 0x00,
  170. Cfg9346_Unlock = 0xC0,
  171. /*rx_mode_bits */
  172. AcceptErr = 0x20,
  173. AcceptRunt = 0x10,
  174. AcceptBroadcast = 0x08,
  175. AcceptMulticast = 0x04,
  176. AcceptMyPhys = 0x02,
  177. AcceptAllPhys = 0x01,
  178. /*RxConfigBits */
  179. RxCfgFIFOShift = 13,
  180. RxCfgDMAShift = 8,
  181. /*TxConfigBits */
  182. TxInterFrameGapShift = 24,
  183. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  184. /*rtl8169_PHYstatus */
  185. TBI_Enable = 0x80,
  186. TxFlowCtrl = 0x40,
  187. RxFlowCtrl = 0x20,
  188. _1000bpsF = 0x10,
  189. _100bps = 0x08,
  190. _10bps = 0x04,
  191. LinkStatus = 0x02,
  192. FullDup = 0x01,
  193. /*GIGABIT_PHY_registers */
  194. PHY_CTRL_REG = 0,
  195. PHY_STAT_REG = 1,
  196. PHY_AUTO_NEGO_REG = 4,
  197. PHY_1000_CTRL_REG = 9,
  198. /*GIGABIT_PHY_REG_BIT */
  199. PHY_Restart_Auto_Nego = 0x0200,
  200. PHY_Enable_Auto_Nego = 0x1000,
  201. /* PHY_STAT_REG = 1; */
  202. PHY_Auto_Neco_Comp = 0x0020,
  203. /* PHY_AUTO_NEGO_REG = 4; */
  204. PHY_Cap_10_Half = 0x0020,
  205. PHY_Cap_10_Full = 0x0040,
  206. PHY_Cap_100_Half = 0x0080,
  207. PHY_Cap_100_Full = 0x0100,
  208. /* PHY_1000_CTRL_REG = 9; */
  209. PHY_Cap_1000_Full = 0x0200,
  210. PHY_Cap_Null = 0x0,
  211. /*_MediaType*/
  212. _10_Half = 0x01,
  213. _10_Full = 0x02,
  214. _100_Half = 0x04,
  215. _100_Full = 0x08,
  216. _1000_Full = 0x10,
  217. /*_TBICSRBit*/
  218. TBILinkOK = 0x02000000,
  219. };
  220. static struct {
  221. const char *name;
  222. u8 version; /* depend on RTL8169 docs */
  223. u32 RxConfigMask; /* should clear the bits supported by this chip */
  224. } rtl_chip_info[] = {
  225. {
  226. "RTL-8169", 0x00, 0xff7e1880,},};
  227. enum _DescStatusBit {
  228. OWNbit = 0x80000000,
  229. EORbit = 0x40000000,
  230. FSbit = 0x20000000,
  231. LSbit = 0x10000000,
  232. };
  233. struct TxDesc {
  234. u32 status;
  235. u32 vlan_tag;
  236. u32 buf_addr;
  237. u32 buf_Haddr;
  238. };
  239. struct RxDesc {
  240. u32 status;
  241. u32 vlan_tag;
  242. u32 buf_addr;
  243. u32 buf_Haddr;
  244. };
  245. /* The descriptors for this card are required to be aligned on
  246. 256 byte boundaries. As the align attribute does not do more than
  247. 16 bytes of alignment it requires some extra steps. Add 256 to the
  248. size of the array and the init_ring adjusts the alignment */
  249. /* Define the TX Descriptor */
  250. static u8 tx_ring[NUM_TX_DESC * sizeof(struct TxDesc) + 256];
  251. /* Create a static buffer of size RX_BUF_SZ for each
  252. TX Descriptor. All descriptors point to a
  253. part of this buffer */
  254. static unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
  255. /* Define the RX Descriptor */
  256. static u8 rx_ring[NUM_RX_DESC * sizeof(struct TxDesc) + 256];
  257. /* Create a static buffer of size RX_BUF_SZ for each
  258. RX Descriptor All descriptors point to a
  259. part of this buffer */
  260. static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
  261. struct rtl8169_private {
  262. void *mmio_addr; /* memory map physical address */
  263. int chipset;
  264. unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  265. unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  266. unsigned char *TxDescArrays; /* Index of Tx Descriptor buffer */
  267. unsigned char *RxDescArrays; /* Index of Rx Descriptor buffer */
  268. struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
  269. struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
  270. unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
  271. unsigned char *Tx_skbuff[NUM_TX_DESC];
  272. } tpx;
  273. static struct rtl8169_private *tpc;
  274. static const u16 rtl8169_intr_mask =
  275. SYSErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver | TxErr |
  276. TxOK | RxErr | RxOK;
  277. static const unsigned int rtl8169_rx_config =
  278. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  279. void mdio_write(int RegAddr, int value)
  280. {
  281. int i;
  282. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  283. udelay(1000);
  284. for (i = 2000; i > 0; i--) {
  285. /* Check if the RTL8169 has completed writing to the specified MII register */
  286. if (!(RTL_R32(PHYAR) & 0x80000000)) {
  287. break;
  288. } else {
  289. udelay(100);
  290. }
  291. }
  292. }
  293. int mdio_read(int RegAddr)
  294. {
  295. int i, value = -1;
  296. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  297. udelay(1000);
  298. for (i = 2000; i > 0; i--) {
  299. /* Check if the RTL8169 has completed retrieving data from the specified MII register */
  300. if (RTL_R32(PHYAR) & 0x80000000) {
  301. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  302. break;
  303. } else {
  304. udelay(100);
  305. }
  306. }
  307. return value;
  308. }
  309. static int rtl8169_init_board(struct pci_device *pdev)
  310. {
  311. int i;
  312. unsigned long rtreg_base, rtreg_len;
  313. u32 tmp;
  314. rtreg_base = pci_bar_start(pdev, PCI_BASE_ADDRESS_1);
  315. rtreg_len = pci_bar_size(pdev, PCI_BASE_ADDRESS_1);
  316. /* check for weird/broken PCI region reporting */
  317. if (rtreg_len < RTL_MIN_IO_SIZE) {
  318. printf("Invalid PCI region size(s), aborting\n");
  319. }
  320. adjust_pci_device(pdev);
  321. /* pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM); */
  322. /* ioremap MMIO region */
  323. ioaddr = (unsigned long) ioremap(rtreg_base, rtreg_len);
  324. if (ioaddr == 0)
  325. return 0;
  326. tpc->mmio_addr = &ioaddr;
  327. /* Soft reset the chip. */
  328. RTL_W8(ChipCmd, CmdReset);
  329. /* Check that the chip has finished the reset. */
  330. for (i = 1000; i > 0; i--)
  331. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  332. break;
  333. else
  334. udelay(10);
  335. /* identify chip attached to board */
  336. tmp = RTL_R32(TxConfig);
  337. tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;
  338. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--)
  339. if (tmp == rtl_chip_info[i].version) {
  340. tpc->chipset = i;
  341. goto match;
  342. }
  343. /* if unknown chip, assume array element #0, original RTL-8169 in this case */
  344. dprintf(("PCI device: unknown chip version, assuming RTL-8169\n"));
  345. dprintf(("PCI device: TxConfig = 0x%hX\n",
  346. (unsigned long) RTL_R32(TxConfig)));
  347. tpc->chipset = 0;
  348. return 1;
  349. match:
  350. return 0;
  351. }
  352. /**************************************************************************
  353. IRQ - Wait for a frame
  354. ***************************************************************************/
  355. void r8169_irq ( struct nic *nic __unused, irq_action_t action ) {
  356. int intr_status = 0;
  357. int interested = RxUnderrun | RxOverflow | RxFIFOOver | RxErr | RxOK;
  358. switch ( action ) {
  359. case DISABLE:
  360. case ENABLE:
  361. intr_status = RTL_R16(IntrStatus);
  362. /* h/w no longer present (hotplug?) or major error,
  363. bail */
  364. if (intr_status == 0xFFFF)
  365. break;
  366. intr_status = intr_status & ~interested;
  367. if ( action == ENABLE )
  368. intr_status = intr_status | interested;
  369. RTL_W16(IntrMask, intr_status);
  370. break;
  371. case FORCE :
  372. RTL_W8(TxPoll, (RTL_R8(TxPoll) | 0x01));
  373. break;
  374. }
  375. }
  376. /**************************************************************************
  377. POLL - Wait for a frame
  378. ***************************************************************************/
  379. static int r8169_poll(struct nic *nic, int retreive)
  380. {
  381. /* return true if there's an ethernet packet ready to read */
  382. /* nic->packet should contain data on return */
  383. /* nic->packetlen should contain length of data */
  384. int cur_rx;
  385. unsigned int intr_status = 0;
  386. cur_rx = tpc->cur_rx;
  387. if ((tpc->RxDescArray[cur_rx].status & OWNbit) == 0) {
  388. /* There is a packet ready */
  389. if(!retreive)
  390. return 1;
  391. intr_status = RTL_R16(IntrStatus);
  392. /* h/w no longer present (hotplug?) or major error,
  393. bail */
  394. if (intr_status == 0xFFFF)
  395. return 0;
  396. RTL_W16(IntrStatus, intr_status &
  397. ~(RxFIFOOver | RxOverflow | RxOK));
  398. if (!(tpc->RxDescArray[cur_rx].status & RxRES)) {
  399. nic->packetlen = (int) (tpc->RxDescArray[cur_rx].
  400. status & 0x00001FFF) - 4;
  401. memcpy(nic->packet, tpc->RxBufferRing[cur_rx],
  402. nic->packetlen);
  403. if (cur_rx == NUM_RX_DESC - 1)
  404. tpc->RxDescArray[cur_rx].status =
  405. (OWNbit | EORbit) + RX_BUF_SIZE;
  406. else
  407. tpc->RxDescArray[cur_rx].status =
  408. OWNbit + RX_BUF_SIZE;
  409. tpc->RxDescArray[cur_rx].buf_addr =
  410. virt_to_bus(tpc->RxBufferRing[cur_rx]);
  411. } else
  412. printf("Error Rx");
  413. /* FIXME: shouldn't I reset the status on an error */
  414. cur_rx = (cur_rx + 1) % NUM_RX_DESC;
  415. tpc->cur_rx = cur_rx;
  416. RTL_W16(IntrStatus, intr_status &
  417. (RxFIFOOver | RxOverflow | RxOK));
  418. return 1;
  419. }
  420. tpc->cur_rx = cur_rx;
  421. /* FIXME: There is no reason to do this as cur_rx did not change */
  422. return (0); /* initially as this is called to flush the input */
  423. }
  424. /**************************************************************************
  425. TRANSMIT - Transmit a frame
  426. ***************************************************************************/
  427. static void r8169_transmit(struct nic *nic, const char *d, /* Destination */
  428. unsigned int t, /* Type */
  429. unsigned int s, /* size */
  430. const char *p)
  431. { /* Packet */
  432. /* send the packet to destination */
  433. u16 nstype;
  434. u32 to;
  435. u8 *ptxb;
  436. int entry = tpc->cur_tx % NUM_TX_DESC;
  437. /* point to the current txb incase multiple tx_rings are used */
  438. ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
  439. memcpy(ptxb, d, ETH_ALEN);
  440. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  441. nstype = htons((u16) t);
  442. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
  443. memcpy(ptxb + ETH_HLEN, p, s);
  444. s += ETH_HLEN;
  445. s &= 0x0FFF;
  446. while (s < ETH_ZLEN)
  447. ptxb[s++] = '\0';
  448. tpc->TxDescArray[entry].buf_addr = virt_to_bus(ptxb);
  449. if (entry != (NUM_TX_DESC - 1))
  450. tpc->TxDescArray[entry].status =
  451. (OWNbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s :
  452. ETH_ZLEN);
  453. else
  454. tpc->TxDescArray[entry].status =
  455. (OWNbit | EORbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s
  456. : ETH_ZLEN);
  457. RTL_W8(TxPoll, 0x40); /* set polling bit */
  458. tpc->cur_tx++;
  459. to = currticks() + TX_TIMEOUT;
  460. while ((tpc->TxDescArray[entry].status & OWNbit) && (currticks() < to)); /* wait */
  461. if (currticks() >= to) {
  462. printf("TX Time Out");
  463. }
  464. }
  465. static void rtl8169_set_rx_mode(struct nic *nic __unused)
  466. {
  467. u32 mc_filter[2]; /* Multicast hash filter */
  468. int rx_mode;
  469. u32 tmp = 0;
  470. /* IFF_ALLMULTI */
  471. /* Too many to filter perfectly -- accept all multicasts. */
  472. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  473. mc_filter[1] = mc_filter[0] = 0xffffffff;
  474. tmp =
  475. rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
  476. rtl_chip_info[tpc->chipset].
  477. RxConfigMask);
  478. RTL_W32(RxConfig, tmp);
  479. RTL_W32(MAR0 + 0, mc_filter[0]);
  480. RTL_W32(MAR0 + 4, mc_filter[1]);
  481. }
  482. static void rtl8169_hw_start(struct nic *nic)
  483. {
  484. u32 i;
  485. /* Soft reset the chip. */
  486. RTL_W8(ChipCmd, CmdReset);
  487. /* Check that the chip has finished the reset. */
  488. for (i = 1000; i > 0; i--) {
  489. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  490. break;
  491. else
  492. udelay(10);
  493. }
  494. RTL_W8(Cfg9346, Cfg9346_Unlock);
  495. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  496. RTL_W8(EarlyTxThres, EarlyTxThld);
  497. /* For gigabit rtl8169 */
  498. RTL_W16(RxMaxSize, RxPacketMaxSize);
  499. /* Set Rx Config register */
  500. i = rtl8169_rx_config | (RTL_R32(RxConfig) &
  501. rtl_chip_info[tpc->chipset].RxConfigMask);
  502. RTL_W32(RxConfig, i);
  503. /* Set DMA burst size and Interframe Gap Time */
  504. RTL_W32(TxConfig,
  505. (TX_DMA_BURST << TxDMAShift) | (InterFrameGap <<
  506. TxInterFrameGapShift));
  507. tpc->cur_rx = 0;
  508. RTL_W32(TxDescStartAddr, virt_to_le32desc(tpc->TxDescArray));
  509. RTL_W32(RxDescStartAddr, virt_to_le32desc(tpc->RxDescArray));
  510. RTL_W8(Cfg9346, Cfg9346_Lock);
  511. udelay(10);
  512. RTL_W32(RxMissed, 0);
  513. rtl8169_set_rx_mode(nic);
  514. /* no early-rx interrupts */
  515. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  516. }
  517. static void rtl8169_init_ring(struct nic *nic __unused)
  518. {
  519. int i;
  520. tpc->cur_rx = 0;
  521. tpc->cur_tx = 0;
  522. memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
  523. memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
  524. for (i = 0; i < NUM_TX_DESC; i++) {
  525. tpc->Tx_skbuff[i] = &txb[i];
  526. }
  527. for (i = 0; i < NUM_RX_DESC; i++) {
  528. if (i == (NUM_RX_DESC - 1))
  529. tpc->RxDescArray[i].status =
  530. (OWNbit | EORbit) + RX_BUF_SIZE;
  531. else
  532. tpc->RxDescArray[i].status = OWNbit + RX_BUF_SIZE;
  533. tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
  534. tpc->RxDescArray[i].buf_addr =
  535. virt_to_bus(tpc->RxBufferRing[i]);
  536. }
  537. }
  538. /**************************************************************************
  539. RESET - Finish setting up the ethernet interface
  540. ***************************************************************************/
  541. static void r8169_reset(struct nic *nic)
  542. {
  543. int i;
  544. u8 diff;
  545. u32 TxPhyAddr, RxPhyAddr;
  546. tpc->TxDescArrays = tx_ring;
  547. if (tpc->TxDescArrays == 0)
  548. printf("Allot Error");
  549. /* Tx Desscriptor needs 256 bytes alignment; */
  550. TxPhyAddr = virt_to_bus(tpc->TxDescArrays);
  551. diff = 256 - (TxPhyAddr - ((TxPhyAddr >> 8) << 8));
  552. TxPhyAddr += diff;
  553. tpc->TxDescArray = (struct TxDesc *) (tpc->TxDescArrays + diff);
  554. tpc->RxDescArrays = rx_ring;
  555. /* Rx Desscriptor needs 256 bytes alignment; */
  556. RxPhyAddr = virt_to_bus(tpc->RxDescArrays);
  557. diff = 256 - (RxPhyAddr - ((RxPhyAddr >> 8) << 8));
  558. RxPhyAddr += diff;
  559. tpc->RxDescArray = (struct RxDesc *) (tpc->RxDescArrays + diff);
  560. if (tpc->TxDescArrays == NULL || tpc->RxDescArrays == NULL) {
  561. printf("Allocate RxDescArray or TxDescArray failed\n");
  562. return;
  563. }
  564. rtl8169_init_ring(nic);
  565. rtl8169_hw_start(nic);
  566. /* Construct a perfect filter frame with the mac address as first match
  567. * and broadcast for all others */
  568. for (i = 0; i < 192; i++)
  569. txb[i] = 0xFF;
  570. txb[0] = nic->node_addr[0];
  571. txb[1] = nic->node_addr[1];
  572. txb[2] = nic->node_addr[2];
  573. txb[3] = nic->node_addr[3];
  574. txb[4] = nic->node_addr[4];
  575. txb[5] = nic->node_addr[5];
  576. }
  577. /**************************************************************************
  578. DISABLE - Turn off ethernet interface
  579. ***************************************************************************/
  580. static void r8169_disable ( struct nic *nic __unused ) {
  581. int i;
  582. /* Stop the chip's Tx and Rx DMA processes. */
  583. RTL_W8(ChipCmd, 0x00);
  584. /* Disable interrupts by clearing the interrupt mask. */
  585. RTL_W16(IntrMask, 0x0000);
  586. RTL_W32(RxMissed, 0);
  587. tpc->TxDescArrays = NULL;
  588. tpc->RxDescArrays = NULL;
  589. tpc->TxDescArray = NULL;
  590. tpc->RxDescArray = NULL;
  591. for (i = 0; i < NUM_RX_DESC; i++) {
  592. tpc->RxBufferRing[i] = NULL;
  593. }
  594. }
  595. static struct nic_operations r8169_operations = {
  596. .connect = dummy_connect,
  597. .poll = r8169_poll,
  598. .transmit = r8169_transmit,
  599. .irq = r8169_irq,
  600. .disable = r8169_disable,
  601. };
  602. static struct pci_id r8169_nics[] = {
  603. PCI_ROM(0x10ec, 0x8169, "r8169", "RealTek RTL8169 Gigabit Ethernet"),
  604. };
  605. static struct pci_driver r8169_driver =
  606. PCI_DRIVER ( "r8169/PCI", r8169_nics, PCI_NO_CLASS );
  607. /**************************************************************************
  608. PROBE - Look for an adapter, this routine's visible to the outside
  609. ***************************************************************************/
  610. #define board_found 1
  611. #define valid_link 0
  612. static int r8169_probe ( struct dev *dev, struct pci_device *pci ) {
  613. struct nic *nic = nic_device ( dev );
  614. static int board_idx = -1;
  615. static int printed_version = 0;
  616. int i, rc;
  617. int option = -1, Cap10_100 = 0, Cap1000 = 0;
  618. if ( ! find_pci_device ( pci, &r8169_driver ) )
  619. return 0;
  620. printf("r8169.c: Found %s, Vendor=%hX Device=%hX\n",
  621. dev->name, pci->vendor, pci->dev_id);
  622. board_idx++;
  623. printed_version = 1;
  624. /* point to private storage */
  625. tpc = &tpx;
  626. rc = rtl8169_init_board(pci); /* Return code is meaningless */
  627. /* Get MAC address. FIXME: read EEPROM */
  628. for (i = 0; i < MAC_ADDR_LEN; i++)
  629. nic->node_addr[i] = RTL_R8(MAC0 + i);
  630. dprintf(("%s: Identified chip type is '%s'.\n", pci->name,
  631. rtl_chip_info[tpc->chipset].name));
  632. /* Print out some hardware info */
  633. printf("%s: %! at ioaddr %hX, ", dev->name, nic->node_addr,
  634. ioaddr);
  635. /* if TBI is not endbled */
  636. if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
  637. int val = mdio_read(PHY_AUTO_NEGO_REG);
  638. option = media;
  639. /* Force RTL8169 in 10/100/1000 Full/Half mode. */
  640. if (option > 0) {
  641. printf(" Force-mode Enabled.\n");
  642. Cap10_100 = 0, Cap1000 = 0;
  643. switch (option) {
  644. case _10_Half:
  645. Cap10_100 = PHY_Cap_10_Half;
  646. Cap1000 = PHY_Cap_Null;
  647. break;
  648. case _10_Full:
  649. Cap10_100 = PHY_Cap_10_Full;
  650. Cap1000 = PHY_Cap_Null;
  651. break;
  652. case _100_Half:
  653. Cap10_100 = PHY_Cap_100_Half;
  654. Cap1000 = PHY_Cap_Null;
  655. break;
  656. case _100_Full:
  657. Cap10_100 = PHY_Cap_100_Full;
  658. Cap1000 = PHY_Cap_Null;
  659. break;
  660. case _1000_Full:
  661. Cap10_100 = PHY_Cap_Null;
  662. Cap1000 = PHY_Cap_1000_Full;
  663. break;
  664. default:
  665. break;
  666. }
  667. /* leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
  668. mdio_write(PHY_AUTO_NEGO_REG,
  669. Cap10_100 | (val & 0x1F));
  670. mdio_write(PHY_1000_CTRL_REG, Cap1000);
  671. } else {
  672. dprintf(("Auto-negotiation Enabled.\n",
  673. pci->name));
  674. /* enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
  675. mdio_write(PHY_AUTO_NEGO_REG,
  676. PHY_Cap_10_Half | PHY_Cap_10_Full |
  677. PHY_Cap_100_Half | PHY_Cap_100_Full |
  678. (val & 0x1F));
  679. /* enable 1000 Full Mode */
  680. mdio_write(PHY_1000_CTRL_REG, PHY_Cap_1000_Full);
  681. }
  682. /* Enable auto-negotiation and restart auto-nigotiation */
  683. mdio_write(PHY_CTRL_REG,
  684. PHY_Enable_Auto_Nego | PHY_Restart_Auto_Nego);
  685. udelay(100);
  686. /* wait for auto-negotiation process */
  687. for (i = 10000; i > 0; i--) {
  688. /* Check if auto-negotiation complete */
  689. if (mdio_read(PHY_STAT_REG) & PHY_Auto_Neco_Comp) {
  690. udelay(100);
  691. option = RTL_R8(PHYstatus);
  692. if (option & _1000bpsF) {
  693. printf
  694. ("1000Mbps Full-duplex operation.\n");
  695. } else {
  696. printf
  697. ("%sMbps %s-duplex operation.\n",
  698. (option & _100bps) ? "100" :
  699. "10",
  700. (option & FullDup) ? "Full" :
  701. "Half");
  702. }
  703. break;
  704. } else {
  705. udelay(100);
  706. }
  707. } /* end for-loop to wait for auto-negotiation process */
  708. } else {
  709. udelay(100);
  710. printf
  711. ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
  712. dev->name,
  713. (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
  714. }
  715. r8169_reset(nic);
  716. /* point to NIC specific routines */
  717. nic->nic_op = &r8169_operations;
  718. nic->irqno = pci->irq;
  719. nic->ioaddr = ioaddr;
  720. return 1;
  721. }
  722. BOOT_DRIVER ( "r8169/PCI", r8169_probe );