pxa2xx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 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 = CKEN_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/linux/spi/pxa2xx_spi.h". The pxa2xx_spi master controller driver
  82. will uses the configuration whenever the driver communicates with the slave
  83. device. All fields are optional.
  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 = 8,
  97. .rx_threshold = 8,
  98. The range is 1 to 16 where zero indicates "use default".
  99. The "pxa2xx_spi_chip.dma_burst_size" field is used to configure PXA2xx DMA
  100. engine and is related the "spi_device.bits_per_word" field. Read and understand
  101. the PXA2xx "Developer Manual" sections on the DMA controller and SSP Controllers
  102. to determine the correct value. An SSP configured for byte-wide transfers would
  103. use a value of 8. The driver will determine a reasonable default if
  104. dma_burst_size == 0.
  105. The "pxa2xx_spi_chip.timeout" fields is used to efficiently handle
  106. trailing bytes in the SSP receiver fifo. The correct value for this field is
  107. dependent on the SPI bus speed ("spi_board_info.max_speed_hz") and the specific
  108. slave device. Please note that the PXA2xx SSP 1 does not support trailing byte
  109. timeouts and must busy-wait any trailing bytes.
  110. The "pxa2xx_spi_chip.enable_loopback" field is used to place the SSP porting
  111. into internal loopback mode. In this mode the SSP controller internally
  112. connects the SSPTX pin to the SSPRX pin. This is useful for initial setup
  113. testing.
  114. The "pxa2xx_spi_chip.cs_control" field is used to point to a board specific
  115. function for asserting/deasserting a slave device chip select. If the field is
  116. NULL, the pxa2xx_spi master controller driver assumes that the SSP port is
  117. configured to use SSPFRM instead.
  118. NOTE: the SPI driver cannot control the chip select if SSPFRM is used, so the
  119. chipselect is dropped after each spi_transfer. Most devices need chip select
  120. asserted around the complete message. Use SSPFRM as a GPIO (through cs_control)
  121. to accommodate these chips.
  122. NSSP SLAVE SAMPLE
  123. -----------------
  124. The pxa2xx_spi_chip structure is passed to the pxa2xx_spi driver in the
  125. "spi_board_info.controller_data" field. Below is a sample configuration using
  126. the PXA255 NSSP.
  127. /* Chip Select control for the CS8415A SPI slave device */
  128. static void cs8415a_cs_control(u32 command)
  129. {
  130. if (command & PXA2XX_CS_ASSERT)
  131. GPCR(2) = GPIO_bit(2);
  132. else
  133. GPSR(2) = GPIO_bit(2);
  134. }
  135. /* Chip Select control for the CS8405A SPI slave device */
  136. static void cs8405a_cs_control(u32 command)
  137. {
  138. if (command & PXA2XX_CS_ASSERT)
  139. GPCR(3) = GPIO_bit(3);
  140. else
  141. GPSR(3) = GPIO_bit(3);
  142. }
  143. static struct pxa2xx_spi_chip cs8415a_chip_info = {
  144. .tx_threshold = 8, /* SSP hardward FIFO threshold */
  145. .rx_threshold = 8, /* SSP hardward FIFO threshold */
  146. .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
  147. .timeout = 235, /* See Intel documentation */
  148. .cs_control = cs8415a_cs_control, /* Use external chip select */
  149. };
  150. static struct pxa2xx_spi_chip cs8405a_chip_info = {
  151. .tx_threshold = 8, /* SSP hardward FIFO threshold */
  152. .rx_threshold = 8, /* SSP hardward FIFO threshold */
  153. .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
  154. .timeout = 235, /* See Intel documentation */
  155. .cs_control = cs8405a_cs_control, /* Use external chip select */
  156. };
  157. static struct spi_board_info streetracer_spi_board_info[] __initdata = {
  158. {
  159. .modalias = "cs8415a", /* 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 = 0, /* Framework chip select */
  163. .platform_data = NULL; /* No spi_driver specific config */
  164. .controller_data = &cs8415a_chip_info, /* Master chip config */
  165. .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
  166. },
  167. {
  168. .modalias = "cs8405a", /* Name of spi_driver for this device */
  169. .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
  170. .bus_num = 2, /* Framework bus number */
  171. .chip_select = 1, /* Framework chip select */
  172. .controller_data = &cs8405a_chip_info, /* Master chip config */
  173. .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
  174. },
  175. };
  176. static void __init streetracer_init(void)
  177. {
  178. spi_register_board_info(streetracer_spi_board_info,
  179. ARRAY_SIZE(streetracer_spi_board_info));
  180. }
  181. DMA and PIO I/O Support
  182. -----------------------
  183. The pxa2xx_spi driver supports both DMA and interrupt driven PIO message
  184. transfers. The driver defaults to PIO mode and DMA transfers must be enabled
  185. by setting the "enable_dma" flag in the "pxa2xx_spi_master" structure. The DMA
  186. mode supports both coherent and stream based DMA mappings.
  187. The following logic is used to determine the type of I/O to be used on
  188. a per "spi_transfer" basis:
  189. if !enable_dma then
  190. always use PIO transfers
  191. if spi_message.len > 8191 then
  192. print "rate limited" warning
  193. use PIO transfers
  194. if spi_message.is_dma_mapped and rx_dma_buf != 0 and tx_dma_buf != 0 then
  195. use coherent DMA mode
  196. if rx_buf and tx_buf are aligned on 8 byte boundary then
  197. use streaming DMA mode
  198. otherwise
  199. use PIO transfer
  200. THANKS TO
  201. ---------
  202. David Brownell and others for mentoring the development of this driver.