2 * arch/arm/mach-u300/dummyspichip.c
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>
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>
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!
26 #include <linux/amba/pl022.h>
33 #define DMA_TEST_SIZE 2048
35 /* When we cat /sys/bus/spi/devices/spi0.0/looptest this will be triggered */
36 static ssize_t dummy_looptest(struct device *dev,
37 struct device_attribute *attr, char *buf)
39 struct spi_device *spi = to_spi_device(dev);
40 struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
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.
50 u8 txbuf[14] = {0xDE, 0xAD, 0xBE, 0xEF, 0x2B, 0xAD,
51 0xCA, 0xFE, 0xBA, 0xBE, 0xB1, 0x05,
57 if (mutex_lock_interruptible(&p_dummy->lock))
60 bigtxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
61 if (bigtxbuf_virtual == NULL) {
65 bigrxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
67 /* Fill TXBUF with some happy pattern */
68 memset(bigtxbuf_virtual, 0xAA, DMA_TEST_SIZE);
71 * Force chip to 8 bit mode
72 * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
74 spi->bits_per_word = 8;
75 /* You should NOT DO THIS EITHER */
76 spi->master->setup(spi);
78 /* Now run the tests for 8bit mode */
79 pr_info("Simple test 1: write 0xAA byte, read back garbage byte "
81 status = spi_w8r8(spi, 0xAA);
83 pr_warn("Simple test 1: FAILURE: spi_write_then_read failed with status %d\n",
86 pr_info("Simple test 1: SUCCESS!\n");
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);
92 pr_warn("Simple test 2: FAILURE: spi_write_then_read() failed with status %d\n",
95 pr_info("Simple test 2: SUCCESS!\n");
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);
101 pr_warn("Simple test 3: FAILURE: failed with status %d (probably FIFO overrun)\n",
104 pr_info("Simple test 3: SUCCESS!\n");
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);
110 pr_warn("Simple test 4 step 1: FAILURE: spi_write() failed with status %d\n",
113 pr_info("Simple test 4 step 1: SUCCESS!\n");
114 status = spi_read(spi, &rxbuf[0], 8);
116 pr_warn("Simple test 4 step 2: FAILURE: spi_read() failed with status %d\n",
119 pr_info("Simple test 4 step 2: SUCCESS!\n");
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);
125 pr_warn("Simple test 5 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
128 pr_info("Simple test 5 step 1: SUCCESS!\n");
129 status = spi_read(spi, &rxbuf[0], 14);
131 pr_warn("Simple test 5 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
134 pr_info("Simple test 5: SUCCESS!\n");
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);
141 pr_warn("Simple test 6 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
144 pr_info("Simple test 6 step 1: SUCCESS!\n");
145 status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
147 pr_warn("Simple test 6 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
150 pr_info("Simple test 6: SUCCESS!\n");
154 * Force chip to 16 bit mode
155 * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
157 spi->bits_per_word = 16;
158 /* You should NOT DO THIS EITHER */
159 spi->master->setup(spi);
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);
165 pr_info("Simple test 7: SUCCESS! (expected failure with "
168 pr_warn("Simple test 7: FAILURE: spi_write_then_read failed with status %d\n",
171 pr_warn("Simple test 7: FAILURE: spi_write_then_read succeeded but it was expected to fail!\n");
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);
177 pr_warn("Simple test 8: FAILURE: spi_write_then_read() failed with status %d\n",
180 pr_info("Simple test 8: SUCCESS!\n");
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);
186 pr_warn("Simple test 9: FAILURE: failed with status %d (probably FIFO overrun)\n",
189 pr_info("Simple test 9: SUCCESS!\n");
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);
196 pr_warn("Simple test 10 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
199 pr_info("Simple test 10 step 1: SUCCESS!\n");
201 status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
203 pr_warn("Simple test 10 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
206 pr_info("Simple test 10: SUCCESS!\n");
208 status = sprintf(buf, "loop test complete\n");
209 kfree(bigrxbuf_virtual);
210 kfree(bigtxbuf_virtual);
212 mutex_unlock(&p_dummy->lock);
216 static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL);
218 static int pl022_dummy_probe(struct spi_device *spi)
220 struct dummy *p_dummy;
223 dev_info(&spi->dev, "probing dummy SPI device\n");
225 p_dummy = kzalloc(sizeof *p_dummy, GFP_KERNEL);
229 dev_set_drvdata(&spi->dev, p_dummy);
230 mutex_init(&p_dummy->lock);
233 status = device_create_file(&spi->dev, &dev_attr_looptest);
235 dev_dbg(&spi->dev, "device_create_file looptest failure.\n");
236 goto out_dev_create_looptest_failed;
241 out_dev_create_looptest_failed:
242 dev_set_drvdata(&spi->dev, NULL);
247 static int pl022_dummy_remove(struct spi_device *spi)
249 struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
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);
259 static const struct of_device_id pl022_dummy_dt_match[] = {
260 { .compatible = "arm,pl022-dummy" },
264 static struct spi_driver pl022_dummy_driver = {
267 .of_match_table = pl022_dummy_dt_match,
269 .probe = pl022_dummy_probe,
270 .remove = pl022_dummy_remove,
273 module_spi_driver(pl022_dummy_driver);
274 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
275 MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver");
276 MODULE_LICENSE("GPL");