You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

natsemi.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. natsemi.c - gPXE driver for the NatSemi DP8381x series.
  3. Based on:
  4. natsemi.c: An Etherboot driver for the NatSemi DP8381x series.
  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@etherboot.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. /*
  35. 02 Jul 2007 Udayan Kumar 1.2 ported the driver from etherboot to gPXE API.
  36. Fully rewritten,adapting the old driver.
  37. Added a circular buffer for transmit and receive.
  38. transmit routine will not wait for transmission to finish.
  39. poll routine deals with it.
  40. 13 Dec 2003 Tim Legge 1.1 Enabled Multicast Support
  41. 29 May 2001 Marty Connor 1.0 Initial Release. Tested with Netgear FA311 and FA312 boards
  42. */
  43. #include <stdint.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include <errno.h>
  48. #include <timer.h>
  49. #include <byteswap.h>
  50. #include <gpxe/pci.h>
  51. #include <gpxe/if_ether.h>
  52. #include <gpxe/ethernet.h>
  53. #include <gpxe/iobuf.h>
  54. #include <gpxe/netdevice.h>
  55. #include <gpxe/spi_bit.h>
  56. #include <gpxe/threewire.h>
  57. #include <gpxe/nvo.h>
  58. #include "natsemi.h"
  59. /* Function Prototypes: */
  60. static int natsemi_spi_read_bit ( struct bit_basher *, unsigned int );
  61. static void natsemi_spi_write_bit ( struct bit_basher *,unsigned int, unsigned long );
  62. static void natsemi_init_eeprom ( struct natsemi_private * );
  63. static int natsemi_probe (struct pci_device *pci, const struct pci_device_id *id);
  64. static void natsemi_reset (struct net_device *netdev);
  65. static int natsemi_open (struct net_device *netdev);
  66. static int natsemi_transmit (struct net_device *netdev, struct io_buffer *iobuf);
  67. static void natsemi_poll (struct net_device *netdev);
  68. static void natsemi_close (struct net_device *netdev);
  69. static void natsemi_irq (struct net_device *netdev, int enable);
  70. static void natsemi_remove (struct pci_device *pci);
  71. /** natsemi net device operations */
  72. static struct net_device_operations natsemi_operations = {
  73. .open = natsemi_open,
  74. .close = natsemi_close,
  75. .transmit = natsemi_transmit,
  76. .poll = natsemi_poll,
  77. .irq = natsemi_irq,
  78. };
  79. static int natsemi_spi_read_bit ( struct bit_basher *basher,
  80. unsigned int bit_id ) {
  81. struct natsemi_private *np = container_of ( basher, struct natsemi_private,
  82. spibit.basher );
  83. uint8_t mask = natsemi_ee_bits[bit_id];
  84. uint8_t eereg;
  85. eereg = inb ( np->ioaddr + EE_REG );
  86. return ( eereg & mask );
  87. }
  88. static void natsemi_spi_write_bit ( struct bit_basher *basher,
  89. unsigned int bit_id, unsigned long data ) {
  90. struct natsemi_private *np = container_of ( basher, struct natsemi_private,
  91. spibit.basher );
  92. uint8_t mask = natsemi_ee_bits[bit_id];
  93. uint8_t eereg;
  94. eereg = inb ( np->ioaddr + EE_REG );
  95. eereg &= ~mask;
  96. eereg |= ( data & mask );
  97. outb ( eereg, np->ioaddr + EE_REG );
  98. }
  99. static struct bit_basher_operations natsemi_basher_ops = {
  100. .read = natsemi_spi_read_bit,
  101. .write = natsemi_spi_write_bit,
  102. };
  103. /* It looks that this portion of EEPROM can be used for
  104. * non-volatile stored options. Data sheet does not talk about this region.
  105. * Currently it is not working. But with some efforts it can.
  106. */
  107. static struct nvo_fragment natsemi_nvo_fragments[] = {
  108. { 0x0c, 0x68 },
  109. { 0, 0 }
  110. };
  111. /*
  112. * Set up for EEPROM access
  113. *
  114. * @v NAT NATSEMI NIC
  115. */
  116. static void natsemi_init_eeprom ( struct natsemi_private *np ) {
  117. /* Initialise three-wire bus
  118. */
  119. np->spibit.basher.op = &natsemi_basher_ops;
  120. np->spibit.bus.mode = SPI_MODE_THREEWIRE;
  121. np->spibit.endianness = SPI_BIT_LITTLE_ENDIAN;
  122. init_spi_bit_basher ( &np->spibit );
  123. /*natsemi DP 83815 only supports at93c46
  124. */
  125. init_at93c46 ( &np->eeprom, 16 );
  126. np->eeprom.bus = &np->spibit.bus;
  127. np->nvo.nvs = &np->eeprom.nvs;
  128. np->nvo.fragments = natsemi_nvo_fragments;
  129. }
  130. /**
  131. * Probe PCI device
  132. *
  133. * @v pci PCI device
  134. * @v id PCI ID
  135. * @ret rc Return status code
  136. */
  137. static int natsemi_probe (struct pci_device *pci,
  138. const struct pci_device_id *id __unused) {
  139. struct net_device *netdev;
  140. struct natsemi_private *np = NULL;
  141. uint8_t ll_addr_encoded[MAX_LL_ADDR_LEN];
  142. uint8_t last=0,last1=0;
  143. uint8_t prev_bytes[2];
  144. int i;
  145. int rc;
  146. /* Allocate net device
  147. */
  148. netdev = alloc_etherdev (sizeof (*np));
  149. if (! netdev)
  150. return -ENOMEM;
  151. netdev_init (netdev, &natsemi_operations);
  152. np = netdev->priv;
  153. pci_set_drvdata (pci, netdev);
  154. netdev->dev = &pci->dev;
  155. memset (np, 0, sizeof (*np));
  156. np->ioaddr = pci->ioaddr;
  157. adjust_pci_device (pci);
  158. natsemi_reset (netdev);
  159. natsemi_init_eeprom ( np );
  160. nvs_read ( &np->eeprom.nvs, EE_MAC-1, prev_bytes, 1 );
  161. nvs_read ( &np->eeprom.nvs, EE_MAC, ll_addr_encoded, ETH_ALEN );
  162. /* decoding the MAC address read from NVS
  163. * and save it in netdev->ll_addr
  164. */
  165. last = prev_bytes[1] >> 7;
  166. for ( i = 0 ; i < ETH_ALEN ; i++ ) {
  167. last1 = ll_addr_encoded[i] >> 7;
  168. netdev->ll_addr[i] = ll_addr_encoded[i] << 1 | last;
  169. last = last1;
  170. }
  171. if ((rc = register_netdev (netdev)) != 0)
  172. goto err_register_netdev;
  173. return 0;
  174. err_register_netdev:
  175. natsemi_reset (netdev);
  176. netdev_put (netdev);
  177. return rc;
  178. }
  179. /**
  180. * Remove PCI device
  181. *
  182. * @v pci PCI device
  183. */
  184. static void natsemi_remove (struct pci_device *pci) {
  185. struct net_device *netdev = pci_get_drvdata (pci);
  186. unregister_netdev (netdev);
  187. natsemi_reset (netdev);
  188. netdev_nullify ( netdev );
  189. netdev_put (netdev);
  190. }
  191. /**
  192. * Reset NIC
  193. *
  194. * @v NATSEMI NIC
  195. *
  196. * Issues a hardware reset and waits for the reset to complete.
  197. */
  198. static void natsemi_reset (struct net_device *netdev)
  199. {
  200. struct natsemi_private *np = netdev->priv;
  201. int i;
  202. u32 cfg;
  203. u32 wcsr;
  204. u32 rfcr;
  205. u16 pmatch[3];
  206. u16 sopass[3];
  207. natsemi_irq (netdev, 0);
  208. /*
  209. * Resetting the chip causes some registers to be lost.
  210. * Natsemi suggests NOT reloading the EEPROM while live, so instead
  211. * we save the state that would have been loaded from EEPROM
  212. * on a normal power-up (see the spec EEPROM map).
  213. */
  214. /* CFG */
  215. cfg = inl (np->ioaddr + ChipConfig) & CFG_RESET_SAVE;
  216. /* WCSR */
  217. wcsr = inl (np->ioaddr + WOLCmd) & WCSR_RESET_SAVE;
  218. /* RFCR */
  219. rfcr = readl (np->ioaddr + RxFilterAddr) & RFCR_RESET_SAVE;
  220. /* PMATCH */
  221. for (i = 0; i < 3; i++) {
  222. outl(i*2, np->ioaddr + RxFilterAddr);
  223. pmatch[i] = inw(np->ioaddr + RxFilterData);
  224. }
  225. /* SOPAS */
  226. for (i = 0; i < 3; i++) {
  227. outl(0xa+(i*2), np->ioaddr + RxFilterAddr);
  228. sopass[i] = inw(np->ioaddr + RxFilterData);
  229. }
  230. /* now whack the chip */
  231. outl(ChipReset, np->ioaddr + ChipCmd);
  232. for (i=0; i<NATSEMI_HW_TIMEOUT; i++) {
  233. if (! (inl (np->ioaddr + ChipCmd) & ChipReset))
  234. break;
  235. udelay(5);
  236. }
  237. if (i == NATSEMI_HW_TIMEOUT) {
  238. DBG ("natsemi_reset: reset did not complete in %d usec.\n", i*5);
  239. }
  240. /* restore CFG */
  241. cfg |= inl(np->ioaddr + ChipConfig) & ~CFG_RESET_SAVE;
  242. cfg &= ~(CfgExtPhy | CfgPhyDis);
  243. outl (cfg, np->ioaddr + ChipConfig);
  244. /* restore WCSR */
  245. wcsr |= inl (np->ioaddr + WOLCmd) & ~WCSR_RESET_SAVE;
  246. outl (wcsr, np->ioaddr + WOLCmd);
  247. /* read RFCR */
  248. rfcr |= inl (np->ioaddr + RxFilterAddr) & ~RFCR_RESET_SAVE;
  249. /* restore PMATCH */
  250. for (i = 0; i < 3; i++) {
  251. outl (i*2, np->ioaddr + RxFilterAddr);
  252. outw (pmatch[i], np->ioaddr + RxFilterData);
  253. }
  254. for (i = 0; i < 3; i++) {
  255. outl (0xa+(i*2), np->ioaddr + RxFilterAddr);
  256. outw (sopass[i], np->ioaddr + RxFilterData);
  257. }
  258. /* restore RFCR */
  259. outl (rfcr, np->ioaddr + RxFilterAddr);
  260. }
  261. /**
  262. * Open NIC
  263. *
  264. * @v netdev Net device
  265. * @ret rc Return status code
  266. */
  267. static int natsemi_open (struct net_device *netdev)
  268. {
  269. struct natsemi_private *np = netdev->priv;
  270. uint32_t tx_config, rx_config;
  271. int i;
  272. /* Disable PME:
  273. * The PME bit is initialized from the EEPROM contents.
  274. * PCI cards probably have PME disabled, but motherboard
  275. * implementations may have PME set to enable WakeOnLan.
  276. * With PME set the chip will scan incoming packets but
  277. * nothing will be written to memory.
  278. */
  279. outl (inl (np->ioaddr + ClkRun) & ~0x100, np->ioaddr + ClkRun);
  280. /* Set MAC address in NIC
  281. */
  282. for (i = 0 ; i < ETH_ALEN ; i+=2) {
  283. outl (i, np->ioaddr + RxFilterAddr);
  284. outw (netdev->ll_addr[i] + (netdev->ll_addr[i + 1] << 8),
  285. np->ioaddr + RxFilterData);
  286. }
  287. /* Setup Tx Ring
  288. */
  289. np->tx_cur = 0;
  290. np->tx_dirty = 0;
  291. for (i = 0 ; i < TX_RING_SIZE ; i++) {
  292. np->tx[i].link = virt_to_bus ((i + 1 < TX_RING_SIZE) ? &np->tx[i + 1] : &np->tx[0]);
  293. np->tx[i].cmdsts = 0;
  294. np->tx[i].bufptr = 0;
  295. }
  296. outl (virt_to_bus (&np->tx[0]),np->ioaddr + TxRingPtr);
  297. DBG ("Natsemi Tx descriptor loaded with: %#08lx\n",
  298. inl (np->ioaddr + TxRingPtr));
  299. /* Setup RX ring
  300. */
  301. np->rx_cur = 0;
  302. for (i = 0 ; i < NUM_RX_DESC ; i++) {
  303. np->iobuf[i] = alloc_iob (RX_BUF_SIZE);
  304. if (! np->iobuf[i])
  305. goto memory_alloc_err;
  306. np->rx[i].link = virt_to_bus ((i + 1 < NUM_RX_DESC)
  307. ? &np->rx[i + 1] : &np->rx[0]);
  308. np->rx[i].cmdsts = RX_BUF_SIZE;
  309. np->rx[i].bufptr = virt_to_bus (np->iobuf[i]->data);
  310. DBG (" Address of iobuf [%d] = %p and iobuf->data = %p \n", i,
  311. &np->iobuf[i], &np->iobuf[i]->data);
  312. }
  313. outl (virt_to_bus (&np->rx[0]), np->ioaddr + RxRingPtr);
  314. DBG ("Natsemi Rx descriptor loaded with: %#08lx\n",
  315. inl (np->ioaddr + RxRingPtr));
  316. /* Setup RX Filter
  317. */
  318. outl (RxFilterEnable | AcceptBroadcast | AcceptAllMulticast | AcceptMyPhys,
  319. np->ioaddr + RxFilterAddr);
  320. /* Initialize other registers.
  321. * Configure the PCI bus bursts and FIFO thresholds.
  322. * Configure for standard, in-spec Ethernet.
  323. */
  324. if (inl (np->ioaddr + ChipConfig) & 0x20000000) { /* Full duplex */
  325. DBG ("Full duplex\n");
  326. tx_config = 0xD0801002 | 0xC0000000;
  327. rx_config = 0x10000020 | 0x10000000;
  328. } else {
  329. DBG ("Half duplex\n");
  330. tx_config = 0x10801002 & ~0xC0000000;
  331. rx_config = 0x00000020 & ~0x10000000;
  332. }
  333. outl (tx_config, np->ioaddr + TxConfig);
  334. outl (rx_config, np->ioaddr + RxConfig);
  335. DBG ("Tx config register = %#08lx Rx config register = %#08lx\n",
  336. inl (np->ioaddr + TxConfig),
  337. inl (np->ioaddr + RxConfig));
  338. /*Set the Interrupt Mask register
  339. */
  340. outl((RxOk|RxErr|TxOk|TxErr),np->ioaddr + IntrMask);
  341. /*start the receiver
  342. */
  343. outl (RxOn, np->ioaddr + ChipCmd);
  344. return 0;
  345. memory_alloc_err:
  346. /* Frees any allocated buffers when memory
  347. * for all buffers requested is not available
  348. */
  349. i = 0;
  350. while (np->rx[i].cmdsts == RX_BUF_SIZE) {
  351. free_iob (np->iobuf[i]);
  352. i++;
  353. }
  354. return -ENOMEM;
  355. }
  356. /**
  357. * Close NIC
  358. *
  359. * @v netdev Net device
  360. */
  361. static void natsemi_close (struct net_device *netdev)
  362. {
  363. struct natsemi_private *np = netdev->priv;
  364. int i;
  365. natsemi_reset (netdev);
  366. for (i = 0; i < NUM_RX_DESC ; i++) {
  367. free_iob (np->iobuf[i]);
  368. }
  369. }
  370. /**
  371. * Transmit packet
  372. *
  373. * @v netdev Network device
  374. * @v iobuf I/O buffer
  375. * @ret rc Return status code
  376. */
  377. static int natsemi_transmit (struct net_device *netdev, struct io_buffer *iobuf)
  378. {
  379. struct natsemi_private *np = netdev->priv;
  380. if (np->tx[np->tx_cur].cmdsts != 0) {
  381. DBG ("TX overflow\n");
  382. return -ENOBUFS;
  383. }
  384. /* Used by netdev_tx_complete ()
  385. */
  386. np->tx_iobuf[np->tx_cur] = iobuf;
  387. /* Pad and align packet has not been used because its not required
  388. * by the hardware.
  389. * iob_pad (iobuf, ETH_ZLEN);
  390. * can be used to achieve it, if required
  391. */
  392. /* Add the packet to TX ring
  393. */
  394. np->tx[np->tx_cur].bufptr = virt_to_bus (iobuf->data);
  395. np->tx[np->tx_cur].cmdsts = iob_len (iobuf) | OWN;
  396. DBG ("TX id %d at %#08lx + %#08zx\n", np->tx_cur,
  397. virt_to_bus (&iobuf->data), iob_len (iobuf));
  398. /* increment the circular buffer pointer to the next buffer location
  399. */
  400. np->tx_cur = (np->tx_cur + 1) % TX_RING_SIZE;
  401. /*start the transmitter
  402. */
  403. outl (TxOn, np->ioaddr + ChipCmd);
  404. return 0;
  405. }
  406. /**
  407. * Poll for received packets
  408. *
  409. * @v netdev Network device
  410. */
  411. static void natsemi_poll (struct net_device *netdev)
  412. {
  413. struct natsemi_private *np = netdev->priv;
  414. unsigned int tx_status;
  415. unsigned int rx_status;
  416. unsigned int intr_status;
  417. unsigned int rx_len;
  418. struct io_buffer *rx_iob;
  419. int i;
  420. /* read the interrupt register
  421. */
  422. intr_status = inl (np->ioaddr + IntrStatus);
  423. if (!intr_status)
  424. goto end;
  425. DBG ("natsemi_poll: intr_status = %#08x\n", intr_status);
  426. /* Check status of transmitted packets
  427. */
  428. i = np->tx_dirty;
  429. while (i != np->tx_cur) {
  430. tx_status = np->tx[np->tx_dirty].cmdsts;
  431. DBG ("tx_dirty = %d tx_cur=%d tx_status=%#08x\n",
  432. np->tx_dirty, np->tx_cur, tx_status);
  433. if (tx_status & OWN)
  434. break;
  435. if (! (tx_status & DescPktOK)) {
  436. netdev_tx_complete_err (netdev,np->tx_iobuf[np->tx_dirty],-EINVAL);
  437. DBG ("Error transmitting packet, tx_status: %#08x\n",
  438. tx_status);
  439. } else {
  440. netdev_tx_complete (netdev, np->tx_iobuf[np->tx_dirty]);
  441. DBG ("Success transmitting packet\n");
  442. }
  443. np->tx[np->tx_dirty].cmdsts = 0;
  444. np->tx_dirty = (np->tx_dirty + 1) % TX_RING_SIZE;
  445. i = (i + 1) % TX_RING_SIZE;
  446. }
  447. /* Process received packets
  448. */
  449. rx_status = (unsigned int) np->rx[np->rx_cur].cmdsts;
  450. while ((rx_status & OWN)) {
  451. rx_len = (rx_status & DSIZE) - CRC_SIZE;
  452. DBG ("Received packet, rx_curr = %d, rx_status = %#08x, rx_len = %d\n",
  453. np->rx_cur, rx_status, rx_len);
  454. if ((rx_status & (DescMore | DescPktOK | RxTooLong)) != DescPktOK) {
  455. netdev_rx_err (netdev, NULL, -EINVAL);
  456. DBG ("natsemi_poll: Corrupted packet received!"
  457. " Status = %#08lx\n",
  458. np->rx[np->rx_cur].cmdsts);
  459. } else {
  460. /* If unable allocate space for this packet,
  461. * try again next poll
  462. */
  463. rx_iob = alloc_iob (rx_len);
  464. if (! rx_iob)
  465. goto end;
  466. memcpy (iob_put (rx_iob, rx_len),
  467. np->iobuf[np->rx_cur]->data, rx_len);
  468. /* Add this packet to the receive queue.
  469. */
  470. netdev_rx (netdev, rx_iob);
  471. }
  472. np->rx[np->rx_cur].cmdsts = RX_BUF_SIZE;
  473. np->rx_cur = (np->rx_cur + 1) % NUM_RX_DESC;
  474. rx_status = np->rx[np->rx_cur].cmdsts;
  475. }
  476. end:
  477. /* re-enable the potentially idle receive state machine
  478. */
  479. outl (RxOn, np->ioaddr + ChipCmd);
  480. }
  481. /**
  482. * Enable/disable interrupts
  483. *
  484. * @v netdev Network device
  485. * @v enable Non-zero for enable, zero for disable
  486. */
  487. static void natsemi_irq (struct net_device *netdev, int enable)
  488. {
  489. struct natsemi_private *np = netdev->priv;
  490. outl ((enable ? (RxOk | RxErr | TxOk|TxErr) : 0),
  491. np->ioaddr + IntrMask);
  492. outl ((enable ? 1 : 0), np->ioaddr + IntrEnable);
  493. }
  494. static struct pci_device_id natsemi_nics[] = {
  495. PCI_ROM(0x100b, 0x0020, "dp83815", "DP83815"),
  496. };
  497. struct pci_driver natsemi_driver __pci_driver = {
  498. .ids = natsemi_nics,
  499. .id_count = (sizeof (natsemi_nics) / sizeof (natsemi_nics[0])),
  500. .probe = natsemi_probe,
  501. .remove = natsemi_remove,
  502. };