Linux Audio

Check our new training course

Embedded Linux training

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