pxa2xx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. PXA2xx SPI on SSP driver HOWTO
  2. ===================================================
  3. This a mini howto on the pxa2xx_spi driver. The driver turns a PXA2xx
  4. synchronous serial port into a SPI master controller
  5. (see Documentation/spi/spi-summary). The driver has the following features
  6. - Support for any PXA2xx SSP
  7. - SSP PIO and SSP DMA data transfers.
  8. - External and Internal (SSPFRM) chip selects.
  9. - Per slave device (chip) configuration.
  10. - Full suspend, freeze, resume support.
  11. The driver is built around a "spi_message" fifo serviced by workqueue and a
  12. tasklet. The workqueue, "pump_messages", drives message fifo and the tasklet
  13. (pump_transfer) is responsible for queuing SPI transactions and setting up and
  14. launching the dma/interrupt driven transfers.
  15. Declaring PXA2xx Master Controllers
  16. -----------------------------------
  17. Typically a SPI master is defined in the arch/.../mach-*/board-*.c as a
  18. "platform device". The master configuration is passed to the driver via a table
  19. found in include/linux/spi/pxa2xx_spi.h:
  20. struct pxa2xx_spi_master {
  21. u32 clock_enable;
  22. u16 num_chipselect;
  23. u8 enable_dma;
  24. };
  25. The "pxa2xx_spi_master.clock_enable" field is used to enable/disable the
  26. corresponding SSP peripheral block in the "Clock Enable Register (CKEN"). See
  27. the "PXA2xx Developer Manual" section "Clocks and Power Management".
  28. The "pxa2xx_spi_master.num_chipselect" field is used to determine the number of
  29. slave device (chips) attached to this SPI master.
  30. The "pxa2xx_spi_master.enable_dma" field informs the driver that SSP DMA should
  31. be used. This caused the driver to acquire two DMA channels: rx_channel and
  32. tx_channel. The rx_channel has a higher DMA service priority the tx_channel.
  33. See the "PXA2xx Developer Manual" section "DMA Controller".
  34. NSSP MASTER SAMPLE
  35. ------------------
  36. Below is a sample configuration using the PXA255 NSSP.
  37. static struct resource pxa_spi_nssp_resources[] = {
  38. [0] = {
  39. .start = __PREG(SSCR0_P(2)), /* Start address of NSSP */
  40. .end = __PREG(SSCR0_P(2)) + 0x2c, /* Range of registers */
  41. .flags = IORESOURCE_MEM,
  42. },
  43. [1] = {
  44. .start = IRQ_NSSP, /* NSSP IRQ */
  45. .end = IRQ_NSSP,
  46. .flags = IORESOURCE_IRQ,
  47. },
  48. };
  49. static struct pxa2xx_spi_master pxa_nssp_master_info = {
  50. .clock_enable = CKEN_NSSP, /* NSSP Peripheral clock */
  51. .num_chipselect = 1, /* Matches the number of chips attached to NSSP */
  52. .enable_dma = 1, /* Enables NSSP DMA */
  53. };
  54. static struct platform_device pxa_spi_nssp = {
  55. .name = "pxa2xx-spi", /* MUST BE THIS VALUE, so device match driver */
  56. .id = 2, /* Bus number, MUST MATCH SSP number 1..n */
  57. .resource = pxa_spi_nssp_resources,
  58. .num_resources = ARRAY_SIZE(pxa_spi_nssp_resources),
  59. .dev = {
  60. .platform_data = &pxa_nssp_master_info, /* Passed to driver */
  61. },
  62. };
  63. static struct platform_device *devices[] __initdata = {
  64. &pxa_spi_nssp,
  65. };
  66. static void __init board_init(void)
  67. {
  68. (void)platform_add_device(devices, ARRAY_SIZE(devices));
  69. }
  70. Declaring Slave Devices
  71. -----------------------
  72. Typically each SPI slave (chip) is defined in the arch/.../mach-*/board-*.c
  73. using the "spi_board_info" structure found in "linux/spi/spi.h". See
  74. "Documentation/spi/spi-summary" for additional information.
  75. Each slave device attached to the PXA must provide slave specific configuration
  76. information via the structure "pxa2xx_spi_chip" found in
  77. "include/linux/spi/pxa2xx_spi.h". The pxa2xx_spi master controller driver
  78. will uses the configuration whenever the driver communicates with the slave
  79. device. All fields are optional.
  80. struct pxa2xx_spi_chip {
  81. u8 tx_threshold;
  82. u8 rx_threshold;
  83. u8 dma_burst_size;
  84. u32 timeout;
  85. u8 enable_loopback;
  86. void (*cs_control)(u32 command);
  87. };
  88. The "pxa2xx_spi_chip.tx_threshold" and "pxa2xx_spi_chip.rx_threshold" fields are
  89. used to configure the SSP hardware fifo. These fields are critical to the
  90. performance of pxa2xx_spi driver and misconfiguration will result in rx
  91. fifo overruns (especially in PIO mode transfers). Good default values are
  92. .tx_threshold = 8,
  93. .rx_threshold = 8,
  94. The range is 1 to 16 where zero indicates "use default".
  95. The "pxa2xx_spi_chip.dma_burst_size" field is used to configure PXA2xx DMA
  96. engine and is related the "spi_device.bits_per_word" field. Read and understand
  97. the PXA2xx "Developer Manual" sections on the DMA controller and SSP Controllers
  98. to determine the correct value. An SSP configured for byte-wide transfers would
  99. use a value of 8. The driver will determine a reasonable default if
  100. dma_burst_size == 0.
  101. The "pxa2xx_spi_chip.timeout" fields is used to efficiently handle
  102. trailing bytes in the SSP receiver fifo. The correct value for this field is
  103. dependent on the SPI bus speed ("spi_board_info.max_speed_hz") and the specific
  104. slave device. Please note that the PXA2xx SSP 1 does not support trailing byte
  105. timeouts and must busy-wait any trailing bytes.
  106. The "pxa2xx_spi_chip.enable_loopback" field is used to place the SSP porting
  107. into internal loopback mode. In this mode the SSP controller internally
  108. connects the SSPTX pin to the SSPRX pin. This is useful for initial setup
  109. testing.
  110. The "pxa2xx_spi_chip.cs_control" field is used to point to a board specific
  111. function for asserting/deasserting a slave device chip select. If the field is
  112. NULL, the pxa2xx_spi master controller driver assumes that the SSP port is
  113. configured to use SSPFRM instead.
  114. NOTE: the SPI driver cannot control the chip select if SSPFRM is used, so the
  115. chipselect is dropped after each spi_transfer. Most devices need chip select
  116. asserted around the complete message. Use SSPFRM as a GPIO (through cs_control)
  117. to accommodate these chips.
  118. NSSP SLAVE SAMPLE
  119. -----------------
  120. The pxa2xx_spi_chip structure is passed to the pxa2xx_spi driver in the
  121. "spi_board_info.controller_data" field. Below is a sample configuration using
  122. the PXA255 NSSP.
  123. /* Chip Select control for the CS8415A SPI slave device */
  124. static void cs8415a_cs_control(u32 command)
  125. {
  126. if (command & PXA2XX_CS_ASSERT)
  127. GPCR(2) = GPIO_bit(2);
  128. else
  129. GPSR(2) = GPIO_bit(2);
  130. }
  131. /* Chip Select control for the CS8405A SPI slave device */
  132. static void cs8405a_cs_control(u32 command)
  133. {
  134. if (command & PXA2XX_CS_ASSERT)
  135. GPCR(3) = GPIO_bit(3);
  136. else
  137. GPSR(3) = GPIO_bit(3);
  138. }
  139. static struct pxa2xx_spi_chip cs8415a_chip_info = {
  140. .tx_threshold = 8, /* SSP hardward FIFO threshold */
  141. .rx_threshold = 8, /* SSP hardward FIFO threshold */
  142. .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
  143. .timeout = 235, /* See Intel documentation */
  144. .cs_control = cs8415a_cs_control, /* Use external chip select */
  145. };
  146. static struct pxa2xx_spi_chip cs8405a_chip_info = {
  147. .tx_threshold = 8, /* SSP hardward FIFO threshold */
  148. .rx_threshold = 8, /* SSP hardward FIFO threshold */
  149. .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
  150. .timeout = 235, /* See Intel documentation */
  151. .cs_control = cs8405a_cs_control, /* Use external chip select */
  152. };
  153. static struct spi_board_info streetracer_spi_board_info[] __initdata = {
  154. {
  155. .modalias = "cs8415a", /* Name of spi_driver for this device */
  156. .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
  157. .bus_num = 2, /* Framework bus number */
  158. .chip_select = 0, /* Framework chip select */
  159. .platform_data = NULL; /* No spi_driver specific config */
  160. .controller_data = &cs8415a_chip_info, /* Master chip config */
  161. .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
  162. },
  163. {
  164. .modalias = "cs8405a", /* Name of spi_driver for this device */
  165. .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
  166. .bus_num = 2, /* Framework bus number */
  167. .chip_select = 1, /* Framework chip select */
  168. .controller_data = &cs8405a_chip_info, /* Master chip config */
  169. .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
  170. },
  171. };
  172. static void __init streetracer_init(void)
  173. {
  174. spi_register_board_info(streetracer_spi_board_info,
  175. ARRAY_SIZE(streetracer_spi_board_info));
  176. }
  177. DMA and PIO I/O Support
  178. -----------------------
  179. The pxa2xx_spi driver supports both DMA and interrupt driven PIO message
  180. transfers. The driver defaults to PIO mode and DMA transfers must be enabled
  181. by setting the "enable_dma" flag in the "pxa2xx_spi_master" structure. The DMA
  182. mode supports both coherent and stream based DMA mappings.
  183. The following logic is used to determine the type of I/O to be used on
  184. a per "spi_transfer" basis:
  185. if !enable_dma then
  186. always use PIO transfers
  187. if spi_message.len > 8191 then
  188. print "rate limited" warning
  189. use PIO transfers
  190. if spi_message.is_dma_mapped and rx_dma_buf != 0 and tx_dma_buf != 0 then
  191. use coherent DMA mode
  192. if rx_buf and tx_buf are aligned on 8 byte boundary then
  193. use streaming DMA mode
  194. otherwise
  195. use PIO transfer
  196. THANKS TO
  197. ---------
  198. David Brownell and others for mentoring the development of this driver.