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.

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