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