blob: 1865c23f21753bbcb12560ae90863c4fcf0e598c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Stephen Streete0c99052006-03-07 23:53:24 -080033
34#include <asm/io.h>
35#include <asm/irq.h>
Stephen Streete0c99052006-03-07 23:53:24 -080036#include <asm/delay.h>
Stephen Streete0c99052006-03-07 23:53:24 -080037
Russell Kingdcea83a2008-11-29 11:40:28 +000038#include <mach/dma.h>
Haojian Zhuang54c39b42010-03-16 17:12:37 +080039#include <plat/ssp.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010040#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
Mike Rapoport7ad0ba92009-04-06 19:00:57 -070057#define DMA_ALIGNMENT 8
Stephen Streete0c99052006-03-07 23:53:24 -080058
Ned Forresterb97c74b2008-02-23 15:23:40 -080059/*
60 * for testing SSCR1 changes that require SSP restart, basically
61 * everything except the service and interrupt enables, the pxa270 developer
62 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
63 * list, but the PXA255 dev man says all bits without really meaning the
64 * service and interrupt enables
65 */
66#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080067 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080068 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
69 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
70 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
71 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080072
Stephen Streete0c99052006-03-07 23:53:24 -080073#define DEFINE_SSP_REG(reg, off) \
David Brownellcf433692008-04-28 02:14:17 -070074static inline u32 read_##reg(void const __iomem *p) \
75{ return __raw_readl(p + (off)); } \
76\
77static inline void write_##reg(u32 v, void __iomem *p) \
78{ __raw_writel(v, p + (off)); }
Stephen Streete0c99052006-03-07 23:53:24 -080079
80DEFINE_SSP_REG(SSCR0, 0x00)
81DEFINE_SSP_REG(SSCR1, 0x04)
82DEFINE_SSP_REG(SSSR, 0x08)
83DEFINE_SSP_REG(SSITR, 0x0c)
84DEFINE_SSP_REG(SSDR, 0x10)
85DEFINE_SSP_REG(SSTO, 0x28)
86DEFINE_SSP_REG(SSPSP, 0x2c)
87
88#define START_STATE ((void*)0)
89#define RUNNING_STATE ((void*)1)
90#define DONE_STATE ((void*)2)
91#define ERROR_STATE ((void*)-1)
92
93#define QUEUE_RUNNING 0
94#define QUEUE_STOPPED 1
95
96struct driver_data {
97 /* Driver model hookup */
98 struct platform_device *pdev;
99
eric miao2f1a74e2007-11-21 18:50:53 +0800100 /* SSP Info */
101 struct ssp_device *ssp;
102
Stephen Streete0c99052006-03-07 23:53:24 -0800103 /* SPI framework hookup */
104 enum pxa_ssp_type ssp_type;
105 struct spi_master *master;
106
107 /* PXA hookup */
108 struct pxa2xx_spi_master *master_info;
109
110 /* DMA setup stuff */
111 int rx_channel;
112 int tx_channel;
113 u32 *null_dma_buf;
114
115 /* SSP register addresses */
David Brownellcf433692008-04-28 02:14:17 -0700116 void __iomem *ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800117 u32 ssdr_physical;
118
119 /* SSP masks*/
120 u32 dma_cr1;
121 u32 int_cr1;
122 u32 clear_sr;
123 u32 mask_sr;
124
125 /* Driver message queue */
126 struct workqueue_struct *workqueue;
127 struct work_struct pump_messages;
128 spinlock_t lock;
129 struct list_head queue;
130 int busy;
131 int run;
132
133 /* Message Transfer pump */
134 struct tasklet_struct pump_transfers;
135
136 /* Current message transfer state info */
137 struct spi_message* cur_msg;
138 struct spi_transfer* cur_transfer;
139 struct chip_data *cur_chip;
140 size_t len;
141 void *tx;
142 void *tx_end;
143 void *rx;
144 void *rx_end;
145 int dma_mapped;
146 dma_addr_t rx_dma;
147 dma_addr_t tx_dma;
148 size_t rx_map_len;
149 size_t tx_map_len;
Stephen Street9708c122006-03-28 14:05:23 -0800150 u8 n_bytes;
151 u32 dma_width;
Stephen Street8d94cc52006-12-10 02:18:54 -0800152 int (*write)(struct driver_data *drv_data);
153 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800154 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
155 void (*cs_control)(u32 command);
156};
157
158struct chip_data {
159 u32 cr0;
160 u32 cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800161 u32 psp;
162 u32 timeout;
163 u8 n_bytes;
164 u32 dma_width;
165 u32 dma_burst_size;
166 u32 threshold;
167 u32 dma_threshold;
168 u8 enable_dma;
Stephen Street9708c122006-03-28 14:05:23 -0800169 u8 bits_per_word;
170 u32 speed_hz;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700171 int gpio_cs;
172 int gpio_cs_inverted;
Stephen Street8d94cc52006-12-10 02:18:54 -0800173 int (*write)(struct driver_data *drv_data);
174 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800175 void (*cs_control)(u32 command);
176};
177
David Howells6d5aefb2006-12-05 19:36:26 +0000178static void pump_messages(struct work_struct *work);
Stephen Streete0c99052006-03-07 23:53:24 -0800179
Eric Miaoa7bb3902009-04-06 19:00:54 -0700180static void cs_assert(struct driver_data *drv_data)
181{
182 struct chip_data *chip = drv_data->cur_chip;
183
184 if (chip->cs_control) {
185 chip->cs_control(PXA2XX_CS_ASSERT);
186 return;
187 }
188
189 if (gpio_is_valid(chip->gpio_cs))
190 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
191}
192
193static void cs_deassert(struct driver_data *drv_data)
194{
195 struct chip_data *chip = drv_data->cur_chip;
196
197 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300198 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700199 return;
200 }
201
202 if (gpio_is_valid(chip->gpio_cs))
203 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
204}
205
Stephen Streete0c99052006-03-07 23:53:24 -0800206static int flush(struct driver_data *drv_data)
207{
208 unsigned long limit = loops_per_jiffy << 1;
209
David Brownellcf433692008-04-28 02:14:17 -0700210 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800211
212 do {
213 while (read_SSSR(reg) & SSSR_RNE) {
214 read_SSDR(reg);
215 }
Roel Kluin306c68a2009-04-21 12:24:46 -0700216 } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
Stephen Streete0c99052006-03-07 23:53:24 -0800217 write_SSSR(SSSR_ROR, reg);
218
219 return limit;
220}
221
Stephen Street8d94cc52006-12-10 02:18:54 -0800222static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800223{
David Brownellcf433692008-04-28 02:14:17 -0700224 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800225 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800226
Stephen Street8d94cc52006-12-10 02:18:54 -0800227 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
228 || (drv_data->tx == drv_data->tx_end))
229 return 0;
230
231 write_SSDR(0, reg);
232 drv_data->tx += n_bytes;
233
234 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800235}
236
Stephen Street8d94cc52006-12-10 02:18:54 -0800237static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800238{
David Brownellcf433692008-04-28 02:14:17 -0700239 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800240 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800241
242 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800243 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800244 read_SSDR(reg);
245 drv_data->rx += n_bytes;
246 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800247
248 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800249}
250
Stephen Street8d94cc52006-12-10 02:18:54 -0800251static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800252{
David Brownellcf433692008-04-28 02:14:17 -0700253 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800254
Stephen Street8d94cc52006-12-10 02:18:54 -0800255 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
256 || (drv_data->tx == drv_data->tx_end))
257 return 0;
258
259 write_SSDR(*(u8 *)(drv_data->tx), reg);
260 ++drv_data->tx;
261
262 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800263}
264
Stephen Street8d94cc52006-12-10 02:18:54 -0800265static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800266{
David Brownellcf433692008-04-28 02:14:17 -0700267 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800268
269 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800270 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800271 *(u8 *)(drv_data->rx) = read_SSDR(reg);
272 ++drv_data->rx;
273 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800274
275 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800276}
277
Stephen Street8d94cc52006-12-10 02:18:54 -0800278static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800279{
David Brownellcf433692008-04-28 02:14:17 -0700280 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800281
Stephen Street8d94cc52006-12-10 02:18:54 -0800282 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
283 || (drv_data->tx == drv_data->tx_end))
284 return 0;
285
286 write_SSDR(*(u16 *)(drv_data->tx), reg);
287 drv_data->tx += 2;
288
289 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800290}
291
Stephen Street8d94cc52006-12-10 02:18:54 -0800292static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800293{
David Brownellcf433692008-04-28 02:14:17 -0700294 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800295
296 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800297 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800298 *(u16 *)(drv_data->rx) = read_SSDR(reg);
299 drv_data->rx += 2;
300 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800301
302 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800303}
Stephen Street8d94cc52006-12-10 02:18:54 -0800304
305static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800306{
David Brownellcf433692008-04-28 02:14:17 -0700307 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800308
Stephen Street8d94cc52006-12-10 02:18:54 -0800309 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
310 || (drv_data->tx == drv_data->tx_end))
311 return 0;
312
313 write_SSDR(*(u32 *)(drv_data->tx), reg);
314 drv_data->tx += 4;
315
316 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800317}
318
Stephen Street8d94cc52006-12-10 02:18:54 -0800319static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800320{
David Brownellcf433692008-04-28 02:14:17 -0700321 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800322
323 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800324 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800325 *(u32 *)(drv_data->rx) = read_SSDR(reg);
326 drv_data->rx += 4;
327 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800328
329 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800330}
331
332static void *next_transfer(struct driver_data *drv_data)
333{
334 struct spi_message *msg = drv_data->cur_msg;
335 struct spi_transfer *trans = drv_data->cur_transfer;
336
337 /* Move to next transfer */
338 if (trans->transfer_list.next != &msg->transfers) {
339 drv_data->cur_transfer =
340 list_entry(trans->transfer_list.next,
341 struct spi_transfer,
342 transfer_list);
343 return RUNNING_STATE;
344 } else
345 return DONE_STATE;
346}
347
348static int map_dma_buffers(struct driver_data *drv_data)
349{
350 struct spi_message *msg = drv_data->cur_msg;
351 struct device *dev = &msg->spi->dev;
352
353 if (!drv_data->cur_chip->enable_dma)
354 return 0;
355
356 if (msg->is_dma_mapped)
357 return drv_data->rx_dma && drv_data->tx_dma;
358
359 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
360 return 0;
361
362 /* Modify setup if rx buffer is null */
363 if (drv_data->rx == NULL) {
364 *drv_data->null_dma_buf = 0;
365 drv_data->rx = drv_data->null_dma_buf;
366 drv_data->rx_map_len = 4;
367 } else
368 drv_data->rx_map_len = drv_data->len;
369
370
371 /* Modify setup if tx buffer is null */
372 if (drv_data->tx == NULL) {
373 *drv_data->null_dma_buf = 0;
374 drv_data->tx = drv_data->null_dma_buf;
375 drv_data->tx_map_len = 4;
376 } else
377 drv_data->tx_map_len = drv_data->len;
378
Ned Forrester393df742008-11-19 15:36:21 -0800379 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
380 * so we flush the cache *before* invalidating it, in case
381 * the tx and rx buffers overlap.
382 */
383 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
384 drv_data->tx_map_len, DMA_TO_DEVICE);
385 if (dma_mapping_error(dev, drv_data->tx_dma))
Stephen Streete0c99052006-03-07 23:53:24 -0800386 return 0;
387
Ned Forrester393df742008-11-19 15:36:21 -0800388 /* Stream map the rx buffer */
389 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
Stephen Streete0c99052006-03-07 23:53:24 -0800390 drv_data->rx_map_len, DMA_FROM_DEVICE);
Ned Forrester393df742008-11-19 15:36:21 -0800391 if (dma_mapping_error(dev, drv_data->rx_dma)) {
392 dma_unmap_single(dev, drv_data->tx_dma,
393 drv_data->tx_map_len, DMA_TO_DEVICE);
Stephen Streete0c99052006-03-07 23:53:24 -0800394 return 0;
395 }
396
397 return 1;
398}
399
400static void unmap_dma_buffers(struct driver_data *drv_data)
401{
402 struct device *dev;
403
404 if (!drv_data->dma_mapped)
405 return;
406
407 if (!drv_data->cur_msg->is_dma_mapped) {
408 dev = &drv_data->cur_msg->spi->dev;
409 dma_unmap_single(dev, drv_data->rx_dma,
410 drv_data->rx_map_len, DMA_FROM_DEVICE);
411 dma_unmap_single(dev, drv_data->tx_dma,
412 drv_data->tx_map_len, DMA_TO_DEVICE);
413 }
414
415 drv_data->dma_mapped = 0;
416}
417
418/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700419static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800420{
421 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700422 unsigned long flags;
423 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800424
Stephen Street5daa3ba2006-05-20 15:00:19 -0700425 spin_lock_irqsave(&drv_data->lock, flags);
426 msg = drv_data->cur_msg;
427 drv_data->cur_msg = NULL;
428 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700429 queue_work(drv_data->workqueue, &drv_data->pump_messages);
430 spin_unlock_irqrestore(&drv_data->lock, flags);
431
432 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800433 struct spi_transfer,
434 transfer_list);
435
Ned Forrester84235972008-09-13 02:33:17 -0700436 /* Delay if requested before any change in chip select */
437 if (last_transfer->delay_usecs)
438 udelay(last_transfer->delay_usecs);
439
440 /* Drop chip select UNLESS cs_change is true or we are returning
441 * a message with an error, or next message is for another chip
442 */
Stephen Streete0c99052006-03-07 23:53:24 -0800443 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700444 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700445 else {
446 struct spi_message *next_msg;
447
448 /* Holding of cs was hinted, but we need to make sure
449 * the next message is for the same chip. Don't waste
450 * time with the following tests unless this was hinted.
451 *
452 * We cannot postpone this until pump_messages, because
453 * after calling msg->complete (below) the driver that
454 * sent the current message could be unloaded, which
455 * could invalidate the cs_control() callback...
456 */
457
458 /* get a pointer to the next message, if any */
459 spin_lock_irqsave(&drv_data->lock, flags);
460 if (list_empty(&drv_data->queue))
461 next_msg = NULL;
462 else
463 next_msg = list_entry(drv_data->queue.next,
464 struct spi_message, queue);
465 spin_unlock_irqrestore(&drv_data->lock, flags);
466
467 /* see if the next and current messages point
468 * to the same chip
469 */
470 if (next_msg && next_msg->spi != msg->spi)
471 next_msg = NULL;
472 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700473 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700474 }
Stephen Streete0c99052006-03-07 23:53:24 -0800475
Stephen Street5daa3ba2006-05-20 15:00:19 -0700476 msg->state = NULL;
477 if (msg->complete)
478 msg->complete(msg->context);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700479
480 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800481}
482
David Brownellcf433692008-04-28 02:14:17 -0700483static int wait_ssp_rx_stall(void const __iomem *ioaddr)
Stephen Streete0c99052006-03-07 23:53:24 -0800484{
485 unsigned long limit = loops_per_jiffy << 1;
486
Roel Kluin306c68a2009-04-21 12:24:46 -0700487 while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800488 cpu_relax();
489
490 return limit;
491}
492
493static int wait_dma_channel_stop(int channel)
494{
495 unsigned long limit = loops_per_jiffy << 1;
496
Roel Kluin306c68a2009-04-21 12:24:46 -0700497 while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800498 cpu_relax();
499
500 return limit;
501}
502
David Brownellcf433692008-04-28 02:14:17 -0700503static void dma_error_stop(struct driver_data *drv_data, const char *msg)
Stephen Street8d94cc52006-12-10 02:18:54 -0800504{
David Brownellcf433692008-04-28 02:14:17 -0700505 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800506
507 /* Stop and reset */
508 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
509 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
510 write_SSSR(drv_data->clear_sr, reg);
511 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
512 if (drv_data->ssp_type != PXA25x_SSP)
513 write_SSTO(0, reg);
514 flush(drv_data);
515 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
516
517 unmap_dma_buffers(drv_data);
518
519 dev_err(&drv_data->pdev->dev, "%s\n", msg);
520
521 drv_data->cur_msg->state = ERROR_STATE;
522 tasklet_schedule(&drv_data->pump_transfers);
523}
524
525static void dma_transfer_complete(struct driver_data *drv_data)
526{
David Brownellcf433692008-04-28 02:14:17 -0700527 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800528 struct spi_message *msg = drv_data->cur_msg;
529
530 /* Clear and disable interrupts on SSP and DMA channels*/
531 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
532 write_SSSR(drv_data->clear_sr, 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_handler: 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 /* update the buffer pointer for the amount completed in dma */
547 drv_data->rx += drv_data->len -
548 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
549
550 /* read trailing data from fifo, it does not matter how many
551 * bytes are in the fifo just read until buffer is full
552 * or fifo is empty, which ever occurs first */
553 drv_data->read(drv_data);
554
555 /* return count of what was actually read */
556 msg->actual_length += drv_data->len -
557 (drv_data->rx_end - drv_data->rx);
558
Ned Forrester84235972008-09-13 02:33:17 -0700559 /* Transfer delays and chip select release are
560 * handled in pump_transfers or giveback
561 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800562
563 /* Move to next transfer */
564 msg->state = next_transfer(drv_data);
565
566 /* Schedule transfer tasklet */
567 tasklet_schedule(&drv_data->pump_transfers);
568}
569
David Howells7d12e782006-10-05 14:55:46 +0100570static void dma_handler(int channel, void *data)
Stephen Streete0c99052006-03-07 23:53:24 -0800571{
572 struct driver_data *drv_data = data;
Stephen Streete0c99052006-03-07 23:53:24 -0800573 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
Stephen Streete0c99052006-03-07 23:53:24 -0800574
575 if (irq_status & DCSR_BUSERR) {
576
Stephen Streete0c99052006-03-07 23:53:24 -0800577 if (channel == drv_data->tx_channel)
Stephen Street8d94cc52006-12-10 02:18:54 -0800578 dma_error_stop(drv_data,
579 "dma_handler: "
580 "bad bus address on tx channel");
Stephen Streete0c99052006-03-07 23:53:24 -0800581 else
Stephen Street8d94cc52006-12-10 02:18:54 -0800582 dma_error_stop(drv_data,
583 "dma_handler: "
584 "bad bus address on rx channel");
585 return;
Stephen Streete0c99052006-03-07 23:53:24 -0800586 }
587
588 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
Stephen Street8d94cc52006-12-10 02:18:54 -0800589 if ((channel == drv_data->tx_channel)
590 && (irq_status & DCSR_ENDINTR)
591 && (drv_data->ssp_type == PXA25x_SSP)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800592
593 /* Wait for rx to stall */
594 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
595 dev_err(&drv_data->pdev->dev,
596 "dma_handler: ssp rx stall failed\n");
597
Stephen Street8d94cc52006-12-10 02:18:54 -0800598 /* finish this transfer, start the next */
599 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800600 }
601}
602
603static irqreturn_t dma_transfer(struct driver_data *drv_data)
604{
605 u32 irq_status;
David Brownellcf433692008-04-28 02:14:17 -0700606 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800607
608 irq_status = read_SSSR(reg) & drv_data->mask_sr;
609 if (irq_status & SSSR_ROR) {
Stephen Street8d94cc52006-12-10 02:18:54 -0800610 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
Stephen Streete0c99052006-03-07 23:53:24 -0800611 return IRQ_HANDLED;
612 }
613
614 /* Check for false positive timeout */
Stephen Street8d94cc52006-12-10 02:18:54 -0800615 if ((irq_status & SSSR_TINT)
616 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800617 write_SSSR(SSSR_TINT, reg);
618 return IRQ_HANDLED;
619 }
620
621 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
622
Stephen Street8d94cc52006-12-10 02:18:54 -0800623 /* Clear and disable timeout interrupt, do the rest in
624 * dma_transfer_complete */
Stephen Streete0c99052006-03-07 23:53:24 -0800625 if (drv_data->ssp_type != PXA25x_SSP)
626 write_SSTO(0, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800627
Stephen Street8d94cc52006-12-10 02:18:54 -0800628 /* finish this transfer, start the next */
629 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800630
631 return IRQ_HANDLED;
632 }
633
634 /* Opps problem detected */
635 return IRQ_NONE;
636}
637
Stephen Street8d94cc52006-12-10 02:18:54 -0800638static void int_error_stop(struct driver_data *drv_data, const char* msg)
639{
David Brownellcf433692008-04-28 02:14:17 -0700640 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800641
642 /* Stop and reset SSP */
643 write_SSSR(drv_data->clear_sr, reg);
644 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
645 if (drv_data->ssp_type != PXA25x_SSP)
646 write_SSTO(0, reg);
647 flush(drv_data);
648 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
649
650 dev_err(&drv_data->pdev->dev, "%s\n", msg);
651
652 drv_data->cur_msg->state = ERROR_STATE;
653 tasklet_schedule(&drv_data->pump_transfers);
654}
655
656static void int_transfer_complete(struct driver_data *drv_data)
657{
David Brownellcf433692008-04-28 02:14:17 -0700658 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800659
660 /* Stop SSP */
661 write_SSSR(drv_data->clear_sr, reg);
662 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
663 if (drv_data->ssp_type != PXA25x_SSP)
664 write_SSTO(0, reg);
665
666 /* Update total byte transfered return count actual bytes read */
667 drv_data->cur_msg->actual_length += drv_data->len -
668 (drv_data->rx_end - drv_data->rx);
669
Ned Forrester84235972008-09-13 02:33:17 -0700670 /* Transfer delays and chip select release are
671 * handled in pump_transfers or giveback
672 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800673
674 /* Move to next transfer */
675 drv_data->cur_msg->state = next_transfer(drv_data);
676
677 /* Schedule transfer tasklet */
678 tasklet_schedule(&drv_data->pump_transfers);
679}
680
Stephen Streete0c99052006-03-07 23:53:24 -0800681static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
682{
David Brownellcf433692008-04-28 02:14:17 -0700683 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800684
Stephen Street5daa3ba2006-05-20 15:00:19 -0700685 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
686 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800687
Stephen Street8d94cc52006-12-10 02:18:54 -0800688 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800689
Stephen Street8d94cc52006-12-10 02:18:54 -0800690 if (irq_status & SSSR_ROR) {
691 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
692 return IRQ_HANDLED;
693 }
Stephen Streete0c99052006-03-07 23:53:24 -0800694
Stephen Street8d94cc52006-12-10 02:18:54 -0800695 if (irq_status & SSSR_TINT) {
696 write_SSSR(SSSR_TINT, reg);
697 if (drv_data->read(drv_data)) {
698 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800699 return IRQ_HANDLED;
700 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800701 }
Stephen Streete0c99052006-03-07 23:53:24 -0800702
Stephen Street8d94cc52006-12-10 02:18:54 -0800703 /* Drain rx fifo, Fill tx fifo and prevent overruns */
704 do {
705 if (drv_data->read(drv_data)) {
706 int_transfer_complete(drv_data);
707 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800708 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800709 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800710
Stephen Street8d94cc52006-12-10 02:18:54 -0800711 if (drv_data->read(drv_data)) {
712 int_transfer_complete(drv_data);
713 return IRQ_HANDLED;
714 }
Stephen Streete0c99052006-03-07 23:53:24 -0800715
Stephen Street8d94cc52006-12-10 02:18:54 -0800716 if (drv_data->tx == drv_data->tx_end) {
717 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
718 /* PXA25x_SSP has no timeout, read trailing bytes */
719 if (drv_data->ssp_type == PXA25x_SSP) {
720 if (!wait_ssp_rx_stall(reg))
721 {
722 int_error_stop(drv_data, "interrupt_transfer: "
723 "rx stall failed");
724 return IRQ_HANDLED;
725 }
726 if (!drv_data->read(drv_data))
727 {
728 int_error_stop(drv_data,
729 "interrupt_transfer: "
730 "trailing byte read failed");
731 return IRQ_HANDLED;
732 }
733 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800734 }
Stephen Streete0c99052006-03-07 23:53:24 -0800735 }
736
Stephen Street5daa3ba2006-05-20 15:00:19 -0700737 /* We did something */
738 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800739}
740
David Howells7d12e782006-10-05 14:55:46 +0100741static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800742{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400743 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700744 void __iomem *reg = drv_data->ioaddr;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800745 u32 sccr1_reg = read_SSCR1(reg);
746 u32 mask = drv_data->mask_sr;
747 u32 status;
748
749 status = read_SSSR(reg);
750
751 /* Ignore possible writes if we don't need to write */
752 if (!(sccr1_reg & SSCR1_TIE))
753 mask &= ~SSSR_TFS;
754
755 if (!(status & mask))
756 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800757
758 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700759
760 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
761 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
762 if (drv_data->ssp_type != PXA25x_SSP)
763 write_SSTO(0, reg);
764 write_SSSR(drv_data->clear_sr, reg);
765
Stephen Streete0c99052006-03-07 23:53:24 -0800766 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800767 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700768
Stephen Streete0c99052006-03-07 23:53:24 -0800769 /* Never fail */
770 return IRQ_HANDLED;
771 }
772
773 return drv_data->transfer_handler(drv_data);
774}
775
David Brownellcf433692008-04-28 02:14:17 -0700776static int set_dma_burst_and_threshold(struct chip_data *chip,
777 struct spi_device *spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800778 u8 bits_per_word, u32 *burst_code,
779 u32 *threshold)
780{
781 struct pxa2xx_spi_chip *chip_info =
782 (struct pxa2xx_spi_chip *)spi->controller_data;
783 int bytes_per_word;
784 int burst_bytes;
785 int thresh_words;
786 int req_burst_size;
787 int retval = 0;
788
789 /* Set the threshold (in registers) to equal the same amount of data
790 * as represented by burst size (in bytes). The computation below
791 * is (burst_size rounded up to nearest 8 byte, word or long word)
792 * divided by (bytes/register); the tx threshold is the inverse of
793 * the rx, so that there will always be enough data in the rx fifo
794 * to satisfy a burst, and there will always be enough space in the
795 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
796 * there is not enough space), there must always remain enough empty
797 * space in the rx fifo for any data loaded to the tx fifo.
798 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
799 * will be 8, or half the fifo;
800 * The threshold can only be set to 2, 4 or 8, but not 16, because
801 * to burst 16 to the tx fifo, the fifo would have to be empty;
802 * however, the minimum fifo trigger level is 1, and the tx will
803 * request service when the fifo is at this level, with only 15 spaces.
804 */
805
806 /* find bytes/word */
807 if (bits_per_word <= 8)
808 bytes_per_word = 1;
809 else if (bits_per_word <= 16)
810 bytes_per_word = 2;
811 else
812 bytes_per_word = 4;
813
814 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
815 if (chip_info)
816 req_burst_size = chip_info->dma_burst_size;
817 else {
818 switch (chip->dma_burst_size) {
819 default:
820 /* if the default burst size is not set,
821 * do it now */
822 chip->dma_burst_size = DCMD_BURST8;
823 case DCMD_BURST8:
824 req_burst_size = 8;
825 break;
826 case DCMD_BURST16:
827 req_burst_size = 16;
828 break;
829 case DCMD_BURST32:
830 req_burst_size = 32;
831 break;
832 }
833 }
834 if (req_burst_size <= 8) {
835 *burst_code = DCMD_BURST8;
836 burst_bytes = 8;
837 } else if (req_burst_size <= 16) {
838 if (bytes_per_word == 1) {
839 /* don't burst more than 1/2 the fifo */
840 *burst_code = DCMD_BURST8;
841 burst_bytes = 8;
842 retval = 1;
843 } else {
844 *burst_code = DCMD_BURST16;
845 burst_bytes = 16;
846 }
847 } else {
848 if (bytes_per_word == 1) {
849 /* don't burst more than 1/2 the fifo */
850 *burst_code = DCMD_BURST8;
851 burst_bytes = 8;
852 retval = 1;
853 } else if (bytes_per_word == 2) {
854 /* don't burst more than 1/2 the fifo */
855 *burst_code = DCMD_BURST16;
856 burst_bytes = 16;
857 retval = 1;
858 } else {
859 *burst_code = DCMD_BURST32;
860 burst_bytes = 32;
861 }
862 }
863
864 thresh_words = burst_bytes / bytes_per_word;
865
866 /* thresh_words will be between 2 and 8 */
867 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
868 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
869
870 return retval;
871}
872
eric miao2f1a74e2007-11-21 18:50:53 +0800873static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
874{
875 unsigned long ssp_clk = clk_get_rate(ssp->clk);
876
877 if (ssp->type == PXA25x_SSP)
878 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
879 else
880 return ((ssp_clk / rate - 1) & 0xfff) << 8;
881}
882
Stephen Streete0c99052006-03-07 23:53:24 -0800883static void pump_transfers(unsigned long data)
884{
885 struct driver_data *drv_data = (struct driver_data *)data;
886 struct spi_message *message = NULL;
887 struct spi_transfer *transfer = NULL;
888 struct spi_transfer *previous = NULL;
889 struct chip_data *chip = NULL;
eric miao2f1a74e2007-11-21 18:50:53 +0800890 struct ssp_device *ssp = drv_data->ssp;
David Brownellcf433692008-04-28 02:14:17 -0700891 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800892 u32 clk_div = 0;
893 u8 bits = 0;
894 u32 speed = 0;
895 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800896 u32 cr1;
897 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
898 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800899
900 /* Get current state information */
901 message = drv_data->cur_msg;
902 transfer = drv_data->cur_transfer;
903 chip = drv_data->cur_chip;
904
905 /* Handle for abort */
906 if (message->state == ERROR_STATE) {
907 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700908 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800909 return;
910 }
911
912 /* Handle end of message */
913 if (message->state == DONE_STATE) {
914 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700915 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800916 return;
917 }
918
Ned Forrester84235972008-09-13 02:33:17 -0700919 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800920 if (message->state == RUNNING_STATE) {
921 previous = list_entry(transfer->transfer_list.prev,
922 struct spi_transfer,
923 transfer_list);
924 if (previous->delay_usecs)
925 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700926
927 /* Drop chip select only if cs_change is requested */
928 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700929 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800930 }
931
Ned Forrester7e964452008-09-13 02:33:18 -0700932 /* Check for transfers that need multiple DMA segments */
933 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
934
935 /* reject already-mapped transfers; PIO won't always work */
936 if (message->is_dma_mapped
937 || transfer->rx_dma || transfer->tx_dma) {
938 dev_err(&drv_data->pdev->dev,
939 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700940 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700941 transfer->len, MAX_DMA_LEN);
942 message->status = -EINVAL;
943 giveback(drv_data);
944 return;
945 }
946
947 /* warn ... we force this to PIO mode */
948 if (printk_ratelimit())
949 dev_warn(&message->spi->dev, "pump_transfers: "
950 "DMA disabled for transfer length %ld "
951 "greater than %d\n",
952 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800953 }
954
Stephen Streete0c99052006-03-07 23:53:24 -0800955 /* Setup the transfer state based on the type of transfer */
956 if (flush(drv_data) == 0) {
957 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
958 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700959 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800960 return;
961 }
Stephen Street9708c122006-03-28 14:05:23 -0800962 drv_data->n_bytes = chip->n_bytes;
963 drv_data->dma_width = chip->dma_width;
Stephen Streete0c99052006-03-07 23:53:24 -0800964 drv_data->tx = (void *)transfer->tx_buf;
965 drv_data->tx_end = drv_data->tx + transfer->len;
966 drv_data->rx = transfer->rx_buf;
967 drv_data->rx_end = drv_data->rx + transfer->len;
968 drv_data->rx_dma = transfer->rx_dma;
969 drv_data->tx_dma = transfer->tx_dma;
Stephen Street8d94cc52006-12-10 02:18:54 -0800970 drv_data->len = transfer->len & DCMD_LENGTH;
Stephen Streete0c99052006-03-07 23:53:24 -0800971 drv_data->write = drv_data->tx ? chip->write : null_writer;
972 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800973
974 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800975 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800976 if (transfer->speed_hz || transfer->bits_per_word) {
977
Stephen Street9708c122006-03-28 14:05:23 -0800978 bits = chip->bits_per_word;
979 speed = chip->speed_hz;
980
981 if (transfer->speed_hz)
982 speed = transfer->speed_hz;
983
984 if (transfer->bits_per_word)
985 bits = transfer->bits_per_word;
986
eric miao2f1a74e2007-11-21 18:50:53 +0800987 clk_div = ssp_get_clk_div(ssp, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800988
989 if (bits <= 8) {
990 drv_data->n_bytes = 1;
991 drv_data->dma_width = DCMD_WIDTH1;
992 drv_data->read = drv_data->read != null_reader ?
993 u8_reader : null_reader;
994 drv_data->write = drv_data->write != null_writer ?
995 u8_writer : null_writer;
996 } else if (bits <= 16) {
997 drv_data->n_bytes = 2;
998 drv_data->dma_width = DCMD_WIDTH2;
999 drv_data->read = drv_data->read != null_reader ?
1000 u16_reader : null_reader;
1001 drv_data->write = drv_data->write != null_writer ?
1002 u16_writer : null_writer;
1003 } else if (bits <= 32) {
1004 drv_data->n_bytes = 4;
1005 drv_data->dma_width = DCMD_WIDTH4;
1006 drv_data->read = drv_data->read != null_reader ?
1007 u32_reader : null_reader;
1008 drv_data->write = drv_data->write != null_writer ?
1009 u32_writer : null_writer;
1010 }
Stephen Street8d94cc52006-12-10 02:18:54 -08001011 /* if bits/word is changed in dma mode, then must check the
1012 * thresholds and burst also */
1013 if (chip->enable_dma) {
1014 if (set_dma_burst_and_threshold(chip, message->spi,
1015 bits, &dma_burst,
1016 &dma_thresh))
1017 if (printk_ratelimit())
1018 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -07001019 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -08001020 "DMA burst size reduced to "
1021 "match bits_per_word\n");
1022 }
Stephen Street9708c122006-03-28 14:05:23 -08001023
1024 cr0 = clk_div
1025 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001026 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -08001027 | SSCR0_SSE
1028 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -08001029 }
1030
Stephen Streete0c99052006-03-07 23:53:24 -08001031 message->state = RUNNING_STATE;
1032
Ned Forrester7e964452008-09-13 02:33:18 -07001033 /* Try to map dma buffer and do a dma transfer if successful, but
1034 * only if the length is non-zero and less than MAX_DMA_LEN.
1035 *
1036 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
1037 * of PIO instead. Care is needed above because the transfer may
1038 * have have been passed with buffers that are already dma mapped.
1039 * A zero-length transfer in PIO mode will not try to write/read
1040 * to/from the buffers
1041 *
1042 * REVISIT large transfers are exactly where we most want to be
1043 * using DMA. If this happens much, split those transfers into
1044 * multiple DMA segments rather than forcing PIO.
1045 */
1046 drv_data->dma_mapped = 0;
1047 if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN)
1048 drv_data->dma_mapped = map_dma_buffers(drv_data);
1049 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001050
1051 /* Ensure we have the correct interrupt handler */
1052 drv_data->transfer_handler = dma_transfer;
1053
1054 /* Setup rx DMA Channel */
1055 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
1056 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
1057 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
1058 if (drv_data->rx == drv_data->null_dma_buf)
1059 /* No target address increment */
1060 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001061 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001062 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001063 | drv_data->len;
1064 else
1065 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
1066 | DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001067 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001068 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001069 | drv_data->len;
1070
1071 /* Setup tx DMA Channel */
1072 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
1073 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
1074 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
1075 if (drv_data->tx == drv_data->null_dma_buf)
1076 /* No source address increment */
1077 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001078 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001079 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001080 | drv_data->len;
1081 else
1082 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
1083 | DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001084 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001085 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001086 | drv_data->len;
1087
1088 /* Enable dma end irqs on SSP to detect end of transfer */
1089 if (drv_data->ssp_type == PXA25x_SSP)
1090 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
1091
Stephen Street8d94cc52006-12-10 02:18:54 -08001092 /* Clear status and start DMA engine */
1093 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001094 write_SSSR(drv_data->clear_sr, reg);
1095 DCSR(drv_data->rx_channel) |= DCSR_RUN;
1096 DCSR(drv_data->tx_channel) |= DCSR_RUN;
Stephen Streete0c99052006-03-07 23:53:24 -08001097 } else {
1098 /* Ensure we have the correct interrupt handler */
1099 drv_data->transfer_handler = interrupt_transfer;
1100
Stephen Street8d94cc52006-12-10 02:18:54 -08001101 /* Clear status */
1102 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001103 write_SSSR(drv_data->clear_sr, reg);
Stephen Street8d94cc52006-12-10 02:18:54 -08001104 }
1105
1106 /* see if we need to reload the config registers */
1107 if ((read_SSCR0(reg) != cr0)
1108 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1109 (cr1 & SSCR1_CHANGE_MASK)) {
1110
Ned Forresterb97c74b2008-02-23 15:23:40 -08001111 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -08001112 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001113 if (drv_data->ssp_type != PXA25x_SSP)
1114 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001115 /* first set CR1 without interrupt and service enables */
1116 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1117 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -08001118 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001119
Stephen Street8d94cc52006-12-10 02:18:54 -08001120 } else {
1121 if (drv_data->ssp_type != PXA25x_SSP)
1122 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001123 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001124
Eric Miaoa7bb3902009-04-06 19:00:54 -07001125 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001126
1127 /* after chip select, release the data by enabling service
1128 * requests and interrupts, without changing any mode bits */
1129 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001130}
1131
David Howells6d5aefb2006-12-05 19:36:26 +00001132static void pump_messages(struct work_struct *work)
Stephen Streete0c99052006-03-07 23:53:24 -08001133{
David Howells6d5aefb2006-12-05 19:36:26 +00001134 struct driver_data *drv_data =
1135 container_of(work, struct driver_data, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001136 unsigned long flags;
1137
1138 /* Lock queue and check for queue work */
1139 spin_lock_irqsave(&drv_data->lock, flags);
1140 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1141 drv_data->busy = 0;
1142 spin_unlock_irqrestore(&drv_data->lock, flags);
1143 return;
1144 }
1145
1146 /* Make sure we are not already running a message */
1147 if (drv_data->cur_msg) {
1148 spin_unlock_irqrestore(&drv_data->lock, flags);
1149 return;
1150 }
1151
1152 /* Extract head of queue */
1153 drv_data->cur_msg = list_entry(drv_data->queue.next,
1154 struct spi_message, queue);
1155 list_del_init(&drv_data->cur_msg->queue);
Stephen Streete0c99052006-03-07 23:53:24 -08001156
1157 /* Initial message state*/
1158 drv_data->cur_msg->state = START_STATE;
1159 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1160 struct spi_transfer,
1161 transfer_list);
1162
Stephen Street8d94cc52006-12-10 02:18:54 -08001163 /* prepare to setup the SSP, in pump_transfers, using the per
1164 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001165 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001166
1167 /* Mark as busy and launch transfers */
1168 tasklet_schedule(&drv_data->pump_transfers);
Stephen Street5daa3ba2006-05-20 15:00:19 -07001169
1170 drv_data->busy = 1;
1171 spin_unlock_irqrestore(&drv_data->lock, flags);
Stephen Streete0c99052006-03-07 23:53:24 -08001172}
1173
1174static int transfer(struct spi_device *spi, struct spi_message *msg)
1175{
1176 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1177 unsigned long flags;
1178
1179 spin_lock_irqsave(&drv_data->lock, flags);
1180
1181 if (drv_data->run == QUEUE_STOPPED) {
1182 spin_unlock_irqrestore(&drv_data->lock, flags);
1183 return -ESHUTDOWN;
1184 }
1185
1186 msg->actual_length = 0;
1187 msg->status = -EINPROGRESS;
1188 msg->state = START_STATE;
1189
1190 list_add_tail(&msg->queue, &drv_data->queue);
1191
1192 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1193 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1194
1195 spin_unlock_irqrestore(&drv_data->lock, flags);
1196
1197 return 0;
1198}
1199
Eric Miaoa7bb3902009-04-06 19:00:54 -07001200static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1201 struct pxa2xx_spi_chip *chip_info)
1202{
1203 int err = 0;
1204
1205 if (chip == NULL || chip_info == NULL)
1206 return 0;
1207
1208 /* NOTE: setup() can be called multiple times, possibly with
1209 * different chip_info, release previously requested GPIO
1210 */
1211 if (gpio_is_valid(chip->gpio_cs))
1212 gpio_free(chip->gpio_cs);
1213
1214 /* If (*cs_control) is provided, ignore GPIO chip select */
1215 if (chip_info->cs_control) {
1216 chip->cs_control = chip_info->cs_control;
1217 return 0;
1218 }
1219
1220 if (gpio_is_valid(chip_info->gpio_cs)) {
1221 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1222 if (err) {
1223 dev_err(&spi->dev, "failed to request chip select "
1224 "GPIO%d\n", chip_info->gpio_cs);
1225 return err;
1226 }
1227
1228 chip->gpio_cs = chip_info->gpio_cs;
1229 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1230
1231 err = gpio_direction_output(chip->gpio_cs,
1232 !chip->gpio_cs_inverted);
1233 }
1234
1235 return err;
1236}
1237
Stephen Streete0c99052006-03-07 23:53:24 -08001238static int setup(struct spi_device *spi)
1239{
1240 struct pxa2xx_spi_chip *chip_info = NULL;
1241 struct chip_data *chip;
1242 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
eric miao2f1a74e2007-11-21 18:50:53 +08001243 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001244 unsigned int clk_div;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001245 uint tx_thres = TX_THRESH_DFLT;
1246 uint rx_thres = RX_THRESH_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001247
Stephen Streete0c99052006-03-07 23:53:24 -08001248 if (drv_data->ssp_type != PXA25x_SSP
Stephen Street8d94cc52006-12-10 02:18:54 -08001249 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1250 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1251 "b/w not 4-32 for type non-PXA25x_SSP\n",
1252 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001253 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001254 }
1255 else if (drv_data->ssp_type == PXA25x_SSP
1256 && (spi->bits_per_word < 4
1257 || spi->bits_per_word > 16)) {
1258 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1259 "b/w not 4-16 for type PXA25x_SSP\n",
1260 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001261 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001262 }
Stephen Streete0c99052006-03-07 23:53:24 -08001263
Stephen Street8d94cc52006-12-10 02:18:54 -08001264 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001265 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001266 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001267 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -08001268 if (!chip) {
1269 dev_err(&spi->dev,
1270 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001271 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -08001272 }
Stephen Streete0c99052006-03-07 23:53:24 -08001273
Eric Miaoa7bb3902009-04-06 19:00:54 -07001274 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001275 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001276 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001277 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1278 DCMD_BURST8 : 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001279 }
1280
Stephen Street8d94cc52006-12-10 02:18:54 -08001281 /* protocol drivers may change the chip settings, so...
1282 * if chip_info exists, use it */
1283 chip_info = spi->controller_data;
1284
Stephen Streete0c99052006-03-07 23:53:24 -08001285 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001286 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001287 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001288 if (chip_info->timeout)
1289 chip->timeout = chip_info->timeout;
1290 if (chip_info->tx_threshold)
1291 tx_thres = chip_info->tx_threshold;
1292 if (chip_info->rx_threshold)
1293 rx_thres = chip_info->rx_threshold;
1294 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001295 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001296 if (chip_info->enable_loopback)
1297 chip->cr1 = SSCR1_LBM;
1298 }
1299
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001300 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1301 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1302
Stephen Street8d94cc52006-12-10 02:18:54 -08001303 /* set dma burst and threshold outside of chip_info path so that if
1304 * chip_info goes away after setting chip->enable_dma, the
1305 * burst and threshold can still respond to changes in bits_per_word */
1306 if (chip->enable_dma) {
1307 /* set up legal burst and threshold for dma */
1308 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1309 &chip->dma_burst_size,
1310 &chip->dma_threshold)) {
1311 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1312 "to match bits_per_word\n");
1313 }
1314 }
1315
eric miao2f1a74e2007-11-21 18:50:53 +08001316 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001317 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001318
1319 chip->cr0 = clk_div
1320 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001321 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1322 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -08001323 | SSCR0_SSE
1324 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001325 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1326 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1327 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001328
1329 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1330 if (drv_data->ssp_type != PXA25x_SSP)
David Brownell7d077192009-06-17 16:26:03 -07001331 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Eric Miaoc9840da2010-03-16 16:48:01 +08001332 clk_get_rate(ssp->clk)
1333 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
1334 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001335 else
David Brownell7d077192009-06-17 16:26:03 -07001336 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Eric Miaoc9840da2010-03-16 16:48:01 +08001337 clk_get_rate(ssp->clk) / 2
1338 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1339 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001340
1341 if (spi->bits_per_word <= 8) {
1342 chip->n_bytes = 1;
1343 chip->dma_width = DCMD_WIDTH1;
1344 chip->read = u8_reader;
1345 chip->write = u8_writer;
1346 } else if (spi->bits_per_word <= 16) {
1347 chip->n_bytes = 2;
1348 chip->dma_width = DCMD_WIDTH2;
1349 chip->read = u16_reader;
1350 chip->write = u16_writer;
1351 } else if (spi->bits_per_word <= 32) {
1352 chip->cr0 |= SSCR0_EDSS;
1353 chip->n_bytes = 4;
1354 chip->dma_width = DCMD_WIDTH4;
1355 chip->read = u32_reader;
1356 chip->write = u32_writer;
1357 } else {
1358 dev_err(&spi->dev, "invalid wordsize\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001359 return -ENODEV;
1360 }
Stephen Street9708c122006-03-28 14:05:23 -08001361 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001362
1363 spi_set_ctldata(spi, chip);
1364
Eric Miaoa7bb3902009-04-06 19:00:54 -07001365 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001366}
1367
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001368static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001369{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001370 struct chip_data *chip = spi_get_ctldata(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001371
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001372 if (!chip)
1373 return;
1374
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
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001381static int __devinit 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
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001469static int __devinit 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
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001480 ssp = pxa_ssp_request(pdev->id, pdev->name);
eric miao2f1a74e2007-11-21 18:50:53 +08001481 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");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001490 pxa_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
David Brownelle7db06b2009-06-17 16:26:04 -07001499 /* the spi->mode bits understood by this driver: */
Daniel Ribeiro50e0a7b2009-06-17 16:26:06 -07001500 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
David Brownelle7db06b2009-06-17 16:26:04 -07001501
Stephen Streete0c99052006-03-07 23:53:24 -08001502 master->bus_num = pdev->id;
1503 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001504 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001505 master->cleanup = cleanup;
1506 master->setup = setup;
1507 master->transfer = transfer;
1508
eric miao2f1a74e2007-11-21 18:50:53 +08001509 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001510 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1511 sizeof(struct driver_data)), 8);
1512
eric miao2f1a74e2007-11-21 18:50:53 +08001513 drv_data->ioaddr = ssp->mmio_base;
1514 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1515 if (ssp->type == PXA25x_SSP) {
Stephen Streete0c99052006-03-07 23:53:24 -08001516 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1517 drv_data->dma_cr1 = 0;
1518 drv_data->clear_sr = SSSR_ROR;
1519 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1520 } else {
1521 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1522 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1523 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1524 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1525 }
1526
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001527 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1528 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001529 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001530 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001531 goto out_error_master_alloc;
1532 }
1533
1534 /* Setup DMA if requested */
1535 drv_data->tx_channel = -1;
1536 drv_data->rx_channel = -1;
1537 if (platform_info->enable_dma) {
1538
1539 /* Get two DMA channels (rx and tx) */
1540 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1541 DMA_PRIO_HIGH,
1542 dma_handler,
1543 drv_data);
1544 if (drv_data->rx_channel < 0) {
1545 dev_err(dev, "problem (%d) requesting rx channel\n",
1546 drv_data->rx_channel);
1547 status = -ENODEV;
1548 goto out_error_irq_alloc;
1549 }
1550 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1551 DMA_PRIO_MEDIUM,
1552 dma_handler,
1553 drv_data);
1554 if (drv_data->tx_channel < 0) {
1555 dev_err(dev, "problem (%d) requesting tx channel\n",
1556 drv_data->tx_channel);
1557 status = -ENODEV;
1558 goto out_error_dma_alloc;
1559 }
1560
eric miao2f1a74e2007-11-21 18:50:53 +08001561 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1562 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
Stephen Streete0c99052006-03-07 23:53:24 -08001563 }
1564
1565 /* Enable SOC clock */
eric miao2f1a74e2007-11-21 18:50:53 +08001566 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001567
1568 /* Load default SSP configuration */
1569 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001570 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1571 SSCR1_TxTresh(TX_THRESH_DFLT),
1572 drv_data->ioaddr);
Eric Miaoc9840da2010-03-16 16:48:01 +08001573 write_SSCR0(SSCR0_SCR(2)
Stephen Streete0c99052006-03-07 23:53:24 -08001574 | SSCR0_Motorola
1575 | SSCR0_DataSize(8),
1576 drv_data->ioaddr);
1577 if (drv_data->ssp_type != PXA25x_SSP)
1578 write_SSTO(0, drv_data->ioaddr);
1579 write_SSPSP(0, drv_data->ioaddr);
1580
1581 /* Initial and start queue */
1582 status = init_queue(drv_data);
1583 if (status != 0) {
1584 dev_err(&pdev->dev, "problem initializing queue\n");
1585 goto out_error_clock_enabled;
1586 }
1587 status = start_queue(drv_data);
1588 if (status != 0) {
1589 dev_err(&pdev->dev, "problem starting queue\n");
1590 goto out_error_clock_enabled;
1591 }
1592
1593 /* Register with the SPI framework */
1594 platform_set_drvdata(pdev, drv_data);
1595 status = spi_register_master(master);
1596 if (status != 0) {
1597 dev_err(&pdev->dev, "problem registering spi master\n");
1598 goto out_error_queue_alloc;
1599 }
1600
1601 return status;
1602
1603out_error_queue_alloc:
1604 destroy_queue(drv_data);
1605
1606out_error_clock_enabled:
eric miao2f1a74e2007-11-21 18:50:53 +08001607 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001608
1609out_error_dma_alloc:
1610 if (drv_data->tx_channel != -1)
1611 pxa_free_dma(drv_data->tx_channel);
1612 if (drv_data->rx_channel != -1)
1613 pxa_free_dma(drv_data->rx_channel);
1614
1615out_error_irq_alloc:
eric miao2f1a74e2007-11-21 18:50:53 +08001616 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001617
1618out_error_master_alloc:
1619 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001620 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001621 return status;
1622}
1623
1624static int pxa2xx_spi_remove(struct platform_device *pdev)
1625{
1626 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001627 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001628 int status = 0;
1629
1630 if (!drv_data)
1631 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001632 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001633
1634 /* Remove the queue */
1635 status = destroy_queue(drv_data);
1636 if (status != 0)
Stephen Street8d94cc52006-12-10 02:18:54 -08001637 /* the kernel does not check the return status of this
1638 * this routine (mod->exit, within the kernel). Therefore
1639 * nothing is gained by returning from here, the module is
1640 * going away regardless, and we should not leave any more
1641 * resources allocated than necessary. We cannot free the
1642 * message memory in drv_data->queue, but we can release the
1643 * resources below. I think the kernel should honor -EBUSY
1644 * returns but... */
1645 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1646 "complete, message memory not freed\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001647
1648 /* Disable the SSP at the peripheral and SOC level */
1649 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001650 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001651
1652 /* Release DMA */
1653 if (drv_data->master_info->enable_dma) {
eric miao2f1a74e2007-11-21 18:50:53 +08001654 DRCMR(ssp->drcmr_rx) = 0;
1655 DRCMR(ssp->drcmr_tx) = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001656 pxa_free_dma(drv_data->tx_channel);
1657 pxa_free_dma(drv_data->rx_channel);
1658 }
1659
1660 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001661 free_irq(ssp->irq, drv_data);
1662
1663 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001664 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001665
1666 /* Disconnect from the SPI framework */
1667 spi_unregister_master(drv_data->master);
1668
1669 /* Prevent double remove */
1670 platform_set_drvdata(pdev, NULL);
1671
1672 return 0;
1673}
1674
1675static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1676{
1677 int status = 0;
1678
1679 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1680 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1681}
1682
1683#ifdef CONFIG_PM
Mike Rapoport86d25932009-07-21 17:50:16 +03001684static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001685{
Mike Rapoport86d25932009-07-21 17:50:16 +03001686 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001687 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001688 int status = 0;
1689
Stephen Streete0c99052006-03-07 23:53:24 -08001690 status = stop_queue(drv_data);
1691 if (status != 0)
1692 return status;
1693 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001694 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001695
1696 return 0;
1697}
1698
Mike Rapoport86d25932009-07-21 17:50:16 +03001699static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001700{
Mike Rapoport86d25932009-07-21 17:50:16 +03001701 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001702 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001703 int status = 0;
1704
Daniel Ribeiro148da332009-04-21 12:24:43 -07001705 if (drv_data->rx_channel != -1)
1706 DRCMR(drv_data->ssp->drcmr_rx) =
1707 DRCMR_MAPVLD | drv_data->rx_channel;
1708 if (drv_data->tx_channel != -1)
1709 DRCMR(drv_data->ssp->drcmr_tx) =
1710 DRCMR_MAPVLD | drv_data->tx_channel;
1711
Stephen Streete0c99052006-03-07 23:53:24 -08001712 /* Enable the SSP clock */
Eric BENARD0cf942d2008-05-12 14:02:01 -07001713 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001714
1715 /* Start the queue running */
1716 status = start_queue(drv_data);
1717 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001718 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001719 return status;
1720 }
1721
1722 return 0;
1723}
Mike Rapoport86d25932009-07-21 17:50:16 +03001724
Alexey Dobriyan47145212009-12-14 18:00:08 -08001725static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001726 .suspend = pxa2xx_spi_suspend,
1727 .resume = pxa2xx_spi_resume,
1728};
1729#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001730
1731static struct platform_driver driver = {
1732 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001733 .name = "pxa2xx-spi",
1734 .owner = THIS_MODULE,
1735#ifdef CONFIG_PM
1736 .pm = &pxa2xx_spi_pm_ops,
1737#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001738 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001739 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001740 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001741 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001742};
1743
1744static int __init pxa2xx_spi_init(void)
1745{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001746 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001747}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001748subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001749
1750static void __exit pxa2xx_spi_exit(void)
1751{
1752 platform_driver_unregister(&driver);
1753}
1754module_exit(pxa2xx_spi_exit);