Linux Audio

Check our new training course

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