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