blob: 2ecc2d6464fbdd726a7425a69eb5872c1873687e [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>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080026#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080027#include <linux/dma-mapping.h>
28#include <linux/spi/spi.h>
29#include <linux/workqueue.h>
Stephen Streete0c99052006-03-07 23:53:24 -080030#include <linux/delay.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
Stephen Streete0c99052006-03-07 23:53:24 -080038
39MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080040MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080041MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070042MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080043
44#define MAX_BUSES 3
45
Vernon Sauderf1f640a2008-10-15 22:02:43 -070046#define TIMOUT_DFLT 1000
47
Ned Forrester7e964452008-09-13 02:33:18 -070048#define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
49#define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
Mike Rapoport20b918d2008-10-01 10:39:24 -070050#define IS_DMA_ALIGNED(x) ((((u32)(x)) & 0x07) == 0)
Ned Forrester7e964452008-09-13 02:33:18 -070051#define MAX_DMA_LEN 8191
Mike Rapoport7ad0ba92009-04-06 19:00:57 -070052#define DMA_ALIGNMENT 8
Stephen Streete0c99052006-03-07 23:53:24 -080053
Ned Forresterb97c74b2008-02-23 15:23:40 -080054/*
55 * for testing SSCR1 changes that require SSP restart, basically
56 * everything except the service and interrupt enables, the pxa270 developer
57 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
58 * list, but the PXA255 dev man says all bits without really meaning the
59 * service and interrupt enables
60 */
61#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080062 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080063 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
64 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
65 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
66 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080067
Stephen Streete0c99052006-03-07 23:53:24 -080068#define DEFINE_SSP_REG(reg, off) \
David Brownellcf433692008-04-28 02:14:17 -070069static inline u32 read_##reg(void const __iomem *p) \
70{ return __raw_readl(p + (off)); } \
71\
72static inline void write_##reg(u32 v, void __iomem *p) \
73{ __raw_writel(v, p + (off)); }
Stephen Streete0c99052006-03-07 23:53:24 -080074
75DEFINE_SSP_REG(SSCR0, 0x00)
76DEFINE_SSP_REG(SSCR1, 0x04)
77DEFINE_SSP_REG(SSSR, 0x08)
78DEFINE_SSP_REG(SSITR, 0x0c)
79DEFINE_SSP_REG(SSDR, 0x10)
80DEFINE_SSP_REG(SSTO, 0x28)
81DEFINE_SSP_REG(SSPSP, 0x2c)
82
83#define START_STATE ((void*)0)
84#define RUNNING_STATE ((void*)1)
85#define DONE_STATE ((void*)2)
86#define ERROR_STATE ((void*)-1)
87
88#define QUEUE_RUNNING 0
89#define QUEUE_STOPPED 1
90
91struct driver_data {
92 /* Driver model hookup */
93 struct platform_device *pdev;
94
eric miao2f1a74e2007-11-21 18:50:53 +080095 /* SSP Info */
96 struct ssp_device *ssp;
97
Stephen Streete0c99052006-03-07 23:53:24 -080098 /* SPI framework hookup */
99 enum pxa_ssp_type ssp_type;
100 struct spi_master *master;
101
102 /* PXA hookup */
103 struct pxa2xx_spi_master *master_info;
104
105 /* DMA setup stuff */
106 int rx_channel;
107 int tx_channel;
108 u32 *null_dma_buf;
109
110 /* SSP register addresses */
David Brownellcf433692008-04-28 02:14:17 -0700111 void __iomem *ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800112 u32 ssdr_physical;
113
114 /* SSP masks*/
115 u32 dma_cr1;
116 u32 int_cr1;
117 u32 clear_sr;
118 u32 mask_sr;
119
120 /* Driver message queue */
121 struct workqueue_struct *workqueue;
122 struct work_struct pump_messages;
123 spinlock_t lock;
124 struct list_head queue;
125 int busy;
126 int run;
127
128 /* Message Transfer pump */
129 struct tasklet_struct pump_transfers;
130
131 /* Current message transfer state info */
132 struct spi_message* cur_msg;
133 struct spi_transfer* cur_transfer;
134 struct chip_data *cur_chip;
135 size_t len;
136 void *tx;
137 void *tx_end;
138 void *rx;
139 void *rx_end;
140 int dma_mapped;
141 dma_addr_t rx_dma;
142 dma_addr_t tx_dma;
143 size_t rx_map_len;
144 size_t tx_map_len;
Stephen Street9708c122006-03-28 14:05:23 -0800145 u8 n_bytes;
146 u32 dma_width;
Stephen Street8d94cc52006-12-10 02:18:54 -0800147 int (*write)(struct driver_data *drv_data);
148 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800149 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
150 void (*cs_control)(u32 command);
151};
152
153struct chip_data {
154 u32 cr0;
155 u32 cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800156 u32 psp;
157 u32 timeout;
158 u8 n_bytes;
159 u32 dma_width;
160 u32 dma_burst_size;
161 u32 threshold;
162 u32 dma_threshold;
163 u8 enable_dma;
Stephen Street9708c122006-03-28 14:05:23 -0800164 u8 bits_per_word;
165 u32 speed_hz;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800166 union {
167 int gpio_cs;
168 unsigned int frm;
169 };
Eric Miaoa7bb3902009-04-06 19:00:54 -0700170 int gpio_cs_inverted;
Stephen Street8d94cc52006-12-10 02:18:54 -0800171 int (*write)(struct driver_data *drv_data);
172 int (*read)(struct driver_data *drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800173 void (*cs_control)(u32 command);
174};
175
David Howells6d5aefb2006-12-05 19:36:26 +0000176static void pump_messages(struct work_struct *work);
Stephen Streete0c99052006-03-07 23:53:24 -0800177
Eric Miaoa7bb3902009-04-06 19:00:54 -0700178static void cs_assert(struct driver_data *drv_data)
179{
180 struct chip_data *chip = drv_data->cur_chip;
181
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800182 if (drv_data->ssp_type == CE4100_SSP) {
183 write_SSSR(drv_data->cur_chip->frm, drv_data->ioaddr);
184 return;
185 }
186
Eric Miaoa7bb3902009-04-06 19:00:54 -0700187 if (chip->cs_control) {
188 chip->cs_control(PXA2XX_CS_ASSERT);
189 return;
190 }
191
192 if (gpio_is_valid(chip->gpio_cs))
193 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
194}
195
196static void cs_deassert(struct driver_data *drv_data)
197{
198 struct chip_data *chip = drv_data->cur_chip;
199
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800200 if (drv_data->ssp_type == CE4100_SSP)
201 return;
202
Eric Miaoa7bb3902009-04-06 19:00:54 -0700203 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300204 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700205 return;
206 }
207
208 if (gpio_is_valid(chip->gpio_cs))
209 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
210}
211
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800212static void write_SSSR_CS(struct driver_data *drv_data, u32 val)
213{
214 void __iomem *reg = drv_data->ioaddr;
215
216 if (drv_data->ssp_type == CE4100_SSP)
217 val |= read_SSSR(reg) & SSSR_ALT_FRM_MASK;
218
219 write_SSSR(val, reg);
220}
221
222static int pxa25x_ssp_comp(struct driver_data *drv_data)
223{
224 if (drv_data->ssp_type == PXA25x_SSP)
225 return 1;
226 if (drv_data->ssp_type == CE4100_SSP)
227 return 1;
228 return 0;
229}
230
Stephen Streete0c99052006-03-07 23:53:24 -0800231static int flush(struct driver_data *drv_data)
232{
233 unsigned long limit = loops_per_jiffy << 1;
234
David Brownellcf433692008-04-28 02:14:17 -0700235 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800236
237 do {
238 while (read_SSSR(reg) & SSSR_RNE) {
239 read_SSDR(reg);
240 }
Roel Kluin306c68a2009-04-21 12:24:46 -0700241 } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800242 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800243
244 return limit;
245}
246
Stephen Street8d94cc52006-12-10 02:18:54 -0800247static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800248{
David Brownellcf433692008-04-28 02:14:17 -0700249 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800250 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800251
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800252 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800253 || (drv_data->tx == drv_data->tx_end))
254 return 0;
255
256 write_SSDR(0, reg);
257 drv_data->tx += n_bytes;
258
259 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800260}
261
Stephen Street8d94cc52006-12-10 02:18:54 -0800262static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800263{
David Brownellcf433692008-04-28 02:14:17 -0700264 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800265 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800266
267 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800268 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800269 read_SSDR(reg);
270 drv_data->rx += n_bytes;
271 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800272
273 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800274}
275
Stephen Street8d94cc52006-12-10 02:18:54 -0800276static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800277{
David Brownellcf433692008-04-28 02:14:17 -0700278 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800279
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800280 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800281 || (drv_data->tx == drv_data->tx_end))
282 return 0;
283
284 write_SSDR(*(u8 *)(drv_data->tx), reg);
285 ++drv_data->tx;
286
287 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800288}
289
Stephen Street8d94cc52006-12-10 02:18:54 -0800290static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800291{
David Brownellcf433692008-04-28 02:14:17 -0700292 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800293
294 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800295 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800296 *(u8 *)(drv_data->rx) = read_SSDR(reg);
297 ++drv_data->rx;
298 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800299
300 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800301}
302
Stephen Street8d94cc52006-12-10 02:18:54 -0800303static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800304{
David Brownellcf433692008-04-28 02:14:17 -0700305 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800306
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800307 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800308 || (drv_data->tx == drv_data->tx_end))
309 return 0;
310
311 write_SSDR(*(u16 *)(drv_data->tx), reg);
312 drv_data->tx += 2;
313
314 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800315}
316
Stephen Street8d94cc52006-12-10 02:18:54 -0800317static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800318{
David Brownellcf433692008-04-28 02:14:17 -0700319 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800320
321 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800322 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800323 *(u16 *)(drv_data->rx) = read_SSDR(reg);
324 drv_data->rx += 2;
325 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800326
327 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800328}
Stephen Street8d94cc52006-12-10 02:18:54 -0800329
330static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800331{
David Brownellcf433692008-04-28 02:14:17 -0700332 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800333
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800334 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800335 || (drv_data->tx == drv_data->tx_end))
336 return 0;
337
338 write_SSDR(*(u32 *)(drv_data->tx), reg);
339 drv_data->tx += 4;
340
341 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800342}
343
Stephen Street8d94cc52006-12-10 02:18:54 -0800344static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800345{
David Brownellcf433692008-04-28 02:14:17 -0700346 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800347
348 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800349 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800350 *(u32 *)(drv_data->rx) = read_SSDR(reg);
351 drv_data->rx += 4;
352 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800353
354 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800355}
356
357static void *next_transfer(struct driver_data *drv_data)
358{
359 struct spi_message *msg = drv_data->cur_msg;
360 struct spi_transfer *trans = drv_data->cur_transfer;
361
362 /* Move to next transfer */
363 if (trans->transfer_list.next != &msg->transfers) {
364 drv_data->cur_transfer =
365 list_entry(trans->transfer_list.next,
366 struct spi_transfer,
367 transfer_list);
368 return RUNNING_STATE;
369 } else
370 return DONE_STATE;
371}
372
373static int map_dma_buffers(struct driver_data *drv_data)
374{
375 struct spi_message *msg = drv_data->cur_msg;
376 struct device *dev = &msg->spi->dev;
377
378 if (!drv_data->cur_chip->enable_dma)
379 return 0;
380
381 if (msg->is_dma_mapped)
382 return drv_data->rx_dma && drv_data->tx_dma;
383
384 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
385 return 0;
386
387 /* Modify setup if rx buffer is null */
388 if (drv_data->rx == NULL) {
389 *drv_data->null_dma_buf = 0;
390 drv_data->rx = drv_data->null_dma_buf;
391 drv_data->rx_map_len = 4;
392 } else
393 drv_data->rx_map_len = drv_data->len;
394
395
396 /* Modify setup if tx buffer is null */
397 if (drv_data->tx == NULL) {
398 *drv_data->null_dma_buf = 0;
399 drv_data->tx = drv_data->null_dma_buf;
400 drv_data->tx_map_len = 4;
401 } else
402 drv_data->tx_map_len = drv_data->len;
403
Ned Forrester393df742008-11-19 15:36:21 -0800404 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
405 * so we flush the cache *before* invalidating it, in case
406 * the tx and rx buffers overlap.
407 */
408 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
409 drv_data->tx_map_len, DMA_TO_DEVICE);
410 if (dma_mapping_error(dev, drv_data->tx_dma))
Stephen Streete0c99052006-03-07 23:53:24 -0800411 return 0;
412
Ned Forrester393df742008-11-19 15:36:21 -0800413 /* Stream map the rx buffer */
414 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
Stephen Streete0c99052006-03-07 23:53:24 -0800415 drv_data->rx_map_len, DMA_FROM_DEVICE);
Ned Forrester393df742008-11-19 15:36:21 -0800416 if (dma_mapping_error(dev, drv_data->rx_dma)) {
417 dma_unmap_single(dev, drv_data->tx_dma,
418 drv_data->tx_map_len, DMA_TO_DEVICE);
Stephen Streete0c99052006-03-07 23:53:24 -0800419 return 0;
420 }
421
422 return 1;
423}
424
425static void unmap_dma_buffers(struct driver_data *drv_data)
426{
427 struct device *dev;
428
429 if (!drv_data->dma_mapped)
430 return;
431
432 if (!drv_data->cur_msg->is_dma_mapped) {
433 dev = &drv_data->cur_msg->spi->dev;
434 dma_unmap_single(dev, drv_data->rx_dma,
435 drv_data->rx_map_len, DMA_FROM_DEVICE);
436 dma_unmap_single(dev, drv_data->tx_dma,
437 drv_data->tx_map_len, DMA_TO_DEVICE);
438 }
439
440 drv_data->dma_mapped = 0;
441}
442
443/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700444static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800445{
446 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700447 unsigned long flags;
448 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800449
Stephen Street5daa3ba2006-05-20 15:00:19 -0700450 spin_lock_irqsave(&drv_data->lock, flags);
451 msg = drv_data->cur_msg;
452 drv_data->cur_msg = NULL;
453 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700454 queue_work(drv_data->workqueue, &drv_data->pump_messages);
455 spin_unlock_irqrestore(&drv_data->lock, flags);
456
457 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800458 struct spi_transfer,
459 transfer_list);
460
Ned Forrester84235972008-09-13 02:33:17 -0700461 /* Delay if requested before any change in chip select */
462 if (last_transfer->delay_usecs)
463 udelay(last_transfer->delay_usecs);
464
465 /* Drop chip select UNLESS cs_change is true or we are returning
466 * a message with an error, or next message is for another chip
467 */
Stephen Streete0c99052006-03-07 23:53:24 -0800468 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700469 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700470 else {
471 struct spi_message *next_msg;
472
473 /* Holding of cs was hinted, but we need to make sure
474 * the next message is for the same chip. Don't waste
475 * time with the following tests unless this was hinted.
476 *
477 * We cannot postpone this until pump_messages, because
478 * after calling msg->complete (below) the driver that
479 * sent the current message could be unloaded, which
480 * could invalidate the cs_control() callback...
481 */
482
483 /* get a pointer to the next message, if any */
484 spin_lock_irqsave(&drv_data->lock, flags);
485 if (list_empty(&drv_data->queue))
486 next_msg = NULL;
487 else
488 next_msg = list_entry(drv_data->queue.next,
489 struct spi_message, queue);
490 spin_unlock_irqrestore(&drv_data->lock, flags);
491
492 /* see if the next and current messages point
493 * to the same chip
494 */
495 if (next_msg && next_msg->spi != msg->spi)
496 next_msg = NULL;
497 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700498 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700499 }
Stephen Streete0c99052006-03-07 23:53:24 -0800500
Stephen Street5daa3ba2006-05-20 15:00:19 -0700501 msg->state = NULL;
502 if (msg->complete)
503 msg->complete(msg->context);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700504
505 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800506}
507
David Brownellcf433692008-04-28 02:14:17 -0700508static int wait_ssp_rx_stall(void const __iomem *ioaddr)
Stephen Streete0c99052006-03-07 23:53:24 -0800509{
510 unsigned long limit = loops_per_jiffy << 1;
511
Roel Kluin306c68a2009-04-21 12:24:46 -0700512 while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800513 cpu_relax();
514
515 return limit;
516}
517
518static int wait_dma_channel_stop(int channel)
519{
520 unsigned long limit = loops_per_jiffy << 1;
521
Roel Kluin306c68a2009-04-21 12:24:46 -0700522 while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
Stephen Streete0c99052006-03-07 23:53:24 -0800523 cpu_relax();
524
525 return limit;
526}
527
David Brownellcf433692008-04-28 02:14:17 -0700528static void dma_error_stop(struct driver_data *drv_data, const char *msg)
Stephen Street8d94cc52006-12-10 02:18:54 -0800529{
David Brownellcf433692008-04-28 02:14:17 -0700530 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800531
532 /* Stop and reset */
533 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
534 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800535 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800536 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800537 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800538 write_SSTO(0, reg);
539 flush(drv_data);
540 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
541
542 unmap_dma_buffers(drv_data);
543
544 dev_err(&drv_data->pdev->dev, "%s\n", msg);
545
546 drv_data->cur_msg->state = ERROR_STATE;
547 tasklet_schedule(&drv_data->pump_transfers);
548}
549
550static void dma_transfer_complete(struct driver_data *drv_data)
551{
David Brownellcf433692008-04-28 02:14:17 -0700552 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800553 struct spi_message *msg = drv_data->cur_msg;
554
555 /* Clear and disable interrupts on SSP and DMA channels*/
556 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800557 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800558 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
559 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
560
561 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
562 dev_err(&drv_data->pdev->dev,
563 "dma_handler: dma rx channel stop failed\n");
564
565 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
566 dev_err(&drv_data->pdev->dev,
567 "dma_transfer: ssp rx stall failed\n");
568
569 unmap_dma_buffers(drv_data);
570
571 /* update the buffer pointer for the amount completed in dma */
572 drv_data->rx += drv_data->len -
573 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
574
575 /* read trailing data from fifo, it does not matter how many
576 * bytes are in the fifo just read until buffer is full
577 * or fifo is empty, which ever occurs first */
578 drv_data->read(drv_data);
579
580 /* return count of what was actually read */
581 msg->actual_length += drv_data->len -
582 (drv_data->rx_end - drv_data->rx);
583
Ned Forrester84235972008-09-13 02:33:17 -0700584 /* Transfer delays and chip select release are
585 * handled in pump_transfers or giveback
586 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800587
588 /* Move to next transfer */
589 msg->state = next_transfer(drv_data);
590
591 /* Schedule transfer tasklet */
592 tasklet_schedule(&drv_data->pump_transfers);
593}
594
David Howells7d12e782006-10-05 14:55:46 +0100595static void dma_handler(int channel, void *data)
Stephen Streete0c99052006-03-07 23:53:24 -0800596{
597 struct driver_data *drv_data = data;
Stephen Streete0c99052006-03-07 23:53:24 -0800598 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
Stephen Streete0c99052006-03-07 23:53:24 -0800599
600 if (irq_status & DCSR_BUSERR) {
601
Stephen Streete0c99052006-03-07 23:53:24 -0800602 if (channel == drv_data->tx_channel)
Stephen Street8d94cc52006-12-10 02:18:54 -0800603 dma_error_stop(drv_data,
604 "dma_handler: "
605 "bad bus address on tx channel");
Stephen Streete0c99052006-03-07 23:53:24 -0800606 else
Stephen Street8d94cc52006-12-10 02:18:54 -0800607 dma_error_stop(drv_data,
608 "dma_handler: "
609 "bad bus address on rx channel");
610 return;
Stephen Streete0c99052006-03-07 23:53:24 -0800611 }
612
613 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
Stephen Street8d94cc52006-12-10 02:18:54 -0800614 if ((channel == drv_data->tx_channel)
615 && (irq_status & DCSR_ENDINTR)
616 && (drv_data->ssp_type == PXA25x_SSP)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800617
618 /* Wait for rx to stall */
619 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
620 dev_err(&drv_data->pdev->dev,
621 "dma_handler: ssp rx stall failed\n");
622
Stephen Street8d94cc52006-12-10 02:18:54 -0800623 /* finish this transfer, start the next */
624 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800625 }
626}
627
628static irqreturn_t dma_transfer(struct driver_data *drv_data)
629{
630 u32 irq_status;
David Brownellcf433692008-04-28 02:14:17 -0700631 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800632
633 irq_status = read_SSSR(reg) & drv_data->mask_sr;
634 if (irq_status & SSSR_ROR) {
Stephen Street8d94cc52006-12-10 02:18:54 -0800635 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
Stephen Streete0c99052006-03-07 23:53:24 -0800636 return IRQ_HANDLED;
637 }
638
639 /* Check for false positive timeout */
Stephen Street8d94cc52006-12-10 02:18:54 -0800640 if ((irq_status & SSSR_TINT)
641 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800642 write_SSSR(SSSR_TINT, reg);
643 return IRQ_HANDLED;
644 }
645
646 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
647
Stephen Street8d94cc52006-12-10 02:18:54 -0800648 /* Clear and disable timeout interrupt, do the rest in
649 * dma_transfer_complete */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800650 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -0800651 write_SSTO(0, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800652
Stephen Street8d94cc52006-12-10 02:18:54 -0800653 /* finish this transfer, start the next */
654 dma_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800655
656 return IRQ_HANDLED;
657 }
658
659 /* Opps problem detected */
660 return IRQ_NONE;
661}
662
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800663static void reset_sccr1(struct driver_data *drv_data)
664{
665 void __iomem *reg = drv_data->ioaddr;
666 struct chip_data *chip = drv_data->cur_chip;
667 u32 sccr1_reg;
668
669 sccr1_reg = read_SSCR1(reg) & ~drv_data->int_cr1;
670 sccr1_reg &= ~SSCR1_RFT;
671 sccr1_reg |= chip->threshold;
672 write_SSCR1(sccr1_reg, reg);
673}
674
Stephen Street8d94cc52006-12-10 02:18:54 -0800675static void int_error_stop(struct driver_data *drv_data, const char* msg)
676{
David Brownellcf433692008-04-28 02:14:17 -0700677 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800678
679 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800680 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800681 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800682 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800683 write_SSTO(0, reg);
684 flush(drv_data);
685 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
686
687 dev_err(&drv_data->pdev->dev, "%s\n", msg);
688
689 drv_data->cur_msg->state = ERROR_STATE;
690 tasklet_schedule(&drv_data->pump_transfers);
691}
692
693static void int_transfer_complete(struct driver_data *drv_data)
694{
David Brownellcf433692008-04-28 02:14:17 -0700695 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800696
697 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800698 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800699 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800700 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800701 write_SSTO(0, reg);
702
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300703 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800704 drv_data->cur_msg->actual_length += drv_data->len -
705 (drv_data->rx_end - drv_data->rx);
706
Ned Forrester84235972008-09-13 02:33:17 -0700707 /* Transfer delays and chip select release are
708 * handled in pump_transfers or giveback
709 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800710
711 /* Move to next transfer */
712 drv_data->cur_msg->state = next_transfer(drv_data);
713
714 /* Schedule transfer tasklet */
715 tasklet_schedule(&drv_data->pump_transfers);
716}
717
Stephen Streete0c99052006-03-07 23:53:24 -0800718static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
719{
David Brownellcf433692008-04-28 02:14:17 -0700720 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800721
Stephen Street5daa3ba2006-05-20 15:00:19 -0700722 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
723 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800724
Stephen Street8d94cc52006-12-10 02:18:54 -0800725 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800726
Stephen Street8d94cc52006-12-10 02:18:54 -0800727 if (irq_status & SSSR_ROR) {
728 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
729 return IRQ_HANDLED;
730 }
Stephen Streete0c99052006-03-07 23:53:24 -0800731
Stephen Street8d94cc52006-12-10 02:18:54 -0800732 if (irq_status & SSSR_TINT) {
733 write_SSSR(SSSR_TINT, reg);
734 if (drv_data->read(drv_data)) {
735 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800736 return IRQ_HANDLED;
737 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800738 }
Stephen Streete0c99052006-03-07 23:53:24 -0800739
Stephen Street8d94cc52006-12-10 02:18:54 -0800740 /* Drain rx fifo, Fill tx fifo and prevent overruns */
741 do {
742 if (drv_data->read(drv_data)) {
743 int_transfer_complete(drv_data);
744 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800745 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800746 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800747
Stephen Street8d94cc52006-12-10 02:18:54 -0800748 if (drv_data->read(drv_data)) {
749 int_transfer_complete(drv_data);
750 return IRQ_HANDLED;
751 }
Stephen Streete0c99052006-03-07 23:53:24 -0800752
Stephen Street8d94cc52006-12-10 02:18:54 -0800753 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800754 u32 bytes_left;
755 u32 sccr1_reg;
756
757 sccr1_reg = read_SSCR1(reg);
758 sccr1_reg &= ~SSCR1_TIE;
759
760 /*
761 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300762 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800763 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800764 if (pxa25x_ssp_comp(drv_data)) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800765
766 sccr1_reg &= ~SSCR1_RFT;
767
768 bytes_left = drv_data->rx_end - drv_data->rx;
769 switch (drv_data->n_bytes) {
770 case 4:
771 bytes_left >>= 1;
772 case 2:
773 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800774 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800775
776 if (bytes_left > RX_THRESH_DFLT)
777 bytes_left = RX_THRESH_DFLT;
778
779 sccr1_reg |= SSCR1_RxTresh(bytes_left);
Stephen Streete0c99052006-03-07 23:53:24 -0800780 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800781 write_SSCR1(sccr1_reg, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800782 }
783
Stephen Street5daa3ba2006-05-20 15:00:19 -0700784 /* We did something */
785 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800786}
787
David Howells7d12e782006-10-05 14:55:46 +0100788static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800789{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400790 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700791 void __iomem *reg = drv_data->ioaddr;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800792 u32 sccr1_reg = read_SSCR1(reg);
793 u32 mask = drv_data->mask_sr;
794 u32 status;
795
796 status = read_SSSR(reg);
797
798 /* Ignore possible writes if we don't need to write */
799 if (!(sccr1_reg & SSCR1_TIE))
800 mask &= ~SSSR_TFS;
801
Tan, Jui Neeafd711bb2015-09-01 10:22:51 +0800802 /* Ignore RX timeout interrupt if it is disabled */
803 if (!(sccr1_reg & SSCR1_TINTE))
804 mask &= ~SSSR_TINT;
805
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800806 if (!(status & mask))
807 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800808
809 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700810
811 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
812 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800813 if (!pxa25x_ssp_comp(drv_data))
Stephen Street5daa3ba2006-05-20 15:00:19 -0700814 write_SSTO(0, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800815 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700816
Stephen Streete0c99052006-03-07 23:53:24 -0800817 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800818 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700819
Stephen Streete0c99052006-03-07 23:53:24 -0800820 /* Never fail */
821 return IRQ_HANDLED;
822 }
823
824 return drv_data->transfer_handler(drv_data);
825}
826
David Brownellcf433692008-04-28 02:14:17 -0700827static int set_dma_burst_and_threshold(struct chip_data *chip,
828 struct spi_device *spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800829 u8 bits_per_word, u32 *burst_code,
830 u32 *threshold)
831{
832 struct pxa2xx_spi_chip *chip_info =
833 (struct pxa2xx_spi_chip *)spi->controller_data;
834 int bytes_per_word;
835 int burst_bytes;
836 int thresh_words;
837 int req_burst_size;
838 int retval = 0;
839
840 /* Set the threshold (in registers) to equal the same amount of data
841 * as represented by burst size (in bytes). The computation below
842 * is (burst_size rounded up to nearest 8 byte, word or long word)
843 * divided by (bytes/register); the tx threshold is the inverse of
844 * the rx, so that there will always be enough data in the rx fifo
845 * to satisfy a burst, and there will always be enough space in the
846 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
847 * there is not enough space), there must always remain enough empty
848 * space in the rx fifo for any data loaded to the tx fifo.
849 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
850 * will be 8, or half the fifo;
851 * The threshold can only be set to 2, 4 or 8, but not 16, because
852 * to burst 16 to the tx fifo, the fifo would have to be empty;
853 * however, the minimum fifo trigger level is 1, and the tx will
854 * request service when the fifo is at this level, with only 15 spaces.
855 */
856
857 /* find bytes/word */
858 if (bits_per_word <= 8)
859 bytes_per_word = 1;
860 else if (bits_per_word <= 16)
861 bytes_per_word = 2;
862 else
863 bytes_per_word = 4;
864
865 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
866 if (chip_info)
867 req_burst_size = chip_info->dma_burst_size;
868 else {
869 switch (chip->dma_burst_size) {
870 default:
871 /* if the default burst size is not set,
872 * do it now */
873 chip->dma_burst_size = DCMD_BURST8;
874 case DCMD_BURST8:
875 req_burst_size = 8;
876 break;
877 case DCMD_BURST16:
878 req_burst_size = 16;
879 break;
880 case DCMD_BURST32:
881 req_burst_size = 32;
882 break;
883 }
884 }
885 if (req_burst_size <= 8) {
886 *burst_code = DCMD_BURST8;
887 burst_bytes = 8;
888 } else if (req_burst_size <= 16) {
889 if (bytes_per_word == 1) {
890 /* don't burst more than 1/2 the fifo */
891 *burst_code = DCMD_BURST8;
892 burst_bytes = 8;
893 retval = 1;
894 } else {
895 *burst_code = DCMD_BURST16;
896 burst_bytes = 16;
897 }
898 } else {
899 if (bytes_per_word == 1) {
900 /* don't burst more than 1/2 the fifo */
901 *burst_code = DCMD_BURST8;
902 burst_bytes = 8;
903 retval = 1;
904 } else if (bytes_per_word == 2) {
905 /* don't burst more than 1/2 the fifo */
906 *burst_code = DCMD_BURST16;
907 burst_bytes = 16;
908 retval = 1;
909 } else {
910 *burst_code = DCMD_BURST32;
911 burst_bytes = 32;
912 }
913 }
914
915 thresh_words = burst_bytes / bytes_per_word;
916
917 /* thresh_words will be between 2 and 8 */
918 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
919 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
920
921 return retval;
922}
923
eric miao2f1a74e2007-11-21 18:50:53 +0800924static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
925{
926 unsigned long ssp_clk = clk_get_rate(ssp->clk);
927
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800928 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
eric miao2f1a74e2007-11-21 18:50:53 +0800929 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
930 else
931 return ((ssp_clk / rate - 1) & 0xfff) << 8;
932}
933
Stephen Streete0c99052006-03-07 23:53:24 -0800934static void pump_transfers(unsigned long data)
935{
936 struct driver_data *drv_data = (struct driver_data *)data;
937 struct spi_message *message = NULL;
938 struct spi_transfer *transfer = NULL;
939 struct spi_transfer *previous = NULL;
940 struct chip_data *chip = NULL;
eric miao2f1a74e2007-11-21 18:50:53 +0800941 struct ssp_device *ssp = drv_data->ssp;
David Brownellcf433692008-04-28 02:14:17 -0700942 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800943 u32 clk_div = 0;
944 u8 bits = 0;
945 u32 speed = 0;
946 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800947 u32 cr1;
948 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
949 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800950
951 /* Get current state information */
952 message = drv_data->cur_msg;
953 transfer = drv_data->cur_transfer;
954 chip = drv_data->cur_chip;
955
956 /* Handle for abort */
957 if (message->state == ERROR_STATE) {
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 }
962
963 /* Handle end of message */
964 if (message->state == DONE_STATE) {
965 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700966 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800967 return;
968 }
969
Ned Forrester84235972008-09-13 02:33:17 -0700970 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800971 if (message->state == RUNNING_STATE) {
972 previous = list_entry(transfer->transfer_list.prev,
973 struct spi_transfer,
974 transfer_list);
975 if (previous->delay_usecs)
976 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700977
978 /* Drop chip select only if cs_change is requested */
979 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700980 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800981 }
982
Ned Forrester7e964452008-09-13 02:33:18 -0700983 /* Check for transfers that need multiple DMA segments */
984 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
985
986 /* reject already-mapped transfers; PIO won't always work */
987 if (message->is_dma_mapped
988 || transfer->rx_dma || transfer->tx_dma) {
989 dev_err(&drv_data->pdev->dev,
990 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700991 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700992 transfer->len, MAX_DMA_LEN);
993 message->status = -EINVAL;
994 giveback(drv_data);
995 return;
996 }
997
998 /* warn ... we force this to PIO mode */
999 if (printk_ratelimit())
1000 dev_warn(&message->spi->dev, "pump_transfers: "
1001 "DMA disabled for transfer length %ld "
1002 "greater than %d\n",
1003 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -08001004 }
1005
Stephen Streete0c99052006-03-07 23:53:24 -08001006 /* Setup the transfer state based on the type of transfer */
1007 if (flush(drv_data) == 0) {
1008 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
1009 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -07001010 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001011 return;
1012 }
Stephen Street9708c122006-03-28 14:05:23 -08001013 drv_data->n_bytes = chip->n_bytes;
1014 drv_data->dma_width = chip->dma_width;
Stephen Streete0c99052006-03-07 23:53:24 -08001015 drv_data->tx = (void *)transfer->tx_buf;
1016 drv_data->tx_end = drv_data->tx + transfer->len;
1017 drv_data->rx = transfer->rx_buf;
1018 drv_data->rx_end = drv_data->rx + transfer->len;
1019 drv_data->rx_dma = transfer->rx_dma;
1020 drv_data->tx_dma = transfer->tx_dma;
Stephen Street8d94cc52006-12-10 02:18:54 -08001021 drv_data->len = transfer->len & DCMD_LENGTH;
Stephen Streete0c99052006-03-07 23:53:24 -08001022 drv_data->write = drv_data->tx ? chip->write : null_writer;
1023 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -08001024
1025 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -08001026 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -08001027 if (transfer->speed_hz || transfer->bits_per_word) {
1028
Stephen Street9708c122006-03-28 14:05:23 -08001029 bits = chip->bits_per_word;
1030 speed = chip->speed_hz;
1031
1032 if (transfer->speed_hz)
1033 speed = transfer->speed_hz;
1034
1035 if (transfer->bits_per_word)
1036 bits = transfer->bits_per_word;
1037
eric miao2f1a74e2007-11-21 18:50:53 +08001038 clk_div = ssp_get_clk_div(ssp, speed);
Stephen Street9708c122006-03-28 14:05:23 -08001039
1040 if (bits <= 8) {
1041 drv_data->n_bytes = 1;
1042 drv_data->dma_width = DCMD_WIDTH1;
1043 drv_data->read = drv_data->read != null_reader ?
1044 u8_reader : null_reader;
1045 drv_data->write = drv_data->write != null_writer ?
1046 u8_writer : null_writer;
1047 } else if (bits <= 16) {
1048 drv_data->n_bytes = 2;
1049 drv_data->dma_width = DCMD_WIDTH2;
1050 drv_data->read = drv_data->read != null_reader ?
1051 u16_reader : null_reader;
1052 drv_data->write = drv_data->write != null_writer ?
1053 u16_writer : null_writer;
1054 } else if (bits <= 32) {
1055 drv_data->n_bytes = 4;
1056 drv_data->dma_width = DCMD_WIDTH4;
1057 drv_data->read = drv_data->read != null_reader ?
1058 u32_reader : null_reader;
1059 drv_data->write = drv_data->write != null_writer ?
1060 u32_writer : null_writer;
1061 }
Stephen Street8d94cc52006-12-10 02:18:54 -08001062 /* if bits/word is changed in dma mode, then must check the
1063 * thresholds and burst also */
1064 if (chip->enable_dma) {
1065 if (set_dma_burst_and_threshold(chip, message->spi,
1066 bits, &dma_burst,
1067 &dma_thresh))
1068 if (printk_ratelimit())
1069 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -07001070 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -08001071 "DMA burst size reduced to "
1072 "match bits_per_word\n");
1073 }
Stephen Street9708c122006-03-28 14:05:23 -08001074
1075 cr0 = clk_div
1076 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001077 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -08001078 | SSCR0_SSE
1079 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -08001080 }
1081
Stephen Streete0c99052006-03-07 23:53:24 -08001082 message->state = RUNNING_STATE;
1083
Ned Forrester7e964452008-09-13 02:33:18 -07001084 /* Try to map dma buffer and do a dma transfer if successful, but
1085 * only if the length is non-zero and less than MAX_DMA_LEN.
1086 *
1087 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
1088 * of PIO instead. Care is needed above because the transfer may
1089 * have have been passed with buffers that are already dma mapped.
1090 * A zero-length transfer in PIO mode will not try to write/read
1091 * to/from the buffers
1092 *
1093 * REVISIT large transfers are exactly where we most want to be
1094 * using DMA. If this happens much, split those transfers into
1095 * multiple DMA segments rather than forcing PIO.
1096 */
1097 drv_data->dma_mapped = 0;
1098 if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN)
1099 drv_data->dma_mapped = map_dma_buffers(drv_data);
1100 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -08001101
1102 /* Ensure we have the correct interrupt handler */
1103 drv_data->transfer_handler = dma_transfer;
1104
1105 /* Setup rx DMA Channel */
1106 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
1107 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
1108 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
1109 if (drv_data->rx == drv_data->null_dma_buf)
1110 /* No target address increment */
1111 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001112 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001113 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001114 | drv_data->len;
1115 else
1116 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
1117 | DCMD_FLOWSRC
Stephen Street9708c122006-03-28 14:05:23 -08001118 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001119 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001120 | drv_data->len;
1121
1122 /* Setup tx DMA Channel */
1123 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
1124 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
1125 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
1126 if (drv_data->tx == drv_data->null_dma_buf)
1127 /* No source address increment */
1128 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001129 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001130 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001131 | drv_data->len;
1132 else
1133 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
1134 | DCMD_FLOWTRG
Stephen Street9708c122006-03-28 14:05:23 -08001135 | drv_data->dma_width
Stephen Street8d94cc52006-12-10 02:18:54 -08001136 | dma_burst
Stephen Streete0c99052006-03-07 23:53:24 -08001137 | drv_data->len;
1138
1139 /* Enable dma end irqs on SSP to detect end of transfer */
1140 if (drv_data->ssp_type == PXA25x_SSP)
1141 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
1142
Stephen Street8d94cc52006-12-10 02:18:54 -08001143 /* Clear status and start DMA engine */
1144 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -08001145 write_SSSR(drv_data->clear_sr, reg);
1146 DCSR(drv_data->rx_channel) |= DCSR_RUN;
1147 DCSR(drv_data->tx_channel) |= DCSR_RUN;
Stephen Streete0c99052006-03-07 23:53:24 -08001148 } else {
1149 /* Ensure we have the correct interrupt handler */
1150 drv_data->transfer_handler = interrupt_transfer;
1151
Stephen Street8d94cc52006-12-10 02:18:54 -08001152 /* Clear status */
1153 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001154 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -08001155 }
1156
1157 /* see if we need to reload the config registers */
1158 if ((read_SSCR0(reg) != cr0)
1159 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1160 (cr1 & SSCR1_CHANGE_MASK)) {
1161
Ned Forresterb97c74b2008-02-23 15:23:40 -08001162 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -08001163 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001164 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -08001165 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001166 /* first set CR1 without interrupt and service enables */
1167 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1168 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -08001169 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001170
Stephen Street8d94cc52006-12-10 02:18:54 -08001171 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001172 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -08001173 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001174 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001175
Eric Miaoa7bb3902009-04-06 19:00:54 -07001176 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001177
1178 /* after chip select, release the data by enabling service
1179 * requests and interrupts, without changing any mode bits */
1180 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -08001181}
1182
David Howells6d5aefb2006-12-05 19:36:26 +00001183static void pump_messages(struct work_struct *work)
Stephen Streete0c99052006-03-07 23:53:24 -08001184{
David Howells6d5aefb2006-12-05 19:36:26 +00001185 struct driver_data *drv_data =
1186 container_of(work, struct driver_data, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001187 unsigned long flags;
1188
1189 /* Lock queue and check for queue work */
1190 spin_lock_irqsave(&drv_data->lock, flags);
1191 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1192 drv_data->busy = 0;
1193 spin_unlock_irqrestore(&drv_data->lock, flags);
1194 return;
1195 }
1196
1197 /* Make sure we are not already running a message */
1198 if (drv_data->cur_msg) {
1199 spin_unlock_irqrestore(&drv_data->lock, flags);
1200 return;
1201 }
1202
1203 /* Extract head of queue */
1204 drv_data->cur_msg = list_entry(drv_data->queue.next,
1205 struct spi_message, queue);
1206 list_del_init(&drv_data->cur_msg->queue);
Stephen Streete0c99052006-03-07 23:53:24 -08001207
1208 /* Initial message state*/
1209 drv_data->cur_msg->state = START_STATE;
1210 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1211 struct spi_transfer,
1212 transfer_list);
1213
Stephen Street8d94cc52006-12-10 02:18:54 -08001214 /* prepare to setup the SSP, in pump_transfers, using the per
1215 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001216 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001217
1218 /* Mark as busy and launch transfers */
1219 tasklet_schedule(&drv_data->pump_transfers);
Stephen Street5daa3ba2006-05-20 15:00:19 -07001220
1221 drv_data->busy = 1;
1222 spin_unlock_irqrestore(&drv_data->lock, flags);
Stephen Streete0c99052006-03-07 23:53:24 -08001223}
1224
1225static int transfer(struct spi_device *spi, struct spi_message *msg)
1226{
1227 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1228 unsigned long flags;
1229
1230 spin_lock_irqsave(&drv_data->lock, flags);
1231
1232 if (drv_data->run == QUEUE_STOPPED) {
1233 spin_unlock_irqrestore(&drv_data->lock, flags);
1234 return -ESHUTDOWN;
1235 }
1236
1237 msg->actual_length = 0;
1238 msg->status = -EINPROGRESS;
1239 msg->state = START_STATE;
1240
1241 list_add_tail(&msg->queue, &drv_data->queue);
1242
1243 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1244 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1245
1246 spin_unlock_irqrestore(&drv_data->lock, flags);
1247
1248 return 0;
1249}
1250
Eric Miaoa7bb3902009-04-06 19:00:54 -07001251static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1252 struct pxa2xx_spi_chip *chip_info)
1253{
1254 int err = 0;
1255
1256 if (chip == NULL || chip_info == NULL)
1257 return 0;
1258
1259 /* NOTE: setup() can be called multiple times, possibly with
1260 * different chip_info, release previously requested GPIO
1261 */
1262 if (gpio_is_valid(chip->gpio_cs))
1263 gpio_free(chip->gpio_cs);
1264
1265 /* If (*cs_control) is provided, ignore GPIO chip select */
1266 if (chip_info->cs_control) {
1267 chip->cs_control = chip_info->cs_control;
1268 return 0;
1269 }
1270
1271 if (gpio_is_valid(chip_info->gpio_cs)) {
1272 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1273 if (err) {
1274 dev_err(&spi->dev, "failed to request chip select "
1275 "GPIO%d\n", chip_info->gpio_cs);
1276 return err;
1277 }
1278
1279 chip->gpio_cs = chip_info->gpio_cs;
1280 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1281
1282 err = gpio_direction_output(chip->gpio_cs,
1283 !chip->gpio_cs_inverted);
1284 }
1285
1286 return err;
1287}
1288
Stephen Streete0c99052006-03-07 23:53:24 -08001289static int setup(struct spi_device *spi)
1290{
1291 struct pxa2xx_spi_chip *chip_info = NULL;
1292 struct chip_data *chip;
1293 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
eric miao2f1a74e2007-11-21 18:50:53 +08001294 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001295 unsigned int clk_div;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001296 uint tx_thres = TX_THRESH_DFLT;
1297 uint rx_thres = RX_THRESH_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001298
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001299 if (!pxa25x_ssp_comp(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -08001300 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1301 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1302 "b/w not 4-32 for type non-PXA25x_SSP\n",
1303 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001304 return -EINVAL;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001305 } else if (pxa25x_ssp_comp(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -08001306 && (spi->bits_per_word < 4
1307 || spi->bits_per_word > 16)) {
1308 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1309 "b/w not 4-16 for type PXA25x_SSP\n",
1310 drv_data->ssp_type, spi->bits_per_word);
Stephen Streete0c99052006-03-07 23:53:24 -08001311 return -EINVAL;
Stephen Street8d94cc52006-12-10 02:18:54 -08001312 }
Stephen Streete0c99052006-03-07 23:53:24 -08001313
Stephen Street8d94cc52006-12-10 02:18:54 -08001314 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001315 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001316 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001317 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -08001318 if (!chip) {
1319 dev_err(&spi->dev,
1320 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001321 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -08001322 }
Stephen Streete0c99052006-03-07 23:53:24 -08001323
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001324 if (drv_data->ssp_type == CE4100_SSP) {
1325 if (spi->chip_select > 4) {
1326 dev_err(&spi->dev, "failed setup: "
1327 "cs number must not be > 4.\n");
1328 kfree(chip);
1329 return -EINVAL;
1330 }
1331
1332 chip->frm = spi->chip_select;
1333 } else
1334 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001335 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001336 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001337 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1338 DCMD_BURST8 : 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001339 }
1340
Stephen Street8d94cc52006-12-10 02:18:54 -08001341 /* protocol drivers may change the chip settings, so...
1342 * if chip_info exists, use it */
1343 chip_info = spi->controller_data;
1344
Stephen Streete0c99052006-03-07 23:53:24 -08001345 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001346 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001347 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001348 if (chip_info->timeout)
1349 chip->timeout = chip_info->timeout;
1350 if (chip_info->tx_threshold)
1351 tx_thres = chip_info->tx_threshold;
1352 if (chip_info->rx_threshold)
1353 rx_thres = chip_info->rx_threshold;
1354 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001355 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001356 if (chip_info->enable_loopback)
1357 chip->cr1 = SSCR1_LBM;
1358 }
1359
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001360 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1361 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1362
Stephen Street8d94cc52006-12-10 02:18:54 -08001363 /* set dma burst and threshold outside of chip_info path so that if
1364 * chip_info goes away after setting chip->enable_dma, the
1365 * burst and threshold can still respond to changes in bits_per_word */
1366 if (chip->enable_dma) {
1367 /* set up legal burst and threshold for dma */
1368 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1369 &chip->dma_burst_size,
1370 &chip->dma_threshold)) {
1371 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1372 "to match bits_per_word\n");
1373 }
1374 }
1375
eric miao2f1a74e2007-11-21 18:50:53 +08001376 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001377 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001378
1379 chip->cr0 = clk_div
1380 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -07001381 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1382 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -08001383 | SSCR0_SSE
1384 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001385 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1386 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1387 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001388
1389 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001390 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -07001391 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Eric Miaoc9840da2010-03-16 16:48:01 +08001392 clk_get_rate(ssp->clk)
1393 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
1394 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001395 else
David Brownell7d077192009-06-17 16:26:03 -07001396 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Eric Miaoc9840da2010-03-16 16:48:01 +08001397 clk_get_rate(ssp->clk) / 2
1398 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1399 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001400
1401 if (spi->bits_per_word <= 8) {
1402 chip->n_bytes = 1;
1403 chip->dma_width = DCMD_WIDTH1;
1404 chip->read = u8_reader;
1405 chip->write = u8_writer;
1406 } else if (spi->bits_per_word <= 16) {
1407 chip->n_bytes = 2;
1408 chip->dma_width = DCMD_WIDTH2;
1409 chip->read = u16_reader;
1410 chip->write = u16_writer;
1411 } else if (spi->bits_per_word <= 32) {
1412 chip->cr0 |= SSCR0_EDSS;
1413 chip->n_bytes = 4;
1414 chip->dma_width = DCMD_WIDTH4;
1415 chip->read = u32_reader;
1416 chip->write = u32_writer;
1417 } else {
1418 dev_err(&spi->dev, "invalid wordsize\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001419 return -ENODEV;
1420 }
Stephen Street9708c122006-03-28 14:05:23 -08001421 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001422
1423 spi_set_ctldata(spi, chip);
1424
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001425 if (drv_data->ssp_type == CE4100_SSP)
1426 return 0;
1427
Eric Miaoa7bb3902009-04-06 19:00:54 -07001428 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001429}
1430
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001431static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001432{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001433 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001434 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001435
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001436 if (!chip)
1437 return;
1438
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001439 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001440 gpio_free(chip->gpio_cs);
1441
Stephen Streete0c99052006-03-07 23:53:24 -08001442 kfree(chip);
1443}
1444
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001445static int __devinit init_queue(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -08001446{
1447 INIT_LIST_HEAD(&drv_data->queue);
1448 spin_lock_init(&drv_data->lock);
1449
1450 drv_data->run = QUEUE_STOPPED;
1451 drv_data->busy = 0;
1452
1453 tasklet_init(&drv_data->pump_transfers,
1454 pump_transfers, (unsigned long)drv_data);
1455
David Howells6d5aefb2006-12-05 19:36:26 +00001456 INIT_WORK(&drv_data->pump_messages, pump_messages);
Stephen Streete0c99052006-03-07 23:53:24 -08001457 drv_data->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001458 dev_name(drv_data->master->dev.parent));
Stephen Streete0c99052006-03-07 23:53:24 -08001459 if (drv_data->workqueue == NULL)
1460 return -EBUSY;
1461
1462 return 0;
1463}
1464
1465static int start_queue(struct driver_data *drv_data)
1466{
1467 unsigned long flags;
1468
1469 spin_lock_irqsave(&drv_data->lock, flags);
1470
1471 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1472 spin_unlock_irqrestore(&drv_data->lock, flags);
1473 return -EBUSY;
1474 }
1475
1476 drv_data->run = QUEUE_RUNNING;
1477 drv_data->cur_msg = NULL;
1478 drv_data->cur_transfer = NULL;
1479 drv_data->cur_chip = NULL;
1480 spin_unlock_irqrestore(&drv_data->lock, flags);
1481
1482 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1483
1484 return 0;
1485}
1486
1487static int stop_queue(struct driver_data *drv_data)
1488{
1489 unsigned long flags;
1490 unsigned limit = 500;
1491 int status = 0;
1492
1493 spin_lock_irqsave(&drv_data->lock, flags);
1494
1495 /* This is a bit lame, but is optimized for the common execution path.
1496 * A wait_queue on the drv_data->busy could be used, but then the common
1497 * execution path (pump_messages) would be required to call wake_up or
1498 * friends on every SPI message. Do this instead */
1499 drv_data->run = QUEUE_STOPPED;
Vasily Khoruzhick850a28e2011-04-06 17:49:15 +03001500 while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
Stephen Streete0c99052006-03-07 23:53:24 -08001501 spin_unlock_irqrestore(&drv_data->lock, flags);
1502 msleep(10);
1503 spin_lock_irqsave(&drv_data->lock, flags);
1504 }
1505
1506 if (!list_empty(&drv_data->queue) || drv_data->busy)
1507 status = -EBUSY;
1508
1509 spin_unlock_irqrestore(&drv_data->lock, flags);
1510
1511 return status;
1512}
1513
1514static int destroy_queue(struct driver_data *drv_data)
1515{
1516 int status;
1517
1518 status = stop_queue(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -08001519 /* we are unloading the module or failing to load (only two calls
1520 * to this routine), and neither call can handle a return value.
1521 * However, destroy_workqueue calls flush_workqueue, and that will
1522 * block until all work is done. If the reason that stop_queue
1523 * timed out is that the work will never finish, then it does no
1524 * good to call destroy_workqueue, so return anyway. */
Stephen Streete0c99052006-03-07 23:53:24 -08001525 if (status != 0)
1526 return status;
1527
1528 destroy_workqueue(drv_data->workqueue);
1529
1530 return 0;
1531}
1532
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001533static int __devinit pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001534{
1535 struct device *dev = &pdev->dev;
1536 struct pxa2xx_spi_master *platform_info;
1537 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001538 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001539 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001540 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001541
1542 platform_info = dev->platform_data;
1543
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001544 ssp = pxa_ssp_request(pdev->id, pdev->name);
eric miao2f1a74e2007-11-21 18:50:53 +08001545 if (ssp == NULL) {
1546 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
Stephen Streete0c99052006-03-07 23:53:24 -08001547 return -ENODEV;
1548 }
1549
1550 /* Allocate master with space for drv_data and null dma buffer */
1551 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1552 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001553 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001554 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001555 return -ENOMEM;
1556 }
1557 drv_data = spi_master_get_devdata(master);
1558 drv_data->master = master;
1559 drv_data->master_info = platform_info;
1560 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001561 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001562
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001563 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001564 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001565 /* the spi->mode bits understood by this driver: */
Daniel Ribeiro50e0a7b2009-06-17 16:26:06 -07001566 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
David Brownelle7db06b2009-06-17 16:26:04 -07001567
Stephen Streete0c99052006-03-07 23:53:24 -08001568 master->bus_num = pdev->id;
1569 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001570 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001571 master->cleanup = cleanup;
1572 master->setup = setup;
1573 master->transfer = transfer;
1574
eric miao2f1a74e2007-11-21 18:50:53 +08001575 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001576 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1577 sizeof(struct driver_data)), 8);
1578
eric miao2f1a74e2007-11-21 18:50:53 +08001579 drv_data->ioaddr = ssp->mmio_base;
1580 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001581 if (pxa25x_ssp_comp(drv_data)) {
Stephen Streete0c99052006-03-07 23:53:24 -08001582 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1583 drv_data->dma_cr1 = 0;
1584 drv_data->clear_sr = SSSR_ROR;
1585 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1586 } else {
1587 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1588 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1589 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1590 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1591 }
1592
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001593 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1594 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001595 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001596 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001597 goto out_error_master_alloc;
1598 }
1599
1600 /* Setup DMA if requested */
1601 drv_data->tx_channel = -1;
1602 drv_data->rx_channel = -1;
1603 if (platform_info->enable_dma) {
1604
1605 /* Get two DMA channels (rx and tx) */
1606 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1607 DMA_PRIO_HIGH,
1608 dma_handler,
1609 drv_data);
1610 if (drv_data->rx_channel < 0) {
1611 dev_err(dev, "problem (%d) requesting rx channel\n",
1612 drv_data->rx_channel);
1613 status = -ENODEV;
1614 goto out_error_irq_alloc;
1615 }
1616 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1617 DMA_PRIO_MEDIUM,
1618 dma_handler,
1619 drv_data);
1620 if (drv_data->tx_channel < 0) {
1621 dev_err(dev, "problem (%d) requesting tx channel\n",
1622 drv_data->tx_channel);
1623 status = -ENODEV;
1624 goto out_error_dma_alloc;
1625 }
1626
eric miao2f1a74e2007-11-21 18:50:53 +08001627 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1628 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
Stephen Streete0c99052006-03-07 23:53:24 -08001629 }
1630
1631 /* Enable SOC clock */
eric miao2f1a74e2007-11-21 18:50:53 +08001632 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001633
1634 /* Load default SSP configuration */
1635 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001636 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1637 SSCR1_TxTresh(TX_THRESH_DFLT),
1638 drv_data->ioaddr);
Eric Miaoc9840da2010-03-16 16:48:01 +08001639 write_SSCR0(SSCR0_SCR(2)
Stephen Streete0c99052006-03-07 23:53:24 -08001640 | SSCR0_Motorola
1641 | SSCR0_DataSize(8),
1642 drv_data->ioaddr);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001643 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -08001644 write_SSTO(0, drv_data->ioaddr);
1645 write_SSPSP(0, drv_data->ioaddr);
1646
1647 /* Initial and start queue */
1648 status = init_queue(drv_data);
1649 if (status != 0) {
1650 dev_err(&pdev->dev, "problem initializing queue\n");
1651 goto out_error_clock_enabled;
1652 }
1653 status = start_queue(drv_data);
1654 if (status != 0) {
1655 dev_err(&pdev->dev, "problem starting queue\n");
1656 goto out_error_clock_enabled;
1657 }
1658
1659 /* Register with the SPI framework */
1660 platform_set_drvdata(pdev, drv_data);
1661 status = spi_register_master(master);
1662 if (status != 0) {
1663 dev_err(&pdev->dev, "problem registering spi master\n");
1664 goto out_error_queue_alloc;
1665 }
1666
1667 return status;
1668
1669out_error_queue_alloc:
1670 destroy_queue(drv_data);
1671
1672out_error_clock_enabled:
eric miao2f1a74e2007-11-21 18:50:53 +08001673 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001674
1675out_error_dma_alloc:
1676 if (drv_data->tx_channel != -1)
1677 pxa_free_dma(drv_data->tx_channel);
1678 if (drv_data->rx_channel != -1)
1679 pxa_free_dma(drv_data->rx_channel);
1680
1681out_error_irq_alloc:
eric miao2f1a74e2007-11-21 18:50:53 +08001682 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001683
1684out_error_master_alloc:
1685 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001686 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001687 return status;
1688}
1689
1690static int pxa2xx_spi_remove(struct platform_device *pdev)
1691{
1692 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001693 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001694 int status = 0;
1695
1696 if (!drv_data)
1697 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001698 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001699
1700 /* Remove the queue */
1701 status = destroy_queue(drv_data);
1702 if (status != 0)
Stephen Street8d94cc52006-12-10 02:18:54 -08001703 /* the kernel does not check the return status of this
1704 * this routine (mod->exit, within the kernel). Therefore
1705 * nothing is gained by returning from here, the module is
1706 * going away regardless, and we should not leave any more
1707 * resources allocated than necessary. We cannot free the
1708 * message memory in drv_data->queue, but we can release the
1709 * resources below. I think the kernel should honor -EBUSY
1710 * returns but... */
1711 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1712 "complete, message memory not freed\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001713
1714 /* Disable the SSP at the peripheral and SOC level */
1715 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001716 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001717
1718 /* Release DMA */
1719 if (drv_data->master_info->enable_dma) {
eric miao2f1a74e2007-11-21 18:50:53 +08001720 DRCMR(ssp->drcmr_rx) = 0;
1721 DRCMR(ssp->drcmr_tx) = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001722 pxa_free_dma(drv_data->tx_channel);
1723 pxa_free_dma(drv_data->rx_channel);
1724 }
1725
1726 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001727 free_irq(ssp->irq, drv_data);
1728
1729 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001730 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001731
1732 /* Disconnect from the SPI framework */
1733 spi_unregister_master(drv_data->master);
1734
1735 /* Prevent double remove */
1736 platform_set_drvdata(pdev, NULL);
1737
1738 return 0;
1739}
1740
1741static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1742{
1743 int status = 0;
1744
1745 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1746 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1747}
1748
1749#ifdef CONFIG_PM
Mike Rapoport86d25932009-07-21 17:50:16 +03001750static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001751{
Mike Rapoport86d25932009-07-21 17:50:16 +03001752 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001753 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001754 int status = 0;
1755
Stephen Streete0c99052006-03-07 23:53:24 -08001756 status = stop_queue(drv_data);
1757 if (status != 0)
1758 return status;
1759 write_SSCR0(0, drv_data->ioaddr);
eric miao2f1a74e2007-11-21 18:50:53 +08001760 clk_disable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001761
1762 return 0;
1763}
1764
Mike Rapoport86d25932009-07-21 17:50:16 +03001765static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001766{
Mike Rapoport86d25932009-07-21 17:50:16 +03001767 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001768 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001769 int status = 0;
1770
Daniel Ribeiro148da332009-04-21 12:24:43 -07001771 if (drv_data->rx_channel != -1)
1772 DRCMR(drv_data->ssp->drcmr_rx) =
1773 DRCMR_MAPVLD | drv_data->rx_channel;
1774 if (drv_data->tx_channel != -1)
1775 DRCMR(drv_data->ssp->drcmr_tx) =
1776 DRCMR_MAPVLD | drv_data->tx_channel;
1777
Stephen Streete0c99052006-03-07 23:53:24 -08001778 /* Enable the SSP clock */
Eric BENARD0cf942d2008-05-12 14:02:01 -07001779 clk_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001780
1781 /* Start the queue running */
1782 status = start_queue(drv_data);
1783 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001784 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001785 return status;
1786 }
1787
1788 return 0;
1789}
Mike Rapoport86d25932009-07-21 17:50:16 +03001790
Alexey Dobriyan47145212009-12-14 18:00:08 -08001791static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001792 .suspend = pxa2xx_spi_suspend,
1793 .resume = pxa2xx_spi_resume,
1794};
1795#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001796
1797static struct platform_driver driver = {
1798 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001799 .name = "pxa2xx-spi",
1800 .owner = THIS_MODULE,
1801#ifdef CONFIG_PM
1802 .pm = &pxa2xx_spi_pm_ops,
1803#endif
Stephen Streete0c99052006-03-07 23:53:24 -08001804 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001805 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001806 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001807 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001808};
1809
1810static int __init pxa2xx_spi_init(void)
1811{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001812 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001813}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001814subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001815
1816static void __exit pxa2xx_spi_exit(void)
1817{
1818 platform_driver_unregister(&driver);
1819}
1820module_exit(pxa2xx_spi_exit);