pxa2xx 8.2 KB

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