ns8382x.c 23 KB

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