blob: 882d3cc72cc2b1598d603893c0e3d47a2f931785 [file] [log] [blame]
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -03001==============================
Jan Engelhardt96de0e22007-10-19 23:21:04 +02002PXA2xx SPI on SSP driver HOWTO
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -03003==============================
4
Stephen Streete0c99052006-03-07 23:53:24 -08005This a mini howto on the pxa2xx_spi driver. The driver turns a PXA2xx
6synchronous serial port into a SPI master controller
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -03007(see Documentation/spi/spi-summary.rst). The driver has the following features
Stephen Streete0c99052006-03-07 23:53:24 -08008
9- Support for any PXA2xx SSP
10- SSP PIO and SSP DMA data transfers.
11- External and Internal (SSPFRM) chip selects.
12- Per slave device (chip) configuration.
13- Full suspend, freeze, resume support.
14
15The driver is built around a "spi_message" fifo serviced by workqueue and a
16tasklet. The workqueue, "pump_messages", drives message fifo and the tasklet
17(pump_transfer) is responsible for queuing SPI transactions and setting up and
18launching the dma/interrupt driven transfers.
19
20Declaring PXA2xx Master Controllers
21-----------------------------------
22Typically a SPI master is defined in the arch/.../mach-*/board-*.c as a
23"platform device". The master configuration is passed to the driver via a table
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030024found in include/linux/spi/pxa2xx_spi.h::
Stephen Streete0c99052006-03-07 23:53:24 -080025
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030026 struct pxa2xx_spi_controller {
Stephen Streete0c99052006-03-07 23:53:24 -080027 u16 num_chipselect;
28 u8 enable_dma;
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030029 };
Stephen Streete0c99052006-03-07 23:53:24 -080030
Lubomir Rintel51eea522019-01-16 16:13:31 +010031The "pxa2xx_spi_controller.num_chipselect" field is used to determine the number of
Stephen Streete0c99052006-03-07 23:53:24 -080032slave device (chips) attached to this SPI master.
33
Lubomir Rintel51eea522019-01-16 16:13:31 +010034The "pxa2xx_spi_controller.enable_dma" field informs the driver that SSP DMA should
Stephen Streete0c99052006-03-07 23:53:24 -080035be used. This caused the driver to acquire two DMA channels: rx_channel and
36tx_channel. The rx_channel has a higher DMA service priority the tx_channel.
37See the "PXA2xx Developer Manual" section "DMA Controller".
38
39NSSP MASTER SAMPLE
40------------------
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030041Below is a sample configuration using the PXA255 NSSP::
Stephen Streete0c99052006-03-07 23:53:24 -080042
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030043 static struct resource pxa_spi_nssp_resources[] = {
Stephen Streete0c99052006-03-07 23:53:24 -080044 [0] = {
45 .start = __PREG(SSCR0_P(2)), /* Start address of NSSP */
46 .end = __PREG(SSCR0_P(2)) + 0x2c, /* Range of registers */
47 .flags = IORESOURCE_MEM,
48 },
49 [1] = {
50 .start = IRQ_NSSP, /* NSSP IRQ */
51 .end = IRQ_NSSP,
52 .flags = IORESOURCE_IRQ,
53 },
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030054 };
Stephen Streete0c99052006-03-07 23:53:24 -080055
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030056 static struct pxa2xx_spi_controller pxa_nssp_master_info = {
Stephen Streete0c99052006-03-07 23:53:24 -080057 .num_chipselect = 1, /* Matches the number of chips attached to NSSP */
58 .enable_dma = 1, /* Enables NSSP DMA */
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030059 };
Stephen Streete0c99052006-03-07 23:53:24 -080060
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030061 static struct platform_device pxa_spi_nssp = {
Stephen Streete0c99052006-03-07 23:53:24 -080062 .name = "pxa2xx-spi", /* MUST BE THIS VALUE, so device match driver */
63 .id = 2, /* Bus number, MUST MATCH SSP number 1..n */
64 .resource = pxa_spi_nssp_resources,
65 .num_resources = ARRAY_SIZE(pxa_spi_nssp_resources),
66 .dev = {
67 .platform_data = &pxa_nssp_master_info, /* Passed to driver */
68 },
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030069 };
Stephen Streete0c99052006-03-07 23:53:24 -080070
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030071 static struct platform_device *devices[] __initdata = {
Stephen Streete0c99052006-03-07 23:53:24 -080072 &pxa_spi_nssp,
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030073 };
Stephen Streete0c99052006-03-07 23:53:24 -080074
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030075 static void __init board_init(void)
76 {
Stephen Streete0c99052006-03-07 23:53:24 -080077 (void)platform_add_device(devices, ARRAY_SIZE(devices));
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030078 }
Stephen Streete0c99052006-03-07 23:53:24 -080079
80Declaring Slave Devices
81-----------------------
82Typically each SPI slave (chip) is defined in the arch/.../mach-*/board-*.c
83using the "spi_board_info" structure found in "linux/spi/spi.h". See
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030084"Documentation/spi/spi-summary.rst" for additional information.
Stephen Streete0c99052006-03-07 23:53:24 -080085
86Each slave device attached to the PXA must provide slave specific configuration
87information via the structure "pxa2xx_spi_chip" found in
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080088"include/linux/spi/pxa2xx_spi.h". The pxa2xx_spi master controller driver
Stephen Streete0c99052006-03-07 23:53:24 -080089will uses the configuration whenever the driver communicates with the slave
Vernon Sauderf1f640a2008-10-15 22:02:43 -070090device. All fields are optional.
Stephen Streete0c99052006-03-07 23:53:24 -080091
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -030092::
93
94 struct pxa2xx_spi_chip {
Stephen Streete0c99052006-03-07 23:53:24 -080095 u8 tx_threshold;
96 u8 rx_threshold;
97 u8 dma_burst_size;
Stephen Street8d94cc52006-12-10 02:18:54 -080098 u32 timeout;
Stephen Streete0c99052006-03-07 23:53:24 -080099 u8 enable_loopback;
100 void (*cs_control)(u32 command);
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300101 };
Stephen Streete0c99052006-03-07 23:53:24 -0800102
103The "pxa2xx_spi_chip.tx_threshold" and "pxa2xx_spi_chip.rx_threshold" fields are
104used to configure the SSP hardware fifo. These fields are critical to the
105performance of pxa2xx_spi driver and misconfiguration will result in rx
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300106fifo overruns (especially in PIO mode transfers). Good default values are::
Stephen Streete0c99052006-03-07 23:53:24 -0800107
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700108 .tx_threshold = 8,
109 .rx_threshold = 8,
110
111The range is 1 to 16 where zero indicates "use default".
Stephen Streete0c99052006-03-07 23:53:24 -0800112
113The "pxa2xx_spi_chip.dma_burst_size" field is used to configure PXA2xx DMA
114engine and is related the "spi_device.bits_per_word" field. Read and understand
115the PXA2xx "Developer Manual" sections on the DMA controller and SSP Controllers
116to determine the correct value. An SSP configured for byte-wide transfers would
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700117use a value of 8. The driver will determine a reasonable default if
118dma_burst_size == 0.
Stephen Streete0c99052006-03-07 23:53:24 -0800119
Stephen Street8d94cc52006-12-10 02:18:54 -0800120The "pxa2xx_spi_chip.timeout" fields is used to efficiently handle
Stephen Streete0c99052006-03-07 23:53:24 -0800121trailing bytes in the SSP receiver fifo. The correct value for this field is
122dependent on the SPI bus speed ("spi_board_info.max_speed_hz") and the specific
Paolo Ornati670e9f32006-10-03 22:57:56 +0200123slave device. Please note that the PXA2xx SSP 1 does not support trailing byte
Stephen Streete0c99052006-03-07 23:53:24 -0800124timeouts and must busy-wait any trailing bytes.
125
126The "pxa2xx_spi_chip.enable_loopback" field is used to place the SSP porting
127into internal loopback mode. In this mode the SSP controller internally
Paolo Ornati670e9f32006-10-03 22:57:56 +0200128connects the SSPTX pin to the SSPRX pin. This is useful for initial setup
Stephen Streete0c99052006-03-07 23:53:24 -0800129testing.
130
131The "pxa2xx_spi_chip.cs_control" field is used to point to a board specific
132function for asserting/deasserting a slave device chip select. If the field is
133NULL, the pxa2xx_spi master controller driver assumes that the SSP port is
134configured to use SSPFRM instead.
135
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700136NOTE: the SPI driver cannot control the chip select if SSPFRM is used, so the
137chipselect is dropped after each spi_transfer. Most devices need chip select
138asserted around the complete message. Use SSPFRM as a GPIO (through cs_control)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300139to accommodate these chips.
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700140
141
142NSSP SLAVE SAMPLE
Stephen Streete0c99052006-03-07 23:53:24 -0800143-----------------
144The pxa2xx_spi_chip structure is passed to the pxa2xx_spi driver in the
145"spi_board_info.controller_data" field. Below is a sample configuration using
146the PXA255 NSSP.
147
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300148::
149
150 /* Chip Select control for the CS8415A SPI slave device */
151 static void cs8415a_cs_control(u32 command)
152 {
Stephen Streete0c99052006-03-07 23:53:24 -0800153 if (command & PXA2XX_CS_ASSERT)
154 GPCR(2) = GPIO_bit(2);
155 else
156 GPSR(2) = GPIO_bit(2);
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300157 }
Stephen Streete0c99052006-03-07 23:53:24 -0800158
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300159 /* Chip Select control for the CS8405A SPI slave device */
160 static void cs8405a_cs_control(u32 command)
161 {
Stephen Streete0c99052006-03-07 23:53:24 -0800162 if (command & PXA2XX_CS_ASSERT)
163 GPCR(3) = GPIO_bit(3);
164 else
165 GPSR(3) = GPIO_bit(3);
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300166 }
Stephen Streete0c99052006-03-07 23:53:24 -0800167
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300168 static struct pxa2xx_spi_chip cs8415a_chip_info = {
Stephen Street8d94cc52006-12-10 02:18:54 -0800169 .tx_threshold = 8, /* SSP hardward FIFO threshold */
170 .rx_threshold = 8, /* SSP hardward FIFO threshold */
Stephen Streete0c99052006-03-07 23:53:24 -0800171 .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
Stephen Street8d94cc52006-12-10 02:18:54 -0800172 .timeout = 235, /* See Intel documentation */
Stephen Streete0c99052006-03-07 23:53:24 -0800173 .cs_control = cs8415a_cs_control, /* Use external chip select */
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300174 };
Stephen Streete0c99052006-03-07 23:53:24 -0800175
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300176 static struct pxa2xx_spi_chip cs8405a_chip_info = {
Stephen Street8d94cc52006-12-10 02:18:54 -0800177 .tx_threshold = 8, /* SSP hardward FIFO threshold */
178 .rx_threshold = 8, /* SSP hardward FIFO threshold */
Stephen Streete0c99052006-03-07 23:53:24 -0800179 .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
Stephen Street8d94cc52006-12-10 02:18:54 -0800180 .timeout = 235, /* See Intel documentation */
Stephen Streete0c99052006-03-07 23:53:24 -0800181 .cs_control = cs8405a_cs_control, /* Use external chip select */
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300182 };
Stephen Streete0c99052006-03-07 23:53:24 -0800183
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300184 static struct spi_board_info streetracer_spi_board_info[] __initdata = {
Stephen Streete0c99052006-03-07 23:53:24 -0800185 {
186 .modalias = "cs8415a", /* Name of spi_driver for this device */
187 .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
188 .bus_num = 2, /* Framework bus number */
189 .chip_select = 0, /* Framework chip select */
190 .platform_data = NULL; /* No spi_driver specific config */
191 .controller_data = &cs8415a_chip_info, /* Master chip config */
192 .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
193 },
194 {
195 .modalias = "cs8405a", /* Name of spi_driver for this device */
196 .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
197 .bus_num = 2, /* Framework bus number */
198 .chip_select = 1, /* Framework chip select */
199 .controller_data = &cs8405a_chip_info, /* Master chip config */
200 .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
201 },
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300202 };
Stephen Streete0c99052006-03-07 23:53:24 -0800203
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300204 static void __init streetracer_init(void)
205 {
Stephen Streete0c99052006-03-07 23:53:24 -0800206 spi_register_board_info(streetracer_spi_board_info,
207 ARRAY_SIZE(streetracer_spi_board_info));
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300208 }
Stephen Streete0c99052006-03-07 23:53:24 -0800209
210
211DMA and PIO I/O Support
212-----------------------
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700213The pxa2xx_spi driver supports both DMA and interrupt driven PIO message
214transfers. The driver defaults to PIO mode and DMA transfers must be enabled
Lubomir Rintel51eea522019-01-16 16:13:31 +0100215by setting the "enable_dma" flag in the "pxa2xx_spi_controller" structure. The DMA
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700216mode supports both coherent and stream based DMA mappings.
Stephen Streete0c99052006-03-07 23:53:24 -0800217
218The following logic is used to determine the type of I/O to be used on
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300219a per "spi_transfer" basis::
Stephen Streete0c99052006-03-07 23:53:24 -0800220
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300221 if !enable_dma then
Stephen Streete0c99052006-03-07 23:53:24 -0800222 always use PIO transfers
223
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300224 if spi_message.len > 8191 then
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700225 print "rate limited" warning
226 use PIO transfers
227
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300228 if spi_message.is_dma_mapped and rx_dma_buf != 0 and tx_dma_buf != 0 then
Stephen Streete0c99052006-03-07 23:53:24 -0800229 use coherent DMA mode
230
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300231 if rx_buf and tx_buf are aligned on 8 byte boundary then
Stephen Streete0c99052006-03-07 23:53:24 -0800232 use streaming DMA mode
233
Mauro Carvalho Chehab9cdd2732019-07-31 17:08:50 -0300234 otherwise
Stephen Streete0c99052006-03-07 23:53:24 -0800235 use PIO transfer
236
237THANKS TO
238---------
239
240David Brownell and others for mentoring the development of this driver.