linux/arch/arm/mach-u300/dummyspichip.c
<<
>>
Prefs
   1/*
   2 * arch/arm/mach-u300/dummyspichip.c
   3 *
   4 * Copyright (C) 2007-2009 ST-Ericsson AB
   5 * License terms: GNU General Public License (GPL) version 2
   6 * This is a dummy loopback SPI "chip" used for testing SPI.
   7 * Author: Linus Walleij <linus.walleij@stericsson.com>
   8 */
   9#include <linux/init.h>
  10#include <linux/module.h>
  11#include <linux/kernel.h>
  12#include <linux/device.h>
  13#include <linux/err.h>
  14#include <linux/sysfs.h>
  15#include <linux/mutex.h>
  16#include <linux/spi/spi.h>
  17#include <linux/dma-mapping.h>
  18#include <linux/slab.h>
  19/*
  20 * WARNING! Do not include this pl022-specific controller header
  21 * for any generic driver. It is only done in this dummy chip
  22 * because we alter the chip configuration in order to test some
  23 * different settings on the loopback device. Normal chip configs
  24 * shall be STATIC and not altered by the driver!
  25 */
  26#include <linux/amba/pl022.h>
  27
  28struct dummy {
  29        struct device *dev;
  30        struct mutex lock;
  31};
  32
  33#define DMA_TEST_SIZE 2048
  34
  35/* When we cat /sys/bus/spi/devices/spi0.0/looptest this will be triggered */
  36static ssize_t dummy_looptest(struct device *dev,
  37                struct device_attribute *attr, char *buf)
  38{
  39        struct spi_device *spi = to_spi_device(dev);
  40        struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
  41
  42        /*
  43         * WARNING! Do not dereference the chip-specific data in any normal
  44         * driver for a chip. It is usually STATIC and shall not be read
  45         * or written to. Your chip driver should NOT depend on fields in this
  46         * struct, this is just used here to alter the behaviour of the chip
  47         * in order to perform tests.
  48         */
  49        int status;
  50        u8 txbuf[14] = {0xDE, 0xAD, 0xBE, 0xEF, 0x2B, 0xAD,
  51                        0xCA, 0xFE, 0xBA, 0xBE, 0xB1, 0x05,
  52                        0xF0, 0x0D};
  53        u8 rxbuf[14];
  54        u8 *bigtxbuf_virtual;
  55        u8 *bigrxbuf_virtual;
  56
  57        if (mutex_lock_interruptible(&p_dummy->lock))
  58                return -ERESTARTSYS;
  59
  60        bigtxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
  61        if (bigtxbuf_virtual == NULL) {
  62                status = -ENOMEM;
  63                goto out;
  64        }
  65        bigrxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
  66
  67        /* Fill TXBUF with some happy pattern */
  68        memset(bigtxbuf_virtual, 0xAA, DMA_TEST_SIZE);
  69
  70        /*
  71         * Force chip to 8 bit mode
  72         * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
  73         */
  74        spi->bits_per_word = 8;
  75        /* You should NOT DO THIS EITHER */
  76        spi->master->setup(spi);
  77
  78        /* Now run the tests for 8bit mode */
  79        pr_info("Simple test 1: write 0xAA byte, read back garbage byte "
  80                "in 8bit mode\n");
  81        status = spi_w8r8(spi, 0xAA);
  82        if (status < 0)
  83                pr_warn("Simple test 1: FAILURE: spi_write_then_read failed with status %d\n",
  84                        status);
  85        else
  86                pr_info("Simple test 1: SUCCESS!\n");
  87
  88        pr_info("Simple test 2: write 8 bytes, read back 8 bytes garbage "
  89                "in 8bit mode (full FIFO)\n");
  90        status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8);
  91        if (status < 0)
  92                pr_warn("Simple test 2: FAILURE: spi_write_then_read() failed with status %d\n",
  93                        status);
  94        else
  95                pr_info("Simple test 2: SUCCESS!\n");
  96
  97        pr_info("Simple test 3: write 14 bytes, read back 14 bytes garbage "
  98                "in 8bit mode (see if we overflow FIFO)\n");
  99        status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14);
 100        if (status < 0)
 101                pr_warn("Simple test 3: FAILURE: failed with status %d (probably FIFO overrun)\n",
 102                        status);
 103        else
 104                pr_info("Simple test 3: SUCCESS!\n");
 105
 106        pr_info("Simple test 4: write 8 bytes with spi_write(), read 8 "
 107                "bytes garbage with spi_read() in 8bit mode\n");
 108        status = spi_write(spi, &txbuf[0], 8);
 109        if (status < 0)
 110                pr_warn("Simple test 4 step 1: FAILURE: spi_write() failed with status %d\n",
 111                        status);
 112        else
 113                pr_info("Simple test 4 step 1: SUCCESS!\n");
 114        status = spi_read(spi, &rxbuf[0], 8);
 115        if (status < 0)
 116                pr_warn("Simple test 4 step 2: FAILURE: spi_read() failed with status %d\n",
 117                        status);
 118        else
 119                pr_info("Simple test 4 step 2: SUCCESS!\n");
 120
 121        pr_info("Simple test 5: write 14 bytes with spi_write(), read "
 122                "14 bytes garbage with spi_read() in 8bit mode\n");
 123        status = spi_write(spi, &txbuf[0], 14);
 124        if (status < 0)
 125                pr_warn("Simple test 5 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
 126                        status);
 127        else
 128                pr_info("Simple test 5 step 1: SUCCESS!\n");
 129        status = spi_read(spi, &rxbuf[0], 14);
 130        if (status < 0)
 131                pr_warn("Simple test 5 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
 132                        status);
 133        else
 134                pr_info("Simple test 5: SUCCESS!\n");
 135
 136        pr_info("Simple test 6: write %d bytes with spi_write(), "
 137                "read %d bytes garbage with spi_read() in 8bit mode\n",
 138                DMA_TEST_SIZE, DMA_TEST_SIZE);
 139        status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
 140        if (status < 0)
 141                pr_warn("Simple test 6 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
 142                        status);
 143        else
 144                pr_info("Simple test 6 step 1: SUCCESS!\n");
 145        status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
 146        if (status < 0)
 147                pr_warn("Simple test 6 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
 148                        status);
 149        else
 150                pr_info("Simple test 6: SUCCESS!\n");
 151
 152
 153        /*
 154         * Force chip to 16 bit mode
 155         * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
 156         */
 157        spi->bits_per_word = 16;
 158        /* You should NOT DO THIS EITHER */
 159        spi->master->setup(spi);
 160
 161        pr_info("Simple test 7: write 0xAA byte, read back garbage byte "
 162                "in 16bit bus mode\n");
 163        status = spi_w8r8(spi, 0xAA);
 164        if (status == -EIO)
 165                pr_info("Simple test 7: SUCCESS! (expected failure with "
 166                        "status EIO)\n");
 167        else if (status < 0)
 168                pr_warn("Simple test 7: FAILURE: spi_write_then_read failed with status %d\n",
 169                        status);
 170        else
 171                pr_warn("Simple test 7: FAILURE: spi_write_then_read succeeded but it was expected to fail!\n");
 172
 173        pr_info("Simple test 8: write 8 bytes, read back 8 bytes garbage "
 174                "in 16bit mode (full FIFO)\n");
 175        status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8);
 176        if (status < 0)
 177                pr_warn("Simple test 8: FAILURE: spi_write_then_read() failed with status %d\n",
 178                        status);
 179        else
 180                pr_info("Simple test 8: SUCCESS!\n");
 181
 182        pr_info("Simple test 9: write 14 bytes, read back 14 bytes garbage "
 183                "in 16bit mode (see if we overflow FIFO)\n");
 184        status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14);
 185        if (status < 0)
 186                pr_warn("Simple test 9: FAILURE: failed with status %d (probably FIFO overrun)\n",
 187                        status);
 188        else
 189                pr_info("Simple test 9: SUCCESS!\n");
 190
 191        pr_info("Simple test 10: write %d bytes with spi_write(), "
 192               "read %d bytes garbage with spi_read() in 16bit mode\n",
 193               DMA_TEST_SIZE, DMA_TEST_SIZE);
 194        status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
 195        if (status < 0)
 196                pr_warn("Simple test 10 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
 197                        status);
 198        else
 199                pr_info("Simple test 10 step 1: SUCCESS!\n");
 200
 201        status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
 202        if (status < 0)
 203                pr_warn("Simple test 10 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
 204                        status);
 205        else
 206                pr_info("Simple test 10: SUCCESS!\n");
 207
 208        status = sprintf(buf, "loop test complete\n");
 209        kfree(bigrxbuf_virtual);
 210        kfree(bigtxbuf_virtual);
 211 out:
 212        mutex_unlock(&p_dummy->lock);
 213        return status;
 214}
 215
 216static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL);
 217
 218static int pl022_dummy_probe(struct spi_device *spi)
 219{
 220        struct dummy *p_dummy;
 221        int status;
 222
 223        dev_info(&spi->dev, "probing dummy SPI device\n");
 224
 225        p_dummy = kzalloc(sizeof *p_dummy, GFP_KERNEL);
 226        if (!p_dummy)
 227                return -ENOMEM;
 228
 229        dev_set_drvdata(&spi->dev, p_dummy);
 230        mutex_init(&p_dummy->lock);
 231
 232        /* sysfs hook */
 233        status = device_create_file(&spi->dev, &dev_attr_looptest);
 234        if (status) {
 235                dev_dbg(&spi->dev, "device_create_file looptest failure.\n");
 236                goto out_dev_create_looptest_failed;
 237        }
 238
 239        return 0;
 240
 241out_dev_create_looptest_failed:
 242        dev_set_drvdata(&spi->dev, NULL);
 243        kfree(p_dummy);
 244        return status;
 245}
 246
 247static int pl022_dummy_remove(struct spi_device *spi)
 248{
 249        struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
 250
 251        dev_info(&spi->dev, "removing dummy SPI device\n");
 252        device_remove_file(&spi->dev, &dev_attr_looptest);
 253        dev_set_drvdata(&spi->dev, NULL);
 254        kfree(p_dummy);
 255
 256        return 0;
 257}
 258
 259static const struct of_device_id pl022_dummy_dt_match[] = {
 260        { .compatible = "arm,pl022-dummy" },
 261        {},
 262};
 263
 264static struct spi_driver pl022_dummy_driver = {
 265        .driver = {
 266                .name   = "spi-dummy",
 267                .of_match_table = pl022_dummy_dt_match,
 268        },
 269        .probe  = pl022_dummy_probe,
 270        .remove = pl022_dummy_remove,
 271};
 272
 273module_spi_driver(pl022_dummy_driver);
 274MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
 275MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver");
 276MODULE_LICENSE("GPL");
 277