dummyspichip.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. struct dummy {
  28. struct device *dev;
  29. struct mutex lock;
  30. };
  31. #define DMA_TEST_SIZE 2048
  32. /* When we cat /sys/bus/spi/devices/spi0.0/looptest this will be triggered */
  33. static ssize_t dummy_looptest(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct spi_device *spi = to_spi_device(dev);
  37. struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
  38. /*
  39. * WARNING! Do not dereference the chip-specific data in any normal
  40. * driver for a chip. It is usually STATIC and shall not be read
  41. * or written to. Your chip driver should NOT depend on fields in this
  42. * struct, this is just used here to alter the behaviour of the chip
  43. * in order to perform tests.
  44. */
  45. struct pl022_config_chip *chip_info = spi->controller_data;
  46. int status;
  47. u8 txbuf[14] = {0xDE, 0xAD, 0xBE, 0xEF, 0x2B, 0xAD,
  48. 0xCA, 0xFE, 0xBA, 0xBE, 0xB1, 0x05,
  49. 0xF0, 0x0D};
  50. u8 rxbuf[14];
  51. u8 *bigtxbuf_virtual;
  52. u8 *bigrxbuf_virtual;
  53. if (mutex_lock_interruptible(&p_dummy->lock))
  54. return -ERESTARTSYS;
  55. bigtxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
  56. if (bigtxbuf_virtual == NULL) {
  57. status = -ENOMEM;
  58. goto out;
  59. }
  60. bigrxbuf_virtual = kmalloc(DMA_TEST_SIZE, GFP_KERNEL);
  61. /* Fill TXBUF with some happy pattern */
  62. memset(bigtxbuf_virtual, 0xAA, DMA_TEST_SIZE);
  63. /*
  64. * Force chip to 8 bit mode
  65. * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
  66. */
  67. chip_info->data_size = SSP_DATA_BITS_8;
  68. /* You should NOT DO THIS EITHER */
  69. spi->master->setup(spi);
  70. /* Now run the tests for 8bit mode */
  71. pr_info("Simple test 1: write 0xAA byte, read back garbage byte "
  72. "in 8bit mode\n");
  73. status = spi_w8r8(spi, 0xAA);
  74. if (status < 0)
  75. pr_warning("Siple test 1: FAILURE: spi_write_then_read "
  76. "failed with status %d\n", status);
  77. else
  78. pr_info("Simple test 1: SUCCESS!\n");
  79. pr_info("Simple test 2: write 8 bytes, read back 8 bytes garbage "
  80. "in 8bit mode (full FIFO)\n");
  81. status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8);
  82. if (status < 0)
  83. pr_warning("Simple test 2: FAILURE: spi_write_then_read() "
  84. "failed with status %d\n", status);
  85. else
  86. pr_info("Simple test 2: SUCCESS!\n");
  87. pr_info("Simple test 3: write 14 bytes, read back 14 bytes garbage "
  88. "in 8bit mode (see if we overflow FIFO)\n");
  89. status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14);
  90. if (status < 0)
  91. pr_warning("Simple test 3: FAILURE: failed with status %d "
  92. "(probably FIFO overrun)\n", status);
  93. else
  94. pr_info("Simple test 3: SUCCESS!\n");
  95. pr_info("Simple test 4: write 8 bytes with spi_write(), read 8 "
  96. "bytes garbage with spi_read() in 8bit mode\n");
  97. status = spi_write(spi, &txbuf[0], 8);
  98. if (status < 0)
  99. pr_warning("Simple test 4 step 1: FAILURE: spi_write() "
  100. "failed with status %d\n", status);
  101. else
  102. pr_info("Simple test 4 step 1: SUCCESS!\n");
  103. status = spi_read(spi, &rxbuf[0], 8);
  104. if (status < 0)
  105. pr_warning("Simple test 4 step 2: FAILURE: spi_read() "
  106. "failed with status %d\n", status);
  107. else
  108. pr_info("Simple test 4 step 2: SUCCESS!\n");
  109. pr_info("Simple test 5: write 14 bytes with spi_write(), read "
  110. "14 bytes garbage with spi_read() in 8bit mode\n");
  111. status = spi_write(spi, &txbuf[0], 14);
  112. if (status < 0)
  113. pr_warning("Simple test 5 step 1: FAILURE: spi_write() "
  114. "failed with status %d (probably FIFO overrun)\n",
  115. status);
  116. else
  117. pr_info("Simple test 5 step 1: SUCCESS!\n");
  118. status = spi_read(spi, &rxbuf[0], 14);
  119. if (status < 0)
  120. pr_warning("Simple test 5 step 2: FAILURE: spi_read() "
  121. "failed with status %d (probably FIFO overrun)\n",
  122. status);
  123. else
  124. pr_info("Simple test 5: SUCCESS!\n");
  125. pr_info("Simple test 6: write %d bytes with spi_write(), "
  126. "read %d bytes garbage with spi_read() in 8bit mode\n",
  127. DMA_TEST_SIZE, DMA_TEST_SIZE);
  128. status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
  129. if (status < 0)
  130. pr_warning("Simple test 6 step 1: FAILURE: spi_write() "
  131. "failed with status %d (probably FIFO overrun)\n",
  132. status);
  133. else
  134. pr_info("Simple test 6 step 1: SUCCESS!\n");
  135. status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
  136. if (status < 0)
  137. pr_warning("Simple test 6 step 2: FAILURE: spi_read() "
  138. "failed with status %d (probably FIFO overrun)\n",
  139. status);
  140. else
  141. pr_info("Simple test 6: SUCCESS!\n");
  142. /*
  143. * Force chip to 16 bit mode
  144. * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
  145. */
  146. chip_info->data_size = SSP_DATA_BITS_16;
  147. /* You should NOT DO THIS EITHER */
  148. spi->master->setup(spi);
  149. pr_info("Simple test 7: write 0xAA byte, read back garbage byte "
  150. "in 16bit bus mode\n");
  151. status = spi_w8r8(spi, 0xAA);
  152. if (status == -EIO)
  153. pr_info("Simple test 7: SUCCESS! (expected failure with "
  154. "status EIO)\n");
  155. else if (status < 0)
  156. pr_warning("Siple test 7: FAILURE: spi_write_then_read "
  157. "failed with status %d\n", status);
  158. else
  159. pr_warning("Siple test 7: FAILURE: spi_write_then_read "
  160. "succeeded but it was expected to fail!\n");
  161. pr_info("Simple test 8: write 8 bytes, read back 8 bytes garbage "
  162. "in 16bit mode (full FIFO)\n");
  163. status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8);
  164. if (status < 0)
  165. pr_warning("Simple test 8: FAILURE: spi_write_then_read() "
  166. "failed with status %d\n", status);
  167. else
  168. pr_info("Simple test 8: SUCCESS!\n");
  169. pr_info("Simple test 9: write 14 bytes, read back 14 bytes garbage "
  170. "in 16bit mode (see if we overflow FIFO)\n");
  171. status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14);
  172. if (status < 0)
  173. pr_warning("Simple test 9: FAILURE: failed with status %d "
  174. "(probably FIFO overrun)\n", status);
  175. else
  176. pr_info("Simple test 9: SUCCESS!\n");
  177. pr_info("Simple test 10: write %d bytes with spi_write(), "
  178. "read %d bytes garbage with spi_read() in 16bit mode\n",
  179. DMA_TEST_SIZE, DMA_TEST_SIZE);
  180. status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
  181. if (status < 0)
  182. pr_warning("Simple test 10 step 1: FAILURE: spi_write() "
  183. "failed with status %d (probably FIFO overrun)\n",
  184. status);
  185. else
  186. pr_info("Simple test 10 step 1: SUCCESS!\n");
  187. status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
  188. if (status < 0)
  189. pr_warning("Simple test 10 step 2: FAILURE: spi_read() "
  190. "failed with status %d (probably FIFO overrun)\n",
  191. status);
  192. else
  193. pr_info("Simple test 10: SUCCESS!\n");
  194. status = sprintf(buf, "loop test complete\n");
  195. kfree(bigrxbuf_virtual);
  196. kfree(bigtxbuf_virtual);
  197. out:
  198. mutex_unlock(&p_dummy->lock);
  199. return status;
  200. }
  201. static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL);
  202. static int __devinit pl022_dummy_probe(struct spi_device *spi)
  203. {
  204. struct dummy *p_dummy;
  205. int status;
  206. dev_info(&spi->dev, "probing dummy SPI device\n");
  207. p_dummy = kzalloc(sizeof *p_dummy, GFP_KERNEL);
  208. if (!p_dummy)
  209. return -ENOMEM;
  210. dev_set_drvdata(&spi->dev, p_dummy);
  211. mutex_init(&p_dummy->lock);
  212. /* sysfs hook */
  213. status = device_create_file(&spi->dev, &dev_attr_looptest);
  214. if (status) {
  215. dev_dbg(&spi->dev, "device_create_file looptest failure.\n");
  216. goto out_dev_create_looptest_failed;
  217. }
  218. return 0;
  219. out_dev_create_looptest_failed:
  220. dev_set_drvdata(&spi->dev, NULL);
  221. kfree(p_dummy);
  222. return status;
  223. }
  224. static int __devexit pl022_dummy_remove(struct spi_device *spi)
  225. {
  226. struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
  227. dev_info(&spi->dev, "removing dummy SPI device\n");
  228. device_remove_file(&spi->dev, &dev_attr_looptest);
  229. dev_set_drvdata(&spi->dev, NULL);
  230. kfree(p_dummy);
  231. return 0;
  232. }
  233. static struct spi_driver pl022_dummy_driver = {
  234. .driver = {
  235. .name = "spi-dummy",
  236. .owner = THIS_MODULE,
  237. },
  238. .probe = pl022_dummy_probe,
  239. .remove = __devexit_p(pl022_dummy_remove),
  240. };
  241. static int __init pl022_init_dummy(void)
  242. {
  243. return spi_register_driver(&pl022_dummy_driver);
  244. }
  245. static void __exit pl022_exit_dummy(void)
  246. {
  247. spi_unregister_driver(&pl022_dummy_driver);
  248. }
  249. module_init(pl022_init_dummy);
  250. module_exit(pl022_exit_dummy);
  251. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  252. MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver");
  253. MODULE_LICENSE("GPL");