netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / drivers / spi / pxa2xx_spi.c
index d47d363..c2f707e 100644 (file)
 #include <linux/workqueue.h>
 #include <linux/delay.h>
 #include <linux/clk.h>
+#include <linux/gpio.h>
 
 #include <asm/io.h>
 #include <asm/irq.h>
 #include <asm/delay.h>
-#include <asm/dma.h>
 
-#include <mach/hardware.h>
-#include <mach/pxa-regs.h>
+#include <mach/dma.h>
 #include <mach/regs-ssp.h>
 #include <mach/ssp.h>
 #include <mach/pxa2xx_spi.h>
@@ -47,10 +46,15 @@ MODULE_ALIAS("platform:pxa2xx-spi");
 
 #define MAX_BUSES 3
 
+#define RX_THRESH_DFLT         8
+#define TX_THRESH_DFLT         8
+#define TIMOUT_DFLT            1000
+
 #define DMA_INT_MASK           (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
 #define RESET_DMA_CHANNEL      (DCSR_NODESC | DMA_INT_MASK)
 #define IS_DMA_ALIGNED(x)      ((((u32)(x)) & 0x07) == 0)
 #define MAX_DMA_LEN            8191
+#define DMA_ALIGNMENT          8
 
 /*
  * for testing SSCR1 changes that require SSP restart, basically
@@ -164,6 +168,8 @@ struct chip_data {
        u8 enable_dma;
        u8 bits_per_word;
        u32 speed_hz;
+       int gpio_cs;
+       int gpio_cs_inverted;
        int (*write)(struct driver_data *drv_data);
        int (*read)(struct driver_data *drv_data);
        void (*cs_control)(u32 command);
@@ -171,6 +177,32 @@ struct chip_data {
 
 static void pump_messages(struct work_struct *work);
 
+static void cs_assert(struct driver_data *drv_data)
+{
+       struct chip_data *chip = drv_data->cur_chip;
+
+       if (chip->cs_control) {
+               chip->cs_control(PXA2XX_CS_ASSERT);
+               return;
+       }
+
+       if (gpio_is_valid(chip->gpio_cs))
+               gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
+}
+
+static void cs_deassert(struct driver_data *drv_data)
+{
+       struct chip_data *chip = drv_data->cur_chip;
+
+       if (chip->cs_control) {
+               chip->cs_control(PXA2XX_CS_DEASSERT);
+               return;
+       }
+
+       if (gpio_is_valid(chip->gpio_cs))
+               gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
+}
+
 static int flush(struct driver_data *drv_data)
 {
        unsigned long limit = loops_per_jiffy << 1;
@@ -181,16 +213,12 @@ static int flush(struct driver_data *drv_data)
                while (read_SSSR(reg) & SSSR_RNE) {
                        read_SSDR(reg);
                }
-       } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
+       } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
        write_SSSR(SSSR_ROR, reg);
 
        return limit;
 }
 
-static void null_cs_control(u32 command)
-{
-}
-
 static int null_writer(struct driver_data *drv_data)
 {
        void __iomem *reg = drv_data->ioaddr;
@@ -348,21 +376,21 @@ static int map_dma_buffers(struct driver_data *drv_data)
        } else
                drv_data->tx_map_len = drv_data->len;
 
-       /* Stream map the rx buffer */
-       drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
-                                               drv_data->rx_map_len,
-                                               DMA_FROM_DEVICE);
-       if (dma_mapping_error(dev, drv_data->rx_dma))
-               return 0;
-
-       /* Stream map the tx buffer */
+       /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
+        * so we flush the cache *before* invalidating it, in case
+        * the tx and rx buffers overlap.
+        */
        drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
-                                               drv_data->tx_map_len,
-                                               DMA_TO_DEVICE);
+                                       drv_data->tx_map_len, DMA_TO_DEVICE);
+       if (dma_mapping_error(dev, drv_data->tx_dma))
+               return 0;
 
-       if (dma_mapping_error(dev, drv_data->tx_dma)) {
-               dma_unmap_single(dev, drv_data->rx_dma,
+       /* Stream map the rx buffer */
+       drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
                                        drv_data->rx_map_len, DMA_FROM_DEVICE);
+       if (dma_mapping_error(dev, drv_data->rx_dma)) {
+               dma_unmap_single(dev, drv_data->tx_dma,
+                                       drv_data->tx_map_len, DMA_TO_DEVICE);
                return 0;
        }
 
@@ -398,7 +426,6 @@ static void giveback(struct driver_data *drv_data)
        msg = drv_data->cur_msg;
        drv_data->cur_msg = NULL;
        drv_data->cur_transfer = NULL;
-       drv_data->cur_chip = NULL;
        queue_work(drv_data->workqueue, &drv_data->pump_messages);
        spin_unlock_irqrestore(&drv_data->lock, flags);
 
@@ -414,7 +441,7 @@ static void giveback(struct driver_data *drv_data)
         * a message with an error, or next message is for another chip
         */
        if (!last_transfer->cs_change)
-               drv_data->cs_control(PXA2XX_CS_DEASSERT);
+               cs_deassert(drv_data);
        else {
                struct spi_message *next_msg;
 
@@ -443,19 +470,21 @@ static void giveback(struct driver_data *drv_data)
                if (next_msg && next_msg->spi != msg->spi)
                        next_msg = NULL;
                if (!next_msg || msg->state == ERROR_STATE)
-                       drv_data->cs_control(PXA2XX_CS_DEASSERT);
+                       cs_deassert(drv_data);
        }
 
        msg->state = NULL;
        if (msg->complete)
                msg->complete(msg->context);
+
+       drv_data->cur_chip = NULL;
 }
 
 static int wait_ssp_rx_stall(void const __iomem *ioaddr)
 {
        unsigned long limit = loops_per_jiffy << 1;
 
-       while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
+       while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit)
                cpu_relax();
 
        return limit;
@@ -465,7 +494,7 @@ static int wait_dma_channel_stop(int channel)
 {
        unsigned long limit = loops_per_jiffy << 1;
 
-       while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
+       while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
                cpu_relax();
 
        return limit;
@@ -885,7 +914,7 @@ static void pump_transfers(unsigned long data)
 
                /* Drop chip select only if cs_change is requested */
                if (previous->cs_change)
-                       drv_data->cs_control(PXA2XX_CS_DEASSERT);
+                       cs_deassert(drv_data);
        }
 
        /* Check for transfers that need multiple DMA segments */
@@ -920,7 +949,6 @@ static void pump_transfers(unsigned long data)
        }
        drv_data->n_bytes = chip->n_bytes;
        drv_data->dma_width = chip->dma_width;
-       drv_data->cs_control = chip->cs_control;
        drv_data->tx = (void *)transfer->tx_buf;
        drv_data->tx_end = drv_data->tx + transfer->len;
        drv_data->rx = transfer->rx_buf;
@@ -1082,11 +1110,7 @@ static void pump_transfers(unsigned long data)
                        write_SSTO(chip->timeout, reg);
        }
 
-       /* FIXME, need to handle cs polarity,
-        * this driver uses struct pxa2xx_spi_chip.cs_control to
-        * specify a CS handling function, and it ignores most
-        * struct spi_device.mode[s], including SPI_CS_HIGH */
-       drv_data->cs_control(PXA2XX_CS_ASSERT);
+       cs_assert(drv_data);
 
        /* after chip select, release the data by enabling service
         * requests and interrupts, without changing any mode bits */
@@ -1161,8 +1185,43 @@ static int transfer(struct spi_device *spi, struct spi_message *msg)
        return 0;
 }
 
-/* the spi->mode bits understood by this driver: */
-#define MODEBITS (SPI_CPOL | SPI_CPHA)
+static int setup_cs(struct spi_device *spi, struct chip_data *chip,
+                   struct pxa2xx_spi_chip *chip_info)
+{
+       int err = 0;
+
+       if (chip == NULL || chip_info == NULL)
+               return 0;
+
+       /* NOTE: setup() can be called multiple times, possibly with
+        * different chip_info, release previously requested GPIO
+        */
+       if (gpio_is_valid(chip->gpio_cs))
+               gpio_free(chip->gpio_cs);
+
+       /* If (*cs_control) is provided, ignore GPIO chip select */
+       if (chip_info->cs_control) {
+               chip->cs_control = chip_info->cs_control;
+               return 0;
+       }
+
+       if (gpio_is_valid(chip_info->gpio_cs)) {
+               err = gpio_request(chip_info->gpio_cs, "SPI_CS");
+               if (err) {
+                       dev_err(&spi->dev, "failed to request chip select "
+                                       "GPIO%d\n", chip_info->gpio_cs);
+                       return err;
+               }
+
+               chip->gpio_cs = chip_info->gpio_cs;
+               chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
+
+               err = gpio_direction_output(chip->gpio_cs,
+                                       !chip->gpio_cs_inverted);
+       }
+
+       return err;
+}
 
 static int setup(struct spi_device *spi)
 {
@@ -1171,9 +1230,8 @@ static int setup(struct spi_device *spi)
        struct driver_data *drv_data = spi_master_get_devdata(spi->master);
        struct ssp_device *ssp = drv_data->ssp;
        unsigned int clk_div;
-
-       if (!spi->bits_per_word)
-               spi->bits_per_word = 8;
+       uint tx_thres = TX_THRESH_DFLT;
+       uint rx_thres = RX_THRESH_DFLT;
 
        if (drv_data->ssp_type != PXA25x_SSP
                && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
@@ -1191,12 +1249,6 @@ static int setup(struct spi_device *spi)
                return -EINVAL;
        }
 
-       if (spi->mode & ~MODEBITS) {
-               dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
-                       spi->mode & ~MODEBITS);
-               return -EINVAL;
-       }
-
        /* Only alloc on first setup */
        chip = spi_get_ctldata(spi);
        if (!chip) {
@@ -1207,10 +1259,9 @@ static int setup(struct spi_device *spi)
                        return -ENOMEM;
                }
 
-               chip->cs_control = null_cs_control;
+               chip->gpio_cs = -1;
                chip->enable_dma = 0;
-               chip->timeout = 1000;
-               chip->threshold = SSCR1_RxTresh(1) | SSCR1_TxTresh(1);
+               chip->timeout = TIMOUT_DFLT;
                chip->dma_burst_size = drv_data->master_info->enable_dma ?
                                        DCMD_BURST8 : 0;
        }
@@ -1222,24 +1273,21 @@ static int setup(struct spi_device *spi)
        /* chip_info isn't always needed */
        chip->cr1 = 0;
        if (chip_info) {
-               if (chip_info->cs_control)
-                       chip->cs_control = chip_info->cs_control;
-
-               chip->timeout = chip_info->timeout;
-
-               chip->threshold = (SSCR1_RxTresh(chip_info->rx_threshold) &
-                                                               SSCR1_RFT) |
-                               (SSCR1_TxTresh(chip_info->tx_threshold) &
-                                                               SSCR1_TFT);
-
-               chip->enable_dma = chip_info->dma_burst_size != 0
-                                       && drv_data->master_info->enable_dma;
+               if (chip_info->timeout)
+                       chip->timeout = chip_info->timeout;
+               if (chip_info->tx_threshold)
+                       tx_thres = chip_info->tx_threshold;
+               if (chip_info->rx_threshold)
+                       rx_thres = chip_info->rx_threshold;
+               chip->enable_dma = drv_data->master_info->enable_dma;
                chip->dma_threshold = 0;
-
                if (chip_info->enable_loopback)
                        chip->cr1 = SSCR1_LBM;
        }
 
+       chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
+                       (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
+
        /* set dma burst and threshold outside of chip_info path so that if
         * chip_info goes away after setting chip->enable_dma, the
         * burst and threshold can still respond to changes in bits_per_word */
@@ -1268,17 +1316,15 @@ static int setup(struct spi_device *spi)
 
        /* NOTE:  PXA25x_SSP _could_ use external clocking ... */
        if (drv_data->ssp_type != PXA25x_SSP)
-               dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
-                               spi->bits_per_word,
+               dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
                                clk_get_rate(ssp->clk)
                                        / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
-                               spi->mode & 0x3);
+                               chip->enable_dma ? "DMA" : "PIO");
        else
-               dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
-                               spi->bits_per_word,
-                               clk_get_rate(ssp->clk)
+               dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
+                               clk_get_rate(ssp->clk) / 2
                                        / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
-                               spi->mode & 0x3);
+                               chip->enable_dma ? "DMA" : "PIO");
 
        if (spi->bits_per_word <= 8) {
                chip->n_bytes = 1;
@@ -1304,13 +1350,19 @@ static int setup(struct spi_device *spi)
 
        spi_set_ctldata(spi, chip);
 
-       return 0;
+       return setup_cs(spi, chip, chip_info);
 }
 
 static void cleanup(struct spi_device *spi)
 {
        struct chip_data *chip = spi_get_ctldata(spi);
 
+       if (!chip)
+               return;
+
+       if (gpio_is_valid(chip->gpio_cs))
+               gpio_free(chip->gpio_cs);
+
        kfree(chip);
 }
 
@@ -1327,7 +1379,7 @@ static int __init init_queue(struct driver_data *drv_data)
 
        INIT_WORK(&drv_data->pump_messages, pump_messages);
        drv_data->workqueue = create_singlethread_workqueue(
-                                       drv_data->master->dev.parent->bus_id);
+                               dev_name(drv_data->master->dev.parent));
        if (drv_data->workqueue == NULL)
                return -EBUSY;
 
@@ -1407,9 +1459,9 @@ static int __init pxa2xx_spi_probe(struct platform_device *pdev)
        struct device *dev = &pdev->dev;
        struct pxa2xx_spi_master *platform_info;
        struct spi_master *master;
-       struct driver_data *drv_data = NULL;
+       struct driver_data *drv_data;
        struct ssp_device *ssp;
-       int status = 0;
+       int status;
 
        platform_info = dev->platform_data;
 
@@ -1422,7 +1474,7 @@ static int __init pxa2xx_spi_probe(struct platform_device *pdev)
        /* Allocate master with space for drv_data and null dma buffer */
        master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
        if (!master) {
-               dev_err(&pdev->dev, "can not alloc spi_master\n");
+               dev_err(&pdev->dev, "cannot alloc spi_master\n");
                ssp_free(ssp);
                return -ENOMEM;
        }
@@ -1432,8 +1484,12 @@ static int __init pxa2xx_spi_probe(struct platform_device *pdev)
        drv_data->pdev = pdev;
        drv_data->ssp = ssp;
 
+       /* the spi->mode bits understood by this driver: */
+       master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
+
        master->bus_num = pdev->id;
        master->num_chipselect = platform_info->num_chipselect;
+       master->dma_alignment = DMA_ALIGNMENT;
        master->cleanup = cleanup;
        master->setup = setup;
        master->transfer = transfer;
@@ -1456,9 +1512,9 @@ static int __init pxa2xx_spi_probe(struct platform_device *pdev)
                drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
        }
 
-       status = request_irq(ssp->irq, ssp_int, 0, dev->bus_id, drv_data);
+       status = request_irq(ssp->irq, ssp_int, 0, dev_name(dev), drv_data);
        if (status < 0) {
-               dev_err(&pdev->dev, "can not get IRQ\n");
+               dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
                goto out_error_master_alloc;
        }
 
@@ -1498,7 +1554,9 @@ static int __init pxa2xx_spi_probe(struct platform_device *pdev)
 
        /* Load default SSP configuration */
        write_SSCR0(0, drv_data->ioaddr);
-       write_SSCR1(SSCR1_RxTresh(4) | SSCR1_TxTresh(12), drv_data->ioaddr);
+       write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
+                               SSCR1_TxTresh(TX_THRESH_DFLT),
+                               drv_data->ioaddr);
        write_SSCR0(SSCR0_SerClkDiv(2)
                        | SSCR0_Motorola
                        | SSCR0_DataSize(8),
@@ -1553,11 +1611,12 @@ out_error_master_alloc:
 static int pxa2xx_spi_remove(struct platform_device *pdev)
 {
        struct driver_data *drv_data = platform_get_drvdata(pdev);
-       struct ssp_device *ssp = drv_data->ssp;
+       struct ssp_device *ssp;
        int status = 0;
 
        if (!drv_data)
                return 0;
+       ssp = drv_data->ssp;
 
        /* Remove the queue */
        status = destroy_queue(drv_data);
@@ -1609,10 +1668,9 @@ static void pxa2xx_spi_shutdown(struct platform_device *pdev)
 }
 
 #ifdef CONFIG_PM
-
-static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
+static int pxa2xx_spi_suspend(struct device *dev)
 {
-       struct driver_data *drv_data = platform_get_drvdata(pdev);
+       struct driver_data *drv_data = dev_get_drvdata(dev);
        struct ssp_device *ssp = drv_data->ssp;
        int status = 0;
 
@@ -1625,45 +1683,55 @@ static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
        return 0;
 }
 
-static int pxa2xx_spi_resume(struct platform_device *pdev)
+static int pxa2xx_spi_resume(struct device *dev)
 {
-       struct driver_data *drv_data = platform_get_drvdata(pdev);
+       struct driver_data *drv_data = dev_get_drvdata(dev);
        struct ssp_device *ssp = drv_data->ssp;
        int status = 0;
 
+       if (drv_data->rx_channel != -1)
+               DRCMR(drv_data->ssp->drcmr_rx) =
+                       DRCMR_MAPVLD | drv_data->rx_channel;
+       if (drv_data->tx_channel != -1)
+               DRCMR(drv_data->ssp->drcmr_tx) =
+                       DRCMR_MAPVLD | drv_data->tx_channel;
+
        /* Enable the SSP clock */
        clk_enable(ssp->clk);
 
        /* Start the queue running */
        status = start_queue(drv_data);
        if (status != 0) {
-               dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
+               dev_err(dev, "problem starting queue (%d)\n", status);
                return status;
        }
 
        return 0;
 }
-#else
-#define pxa2xx_spi_suspend NULL
-#define pxa2xx_spi_resume NULL
-#endif /* CONFIG_PM */
+
+static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
+       .suspend        = pxa2xx_spi_suspend,
+       .resume         = pxa2xx_spi_resume,
+};
+#endif
 
 static struct platform_driver driver = {
        .driver = {
-               .name = "pxa2xx-spi",
-               .owner = THIS_MODULE,
+               .name   = "pxa2xx-spi",
+               .owner  = THIS_MODULE,
+#ifdef CONFIG_PM
+               .pm     = &pxa2xx_spi_pm_ops,
+#endif
        },
        .remove = pxa2xx_spi_remove,
        .shutdown = pxa2xx_spi_shutdown,
-       .suspend = pxa2xx_spi_suspend,
-       .resume = pxa2xx_spi_resume,
 };
 
 static int __init pxa2xx_spi_init(void)
 {
        return platform_driver_probe(&driver, pxa2xx_spi_probe);
 }
-module_init(pxa2xx_spi_init);
+subsys_initcall(pxa2xx_spi_init);
 
 static void __exit pxa2xx_spi_exit(void)
 {