aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@g5.osdl.org>2006-05-16 18:18:26 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-05-16 18:18:26 -0400
commit0c056c50a6218e0e577817c16ba8851af593d742 (patch)
treefeabddbd93b49ce94103c6054336078f240848ee
parent4fbca5320eb102d2e15bdeffe79e125c11cf925e (diff)
parent1e316d7566b63767aa18902235c719e9e95465d0 (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/spi-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/spi-2.6: [PATCH] SPI: spi_bitbang: clocking fixes [PATCH] spi: Update to PXA2xx SPI Driver [PATCH] SPI: busnum == 0 needs to work [PATCH] SPI: devices can require LSB-first encodings [PATCH] SPI: Renamed bitbang_transfer_setup to spi_bitbang_setup_transfer and export it [PATCH] SPI: Add David as the SPI subsystem maintainer [PATCH] SPI: spi bounce buffer has a minimum length [PATCH] SPI: spi whitespace fixes [PATCH] SPI: add PXA2xx SSP SPI Driver [PATCH] SPI: per-transfer overrides for wordsize and clocking
-rw-r--r--Documentation/spi/pxa2xx234
-rw-r--r--Documentation/spi/spi-summary34
-rw-r--r--MAINTAINERS6
-rw-r--r--drivers/spi/Kconfig8
-rw-r--r--drivers/spi/Makefile1
-rw-r--r--drivers/spi/pxa2xx_spi.c1467
-rw-r--r--drivers/spi/spi.c7
-rw-r--r--drivers/spi/spi_bitbang.c104
-rw-r--r--include/asm-arm/arch-pxa/pxa2xx_spi.h68
-rw-r--r--include/linux/spi/spi.h45
-rw-r--r--include/linux/spi/spi_bitbang.h8
11 files changed, 1941 insertions, 41 deletions
diff --git a/Documentation/spi/pxa2xx b/Documentation/spi/pxa2xx
new file mode 100644
index 00000000000..9c45f3df2e1
--- /dev/null
+++ b/Documentation/spi/pxa2xx
@@ -0,0 +1,234 @@
1PXA2xx SPI on SSP driver HOWTO
2===================================================
3This a mini howto on the pxa2xx_spi driver. The driver turns a PXA2xx
4synchronous serial port into a SPI master controller
5(see Documentation/spi/spi_summary). The driver has the following features
6
7- Support for any PXA2xx SSP
8- SSP PIO and SSP DMA data transfers.
9- External and Internal (SSPFRM) chip selects.
10- Per slave device (chip) configuration.
11- Full suspend, freeze, resume support.
12
13The driver is built around a "spi_message" fifo serviced by workqueue and a
14tasklet. The workqueue, "pump_messages", drives message fifo and the tasklet
15(pump_transfer) is responsible for queuing SPI transactions and setting up and
16launching the dma/interrupt driven transfers.
17
18Declaring PXA2xx Master Controllers
19-----------------------------------
20Typically a SPI master is defined in the arch/.../mach-*/board-*.c as a
21"platform device". The master configuration is passed to the driver via a table
22found in include/asm-arm/arch-pxa/pxa2xx_spi.h:
23
24struct pxa2xx_spi_master {
25 enum pxa_ssp_type ssp_type;
26 u32 clock_enable;
27 u16 num_chipselect;
28 u8 enable_dma;
29};
30
31The "pxa2xx_spi_master.ssp_type" field must have a value between 1 and 3 and
32informs the driver which features a particular SSP supports.
33
34The "pxa2xx_spi_master.clock_enable" field is used to enable/disable the
35corresponding SSP peripheral block in the "Clock Enable Register (CKEN"). See
36the "PXA2xx Developer Manual" section "Clocks and Power Management".
37
38The "pxa2xx_spi_master.num_chipselect" field is used to determine the number of
39slave device (chips) attached to this SPI master.
40
41The "pxa2xx_spi_master.enable_dma" field informs the driver that SSP DMA should
42be used. This caused the driver to acquire two DMA channels: rx_channel and
43tx_channel. The rx_channel has a higher DMA service priority the tx_channel.
44See the "PXA2xx Developer Manual" section "DMA Controller".
45
46NSSP MASTER SAMPLE
47------------------
48Below is a sample configuration using the PXA255 NSSP.
49
50static struct resource pxa_spi_nssp_resources[] = {
51 [0] = {
52 .start = __PREG(SSCR0_P(2)), /* Start address of NSSP */
53 .end = __PREG(SSCR0_P(2)) + 0x2c, /* Range of registers */
54 .flags = IORESOURCE_MEM,
55 },
56 [1] = {
57 .start = IRQ_NSSP, /* NSSP IRQ */
58 .end = IRQ_NSSP,
59 .flags = IORESOURCE_IRQ,
60 },
61};
62
63static struct pxa2xx_spi_master pxa_nssp_master_info = {
64 .ssp_type = PXA25x_NSSP, /* Type of SSP */
65 .clock_enable = CKEN9_NSSP, /* NSSP Peripheral clock */
66 .num_chipselect = 1, /* Matches the number of chips attached to NSSP */
67 .enable_dma = 1, /* Enables NSSP DMA */
68};
69
70static struct platform_device pxa_spi_nssp = {
71 .name = "pxa2xx-spi", /* MUST BE THIS VALUE, so device match driver */
72 .id = 2, /* Bus number, MUST MATCH SSP number 1..n */
73 .resource = pxa_spi_nssp_resources,
74 .num_resources = ARRAY_SIZE(pxa_spi_nssp_resources),
75 .dev = {
76 .platform_data = &pxa_nssp_master_info, /* Passed to driver */
77 },
78};
79
80static struct platform_device *devices[] __initdata = {
81 &pxa_spi_nssp,
82};
83
84static void __init board_init(void)
85{
86 (void)platform_add_device(devices, ARRAY_SIZE(devices));
87}
88
89Declaring Slave Devices
90-----------------------
91Typically each SPI slave (chip) is defined in the arch/.../mach-*/board-*.c
92using the "spi_board_info" structure found in "linux/spi/spi.h". See
93"Documentation/spi/spi_summary" for additional information.
94
95Each slave device attached to the PXA must provide slave specific configuration
96information via the structure "pxa2xx_spi_chip" found in
97"include/asm-arm/arch-pxa/pxa2xx_spi.h". The pxa2xx_spi master controller driver
98will uses the configuration whenever the driver communicates with the slave
99device.
100
101struct pxa2xx_spi_chip {
102 u8 tx_threshold;
103 u8 rx_threshold;
104 u8 dma_burst_size;
105 u32 timeout_microsecs;
106 u8 enable_loopback;
107 void (*cs_control)(u32 command);
108};
109
110The "pxa2xx_spi_chip.tx_threshold" and "pxa2xx_spi_chip.rx_threshold" fields are
111used to configure the SSP hardware fifo. These fields are critical to the
112performance of pxa2xx_spi driver and misconfiguration will result in rx
113fifo overruns (especially in PIO mode transfers). Good default values are
114
115 .tx_threshold = 12,
116 .rx_threshold = 4,
117
118The "pxa2xx_spi_chip.dma_burst_size" field is used to configure PXA2xx DMA
119engine and is related the "spi_device.bits_per_word" field. Read and understand
120the PXA2xx "Developer Manual" sections on the DMA controller and SSP Controllers
121to determine the correct value. An SSP configured for byte-wide transfers would
122use a value of 8.
123
124The "pxa2xx_spi_chip.timeout_microsecs" fields is used to efficiently handle
125trailing bytes in the SSP receiver fifo. The correct value for this field is
126dependent on the SPI bus speed ("spi_board_info.max_speed_hz") and the specific
127slave device. Please note the the PXA2xx SSP 1 does not support trailing byte
128timeouts and must busy-wait any trailing bytes.
129
130The "pxa2xx_spi_chip.enable_loopback" field is used to place the SSP porting
131into internal loopback mode. In this mode the SSP controller internally
132connects the SSPTX pin the the SSPRX pin. This is useful for initial setup
133testing.
134
135The "pxa2xx_spi_chip.cs_control" field is used to point to a board specific
136function for asserting/deasserting a slave device chip select. If the field is
137NULL, the pxa2xx_spi master controller driver assumes that the SSP port is
138configured to use SSPFRM instead.
139
140NSSP SALVE SAMPLE
141-----------------
142The pxa2xx_spi_chip structure is passed to the pxa2xx_spi driver in the
143"spi_board_info.controller_data" field. Below is a sample configuration using
144the PXA255 NSSP.
145
146/* Chip Select control for the CS8415A SPI slave device */
147static void cs8415a_cs_control(u32 command)
148{
149 if (command & PXA2XX_CS_ASSERT)
150 GPCR(2) = GPIO_bit(2);
151 else
152 GPSR(2) = GPIO_bit(2);
153}
154
155/* Chip Select control for the CS8405A SPI slave device */
156static void cs8405a_cs_control(u32 command)
157{
158 if (command & PXA2XX_CS_ASSERT)
159 GPCR(3) = GPIO_bit(3);
160 else
161 GPSR(3) = GPIO_bit(3);
162}
163
164static struct pxa2xx_spi_chip cs8415a_chip_info = {
165 .tx_threshold = 12, /* SSP hardward FIFO threshold */
166 .rx_threshold = 4, /* SSP hardward FIFO threshold */
167 .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
168 .timeout_microsecs = 64, /* Wait at least 64usec to handle trailing */
169 .cs_control = cs8415a_cs_control, /* Use external chip select */
170};
171
172static struct pxa2xx_spi_chip cs8405a_chip_info = {
173 .tx_threshold = 12, /* SSP hardward FIFO threshold */
174 .rx_threshold = 4, /* SSP hardward FIFO threshold */
175 .dma_burst_size = 8, /* Byte wide transfers used so 8 byte bursts */
176 .timeout_microsecs = 64, /* Wait at least 64usec to handle trailing */
177 .cs_control = cs8405a_cs_control, /* Use external chip select */
178};
179
180static struct spi_board_info streetracer_spi_board_info[] __initdata = {
181 {
182 .modalias = "cs8415a", /* Name of spi_driver for this device */
183 .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
184 .bus_num = 2, /* Framework bus number */
185 .chip_select = 0, /* Framework chip select */
186 .platform_data = NULL; /* No spi_driver specific config */
187 .controller_data = &cs8415a_chip_info, /* Master chip config */
188 .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
189 },
190 {
191 .modalias = "cs8405a", /* Name of spi_driver for this device */
192 .max_speed_hz = 3686400, /* Run SSP as fast a possbile */
193 .bus_num = 2, /* Framework bus number */
194 .chip_select = 1, /* Framework chip select */
195 .controller_data = &cs8405a_chip_info, /* Master chip config */
196 .irq = STREETRACER_APCI_IRQ, /* Slave device interrupt */
197 },
198};
199
200static void __init streetracer_init(void)
201{
202 spi_register_board_info(streetracer_spi_board_info,
203 ARRAY_SIZE(streetracer_spi_board_info));
204}
205
206
207DMA and PIO I/O Support
208-----------------------
209The pxa2xx_spi driver support both DMA and interrupt driven PIO message
210transfers. The driver defaults to PIO mode and DMA transfers must enabled by
211setting the "enable_dma" flag in the "pxa2xx_spi_master" structure and and
212ensuring that the "pxa2xx_spi_chip.dma_burst_size" field is non-zero. The DMA
213mode support both coherent and stream based DMA mappings.
214
215The following logic is used to determine the type of I/O to be used on
216a per "spi_transfer" basis:
217
218if !enable_dma or dma_burst_size == 0 then
219 always use PIO transfers
220
221if spi_message.is_dma_mapped and rx_dma_buf != 0 and tx_dma_buf != 0 then
222 use coherent DMA mode
223
224if rx_buf and tx_buf are aligned on 8 byte boundary then
225 use streaming DMA mode
226
227otherwise
228 use PIO transfer
229
230THANKS TO
231---------
232
233David Brownell and others for mentoring the development of this driver.
234
diff --git a/Documentation/spi/spi-summary b/Documentation/spi/spi-summary
index a5ffba33a35..068732d3227 100644
--- a/Documentation/spi/spi-summary
+++ b/Documentation/spi/spi-summary
@@ -414,7 +414,33 @@ to get the driver-private data allocated for that device.
414The driver will initialize the fields of that spi_master, including the 414The driver will initialize the fields of that spi_master, including the
415bus number (maybe the same as the platform device ID) and three methods 415bus number (maybe the same as the platform device ID) and three methods
416used to interact with the SPI core and SPI protocol drivers. It will 416used to interact with the SPI core and SPI protocol drivers. It will
417also initialize its own internal state. 417also initialize its own internal state. (See below about bus numbering
418and those methods.)
419
420After you initialize the spi_master, then use spi_register_master() to
421publish it to the rest of the system. At that time, device nodes for
422the controller and any predeclared spi devices will be made available,
423and the driver model core will take care of binding them to drivers.
424
425If you need to remove your SPI controller driver, spi_unregister_master()
426will reverse the effect of spi_register_master().
427
428
429BUS NUMBERING
430
431Bus numbering is important, since that's how Linux identifies a given
432SPI bus (shared SCK, MOSI, MISO). Valid bus numbers start at zero. On
433SOC systems, the bus numbers should match the numbers defined by the chip
434manufacturer. For example, hardware controller SPI2 would be bus number 2,
435and spi_board_info for devices connected to it would use that number.
436
437If you don't have such hardware-assigned bus number, and for some reason
438you can't just assign them, then provide a negative bus number. That will
439then be replaced by a dynamically assigned number. You'd then need to treat
440this as a non-static configuration (see above).
441
442
443SPI MASTER METHODS
418 444
419 master->setup(struct spi_device *spi) 445 master->setup(struct spi_device *spi)
420 This sets up the device clock rate, SPI mode, and word sizes. 446 This sets up the device clock rate, SPI mode, and word sizes.
@@ -431,6 +457,9 @@ also initialize its own internal state.
431 state it dynamically associates with that device. If you do that, 457 state it dynamically associates with that device. If you do that,
432 be sure to provide the cleanup() method to free that state. 458 be sure to provide the cleanup() method to free that state.
433 459
460
461SPI MESSAGE QUEUE
462
434The bulk of the driver will be managing the I/O queue fed by transfer(). 463The bulk of the driver will be managing the I/O queue fed by transfer().
435 464
436That queue could be purely conceptual. For example, a driver used only 465That queue could be purely conceptual. For example, a driver used only
@@ -440,6 +469,9 @@ But the queue will probably be very real, using message->queue, PIO,
440often DMA (especially if the root filesystem is in SPI flash), and 469often DMA (especially if the root filesystem is in SPI flash), and
441execution contexts like IRQ handlers, tasklets, or workqueues (such 470execution contexts like IRQ handlers, tasklets, or workqueues (such
442as keventd). Your driver can be as fancy, or as simple, as you need. 471as keventd). Your driver can be as fancy, or as simple, as you need.
472Such a transfer() method would normally just add the message to a
473queue, and then start some asynchronous transfer engine (unless it's
474already running).
443 475
444 476
445THANKS TO 477THANKS TO
diff --git a/MAINTAINERS b/MAINTAINERS
index 246153dee7f..753584cf4e7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2518,6 +2518,12 @@ M: perex@suse.cz
2518L: alsa-devel@alsa-project.org 2518L: alsa-devel@alsa-project.org
2519S: Maintained 2519S: Maintained
2520 2520
2521SPI SUBSYSTEM
2522P: David Brownell
2523M: dbrownell@users.sourceforge.net
2524L: spi-devel-general@lists.sourceforge.net
2525S: Maintained
2526
2521TPM DEVICE DRIVER 2527TPM DEVICE DRIVER
2522P: Kylene Hall 2528P: Kylene Hall
2523M: kjhall@us.ibm.com 2529M: kjhall@us.ibm.com
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 7a75faeb052..9ce1d01469b 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -75,6 +75,14 @@ config SPI_BUTTERFLY
75 inexpensive battery powered microcontroller evaluation board. 75 inexpensive battery powered microcontroller evaluation board.
76 This same cable can be used to flash new firmware. 76 This same cable can be used to flash new firmware.
77 77
78config SPI_PXA2XX
79 tristate "PXA2xx SSP SPI master"
80 depends on SPI_MASTER && ARCH_PXA && EXPERIMENTAL
81 help
82 This enables using a PXA2xx SSP port as a SPI master controller.
83 The driver can be configured to use any SSP port and additional
84 documentation can be found a Documentation/spi/pxa2xx.
85
78# 86#
79# Add new SPI master controllers in alphabetical order above this line 87# Add new SPI master controllers in alphabetical order above this line
80# 88#
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index c2c87e845ab..1bca5f95de2 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_SPI_MASTER) += spi.o
13# SPI master controller drivers (bus) 13# SPI master controller drivers (bus)
14obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o 14obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
15obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o 15obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
16obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o
16# ... add above this line ... 17# ... add above this line ...
17 18
18# SPI protocol drivers (device/link on bus) 19# SPI protocol drivers (device/link on bus)
diff --git a/drivers/spi/pxa2xx_spi.c b/drivers/spi/pxa2xx_spi.c
new file mode 100644
index 00000000000..596bf820b70
--- /dev/null
+++ b/drivers/spi/pxa2xx_spi.c
@@ -0,0 +1,1467 @@
1/*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/ioport.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/dma-mapping.h>
27#include <linux/spi/spi.h>
28#include <linux/workqueue.h>
29#include <linux/errno.h>
30#include <linux/delay.h>
31
32#include <asm/io.h>
33#include <asm/irq.h>
34#include <asm/hardware.h>
35#include <asm/delay.h>
36#include <asm/dma.h>
37
38#include <asm/arch/hardware.h>
39#include <asm/arch/pxa-regs.h>
40#include <asm/arch/pxa2xx_spi.h>
41
42MODULE_AUTHOR("Stephen Street");
43MODULE_DESCRIPTION("PXA2xx SSP SPI Contoller");
44MODULE_LICENSE("GPL");
45
46#define MAX_BUSES 3
47
48#define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
49#define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
50#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
51
52#define DEFINE_SSP_REG(reg, off) \
53static inline u32 read_##reg(void *p) { return __raw_readl(p + (off)); } \
54static inline void write_##reg(u32 v, void *p) { __raw_writel(v, p + (off)); }
55
56DEFINE_SSP_REG(SSCR0, 0x00)
57DEFINE_SSP_REG(SSCR1, 0x04)
58DEFINE_SSP_REG(SSSR, 0x08)
59DEFINE_SSP_REG(SSITR, 0x0c)
60DEFINE_SSP_REG(SSDR, 0x10)
61DEFINE_SSP_REG(SSTO, 0x28)
62DEFINE_SSP_REG(SSPSP, 0x2c)
63
64#define START_STATE ((void*)0)
65#define RUNNING_STATE ((void*)1)
66#define DONE_STATE ((void*)2)
67#define ERROR_STATE ((void*)-1)
68
69#define QUEUE_RUNNING 0
70#define QUEUE_STOPPED 1
71
72struct driver_data {
73 /* Driver model hookup */
74 struct platform_device *pdev;
75
76 /* SPI framework hookup */
77 enum pxa_ssp_type ssp_type;
78 struct spi_master *master;
79
80 /* PXA hookup */
81 struct pxa2xx_spi_master *master_info;
82
83 /* DMA setup stuff */
84 int rx_channel;
85 int tx_channel;
86 u32 *null_dma_buf;
87
88 /* SSP register addresses */
89 void *ioaddr;
90 u32 ssdr_physical;
91
92 /* SSP masks*/
93 u32 dma_cr1;
94 u32 int_cr1;
95 u32 clear_sr;
96 u32 mask_sr;
97
98 /* Driver message queue */
99 struct workqueue_struct *workqueue;
100 struct work_struct pump_messages;
101 spinlock_t lock;
102 struct list_head queue;
103 int busy;
104 int run;
105
106 /* Message Transfer pump */
107 struct tasklet_struct pump_transfers;
108
109 /* Current message transfer state info */
110 struct spi_message* cur_msg;
111 struct spi_transfer* cur_transfer;
112 struct chip_data *cur_chip;
113 size_t len;
114 void *tx;
115 void *tx_end;
116 void *rx;
117 void *rx_end;
118 int dma_mapped;
119 dma_addr_t rx_dma;
120 dma_addr_t tx_dma;
121 size_t rx_map_len;
122 size_t tx_map_len;
123 u8 n_bytes;
124 u32 dma_width;
125 int cs_change;
126 void (*write)(struct driver_data *drv_data);
127 void (*read)(struct driver_data *drv_data);
128 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
129 void (*cs_control)(u32 command);
130};
131
132struct chip_data {
133 u32 cr0;
134 u32 cr1;
135 u32 to;
136 u32 psp;
137 u32 timeout;
138 u8 n_bytes;
139 u32 dma_width;
140 u32 dma_burst_size;
141 u32 threshold;
142 u32 dma_threshold;
143 u8 enable_dma;
144 u8 bits_per_word;
145 u32 speed_hz;
146 void (*write)(struct driver_data *drv_data);
147 void (*read)(struct driver_data *drv_data);
148 void (*cs_control)(u32 command);
149};
150
151static void pump_messages(void *data);
152
153static int flush(struct driver_data *drv_data)
154{
155 unsigned long limit = loops_per_jiffy << 1;
156
157 void *reg = drv_data->ioaddr;
158
159 do {
160 while (read_SSSR(reg) & SSSR_RNE) {
161 read_SSDR(reg);
162 }
163 } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
164 write_SSSR(SSSR_ROR, reg);
165
166 return limit;
167}
168
169static void restore_state(struct driver_data *drv_data)
170{
171 void *reg = drv_data->ioaddr;
172
173 /* Clear status and disable clock */
174 write_SSSR(drv_data->clear_sr, reg);
175 write_SSCR0(drv_data->cur_chip->cr0 & ~SSCR0_SSE, reg);
176
177 /* Load the registers */
178 write_SSCR1(drv_data->cur_chip->cr1, reg);
179 write_SSCR0(drv_data->cur_chip->cr0, reg);
180 if (drv_data->ssp_type != PXA25x_SSP) {
181 write_SSTO(0, reg);
182 write_SSPSP(drv_data->cur_chip->psp, reg);
183 }
184}
185
186static void null_cs_control(u32 command)
187{
188}
189
190static void null_writer(struct driver_data *drv_data)
191{
192 void *reg = drv_data->ioaddr;
193 u8 n_bytes = drv_data->n_bytes;
194
195 while ((read_SSSR(reg) & SSSR_TNF)
196 && (drv_data->tx < drv_data->tx_end)) {
197 write_SSDR(0, reg);
198 drv_data->tx += n_bytes;
199 }
200}
201
202static void null_reader(struct driver_data *drv_data)
203{
204 void *reg = drv_data->ioaddr;
205 u8 n_bytes = drv_data->n_bytes;
206
207 while ((read_SSSR(reg) & SSSR_RNE)
208 && (drv_data->rx < drv_data->rx_end)) {
209 read_SSDR(reg);
210 drv_data->rx += n_bytes;
211 }
212}
213
214static void u8_writer(struct driver_data *drv_data)
215{
216 void *reg = drv_data->ioaddr;
217
218 while ((read_SSSR(reg) & SSSR_TNF)
219 && (drv_data->tx < drv_data->tx_end)) {
220 write_SSDR(*(u8 *)(drv_data->tx), reg);
221 ++drv_data->tx;
222 }
223}
224
225static void u8_reader(struct driver_data *drv_data)
226{
227 void *reg = drv_data->ioaddr;
228
229 while ((read_SSSR(reg) & SSSR_RNE)
230 && (drv_data->rx < drv_data->rx_end)) {
231 *(u8 *)(drv_data->rx) = read_SSDR(reg);
232 ++drv_data->rx;
233 }
234}
235
236static void u16_writer(struct driver_data *drv_data)
237{
238 void *reg = drv_data->ioaddr;
239
240 while ((read_SSSR(reg) & SSSR_TNF)
241 && (drv_data->tx < drv_data->tx_end)) {
242 write_SSDR(*(u16 *)(drv_data->tx), reg);
243 drv_data->tx += 2;
244 }
245}
246
247static void u16_reader(struct driver_data *drv_data)
248{
249 void *reg = drv_data->ioaddr;
250
251 while ((read_SSSR(reg) & SSSR_RNE)
252 && (drv_data->rx < drv_data->rx_end)) {
253 *(u16 *)(drv_data->rx) = read_SSDR(reg);
254 drv_data->rx += 2;
255 }
256}
257static void u32_writer(struct driver_data *drv_data)
258{
259 void *reg = drv_data->ioaddr;
260
261 while ((read_SSSR(reg) & SSSR_TNF)
262 && (drv_data->tx < drv_data->tx_end)) {
263 write_SSDR(*(u32 *)(drv_data->tx), reg);
264 drv_data->tx += 4;
265 }
266}
267
268static void u32_reader(struct driver_data *drv_data)
269{
270 void *reg = drv_data->ioaddr;
271
272 while ((read_SSSR(reg) & SSSR_RNE)
273 && (drv_data->rx < drv_data->rx_end)) {
274 *(u32 *)(drv_data->rx) = read_SSDR(reg);
275 drv_data->rx += 4;
276 }
277}
278
279static void *next_transfer(struct driver_data *drv_data)
280{
281 struct spi_message *msg = drv_data->cur_msg;
282 struct spi_transfer *trans = drv_data->cur_transfer;
283
284 /* Move to next transfer */
285 if (trans->transfer_list.next != &msg->transfers) {
286 drv_data->cur_transfer =
287 list_entry(trans->transfer_list.next,
288 struct spi_transfer,
289 transfer_list);
290 return RUNNING_STATE;
291 } else
292 return DONE_STATE;
293}
294
295static int map_dma_buffers(struct driver_data *drv_data)
296{
297 struct spi_message *msg = drv_data->cur_msg;
298 struct device *dev = &msg->spi->dev;
299
300 if (!drv_data->cur_chip->enable_dma)
301 return 0;
302
303 if (msg->is_dma_mapped)
304 return drv_data->rx_dma && drv_data->tx_dma;
305
306 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
307 return 0;
308
309 /* Modify setup if rx buffer is null */
310 if (drv_data->rx == NULL) {
311 *drv_data->null_dma_buf = 0;
312 drv_data->rx = drv_data->null_dma_buf;
313 drv_data->rx_map_len = 4;
314 } else
315 drv_data->rx_map_len = drv_data->len;
316
317
318 /* Modify setup if tx buffer is null */
319 if (drv_data->tx == NULL) {
320 *drv_data->null_dma_buf = 0;
321 drv_data->tx = drv_data->null_dma_buf;
322 drv_data->tx_map_len = 4;
323 } else
324 drv_data->tx_map_len = drv_data->len;
325
326 /* Stream map the rx buffer */
327 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
328 drv_data->rx_map_len,
329 DMA_FROM_DEVICE);
330 if (dma_mapping_error(drv_data->rx_dma))
331 return 0;
332
333 /* Stream map the tx buffer */
334 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
335 drv_data->tx_map_len,
336 DMA_TO_DEVICE);
337
338 if (dma_mapping_error(drv_data->tx_dma)) {
339 dma_unmap_single(dev, drv_data->rx_dma,
340 drv_data->rx_map_len, DMA_FROM_DEVICE);
341 return 0;
342 }
343
344 return 1;
345}
346
347static void unmap_dma_buffers(struct driver_data *drv_data)
348{
349 struct device *dev;
350
351 if (!drv_data->dma_mapped)
352 return;
353
354 if (!drv_data->cur_msg->is_dma_mapped) {
355 dev = &drv_data->cur_msg->spi->dev;
356 dma_unmap_single(dev, drv_data->rx_dma,
357 drv_data->rx_map_len, DMA_FROM_DEVICE);
358 dma_unmap_single(dev, drv_data->tx_dma,
359 drv_data->tx_map_len, DMA_TO_DEVICE);
360 }
361
362 drv_data->dma_mapped = 0;
363}
364
365/* caller already set message->status; dma and pio irqs are blocked */
366static void giveback(struct spi_message *message, struct driver_data *drv_data)
367{
368 struct spi_transfer* last_transfer;
369
370 last_transfer = list_entry(message->transfers.prev,
371 struct spi_transfer,
372 transfer_list);
373
374 if (!last_transfer->cs_change)
375 drv_data->cs_control(PXA2XX_CS_DEASSERT);
376
377 message->state = NULL;
378 if (message->complete)
379 message->complete(message->context);
380
381 drv_data->cur_msg = NULL;
382 drv_data->cur_transfer = NULL;
383 drv_data->cur_chip = NULL;
384 queue_work(drv_data->workqueue, &drv_data->pump_messages);
385}
386
387static int wait_ssp_rx_stall(void *ioaddr)
388{
389 unsigned long limit = loops_per_jiffy << 1;
390
391 while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
392 cpu_relax();
393
394 return limit;
395}
396
397static int wait_dma_channel_stop(int channel)
398{
399 unsigned long limit = loops_per_jiffy << 1;
400
401 while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
402 cpu_relax();
403
404 return limit;
405}
406
407static void dma_handler(int channel, void *data, struct pt_regs *regs)
408{
409 struct driver_data *drv_data = data;
410 struct spi_message *msg = drv_data->cur_msg;
411 void *reg = drv_data->ioaddr;
412 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
413 u32 trailing_sssr = 0;
414
415 if (irq_status & DCSR_BUSERR) {
416
417 /* Disable interrupts, clear status and reset DMA */
418 if (drv_data->ssp_type != PXA25x_SSP)
419 write_SSTO(0, reg);
420 write_SSSR(drv_data->clear_sr, reg);
421 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
422 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
423 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
424
425 if (flush(drv_data) == 0)
426 dev_err(&drv_data->pdev->dev,
427 "dma_handler: flush fail\n");
428
429 unmap_dma_buffers(drv_data);
430
431 if (channel == drv_data->tx_channel)
432 dev_err(&drv_data->pdev->dev,
433 "dma_handler: bad bus address on "
434 "tx channel %d, source %x target = %x\n",
435 channel, DSADR(channel), DTADR(channel));
436 else
437 dev_err(&drv_data->pdev->dev,
438 "dma_handler: bad bus address on "
439 "rx channel %d, source %x target = %x\n",
440 channel, DSADR(channel), DTADR(channel));
441
442 msg->state = ERROR_STATE;
443 tasklet_schedule(&drv_data->pump_transfers);
444 }
445
446 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
447 if ((drv_data->ssp_type == PXA25x_SSP)
448 && (channel == drv_data->tx_channel)
449 && (irq_status & DCSR_ENDINTR)) {
450
451 /* Wait for rx to stall */
452 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
453 dev_err(&drv_data->pdev->dev,
454 "dma_handler: ssp rx stall failed\n");
455
456 /* Clear and disable interrupts on SSP and DMA channels*/
457 write_SSSR(drv_data->clear_sr, reg);
458 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
459 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
460 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
461 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
462 dev_err(&drv_data->pdev->dev,
463 "dma_handler: dma rx channel stop failed\n");
464
465 unmap_dma_buffers(drv_data);
466
467 /* Read trailing bytes */
468 /* Calculate number of trailing bytes, read them */
469 trailing_sssr = read_SSSR(reg);
470 if ((trailing_sssr & 0xf008) != 0xf000) {
471 drv_data->rx = drv_data->rx_end -
472 (((trailing_sssr >> 12) & 0x0f) + 1);
473 drv_data->read(drv_data);
474 }
475 msg->actual_length += drv_data->len;
476
477 /* Release chip select if requested, transfer delays are
478 * handled in pump_transfers */
479 if (drv_data->cs_change)
480 drv_data->cs_control(PXA2XX_CS_DEASSERT);
481
482 /* Move to next transfer */
483 msg->state = next_transfer(drv_data);
484
485 /* Schedule transfer tasklet */
486 tasklet_schedule(&drv_data->pump_transfers);
487 }
488}
489
490static irqreturn_t dma_transfer(struct driver_data *drv_data)
491{
492 u32 irq_status;
493 u32 trailing_sssr = 0;
494 struct spi_message *msg = drv_data->cur_msg;
495 void *reg = drv_data->ioaddr;
496
497 irq_status = read_SSSR(reg) & drv_data->mask_sr;
498 if (irq_status & SSSR_ROR) {
499 /* Clear and disable interrupts on SSP and DMA channels*/
500 if (drv_data->ssp_type != PXA25x_SSP)
501 write_SSTO(0, reg);
502 write_SSSR(drv_data->clear_sr, reg);
503 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
504 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
505 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
506 unmap_dma_buffers(drv_data);
507
508 if (flush(drv_data) == 0)
509 dev_err(&drv_data->pdev->dev,
510 "dma_transfer: flush fail\n");
511
512 dev_warn(&drv_data->pdev->dev, "dma_transfer: fifo overun\n");
513
514 drv_data->cur_msg->state = ERROR_STATE;
515 tasklet_schedule(&drv_data->pump_transfers);
516
517 return IRQ_HANDLED;
518 }
519
520 /* Check for false positive timeout */
521 if ((irq_status & SSSR_TINT) && DCSR(drv_data->tx_channel) & DCSR_RUN) {
522 write_SSSR(SSSR_TINT, reg);
523 return IRQ_HANDLED;
524 }
525
526 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
527
528 /* Clear and disable interrupts on SSP and DMA channels*/
529 if (drv_data->ssp_type != PXA25x_SSP)
530 write_SSTO(0, reg);
531 write_SSSR(drv_data->clear_sr, reg);
532 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
533 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
534 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
535
536 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
537 dev_err(&drv_data->pdev->dev,
538 "dma_transfer: dma rx channel stop failed\n");
539
540 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
541 dev_err(&drv_data->pdev->dev,
542 "dma_transfer: ssp rx stall failed\n");
543
544 unmap_dma_buffers(drv_data);
545
546 /* Calculate number of trailing bytes, read them */
547 trailing_sssr = read_SSSR(reg);
548 if ((trailing_sssr & 0xf008) != 0xf000) {
549 drv_data->rx = drv_data->rx_end -
550 (((trailing_sssr >> 12) & 0x0f) + 1);
551 drv_data->read(drv_data);
552 }
553 msg->actual_length += drv_data->len;
554
555 /* Release chip select if requested, transfer delays are
556 * handled in pump_transfers */
557 if (drv_data->cs_change)
558 drv_data->cs_control(PXA2XX_CS_DEASSERT);
559
560 /* Move to next transfer */
561 msg->state = next_transfer(drv_data);
562
563 /* Schedule transfer tasklet */
564 tasklet_schedule(&drv_data->pump_transfers);
565
566 return IRQ_HANDLED;
567 }
568
569 /* Opps problem detected */
570 return IRQ_NONE;
571}
572
573static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
574{
575 u32 irq_status;
576 struct spi_message *msg = drv_data->cur_msg;
577 void *reg = drv_data->ioaddr;
578 irqreturn_t handled = IRQ_NONE;
579 unsigned long limit = loops_per_jiffy << 1;
580
581 while ((irq_status = (read_SSSR(reg) & drv_data->mask_sr))) {
582
583 if (irq_status & SSSR_ROR) {
584
585 /* Clear and disable interrupts */
586 if (drv_data->ssp_type != PXA25x_SSP)
587 write_SSTO(0, reg);
588 write_SSSR(drv_data->clear_sr, reg);
589 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
590
591 if (flush(drv_data) == 0)
592 dev_err(&drv_data->pdev->dev,
593 "interrupt_transfer: flush fail\n");
594
595 dev_warn(&drv_data->pdev->dev,
596 "interrupt_transfer: fifo overun\n");
597
598 msg->state = ERROR_STATE;
599 tasklet_schedule(&drv_data->pump_transfers);
600
601 return IRQ_HANDLED;
602 }
603
604 /* Look for false positive timeout */
605 if ((irq_status & SSSR_TINT)
606 && (drv_data->rx < drv_data->rx_end))
607 write_SSSR(SSSR_TINT, reg);
608
609 /* Pump data */
610 drv_data->read(drv_data);
611 drv_data->write(drv_data);
612
613 if (drv_data->tx == drv_data->tx_end) {
614 /* Disable tx interrupt */
615 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
616
617 /* PXA25x_SSP has no timeout, read trailing bytes */
618 if (drv_data->ssp_type == PXA25x_SSP) {
619 while ((read_SSSR(reg) & SSSR_BSY) && limit--)
620 drv_data->read(drv_data);
621
622 if (limit == 0)
623 dev_err(&drv_data->pdev->dev,
624 "interrupt_transfer: "
625 "trailing byte read failed\n");
626 }
627 }
628
629 if ((irq_status & SSSR_TINT)
630 || (drv_data->rx == drv_data->rx_end)) {
631
632 /* Clear timeout */
633 if (drv_data->ssp_type != PXA25x_SSP)
634 write_SSTO(0, reg);
635 write_SSSR(drv_data->clear_sr, reg);
636 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
637
638 /* Update total byte transfered */
639 msg->actual_length += drv_data->len;
640
641 /* Release chip select if requested, transfer delays are
642 * handled in pump_transfers */
643 if (drv_data->cs_change)
644 drv_data->cs_control(PXA2XX_CS_DEASSERT);
645
646 /* Move to next transfer */
647 msg->state = next_transfer(drv_data);
648
649 /* Schedule transfer tasklet */
650 tasklet_schedule(&drv_data->pump_transfers);
651
652 return IRQ_HANDLED;
653 }
654
655 /* We did something */
656 handled = IRQ_HANDLED;
657 }
658
659 return handled;
660}
661
662static irqreturn_t ssp_int(int irq, void *dev_id, struct pt_regs *regs)
663{
664 struct driver_data *drv_data = (struct driver_data *)dev_id;
665
666 if (!drv_data->cur_msg) {
667 dev_err(&drv_data->pdev->dev, "bad message state "
668 "in interrupt handler\n");
669 /* Never fail */
670 return IRQ_HANDLED;
671 }
672
673 return drv_data->transfer_handler(drv_data);
674}
675
676static void pump_transfers(unsigned long data)
677{
678 struct driver_data *drv_data = (struct driver_data *)data;
679 struct spi_message *message = NULL;
680 struct spi_transfer *transfer = NULL;
681 struct spi_transfer *previous = NULL;
682 struct chip_data *chip = NULL;
683 void *reg = drv_data->ioaddr;
684 u32 clk_div = 0;
685 u8 bits = 0;
686 u32 speed = 0;
687 u32 cr0;
688
689 /* Get current state information */
690 message = drv_data->cur_msg;
691 transfer = drv_data->cur_transfer;
692 chip = drv_data->cur_chip;
693
694 /* Handle for abort */
695 if (message->state == ERROR_STATE) {
696 message->status = -EIO;
697 giveback(message, drv_data);
698 return;
699 }
700
701 /* Handle end of message */
702 if (message->state == DONE_STATE) {
703 message->status = 0;
704 giveback(message, drv_data);
705 return;
706 }
707
708 /* Delay if requested at end of transfer*/
709 if (message->state == RUNNING_STATE) {
710 previous = list_entry(transfer->transfer_list.prev,
711 struct spi_transfer,
712 transfer_list);
713 if (previous->delay_usecs)
714 udelay(previous->delay_usecs);
715 }
716
717 /* Setup the transfer state based on the type of transfer */
718 if (flush(drv_data) == 0) {
719 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
720 message->status = -EIO;
721 giveback(message, drv_data);
722 return;
723 }
724 drv_data->n_bytes = chip->n_bytes;
725 drv_data->dma_width = chip->dma_width;
726 drv_data->cs_control = chip->cs_control;
727 drv_data->tx = (void *)transfer->tx_buf;
728 drv_data->tx_end = drv_data->tx + transfer->len;
729 drv_data->rx = transfer->rx_buf;
730 drv_data->rx_end = drv_data->rx + transfer->len;
731 drv_data->rx_dma = transfer->rx_dma;
732 drv_data->tx_dma = transfer->tx_dma;
733 drv_data->len = transfer->len;
734 drv_data->write = drv_data->tx ? chip->write : null_writer;
735 drv_data->read = drv_data->rx ? chip->read : null_reader;
736 drv_data->cs_change = transfer->cs_change;
737
738 /* Change speed and bit per word on a per transfer */
739 if (transfer->speed_hz || transfer->bits_per_word) {
740
741 /* Disable clock */
742 write_SSCR0(chip->cr0 & ~SSCR0_SSE, reg);
743 cr0 = chip->cr0;
744 bits = chip->bits_per_word;
745 speed = chip->speed_hz;
746
747 if (transfer->speed_hz)
748 speed = transfer->speed_hz;
749
750 if (transfer->bits_per_word)
751 bits = transfer->bits_per_word;
752
753 if (reg == SSP1_VIRT)
754 clk_div = SSP1_SerClkDiv(speed);
755 else if (reg == SSP2_VIRT)
756 clk_div = SSP2_SerClkDiv(speed);
757 else if (reg == SSP3_VIRT)
758 clk_div = SSP3_SerClkDiv(speed);
759
760 if (bits <= 8) {
761 drv_data->n_bytes = 1;
762 drv_data->dma_width = DCMD_WIDTH1;
763 drv_data->read = drv_data->read != null_reader ?
764 u8_reader : null_reader;
765 drv_data->write = drv_data->write != null_writer ?
766 u8_writer : null_writer;
767 } else if (bits <= 16) {
768 drv_data->n_bytes = 2;
769 drv_data->dma_width = DCMD_WIDTH2;
770 drv_data->read = drv_data->read != null_reader ?
771 u16_reader : null_reader;
772 drv_data->write = drv_data->write != null_writer ?
773 u16_writer : null_writer;
774 } else if (bits <= 32) {
775 drv_data->n_bytes = 4;
776 drv_data->dma_width = DCMD_WIDTH4;
777 drv_data->read = drv_data->read != null_reader ?
778 u32_reader : null_reader;
779 drv_data->write = drv_data->write != null_writer ?
780 u32_writer : null_writer;
781 }
782
783 cr0 = clk_div
784 | SSCR0_Motorola
785 | SSCR0_DataSize(bits & 0x0f)
786 | SSCR0_SSE
787 | (bits > 16 ? SSCR0_EDSS : 0);
788
789 /* Start it back up */
790 write_SSCR0(cr0, reg);
791 }
792
793 message->state = RUNNING_STATE;
794
795 /* Try to map dma buffer and do a dma transfer if successful */
796 if ((drv_data->dma_mapped = map_dma_buffers(drv_data))) {
797
798 /* Ensure we have the correct interrupt handler */
799 drv_data->transfer_handler = dma_transfer;
800
801 /* Setup rx DMA Channel */
802 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
803 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
804 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
805 if (drv_data->rx == drv_data->null_dma_buf)
806 /* No target address increment */
807 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
808 | drv_data->dma_width
809 | chip->dma_burst_size
810 | drv_data->len;
811 else
812 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
813 | DCMD_FLOWSRC
814 | drv_data->dma_width
815 | chip->dma_burst_size
816 | drv_data->len;
817
818 /* Setup tx DMA Channel */
819 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
820 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
821 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
822 if (drv_data->tx == drv_data->null_dma_buf)
823 /* No source address increment */
824 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
825 | drv_data->dma_width
826 | chip->dma_burst_size
827 | drv_data->len;
828 else
829 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
830 | DCMD_FLOWTRG
831 | drv_data->dma_width
832 | chip->dma_burst_size
833 | drv_data->len;
834
835 /* Enable dma end irqs on SSP to detect end of transfer */
836 if (drv_data->ssp_type == PXA25x_SSP)
837 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
838
839 /* Fix me, need to handle cs polarity */
840 drv_data->cs_control(PXA2XX_CS_ASSERT);
841
842 /* Go baby, go */
843 write_SSSR(drv_data->clear_sr, reg);
844 DCSR(drv_data->rx_channel) |= DCSR_RUN;
845 DCSR(drv_data->tx_channel) |= DCSR_RUN;
846 if (drv_data->ssp_type != PXA25x_SSP)
847 write_SSTO(chip->timeout, reg);
848 write_SSCR1(chip->cr1
849 | chip->dma_threshold
850 | drv_data->dma_cr1,
851 reg);
852 } else {
853 /* Ensure we have the correct interrupt handler */
854 drv_data->transfer_handler = interrupt_transfer;
855
856 /* Fix me, need to handle cs polarity */
857 drv_data->cs_control(PXA2XX_CS_ASSERT);
858
859 /* Go baby, go */
860 write_SSSR(drv_data->clear_sr, reg);
861 if (drv_data->ssp_type != PXA25x_SSP)
862 write_SSTO(chip->timeout, reg);
863 write_SSCR1(chip->cr1
864 | chip->threshold
865 | drv_data->int_cr1,
866 reg);
867 }
868}
869
870static void pump_messages(void *data)
871{
872 struct driver_data *drv_data = data;
873 unsigned long flags;
874
875 /* Lock queue and check for queue work */
876 spin_lock_irqsave(&drv_data->lock, flags);
877 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
878 drv_data->busy = 0;
879 spin_unlock_irqrestore(&drv_data->lock, flags);
880 return;
881 }
882
883 /* Make sure we are not already running a message */
884 if (drv_data->cur_msg) {
885 spin_unlock_irqrestore(&drv_data->lock, flags);
886 return;
887 }
888
889 /* Extract head of queue */
890 drv_data->cur_msg = list_entry(drv_data->queue.next,
891 struct spi_message, queue);
892 list_del_init(&drv_data->cur_msg->queue);
893 drv_data->busy = 1;
894 spin_unlock_irqrestore(&drv_data->lock, flags);
895
896 /* Initial message state*/
897 drv_data->cur_msg->state = START_STATE;
898 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
899 struct spi_transfer,
900 transfer_list);
901
902 /* Setup the SSP using the per chip configuration */
903 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
904 restore_state(drv_data);
905
906 /* Mark as busy and launch transfers */
907 tasklet_schedule(&drv_data->pump_transfers);
908}
909
910static int transfer(struct spi_device *spi, struct spi_message *msg)
911{
912 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
913 unsigned long flags;
914
915 spin_lock_irqsave(&drv_data->lock, flags);
916
917 if (drv_data->run == QUEUE_STOPPED) {
918 spin_unlock_irqrestore(&drv_data->lock, flags);
919 return -ESHUTDOWN;
920 }
921
922 msg->actual_length = 0;
923 msg->status = -EINPROGRESS;
924 msg->state = START_STATE;
925
926 list_add_tail(&msg->queue, &drv_data->queue);
927
928 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
929 queue_work(drv_data->workqueue, &drv_data->pump_messages);
930
931 spin_unlock_irqrestore(&drv_data->lock, flags);
932
933 return 0;
934}
935
936static int setup(struct spi_device *spi)
937{
938 struct pxa2xx_spi_chip *chip_info = NULL;
939 struct chip_data *chip;
940 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
941 unsigned int clk_div;
942
943 if (!spi->bits_per_word)
944 spi->bits_per_word = 8;
945
946 if (drv_data->ssp_type != PXA25x_SSP
947 && (spi->bits_per_word < 4 || spi->bits_per_word > 32))
948 return -EINVAL;
949 else if (spi->bits_per_word < 4 || spi->bits_per_word > 16)
950 return -EINVAL;
951
952 /* Only alloc (or use chip_info) on first setup */
953 chip = spi_get_ctldata(spi);
954 if (chip == NULL) {
955 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
956 if (!chip)
957 return -ENOMEM;
958
959 chip->cs_control = null_cs_control;
960 chip->enable_dma = 0;
961 chip->timeout = 5;
962 chip->threshold = SSCR1_RxTresh(1) | SSCR1_TxTresh(1);
963 chip->dma_burst_size = drv_data->master_info->enable_dma ?
964 DCMD_BURST8 : 0;
965
966 chip_info = spi->controller_data;
967 }
968
969 /* chip_info isn't always needed */
970 if (chip_info) {
971 if (chip_info->cs_control)
972 chip->cs_control = chip_info->cs_control;
973
974 chip->timeout = (chip_info->timeout_microsecs * 10000) / 2712;
975
976 chip->threshold = SSCR1_RxTresh(chip_info->rx_threshold)
977 | SSCR1_TxTresh(chip_info->tx_threshold);
978
979 chip->enable_dma = chip_info->dma_burst_size != 0
980 && drv_data->master_info->enable_dma;
981 chip->dma_threshold = 0;
982
983 if (chip->enable_dma) {
984 if (chip_info->dma_burst_size <= 8) {
985 chip->dma_threshold = SSCR1_RxTresh(8)
986 | SSCR1_TxTresh(8);
987 chip->dma_burst_size = DCMD_BURST8;
988 } else if (chip_info->dma_burst_size <= 16) {
989 chip->dma_threshold = SSCR1_RxTresh(16)
990 | SSCR1_TxTresh(16);
991 chip->dma_burst_size = DCMD_BURST16;
992 } else {
993 chip->dma_threshold = SSCR1_RxTresh(32)
994 | SSCR1_TxTresh(32);
995 chip->dma_burst_size = DCMD_BURST32;
996 }
997 }
998
999
1000 if (chip_info->enable_loopback)
1001 chip->cr1 = SSCR1_LBM;
1002 }
1003
1004 if (drv_data->ioaddr == SSP1_VIRT)
1005 clk_div = SSP1_SerClkDiv(spi->max_speed_hz);
1006 else if (drv_data->ioaddr == SSP2_VIRT)
1007 clk_div = SSP2_SerClkDiv(spi->max_speed_hz);
1008 else if (drv_data->ioaddr == SSP3_VIRT)
1009 clk_div = SSP3_SerClkDiv(spi->max_speed_hz);
1010 else
1011 return -ENODEV;
1012 chip->speed_hz = spi->max_speed_hz;
1013
1014 chip->cr0 = clk_div
1015 | SSCR0_Motorola
1016 | SSCR0_DataSize(spi->bits_per_word & 0x0f)
1017 | SSCR0_SSE
1018 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
1019 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) << 4)
1020 | (((spi->mode & SPI_CPOL) != 0) << 3);
1021
1022 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1023 if (drv_data->ssp_type != PXA25x_SSP)
1024 dev_dbg(&spi->dev, "%d bits/word, %d Hz, mode %d\n",
1025 spi->bits_per_word,
1026 (CLOCK_SPEED_HZ)
1027 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1028 spi->mode & 0x3);
1029 else
1030 dev_dbg(&spi->dev, "%d bits/word, %d Hz, mode %d\n",
1031 spi->bits_per_word,
1032 (CLOCK_SPEED_HZ/2)
1033 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1034 spi->mode & 0x3);
1035
1036 if (spi->bits_per_word <= 8) {
1037 chip->n_bytes = 1;
1038 chip->dma_width = DCMD_WIDTH1;
1039 chip->read = u8_reader;
1040 chip->write = u8_writer;
1041 } else if (spi->bits_per_word <= 16) {
1042 chip->n_bytes = 2;
1043 chip->dma_width = DCMD_WIDTH2;
1044 chip->read = u16_reader;
1045 chip->write = u16_writer;
1046 } else if (spi->bits_per_word <= 32) {
1047 chip->cr0 |= SSCR0_EDSS;
1048 chip->n_bytes = 4;
1049 chip->dma_width = DCMD_WIDTH4;
1050 chip->read = u32_reader;
1051 chip->write = u32_writer;
1052 } else {
1053 dev_err(&spi->dev, "invalid wordsize\n");
1054 kfree(chip);
1055 return -ENODEV;
1056 }
1057 chip->bits_per_word = spi->bits_per_word;
1058
1059 spi_set_ctldata(spi, chip);
1060
1061 return 0;
1062}
1063
1064static void cleanup(const struct spi_device *spi)
1065{
1066 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
1067
1068 kfree(chip);
1069}
1070
1071static int init_queue(struct driver_data *drv_data)
1072{
1073 INIT_LIST_HEAD(&drv_data->queue);
1074 spin_lock_init(&drv_data->lock);
1075
1076 drv_data->run = QUEUE_STOPPED;
1077 drv_data->busy = 0;
1078
1079 tasklet_init(&drv_data->pump_transfers,
1080 pump_transfers, (unsigned long)drv_data);
1081
1082 INIT_WORK(&drv_data->pump_messages, pump_messages, drv_data);
1083 drv_data->workqueue = create_singlethread_workqueue(
1084 drv_data->master->cdev.dev->bus_id);
1085 if (drv_data->workqueue == NULL)
1086 return -EBUSY;
1087
1088 return 0;
1089}
1090
1091static int start_queue(struct driver_data *drv_data)
1092{
1093 unsigned long flags;
1094
1095 spin_lock_irqsave(&drv_data->lock, flags);
1096
1097 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1098 spin_unlock_irqrestore(&drv_data->lock, flags);
1099 return -EBUSY;
1100 }
1101
1102 drv_data->run = QUEUE_RUNNING;
1103 drv_data->cur_msg = NULL;
1104 drv_data->cur_transfer = NULL;
1105 drv_data->cur_chip = NULL;
1106 spin_unlock_irqrestore(&drv_data->lock, flags);
1107
1108 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1109
1110 return 0;
1111}
1112
1113static int stop_queue(struct driver_data *drv_data)
1114{
1115 unsigned long flags;
1116 unsigned limit = 500;
1117 int status = 0;
1118
1119 spin_lock_irqsave(&drv_data->lock, flags);
1120
1121 /* This is a bit lame, but is optimized for the common execution path.
1122 * A wait_queue on the drv_data->busy could be used, but then the common
1123 * execution path (pump_messages) would be required to call wake_up or
1124 * friends on every SPI message. Do this instead */
1125 drv_data->run = QUEUE_STOPPED;
1126 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1127 spin_unlock_irqrestore(&drv_data->lock, flags);
1128 msleep(10);
1129 spin_lock_irqsave(&drv_data->lock, flags);
1130 }
1131
1132 if (!list_empty(&drv_data->queue) || drv_data->busy)
1133 status = -EBUSY;
1134
1135 spin_unlock_irqrestore(&drv_data->lock, flags);
1136
1137 return status;
1138}
1139
1140static int destroy_queue(struct driver_data *drv_data)
1141{
1142 int status;
1143
1144 status = stop_queue(drv_data);
1145 if (status != 0)
1146 return status;
1147
1148 destroy_workqueue(drv_data->workqueue);
1149
1150 return 0;
1151}
1152
1153static int pxa2xx_spi_probe(struct platform_device *pdev)
1154{
1155 struct device *dev = &pdev->dev;
1156 struct pxa2xx_spi_master *platform_info;
1157 struct spi_master *master;
1158 struct driver_data *drv_data = 0;
1159 struct resource *memory_resource;
1160 int irq;
1161 int status = 0;
1162
1163 platform_info = dev->platform_data;
1164
1165 if (platform_info->ssp_type == SSP_UNDEFINED) {
1166 dev_err(&pdev->dev, "undefined SSP\n");
1167 return -ENODEV;
1168 }
1169
1170 /* Allocate master with space for drv_data and null dma buffer */
1171 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1172 if (!master) {
1173 dev_err(&pdev->dev, "can not alloc spi_master\n");
1174 return -ENOMEM;
1175 }
1176 drv_data = spi_master_get_devdata(master);
1177 drv_data->master = master;
1178 drv_data->master_info = platform_info;
1179 drv_data->pdev = pdev;
1180
1181 master->bus_num = pdev->id;
1182 master->num_chipselect = platform_info->num_chipselect;
1183 master->cleanup = cleanup;
1184 master->setup = setup;
1185 master->transfer = transfer;
1186
1187 drv_data->ssp_type = platform_info->ssp_type;
1188 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1189 sizeof(struct driver_data)), 8);
1190
1191 /* Setup register addresses */
1192 memory_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1193 if (!memory_resource) {
1194 dev_err(&pdev->dev, "memory resources not defined\n");
1195 status = -ENODEV;
1196 goto out_error_master_alloc;
1197 }
1198
1199 drv_data->ioaddr = (void *)io_p2v(memory_resource->start);
1200 drv_data->ssdr_physical = memory_resource->start + 0x00000010;
1201 if (platform_info->ssp_type == PXA25x_SSP) {
1202 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1203 drv_data->dma_cr1 = 0;
1204 drv_data->clear_sr = SSSR_ROR;
1205 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1206 } else {
1207 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1208 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1209 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1210 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1211 }
1212
1213 /* Attach to IRQ */
1214 irq = platform_get_irq(pdev, 0);
1215 if (irq < 0) {
1216 dev_err(&pdev->dev, "irq resource not defined\n");
1217 status = -ENODEV;
1218 goto out_error_master_alloc;
1219 }
1220
1221 status = request_irq(irq, ssp_int, SA_INTERRUPT, dev->bus_id, drv_data);
1222 if (status < 0) {
1223 dev_err(&pdev->dev, "can not get IRQ\n");
1224 goto out_error_master_alloc;
1225 }
1226
1227 /* Setup DMA if requested */
1228 drv_data->tx_channel = -1;
1229 drv_data->rx_channel = -1;
1230 if (platform_info->enable_dma) {
1231
1232 /* Get two DMA channels (rx and tx) */
1233 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1234 DMA_PRIO_HIGH,
1235 dma_handler,
1236 drv_data);
1237 if (drv_data->rx_channel < 0) {
1238 dev_err(dev, "problem (%d) requesting rx channel\n",
1239 drv_data->rx_channel);
1240 status = -ENODEV;
1241 goto out_error_irq_alloc;
1242 }
1243 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1244 DMA_PRIO_MEDIUM,
1245 dma_handler,
1246 drv_data);
1247 if (drv_data->tx_channel < 0) {
1248 dev_err(dev, "problem (%d) requesting tx channel\n",
1249 drv_data->tx_channel);
1250 status = -ENODEV;
1251 goto out_error_dma_alloc;
1252 }
1253
1254 if (drv_data->ioaddr == SSP1_VIRT) {
1255 DRCMRRXSSDR = DRCMR_MAPVLD
1256 | drv_data->rx_channel;
1257 DRCMRTXSSDR = DRCMR_MAPVLD
1258 | drv_data->tx_channel;
1259 } else if (drv_data->ioaddr == SSP2_VIRT) {
1260 DRCMRRXSS2DR = DRCMR_MAPVLD
1261 | drv_data->rx_channel;
1262 DRCMRTXSS2DR = DRCMR_MAPVLD
1263 | drv_data->tx_channel;
1264 } else if (drv_data->ioaddr == SSP3_VIRT) {
1265 DRCMRRXSS3DR = DRCMR_MAPVLD
1266 | drv_data->rx_channel;
1267 DRCMRTXSS3DR = DRCMR_MAPVLD
1268 | drv_data->tx_channel;
1269 } else {
1270 dev_err(dev, "bad SSP type\n");
1271 goto out_error_dma_alloc;
1272 }
1273 }
1274
1275 /* Enable SOC clock */
1276 pxa_set_cken(platform_info->clock_enable, 1);
1277
1278 /* Load default SSP configuration */
1279 write_SSCR0(0, drv_data->ioaddr);
1280 write_SSCR1(SSCR1_RxTresh(4) | SSCR1_TxTresh(12), drv_data->ioaddr);
1281 write_SSCR0(SSCR0_SerClkDiv(2)
1282 | SSCR0_Motorola
1283 | SSCR0_DataSize(8),
1284 drv_data->ioaddr);
1285 if (drv_data->ssp_type != PXA25x_SSP)
1286 write_SSTO(0, drv_data->ioaddr);
1287 write_SSPSP(0, drv_data->ioaddr);
1288
1289 /* Initial and start queue */
1290 status = init_queue(drv_data);
1291 if (status != 0) {
1292 dev_err(&pdev->dev, "problem initializing queue\n");
1293 goto out_error_clock_enabled;
1294 }
1295 status = start_queue(drv_data);
1296 if (status != 0) {
1297 dev_err(&pdev->dev, "problem starting queue\n");
1298 goto out_error_clock_enabled;
1299 }
1300
1301 /* Register with the SPI framework */
1302 platform_set_drvdata(pdev, drv_data);
1303 status = spi_register_master(master);
1304 if (status != 0) {
1305 dev_err(&pdev->dev, "problem registering spi master\n");
1306 goto out_error_queue_alloc;
1307 }
1308
1309 return status;
1310
1311out_error_queue_alloc:
1312 destroy_queue(drv_data);
1313
1314out_error_clock_enabled:
1315 pxa_set_cken(platform_info->clock_enable, 0);
1316
1317out_error_dma_alloc:
1318 if (drv_data->tx_channel != -1)
1319 pxa_free_dma(drv_data->tx_channel);
1320 if (drv_data->rx_channel != -1)
1321 pxa_free_dma(drv_data->rx_channel);
1322
1323out_error_irq_alloc:
1324 free_irq(irq, drv_data);
1325
1326out_error_master_alloc:
1327 spi_master_put(master);
1328 return status;
1329}
1330
1331static int pxa2xx_spi_remove(struct platform_device *pdev)
1332{
1333 struct driver_data *drv_data = platform_get_drvdata(pdev);
1334 int irq;
1335 int status = 0;
1336
1337 if (!drv_data)
1338 return 0;
1339
1340 /* Remove the queue */
1341 status = destroy_queue(drv_data);
1342 if (status != 0)
1343 return status;
1344
1345 /* Disable the SSP at the peripheral and SOC level */
1346 write_SSCR0(0, drv_data->ioaddr);
1347 pxa_set_cken(drv_data->master_info->clock_enable, 0);
1348
1349 /* Release DMA */
1350 if (drv_data->master_info->enable_dma) {
1351 if (drv_data->ioaddr == SSP1_VIRT) {
1352 DRCMRRXSSDR = 0;
1353 DRCMRTXSSDR = 0;
1354 } else if (drv_data->ioaddr == SSP2_VIRT) {
1355 DRCMRRXSS2DR = 0;
1356 DRCMRTXSS2DR = 0;
1357 } else if (drv_data->ioaddr == SSP3_VIRT) {
1358 DRCMRRXSS3DR = 0;
1359 DRCMRTXSS3DR = 0;
1360 }
1361 pxa_free_dma(drv_data->tx_channel);
1362 pxa_free_dma(drv_data->rx_channel);
1363 }
1364
1365 /* Release IRQ */
1366 irq = platform_get_irq(pdev, 0);
1367 if (irq >= 0)
1368 free_irq(irq, drv_data);
1369
1370 /* Disconnect from the SPI framework */
1371 spi_unregister_master(drv_data->master);
1372
1373 /* Prevent double remove */
1374 platform_set_drvdata(pdev, NULL);
1375
1376 return 0;
1377}
1378
1379static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1380{
1381 int status = 0;
1382
1383 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1384 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1385}
1386
1387#ifdef CONFIG_PM
1388static int suspend_devices(struct device *dev, void *pm_message)
1389{
1390 pm_message_t *state = pm_message;
1391
1392 if (dev->power.power_state.event != state->event) {
1393 dev_warn(dev, "pm state does not match request\n");
1394 return -1;
1395 }
1396
1397 return 0;
1398}
1399
1400static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1401{
1402 struct driver_data *drv_data = platform_get_drvdata(pdev);
1403 int status = 0;
1404
1405 /* Check all childern for current power state */
1406 if (device_for_each_child(&pdev->dev, &state, suspend_devices) != 0) {
1407 dev_warn(&pdev->dev, "suspend aborted\n");
1408 return -1;
1409 }
1410
1411 status = stop_queue(drv_data);
1412 if (status != 0)
1413 return status;
1414 write_SSCR0(0, drv_data->ioaddr);
1415 pxa_set_cken(drv_data->master_info->clock_enable, 0);
1416
1417 return 0;
1418}
1419
1420static int pxa2xx_spi_resume(struct platform_device *pdev)
1421{
1422 struct driver_data *drv_data = platform_get_drvdata(pdev);
1423 int status = 0;
1424
1425 /* Enable the SSP clock */
1426 pxa_set_cken(drv_data->master_info->clock_enable, 1);
1427
1428 /* Start the queue running */
1429 status = start_queue(drv_data);
1430 if (status != 0) {
1431 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1432 return status;
1433 }
1434
1435 return 0;
1436}
1437#else
1438#define pxa2xx_spi_suspend NULL
1439#define pxa2xx_spi_resume NULL
1440#endif /* CONFIG_PM */
1441
1442static struct platform_driver driver = {
1443 .driver = {
1444 .name = "pxa2xx-spi",
1445 .bus = &platform_bus_type,
1446 .owner = THIS_MODULE,
1447 },
1448 .probe = pxa2xx_spi_probe,
1449 .remove = __devexit_p(pxa2xx_spi_remove),
1450 .shutdown = pxa2xx_spi_shutdown,
1451 .suspend = pxa2xx_spi_suspend,
1452 .resume = pxa2xx_spi_resume,
1453};
1454
1455static int __init pxa2xx_spi_init(void)
1456{
1457 platform_driver_register(&driver);
1458
1459 return 0;
1460}
1461module_init(pxa2xx_spi_init);
1462
1463static void __exit pxa2xx_spi_exit(void)
1464{
1465 platform_driver_unregister(&driver);
1466}
1467module_exit(pxa2xx_spi_exit);
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 94f5e8ed83a..7a3f733051e 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -395,7 +395,7 @@ EXPORT_SYMBOL_GPL(spi_alloc_master);
395int __init_or_module 395int __init_or_module
396spi_register_master(struct spi_master *master) 396spi_register_master(struct spi_master *master)
397{ 397{
398 static atomic_t dyn_bus_id = ATOMIC_INIT(0); 398 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<16) - 1);
399 struct device *dev = master->cdev.dev; 399 struct device *dev = master->cdev.dev;
400 int status = -ENODEV; 400 int status = -ENODEV;
401 int dynamic = 0; 401 int dynamic = 0;
@@ -404,7 +404,7 @@ spi_register_master(struct spi_master *master)
404 return -ENODEV; 404 return -ENODEV;
405 405
406 /* convention: dynamically assigned bus IDs count down from the max */ 406 /* convention: dynamically assigned bus IDs count down from the max */
407 if (master->bus_num == 0) { 407 if (master->bus_num < 0) {
408 master->bus_num = atomic_dec_return(&dyn_bus_id); 408 master->bus_num = atomic_dec_return(&dyn_bus_id);
409 dynamic = 1; 409 dynamic = 1;
410 } 410 }
@@ -522,7 +522,8 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
522} 522}
523EXPORT_SYMBOL_GPL(spi_sync); 523EXPORT_SYMBOL_GPL(spi_sync);
524 524
525#define SPI_BUFSIZ (SMP_CACHE_BYTES) 525/* portable code must never pass more than 32 bytes */
526#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
526 527
527static u8 *buf; 528static u8 *buf;
528 529
diff --git a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c
index f037e559326..dd2f950b21a 100644
--- a/drivers/spi/spi_bitbang.c
+++ b/drivers/spi/spi_bitbang.c
@@ -138,6 +138,45 @@ static unsigned bitbang_txrx_32(
138 return t->len - count; 138 return t->len - count;
139} 139}
140 140
141int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
142{
143 struct spi_bitbang_cs *cs = spi->controller_state;
144 u8 bits_per_word;
145 u32 hz;
146
147 if (t) {
148 bits_per_word = t->bits_per_word;
149 hz = t->speed_hz;
150 } else {
151 bits_per_word = 0;
152 hz = 0;
153 }
154
155 /* spi_transfer level calls that work per-word */
156 if (!bits_per_word)
157 bits_per_word = spi->bits_per_word;
158 if (bits_per_word <= 8)
159 cs->txrx_bufs = bitbang_txrx_8;
160 else if (bits_per_word <= 16)
161 cs->txrx_bufs = bitbang_txrx_16;
162 else if (bits_per_word <= 32)
163 cs->txrx_bufs = bitbang_txrx_32;
164 else
165 return -EINVAL;
166
167 /* nsecs = (clock period)/2 */
168 if (!hz)
169 hz = spi->max_speed_hz;
170 if (hz) {
171 cs->nsecs = (1000000000/2) / hz;
172 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
173 return -EINVAL;
174 }
175
176 return 0;
177}
178EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
179
141/** 180/**
142 * spi_bitbang_setup - default setup for per-word I/O loops 181 * spi_bitbang_setup - default setup for per-word I/O loops
143 */ 182 */
@@ -145,8 +184,16 @@ int spi_bitbang_setup(struct spi_device *spi)
145{ 184{
146 struct spi_bitbang_cs *cs = spi->controller_state; 185 struct spi_bitbang_cs *cs = spi->controller_state;
147 struct spi_bitbang *bitbang; 186 struct spi_bitbang *bitbang;
187 int retval;
148 188
149 if (!spi->max_speed_hz) 189 bitbang = spi_master_get_devdata(spi->master);
190
191 /* REVISIT: some systems will want to support devices using lsb-first
192 * bit encodings on the wire. In pure software that would be trivial,
193 * just bitbang_txrx_le_cphaX() routines shifting the other way, and
194 * some hardware controllers also have this support.
195 */
196 if ((spi->mode & SPI_LSB_FIRST) != 0)
150 return -EINVAL; 197 return -EINVAL;
151 198
152 if (!cs) { 199 if (!cs) {
@@ -155,32 +202,20 @@ int spi_bitbang_setup(struct spi_device *spi)
155 return -ENOMEM; 202 return -ENOMEM;
156 spi->controller_state = cs; 203 spi->controller_state = cs;
157 } 204 }
158 bitbang = spi_master_get_devdata(spi->master);
159 205
160 if (!spi->bits_per_word) 206 if (!spi->bits_per_word)
161 spi->bits_per_word = 8; 207 spi->bits_per_word = 8;
162 208
163 /* spi_transfer level calls that work per-word */
164 if (spi->bits_per_word <= 8)
165 cs->txrx_bufs = bitbang_txrx_8;
166 else if (spi->bits_per_word <= 16)
167 cs->txrx_bufs = bitbang_txrx_16;
168 else if (spi->bits_per_word <= 32)
169 cs->txrx_bufs = bitbang_txrx_32;
170 else
171 return -EINVAL;
172
173 /* per-word shift register access, in hardware or bitbanging */ 209 /* per-word shift register access, in hardware or bitbanging */
174 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)]; 210 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
175 if (!cs->txrx_word) 211 if (!cs->txrx_word)
176 return -EINVAL; 212 return -EINVAL;
177 213
178 /* nsecs = (clock period)/2 */ 214 retval = spi_bitbang_setup_transfer(spi, NULL);
179 cs->nsecs = (1000000000/2) / (spi->max_speed_hz); 215 if (retval < 0)
180 if (cs->nsecs > MAX_UDELAY_MS * 1000) 216 return retval;
181 return -EINVAL;
182 217
183 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n", 218 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec/bit\n",
184 __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA), 219 __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
185 spi->bits_per_word, 2 * cs->nsecs); 220 spi->bits_per_word, 2 * cs->nsecs);
186 221
@@ -246,6 +281,8 @@ static void bitbang_work(void *_bitbang)
246 unsigned tmp; 281 unsigned tmp;
247 unsigned cs_change; 282 unsigned cs_change;
248 int status; 283 int status;
284 int (*setup_transfer)(struct spi_device *,
285 struct spi_transfer *);
249 286
250 m = container_of(bitbang->queue.next, struct spi_message, 287 m = container_of(bitbang->queue.next, struct spi_message,
251 queue); 288 queue);
@@ -262,6 +299,7 @@ static void bitbang_work(void *_bitbang)
262 tmp = 0; 299 tmp = 0;
263 cs_change = 1; 300 cs_change = 1;
264 status = 0; 301 status = 0;
302 setup_transfer = NULL;
265 303
266 list_for_each_entry (t, &m->transfers, transfer_list) { 304 list_for_each_entry (t, &m->transfers, transfer_list) {
267 if (bitbang->shutdown) { 305 if (bitbang->shutdown) {
@@ -269,6 +307,20 @@ static void bitbang_work(void *_bitbang)
269 break; 307 break;
270 } 308 }
271 309
310 /* override or restore speed and wordsize */
311 if (t->speed_hz || t->bits_per_word) {
312 setup_transfer = bitbang->setup_transfer;
313 if (!setup_transfer) {
314 status = -ENOPROTOOPT;
315 break;
316 }
317 }
318 if (setup_transfer) {
319 status = setup_transfer(spi, t);
320 if (status < 0)
321 break;
322 }
323
272 /* set up default clock polarity, and activate chip; 324 /* set up default clock polarity, and activate chip;
273 * this implicitly updates clock and spi modes as 325 * this implicitly updates clock and spi modes as
274 * previously recorded for this device via setup(). 326 * previously recorded for this device via setup().
@@ -325,6 +377,10 @@ static void bitbang_work(void *_bitbang)
325 m->status = status; 377 m->status = status;
326 m->complete(m->context); 378 m->complete(m->context);
327 379
380 /* restore speed and wordsize */
381 if (setup_transfer)
382 setup_transfer(spi, NULL);
383
328 /* normally deactivate chipselect ... unless no error and 384 /* normally deactivate chipselect ... unless no error and
329 * cs_change has hinted that the next message will probably 385 * cs_change has hinted that the next message will probably
330 * be for this chip too. 386 * be for this chip too.
@@ -348,6 +404,7 @@ int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
348{ 404{
349 struct spi_bitbang *bitbang; 405 struct spi_bitbang *bitbang;
350 unsigned long flags; 406 unsigned long flags;
407 int status = 0;
351 408
352 m->actual_length = 0; 409 m->actual_length = 0;
353 m->status = -EINPROGRESS; 410 m->status = -EINPROGRESS;
@@ -357,11 +414,15 @@ int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
357 return -ESHUTDOWN; 414 return -ESHUTDOWN;
358 415
359 spin_lock_irqsave(&bitbang->lock, flags); 416 spin_lock_irqsave(&bitbang->lock, flags);
360 list_add_tail(&m->queue, &bitbang->queue); 417 if (!spi->max_speed_hz)
361 queue_work(bitbang->workqueue, &bitbang->work); 418 status = -ENETDOWN;
419 else {
420 list_add_tail(&m->queue, &bitbang->queue);
421 queue_work(bitbang->workqueue, &bitbang->work);
422 }
362 spin_unlock_irqrestore(&bitbang->lock, flags); 423 spin_unlock_irqrestore(&bitbang->lock, flags);
363 424
364 return 0; 425 return status;
365} 426}
366EXPORT_SYMBOL_GPL(spi_bitbang_transfer); 427EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
367 428
@@ -406,6 +467,9 @@ int spi_bitbang_start(struct spi_bitbang *bitbang)
406 bitbang->use_dma = 0; 467 bitbang->use_dma = 0;
407 bitbang->txrx_bufs = spi_bitbang_bufs; 468 bitbang->txrx_bufs = spi_bitbang_bufs;
408 if (!bitbang->master->setup) { 469 if (!bitbang->master->setup) {
470 if (!bitbang->setup_transfer)
471 bitbang->setup_transfer =
472 spi_bitbang_setup_transfer;
409 bitbang->master->setup = spi_bitbang_setup; 473 bitbang->master->setup = spi_bitbang_setup;
410 bitbang->master->cleanup = spi_bitbang_cleanup; 474 bitbang->master->cleanup = spi_bitbang_cleanup;
411 } 475 }
diff --git a/include/asm-arm/arch-pxa/pxa2xx_spi.h b/include/asm-arm/arch-pxa/pxa2xx_spi.h
new file mode 100644
index 00000000000..1e70908b816
--- /dev/null
+++ b/include/asm-arm/arch-pxa/pxa2xx_spi.h
@@ -0,0 +1,68 @@
1/*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19#ifndef PXA2XX_SPI_H_
20#define PXA2XX_SPI_H_
21
22#define PXA2XX_CS_ASSERT (0x01)
23#define PXA2XX_CS_DEASSERT (0x02)
24
25#if defined(CONFIG_PXA25x)
26#define CLOCK_SPEED_HZ 3686400
27#define SSP1_SerClkDiv(x) (((CLOCK_SPEED_HZ/2/(x+1))<<8)&0x0000ff00)
28#define SSP2_SerClkDiv(x) (((CLOCK_SPEED_HZ/(x+1))<<8)&0x000fff00)
29#define SSP3_SerClkDiv(x) (((CLOCK_SPEED_HZ/(x+1))<<8)&0x000fff00)
30#elif defined(CONFIG_PXA27x)
31#define CLOCK_SPEED_HZ 13000000
32#define SSP1_SerClkDiv(x) (((CLOCK_SPEED_HZ/(x+1))<<8)&0x000fff00)
33#define SSP2_SerClkDiv(x) (((CLOCK_SPEED_HZ/(x+1))<<8)&0x000fff00)
34#define SSP3_SerClkDiv(x) (((CLOCK_SPEED_HZ/(x+1))<<8)&0x000fff00)
35#endif
36
37#define SSP1_VIRT ((void *)(io_p2v(__PREG(SSCR0_P(1)))))
38#define SSP2_VIRT ((void *)(io_p2v(__PREG(SSCR0_P(2)))))
39#define SSP3_VIRT ((void *)(io_p2v(__PREG(SSCR0_P(3)))))
40
41enum pxa_ssp_type {
42 SSP_UNDEFINED = 0,
43 PXA25x_SSP, /* pxa 210, 250, 255, 26x */
44 PXA25x_NSSP, /* pxa 255, 26x (including ASSP) */
45 PXA27x_SSP,
46};
47
48/* device.platform_data for SSP controller devices */
49struct pxa2xx_spi_master {
50 enum pxa_ssp_type ssp_type;
51 u32 clock_enable;
52 u16 num_chipselect;
53 u8 enable_dma;
54};
55
56/* spi_board_info.controller_data for SPI slave devices,
57 * copied to spi_device.platform_data ... mostly for dma tuning
58 */
59struct pxa2xx_spi_chip {
60 u8 tx_threshold;
61 u8 rx_threshold;
62 u8 dma_burst_size;
63 u32 timeout_microsecs;
64 u8 enable_loopback;
65 void (*cs_control)(u32 command);
66};
67
68#endif /*PXA2XX_SPI_H_*/
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index b05f1463a26..e928c0dcc29 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -31,18 +31,23 @@ extern struct bus_type spi_bus_type;
31 * @master: SPI controller used with the device. 31 * @master: SPI controller used with the device.
32 * @max_speed_hz: Maximum clock rate to be used with this chip 32 * @max_speed_hz: Maximum clock rate to be used with this chip
33 * (on this board); may be changed by the device's driver. 33 * (on this board); may be changed by the device's driver.
34 * The spi_transfer.speed_hz can override this for each transfer.
34 * @chip-select: Chipselect, distinguishing chips handled by "master". 35 * @chip-select: Chipselect, distinguishing chips handled by "master".
35 * @mode: The spi mode defines how data is clocked out and in. 36 * @mode: The spi mode defines how data is clocked out and in.
36 * This may be changed by the device's driver. 37 * This may be changed by the device's driver.
38 * The "active low" default for chipselect mode can be overridden,
39 * as can the "MSB first" default for each word in a transfer.
37 * @bits_per_word: Data transfers involve one or more words; word sizes 40 * @bits_per_word: Data transfers involve one or more words; word sizes
38 * like eight or 12 bits are common. In-memory wordsizes are 41 * like eight or 12 bits are common. In-memory wordsizes are
39 * powers of two bytes (e.g. 20 bit samples use 32 bits). 42 * powers of two bytes (e.g. 20 bit samples use 32 bits).
40 * This may be changed by the device's driver. 43 * This may be changed by the device's driver, or left at the
44 * default (0) indicating protocol words are eight bit bytes.
45 * The spi_transfer.bits_per_word can override this for each transfer.
41 * @irq: Negative, or the number passed to request_irq() to receive 46 * @irq: Negative, or the number passed to request_irq() to receive
42 * interrupts from this device. 47 * interrupts from this device.
43 * @controller_state: Controller's runtime state 48 * @controller_state: Controller's runtime state
44 * @controller_data: Board-specific definitions for controller, such as 49 * @controller_data: Board-specific definitions for controller, such as
45 * FIFO initialization parameters; from board_info.controller_data 50 * FIFO initialization parameters; from board_info.controller_data
46 * 51 *
47 * An spi_device is used to interchange data between an SPI slave 52 * An spi_device is used to interchange data between an SPI slave
48 * (usually a discrete chip) and CPU memory. 53 * (usually a discrete chip) and CPU memory.
@@ -65,6 +70,7 @@ struct spi_device {
65#define SPI_MODE_2 (SPI_CPOL|0) 70#define SPI_MODE_2 (SPI_CPOL|0)
66#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 71#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
67#define SPI_CS_HIGH 0x04 /* chipselect active high? */ 72#define SPI_CS_HIGH 0x04 /* chipselect active high? */
73#define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
68 u8 bits_per_word; 74 u8 bits_per_word;
69 int irq; 75 int irq;
70 void *controller_state; 76 void *controller_state;
@@ -73,7 +79,6 @@ struct spi_device {
73 79
74 // likely need more hooks for more protocol options affecting how 80 // likely need more hooks for more protocol options affecting how
75 // the controller talks to each chip, like: 81 // the controller talks to each chip, like:
76 // - bit order (default is wordwise msb-first)
77 // - memory packing (12 bit samples into low bits, others zeroed) 82 // - memory packing (12 bit samples into low bits, others zeroed)
78 // - priority 83 // - priority
79 // - drop chipselect after each word 84 // - drop chipselect after each word
@@ -143,13 +148,13 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
143 * struct spi_master - interface to SPI master controller 148 * struct spi_master - interface to SPI master controller
144 * @cdev: class interface to this driver 149 * @cdev: class interface to this driver
145 * @bus_num: board-specific (and often SOC-specific) identifier for a 150 * @bus_num: board-specific (and often SOC-specific) identifier for a
146 * given SPI controller. 151 * given SPI controller.
147 * @num_chipselect: chipselects are used to distinguish individual 152 * @num_chipselect: chipselects are used to distinguish individual
148 * SPI slaves, and are numbered from zero to num_chipselects. 153 * SPI slaves, and are numbered from zero to num_chipselects.
149 * each slave has a chipselect signal, but it's common that not 154 * each slave has a chipselect signal, but it's common that not
150 * every chipselect is connected to a slave. 155 * every chipselect is connected to a slave.
151 * @setup: updates the device mode and clocking records used by a 156 * @setup: updates the device mode and clocking records used by a
152 * device's SPI controller; protocol code may call this. 157 * device's SPI controller; protocol code may call this.
153 * @transfer: adds a message to the controller's transfer queue. 158 * @transfer: adds a message to the controller's transfer queue.
154 * @cleanup: frees controller-specific state 159 * @cleanup: frees controller-specific state
155 * 160 *
@@ -167,13 +172,13 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
167struct spi_master { 172struct spi_master {
168 struct class_device cdev; 173 struct class_device cdev;
169 174
170 /* other than zero (== assign one dynamically), bus_num is fully 175 /* other than negative (== assign one dynamically), bus_num is fully
171 * board-specific. usually that simplifies to being SOC-specific. 176 * board-specific. usually that simplifies to being SOC-specific.
172 * example: one SOC has three SPI controllers, numbered 1..3, 177 * example: one SOC has three SPI controllers, numbered 0..2,
173 * and one board's schematics might show it using SPI-2. software 178 * and one board's schematics might show it using SPI-2. software
174 * would normally use bus_num=2 for that controller. 179 * would normally use bus_num=2 for that controller.
175 */ 180 */
176 u16 bus_num; 181 s16 bus_num;
177 182
178 /* chipselects will be integral to many controllers; some others 183 /* chipselects will be integral to many controllers; some others
179 * might use board-specific GPIOs. 184 * might use board-specific GPIOs.
@@ -268,10 +273,14 @@ extern struct spi_master *spi_busnum_to_master(u16 busnum);
268 * @tx_dma: DMA address of tx_buf, if spi_message.is_dma_mapped 273 * @tx_dma: DMA address of tx_buf, if spi_message.is_dma_mapped
269 * @rx_dma: DMA address of rx_buf, if spi_message.is_dma_mapped 274 * @rx_dma: DMA address of rx_buf, if spi_message.is_dma_mapped
270 * @len: size of rx and tx buffers (in bytes) 275 * @len: size of rx and tx buffers (in bytes)
276 * @speed_hz: Select a speed other then the device default for this
277 * transfer. If 0 the default (from spi_device) is used.
278 * @bits_per_word: select a bits_per_word other then the device default
279 * for this transfer. If 0 the default (from spi_device) is used.
271 * @cs_change: affects chipselect after this transfer completes 280 * @cs_change: affects chipselect after this transfer completes
272 * @delay_usecs: microseconds to delay after this transfer before 281 * @delay_usecs: microseconds to delay after this transfer before
273 * (optionally) changing the chipselect status, then starting 282 * (optionally) changing the chipselect status, then starting
274 * the next transfer or completing this spi_message. 283 * the next transfer or completing this spi_message.
275 * @transfer_list: transfers are sequenced through spi_message.transfers 284 * @transfer_list: transfers are sequenced through spi_message.transfers
276 * 285 *
277 * SPI transfers always write the same number of bytes as they read. 286 * SPI transfers always write the same number of bytes as they read.
@@ -322,7 +331,9 @@ struct spi_transfer {
322 dma_addr_t rx_dma; 331 dma_addr_t rx_dma;
323 332
324 unsigned cs_change:1; 333 unsigned cs_change:1;
334 u8 bits_per_word;
325 u16 delay_usecs; 335 u16 delay_usecs;
336 u32 speed_hz;
326 337
327 struct list_head transfer_list; 338 struct list_head transfer_list;
328}; 339};
@@ -356,7 +367,7 @@ struct spi_transfer {
356 * and its transfers, ignore them until its completion callback. 367 * and its transfers, ignore them until its completion callback.
357 */ 368 */
358struct spi_message { 369struct spi_message {
359 struct list_head transfers; 370 struct list_head transfers;
360 371
361 struct spi_device *spi; 372 struct spi_device *spi;
362 373
@@ -374,7 +385,7 @@ struct spi_message {
374 */ 385 */
375 386
376 /* completion is reported through a callback */ 387 /* completion is reported through a callback */
377 void (*complete)(void *context); 388 void (*complete)(void *context);
378 void *context; 389 void *context;
379 unsigned actual_length; 390 unsigned actual_length;
380 int status; 391 int status;
diff --git a/include/linux/spi/spi_bitbang.h b/include/linux/spi/spi_bitbang.h
index c961fe9bf3e..16ce178f54d 100644
--- a/include/linux/spi/spi_bitbang.h
+++ b/include/linux/spi/spi_bitbang.h
@@ -30,6 +30,12 @@ struct spi_bitbang {
30 30
31 struct spi_master *master; 31 struct spi_master *master;
32 32
33 /* setup_transfer() changes clock and/or wordsize to match settings
34 * for this transfer; zeroes restore defaults from spi_device.
35 */
36 int (*setup_transfer)(struct spi_device *spi,
37 struct spi_transfer *t);
38
33 void (*chipselect)(struct spi_device *spi, int is_on); 39 void (*chipselect)(struct spi_device *spi, int is_on);
34#define BITBANG_CS_ACTIVE 1 /* normally nCS, active low */ 40#define BITBANG_CS_ACTIVE 1 /* normally nCS, active low */
35#define BITBANG_CS_INACTIVE 0 41#define BITBANG_CS_INACTIVE 0
@@ -51,6 +57,8 @@ struct spi_bitbang {
51extern int spi_bitbang_setup(struct spi_device *spi); 57extern int spi_bitbang_setup(struct spi_device *spi);
52extern void spi_bitbang_cleanup(const struct spi_device *spi); 58extern void spi_bitbang_cleanup(const struct spi_device *spi);
53extern int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m); 59extern int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m);
60extern int spi_bitbang_setup_transfer(struct spi_device *spi,
61 struct spi_transfer *t);
54 62
55/* start or stop queue processing */ 63/* start or stop queue processing */
56extern int spi_bitbang_start(struct spi_bitbang *spi); 64extern int spi_bitbang_start(struct spi_bitbang *spi);