/drivers/net/ethernet/micrel/ks8851.c

http://github.com/mirrors/linux · C · 1613 lines · 913 code · 306 blank · 394 comment · 90 complexity · eb9756d1c10e13d1f7e1ec866d9ec32e MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* drivers/net/ethernet/micrel/ks8851.c
  3. *
  4. * Copyright 2009 Simtec Electronics
  5. * http://www.simtec.co.uk/
  6. * Ben Dooks <ben@simtec.co.uk>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #define DEBUG
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/cache.h>
  17. #include <linux/crc32.h>
  18. #include <linux/mii.h>
  19. #include <linux/eeprom_93cx6.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_net.h>
  25. #include "ks8851.h"
  26. /**
  27. * struct ks8851_rxctrl - KS8851 driver rx control
  28. * @mchash: Multicast hash-table data.
  29. * @rxcr1: KS_RXCR1 register setting
  30. * @rxcr2: KS_RXCR2 register setting
  31. *
  32. * Representation of the settings needs to control the receive filtering
  33. * such as the multicast hash-filter and the receive register settings. This
  34. * is used to make the job of working out if the receive settings change and
  35. * then issuing the new settings to the worker that will send the necessary
  36. * commands.
  37. */
  38. struct ks8851_rxctrl {
  39. u16 mchash[4];
  40. u16 rxcr1;
  41. u16 rxcr2;
  42. };
  43. /**
  44. * union ks8851_tx_hdr - tx header data
  45. * @txb: The header as bytes
  46. * @txw: The header as 16bit, little-endian words
  47. *
  48. * A dual representation of the tx header data to allow
  49. * access to individual bytes, and to allow 16bit accesses
  50. * with 16bit alignment.
  51. */
  52. union ks8851_tx_hdr {
  53. u8 txb[6];
  54. __le16 txw[3];
  55. };
  56. /**
  57. * struct ks8851_net - KS8851 driver private data
  58. * @netdev: The network device we're bound to
  59. * @spidev: The spi device we're bound to.
  60. * @lock: Lock to ensure that the device is not accessed when busy.
  61. * @statelock: Lock on this structure for tx list.
  62. * @mii: The MII state information for the mii calls.
  63. * @rxctrl: RX settings for @rxctrl_work.
  64. * @tx_work: Work queue for tx packets
  65. * @rxctrl_work: Work queue for updating RX mode and multicast lists
  66. * @txq: Queue of packets for transmission.
  67. * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
  68. * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
  69. * @txh: Space for generating packet TX header in DMA-able data
  70. * @rxd: Space for receiving SPI data, in DMA-able space.
  71. * @txd: Space for transmitting SPI data, in DMA-able space.
  72. * @msg_enable: The message flags controlling driver output (see ethtool).
  73. * @fid: Incrementing frame id tag.
  74. * @rc_ier: Cached copy of KS_IER.
  75. * @rc_ccr: Cached copy of KS_CCR.
  76. * @rc_rxqcr: Cached copy of KS_RXQCR.
  77. * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
  78. * @vdd_reg: Optional regulator supplying the chip
  79. * @vdd_io: Optional digital power supply for IO
  80. * @gpio: Optional reset_n gpio
  81. *
  82. * The @lock ensures that the chip is protected when certain operations are
  83. * in progress. When the read or write packet transfer is in progress, most
  84. * of the chip registers are not ccessible until the transfer is finished and
  85. * the DMA has been de-asserted.
  86. *
  87. * The @statelock is used to protect information in the structure which may
  88. * need to be accessed via several sources, such as the network driver layer
  89. * or one of the work queues.
  90. *
  91. * We align the buffers we may use for rx/tx to ensure that if the SPI driver
  92. * wants to DMA map them, it will not have any problems with data the driver
  93. * modifies.
  94. */
  95. struct ks8851_net {
  96. struct net_device *netdev;
  97. struct spi_device *spidev;
  98. struct mutex lock;
  99. spinlock_t statelock;
  100. union ks8851_tx_hdr txh ____cacheline_aligned;
  101. u8 rxd[8];
  102. u8 txd[8];
  103. u32 msg_enable ____cacheline_aligned;
  104. u16 tx_space;
  105. u8 fid;
  106. u16 rc_ier;
  107. u16 rc_rxqcr;
  108. u16 rc_ccr;
  109. struct mii_if_info mii;
  110. struct ks8851_rxctrl rxctrl;
  111. struct work_struct tx_work;
  112. struct work_struct rxctrl_work;
  113. struct sk_buff_head txq;
  114. struct spi_message spi_msg1;
  115. struct spi_message spi_msg2;
  116. struct spi_transfer spi_xfer1;
  117. struct spi_transfer spi_xfer2[2];
  118. struct eeprom_93cx6 eeprom;
  119. struct regulator *vdd_reg;
  120. struct regulator *vdd_io;
  121. int gpio;
  122. };
  123. static int msg_enable;
  124. /* SPI frame opcodes */
  125. #define KS_SPIOP_RD (0x00)
  126. #define KS_SPIOP_WR (0x40)
  127. #define KS_SPIOP_RXFIFO (0x80)
  128. #define KS_SPIOP_TXFIFO (0xC0)
  129. /* shift for byte-enable data */
  130. #define BYTE_EN(_x) ((_x) << 2)
  131. /* turn register number and byte-enable mask into data for start of packet */
  132. #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
  133. /* SPI register read/write calls.
  134. *
  135. * All these calls issue SPI transactions to access the chip's registers. They
  136. * all require that the necessary lock is held to prevent accesses when the
  137. * chip is busy transferring packet data (RX/TX FIFO accesses).
  138. */
  139. /**
  140. * ks8851_wrreg16 - write 16bit register value to chip
  141. * @ks: The chip state
  142. * @reg: The register address
  143. * @val: The value to write
  144. *
  145. * Issue a write to put the value @val into the register specified in @reg.
  146. */
  147. static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
  148. {
  149. struct spi_transfer *xfer = &ks->spi_xfer1;
  150. struct spi_message *msg = &ks->spi_msg1;
  151. __le16 txb[2];
  152. int ret;
  153. txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
  154. txb[1] = cpu_to_le16(val);
  155. xfer->tx_buf = txb;
  156. xfer->rx_buf = NULL;
  157. xfer->len = 4;
  158. ret = spi_sync(ks->spidev, msg);
  159. if (ret < 0)
  160. netdev_err(ks->netdev, "spi_sync() failed\n");
  161. }
  162. /**
  163. * ks8851_wrreg8 - write 8bit register value to chip
  164. * @ks: The chip state
  165. * @reg: The register address
  166. * @val: The value to write
  167. *
  168. * Issue a write to put the value @val into the register specified in @reg.
  169. */
  170. static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
  171. {
  172. struct spi_transfer *xfer = &ks->spi_xfer1;
  173. struct spi_message *msg = &ks->spi_msg1;
  174. __le16 txb[2];
  175. int ret;
  176. int bit;
  177. bit = 1 << (reg & 3);
  178. txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
  179. txb[1] = val;
  180. xfer->tx_buf = txb;
  181. xfer->rx_buf = NULL;
  182. xfer->len = 3;
  183. ret = spi_sync(ks->spidev, msg);
  184. if (ret < 0)
  185. netdev_err(ks->netdev, "spi_sync() failed\n");
  186. }
  187. /**
  188. * ks8851_rdreg - issue read register command and return the data
  189. * @ks: The device state
  190. * @op: The register address and byte enables in message format.
  191. * @rxb: The RX buffer to return the result into
  192. * @rxl: The length of data expected.
  193. *
  194. * This is the low level read call that issues the necessary spi message(s)
  195. * to read data from the register specified in @op.
  196. */
  197. static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
  198. u8 *rxb, unsigned rxl)
  199. {
  200. struct spi_transfer *xfer;
  201. struct spi_message *msg;
  202. __le16 *txb = (__le16 *)ks->txd;
  203. u8 *trx = ks->rxd;
  204. int ret;
  205. txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
  206. if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
  207. msg = &ks->spi_msg2;
  208. xfer = ks->spi_xfer2;
  209. xfer->tx_buf = txb;
  210. xfer->rx_buf = NULL;
  211. xfer->len = 2;
  212. xfer++;
  213. xfer->tx_buf = NULL;
  214. xfer->rx_buf = trx;
  215. xfer->len = rxl;
  216. } else {
  217. msg = &ks->spi_msg1;
  218. xfer = &ks->spi_xfer1;
  219. xfer->tx_buf = txb;
  220. xfer->rx_buf = trx;
  221. xfer->len = rxl + 2;
  222. }
  223. ret = spi_sync(ks->spidev, msg);
  224. if (ret < 0)
  225. netdev_err(ks->netdev, "read: spi_sync() failed\n");
  226. else if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
  227. memcpy(rxb, trx, rxl);
  228. else
  229. memcpy(rxb, trx + 2, rxl);
  230. }
  231. /**
  232. * ks8851_rdreg8 - read 8 bit register from device
  233. * @ks: The chip information
  234. * @reg: The register address
  235. *
  236. * Read a 8bit register from the chip, returning the result
  237. */
  238. static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
  239. {
  240. u8 rxb[1];
  241. ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
  242. return rxb[0];
  243. }
  244. /**
  245. * ks8851_rdreg16 - read 16 bit register from device
  246. * @ks: The chip information
  247. * @reg: The register address
  248. *
  249. * Read a 16bit register from the chip, returning the result
  250. */
  251. static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
  252. {
  253. __le16 rx = 0;
  254. ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
  255. return le16_to_cpu(rx);
  256. }
  257. /**
  258. * ks8851_rdreg32 - read 32 bit register from device
  259. * @ks: The chip information
  260. * @reg: The register address
  261. *
  262. * Read a 32bit register from the chip.
  263. *
  264. * Note, this read requires the address be aligned to 4 bytes.
  265. */
  266. static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
  267. {
  268. __le32 rx = 0;
  269. WARN_ON(reg & 3);
  270. ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
  271. return le32_to_cpu(rx);
  272. }
  273. /**
  274. * ks8851_soft_reset - issue one of the soft reset to the device
  275. * @ks: The device state.
  276. * @op: The bit(s) to set in the GRR
  277. *
  278. * Issue the relevant soft-reset command to the device's GRR register
  279. * specified by @op.
  280. *
  281. * Note, the delays are in there as a caution to ensure that the reset
  282. * has time to take effect and then complete. Since the datasheet does
  283. * not currently specify the exact sequence, we have chosen something
  284. * that seems to work with our device.
  285. */
  286. static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
  287. {
  288. ks8851_wrreg16(ks, KS_GRR, op);
  289. mdelay(1); /* wait a short time to effect reset */
  290. ks8851_wrreg16(ks, KS_GRR, 0);
  291. mdelay(1); /* wait for condition to clear */
  292. }
  293. /**
  294. * ks8851_set_powermode - set power mode of the device
  295. * @ks: The device state
  296. * @pwrmode: The power mode value to write to KS_PMECR.
  297. *
  298. * Change the power mode of the chip.
  299. */
  300. static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
  301. {
  302. unsigned pmecr;
  303. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  304. pmecr = ks8851_rdreg16(ks, KS_PMECR);
  305. pmecr &= ~PMECR_PM_MASK;
  306. pmecr |= pwrmode;
  307. ks8851_wrreg16(ks, KS_PMECR, pmecr);
  308. }
  309. /**
  310. * ks8851_write_mac_addr - write mac address to device registers
  311. * @dev: The network device
  312. *
  313. * Update the KS8851 MAC address registers from the address in @dev.
  314. *
  315. * This call assumes that the chip is not running, so there is no need to
  316. * shutdown the RXQ process whilst setting this.
  317. */
  318. static int ks8851_write_mac_addr(struct net_device *dev)
  319. {
  320. struct ks8851_net *ks = netdev_priv(dev);
  321. int i;
  322. mutex_lock(&ks->lock);
  323. /*
  324. * Wake up chip in case it was powered off when stopped; otherwise,
  325. * the first write to the MAC address does not take effect.
  326. */
  327. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  328. for (i = 0; i < ETH_ALEN; i++)
  329. ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
  330. if (!netif_running(dev))
  331. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  332. mutex_unlock(&ks->lock);
  333. return 0;
  334. }
  335. /**
  336. * ks8851_read_mac_addr - read mac address from device registers
  337. * @dev: The network device
  338. *
  339. * Update our copy of the KS8851 MAC address from the registers of @dev.
  340. */
  341. static void ks8851_read_mac_addr(struct net_device *dev)
  342. {
  343. struct ks8851_net *ks = netdev_priv(dev);
  344. int i;
  345. mutex_lock(&ks->lock);
  346. for (i = 0; i < ETH_ALEN; i++)
  347. dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
  348. mutex_unlock(&ks->lock);
  349. }
  350. /**
  351. * ks8851_init_mac - initialise the mac address
  352. * @ks: The device structure
  353. *
  354. * Get or create the initial mac address for the device and then set that
  355. * into the station address register. A mac address supplied in the device
  356. * tree takes precedence. Otherwise, if there is an EEPROM present, then
  357. * we try that. If no valid mac address is found we use eth_random_addr()
  358. * to create a new one.
  359. */
  360. static void ks8851_init_mac(struct ks8851_net *ks)
  361. {
  362. struct net_device *dev = ks->netdev;
  363. const u8 *mac_addr;
  364. mac_addr = of_get_mac_address(ks->spidev->dev.of_node);
  365. if (!IS_ERR(mac_addr)) {
  366. ether_addr_copy(dev->dev_addr, mac_addr);
  367. ks8851_write_mac_addr(dev);
  368. return;
  369. }
  370. if (ks->rc_ccr & CCR_EEPROM) {
  371. ks8851_read_mac_addr(dev);
  372. if (is_valid_ether_addr(dev->dev_addr))
  373. return;
  374. netdev_err(ks->netdev, "invalid mac address read %pM\n",
  375. dev->dev_addr);
  376. }
  377. eth_hw_addr_random(dev);
  378. ks8851_write_mac_addr(dev);
  379. }
  380. /**
  381. * ks8851_rdfifo - read data from the receive fifo
  382. * @ks: The device state.
  383. * @buff: The buffer address
  384. * @len: The length of the data to read
  385. *
  386. * Issue an RXQ FIFO read command and read the @len amount of data from
  387. * the FIFO into the buffer specified by @buff.
  388. */
  389. static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
  390. {
  391. struct spi_transfer *xfer = ks->spi_xfer2;
  392. struct spi_message *msg = &ks->spi_msg2;
  393. u8 txb[1];
  394. int ret;
  395. netif_dbg(ks, rx_status, ks->netdev,
  396. "%s: %d@%p\n", __func__, len, buff);
  397. /* set the operation we're issuing */
  398. txb[0] = KS_SPIOP_RXFIFO;
  399. xfer->tx_buf = txb;
  400. xfer->rx_buf = NULL;
  401. xfer->len = 1;
  402. xfer++;
  403. xfer->rx_buf = buff;
  404. xfer->tx_buf = NULL;
  405. xfer->len = len;
  406. ret = spi_sync(ks->spidev, msg);
  407. if (ret < 0)
  408. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  409. }
  410. /**
  411. * ks8851_dbg_dumpkkt - dump initial packet contents to debug
  412. * @ks: The device state
  413. * @rxpkt: The data for the received packet
  414. *
  415. * Dump the initial data from the packet to dev_dbg().
  416. */
  417. static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
  418. {
  419. netdev_dbg(ks->netdev,
  420. "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  421. rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
  422. rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
  423. rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
  424. }
  425. /**
  426. * ks8851_rx_pkts - receive packets from the host
  427. * @ks: The device information.
  428. *
  429. * This is called from the IRQ work queue when the system detects that there
  430. * are packets in the receive queue. Find out how many packets there are and
  431. * read them from the FIFO.
  432. */
  433. static void ks8851_rx_pkts(struct ks8851_net *ks)
  434. {
  435. struct sk_buff *skb;
  436. unsigned rxfc;
  437. unsigned rxlen;
  438. unsigned rxstat;
  439. u32 rxh;
  440. u8 *rxpkt;
  441. rxfc = ks8851_rdreg8(ks, KS_RXFC);
  442. netif_dbg(ks, rx_status, ks->netdev,
  443. "%s: %d packets\n", __func__, rxfc);
  444. /* Currently we're issuing a read per packet, but we could possibly
  445. * improve the code by issuing a single read, getting the receive
  446. * header, allocating the packet and then reading the packet data
  447. * out in one go.
  448. *
  449. * This form of operation would require us to hold the SPI bus'
  450. * chipselect low during the entie transaction to avoid any
  451. * reset to the data stream coming from the chip.
  452. */
  453. for (; rxfc != 0; rxfc--) {
  454. rxh = ks8851_rdreg32(ks, KS_RXFHSR);
  455. rxstat = rxh & 0xffff;
  456. rxlen = (rxh >> 16) & 0xfff;
  457. netif_dbg(ks, rx_status, ks->netdev,
  458. "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
  459. /* the length of the packet includes the 32bit CRC */
  460. /* set dma read address */
  461. ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
  462. /* start DMA access */
  463. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  464. if (rxlen > 4) {
  465. unsigned int rxalign;
  466. rxlen -= 4;
  467. rxalign = ALIGN(rxlen, 4);
  468. skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
  469. if (skb) {
  470. /* 4 bytes of status header + 4 bytes of
  471. * garbage: we put them before ethernet
  472. * header, so that they are copied,
  473. * but ignored.
  474. */
  475. rxpkt = skb_put(skb, rxlen) - 8;
  476. ks8851_rdfifo(ks, rxpkt, rxalign + 8);
  477. if (netif_msg_pktdata(ks))
  478. ks8851_dbg_dumpkkt(ks, rxpkt);
  479. skb->protocol = eth_type_trans(skb, ks->netdev);
  480. netif_rx_ni(skb);
  481. ks->netdev->stats.rx_packets++;
  482. ks->netdev->stats.rx_bytes += rxlen;
  483. }
  484. }
  485. /* end DMA access and dequeue packet */
  486. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
  487. }
  488. }
  489. /**
  490. * ks8851_irq - IRQ handler for dealing with interrupt requests
  491. * @irq: IRQ number
  492. * @_ks: cookie
  493. *
  494. * This handler is invoked when the IRQ line asserts to find out what happened.
  495. * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
  496. * in thread context.
  497. *
  498. * Read the interrupt status, work out what needs to be done and then clear
  499. * any of the interrupts that are not needed.
  500. */
  501. static irqreturn_t ks8851_irq(int irq, void *_ks)
  502. {
  503. struct ks8851_net *ks = _ks;
  504. unsigned status;
  505. unsigned handled = 0;
  506. mutex_lock(&ks->lock);
  507. status = ks8851_rdreg16(ks, KS_ISR);
  508. netif_dbg(ks, intr, ks->netdev,
  509. "%s: status 0x%04x\n", __func__, status);
  510. if (status & IRQ_LCI)
  511. handled |= IRQ_LCI;
  512. if (status & IRQ_LDI) {
  513. u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
  514. pmecr &= ~PMECR_WKEVT_MASK;
  515. ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  516. handled |= IRQ_LDI;
  517. }
  518. if (status & IRQ_RXPSI)
  519. handled |= IRQ_RXPSI;
  520. if (status & IRQ_TXI) {
  521. handled |= IRQ_TXI;
  522. /* no lock here, tx queue should have been stopped */
  523. /* update our idea of how much tx space is available to the
  524. * system */
  525. ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
  526. netif_dbg(ks, intr, ks->netdev,
  527. "%s: txspace %d\n", __func__, ks->tx_space);
  528. }
  529. if (status & IRQ_RXI)
  530. handled |= IRQ_RXI;
  531. if (status & IRQ_SPIBEI) {
  532. dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
  533. handled |= IRQ_SPIBEI;
  534. }
  535. ks8851_wrreg16(ks, KS_ISR, handled);
  536. if (status & IRQ_RXI) {
  537. /* the datasheet says to disable the rx interrupt during
  538. * packet read-out, however we're masking the interrupt
  539. * from the device so do not bother masking just the RX
  540. * from the device. */
  541. ks8851_rx_pkts(ks);
  542. }
  543. /* if something stopped the rx process, probably due to wanting
  544. * to change the rx settings, then do something about restarting
  545. * it. */
  546. if (status & IRQ_RXPSI) {
  547. struct ks8851_rxctrl *rxc = &ks->rxctrl;
  548. /* update the multicast hash table */
  549. ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
  550. ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
  551. ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
  552. ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
  553. ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
  554. ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
  555. }
  556. mutex_unlock(&ks->lock);
  557. if (status & IRQ_LCI)
  558. mii_check_link(&ks->mii);
  559. if (status & IRQ_TXI)
  560. netif_wake_queue(ks->netdev);
  561. return IRQ_HANDLED;
  562. }
  563. /**
  564. * calc_txlen - calculate size of message to send packet
  565. * @len: Length of data
  566. *
  567. * Returns the size of the TXFIFO message needed to send
  568. * this packet.
  569. */
  570. static inline unsigned calc_txlen(unsigned len)
  571. {
  572. return ALIGN(len + 4, 4);
  573. }
  574. /**
  575. * ks8851_wrpkt - write packet to TX FIFO
  576. * @ks: The device state.
  577. * @txp: The sk_buff to transmit.
  578. * @irq: IRQ on completion of the packet.
  579. *
  580. * Send the @txp to the chip. This means creating the relevant packet header
  581. * specifying the length of the packet and the other information the chip
  582. * needs, such as IRQ on completion. Send the header and the packet data to
  583. * the device.
  584. */
  585. static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
  586. {
  587. struct spi_transfer *xfer = ks->spi_xfer2;
  588. struct spi_message *msg = &ks->spi_msg2;
  589. unsigned fid = 0;
  590. int ret;
  591. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  592. __func__, txp, txp->len, txp->data, irq);
  593. fid = ks->fid++;
  594. fid &= TXFR_TXFID_MASK;
  595. if (irq)
  596. fid |= TXFR_TXIC; /* irq on completion */
  597. /* start header at txb[1] to align txw entries */
  598. ks->txh.txb[1] = KS_SPIOP_TXFIFO;
  599. ks->txh.txw[1] = cpu_to_le16(fid);
  600. ks->txh.txw[2] = cpu_to_le16(txp->len);
  601. xfer->tx_buf = &ks->txh.txb[1];
  602. xfer->rx_buf = NULL;
  603. xfer->len = 5;
  604. xfer++;
  605. xfer->tx_buf = txp->data;
  606. xfer->rx_buf = NULL;
  607. xfer->len = ALIGN(txp->len, 4);
  608. ret = spi_sync(ks->spidev, msg);
  609. if (ret < 0)
  610. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  611. }
  612. /**
  613. * ks8851_done_tx - update and then free skbuff after transmitting
  614. * @ks: The device state
  615. * @txb: The buffer transmitted
  616. */
  617. static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
  618. {
  619. struct net_device *dev = ks->netdev;
  620. dev->stats.tx_bytes += txb->len;
  621. dev->stats.tx_packets++;
  622. dev_kfree_skb(txb);
  623. }
  624. /**
  625. * ks8851_tx_work - process tx packet(s)
  626. * @work: The work strucutre what was scheduled.
  627. *
  628. * This is called when a number of packets have been scheduled for
  629. * transmission and need to be sent to the device.
  630. */
  631. static void ks8851_tx_work(struct work_struct *work)
  632. {
  633. struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
  634. struct sk_buff *txb;
  635. bool last = skb_queue_empty(&ks->txq);
  636. mutex_lock(&ks->lock);
  637. while (!last) {
  638. txb = skb_dequeue(&ks->txq);
  639. last = skb_queue_empty(&ks->txq);
  640. if (txb != NULL) {
  641. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  642. ks8851_wrpkt(ks, txb, last);
  643. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  644. ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
  645. ks8851_done_tx(ks, txb);
  646. }
  647. }
  648. mutex_unlock(&ks->lock);
  649. }
  650. /**
  651. * ks8851_net_open - open network device
  652. * @dev: The network device being opened.
  653. *
  654. * Called when the network device is marked active, such as a user executing
  655. * 'ifconfig up' on the device.
  656. */
  657. static int ks8851_net_open(struct net_device *dev)
  658. {
  659. struct ks8851_net *ks = netdev_priv(dev);
  660. int ret;
  661. ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
  662. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  663. dev->name, ks);
  664. if (ret < 0) {
  665. netdev_err(dev, "failed to get irq\n");
  666. return ret;
  667. }
  668. /* lock the card, even if we may not actually be doing anything
  669. * else at the moment */
  670. mutex_lock(&ks->lock);
  671. netif_dbg(ks, ifup, ks->netdev, "opening\n");
  672. /* bring chip out of any power saving mode it was in */
  673. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  674. /* issue a soft reset to the RX/TX QMU to put it into a known
  675. * state. */
  676. ks8851_soft_reset(ks, GRR_QMU);
  677. /* setup transmission parameters */
  678. ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
  679. TXCR_TXPE | /* pad to min length */
  680. TXCR_TXCRC | /* add CRC */
  681. TXCR_TXFCE)); /* enable flow control */
  682. /* auto-increment tx data, reset tx pointer */
  683. ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  684. /* setup receiver control */
  685. ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
  686. RXCR1_RXFCE | /* enable flow control */
  687. RXCR1_RXBE | /* broadcast enable */
  688. RXCR1_RXUE | /* unicast enable */
  689. RXCR1_RXE)); /* enable rx block */
  690. /* transfer entire frames out in one go */
  691. ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
  692. /* set receive counter timeouts */
  693. ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
  694. ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
  695. ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
  696. ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
  697. RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
  698. RXQCR_RXDTTE); /* IRQ on time exceeded */
  699. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  700. /* clear then enable interrupts */
  701. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  702. IRQ_TXI | /* TX done */ \
  703. IRQ_RXI | /* RX done */ \
  704. IRQ_SPIBEI | /* SPI bus error */ \
  705. IRQ_TXPSI | /* TX process stop */ \
  706. IRQ_RXPSI) /* RX process stop */
  707. ks->rc_ier = STD_IRQ;
  708. ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
  709. ks8851_wrreg16(ks, KS_IER, STD_IRQ);
  710. netif_start_queue(ks->netdev);
  711. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  712. mutex_unlock(&ks->lock);
  713. mii_check_link(&ks->mii);
  714. return 0;
  715. }
  716. /**
  717. * ks8851_net_stop - close network device
  718. * @dev: The device being closed.
  719. *
  720. * Called to close down a network device which has been active. Cancell any
  721. * work, shutdown the RX and TX process and then place the chip into a low
  722. * power state whilst it is not being used.
  723. */
  724. static int ks8851_net_stop(struct net_device *dev)
  725. {
  726. struct ks8851_net *ks = netdev_priv(dev);
  727. netif_info(ks, ifdown, dev, "shutting down\n");
  728. netif_stop_queue(dev);
  729. mutex_lock(&ks->lock);
  730. /* turn off the IRQs and ack any outstanding */
  731. ks8851_wrreg16(ks, KS_IER, 0x0000);
  732. ks8851_wrreg16(ks, KS_ISR, 0xffff);
  733. mutex_unlock(&ks->lock);
  734. /* stop any outstanding work */
  735. flush_work(&ks->tx_work);
  736. flush_work(&ks->rxctrl_work);
  737. mutex_lock(&ks->lock);
  738. /* shutdown RX process */
  739. ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
  740. /* shutdown TX process */
  741. ks8851_wrreg16(ks, KS_TXCR, 0x0000);
  742. /* set powermode to soft power down to save power */
  743. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  744. mutex_unlock(&ks->lock);
  745. /* ensure any queued tx buffers are dumped */
  746. while (!skb_queue_empty(&ks->txq)) {
  747. struct sk_buff *txb = skb_dequeue(&ks->txq);
  748. netif_dbg(ks, ifdown, ks->netdev,
  749. "%s: freeing txb %p\n", __func__, txb);
  750. dev_kfree_skb(txb);
  751. }
  752. free_irq(dev->irq, ks);
  753. return 0;
  754. }
  755. /**
  756. * ks8851_start_xmit - transmit packet
  757. * @skb: The buffer to transmit
  758. * @dev: The device used to transmit the packet.
  759. *
  760. * Called by the network layer to transmit the @skb. Queue the packet for
  761. * the device and schedule the necessary work to transmit the packet when
  762. * it is free.
  763. *
  764. * We do this to firstly avoid sleeping with the network device locked,
  765. * and secondly so we can round up more than one packet to transmit which
  766. * means we can try and avoid generating too many transmit done interrupts.
  767. */
  768. static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
  769. struct net_device *dev)
  770. {
  771. struct ks8851_net *ks = netdev_priv(dev);
  772. unsigned needed = calc_txlen(skb->len);
  773. netdev_tx_t ret = NETDEV_TX_OK;
  774. netif_dbg(ks, tx_queued, ks->netdev,
  775. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  776. spin_lock(&ks->statelock);
  777. if (needed > ks->tx_space) {
  778. netif_stop_queue(dev);
  779. ret = NETDEV_TX_BUSY;
  780. } else {
  781. ks->tx_space -= needed;
  782. skb_queue_tail(&ks->txq, skb);
  783. }
  784. spin_unlock(&ks->statelock);
  785. schedule_work(&ks->tx_work);
  786. return ret;
  787. }
  788. /**
  789. * ks8851_rxctrl_work - work handler to change rx mode
  790. * @work: The work structure this belongs to.
  791. *
  792. * Lock the device and issue the necessary changes to the receive mode from
  793. * the network device layer. This is done so that we can do this without
  794. * having to sleep whilst holding the network device lock.
  795. *
  796. * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
  797. * receive parameters are programmed, we issue a write to disable the RXQ and
  798. * then wait for the interrupt handler to be triggered once the RXQ shutdown is
  799. * complete. The interrupt handler then writes the new values into the chip.
  800. */
  801. static void ks8851_rxctrl_work(struct work_struct *work)
  802. {
  803. struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
  804. mutex_lock(&ks->lock);
  805. /* need to shutdown RXQ before modifying filter parameters */
  806. ks8851_wrreg16(ks, KS_RXCR1, 0x00);
  807. mutex_unlock(&ks->lock);
  808. }
  809. static void ks8851_set_rx_mode(struct net_device *dev)
  810. {
  811. struct ks8851_net *ks = netdev_priv(dev);
  812. struct ks8851_rxctrl rxctrl;
  813. memset(&rxctrl, 0, sizeof(rxctrl));
  814. if (dev->flags & IFF_PROMISC) {
  815. /* interface to receive everything */
  816. rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
  817. } else if (dev->flags & IFF_ALLMULTI) {
  818. /* accept all multicast packets */
  819. rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
  820. RXCR1_RXPAFMA | RXCR1_RXMAFMA);
  821. } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
  822. struct netdev_hw_addr *ha;
  823. u32 crc;
  824. /* accept some multicast */
  825. netdev_for_each_mc_addr(ha, dev) {
  826. crc = ether_crc(ETH_ALEN, ha->addr);
  827. crc >>= (32 - 6); /* get top six bits */
  828. rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
  829. }
  830. rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
  831. } else {
  832. /* just accept broadcast / unicast */
  833. rxctrl.rxcr1 = RXCR1_RXPAFMA;
  834. }
  835. rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
  836. RXCR1_RXBE | /* broadcast enable */
  837. RXCR1_RXE | /* RX process enable */
  838. RXCR1_RXFCE); /* enable flow control */
  839. rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
  840. /* schedule work to do the actual set of the data if needed */
  841. spin_lock(&ks->statelock);
  842. if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
  843. memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
  844. schedule_work(&ks->rxctrl_work);
  845. }
  846. spin_unlock(&ks->statelock);
  847. }
  848. static int ks8851_set_mac_address(struct net_device *dev, void *addr)
  849. {
  850. struct sockaddr *sa = addr;
  851. if (netif_running(dev))
  852. return -EBUSY;
  853. if (!is_valid_ether_addr(sa->sa_data))
  854. return -EADDRNOTAVAIL;
  855. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  856. return ks8851_write_mac_addr(dev);
  857. }
  858. static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  859. {
  860. struct ks8851_net *ks = netdev_priv(dev);
  861. if (!netif_running(dev))
  862. return -EINVAL;
  863. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  864. }
  865. static const struct net_device_ops ks8851_netdev_ops = {
  866. .ndo_open = ks8851_net_open,
  867. .ndo_stop = ks8851_net_stop,
  868. .ndo_do_ioctl = ks8851_net_ioctl,
  869. .ndo_start_xmit = ks8851_start_xmit,
  870. .ndo_set_mac_address = ks8851_set_mac_address,
  871. .ndo_set_rx_mode = ks8851_set_rx_mode,
  872. .ndo_validate_addr = eth_validate_addr,
  873. };
  874. /* ethtool support */
  875. static void ks8851_get_drvinfo(struct net_device *dev,
  876. struct ethtool_drvinfo *di)
  877. {
  878. strlcpy(di->driver, "KS8851", sizeof(di->driver));
  879. strlcpy(di->version, "1.00", sizeof(di->version));
  880. strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
  881. }
  882. static u32 ks8851_get_msglevel(struct net_device *dev)
  883. {
  884. struct ks8851_net *ks = netdev_priv(dev);
  885. return ks->msg_enable;
  886. }
  887. static void ks8851_set_msglevel(struct net_device *dev, u32 to)
  888. {
  889. struct ks8851_net *ks = netdev_priv(dev);
  890. ks->msg_enable = to;
  891. }
  892. static int ks8851_get_link_ksettings(struct net_device *dev,
  893. struct ethtool_link_ksettings *cmd)
  894. {
  895. struct ks8851_net *ks = netdev_priv(dev);
  896. mii_ethtool_get_link_ksettings(&ks->mii, cmd);
  897. return 0;
  898. }
  899. static int ks8851_set_link_ksettings(struct net_device *dev,
  900. const struct ethtool_link_ksettings *cmd)
  901. {
  902. struct ks8851_net *ks = netdev_priv(dev);
  903. return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
  904. }
  905. static u32 ks8851_get_link(struct net_device *dev)
  906. {
  907. struct ks8851_net *ks = netdev_priv(dev);
  908. return mii_link_ok(&ks->mii);
  909. }
  910. static int ks8851_nway_reset(struct net_device *dev)
  911. {
  912. struct ks8851_net *ks = netdev_priv(dev);
  913. return mii_nway_restart(&ks->mii);
  914. }
  915. /* EEPROM support */
  916. static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
  917. {
  918. struct ks8851_net *ks = ee->data;
  919. unsigned val;
  920. val = ks8851_rdreg16(ks, KS_EEPCR);
  921. ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
  922. ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
  923. ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
  924. }
  925. static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
  926. {
  927. struct ks8851_net *ks = ee->data;
  928. unsigned val = EEPCR_EESA; /* default - eeprom access on */
  929. if (ee->drive_data)
  930. val |= EEPCR_EESRWA;
  931. if (ee->reg_data_in)
  932. val |= EEPCR_EEDO;
  933. if (ee->reg_data_clock)
  934. val |= EEPCR_EESCK;
  935. if (ee->reg_chip_select)
  936. val |= EEPCR_EECS;
  937. ks8851_wrreg16(ks, KS_EEPCR, val);
  938. }
  939. /**
  940. * ks8851_eeprom_claim - claim device EEPROM and activate the interface
  941. * @ks: The network device state.
  942. *
  943. * Check for the presence of an EEPROM, and then activate software access
  944. * to the device.
  945. */
  946. static int ks8851_eeprom_claim(struct ks8851_net *ks)
  947. {
  948. if (!(ks->rc_ccr & CCR_EEPROM))
  949. return -ENOENT;
  950. mutex_lock(&ks->lock);
  951. /* start with clock low, cs high */
  952. ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
  953. return 0;
  954. }
  955. /**
  956. * ks8851_eeprom_release - release the EEPROM interface
  957. * @ks: The device state
  958. *
  959. * Release the software access to the device EEPROM
  960. */
  961. static void ks8851_eeprom_release(struct ks8851_net *ks)
  962. {
  963. unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
  964. ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
  965. mutex_unlock(&ks->lock);
  966. }
  967. #define KS_EEPROM_MAGIC (0x00008851)
  968. static int ks8851_set_eeprom(struct net_device *dev,
  969. struct ethtool_eeprom *ee, u8 *data)
  970. {
  971. struct ks8851_net *ks = netdev_priv(dev);
  972. int offset = ee->offset;
  973. int len = ee->len;
  974. u16 tmp;
  975. /* currently only support byte writing */
  976. if (len != 1)
  977. return -EINVAL;
  978. if (ee->magic != KS_EEPROM_MAGIC)
  979. return -EINVAL;
  980. if (ks8851_eeprom_claim(ks))
  981. return -ENOENT;
  982. eeprom_93cx6_wren(&ks->eeprom, true);
  983. /* ethtool currently only supports writing bytes, which means
  984. * we have to read/modify/write our 16bit EEPROMs */
  985. eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
  986. if (offset & 1) {
  987. tmp &= 0xff;
  988. tmp |= *data << 8;
  989. } else {
  990. tmp &= 0xff00;
  991. tmp |= *data;
  992. }
  993. eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
  994. eeprom_93cx6_wren(&ks->eeprom, false);
  995. ks8851_eeprom_release(ks);
  996. return 0;
  997. }
  998. static int ks8851_get_eeprom(struct net_device *dev,
  999. struct ethtool_eeprom *ee, u8 *data)
  1000. {
  1001. struct ks8851_net *ks = netdev_priv(dev);
  1002. int offset = ee->offset;
  1003. int len = ee->len;
  1004. /* must be 2 byte aligned */
  1005. if (len & 1 || offset & 1)
  1006. return -EINVAL;
  1007. if (ks8851_eeprom_claim(ks))
  1008. return -ENOENT;
  1009. ee->magic = KS_EEPROM_MAGIC;
  1010. eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
  1011. ks8851_eeprom_release(ks);
  1012. return 0;
  1013. }
  1014. static int ks8851_get_eeprom_len(struct net_device *dev)
  1015. {
  1016. struct ks8851_net *ks = netdev_priv(dev);
  1017. /* currently, we assume it is an 93C46 attached, so return 128 */
  1018. return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
  1019. }
  1020. static const struct ethtool_ops ks8851_ethtool_ops = {
  1021. .get_drvinfo = ks8851_get_drvinfo,
  1022. .get_msglevel = ks8851_get_msglevel,
  1023. .set_msglevel = ks8851_set_msglevel,
  1024. .get_link = ks8851_get_link,
  1025. .nway_reset = ks8851_nway_reset,
  1026. .get_eeprom_len = ks8851_get_eeprom_len,
  1027. .get_eeprom = ks8851_get_eeprom,
  1028. .set_eeprom = ks8851_set_eeprom,
  1029. .get_link_ksettings = ks8851_get_link_ksettings,
  1030. .set_link_ksettings = ks8851_set_link_ksettings,
  1031. };
  1032. /* MII interface controls */
  1033. /**
  1034. * ks8851_phy_reg - convert MII register into a KS8851 register
  1035. * @reg: MII register number.
  1036. *
  1037. * Return the KS8851 register number for the corresponding MII PHY register
  1038. * if possible. Return zero if the MII register has no direct mapping to the
  1039. * KS8851 register set.
  1040. */
  1041. static int ks8851_phy_reg(int reg)
  1042. {
  1043. switch (reg) {
  1044. case MII_BMCR:
  1045. return KS_P1MBCR;
  1046. case MII_BMSR:
  1047. return KS_P1MBSR;
  1048. case MII_PHYSID1:
  1049. return KS_PHY1ILR;
  1050. case MII_PHYSID2:
  1051. return KS_PHY1IHR;
  1052. case MII_ADVERTISE:
  1053. return KS_P1ANAR;
  1054. case MII_LPA:
  1055. return KS_P1ANLPR;
  1056. }
  1057. return 0x0;
  1058. }
  1059. /**
  1060. * ks8851_phy_read - MII interface PHY register read.
  1061. * @dev: The network device the PHY is on.
  1062. * @phy_addr: Address of PHY (ignored as we only have one)
  1063. * @reg: The register to read.
  1064. *
  1065. * This call reads data from the PHY register specified in @reg. Since the
  1066. * device does not support all the MII registers, the non-existent values
  1067. * are always returned as zero.
  1068. *
  1069. * We return zero for unsupported registers as the MII code does not check
  1070. * the value returned for any error status, and simply returns it to the
  1071. * caller. The mii-tool that the driver was tested with takes any -ve error
  1072. * as real PHY capabilities, thus displaying incorrect data to the user.
  1073. */
  1074. static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
  1075. {
  1076. struct ks8851_net *ks = netdev_priv(dev);
  1077. int ksreg;
  1078. int result;
  1079. ksreg = ks8851_phy_reg(reg);
  1080. if (!ksreg)
  1081. return 0x0; /* no error return allowed, so use zero */
  1082. mutex_lock(&ks->lock);
  1083. result = ks8851_rdreg16(ks, ksreg);
  1084. mutex_unlock(&ks->lock);
  1085. return result;
  1086. }
  1087. static void ks8851_phy_write(struct net_device *dev,
  1088. int phy, int reg, int value)
  1089. {
  1090. struct ks8851_net *ks = netdev_priv(dev);
  1091. int ksreg;
  1092. ksreg = ks8851_phy_reg(reg);
  1093. if (ksreg) {
  1094. mutex_lock(&ks->lock);
  1095. ks8851_wrreg16(ks, ksreg, value);
  1096. mutex_unlock(&ks->lock);
  1097. }
  1098. }
  1099. /**
  1100. * ks8851_read_selftest - read the selftest memory info.
  1101. * @ks: The device state
  1102. *
  1103. * Read and check the TX/RX memory selftest information.
  1104. */
  1105. static int ks8851_read_selftest(struct ks8851_net *ks)
  1106. {
  1107. unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
  1108. int ret = 0;
  1109. unsigned rd;
  1110. rd = ks8851_rdreg16(ks, KS_MBIR);
  1111. if ((rd & both_done) != both_done) {
  1112. netdev_warn(ks->netdev, "Memory selftest not finished\n");
  1113. return 0;
  1114. }
  1115. if (rd & MBIR_TXMBFA) {
  1116. netdev_err(ks->netdev, "TX memory selftest fail\n");
  1117. ret |= 1;
  1118. }
  1119. if (rd & MBIR_RXMBFA) {
  1120. netdev_err(ks->netdev, "RX memory selftest fail\n");
  1121. ret |= 2;
  1122. }
  1123. return 0;
  1124. }
  1125. /* driver bus management functions */
  1126. #ifdef CONFIG_PM_SLEEP
  1127. static int ks8851_suspend(struct device *dev)
  1128. {
  1129. struct ks8851_net *ks = dev_get_drvdata(dev);
  1130. struct net_device *netdev = ks->netdev;
  1131. if (netif_running(netdev)) {
  1132. netif_device_detach(netdev);
  1133. ks8851_net_stop(netdev);
  1134. }
  1135. return 0;
  1136. }
  1137. static int ks8851_resume(struct device *dev)
  1138. {
  1139. struct ks8851_net *ks = dev_get_drvdata(dev);
  1140. struct net_device *netdev = ks->netdev;
  1141. if (netif_running(netdev)) {
  1142. ks8851_net_open(netdev);
  1143. netif_device_attach(netdev);
  1144. }
  1145. return 0;
  1146. }
  1147. #endif
  1148. static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
  1149. static int ks8851_probe(struct spi_device *spi)
  1150. {
  1151. struct net_device *ndev;
  1152. struct ks8851_net *ks;
  1153. int ret;
  1154. unsigned cider;
  1155. int gpio;
  1156. ndev = alloc_etherdev(sizeof(struct ks8851_net));
  1157. if (!ndev)
  1158. return -ENOMEM;
  1159. spi->bits_per_word = 8;
  1160. ks = netdev_priv(ndev);
  1161. ks->netdev = ndev;
  1162. ks->spidev = spi;
  1163. ks->tx_space = 6144;
  1164. gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
  1165. 0, NULL);
  1166. if (gpio == -EPROBE_DEFER) {
  1167. ret = gpio;
  1168. goto err_gpio;
  1169. }
  1170. ks->gpio = gpio;
  1171. if (gpio_is_valid(gpio)) {
  1172. ret = devm_gpio_request_one(&spi->dev, gpio,
  1173. GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
  1174. if (ret) {
  1175. dev_err(&spi->dev, "reset gpio request failed\n");
  1176. goto err_gpio;
  1177. }
  1178. }
  1179. ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
  1180. if (IS_ERR(ks->vdd_io)) {
  1181. ret = PTR_ERR(ks->vdd_io);
  1182. goto err_reg_io;
  1183. }
  1184. ret = regulator_enable(ks->vdd_io);
  1185. if (ret) {
  1186. dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
  1187. ret);
  1188. goto err_reg_io;
  1189. }
  1190. ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
  1191. if (IS_ERR(ks->vdd_reg)) {
  1192. ret = PTR_ERR(ks->vdd_reg);
  1193. goto err_reg;
  1194. }
  1195. ret = regulator_enable(ks->vdd_reg);
  1196. if (ret) {
  1197. dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
  1198. ret);
  1199. goto err_reg;
  1200. }
  1201. if (gpio_is_valid(gpio)) {
  1202. usleep_range(10000, 11000);
  1203. gpio_set_value(gpio, 1);
  1204. }
  1205. mutex_init(&ks->lock);
  1206. spin_lock_init(&ks->statelock);
  1207. INIT_WORK(&ks->tx_work, ks8851_tx_work);
  1208. INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
  1209. /* initialise pre-made spi transfer messages */
  1210. spi_message_init(&ks->spi_msg1);
  1211. spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
  1212. spi_message_init(&ks->spi_msg2);
  1213. spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
  1214. spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
  1215. /* setup EEPROM state */
  1216. ks->eeprom.data = ks;
  1217. ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1218. ks->eeprom.register_read = ks8851_eeprom_regread;
  1219. ks->eeprom.register_write = ks8851_eeprom_regwrite;
  1220. /* setup mii state */
  1221. ks->mii.dev = ndev;
  1222. ks->mii.phy_id = 1,
  1223. ks->mii.phy_id_mask = 1;
  1224. ks->mii.reg_num_mask = 0xf;
  1225. ks->mii.mdio_read = ks8851_phy_read;
  1226. ks->mii.mdio_write = ks8851_phy_write;
  1227. dev_info(&spi->dev, "message enable is %d\n", msg_enable);
  1228. /* set the default message enable */
  1229. ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
  1230. NETIF_MSG_PROBE |
  1231. NETIF_MSG_LINK));
  1232. skb_queue_head_init(&ks->txq);
  1233. ndev->ethtool_ops = &ks8851_ethtool_ops;
  1234. SET_NETDEV_DEV(ndev, &spi->dev);
  1235. spi_set_drvdata(spi, ks);
  1236. netif_carrier_off(ks->netdev);
  1237. ndev->if_port = IF_PORT_100BASET;
  1238. ndev->netdev_ops = &ks8851_netdev_ops;
  1239. ndev->irq = spi->irq;
  1240. /* issue a global soft reset to reset the device. */
  1241. ks8851_soft_reset(ks, GRR_GSR);
  1242. /* simple check for a valid chip being connected to the bus */
  1243. cider = ks8851_rdreg16(ks, KS_CIDER);
  1244. if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
  1245. dev_err(&spi->dev, "failed to read device ID\n");
  1246. ret = -ENODEV;
  1247. goto err_id;
  1248. }
  1249. /* cache the contents of the CCR register for EEPROM, etc. */
  1250. ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
  1251. ks8851_read_selftest(ks);
  1252. ks8851_init_mac(ks);
  1253. ret = register_netdev(ndev);
  1254. if (ret) {
  1255. dev_err(&spi->dev, "failed to register network device\n");
  1256. goto err_netdev;
  1257. }
  1258. netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
  1259. CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
  1260. ks->rc_ccr & CCR_EEPROM ? "has" : "no");
  1261. return 0;
  1262. err_netdev:
  1263. err_id:
  1264. if (gpio_is_valid(gpio))
  1265. gpio_set_value(gpio, 0);
  1266. regulator_disable(ks->vdd_reg);
  1267. err_reg:
  1268. regulator_disable(ks->vdd_io);
  1269. err_reg_io:
  1270. err_gpio:
  1271. free_netdev(ndev);
  1272. return ret;
  1273. }
  1274. static int ks8851_remove(struct spi_device *spi)
  1275. {
  1276. struct ks8851_net *priv = spi_get_drvdata(spi);
  1277. if (netif_msg_drv(priv))
  1278. dev_info(&spi->dev, "remove\n");
  1279. unregister_netdev(priv->netdev);
  1280. if (gpio_is_valid(priv->gpio))
  1281. gpio_set_value(priv->gpio, 0);
  1282. regulator_disable(priv->vdd_reg);
  1283. regulator_disable(priv->vdd_io);
  1284. free_netdev(priv->netdev);
  1285. return 0;
  1286. }
  1287. static const struct of_device_id ks8851_match_table[] = {
  1288. { .compatible = "micrel,ks8851" },
  1289. { }
  1290. };
  1291. MODULE_DEVICE_TABLE(of, ks8851_match_table);
  1292. static struct spi_driver ks8851_driver = {
  1293. .driver = {
  1294. .name = "ks8851",
  1295. .of_match_table = ks8851_match_table,
  1296. .pm = &ks8851_pm_ops,
  1297. },
  1298. .probe = ks8851_probe,
  1299. .remove = ks8851_remove,
  1300. };
  1301. module_spi_driver(ks8851_driver);
  1302. MODULE_DESCRIPTION("KS8851 Network driver");
  1303. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1304. MODULE_LICENSE("GPL");
  1305. module_param_named(message, msg_enable, int, 0);
  1306. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  1307. MODULE_ALIAS("spi:ks8851");