Linus Walleij | c7c8c78 | 2009-08-14 10:59:05 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Walleij | c7c8c78 | 2009-08-14 10:59:05 +0100 | [diff] [blame] | 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 | struct pl022_config_chip *chip_info = spi->controller_data; |
| 50 | int status; |
| 51 | u8 txbuf[14] = {0xDE, 0xAD, 0xBE, 0xEF, 0x2B, 0xAD, |
| 52 | 0xCA, 0xFE, 0xBA, 0xBE, 0xB1, 0x05, |
| 53 | 0xF0, 0x0D}; |
| 54 | u8 rxbuf[14]; |
| 55 | u8 *bigtxbuf_virtual; |
| 56 | u8 *bigrxbuf_virtual; |
| 57 | |
| 58 | if (mutex_lock_interruptible(&p_dummy->lock)) |
| 59 | return -ERESTARTSYS; |
| 60 | |
| 61 | bigtxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL); |
| 62 | if (bigtxbuf_virtual == NULL) { |
| 63 | status = -ENOMEM; |
| 64 | goto out; |
| 65 | } |
| 66 | bigrxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL); |
| 67 | |
| 68 | /* Fill TXBUF with some happy pattern */ |
| 69 | memset(bigtxbuf_virtual, 0xAA, DMA_TEST_SIZE); |
| 70 | |
| 71 | /* |
| 72 | * Force chip to 8 bit mode |
| 73 | * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC! |
| 74 | */ |
| 75 | chip_info->data_size = SSP_DATA_BITS_8; |
| 76 | /* You should NOT DO THIS EITHER */ |
| 77 | spi->master->setup(spi); |
| 78 | |
| 79 | /* Now run the tests for 8bit mode */ |
| 80 | pr_info("Simple test 1: write 0xAA byte, read back garbage byte " |
| 81 | "in 8bit mode\n"); |
| 82 | status = spi_w8r8(spi, 0xAA); |
| 83 | if (status < 0) |
| 84 | pr_warning("Siple test 1: FAILURE: spi_write_then_read " |
| 85 | "failed with status %d\n", status); |
| 86 | else |
| 87 | pr_info("Simple test 1: SUCCESS!\n"); |
| 88 | |
| 89 | pr_info("Simple test 2: write 8 bytes, read back 8 bytes garbage " |
| 90 | "in 8bit mode (full FIFO)\n"); |
| 91 | status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8); |
| 92 | if (status < 0) |
| 93 | pr_warning("Simple test 2: FAILURE: spi_write_then_read() " |
| 94 | "failed with status %d\n", status); |
| 95 | else |
| 96 | pr_info("Simple test 2: SUCCESS!\n"); |
| 97 | |
| 98 | pr_info("Simple test 3: write 14 bytes, read back 14 bytes garbage " |
| 99 | "in 8bit mode (see if we overflow FIFO)\n"); |
| 100 | status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14); |
| 101 | if (status < 0) |
| 102 | pr_warning("Simple test 3: FAILURE: failed with status %d " |
| 103 | "(probably FIFO overrun)\n", status); |
| 104 | else |
| 105 | pr_info("Simple test 3: SUCCESS!\n"); |
| 106 | |
| 107 | pr_info("Simple test 4: write 8 bytes with spi_write(), read 8 " |
| 108 | "bytes garbage with spi_read() in 8bit mode\n"); |
| 109 | status = spi_write(spi, &txbuf[0], 8); |
| 110 | if (status < 0) |
| 111 | pr_warning("Simple test 4 step 1: FAILURE: spi_write() " |
| 112 | "failed with status %d\n", status); |
| 113 | else |
| 114 | pr_info("Simple test 4 step 1: SUCCESS!\n"); |
| 115 | status = spi_read(spi, &rxbuf[0], 8); |
| 116 | if (status < 0) |
| 117 | pr_warning("Simple test 4 step 2: FAILURE: spi_read() " |
| 118 | "failed with status %d\n", status); |
| 119 | else |
| 120 | pr_info("Simple test 4 step 2: SUCCESS!\n"); |
| 121 | |
| 122 | pr_info("Simple test 5: write 14 bytes with spi_write(), read " |
| 123 | "14 bytes garbage with spi_read() in 8bit mode\n"); |
| 124 | status = spi_write(spi, &txbuf[0], 14); |
| 125 | if (status < 0) |
| 126 | pr_warning("Simple test 5 step 1: FAILURE: spi_write() " |
| 127 | "failed with status %d (probably FIFO overrun)\n", |
| 128 | status); |
| 129 | else |
| 130 | pr_info("Simple test 5 step 1: SUCCESS!\n"); |
| 131 | status = spi_read(spi, &rxbuf[0], 14); |
| 132 | if (status < 0) |
| 133 | pr_warning("Simple test 5 step 2: FAILURE: spi_read() " |
| 134 | "failed with status %d (probably FIFO overrun)\n", |
| 135 | status); |
| 136 | else |
| 137 | pr_info("Simple test 5: SUCCESS!\n"); |
| 138 | |
| 139 | pr_info("Simple test 6: write %d bytes with spi_write(), " |
| 140 | "read %d bytes garbage with spi_read() in 8bit mode\n", |
| 141 | DMA_TEST_SIZE, DMA_TEST_SIZE); |
| 142 | status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE); |
| 143 | if (status < 0) |
| 144 | pr_warning("Simple test 6 step 1: FAILURE: spi_write() " |
| 145 | "failed with status %d (probably FIFO overrun)\n", |
| 146 | status); |
| 147 | else |
| 148 | pr_info("Simple test 6 step 1: SUCCESS!\n"); |
| 149 | status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE); |
| 150 | if (status < 0) |
| 151 | pr_warning("Simple test 6 step 2: FAILURE: spi_read() " |
| 152 | "failed with status %d (probably FIFO overrun)\n", |
| 153 | status); |
| 154 | else |
| 155 | pr_info("Simple test 6: SUCCESS!\n"); |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | * Force chip to 16 bit mode |
| 160 | * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC! |
| 161 | */ |
| 162 | chip_info->data_size = SSP_DATA_BITS_16; |
| 163 | /* You should NOT DO THIS EITHER */ |
| 164 | spi->master->setup(spi); |
| 165 | |
| 166 | pr_info("Simple test 7: write 0xAA byte, read back garbage byte " |
| 167 | "in 16bit bus mode\n"); |
| 168 | status = spi_w8r8(spi, 0xAA); |
| 169 | if (status == -EIO) |
| 170 | pr_info("Simple test 7: SUCCESS! (expected failure with " |
| 171 | "status EIO)\n"); |
| 172 | else if (status < 0) |
| 173 | pr_warning("Siple test 7: FAILURE: spi_write_then_read " |
| 174 | "failed with status %d\n", status); |
| 175 | else |
| 176 | pr_warning("Siple test 7: FAILURE: spi_write_then_read " |
| 177 | "succeeded but it was expected to fail!\n"); |
| 178 | |
| 179 | pr_info("Simple test 8: write 8 bytes, read back 8 bytes garbage " |
| 180 | "in 16bit mode (full FIFO)\n"); |
| 181 | status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8); |
| 182 | if (status < 0) |
| 183 | pr_warning("Simple test 8: FAILURE: spi_write_then_read() " |
| 184 | "failed with status %d\n", status); |
| 185 | else |
| 186 | pr_info("Simple test 8: SUCCESS!\n"); |
| 187 | |
| 188 | pr_info("Simple test 9: write 14 bytes, read back 14 bytes garbage " |
| 189 | "in 16bit mode (see if we overflow FIFO)\n"); |
| 190 | status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14); |
| 191 | if (status < 0) |
| 192 | pr_warning("Simple test 9: FAILURE: failed with status %d " |
| 193 | "(probably FIFO overrun)\n", status); |
| 194 | else |
| 195 | pr_info("Simple test 9: SUCCESS!\n"); |
| 196 | |
| 197 | pr_info("Simple test 10: write %d bytes with spi_write(), " |
| 198 | "read %d bytes garbage with spi_read() in 16bit mode\n", |
| 199 | DMA_TEST_SIZE, DMA_TEST_SIZE); |
| 200 | status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE); |
| 201 | if (status < 0) |
| 202 | pr_warning("Simple test 10 step 1: FAILURE: spi_write() " |
| 203 | "failed with status %d (probably FIFO overrun)\n", |
| 204 | status); |
| 205 | else |
| 206 | pr_info("Simple test 10 step 1: SUCCESS!\n"); |
| 207 | |
| 208 | status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE); |
| 209 | if (status < 0) |
| 210 | pr_warning("Simple test 10 step 2: FAILURE: spi_read() " |
| 211 | "failed with status %d (probably FIFO overrun)\n", |
| 212 | status); |
| 213 | else |
| 214 | pr_info("Simple test 10: SUCCESS!\n"); |
| 215 | |
| 216 | status = sprintf(buf, "loop test complete\n"); |
| 217 | kfree(bigrxbuf_virtual); |
| 218 | kfree(bigtxbuf_virtual); |
| 219 | out: |
| 220 | mutex_unlock(&p_dummy->lock); |
| 221 | return status; |
| 222 | } |
| 223 | |
| 224 | static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL); |
| 225 | |
| 226 | static int __devinit pl022_dummy_probe(struct spi_device *spi) |
| 227 | { |
| 228 | struct dummy *p_dummy; |
| 229 | int status; |
| 230 | |
| 231 | dev_info(&spi->dev, "probing dummy SPI device\n"); |
| 232 | |
| 233 | p_dummy = kzalloc(sizeof *p_dummy, GFP_KERNEL); |
| 234 | if (!p_dummy) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | dev_set_drvdata(&spi->dev, p_dummy); |
| 238 | mutex_init(&p_dummy->lock); |
| 239 | |
| 240 | /* sysfs hook */ |
| 241 | status = device_create_file(&spi->dev, &dev_attr_looptest); |
| 242 | if (status) { |
| 243 | dev_dbg(&spi->dev, "device_create_file looptest failure.\n"); |
| 244 | goto out_dev_create_looptest_failed; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | |
| 249 | out_dev_create_looptest_failed: |
| 250 | dev_set_drvdata(&spi->dev, NULL); |
| 251 | kfree(p_dummy); |
| 252 | return status; |
| 253 | } |
| 254 | |
| 255 | static int __devexit pl022_dummy_remove(struct spi_device *spi) |
| 256 | { |
| 257 | struct dummy *p_dummy = dev_get_drvdata(&spi->dev); |
| 258 | |
| 259 | dev_info(&spi->dev, "removing dummy SPI device\n"); |
| 260 | device_remove_file(&spi->dev, &dev_attr_looptest); |
| 261 | dev_set_drvdata(&spi->dev, NULL); |
| 262 | kfree(p_dummy); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static struct spi_driver pl022_dummy_driver = { |
| 268 | .driver = { |
| 269 | .name = "spi-dummy", |
| 270 | .owner = THIS_MODULE, |
| 271 | }, |
| 272 | .probe = pl022_dummy_probe, |
| 273 | .remove = __devexit_p(pl022_dummy_remove), |
| 274 | }; |
| 275 | |
| 276 | static int __init pl022_init_dummy(void) |
| 277 | { |
| 278 | return spi_register_driver(&pl022_dummy_driver); |
| 279 | } |
| 280 | |
| 281 | static void __exit pl022_exit_dummy(void) |
| 282 | { |
| 283 | spi_unregister_driver(&pl022_dummy_driver); |
| 284 | } |
| 285 | |
| 286 | module_init(pl022_init_dummy); |
| 287 | module_exit(pl022_exit_dummy); |
| 288 | |
| 289 | MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); |
| 290 | MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver"); |
| 291 | MODULE_LICENSE("GPL"); |