spi_porting.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. - CAIF SPI porting -
  2. - CAIF SPI basics:
  3. Running CAIF over SPI needs some extra setup, owing to the nature of SPI.
  4. Two extra GPIOs have been added in order to negotiate the transfers
  5. between the master and the slave. The minimum requirement for running
  6. CAIF over SPI is a SPI slave chip and two GPIOs (more details below).
  7. Please note that running as a slave implies that you need to keep up
  8. with the master clock. An overrun or underrun event is fatal.
  9. - CAIF SPI framework:
  10. To make porting as easy as possible, the CAIF SPI has been divided in
  11. two parts. The first part (called the interface part) deals with all
  12. generic functionality such as length framing, SPI frame negotiation
  13. and SPI frame delivery and transmission. The other part is the CAIF
  14. SPI slave device part, which is the module that you have to write if
  15. you want to run SPI CAIF on a new hardware. This part takes care of
  16. the physical hardware, both with regard to SPI and to GPIOs.
  17. - Implementing a CAIF SPI device:
  18. - Functionality provided by the CAIF SPI slave device:
  19. In order to implement a SPI device you will, as a minimum,
  20. need to implement the following
  21. functions:
  22. int (*init_xfer) (struct cfspi_xfer * xfer, struct cfspi_dev *dev):
  23. This function is called by the CAIF SPI interface to give
  24. you a chance to set up your hardware to be ready to receive
  25. a stream of data from the master. The xfer structure contains
  26. both physical and logical addresses, as well as the total length
  27. of the transfer in both directions.The dev parameter can be used
  28. to map to different CAIF SPI slave devices.
  29. void (*sig_xfer) (bool xfer, struct cfspi_dev *dev):
  30. This function is called by the CAIF SPI interface when the output
  31. (SPI_INT) GPIO needs to change state. The boolean value of the xfer
  32. variable indicates whether the GPIO should be asserted (HIGH) or
  33. deasserted (LOW). The dev parameter can be used to map to different CAIF
  34. SPI slave devices.
  35. - Functionality provided by the CAIF SPI interface:
  36. void (*ss_cb) (bool assert, struct cfspi_ifc *ifc);
  37. This function is called by the CAIF SPI slave device in order to
  38. signal a change of state of the input GPIO (SS) to the interface.
  39. Only active edges are mandatory to be reported.
  40. This function can be called from IRQ context (recommended in order
  41. not to introduce latency). The ifc parameter should be the pointer
  42. returned from the platform probe function in the SPI device structure.
  43. void (*xfer_done_cb) (struct cfspi_ifc *ifc);
  44. This function is called by the CAIF SPI slave device in order to
  45. report that a transfer is completed. This function should only be
  46. called once both the transmission and the reception are completed.
  47. This function can be called from IRQ context (recommended in order
  48. not to introduce latency). The ifc parameter should be the pointer
  49. returned from the platform probe function in the SPI device structure.
  50. - Connecting the bits and pieces:
  51. - Filling in the SPI slave device structure:
  52. Connect the necessary callback functions.
  53. Indicate clock speed (used to calculate toggle delays).
  54. Chose a suitable name (helps debugging if you use several CAIF
  55. SPI slave devices).
  56. Assign your private data (can be used to map to your structure).
  57. - Filling in the SPI slave platform device structure:
  58. Add name of driver to connect to ("cfspi_sspi").
  59. Assign the SPI slave device structure as platform data.
  60. - Padding:
  61. In order to optimize throughput, a number of SPI padding options are provided.
  62. Padding can be enabled independently for uplink and downlink transfers.
  63. Padding can be enabled for the head, the tail and for the total frame size.
  64. The padding needs to be correctly configured on both sides of the link.
  65. The padding can be changed via module parameters in cfspi_sspi.c or via
  66. the sysfs directory of the cfspi_sspi driver (before device registration).
  67. - CAIF SPI device template:
  68. /*
  69. * Copyright (C) ST-Ericsson AB 2010
  70. * Author: Daniel Martensson / Daniel.Martensson@stericsson.com
  71. * License terms: GNU General Public License (GPL), version 2.
  72. *
  73. */
  74. #include <linux/init.h>
  75. #include <linux/module.h>
  76. #include <linux/device.h>
  77. #include <linux/wait.h>
  78. #include <linux/interrupt.h>
  79. #include <linux/dma-mapping.h>
  80. #include <net/caif/caif_spi.h>
  81. MODULE_LICENSE("GPL");
  82. struct sspi_struct {
  83. struct cfspi_dev sdev;
  84. struct cfspi_xfer *xfer;
  85. };
  86. static struct sspi_struct slave;
  87. static struct platform_device slave_device;
  88. static irqreturn_t sspi_irq(int irq, void *arg)
  89. {
  90. /* You only need to trigger on an edge to the active state of the
  91. * SS signal. Once a edge is detected, the ss_cb() function should be
  92. * called with the parameter assert set to true. It is OK
  93. * (and even advised) to call the ss_cb() function in IRQ context in
  94. * order not to add any delay. */
  95. return IRQ_HANDLED;
  96. }
  97. static void sspi_complete(void *context)
  98. {
  99. /* Normally the DMA or the SPI framework will call you back
  100. * in something similar to this. The only thing you need to
  101. * do is to call the xfer_done_cb() function, providing the pointer
  102. * to the CAIF SPI interface. It is OK to call this function
  103. * from IRQ context. */
  104. }
  105. static int sspi_init_xfer(struct cfspi_xfer *xfer, struct cfspi_dev *dev)
  106. {
  107. /* Store transfer info. For a normal implementation you should
  108. * set up your DMA here and make sure that you are ready to
  109. * receive the data from the master SPI. */
  110. struct sspi_struct *sspi = (struct sspi_struct *)dev->priv;
  111. sspi->xfer = xfer;
  112. return 0;
  113. }
  114. void sspi_sig_xfer(bool xfer, struct cfspi_dev *dev)
  115. {
  116. /* If xfer is true then you should assert the SPI_INT to indicate to
  117. * the master that you are ready to receive the data from the master
  118. * SPI. If xfer is false then you should de-assert SPI_INT to indicate
  119. * that the transfer is done.
  120. */
  121. struct sspi_struct *sspi = (struct sspi_struct *)dev->priv;
  122. }
  123. static void sspi_release(struct device *dev)
  124. {
  125. /*
  126. * Here you should release your SPI device resources.
  127. */
  128. }
  129. static int __init sspi_init(void)
  130. {
  131. /* Here you should initialize your SPI device by providing the
  132. * necessary functions, clock speed, name and private data. Once
  133. * done, you can register your device with the
  134. * platform_device_register() function. This function will return
  135. * with the CAIF SPI interface initialized. This is probably also
  136. * the place where you should set up your GPIOs, interrupts and SPI
  137. * resources. */
  138. int res = 0;
  139. /* Initialize slave device. */
  140. slave.sdev.init_xfer = sspi_init_xfer;
  141. slave.sdev.sig_xfer = sspi_sig_xfer;
  142. slave.sdev.clk_mhz = 13;
  143. slave.sdev.priv = &slave;
  144. slave.sdev.name = "spi_sspi";
  145. slave_device.dev.release = sspi_release;
  146. /* Initialize platform device. */
  147. slave_device.name = "cfspi_sspi";
  148. slave_device.dev.platform_data = &slave.sdev;
  149. /* Register platform device. */
  150. res = platform_device_register(&slave_device);
  151. if (res) {
  152. printk(KERN_WARNING "sspi_init: failed to register dev.\n");
  153. return -ENODEV;
  154. }
  155. return res;
  156. }
  157. static void __exit sspi_exit(void)
  158. {
  159. platform_device_del(&slave_device);
  160. }
  161. module_init(sspi_init);
  162. module_exit(sspi_exit);