natsemi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. natsemi.c: A U-Boot driver for the NatSemi DP8381x series.
  3. Author: Mark A. Rakes (mark_rakes@vivato.net)
  4. Adapted from an Etherboot driver written by:
  5. Copyright (C) 2001 Entity Cyber, Inc.
  6. This development of this Etherboot driver was funded by
  7. Sicom Systems: http://www.sicompos.com/
  8. Author: Marty Connor (mdc@thinguin.org)
  9. Adapted from a Linux driver which was written by Donald Becker
  10. This software may be used and distributed according to the terms
  11. of the GNU Public License (GPL), incorporated herein by reference.
  12. Original Copyright Notice:
  13. Written/copyright 1999-2001 by Donald Becker.
  14. This software may be used and distributed according to the terms of
  15. the GNU General Public License (GPL), incorporated herein by reference.
  16. Drivers based on or derived from this code fall under the GPL and must
  17. retain the authorship, copyright and license notice. This file is not
  18. a complete program and may only be used when the entire operating
  19. system is licensed under the GPL. License for under other terms may be
  20. available. Contact the original author for details.
  21. The original author may be reached as becker@scyld.com, or at
  22. Scyld Computing Corporation
  23. 410 Severn Ave., Suite 210
  24. Annapolis MD 21403
  25. Support information and updates available at
  26. http://www.scyld.com/network/netsemi.html
  27. References:
  28. http://www.scyld.com/expert/100mbps.html
  29. http://www.scyld.com/expert/NWay.html
  30. Datasheet is available from:
  31. http://www.national.com/pf/DP/DP83815.html
  32. */
  33. /* Revision History
  34. * October 2002 mar 1.0
  35. * Initial U-Boot Release. Tested with Netgear FA311 board
  36. * and dp83815 chipset on custom board
  37. */
  38. /* Includes */
  39. #include <common.h>
  40. #include <malloc.h>
  41. #include <net.h>
  42. #include <asm/io.h>
  43. #include <pci.h>
  44. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
  45. defined(CONFIG_NATSEMI)
  46. /* defines */
  47. #define EEPROM_SIZE 0xb /*12 16-bit chunks, or 24 bytes*/
  48. #define DSIZE 0x00000FFF
  49. #define ETH_ALEN 6
  50. #define CRC_SIZE 4
  51. #define TOUT_LOOP 500000
  52. #define TX_BUF_SIZE 1536
  53. #define RX_BUF_SIZE 1536
  54. #define NUM_RX_DESC 4 /* Number of Rx descriptor registers. */
  55. /* Offsets to the device registers.
  56. Unlike software-only systems, device drivers interact with complex hardware.
  57. It's not useful to define symbolic names for every register bit in the
  58. device. */
  59. enum register_offsets {
  60. ChipCmd = 0x00,
  61. ChipConfig = 0x04,
  62. EECtrl = 0x08,
  63. IntrMask = 0x14,
  64. IntrEnable = 0x18,
  65. TxRingPtr = 0x20,
  66. TxConfig = 0x24,
  67. RxRingPtr = 0x30,
  68. RxConfig = 0x34,
  69. ClkRun = 0x3C,
  70. RxFilterAddr = 0x48,
  71. RxFilterData = 0x4C,
  72. SiliconRev = 0x58,
  73. PCIPM = 0x44,
  74. BasicControl = 0x80,
  75. BasicStatus = 0x84,
  76. /* These are from the spec, around page 78... on a separate table. */
  77. PGSEL = 0xCC,
  78. PMDCSR = 0xE4,
  79. TSTDAT = 0xFC,
  80. DSPCFG = 0xF4,
  81. SDCFG = 0x8C
  82. };
  83. /* Bit in ChipCmd. */
  84. enum ChipCmdBits {
  85. ChipReset = 0x100,
  86. RxReset = 0x20,
  87. TxReset = 0x10,
  88. RxOff = 0x08,
  89. RxOn = 0x04,
  90. TxOff = 0x02,
  91. TxOn = 0x01
  92. };
  93. enum ChipConfigBits {
  94. LinkSts = 0x80000000,
  95. HundSpeed = 0x40000000,
  96. FullDuplex = 0x20000000,
  97. TenPolarity = 0x10000000,
  98. AnegDone = 0x08000000,
  99. AnegEnBothBoth = 0x0000E000,
  100. AnegDis100Full = 0x0000C000,
  101. AnegEn100Both = 0x0000A000,
  102. AnegDis100Half = 0x00008000,
  103. AnegEnBothHalf = 0x00006000,
  104. AnegDis10Full = 0x00004000,
  105. AnegEn10Both = 0x00002000,
  106. DuplexMask = 0x00008000,
  107. SpeedMask = 0x00004000,
  108. AnegMask = 0x00002000,
  109. AnegDis10Half = 0x00000000,
  110. ExtPhy = 0x00001000,
  111. PhyRst = 0x00000400,
  112. PhyDis = 0x00000200,
  113. BootRomDisable = 0x00000004,
  114. BEMode = 0x00000001,
  115. };
  116. enum TxConfig_bits {
  117. TxDrthMask = 0x3f,
  118. TxFlthMask = 0x3f00,
  119. TxMxdmaMask = 0x700000,
  120. TxMxdma_512 = 0x0,
  121. TxMxdma_4 = 0x100000,
  122. TxMxdma_8 = 0x200000,
  123. TxMxdma_16 = 0x300000,
  124. TxMxdma_32 = 0x400000,
  125. TxMxdma_64 = 0x500000,
  126. TxMxdma_128 = 0x600000,
  127. TxMxdma_256 = 0x700000,
  128. TxCollRetry = 0x800000,
  129. TxAutoPad = 0x10000000,
  130. TxMacLoop = 0x20000000,
  131. TxHeartIgn = 0x40000000,
  132. TxCarrierIgn = 0x80000000
  133. };
  134. enum RxConfig_bits {
  135. RxDrthMask = 0x3e,
  136. RxMxdmaMask = 0x700000,
  137. RxMxdma_512 = 0x0,
  138. RxMxdma_4 = 0x100000,
  139. RxMxdma_8 = 0x200000,
  140. RxMxdma_16 = 0x300000,
  141. RxMxdma_32 = 0x400000,
  142. RxMxdma_64 = 0x500000,
  143. RxMxdma_128 = 0x600000,
  144. RxMxdma_256 = 0x700000,
  145. RxAcceptLong = 0x8000000,
  146. RxAcceptTx = 0x10000000,
  147. RxAcceptRunt = 0x40000000,
  148. RxAcceptErr = 0x80000000
  149. };
  150. /* Bits in the RxMode register. */
  151. enum rx_mode_bits {
  152. AcceptErr = 0x20,
  153. AcceptRunt = 0x10,
  154. AcceptBroadcast = 0xC0000000,
  155. AcceptMulticast = 0x00200000,
  156. AcceptAllMulticast = 0x20000000,
  157. AcceptAllPhys = 0x10000000,
  158. AcceptMyPhys = 0x08000000
  159. };
  160. typedef struct _BufferDesc {
  161. u32 link;
  162. vu_long cmdsts;
  163. u32 bufptr;
  164. u32 software_use;
  165. } BufferDesc;
  166. /* Bits in network_desc.status */
  167. enum desc_status_bits {
  168. DescOwn = 0x80000000, DescMore = 0x40000000, DescIntr = 0x20000000,
  169. DescNoCRC = 0x10000000, DescPktOK = 0x08000000,
  170. DescSizeMask = 0xfff,
  171. DescTxAbort = 0x04000000, DescTxFIFO = 0x02000000,
  172. DescTxCarrier = 0x01000000, DescTxDefer = 0x00800000,
  173. DescTxExcDefer = 0x00400000, DescTxOOWCol = 0x00200000,
  174. DescTxExcColl = 0x00100000, DescTxCollCount = 0x000f0000,
  175. DescRxAbort = 0x04000000, DescRxOver = 0x02000000,
  176. DescRxDest = 0x01800000, DescRxLong = 0x00400000,
  177. DescRxRunt = 0x00200000, DescRxInvalid = 0x00100000,
  178. DescRxCRC = 0x00080000, DescRxAlign = 0x00040000,
  179. DescRxLoop = 0x00020000, DesRxColl = 0x00010000,
  180. };
  181. /* Globals */
  182. #ifdef NATSEMI_DEBUG
  183. static int natsemi_debug = 0; /* 1 verbose debugging, 0 normal */
  184. #endif
  185. static u32 SavedClkRun;
  186. static unsigned int cur_rx;
  187. static unsigned int advertising;
  188. static unsigned int rx_config;
  189. static unsigned int tx_config;
  190. /* Note: transmit and receive buffers and descriptors must be
  191. longword aligned */
  192. static BufferDesc txd __attribute__ ((aligned(4)));
  193. static BufferDesc rxd[NUM_RX_DESC] __attribute__ ((aligned(4)));
  194. static unsigned char txb[TX_BUF_SIZE] __attribute__ ((aligned(4)));
  195. static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE]
  196. __attribute__ ((aligned(4)));
  197. /* Function Prototypes */
  198. #if 0
  199. static void write_eeprom(struct eth_device *dev, long addr, int location,
  200. short value);
  201. #endif
  202. static int read_eeprom(struct eth_device *dev, long addr, int location);
  203. static int mdio_read(struct eth_device *dev, int phy_id, int location);
  204. static int natsemi_init(struct eth_device *dev, bd_t * bis);
  205. static void natsemi_reset(struct eth_device *dev);
  206. static void natsemi_init_rxfilter(struct eth_device *dev);
  207. static void natsemi_init_txd(struct eth_device *dev);
  208. static void natsemi_init_rxd(struct eth_device *dev);
  209. static void natsemi_set_rx_mode(struct eth_device *dev);
  210. static void natsemi_check_duplex(struct eth_device *dev);
  211. static int natsemi_send(struct eth_device *dev, volatile void *packet,
  212. int length);
  213. static int natsemi_poll(struct eth_device *dev);
  214. static void natsemi_disable(struct eth_device *dev);
  215. static struct pci_device_id supported[] = {
  216. {PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_83815},
  217. {}
  218. };
  219. #define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
  220. #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
  221. static inline int
  222. INW(struct eth_device *dev, u_long addr)
  223. {
  224. return le16_to_cpu(*(vu_short *) (addr + dev->iobase));
  225. }
  226. static int
  227. INL(struct eth_device *dev, u_long addr)
  228. {
  229. return le32_to_cpu(*(vu_long *) (addr + dev->iobase));
  230. }
  231. static inline void
  232. OUTW(struct eth_device *dev, int command, u_long addr)
  233. {
  234. *(vu_short *) ((addr + dev->iobase)) = cpu_to_le16(command);
  235. }
  236. static inline void
  237. OUTL(struct eth_device *dev, int command, u_long addr)
  238. {
  239. *(vu_long *) ((addr + dev->iobase)) = cpu_to_le32(command);
  240. }
  241. /*
  242. * Function: natsemi_initialize
  243. *
  244. * Description: Retrieves the MAC address of the card, and sets up some
  245. * globals required by other routines, and initializes the NIC, making it
  246. * ready to send and receive packets.
  247. *
  248. * Side effects:
  249. * leaves the natsemi initialized, and ready to recieve packets.
  250. *
  251. * Returns: struct eth_device *: pointer to NIC data structure
  252. */
  253. int
  254. natsemi_initialize(bd_t * bis)
  255. {
  256. pci_dev_t devno;
  257. int card_number = 0;
  258. struct eth_device *dev;
  259. u32 iobase, status, chip_config;
  260. int i, idx = 0;
  261. int prev_eedata;
  262. u32 tmp;
  263. while (1) {
  264. /* Find PCI device(s) */
  265. if ((devno = pci_find_devices(supported, idx++)) < 0) {
  266. break;
  267. }
  268. pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &iobase);
  269. iobase &= ~0x3; /* bit 1: unused and bit 0: I/O Space Indicator */
  270. pci_write_config_dword(devno, PCI_COMMAND,
  271. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  272. /* Check if I/O accesses and Bus Mastering are enabled. */
  273. pci_read_config_dword(devno, PCI_COMMAND, &status);
  274. if (!(status & PCI_COMMAND_MEMORY)) {
  275. printf("Error: Can not enable MEM access.\n");
  276. continue;
  277. } else if (!(status & PCI_COMMAND_MASTER)) {
  278. printf("Error: Can not enable Bus Mastering.\n");
  279. continue;
  280. }
  281. dev = (struct eth_device *) malloc(sizeof *dev);
  282. sprintf(dev->name, "dp83815#%d", card_number);
  283. dev->iobase = bus_to_phys(iobase);
  284. #ifdef NATSEMI_DEBUG
  285. printf("natsemi: NatSemi ns8381[56] @ %#x\n", dev->iobase);
  286. #endif
  287. dev->priv = (void *) devno;
  288. dev->init = natsemi_init;
  289. dev->halt = natsemi_disable;
  290. dev->send = natsemi_send;
  291. dev->recv = natsemi_poll;
  292. eth_register(dev);
  293. card_number++;
  294. /* Set the latency timer for value. */
  295. pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x20);
  296. udelay(10 * 1000);
  297. /* natsemi has a non-standard PM control register
  298. * in PCI config space. Some boards apparently need
  299. * to be brought to D0 in this manner. */
  300. pci_read_config_dword(devno, PCIPM, &tmp);
  301. if (tmp & (0x03 | 0x100)) {
  302. /* D0 state, disable PME assertion */
  303. u32 newtmp = tmp & ~(0x03 | 0x100);
  304. pci_write_config_dword(devno, PCIPM, newtmp);
  305. }
  306. printf("natsemi: EEPROM contents:\n");
  307. for (i = 0; i <= EEPROM_SIZE; i++) {
  308. short eedata = read_eeprom(dev, EECtrl, i);
  309. printf(" %04hx", eedata);
  310. }
  311. printf("\n");
  312. /* get MAC address */
  313. prev_eedata = read_eeprom(dev, EECtrl, 6);
  314. for (i = 0; i < 3; i++) {
  315. int eedata = read_eeprom(dev, EECtrl, i + 7);
  316. dev->enetaddr[i*2] = (eedata << 1) + (prev_eedata >> 15);
  317. dev->enetaddr[i*2+1] = eedata >> 7;
  318. prev_eedata = eedata;
  319. }
  320. /* Reset the chip to erase any previous misconfiguration. */
  321. OUTL(dev, ChipReset, ChipCmd);
  322. advertising = mdio_read(dev, 1, 4);
  323. chip_config = INL(dev, ChipConfig);
  324. #ifdef NATSEMI_DEBUG
  325. printf("%s: Transceiver status %#08X advertising %#08X\n",
  326. dev->name, (int) INL(dev, BasicStatus), advertising);
  327. printf("%s: Transceiver default autoneg. %s 10%s %s duplex.\n",
  328. dev->name, chip_config & AnegMask ? "enabled, advertise" :
  329. "disabled, force", chip_config & SpeedMask ? "0" : "",
  330. chip_config & DuplexMask ? "full" : "half");
  331. #endif
  332. chip_config |= AnegEnBothBoth;
  333. #ifdef NATSEMI_DEBUG
  334. printf("%s: changed to autoneg. %s 10%s %s duplex.\n",
  335. dev->name, chip_config & AnegMask ? "enabled, advertise" :
  336. "disabled, force", chip_config & SpeedMask ? "0" : "",
  337. chip_config & DuplexMask ? "full" : "half");
  338. #endif
  339. /*write new autoneg bits, reset phy*/
  340. OUTL(dev, (chip_config | PhyRst), ChipConfig);
  341. /*un-reset phy*/
  342. OUTL(dev, chip_config, ChipConfig);
  343. /* Disable PME:
  344. * The PME bit is initialized from the EEPROM contents.
  345. * PCI cards probably have PME disabled, but motherboard
  346. * implementations may have PME set to enable WakeOnLan.
  347. * With PME set the chip will scan incoming packets but
  348. * nothing will be written to memory. */
  349. SavedClkRun = INL(dev, ClkRun);
  350. OUTL(dev, SavedClkRun & ~0x100, ClkRun);
  351. }
  352. return card_number;
  353. }
  354. /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
  355. The EEPROM code is for common 93c06/46 EEPROMs w/ 6bit addresses. */
  356. /* Delay between EEPROM clock transitions.
  357. No extra delay is needed with 33Mhz PCI, but future 66Mhz
  358. access may need a delay. */
  359. #define eeprom_delay(ee_addr) INL(dev, ee_addr)
  360. enum EEPROM_Ctrl_Bits {
  361. EE_ShiftClk = 0x04,
  362. EE_DataIn = 0x01,
  363. EE_ChipSelect = 0x08,
  364. EE_DataOut = 0x02
  365. };
  366. #define EE_Write0 (EE_ChipSelect)
  367. #define EE_Write1 (EE_ChipSelect | EE_DataIn)
  368. /* The EEPROM commands include the alway-set leading bit. */
  369. enum EEPROM_Cmds {
  370. EE_WrEnCmd = (4 << 6), EE_WriteCmd = (5 << 6),
  371. EE_ReadCmd = (6 << 6), EE_EraseCmd = (7 << 6),
  372. };
  373. #if 0
  374. static void
  375. write_eeprom(struct eth_device *dev, long addr, int location, short value)
  376. {
  377. int i;
  378. int ee_addr = (typeof(ee_addr))addr;
  379. short wren_cmd = EE_WrEnCmd | 0x30; /*wren is 100 + 11XXXX*/
  380. short write_cmd = location | EE_WriteCmd;
  381. #ifdef NATSEMI_DEBUG
  382. printf("write_eeprom: %08x, %04hx, %04hx\n",
  383. dev->iobase + ee_addr, write_cmd, value);
  384. #endif
  385. /* Shift the write enable command bits out. */
  386. for (i = 9; i >= 0; i--) {
  387. short cmdval = (wren_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
  388. OUTL(dev, cmdval, ee_addr);
  389. eeprom_delay(ee_addr);
  390. OUTL(dev, cmdval | EE_ShiftClk, ee_addr);
  391. eeprom_delay(ee_addr);
  392. }
  393. OUTL(dev, 0, ee_addr); /*bring chip select low*/
  394. OUTL(dev, EE_ShiftClk, ee_addr);
  395. eeprom_delay(ee_addr);
  396. /* Shift the write command bits out. */
  397. for (i = 9; i >= 0; i--) {
  398. short cmdval = (write_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
  399. OUTL(dev, cmdval, ee_addr);
  400. eeprom_delay(ee_addr);
  401. OUTL(dev, cmdval | EE_ShiftClk, ee_addr);
  402. eeprom_delay(ee_addr);
  403. }
  404. for (i = 0; i < 16; i++) {
  405. short cmdval = (value & (1 << i)) ? EE_Write1 : EE_Write0;
  406. OUTL(dev, cmdval, ee_addr);
  407. eeprom_delay(ee_addr);
  408. OUTL(dev, cmdval | EE_ShiftClk, ee_addr);
  409. eeprom_delay(ee_addr);
  410. }
  411. OUTL(dev, 0, ee_addr); /*bring chip select low*/
  412. OUTL(dev, EE_ShiftClk, ee_addr);
  413. for (i = 0; i < 200000; i++) {
  414. OUTL(dev, EE_Write0, ee_addr); /*poll for done*/
  415. if (INL(dev, ee_addr) & EE_DataOut) {
  416. break; /*finished*/
  417. }
  418. }
  419. eeprom_delay(ee_addr);
  420. /* Terminate the EEPROM access. */
  421. OUTL(dev, EE_Write0, ee_addr);
  422. OUTL(dev, 0, ee_addr);
  423. return;
  424. }
  425. #endif
  426. static int
  427. read_eeprom(struct eth_device *dev, long addr, int location)
  428. {
  429. int i;
  430. int retval = 0;
  431. int ee_addr = (typeof(ee_addr))addr;
  432. int read_cmd = location | EE_ReadCmd;
  433. OUTL(dev, EE_Write0, ee_addr);
  434. /* Shift the read command bits out. */
  435. for (i = 10; i >= 0; i--) {
  436. short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
  437. OUTL(dev, dataval, ee_addr);
  438. eeprom_delay(ee_addr);
  439. OUTL(dev, dataval | EE_ShiftClk, ee_addr);
  440. eeprom_delay(ee_addr);
  441. }
  442. OUTL(dev, EE_ChipSelect, ee_addr);
  443. eeprom_delay(ee_addr);
  444. for (i = 0; i < 16; i++) {
  445. OUTL(dev, EE_ChipSelect | EE_ShiftClk, ee_addr);
  446. eeprom_delay(ee_addr);
  447. retval |= (INL(dev, ee_addr) & EE_DataOut) ? 1 << i : 0;
  448. OUTL(dev, EE_ChipSelect, ee_addr);
  449. eeprom_delay(ee_addr);
  450. }
  451. /* Terminate the EEPROM access. */
  452. OUTL(dev, EE_Write0, ee_addr);
  453. OUTL(dev, 0, ee_addr);
  454. #ifdef NATSEMI_DEBUG
  455. if (natsemi_debug)
  456. printf("read_eeprom: %08x, %08x, retval %08x\n",
  457. dev->iobase + ee_addr, read_cmd, retval);
  458. #endif
  459. return retval;
  460. }
  461. /* MII transceiver control section.
  462. The 83815 series has an internal transceiver, and we present the
  463. management registers as if they were MII connected. */
  464. static int
  465. mdio_read(struct eth_device *dev, int phy_id, int location)
  466. {
  467. if (phy_id == 1 && location < 32)
  468. return INL(dev, BasicControl+(location<<2))&0xffff;
  469. else
  470. return 0xffff;
  471. }
  472. /* Function: natsemi_init
  473. *
  474. * Description: resets the ethernet controller chip and configures
  475. * registers and data structures required for sending and receiving packets.
  476. *
  477. * Arguments: struct eth_device *dev: NIC data structure
  478. *
  479. * returns: int.
  480. */
  481. static int
  482. natsemi_init(struct eth_device *dev, bd_t * bis)
  483. {
  484. natsemi_reset(dev);
  485. /* Disable PME:
  486. * The PME bit is initialized from the EEPROM contents.
  487. * PCI cards probably have PME disabled, but motherboard
  488. * implementations may have PME set to enable WakeOnLan.
  489. * With PME set the chip will scan incoming packets but
  490. * nothing will be written to memory. */
  491. OUTL(dev, SavedClkRun & ~0x100, ClkRun);
  492. natsemi_init_rxfilter(dev);
  493. natsemi_init_txd(dev);
  494. natsemi_init_rxd(dev);
  495. /* Configure the PCI bus bursts and FIFO thresholds. */
  496. tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 | (0x1002);
  497. rx_config = RxMxdma_256 | 0x20;
  498. #ifdef NATSEMI_DEBUG
  499. printf("%s: Setting TxConfig Register %#08X\n", dev->name, tx_config);
  500. printf("%s: Setting RxConfig Register %#08X\n", dev->name, rx_config);
  501. #endif
  502. OUTL(dev, tx_config, TxConfig);
  503. OUTL(dev, rx_config, RxConfig);
  504. natsemi_check_duplex(dev);
  505. natsemi_set_rx_mode(dev);
  506. OUTL(dev, (RxOn | TxOn), ChipCmd);
  507. return 1;
  508. }
  509. /*
  510. * Function: natsemi_reset
  511. *
  512. * Description: soft resets the controller chip
  513. *
  514. * Arguments: struct eth_device *dev: NIC data structure
  515. *
  516. * Returns: void.
  517. */
  518. static void
  519. natsemi_reset(struct eth_device *dev)
  520. {
  521. OUTL(dev, ChipReset, ChipCmd);
  522. /* On page 78 of the spec, they recommend some settings for "optimum
  523. performance" to be done in sequence. These settings optimize some
  524. of the 100Mbit autodetection circuitry. Also, we only want to do
  525. this for rev C of the chip. */
  526. if (INL(dev, SiliconRev) == 0x302) {
  527. OUTW(dev, 0x0001, PGSEL);
  528. OUTW(dev, 0x189C, PMDCSR);
  529. OUTW(dev, 0x0000, TSTDAT);
  530. OUTW(dev, 0x5040, DSPCFG);
  531. OUTW(dev, 0x008C, SDCFG);
  532. }
  533. /* Disable interrupts using the mask. */
  534. OUTL(dev, 0, IntrMask);
  535. OUTL(dev, 0, IntrEnable);
  536. }
  537. /* Function: natsemi_init_rxfilter
  538. *
  539. * Description: sets receive filter address to our MAC address
  540. *
  541. * Arguments: struct eth_device *dev: NIC data structure
  542. *
  543. * returns: void.
  544. */
  545. static void
  546. natsemi_init_rxfilter(struct eth_device *dev)
  547. {
  548. int i;
  549. for (i = 0; i < ETH_ALEN; i += 2) {
  550. OUTL(dev, i, RxFilterAddr);
  551. OUTW(dev, dev->enetaddr[i] + (dev->enetaddr[i + 1] << 8),
  552. RxFilterData);
  553. }
  554. }
  555. /*
  556. * Function: natsemi_init_txd
  557. *
  558. * Description: initializes the Tx descriptor
  559. *
  560. * Arguments: struct eth_device *dev: NIC data structure
  561. *
  562. * returns: void.
  563. */
  564. static void
  565. natsemi_init_txd(struct eth_device *dev)
  566. {
  567. txd.link = (u32) 0;
  568. txd.cmdsts = (u32) 0;
  569. txd.bufptr = (u32) & txb[0];
  570. /* load Transmit Descriptor Register */
  571. OUTL(dev, (u32) & txd, TxRingPtr);
  572. #ifdef NATSEMI_DEBUG
  573. printf("natsemi_init_txd: TX descriptor reg loaded with: %#08X\n",
  574. INL(dev, TxRingPtr));
  575. #endif
  576. }
  577. /* Function: natsemi_init_rxd
  578. *
  579. * Description: initializes the Rx descriptor ring
  580. *
  581. * Arguments: struct eth_device *dev: NIC data structure
  582. *
  583. * Returns: void.
  584. */
  585. static void
  586. natsemi_init_rxd(struct eth_device *dev)
  587. {
  588. int i;
  589. cur_rx = 0;
  590. /* init RX descriptor */
  591. for (i = 0; i < NUM_RX_DESC; i++) {
  592. rxd[i].link =
  593. cpu_to_le32((i + 1 <
  594. NUM_RX_DESC) ? (u32) & rxd[i +
  595. 1] : (u32) &
  596. rxd[0]);
  597. rxd[i].cmdsts = cpu_to_le32((u32) RX_BUF_SIZE);
  598. rxd[i].bufptr = cpu_to_le32((u32) & rxb[i * RX_BUF_SIZE]);
  599. #ifdef NATSEMI_DEBUG
  600. printf
  601. ("natsemi_init_rxd: rxd[%d]=%p link=%X cmdsts=%lX bufptr=%X\n",
  602. i, &rxd[i], le32_to_cpu(rxd[i].link),
  603. rxd[i].cmdsts, rxd[i].bufptr);
  604. #endif
  605. }
  606. /* load Receive Descriptor Register */
  607. OUTL(dev, (u32) & rxd[0], RxRingPtr);
  608. #ifdef NATSEMI_DEBUG
  609. printf("natsemi_init_rxd: RX descriptor register loaded with: %X\n",
  610. INL(dev, RxRingPtr));
  611. #endif
  612. }
  613. /* Function: natsemi_set_rx_mode
  614. *
  615. * Description:
  616. * sets the receive mode to accept all broadcast packets and packets
  617. * with our MAC address, and reject all multicast packets.
  618. *
  619. * Arguments: struct eth_device *dev: NIC data structure
  620. *
  621. * Returns: void.
  622. */
  623. static void
  624. natsemi_set_rx_mode(struct eth_device *dev)
  625. {
  626. u32 rx_mode = AcceptBroadcast | AcceptMyPhys;
  627. OUTL(dev, rx_mode, RxFilterAddr);
  628. }
  629. static void
  630. natsemi_check_duplex(struct eth_device *dev)
  631. {
  632. int duplex = INL(dev, ChipConfig) & FullDuplex ? 1 : 0;
  633. #ifdef NATSEMI_DEBUG
  634. printf("%s: Setting %s-duplex based on negotiated link"
  635. " capability.\n", dev->name, duplex ? "full" : "half");
  636. #endif
  637. if (duplex) {
  638. rx_config |= RxAcceptTx;
  639. tx_config |= (TxCarrierIgn | TxHeartIgn);
  640. } else {
  641. rx_config &= ~RxAcceptTx;
  642. tx_config &= ~(TxCarrierIgn | TxHeartIgn);
  643. }
  644. OUTL(dev, tx_config, TxConfig);
  645. OUTL(dev, rx_config, RxConfig);
  646. }
  647. /* Function: natsemi_send
  648. *
  649. * Description: transmits a packet and waits for completion or timeout.
  650. *
  651. * Returns: void. */
  652. static int
  653. natsemi_send(struct eth_device *dev, volatile void *packet, int length)
  654. {
  655. u32 i, status = 0;
  656. u32 tx_status = 0;
  657. /* Stop the transmitter */
  658. OUTL(dev, TxOff, ChipCmd);
  659. #ifdef NATSEMI_DEBUG
  660. if (natsemi_debug)
  661. printf("natsemi_send: sending %d bytes\n", (int) length);
  662. #endif
  663. /* set the transmit buffer descriptor and enable Transmit State Machine */
  664. txd.link = cpu_to_le32(0);
  665. txd.bufptr = cpu_to_le32(phys_to_bus((u32) packet));
  666. txd.cmdsts = cpu_to_le32(DescOwn | length);
  667. /* load Transmit Descriptor Register */
  668. OUTL(dev, phys_to_bus((u32) & txd), TxRingPtr);
  669. #ifdef NATSEMI_DEBUG
  670. if (natsemi_debug)
  671. printf("natsemi_send: TX descriptor register loaded with: %#08X\n",
  672. INL(dev, TxRingPtr));
  673. #endif
  674. /* restart the transmitter */
  675. OUTL(dev, TxOn, ChipCmd);
  676. for (i = 0;
  677. ((vu_long)tx_status = le32_to_cpu(txd.cmdsts)) & DescOwn;
  678. i++) {
  679. if (i >= TOUT_LOOP) {
  680. printf
  681. ("%s: tx error buffer not ready: txd.cmdsts == %#X\n",
  682. dev->name, tx_status);
  683. goto Done;
  684. }
  685. }
  686. if (!(tx_status & DescPktOK)) {
  687. printf("natsemi_send: Transmit error, Tx status %X.\n",
  688. tx_status);
  689. goto Done;
  690. }
  691. status = 1;
  692. Done:
  693. return status;
  694. }
  695. /* Function: natsemi_poll
  696. *
  697. * Description: checks for a received packet and returns it if found.
  698. *
  699. * Arguments: struct eth_device *dev: NIC data structure
  700. *
  701. * Returns: 1 if packet was received.
  702. * 0 if no packet was received.
  703. *
  704. * Side effects:
  705. * Returns (copies) the packet to the array dev->packet.
  706. * Returns the length of the packet.
  707. */
  708. static int
  709. natsemi_poll(struct eth_device *dev)
  710. {
  711. int retstat = 0;
  712. int length = 0;
  713. u32 rx_status = le32_to_cpu(rxd[cur_rx].cmdsts);
  714. if (!(rx_status & (u32) DescOwn))
  715. return retstat;
  716. #ifdef NATSEMI_DEBUG
  717. if (natsemi_debug)
  718. printf("natsemi_poll: got a packet: cur_rx:%d, status:%X\n",
  719. cur_rx, rx_status);
  720. #endif
  721. length = (rx_status & DSIZE) - CRC_SIZE;
  722. if ((rx_status & (DescMore | DescPktOK | DescRxLong)) != DescPktOK) {
  723. printf
  724. ("natsemi_poll: Corrupted packet received, buffer status = %X\n",
  725. rx_status);
  726. retstat = 0;
  727. } else { /* give packet to higher level routine */
  728. NetReceive((rxb + cur_rx * RX_BUF_SIZE), length);
  729. retstat = 1;
  730. }
  731. /* return the descriptor and buffer to receive ring */
  732. rxd[cur_rx].cmdsts = cpu_to_le32(RX_BUF_SIZE);
  733. rxd[cur_rx].bufptr = cpu_to_le32((u32) & rxb[cur_rx * RX_BUF_SIZE]);
  734. if (++cur_rx == NUM_RX_DESC)
  735. cur_rx = 0;
  736. /* re-enable the potentially idle receive state machine */
  737. OUTL(dev, RxOn, ChipCmd);
  738. return retstat;
  739. }
  740. /* Function: natsemi_disable
  741. *
  742. * Description: Turns off interrupts and stops Tx and Rx engines
  743. *
  744. * Arguments: struct eth_device *dev: NIC data structure
  745. *
  746. * Returns: void.
  747. */
  748. static void
  749. natsemi_disable(struct eth_device *dev)
  750. {
  751. /* Disable interrupts using the mask. */
  752. OUTL(dev, 0, IntrMask);
  753. OUTL(dev, 0, IntrEnable);
  754. /* Stop the chip's Tx and Rx processes. */
  755. OUTL(dev, RxOff | TxOff, ChipCmd);
  756. /* Restore PME enable bit */
  757. OUTL(dev, SavedClkRun, ClkRun);
  758. }
  759. #endif