Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /**************************************************************************
  2. *
  3. * sundance.c -- Etherboot device driver for the Sundance ST201 "Alta".
  4. * Written 2002-2002 by Timothy Legge <tlegge@rogers.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Portions of this code based on:
  21. * sundance.c: A Linux device driver for the Sundance ST201 "Alta"
  22. * Written 1999-2002 by Donald Becker
  23. *
  24. * tulip.c: Tulip and Clone Etherboot Driver
  25. * By Marty Conner
  26. * Copyright (C) 2001 Entity Cyber, Inc.
  27. *
  28. * Linux Driver Version LK1.09a, 10-Jul-2003 (2.4.25)
  29. *
  30. * REVISION HISTORY:
  31. * ================
  32. * v1.1 01-01-2003 timlegge Initial implementation
  33. * v1.7 04-10-2003 timlegge Transfers Linux Kernel (30 sec)
  34. * v1.8 04-13-2003 timlegge Fix multiple transmission bug
  35. * v1.9 08-19-2003 timlegge Support Multicast
  36. * v1.10 01-17-2004 timlegge Initial driver output cleanup
  37. * v1.11 03-21-2004 timlegge Remove unused variables
  38. * v1.12 03-21-2004 timlegge Remove excess MII defines
  39. * v1.13 03-24-2004 timlegge Update to Linux 2.4.25 driver
  40. *
  41. ****************************************************************************/
  42. FILE_LICENCE ( GPL2_OR_LATER );
  43. /* to get some global routines like printf */
  44. #include "etherboot.h"
  45. /* to get the interface to the body of the program */
  46. #include "nic.h"
  47. /* to get the PCI support functions, if this is a PCI NIC */
  48. #include <gpxe/pci.h>
  49. #include "mii.h"
  50. #define drv_version "v1.12"
  51. #define drv_date "2004-03-21"
  52. #define HZ 100
  53. /* Condensed operations for readability. */
  54. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  55. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  56. /* Set the mtu */
  57. static int mtu = 1514;
  58. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
  59. The sundance uses a 64 element hash table based on the Ethernet CRC. */
  60. // static int multicast_filter_limit = 32;
  61. /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  62. Setting to > 1518 effectively disables this feature.
  63. This chip can receive into any byte alignment buffers, so word-oriented
  64. archs do not need a copy-align of the IP header. */
  65. static int rx_copybreak = 0;
  66. static int flowctrl = 1;
  67. /* Allow forcing the media type */
  68. /* media[] specifies the media type the NIC operates at.
  69. autosense Autosensing active media.
  70. 10mbps_hd 10Mbps half duplex.
  71. 10mbps_fd 10Mbps full duplex.
  72. 100mbps_hd 100Mbps half duplex.
  73. 100mbps_fd 100Mbps full duplex.
  74. */
  75. static char media[] = "autosense";
  76. /* Operational parameters that are set at compile time. */
  77. /* As Etherboot uses a Polling driver we can keep the number of rings
  78. to the minimum number required. In general that is 1 transmit and 4 receive receive rings. However some cards require that
  79. there be a minimum of 2 rings */
  80. #define TX_RING_SIZE 2
  81. #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
  82. #define RX_RING_SIZE 4
  83. /* Operational parameters that usually are not changed. */
  84. /* Time in jiffies before concluding the transmitter is hung. */
  85. #define TX_TIME_OUT (4*HZ)
  86. #define PKT_BUF_SZ 1536
  87. /* Offsets to the device registers.
  88. Unlike software-only systems, device drivers interact with complex hardware.
  89. It's not useful to define symbolic names for every register bit in the
  90. device. The name can only partially document the semantics and make
  91. the driver longer and more difficult to read.
  92. In general, only the important configuration values or bits changed
  93. multiple times should be defined symbolically.
  94. */
  95. enum alta_offsets {
  96. DMACtrl = 0x00,
  97. TxListPtr = 0x04,
  98. TxDMABurstThresh = 0x08,
  99. TxDMAUrgentThresh = 0x09,
  100. TxDMAPollPeriod = 0x0a,
  101. RxDMAStatus = 0x0c,
  102. RxListPtr = 0x10,
  103. DebugCtrl0 = 0x1a,
  104. DebugCtrl1 = 0x1c,
  105. RxDMABurstThresh = 0x14,
  106. RxDMAUrgentThresh = 0x15,
  107. RxDMAPollPeriod = 0x16,
  108. LEDCtrl = 0x1a,
  109. ASICCtrl = 0x30,
  110. EEData = 0x34,
  111. EECtrl = 0x36,
  112. TxStartThresh = 0x3c,
  113. RxEarlyThresh = 0x3e,
  114. FlashAddr = 0x40,
  115. FlashData = 0x44,
  116. TxStatus = 0x46,
  117. TxFrameId = 0x47,
  118. DownCounter = 0x18,
  119. IntrClear = 0x4a,
  120. IntrEnable = 0x4c,
  121. IntrStatus = 0x4e,
  122. MACCtrl0 = 0x50,
  123. MACCtrl1 = 0x52,
  124. StationAddr = 0x54,
  125. MaxFrameSize = 0x5A,
  126. RxMode = 0x5c,
  127. MIICtrl = 0x5e,
  128. MulticastFilter0 = 0x60,
  129. MulticastFilter1 = 0x64,
  130. RxOctetsLow = 0x68,
  131. RxOctetsHigh = 0x6a,
  132. TxOctetsLow = 0x6c,
  133. TxOctetsHigh = 0x6e,
  134. TxFramesOK = 0x70,
  135. RxFramesOK = 0x72,
  136. StatsCarrierError = 0x74,
  137. StatsLateColl = 0x75,
  138. StatsMultiColl = 0x76,
  139. StatsOneColl = 0x77,
  140. StatsTxDefer = 0x78,
  141. RxMissed = 0x79,
  142. StatsTxXSDefer = 0x7a,
  143. StatsTxAbort = 0x7b,
  144. StatsBcastTx = 0x7c,
  145. StatsBcastRx = 0x7d,
  146. StatsMcastTx = 0x7e,
  147. StatsMcastRx = 0x7f,
  148. /* Aliased and bogus values! */
  149. RxStatus = 0x0c,
  150. };
  151. enum ASICCtrl_HiWord_bit {
  152. GlobalReset = 0x0001,
  153. RxReset = 0x0002,
  154. TxReset = 0x0004,
  155. DMAReset = 0x0008,
  156. FIFOReset = 0x0010,
  157. NetworkReset = 0x0020,
  158. HostReset = 0x0040,
  159. ResetBusy = 0x0400,
  160. };
  161. /* Bits in the interrupt status/mask registers. */
  162. enum intr_status_bits {
  163. IntrSummary = 0x0001, IntrPCIErr = 0x0002, IntrMACCtrl = 0x0008,
  164. IntrTxDone = 0x0004, IntrRxDone = 0x0010, IntrRxStart = 0x0020,
  165. IntrDrvRqst = 0x0040,
  166. StatsMax = 0x0080, LinkChange = 0x0100,
  167. IntrTxDMADone = 0x0200, IntrRxDMADone = 0x0400,
  168. };
  169. /* Bits in the RxMode register. */
  170. enum rx_mode_bits {
  171. AcceptAllIPMulti = 0x20, AcceptMultiHash = 0x10, AcceptAll = 0x08,
  172. AcceptBroadcast = 0x04, AcceptMulticast = 0x02, AcceptMyPhys =
  173. 0x01,
  174. };
  175. /* Bits in MACCtrl. */
  176. enum mac_ctrl0_bits {
  177. EnbFullDuplex = 0x20, EnbRcvLargeFrame = 0x40,
  178. EnbFlowCtrl = 0x100, EnbPassRxCRC = 0x200,
  179. };
  180. enum mac_ctrl1_bits {
  181. StatsEnable = 0x0020, StatsDisable = 0x0040, StatsEnabled = 0x0080,
  182. TxEnable = 0x0100, TxDisable = 0x0200, TxEnabled = 0x0400,
  183. RxEnable = 0x0800, RxDisable = 0x1000, RxEnabled = 0x2000,
  184. };
  185. /* The Rx and Tx buffer descriptors.
  186. Using only 32 bit fields simplifies software endian correction.
  187. This structure must be aligned, and should avoid spanning cache lines.
  188. */
  189. struct netdev_desc {
  190. u32 next_desc;
  191. u32 status;
  192. u32 addr;
  193. u32 length;
  194. };
  195. /* Bits in netdev_desc.status */
  196. enum desc_status_bits {
  197. DescOwn = 0x8000,
  198. DescEndPacket = 0x4000,
  199. DescEndRing = 0x2000,
  200. LastFrag = 0x80000000,
  201. DescIntrOnTx = 0x8000,
  202. DescIntrOnDMADone = 0x80000000,
  203. DisableAlign = 0x00000001,
  204. };
  205. /**********************************************
  206. * Descriptor Ring and Buffer defination
  207. ***********************************************/
  208. /* Define the TX Descriptor */
  209. static struct netdev_desc tx_ring[TX_RING_SIZE];
  210. /* Define the RX Descriptor */
  211. static struct netdev_desc rx_ring[RX_RING_SIZE];
  212. /* Create a static buffer of size PKT_BUF_SZ for each RX and TX descriptor.
  213. All descriptors point to a part of this buffer */
  214. struct {
  215. unsigned char txb[PKT_BUF_SZ * TX_RING_SIZE];
  216. unsigned char rxb[RX_RING_SIZE * PKT_BUF_SZ];
  217. } rx_tx_buf __shared;
  218. #define rxb rx_tx_buf.rxb
  219. #define txb rx_tx_buf.txb
  220. /* FIXME: Move BASE to the private structure */
  221. static u32 BASE;
  222. #define EEPROM_SIZE 128
  223. enum pci_id_flags_bits {
  224. PCI_USES_IO = 1, PCI_USES_MEM = 2, PCI_USES_MASTER = 4,
  225. PCI_ADDR0 = 0 << 4, PCI_ADDR1 = 1 << 4, PCI_ADDR2 =
  226. 2 << 4, PCI_ADDR3 = 3 << 4,
  227. };
  228. enum chip_capability_flags { CanHaveMII = 1, KendinPktDropBug = 2, };
  229. #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)
  230. #define MII_CNT 4
  231. static struct sundance_private {
  232. const char *nic_name;
  233. /* Frequently used values */
  234. unsigned int cur_rx; /* Producer/consumer ring indicies */
  235. unsigned int mtu;
  236. /* These values keep track of the tranceiver/media in use */
  237. unsigned int flowctrl:1;
  238. unsigned int an_enable:1;
  239. unsigned int speed;
  240. /* MII tranceiver section */
  241. struct mii_if_info mii_if;
  242. int mii_preamble_required;
  243. unsigned char phys[MII_CNT];
  244. unsigned char pci_rev_id;
  245. } sdx;
  246. static struct sundance_private *sdc;
  247. /* Station Address location within the EEPROM */
  248. #define EEPROM_SA_OFFSET 0x10
  249. #define DEFAULT_INTR (IntrRxDMADone | IntrPCIErr | \
  250. IntrDrvRqst | IntrTxDone | StatsMax | \
  251. LinkChange)
  252. static int eeprom_read(long ioaddr, int location);
  253. static int mdio_read(struct nic *nic, int phy_id, unsigned int location);
  254. static void mdio_write(struct nic *nic, int phy_id, unsigned int location,
  255. int value);
  256. static void set_rx_mode(struct nic *nic);
  257. static void check_duplex(struct nic *nic)
  258. {
  259. int mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
  260. int negotiated = mii_lpa & sdc->mii_if.advertising;
  261. int duplex;
  262. /* Force media */
  263. if (!sdc->an_enable || mii_lpa == 0xffff) {
  264. if (sdc->mii_if.full_duplex)
  265. outw(inw(BASE + MACCtrl0) | EnbFullDuplex,
  266. BASE + MACCtrl0);
  267. return;
  268. }
  269. /* Autonegotiation */
  270. duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
  271. if (sdc->mii_if.full_duplex != duplex) {
  272. sdc->mii_if.full_duplex = duplex;
  273. DBG ("%s: Setting %s-duplex based on MII #%d "
  274. "negotiated capability %4.4x.\n", sdc->nic_name,
  275. duplex ? "full" : "half", sdc->phys[0],
  276. negotiated );
  277. outw(inw(BASE + MACCtrl0) | duplex ? 0x20 : 0,
  278. BASE + MACCtrl0);
  279. }
  280. }
  281. /**************************************************************************
  282. * init_ring - setup the tx and rx descriptors
  283. *************************************************************************/
  284. static void init_ring(struct nic *nic __unused)
  285. {
  286. int i;
  287. sdc->cur_rx = 0;
  288. /* Initialize all the Rx descriptors */
  289. for (i = 0; i < RX_RING_SIZE; i++) {
  290. rx_ring[i].next_desc = virt_to_le32desc(&rx_ring[i + 1]);
  291. rx_ring[i].status = 0;
  292. rx_ring[i].length = 0;
  293. rx_ring[i].addr = 0;
  294. }
  295. /* Mark the last entry as wrapping the ring */
  296. rx_ring[i - 1].next_desc = virt_to_le32desc(&rx_ring[0]);
  297. for (i = 0; i < RX_RING_SIZE; i++) {
  298. rx_ring[i].addr = virt_to_le32desc(&rxb[i * PKT_BUF_SZ]);
  299. rx_ring[i].length = cpu_to_le32(PKT_BUF_SZ | LastFrag);
  300. }
  301. /* We only use one transmit buffer, but two
  302. * descriptors so transmit engines have somewhere
  303. * to point should they feel the need */
  304. tx_ring[0].status = 0x00000000;
  305. tx_ring[0].addr = virt_to_bus(&txb[0]);
  306. tx_ring[0].next_desc = 0; /* virt_to_bus(&tx_ring[1]); */
  307. /* This descriptor is never used */
  308. tx_ring[1].status = 0x00000000;
  309. tx_ring[1].addr = 0; /*virt_to_bus(&txb[0]); */
  310. tx_ring[1].next_desc = 0;
  311. /* Mark the last entry as wrapping the ring,
  312. * though this should never happen */
  313. tx_ring[1].length = cpu_to_le32(LastFrag | PKT_BUF_SZ);
  314. }
  315. /**************************************************************************
  316. * RESET - Reset Adapter
  317. * ***********************************************************************/
  318. static void sundance_reset(struct nic *nic)
  319. {
  320. int i;
  321. init_ring(nic);
  322. outl(virt_to_le32desc(&rx_ring[0]), BASE + RxListPtr);
  323. /* The Tx List Pointer is written as packets are queued */
  324. /* Initialize other registers. */
  325. /* __set_mac_addr(dev); */
  326. {
  327. u16 addr16;
  328. addr16 = (nic->node_addr[0] | (nic->node_addr[1] << 8));
  329. outw(addr16, BASE + StationAddr);
  330. addr16 = (nic->node_addr[2] | (nic->node_addr[3] << 8));
  331. outw(addr16, BASE + StationAddr + 2);
  332. addr16 = (nic->node_addr[4] | (nic->node_addr[5] << 8));
  333. outw(addr16, BASE + StationAddr + 4);
  334. }
  335. outw(sdc->mtu + 14, BASE + MaxFrameSize);
  336. if (sdc->mtu > 2047) /* this will never happen with default options */
  337. outl(inl(BASE + ASICCtrl) | 0x0c, BASE + ASICCtrl);
  338. set_rx_mode(nic);
  339. outw(0, BASE + DownCounter);
  340. /* Set the chip to poll every N*30nsec */
  341. outb(100, BASE + RxDMAPollPeriod);
  342. /* Fix DFE-580TX packet drop issue */
  343. if (sdc->pci_rev_id >= 0x14)
  344. writeb(0x01, BASE + DebugCtrl1);
  345. outw(RxEnable | TxEnable, BASE + MACCtrl1);
  346. /* Construct a perfect filter frame with the mac address as first match
  347. * and broadcast for all others */
  348. for (i = 0; i < 192; i++)
  349. txb[i] = 0xFF;
  350. txb[0] = nic->node_addr[0];
  351. txb[1] = nic->node_addr[1];
  352. txb[2] = nic->node_addr[2];
  353. txb[3] = nic->node_addr[3];
  354. txb[4] = nic->node_addr[4];
  355. txb[5] = nic->node_addr[5];
  356. DBG ( "%s: Done sundance_reset, status: Rx %hX Tx %hX "
  357. "MAC Control %hX, %hX %hX\n",
  358. sdc->nic_name, (int) inl(BASE + RxStatus),
  359. (int) inw(BASE + TxStatus), (int) inl(BASE + MACCtrl0),
  360. (int) inw(BASE + MACCtrl1), (int) inw(BASE + MACCtrl0) );
  361. }
  362. /**************************************************************************
  363. IRQ - Wait for a frame
  364. ***************************************************************************/
  365. static void sundance_irq ( struct nic *nic, irq_action_t action ) {
  366. unsigned int intr_status;
  367. switch ( action ) {
  368. case DISABLE :
  369. case ENABLE :
  370. intr_status = inw(nic->ioaddr + IntrStatus);
  371. intr_status = intr_status & ~DEFAULT_INTR;
  372. if ( action == ENABLE )
  373. intr_status = intr_status | DEFAULT_INTR;
  374. outw(intr_status, nic->ioaddr + IntrEnable);
  375. break;
  376. case FORCE :
  377. outw(0x0200, BASE + ASICCtrl);
  378. break;
  379. }
  380. }
  381. /**************************************************************************
  382. POLL - Wait for a frame
  383. ***************************************************************************/
  384. static int sundance_poll(struct nic *nic, int retreive)
  385. {
  386. /* return true if there's an ethernet packet ready to read */
  387. /* nic->packet should contain data on return */
  388. /* nic->packetlen should contain length of data */
  389. int entry = sdc->cur_rx % RX_RING_SIZE;
  390. u32 frame_status = le32_to_cpu(rx_ring[entry].status);
  391. int intr_status;
  392. int pkt_len = 0;
  393. if (!(frame_status & DescOwn))
  394. return 0;
  395. /* There is a packet ready */
  396. if(!retreive)
  397. return 1;
  398. intr_status = inw(nic->ioaddr + IntrStatus);
  399. outw(intr_status, nic->ioaddr + IntrStatus);
  400. pkt_len = frame_status & 0x1fff;
  401. if (frame_status & 0x001f4000) {
  402. DBG ( "Polling frame_status error\n" ); /* Do we really care about this */
  403. } else {
  404. if (pkt_len < rx_copybreak) {
  405. /* FIXME: What should happen Will this ever occur */
  406. printf("Poll Error: pkt_len < rx_copybreak");
  407. } else {
  408. nic->packetlen = pkt_len;
  409. memcpy(nic->packet, rxb +
  410. (sdc->cur_rx * PKT_BUF_SZ), nic->packetlen);
  411. }
  412. }
  413. rx_ring[entry].length = cpu_to_le32(PKT_BUF_SZ | LastFrag);
  414. rx_ring[entry].status = 0;
  415. entry++;
  416. sdc->cur_rx = entry % RX_RING_SIZE;
  417. outw(DEFAULT_INTR & ~(IntrRxDone|IntrRxDMADone),
  418. nic->ioaddr + IntrStatus);
  419. return 1;
  420. }
  421. /**************************************************************************
  422. TRANSMIT - Transmit a frame
  423. ***************************************************************************/
  424. static void sundance_transmit(struct nic *nic, const char *d, /* Destination */
  425. unsigned int t, /* Type */
  426. unsigned int s, /* size */
  427. const char *p)
  428. { /* Packet */
  429. u16 nstype;
  430. u32 to;
  431. /* Disable the Tx */
  432. outw(TxDisable, BASE + MACCtrl1);
  433. memcpy(txb, d, ETH_ALEN);
  434. memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  435. nstype = htons((u16) t);
  436. memcpy(txb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
  437. memcpy(txb + ETH_HLEN, p, s);
  438. s += ETH_HLEN;
  439. s &= 0x0FFF;
  440. while (s < ETH_ZLEN)
  441. txb[s++] = '\0';
  442. /* Setup the transmit descriptor */
  443. tx_ring[0].length = cpu_to_le32(s | LastFrag);
  444. tx_ring[0].status = cpu_to_le32(0x00000001);
  445. /* Point to transmit descriptor */
  446. outl(virt_to_le32desc(&tx_ring[0]), BASE + TxListPtr);
  447. /* Enable Tx */
  448. outw(TxEnable, BASE + MACCtrl1);
  449. /* Trigger an immediate send */
  450. outw(0, BASE + TxStatus);
  451. to = currticks() + TX_TIME_OUT;
  452. while (!(tx_ring[0].status & 0x00010000) && (currticks() < to)); /* wait */
  453. if (currticks() >= to) {
  454. printf("TX Time Out");
  455. }
  456. /* Disable Tx */
  457. outw(TxDisable, BASE + MACCtrl1);
  458. }
  459. /**************************************************************************
  460. DISABLE - Turn off ethernet interface
  461. ***************************************************************************/
  462. static void sundance_disable ( struct nic *nic __unused ) {
  463. /* put the card in its initial state */
  464. /* This function serves 3 purposes.
  465. * This disables DMA and interrupts so we don't receive
  466. * unexpected packets or interrupts from the card after
  467. * etherboot has finished.
  468. * This frees resources so etherboot may use
  469. * this driver on another interface
  470. * This allows etherboot to reinitialize the interface
  471. * if something is something goes wrong.
  472. */
  473. outw(0x0000, BASE + IntrEnable);
  474. /* Stop the Chipchips Tx and Rx Status */
  475. outw(TxDisable | RxDisable | StatsDisable, BASE + MACCtrl1);
  476. }
  477. static struct nic_operations sundance_operations = {
  478. .connect = dummy_connect,
  479. .poll = sundance_poll,
  480. .transmit = sundance_transmit,
  481. .irq = sundance_irq,
  482. };
  483. /**************************************************************************
  484. PROBE - Look for an adapter, this routine's visible to the outside
  485. ***************************************************************************/
  486. static int sundance_probe ( struct nic *nic, struct pci_device *pci ) {
  487. u8 ee_data[EEPROM_SIZE];
  488. u16 mii_ctl;
  489. int i;
  490. int speed;
  491. if (pci->ioaddr == 0)
  492. return 0;
  493. /* BASE is used throughout to address the card */
  494. BASE = pci->ioaddr;
  495. printf(" sundance.c: Found %s Vendor=0x%hX Device=0x%hX\n",
  496. pci->driver_name, pci->vendor, pci->device);
  497. /* Get the MAC Address by reading the EEPROM */
  498. for (i = 0; i < 3; i++) {
  499. ((u16 *) ee_data)[i] =
  500. le16_to_cpu(eeprom_read(BASE, i + EEPROM_SA_OFFSET));
  501. }
  502. /* Update the nic structure with the MAC Address */
  503. for (i = 0; i < ETH_ALEN; i++) {
  504. nic->node_addr[i] = ee_data[i];
  505. }
  506. /* Set the card as PCI Bus Master */
  507. adjust_pci_device(pci);
  508. // sdc->mii_if.dev = pci;
  509. // sdc->mii_if.phy_id_mask = 0x1f;
  510. // sdc->mii_if.reg_num_mask = 0x1f;
  511. /* point to private storage */
  512. sdc = &sdx;
  513. sdc->nic_name = pci->driver_name;
  514. sdc->mtu = mtu;
  515. pci_read_config_byte(pci, PCI_REVISION_ID, &sdc->pci_rev_id);
  516. DBG ( "Device revision id: %hx\n", sdc->pci_rev_id );
  517. /* Print out some hardware info */
  518. DBG ( "%s: %s at ioaddr %hX, ", pci->driver_name, nic->node_addr, (unsigned int) BASE);
  519. sdc->mii_preamble_required = 0;
  520. if (1) {
  521. int phy, phy_idx = 0;
  522. sdc->phys[0] = 1; /* Default Setting */
  523. sdc->mii_preamble_required++;
  524. for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
  525. int mii_status = mdio_read(nic, phy, MII_BMSR);
  526. if (mii_status != 0xffff && mii_status != 0x0000) {
  527. sdc->phys[phy_idx++] = phy;
  528. sdc->mii_if.advertising =
  529. mdio_read(nic, phy, MII_ADVERTISE);
  530. if ((mii_status & 0x0040) == 0)
  531. sdc->mii_preamble_required++;
  532. DBG
  533. ( "%s: MII PHY found at address %d, status " "%hX advertising %hX\n", sdc->nic_name, phy, mii_status, sdc->mii_if.advertising );
  534. }
  535. }
  536. sdc->mii_preamble_required--;
  537. if (phy_idx == 0)
  538. printf("%s: No MII transceiver found!\n",
  539. sdc->nic_name);
  540. sdc->mii_if.phy_id = sdc->phys[0];
  541. }
  542. /* Parse override configuration */
  543. sdc->an_enable = 1;
  544. if (strcasecmp(media, "autosense") != 0) {
  545. sdc->an_enable = 0;
  546. if (strcasecmp(media, "100mbps_fd") == 0 ||
  547. strcasecmp(media, "4") == 0) {
  548. sdc->speed = 100;
  549. sdc->mii_if.full_duplex = 1;
  550. } else if (strcasecmp(media, "100mbps_hd") == 0
  551. || strcasecmp(media, "3") == 0) {
  552. sdc->speed = 100;
  553. sdc->mii_if.full_duplex = 0;
  554. } else if (strcasecmp(media, "10mbps_fd") == 0 ||
  555. strcasecmp(media, "2") == 0) {
  556. sdc->speed = 10;
  557. sdc->mii_if.full_duplex = 1;
  558. } else if (strcasecmp(media, "10mbps_hd") == 0 ||
  559. strcasecmp(media, "1") == 0) {
  560. sdc->speed = 10;
  561. sdc->mii_if.full_duplex = 0;
  562. } else {
  563. sdc->an_enable = 1;
  564. }
  565. }
  566. if (flowctrl == 1)
  567. sdc->flowctrl = 1;
  568. /* Fibre PHY? */
  569. if (inl(BASE + ASICCtrl) & 0x80) {
  570. /* Default 100Mbps Full */
  571. if (sdc->an_enable) {
  572. sdc->speed = 100;
  573. sdc->mii_if.full_duplex = 1;
  574. sdc->an_enable = 0;
  575. }
  576. }
  577. /* The Linux driver uses flow control and resets the link here. This means the
  578. mii section from above would need to be re done I believe. Since it serves
  579. no real purpose leave it out. */
  580. /* Force media type */
  581. if (!sdc->an_enable) {
  582. mii_ctl = 0;
  583. mii_ctl |= (sdc->speed == 100) ? BMCR_SPEED100 : 0;
  584. mii_ctl |= (sdc->mii_if.full_duplex) ? BMCR_FULLDPLX : 0;
  585. mdio_write(nic, sdc->phys[0], MII_BMCR, mii_ctl);
  586. printf("Override speed=%d, %s duplex\n",
  587. sdc->speed,
  588. sdc->mii_if.full_duplex ? "Full" : "Half");
  589. }
  590. /* Reset the chip to erase previous misconfiguration */
  591. DBG ( "ASIC Control is %#x\n", inl(BASE + ASICCtrl) );
  592. outw(0x007f, BASE + ASICCtrl + 2);
  593. /*
  594. * wait for reset to complete
  595. * this is heavily inspired by the linux sundance driver
  596. * according to the linux driver it can take up to 1ms for the reset
  597. * to complete
  598. */
  599. i = 0;
  600. while(inl(BASE + ASICCtrl) & (ResetBusy << 16)) {
  601. if(i++ >= 10) {
  602. DBG("sundance: NIC reset did not complete.\n");
  603. break;
  604. }
  605. udelay(100);
  606. }
  607. DBG ( "ASIC Control is now %#x.\n", inl(BASE + ASICCtrl) );
  608. sundance_reset(nic);
  609. if (sdc->an_enable) {
  610. u16 mii_advertise, mii_lpa;
  611. mii_advertise =
  612. mdio_read(nic, sdc->phys[0], MII_ADVERTISE);
  613. mii_lpa = mdio_read(nic, sdc->phys[0], MII_LPA);
  614. mii_advertise &= mii_lpa;
  615. if (mii_advertise & ADVERTISE_100FULL)
  616. sdc->speed = 100;
  617. else if (mii_advertise & ADVERTISE_100HALF)
  618. sdc->speed = 100;
  619. else if (mii_advertise & ADVERTISE_10FULL)
  620. sdc->speed = 10;
  621. else if (mii_advertise & ADVERTISE_10HALF)
  622. sdc->speed = 10;
  623. } else {
  624. mii_ctl = mdio_read(nic, sdc->phys[0], MII_BMCR);
  625. speed = (mii_ctl & BMCR_SPEED100) ? 100 : 10;
  626. sdc->speed = speed;
  627. printf("%s: Link changed: %dMbps ,", sdc->nic_name, speed);
  628. printf("%s duplex.\n", (mii_ctl & BMCR_FULLDPLX) ?
  629. "full" : "half");
  630. }
  631. check_duplex(nic);
  632. if (sdc->flowctrl && sdc->mii_if.full_duplex) {
  633. outw(inw(BASE + MulticastFilter1 + 2) | 0x0200,
  634. BASE + MulticastFilter1 + 2);
  635. outw(inw(BASE + MACCtrl0) | EnbFlowCtrl, BASE + MACCtrl0);
  636. }
  637. printf("%dMbps, %s-Duplex\n", sdc->speed,
  638. sdc->mii_if.full_duplex ? "Full" : "Half");
  639. /* point to NIC specific routines */
  640. nic->nic_op = &sundance_operations;
  641. nic->irqno = pci->irq;
  642. nic->ioaddr = BASE;
  643. return 1;
  644. }
  645. /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */
  646. static int eeprom_read(long ioaddr, int location)
  647. {
  648. int boguscnt = 10000; /* Typical 1900 ticks */
  649. outw(0x0200 | (location & 0xff), ioaddr + EECtrl);
  650. do {
  651. if (!(inw(ioaddr + EECtrl) & 0x8000)) {
  652. return inw(ioaddr + EEData);
  653. }
  654. }
  655. while (--boguscnt > 0);
  656. return 0;
  657. }
  658. /* MII transceiver control section.
  659. Read and write the MII registers using software-generated serial
  660. MDIO protocol. See the MII specifications or DP83840A data sheet
  661. for details.
  662. The maximum data clock rate is 2.5 Mhz.
  663. The timing is decoupled from the processor clock by flushing the write
  664. from the CPU write buffer with a following read, and using PCI
  665. transaction time. */
  666. #define mdio_in(mdio_addr) inb(mdio_addr)
  667. #define mdio_out(value, mdio_addr) outb(value, mdio_addr)
  668. #define mdio_delay(mdio_addr) inb(mdio_addr)
  669. enum mii_reg_bits {
  670. MDIO_ShiftClk = 0x0001, MDIO_Data = 0x0002, MDIO_EnbOutput =
  671. 0x0004,
  672. };
  673. #define MDIO_EnbIn (0)
  674. #define MDIO_WRITE0 (MDIO_EnbOutput)
  675. #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
  676. /* Generate the preamble required for initial synchronization and
  677. a few older transceivers. */
  678. static void mdio_sync(long mdio_addr)
  679. {
  680. int bits = 32;
  681. /* Establish sync by sending at least 32 logic ones. */
  682. while (--bits >= 0) {
  683. mdio_out(MDIO_WRITE1, mdio_addr);
  684. mdio_delay(mdio_addr);
  685. mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
  686. mdio_delay(mdio_addr);
  687. }
  688. }
  689. static int
  690. mdio_read(struct nic *nic __unused, int phy_id, unsigned int location)
  691. {
  692. long mdio_addr = BASE + MIICtrl;
  693. int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
  694. int i, retval = 0;
  695. if (sdc->mii_preamble_required)
  696. mdio_sync(mdio_addr);
  697. /* Shift the read command bits out. */
  698. for (i = 15; i >= 0; i--) {
  699. int dataval =
  700. (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  701. mdio_out(dataval, mdio_addr);
  702. mdio_delay(mdio_addr);
  703. mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
  704. mdio_delay(mdio_addr);
  705. }
  706. /* Read the two transition, 16 data, and wire-idle bits. */
  707. for (i = 19; i > 0; i--) {
  708. mdio_out(MDIO_EnbIn, mdio_addr);
  709. mdio_delay(mdio_addr);
  710. retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_Data)
  711. ? 1 : 0);
  712. mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
  713. mdio_delay(mdio_addr);
  714. }
  715. return (retval >> 1) & 0xffff;
  716. }
  717. static void
  718. mdio_write(struct nic *nic __unused, int phy_id,
  719. unsigned int location, int value)
  720. {
  721. long mdio_addr = BASE + MIICtrl;
  722. int mii_cmd =
  723. (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
  724. int i;
  725. if (sdc->mii_preamble_required)
  726. mdio_sync(mdio_addr);
  727. /* Shift the command bits out. */
  728. for (i = 31; i >= 0; i--) {
  729. int dataval =
  730. (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  731. mdio_out(dataval, mdio_addr);
  732. mdio_delay(mdio_addr);
  733. mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
  734. mdio_delay(mdio_addr);
  735. }
  736. /* Clear out extra bits. */
  737. for (i = 2; i > 0; i--) {
  738. mdio_out(MDIO_EnbIn, mdio_addr);
  739. mdio_delay(mdio_addr);
  740. mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
  741. mdio_delay(mdio_addr);
  742. }
  743. return;
  744. }
  745. static void set_rx_mode(struct nic *nic __unused)
  746. {
  747. int i;
  748. u16 mc_filter[4]; /* Multicast hash filter */
  749. u32 rx_mode;
  750. memset(mc_filter, 0xff, sizeof(mc_filter));
  751. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  752. if (sdc->mii_if.full_duplex && sdc->flowctrl)
  753. mc_filter[3] |= 0x0200;
  754. for (i = 0; i < 4; i++)
  755. outw(mc_filter[i], BASE + MulticastFilter0 + i * 2);
  756. outb(rx_mode, BASE + RxMode);
  757. return;
  758. }
  759. static struct pci_device_id sundance_nics[] = {
  760. PCI_ROM(0x13f0, 0x0201, "sundance", "ST201 Sundance 'Alta' based Adaptor", 0),
  761. PCI_ROM(0x1186, 0x1002, "dfe530txs", "D-Link DFE530TXS (Sundance ST201 Alta)", 0),
  762. PCI_ROM(0x13f0, 0x0200, "ip100a", "IC+ IP100A", 0),
  763. };
  764. PCI_DRIVER ( sundance_driver, sundance_nics, PCI_NO_CLASS );
  765. DRIVER ( "SUNDANCE/PCI", nic_driver, pci_driver, sundance_driver,
  766. sundance_probe, sundance_disable );
  767. /*
  768. * Local variables:
  769. * c-basic-offset: 8
  770. * c-indent-level: 8
  771. * tab-width: 8
  772. * End:
  773. */