linux/drivers/net/ethernet/qualcomm/qca_spi.c
<<
>>
Prefs
   1/*
   2 *   Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
   3 *   Copyright (c) 2014, I2SE GmbH
   4 *
   5 *   Permission to use, copy, modify, and/or distribute this software
   6 *   for any purpose with or without fee is hereby granted, provided
   7 *   that the above copyright notice and this permission notice appear
   8 *   in all copies.
   9 *
  10 *   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11 *   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12 *   WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  13 *   THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  14 *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15 *   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  16 *   NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  17 *   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18 */
  19
  20/*   This module implements the Qualcomm Atheros SPI protocol for
  21 *   kernel-based SPI device; it is essentially an Ethernet-to-SPI
  22 *   serial converter;
  23 */
  24
  25#include <linux/errno.h>
  26#include <linux/etherdevice.h>
  27#include <linux/if_arp.h>
  28#include <linux/if_ether.h>
  29#include <linux/init.h>
  30#include <linux/interrupt.h>
  31#include <linux/jiffies.h>
  32#include <linux/kernel.h>
  33#include <linux/kthread.h>
  34#include <linux/module.h>
  35#include <linux/moduleparam.h>
  36#include <linux/netdevice.h>
  37#include <linux/of.h>
  38#include <linux/of_device.h>
  39#include <linux/of_net.h>
  40#include <linux/sched.h>
  41#include <linux/skbuff.h>
  42#include <linux/spi/spi.h>
  43#include <linux/types.h>
  44
  45#include "qca_7k.h"
  46#include "qca_7k_common.h"
  47#include "qca_debug.h"
  48#include "qca_spi.h"
  49
  50#define MAX_DMA_BURST_LEN 5000
  51
  52/*   Modules parameters     */
  53#define QCASPI_CLK_SPEED_MIN 1000000
  54#define QCASPI_CLK_SPEED_MAX 16000000
  55#define QCASPI_CLK_SPEED     8000000
  56static int qcaspi_clkspeed;
  57module_param(qcaspi_clkspeed, int, 0);
  58MODULE_PARM_DESC(qcaspi_clkspeed, "SPI bus clock speed (Hz). Use 1000000-16000000.");
  59
  60#define QCASPI_BURST_LEN_MIN 1
  61#define QCASPI_BURST_LEN_MAX MAX_DMA_BURST_LEN
  62static int qcaspi_burst_len = MAX_DMA_BURST_LEN;
  63module_param(qcaspi_burst_len, int, 0);
  64MODULE_PARM_DESC(qcaspi_burst_len, "Number of data bytes per burst. Use 1-5000.");
  65
  66#define QCASPI_PLUGGABLE_MIN 0
  67#define QCASPI_PLUGGABLE_MAX 1
  68static int qcaspi_pluggable = QCASPI_PLUGGABLE_MIN;
  69module_param(qcaspi_pluggable, int, 0);
  70MODULE_PARM_DESC(qcaspi_pluggable, "Pluggable SPI connection (yes/no).");
  71
  72#define QCASPI_TX_TIMEOUT (1 * HZ)
  73#define QCASPI_QCA7K_REBOOT_TIME_MS 1000
  74
  75static void
  76start_spi_intr_handling(struct qcaspi *qca, u16 *intr_cause)
  77{
  78        *intr_cause = 0;
  79
  80        qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
  81        qcaspi_read_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
  82        netdev_dbg(qca->net_dev, "interrupts: 0x%04x\n", *intr_cause);
  83}
  84
  85static void
  86end_spi_intr_handling(struct qcaspi *qca, u16 intr_cause)
  87{
  88        u16 intr_enable = (SPI_INT_CPU_ON |
  89                           SPI_INT_PKT_AVLBL |
  90                           SPI_INT_RDBUF_ERR |
  91                           SPI_INT_WRBUF_ERR);
  92
  93        qcaspi_write_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
  94        qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, intr_enable);
  95        netdev_dbg(qca->net_dev, "acking int: 0x%04x\n", intr_cause);
  96}
  97
  98static u32
  99qcaspi_write_burst(struct qcaspi *qca, u8 *src, u32 len)
 100{
 101        __be16 cmd;
 102        struct spi_message *msg = &qca->spi_msg2;
 103        struct spi_transfer *transfer = &qca->spi_xfer2[0];
 104        int ret;
 105
 106        cmd = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
 107        transfer->tx_buf = &cmd;
 108        transfer->rx_buf = NULL;
 109        transfer->len = QCASPI_CMD_LEN;
 110        transfer = &qca->spi_xfer2[1];
 111        transfer->tx_buf = src;
 112        transfer->rx_buf = NULL;
 113        transfer->len = len;
 114
 115        ret = spi_sync(qca->spi_dev, msg);
 116
 117        if (ret || (msg->actual_length != QCASPI_CMD_LEN + len)) {
 118                qcaspi_spi_error(qca);
 119                return 0;
 120        }
 121
 122        return len;
 123}
 124
 125static u32
 126qcaspi_write_legacy(struct qcaspi *qca, u8 *src, u32 len)
 127{
 128        struct spi_message *msg = &qca->spi_msg1;
 129        struct spi_transfer *transfer = &qca->spi_xfer1;
 130        int ret;
 131
 132        transfer->tx_buf = src;
 133        transfer->rx_buf = NULL;
 134        transfer->len = len;
 135
 136        ret = spi_sync(qca->spi_dev, msg);
 137
 138        if (ret || (msg->actual_length != len)) {
 139                qcaspi_spi_error(qca);
 140                return 0;
 141        }
 142
 143        return len;
 144}
 145
 146static u32
 147qcaspi_read_burst(struct qcaspi *qca, u8 *dst, u32 len)
 148{
 149        struct spi_message *msg = &qca->spi_msg2;
 150        __be16 cmd;
 151        struct spi_transfer *transfer = &qca->spi_xfer2[0];
 152        int ret;
 153
 154        cmd = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
 155        transfer->tx_buf = &cmd;
 156        transfer->rx_buf = NULL;
 157        transfer->len = QCASPI_CMD_LEN;
 158        transfer = &qca->spi_xfer2[1];
 159        transfer->tx_buf = NULL;
 160        transfer->rx_buf = dst;
 161        transfer->len = len;
 162
 163        ret = spi_sync(qca->spi_dev, msg);
 164
 165        if (ret || (msg->actual_length != QCASPI_CMD_LEN + len)) {
 166                qcaspi_spi_error(qca);
 167                return 0;
 168        }
 169
 170        return len;
 171}
 172
 173static u32
 174qcaspi_read_legacy(struct qcaspi *qca, u8 *dst, u32 len)
 175{
 176        struct spi_message *msg = &qca->spi_msg1;
 177        struct spi_transfer *transfer = &qca->spi_xfer1;
 178        int ret;
 179
 180        transfer->tx_buf = NULL;
 181        transfer->rx_buf = dst;
 182        transfer->len = len;
 183
 184        ret = spi_sync(qca->spi_dev, msg);
 185
 186        if (ret || (msg->actual_length != len)) {
 187                qcaspi_spi_error(qca);
 188                return 0;
 189        }
 190
 191        return len;
 192}
 193
 194static int
 195qcaspi_tx_cmd(struct qcaspi *qca, u16 cmd)
 196{
 197        __be16 tx_data;
 198        struct spi_message *msg = &qca->spi_msg1;
 199        struct spi_transfer *transfer = &qca->spi_xfer1;
 200        int ret;
 201
 202        tx_data = cpu_to_be16(cmd);
 203        transfer->len = sizeof(tx_data);
 204        transfer->tx_buf = &tx_data;
 205        transfer->rx_buf = NULL;
 206
 207        ret = spi_sync(qca->spi_dev, msg);
 208
 209        if (!ret)
 210                ret = msg->status;
 211
 212        if (ret)
 213                qcaspi_spi_error(qca);
 214
 215        return ret;
 216}
 217
 218static int
 219qcaspi_tx_frame(struct qcaspi *qca, struct sk_buff *skb)
 220{
 221        u32 count;
 222        u32 written;
 223        u32 offset;
 224        u32 len;
 225
 226        len = skb->len;
 227
 228        qcaspi_write_register(qca, SPI_REG_BFR_SIZE, len);
 229        if (qca->legacy_mode)
 230                qcaspi_tx_cmd(qca, QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
 231
 232        offset = 0;
 233        while (len) {
 234                count = len;
 235                if (count > qca->burst_len)
 236                        count = qca->burst_len;
 237
 238                if (qca->legacy_mode) {
 239                        written = qcaspi_write_legacy(qca,
 240                                                      skb->data + offset,
 241                                                      count);
 242                } else {
 243                        written = qcaspi_write_burst(qca,
 244                                                     skb->data + offset,
 245                                                     count);
 246                }
 247
 248                if (written != count)
 249                        return -1;
 250
 251                offset += count;
 252                len -= count;
 253        }
 254
 255        return 0;
 256}
 257
 258static int
 259qcaspi_transmit(struct qcaspi *qca)
 260{
 261        struct net_device_stats *n_stats = &qca->net_dev->stats;
 262        u16 available = 0;
 263        u32 pkt_len;
 264        u16 new_head;
 265        u16 packets = 0;
 266
 267        if (qca->txr.skb[qca->txr.head] == NULL)
 268                return 0;
 269
 270        qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA, &available);
 271
 272        while (qca->txr.skb[qca->txr.head]) {
 273                pkt_len = qca->txr.skb[qca->txr.head]->len + QCASPI_HW_PKT_LEN;
 274
 275                if (available < pkt_len) {
 276                        if (packets == 0)
 277                                qca->stats.write_buf_miss++;
 278                        break;
 279                }
 280
 281                if (qcaspi_tx_frame(qca, qca->txr.skb[qca->txr.head]) == -1) {
 282                        qca->stats.write_err++;
 283                        return -1;
 284                }
 285
 286                packets++;
 287                n_stats->tx_packets++;
 288                n_stats->tx_bytes += qca->txr.skb[qca->txr.head]->len;
 289                available -= pkt_len;
 290
 291                /* remove the skb from the queue */
 292                /* XXX After inconsistent lock states netif_tx_lock()
 293                 * has been replaced by netif_tx_lock_bh() and so on.
 294                 */
 295                netif_tx_lock_bh(qca->net_dev);
 296                dev_kfree_skb(qca->txr.skb[qca->txr.head]);
 297                qca->txr.skb[qca->txr.head] = NULL;
 298                qca->txr.size -= pkt_len;
 299                new_head = qca->txr.head + 1;
 300                if (new_head >= qca->txr.count)
 301                        new_head = 0;
 302                qca->txr.head = new_head;
 303                if (netif_queue_stopped(qca->net_dev))
 304                        netif_wake_queue(qca->net_dev);
 305                netif_tx_unlock_bh(qca->net_dev);
 306        }
 307
 308        return 0;
 309}
 310
 311static int
 312qcaspi_receive(struct qcaspi *qca)
 313{
 314        struct net_device *net_dev = qca->net_dev;
 315        struct net_device_stats *n_stats = &net_dev->stats;
 316        u16 available = 0;
 317        u32 bytes_read;
 318        u8 *cp;
 319
 320        /* Allocate rx SKB if we don't have one available. */
 321        if (!qca->rx_skb) {
 322                qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
 323                                                        net_dev->mtu +
 324                                                        VLAN_ETH_HLEN);
 325                if (!qca->rx_skb) {
 326                        netdev_dbg(net_dev, "out of RX resources\n");
 327                        qca->stats.out_of_mem++;
 328                        return -1;
 329                }
 330        }
 331
 332        /* Read the packet size. */
 333        qcaspi_read_register(qca, SPI_REG_RDBUF_BYTE_AVA, &available);
 334        netdev_dbg(net_dev, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
 335                   available);
 336
 337        if (available == 0) {
 338                netdev_dbg(net_dev, "qcaspi_receive called without any data being available!\n");
 339                return -1;
 340        }
 341
 342        qcaspi_write_register(qca, SPI_REG_BFR_SIZE, available);
 343
 344        if (qca->legacy_mode)
 345                qcaspi_tx_cmd(qca, QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
 346
 347        while (available) {
 348                u32 count = available;
 349
 350                if (count > qca->burst_len)
 351                        count = qca->burst_len;
 352
 353                if (qca->legacy_mode) {
 354                        bytes_read = qcaspi_read_legacy(qca, qca->rx_buffer,
 355                                                        count);
 356                } else {
 357                        bytes_read = qcaspi_read_burst(qca, qca->rx_buffer,
 358                                                       count);
 359                }
 360
 361                netdev_dbg(net_dev, "available: %d, byte read: %d\n",
 362                           available, bytes_read);
 363
 364                if (bytes_read) {
 365                        available -= bytes_read;
 366                } else {
 367                        qca->stats.read_err++;
 368                        return -1;
 369                }
 370
 371                cp = qca->rx_buffer;
 372
 373                while ((bytes_read--) && (qca->rx_skb)) {
 374                        s32 retcode;
 375
 376                        retcode = qcafrm_fsm_decode(&qca->frm_handle,
 377                                                    qca->rx_skb->data,
 378                                                    skb_tailroom(qca->rx_skb),
 379                                                    *cp);
 380                        cp++;
 381                        switch (retcode) {
 382                        case QCAFRM_GATHER:
 383                        case QCAFRM_NOHEAD:
 384                                break;
 385                        case QCAFRM_NOTAIL:
 386                                netdev_dbg(net_dev, "no RX tail\n");
 387                                n_stats->rx_errors++;
 388                                n_stats->rx_dropped++;
 389                                break;
 390                        case QCAFRM_INVLEN:
 391                                netdev_dbg(net_dev, "invalid RX length\n");
 392                                n_stats->rx_errors++;
 393                                n_stats->rx_dropped++;
 394                                break;
 395                        default:
 396                                qca->rx_skb->dev = qca->net_dev;
 397                                n_stats->rx_packets++;
 398                                n_stats->rx_bytes += retcode;
 399                                skb_put(qca->rx_skb, retcode);
 400                                qca->rx_skb->protocol = eth_type_trans(
 401                                        qca->rx_skb, qca->rx_skb->dev);
 402                                qca->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
 403                                netif_rx_ni(qca->rx_skb);
 404                                qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
 405                                        net_dev->mtu + VLAN_ETH_HLEN);
 406                                if (!qca->rx_skb) {
 407                                        netdev_dbg(net_dev, "out of RX resources\n");
 408                                        n_stats->rx_errors++;
 409                                        qca->stats.out_of_mem++;
 410                                        break;
 411                                }
 412                        }
 413                }
 414        }
 415
 416        return 0;
 417}
 418
 419/*   Check that tx ring stores only so much bytes
 420 *   that fit into the internal QCA buffer.
 421 */
 422
 423static int
 424qcaspi_tx_ring_has_space(struct tx_ring *txr)
 425{
 426        if (txr->skb[txr->tail])
 427                return 0;
 428
 429        return (txr->size + QCAFRM_MAX_LEN < QCASPI_HW_BUF_LEN) ? 1 : 0;
 430}
 431
 432/*   Flush the tx ring. This function is only safe to
 433 *   call from the qcaspi_spi_thread.
 434 */
 435
 436static void
 437qcaspi_flush_tx_ring(struct qcaspi *qca)
 438{
 439        int i;
 440
 441        /* XXX After inconsistent lock states netif_tx_lock()
 442         * has been replaced by netif_tx_lock_bh() and so on.
 443         */
 444        netif_tx_lock_bh(qca->net_dev);
 445        for (i = 0; i < TX_RING_MAX_LEN; i++) {
 446                if (qca->txr.skb[i]) {
 447                        dev_kfree_skb(qca->txr.skb[i]);
 448                        qca->txr.skb[i] = NULL;
 449                        qca->net_dev->stats.tx_dropped++;
 450                }
 451        }
 452        qca->txr.tail = 0;
 453        qca->txr.head = 0;
 454        qca->txr.size = 0;
 455        netif_tx_unlock_bh(qca->net_dev);
 456}
 457
 458static void
 459qcaspi_qca7k_sync(struct qcaspi *qca, int event)
 460{
 461        u16 signature = 0;
 462        u16 spi_config;
 463        u16 wrbuf_space = 0;
 464        static u16 reset_count;
 465
 466        if (event == QCASPI_EVENT_CPUON) {
 467                /* Read signature twice, if not valid
 468                 * go back to unknown state.
 469                 */
 470                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 471                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 472                if (signature != QCASPI_GOOD_SIGNATURE) {
 473                        qca->sync = QCASPI_SYNC_UNKNOWN;
 474                        netdev_dbg(qca->net_dev, "sync: got CPU on, but signature was invalid, restart\n");
 475                } else {
 476                        /* ensure that the WRBUF is empty */
 477                        qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA,
 478                                             &wrbuf_space);
 479                        if (wrbuf_space != QCASPI_HW_BUF_LEN) {
 480                                netdev_dbg(qca->net_dev, "sync: got CPU on, but wrbuf not empty. reset!\n");
 481                                qca->sync = QCASPI_SYNC_UNKNOWN;
 482                        } else {
 483                                netdev_dbg(qca->net_dev, "sync: got CPU on, now in sync\n");
 484                                qca->sync = QCASPI_SYNC_READY;
 485                                return;
 486                        }
 487                }
 488        }
 489
 490        switch (qca->sync) {
 491        case QCASPI_SYNC_READY:
 492                /* Read signature, if not valid go to unknown state. */
 493                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 494                if (signature != QCASPI_GOOD_SIGNATURE) {
 495                        qca->sync = QCASPI_SYNC_UNKNOWN;
 496                        netdev_dbg(qca->net_dev, "sync: bad signature, restart\n");
 497                        /* don't reset right away */
 498                        return;
 499                }
 500                break;
 501        case QCASPI_SYNC_UNKNOWN:
 502                /* Read signature, if not valid stay in unknown state */
 503                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 504                if (signature != QCASPI_GOOD_SIGNATURE) {
 505                        netdev_dbg(qca->net_dev, "sync: could not read signature to reset device, retry.\n");
 506                        return;
 507                }
 508
 509                /* TODO: use GPIO to reset QCA7000 in legacy mode*/
 510                netdev_dbg(qca->net_dev, "sync: resetting device.\n");
 511                qcaspi_read_register(qca, SPI_REG_SPI_CONFIG, &spi_config);
 512                spi_config |= QCASPI_SLAVE_RESET_BIT;
 513                qcaspi_write_register(qca, SPI_REG_SPI_CONFIG, spi_config);
 514
 515                qca->sync = QCASPI_SYNC_RESET;
 516                qca->stats.trig_reset++;
 517                reset_count = 0;
 518                break;
 519        case QCASPI_SYNC_RESET:
 520                reset_count++;
 521                netdev_dbg(qca->net_dev, "sync: waiting for CPU on, count %u.\n",
 522                           reset_count);
 523                if (reset_count >= QCASPI_RESET_TIMEOUT) {
 524                        /* reset did not seem to take place, try again */
 525                        qca->sync = QCASPI_SYNC_UNKNOWN;
 526                        qca->stats.reset_timeout++;
 527                        netdev_dbg(qca->net_dev, "sync: reset timeout, restarting process.\n");
 528                }
 529                break;
 530        }
 531}
 532
 533static int
 534qcaspi_spi_thread(void *data)
 535{
 536        struct qcaspi *qca = data;
 537        u16 intr_cause = 0;
 538
 539        netdev_info(qca->net_dev, "SPI thread created\n");
 540        while (!kthread_should_stop()) {
 541                set_current_state(TASK_INTERRUPTIBLE);
 542                if ((qca->intr_req == qca->intr_svc) &&
 543                    (qca->txr.skb[qca->txr.head] == NULL) &&
 544                    (qca->sync == QCASPI_SYNC_READY))
 545                        schedule();
 546
 547                set_current_state(TASK_RUNNING);
 548
 549                netdev_dbg(qca->net_dev, "have work to do. int: %d, tx_skb: %p\n",
 550                           qca->intr_req - qca->intr_svc,
 551                           qca->txr.skb[qca->txr.head]);
 552
 553                qcaspi_qca7k_sync(qca, QCASPI_EVENT_UPDATE);
 554
 555                if (qca->sync != QCASPI_SYNC_READY) {
 556                        netdev_dbg(qca->net_dev, "sync: not ready %u, turn off carrier and flush\n",
 557                                   (unsigned int)qca->sync);
 558                        netif_stop_queue(qca->net_dev);
 559                        netif_carrier_off(qca->net_dev);
 560                        qcaspi_flush_tx_ring(qca);
 561                        msleep(QCASPI_QCA7K_REBOOT_TIME_MS);
 562                }
 563
 564                if (qca->intr_svc != qca->intr_req) {
 565                        qca->intr_svc = qca->intr_req;
 566                        start_spi_intr_handling(qca, &intr_cause);
 567
 568                        if (intr_cause & SPI_INT_CPU_ON) {
 569                                qcaspi_qca7k_sync(qca, QCASPI_EVENT_CPUON);
 570
 571                                /* not synced. */
 572                                if (qca->sync != QCASPI_SYNC_READY)
 573                                        continue;
 574
 575                                qca->stats.device_reset++;
 576                                netif_wake_queue(qca->net_dev);
 577                                netif_carrier_on(qca->net_dev);
 578                        }
 579
 580                        if (intr_cause & SPI_INT_RDBUF_ERR) {
 581                                /* restart sync */
 582                                netdev_dbg(qca->net_dev, "===> rdbuf error!\n");
 583                                qca->stats.read_buf_err++;
 584                                qca->sync = QCASPI_SYNC_UNKNOWN;
 585                                continue;
 586                        }
 587
 588                        if (intr_cause & SPI_INT_WRBUF_ERR) {
 589                                /* restart sync */
 590                                netdev_dbg(qca->net_dev, "===> wrbuf error!\n");
 591                                qca->stats.write_buf_err++;
 592                                qca->sync = QCASPI_SYNC_UNKNOWN;
 593                                continue;
 594                        }
 595
 596                        /* can only handle other interrupts
 597                         * if sync has occurred
 598                         */
 599                        if (qca->sync == QCASPI_SYNC_READY) {
 600                                if (intr_cause & SPI_INT_PKT_AVLBL)
 601                                        qcaspi_receive(qca);
 602                        }
 603
 604                        end_spi_intr_handling(qca, intr_cause);
 605                }
 606
 607                if (qca->sync == QCASPI_SYNC_READY)
 608                        qcaspi_transmit(qca);
 609        }
 610        set_current_state(TASK_RUNNING);
 611        netdev_info(qca->net_dev, "SPI thread exit\n");
 612
 613        return 0;
 614}
 615
 616static irqreturn_t
 617qcaspi_intr_handler(int irq, void *data)
 618{
 619        struct qcaspi *qca = data;
 620
 621        qca->intr_req++;
 622        if (qca->spi_thread &&
 623            qca->spi_thread->state != TASK_RUNNING)
 624                wake_up_process(qca->spi_thread);
 625
 626        return IRQ_HANDLED;
 627}
 628
 629static int
 630qcaspi_netdev_open(struct net_device *dev)
 631{
 632        struct qcaspi *qca = netdev_priv(dev);
 633        int ret = 0;
 634
 635        if (!qca)
 636                return -EINVAL;
 637
 638        qca->intr_req = 1;
 639        qca->intr_svc = 0;
 640        qca->sync = QCASPI_SYNC_UNKNOWN;
 641        qcafrm_fsm_init_spi(&qca->frm_handle);
 642
 643        qca->spi_thread = kthread_run((void *)qcaspi_spi_thread,
 644                                      qca, "%s", dev->name);
 645
 646        if (IS_ERR(qca->spi_thread)) {
 647                netdev_err(dev, "%s: unable to start kernel thread.\n",
 648                           QCASPI_DRV_NAME);
 649                return PTR_ERR(qca->spi_thread);
 650        }
 651
 652        ret = request_irq(qca->spi_dev->irq, qcaspi_intr_handler, 0,
 653                          dev->name, qca);
 654        if (ret) {
 655                netdev_err(dev, "%s: unable to get IRQ %d (irqval=%d).\n",
 656                           QCASPI_DRV_NAME, qca->spi_dev->irq, ret);
 657                kthread_stop(qca->spi_thread);
 658                return ret;
 659        }
 660
 661        netif_start_queue(qca->net_dev);
 662
 663        return 0;
 664}
 665
 666static int
 667qcaspi_netdev_close(struct net_device *dev)
 668{
 669        struct qcaspi *qca = netdev_priv(dev);
 670
 671        netif_stop_queue(dev);
 672
 673        qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
 674        free_irq(qca->spi_dev->irq, qca);
 675
 676        kthread_stop(qca->spi_thread);
 677        qca->spi_thread = NULL;
 678        qcaspi_flush_tx_ring(qca);
 679
 680        return 0;
 681}
 682
 683static netdev_tx_t
 684qcaspi_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
 685{
 686        u32 frame_len;
 687        u8 *ptmp;
 688        struct qcaspi *qca = netdev_priv(dev);
 689        u16 new_tail;
 690        struct sk_buff *tskb;
 691        u8 pad_len = 0;
 692
 693        if (skb->len < QCAFRM_MIN_LEN)
 694                pad_len = QCAFRM_MIN_LEN - skb->len;
 695
 696        if (qca->txr.skb[qca->txr.tail]) {
 697                netdev_warn(qca->net_dev, "queue was unexpectedly full!\n");
 698                netif_stop_queue(qca->net_dev);
 699                qca->stats.ring_full++;
 700                return NETDEV_TX_BUSY;
 701        }
 702
 703        if ((skb_headroom(skb) < QCAFRM_HEADER_LEN) ||
 704            (skb_tailroom(skb) < QCAFRM_FOOTER_LEN + pad_len)) {
 705                tskb = skb_copy_expand(skb, QCAFRM_HEADER_LEN,
 706                                       QCAFRM_FOOTER_LEN + pad_len, GFP_ATOMIC);
 707                if (!tskb) {
 708                        qca->stats.out_of_mem++;
 709                        return NETDEV_TX_BUSY;
 710                }
 711                dev_kfree_skb(skb);
 712                skb = tskb;
 713        }
 714
 715        frame_len = skb->len + pad_len;
 716
 717        ptmp = skb_push(skb, QCAFRM_HEADER_LEN);
 718        qcafrm_create_header(ptmp, frame_len);
 719
 720        if (pad_len) {
 721                ptmp = skb_put_zero(skb, pad_len);
 722        }
 723
 724        ptmp = skb_put(skb, QCAFRM_FOOTER_LEN);
 725        qcafrm_create_footer(ptmp);
 726
 727        netdev_dbg(qca->net_dev, "Tx-ing packet: Size: 0x%08x\n",
 728                   skb->len);
 729
 730        qca->txr.size += skb->len + QCASPI_HW_PKT_LEN;
 731
 732        new_tail = qca->txr.tail + 1;
 733        if (new_tail >= qca->txr.count)
 734                new_tail = 0;
 735
 736        qca->txr.skb[qca->txr.tail] = skb;
 737        qca->txr.tail = new_tail;
 738
 739        if (!qcaspi_tx_ring_has_space(&qca->txr)) {
 740                netif_stop_queue(qca->net_dev);
 741                qca->stats.ring_full++;
 742        }
 743
 744        netif_trans_update(dev);
 745
 746        if (qca->spi_thread &&
 747            qca->spi_thread->state != TASK_RUNNING)
 748                wake_up_process(qca->spi_thread);
 749
 750        return NETDEV_TX_OK;
 751}
 752
 753static void
 754qcaspi_netdev_tx_timeout(struct net_device *dev)
 755{
 756        struct qcaspi *qca = netdev_priv(dev);
 757
 758        netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
 759                    jiffies, jiffies - dev_trans_start(dev));
 760        qca->net_dev->stats.tx_errors++;
 761        /* Trigger tx queue flush and QCA7000 reset */
 762        qca->sync = QCASPI_SYNC_UNKNOWN;
 763}
 764
 765static int
 766qcaspi_netdev_init(struct net_device *dev)
 767{
 768        struct qcaspi *qca = netdev_priv(dev);
 769
 770        dev->mtu = QCAFRM_MAX_MTU;
 771        dev->type = ARPHRD_ETHER;
 772        qca->clkspeed = qcaspi_clkspeed;
 773        qca->burst_len = qcaspi_burst_len;
 774        qca->spi_thread = NULL;
 775        qca->buffer_size = (dev->mtu + VLAN_ETH_HLEN + QCAFRM_HEADER_LEN +
 776                QCAFRM_FOOTER_LEN + 4) * 4;
 777
 778        memset(&qca->stats, 0, sizeof(struct qcaspi_stats));
 779
 780        qca->rx_buffer = kmalloc(qca->buffer_size, GFP_KERNEL);
 781        if (!qca->rx_buffer)
 782                return -ENOBUFS;
 783
 784        qca->rx_skb = netdev_alloc_skb_ip_align(dev, qca->net_dev->mtu +
 785                                                VLAN_ETH_HLEN);
 786        if (!qca->rx_skb) {
 787                kfree(qca->rx_buffer);
 788                netdev_info(qca->net_dev, "Failed to allocate RX sk_buff.\n");
 789                return -ENOBUFS;
 790        }
 791
 792        return 0;
 793}
 794
 795static void
 796qcaspi_netdev_uninit(struct net_device *dev)
 797{
 798        struct qcaspi *qca = netdev_priv(dev);
 799
 800        kfree(qca->rx_buffer);
 801        qca->buffer_size = 0;
 802        if (qca->rx_skb)
 803                dev_kfree_skb(qca->rx_skb);
 804}
 805
 806static const struct net_device_ops qcaspi_netdev_ops = {
 807        .ndo_init = qcaspi_netdev_init,
 808        .ndo_uninit = qcaspi_netdev_uninit,
 809        .ndo_open = qcaspi_netdev_open,
 810        .ndo_stop = qcaspi_netdev_close,
 811        .ndo_start_xmit = qcaspi_netdev_xmit,
 812        .ndo_set_mac_address = eth_mac_addr,
 813        .ndo_tx_timeout = qcaspi_netdev_tx_timeout,
 814        .ndo_validate_addr = eth_validate_addr,
 815};
 816
 817static void
 818qcaspi_netdev_setup(struct net_device *dev)
 819{
 820        struct qcaspi *qca = NULL;
 821
 822        dev->netdev_ops = &qcaspi_netdev_ops;
 823        qcaspi_set_ethtool_ops(dev);
 824        dev->watchdog_timeo = QCASPI_TX_TIMEOUT;
 825        dev->priv_flags &= ~IFF_TX_SKB_SHARING;
 826        dev->tx_queue_len = 100;
 827
 828        /* MTU range: 46 - 1500 */
 829        dev->min_mtu = QCAFRM_MIN_MTU;
 830        dev->max_mtu = QCAFRM_MAX_MTU;
 831
 832        qca = netdev_priv(dev);
 833        memset(qca, 0, sizeof(struct qcaspi));
 834
 835        memset(&qca->spi_xfer1, 0, sizeof(struct spi_transfer));
 836        memset(&qca->spi_xfer2, 0, sizeof(struct spi_transfer) * 2);
 837
 838        spi_message_init(&qca->spi_msg1);
 839        spi_message_add_tail(&qca->spi_xfer1, &qca->spi_msg1);
 840
 841        spi_message_init(&qca->spi_msg2);
 842        spi_message_add_tail(&qca->spi_xfer2[0], &qca->spi_msg2);
 843        spi_message_add_tail(&qca->spi_xfer2[1], &qca->spi_msg2);
 844
 845        memset(&qca->txr, 0, sizeof(qca->txr));
 846        qca->txr.count = TX_RING_MAX_LEN;
 847}
 848
 849static const struct of_device_id qca_spi_of_match[] = {
 850        { .compatible = "qca,qca7000" },
 851        { /* sentinel */ }
 852};
 853MODULE_DEVICE_TABLE(of, qca_spi_of_match);
 854
 855static int
 856qca_spi_probe(struct spi_device *spi)
 857{
 858        struct qcaspi *qca = NULL;
 859        struct net_device *qcaspi_devs = NULL;
 860        u8 legacy_mode = 0;
 861        u16 signature;
 862        const char *mac;
 863
 864        if (!spi->dev.of_node) {
 865                dev_err(&spi->dev, "Missing device tree\n");
 866                return -EINVAL;
 867        }
 868
 869        legacy_mode = of_property_read_bool(spi->dev.of_node,
 870                                            "qca,legacy-mode");
 871
 872        if (qcaspi_clkspeed == 0) {
 873                if (spi->max_speed_hz)
 874                        qcaspi_clkspeed = spi->max_speed_hz;
 875                else
 876                        qcaspi_clkspeed = QCASPI_CLK_SPEED;
 877        }
 878
 879        if ((qcaspi_clkspeed < QCASPI_CLK_SPEED_MIN) ||
 880            (qcaspi_clkspeed > QCASPI_CLK_SPEED_MAX)) {
 881                dev_info(&spi->dev, "Invalid clkspeed: %d\n",
 882                         qcaspi_clkspeed);
 883                return -EINVAL;
 884        }
 885
 886        if ((qcaspi_burst_len < QCASPI_BURST_LEN_MIN) ||
 887            (qcaspi_burst_len > QCASPI_BURST_LEN_MAX)) {
 888                dev_info(&spi->dev, "Invalid burst len: %d\n",
 889                         qcaspi_burst_len);
 890                return -EINVAL;
 891        }
 892
 893        if ((qcaspi_pluggable < QCASPI_PLUGGABLE_MIN) ||
 894            (qcaspi_pluggable > QCASPI_PLUGGABLE_MAX)) {
 895                dev_info(&spi->dev, "Invalid pluggable: %d\n",
 896                         qcaspi_pluggable);
 897                return -EINVAL;
 898        }
 899
 900        dev_info(&spi->dev, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
 901                 QCASPI_DRV_VERSION,
 902                 qcaspi_clkspeed,
 903                 qcaspi_burst_len,
 904                 qcaspi_pluggable);
 905
 906        spi->mode = SPI_MODE_3;
 907        spi->max_speed_hz = qcaspi_clkspeed;
 908        if (spi_setup(spi) < 0) {
 909                dev_err(&spi->dev, "Unable to setup SPI device\n");
 910                return -EFAULT;
 911        }
 912
 913        qcaspi_devs = alloc_etherdev(sizeof(struct qcaspi));
 914        if (!qcaspi_devs)
 915                return -ENOMEM;
 916
 917        qcaspi_netdev_setup(qcaspi_devs);
 918        SET_NETDEV_DEV(qcaspi_devs, &spi->dev);
 919
 920        qca = netdev_priv(qcaspi_devs);
 921        if (!qca) {
 922                free_netdev(qcaspi_devs);
 923                dev_err(&spi->dev, "Fail to retrieve private structure\n");
 924                return -ENOMEM;
 925        }
 926        qca->net_dev = qcaspi_devs;
 927        qca->spi_dev = spi;
 928        qca->legacy_mode = legacy_mode;
 929
 930        spi_set_drvdata(spi, qcaspi_devs);
 931
 932        mac = of_get_mac_address(spi->dev.of_node);
 933
 934        if (mac)
 935                ether_addr_copy(qca->net_dev->dev_addr, mac);
 936
 937        if (!is_valid_ether_addr(qca->net_dev->dev_addr)) {
 938                eth_hw_addr_random(qca->net_dev);
 939                dev_info(&spi->dev, "Using random MAC address: %pM\n",
 940                         qca->net_dev->dev_addr);
 941        }
 942
 943        netif_carrier_off(qca->net_dev);
 944
 945        if (!qcaspi_pluggable) {
 946                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 947                qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
 948
 949                if (signature != QCASPI_GOOD_SIGNATURE) {
 950                        dev_err(&spi->dev, "Invalid signature (0x%04X)\n",
 951                                signature);
 952                        free_netdev(qcaspi_devs);
 953                        return -EFAULT;
 954                }
 955        }
 956
 957        if (register_netdev(qcaspi_devs)) {
 958                dev_info(&spi->dev, "Unable to register net device %s\n",
 959                         qcaspi_devs->name);
 960                free_netdev(qcaspi_devs);
 961                return -EFAULT;
 962        }
 963
 964        qcaspi_init_device_debugfs(qca);
 965
 966        return 0;
 967}
 968
 969static int
 970qca_spi_remove(struct spi_device *spi)
 971{
 972        struct net_device *qcaspi_devs = spi_get_drvdata(spi);
 973        struct qcaspi *qca = netdev_priv(qcaspi_devs);
 974
 975        qcaspi_remove_device_debugfs(qca);
 976
 977        unregister_netdev(qcaspi_devs);
 978        free_netdev(qcaspi_devs);
 979
 980        return 0;
 981}
 982
 983static const struct spi_device_id qca_spi_id[] = {
 984        { "qca7000", 0 },
 985        { /* sentinel */ }
 986};
 987MODULE_DEVICE_TABLE(spi, qca_spi_id);
 988
 989static struct spi_driver qca_spi_driver = {
 990        .driver = {
 991                .name   = QCASPI_DRV_NAME,
 992                .of_match_table = qca_spi_of_match,
 993        },
 994        .id_table = qca_spi_id,
 995        .probe    = qca_spi_probe,
 996        .remove   = qca_spi_remove,
 997};
 998module_spi_driver(qca_spi_driver);
 999
1000MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 SPI Driver");
1001MODULE_AUTHOR("Qualcomm Atheros Communications");
1002MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
1003MODULE_LICENSE("Dual BSD/GPL");
1004MODULE_VERSION(QCASPI_DRV_VERSION);
1005