blob: 70ff0072cb8a236cfbbb9226c0105e4a94325613 [file] [log] [blame]
Stephen Streete0c99052006-03-07 23:53:24 -08001/*
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>
Stephen Streete0c99052006-03-07 23:53:24 -080029#include <linux/delay.h>
eric miao2f1a74e2007-11-21 18:50:53 +080030#include <linux/clk.h>
Eric Miaoa7bb3902009-04-06 19:00:54 -070031#include <linux/gpio.h>
Stephen Streete0c99052006-03-07 23:53:24 -080032
33#include <asm/io.h>
34#include <asm/irq.h>
Stephen Streete0c99052006-03-07 23:53:24 -080035#include <asm/delay.h>
Stephen Streete0c99052006-03-07 23:53:24 -080036
Russell Kingdcea83a2008-11-29 11:40:28 +000037#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/regs-ssp.h>
39#include <mach/ssp.h>
40#include <mach/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080041
42MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080043MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080044MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070045MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080046
47#define MAX_BUSES 3
48
Vernon Sauderf1f640a2008-10-15 22:02:43 -070049#define RX_THRESH_DFLT 8
50#define TX_THRESH_DFLT 8
51#define TIMOUT_DFLT 1000
52
Ned Forrester7e964452008-09-13 02:33:18 -070053#define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
54#define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
Mike Rapoport20b918d2008-10-01 10:39:24 -070055#define IS_DMA_ALIGNED(x) ((((u32)(x)) & 0x07) == 0)
Ned Forrester7e964452008-09-13 02:33:18 -070056#define MAX_DMA_LEN 8191
Stephen Streete0c99052006-03-07 23:53:24 -080057
Ned Forresterb97c74b2008-02-23 15:23:40 -080058/*
59 * for testing SSCR1 changes that require SSP restart, basically
60 * everything except the service and interrupt enables, the pxa270 developer
61 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
62 * list, but the PXA255 dev man says all bits without really meaning the
63 * service and interrupt enables
64 */
65#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080066 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080067 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
68 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
69 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
70 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080071
Stephen Streete0c99052006-03-07 23:53:24 -080072#define DEFINE_SSP_REG(reg, off) \
David Brownellcf433692008-04-28 02:14:17 -070073static inline u32 read_##reg(void const __iomem *p) \
74{ return __raw_readl(p + (off)); } \
75\
76static inline void write_##reg(u32 v, void __iomem *p) \
77{ __raw_writel(v, p + (off)); }
Stephen Streete0c99052006-03-07 23:53:24 -080078
79DEFINE_SSP_REG(SSCR0, 0x00)
80DEFINE_SSP_REG(SSCR1, 0x04)
81DEFINE_SSP_REG(SSSR, 0x08)
82DEFINE_SSP_REG(SSITR, 0x0c)
83DEFINE_SSP_REG(SSDR, 0x10)
84DEFINE_SSP_REG(SSTO, 0x28)
85DEFINE_SSP_REG(SSPSP, 0x2c)
86
87#define START_STATE ((void*)0)
88#define RUNNING_STATE ((void*)1)
89#define DONE_STATE ((void*)2)
90#define ERROR_STATE ((void*)-1)
91
92#define QUEUE_RUNNING 0
93#define QUEUE_STOPPED 1
94
95struct driver_data {
96 /* Driver model hookup */
97 struct platform_device *pdev;
98
eric miao2f1a74e2007-11-21 18:50:53 +080099 /* SSP Info */
100 struct ssp_device *ssp;
101
Stephen Streete0c99052006-03-07 23:53:24 -0800102 /* SPI framework hookup */
103 enum pxa_ssp_type ssp_type;
104 struct spi_master *master;
105
106 /* PXA hookup */
107 struct pxa2xx_spi_master *master_info;
108
109 /* DMA setup stuff */
110 int rx_channel;
111 int tx_channel;
112 u32 *null_dma_buf;
113
114 /* SSP register addresses */
David Brownellcf433692008-04-28 02:14:17 -0700115 void __iomem *ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800116 u32 ssdr_physical;
117
118 /* SSP masks*/
119 u32 dma_cr1;
120 u32 int_cr1;
121 u32 clear_sr;
122 u32 mask_sr;
123
124 /* Driver message queue */
125 struct workqueue_struct *workqueue;
126 struct work_struct pump_messages;
127 spinlock_t lock;
128 struct list_head queue;
129 int busy;
130 int run;
131
132 /* Message Transfer pump */
133 struct tasklet_struct pump_transfers;
134
135 /* Current message transfer state info */
136 struct spi_message* cur_msg;
137 struct spi_transfer* cur_transfer;
138 struct chip_data *cur_chip;
139 size_t len;
140 void *tx;
141 void *tx_end;
142 void *rx;
143 void *rx_end;
144 int dma_mapped;
145 dma_addr_t rx_dma;
146 dma_addr_t tx_dma;
147 size_t rx_map_len;
148 size_t tx_map_len;
Stephen Street9708c122006-03-28 14:05:23 -0800149 u8 n_bytes;
150 u32 dma_width;
Stephen Street8d94cc52006-12-10 02:18:54 -0800151 int (*write)(struct driver_data *drv_data);
152 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800153 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
154 void (*cs_control)(u32 command);
155};
156
157struct chip_data {
158 u32 cr0;
159 u32 cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800160 u32 psp;
161 u32 timeout;
162 u8 n_bytes;
163 u32 dma_width;
164 u32 dma_burst_size;
165 u32 threshold;
166 u32 dma_threshold;
167 u8 enable_dma;
Stephen Street9708c122006-03-28 14:05:23 -0800168 u8 bits_per_word;
169 u32 speed_hz;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700170 int gpio_cs;
171 int gpio_cs_inverted;
Stephen Street8d94cc52006-12-10 02:18:54 -0800172 int (*write)(struct driver_data *drv_data);
173 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800174 void (*cs_control)(u32 command);
175};
176
David Howells6d5aefb2006-12-05 19:36:26 +0000177static void pump_messages(struct work_struct *work);
Stephen Streete0c99052006-03-07 23:53:24 -0800178
Eric Miaoa7bb3902009-04-06 19:00:54 -0700179static void cs_assert(struct driver_data *drv_data)
180{
181 struct chip_data *chip = drv_data->cur_chip;
182
183 if (chip->cs_control) {
184 chip->cs_control(PXA2XX_CS_ASSERT);
185 return;
186 }
187
188 if (gpio_is_valid(chip->gpio_cs))
189 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
190}
191
192static void cs_deassert(struct driver_data *drv_data)
193{
194 struct chip_data *chip = drv_data->cur_chip;
195
196 if (chip->cs_control) {
197 chip->cs_control(PXA2XX_CS_ASSERT);
198 return;
199 }
200
201 if (gpio_is_valid(chip->gpio_cs))
202 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
203}
204
Stephen Streete0c99052006-03-07 23:53:24 -0800205static int flush(struct driver_data *drv_data)
206{
207 unsigned long limit = loops_per_jiffy << 1;
208
David Brownellcf433692008-04-28 02:14:17 -0700209 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800210
211 do {
212 while (read_SSSR(reg) & SSSR_RNE) {
213 read_SSDR(reg);
214 }
215 } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
216 write_SSSR(SSSR_ROR, reg);
217
218 return limit;
219}
220
Stephen Street8d94cc52006-12-10 02:18:54 -0800221static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800222{
David Brownellcf433692008-04-28 02:14:17 -0700223 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800224 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800225
Stephen Street8d94cc52006-12-10 02:18:54 -0800226 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
227 || (drv_data->tx == drv_data->tx_end))
228 return 0;
229
230 write_SSDR(0, reg);
231 drv_data->tx += n_bytes;
232
233 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800234}
235
Stephen Street8d94cc52006-12-10 02:18:54 -0800236static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800237{
David Brownellcf433692008-04-28 02:14:17 -0700238 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800239 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800240
241 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800242 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800243 read_SSDR(reg);
244 drv_data->rx += n_bytes;
245 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800246
247 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800248}
249
Stephen Street8d94cc52006-12-10 02:18:54 -0800250static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800251{
David Brownellcf433692008-04-28 02:14:17 -0700252 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800253
Stephen Street8d94cc52006-12-10 02:18:54 -0800254 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
255 || (drv_data->tx == drv_data->tx_end))
256 return 0;
257
258 write_SSDR(*(u8 *)(drv_data->tx), reg);
259 ++drv_data->tx;
260
261 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800262}
263
Stephen Street8d94cc52006-12-10 02:18:54 -0800264static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800265{
David Brownellcf433692008-04-28 02:14:17 -0700266 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800267
268 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800269 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800270 *(u8 *)(drv_data->rx) = read_SSDR(reg);
271 ++drv_data->rx;
272 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800273
274 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800275}
276
Stephen Street8d94cc52006-12-10 02:18:54 -0800277static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800278{
David Brownellcf433692008-04-28 02:14:17 -0700279 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800280
Stephen Street8d94cc52006-12-10 02:18:54 -0800281 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
282 || (drv_data->tx == drv_data->tx_end))
283 return 0;
284
285 write_SSDR(*(u16 *)(drv_data->tx), reg);
286 drv_data->tx += 2;
287
288 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800289}
290
Stephen Street8d94cc52006-12-10 02:18:54 -0800291static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800292{
David Brownellcf433692008-04-28 02:14:17 -0700293 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800294
295 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800296 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800297 *(u16 *)(drv_data->rx) = read_SSDR(reg);
298 drv_data->rx += 2;
299 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800300
301 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800302}
Stephen Street8d94cc52006-12-10 02:18:54 -0800303
304static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800305{
David Brownellcf433692008-04-28 02:14:17 -0700306 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800307
Stephen Street8d94cc52006-12-10 02:18:54 -0800308 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
309 || (drv_data->tx == drv_data->tx_end))
310 return 0;
311
312 write_SSDR(*(u32 *)(drv_data->tx), reg);
313 drv_data->tx += 4;
314
315 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800316}
317
Stephen Street8d94cc52006-12-10 02:18:54 -0800318static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800319{
David Brownellcf433692008-04-28 02:14:17 -0700320 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800321
322 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800323 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800324 *(u32 *)(drv_data->rx) = read_SSDR(reg);
325 drv_data->rx += 4;
326 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800327
328 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800329}
330
331static void *next_transfer(struct driver_data *drv_data)
332{
333 struct spi_message *msg = drv_data->cur_msg;
334 struct spi_transfer *trans = drv_data->cur_transfer;
335
336 /* Move to next transfer */
337 if (trans->transfer_list.next != &msg->transfers) {
338 drv_data->cur_transfer =
339 list_entry(trans->transfer_list.next,
340 struct spi_transfer,
341 transfer_list);
342 return RUNNING_STATE;
343 } else
344 return DONE_STATE;
345}
346
347static int map_dma_buffers(struct driver_data *drv_data)
348{
349 struct spi_message *msg = drv_data->cur_msg;
350 struct device *dev = &msg->spi->dev;
351
352 if (!drv_data->cur_chip->enable_dma)
353 return 0;
354
355 if (msg->is_dma_mapped)
356 return drv_data->rx_dma && drv_data->tx_dma;
357
358 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
359 return 0;
360
361 /* Modify setup if rx buffer is null */
362 if (drv_data->rx == NULL) {
363 *drv_data->null_dma_buf = 0;
364 drv_data->rx = drv_data->null_dma_buf;
365 drv_data->rx_map_len = 4;
366 } else
367 drv_data->rx_map_len = drv_data->len;
368
369
370 /* Modify setup if tx buffer is null */
371 if (drv_data->tx == NULL) {
372 *drv_data->null_dma_buf = 0;
373 drv_data->tx = drv_data->null_dma_buf;
374 drv_data->tx_map_len = 4;
375 } else
376 drv_data->tx_map_len = drv_data->len;
377
Ned Forrester393df742008-11-19 15:36:21 -0800378 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
379 * so we flush the cache *before* invalidating it, in case
380 * the tx and rx buffers overlap.
381 */
382 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
383 drv_data->tx_map_len, DMA_TO_DEVICE);
384 if (dma_mapping_error(dev, drv_data->tx_dma))
Stephen Streete0c99052006-03-07 23:53:24 -0800385 return 0;
386
Ned Forrester393df742008-11-19 15:36:21 -0800387 /* Stream map the rx buffer */
388 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
Stephen Streete0c99052006-03-07 23:53:24 -0800389 drv_data->rx_map_len, DMA_FROM_DEVICE);
Ned Forrester393df742008-11-19 15:36:21 -0800390 if (dma_mapping_error(dev, drv_data->rx_dma)) {
391 dma_unmap_single(dev, drv_data->tx_dma,
392 drv_data->tx_map_len, DMA_TO_DEVICE);
Stephen Streete0c99052006-03-07 23:53:24 -0800393 return 0;
394 }
395
396 return 1;
397}
398
399static void unmap_dma_buffers(struct driver_data *drv_data)
400{
401 struct device *dev;
402
403 if (!drv_data->dma_mapped)
404 return;
405
406 if (!drv_data->cur_msg->is_dma_mapped) {
407 dev = &drv_data->cur_msg->spi->dev;
408 dma_unmap_single(dev, drv_data->rx_dma,
409 drv_data->rx_map_len, DMA_FROM_DEVICE);
410 dma_unmap_single(dev, drv_data->tx_dma,
411 drv_data->tx_map_len, DMA_TO_DEVICE);
412 }
413
414 drv_data->dma_mapped = 0;
415}
416
417/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700418static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800419{
420 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700421 unsigned long flags;
422 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800423
Stephen Street5daa3ba2006-05-20 15:00:19 -0700424 spin_lock_irqsave(&drv_data->lock, flags);
425 msg = drv_data->cur_msg;
426 drv_data->cur_msg = NULL;
427 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700428 queue_work(drv_data->workqueue, &drv_data->pump_messages);
429 spin_unlock_irqrestore(&drv_data->lock, flags);
430
431 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800432 struct spi_transfer,
433 transfer_list);
434
Ned Forrester84235972008-09-13 02:33:17 -0700435 /* Delay if requested before any change in chip select */
436 if (last_transfer->delay_usecs)
437 udelay(last_transfer->delay_usecs);
438
439 /* Drop chip select UNLESS cs_change is true or we are returning
440 * a message with an error, or next message is for another chip
441 */
Stephen Streete0c99052006-03-07 23:53:24 -0800442 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700443 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700444 else {
445 struct spi_message *next_msg;
446
447 /* Holding of cs was hinted, but we need to make sure
448 * the next message is for the same chip. Don't waste
449 * time with the following tests unless this was hinted.
450 *
451 * We cannot postpone this until pump_messages, because
452 * after calling msg->complete (below) the driver that
453 * sent the current message could be unloaded, which
454 * could invalidate the cs_control() callback...
455 */
456
457 /* get a pointer to the next message, if any */
458 spin_lock_irqsave(&drv_data->lock, flags);
459 if (list_empty(&drv_data->queue))
460 next_msg = NULL;
461 else
462 next_msg = list_entry(drv_data->queue.next,
463 struct spi_message, queue);
464 spin_unlock_irqrestore(&drv_data->lock, flags);
465
466 /* see if the next and current messages point
467 * to the same chip
468 */
469 if (next_msg && next_msg->spi != msg->spi)
470 next_msg = NULL;
471 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700472 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700473 }
Stephen Streete0c99052006-03-07 23:53:24 -0800474
Stephen Street5daa3ba2006-05-20 15:00:19 -0700475 msg->state = NULL;
476 if (msg->complete)
477 msg->complete(msg->context);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700478
479 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800480}
481
David Brownellcf433692008-04-28 02:14:17 -0700482static int wait_ssp_rx_stall(void const __iomem *ioaddr)
Stephen Streete0c99052006-03-07 23:53:24 -0800483{
484 unsigned long limit = loops_per_jiffy << 1;
485
486 while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
487 cpu_relax();
488
489 return limit;
490}
491
492static int wait_dma_channel_stop(int channel)
493{
494 unsigned long limit = loops_per_jiffy << 1;
495
496 while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
497 cpu_relax();
498
499 return limit;
500}
501
David Brownellcf433692008-04-28 02:14:17 -0700502static void dma_error_stop(struct driver_data *drv_data, const char *msg)
Stephen Street8d94cc52006-12-10 02:18:54 -0800503{
David Brownellcf433692008-04-28 02:14:17 -0700504 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800505
506 /* Stop and reset */
507 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
508 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
509 write_SSSR(drv_data->clear_sr, reg);
510 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
511 if (drv_data->ssp_type != PXA25x_SSP)
512 write_SSTO(0, reg);
513 flush(drv_data);
514 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
515
516 unmap_dma_buffers(drv_data);
517
518 dev_err(&drv_data->pdev->dev, "%s\n", msg);
519
520 drv_data->cur_msg->state = ERROR_STATE;
521 tasklet_schedule(&drv_data->pump_transfers);
522}
523
524static void dma_transfer_complete(struct driver_data *drv_data)
525{
David Brownellcf433692008-04-28 02:14:17 -0700526 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800527 struct spi_message *msg = drv_data->cur_msg;
528
529 /* Clear and disable interrupts on SSP and DMA channels*/
530 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
531 write_SSSR(drv_data->clear_sr, reg);
532 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
533 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
534
535 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
536 dev_err(&drv_data->pdev->dev,
537 "dma_handler: dma rx channel stop failed\n");
538
539 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
540 dev_err(&drv_data->pdev->dev,
541 "dma_transfer: ssp rx stall failed\n");
542
543 unmap_dma_buffers(drv_data);
544
545 /* update the buffer pointer for the amount completed in dma */
546 drv_data->rx += drv_data->len -
547 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
548
549 /* read trailing data from fifo, it does not matter how many
550 * bytes are in the fifo just read until buffer is full
551 * or fifo is empty, which ever occurs first */
552 drv_data->read(drv_data);
553
554 /* return count of what was actually read */
555 msg->actual_length += drv_data->len -
556 (drv_data->rx_end - drv_data->rx);
557
Ned Forrester84235972008-09-13 02:33:17 -0700558 /* Transfer delays and chip select release are
559 * handled in pump_transfers or giveback
560 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800561
562 /* Move to next transfer */
563 msg->state = next_transfer(drv_data);
564
565 /* Schedule transfer tasklet */
566 tasklet_schedule(&drv_data->pump_transfers);
567}
568
David Howells7d12e782006-10-05 14:55:46 +0100569static void dma_handler(int channel, void *data)
Stephen Streete0c99052006-03-07 23:53:24 -0800570{
571 struct driver_data *drv_data = data;
Stephen Streete0c99052006-03-07 23:53:24 -0800572 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
Stephen Streete0c99052006-03-07 23:53:24 -0800573
574 if (irq_status & DCSR_BUSERR) {
575
Stephen Streete0c99052006-03-07 23:53:24 -0800576 if (channel == drv_data->tx_channel)
Stephen Street8d94cc52006-12-10 02:18:54 -0800577 dma_error_stop(drv_data,
578 "dma_handler: "
579 "bad bus address on tx channel");
Stephen Streete0c99052006-03-07 23:53:24 -0800580 else
Stephen Street8d94cc52006-12-10 02:18:54 -0800581 dma_error_stop(drv_data,
582 "dma_handler: "
583 "bad bus address on rx channel");
584 return;
Stephen Streete0c99052006-03-07 23:53:24 -0800585 }
586
587 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
Stephen Street8d94cc52006-12-10 02:18:54 -0800588 if ((channel == drv_data->tx_channel)
589 && (irq_status & DCSR_ENDINTR)
590 && (drv_data->ssp_type == PXA25x_SSP)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800591
592 /* Wait for rx to stall */
593 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
594 dev_err(&drv_data->pdev->dev,
595 "dma_handler: ssp rx stall failed\n");
596
Stephen Street8d94cc52006-12-10 02:18:54 -0800597 /* finish this transfer, start the next */
598 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800599 }
600}
601
602static irqreturn_t dma_transfer(struct driver_data *drv_data)
603{
604 u32 irq_status;
David Brownellcf433692008-04-28 02:14:17 -0700605 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800606
607 irq_status = read_SSSR(reg) & drv_data->mask_sr;
608 if (irq_status & SSSR_ROR) {
Stephen Street8d94cc52006-12-10 02:18:54 -0800609 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
Stephen Streete0c99052006-03-07 23:53:24 -0800610 return IRQ_HANDLED;
611 }
612
613 /* Check for false positive timeout */
Stephen Street8d94cc52006-12-10 02:18:54 -0800614 if ((irq_status & SSSR_TINT)
615 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800616 write_SSSR(SSSR_TINT, reg);
617 return IRQ_HANDLED;
618 }
619
620 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
621
Stephen Street8d94cc52006-12-10 02:18:54 -0800622 /* Clear and disable timeout interrupt, do the rest in
623 * dma_transfer_complete */
Stephen Streete0c99052006-03-07 23:53:24 -0800624 if (drv_data->ssp_type != PXA25x_SSP)
625 write_SSTO(0, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800626
Stephen Street8d94cc52006-12-10 02:18:54 -0800627 /* finish this transfer, start the next */
628 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800629
630 return IRQ_HANDLED;
631 }
632
633 /* Opps problem detected */
634 return IRQ_NONE;
635}
636
Stephen Street8d94cc52006-12-10 02:18:54 -0800637static void int_error_stop(struct driver_data *drv_data, const char* msg)
638{
David Brownellcf433692008-04-28 02:14:17 -0700639 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800640
641 /* Stop and reset SSP */
642 write_SSSR(drv_data->clear_sr, reg);
643 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
644 if (drv_data->ssp_type != PXA25x_SSP)
645 write_SSTO(0, reg);
646 flush(drv_data);
647 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
648
649 dev_err(&drv_data->pdev->dev, "%s\n", msg);
650
651 drv_data->cur_msg->state = ERROR_STATE;
652 tasklet_schedule(&drv_data->pump_transfers);
653}
654
655static void int_transfer_complete(struct driver_data *drv_data)
656{
David Brownellcf433692008-04-28 02:14:17 -0700657 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800658
659 /* Stop SSP */
660 write_SSSR(drv_data->clear_sr, reg);
661 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
662 if (drv_data->ssp_type != PXA25x_SSP)
663 write_SSTO(0, reg);
664
665 /* Update total byte transfered return count actual bytes read */
666 drv_data->cur_msg->actual_length += drv_data->len -
667 (drv_data->rx_end - drv_data->rx);
668
Ned Forrester84235972008-09-13 02:33:17 -0700669 /* Transfer delays and chip select release are
670 * handled in pump_transfers or giveback
671 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800672
673 /* Move to next transfer */
674 drv_data->cur_msg->state = next_transfer(drv_data);
675
676 /* Schedule transfer tasklet */
677 tasklet_schedule(&drv_data->pump_transfers);
678}
679
Stephen Streete0c99052006-03-07 23:53:24 -0800680static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
681{
David Brownellcf433692008-04-28 02:14:17 -0700682 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800683
Stephen Street5daa3ba2006-05-20 15:00:19 -0700684 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
685 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800686
Stephen Street8d94cc52006-12-10 02:18:54 -0800687 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800688
Stephen Street8d94cc52006-12-10 02:18:54 -0800689 if (irq_status & SSSR_ROR) {
690 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
691 return IRQ_HANDLED;
692 }
Stephen Streete0c99052006-03-07 23:53:24 -0800693
Stephen Street8d94cc52006-12-10 02:18:54 -0800694 if (irq_status & SSSR_TINT) {
695 write_SSSR(SSSR_TINT, reg);
696 if (drv_data->read(drv_data)) {
697 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800698 return IRQ_HANDLED;
699 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800700 }
Stephen Streete0c99052006-03-07 23:53:24 -0800701
Stephen Street8d94cc52006-12-10 02:18:54 -0800702 /* Drain rx fifo, Fill tx fifo and prevent overruns */
703 do {
704 if (drv_data->read(drv_data)) {
705 int_transfer_complete(drv_data);
706 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800707 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800708 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800709
Stephen Street8d94cc52006-12-10 02:18:54 -0800710 if (drv_data->read(drv_data)) {
711 int_transfer_complete(drv_data);
712 return IRQ_HANDLED;
713 }
Stephen Streete0c99052006-03-07 23:53:24 -0800714
Stephen Street8d94cc52006-12-10 02:18:54 -0800715 if (drv_data->tx == drv_data->tx_end) {
716 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
717 /* PXA25x_SSP has no timeout, read trailing bytes */
718 if (drv_data->ssp_type == PXA25x_SSP) {
719 if (!wait_ssp_rx_stall(reg))
720 {
721 int_error_stop(drv_data, "interrupt_transfer: "
722 "rx stall failed");
723 return IRQ_HANDLED;
724 }
725 if (!drv_data->read(drv_data))
726 {
727 int_error_stop(drv_data,
728 "interrupt_transfer: "
729 "trailing byte read failed");
730 return IRQ_HANDLED;
731 }
732 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800733 }
Stephen Streete0c99052006-03-07 23:53:24 -0800734 }
735
Stephen Street5daa3ba2006-05-20 15:00:19 -0700736 /* We did something */
737 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800738}
739
David Howells7d12e782006-10-05 14:55:46 +0100740static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800741{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400742 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700743 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800744
745 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700746
747 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
748 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
749 if (drv_data->ssp_type != PXA25x_SSP)
750 write_SSTO(0, reg);
751 write_SSSR(drv_data->clear_sr, reg);
752
Stephen Streete0c99052006-03-07 23:53:24 -0800753 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800754 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700755
Stephen Streete0c99052006-03-07 23:53:24 -0800756 /* Never fail */
757 return IRQ_HANDLED;
758 }
759
760 return drv_data->transfer_handler(drv_data);
761}
762
David Brownellcf433692008-04-28 02:14:17 -0700763static int set_dma_burst_and_threshold(struct chip_data *chip,
764 struct spi_device *spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800765 u8 bits_per_word, u32 *burst_code,
766 u32 *threshold)
767{
768 struct pxa2xx_spi_chip *chip_info =
769 (struct pxa2xx_spi_chip *)spi->controller_data;
770 int bytes_per_word;
771 int burst_bytes;
772 int thresh_words;
773 int req_burst_size;
774 int retval = 0;
775
776 /* Set the threshold (in registers) to equal the same amount of data
777 * as represented by burst size (in bytes). The computation below
778 * is (burst_size rounded up to nearest 8 byte, word or long word)
779 * divided by (bytes/register); the tx threshold is the inverse of
780 * the rx, so that there will always be enough data in the rx fifo
781 * to satisfy a burst, and there will always be enough space in the
782 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
783 * there is not enough space), there must always remain enough empty
784 * space in the rx fifo for any data loaded to the tx fifo.
785 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
786 * will be 8, or half the fifo;
787 * The threshold can only be set to 2, 4 or 8, but not 16, because
788 * to burst 16 to the tx fifo, the fifo would have to be empty;
789 * however, the minimum fifo trigger level is 1, and the tx will
790 * request service when the fifo is at this level, with only 15 spaces.
791 */
792
793 /* find bytes/word */
794 if (bits_per_word <= 8)
795 bytes_per_word = 1;
796 else if (bits_per_word <= 16)
797 bytes_per_word = 2;
798 else
799 bytes_per_word = 4;
800
801 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
802 if (chip_info)
803 req_burst_size = chip_info->dma_burst_size;
804 else {
805 switch (chip->dma_burst_size) {
806 default:
807 /* if the default burst size is not set,
808 * do it now */
809 chip->dma_burst_size = DCMD_BURST8;
810 case DCMD_BURST8:
811 req_burst_size = 8;
812 break;
813 case DCMD_BURST16:
814 req_burst_size = 16;
815 break;
816 case DCMD_BURST32:
817 req_burst_size = 32;
818 break;
819 }
820 }
821 if (req_burst_size <= 8) {
822 *burst_code = DCMD_BURST8;
823 burst_bytes = 8;
824 } else if (req_burst_size <= 16) {
825 if (bytes_per_word == 1) {
826 /* don't burst more than 1/2 the fifo */
827 *burst_code = DCMD_BURST8;
828 burst_bytes = 8;
829 retval = 1;
830 } else {
831 *burst_code = DCMD_BURST16;
832 burst_bytes = 16;
833 }
834 } else {
835 if (bytes_per_word == 1) {
836 /* don't burst more than 1/2 the fifo */
837 *burst_code = DCMD_BURST8;
838 burst_bytes = 8;
839 retval = 1;
840 } else if (bytes_per_word == 2) {
841 /* don't burst more than 1/2 the fifo */
842 *burst_code = DCMD_BURST16;
843 burst_bytes = 16;
844 retval = 1;
845 } else {
846 *burst_code = DCMD_BURST32;
847 burst_bytes = 32;
848 }
849 }
850
851 thresh_words = burst_bytes / bytes_per_word;
852
853 /* thresh_words will be between 2 and 8 */
854 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
855 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
856
857 return retval;
858}
859
eric miao2f1a74e2007-11-21 18:50:53 +0800860static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
861{
862 unsigned long ssp_clk = clk_get_rate(ssp->clk);
863
864 if (ssp->type == PXA25x_SSP)
865 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
866 else
867 return ((ssp_clk / rate - 1) & 0xfff) << 8;
868}
869
Stephen Streete0c99052006-03-07 23:53:24 -0800870static void pump_transfers(unsigned long data)
871{
872 struct driver_data *drv_data = (struct driver_data *)data;
873 struct spi_message *message = NULL;
874 struct spi_transfer *transfer = NULL;
875 struct spi_transfer *previous = NULL;
876 struct chip_data *chip = NULL;
eric miao2f1a74e2007-11-21 18:50:53 +0800877 struct ssp_device *ssp = drv_data->ssp;
David Brownellcf433692008-04-28 02:14:17 -0700878 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800879 u32 clk_div = 0;
880 u8 bits = 0;
881 u32 speed = 0;
882 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800883 u32 cr1;
884 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
885 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800886
887 /* Get current state information */
888 message = drv_data->cur_msg;
889 transfer = drv_data->cur_transfer;
890 chip = drv_data->cur_chip;
891
892 /* Handle for abort */
893 if (message->state == ERROR_STATE) {
894 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700895 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800896 return;
897 }
898
899 /* Handle end of message */
900 if (message->state == DONE_STATE) {
901 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700902 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800903 return;
904 }
905
Ned Forrester84235972008-09-13 02:33:17 -0700906 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800907 if (message->state == RUNNING_STATE) {
908 previous = list_entry(transfer->transfer_list.prev,
909 struct spi_transfer,
910 transfer_list);
911 if (previous->delay_usecs)
912 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700913
914 /* Drop chip select only if cs_change is requested */
915 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700916 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800917 }
918
Ned Forrester7e964452008-09-13 02:33:18 -0700919 /* Check for transfers that need multiple DMA segments */
920 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
921
922 /* reject already-mapped transfers; PIO won't always work */
923 if (message->is_dma_mapped
924 || transfer->rx_dma || transfer->tx_dma) {
925 dev_err(&drv_data->pdev->dev,
926 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700927 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700928 transfer->len, MAX_DMA_LEN);
929 message->status = -EINVAL;
930 giveback(drv_data);
931 return;
932 }
933
934 /* warn ... we force this to PIO mode */
935 if (printk_ratelimit())
936 dev_warn(&message->spi->dev, "pump_transfers: "
937 "DMA disabled for transfer length %ld "
938 "greater than %d\n",
939 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800940 }
941
Stephen Streete0c99052006-03-07 23:53:24 -0800942 /* Setup the transfer state based on the type of transfer */
943 if (flush(drv_data) == 0) {
944 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
945 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700946 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800947 return;
948 }
Stephen Street9708c122006-03-28 14:05:23 -0800949 drv_data->n_bytes = chip->n_bytes;
950 drv_data->dma_width = chip->dma_width;
Stephen Streete0c99052006-03-07 23:53:24 -0800951 drv_data->tx = (void *)transfer->tx_buf;
952 drv_data->tx_end = drv_data->tx + transfer->len;
953 drv_data->rx = transfer->rx_buf;
954 drv_data->rx_end = drv_data->rx + transfer->len;
955 drv_data->rx_dma = transfer->rx_dma;
956 drv_data->tx_dma = transfer->tx_dma;
Stephen Street8d94cc52006-12-10 02:18:54 -0800957 drv_data->len = transfer->len & DCMD_LENGTH;
Stephen Streete0c99052006-03-07 23:53:24 -0800958 drv_data->write = drv_data->tx ? chip->write : null_writer;
959 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800960
961 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800962 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800963 if (transfer->speed_hz || transfer->bits_per_word) {
964
Stephen Street9708c122006-03-28 14:05:23 -0800965 bits = chip->bits_per_word;
966 speed = chip->speed_hz;
967
968 if (transfer->speed_hz)
969 speed = transfer->speed_hz;
970
971 if (transfer->bits_per_word)
972 bits = transfer->bits_per_word;
973
eric miao2f1a74e2007-11-21 18:50:53 +0800974 clk_div = ssp_get_clk_div(ssp, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800975
976 if (bits <= 8) {
977 drv_data->n_bytes = 1;
978 drv_data->dma_width = DCMD_WIDTH1;
979 drv_data->read = drv_data->read != null_reader ?
980 u8_reader : null_reader;
981 drv_data->write = drv_data->write != null_writer ?
982 u8_writer : null_writer;
983 } else if (bits <= 16) {
984 drv_data->n_bytes = 2;
985 drv_data->dma_width = DCMD_WIDTH2;
986 drv_data->read = drv_data->read != null_reader ?
987 u16_reader : null_reader;
988 drv_data->write = drv_data->write != null_writer ?
989 u16_writer : null_writer;
990 } else if (bits <= 32) {
991 drv_data->n_bytes = 4;
992 drv_data->dma_width = DCMD_WIDTH4;
993 drv_data->read = drv_data->read != null_reader ?
994 u32_reader : null_reader;
995 drv_data->write = drv_data->write != null_writer ?
996 u32_writer : null_writer;
997 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800998 /* if bits/word is changed in dma mode, then must check the
999 * thresholds and burst also */
1000 if (chip->enable_dma) {
1001 if (set_dma_burst_and_threshold(chip, message->spi,
1002 bits, &dma_burst,
1003 &dma_thresh))
1004 if (printk_ratelimit())
1005 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -07001006 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -08001007 "DMA burst size reduced to "
1008 "match bits_per_word\n");
1009 }
Stephen Street9708c122006-03-28 14:05:23 -08001010
1011 cr0 = clk_div
1012 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001013 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -08001014 | SSCR0_SSE
1015 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -08001016 }
1017
Stephen Streete0c99052006-03-07 23:53:24 -08001018 message->state = RUNNING_STATE;
1019
Ned Forrester7e964452008-09-13 02:33:18 -07001020 /* Try to map dma buffer and do a dma transfer if successful, but
1021 * only if the length is non-zero and less than MAX_DMA_LEN.
1022 *
1023 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
1024 * of PIO instead. Care is needed above because the transfer may
1025 * have have been passed with buffers that are already dma mapped.
1026 * A zero-length transfer in PIO mode will not try to write/read
1027 * to/from the buffers
1028 *
1029 * REVISIT large transfers are exactly where we most want to be
1030 * using DMA. If this happens much, split those transfers into
1031 * multiple DMA segments rather than forcing PIO.
1032 */
1033 drv_data->dma_mapped = 0;
1034 if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN)
1035 drv_data->dma_mapped = map_dma_buffers(drv_data);
1036 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001037
1038 /* Ensure we have the correct interrupt handler */
1039 drv_data->transfer_handler = dma_transfer;
1040
1041 /* Setup rx DMA Channel */
1042 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
1043 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
1044 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
1045 if (drv_data->rx == drv_data->null_dma_buf)
1046 /* No target address increment */
1047 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001048 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001049 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001050 | drv_data->len;
1051 else
1052 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
1053 | DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001054 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001055 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001056 | drv_data->len;
1057
1058 /* Setup tx DMA Channel */
1059 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
1060 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
1061 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
1062 if (drv_data->tx == drv_data->null_dma_buf)
1063 /* No source address increment */
1064 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001065 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001066 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001067 | drv_data->len;
1068 else
1069 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
1070 | DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001071 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001072 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001073 | drv_data->len;
1074
1075 /* Enable dma end irqs on SSP to detect end of transfer */
1076 if (drv_data->ssp_type == PXA25x_SSP)
1077 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
1078
Stephen Street8d94cc52006-12-10 02:18:54 -08001079 /* Clear status and start DMA engine */
1080 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001081 write_SSSR(drv_data->clear_sr, reg);
1082 DCSR(drv_data->rx_channel) |= DCSR_RUN;
1083 DCSR(drv_data->tx_channel) |= DCSR_RUN;
Stephen Streete0c99052006-03-07 23:53:24 -08001084 } else {
1085 /* Ensure we have the correct interrupt handler */
1086 drv_data->transfer_handler = interrupt_transfer;
1087
Stephen Street8d94cc52006-12-10 02:18:54 -08001088 /* Clear status */
1089 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001090 write_SSSR(drv_data->clear_sr, reg);
Stephen Street8d94cc52006-12-10 02:18:54 -08001091 }
1092
1093 /* see if we need to reload the config registers */
1094 if ((read_SSCR0(reg) != cr0)
1095 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1096 (cr1 & SSCR1_CHANGE_MASK)) {
1097
Ned Forresterb97c74b2008-02-23 15:23:40 -08001098 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -08001099 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001100 if (drv_data->ssp_type != PXA25x_SSP)
1101 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001102 /* first set CR1 without interrupt and service enables */
1103 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1104 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -08001105 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001106
Stephen Street8d94cc52006-12-10 02:18:54 -08001107 } else {
1108 if (drv_data->ssp_type != PXA25x_SSP)
1109 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001110 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001111
Eric Miaoa7bb3902009-04-06 19:00:54 -07001112 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001113
1114 /* after chip select, release the data by enabling service
1115 * requests and interrupts, without changing any mode bits */
1116 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001117}
1118
David Howells6d5aefb2006-12-05 19:36:26 +00001119static void pump_messages(struct work_struct *work)
Stephen Streete0c99052006-03-07 23:53:24 -08001120{
David Howells6d5aefb2006-12-05 19:36:26 +00001121 struct driver_data *drv_data =
1122 container_of(work, struct driver_data, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001123 unsigned long flags;
1124
1125 /* Lock queue and check for queue work */
1126 spin_lock_irqsave(&drv_data->lock, flags);
1127 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1128 drv_data->busy = 0;
1129 spin_unlock_irqrestore(&drv_data->lock, flags);
1130 return;
1131 }
1132
1133 /* Make sure we are not already running a message */
1134 if (drv_data->cur_msg) {
1135 spin_unlock_irqrestore(&drv_data->lock, flags);
1136 return;
1137 }
1138
1139 /* Extract head of queue */
1140 drv_data->cur_msg = list_entry(drv_data->queue.next,
1141 struct spi_message, queue);
1142 list_del_init(&drv_data->cur_msg->queue);
Stephen Streete0c99052006-03-07 23:53:24 -08001143
1144 /* Initial message state*/
1145 drv_data->cur_msg->state = START_STATE;
1146 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1147 struct spi_transfer,
1148 transfer_list);
1149
Stephen Street8d94cc52006-12-10 02:18:54 -08001150 /* prepare to setup the SSP, in pump_transfers, using the per
1151 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001152 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001153
1154 /* Mark as busy and launch transfers */
1155 tasklet_schedule(&drv_data->pump_transfers);
Stephen Street5daa3ba2006-05-20 15:00:19 -07001156
1157 drv_data->busy = 1;
1158 spin_unlock_irqrestore(&drv_data->lock, flags);
Stephen Streete0c99052006-03-07 23:53:24 -08001159}
1160
1161static int transfer(struct spi_device *spi, struct spi_message *msg)
1162{
1163 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1164 unsigned long flags;
1165
1166 spin_lock_irqsave(&drv_data->lock, flags);
1167
1168 if (drv_data->run == QUEUE_STOPPED) {
1169 spin_unlock_irqrestore(&drv_data->lock, flags);
1170 return -ESHUTDOWN;
1171 }
1172
1173 msg->actual_length = 0;
1174 msg->status = -EINPROGRESS;
1175 msg->state = START_STATE;
1176
1177 list_add_tail(&msg->queue, &drv_data->queue);
1178
1179 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1180 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1181
1182 spin_unlock_irqrestore(&drv_data->lock, flags);
1183
1184 return 0;
1185}
1186
David Brownelldccd5732007-07-17 04:04:02 -07001187/* the spi->mode bits understood by this driver: */
1188#define MODEBITS (SPI_CPOL | SPI_CPHA)
1189
Eric Miaoa7bb3902009-04-06 19:00:54 -07001190static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1191 struct pxa2xx_spi_chip *chip_info)
1192{
1193 int err = 0;
1194
1195 if (chip == NULL || chip_info == NULL)
1196 return 0;
1197
1198 /* NOTE: setup() can be called multiple times, possibly with
1199 * different chip_info, release previously requested GPIO
1200 */
1201 if (gpio_is_valid(chip->gpio_cs))
1202 gpio_free(chip->gpio_cs);
1203
1204 /* If (*cs_control) is provided, ignore GPIO chip select */
1205 if (chip_info->cs_control) {
1206 chip->cs_control = chip_info->cs_control;
1207 return 0;
1208 }
1209
1210 if (gpio_is_valid(chip_info->gpio_cs)) {
1211 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1212 if (err) {
1213 dev_err(&spi->dev, "failed to request chip select "
1214 "GPIO%d\n", chip_info->gpio_cs);
1215 return err;
1216 }
1217
1218 chip->gpio_cs = chip_info->gpio_cs;
1219 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1220
1221 err = gpio_direction_output(chip->gpio_cs,
1222 !chip->gpio_cs_inverted);
1223 }
1224
1225 return err;
1226}
1227
Stephen Streete0c99052006-03-07 23:53:24 -08001228static int setup(struct spi_device *spi)
1229{
1230 struct pxa2xx_spi_chip *chip_info = NULL;
1231 struct chip_data *chip;
1232 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
eric miao2f1a74e2007-11-21 18:50:53 +08001233 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001234 unsigned int clk_div;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001235 uint tx_thres = TX_THRESH_DFLT;
1236 uint rx_thres = RX_THRESH_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001237
1238 if (!spi->bits_per_word)
1239 spi->bits_per_word = 8;
1240
1241 if (drv_data->ssp_type != PXA25x_SSP
Stephen Street8d94cc52006-12-10 02:18:54 -08001242 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1243 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1244 "b/w not 4-32 for type non-PXA25x_SSP\n",
1245 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001246 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001247 }
1248 else if (drv_data->ssp_type == PXA25x_SSP
1249 && (spi->bits_per_word < 4
1250 || spi->bits_per_word > 16)) {
1251 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1252 "b/w not 4-16 for type PXA25x_SSP\n",
1253 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001254 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001255 }
Stephen Streete0c99052006-03-07 23:53:24 -08001256
David Brownelldccd5732007-07-17 04:04:02 -07001257 if (spi->mode & ~MODEBITS) {
1258 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
1259 spi->mode & ~MODEBITS);
1260 return -EINVAL;
1261 }
1262
Stephen Street8d94cc52006-12-10 02:18:54 -08001263 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001264 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001265 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001266 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -08001267 if (!chip) {
1268 dev_err(&spi->dev,
1269 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001270 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -08001271 }
Stephen Streete0c99052006-03-07 23:53:24 -08001272
Eric Miaoa7bb3902009-04-06 19:00:54 -07001273 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001274 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001275 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001276 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1277 DCMD_BURST8 : 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001278 }
1279
Stephen Street8d94cc52006-12-10 02:18:54 -08001280 /* protocol drivers may change the chip settings, so...
1281 * if chip_info exists, use it */
1282 chip_info = spi->controller_data;
1283
Stephen Streete0c99052006-03-07 23:53:24 -08001284 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001285 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001286 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001287 if (chip_info->timeout)
1288 chip->timeout = chip_info->timeout;
1289 if (chip_info->tx_threshold)
1290 tx_thres = chip_info->tx_threshold;
1291 if (chip_info->rx_threshold)
1292 rx_thres = chip_info->rx_threshold;
1293 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001294 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001295 if (chip_info->enable_loopback)
1296 chip->cr1 = SSCR1_LBM;
1297 }
1298
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001299 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1300 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1301
Stephen Street8d94cc52006-12-10 02:18:54 -08001302 /* set dma burst and threshold outside of chip_info path so that if
1303 * chip_info goes away after setting chip->enable_dma, the
1304 * burst and threshold can still respond to changes in bits_per_word */
1305 if (chip->enable_dma) {
1306 /* set up legal burst and threshold for dma */
1307 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1308 &chip->dma_burst_size,
1309 &chip->dma_threshold)) {
1310 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1311 "to match bits_per_word\n");
1312 }
1313 }
1314
eric miao2f1a74e2007-11-21 18:50:53 +08001315 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001316 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001317
1318 chip->cr0 = clk_div
1319 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001320 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1321 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -08001322 | SSCR0_SSE
1323 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001324 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1325 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1326 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001327
1328 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1329 if (drv_data->ssp_type != PXA25x_SSP)
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001330 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d, %s\n",
Stephen Streete0c99052006-03-07 23:53:24 -08001331 spi->bits_per_word,
eric miao2f1a74e2007-11-21 18:50:53 +08001332 clk_get_rate(ssp->clk)
Stephen Streete0c99052006-03-07 23:53:24 -08001333 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001334 spi->mode & 0x3,
1335 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001336 else
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001337 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d, %s\n",
Stephen Streete0c99052006-03-07 23:53:24 -08001338 spi->bits_per_word,
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001339 clk_get_rate(ssp->clk) / 2
Stephen Streete0c99052006-03-07 23:53:24 -08001340 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001341 spi->mode & 0x3,
1342 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001343
1344 if (spi->bits_per_word <= 8) {
1345 chip->n_bytes = 1;
1346 chip->dma_width = DCMD_WIDTH1;
1347 chip->read = u8_reader;
1348 chip->write = u8_writer;
1349 } else if (spi->bits_per_word <= 16) {
1350 chip->n_bytes = 2;
1351 chip->dma_width = DCMD_WIDTH2;
1352 chip->read = u16_reader;
1353 chip->write = u16_writer;
1354 } else if (spi->bits_per_word <= 32) {
1355 chip->cr0 |= SSCR0_EDSS;
1356 chip->n_bytes = 4;
1357 chip->dma_width = DCMD_WIDTH4;
1358 chip->read = u32_reader;
1359 chip->write = u32_writer;
1360 } else {
1361 dev_err(&spi->dev, "invalid wordsize\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001362 return -ENODEV;
1363 }
Stephen Street9708c122006-03-28 14:05:23 -08001364 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001365
1366 spi_set_ctldata(spi, chip);
1367
Eric Miaoa7bb3902009-04-06 19:00:54 -07001368 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001369}
1370
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001371static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001372{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001373 struct chip_data *chip = spi_get_ctldata(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001374
Eric Miaoa7bb3902009-04-06 19:00:54 -07001375 if (gpio_is_valid(chip->gpio_cs))
1376 gpio_free(chip->gpio_cs);
1377
Stephen Streete0c99052006-03-07 23:53:24 -08001378 kfree(chip);
1379}
1380
David Brownelld1e44d92007-10-16 01:27:46 -07001381static int __init init_queue(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -08001382{
1383 INIT_LIST_HEAD(&drv_data->queue);
1384 spin_lock_init(&drv_data->lock);
1385
1386 drv_data->run = QUEUE_STOPPED;
1387 drv_data->busy = 0;
1388
1389 tasklet_init(&drv_data->pump_transfers,
1390 pump_transfers, (unsigned long)drv_data);
1391
David Howells6d5aefb2006-12-05 19:36:26 +00001392 INIT_WORK(&drv_data->pump_messages, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001393 drv_data->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001394 dev_name(drv_data->master->dev.parent));
Stephen Streete0c99052006-03-07 23:53:24 -08001395 if (drv_data->workqueue == NULL)
1396 return -EBUSY;
1397
1398 return 0;
1399}
1400
1401static int start_queue(struct driver_data *drv_data)
1402{
1403 unsigned long flags;
1404
1405 spin_lock_irqsave(&drv_data->lock, flags);
1406
1407 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1408 spin_unlock_irqrestore(&drv_data->lock, flags);
1409 return -EBUSY;
1410 }
1411
1412 drv_data->run = QUEUE_RUNNING;
1413 drv_data->cur_msg = NULL;
1414 drv_data->cur_transfer = NULL;
1415 drv_data->cur_chip = NULL;
1416 spin_unlock_irqrestore(&drv_data->lock, flags);
1417
1418 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1419
1420 return 0;
1421}
1422
1423static int stop_queue(struct driver_data *drv_data)
1424{
1425 unsigned long flags;
1426 unsigned limit = 500;
1427 int status = 0;
1428
1429 spin_lock_irqsave(&drv_data->lock, flags);
1430
1431 /* This is a bit lame, but is optimized for the common execution path.
1432 * A wait_queue on the drv_data->busy could be used, but then the common
1433 * execution path (pump_messages) would be required to call wake_up or
1434 * friends on every SPI message. Do this instead */
1435 drv_data->run = QUEUE_STOPPED;
1436 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1437 spin_unlock_irqrestore(&drv_data->lock, flags);
1438 msleep(10);
1439 spin_lock_irqsave(&drv_data->lock, flags);
1440 }
1441
1442 if (!list_empty(&drv_data->queue) || drv_data->busy)
1443 status = -EBUSY;
1444
1445 spin_unlock_irqrestore(&drv_data->lock, flags);
1446
1447 return status;
1448}
1449
1450static int destroy_queue(struct driver_data *drv_data)
1451{
1452 int status;
1453
1454 status = stop_queue(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -08001455 /* we are unloading the module or failing to load (only two calls
1456 * to this routine), and neither call can handle a return value.
1457 * However, destroy_workqueue calls flush_workqueue, and that will
1458 * block until all work is done. If the reason that stop_queue
1459 * timed out is that the work will never finish, then it does no
1460 * good to call destroy_workqueue, so return anyway. */
Stephen Streete0c99052006-03-07 23:53:24 -08001461 if (status != 0)
1462 return status;
1463
1464 destroy_workqueue(drv_data->workqueue);
1465
1466 return 0;
1467}
1468
David Brownelld1e44d92007-10-16 01:27:46 -07001469static int __init pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001470{
1471 struct device *dev = &pdev->dev;
1472 struct pxa2xx_spi_master *platform_info;
1473 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001474 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001475 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001476 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001477
1478 platform_info = dev->platform_data;
1479
eric miao2f1a74e2007-11-21 18:50:53 +08001480 ssp = ssp_request(pdev->id, pdev->name);
1481 if (ssp == NULL) {
1482 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
Stephen Streete0c99052006-03-07 23:53:24 -08001483 return -ENODEV;
1484 }
1485
1486 /* Allocate master with space for drv_data and null dma buffer */
1487 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1488 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001489 dev_err(&pdev->dev, "cannot alloc spi_master\n");
eric miao2f1a74e2007-11-21 18:50:53 +08001490 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001491 return -ENOMEM;
1492 }
1493 drv_data = spi_master_get_devdata(master);
1494 drv_data->master = master;
1495 drv_data->master_info = platform_info;
1496 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001497 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001498
1499 master->bus_num = pdev->id;
1500 master->num_chipselect = platform_info->num_chipselect;
1501 master->cleanup = cleanup;
1502 master->setup = setup;
1503 master->transfer = transfer;
1504
eric miao2f1a74e2007-11-21 18:50:53 +08001505 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001506 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1507 sizeof(struct driver_data)), 8);
1508
eric miao2f1a74e2007-11-21 18:50:53 +08001509 drv_data->ioaddr = ssp->mmio_base;
1510 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1511 if (ssp->type == PXA25x_SSP) {
Stephen Streete0c99052006-03-07 23:53:24 -08001512 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1513 drv_data->dma_cr1 = 0;
1514 drv_data->clear_sr = SSSR_ROR;
1515 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1516 } else {
1517 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1518 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1519 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1520 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1521 }
1522
Kay Sievers6c7377a2009-03-24 16:38:21 -07001523 status = request_irq(ssp->irq, ssp_int, 0, dev_name(dev), drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001524 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001525 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001526 goto out_error_master_alloc;
1527 }
1528
1529 /* Setup DMA if requested */
1530 drv_data->tx_channel = -1;
1531 drv_data->rx_channel = -1;
1532 if (platform_info->enable_dma) {
1533
1534 /* Get two DMA channels (rx and tx) */
1535 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1536 DMA_PRIO_HIGH,
1537 dma_handler,
1538 drv_data);
1539 if (drv_data->rx_channel < 0) {
1540 dev_err(dev, "problem (%d) requesting rx channel\n",
1541 drv_data->rx_channel);
1542 status = -ENODEV;
1543 goto out_error_irq_alloc;
1544 }
1545 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1546 DMA_PRIO_MEDIUM,
1547 dma_handler,
1548 drv_data);
1549 if (drv_data->tx_channel < 0) {
1550 dev_err(dev, "problem (%d) requesting tx channel\n",
1551 drv_data->tx_channel);
1552 status = -ENODEV;
1553 goto out_error_dma_alloc;
1554 }
1555
eric miao2f1a74e2007-11-21 18:50:53 +08001556 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1557 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
Stephen Streete0c99052006-03-07 23:53:24 -08001558 }
1559
1560 /* Enable SOC clock */
eric miao2f1a74e2007-11-21 18:50:53 +08001561 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001562
1563 /* Load default SSP configuration */
1564 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001565 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1566 SSCR1_TxTresh(TX_THRESH_DFLT),
1567 drv_data->ioaddr);
Stephen Streete0c99052006-03-07 23:53:24 -08001568 write_SSCR0(SSCR0_SerClkDiv(2)
1569 | SSCR0_Motorola
1570 | SSCR0_DataSize(8),
1571 drv_data->ioaddr);
1572 if (drv_data->ssp_type != PXA25x_SSP)
1573 write_SSTO(0, drv_data->ioaddr);
1574 write_SSPSP(0, drv_data->ioaddr);
1575
1576 /* Initial and start queue */
1577 status = init_queue(drv_data);
1578 if (status != 0) {
1579 dev_err(&pdev->dev, "problem initializing queue\n");
1580 goto out_error_clock_enabled;
1581 }
1582 status = start_queue(drv_data);
1583 if (status != 0) {
1584 dev_err(&pdev->dev, "problem starting queue\n");
1585 goto out_error_clock_enabled;
1586 }
1587
1588 /* Register with the SPI framework */
1589 platform_set_drvdata(pdev, drv_data);
1590 status = spi_register_master(master);
1591 if (status != 0) {
1592 dev_err(&pdev->dev, "problem registering spi master\n");
1593 goto out_error_queue_alloc;
1594 }
1595
1596 return status;
1597
1598out_error_queue_alloc:
1599 destroy_queue(drv_data);
1600
1601out_error_clock_enabled:
eric miao2f1a74e2007-11-21 18:50:53 +08001602 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001603
1604out_error_dma_alloc:
1605 if (drv_data->tx_channel != -1)
1606 pxa_free_dma(drv_data->tx_channel);
1607 if (drv_data->rx_channel != -1)
1608 pxa_free_dma(drv_data->rx_channel);
1609
1610out_error_irq_alloc:
eric miao2f1a74e2007-11-21 18:50:53 +08001611 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001612
1613out_error_master_alloc:
1614 spi_master_put(master);
eric miao2f1a74e2007-11-21 18:50:53 +08001615 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001616 return status;
1617}
1618
1619static int pxa2xx_spi_remove(struct platform_device *pdev)
1620{
1621 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001622 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001623 int status = 0;
1624
1625 if (!drv_data)
1626 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001627 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001628
1629 /* Remove the queue */
1630 status = destroy_queue(drv_data);
1631 if (status != 0)
Stephen Street8d94cc52006-12-10 02:18:54 -08001632 /* the kernel does not check the return status of this
1633 * this routine (mod->exit, within the kernel). Therefore
1634 * nothing is gained by returning from here, the module is
1635 * going away regardless, and we should not leave any more
1636 * resources allocated than necessary. We cannot free the
1637 * message memory in drv_data->queue, but we can release the
1638 * resources below. I think the kernel should honor -EBUSY
1639 * returns but... */
1640 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1641 "complete, message memory not freed\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001642
1643 /* Disable the SSP at the peripheral and SOC level */
1644 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001645 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001646
1647 /* Release DMA */
1648 if (drv_data->master_info->enable_dma) {
eric miao2f1a74e2007-11-21 18:50:53 +08001649 DRCMR(ssp->drcmr_rx) = 0;
1650 DRCMR(ssp->drcmr_tx) = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001651 pxa_free_dma(drv_data->tx_channel);
1652 pxa_free_dma(drv_data->rx_channel);
1653 }
1654
1655 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001656 free_irq(ssp->irq, drv_data);
1657
1658 /* Release SSP */
1659 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001660
1661 /* Disconnect from the SPI framework */
1662 spi_unregister_master(drv_data->master);
1663
1664 /* Prevent double remove */
1665 platform_set_drvdata(pdev, NULL);
1666
1667 return 0;
1668}
1669
1670static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1671{
1672 int status = 0;
1673
1674 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1675 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1676}
1677
1678#ifdef CONFIG_PM
Stephen Streete0c99052006-03-07 23:53:24 -08001679
1680static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1681{
1682 struct driver_data *drv_data = platform_get_drvdata(pdev);
eric miao2f1a74e2007-11-21 18:50:53 +08001683 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001684 int status = 0;
1685
Stephen Streete0c99052006-03-07 23:53:24 -08001686 status = stop_queue(drv_data);
1687 if (status != 0)
1688 return status;
1689 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001690 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001691
1692 return 0;
1693}
1694
1695static int pxa2xx_spi_resume(struct platform_device *pdev)
1696{
1697 struct driver_data *drv_data = platform_get_drvdata(pdev);
eric miao2f1a74e2007-11-21 18:50:53 +08001698 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001699 int status = 0;
1700
1701 /* Enable the SSP clock */
Eric BENARD0cf942d2008-05-12 14:02:01 -07001702 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001703
1704 /* Start the queue running */
1705 status = start_queue(drv_data);
1706 if (status != 0) {
1707 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1708 return status;
1709 }
1710
1711 return 0;
1712}
1713#else
1714#define pxa2xx_spi_suspend NULL
1715#define pxa2xx_spi_resume NULL
1716#endif /* CONFIG_PM */
1717
1718static struct platform_driver driver = {
1719 .driver = {
1720 .name = "pxa2xx-spi",
Stephen Streete0c99052006-03-07 23:53:24 -08001721 .owner = THIS_MODULE,
1722 },
David Brownelld1e44d92007-10-16 01:27:46 -07001723 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001724 .shutdown = pxa2xx_spi_shutdown,
1725 .suspend = pxa2xx_spi_suspend,
1726 .resume = pxa2xx_spi_resume,
1727};
1728
1729static int __init pxa2xx_spi_init(void)
1730{
David Brownelld1e44d92007-10-16 01:27:46 -07001731 return platform_driver_probe(&driver, pxa2xx_spi_probe);
Stephen Streete0c99052006-03-07 23:53:24 -08001732}
1733module_init(pxa2xx_spi_init);
1734
1735static void __exit pxa2xx_spi_exit(void)
1736{
1737 platform_driver_unregister(&driver);
1738}
1739module_exit(pxa2xx_spi_exit);