rtl8169.c 23 KB

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