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.

w89c840.c 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Etherboot - BOOTP/TFTP Bootstrap Program
  3. *
  4. * w89c840.c -- This file implements the winbond-840 driver for etherboot.
  5. *
  6. */
  7. /*
  8. * Adapted by Igor V. Kovalenko
  9. * -- <garrison@mail.ru>
  10. * OR
  11. * -- <iko@crec.mipt.ru>
  12. * Initial adaptaion stage, including testing, completed 23 August 2000.
  13. */
  14. /*
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. /*
  30. * date version by what
  31. * Written: Aug 20 2000 V0.10 iko Initial revision.
  32. * changes: Aug 22 2000 V0.90 iko Works!
  33. * Aug 23 2000 V0.91 iko Cleanup, posted to etherboot
  34. * maintainer.
  35. * Aug 26 2000 V0.92 iko Fixed Rx ring handling.
  36. * First Linux Kernel (TM)
  37. * successfully loaded using
  38. * this driver.
  39. * Jan 07 2001 V0.93 iko Transmitter timeouts are handled
  40. * using timer2 routines. Proposed
  41. * by Ken Yap to eliminate CPU speed
  42. * dependency.
  43. * Dec 12 2003 V0.94 timlegge Fixed issues in 5.2, removed
  44. * interrupt usage, enabled
  45. * multicast support
  46. *
  47. * This is the etherboot driver for cards based on Winbond W89c840F chip.
  48. *
  49. * It was written from skeleton source, with Donald Becker's winbond-840.c
  50. * kernel driver as a guideline. Mostly the w89c840 related definitions
  51. * and the lower level routines have been cut-and-pasted into this source.
  52. *
  53. * Frankly speaking, about 90% of the code was obtained using cut'n'paste
  54. * sequence :) while the remainder appeared while brainstorming
  55. * Linux Kernel 2.4.0-testX source code. Thanks, Donald and Linus!
  56. *
  57. * There was a demand for using this card in a rather large
  58. * remote boot environment at MSKP OVTI Lab of
  59. * Moscow Institute for Physics and Technology (MIPT) -- http://www.mipt.ru/
  60. * so you may count that for motivation.
  61. *
  62. */
  63. /*
  64. * If you want to see debugging output then define W89C840_DEBUG
  65. */
  66. /*
  67. #define W89C840_DEBUG
  68. */
  69. /*
  70. * Keep using IO_OPS for Etherboot driver!
  71. */
  72. #define USE_IO_OPS
  73. #include "etherboot.h"
  74. #include "nic.h"
  75. #include <gpxe/pci.h>
  76. #include <gpxe/ethernet.h>
  77. #include "timer.h"
  78. static const char *w89c840_version = "driver Version 0.94 - December 12, 2003";
  79. /* Linux support functions */
  80. #define virt_to_le32desc(addr) virt_to_bus(addr)
  81. #define le32desc_to_virt(addr) bus_to_virt(addr)
  82. /*
  83. #define cpu_to_le32(val) (val)
  84. #define le32_to_cpu(val) (val)
  85. */
  86. /* Operational parameters that are set at compile time. */
  87. /* Keep the ring sizes a power of two for compile efficiency.
  88. The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  89. Making the Tx ring too large decreases the effectiveness of channel
  90. bonding and packet priority.
  91. There are no ill effects from too-large receive rings. */
  92. #define TX_RING_SIZE 2
  93. #define RX_RING_SIZE 2
  94. /* The presumed FIFO size for working around the Tx-FIFO-overflow bug.
  95. To avoid overflowing we don't queue again until we have room for a
  96. full-size packet.
  97. */
  98. #define TX_FIFO_SIZE (2048)
  99. #define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)
  100. /* Operational parameters that usually are not changed. */
  101. /* Time in jiffies before concluding the transmitter is hung. */
  102. #define TX_TIMEOUT (10*TICKS_PER_MS)
  103. #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
  104. /*
  105. * Used to be this much CPU loops on Celeron@400 (?),
  106. * now using real timer and TX_TIMEOUT!
  107. * #define TX_LOOP_COUNT 10000000
  108. */
  109. #if !defined(__OPTIMIZE__)
  110. #warning You must compile this file with the correct options!
  111. #warning See the last lines of the source file.
  112. #error You must compile this driver with "-O".
  113. #endif
  114. enum chip_capability_flags {CanHaveMII=1, HasBrokenTx=2};
  115. #ifdef USE_IO_OPS
  116. #define W840_FLAGS (PCI_USES_IO | PCI_ADDR0 | PCI_USES_MASTER)
  117. #else
  118. #define W840_FLAGS (PCI_USES_MEM | PCI_ADDR1 | PCI_USES_MASTER)
  119. #endif
  120. static u32 driver_flags = CanHaveMII | HasBrokenTx;
  121. /* This driver was written to use PCI memory space, however some x86 systems
  122. work only with I/O space accesses. Pass -DUSE_IO_OPS to use PCI I/O space
  123. accesses instead of memory space. */
  124. #ifdef USE_IO_OPS
  125. #undef readb
  126. #undef readw
  127. #undef readl
  128. #undef writeb
  129. #undef writew
  130. #undef writel
  131. #define readb inb
  132. #define readw inw
  133. #define readl inl
  134. #define writeb outb
  135. #define writew outw
  136. #define writel outl
  137. #endif
  138. /* Offsets to the Command and Status Registers, "CSRs".
  139. While similar to the Tulip, these registers are longword aligned.
  140. Note: It's not useful to define symbolic names for every register bit in
  141. the device. The name can only partially document the semantics and make
  142. the driver longer and more difficult to read.
  143. */
  144. enum w840_offsets {
  145. PCIBusCfg=0x00, TxStartDemand=0x04, RxStartDemand=0x08,
  146. RxRingPtr=0x0C, TxRingPtr=0x10,
  147. IntrStatus=0x14, NetworkConfig=0x18, IntrEnable=0x1C,
  148. RxMissed=0x20, EECtrl=0x24, MIICtrl=0x24, BootRom=0x28, GPTimer=0x2C,
  149. CurRxDescAddr=0x30, CurRxBufAddr=0x34, /* Debug use */
  150. MulticastFilter0=0x38, MulticastFilter1=0x3C, StationAddr=0x40,
  151. CurTxDescAddr=0x4C, CurTxBufAddr=0x50,
  152. };
  153. /* Bits in the interrupt status/enable registers. */
  154. /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
  155. enum intr_status_bits {
  156. NormalIntr=0x10000, AbnormalIntr=0x8000,
  157. IntrPCIErr=0x2000, TimerInt=0x800,
  158. IntrRxDied=0x100, RxNoBuf=0x80, IntrRxDone=0x40,
  159. TxFIFOUnderflow=0x20, RxErrIntr=0x10,
  160. TxIdle=0x04, IntrTxStopped=0x02, IntrTxDone=0x01,
  161. };
  162. /* Bits in the NetworkConfig register. */
  163. enum rx_mode_bits {
  164. AcceptErr=0x80, AcceptRunt=0x40,
  165. AcceptBroadcast=0x20, AcceptMulticast=0x10,
  166. AcceptAllPhys=0x08, AcceptMyPhys=0x02,
  167. };
  168. enum mii_reg_bits {
  169. MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,
  170. MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,
  171. };
  172. /* The Tulip Rx and Tx buffer descriptors. */
  173. struct w840_rx_desc {
  174. s32 status;
  175. s32 length;
  176. u32 buffer1;
  177. u32 next_desc;
  178. };
  179. struct w840_tx_desc {
  180. s32 status;
  181. s32 length;
  182. u32 buffer1, buffer2; /* We use only buffer 1. */
  183. };
  184. /* Bits in network_desc.status */
  185. enum desc_status_bits {
  186. DescOwn=0x80000000, DescEndRing=0x02000000, DescUseLink=0x01000000,
  187. DescWholePkt=0x60000000, DescStartPkt=0x20000000, DescEndPkt=0x40000000,
  188. DescIntr=0x80000000,
  189. };
  190. #define PRIV_ALIGN 15 /* Required alignment mask */
  191. #define PRIV_ALIGN_BYTES 32
  192. static struct winbond_private
  193. {
  194. /* Descriptor rings first for alignment. */
  195. struct w840_rx_desc rx_ring[RX_RING_SIZE];
  196. struct w840_tx_desc tx_ring[TX_RING_SIZE];
  197. struct net_device *next_module; /* Link for devices of this type. */
  198. void *priv_addr; /* Unaligned address for kfree */
  199. const char *product_name;
  200. /* Frequently used values: keep some adjacent for cache effect. */
  201. int chip_id, drv_flags;
  202. struct pci_dev *pci_dev;
  203. int csr6;
  204. struct w840_rx_desc *rx_head_desc;
  205. unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
  206. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  207. unsigned int cur_tx, dirty_tx;
  208. int tx_q_bytes;
  209. unsigned int tx_full:1; /* The Tx queue is full. */
  210. /* These values are keep track of the transceiver/media in use. */
  211. unsigned int full_duplex:1; /* Full-duplex operation requested. */
  212. unsigned int duplex_lock:1;
  213. unsigned int medialock:1; /* Do not sense media. */
  214. unsigned int default_port:4; /* Last dev->if_port value. */
  215. /* MII transceiver section. */
  216. int mii_cnt; /* MII device addresses. */
  217. u16 advertising; /* NWay media advertisement */
  218. unsigned char phys[2]; /* MII device addresses. */
  219. } w840private __attribute__ ((aligned (PRIV_ALIGN_BYTES)));
  220. /* NIC specific static variables go here */
  221. static int ioaddr;
  222. static unsigned short eeprom [0x40];
  223. struct {
  224. char rx_packet[PKT_BUF_SZ * RX_RING_SIZE];
  225. char tx_packet[PKT_BUF_SZ * TX_RING_SIZE];
  226. } w89c840_buf __shared;
  227. static int eeprom_read(long ioaddr, int location);
  228. static int mdio_read(int base_address, int phy_id, int location);
  229. #if 0
  230. static void mdio_write(int base_address, int phy_id, int location, int value);
  231. #endif
  232. static void check_duplex(void);
  233. static void set_rx_mode(void);
  234. static void init_ring(void);
  235. #if defined(W89C840_DEBUG)
  236. static void decode_interrupt(u32 intr_status)
  237. {
  238. printf("Interrupt status: ");
  239. #define TRACE_INTR(_intr_) \
  240. if (intr_status & (_intr_)) { printf (" " #_intr_); }
  241. TRACE_INTR(NormalIntr);
  242. TRACE_INTR(AbnormalIntr);
  243. TRACE_INTR(IntrPCIErr);
  244. TRACE_INTR(TimerInt);
  245. TRACE_INTR(IntrRxDied);
  246. TRACE_INTR(RxNoBuf);
  247. TRACE_INTR(IntrRxDone);
  248. TRACE_INTR(TxFIFOUnderflow);
  249. TRACE_INTR(RxErrIntr);
  250. TRACE_INTR(TxIdle);
  251. TRACE_INTR(IntrTxStopped);
  252. TRACE_INTR(IntrTxDone);
  253. printf("\n");
  254. /*sleep(1);*/
  255. }
  256. #endif
  257. /**************************************************************************
  258. w89c840_reset - Reset adapter
  259. ***************************************************************************/
  260. static void w89c840_reset(struct nic *nic)
  261. {
  262. int i;
  263. /* Reset the chip to erase previous misconfiguration.
  264. No hold time required! */
  265. writel(0x00000001, ioaddr + PCIBusCfg);
  266. init_ring();
  267. writel(virt_to_bus(w840private.rx_ring), ioaddr + RxRingPtr);
  268. writel(virt_to_bus(w840private.tx_ring), ioaddr + TxRingPtr);
  269. for (i = 0; i < ETH_ALEN; i++)
  270. writeb(nic->node_addr[i], ioaddr + StationAddr + i);
  271. /* Initialize other registers. */
  272. /* Configure the PCI bus bursts and FIFO thresholds.
  273. 486: Set 8 longword cache alignment, 8 longword burst.
  274. 586: Set 16 longword cache alignment, no burst limit.
  275. Cache alignment bits 15:14 Burst length 13:8
  276. 0000 <not allowed> 0000 align to cache 0800 8 longwords
  277. 4000 8 longwords 0100 1 longword 1000 16 longwords
  278. 8000 16 longwords 0200 2 longwords 2000 32 longwords
  279. C000 32 longwords 0400 4 longwords
  280. Wait the specified 50 PCI cycles after a reset by initializing
  281. Tx and Rx queues and the address filter list. */
  282. writel(0xE010, ioaddr + PCIBusCfg);
  283. writel(0, ioaddr + RxStartDemand);
  284. w840private.csr6 = 0x20022002;
  285. check_duplex();
  286. set_rx_mode();
  287. /* Do not enable the interrupts Etherboot doesn't need them */
  288. /*
  289. writel(0x1A0F5, ioaddr + IntrStatus);
  290. writel(0x1A0F5, ioaddr + IntrEnable);
  291. */
  292. #if defined(W89C840_DEBUG)
  293. printf("winbond-840 : Done reset.\n");
  294. #endif
  295. }
  296. #if 0
  297. static void handle_intr(u32 intr_stat)
  298. {
  299. if ((intr_stat & (NormalIntr|AbnormalIntr)) == 0) {
  300. /* we are polling, do not return now */
  301. /*return 0;*/
  302. } else {
  303. /* Acknowledge all of the current interrupt sources ASAP. */
  304. writel(intr_stat & 0x001ffff, ioaddr + IntrStatus);
  305. }
  306. if (intr_stat & AbnormalIntr) {
  307. /* There was an abnormal interrupt */
  308. printf("\n-=- Abnormal interrupt.\n");
  309. #if defined(W89C840_DEBUG)
  310. decode_interrupt(intr_stat);
  311. #endif
  312. if (intr_stat & RxNoBuf) {
  313. /* There was an interrupt */
  314. printf("-=- <=> No receive buffers available.\n");
  315. writel(0, ioaddr + RxStartDemand);
  316. }
  317. }
  318. }
  319. #endif
  320. /**************************************************************************
  321. w89c840_poll - Wait for a frame
  322. ***************************************************************************/
  323. static int w89c840_poll(struct nic *nic, int retrieve)
  324. {
  325. /* return true if there's an ethernet packet ready to read */
  326. /* nic->packet should contain data on return */
  327. /* nic->packetlen should contain length of data */
  328. int packet_received = 0;
  329. #if defined(W89C840_DEBUG)
  330. u32 intr_status = readl(ioaddr + IntrStatus);
  331. #endif
  332. do {
  333. /* Code from netdev_rx(dev) */
  334. int entry = w840private.cur_rx % RX_RING_SIZE;
  335. struct w840_rx_desc *desc = w840private.rx_head_desc;
  336. s32 status = desc->status;
  337. if (status & DescOwn) {
  338. /* DescOwn bit is still set, we should wait for RX to complete */
  339. packet_received = 0;
  340. break;
  341. }
  342. if ( !retrieve ) {
  343. packet_received = 1;
  344. break;
  345. }
  346. if ((status & 0x38008300) != 0x0300) {
  347. if ((status & 0x38000300) != 0x0300) {
  348. /* Ingore earlier buffers. */
  349. if ((status & 0xffff) != 0x7fff) {
  350. printf("winbond-840 : Oversized Ethernet frame spanned "
  351. "multiple buffers, entry %d status %X !\n",
  352. w840private.cur_rx, (unsigned int) status);
  353. }
  354. } else if (status & 0x8000) {
  355. /* There was a fatal error. */
  356. #if defined(W89C840_DEBUG)
  357. printf("winbond-840 : Receive error, Rx status %X :", status);
  358. if (status & 0x0890) {
  359. printf(" RXLEN_ERROR");
  360. }
  361. if (status & 0x004C) {
  362. printf(", FRAME_ERROR");
  363. }
  364. if (status & 0x0002) {
  365. printf(", CRC_ERROR");
  366. }
  367. printf("\n");
  368. #endif
  369. /* Simpy do a reset now... */
  370. w89c840_reset(nic);
  371. packet_received = 0;
  372. break;
  373. }
  374. } else {
  375. /* Omit the four octet CRC from the length. */
  376. int pkt_len = ((status >> 16) & 0x7ff) - 4;
  377. #if defined(W89C840_DEBUG)
  378. printf(" netdev_rx() normal Rx pkt ring %d length %d status %X\n", entry, pkt_len, status);
  379. #endif
  380. nic->packetlen = pkt_len;
  381. /* Check if the packet is long enough to accept without copying
  382. to a minimally-sized skbuff. */
  383. memcpy(nic->packet, le32desc_to_virt(w840private.rx_ring[entry].buffer1), pkt_len);
  384. packet_received = 1;
  385. /* Release buffer to NIC */
  386. w840private.rx_ring[entry].status = DescOwn;
  387. #if defined(W89C840_DEBUG)
  388. /* You will want this info for the initial debug. */
  389. printf(" Rx data %hhX:%hhX:%hhX:%hhX:%hhX:"
  390. "%hhX %hhX:%hhX:%hhX:%hhX:%hhX:%hhX %hhX%hhX "
  391. "%hhX.%hhX.%hhX.%hhX.\n",
  392. nic->packet[0], nic->packet[1], nic->packet[2], nic->packet[3],
  393. nic->packet[4], nic->packet[5], nic->packet[6], nic->packet[7],
  394. nic->packet[8], nic->packet[9], nic->packet[10],
  395. nic->packet[11], nic->packet[12], nic->packet[13],
  396. nic->packet[14], nic->packet[15], nic->packet[16],
  397. nic->packet[17]);
  398. #endif
  399. }
  400. entry = (++w840private.cur_rx) % RX_RING_SIZE;
  401. w840private.rx_head_desc = &w840private.rx_ring[entry];
  402. } while (0);
  403. return packet_received;
  404. }
  405. /**************************************************************************
  406. w89c840_transmit - Transmit a frame
  407. ***************************************************************************/
  408. static void w89c840_transmit(
  409. struct nic *nic,
  410. const char *d, /* Destination */
  411. unsigned int t, /* Type */
  412. unsigned int s, /* size */
  413. const char *p) /* Packet */
  414. {
  415. /* send the packet to destination */
  416. unsigned entry;
  417. int transmit_status;
  418. /* Caution: the write order is important here, set the field
  419. with the "ownership" bits last. */
  420. /* Fill in our transmit buffer */
  421. entry = w840private.cur_tx % TX_RING_SIZE;
  422. memcpy (w89c840_buf.tx_packet, d, ETH_ALEN); /* dst */
  423. memcpy (w89c840_buf.tx_packet + ETH_ALEN, nic->node_addr, ETH_ALEN);/*src*/
  424. *((char *) w89c840_buf.tx_packet + 12) = t >> 8; /* type */
  425. *((char *) w89c840_buf.tx_packet + 13) = t;
  426. memcpy (w89c840_buf.tx_packet + ETH_HLEN, p, s);
  427. s += ETH_HLEN;
  428. while (s < ETH_ZLEN)
  429. *((char *) w89c840_buf.tx_packet + ETH_HLEN + (s++)) = 0;
  430. w840private.tx_ring[entry].buffer1
  431. = virt_to_le32desc(w89c840_buf.tx_packet);
  432. w840private.tx_ring[entry].length = (DescWholePkt | (u32) s);
  433. if (entry >= TX_RING_SIZE-1) /* Wrap ring */
  434. w840private.tx_ring[entry].length |= (DescIntr | DescEndRing);
  435. w840private.tx_ring[entry].status = (DescOwn);
  436. w840private.cur_tx++;
  437. w840private.tx_q_bytes = (u16) s;
  438. writel(0, ioaddr + TxStartDemand);
  439. /* Work around horrible bug in the chip by marking the queue as full
  440. when we do not have FIFO room for a maximum sized packet. */
  441. if ((w840private.drv_flags & HasBrokenTx) && w840private.tx_q_bytes > TX_BUG_FIFO_LIMIT) {
  442. /* Actually this is left to help finding error tails later in debugging...
  443. * See Linux kernel driver in winbond-840.c for details.
  444. */
  445. w840private.tx_full = 1;
  446. }
  447. #if defined(W89C840_DEBUG)
  448. printf("winbond-840 : Transmit frame # %d size %d queued in slot %d.\n", w840private.cur_tx, s, entry);
  449. #endif
  450. /* Now wait for TX to complete. */
  451. transmit_status = w840private.tx_ring[entry].status;
  452. load_timer2(TX_TIMEOUT);
  453. {
  454. #if defined W89C840_DEBUG
  455. u32 intr_stat = 0;
  456. #endif
  457. while (1) {
  458. #if defined(W89C840_DEBUG)
  459. decode_interrupt(intr_stat);
  460. #endif
  461. while ( (transmit_status & DescOwn) && timer2_running()) {
  462. transmit_status = w840private.tx_ring[entry].status;
  463. }
  464. break;
  465. }
  466. }
  467. if ((transmit_status & DescOwn) == 0) {
  468. #if defined(W89C840_DEBUG)
  469. printf("winbond-840 : transmission complete after wait loop iterations, status %X\n",
  470. w840private.tx_ring[entry].status);
  471. #endif
  472. return;
  473. }
  474. /* Transmit timed out... */
  475. printf("winbond-840 : transmission TIMEOUT : status %X\n",
  476. (unsigned int) w840private.tx_ring[entry].status);
  477. return;
  478. }
  479. /**************************************************************************
  480. w89c840_disable - Turn off ethernet interface
  481. ***************************************************************************/
  482. static void w89c840_disable ( struct nic *nic ) {
  483. w89c840_reset(nic);
  484. /* Don't know what to do to disable the board. Is this needed at all? */
  485. /* Yes, a live NIC can corrupt the loaded memory later [Ken] */
  486. /* Stop the chip's Tx and Rx processes. */
  487. writel(w840private.csr6 &= ~0x20FA, ioaddr + NetworkConfig);
  488. }
  489. /**************************************************************************
  490. w89c840_irq - Enable, Disable, or Force interrupts
  491. ***************************************************************************/
  492. static void w89c840_irq(struct nic *nic __unused, irq_action_t action __unused)
  493. {
  494. switch ( action ) {
  495. case DISABLE :
  496. break;
  497. case ENABLE :
  498. break;
  499. case FORCE :
  500. break;
  501. }
  502. }
  503. static struct nic_operations w89c840_operations = {
  504. .connect = dummy_connect,
  505. .poll = w89c840_poll,
  506. .transmit = w89c840_transmit,
  507. .irq = w89c840_irq,
  508. };
  509. static struct pci_device_id w89c840_nics[] = {
  510. PCI_ROM(0x1050, 0x0840, "winbond840", "Winbond W89C840F"),
  511. PCI_ROM(0x11f6, 0x2011, "compexrl100atx", "Compex RL100ATX"),
  512. };
  513. PCI_DRIVER ( w89c840_driver, w89c840_nics, PCI_NO_CLASS );
  514. /**************************************************************************
  515. w89c840_probe - Look for an adapter, this routine's visible to the outside
  516. ***************************************************************************/
  517. static int w89c840_probe ( struct nic *nic, struct pci_device *p ) {
  518. u16 sum = 0;
  519. int i, j;
  520. unsigned short value;
  521. if (p->ioaddr == 0)
  522. return 0;
  523. nic->ioaddr = p->ioaddr;
  524. nic->irqno = 0;
  525. #if defined(W89C840_DEBUG)
  526. printf("winbond-840: PCI bus %hhX device function %hhX: I/O address: %hX\n", p->bus, p->devfn, ioaddr);
  527. #endif
  528. ioaddr = ioaddr & ~3; /* Mask the bit that says "this is an io addr" */
  529. #define PCI_DEVICE_ID_WINBOND2_89C840 0x0840
  530. #define PCI_DEVICE_ID_COMPEX_RL100ATX 0x2011
  531. /* From Matt Hortman <mbhortman@acpthinclient.com> */
  532. if (p->vendor == PCI_VENDOR_ID_WINBOND2
  533. && p->device == PCI_DEVICE_ID_WINBOND2_89C840) {
  534. /* detected "Winbond W89c840 Fast Ethernet PCI NIC" */
  535. } else if ( p->vendor == PCI_VENDOR_ID_COMPEX
  536. && p->device == PCI_DEVICE_ID_COMPEX_RL100ATX) {
  537. /* detected "Compex RL100ATX Fast Ethernet PCI NIC" */
  538. } else {
  539. /* Gee, guess what? They missed again. */
  540. printf("device ID : %X - is not a Compex RL100ATX NIC.\n",
  541. p->device);
  542. return 0;
  543. }
  544. printf(" %s\n", w89c840_version);
  545. adjust_pci_device(p);
  546. /* Ok. Got one. Read the eeprom. */
  547. for (j = 0, i = 0; i < 0x40; i++) {
  548. value = eeprom_read(ioaddr, i);
  549. eeprom[i] = value;
  550. sum += value;
  551. }
  552. for (i=0;i<ETH_ALEN;i++) {
  553. nic->node_addr[i] = (eeprom[i/2] >> (8*(i&1))) & 0xff;
  554. }
  555. DBG ( "Ethernet addr: %s\n", eth_ntoa ( nic->node_addr ) );
  556. #if defined(W89C840_DEBUG)
  557. printf("winbond-840: EEPROM checksum %hX, got eeprom", sum);
  558. #endif
  559. /* Reset the chip to erase previous misconfiguration.
  560. No hold time required! */
  561. writel(0x00000001, ioaddr + PCIBusCfg);
  562. if (driver_flags & CanHaveMII) {
  563. int phy, phy_idx = 0;
  564. for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
  565. int mii_status = mdio_read(ioaddr, phy, 1);
  566. if (mii_status != 0xffff && mii_status != 0x0000) {
  567. w840private.phys[phy_idx++] = phy;
  568. w840private.advertising = mdio_read(ioaddr, phy, 4);
  569. #if defined(W89C840_DEBUG)
  570. printf("winbond-840 : MII PHY found at address %d, status "
  571. "%X advertising %hX.\n", phy, mii_status, w840private.advertising);
  572. #endif
  573. }
  574. }
  575. w840private.mii_cnt = phy_idx;
  576. if (phy_idx == 0) {
  577. printf("winbond-840 : MII PHY not found -- this device may not operate correctly.\n");
  578. }
  579. }
  580. /* point to NIC specific routines */
  581. nic->nic_op = &w89c840_operations;
  582. w89c840_reset(nic);
  583. return 1;
  584. }
  585. /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. These are
  586. often serial bit streams generated by the host processor.
  587. The example below is for the common 93c46 EEPROM, 64 16 bit words. */
  588. /* Delay between EEPROM clock transitions.
  589. No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
  590. a delay. Note that pre-2.0.34 kernels had a cache-alignment bug that
  591. made udelay() unreliable.
  592. The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
  593. depricated.
  594. */
  595. #define eeprom_delay(ee_addr) readl(ee_addr)
  596. enum EEPROM_Ctrl_Bits {
  597. EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
  598. EE_ChipSelect=0x801, EE_DataIn=0x08,
  599. };
  600. /* The EEPROM commands include the alway-set leading bit. */
  601. enum EEPROM_Cmds {
  602. EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
  603. };
  604. static int eeprom_read(long addr, int location)
  605. {
  606. int i;
  607. int retval = 0;
  608. int ee_addr = addr + EECtrl;
  609. int read_cmd = location | EE_ReadCmd;
  610. writel(EE_ChipSelect, ee_addr);
  611. /* Shift the read command bits out. */
  612. for (i = 10; i >= 0; i--) {
  613. short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
  614. writel(dataval, ee_addr);
  615. eeprom_delay(ee_addr);
  616. writel(dataval | EE_ShiftClk, ee_addr);
  617. eeprom_delay(ee_addr);
  618. }
  619. writel(EE_ChipSelect, ee_addr);
  620. for (i = 16; i > 0; i--) {
  621. writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
  622. eeprom_delay(ee_addr);
  623. retval = (retval << 1) | ((readl(ee_addr) & EE_DataIn) ? 1 : 0);
  624. writel(EE_ChipSelect, ee_addr);
  625. eeprom_delay(ee_addr);
  626. }
  627. /* Terminate the EEPROM access. */
  628. writel(0, ee_addr);
  629. return retval;
  630. }
  631. /* MII transceiver control section.
  632. Read and write the MII registers using software-generated serial
  633. MDIO protocol. See the MII specifications or DP83840A data sheet
  634. for details.
  635. The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
  636. met by back-to-back 33Mhz PCI cycles. */
  637. #define mdio_delay(mdio_addr) readl(mdio_addr)
  638. /* Set iff a MII transceiver on any interface requires mdio preamble.
  639. This only set with older tranceivers, so the extra
  640. code size of a per-interface flag is not worthwhile. */
  641. static char mii_preamble_required = 1;
  642. #define MDIO_WRITE0 (MDIO_EnbOutput)
  643. #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
  644. /* Generate the preamble required for initial synchronization and
  645. a few older transceivers. */
  646. static void mdio_sync(long mdio_addr)
  647. {
  648. int bits = 32;
  649. /* Establish sync by sending at least 32 logic ones. */
  650. while (--bits >= 0) {
  651. writel(MDIO_WRITE1, mdio_addr);
  652. mdio_delay(mdio_addr);
  653. writel(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
  654. mdio_delay(mdio_addr);
  655. }
  656. }
  657. static int mdio_read(int base_address, int phy_id, int location)
  658. {
  659. long mdio_addr = base_address + MIICtrl;
  660. int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
  661. int i, retval = 0;
  662. if (mii_preamble_required)
  663. mdio_sync(mdio_addr);
  664. /* Shift the read command bits out. */
  665. for (i = 15; i >= 0; i--) {
  666. int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  667. writel(dataval, mdio_addr);
  668. mdio_delay(mdio_addr);
  669. writel(dataval | MDIO_ShiftClk, mdio_addr);
  670. mdio_delay(mdio_addr);
  671. }
  672. /* Read the two transition, 16 data, and wire-idle bits. */
  673. for (i = 20; i > 0; i--) {
  674. writel(MDIO_EnbIn, mdio_addr);
  675. mdio_delay(mdio_addr);
  676. retval = (retval << 1) | ((readl(mdio_addr) & MDIO_DataIn) ? 1 : 0);
  677. writel(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
  678. mdio_delay(mdio_addr);
  679. }
  680. return (retval>>1) & 0xffff;
  681. }
  682. #if 0
  683. static void mdio_write(int base_address, int phy_id, int location, int value)
  684. {
  685. long mdio_addr = base_address + MIICtrl;
  686. int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
  687. int i;
  688. if (location == 4 && phy_id == w840private.phys[0])
  689. w840private.advertising = value;
  690. if (mii_preamble_required)
  691. mdio_sync(mdio_addr);
  692. /* Shift the command bits out. */
  693. for (i = 31; i >= 0; i--) {
  694. int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  695. writel(dataval, mdio_addr);
  696. mdio_delay(mdio_addr);
  697. writel(dataval | MDIO_ShiftClk, mdio_addr);
  698. mdio_delay(mdio_addr);
  699. }
  700. /* Clear out extra bits. */
  701. for (i = 2; i > 0; i--) {
  702. writel(MDIO_EnbIn, mdio_addr);
  703. mdio_delay(mdio_addr);
  704. writel(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
  705. mdio_delay(mdio_addr);
  706. }
  707. return;
  708. }
  709. #endif
  710. static void check_duplex(void)
  711. {
  712. int mii_reg5 = mdio_read(ioaddr, w840private.phys[0], 5);
  713. int negotiated = mii_reg5 & w840private.advertising;
  714. int duplex;
  715. if (w840private.duplex_lock || mii_reg5 == 0xffff)
  716. return;
  717. duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
  718. if (w840private.full_duplex != duplex) {
  719. w840private.full_duplex = duplex;
  720. #if defined(W89C840_DEBUG)
  721. printf("winbond-840 : Setting %s-duplex based on MII # %d negotiated capability %X\n",
  722. duplex ? "full" : "half", w840private.phys[0], negotiated);
  723. #endif
  724. w840private.csr6 &= ~0x200;
  725. w840private.csr6 |= duplex ? 0x200 : 0;
  726. }
  727. }
  728. static void set_rx_mode(void)
  729. {
  730. u32 mc_filter[2]; /* Multicast hash filter */
  731. u32 rx_mode;
  732. /* Accept all multicasts from now on. */
  733. memset(mc_filter, 0xff, sizeof(mc_filter));
  734. /*
  735. * works OK with multicast enabled.
  736. */
  737. rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
  738. writel(mc_filter[0], ioaddr + MulticastFilter0);
  739. writel(mc_filter[1], ioaddr + MulticastFilter1);
  740. w840private.csr6 &= ~0x00F8;
  741. w840private.csr6 |= rx_mode;
  742. writel(w840private.csr6, ioaddr + NetworkConfig);
  743. #if defined(W89C840_DEBUG)
  744. printf("winbond-840 : Done setting RX mode.\n");
  745. #endif
  746. }
  747. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  748. static void init_ring(void)
  749. {
  750. int i;
  751. char * p;
  752. w840private.tx_full = 0;
  753. w840private.tx_q_bytes = w840private.cur_rx = w840private.cur_tx = 0;
  754. w840private.dirty_rx = w840private.dirty_tx = 0;
  755. w840private.rx_buf_sz = PKT_BUF_SZ;
  756. w840private.rx_head_desc = &w840private.rx_ring[0];
  757. /* Initial all Rx descriptors. Fill in the Rx buffers. */
  758. p = &w89c840_buf.rx_packet[0];
  759. for (i = 0; i < RX_RING_SIZE; i++) {
  760. w840private.rx_ring[i].length = w840private.rx_buf_sz;
  761. w840private.rx_ring[i].status = 0;
  762. w840private.rx_ring[i].next_desc = virt_to_le32desc(&w840private.rx_ring[i+1]);
  763. w840private.rx_ring[i].buffer1 = virt_to_le32desc(p + (PKT_BUF_SZ * i));
  764. w840private.rx_ring[i].status = DescOwn | DescIntr;
  765. }
  766. /* Mark the last entry as wrapping the ring. */
  767. w840private.rx_ring[i-1].length |= DescEndRing;
  768. w840private.rx_ring[i-1].next_desc = virt_to_le32desc(&w840private.rx_ring[0]);
  769. w840private.dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  770. for (i = 0; i < TX_RING_SIZE; i++) {
  771. w840private.tx_ring[i].status = 0;
  772. }
  773. return;
  774. }
  775. DRIVER ( "W89C840F", nic_driver, pci_driver, w89c840_driver,
  776. w89c840_probe, w89c840_disable );
  777. /*
  778. * Local variables:
  779. * c-basic-offset: 8
  780. * c-indent-level: 8
  781. * tab-width: 8
  782. * End:
  783. */