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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <byteswap.h>
  25. #include <ipxe/netdevice.h>
  26. #include <ipxe/ethernet.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/malloc.h>
  30. #include <ipxe/pci.h>
  31. #include <ipxe/nvs.h>
  32. #include <ipxe/bitbash.h>
  33. #include <ipxe/spi_bit.h>
  34. #include <ipxe/threewire.h>
  35. #include "natsemi.h"
  36. /** @file
  37. *
  38. * National Semiconductor "MacPhyter" network card driver
  39. *
  40. * Based on the following datasheets:
  41. *
  42. * http://www.ti.com/lit/ds/symlink/dp83820.pdf
  43. * http://www.datasheets.org.uk/indexdl/Datasheet-03/DSA0041338.pdf
  44. *
  45. */
  46. /******************************************************************************
  47. *
  48. * EEPROM interface
  49. *
  50. ******************************************************************************
  51. */
  52. /** Pin mapping for SPI bit-bashing interface */
  53. static const uint8_t natsemi_eeprom_bits[] = {
  54. [SPI_BIT_SCLK] = NATSEMI_MEAR_EECLK,
  55. [SPI_BIT_MOSI] = NATSEMI_MEAR_EEDI,
  56. [SPI_BIT_MISO] = NATSEMI_MEAR_EEDO,
  57. [SPI_BIT_SS(0)] = NATSEMI_MEAR_EESEL,
  58. };
  59. /**
  60. * Read input bit
  61. *
  62. * @v basher Bit-bashing interface
  63. * @v bit_id Bit number
  64. * @ret zero Input is a logic 0
  65. * @ret non-zero Input is a logic 1
  66. */
  67. static int natsemi_spi_read_bit ( struct bit_basher *basher,
  68. unsigned int bit_id ) {
  69. struct natsemi_nic *natsemi = container_of ( basher, struct natsemi_nic,
  70. spibit.basher );
  71. uint32_t mask = natsemi_eeprom_bits[bit_id];
  72. uint32_t reg;
  73. DBG_DISABLE ( DBGLVL_IO );
  74. reg = readl ( natsemi->regs + NATSEMI_MEAR );
  75. DBG_ENABLE ( DBGLVL_IO );
  76. return ( reg & mask );
  77. }
  78. /**
  79. * Set/clear output bit
  80. *
  81. * @v basher Bit-bashing interface
  82. * @v bit_id Bit number
  83. * @v data Value to write
  84. */
  85. static void natsemi_spi_write_bit ( struct bit_basher *basher,
  86. unsigned int bit_id, unsigned long data ) {
  87. struct natsemi_nic *natsemi = container_of ( basher, struct natsemi_nic,
  88. spibit.basher );
  89. uint32_t mask = natsemi_eeprom_bits[bit_id];
  90. uint32_t reg;
  91. DBG_DISABLE ( DBGLVL_IO );
  92. reg = readl ( natsemi->regs + NATSEMI_MEAR );
  93. reg &= ~mask;
  94. reg |= ( data & mask );
  95. writel ( reg, natsemi->regs + NATSEMI_MEAR );
  96. DBG_ENABLE ( DBGLVL_IO );
  97. }
  98. /** SPI bit-bashing interface */
  99. static struct bit_basher_operations natsemi_basher_ops = {
  100. .read = natsemi_spi_read_bit,
  101. .write = natsemi_spi_write_bit,
  102. };
  103. /**
  104. * Initialise EEPROM
  105. *
  106. * @v natsemi National Semiconductor device
  107. */
  108. static void natsemi_init_eeprom ( struct natsemi_nic *natsemi ) {
  109. /* Initialise SPI bit-bashing interface */
  110. natsemi->spibit.basher.op = &natsemi_basher_ops;
  111. natsemi->spibit.bus.mode = SPI_MODE_THREEWIRE;
  112. natsemi->spibit.endianness =
  113. ( ( natsemi->flags & NATSEMI_EEPROM_LITTLE_ENDIAN ) ?
  114. SPI_BIT_LITTLE_ENDIAN : SPI_BIT_BIG_ENDIAN );
  115. init_spi_bit_basher ( &natsemi->spibit );
  116. /* Initialise EEPROM device */
  117. init_at93c06 ( &natsemi->eeprom, 16 );
  118. natsemi->eeprom.bus = &natsemi->spibit.bus;
  119. }
  120. /**
  121. * Get hardware address from sane EEPROM data
  122. *
  123. * @v natsemi National Semiconductor device
  124. * @v eeprom EEPROM data
  125. * @v hw_addr Hardware address to fill in
  126. */
  127. static void natsemi_hwaddr_sane ( struct natsemi_nic *natsemi,
  128. const uint16_t *eeprom, uint16_t *hw_addr ) {
  129. int i;
  130. /* Copy MAC address from EEPROM data */
  131. for ( i = ( ( ETH_ALEN / 2 ) - 1 ) ; i >= 0 ; i-- )
  132. *(hw_addr++) = eeprom[ NATSEMI_EEPROM_MAC_SANE + i ];
  133. DBGC ( natsemi, "NATSEMI %p has sane EEPROM layout\n", natsemi );
  134. }
  135. /**
  136. * Get hardware address from insane EEPROM data
  137. *
  138. * @v natsemi National Semiconductor device
  139. * @v eeprom EEPROM data
  140. * @v hw_addr Hardware address to fill in
  141. */
  142. static void natsemi_hwaddr_insane ( struct natsemi_nic *natsemi,
  143. const uint16_t *eeprom,
  144. uint16_t *hw_addr ) {
  145. unsigned int i;
  146. unsigned int offset;
  147. uint16_t word;
  148. /* Copy MAC address from EEPROM data */
  149. for ( i = 0 ; i < ( ETH_ALEN / 2 ) ; i++ ) {
  150. offset = ( NATSEMI_EEPROM_MAC_INSANE + i );
  151. word = ( ( le16_to_cpu ( eeprom[ offset ] ) >> 15 ) |
  152. ( le16_to_cpu ( eeprom[ offset + 1 ] << 1 ) ) );
  153. hw_addr[i] = cpu_to_le16 ( word );
  154. }
  155. DBGC ( natsemi, "NATSEMI %p has insane EEPROM layout\n", natsemi );
  156. }
  157. /**
  158. * Get hardware address from EEPROM
  159. *
  160. * @v natsemi National Semiconductor device
  161. * @v hw_addr Hardware address to fill in
  162. * @ret rc Return status code
  163. */
  164. static int natsemi_hwaddr ( struct natsemi_nic *natsemi, void *hw_addr ) {
  165. uint16_t buf[NATSEMI_EEPROM_SIZE];
  166. void ( * extract ) ( struct natsemi_nic *natsemi,
  167. const uint16_t *eeprom, uint16_t *hw_addr );
  168. int rc;
  169. /* Read EEPROM contents */
  170. if ( ( rc = nvs_read ( &natsemi->eeprom.nvs, 0, buf,
  171. sizeof ( buf ) ) ) != 0 ) {
  172. DBGC ( natsemi, "NATSEMI %p could not read EEPROM: %s\n",
  173. natsemi, strerror ( rc ) );
  174. return rc;
  175. }
  176. DBGC2 ( natsemi, "NATSEMI %p EEPROM contents:\n", natsemi );
  177. DBGC2_HDA ( natsemi, 0, buf, sizeof ( buf ) );
  178. /* Extract MAC address from EEPROM contents */
  179. extract = ( ( natsemi->flags & NATSEMI_EEPROM_INSANE ) ?
  180. natsemi_hwaddr_insane : natsemi_hwaddr_sane );
  181. extract ( natsemi, buf, hw_addr );
  182. return 0;
  183. }
  184. /******************************************************************************
  185. *
  186. * Device reset
  187. *
  188. ******************************************************************************
  189. */
  190. /**
  191. * Reset controller chip
  192. *
  193. * @v natsemi National Semiconductor device
  194. * @ret rc Return status code
  195. */
  196. static int natsemi_soft_reset ( struct natsemi_nic *natsemi ) {
  197. unsigned int i;
  198. /* Initiate reset */
  199. writel ( NATSEMI_CR_RST, natsemi->regs + NATSEMI_CR );
  200. /* Wait for reset to complete */
  201. for ( i = 0 ; i < NATSEMI_RESET_MAX_WAIT_MS ; i++ ) {
  202. /* If reset is not complete, delay 1ms and retry */
  203. if ( readl ( natsemi->regs + NATSEMI_CR ) & NATSEMI_CR_RST ) {
  204. mdelay ( 1 );
  205. continue;
  206. }
  207. return 0;
  208. }
  209. DBGC ( natsemi, "NATSEMI %p timed out waiting for reset\n", natsemi );
  210. return -ETIMEDOUT;
  211. }
  212. /**
  213. * Reload configuration from EEPROM
  214. *
  215. * @v natsemi National Semiconductor device
  216. * @ret rc Return status code
  217. */
  218. static int natsemi_reload_config ( struct natsemi_nic *natsemi ) {
  219. unsigned int i;
  220. /* Initiate reload */
  221. writel ( NATSEMI_PTSCR_EELOAD_EN, natsemi->regs + NATSEMI_PTSCR );
  222. /* Wait for reload to complete */
  223. for ( i = 0 ; i < NATSEMI_EELOAD_MAX_WAIT_MS ; i++ ) {
  224. /* If reload is not complete, delay 1ms and retry */
  225. if ( readl ( natsemi->regs + NATSEMI_PTSCR ) &
  226. NATSEMI_PTSCR_EELOAD_EN ) {
  227. mdelay ( 1 );
  228. continue;
  229. }
  230. return 0;
  231. }
  232. DBGC ( natsemi, "NATSEMI %p timed out waiting for configuration "
  233. "reload\n", natsemi );
  234. return -ETIMEDOUT;
  235. }
  236. /**
  237. * Reset hardware
  238. *
  239. * @v natsemi National Semiconductor device
  240. * @ret rc Return status code
  241. */
  242. static int natsemi_reset ( struct natsemi_nic *natsemi ) {
  243. uint32_t cfg;
  244. int rc;
  245. /* Perform soft reset */
  246. if ( ( rc = natsemi_soft_reset ( natsemi ) ) != 0 )
  247. return rc;
  248. /* Reload configuration from EEPROM */
  249. if ( ( rc = natsemi_reload_config ( natsemi ) ) != 0 )
  250. return rc;
  251. /* Configure 64-bit operation, if applicable */
  252. cfg = readl ( natsemi->regs + NATSEMI_CFG );
  253. if ( natsemi->flags & NATSEMI_64BIT ) {
  254. cfg |= ( NATSEMI_CFG_M64ADDR | NATSEMI_CFG_EXTSTS_EN );
  255. if ( ! ( cfg & NATSEMI_CFG_PCI64_DET ) )
  256. cfg &= ~NATSEMI_CFG_DATA64_EN;
  257. }
  258. writel ( cfg, natsemi->regs + NATSEMI_CFG );
  259. /* Invalidate link status cache to force an update */
  260. natsemi->cfg = ~cfg;
  261. DBGC ( natsemi, "NATSEMI %p using configuration %08x\n",
  262. natsemi, cfg );
  263. return 0;
  264. }
  265. /******************************************************************************
  266. *
  267. * Link state
  268. *
  269. ******************************************************************************
  270. */
  271. /**
  272. * Check link state
  273. *
  274. * @v netdev Network device
  275. */
  276. static void natsemi_check_link ( struct net_device *netdev ) {
  277. struct natsemi_nic *natsemi = netdev->priv;
  278. uint32_t cfg;
  279. /* Read link status */
  280. cfg = readl ( natsemi->regs + NATSEMI_CFG );
  281. /* Do nothing unless link status has changed */
  282. if ( cfg == natsemi->cfg )
  283. return;
  284. /* Set gigabit mode (if applicable) */
  285. if ( natsemi->flags & NATSEMI_1000 ) {
  286. cfg &= ~NATSEMI_CFG_MODE_1000;
  287. if ( ! ( cfg & NATSEMI_CFG_SPDSTS1 ) )
  288. cfg |= NATSEMI_CFG_MODE_1000;
  289. writel ( cfg, natsemi->regs + NATSEMI_CFG );
  290. }
  291. /* Update link status */
  292. natsemi->cfg = cfg;
  293. DBGC ( natsemi, "NATSEMI %p link status is %08x\n", natsemi, cfg );
  294. /* Update network device */
  295. if ( cfg & NATSEMI_CFG_LNKSTS ) {
  296. netdev_link_up ( netdev );
  297. } else {
  298. netdev_link_down ( netdev );
  299. }
  300. }
  301. /******************************************************************************
  302. *
  303. * Network device interface
  304. *
  305. ******************************************************************************
  306. */
  307. /**
  308. * Set perfect match filter address
  309. *
  310. * @v natsemi National Semiconductor device
  311. * @v mac MAC address
  312. */
  313. static void natsemi_pmatch ( struct natsemi_nic *natsemi, const void *mac ) {
  314. const uint16_t *pmatch = mac;
  315. uint32_t rfcr;
  316. unsigned int rfaddr;
  317. unsigned int i;
  318. for ( i = 0 ; i < ETH_ALEN ; i += sizeof ( *pmatch ) ) {
  319. /* Select receive filter register address */
  320. rfaddr = ( NATSEMI_RFADDR_PMATCH_BASE + i );
  321. rfcr = readl ( natsemi->regs + NATSEMI_RFCR );
  322. rfcr &= ~NATSEMI_RFCR_RFADDR_MASK;
  323. rfcr |= NATSEMI_RFCR_RFADDR ( rfaddr );
  324. writel ( rfcr, natsemi->regs + NATSEMI_RFCR );
  325. /* Write receive filter data */
  326. writel ( ( le16_to_cpu ( *(pmatch++) ) | NATSEMI_RFDR_BMASK ),
  327. natsemi->regs + NATSEMI_RFDR );
  328. }
  329. }
  330. /**
  331. * Create descriptor ring
  332. *
  333. * @v natsemi National Semiconductor device
  334. * @v ring Descriptor ring
  335. * @ret rc Return status code
  336. */
  337. static int natsemi_create_ring ( struct natsemi_nic *natsemi,
  338. struct natsemi_ring *ring ) {
  339. size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
  340. union natsemi_descriptor *desc;
  341. union natsemi_descriptor *linked_desc;
  342. physaddr_t address;
  343. physaddr_t link;
  344. size_t offset;
  345. unsigned int i;
  346. int rc;
  347. /* Calculate descriptor offset */
  348. offset = ( ( natsemi->flags & NATSEMI_64BIT ) ? 0 :
  349. offsetof ( typeof ( desc[i].d32pad ), d32 ) );
  350. /* Allocate descriptor ring. Align ring on its own size to
  351. * ensure that it can't possibly cross the boundary of 32-bit
  352. * address space.
  353. */
  354. ring->desc = malloc_dma ( len, len );
  355. if ( ! ring->desc ) {
  356. rc = -ENOMEM;
  357. goto err_alloc;
  358. }
  359. address = ( virt_to_bus ( ring->desc ) + offset );
  360. /* Check address is usable by card */
  361. if ( ! natsemi_address_ok ( natsemi, address ) ) {
  362. DBGC ( natsemi, "NATSEMI %p cannot support 64-bit ring "
  363. "address\n", natsemi );
  364. rc = -ENOTSUP;
  365. goto err_64bit;
  366. }
  367. /* Initialise descriptor ring */
  368. memset ( ring->desc, 0, len );
  369. for ( i = 0 ; i < ring->count ; i++ ) {
  370. linked_desc = &ring->desc [ ( i + 1 ) % ring->count ];
  371. link = ( virt_to_bus ( linked_desc ) + offset );
  372. if ( natsemi->flags & NATSEMI_64BIT ) {
  373. ring->desc[i].d64.link = cpu_to_le64 ( link );
  374. } else {
  375. ring->desc[i].d32pad.d32.link = cpu_to_le32 ( link );
  376. }
  377. }
  378. /* Program ring address */
  379. writel ( ( address & 0xffffffffUL ), natsemi->regs + ring->reg );
  380. if ( natsemi->flags & NATSEMI_64BIT ) {
  381. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
  382. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  383. natsemi->regs + ring->reg + 4 );
  384. } else {
  385. writel ( 0, natsemi->regs + ring->reg + 4 );
  386. }
  387. }
  388. DBGC ( natsemi, "NATSEMI %p ring %02x is at [%08llx,%08llx)\n",
  389. natsemi, ring->reg,
  390. ( ( unsigned long long ) virt_to_bus ( ring->desc ) ),
  391. ( ( unsigned long long ) virt_to_bus ( ring->desc ) + len ) );
  392. return 0;
  393. err_64bit:
  394. free_dma ( ring->desc, len );
  395. ring->desc = NULL;
  396. err_alloc:
  397. return rc;
  398. }
  399. /**
  400. * Destroy descriptor ring
  401. *
  402. * @v natsemi National Semiconductor device
  403. * @v ring Descriptor ring
  404. */
  405. static void natsemi_destroy_ring ( struct natsemi_nic *natsemi,
  406. struct natsemi_ring *ring ) {
  407. size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
  408. /* Clear ring address */
  409. writel ( 0, natsemi->regs + ring->reg );
  410. if ( natsemi->flags & NATSEMI_64BIT )
  411. writel ( 0, natsemi->regs + ring->reg + 4 );
  412. /* Free descriptor ring */
  413. free_dma ( ring->desc, len );
  414. ring->desc = NULL;
  415. ring->prod = 0;
  416. ring->cons = 0;
  417. }
  418. /**
  419. * Refill receive descriptor ring
  420. *
  421. * @v netdev Network device
  422. */
  423. static void natsemi_refill_rx ( struct net_device *netdev ) {
  424. struct natsemi_nic *natsemi = netdev->priv;
  425. union natsemi_descriptor *rx;
  426. struct io_buffer *iobuf;
  427. unsigned int rx_idx;
  428. physaddr_t address;
  429. while ( ( natsemi->rx.prod - natsemi->rx.cons ) < NATSEMI_NUM_RX_DESC ){
  430. /* Allocate I/O buffer */
  431. iobuf = alloc_iob ( NATSEMI_RX_MAX_LEN );
  432. if ( ! iobuf ) {
  433. /* Wait for next refill */
  434. return;
  435. }
  436. /* Check address is usable by card */
  437. address = virt_to_bus ( iobuf->data );
  438. if ( ! natsemi_address_ok ( natsemi, address ) ) {
  439. DBGC ( natsemi, "NATSEMI %p cannot support 64-bit RX "
  440. "buffer address\n", natsemi );
  441. netdev_rx_err ( netdev, iobuf, -ENOTSUP );
  442. return;
  443. }
  444. /* Get next receive descriptor */
  445. rx_idx = ( natsemi->rx.prod++ % NATSEMI_NUM_RX_DESC );
  446. rx = &natsemi->rx.desc[rx_idx];
  447. /* Populate receive descriptor */
  448. if ( natsemi->flags & NATSEMI_64BIT ) {
  449. rx->d64.bufptr = cpu_to_le64 ( address );
  450. } else {
  451. rx->d32pad.d32.bufptr = cpu_to_le32 ( address );
  452. }
  453. wmb();
  454. rx->common.cmdsts = cpu_to_le32 ( NATSEMI_DESC_INTR |
  455. NATSEMI_RX_MAX_LEN );
  456. wmb();
  457. /* Record I/O buffer */
  458. assert ( natsemi->rx_iobuf[rx_idx] == NULL );
  459. natsemi->rx_iobuf[rx_idx] = iobuf;
  460. /* Notify card that there are descriptors available */
  461. writel ( NATSEMI_CR_RXE, natsemi->regs + NATSEMI_CR );
  462. DBGC2 ( natsemi, "NATSEMI %p RX %d is [%llx,%llx)\n", natsemi,
  463. rx_idx, ( ( unsigned long long ) address ),
  464. ( ( unsigned long long ) address + NATSEMI_RX_MAX_LEN));
  465. }
  466. }
  467. /**
  468. * Open network device
  469. *
  470. * @v netdev Network device
  471. * @ret rc Return status code
  472. */
  473. static int natsemi_open ( struct net_device *netdev ) {
  474. struct natsemi_nic *natsemi = netdev->priv;
  475. int rc;
  476. /* Set MAC address */
  477. natsemi_pmatch ( natsemi, netdev->ll_addr );
  478. /* Create transmit descriptor ring */
  479. if ( ( rc = natsemi_create_ring ( natsemi, &natsemi->tx ) ) != 0 )
  480. goto err_create_tx;
  481. /* Set transmit configuration */
  482. writel ( ( NATSEMI_TXCFG_CSI | NATSEMI_TXCFG_HBI | NATSEMI_TXCFG_ATP |
  483. NATSEMI_TXCFG_ECRETRY | NATSEMI_TXCFG_MXDMA_DEFAULT |
  484. NATSEMI_TXCFG_FLTH_DEFAULT | NATSEMI_TXCFG_DRTH_DEFAULT ),
  485. ( natsemi->regs + ( ( natsemi->flags & NATSEMI_64BIT ) ?
  486. NATSEMI_TXCFG_64 : NATSEMI_TXCFG_32 ) ) );
  487. /* Create receive descriptor ring */
  488. if ( ( rc = natsemi_create_ring ( natsemi, &natsemi->rx ) ) != 0 )
  489. goto err_create_rx;
  490. /* Set receive configuration */
  491. writel ( ( NATSEMI_RXCFG_ARP | NATSEMI_RXCFG_ATX | NATSEMI_RXCFG_ALP |
  492. NATSEMI_RXCFG_MXDMA_DEFAULT | NATSEMI_RXCFG_DRTH_DEFAULT ),
  493. ( natsemi->regs + ( ( natsemi->flags & NATSEMI_64BIT ) ?
  494. NATSEMI_RXCFG_64 : NATSEMI_RXCFG_32 ) ) );
  495. /* Set receive filter configuration */
  496. writel ( ( NATSEMI_RFCR_RFEN | NATSEMI_RFCR_AAB | NATSEMI_RFCR_AAM |
  497. NATSEMI_RFCR_AAU ), natsemi->regs + NATSEMI_RFCR );
  498. /* Fill receive ring */
  499. natsemi_refill_rx ( netdev );
  500. /* Unmask transmit and receive interrupts. (Interrupts will
  501. * not be generated unless enabled via the IER.)
  502. */
  503. writel ( ( NATSEMI_IRQ_TXDESC | NATSEMI_IRQ_RXDESC ),
  504. natsemi->regs + NATSEMI_IMR );
  505. /* Update link state */
  506. natsemi_check_link ( netdev );
  507. return 0;
  508. natsemi_destroy_ring ( natsemi, &natsemi->rx );
  509. err_create_rx:
  510. natsemi_destroy_ring ( natsemi, &natsemi->tx );
  511. err_create_tx:
  512. return rc;
  513. }
  514. /**
  515. * Close network device
  516. *
  517. * @v netdev Network device
  518. */
  519. static void natsemi_close ( struct net_device *netdev ) {
  520. struct natsemi_nic *natsemi = netdev->priv;
  521. unsigned int i;
  522. /* Mask transmit and receive interrupts */
  523. writel ( 0, natsemi->regs + NATSEMI_IMR );
  524. /* Reset and disable transmitter and receiver */
  525. writel ( ( NATSEMI_CR_RXR | NATSEMI_CR_TXR ),
  526. natsemi->regs + NATSEMI_CR );
  527. /* Discard any unused receive buffers */
  528. for ( i = 0 ; i < NATSEMI_NUM_RX_DESC ; i++ ) {
  529. if ( natsemi->rx_iobuf[i] )
  530. free_iob ( natsemi->rx_iobuf[i] );
  531. natsemi->rx_iobuf[i] = NULL;
  532. }
  533. /* Destroy receive descriptor ring */
  534. natsemi_destroy_ring ( natsemi, &natsemi->rx );
  535. /* Destroy transmit descriptor ring */
  536. natsemi_destroy_ring ( natsemi, &natsemi->tx );
  537. }
  538. /**
  539. * Transmit packet
  540. *
  541. * @v netdev Network device
  542. * @v iobuf I/O buffer
  543. * @ret rc Return status code
  544. */
  545. static int natsemi_transmit ( struct net_device *netdev,
  546. struct io_buffer *iobuf ) {
  547. struct natsemi_nic *natsemi = netdev->priv;
  548. union natsemi_descriptor *tx;
  549. unsigned int tx_idx;
  550. physaddr_t address;
  551. /* Check address is usable by card */
  552. address = virt_to_bus ( iobuf->data );
  553. if ( ! natsemi_address_ok ( natsemi, address ) ) {
  554. DBGC ( natsemi, "NATSEMI %p cannot support 64-bit TX buffer "
  555. "address\n", natsemi );
  556. return -ENOTSUP;
  557. }
  558. /* Get next transmit descriptor */
  559. if ( ( natsemi->tx.prod - natsemi->tx.cons ) >= NATSEMI_NUM_TX_DESC ) {
  560. DBGC ( natsemi, "NATSEMI %p out of transmit descriptors\n",
  561. natsemi );
  562. return -ENOBUFS;
  563. }
  564. tx_idx = ( natsemi->tx.prod++ % NATSEMI_NUM_TX_DESC );
  565. tx = &natsemi->tx.desc[tx_idx];
  566. /* Populate transmit descriptor */
  567. if ( natsemi->flags & NATSEMI_64BIT ) {
  568. tx->d64.bufptr = cpu_to_le64 ( address );
  569. } else {
  570. tx->d32pad.d32.bufptr = cpu_to_le32 ( address );
  571. }
  572. wmb();
  573. tx->common.cmdsts = cpu_to_le32 ( NATSEMI_DESC_OWN | NATSEMI_DESC_INTR |
  574. iob_len ( iobuf ) );
  575. wmb();
  576. /* Notify card that there are packets ready to transmit */
  577. writel ( NATSEMI_CR_TXE, natsemi->regs + NATSEMI_CR );
  578. DBGC2 ( natsemi, "NATSEMI %p TX %d is [%llx,%llx)\n", natsemi, tx_idx,
  579. ( ( unsigned long long ) address ),
  580. ( ( unsigned long long ) address + iob_len ( iobuf ) ) );
  581. return 0;
  582. }
  583. /**
  584. * Poll for completed packets
  585. *
  586. * @v netdev Network device
  587. */
  588. static void natsemi_poll_tx ( struct net_device *netdev ) {
  589. struct natsemi_nic *natsemi = netdev->priv;
  590. union natsemi_descriptor *tx;
  591. unsigned int tx_idx;
  592. /* Check for completed packets */
  593. while ( natsemi->tx.cons != natsemi->tx.prod ) {
  594. /* Get next transmit descriptor */
  595. tx_idx = ( natsemi->tx.cons % NATSEMI_NUM_TX_DESC );
  596. tx = &natsemi->tx.desc[tx_idx];
  597. /* Stop if descriptor is still in use */
  598. if ( tx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OWN ) )
  599. return;
  600. /* Complete TX descriptor */
  601. if ( tx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OK ) ) {
  602. DBGC2 ( natsemi, "NATSEMI %p TX %d complete\n",
  603. natsemi, tx_idx );
  604. netdev_tx_complete_next ( netdev );
  605. } else {
  606. DBGC ( natsemi, "NATSEMI %p TX %d completion error "
  607. "(%08x)\n", natsemi, tx_idx,
  608. le32_to_cpu ( tx->common.cmdsts ) );
  609. netdev_tx_complete_next_err ( netdev, -EIO );
  610. }
  611. natsemi->tx.cons++;
  612. }
  613. }
  614. /**
  615. * Poll for received packets
  616. *
  617. * @v netdev Network device
  618. */
  619. static void natsemi_poll_rx ( struct net_device *netdev ) {
  620. struct natsemi_nic *natsemi = netdev->priv;
  621. union natsemi_descriptor *rx;
  622. struct io_buffer *iobuf;
  623. unsigned int rx_idx;
  624. size_t len;
  625. /* Check for received packets */
  626. while ( natsemi->rx.cons != natsemi->rx.prod ) {
  627. /* Get next receive descriptor */
  628. rx_idx = ( natsemi->rx.cons % NATSEMI_NUM_RX_DESC );
  629. rx = &natsemi->rx.desc[rx_idx];
  630. /* Stop if descriptor is still in use */
  631. if ( ! ( rx->common.cmdsts & NATSEMI_DESC_OWN ) )
  632. return;
  633. /* Populate I/O buffer */
  634. iobuf = natsemi->rx_iobuf[rx_idx];
  635. natsemi->rx_iobuf[rx_idx] = NULL;
  636. len = ( le32_to_cpu ( rx->common.cmdsts ) &
  637. NATSEMI_DESC_SIZE_MASK );
  638. iob_put ( iobuf, len - 4 /* strip CRC */ );
  639. /* Hand off to network stack */
  640. if ( rx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OK ) ) {
  641. DBGC2 ( natsemi, "NATSEMI %p RX %d complete (length "
  642. "%zd)\n", natsemi, rx_idx, len );
  643. netdev_rx ( netdev, iobuf );
  644. } else {
  645. DBGC ( natsemi, "NATSEMI %p RX %d error (length %zd, "
  646. "status %08x)\n", natsemi, rx_idx, len,
  647. le32_to_cpu ( rx->common.cmdsts ) );
  648. netdev_rx_err ( netdev, iobuf, -EIO );
  649. }
  650. natsemi->rx.cons++;
  651. }
  652. }
  653. /**
  654. * Poll for completed and received packets
  655. *
  656. * @v netdev Network device
  657. */
  658. static void natsemi_poll ( struct net_device *netdev ) {
  659. struct natsemi_nic *natsemi = netdev->priv;
  660. uint32_t isr;
  661. /* Poll for link state. The PHY interrupt seems not to
  662. * function as expected, and polling for the link state is
  663. * only a single register read.
  664. */
  665. natsemi_check_link ( netdev );
  666. /* Check for and acknowledge interrupts */
  667. isr = readl ( natsemi->regs + NATSEMI_ISR );
  668. if ( ! isr )
  669. return;
  670. /* Poll for TX completions, if applicable */
  671. if ( isr & NATSEMI_IRQ_TXDESC )
  672. natsemi_poll_tx ( netdev );
  673. /* Poll for RX completionsm, if applicable */
  674. if ( isr & NATSEMI_IRQ_RXDESC )
  675. natsemi_poll_rx ( netdev );
  676. /* Refill RX ring */
  677. natsemi_refill_rx ( netdev );
  678. }
  679. /**
  680. * Enable or disable interrupts
  681. *
  682. * @v netdev Network device
  683. * @v enable Interrupts should be enabled
  684. */
  685. static void natsemi_irq ( struct net_device *netdev, int enable ) {
  686. struct natsemi_nic *natsemi = netdev->priv;
  687. /* Enable or disable interrupts */
  688. writel ( ( enable ? NATSEMI_IER_IE : 0 ), natsemi->regs + NATSEMI_IER );
  689. }
  690. /** National Semiconductor network device operations */
  691. static struct net_device_operations natsemi_operations = {
  692. .open = natsemi_open,
  693. .close = natsemi_close,
  694. .transmit = natsemi_transmit,
  695. .poll = natsemi_poll,
  696. .irq = natsemi_irq,
  697. };
  698. /******************************************************************************
  699. *
  700. * PCI interface
  701. *
  702. ******************************************************************************
  703. */
  704. /**
  705. * Probe PCI device
  706. *
  707. * @v pci PCI device
  708. * @ret rc Return status code
  709. */
  710. static int natsemi_probe ( struct pci_device *pci ) {
  711. struct net_device *netdev;
  712. struct natsemi_nic *natsemi;
  713. int rc;
  714. /* Allocate and initialise net device */
  715. netdev = alloc_etherdev ( sizeof ( *natsemi ) );
  716. if ( ! netdev ) {
  717. rc = -ENOMEM;
  718. goto err_alloc;
  719. }
  720. netdev_init ( netdev, &natsemi_operations );
  721. natsemi = netdev->priv;
  722. pci_set_drvdata ( pci, netdev );
  723. netdev->dev = &pci->dev;
  724. memset ( natsemi, 0, sizeof ( *natsemi ) );
  725. natsemi->flags = pci->id->driver_data;
  726. natsemi_init_ring ( &natsemi->tx, NATSEMI_NUM_TX_DESC, NATSEMI_TXDP );
  727. natsemi_init_ring ( &natsemi->rx, NATSEMI_NUM_RX_DESC, NATSEMI_RXDP );
  728. /* Fix up PCI device */
  729. adjust_pci_device ( pci );
  730. /* Map registers */
  731. natsemi->regs = ioremap ( pci->membase, NATSEMI_BAR_SIZE );
  732. if ( ! natsemi->regs ) {
  733. rc = -ENODEV;
  734. goto err_ioremap;
  735. }
  736. /* Reset the NIC */
  737. if ( ( rc = natsemi_reset ( natsemi ) ) != 0 )
  738. goto err_reset;
  739. /* Initialise EEPROM */
  740. natsemi_init_eeprom ( natsemi );
  741. /* Read initial MAC address */
  742. if ( ( rc = natsemi_hwaddr ( natsemi, netdev->hw_addr ) ) != 0 )
  743. goto err_hwaddr;
  744. /* Register network device */
  745. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  746. goto err_register_netdev;
  747. /* Set initial link state */
  748. natsemi_check_link ( netdev );
  749. return 0;
  750. unregister_netdev ( netdev );
  751. err_register_netdev:
  752. err_hwaddr:
  753. natsemi_reset ( natsemi );
  754. err_reset:
  755. iounmap ( natsemi->regs );
  756. err_ioremap:
  757. netdev_nullify ( netdev );
  758. netdev_put ( netdev );
  759. err_alloc:
  760. return rc;
  761. }
  762. /**
  763. * Remove PCI device
  764. *
  765. * @v pci PCI device
  766. */
  767. static void natsemi_remove ( struct pci_device *pci ) {
  768. struct net_device *netdev = pci_get_drvdata ( pci );
  769. struct natsemi_nic *natsemi = netdev->priv;
  770. /* Unregister network device */
  771. unregister_netdev ( netdev );
  772. /* Reset card */
  773. natsemi_reset ( natsemi );
  774. /* Free network device */
  775. iounmap ( natsemi->regs );
  776. netdev_nullify ( netdev );
  777. netdev_put ( netdev );
  778. }
  779. /** Flags for DP83815 */
  780. #define DP83815_FLAGS ( NATSEMI_EEPROM_LITTLE_ENDIAN | NATSEMI_EEPROM_INSANE )
  781. /** Flags for DP83820 */
  782. #define DP83820_FLAGS ( NATSEMI_64BIT | NATSEMI_1000 )
  783. /** National Semiconductor PCI device IDs */
  784. static struct pci_device_id natsemi_nics[] = {
  785. PCI_ROM ( 0x100b, 0x0020, "dp83815", "DP83815", DP83815_FLAGS ),
  786. PCI_ROM ( 0x100b, 0x0022, "dp83820", "DP83820", DP83820_FLAGS ),
  787. };
  788. /** National Semiconductor PCI driver */
  789. struct pci_driver natsemi_driver __pci_driver = {
  790. .ids = natsemi_nics,
  791. .id_count = ( sizeof ( natsemi_nics ) / sizeof ( natsemi_nics[0] ) ),
  792. .probe = natsemi_probe,
  793. .remove = natsemi_remove,
  794. };