pxa2xx 8.3 KB

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