dummyspichip.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_warn("Simple test 1: FAILURE: spi_write_then_read failed with status %d\n",
  75. 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_warn("Simple test 2: FAILURE: spi_write_then_read() failed with status %d\n",
  83. 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_warn("Simple test 3: FAILURE: failed with status %d (probably FIFO overrun)\n",
  91. 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_warn("Simple test 4 step 1: FAILURE: spi_write() failed with status %d\n",
  99. 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_warn("Simple test 4 step 2: FAILURE: spi_read() failed with status %d\n",
  105. 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_warn("Simple test 5 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
  113. status);
  114. else
  115. pr_info("Simple test 5 step 1: SUCCESS!\n");
  116. status = spi_read(spi, &rxbuf[0], 14);
  117. if (status < 0)
  118. pr_warn("Simple test 5 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
  119. status);
  120. else
  121. pr_info("Simple test 5: SUCCESS!\n");
  122. pr_info("Simple test 6: write %d bytes with spi_write(), "
  123. "read %d bytes garbage with spi_read() in 8bit mode\n",
  124. DMA_TEST_SIZE, DMA_TEST_SIZE);
  125. status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
  126. if (status < 0)
  127. pr_warn("Simple test 6 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
  128. status);
  129. else
  130. pr_info("Simple test 6 step 1: SUCCESS!\n");
  131. status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
  132. if (status < 0)
  133. pr_warn("Simple test 6 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
  134. status);
  135. else
  136. pr_info("Simple test 6: SUCCESS!\n");
  137. /*
  138. * Force chip to 16 bit mode
  139. * WARNING: NEVER DO THIS IN REAL DRIVER CODE, THIS SHOULD BE STATIC!
  140. */
  141. spi->bits_per_word = 16;
  142. /* You should NOT DO THIS EITHER */
  143. spi->master->setup(spi);
  144. pr_info("Simple test 7: write 0xAA byte, read back garbage byte "
  145. "in 16bit bus mode\n");
  146. status = spi_w8r8(spi, 0xAA);
  147. if (status == -EIO)
  148. pr_info("Simple test 7: SUCCESS! (expected failure with "
  149. "status EIO)\n");
  150. else if (status < 0)
  151. pr_warn("Simple test 7: FAILURE: spi_write_then_read failed with status %d\n",
  152. status);
  153. else
  154. pr_warn("Simple test 7: FAILURE: spi_write_then_read succeeded but it was expected to fail!\n");
  155. pr_info("Simple test 8: write 8 bytes, read back 8 bytes garbage "
  156. "in 16bit mode (full FIFO)\n");
  157. status = spi_write_then_read(spi, &txbuf[0], 8, &rxbuf[0], 8);
  158. if (status < 0)
  159. pr_warn("Simple test 8: FAILURE: spi_write_then_read() failed with status %d\n",
  160. status);
  161. else
  162. pr_info("Simple test 8: SUCCESS!\n");
  163. pr_info("Simple test 9: write 14 bytes, read back 14 bytes garbage "
  164. "in 16bit mode (see if we overflow FIFO)\n");
  165. status = spi_write_then_read(spi, &txbuf[0], 14, &rxbuf[0], 14);
  166. if (status < 0)
  167. pr_warn("Simple test 9: FAILURE: failed with status %d (probably FIFO overrun)\n",
  168. status);
  169. else
  170. pr_info("Simple test 9: SUCCESS!\n");
  171. pr_info("Simple test 10: write %d bytes with spi_write(), "
  172. "read %d bytes garbage with spi_read() in 16bit mode\n",
  173. DMA_TEST_SIZE, DMA_TEST_SIZE);
  174. status = spi_write(spi, &bigtxbuf_virtual[0], DMA_TEST_SIZE);
  175. if (status < 0)
  176. pr_warn("Simple test 10 step 1: FAILURE: spi_write() failed with status %d (probably FIFO overrun)\n",
  177. status);
  178. else
  179. pr_info("Simple test 10 step 1: SUCCESS!\n");
  180. status = spi_read(spi, &bigrxbuf_virtual[0], DMA_TEST_SIZE);
  181. if (status < 0)
  182. pr_warn("Simple test 10 step 2: FAILURE: spi_read() failed with status %d (probably FIFO overrun)\n",
  183. status);
  184. else
  185. pr_info("Simple test 10: SUCCESS!\n");
  186. status = sprintf(buf, "loop test complete\n");
  187. kfree(bigrxbuf_virtual);
  188. kfree(bigtxbuf_virtual);
  189. out:
  190. mutex_unlock(&p_dummy->lock);
  191. return status;
  192. }
  193. static DEVICE_ATTR(looptest, S_IRUGO, dummy_looptest, NULL);
  194. static int pl022_dummy_probe(struct spi_device *spi)
  195. {
  196. struct dummy *p_dummy;
  197. int status;
  198. dev_info(&spi->dev, "probing dummy SPI device\n");
  199. p_dummy = kzalloc(sizeof *p_dummy, GFP_KERNEL);
  200. if (!p_dummy)
  201. return -ENOMEM;
  202. dev_set_drvdata(&spi->dev, p_dummy);
  203. mutex_init(&p_dummy->lock);
  204. /* sysfs hook */
  205. status = device_create_file(&spi->dev, &dev_attr_looptest);
  206. if (status) {
  207. dev_dbg(&spi->dev, "device_create_file looptest failure.\n");
  208. goto out_dev_create_looptest_failed;
  209. }
  210. return 0;
  211. out_dev_create_looptest_failed:
  212. dev_set_drvdata(&spi->dev, NULL);
  213. kfree(p_dummy);
  214. return status;
  215. }
  216. static int pl022_dummy_remove(struct spi_device *spi)
  217. {
  218. struct dummy *p_dummy = dev_get_drvdata(&spi->dev);
  219. dev_info(&spi->dev, "removing dummy SPI device\n");
  220. device_remove_file(&spi->dev, &dev_attr_looptest);
  221. dev_set_drvdata(&spi->dev, NULL);
  222. kfree(p_dummy);
  223. return 0;
  224. }
  225. static const struct of_device_id pl022_dummy_dt_match[] = {
  226. { .compatible = "arm,pl022-dummy" },
  227. {},
  228. };
  229. static struct spi_driver pl022_dummy_driver = {
  230. .driver = {
  231. .name = "spi-dummy",
  232. .of_match_table = pl022_dummy_dt_match,
  233. },
  234. .probe = pl022_dummy_probe,
  235. .remove = pl022_dummy_remove,
  236. };
  237. module_spi_driver(pl022_dummy_driver);
  238. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  239. MODULE_DESCRIPTION("PL022 SSP/SPI DUMMY Linux driver");
  240. MODULE_LICENSE("GPL");