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

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