~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/arm/mach-u300/dummyspichip.c

Version: ~ [ linux-5.5 ] ~ [ linux-5.4.15 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.98 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.167 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.211 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.211 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 28 struct 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 */
 36 static 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 
216 static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL);
217 
218 static 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 
241 out_dev_create_looptest_failed:
242         dev_set_drvdata(&spi->dev, NULL);
243         kfree(p_dummy);
244         return status;
245 }
246 
247 static 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 
259 static const struct of_device_id pl022_dummy_dt_match[] = {
260         { .compatible = "arm,pl022-dummy" },
261         {},
262 };
263 
264 static struct spi_driver pl022_dummy_driver = {
265         .driver = {
266                 .name   = "spi-dummy",
267                 .owner  = THIS_MODULE,
268                 .of_match_table = pl022_dummy_dt_match,
269         },
270         .probe  = pl022_dummy_probe,
271         .remove = pl022_dummy_remove,
272 };
273 
274 module_spi_driver(pl022_dummy_driver);
275 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
276 MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver");
277 MODULE_LICENSE("GPL");
278 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp