Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6-microblaze.git] / drivers / net / wireless / wl12xx / wl1271_spi.c
index 053c84a..256e84a 100644 (file)
  *
  */
 
+#include <linux/irq.h>
 #include <linux/module.h>
-#include <linux/platform_device.h>
 #include <linux/crc7.h>
 #include <linux/spi/spi.h>
+#include <linux/spi/wl12xx.h>
 #include <linux/slab.h>
 
 #include "wl1271.h"
 #include "wl12xx_80211.h"
-#include "wl1271_spi.h"
+#include "wl1271_io.h"
+
+#include "wl1271_reg.h"
+
+#define WSPI_CMD_READ                 0x40000000
+#define WSPI_CMD_WRITE                0x00000000
+#define WSPI_CMD_FIXED                0x20000000
+#define WSPI_CMD_BYTE_LENGTH          0x1FFE0000
+#define WSPI_CMD_BYTE_LENGTH_OFFSET   17
+#define WSPI_CMD_BYTE_ADDR            0x0001FFFF
+
+#define WSPI_INIT_CMD_CRC_LEN       5
+
+#define WSPI_INIT_CMD_START         0x00
+#define WSPI_INIT_CMD_TX            0x40
+/* the extra bypass bit is sampled by the TNET as '1' */
+#define WSPI_INIT_CMD_BYPASS_BIT    0x80
+#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
+#define WSPI_INIT_CMD_EN_FIXEDBUSY  0x80
+#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
+#define WSPI_INIT_CMD_IOD           0x40
+#define WSPI_INIT_CMD_IP            0x20
+#define WSPI_INIT_CMD_CS            0x10
+#define WSPI_INIT_CMD_WS            0x08
+#define WSPI_INIT_CMD_WSPI          0x01
+#define WSPI_INIT_CMD_END           0x01
+
+#define WSPI_INIT_CMD_LEN           8
+
+#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
+               ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
+#define HW_ACCESS_WSPI_INIT_CMD_MASK  0
+
+static inline struct spi_device *wl_to_spi(struct wl1271 *wl)
+{
+       return wl->if_priv;
+}
+
+static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
+{
+       return &(wl_to_spi(wl)->dev);
+}
 
+static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
+{
+       disable_irq(wl->irq);
+}
+
+static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
+{
+       enable_irq(wl->irq);
+}
 
-void wl1271_spi_reset(struct wl1271 *wl)
+static void wl1271_spi_reset(struct wl1271 *wl)
 {
        u8 *cmd;
        struct spi_transfer t;
@@ -53,12 +104,12 @@ void wl1271_spi_reset(struct wl1271 *wl)
        t.len = WSPI_INIT_CMD_LEN;
        spi_message_add_tail(&t, &m);
 
-       spi_sync(wl->spi, &m);
+       spi_sync(wl_to_spi(wl), &m);
 
        wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
 }
 
-void wl1271_spi_init(struct wl1271 *wl)
+static void wl1271_spi_init(struct wl1271 *wl)
 {
        u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
        struct spi_transfer t;
@@ -107,7 +158,7 @@ void wl1271_spi_init(struct wl1271 *wl)
        t.len = WSPI_INIT_CMD_LEN;
        spi_message_add_tail(&t, &m);
 
-       spi_sync(wl->spi, &m);
+       spi_sync(wl_to_spi(wl), &m);
 
        wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
 }
@@ -139,7 +190,7 @@ static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
                        t[0].rx_buf = buf + (len - num_busy_bytes);
                        t[0].len = num_busy_bytes;
                        spi_message_add_tail(&t[0], &m);
-                       spi_sync(wl->spi, &m);
+                       spi_sync(wl_to_spi(wl), &m);
                        return;
                }
        }
@@ -159,7 +210,7 @@ static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
                t[0].rx_buf = busy_buf;
                t[0].len = sizeof(u32);
                spi_message_add_tail(&t[0], &m);
-               spi_sync(wl->spi, &m);
+               spi_sync(wl_to_spi(wl), &m);
 
                if (*busy_buf & 0x1) {
                        spi_message_init(&m);
@@ -167,7 +218,7 @@ static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
                        t[0].rx_buf = buf;
                        t[0].len = len;
                        spi_message_add_tail(&t[0], &m);
-                       spi_sync(wl->spi, &m);
+                       spi_sync(wl_to_spi(wl), &m);
                        return;
                }
        }
@@ -178,7 +229,7 @@ static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
 }
 #endif
 
-void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
+static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
                         size_t len, bool fixed)
 {
        struct spi_transfer t[3];
@@ -213,7 +264,7 @@ void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
        t[2].len = len;
        spi_message_add_tail(&t[2], &m);
 
-       spi_sync(wl->spi, &m);
+       spi_sync(wl_to_spi(wl), &m);
 
        /* FIXME: Check busy words, removed due to SPI bug */
        /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
@@ -223,7 +274,7 @@ void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
        wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
 }
 
-void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
+static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
                          size_t len, bool fixed)
 {
        struct spi_transfer t[2];
@@ -251,8 +302,181 @@ void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
        t[1].len = len;
        spi_message_add_tail(&t[1], &m);
 
-       spi_sync(wl->spi, &m);
+       spi_sync(wl_to_spi(wl), &m);
 
        wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
        wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
 }
+
+static irqreturn_t wl1271_irq(int irq, void *cookie)
+{
+       struct wl1271 *wl;
+       unsigned long flags;
+
+       wl1271_debug(DEBUG_IRQ, "IRQ");
+
+       wl = cookie;
+
+       /* complete the ELP completion */
+       spin_lock_irqsave(&wl->wl_lock, flags);
+       if (wl->elp_compl) {
+               complete(wl->elp_compl);
+               wl->elp_compl = NULL;
+       }
+
+       if (!test_and_set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
+               ieee80211_queue_work(wl->hw, &wl->irq_work);
+       set_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
+       spin_unlock_irqrestore(&wl->wl_lock, flags);
+
+       return IRQ_HANDLED;
+}
+
+static void wl1271_spi_set_power(struct wl1271 *wl, bool enable)
+{
+       if (wl->set_power)
+               wl->set_power(enable);
+}
+
+static struct wl1271_if_operations spi_ops = {
+       .read           = wl1271_spi_raw_read,
+       .write          = wl1271_spi_raw_write,
+       .reset          = wl1271_spi_reset,
+       .init           = wl1271_spi_init,
+       .power          = wl1271_spi_set_power,
+       .dev            = wl1271_spi_wl_to_dev,
+       .enable_irq     = wl1271_spi_enable_interrupts,
+       .disable_irq    = wl1271_spi_disable_interrupts
+};
+
+static int __devinit wl1271_probe(struct spi_device *spi)
+{
+       struct wl12xx_platform_data *pdata;
+       struct ieee80211_hw *hw;
+       struct wl1271 *wl;
+       int ret;
+
+       pdata = spi->dev.platform_data;
+       if (!pdata) {
+               wl1271_error("no platform data");
+               return -ENODEV;
+       }
+
+       hw = wl1271_alloc_hw();
+       if (IS_ERR(hw))
+               return PTR_ERR(hw);
+
+       wl = hw->priv;
+
+       dev_set_drvdata(&spi->dev, wl);
+       wl->if_priv = spi;
+
+       wl->if_ops = &spi_ops;
+
+       /* This is the only SPI value that we need to set here, the rest
+        * comes from the board-peripherals file */
+       spi->bits_per_word = 32;
+
+       ret = spi_setup(spi);
+       if (ret < 0) {
+               wl1271_error("spi_setup failed");
+               goto out_free;
+       }
+
+       wl->set_power = pdata->set_power;
+       if (!wl->set_power) {
+               wl1271_error("set power function missing in platform data");
+               ret = -ENODEV;
+               goto out_free;
+       }
+
+       wl->irq = spi->irq;
+       if (wl->irq < 0) {
+               wl1271_error("irq missing in platform data");
+               ret = -ENODEV;
+               goto out_free;
+       }
+
+       ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
+       if (ret < 0) {
+               wl1271_error("request_irq() failed: %d", ret);
+               goto out_free;
+       }
+
+       set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
+
+       disable_irq(wl->irq);
+
+       ret = wl1271_init_ieee80211(wl);
+       if (ret)
+               goto out_irq;
+
+       ret = wl1271_register_hw(wl);
+       if (ret)
+               goto out_irq;
+
+       wl1271_notice("initialized");
+
+       return 0;
+
+ out_irq:
+       free_irq(wl->irq, wl);
+
+ out_free:
+       wl1271_free_hw(wl);
+
+       return ret;
+}
+
+static int __devexit wl1271_remove(struct spi_device *spi)
+{
+       struct wl1271 *wl = dev_get_drvdata(&spi->dev);
+
+       free_irq(wl->irq, wl);
+
+       wl1271_unregister_hw(wl);
+       wl1271_free_hw(wl);
+
+       return 0;
+}
+
+
+static struct spi_driver wl1271_spi_driver = {
+       .driver = {
+               .name           = "wl1271",
+               .bus            = &spi_bus_type,
+               .owner          = THIS_MODULE,
+       },
+
+       .probe          = wl1271_probe,
+       .remove         = __devexit_p(wl1271_remove),
+};
+
+static int __init wl1271_init(void)
+{
+       int ret;
+
+       ret = spi_register_driver(&wl1271_spi_driver);
+       if (ret < 0) {
+               wl1271_error("failed to register spi driver: %d", ret);
+               goto out;
+       }
+
+out:
+       return ret;
+}
+
+static void __exit wl1271_exit(void)
+{
+       spi_unregister_driver(&wl1271_spi_driver);
+
+       wl1271_notice("unloaded");
+}
+
+module_init(wl1271_init);
+module_exit(wl1271_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
+MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
+MODULE_FIRMWARE(WL1271_FW_NAME);