Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

natsemi.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /* natsemi.c - gPXE driver for the NatSemi DP8381x series. */
  2. /*
  3. natsemi.c: An Etherboot driver for the NatSemi DP8381x series.
  4. Copyright (C) 2001 Entity Cyber, Inc.
  5. This development of this Etherboot driver was funded by
  6. Sicom Systems: http://www.sicompos.com/
  7. Author: Marty Connor (mdc@thinguin.org)
  8. Adapted from a Linux driver which was written by Donald Becker
  9. This software may be used and distributed according to the terms
  10. of the GNU Public License (GPL), incorporated herein by reference.
  11. Original Copyright Notice:
  12. Written/copyright 1999-2001 by Donald Becker.
  13. This software may be used and distributed according to the terms of
  14. the GNU General Public License (GPL), incorporated herein by reference.
  15. Drivers based on or derived from this code fall under the GPL and must
  16. retain the authorship, copyright and license notice. This file is not
  17. a complete program and may only be used when the entire operating
  18. system is licensed under the GPL. License for under other terms may be
  19. available. Contact the original author for details.
  20. The original author may be reached as becker@scyld.com, or at
  21. Scyld Computing Corporation
  22. 410 Severn Ave., Suite 210
  23. Annapolis MD 21403
  24. Support information and updates available at
  25. http://www.scyld.com/network/netsemi.html
  26. References:
  27. http://www.scyld.com/expert/100mbps.html
  28. http://www.scyld.com/expert/NWay.html
  29. Datasheet is available from:
  30. http://www.national.com/pf/DP/DP83815.html
  31. */
  32. /* Revision History */
  33. /*
  34. 02 JUL 2007 Udayan Kumar 1.2 ported the driver from etherboot to gPXE API.
  35. Fully rewritten,adapting the old driver.
  36. Added a circular buffer for transmit and receive.
  37. transmit routine will not wait for transmission to finish.
  38. poll routine deals with it.
  39. 13 Dec 2003 timlegge 1.1 Enabled Multicast Support
  40. 29 May 2001 mdc 1.0
  41. Initial Release. Tested with Netgear FA311 and FA312 boards
  42. */
  43. #include <stdint.h>
  44. #include <pic8259.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <io.h>
  48. #include <errno.h>
  49. #include <timer.h>
  50. #include <byteswap.h>
  51. #include <gpxe/pci.h>
  52. #include <gpxe/if_ether.h>
  53. #include <gpxe/ethernet.h>
  54. #include <gpxe/iobuf.h>
  55. #include <gpxe/netdevice.h>
  56. #include <gpxe/spi_bit.h>
  57. #include <gpxe/threewire.h>
  58. #include <gpxe/nvo.h>
  59. #include <mii.h>
  60. #define TX_RING_SIZE 4
  61. #define NUM_RX_DESC 4
  62. #define RX_BUF_SIZE 1536
  63. #define OWN 0x80000000
  64. #define DSIZE 0x00000FFF
  65. #define CRC_SIZE 4
  66. struct natsemi_tx {
  67. uint32_t link;
  68. uint32_t cmdsts;
  69. uint32_t bufptr;
  70. };
  71. struct natsemi_rx {
  72. uint32_t link;
  73. uint32_t cmdsts;
  74. uint32_t bufptr;
  75. };
  76. struct natsemi_nic {
  77. unsigned short ioaddr;
  78. unsigned short tx_cur;
  79. unsigned short tx_dirty;
  80. unsigned short rx_cur;
  81. struct natsemi_tx tx[TX_RING_SIZE];
  82. struct natsemi_rx rx[NUM_RX_DESC];
  83. /* need to add iobuf as we cannot free iobuf->data in close without this
  84. * alternatively substracting sizeof(head) and sizeof(list_head) can also
  85. * give the same.
  86. */
  87. struct io_buffer *iobuf[NUM_RX_DESC];
  88. /* netdev_tx_complete needs pointer to the iobuf of the data so as to free
  89. * it from the memory.
  90. */
  91. struct io_buffer *tx_iobuf[TX_RING_SIZE];
  92. struct spi_bit_basher spibit;
  93. struct spi_device eeprom;
  94. struct nvo_block nvo;
  95. };
  96. /*
  97. * Support for fibre connections on Am79C874:
  98. * This phy needs a special setup when connected to a fibre cable.
  99. * http://www.amd.com/files/connectivitysolutions/networking/archivednetworking/22235.pdf
  100. */
  101. #define PHYID_AM79C874 0x0022561b
  102. enum {
  103. MII_MCTRL = 0x15, /* mode control register */
  104. MII_FX_SEL = 0x0001, /* 100BASE-FX (fiber) */
  105. MII_EN_SCRM = 0x0004, /* enable scrambler (tp) */
  106. };
  107. /* values we might find in the silicon revision register */
  108. #define SRR_DP83815_C 0x0302
  109. #define SRR_DP83815_D 0x0403
  110. #define SRR_DP83816_A4 0x0504
  111. #define SRR_DP83816_A5 0x0505
  112. /* NATSEMI: Offsets to the device registers.
  113. * Unlike software-only systems, device drivers interact with complex hardware.
  114. * It's not useful to define symbolic names for every register bit in the
  115. * device.
  116. */
  117. enum register_offsets {
  118. ChipCmd = 0x00,
  119. ChipConfig = 0x04,
  120. EECtrl = 0x08,
  121. PCIBusCfg = 0x0C,
  122. IntrStatus = 0x10,
  123. IntrMask = 0x14,
  124. IntrEnable = 0x18,
  125. TxRingPtr = 0x20,
  126. TxConfig = 0x24,
  127. RxRingPtr = 0x30,
  128. RxConfig = 0x34,
  129. ClkRun = 0x3C,
  130. WOLCmd = 0x40,
  131. PauseCmd = 0x44,
  132. RxFilterAddr = 0x48,
  133. RxFilterData = 0x4C,
  134. BootRomAddr = 0x50,
  135. BootRomData = 0x54,
  136. SiliconRev = 0x58,
  137. StatsCtrl = 0x5C,
  138. StatsData = 0x60,
  139. RxPktErrs = 0x60,
  140. RxMissed = 0x68,
  141. RxCRCErrs = 0x64,
  142. PCIPM = 0x44,
  143. PhyStatus = 0xC0,
  144. MIntrCtrl = 0xC4,
  145. MIntrStatus = 0xC8,
  146. /* These are from the spec, around page 78... on a separate table.
  147. */
  148. PGSEL = 0xCC,
  149. PMDCSR = 0xE4,
  150. TSTDAT = 0xFC,
  151. DSPCFG = 0xF4,
  152. SDCFG = 0x8C,
  153. BasicControl = 0x80,
  154. BasicStatus = 0x84
  155. };
  156. /* the values for the 'magic' registers above (PGSEL=1) */
  157. #define PMDCSR_VAL 0x189c /* enable preferred adaptation circuitry */
  158. #define TSTDAT_VAL 0x0
  159. #define DSPCFG_VAL 0x5040
  160. #define SDCFG_VAL 0x008c /* set voltage thresholds for Signal Detect */
  161. #define DSPCFG_LOCK 0x20 /* coefficient lock bit in DSPCFG */
  162. #define DSPCFG_COEF 0x1000 /* see coefficient (in TSTDAT) bit in DSPCFG */
  163. #define TSTDAT_FIXED 0xe8 /* magic number for bad coefficients */
  164. /* Bit in ChipCmd.
  165. */
  166. enum ChipCmdBits {
  167. ChipReset = 0x100,
  168. RxReset = 0x20,
  169. TxReset = 0x10,
  170. RxOff = 0x08,
  171. RxOn = 0x04,
  172. TxOff = 0x02,
  173. TxOn = 0x01
  174. };
  175. enum ChipConfig_bits {
  176. CfgPhyDis = 0x200,
  177. CfgPhyRst = 0x400,
  178. CfgExtPhy = 0x1000,
  179. CfgAnegEnable = 0x2000,
  180. CfgAneg100 = 0x4000,
  181. CfgAnegFull = 0x8000,
  182. CfgAnegDone = 0x8000000,
  183. CfgFullDuplex = 0x20000000,
  184. CfgSpeed100 = 0x40000000,
  185. CfgLink = 0x80000000,
  186. };
  187. /* Bits in the RxMode register.
  188. */
  189. enum rx_mode_bits {
  190. AcceptErr = 0x20,
  191. AcceptRunt = 0x10,
  192. AcceptBroadcast = 0xC0000000,
  193. AcceptMulticast = 0x00200000,
  194. AcceptAllMulticast = 0x20000000,
  195. AcceptAllPhys = 0x10000000,
  196. AcceptMyPhys = 0x08000000,
  197. RxFilterEnable = 0x80000000
  198. };
  199. /* Bits in network_desc.status
  200. */
  201. enum desc_status_bits {
  202. DescOwn = 0x80000000,
  203. DescMore = 0x40000000,
  204. DescIntr = 0x20000000,
  205. DescNoCRC = 0x10000000,
  206. DescPktOK = 0x08000000,
  207. RxTooLong = 0x00400000
  208. };
  209. /*Bits in Interrupt Mask register
  210. */
  211. enum Intr_mask_register_bits {
  212. RxOk = 0x001,
  213. RxErr = 0x004,
  214. TxOk = 0x040,
  215. TxErr = 0x100
  216. };
  217. /* EEPROM access , values are devices specific
  218. */
  219. #define EE_CS 0x08 /* EEPROM chip select */
  220. #define EE_SK 0x04 /* EEPROM shift clock */
  221. #define EE_DI 0x01 /* Data in */
  222. #define EE_DO 0x02 /* Data out */
  223. /* Offsets within EEPROM (these are word offsets)
  224. */
  225. #define EE_MAC 7
  226. #define EE_REG EECtrl
  227. static uint32_t SavedClkRun;
  228. static const uint8_t nat_ee_bits[] = {
  229. [SPI_BIT_SCLK] = EE_SK,
  230. [SPI_BIT_MOSI] = EE_DI,
  231. [SPI_BIT_MISO] = EE_DO,
  232. [SPI_BIT_SS(0)] = EE_CS,
  233. };
  234. static int nat_spi_read_bit ( struct bit_basher *basher,
  235. unsigned int bit_id ) {
  236. struct natsemi_nic *nat = container_of ( basher, struct natsemi_nic,
  237. spibit.basher );
  238. uint8_t mask = nat_ee_bits[bit_id];
  239. uint8_t eereg;
  240. eereg = inb ( nat->ioaddr + EE_REG );
  241. return ( eereg & mask );
  242. }
  243. static void nat_spi_write_bit ( struct bit_basher *basher,
  244. unsigned int bit_id, unsigned long data ) {
  245. struct natsemi_nic *nat = container_of ( basher, struct natsemi_nic,
  246. spibit.basher );
  247. uint8_t mask = nat_ee_bits[bit_id];
  248. uint8_t eereg;
  249. eereg = inb ( nat->ioaddr + EE_REG );
  250. eereg &= ~mask;
  251. eereg |= ( data & mask );
  252. outb ( eereg, nat->ioaddr + EE_REG );
  253. }
  254. static struct bit_basher_operations nat_basher_ops = {
  255. .read = nat_spi_read_bit,
  256. .write = nat_spi_write_bit,
  257. };
  258. /* It looks that this portion of EEPROM can be used for
  259. * non-volatile stored options. Data sheet does not talk about this region.
  260. * Currently it is not working. But with some efforts it can.
  261. */
  262. static struct nvo_fragment nat_nvo_fragments[] = {
  263. { 0x0c, 0x68 },
  264. { 0, 0 }
  265. };
  266. /*
  267. * Set up for EEPROM access
  268. *
  269. * @v NAT NATSEMI NIC
  270. */
  271. void nat_init_eeprom ( struct natsemi_nic *nat ) {
  272. /* Initialise three-wire bus
  273. */
  274. nat->spibit.basher.op = &nat_basher_ops;
  275. nat->spibit.bus.mode = SPI_MODE_THREEWIRE;
  276. nat->spibit.endianness = SPI_BIT_LITTLE_ENDIAN;
  277. init_spi_bit_basher ( &nat->spibit );
  278. /*natsemi DP 83815 only supports at93c46
  279. */
  280. init_at93c46 ( &nat->eeprom, 16 );
  281. nat->eeprom.bus = &nat->spibit.bus;
  282. nat->nvo.nvs = &nat->eeprom.nvs;
  283. nat->nvo.fragments = nat_nvo_fragments;
  284. }
  285. /*
  286. * Reset NIC
  287. *
  288. * @v NATSEMI NIC
  289. *
  290. * Issues a hardware reset and waits for the reset to complete.
  291. */
  292. static void nat_reset ( struct natsemi_nic *nat ) {
  293. int i;
  294. /* Reset chip
  295. */
  296. outl ( ChipReset, nat->ioaddr + ChipCmd );
  297. mdelay ( 10 );
  298. nat->tx_dirty = 0;
  299. nat->tx_cur = 0;
  300. for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
  301. nat->tx[i].link = 0;
  302. nat->tx[i].cmdsts = 0;
  303. nat->tx[i].bufptr = 0;
  304. }
  305. nat->rx_cur = 0;
  306. outl ( virt_to_bus( &nat->tx[0] ),nat->ioaddr + TxRingPtr );
  307. outl ( virt_to_bus( &nat->rx[0] ),nat->ioaddr + RxRingPtr );
  308. outl ( TxOff|RxOff, nat->ioaddr + ChipCmd );
  309. /* Restore PME enable bit
  310. */
  311. outl ( SavedClkRun, nat->ioaddr + ClkRun );
  312. }
  313. static int mdio_read(struct net_device *netdev, int reg) {
  314. struct natsemi_nic *nat = netdev->priv;
  315. /* The 83815 series has two ports:
  316. * - an internal transceiver
  317. * - an external mii bus
  318. */
  319. return inw(nat->ioaddr+BasicControl+(reg<<2));
  320. }
  321. static void mdio_write(struct net_device *netdev, int reg, u16 data) {
  322. struct natsemi_nic *nat = netdev->priv;
  323. /* The 83815 series has an internal transceiver; handle separately */
  324. writew(data, nat->ioaddr+BasicControl+(reg<<2));
  325. }
  326. static void init_phy_fixup(struct net_device *netdev) {
  327. struct natsemi_nic *nat = netdev->priv;
  328. int i;
  329. u32 cfg;
  330. u16 tmp;
  331. uint16_t advertising;
  332. int mii;
  333. /* restore stuff lost when power was out */
  334. tmp = mdio_read(netdev, MII_BMCR);
  335. advertising= mdio_read(netdev, MII_ADVERTISE);
  336. // if (np->autoneg == AUTONEG_ENABLE) {
  337. /* renegotiate if something changed */
  338. if ((tmp & BMCR_ANENABLE) == 0
  339. || advertising != mdio_read(netdev, MII_ADVERTISE))
  340. {
  341. /* turn on autonegotiation and force negotiation */
  342. tmp |= (BMCR_ANENABLE | BMCR_ANRESTART);
  343. mdio_write(netdev, MII_ADVERTISE, advertising);
  344. }
  345. // } else {
  346. /* turn off auto negotiation, set speed and duplexity */
  347. // tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
  348. // if (np->speed == SPEED_100)
  349. /// tmp |= BMCR_SPEED100;
  350. // if (np->duplex == DUPLEX_FULL)
  351. // tmp |= BMCR_FULLDPLX;
  352. /*
  353. * Note: there is no good way to inform the link partner
  354. * that our capabilities changed. The user has to unplug
  355. * and replug the network cable after some changes, e.g.
  356. * after switching from 10HD, autoneg off to 100 HD,
  357. * autoneg off.
  358. */
  359. // }
  360. mdio_write(netdev, MII_BMCR, tmp);
  361. inl(nat->ioaddr + ChipConfig);
  362. udelay(1);
  363. /* find out what phy this is */
  364. mii = (mdio_read(netdev, MII_PHYSID1) << 16)
  365. + mdio_read(netdev, MII_PHYSID2);
  366. /* handle external phys here */
  367. switch (mii) {
  368. case PHYID_AM79C874:
  369. /* phy specific configuration for fibre/tp operation */
  370. tmp = mdio_read(netdev, MII_MCTRL);
  371. tmp &= ~(MII_FX_SEL | MII_EN_SCRM);
  372. //if (dev->if_port == PORT_FIBRE)
  373. // tmp |= MII_FX_SEL;
  374. //else
  375. tmp |= MII_EN_SCRM;
  376. mdio_write(netdev, MII_MCTRL, tmp);
  377. break;
  378. default:
  379. break;
  380. }
  381. cfg = inl(nat->ioaddr + ChipConfig);
  382. if (cfg & CfgExtPhy)
  383. return;
  384. /* On page 78 of the spec, they recommend some settings for "optimum
  385. performance" to be done in sequence. These settings optimize some
  386. of the 100Mbit autodetection circuitry. They say we only want to
  387. do this for rev C of the chip, but engineers at NSC (Bradley
  388. Kennedy) recommends always setting them. If you don't, you get
  389. errors on some autonegotiations that make the device unusable.
  390. It seems that the DSP needs a few usec to reinitialize after
  391. the start of the phy. Just retry writing these values until they
  392. stick.
  393. */
  394. uint32_t srr = inl(nat->ioaddr + SiliconRev);
  395. DBG ( "Natsemi : silicon revision %#04x.\n",(unsigned int)srr);
  396. int NATSEMI_HW_TIMEOUT = 400;
  397. for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
  398. int dspcfg,dspcfg_1;
  399. outw(1, nat->ioaddr + PGSEL);
  400. outw(PMDCSR_VAL, nat->ioaddr + PMDCSR);
  401. outw(TSTDAT_VAL, nat->ioaddr + TSTDAT);
  402. dspcfg = (srr <= SRR_DP83815_C)?
  403. DSPCFG_VAL : (DSPCFG_COEF | readw(nat->ioaddr + DSPCFG));
  404. outw(dspcfg, nat->ioaddr + DSPCFG);
  405. outw(SDCFG_VAL, nat->ioaddr + SDCFG);
  406. outw(0, nat->ioaddr + PGSEL);
  407. inl(nat->ioaddr + ChipConfig);
  408. udelay(10);
  409. outw(1, nat->ioaddr + PGSEL);
  410. dspcfg_1 = readw(nat->ioaddr + DSPCFG);
  411. outw(0, nat->ioaddr + PGSEL);
  412. if (dspcfg == dspcfg_1)
  413. break;
  414. }
  415. if (i==NATSEMI_HW_TIMEOUT) {
  416. DBG ( "Natsemi: DSPCFG mismatch after retrying for"
  417. " %d usec.\n", i*10);
  418. } else {
  419. DBG ( "NATSEMI: DSPCFG accepted after %d usec.\n",
  420. i*10);
  421. }
  422. /*
  423. * Enable PHY Specific event based interrupts. Link state change
  424. * and Auto-Negotiation Completion are among the affected.
  425. * Read the intr status to clear it (needed for wake events).
  426. */
  427. inw(nat->ioaddr + MIntrStatus);
  428. //MICRIntEn = 0x2
  429. outw(0x2, nat->ioaddr + MIntrCtrl);
  430. }
  431. /*
  432. * Patch up for fixing CRC errors.
  433. * adapted from linux natsemi driver
  434. *
  435. */
  436. static void do_cable_magic ( struct net_device *netdev ) {
  437. struct natsemi_nic *nat = netdev->priv;
  438. uint16_t data;
  439. /*
  440. * 100 MBit links with short cables can trip an issue with the chip.
  441. * The problem manifests as lots of CRC errors and/or flickering
  442. * activity LED while idle. This process is based on instructions
  443. * from engineers at National.
  444. */
  445. if (inl(nat->ioaddr + ChipConfig) & CfgSpeed100) {
  446. outw(1, nat->ioaddr + PGSEL);
  447. /*
  448. * coefficient visibility should already be enabled via
  449. * DSPCFG | 0x1000
  450. */
  451. data = inw(nat->ioaddr + TSTDAT) & 0xff;
  452. /*
  453. * the value must be negative, and within certain values
  454. * (these values all come from National)
  455. */
  456. if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
  457. /* the bug has been triggered - fix the coefficient */
  458. outw(TSTDAT_FIXED, nat->ioaddr + TSTDAT);
  459. /* lock the value */
  460. data = inw(nat->ioaddr + DSPCFG);
  461. //np->dspcfg = data | DSPCFG_LOCK;
  462. outw(data | DSPCFG_LOCK , nat->ioaddr + DSPCFG);
  463. }
  464. outw(0, nat->ioaddr + PGSEL);
  465. }
  466. }
  467. /*
  468. * Open NIC
  469. *
  470. * @v netdev Net device
  471. * @ret rc Return status code
  472. */
  473. static int nat_open ( struct net_device *netdev ) {
  474. struct natsemi_nic *nat = netdev->priv;
  475. int i;
  476. uint32_t tx_config,rx_config;
  477. /* Disable PME:
  478. * The PME bit is initialized from the EEPROM contents.
  479. * PCI cards probably have PME disabled, but motherboard
  480. * implementations may have PME set to enable WakeOnLan.
  481. * With PME set the chip will scan incoming packets but
  482. * nothing will be written to memory.
  483. */
  484. SavedClkRun = inl ( nat->ioaddr + ClkRun );
  485. outl ( SavedClkRun & ~0x100, nat->ioaddr + ClkRun );
  486. /* Setting up Mac address in the NIC
  487. */
  488. for ( i = 0 ; i < ETH_ALEN ; i+=2 ) {
  489. outl ( i,nat->ioaddr + RxFilterAddr );
  490. outw ( netdev->ll_addr[i] + ( netdev->ll_addr[i + 1] << 8 ),
  491. nat->ioaddr + RxFilterData );
  492. }
  493. /*Set up the Tx Ring
  494. */
  495. nat->tx_cur = 0;
  496. nat->tx_dirty = 0;
  497. for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
  498. nat->tx[i].link = virt_to_bus ( ( i + 1 < TX_RING_SIZE ) ? &nat->tx[i + 1] : &nat->tx[0] );
  499. nat->tx[i].cmdsts = 0;
  500. nat->tx[i].bufptr = 0;
  501. }
  502. /* Set up RX ring
  503. */
  504. nat->rx_cur = 0;
  505. for ( i = 0 ; i < NUM_RX_DESC ; i++ ) {
  506. nat->iobuf[i] = alloc_iob ( RX_BUF_SIZE );
  507. if ( !nat->iobuf[i] )
  508. goto memory_alloc_err;
  509. nat->rx[i].link = virt_to_bus ( ( i + 1 < NUM_RX_DESC ) ? &nat->rx[i + 1] : &nat->rx[0] );
  510. nat->rx[i].cmdsts = RX_BUF_SIZE;
  511. nat->rx[i].bufptr = virt_to_bus ( nat->iobuf[i]->data );
  512. // DBG ( " Address of iobuf [%d] = %x and iobuf->data = %x \n", i,
  513. // nat->iobuf[i],nat->iobuf[i]->data);
  514. }
  515. /* load Receive Descriptor Register
  516. */
  517. outl ( virt_to_bus ( &nat->rx[0] ), nat->ioaddr + RxRingPtr );
  518. DBG ( "Natsemi Rx descriptor loaded with: %X\n",
  519. (unsigned int) inl ( nat->ioaddr + RxRingPtr ) );
  520. /* setup Tx ring
  521. */
  522. outl ( virt_to_bus ( &nat->tx[0] ),nat->ioaddr + TxRingPtr );
  523. DBG ( "Natsemi Tx descriptor loaded with: %X\n",
  524. (unsigned int)inl ( nat->ioaddr + TxRingPtr ) );
  525. /* Enables RX
  526. */
  527. outl ( RxFilterEnable|AcceptBroadcast|AcceptAllMulticast|AcceptMyPhys,
  528. nat->ioaddr + RxFilterAddr );
  529. /* Initialize other registers.
  530. * Configure the PCI bus bursts and FIFO thresholds.
  531. * Configure for standard, in-spec Ethernet.
  532. */
  533. if ( inl ( nat->ioaddr + ChipConfig ) & 0x20000000 ) { /* Full duplex */
  534. tx_config = 0xD0801002 | 0xC0000000;
  535. DBG ( "Full duplex\n" );
  536. rx_config = 0x10000020 | 0x10000000;
  537. } else {
  538. tx_config = 0x10801002 & ~0xC0000000;
  539. DBG ( "Half duplex\n" );
  540. rx_config = 0x0020 & ~0x10000000;
  541. }
  542. outl ( tx_config, nat->ioaddr + TxConfig );
  543. outl ( rx_config, nat->ioaddr + RxConfig );
  544. DBG ( "Tx config register = %x Rx config register = %x\n",
  545. (unsigned int) inl ( nat->ioaddr + TxConfig),
  546. (unsigned int) inl ( nat->ioaddr + RxConfig) );
  547. /*start the receiver
  548. */
  549. outl ( RxOn, nat->ioaddr + ChipCmd );
  550. /* lines 1586 linux-natsemi.c uses cable magic
  551. * testing this feature is required or not
  552. */
  553. do_cable_magic ( netdev );
  554. init_phy_fixup ( netdev );
  555. /* mask the interrupts. note interrupt is not enabled here
  556. */
  557. return 0;
  558. memory_alloc_err:
  559. /* this block frees the previously allocated buffers
  560. * if memory for all the buffers is not available
  561. */
  562. i = 0;
  563. while ( nat->rx[i].cmdsts == RX_BUF_SIZE ) {
  564. free_iob ( nat->iobuf[i] );
  565. i++;
  566. }
  567. return -ENOMEM;
  568. }
  569. /**
  570. * Close NIC
  571. *
  572. * @v netdev Net device
  573. */
  574. static void nat_close ( struct net_device *netdev ) {
  575. struct natsemi_nic *nat = netdev->priv;
  576. int i;
  577. /* Reset the hardware to disable everything in one go
  578. */
  579. nat_reset ( nat );
  580. /* Free RX ring
  581. */
  582. for ( i = 0; i < NUM_RX_DESC ; i++ ) {
  583. free_iob ( nat->iobuf[i] );
  584. }
  585. }
  586. /**
  587. * Transmit packet
  588. *
  589. * @v netdev Network device
  590. * @v iobuf I/O buffer
  591. * @ret rc Return status code
  592. */
  593. static int nat_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  594. struct natsemi_nic *nat = netdev->priv;
  595. /* check for space in TX ring
  596. */
  597. if ( nat->tx[nat->tx_cur].cmdsts != 0 ) {
  598. DBG ( "TX overflow\n" );
  599. return -ENOBUFS;
  600. }
  601. /* to be used in netdev_tx_complete
  602. */
  603. nat->tx_iobuf[nat->tx_cur] = iobuf;
  604. /* Pad and align packet has not been used because its not required here
  605. * iob_pad ( iobuf, ETH_ZLEN ); can be used to achieve it
  606. */
  607. /* Add to TX ring
  608. */
  609. DBG ( "TX id %d at %lx + %x\n", nat->tx_cur,
  610. virt_to_bus ( &iobuf->data ), iob_len ( iobuf ) );
  611. nat->tx[nat->tx_cur].bufptr = virt_to_bus ( iobuf->data );
  612. nat->tx[nat->tx_cur].cmdsts = iob_len ( iobuf ) | OWN;
  613. /* increment the circular buffer pointer to the next buffer location
  614. */
  615. nat->tx_cur = ( nat->tx_cur + 1 ) % TX_RING_SIZE;
  616. /*start the transmitter
  617. */
  618. outl ( TxOn, nat->ioaddr + ChipCmd );
  619. return 0;
  620. }
  621. /**
  622. * Poll for received packets
  623. *
  624. * @v netdev Network device
  625. * @v rx_quota Maximum number of packets to receive
  626. */
  627. static void nat_poll ( struct net_device *netdev) {
  628. struct natsemi_nic *nat = netdev->priv;
  629. unsigned int status;
  630. unsigned int rx_status;
  631. unsigned int intr_status;
  632. unsigned int rx_len;
  633. struct io_buffer *rx_iob;
  634. int i;
  635. /* read the interrupt register
  636. */
  637. intr_status = inl ( nat->ioaddr + IntrStatus );
  638. if ( !intr_status )
  639. goto end;
  640. /* check the status of packets given to card for transmission
  641. */
  642. DBG ( "Intr status %X\n",intr_status );
  643. i = nat->tx_dirty;
  644. while ( i!= nat->tx_cur ) {
  645. status = nat->tx[nat->tx_dirty].cmdsts;
  646. DBG ( "value of tx_dirty = %d tx_cur=%d status=%X\n",
  647. nat->tx_dirty,nat->tx_cur,status );
  648. /* check if current packet has been transmitted or not
  649. */
  650. if ( status & OWN )
  651. break;
  652. /* Check if any errors in transmission
  653. */
  654. if (! ( status & DescPktOK ) ) {
  655. DBG ( "Error in sending Packet status:%X\n",
  656. (unsigned int) status );
  657. netdev_tx_complete_err ( netdev,nat->tx_iobuf[nat->tx_dirty],-EINVAL );
  658. } else {
  659. DBG ( "Success in transmitting Packet\n" );
  660. netdev_tx_complete ( netdev,nat->tx_iobuf[nat->tx_dirty] );
  661. }
  662. /* setting cmdsts zero, indicating that it can be reused
  663. */
  664. nat->tx[nat->tx_dirty].cmdsts = 0;
  665. nat->tx_dirty = ( nat->tx_dirty + 1 ) % TX_RING_SIZE;
  666. i = ( i + 1 ) % TX_RING_SIZE;
  667. }
  668. /* Handle received packets
  669. */
  670. rx_status = (unsigned int) nat->rx[nat->rx_cur].cmdsts;
  671. while ( ( rx_status & OWN ) ) {
  672. rx_len = ( rx_status & DSIZE ) - CRC_SIZE;
  673. DBG ( " Status of received packet = %X , Lenght of Packet = %X\n",
  674. rx_status,rx_len );
  675. /*check for the corrupt packet
  676. */
  677. if ( ( rx_status & ( DescMore|DescPktOK|RxTooLong ) ) != DescPktOK) {
  678. DBG ( "natsemi_poll: Corrupted packet received, "
  679. "buffer status = %X \n",
  680. (unsigned int) nat->rx[nat->rx_cur].cmdsts );
  681. //DBG_HD ( nat->iobuf[nat->rx_cur]->data,rx_len);
  682. netdev_rx_err ( netdev,NULL,-EINVAL );
  683. } else {
  684. rx_iob = alloc_iob ( rx_len );
  685. if ( !rx_iob )
  686. /* leave packet for next call to poll
  687. */
  688. goto end;
  689. memcpy ( iob_put ( rx_iob,rx_len ),
  690. nat->iobuf[nat->rx_cur]->data,rx_len );
  691. DBG ( "received packet\n" );
  692. //DBG_HD ( nat->iobuf[nat->rx_cur]->data,30);
  693. /* add to the receive queue.
  694. */
  695. netdev_rx ( netdev,rx_iob );
  696. }
  697. nat->rx[nat->rx_cur].cmdsts = RX_BUF_SIZE;
  698. nat->rx_cur = ( nat->rx_cur + 1 ) % NUM_RX_DESC;
  699. rx_status = nat->rx[nat->rx_cur].cmdsts;
  700. }
  701. end:
  702. /* re-enable the potentially idle receive state machine
  703. */
  704. outl ( RxOn, nat->ioaddr + ChipCmd );
  705. }
  706. /**
  707. * Enable/disable interrupts
  708. *
  709. * @v netdev Network device
  710. * @v enable Interrupts should be enabled
  711. */
  712. static void nat_irq ( struct net_device *netdev, int enable ) {
  713. struct natsemi_nic *nat = netdev->priv;
  714. outl ( ( enable ? ( RxOk|RxErr|TxOk|TxErr ) :0 ),
  715. nat->ioaddr + IntrMask);
  716. outl ( ( enable ? 1:0 ),nat->ioaddr + IntrEnable );
  717. }
  718. /** natsemi net device operations */
  719. static struct net_device_operations nat_operations = {
  720. .open = nat_open,
  721. .close = nat_close,
  722. .transmit = nat_transmit,
  723. .poll = nat_poll,
  724. .irq = nat_irq,
  725. };
  726. /*
  727. * Probe PCI device
  728. *
  729. * @v pci PCI device
  730. * @v id PCI ID
  731. * @ret rc Return status code
  732. */
  733. static int nat_probe ( struct pci_device *pci,
  734. const struct pci_device_id *id __unused ) {
  735. struct net_device *netdev;
  736. struct natsemi_nic *nat = NULL;
  737. int rc;
  738. int i;
  739. uint8_t ll_addr_encoded[MAX_LL_ADDR_LEN];
  740. uint8_t last = 0;
  741. uint8_t last1 = 0;
  742. uint8_t prev_bytes[2];
  743. /* Allocate net device
  744. */
  745. netdev = alloc_etherdev ( sizeof ( *nat ) );
  746. if ( ! netdev )
  747. return -ENOMEM;
  748. netdev_init ( netdev,&nat_operations );
  749. nat = netdev->priv;
  750. pci_set_drvdata ( pci, netdev );
  751. netdev->dev = &pci->dev;
  752. memset ( nat, 0, sizeof ( *nat ) );
  753. nat->ioaddr = pci->ioaddr;
  754. /* Fix up PCI device
  755. */
  756. adjust_pci_device ( pci );
  757. /* Reset the NIC, set up EEPROM access and read MAC address
  758. */
  759. nat_reset ( nat );
  760. nat_init_eeprom ( nat );
  761. nvs_read ( &nat->eeprom.nvs, EE_MAC-1, prev_bytes, 1 );
  762. nvs_read ( &nat->eeprom.nvs, EE_MAC, ll_addr_encoded, ETH_ALEN );
  763. /* decoding the MAC address read from NVS
  764. * and save it in netdev->ll_addr
  765. */
  766. last = prev_bytes[1] >> 7;
  767. for ( i = 0 ; i < ETH_ALEN ; i++ ) {
  768. last1 = ll_addr_encoded[i] >> 7;
  769. netdev->ll_addr[i] = ll_addr_encoded[i] << 1 | last;
  770. last = last1;
  771. }
  772. /* Register network device
  773. */
  774. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  775. goto err_register_netdev;
  776. return 0;
  777. err_register_netdev:
  778. /* Disable NIC
  779. */
  780. nat_reset ( nat );
  781. /* Free net device
  782. */
  783. netdev_put ( netdev );
  784. return rc;
  785. }
  786. /**
  787. * Remove PCI device
  788. *
  789. * @v pci PCI device
  790. */
  791. static void nat_remove ( struct pci_device *pci ) {
  792. struct net_device *netdev = pci_get_drvdata ( pci );
  793. struct natsemi_nic *nat = netdev->priv;
  794. if ( nat->nvo.nvs )
  795. nvo_unregister ( &nat->nvo );
  796. unregister_netdev ( netdev );
  797. nat_reset ( nat );
  798. netdev_put ( netdev );
  799. }
  800. static struct pci_device_id natsemi_nics[] = {
  801. PCI_ROM(0x100b, 0x0020, "dp83815", "DP83815"),
  802. };
  803. struct pci_driver natsemi_driver __pci_driver = {
  804. .ids = natsemi_nics,
  805. .id_count = ( sizeof ( natsemi_nics ) / sizeof ( natsemi_nics[0] ) ),
  806. .probe = nat_probe,
  807. .remove = nat_remove,
  808. };