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.

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