rtl8169.c 22 KB

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