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