rtl8169.c 22 KB

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