blob: 36828358a4d8602a1fdbbc56f5aeb588e3467d1a [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>
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/regs-ssp.h>
40#include <mach/ssp.h>
41#include <mach/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080042
43MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080044MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080045MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070046MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080047
48#define MAX_BUSES 3
49
Vernon Sauderf1f640a2008-10-15 22:02:43 -070050#define RX_THRESH_DFLT 8
51#define TX_THRESH_DFLT 8
52#define TIMOUT_DFLT 1000
53
Ned Forrester7e964452008-09-13 02:33:18 -070054#define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
55#define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
Mike Rapoport20b918d2008-10-01 10:39:24 -070056#define IS_DMA_ALIGNED(x) ((((u32)(x)) & 0x07) == 0)
Ned Forrester7e964452008-09-13 02:33:18 -070057#define MAX_DMA_LEN 8191
Mike Rapoport7ad0ba92009-04-06 19:00:57 -070058#define DMA_ALIGNMENT 8
Stephen Streete0c99052006-03-07 23:53:24 -080059
Ned Forresterb97c74b2008-02-23 15:23:40 -080060/*
61 * for testing SSCR1 changes that require SSP restart, basically
62 * everything except the service and interrupt enables, the pxa270 developer
63 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
64 * list, but the PXA255 dev man says all bits without really meaning the
65 * service and interrupt enables
66 */
67#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080068 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080069 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
70 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
71 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
72 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080073
Stephen Streete0c99052006-03-07 23:53:24 -080074#define DEFINE_SSP_REG(reg, off) \
David Brownellcf433692008-04-28 02:14:17 -070075static inline u32 read_##reg(void const __iomem *p) \
76{ return __raw_readl(p + (off)); } \
77\
78static inline void write_##reg(u32 v, void __iomem *p) \
79{ __raw_writel(v, p + (off)); }
Stephen Streete0c99052006-03-07 23:53:24 -080080
81DEFINE_SSP_REG(SSCR0, 0x00)
82DEFINE_SSP_REG(SSCR1, 0x04)
83DEFINE_SSP_REG(SSSR, 0x08)
84DEFINE_SSP_REG(SSITR, 0x0c)
85DEFINE_SSP_REG(SSDR, 0x10)
86DEFINE_SSP_REG(SSTO, 0x28)
87DEFINE_SSP_REG(SSPSP, 0x2c)
88
89#define START_STATE ((void*)0)
90#define RUNNING_STATE ((void*)1)
91#define DONE_STATE ((void*)2)
92#define ERROR_STATE ((void*)-1)
93
94#define QUEUE_RUNNING 0
95#define QUEUE_STOPPED 1
96
97struct driver_data {
98 /* Driver model hookup */
99 struct platform_device *pdev;
100
eric miao2f1a74e2007-11-21 18:50:53 +0800101 /* SSP Info */
102 struct ssp_device *ssp;
103
Stephen Streete0c99052006-03-07 23:53:24 -0800104 /* SPI framework hookup */
105 enum pxa_ssp_type ssp_type;
106 struct spi_master *master;
107
108 /* PXA hookup */
109 struct pxa2xx_spi_master *master_info;
110
111 /* DMA setup stuff */
112 int rx_channel;
113 int tx_channel;
114 u32 *null_dma_buf;
115
116 /* SSP register addresses */
David Brownellcf433692008-04-28 02:14:17 -0700117 void __iomem *ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800118 u32 ssdr_physical;
119
120 /* SSP masks*/
121 u32 dma_cr1;
122 u32 int_cr1;
123 u32 clear_sr;
124 u32 mask_sr;
125
126 /* Driver message queue */
127 struct workqueue_struct *workqueue;
128 struct work_struct pump_messages;
129 spinlock_t lock;
130 struct list_head queue;
131 int busy;
132 int run;
133
134 /* Message Transfer pump */
135 struct tasklet_struct pump_transfers;
136
137 /* Current message transfer state info */
138 struct spi_message* cur_msg;
139 struct spi_transfer* cur_transfer;
140 struct chip_data *cur_chip;
141 size_t len;
142 void *tx;
143 void *tx_end;
144 void *rx;
145 void *rx_end;
146 int dma_mapped;
147 dma_addr_t rx_dma;
148 dma_addr_t tx_dma;
149 size_t rx_map_len;
150 size_t tx_map_len;
Stephen Street9708c122006-03-28 14:05:23 -0800151 u8 n_bytes;
152 u32 dma_width;
Stephen Street8d94cc52006-12-10 02:18:54 -0800153 int (*write)(struct driver_data *drv_data);
154 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800155 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
156 void (*cs_control)(u32 command);
157};
158
159struct chip_data {
160 u32 cr0;
161 u32 cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800162 u32 psp;
163 u32 timeout;
164 u8 n_bytes;
165 u32 dma_width;
166 u32 dma_burst_size;
167 u32 threshold;
168 u32 dma_threshold;
169 u8 enable_dma;
Stephen Street9708c122006-03-28 14:05:23 -0800170 u8 bits_per_word;
171 u32 speed_hz;
Eric Miaoa7bb3902009-04-06 19:00:54 -0700172 int gpio_cs;
173 int gpio_cs_inverted;
Stephen Street8d94cc52006-12-10 02:18:54 -0800174 int (*write)(struct driver_data *drv_data);
175 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800176 void (*cs_control)(u32 command);
177};
178
David Howells6d5aefb2006-12-05 19:36:26 +0000179static void pump_messages(struct work_struct *work);
Stephen Streete0c99052006-03-07 23:53:24 -0800180
Eric Miaoa7bb3902009-04-06 19:00:54 -0700181static void cs_assert(struct driver_data *drv_data)
182{
183 struct chip_data *chip = drv_data->cur_chip;
184
185 if (chip->cs_control) {
186 chip->cs_control(PXA2XX_CS_ASSERT);
187 return;
188 }
189
190 if (gpio_is_valid(chip->gpio_cs))
191 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
192}
193
194static void cs_deassert(struct driver_data *drv_data)
195{
196 struct chip_data *chip = drv_data->cur_chip;
197
198 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300199 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700200 return;
201 }
202
203 if (gpio_is_valid(chip->gpio_cs))
204 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
205}
206
Stephen Streete0c99052006-03-07 23:53:24 -0800207static int flush(struct driver_data *drv_data)
208{
209 unsigned long limit = loops_per_jiffy << 1;
210
David Brownellcf433692008-04-28 02:14:17 -0700211 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800212
213 do {
214 while (read_SSSR(reg) & SSSR_RNE) {
215 read_SSDR(reg);
216 }
Roel Kluin306c68a2009-04-21 12:24:46 -0700217 } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
Stephen Streete0c99052006-03-07 23:53:24 -0800218 write_SSSR(SSSR_ROR, reg);
219
220 return limit;
221}
222
Stephen Street8d94cc52006-12-10 02:18:54 -0800223static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800224{
David Brownellcf433692008-04-28 02:14:17 -0700225 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800226 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800227
Stephen Street8d94cc52006-12-10 02:18:54 -0800228 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
229 || (drv_data->tx == drv_data->tx_end))
230 return 0;
231
232 write_SSDR(0, reg);
233 drv_data->tx += n_bytes;
234
235 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800236}
237
Stephen Street8d94cc52006-12-10 02:18:54 -0800238static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800239{
David Brownellcf433692008-04-28 02:14:17 -0700240 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800241 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800242
243 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800244 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800245 read_SSDR(reg);
246 drv_data->rx += n_bytes;
247 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800248
249 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800250}
251
Stephen Street8d94cc52006-12-10 02:18:54 -0800252static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800253{
David Brownellcf433692008-04-28 02:14:17 -0700254 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800255
Stephen Street8d94cc52006-12-10 02:18:54 -0800256 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
257 || (drv_data->tx == drv_data->tx_end))
258 return 0;
259
260 write_SSDR(*(u8 *)(drv_data->tx), reg);
261 ++drv_data->tx;
262
263 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800264}
265
Stephen Street8d94cc52006-12-10 02:18:54 -0800266static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800267{
David Brownellcf433692008-04-28 02:14:17 -0700268 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800269
270 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800271 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800272 *(u8 *)(drv_data->rx) = read_SSDR(reg);
273 ++drv_data->rx;
274 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800275
276 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800277}
278
Stephen Street8d94cc52006-12-10 02:18:54 -0800279static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800280{
David Brownellcf433692008-04-28 02:14:17 -0700281 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800282
Stephen Street8d94cc52006-12-10 02:18:54 -0800283 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
284 || (drv_data->tx == drv_data->tx_end))
285 return 0;
286
287 write_SSDR(*(u16 *)(drv_data->tx), reg);
288 drv_data->tx += 2;
289
290 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800291}
292
Stephen Street8d94cc52006-12-10 02:18:54 -0800293static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800294{
David Brownellcf433692008-04-28 02:14:17 -0700295 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800296
297 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800298 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800299 *(u16 *)(drv_data->rx) = read_SSDR(reg);
300 drv_data->rx += 2;
301 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800302
303 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800304}
Stephen Street8d94cc52006-12-10 02:18:54 -0800305
306static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800307{
David Brownellcf433692008-04-28 02:14:17 -0700308 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800309
Stephen Street8d94cc52006-12-10 02:18:54 -0800310 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
311 || (drv_data->tx == drv_data->tx_end))
312 return 0;
313
314 write_SSDR(*(u32 *)(drv_data->tx), reg);
315 drv_data->tx += 4;
316
317 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800318}
319
Stephen Street8d94cc52006-12-10 02:18:54 -0800320static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800321{
David Brownellcf433692008-04-28 02:14:17 -0700322 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800323
324 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800325 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800326 *(u32 *)(drv_data->rx) = read_SSDR(reg);
327 drv_data->rx += 4;
328 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800329
330 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800331}
332
333static void *next_transfer(struct driver_data *drv_data)
334{
335 struct spi_message *msg = drv_data->cur_msg;
336 struct spi_transfer *trans = drv_data->cur_transfer;
337
338 /* Move to next transfer */
339 if (trans->transfer_list.next != &msg->transfers) {
340 drv_data->cur_transfer =
341 list_entry(trans->transfer_list.next,
342 struct spi_transfer,
343 transfer_list);
344 return RUNNING_STATE;
345 } else
346 return DONE_STATE;
347}
348
349static int map_dma_buffers(struct driver_data *drv_data)
350{
351 struct spi_message *msg = drv_data->cur_msg;
352 struct device *dev = &msg->spi->dev;
353
354 if (!drv_data->cur_chip->enable_dma)
355 return 0;
356
357 if (msg->is_dma_mapped)
358 return drv_data->rx_dma && drv_data->tx_dma;
359
360 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
361 return 0;
362
363 /* Modify setup if rx buffer is null */
364 if (drv_data->rx == NULL) {
365 *drv_data->null_dma_buf = 0;
366 drv_data->rx = drv_data->null_dma_buf;
367 drv_data->rx_map_len = 4;
368 } else
369 drv_data->rx_map_len = drv_data->len;
370
371
372 /* Modify setup if tx buffer is null */
373 if (drv_data->tx == NULL) {
374 *drv_data->null_dma_buf = 0;
375 drv_data->tx = drv_data->null_dma_buf;
376 drv_data->tx_map_len = 4;
377 } else
378 drv_data->tx_map_len = drv_data->len;
379
Ned Forrester393df742008-11-19 15:36:21 -0800380 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
381 * so we flush the cache *before* invalidating it, in case
382 * the tx and rx buffers overlap.
383 */
384 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
385 drv_data->tx_map_len, DMA_TO_DEVICE);
386 if (dma_mapping_error(dev, drv_data->tx_dma))
Stephen Streete0c99052006-03-07 23:53:24 -0800387 return 0;
388
Ned Forrester393df742008-11-19 15:36:21 -0800389 /* Stream map the rx buffer */
390 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
Stephen Streete0c99052006-03-07 23:53:24 -0800391 drv_data->rx_map_len, DMA_FROM_DEVICE);
Ned Forrester393df742008-11-19 15:36:21 -0800392 if (dma_mapping_error(dev, drv_data->rx_dma)) {
393 dma_unmap_single(dev, drv_data->tx_dma,
394 drv_data->tx_map_len, DMA_TO_DEVICE);
Stephen Streete0c99052006-03-07 23:53:24 -0800395 return 0;
396 }
397
398 return 1;
399}
400
401static void unmap_dma_buffers(struct driver_data *drv_data)
402{
403 struct device *dev;
404
405 if (!drv_data->dma_mapped)
406 return;
407
408 if (!drv_data->cur_msg->is_dma_mapped) {
409 dev = &drv_data->cur_msg->spi->dev;
410 dma_unmap_single(dev, drv_data->rx_dma,
411 drv_data->rx_map_len, DMA_FROM_DEVICE);
412 dma_unmap_single(dev, drv_data->tx_dma,
413 drv_data->tx_map_len, DMA_TO_DEVICE);
414 }
415
416 drv_data->dma_mapped = 0;
417}
418
419/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700420static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800421{
422 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700423 unsigned long flags;
424 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800425
Stephen Street5daa3ba2006-05-20 15:00:19 -0700426 spin_lock_irqsave(&drv_data->lock, flags);
427 msg = drv_data->cur_msg;
428 drv_data->cur_msg = NULL;
429 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700430 queue_work(drv_data->workqueue, &drv_data->pump_messages);
431 spin_unlock_irqrestore(&drv_data->lock, flags);
432
433 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800434 struct spi_transfer,
435 transfer_list);
436
Ned Forrester84235972008-09-13 02:33:17 -0700437 /* Delay if requested before any change in chip select */
438 if (last_transfer->delay_usecs)
439 udelay(last_transfer->delay_usecs);
440
441 /* Drop chip select UNLESS cs_change is true or we are returning
442 * a message with an error, or next message is for another chip
443 */
Stephen Streete0c99052006-03-07 23:53:24 -0800444 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700445 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700446 else {
447 struct spi_message *next_msg;
448
449 /* Holding of cs was hinted, but we need to make sure
450 * the next message is for the same chip. Don't waste
451 * time with the following tests unless this was hinted.
452 *
453 * We cannot postpone this until pump_messages, because
454 * after calling msg->complete (below) the driver that
455 * sent the current message could be unloaded, which
456 * could invalidate the cs_control() callback...
457 */
458
459 /* get a pointer to the next message, if any */
460 spin_lock_irqsave(&drv_data->lock, flags);
461 if (list_empty(&drv_data->queue))
462 next_msg = NULL;
463 else
464 next_msg = list_entry(drv_data->queue.next,
465 struct spi_message, queue);
466 spin_unlock_irqrestore(&drv_data->lock, flags);
467
468 /* see if the next and current messages point
469 * to the same chip
470 */
471 if (next_msg && next_msg->spi != msg->spi)
472 next_msg = NULL;
473 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700474 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700475 }
Stephen Streete0c99052006-03-07 23:53:24 -0800476
Stephen Street5daa3ba2006-05-20 15:00:19 -0700477 msg->state = NULL;
478 if (msg->complete)
479 msg->complete(msg->context);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700480
481 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800482}
483
David Brownellcf433692008-04-28 02:14:17 -0700484static int wait_ssp_rx_stall(void const __iomem *ioaddr)
Stephen Streete0c99052006-03-07 23:53:24 -0800485{
486 unsigned long limit = loops_per_jiffy << 1;
487
Roel Kluin306c68a2009-04-21 12:24:46 -0700488 while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800489 cpu_relax();
490
491 return limit;
492}
493
494static int wait_dma_channel_stop(int channel)
495{
496 unsigned long limit = loops_per_jiffy << 1;
497
Roel Kluin306c68a2009-04-21 12:24:46 -0700498 while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800499 cpu_relax();
500
501 return limit;
502}
503
David Brownellcf433692008-04-28 02:14:17 -0700504static void dma_error_stop(struct driver_data *drv_data, const char *msg)
Stephen Street8d94cc52006-12-10 02:18:54 -0800505{
David Brownellcf433692008-04-28 02:14:17 -0700506 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800507
508 /* Stop and reset */
509 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
510 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
511 write_SSSR(drv_data->clear_sr, reg);
512 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
513 if (drv_data->ssp_type != PXA25x_SSP)
514 write_SSTO(0, reg);
515 flush(drv_data);
516 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
517
518 unmap_dma_buffers(drv_data);
519
520 dev_err(&drv_data->pdev->dev, "%s\n", msg);
521
522 drv_data->cur_msg->state = ERROR_STATE;
523 tasklet_schedule(&drv_data->pump_transfers);
524}
525
526static void dma_transfer_complete(struct driver_data *drv_data)
527{
David Brownellcf433692008-04-28 02:14:17 -0700528 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800529 struct spi_message *msg = drv_data->cur_msg;
530
531 /* Clear and disable interrupts on SSP and DMA channels*/
532 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
533 write_SSSR(drv_data->clear_sr, reg);
534 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
535 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
536
537 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
538 dev_err(&drv_data->pdev->dev,
539 "dma_handler: dma rx channel stop failed\n");
540
541 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
542 dev_err(&drv_data->pdev->dev,
543 "dma_transfer: ssp rx stall failed\n");
544
545 unmap_dma_buffers(drv_data);
546
547 /* update the buffer pointer for the amount completed in dma */
548 drv_data->rx += drv_data->len -
549 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
550
551 /* read trailing data from fifo, it does not matter how many
552 * bytes are in the fifo just read until buffer is full
553 * or fifo is empty, which ever occurs first */
554 drv_data->read(drv_data);
555
556 /* return count of what was actually read */
557 msg->actual_length += drv_data->len -
558 (drv_data->rx_end - drv_data->rx);
559
Ned Forrester84235972008-09-13 02:33:17 -0700560 /* Transfer delays and chip select release are
561 * handled in pump_transfers or giveback
562 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800563
564 /* Move to next transfer */
565 msg->state = next_transfer(drv_data);
566
567 /* Schedule transfer tasklet */
568 tasklet_schedule(&drv_data->pump_transfers);
569}
570
David Howells7d12e782006-10-05 14:55:46 +0100571static void dma_handler(int channel, void *data)
Stephen Streete0c99052006-03-07 23:53:24 -0800572{
573 struct driver_data *drv_data = data;
Stephen Streete0c99052006-03-07 23:53:24 -0800574 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
Stephen Streete0c99052006-03-07 23:53:24 -0800575
576 if (irq_status & DCSR_BUSERR) {
577
Stephen Streete0c99052006-03-07 23:53:24 -0800578 if (channel == drv_data->tx_channel)
Stephen Street8d94cc52006-12-10 02:18:54 -0800579 dma_error_stop(drv_data,
580 "dma_handler: "
581 "bad bus address on tx channel");
Stephen Streete0c99052006-03-07 23:53:24 -0800582 else
Stephen Street8d94cc52006-12-10 02:18:54 -0800583 dma_error_stop(drv_data,
584 "dma_handler: "
585 "bad bus address on rx channel");
586 return;
Stephen Streete0c99052006-03-07 23:53:24 -0800587 }
588
589 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
Stephen Street8d94cc52006-12-10 02:18:54 -0800590 if ((channel == drv_data->tx_channel)
591 && (irq_status & DCSR_ENDINTR)
592 && (drv_data->ssp_type == PXA25x_SSP)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800593
594 /* Wait for rx to stall */
595 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
596 dev_err(&drv_data->pdev->dev,
597 "dma_handler: ssp rx stall failed\n");
598
Stephen Street8d94cc52006-12-10 02:18:54 -0800599 /* finish this transfer, start the next */
600 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800601 }
602}
603
604static irqreturn_t dma_transfer(struct driver_data *drv_data)
605{
606 u32 irq_status;
David Brownellcf433692008-04-28 02:14:17 -0700607 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800608
609 irq_status = read_SSSR(reg) & drv_data->mask_sr;
610 if (irq_status & SSSR_ROR) {
Stephen Street8d94cc52006-12-10 02:18:54 -0800611 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
Stephen Streete0c99052006-03-07 23:53:24 -0800612 return IRQ_HANDLED;
613 }
614
615 /* Check for false positive timeout */
Stephen Street8d94cc52006-12-10 02:18:54 -0800616 if ((irq_status & SSSR_TINT)
617 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800618 write_SSSR(SSSR_TINT, reg);
619 return IRQ_HANDLED;
620 }
621
622 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
623
Stephen Street8d94cc52006-12-10 02:18:54 -0800624 /* Clear and disable timeout interrupt, do the rest in
625 * dma_transfer_complete */
Stephen Streete0c99052006-03-07 23:53:24 -0800626 if (drv_data->ssp_type != PXA25x_SSP)
627 write_SSTO(0, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800628
Stephen Street8d94cc52006-12-10 02:18:54 -0800629 /* finish this transfer, start the next */
630 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800631
632 return IRQ_HANDLED;
633 }
634
635 /* Opps problem detected */
636 return IRQ_NONE;
637}
638
Stephen Street8d94cc52006-12-10 02:18:54 -0800639static void int_error_stop(struct driver_data *drv_data, const char* msg)
640{
David Brownellcf433692008-04-28 02:14:17 -0700641 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800642
643 /* Stop and reset SSP */
644 write_SSSR(drv_data->clear_sr, reg);
645 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
646 if (drv_data->ssp_type != PXA25x_SSP)
647 write_SSTO(0, reg);
648 flush(drv_data);
649 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
650
651 dev_err(&drv_data->pdev->dev, "%s\n", msg);
652
653 drv_data->cur_msg->state = ERROR_STATE;
654 tasklet_schedule(&drv_data->pump_transfers);
655}
656
657static void int_transfer_complete(struct driver_data *drv_data)
658{
David Brownellcf433692008-04-28 02:14:17 -0700659 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800660
661 /* Stop SSP */
662 write_SSSR(drv_data->clear_sr, reg);
663 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
664 if (drv_data->ssp_type != PXA25x_SSP)
665 write_SSTO(0, reg);
666
667 /* Update total byte transfered return count actual bytes read */
668 drv_data->cur_msg->actual_length += drv_data->len -
669 (drv_data->rx_end - drv_data->rx);
670
Ned Forrester84235972008-09-13 02:33:17 -0700671 /* Transfer delays and chip select release are
672 * handled in pump_transfers or giveback
673 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800674
675 /* Move to next transfer */
676 drv_data->cur_msg->state = next_transfer(drv_data);
677
678 /* Schedule transfer tasklet */
679 tasklet_schedule(&drv_data->pump_transfers);
680}
681
Stephen Streete0c99052006-03-07 23:53:24 -0800682static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
683{
David Brownellcf433692008-04-28 02:14:17 -0700684 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800685
Stephen Street5daa3ba2006-05-20 15:00:19 -0700686 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
687 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800688
Stephen Street8d94cc52006-12-10 02:18:54 -0800689 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800690
Stephen Street8d94cc52006-12-10 02:18:54 -0800691 if (irq_status & SSSR_ROR) {
692 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
693 return IRQ_HANDLED;
694 }
Stephen Streete0c99052006-03-07 23:53:24 -0800695
Stephen Street8d94cc52006-12-10 02:18:54 -0800696 if (irq_status & SSSR_TINT) {
697 write_SSSR(SSSR_TINT, reg);
698 if (drv_data->read(drv_data)) {
699 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800700 return IRQ_HANDLED;
701 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800702 }
Stephen Streete0c99052006-03-07 23:53:24 -0800703
Stephen Street8d94cc52006-12-10 02:18:54 -0800704 /* Drain rx fifo, Fill tx fifo and prevent overruns */
705 do {
706 if (drv_data->read(drv_data)) {
707 int_transfer_complete(drv_data);
708 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800709 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800710 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800711
Stephen Street8d94cc52006-12-10 02:18:54 -0800712 if (drv_data->read(drv_data)) {
713 int_transfer_complete(drv_data);
714 return IRQ_HANDLED;
715 }
Stephen Streete0c99052006-03-07 23:53:24 -0800716
Stephen Street8d94cc52006-12-10 02:18:54 -0800717 if (drv_data->tx == drv_data->tx_end) {
718 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
719 /* PXA25x_SSP has no timeout, read trailing bytes */
720 if (drv_data->ssp_type == PXA25x_SSP) {
721 if (!wait_ssp_rx_stall(reg))
722 {
723 int_error_stop(drv_data, "interrupt_transfer: "
724 "rx stall failed");
725 return IRQ_HANDLED;
726 }
727 if (!drv_data->read(drv_data))
728 {
729 int_error_stop(drv_data,
730 "interrupt_transfer: "
731 "trailing byte read failed");
732 return IRQ_HANDLED;
733 }
734 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800735 }
Stephen Streete0c99052006-03-07 23:53:24 -0800736 }
737
Stephen Street5daa3ba2006-05-20 15:00:19 -0700738 /* We did something */
739 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800740}
741
David Howells7d12e782006-10-05 14:55:46 +0100742static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800743{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400744 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700745 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800746
747 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700748
749 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
750 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
751 if (drv_data->ssp_type != PXA25x_SSP)
752 write_SSTO(0, reg);
753 write_SSSR(drv_data->clear_sr, reg);
754
Stephen Streete0c99052006-03-07 23:53:24 -0800755 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800756 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700757
Stephen Streete0c99052006-03-07 23:53:24 -0800758 /* Never fail */
759 return IRQ_HANDLED;
760 }
761
762 return drv_data->transfer_handler(drv_data);
763}
764
David Brownellcf433692008-04-28 02:14:17 -0700765static int set_dma_burst_and_threshold(struct chip_data *chip,
766 struct spi_device *spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800767 u8 bits_per_word, u32 *burst_code,
768 u32 *threshold)
769{
770 struct pxa2xx_spi_chip *chip_info =
771 (struct pxa2xx_spi_chip *)spi->controller_data;
772 int bytes_per_word;
773 int burst_bytes;
774 int thresh_words;
775 int req_burst_size;
776 int retval = 0;
777
778 /* Set the threshold (in registers) to equal the same amount of data
779 * as represented by burst size (in bytes). The computation below
780 * is (burst_size rounded up to nearest 8 byte, word or long word)
781 * divided by (bytes/register); the tx threshold is the inverse of
782 * the rx, so that there will always be enough data in the rx fifo
783 * to satisfy a burst, and there will always be enough space in the
784 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
785 * there is not enough space), there must always remain enough empty
786 * space in the rx fifo for any data loaded to the tx fifo.
787 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
788 * will be 8, or half the fifo;
789 * The threshold can only be set to 2, 4 or 8, but not 16, because
790 * to burst 16 to the tx fifo, the fifo would have to be empty;
791 * however, the minimum fifo trigger level is 1, and the tx will
792 * request service when the fifo is at this level, with only 15 spaces.
793 */
794
795 /* find bytes/word */
796 if (bits_per_word <= 8)
797 bytes_per_word = 1;
798 else if (bits_per_word <= 16)
799 bytes_per_word = 2;
800 else
801 bytes_per_word = 4;
802
803 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
804 if (chip_info)
805 req_burst_size = chip_info->dma_burst_size;
806 else {
807 switch (chip->dma_burst_size) {
808 default:
809 /* if the default burst size is not set,
810 * do it now */
811 chip->dma_burst_size = DCMD_BURST8;
812 case DCMD_BURST8:
813 req_burst_size = 8;
814 break;
815 case DCMD_BURST16:
816 req_burst_size = 16;
817 break;
818 case DCMD_BURST32:
819 req_burst_size = 32;
820 break;
821 }
822 }
823 if (req_burst_size <= 8) {
824 *burst_code = DCMD_BURST8;
825 burst_bytes = 8;
826 } else if (req_burst_size <= 16) {
827 if (bytes_per_word == 1) {
828 /* don't burst more than 1/2 the fifo */
829 *burst_code = DCMD_BURST8;
830 burst_bytes = 8;
831 retval = 1;
832 } else {
833 *burst_code = DCMD_BURST16;
834 burst_bytes = 16;
835 }
836 } else {
837 if (bytes_per_word == 1) {
838 /* don't burst more than 1/2 the fifo */
839 *burst_code = DCMD_BURST8;
840 burst_bytes = 8;
841 retval = 1;
842 } else if (bytes_per_word == 2) {
843 /* don't burst more than 1/2 the fifo */
844 *burst_code = DCMD_BURST16;
845 burst_bytes = 16;
846 retval = 1;
847 } else {
848 *burst_code = DCMD_BURST32;
849 burst_bytes = 32;
850 }
851 }
852
853 thresh_words = burst_bytes / bytes_per_word;
854
855 /* thresh_words will be between 2 and 8 */
856 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
857 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
858
859 return retval;
860}
861
eric miao2f1a74e2007-11-21 18:50:53 +0800862static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
863{
864 unsigned long ssp_clk = clk_get_rate(ssp->clk);
865
866 if (ssp->type == PXA25x_SSP)
867 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
868 else
869 return ((ssp_clk / rate - 1) & 0xfff) << 8;
870}
871
Stephen Streete0c99052006-03-07 23:53:24 -0800872static void pump_transfers(unsigned long data)
873{
874 struct driver_data *drv_data = (struct driver_data *)data;
875 struct spi_message *message = NULL;
876 struct spi_transfer *transfer = NULL;
877 struct spi_transfer *previous = NULL;
878 struct chip_data *chip = NULL;
eric miao2f1a74e2007-11-21 18:50:53 +0800879 struct ssp_device *ssp = drv_data->ssp;
David Brownellcf433692008-04-28 02:14:17 -0700880 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800881 u32 clk_div = 0;
882 u8 bits = 0;
883 u32 speed = 0;
884 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800885 u32 cr1;
886 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
887 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800888
889 /* Get current state information */
890 message = drv_data->cur_msg;
891 transfer = drv_data->cur_transfer;
892 chip = drv_data->cur_chip;
893
894 /* Handle for abort */
895 if (message->state == ERROR_STATE) {
896 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700897 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800898 return;
899 }
900
901 /* Handle end of message */
902 if (message->state == DONE_STATE) {
903 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700904 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800905 return;
906 }
907
Ned Forrester84235972008-09-13 02:33:17 -0700908 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800909 if (message->state == RUNNING_STATE) {
910 previous = list_entry(transfer->transfer_list.prev,
911 struct spi_transfer,
912 transfer_list);
913 if (previous->delay_usecs)
914 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700915
916 /* Drop chip select only if cs_change is requested */
917 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700918 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800919 }
920
Ned Forrester7e964452008-09-13 02:33:18 -0700921 /* Check for transfers that need multiple DMA segments */
922 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
923
924 /* reject already-mapped transfers; PIO won't always work */
925 if (message->is_dma_mapped
926 || transfer->rx_dma || transfer->tx_dma) {
927 dev_err(&drv_data->pdev->dev,
928 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700929 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700930 transfer->len, MAX_DMA_LEN);
931 message->status = -EINVAL;
932 giveback(drv_data);
933 return;
934 }
935
936 /* warn ... we force this to PIO mode */
937 if (printk_ratelimit())
938 dev_warn(&message->spi->dev, "pump_transfers: "
939 "DMA disabled for transfer length %ld "
940 "greater than %d\n",
941 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800942 }
943
Stephen Streete0c99052006-03-07 23:53:24 -0800944 /* Setup the transfer state based on the type of transfer */
945 if (flush(drv_data) == 0) {
946 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
947 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700948 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800949 return;
950 }
Stephen Street9708c122006-03-28 14:05:23 -0800951 drv_data->n_bytes = chip->n_bytes;
952 drv_data->dma_width = chip->dma_width;
Stephen Streete0c99052006-03-07 23:53:24 -0800953 drv_data->tx = (void *)transfer->tx_buf;
954 drv_data->tx_end = drv_data->tx + transfer->len;
955 drv_data->rx = transfer->rx_buf;
956 drv_data->rx_end = drv_data->rx + transfer->len;
957 drv_data->rx_dma = transfer->rx_dma;
958 drv_data->tx_dma = transfer->tx_dma;
Stephen Street8d94cc52006-12-10 02:18:54 -0800959 drv_data->len = transfer->len & DCMD_LENGTH;
Stephen Streete0c99052006-03-07 23:53:24 -0800960 drv_data->write = drv_data->tx ? chip->write : null_writer;
961 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800962
963 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800964 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800965 if (transfer->speed_hz || transfer->bits_per_word) {
966
Stephen Street9708c122006-03-28 14:05:23 -0800967 bits = chip->bits_per_word;
968 speed = chip->speed_hz;
969
970 if (transfer->speed_hz)
971 speed = transfer->speed_hz;
972
973 if (transfer->bits_per_word)
974 bits = transfer->bits_per_word;
975
eric miao2f1a74e2007-11-21 18:50:53 +0800976 clk_div = ssp_get_clk_div(ssp, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800977
978 if (bits <= 8) {
979 drv_data->n_bytes = 1;
980 drv_data->dma_width = DCMD_WIDTH1;
981 drv_data->read = drv_data->read != null_reader ?
982 u8_reader : null_reader;
983 drv_data->write = drv_data->write != null_writer ?
984 u8_writer : null_writer;
985 } else if (bits <= 16) {
986 drv_data->n_bytes = 2;
987 drv_data->dma_width = DCMD_WIDTH2;
988 drv_data->read = drv_data->read != null_reader ?
989 u16_reader : null_reader;
990 drv_data->write = drv_data->write != null_writer ?
991 u16_writer : null_writer;
992 } else if (bits <= 32) {
993 drv_data->n_bytes = 4;
994 drv_data->dma_width = DCMD_WIDTH4;
995 drv_data->read = drv_data->read != null_reader ?
996 u32_reader : null_reader;
997 drv_data->write = drv_data->write != null_writer ?
998 u32_writer : null_writer;
999 }
Stephen Street8d94cc52006-12-10 02:18:54 -08001000 /* if bits/word is changed in dma mode, then must check the
1001 * thresholds and burst also */
1002 if (chip->enable_dma) {
1003 if (set_dma_burst_and_threshold(chip, message->spi,
1004 bits, &dma_burst,
1005 &dma_thresh))
1006 if (printk_ratelimit())
1007 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -07001008 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -08001009 "DMA burst size reduced to "
1010 "match bits_per_word\n");
1011 }
Stephen Street9708c122006-03-28 14:05:23 -08001012
1013 cr0 = clk_div
1014 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001015 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -08001016 | SSCR0_SSE
1017 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -08001018 }
1019
Stephen Streete0c99052006-03-07 23:53:24 -08001020 message->state = RUNNING_STATE;
1021
Ned Forrester7e964452008-09-13 02:33:18 -07001022 /* Try to map dma buffer and do a dma transfer if successful, but
1023 * only if the length is non-zero and less than MAX_DMA_LEN.
1024 *
1025 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
1026 * of PIO instead. Care is needed above because the transfer may
1027 * have have been passed with buffers that are already dma mapped.
1028 * A zero-length transfer in PIO mode will not try to write/read
1029 * to/from the buffers
1030 *
1031 * REVISIT large transfers are exactly where we most want to be
1032 * using DMA. If this happens much, split those transfers into
1033 * multiple DMA segments rather than forcing PIO.
1034 */
1035 drv_data->dma_mapped = 0;
1036 if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN)
1037 drv_data->dma_mapped = map_dma_buffers(drv_data);
1038 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001039
1040 /* Ensure we have the correct interrupt handler */
1041 drv_data->transfer_handler = dma_transfer;
1042
1043 /* Setup rx DMA Channel */
1044 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
1045 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
1046 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
1047 if (drv_data->rx == drv_data->null_dma_buf)
1048 /* No target address increment */
1049 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001050 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001051 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001052 | drv_data->len;
1053 else
1054 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
1055 | DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001056 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001057 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001058 | drv_data->len;
1059
1060 /* Setup tx DMA Channel */
1061 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
1062 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
1063 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
1064 if (drv_data->tx == drv_data->null_dma_buf)
1065 /* No source address increment */
1066 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
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 else
1071 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
1072 | DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001073 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001074 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001075 | drv_data->len;
1076
1077 /* Enable dma end irqs on SSP to detect end of transfer */
1078 if (drv_data->ssp_type == PXA25x_SSP)
1079 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
1080
Stephen Street8d94cc52006-12-10 02:18:54 -08001081 /* Clear status and start DMA engine */
1082 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001083 write_SSSR(drv_data->clear_sr, reg);
1084 DCSR(drv_data->rx_channel) |= DCSR_RUN;
1085 DCSR(drv_data->tx_channel) |= DCSR_RUN;
Stephen Streete0c99052006-03-07 23:53:24 -08001086 } else {
1087 /* Ensure we have the correct interrupt handler */
1088 drv_data->transfer_handler = interrupt_transfer;
1089
Stephen Street8d94cc52006-12-10 02:18:54 -08001090 /* Clear status */
1091 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001092 write_SSSR(drv_data->clear_sr, reg);
Stephen Street8d94cc52006-12-10 02:18:54 -08001093 }
1094
1095 /* see if we need to reload the config registers */
1096 if ((read_SSCR0(reg) != cr0)
1097 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1098 (cr1 & SSCR1_CHANGE_MASK)) {
1099
Ned Forresterb97c74b2008-02-23 15:23:40 -08001100 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -08001101 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001102 if (drv_data->ssp_type != PXA25x_SSP)
1103 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001104 /* first set CR1 without interrupt and service enables */
1105 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1106 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -08001107 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001108
Stephen Street8d94cc52006-12-10 02:18:54 -08001109 } else {
1110 if (drv_data->ssp_type != PXA25x_SSP)
1111 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001112 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001113
Eric Miaoa7bb3902009-04-06 19:00:54 -07001114 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001115
1116 /* after chip select, release the data by enabling service
1117 * requests and interrupts, without changing any mode bits */
1118 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001119}
1120
David Howells6d5aefb2006-12-05 19:36:26 +00001121static void pump_messages(struct work_struct *work)
Stephen Streete0c99052006-03-07 23:53:24 -08001122{
David Howells6d5aefb2006-12-05 19:36:26 +00001123 struct driver_data *drv_data =
1124 container_of(work, struct driver_data, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001125 unsigned long flags;
1126
1127 /* Lock queue and check for queue work */
1128 spin_lock_irqsave(&drv_data->lock, flags);
1129 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1130 drv_data->busy = 0;
1131 spin_unlock_irqrestore(&drv_data->lock, flags);
1132 return;
1133 }
1134
1135 /* Make sure we are not already running a message */
1136 if (drv_data->cur_msg) {
1137 spin_unlock_irqrestore(&drv_data->lock, flags);
1138 return;
1139 }
1140
1141 /* Extract head of queue */
1142 drv_data->cur_msg = list_entry(drv_data->queue.next,
1143 struct spi_message, queue);
1144 list_del_init(&drv_data->cur_msg->queue);
Stephen Streete0c99052006-03-07 23:53:24 -08001145
1146 /* Initial message state*/
1147 drv_data->cur_msg->state = START_STATE;
1148 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1149 struct spi_transfer,
1150 transfer_list);
1151
Stephen Street8d94cc52006-12-10 02:18:54 -08001152 /* prepare to setup the SSP, in pump_transfers, using the per
1153 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001154 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001155
1156 /* Mark as busy and launch transfers */
1157 tasklet_schedule(&drv_data->pump_transfers);
Stephen Street5daa3ba2006-05-20 15:00:19 -07001158
1159 drv_data->busy = 1;
1160 spin_unlock_irqrestore(&drv_data->lock, flags);
Stephen Streete0c99052006-03-07 23:53:24 -08001161}
1162
1163static int transfer(struct spi_device *spi, struct spi_message *msg)
1164{
1165 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1166 unsigned long flags;
1167
1168 spin_lock_irqsave(&drv_data->lock, flags);
1169
1170 if (drv_data->run == QUEUE_STOPPED) {
1171 spin_unlock_irqrestore(&drv_data->lock, flags);
1172 return -ESHUTDOWN;
1173 }
1174
1175 msg->actual_length = 0;
1176 msg->status = -EINPROGRESS;
1177 msg->state = START_STATE;
1178
1179 list_add_tail(&msg->queue, &drv_data->queue);
1180
1181 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1182 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1183
1184 spin_unlock_irqrestore(&drv_data->lock, flags);
1185
1186 return 0;
1187}
1188
Eric Miaoa7bb3902009-04-06 19:00:54 -07001189static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1190 struct pxa2xx_spi_chip *chip_info)
1191{
1192 int err = 0;
1193
1194 if (chip == NULL || chip_info == NULL)
1195 return 0;
1196
1197 /* NOTE: setup() can be called multiple times, possibly with
1198 * different chip_info, release previously requested GPIO
1199 */
1200 if (gpio_is_valid(chip->gpio_cs))
1201 gpio_free(chip->gpio_cs);
1202
1203 /* If (*cs_control) is provided, ignore GPIO chip select */
1204 if (chip_info->cs_control) {
1205 chip->cs_control = chip_info->cs_control;
1206 return 0;
1207 }
1208
1209 if (gpio_is_valid(chip_info->gpio_cs)) {
1210 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1211 if (err) {
1212 dev_err(&spi->dev, "failed to request chip select "
1213 "GPIO%d\n", chip_info->gpio_cs);
1214 return err;
1215 }
1216
1217 chip->gpio_cs = chip_info->gpio_cs;
1218 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1219
1220 err = gpio_direction_output(chip->gpio_cs,
1221 !chip->gpio_cs_inverted);
1222 }
1223
1224 return err;
1225}
1226
Stephen Streete0c99052006-03-07 23:53:24 -08001227static int setup(struct spi_device *spi)
1228{
1229 struct pxa2xx_spi_chip *chip_info = NULL;
1230 struct chip_data *chip;
1231 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
eric miao2f1a74e2007-11-21 18:50:53 +08001232 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001233 unsigned int clk_div;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001234 uint tx_thres = TX_THRESH_DFLT;
1235 uint rx_thres = RX_THRESH_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001236
Stephen Streete0c99052006-03-07 23:53:24 -08001237 if (drv_data->ssp_type != PXA25x_SSP
Stephen Street8d94cc52006-12-10 02:18:54 -08001238 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1239 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1240 "b/w not 4-32 for type non-PXA25x_SSP\n",
1241 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001242 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001243 }
1244 else if (drv_data->ssp_type == PXA25x_SSP
1245 && (spi->bits_per_word < 4
1246 || spi->bits_per_word > 16)) {
1247 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1248 "b/w not 4-16 for type PXA25x_SSP\n",
1249 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001250 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001251 }
Stephen Streete0c99052006-03-07 23:53:24 -08001252
Stephen Street8d94cc52006-12-10 02:18:54 -08001253 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001254 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001255 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001256 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -08001257 if (!chip) {
1258 dev_err(&spi->dev,
1259 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001260 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -08001261 }
Stephen Streete0c99052006-03-07 23:53:24 -08001262
Eric Miaoa7bb3902009-04-06 19:00:54 -07001263 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001264 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001265 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001266 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1267 DCMD_BURST8 : 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001268 }
1269
Stephen Street8d94cc52006-12-10 02:18:54 -08001270 /* protocol drivers may change the chip settings, so...
1271 * if chip_info exists, use it */
1272 chip_info = spi->controller_data;
1273
Stephen Streete0c99052006-03-07 23:53:24 -08001274 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001275 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001276 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001277 if (chip_info->timeout)
1278 chip->timeout = chip_info->timeout;
1279 if (chip_info->tx_threshold)
1280 tx_thres = chip_info->tx_threshold;
1281 if (chip_info->rx_threshold)
1282 rx_thres = chip_info->rx_threshold;
1283 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001284 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001285 if (chip_info->enable_loopback)
1286 chip->cr1 = SSCR1_LBM;
1287 }
1288
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001289 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1290 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1291
Stephen Street8d94cc52006-12-10 02:18:54 -08001292 /* set dma burst and threshold outside of chip_info path so that if
1293 * chip_info goes away after setting chip->enable_dma, the
1294 * burst and threshold can still respond to changes in bits_per_word */
1295 if (chip->enable_dma) {
1296 /* set up legal burst and threshold for dma */
1297 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1298 &chip->dma_burst_size,
1299 &chip->dma_threshold)) {
1300 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1301 "to match bits_per_word\n");
1302 }
1303 }
1304
eric miao2f1a74e2007-11-21 18:50:53 +08001305 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001306 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001307
1308 chip->cr0 = clk_div
1309 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001310 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1311 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -08001312 | SSCR0_SSE
1313 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001314 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1315 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1316 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001317
1318 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1319 if (drv_data->ssp_type != PXA25x_SSP)
David Brownell7d077192009-06-17 16:26:03 -07001320 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
eric miao2f1a74e2007-11-21 18:50:53 +08001321 clk_get_rate(ssp->clk)
Stephen Streete0c99052006-03-07 23:53:24 -08001322 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001323 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001324 else
David Brownell7d077192009-06-17 16:26:03 -07001325 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001326 clk_get_rate(ssp->clk) / 2
Stephen Streete0c99052006-03-07 23:53:24 -08001327 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001328 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001329
1330 if (spi->bits_per_word <= 8) {
1331 chip->n_bytes = 1;
1332 chip->dma_width = DCMD_WIDTH1;
1333 chip->read = u8_reader;
1334 chip->write = u8_writer;
1335 } else if (spi->bits_per_word <= 16) {
1336 chip->n_bytes = 2;
1337 chip->dma_width = DCMD_WIDTH2;
1338 chip->read = u16_reader;
1339 chip->write = u16_writer;
1340 } else if (spi->bits_per_word <= 32) {
1341 chip->cr0 |= SSCR0_EDSS;
1342 chip->n_bytes = 4;
1343 chip->dma_width = DCMD_WIDTH4;
1344 chip->read = u32_reader;
1345 chip->write = u32_writer;
1346 } else {
1347 dev_err(&spi->dev, "invalid wordsize\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001348 return -ENODEV;
1349 }
Stephen Street9708c122006-03-28 14:05:23 -08001350 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001351
1352 spi_set_ctldata(spi, chip);
1353
Eric Miaoa7bb3902009-04-06 19:00:54 -07001354 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001355}
1356
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001357static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001358{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001359 struct chip_data *chip = spi_get_ctldata(spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001360
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001361 if (!chip)
1362 return;
1363
Eric Miaoa7bb3902009-04-06 19:00:54 -07001364 if (gpio_is_valid(chip->gpio_cs))
1365 gpio_free(chip->gpio_cs);
1366
Stephen Streete0c99052006-03-07 23:53:24 -08001367 kfree(chip);
1368}
1369
David Brownelld1e44d92007-10-16 01:27:46 -07001370static int __init init_queue(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -08001371{
1372 INIT_LIST_HEAD(&drv_data->queue);
1373 spin_lock_init(&drv_data->lock);
1374
1375 drv_data->run = QUEUE_STOPPED;
1376 drv_data->busy = 0;
1377
1378 tasklet_init(&drv_data->pump_transfers,
1379 pump_transfers, (unsigned long)drv_data);
1380
David Howells6d5aefb2006-12-05 19:36:26 +00001381 INIT_WORK(&drv_data->pump_messages, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001382 drv_data->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001383 dev_name(drv_data->master->dev.parent));
Stephen Streete0c99052006-03-07 23:53:24 -08001384 if (drv_data->workqueue == NULL)
1385 return -EBUSY;
1386
1387 return 0;
1388}
1389
1390static int start_queue(struct driver_data *drv_data)
1391{
1392 unsigned long flags;
1393
1394 spin_lock_irqsave(&drv_data->lock, flags);
1395
1396 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1397 spin_unlock_irqrestore(&drv_data->lock, flags);
1398 return -EBUSY;
1399 }
1400
1401 drv_data->run = QUEUE_RUNNING;
1402 drv_data->cur_msg = NULL;
1403 drv_data->cur_transfer = NULL;
1404 drv_data->cur_chip = NULL;
1405 spin_unlock_irqrestore(&drv_data->lock, flags);
1406
1407 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1408
1409 return 0;
1410}
1411
1412static int stop_queue(struct driver_data *drv_data)
1413{
1414 unsigned long flags;
1415 unsigned limit = 500;
1416 int status = 0;
1417
1418 spin_lock_irqsave(&drv_data->lock, flags);
1419
1420 /* This is a bit lame, but is optimized for the common execution path.
1421 * A wait_queue on the drv_data->busy could be used, but then the common
1422 * execution path (pump_messages) would be required to call wake_up or
1423 * friends on every SPI message. Do this instead */
1424 drv_data->run = QUEUE_STOPPED;
1425 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1426 spin_unlock_irqrestore(&drv_data->lock, flags);
1427 msleep(10);
1428 spin_lock_irqsave(&drv_data->lock, flags);
1429 }
1430
1431 if (!list_empty(&drv_data->queue) || drv_data->busy)
1432 status = -EBUSY;
1433
1434 spin_unlock_irqrestore(&drv_data->lock, flags);
1435
1436 return status;
1437}
1438
1439static int destroy_queue(struct driver_data *drv_data)
1440{
1441 int status;
1442
1443 status = stop_queue(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -08001444 /* we are unloading the module or failing to load (only two calls
1445 * to this routine), and neither call can handle a return value.
1446 * However, destroy_workqueue calls flush_workqueue, and that will
1447 * block until all work is done. If the reason that stop_queue
1448 * timed out is that the work will never finish, then it does no
1449 * good to call destroy_workqueue, so return anyway. */
Stephen Streete0c99052006-03-07 23:53:24 -08001450 if (status != 0)
1451 return status;
1452
1453 destroy_workqueue(drv_data->workqueue);
1454
1455 return 0;
1456}
1457
David Brownelld1e44d92007-10-16 01:27:46 -07001458static int __init pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001459{
1460 struct device *dev = &pdev->dev;
1461 struct pxa2xx_spi_master *platform_info;
1462 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001463 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001464 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001465 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001466
1467 platform_info = dev->platform_data;
1468
eric miao2f1a74e2007-11-21 18:50:53 +08001469 ssp = ssp_request(pdev->id, pdev->name);
1470 if (ssp == NULL) {
1471 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
Stephen Streete0c99052006-03-07 23:53:24 -08001472 return -ENODEV;
1473 }
1474
1475 /* Allocate master with space for drv_data and null dma buffer */
1476 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1477 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001478 dev_err(&pdev->dev, "cannot alloc spi_master\n");
eric miao2f1a74e2007-11-21 18:50:53 +08001479 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001480 return -ENOMEM;
1481 }
1482 drv_data = spi_master_get_devdata(master);
1483 drv_data->master = master;
1484 drv_data->master_info = platform_info;
1485 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001486 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001487
David Brownelle7db06b2009-06-17 16:26:04 -07001488 /* the spi->mode bits understood by this driver: */
Daniel Ribeiro50e0a7b2009-06-17 16:26:06 -07001489 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
David Brownelle7db06b2009-06-17 16:26:04 -07001490
Stephen Streete0c99052006-03-07 23:53:24 -08001491 master->bus_num = pdev->id;
1492 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001493 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001494 master->cleanup = cleanup;
1495 master->setup = setup;
1496 master->transfer = transfer;
1497
eric miao2f1a74e2007-11-21 18:50:53 +08001498 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001499 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1500 sizeof(struct driver_data)), 8);
1501
eric miao2f1a74e2007-11-21 18:50:53 +08001502 drv_data->ioaddr = ssp->mmio_base;
1503 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1504 if (ssp->type == PXA25x_SSP) {
Stephen Streete0c99052006-03-07 23:53:24 -08001505 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1506 drv_data->dma_cr1 = 0;
1507 drv_data->clear_sr = SSSR_ROR;
1508 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1509 } else {
1510 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1511 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1512 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1513 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1514 }
1515
Kay Sievers6c7377a2009-03-24 16:38:21 -07001516 status = request_irq(ssp->irq, ssp_int, 0, dev_name(dev), drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001517 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001518 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001519 goto out_error_master_alloc;
1520 }
1521
1522 /* Setup DMA if requested */
1523 drv_data->tx_channel = -1;
1524 drv_data->rx_channel = -1;
1525 if (platform_info->enable_dma) {
1526
1527 /* Get two DMA channels (rx and tx) */
1528 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1529 DMA_PRIO_HIGH,
1530 dma_handler,
1531 drv_data);
1532 if (drv_data->rx_channel < 0) {
1533 dev_err(dev, "problem (%d) requesting rx channel\n",
1534 drv_data->rx_channel);
1535 status = -ENODEV;
1536 goto out_error_irq_alloc;
1537 }
1538 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1539 DMA_PRIO_MEDIUM,
1540 dma_handler,
1541 drv_data);
1542 if (drv_data->tx_channel < 0) {
1543 dev_err(dev, "problem (%d) requesting tx channel\n",
1544 drv_data->tx_channel);
1545 status = -ENODEV;
1546 goto out_error_dma_alloc;
1547 }
1548
eric miao2f1a74e2007-11-21 18:50:53 +08001549 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1550 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
Stephen Streete0c99052006-03-07 23:53:24 -08001551 }
1552
1553 /* Enable SOC clock */
eric miao2f1a74e2007-11-21 18:50:53 +08001554 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001555
1556 /* Load default SSP configuration */
1557 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001558 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1559 SSCR1_TxTresh(TX_THRESH_DFLT),
1560 drv_data->ioaddr);
Stephen Streete0c99052006-03-07 23:53:24 -08001561 write_SSCR0(SSCR0_SerClkDiv(2)
1562 | SSCR0_Motorola
1563 | SSCR0_DataSize(8),
1564 drv_data->ioaddr);
1565 if (drv_data->ssp_type != PXA25x_SSP)
1566 write_SSTO(0, drv_data->ioaddr);
1567 write_SSPSP(0, drv_data->ioaddr);
1568
1569 /* Initial and start queue */
1570 status = init_queue(drv_data);
1571 if (status != 0) {
1572 dev_err(&pdev->dev, "problem initializing queue\n");
1573 goto out_error_clock_enabled;
1574 }
1575 status = start_queue(drv_data);
1576 if (status != 0) {
1577 dev_err(&pdev->dev, "problem starting queue\n");
1578 goto out_error_clock_enabled;
1579 }
1580
1581 /* Register with the SPI framework */
1582 platform_set_drvdata(pdev, drv_data);
1583 status = spi_register_master(master);
1584 if (status != 0) {
1585 dev_err(&pdev->dev, "problem registering spi master\n");
1586 goto out_error_queue_alloc;
1587 }
1588
1589 return status;
1590
1591out_error_queue_alloc:
1592 destroy_queue(drv_data);
1593
1594out_error_clock_enabled:
eric miao2f1a74e2007-11-21 18:50:53 +08001595 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001596
1597out_error_dma_alloc:
1598 if (drv_data->tx_channel != -1)
1599 pxa_free_dma(drv_data->tx_channel);
1600 if (drv_data->rx_channel != -1)
1601 pxa_free_dma(drv_data->rx_channel);
1602
1603out_error_irq_alloc:
eric miao2f1a74e2007-11-21 18:50:53 +08001604 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001605
1606out_error_master_alloc:
1607 spi_master_put(master);
eric miao2f1a74e2007-11-21 18:50:53 +08001608 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001609 return status;
1610}
1611
1612static int pxa2xx_spi_remove(struct platform_device *pdev)
1613{
1614 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001615 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001616 int status = 0;
1617
1618 if (!drv_data)
1619 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001620 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001621
1622 /* Remove the queue */
1623 status = destroy_queue(drv_data);
1624 if (status != 0)
Stephen Street8d94cc52006-12-10 02:18:54 -08001625 /* the kernel does not check the return status of this
1626 * this routine (mod->exit, within the kernel). Therefore
1627 * nothing is gained by returning from here, the module is
1628 * going away regardless, and we should not leave any more
1629 * resources allocated than necessary. We cannot free the
1630 * message memory in drv_data->queue, but we can release the
1631 * resources below. I think the kernel should honor -EBUSY
1632 * returns but... */
1633 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1634 "complete, message memory not freed\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001635
1636 /* Disable the SSP at the peripheral and SOC level */
1637 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001638 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001639
1640 /* Release DMA */
1641 if (drv_data->master_info->enable_dma) {
eric miao2f1a74e2007-11-21 18:50:53 +08001642 DRCMR(ssp->drcmr_rx) = 0;
1643 DRCMR(ssp->drcmr_tx) = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001644 pxa_free_dma(drv_data->tx_channel);
1645 pxa_free_dma(drv_data->rx_channel);
1646 }
1647
1648 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001649 free_irq(ssp->irq, drv_data);
1650
1651 /* Release SSP */
1652 ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001653
1654 /* Disconnect from the SPI framework */
1655 spi_unregister_master(drv_data->master);
1656
1657 /* Prevent double remove */
1658 platform_set_drvdata(pdev, NULL);
1659
1660 return 0;
1661}
1662
1663static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1664{
1665 int status = 0;
1666
1667 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1668 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1669}
1670
1671#ifdef CONFIG_PM
Mike Rapoport86d25932009-07-21 17:50:16 +03001672static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001673{
Mike Rapoport86d25932009-07-21 17:50:16 +03001674 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001675 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001676 int status = 0;
1677
Stephen Streete0c99052006-03-07 23:53:24 -08001678 status = stop_queue(drv_data);
1679 if (status != 0)
1680 return status;
1681 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001682 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001683
1684 return 0;
1685}
1686
Mike Rapoport86d25932009-07-21 17:50:16 +03001687static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001688{
Mike Rapoport86d25932009-07-21 17:50:16 +03001689 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001690 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001691 int status = 0;
1692
Daniel Ribeiro148da332009-04-21 12:24:43 -07001693 if (drv_data->rx_channel != -1)
1694 DRCMR(drv_data->ssp->drcmr_rx) =
1695 DRCMR_MAPVLD | drv_data->rx_channel;
1696 if (drv_data->tx_channel != -1)
1697 DRCMR(drv_data->ssp->drcmr_tx) =
1698 DRCMR_MAPVLD | drv_data->tx_channel;
1699
Stephen Streete0c99052006-03-07 23:53:24 -08001700 /* Enable the SSP clock */
Eric BENARD0cf942d2008-05-12 14:02:01 -07001701 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001702
1703 /* Start the queue running */
1704 status = start_queue(drv_data);
1705 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001706 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001707 return status;
1708 }
1709
1710 return 0;
1711}
Mike Rapoport86d25932009-07-21 17:50:16 +03001712
Alexey Dobriyan47145212009-12-14 18:00:08 -08001713static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001714 .suspend = pxa2xx_spi_suspend,
1715 .resume = pxa2xx_spi_resume,
1716};
1717#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001718
1719static struct platform_driver driver = {
1720 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001721 .name = "pxa2xx-spi",
1722 .owner = THIS_MODULE,
1723#ifdef CONFIG_PM
1724 .pm = &pxa2xx_spi_pm_ops,
1725#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001726 },
David Brownelld1e44d92007-10-16 01:27:46 -07001727 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001728 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001729};
1730
1731static int __init pxa2xx_spi_init(void)
1732{
David Brownelld1e44d92007-10-16 01:27:46 -07001733 return platform_driver_probe(&driver, pxa2xx_spi_probe);
Stephen Streete0c99052006-03-07 23:53:24 -08001734}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001735subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001736
1737static void __exit pxa2xx_spi_exit(void)
1738{
1739 platform_driver_unregister(&driver);
1740}
1741module_exit(pxa2xx_spi_exit);