ns8382x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. ns8382x.c: A U-Boot driver for the NatSemi DP8382[01].
  3. ported by: Mark A. Rakes (mark_rakes@vivato.net)
  4. Adapted from:
  5. 1. an Etherboot driver for DP8381[56] written by:
  6. Copyright (C) 2001 Entity Cyber, Inc.
  7. This development of this Etherboot driver was funded by
  8. Sicom Systems: http://www.sicompos.com/
  9. Author: Marty Connor (mdc@thinguin.org)
  10. Adapted from a Linux driver which was written by Donald Becker
  11. This software may be used and distributed according to the terms
  12. of the GNU Public License (GPL), incorporated herein by reference.
  13. 2. A Linux driver by Donald Becker, ns820.c:
  14. Written/copyright 1999-2002 by Donald Becker.
  15. This software may be used and distributed according to the terms of
  16. the GNU General Public License (GPL), incorporated herein by reference.
  17. Drivers based on or derived from this code fall under the GPL and must
  18. retain the authorship, copyright and license notice. This file is not
  19. a complete program and may only be used when the entire operating
  20. system is licensed under the GPL. License for under other terms may be
  21. available. Contact the original author for details.
  22. The original author may be reached as becker@scyld.com, or at
  23. Scyld Computing Corporation
  24. 410 Severn Ave., Suite 210
  25. Annapolis MD 21403
  26. Support information and updates available at
  27. http://www.scyld.com/network/netsemi.html
  28. Datasheets available from:
  29. http://www.national.com/pf/DP/DP83820.html
  30. http://www.national.com/pf/DP/DP83821.html
  31. */
  32. /* Revision History
  33. * October 2002 mar 1.0
  34. * Initial U-Boot Release.
  35. * Tested with Netgear GA622T (83820)
  36. * and SMC9452TX (83821)
  37. * NOTE: custom boards with these chips may (likely) require
  38. * a programmed EEPROM device (if present) in order to work
  39. * correctly.
  40. */
  41. /* Includes */
  42. #include <common.h>
  43. #include <malloc.h>
  44. #include <net.h>
  45. #include <netdev.h>
  46. #include <asm/io.h>
  47. #include <pci.h>
  48. /* defines */
  49. #define DSIZE 0x00000FFF
  50. #define ETH_ALEN 6
  51. #define CRC_SIZE 4
  52. #define TOUT_LOOP 500000
  53. #define TX_BUF_SIZE 1536
  54. #define RX_BUF_SIZE 1536
  55. #define NUM_RX_DESC 4 /* Number of Rx descriptor registers. */
  56. enum register_offsets {
  57. ChipCmd = 0x00,
  58. ChipConfig = 0x04,
  59. EECtrl = 0x08,
  60. IntrMask = 0x14,
  61. IntrEnable = 0x18,
  62. TxRingPtr = 0x20,
  63. TxRingPtrHi = 0x24,
  64. TxConfig = 0x28,
  65. RxRingPtr = 0x30,
  66. RxRingPtrHi = 0x34,
  67. RxConfig = 0x38,
  68. PriQueue = 0x3C,
  69. RxFilterAddr = 0x48,
  70. RxFilterData = 0x4C,
  71. ClkRun = 0xCC,
  72. PCIPM = 0x44,
  73. };
  74. enum ChipCmdBits {
  75. ChipReset = 0x100,
  76. RxReset = 0x20,
  77. TxReset = 0x10,
  78. RxOff = 0x08,
  79. RxOn = 0x04,
  80. TxOff = 0x02,
  81. TxOn = 0x01
  82. };
  83. enum ChipConfigBits {
  84. LinkSts = 0x80000000,
  85. GigSpeed = 0x40000000,
  86. HundSpeed = 0x20000000,
  87. FullDuplex = 0x10000000,
  88. TBIEn = 0x01000000,
  89. Mode1000 = 0x00400000,
  90. T64En = 0x00004000,
  91. D64En = 0x00001000,
  92. M64En = 0x00000800,
  93. PhyRst = 0x00000400,
  94. PhyDis = 0x00000200,
  95. ExtStEn = 0x00000100,
  96. BEMode = 0x00000001,
  97. };
  98. #define SpeedStatus_Polarity ( GigSpeed | HundSpeed | FullDuplex)
  99. enum TxConfig_bits {
  100. TxDrthMask = 0x000000ff,
  101. TxFlthMask = 0x0000ff00,
  102. TxMxdmaMask = 0x00700000,
  103. TxMxdma_8 = 0x00100000,
  104. TxMxdma_16 = 0x00200000,
  105. TxMxdma_32 = 0x00300000,
  106. TxMxdma_64 = 0x00400000,
  107. TxMxdma_128 = 0x00500000,
  108. TxMxdma_256 = 0x00600000,
  109. TxMxdma_512 = 0x00700000,
  110. TxMxdma_1024 = 0x00000000,
  111. TxCollRetry = 0x00800000,
  112. TxAutoPad = 0x10000000,
  113. TxMacLoop = 0x20000000,
  114. TxHeartIgn = 0x40000000,
  115. TxCarrierIgn = 0x80000000
  116. };
  117. enum RxConfig_bits {
  118. RxDrthMask = 0x0000003e,
  119. RxMxdmaMask = 0x00700000,
  120. RxMxdma_8 = 0x00100000,
  121. RxMxdma_16 = 0x00200000,
  122. RxMxdma_32 = 0x00300000,
  123. RxMxdma_64 = 0x00400000,
  124. RxMxdma_128 = 0x00500000,
  125. RxMxdma_256 = 0x00600000,
  126. RxMxdma_512 = 0x00700000,
  127. RxMxdma_1024 = 0x00000000,
  128. RxAcceptLenErr = 0x04000000,
  129. RxAcceptLong = 0x08000000,
  130. RxAcceptTx = 0x10000000,
  131. RxStripCRC = 0x20000000,
  132. RxAcceptRunt = 0x40000000,
  133. RxAcceptErr = 0x80000000,
  134. };
  135. /* Bits in the RxMode register. */
  136. enum rx_mode_bits {
  137. RxFilterEnable = 0x80000000,
  138. AcceptAllBroadcast = 0x40000000,
  139. AcceptAllMulticast = 0x20000000,
  140. AcceptAllUnicast = 0x10000000,
  141. AcceptPerfectMatch = 0x08000000,
  142. };
  143. typedef struct _BufferDesc {
  144. u32 link;
  145. u32 bufptr;
  146. vu_long cmdsts;
  147. u32 extsts; /*not used here */
  148. } BufferDesc;
  149. /* Bits in network_desc.status */
  150. enum desc_status_bits {
  151. DescOwn = 0x80000000, DescMore = 0x40000000, DescIntr = 0x20000000,
  152. DescNoCRC = 0x10000000, DescPktOK = 0x08000000,
  153. DescSizeMask = 0xfff,
  154. DescTxAbort = 0x04000000, DescTxFIFO = 0x02000000,
  155. DescTxCarrier = 0x01000000, DescTxDefer = 0x00800000,
  156. DescTxExcDefer = 0x00400000, DescTxOOWCol = 0x00200000,
  157. DescTxExcColl = 0x00100000, DescTxCollCount = 0x000f0000,
  158. DescRxAbort = 0x04000000, DescRxOver = 0x02000000,
  159. DescRxDest = 0x01800000, DescRxLong = 0x00400000,
  160. DescRxRunt = 0x00200000, DescRxInvalid = 0x00100000,
  161. DescRxCRC = 0x00080000, DescRxAlign = 0x00040000,
  162. DescRxLoop = 0x00020000, DesRxColl = 0x00010000,
  163. };
  164. /* Bits in MEAR */
  165. enum mii_reg_bits {
  166. MDIO_ShiftClk = 0x0040,
  167. MDIO_EnbOutput = 0x0020,
  168. MDIO_Data = 0x0010,
  169. };
  170. /* PHY Register offsets. */
  171. enum phy_reg_offsets {
  172. BMCR = 0x00,
  173. BMSR = 0x01,
  174. PHYIDR1 = 0x02,
  175. PHYIDR2 = 0x03,
  176. ANAR = 0x04,
  177. KTCR = 0x09,
  178. };
  179. /* basic mode control register bits */
  180. enum bmcr_bits {
  181. Bmcr_Reset = 0x8000,
  182. Bmcr_Loop = 0x4000,
  183. Bmcr_Speed0 = 0x2000,
  184. Bmcr_AutoNegEn = 0x1000, /*if set ignores Duplex, Speed[01] */
  185. Bmcr_RstAutoNeg = 0x0200,
  186. Bmcr_Duplex = 0x0100,
  187. Bmcr_Speed1 = 0x0040,
  188. Bmcr_Force10H = 0x0000,
  189. Bmcr_Force10F = 0x0100,
  190. Bmcr_Force100H = 0x2000,
  191. Bmcr_Force100F = 0x2100,
  192. Bmcr_Force1000H = 0x0040,
  193. Bmcr_Force1000F = 0x0140,
  194. };
  195. /* auto negotiation advertisement register */
  196. enum anar_bits {
  197. anar_adv_100F = 0x0100,
  198. anar_adv_100H = 0x0080,
  199. anar_adv_10F = 0x0040,
  200. anar_adv_10H = 0x0020,
  201. anar_ieee_8023 = 0x0001,
  202. };
  203. /* 1K-base T control register */
  204. enum ktcr_bits {
  205. ktcr_adv_1000H = 0x0100,
  206. ktcr_adv_1000F = 0x0200,
  207. };
  208. /* Globals */
  209. static u32 SavedClkRun;
  210. static unsigned int cur_rx;
  211. static unsigned int rx_config;
  212. static unsigned int tx_config;
  213. /* Note: transmit and receive buffers and descriptors must be
  214. long long word aligned */
  215. static BufferDesc txd __attribute__ ((aligned(8)));
  216. static BufferDesc rxd[NUM_RX_DESC] __attribute__ ((aligned(8)));
  217. static unsigned char txb[TX_BUF_SIZE] __attribute__ ((aligned(8)));
  218. static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE]
  219. __attribute__ ((aligned(8)));
  220. /* Function Prototypes */
  221. static int mdio_read(struct eth_device *dev, int phy_id, int addr);
  222. static void mdio_write(struct eth_device *dev, int phy_id, int addr, int value);
  223. static void mdio_sync(struct eth_device *dev, u32 offset);
  224. static int ns8382x_init(struct eth_device *dev, bd_t * bis);
  225. static void ns8382x_reset(struct eth_device *dev);
  226. static void ns8382x_init_rxfilter(struct eth_device *dev);
  227. static void ns8382x_init_txd(struct eth_device *dev);
  228. static void ns8382x_init_rxd(struct eth_device *dev);
  229. static void ns8382x_set_rx_mode(struct eth_device *dev);
  230. static void ns8382x_check_duplex(struct eth_device *dev);
  231. static int ns8382x_send(struct eth_device *dev, volatile void *packet,
  232. int length);
  233. static int ns8382x_poll(struct eth_device *dev);
  234. static void ns8382x_disable(struct eth_device *dev);
  235. static struct pci_device_id supported[] = {
  236. {PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_83820},
  237. {}
  238. };
  239. #define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
  240. #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
  241. static inline int
  242. INW(struct eth_device *dev, u_long addr)
  243. {
  244. return le16_to_cpu(*(vu_short *) (addr + dev->iobase));
  245. }
  246. static int
  247. INL(struct eth_device *dev, u_long addr)
  248. {
  249. return le32_to_cpu(*(vu_long *) (addr + dev->iobase));
  250. }
  251. static inline void
  252. OUTW(struct eth_device *dev, int command, u_long addr)
  253. {
  254. *(vu_short *) ((addr + dev->iobase)) = cpu_to_le16(command);
  255. }
  256. static inline void
  257. OUTL(struct eth_device *dev, int command, u_long addr)
  258. {
  259. *(vu_long *) ((addr + dev->iobase)) = cpu_to_le32(command);
  260. }
  261. /* Function: ns8382x_initialize
  262. * Description: Retrieves the MAC address of the card, and sets up some
  263. * globals required by other routines, and initializes the NIC, making it
  264. * ready to send and receive packets.
  265. * Side effects: initializes ns8382xs, ready to recieve packets.
  266. * Returns: int: number of cards found
  267. */
  268. int
  269. ns8382x_initialize(bd_t * bis)
  270. {
  271. pci_dev_t devno;
  272. int card_number = 0;
  273. struct eth_device *dev;
  274. u32 iobase, status;
  275. int i, idx = 0;
  276. u32 phyAddress;
  277. u32 tmp;
  278. u32 chip_config;
  279. while (1) { /* Find PCI device(s) */
  280. if ((devno = pci_find_devices(supported, idx++)) < 0)
  281. break;
  282. pci_read_config_dword(devno, PCI_BASE_ADDRESS_1, &iobase);
  283. iobase &= ~0x3; /* 1: unused and 0:I/O Space Indicator */
  284. #ifdef NS8382X_DEBUG
  285. printf("ns8382x: NatSemi dp8382x @ 0x%x\n", iobase);
  286. #endif
  287. pci_write_config_dword(devno, PCI_COMMAND,
  288. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  289. /* Check if I/O accesses and Bus Mastering are enabled. */
  290. pci_read_config_dword(devno, PCI_COMMAND, &status);
  291. if (!(status & PCI_COMMAND_MEMORY)) {
  292. printf("Error: Can not enable MEM access.\n");
  293. continue;
  294. } else if (!(status & PCI_COMMAND_MASTER)) {
  295. printf("Error: Can not enable Bus Mastering.\n");
  296. continue;
  297. }
  298. dev = (struct eth_device *) malloc(sizeof *dev);
  299. sprintf(dev->name, "dp8382x#%d", card_number);
  300. dev->iobase = bus_to_phys(iobase);
  301. dev->priv = (void *) devno;
  302. dev->init = ns8382x_init;
  303. dev->halt = ns8382x_disable;
  304. dev->send = ns8382x_send;
  305. dev->recv = ns8382x_poll;
  306. /* ns8382x has a non-standard PM control register
  307. * in PCI config space. Some boards apparently need
  308. * to be brought to D0 in this manner. */
  309. pci_read_config_dword(devno, PCIPM, &tmp);
  310. if (tmp & (0x03 | 0x100)) { /* D0 state, disable PME assertion */
  311. u32 newtmp = tmp & ~(0x03 | 0x100);
  312. pci_write_config_dword(devno, PCIPM, newtmp);
  313. }
  314. /* get MAC address */
  315. for (i = 0; i < 3; i++) {
  316. u32 data;
  317. char *mac = (char *)&dev->enetaddr[i * 2];
  318. OUTL(dev, i * 2, RxFilterAddr);
  319. data = INL(dev, RxFilterData);
  320. *mac++ = data;
  321. *mac++ = data >> 8;
  322. }
  323. /* get PHY address, can't be zero */
  324. for (phyAddress = 1; phyAddress < 32; phyAddress++) {
  325. u32 rev, phy1;
  326. phy1 = mdio_read(dev, phyAddress, PHYIDR1);
  327. if (phy1 == 0x2000) { /*check for 83861/91 */
  328. rev = mdio_read(dev, phyAddress, PHYIDR2);
  329. if ((rev & ~(0x000f)) == 0x00005c50 ||
  330. (rev & ~(0x000f)) == 0x00005c60) {
  331. #ifdef NS8382X_DEBUG
  332. printf("phy rev is %x\n", rev);
  333. printf("phy address is %x\n",
  334. phyAddress);
  335. #endif
  336. break;
  337. }
  338. }
  339. }
  340. /* set phy to autonegotiate && advertise everything */
  341. mdio_write(dev, phyAddress, KTCR,
  342. (ktcr_adv_1000H | ktcr_adv_1000F));
  343. mdio_write(dev, phyAddress, ANAR,
  344. (anar_adv_100F | anar_adv_100H | anar_adv_10H |
  345. anar_adv_10F | anar_ieee_8023));
  346. mdio_write(dev, phyAddress, BMCR, 0x0); /*restore */
  347. mdio_write(dev, phyAddress, BMCR,
  348. (Bmcr_AutoNegEn | Bmcr_RstAutoNeg));
  349. /* Reset the chip to erase any previous misconfiguration. */
  350. OUTL(dev, (ChipReset), ChipCmd);
  351. chip_config = INL(dev, ChipConfig);
  352. /* reset the phy */
  353. OUTL(dev, (chip_config | PhyRst), ChipConfig);
  354. /* power up and initialize transceiver */
  355. OUTL(dev, (chip_config & ~(PhyDis)), ChipConfig);
  356. mdio_sync(dev, EECtrl);
  357. #ifdef NS8382X_DEBUG
  358. {
  359. u32 chpcfg =
  360. INL(dev, ChipConfig) ^ SpeedStatus_Polarity;
  361. printf("%s: Transceiver 10%s %s duplex.\n", dev->name,
  362. (chpcfg & GigSpeed) ? "00" : (chpcfg & HundSpeed)
  363. ? "0" : "",
  364. chpcfg & FullDuplex ? "full" : "half");
  365. printf("%s: %02x:%02x:%02x:%02x:%02x:%02x\n", dev->name,
  366. dev->enetaddr[0], dev->enetaddr[1],
  367. dev->enetaddr[2], dev->enetaddr[3],
  368. dev->enetaddr[4], dev->enetaddr[5]);
  369. }
  370. #endif
  371. /* Disable PME:
  372. * The PME bit is initialized from the EEPROM contents.
  373. * PCI cards probably have PME disabled, but motherboard
  374. * implementations may have PME set to enable WakeOnLan.
  375. * With PME set the chip will scan incoming packets but
  376. * nothing will be written to memory. */
  377. SavedClkRun = INL(dev, ClkRun);
  378. OUTL(dev, SavedClkRun & ~0x100, ClkRun);
  379. eth_register(dev);
  380. card_number++;
  381. pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x60);
  382. udelay(10 * 1000);
  383. }
  384. return card_number;
  385. }
  386. /* MII transceiver control section.
  387. Read and write MII registers using software-generated serial MDIO
  388. protocol. See the MII specifications or DP83840A data sheet for details.
  389. The maximum data clock rate is 2.5 MHz. To meet minimum timing we
  390. must flush writes to the PCI bus with a PCI read. */
  391. #define mdio_delay(mdio_addr) INL(dev, mdio_addr)
  392. #define MDIO_EnbIn (0)
  393. #define MDIO_WRITE0 (MDIO_EnbOutput)
  394. #define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
  395. /* Generate the preamble required for initial synchronization and
  396. a few older transceivers. */
  397. static void
  398. mdio_sync(struct eth_device *dev, u32 offset)
  399. {
  400. int bits = 32;
  401. /* Establish sync by sending at least 32 logic ones. */
  402. while (--bits >= 0) {
  403. OUTL(dev, MDIO_WRITE1, offset);
  404. mdio_delay(offset);
  405. OUTL(dev, MDIO_WRITE1 | MDIO_ShiftClk, offset);
  406. mdio_delay(offset);
  407. }
  408. }
  409. static int
  410. mdio_read(struct eth_device *dev, int phy_id, int addr)
  411. {
  412. int mii_cmd = (0xf6 << 10) | (phy_id << 5) | addr;
  413. int i, retval = 0;
  414. /* Shift the read command bits out. */
  415. for (i = 15; i >= 0; i--) {
  416. int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  417. OUTL(dev, dataval, EECtrl);
  418. mdio_delay(EECtrl);
  419. OUTL(dev, dataval | MDIO_ShiftClk, EECtrl);
  420. mdio_delay(EECtrl);
  421. }
  422. /* Read the two transition, 16 data, and wire-idle bits. */
  423. for (i = 19; i > 0; i--) {
  424. OUTL(dev, MDIO_EnbIn, EECtrl);
  425. mdio_delay(EECtrl);
  426. retval =
  427. (retval << 1) | ((INL(dev, EECtrl) & MDIO_Data) ? 1 : 0);
  428. OUTL(dev, MDIO_EnbIn | MDIO_ShiftClk, EECtrl);
  429. mdio_delay(EECtrl);
  430. }
  431. return (retval >> 1) & 0xffff;
  432. }
  433. static void
  434. mdio_write(struct eth_device *dev, int phy_id, int addr, int value)
  435. {
  436. int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (addr << 18) | value;
  437. int i;
  438. /* Shift the command bits out. */
  439. for (i = 31; i >= 0; i--) {
  440. int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  441. OUTL(dev, dataval, EECtrl);
  442. mdio_delay(EECtrl);
  443. OUTL(dev, dataval | MDIO_ShiftClk, EECtrl);
  444. mdio_delay(EECtrl);
  445. }
  446. /* Clear out extra bits. */
  447. for (i = 2; i > 0; i--) {
  448. OUTL(dev, MDIO_EnbIn, EECtrl);
  449. mdio_delay(EECtrl);
  450. OUTL(dev, MDIO_EnbIn | MDIO_ShiftClk, EECtrl);
  451. mdio_delay(EECtrl);
  452. }
  453. return;
  454. }
  455. /* Function: ns8382x_init
  456. * Description: resets the ethernet controller chip and configures
  457. * registers and data structures required for sending and receiving packets.
  458. * Arguments: struct eth_device *dev: NIC data structure
  459. * returns: int.
  460. */
  461. static int
  462. ns8382x_init(struct eth_device *dev, bd_t * bis)
  463. {
  464. u32 config;
  465. ns8382x_reset(dev);
  466. /* Disable PME:
  467. * The PME bit is initialized from the EEPROM contents.
  468. * PCI cards probably have PME disabled, but motherboard
  469. * implementations may have PME set to enable WakeOnLan.
  470. * With PME set the chip will scan incoming packets but
  471. * nothing will be written to memory. */
  472. OUTL(dev, SavedClkRun & ~0x100, ClkRun);
  473. ns8382x_init_rxfilter(dev);
  474. ns8382x_init_txd(dev);
  475. ns8382x_init_rxd(dev);
  476. /*set up ChipConfig */
  477. config = INL(dev, ChipConfig);
  478. /*turn off 64 bit ops && Ten-bit interface
  479. * && big-endian mode && extended status */
  480. config &= ~(TBIEn | Mode1000 | T64En | D64En | M64En | BEMode | PhyDis | ExtStEn);
  481. OUTL(dev, config, ChipConfig);
  482. /* Configure the PCI bus bursts and FIFO thresholds. */
  483. tx_config = TxCarrierIgn | TxHeartIgn | TxAutoPad
  484. | TxCollRetry | TxMxdma_1024 | (0x1002);
  485. rx_config = RxMxdma_1024 | 0x20;
  486. #ifdef NS8382X_DEBUG
  487. printf("%s: Setting TxConfig Register %#08X\n", dev->name, tx_config);
  488. printf("%s: Setting RxConfig Register %#08X\n", dev->name, rx_config);
  489. #endif
  490. OUTL(dev, tx_config, TxConfig);
  491. OUTL(dev, rx_config, RxConfig);
  492. /*turn off priority queueing */
  493. OUTL(dev, 0x0, PriQueue);
  494. ns8382x_check_duplex(dev);
  495. ns8382x_set_rx_mode(dev);
  496. OUTL(dev, (RxOn | TxOn), ChipCmd);
  497. return 1;
  498. }
  499. /* Function: ns8382x_reset
  500. * Description: soft resets the controller chip
  501. * Arguments: struct eth_device *dev: NIC data structure
  502. * Returns: void.
  503. */
  504. static void
  505. ns8382x_reset(struct eth_device *dev)
  506. {
  507. OUTL(dev, ChipReset, ChipCmd);
  508. while (INL(dev, ChipCmd))
  509. /*wait until done */ ;
  510. OUTL(dev, 0, IntrMask);
  511. OUTL(dev, 0, IntrEnable);
  512. }
  513. /* Function: ns8382x_init_rxfilter
  514. * Description: sets receive filter address to our MAC address
  515. * Arguments: struct eth_device *dev: NIC data structure
  516. * returns: void.
  517. */
  518. static void
  519. ns8382x_init_rxfilter(struct eth_device *dev)
  520. {
  521. int i;
  522. for (i = 0; i < ETH_ALEN; i += 2) {
  523. OUTL(dev, i, RxFilterAddr);
  524. OUTW(dev, dev->enetaddr[i] + (dev->enetaddr[i + 1] << 8),
  525. RxFilterData);
  526. }
  527. }
  528. /* Function: ns8382x_init_txd
  529. * Description: initializes the Tx descriptor
  530. * Arguments: struct eth_device *dev: NIC data structure
  531. * returns: void.
  532. */
  533. static void
  534. ns8382x_init_txd(struct eth_device *dev)
  535. {
  536. txd.link = (u32) 0;
  537. txd.bufptr = cpu_to_le32((u32) & txb[0]);
  538. txd.cmdsts = (u32) 0;
  539. txd.extsts = (u32) 0;
  540. OUTL(dev, 0x0, TxRingPtrHi);
  541. OUTL(dev, phys_to_bus((u32)&txd), TxRingPtr);
  542. #ifdef NS8382X_DEBUG
  543. printf("ns8382x_init_txd: TX descriptor register loaded with: %#08X (&txd: %p)\n",
  544. INL(dev, TxRingPtr), &txd);
  545. #endif
  546. }
  547. /* Function: ns8382x_init_rxd
  548. * Description: initializes the Rx descriptor ring
  549. * Arguments: struct eth_device *dev: NIC data structure
  550. * Returns: void.
  551. */
  552. static void
  553. ns8382x_init_rxd(struct eth_device *dev)
  554. {
  555. int i;
  556. OUTL(dev, 0x0, RxRingPtrHi);
  557. cur_rx = 0;
  558. for (i = 0; i < NUM_RX_DESC; i++) {
  559. rxd[i].link =
  560. cpu_to_le32((i + 1 <
  561. NUM_RX_DESC) ? (u32) & rxd[i +
  562. 1] : (u32) &
  563. rxd[0]);
  564. rxd[i].extsts = cpu_to_le32((u32) 0x0);
  565. rxd[i].cmdsts = cpu_to_le32((u32) RX_BUF_SIZE);
  566. rxd[i].bufptr = cpu_to_le32((u32) & rxb[i * RX_BUF_SIZE]);
  567. #ifdef NS8382X_DEBUG
  568. printf
  569. ("ns8382x_init_rxd: rxd[%d]=%p link=%X cmdsts=%X bufptr=%X\n",
  570. i, &rxd[i], le32_to_cpu(rxd[i].link),
  571. le32_to_cpu(rxd[i].cmdsts), le32_to_cpu(rxd[i].bufptr));
  572. #endif
  573. }
  574. OUTL(dev, phys_to_bus((u32) & rxd), RxRingPtr);
  575. #ifdef NS8382X_DEBUG
  576. printf("ns8382x_init_rxd: RX descriptor register loaded with: %X\n",
  577. INL(dev, RxRingPtr));
  578. #endif
  579. }
  580. /* Function: ns8382x_set_rx_mode
  581. * Description:
  582. * sets the receive mode to accept all broadcast packets and packets
  583. * with our MAC address, and reject all multicast packets.
  584. * Arguments: struct eth_device *dev: NIC data structure
  585. * Returns: void.
  586. */
  587. static void
  588. ns8382x_set_rx_mode(struct eth_device *dev)
  589. {
  590. u32 rx_mode = 0x0;
  591. /*spec says RxFilterEnable has to be 0 for rest of
  592. * this stuff to be properly configured. Linux driver
  593. * seems to support this*/
  594. /* OUTL(dev, rx_mode, RxFilterAddr);*/
  595. rx_mode = (RxFilterEnable | AcceptAllBroadcast | AcceptPerfectMatch);
  596. OUTL(dev, rx_mode, RxFilterAddr);
  597. printf("ns8382x_set_rx_mode: set to %X\n", rx_mode);
  598. /*now we turn RxFilterEnable back on */
  599. /*rx_mode |= RxFilterEnable;
  600. OUTL(dev, rx_mode, RxFilterAddr);*/
  601. }
  602. static void
  603. ns8382x_check_duplex(struct eth_device *dev)
  604. {
  605. int gig = 0;
  606. int hun = 0;
  607. int duplex = 0;
  608. int config = (INL(dev, ChipConfig) ^ SpeedStatus_Polarity);
  609. duplex = (config & FullDuplex) ? 1 : 0;
  610. gig = (config & GigSpeed) ? 1 : 0;
  611. hun = (config & HundSpeed) ? 1 : 0;
  612. #ifdef NS8382X_DEBUG
  613. printf("%s: Setting 10%s %s-duplex based on negotiated link"
  614. " capability.\n", dev->name, (gig) ? "00" : (hun) ? "0" : "",
  615. duplex ? "full" : "half");
  616. #endif
  617. if (duplex) {
  618. rx_config |= RxAcceptTx;
  619. tx_config |= (TxCarrierIgn | TxHeartIgn);
  620. } else {
  621. rx_config &= ~RxAcceptTx;
  622. tx_config &= ~(TxCarrierIgn | TxHeartIgn);
  623. }
  624. #ifdef NS8382X_DEBUG
  625. printf("%s: Resetting TxConfig Register %#08X\n", dev->name, tx_config);
  626. printf("%s: Resetting RxConfig Register %#08X\n", dev->name, rx_config);
  627. #endif
  628. OUTL(dev, tx_config, TxConfig);
  629. OUTL(dev, rx_config, RxConfig);
  630. /*if speed is 10 or 100, remove MODE1000,
  631. * if it's 1000, then set it */
  632. config = INL(dev, ChipConfig);
  633. if (gig)
  634. config |= Mode1000;
  635. else
  636. config &= ~Mode1000;
  637. #ifdef NS8382X_DEBUG
  638. printf("%s: %setting Mode1000\n", dev->name, (gig) ? "S" : "Uns");
  639. #endif
  640. OUTL(dev, config, ChipConfig);
  641. }
  642. /* Function: ns8382x_send
  643. * Description: transmits a packet and waits for completion or timeout.
  644. * Returns: void. */
  645. static int
  646. ns8382x_send(struct eth_device *dev, volatile void *packet, int length)
  647. {
  648. u32 i, status = 0;
  649. vu_long tx_stat = 0;
  650. /* Stop the transmitter */
  651. OUTL(dev, TxOff, ChipCmd);
  652. #ifdef NS8382X_DEBUG
  653. printf("ns8382x_send: sending %d bytes\n", (int)length);
  654. #endif
  655. /* set the transmit buffer descriptor and enable Transmit State Machine */
  656. txd.link = cpu_to_le32(0x0);
  657. txd.bufptr = cpu_to_le32(phys_to_bus((u32)packet));
  658. txd.extsts = cpu_to_le32(0x0);
  659. txd.cmdsts = cpu_to_le32(DescOwn | length);
  660. /* load Transmit Descriptor Register */
  661. OUTL(dev, phys_to_bus((u32) & txd), TxRingPtr);
  662. #ifdef NS8382X_DEBUG
  663. printf("ns8382x_send: TX descriptor register loaded with: %#08X\n",
  664. INL(dev, TxRingPtr));
  665. printf("\ttxd.link:%X\tbufp:%X\texsts:%X\tcmdsts:%X\n",
  666. le32_to_cpu(txd.link), le32_to_cpu(txd.bufptr),
  667. le32_to_cpu(txd.extsts), le32_to_cpu(txd.cmdsts));
  668. #endif
  669. /* restart the transmitter */
  670. OUTL(dev, TxOn, ChipCmd);
  671. for (i = 0; (tx_stat = le32_to_cpu(txd.cmdsts)) & DescOwn; i++) {
  672. if (i >= TOUT_LOOP) {
  673. printf ("%s: tx error buffer not ready: txd.cmdsts %#lX\n",
  674. dev->name, tx_stat);
  675. goto Done;
  676. }
  677. }
  678. if (!(tx_stat & DescPktOK)) {
  679. printf("ns8382x_send: Transmit error, Tx status %lX.\n", tx_stat);
  680. goto Done;
  681. }
  682. #ifdef NS8382X_DEBUG
  683. printf("ns8382x_send: tx_stat: %#08X\n", tx_stat);
  684. #endif
  685. status = 1;
  686. Done:
  687. return status;
  688. }
  689. /* Function: ns8382x_poll
  690. * Description: checks for a received packet and returns it if found.
  691. * Arguments: struct eth_device *dev: NIC data structure
  692. * Returns: 1 if packet was received.
  693. * 0 if no packet was received.
  694. * Side effects:
  695. * Returns (copies) the packet to the array dev->packet.
  696. * Returns the length of the packet.
  697. */
  698. static int
  699. ns8382x_poll(struct eth_device *dev)
  700. {
  701. int retstat = 0;
  702. int length = 0;
  703. vu_long rx_status = le32_to_cpu(rxd[cur_rx].cmdsts);
  704. if (!(rx_status & (u32) DescOwn))
  705. return retstat;
  706. #ifdef NS8382X_DEBUG
  707. printf("ns8382x_poll: got a packet: cur_rx:%u, status:%lx\n",
  708. cur_rx, rx_status);
  709. #endif
  710. length = (rx_status & DSIZE) - CRC_SIZE;
  711. if ((rx_status & (DescMore | DescPktOK | DescRxLong)) != DescPktOK) {
  712. /* corrupted packet received */
  713. printf("ns8382x_poll: Corrupted packet, status:%lx\n", rx_status);
  714. retstat = 0;
  715. } else {
  716. /* give packet to higher level routine */
  717. NetReceive((rxb + cur_rx * RX_BUF_SIZE), length);
  718. retstat = 1;
  719. }
  720. /* return the descriptor and buffer to receive ring */
  721. rxd[cur_rx].cmdsts = cpu_to_le32(RX_BUF_SIZE);
  722. rxd[cur_rx].bufptr = cpu_to_le32((u32) & rxb[cur_rx * RX_BUF_SIZE]);
  723. if (++cur_rx == NUM_RX_DESC)
  724. cur_rx = 0;
  725. /* re-enable the potentially idle receive state machine */
  726. OUTL(dev, RxOn, ChipCmd);
  727. return retstat;
  728. }
  729. /* Function: ns8382x_disable
  730. * Description: Turns off interrupts and stops Tx and Rx engines
  731. * Arguments: struct eth_device *dev: NIC data structure
  732. * Returns: void.
  733. */
  734. static void
  735. ns8382x_disable(struct eth_device *dev)
  736. {
  737. /* Disable interrupts using the mask. */
  738. OUTL(dev, 0, IntrMask);
  739. OUTL(dev, 0, IntrEnable);
  740. /* Stop the chip's Tx and Rx processes. */
  741. OUTL(dev, (RxOff | TxOff), ChipCmd);
  742. /* Restore PME enable bit */
  743. OUTL(dev, SavedClkRun, ClkRun);
  744. }