blob: 014af69fc1bbba62ac386a1f147c7e816af14a0b [file] [log] [blame]
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001/*
2 * drivers/spi/spi_imx.c
3 *
4 * Copyright (C) 2006 SWAPP
5 * Andrea Paterniani <a.paterniani@swapp-eng.it>
6 *
7 * Initial version inspired by:
8 * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/device.h>
24#include <linux/ioport.h>
25#include <linux/errno.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/dma-mapping.h>
29#include <linux/spi/spi.h>
30#include <linux/workqueue.h>
31#include <linux/delay.h>
Sascha Hauer38a41fd2008-07-05 10:02:46 +020032#include <linux/clk.h>
Andrea Paterniani69c202a2007-02-12 00:52:39 -080033
34#include <asm/io.h>
35#include <asm/irq.h>
Andrea Paterniani69c202a2007-02-12 00:52:39 -080036#include <asm/delay.h>
37
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/hardware.h>
39#include <mach/imx-dma.h>
40#include <mach/spi_imx.h>
Andrea Paterniani69c202a2007-02-12 00:52:39 -080041
42/*-------------------------------------------------------------------------*/
43/* SPI Registers offsets from peripheral base address */
44#define SPI_RXDATA (0x00)
45#define SPI_TXDATA (0x04)
46#define SPI_CONTROL (0x08)
47#define SPI_INT_STATUS (0x0C)
48#define SPI_TEST (0x10)
49#define SPI_PERIOD (0x14)
50#define SPI_DMA (0x18)
51#define SPI_RESET (0x1C)
52
53/* SPI Control Register Bit Fields & Masks */
54#define SPI_CONTROL_BITCOUNT_MASK (0xF) /* Bit Count Mask */
55#define SPI_CONTROL_BITCOUNT(n) (((n) - 1) & SPI_CONTROL_BITCOUNT_MASK)
56#define SPI_CONTROL_POL (0x1 << 4) /* Clock Polarity Mask */
57#define SPI_CONTROL_POL_ACT_HIGH (0x0 << 4) /* Active high pol. (0=idle) */
58#define SPI_CONTROL_POL_ACT_LOW (0x1 << 4) /* Active low pol. (1=idle) */
59#define SPI_CONTROL_PHA (0x1 << 5) /* Clock Phase Mask */
60#define SPI_CONTROL_PHA_0 (0x0 << 5) /* Clock Phase 0 */
61#define SPI_CONTROL_PHA_1 (0x1 << 5) /* Clock Phase 1 */
62#define SPI_CONTROL_SSCTL (0x1 << 6) /* /SS Waveform Select Mask */
63#define SPI_CONTROL_SSCTL_0 (0x0 << 6) /* Master: /SS stays low between SPI burst
64 Slave: RXFIFO advanced by BIT_COUNT */
65#define SPI_CONTROL_SSCTL_1 (0x1 << 6) /* Master: /SS insert pulse between SPI burst
66 Slave: RXFIFO advanced by /SS rising edge */
67#define SPI_CONTROL_SSPOL (0x1 << 7) /* /SS Polarity Select Mask */
68#define SPI_CONTROL_SSPOL_ACT_LOW (0x0 << 7) /* /SS Active low */
69#define SPI_CONTROL_SSPOL_ACT_HIGH (0x1 << 7) /* /SS Active high */
70#define SPI_CONTROL_XCH (0x1 << 8) /* Exchange */
71#define SPI_CONTROL_SPIEN (0x1 << 9) /* SPI Module Enable */
72#define SPI_CONTROL_MODE (0x1 << 10) /* SPI Mode Select Mask */
73#define SPI_CONTROL_MODE_SLAVE (0x0 << 10) /* SPI Mode Slave */
74#define SPI_CONTROL_MODE_MASTER (0x1 << 10) /* SPI Mode Master */
75#define SPI_CONTROL_DRCTL (0x3 << 11) /* /SPI_RDY Control Mask */
76#define SPI_CONTROL_DRCTL_0 (0x0 << 11) /* Ignore /SPI_RDY */
77#define SPI_CONTROL_DRCTL_1 (0x1 << 11) /* /SPI_RDY falling edge triggers input */
78#define SPI_CONTROL_DRCTL_2 (0x2 << 11) /* /SPI_RDY active low level triggers input */
79#define SPI_CONTROL_DATARATE (0x7 << 13) /* Data Rate Mask */
80#define SPI_PERCLK2_DIV_MIN (0) /* PERCLK2:4 */
81#define SPI_PERCLK2_DIV_MAX (7) /* PERCLK2:512 */
82#define SPI_CONTROL_DATARATE_MIN (SPI_PERCLK2_DIV_MAX << 13)
83#define SPI_CONTROL_DATARATE_MAX (SPI_PERCLK2_DIV_MIN << 13)
84#define SPI_CONTROL_DATARATE_BAD (SPI_CONTROL_DATARATE_MIN + 1)
85
86/* SPI Interrupt/Status Register Bit Fields & Masks */
87#define SPI_STATUS_TE (0x1 << 0) /* TXFIFO Empty Status */
88#define SPI_STATUS_TH (0x1 << 1) /* TXFIFO Half Status */
89#define SPI_STATUS_TF (0x1 << 2) /* TXFIFO Full Status */
90#define SPI_STATUS_RR (0x1 << 3) /* RXFIFO Data Ready Status */
91#define SPI_STATUS_RH (0x1 << 4) /* RXFIFO Half Status */
92#define SPI_STATUS_RF (0x1 << 5) /* RXFIFO Full Status */
93#define SPI_STATUS_RO (0x1 << 6) /* RXFIFO Overflow */
94#define SPI_STATUS_BO (0x1 << 7) /* Bit Count Overflow */
95#define SPI_STATUS (0xFF) /* SPI Status Mask */
96#define SPI_INTEN_TE (0x1 << 8) /* TXFIFO Empty Interrupt Enable */
97#define SPI_INTEN_TH (0x1 << 9) /* TXFIFO Half Interrupt Enable */
98#define SPI_INTEN_TF (0x1 << 10) /* TXFIFO Full Interrupt Enable */
99#define SPI_INTEN_RE (0x1 << 11) /* RXFIFO Data Ready Interrupt Enable */
100#define SPI_INTEN_RH (0x1 << 12) /* RXFIFO Half Interrupt Enable */
101#define SPI_INTEN_RF (0x1 << 13) /* RXFIFO Full Interrupt Enable */
102#define SPI_INTEN_RO (0x1 << 14) /* RXFIFO Overflow Interrupt Enable */
103#define SPI_INTEN_BO (0x1 << 15) /* Bit Count Overflow Interrupt Enable */
104#define SPI_INTEN (0xFF << 8) /* SPI Interrupt Enable Mask */
105
106/* SPI Test Register Bit Fields & Masks */
107#define SPI_TEST_TXCNT (0xF << 0) /* TXFIFO Counter */
108#define SPI_TEST_RXCNT_LSB (4) /* RXFIFO Counter LSB */
109#define SPI_TEST_RXCNT (0xF << 4) /* RXFIFO Counter */
110#define SPI_TEST_SSTATUS (0xF << 8) /* State Machine Status */
111#define SPI_TEST_LBC (0x1 << 14) /* Loop Back Control */
112
113/* SPI Period Register Bit Fields & Masks */
114#define SPI_PERIOD_WAIT (0x7FFF << 0) /* Wait Between Transactions */
115#define SPI_PERIOD_MAX_WAIT (0x7FFF) /* Max Wait Between
116 Transactions */
117#define SPI_PERIOD_CSRC (0x1 << 15) /* Period Clock Source Mask */
118#define SPI_PERIOD_CSRC_BCLK (0x0 << 15) /* Period Clock Source is
119 Bit Clock */
120#define SPI_PERIOD_CSRC_32768 (0x1 << 15) /* Period Clock Source is
121 32.768 KHz Clock */
122
123/* SPI DMA Register Bit Fields & Masks */
Andrea Paternianiac140a82007-06-01 00:47:07 -0700124#define SPI_DMA_RHDMA (0x1 << 4) /* RXFIFO Half Status */
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800125#define SPI_DMA_RFDMA (0x1 << 5) /* RXFIFO Full Status */
126#define SPI_DMA_TEDMA (0x1 << 6) /* TXFIFO Empty Status */
127#define SPI_DMA_THDMA (0x1 << 7) /* TXFIFO Half Status */
128#define SPI_DMA_RHDEN (0x1 << 12) /* RXFIFO Half DMA Request Enable */
129#define SPI_DMA_RFDEN (0x1 << 13) /* RXFIFO Full DMA Request Enable */
130#define SPI_DMA_TEDEN (0x1 << 14) /* TXFIFO Empty DMA Request Enable */
131#define SPI_DMA_THDEN (0x1 << 15) /* TXFIFO Half DMA Request Enable */
132
133/* SPI Soft Reset Register Bit Fields & Masks */
134#define SPI_RESET_START (0x1) /* Start */
135
136/* Default SPI configuration values */
137#define SPI_DEFAULT_CONTROL \
138( \
139 SPI_CONTROL_BITCOUNT(16) | \
140 SPI_CONTROL_POL_ACT_HIGH | \
141 SPI_CONTROL_PHA_0 | \
142 SPI_CONTROL_SPIEN | \
143 SPI_CONTROL_SSCTL_1 | \
144 SPI_CONTROL_MODE_MASTER | \
145 SPI_CONTROL_DRCTL_0 | \
146 SPI_CONTROL_DATARATE_MIN \
147)
148#define SPI_DEFAULT_ENABLE_LOOPBACK (0)
149#define SPI_DEFAULT_ENABLE_DMA (0)
150#define SPI_DEFAULT_PERIOD_WAIT (8)
151/*-------------------------------------------------------------------------*/
152
153
154/*-------------------------------------------------------------------------*/
155/* TX/RX SPI FIFO size */
156#define SPI_FIFO_DEPTH (8)
157#define SPI_FIFO_BYTE_WIDTH (2)
158#define SPI_FIFO_OVERFLOW_MARGIN (2)
159
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200160/* DMA burst length for half full/empty request trigger */
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800161#define SPI_DMA_BLR (SPI_FIFO_DEPTH * SPI_FIFO_BYTE_WIDTH / 2)
162
163/* Dummy char output to achieve reads.
164 Choosing something different from all zeroes may help pattern recogition
165 for oscilloscope analysis, but may break some drivers. */
166#define SPI_DUMMY_u8 0
167#define SPI_DUMMY_u16 ((SPI_DUMMY_u8 << 8) | SPI_DUMMY_u8)
168#define SPI_DUMMY_u32 ((SPI_DUMMY_u16 << 16) | SPI_DUMMY_u16)
169
170/**
171 * Macro to change a u32 field:
172 * @r : register to edit
173 * @m : bit mask
174 * @v : new value for the field correctly bit-alligned
175*/
176#define u32_EDIT(r, m, v) r = (r & ~(m)) | (v)
177
178/* Message state */
179#define START_STATE ((void*)0)
180#define RUNNING_STATE ((void*)1)
181#define DONE_STATE ((void*)2)
182#define ERROR_STATE ((void*)-1)
183
184/* Queue state */
185#define QUEUE_RUNNING (0)
186#define QUEUE_STOPPED (1)
187
188#define IS_DMA_ALIGNED(x) (((u32)(x) & 0x03) == 0)
189/*-------------------------------------------------------------------------*/
190
191
192/*-------------------------------------------------------------------------*/
193/* Driver data structs */
194
195/* Context */
196struct driver_data {
197 /* Driver model hookup */
198 struct platform_device *pdev;
199
200 /* SPI framework hookup */
201 struct spi_master *master;
202
203 /* IMX hookup */
204 struct spi_imx_master *master_info;
205
206 /* Memory resources and SPI regs virtual address */
207 struct resource *ioarea;
208 void __iomem *regs;
209
210 /* SPI RX_DATA physical address */
211 dma_addr_t rd_data_phys;
212
213 /* Driver message queue */
214 struct workqueue_struct *workqueue;
215 struct work_struct work;
216 spinlock_t lock;
217 struct list_head queue;
218 int busy;
219 int run;
220
221 /* Message Transfer pump */
222 struct tasklet_struct pump_transfers;
223
224 /* Current message, transfer and state */
225 struct spi_message *cur_msg;
226 struct spi_transfer *cur_transfer;
227 struct chip_data *cur_chip;
228
229 /* Rd / Wr buffers pointers */
230 size_t len;
231 void *tx;
232 void *tx_end;
233 void *rx;
234 void *rx_end;
235
236 u8 rd_only;
237 u8 n_bytes;
238 int cs_change;
239
240 /* Function pointers */
241 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
242 void (*cs_control)(u32 command);
243
244 /* DMA setup */
245 int rx_channel;
246 int tx_channel;
247 dma_addr_t rx_dma;
248 dma_addr_t tx_dma;
249 int rx_dma_needs_unmap;
250 int tx_dma_needs_unmap;
251 size_t tx_map_len;
252 u32 dummy_dma_buf ____cacheline_aligned;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200253
254 struct clk *clk;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800255};
256
257/* Runtime state */
258struct chip_data {
259 u32 control;
260 u32 period;
261 u32 test;
262
263 u8 enable_dma:1;
264 u8 bits_per_word;
265 u8 n_bytes;
266 u32 max_speed_hz;
267
268 void (*cs_control)(u32 command);
269};
270/*-------------------------------------------------------------------------*/
271
272
273static void pump_messages(struct work_struct *work);
274
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700275static void flush(struct driver_data *drv_data)
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800276{
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800277 void __iomem *regs = drv_data->regs;
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700278 u32 control;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800279
280 dev_dbg(&drv_data->pdev->dev, "flush\n");
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800281
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700282 /* Wait for end of transaction */
283 do {
284 control = readl(regs + SPI_CONTROL);
285 } while (control & SPI_CONTROL_XCH);
286
287 /* Release chip select if requested, transfer delays are
288 handled in pump_transfers */
289 if (drv_data->cs_change)
290 drv_data->cs_control(SPI_CS_DEASSERT);
291
292 /* Disable SPI to flush FIFOs */
293 writel(control & ~SPI_CONTROL_SPIEN, regs + SPI_CONTROL);
294 writel(control, regs + SPI_CONTROL);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800295}
296
297static void restore_state(struct driver_data *drv_data)
298{
299 void __iomem *regs = drv_data->regs;
300 struct chip_data *chip = drv_data->cur_chip;
301
302 /* Load chip registers */
303 dev_dbg(&drv_data->pdev->dev,
304 "restore_state\n"
305 " test = 0x%08X\n"
306 " control = 0x%08X\n",
307 chip->test,
308 chip->control);
309 writel(chip->test, regs + SPI_TEST);
310 writel(chip->period, regs + SPI_PERIOD);
311 writel(0, regs + SPI_INT_STATUS);
312 writel(chip->control, regs + SPI_CONTROL);
313}
314
315static void null_cs_control(u32 command)
316{
317}
318
319static inline u32 data_to_write(struct driver_data *drv_data)
320{
321 return ((u32)(drv_data->tx_end - drv_data->tx)) / drv_data->n_bytes;
322}
323
324static inline u32 data_to_read(struct driver_data *drv_data)
325{
326 return ((u32)(drv_data->rx_end - drv_data->rx)) / drv_data->n_bytes;
327}
328
329static int write(struct driver_data *drv_data)
330{
331 void __iomem *regs = drv_data->regs;
332 void *tx = drv_data->tx;
333 void *tx_end = drv_data->tx_end;
334 u8 n_bytes = drv_data->n_bytes;
335 u32 remaining_writes;
336 u32 fifo_avail_space;
337 u32 n;
338 u16 d;
339
340 /* Compute how many fifo writes to do */
341 remaining_writes = (u32)(tx_end - tx) / n_bytes;
342 fifo_avail_space = SPI_FIFO_DEPTH -
343 (readl(regs + SPI_TEST) & SPI_TEST_TXCNT);
344 if (drv_data->rx && (fifo_avail_space > SPI_FIFO_OVERFLOW_MARGIN))
345 /* Fix misunderstood receive overflow */
346 fifo_avail_space -= SPI_FIFO_OVERFLOW_MARGIN;
347 n = min(remaining_writes, fifo_avail_space);
348
349 dev_dbg(&drv_data->pdev->dev,
350 "write type %s\n"
351 " remaining writes = %d\n"
352 " fifo avail space = %d\n"
353 " fifo writes = %d\n",
354 (n_bytes == 1) ? "u8" : "u16",
355 remaining_writes,
356 fifo_avail_space,
357 n);
358
359 if (n > 0) {
360 /* Fill SPI TXFIFO */
361 if (drv_data->rd_only) {
362 tx += n * n_bytes;
363 while (n--)
364 writel(SPI_DUMMY_u16, regs + SPI_TXDATA);
365 } else {
366 if (n_bytes == 1) {
367 while (n--) {
368 d = *(u8*)tx;
369 writel(d, regs + SPI_TXDATA);
370 tx += 1;
371 }
372 } else {
373 while (n--) {
374 d = *(u16*)tx;
375 writel(d, regs + SPI_TXDATA);
376 tx += 2;
377 }
378 }
379 }
380
381 /* Trigger transfer */
382 writel(readl(regs + SPI_CONTROL) | SPI_CONTROL_XCH,
383 regs + SPI_CONTROL);
384
385 /* Update tx pointer */
386 drv_data->tx = tx;
387 }
388
389 return (tx >= tx_end);
390}
391
392static int read(struct driver_data *drv_data)
393{
394 void __iomem *regs = drv_data->regs;
395 void *rx = drv_data->rx;
396 void *rx_end = drv_data->rx_end;
397 u8 n_bytes = drv_data->n_bytes;
398 u32 remaining_reads;
399 u32 fifo_rxcnt;
400 u32 n;
401 u16 d;
402
403 /* Compute how many fifo reads to do */
404 remaining_reads = (u32)(rx_end - rx) / n_bytes;
405 fifo_rxcnt = (readl(regs + SPI_TEST) & SPI_TEST_RXCNT) >>
406 SPI_TEST_RXCNT_LSB;
407 n = min(remaining_reads, fifo_rxcnt);
408
409 dev_dbg(&drv_data->pdev->dev,
410 "read type %s\n"
411 " remaining reads = %d\n"
412 " fifo rx count = %d\n"
413 " fifo reads = %d\n",
414 (n_bytes == 1) ? "u8" : "u16",
415 remaining_reads,
416 fifo_rxcnt,
417 n);
418
419 if (n > 0) {
420 /* Read SPI RXFIFO */
421 if (n_bytes == 1) {
422 while (n--) {
423 d = readl(regs + SPI_RXDATA);
424 *((u8*)rx) = d;
425 rx += 1;
426 }
427 } else {
428 while (n--) {
429 d = readl(regs + SPI_RXDATA);
430 *((u16*)rx) = d;
431 rx += 2;
432 }
433 }
434
435 /* Update rx pointer */
436 drv_data->rx = rx;
437 }
438
439 return (rx >= rx_end);
440}
441
442static void *next_transfer(struct driver_data *drv_data)
443{
444 struct spi_message *msg = drv_data->cur_msg;
445 struct spi_transfer *trans = drv_data->cur_transfer;
446
447 /* Move to next transfer */
448 if (trans->transfer_list.next != &msg->transfers) {
449 drv_data->cur_transfer =
450 list_entry(trans->transfer_list.next,
451 struct spi_transfer,
452 transfer_list);
453 return RUNNING_STATE;
454 }
455
456 return DONE_STATE;
457}
458
459static int map_dma_buffers(struct driver_data *drv_data)
460{
461 struct spi_message *msg;
462 struct device *dev;
463 void *buf;
464
465 drv_data->rx_dma_needs_unmap = 0;
466 drv_data->tx_dma_needs_unmap = 0;
467
468 if (!drv_data->master_info->enable_dma ||
469 !drv_data->cur_chip->enable_dma)
470 return -1;
471
472 msg = drv_data->cur_msg;
473 dev = &msg->spi->dev;
474 if (msg->is_dma_mapped) {
475 if (drv_data->tx_dma)
476 /* The caller provided at least dma and cpu virtual
477 address for write; pump_transfers() will consider the
478 transfer as write only if cpu rx virtual address is
479 NULL */
480 return 0;
481
482 if (drv_data->rx_dma) {
483 /* The caller provided dma and cpu virtual address to
484 performe read only transfer -->
485 use drv_data->dummy_dma_buf for dummy writes to
486 achive reads */
487 buf = &drv_data->dummy_dma_buf;
488 drv_data->tx_map_len = sizeof(drv_data->dummy_dma_buf);
489 drv_data->tx_dma = dma_map_single(dev,
490 buf,
491 drv_data->tx_map_len,
492 DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700493 if (dma_mapping_error(dev, drv_data->tx_dma))
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800494 return -1;
495
496 drv_data->tx_dma_needs_unmap = 1;
497
498 /* Flags transfer as rd_only for pump_transfers() DMA
499 regs programming (should be redundant) */
500 drv_data->tx = NULL;
501
502 return 0;
503 }
504 }
505
506 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
507 return -1;
508
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800509 if (drv_data->tx == NULL) {
510 /* Read only message --> use drv_data->dummy_dma_buf for dummy
511 writes to achive reads */
512 buf = &drv_data->dummy_dma_buf;
513 drv_data->tx_map_len = sizeof(drv_data->dummy_dma_buf);
514 } else {
515 buf = drv_data->tx;
516 drv_data->tx_map_len = drv_data->len;
517 }
518 drv_data->tx_dma = dma_map_single(dev,
519 buf,
520 drv_data->tx_map_len,
521 DMA_TO_DEVICE);
Andrea Paterniani3b45d632008-11-19 15:36:26 -0800522 if (dma_mapping_error(dev, drv_data->tx_dma))
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800523 return -1;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800524 drv_data->tx_dma_needs_unmap = 1;
525
Andrea Paterniani3b45d632008-11-19 15:36:26 -0800526 /* NULL rx means write-only transfer and no map needed
527 * since rx DMA will not be used */
528 if (drv_data->rx) {
529 buf = drv_data->rx;
530 drv_data->rx_dma = dma_map_single(dev,
531 buf,
532 drv_data->len,
533 DMA_FROM_DEVICE);
534 if (dma_mapping_error(dev, drv_data->rx_dma)) {
535 if (drv_data->tx_dma) {
536 dma_unmap_single(dev,
537 drv_data->tx_dma,
538 drv_data->tx_map_len,
539 DMA_TO_DEVICE);
540 drv_data->tx_dma_needs_unmap = 0;
541 }
542 return -1;
543 }
544 drv_data->rx_dma_needs_unmap = 1;
545 }
546
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800547 return 0;
548}
549
550static void unmap_dma_buffers(struct driver_data *drv_data)
551{
552 struct spi_message *msg = drv_data->cur_msg;
553 struct device *dev = &msg->spi->dev;
554
555 if (drv_data->rx_dma_needs_unmap) {
556 dma_unmap_single(dev,
557 drv_data->rx_dma,
558 drv_data->len,
559 DMA_FROM_DEVICE);
560 drv_data->rx_dma_needs_unmap = 0;
561 }
562 if (drv_data->tx_dma_needs_unmap) {
563 dma_unmap_single(dev,
564 drv_data->tx_dma,
565 drv_data->tx_map_len,
566 DMA_TO_DEVICE);
567 drv_data->tx_dma_needs_unmap = 0;
568 }
569}
570
571/* Caller already set message->status (dma is already blocked) */
572static void giveback(struct spi_message *message, struct driver_data *drv_data)
573{
574 void __iomem *regs = drv_data->regs;
575
576 /* Bring SPI to sleep; restore_state() and pump_transfer()
577 will do new setup */
578 writel(0, regs + SPI_INT_STATUS);
579 writel(0, regs + SPI_DMA);
580
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700581 /* Unconditioned deselct */
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800582 drv_data->cs_control(SPI_CS_DEASSERT);
583
584 message->state = NULL;
585 if (message->complete)
586 message->complete(message->context);
587
588 drv_data->cur_msg = NULL;
589 drv_data->cur_transfer = NULL;
590 drv_data->cur_chip = NULL;
591 queue_work(drv_data->workqueue, &drv_data->work);
592}
593
594static void dma_err_handler(int channel, void *data, int errcode)
595{
596 struct driver_data *drv_data = data;
597 struct spi_message *msg = drv_data->cur_msg;
598
599 dev_dbg(&drv_data->pdev->dev, "dma_err_handler\n");
600
601 /* Disable both rx and tx dma channels */
602 imx_dma_disable(drv_data->rx_channel);
603 imx_dma_disable(drv_data->tx_channel);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800604 unmap_dma_buffers(drv_data);
605
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700606 flush(drv_data);
607
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800608 msg->state = ERROR_STATE;
609 tasklet_schedule(&drv_data->pump_transfers);
610}
611
612static void dma_tx_handler(int channel, void *data)
613{
614 struct driver_data *drv_data = data;
615
616 dev_dbg(&drv_data->pdev->dev, "dma_tx_handler\n");
617
618 imx_dma_disable(channel);
619
620 /* Now waits for TX FIFO empty */
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700621 writel(SPI_INTEN_TE, drv_data->regs + SPI_INT_STATUS);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800622}
623
624static irqreturn_t dma_transfer(struct driver_data *drv_data)
625{
626 u32 status;
627 struct spi_message *msg = drv_data->cur_msg;
628 void __iomem *regs = drv_data->regs;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800629
630 status = readl(regs + SPI_INT_STATUS);
631
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700632 if ((status & (SPI_INTEN_RO | SPI_STATUS_RO))
633 == (SPI_INTEN_RO | SPI_STATUS_RO)) {
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800634 writel(status & ~SPI_INTEN, regs + SPI_INT_STATUS);
635
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700636 imx_dma_disable(drv_data->tx_channel);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800637 imx_dma_disable(drv_data->rx_channel);
638 unmap_dma_buffers(drv_data);
639
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700640 flush(drv_data);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800641
642 dev_warn(&drv_data->pdev->dev,
643 "dma_transfer - fifo overun\n");
644
645 msg->state = ERROR_STATE;
646 tasklet_schedule(&drv_data->pump_transfers);
647
648 return IRQ_HANDLED;
649 }
650
651 if (status & SPI_STATUS_TE) {
652 writel(status & ~SPI_INTEN_TE, regs + SPI_INT_STATUS);
653
654 if (drv_data->rx) {
655 /* Wait end of transfer before read trailing data */
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700656 while (readl(regs + SPI_CONTROL) & SPI_CONTROL_XCH)
657 cpu_relax();
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800658
659 imx_dma_disable(drv_data->rx_channel);
660 unmap_dma_buffers(drv_data);
661
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700662 /* Release chip select if requested, transfer delays are
663 handled in pump_transfers() */
664 if (drv_data->cs_change)
665 drv_data->cs_control(SPI_CS_DEASSERT);
666
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800667 /* Calculate number of trailing data and read them */
668 dev_dbg(&drv_data->pdev->dev,
669 "dma_transfer - test = 0x%08X\n",
670 readl(regs + SPI_TEST));
671 drv_data->rx = drv_data->rx_end -
672 ((readl(regs + SPI_TEST) &
673 SPI_TEST_RXCNT) >>
674 SPI_TEST_RXCNT_LSB)*drv_data->n_bytes;
675 read(drv_data);
676 } else {
677 /* Write only transfer */
678 unmap_dma_buffers(drv_data);
679
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700680 flush(drv_data);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800681 }
682
683 /* End of transfer, update total byte transfered */
684 msg->actual_length += drv_data->len;
685
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800686 /* Move to next transfer */
687 msg->state = next_transfer(drv_data);
688
689 /* Schedule transfer tasklet */
690 tasklet_schedule(&drv_data->pump_transfers);
691
692 return IRQ_HANDLED;
693 }
694
695 /* Opps problem detected */
696 return IRQ_NONE;
697}
698
699static irqreturn_t interrupt_wronly_transfer(struct driver_data *drv_data)
700{
701 struct spi_message *msg = drv_data->cur_msg;
702 void __iomem *regs = drv_data->regs;
703 u32 status;
704 irqreturn_t handled = IRQ_NONE;
705
706 status = readl(regs + SPI_INT_STATUS);
707
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700708 if (status & SPI_INTEN_TE) {
709 /* TXFIFO Empty Interrupt on the last transfered word */
710 writel(status & ~SPI_INTEN, regs + SPI_INT_STATUS);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800711 dev_dbg(&drv_data->pdev->dev,
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700712 "interrupt_wronly_transfer - end of tx\n");
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800713
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700714 flush(drv_data);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800715
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700716 /* Update total byte transfered */
717 msg->actual_length += drv_data->len;
718
719 /* Move to next transfer */
720 msg->state = next_transfer(drv_data);
721
722 /* Schedule transfer tasklet */
723 tasklet_schedule(&drv_data->pump_transfers);
724
725 return IRQ_HANDLED;
726 } else {
727 while (status & SPI_STATUS_TH) {
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800728 dev_dbg(&drv_data->pdev->dev,
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700729 "interrupt_wronly_transfer - status = 0x%08X\n",
730 status);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800731
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700732 /* Pump data */
733 if (write(drv_data)) {
734 /* End of TXFIFO writes,
735 now wait until TXFIFO is empty */
736 writel(SPI_INTEN_TE, regs + SPI_INT_STATUS);
737 return IRQ_HANDLED;
738 }
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800739
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700740 status = readl(regs + SPI_INT_STATUS);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800741
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700742 /* We did something */
743 handled = IRQ_HANDLED;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800744 }
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800745 }
746
747 return handled;
748}
749
750static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
751{
752 struct spi_message *msg = drv_data->cur_msg;
753 void __iomem *regs = drv_data->regs;
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700754 u32 status, control;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800755 irqreturn_t handled = IRQ_NONE;
756 unsigned long limit;
757
758 status = readl(regs + SPI_INT_STATUS);
759
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700760 if (status & SPI_INTEN_TE) {
761 /* TXFIFO Empty Interrupt on the last transfered word */
762 writel(status & ~SPI_INTEN, regs + SPI_INT_STATUS);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800763 dev_dbg(&drv_data->pdev->dev,
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700764 "interrupt_transfer - end of tx\n");
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800765
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700766 if (msg->state == ERROR_STATE) {
767 /* RXFIFO overrun was detected and message aborted */
768 flush(drv_data);
769 } else {
770 /* Wait for end of transaction */
771 do {
772 control = readl(regs + SPI_CONTROL);
773 } while (control & SPI_CONTROL_XCH);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800774
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700775 /* Release chip select if requested, transfer delays are
776 handled in pump_transfers */
777 if (drv_data->cs_change)
778 drv_data->cs_control(SPI_CS_DEASSERT);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800779
780 /* Read trailing bytes */
781 limit = loops_per_jiffy << 1;
Roel Kluinc8fc6572009-04-06 19:00:54 -0700782 while ((read(drv_data) == 0) && --limit)
783 cpu_relax();
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800784
785 if (limit == 0)
786 dev_err(&drv_data->pdev->dev,
787 "interrupt_transfer - "
788 "trailing byte read failed\n");
789 else
790 dev_dbg(&drv_data->pdev->dev,
791 "interrupt_transfer - end of rx\n");
792
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700793 /* Update total byte transfered */
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800794 msg->actual_length += drv_data->len;
795
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800796 /* Move to next transfer */
797 msg->state = next_transfer(drv_data);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800798 }
799
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700800 /* Schedule transfer tasklet */
801 tasklet_schedule(&drv_data->pump_transfers);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800802
Andrea Paterniani5d9f3f62008-04-28 02:14:21 -0700803 return IRQ_HANDLED;
804 } else {
805 while (status & (SPI_STATUS_TH | SPI_STATUS_RO)) {
806 dev_dbg(&drv_data->pdev->dev,
807 "interrupt_transfer - status = 0x%08X\n",
808 status);
809
810 if (status & SPI_STATUS_RO) {
811 /* RXFIFO overrun, abort message end wait
812 until TXFIFO is empty */
813 writel(SPI_INTEN_TE, regs + SPI_INT_STATUS);
814
815 dev_warn(&drv_data->pdev->dev,
816 "interrupt_transfer - fifo overun\n"
817 " data not yet written = %d\n"
818 " data not yet read = %d\n",
819 data_to_write(drv_data),
820 data_to_read(drv_data));
821
822 msg->state = ERROR_STATE;
823
824 return IRQ_HANDLED;
825 }
826
827 /* Pump data */
828 read(drv_data);
829 if (write(drv_data)) {
830 /* End of TXFIFO writes,
831 now wait until TXFIFO is empty */
832 writel(SPI_INTEN_TE, regs + SPI_INT_STATUS);
833 return IRQ_HANDLED;
834 }
835
836 status = readl(regs + SPI_INT_STATUS);
837
838 /* We did something */
839 handled = IRQ_HANDLED;
840 }
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800841 }
842
843 return handled;
844}
845
846static irqreturn_t spi_int(int irq, void *dev_id)
847{
848 struct driver_data *drv_data = (struct driver_data *)dev_id;
849
850 if (!drv_data->cur_msg) {
851 dev_err(&drv_data->pdev->dev,
852 "spi_int - bad message state\n");
853 /* Never fail */
854 return IRQ_HANDLED;
855 }
856
857 return drv_data->transfer_handler(drv_data);
858}
859
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200860static inline u32 spi_speed_hz(struct driver_data *drv_data, u32 data_rate)
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800861{
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200862 return clk_get_rate(drv_data->clk) / (4 << ((data_rate) >> 13));
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800863}
864
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200865static u32 spi_data_rate(struct driver_data *drv_data, u32 speed_hz)
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800866{
867 u32 div;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200868 u32 quantized_hz = clk_get_rate(drv_data->clk) >> 2;
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800869
870 for (div = SPI_PERCLK2_DIV_MIN;
871 div <= SPI_PERCLK2_DIV_MAX;
872 div++, quantized_hz >>= 1) {
873 if (quantized_hz <= speed_hz)
874 /* Max available speed LEQ required speed */
875 return div << 13;
876 }
877 return SPI_CONTROL_DATARATE_BAD;
878}
879
880static void pump_transfers(unsigned long data)
881{
882 struct driver_data *drv_data = (struct driver_data *)data;
883 struct spi_message *message;
884 struct spi_transfer *transfer, *previous;
885 struct chip_data *chip;
886 void __iomem *regs;
887 u32 tmp, control;
888
889 dev_dbg(&drv_data->pdev->dev, "pump_transfer\n");
890
891 message = drv_data->cur_msg;
892
893 /* Handle for abort */
894 if (message->state == ERROR_STATE) {
895 message->status = -EIO;
896 giveback(message, drv_data);
897 return;
898 }
899
900 /* Handle end of message */
901 if (message->state == DONE_STATE) {
902 message->status = 0;
903 giveback(message, drv_data);
904 return;
905 }
906
907 chip = drv_data->cur_chip;
908
909 /* Delay if requested at end of transfer*/
910 transfer = drv_data->cur_transfer;
911 if (message->state == RUNNING_STATE) {
912 previous = list_entry(transfer->transfer_list.prev,
913 struct spi_transfer,
914 transfer_list);
915 if (previous->delay_usecs)
916 udelay(previous->delay_usecs);
917 } else {
918 /* START_STATE */
919 message->state = RUNNING_STATE;
920 drv_data->cs_control = chip->cs_control;
921 }
922
923 transfer = drv_data->cur_transfer;
924 drv_data->tx = (void *)transfer->tx_buf;
925 drv_data->tx_end = drv_data->tx + transfer->len;
926 drv_data->rx = transfer->rx_buf;
927 drv_data->rx_end = drv_data->rx + transfer->len;
928 drv_data->rx_dma = transfer->rx_dma;
929 drv_data->tx_dma = transfer->tx_dma;
930 drv_data->len = transfer->len;
931 drv_data->cs_change = transfer->cs_change;
932 drv_data->rd_only = (drv_data->tx == NULL);
933
934 regs = drv_data->regs;
935 control = readl(regs + SPI_CONTROL);
936
937 /* Bits per word setup */
938 tmp = transfer->bits_per_word;
939 if (tmp == 0) {
940 /* Use device setup */
941 tmp = chip->bits_per_word;
942 drv_data->n_bytes = chip->n_bytes;
943 } else
944 /* Use per-transfer setup */
945 drv_data->n_bytes = (tmp <= 8) ? 1 : 2;
946 u32_EDIT(control, SPI_CONTROL_BITCOUNT_MASK, tmp - 1);
947
948 /* Speed setup (surely valid because already checked) */
949 tmp = transfer->speed_hz;
950 if (tmp == 0)
951 tmp = chip->max_speed_hz;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200952 tmp = spi_data_rate(drv_data, tmp);
Andrea Paterniani69c202a2007-02-12 00:52:39 -0800953 u32_EDIT(control, SPI_CONTROL_DATARATE, tmp);
954
955 writel(control, regs + SPI_CONTROL);
956
957 /* Assert device chip-select */
958 drv_data->cs_control(SPI_CS_ASSERT);
959
960 /* DMA cannot read/write SPI FIFOs other than 16 bits at a time; hence
961 if bits_per_word is less or equal 8 PIO transfers are performed.
962 Moreover DMA is convinient for transfer length bigger than FIFOs
963 byte size. */
964 if ((drv_data->n_bytes == 2) &&
965 (drv_data->len > SPI_FIFO_DEPTH*SPI_FIFO_BYTE_WIDTH) &&
966 (map_dma_buffers(drv_data) == 0)) {
967 dev_dbg(&drv_data->pdev->dev,
968 "pump dma transfer\n"
969 " tx = %p\n"
970 " tx_dma = %08X\n"
971 " rx = %p\n"
972 " rx_dma = %08X\n"
973 " len = %d\n",
974 drv_data->tx,
975 (unsigned int)drv_data->tx_dma,
976 drv_data->rx,
977 (unsigned int)drv_data->rx_dma,
978 drv_data->len);
979
980 /* Ensure we have the correct interrupt handler */
981 drv_data->transfer_handler = dma_transfer;
982
983 /* Trigger transfer */
984 writel(readl(regs + SPI_CONTROL) | SPI_CONTROL_XCH,
985 regs + SPI_CONTROL);
986
987 /* Setup tx DMA */
988 if (drv_data->tx)
989 /* Linear source address */
990 CCR(drv_data->tx_channel) =
991 CCR_DMOD_FIFO |
992 CCR_SMOD_LINEAR |
993 CCR_SSIZ_32 | CCR_DSIZ_16 |
994 CCR_REN;
995 else
996 /* Read only transfer -> fixed source address for
997 dummy write to achive read */
998 CCR(drv_data->tx_channel) =
999 CCR_DMOD_FIFO |
1000 CCR_SMOD_FIFO |
1001 CCR_SSIZ_32 | CCR_DSIZ_16 |
1002 CCR_REN;
1003
1004 imx_dma_setup_single(
1005 drv_data->tx_channel,
1006 drv_data->tx_dma,
1007 drv_data->len,
1008 drv_data->rd_data_phys + 4,
1009 DMA_MODE_WRITE);
1010
1011 if (drv_data->rx) {
1012 /* Setup rx DMA for linear destination address */
1013 CCR(drv_data->rx_channel) =
1014 CCR_DMOD_LINEAR |
1015 CCR_SMOD_FIFO |
1016 CCR_DSIZ_32 | CCR_SSIZ_16 |
1017 CCR_REN;
1018 imx_dma_setup_single(
1019 drv_data->rx_channel,
1020 drv_data->rx_dma,
1021 drv_data->len,
1022 drv_data->rd_data_phys,
1023 DMA_MODE_READ);
1024 imx_dma_enable(drv_data->rx_channel);
1025
1026 /* Enable SPI interrupt */
1027 writel(SPI_INTEN_RO, regs + SPI_INT_STATUS);
1028
1029 /* Set SPI to request DMA service on both
1030 Rx and Tx half fifo watermark */
1031 writel(SPI_DMA_RHDEN | SPI_DMA_THDEN, regs + SPI_DMA);
1032 } else
1033 /* Write only access -> set SPI to request DMA
1034 service on Tx half fifo watermark */
1035 writel(SPI_DMA_THDEN, regs + SPI_DMA);
1036
1037 imx_dma_enable(drv_data->tx_channel);
1038 } else {
1039 dev_dbg(&drv_data->pdev->dev,
1040 "pump pio transfer\n"
1041 " tx = %p\n"
1042 " rx = %p\n"
1043 " len = %d\n",
1044 drv_data->tx,
1045 drv_data->rx,
1046 drv_data->len);
1047
1048 /* Ensure we have the correct interrupt handler */
1049 if (drv_data->rx)
1050 drv_data->transfer_handler = interrupt_transfer;
1051 else
1052 drv_data->transfer_handler = interrupt_wronly_transfer;
1053
1054 /* Enable SPI interrupt */
1055 if (drv_data->rx)
1056 writel(SPI_INTEN_TH | SPI_INTEN_RO,
1057 regs + SPI_INT_STATUS);
1058 else
1059 writel(SPI_INTEN_TH, regs + SPI_INT_STATUS);
1060 }
1061}
1062
1063static void pump_messages(struct work_struct *work)
1064{
1065 struct driver_data *drv_data =
1066 container_of(work, struct driver_data, work);
1067 unsigned long flags;
1068
1069 /* Lock queue and check for queue work */
1070 spin_lock_irqsave(&drv_data->lock, flags);
1071 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1072 drv_data->busy = 0;
1073 spin_unlock_irqrestore(&drv_data->lock, flags);
1074 return;
1075 }
1076
1077 /* Make sure we are not already running a message */
1078 if (drv_data->cur_msg) {
1079 spin_unlock_irqrestore(&drv_data->lock, flags);
1080 return;
1081 }
1082
1083 /* Extract head of queue */
1084 drv_data->cur_msg = list_entry(drv_data->queue.next,
1085 struct spi_message, queue);
1086 list_del_init(&drv_data->cur_msg->queue);
1087 drv_data->busy = 1;
1088 spin_unlock_irqrestore(&drv_data->lock, flags);
1089
1090 /* Initial message state */
1091 drv_data->cur_msg->state = START_STATE;
1092 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1093 struct spi_transfer,
1094 transfer_list);
1095
1096 /* Setup the SPI using the per chip configuration */
1097 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1098 restore_state(drv_data);
1099
1100 /* Mark as busy and launch transfers */
1101 tasklet_schedule(&drv_data->pump_transfers);
1102}
1103
1104static int transfer(struct spi_device *spi, struct spi_message *msg)
1105{
1106 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1107 u32 min_speed_hz, max_speed_hz, tmp;
1108 struct spi_transfer *trans;
1109 unsigned long flags;
1110
1111 msg->actual_length = 0;
1112
1113 /* Per transfer setup check */
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001114 min_speed_hz = spi_speed_hz(drv_data, SPI_CONTROL_DATARATE_MIN);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001115 max_speed_hz = spi->max_speed_hz;
1116 list_for_each_entry(trans, &msg->transfers, transfer_list) {
1117 tmp = trans->bits_per_word;
1118 if (tmp > 16) {
1119 dev_err(&drv_data->pdev->dev,
1120 "message rejected : "
1121 "invalid transfer bits_per_word (%d bits)\n",
1122 tmp);
1123 goto msg_rejected;
1124 }
1125 tmp = trans->speed_hz;
1126 if (tmp) {
1127 if (tmp < min_speed_hz) {
1128 dev_err(&drv_data->pdev->dev,
1129 "message rejected : "
1130 "device min speed (%d Hz) exceeds "
1131 "required transfer speed (%d Hz)\n",
1132 min_speed_hz,
1133 tmp);
1134 goto msg_rejected;
1135 } else if (tmp > max_speed_hz) {
1136 dev_err(&drv_data->pdev->dev,
1137 "message rejected : "
1138 "transfer speed (%d Hz) exceeds "
1139 "device max speed (%d Hz)\n",
1140 tmp,
1141 max_speed_hz);
1142 goto msg_rejected;
1143 }
1144 }
1145 }
1146
1147 /* Message accepted */
1148 msg->status = -EINPROGRESS;
1149 msg->state = START_STATE;
1150
1151 spin_lock_irqsave(&drv_data->lock, flags);
1152 if (drv_data->run == QUEUE_STOPPED) {
1153 spin_unlock_irqrestore(&drv_data->lock, flags);
1154 return -ESHUTDOWN;
1155 }
1156
1157 list_add_tail(&msg->queue, &drv_data->queue);
1158 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1159 queue_work(drv_data->workqueue, &drv_data->work);
1160
1161 spin_unlock_irqrestore(&drv_data->lock, flags);
1162 return 0;
1163
1164msg_rejected:
1165 /* Message rejected and not queued */
1166 msg->status = -EINVAL;
1167 msg->state = ERROR_STATE;
1168 if (msg->complete)
1169 msg->complete(msg->context);
1170 return -EINVAL;
1171}
1172
David Brownelldccd5732007-07-17 04:04:02 -07001173/* the spi->mode bits understood by this driver: */
1174#define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
1175
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001176/* On first setup bad values must free chip_data memory since will cause
1177 spi_new_device to fail. Bad value setup from protocol driver are simply not
1178 applied and notified to the calling driver. */
1179static int setup(struct spi_device *spi)
1180{
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001181 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001182 struct spi_imx_chip *chip_info;
1183 struct chip_data *chip;
1184 int first_setup = 0;
1185 u32 tmp;
1186 int status = 0;
1187
David Brownelldccd5732007-07-17 04:04:02 -07001188 if (spi->mode & ~MODEBITS) {
1189 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
1190 spi->mode & ~MODEBITS);
1191 return -EINVAL;
1192 }
1193
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001194 /* Get controller data */
1195 chip_info = spi->controller_data;
1196
1197 /* Get controller_state */
1198 chip = spi_get_ctldata(spi);
1199 if (chip == NULL) {
1200 first_setup = 1;
1201
1202 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1203 if (!chip) {
1204 dev_err(&spi->dev,
Joe Perches898eb712007-10-18 03:06:30 -07001205 "setup - cannot allocate controller state\n");
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001206 return -ENOMEM;
1207 }
1208 chip->control = SPI_DEFAULT_CONTROL;
1209
1210 if (chip_info == NULL) {
1211 /* spi_board_info.controller_data not is supplied */
1212 chip_info = kzalloc(sizeof(struct spi_imx_chip),
1213 GFP_KERNEL);
1214 if (!chip_info) {
1215 dev_err(&spi->dev,
1216 "setup - "
Joe Perches898eb712007-10-18 03:06:30 -07001217 "cannot allocate controller data\n");
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001218 status = -ENOMEM;
1219 goto err_first_setup;
1220 }
1221 /* Set controller data default value */
1222 chip_info->enable_loopback =
1223 SPI_DEFAULT_ENABLE_LOOPBACK;
1224 chip_info->enable_dma = SPI_DEFAULT_ENABLE_DMA;
1225 chip_info->ins_ss_pulse = 1;
1226 chip_info->bclk_wait = SPI_DEFAULT_PERIOD_WAIT;
1227 chip_info->cs_control = null_cs_control;
1228 }
1229 }
1230
1231 /* Now set controller state based on controller data */
1232
1233 if (first_setup) {
1234 /* SPI loopback */
1235 if (chip_info->enable_loopback)
1236 chip->test = SPI_TEST_LBC;
1237 else
1238 chip->test = 0;
1239
1240 /* SPI dma driven */
1241 chip->enable_dma = chip_info->enable_dma;
1242
1243 /* SPI /SS pulse between spi burst */
1244 if (chip_info->ins_ss_pulse)
1245 u32_EDIT(chip->control,
1246 SPI_CONTROL_SSCTL, SPI_CONTROL_SSCTL_1);
1247 else
1248 u32_EDIT(chip->control,
1249 SPI_CONTROL_SSCTL, SPI_CONTROL_SSCTL_0);
1250
1251 /* SPI bclk waits between each bits_per_word spi burst */
1252 if (chip_info->bclk_wait > SPI_PERIOD_MAX_WAIT) {
1253 dev_err(&spi->dev,
1254 "setup - "
1255 "bclk_wait exceeds max allowed (%d)\n",
1256 SPI_PERIOD_MAX_WAIT);
1257 goto err_first_setup;
1258 }
1259 chip->period = SPI_PERIOD_CSRC_BCLK |
1260 (chip_info->bclk_wait & SPI_PERIOD_WAIT);
1261 }
1262
1263 /* SPI mode */
1264 tmp = spi->mode;
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001265 if (tmp & SPI_CS_HIGH) {
1266 u32_EDIT(chip->control,
1267 SPI_CONTROL_SSPOL, SPI_CONTROL_SSPOL_ACT_HIGH);
1268 }
1269 switch (tmp & SPI_MODE_3) {
1270 case SPI_MODE_0:
1271 tmp = 0;
1272 break;
1273 case SPI_MODE_1:
1274 tmp = SPI_CONTROL_PHA_1;
1275 break;
1276 case SPI_MODE_2:
1277 tmp = SPI_CONTROL_POL_ACT_LOW;
1278 break;
1279 default:
1280 /* SPI_MODE_3 */
1281 tmp = SPI_CONTROL_PHA_1 | SPI_CONTROL_POL_ACT_LOW;
1282 break;
1283 }
1284 u32_EDIT(chip->control, SPI_CONTROL_POL | SPI_CONTROL_PHA, tmp);
1285
1286 /* SPI word width */
1287 tmp = spi->bits_per_word;
1288 if (tmp == 0) {
1289 tmp = 8;
1290 spi->bits_per_word = 8;
1291 } else if (tmp > 16) {
1292 status = -EINVAL;
1293 dev_err(&spi->dev,
1294 "setup - "
1295 "invalid bits_per_word (%d)\n",
1296 tmp);
1297 if (first_setup)
1298 goto err_first_setup;
1299 else {
1300 /* Undo setup using chip as backup copy */
1301 tmp = chip->bits_per_word;
1302 spi->bits_per_word = tmp;
1303 }
1304 }
1305 chip->bits_per_word = tmp;
1306 u32_EDIT(chip->control, SPI_CONTROL_BITCOUNT_MASK, tmp - 1);
1307 chip->n_bytes = (tmp <= 8) ? 1 : 2;
1308
1309 /* SPI datarate */
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001310 tmp = spi_data_rate(drv_data, spi->max_speed_hz);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001311 if (tmp == SPI_CONTROL_DATARATE_BAD) {
1312 status = -EINVAL;
1313 dev_err(&spi->dev,
1314 "setup - "
1315 "HW min speed (%d Hz) exceeds required "
1316 "max speed (%d Hz)\n",
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001317 spi_speed_hz(drv_data, SPI_CONTROL_DATARATE_MIN),
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001318 spi->max_speed_hz);
1319 if (first_setup)
1320 goto err_first_setup;
1321 else
1322 /* Undo setup using chip as backup copy */
1323 spi->max_speed_hz = chip->max_speed_hz;
1324 } else {
1325 u32_EDIT(chip->control, SPI_CONTROL_DATARATE, tmp);
1326 /* Actual rounded max_speed_hz */
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001327 tmp = spi_speed_hz(drv_data, tmp);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001328 spi->max_speed_hz = tmp;
1329 chip->max_speed_hz = tmp;
1330 }
1331
1332 /* SPI chip-select management */
1333 if (chip_info->cs_control)
1334 chip->cs_control = chip_info->cs_control;
1335 else
1336 chip->cs_control = null_cs_control;
1337
1338 /* Save controller_state */
1339 spi_set_ctldata(spi, chip);
1340
1341 /* Summary */
1342 dev_dbg(&spi->dev,
1343 "setup succeded\n"
1344 " loopback enable = %s\n"
1345 " dma enable = %s\n"
1346 " insert /ss pulse = %s\n"
1347 " period wait = %d\n"
1348 " mode = %d\n"
1349 " bits per word = %d\n"
1350 " min speed = %d Hz\n"
1351 " rounded max speed = %d Hz\n",
1352 chip->test & SPI_TEST_LBC ? "Yes" : "No",
1353 chip->enable_dma ? "Yes" : "No",
1354 chip->control & SPI_CONTROL_SSCTL ? "Yes" : "No",
1355 chip->period & SPI_PERIOD_WAIT,
1356 spi->mode,
1357 spi->bits_per_word,
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001358 spi_speed_hz(drv_data, SPI_CONTROL_DATARATE_MIN),
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001359 spi->max_speed_hz);
Andrea Paternianiac140a82007-06-01 00:47:07 -07001360 return status;
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001361
1362err_first_setup:
1363 kfree(chip);
1364 return status;
1365}
1366
David Brownellbb2d1c32007-02-20 13:58:19 -08001367static void cleanup(struct spi_device *spi)
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001368{
David Brownellbb2d1c32007-02-20 13:58:19 -08001369 kfree(spi_get_ctldata(spi));
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001370}
1371
David Brownelld1e44d92007-10-16 01:27:46 -07001372static int __init init_queue(struct driver_data *drv_data)
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001373{
1374 INIT_LIST_HEAD(&drv_data->queue);
1375 spin_lock_init(&drv_data->lock);
1376
1377 drv_data->run = QUEUE_STOPPED;
1378 drv_data->busy = 0;
1379
1380 tasklet_init(&drv_data->pump_transfers,
1381 pump_transfers, (unsigned long)drv_data);
1382
1383 INIT_WORK(&drv_data->work, pump_messages);
1384 drv_data->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001385 dev_name(drv_data->master->dev.parent));
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001386 if (drv_data->workqueue == NULL)
1387 return -EBUSY;
1388
1389 return 0;
1390}
1391
1392static int start_queue(struct driver_data *drv_data)
1393{
1394 unsigned long flags;
1395
1396 spin_lock_irqsave(&drv_data->lock, flags);
1397
1398 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1399 spin_unlock_irqrestore(&drv_data->lock, flags);
1400 return -EBUSY;
1401 }
1402
1403 drv_data->run = QUEUE_RUNNING;
1404 drv_data->cur_msg = NULL;
1405 drv_data->cur_transfer = NULL;
1406 drv_data->cur_chip = NULL;
1407 spin_unlock_irqrestore(&drv_data->lock, flags);
1408
1409 queue_work(drv_data->workqueue, &drv_data->work);
1410
1411 return 0;
1412}
1413
1414static int stop_queue(struct driver_data *drv_data)
1415{
1416 unsigned long flags;
1417 unsigned limit = 500;
1418 int status = 0;
1419
1420 spin_lock_irqsave(&drv_data->lock, flags);
1421
1422 /* This is a bit lame, but is optimized for the common execution path.
1423 * A wait_queue on the drv_data->busy could be used, but then the common
1424 * execution path (pump_messages) would be required to call wake_up or
1425 * friends on every SPI message. Do this instead */
1426 drv_data->run = QUEUE_STOPPED;
1427 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1428 spin_unlock_irqrestore(&drv_data->lock, flags);
1429 msleep(10);
1430 spin_lock_irqsave(&drv_data->lock, flags);
1431 }
1432
1433 if (!list_empty(&drv_data->queue) || drv_data->busy)
1434 status = -EBUSY;
1435
1436 spin_unlock_irqrestore(&drv_data->lock, flags);
1437
1438 return status;
1439}
1440
1441static int destroy_queue(struct driver_data *drv_data)
1442{
1443 int status;
1444
1445 status = stop_queue(drv_data);
1446 if (status != 0)
1447 return status;
1448
1449 if (drv_data->workqueue)
1450 destroy_workqueue(drv_data->workqueue);
1451
1452 return 0;
1453}
1454
David Brownelld1e44d92007-10-16 01:27:46 -07001455static int __init spi_imx_probe(struct platform_device *pdev)
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001456{
1457 struct device *dev = &pdev->dev;
1458 struct spi_imx_master *platform_info;
1459 struct spi_master *master;
Julien Boibessot6a010b52008-12-01 13:13:55 -08001460 struct driver_data *drv_data;
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001461 struct resource *res;
1462 int irq, status = 0;
1463
1464 platform_info = dev->platform_data;
1465 if (platform_info == NULL) {
1466 dev_err(&pdev->dev, "probe - no platform data supplied\n");
1467 status = -ENODEV;
1468 goto err_no_pdata;
1469 }
1470
1471 /* Allocate master with space for drv_data */
1472 master = spi_alloc_master(dev, sizeof(struct driver_data));
1473 if (!master) {
1474 dev_err(&pdev->dev, "probe - cannot alloc spi_master\n");
1475 status = -ENOMEM;
1476 goto err_no_mem;
1477 }
1478 drv_data = spi_master_get_devdata(master);
1479 drv_data->master = master;
1480 drv_data->master_info = platform_info;
1481 drv_data->pdev = pdev;
1482
1483 master->bus_num = pdev->id;
1484 master->num_chipselect = platform_info->num_chipselect;
1485 master->cleanup = cleanup;
1486 master->setup = setup;
1487 master->transfer = transfer;
1488
1489 drv_data->dummy_dma_buf = SPI_DUMMY_u32;
1490
Julien Boibessot6a010b52008-12-01 13:13:55 -08001491 drv_data->clk = clk_get(&pdev->dev, "perclk2");
1492 if (IS_ERR(drv_data->clk)) {
1493 dev_err(&pdev->dev, "probe - cannot get clock\n");
1494 status = PTR_ERR(drv_data->clk);
1495 goto err_no_clk;
1496 }
1497 clk_enable(drv_data->clk);
1498
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001499 /* Find and map resources */
1500 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1501 if (!res) {
1502 dev_err(&pdev->dev, "probe - MEM resources not defined\n");
1503 status = -ENODEV;
1504 goto err_no_iores;
1505 }
1506 drv_data->ioarea = request_mem_region(res->start,
1507 res->end - res->start + 1,
1508 pdev->name);
1509 if (drv_data->ioarea == NULL) {
1510 dev_err(&pdev->dev, "probe - cannot reserve region\n");
1511 status = -ENXIO;
1512 goto err_no_iores;
1513 }
1514 drv_data->regs = ioremap(res->start, res->end - res->start + 1);
1515 if (drv_data->regs == NULL) {
1516 dev_err(&pdev->dev, "probe - cannot map IO\n");
1517 status = -ENXIO;
1518 goto err_no_iomap;
1519 }
1520 drv_data->rd_data_phys = (dma_addr_t)res->start;
1521
1522 /* Attach to IRQ */
1523 irq = platform_get_irq(pdev, 0);
1524 if (irq < 0) {
1525 dev_err(&pdev->dev, "probe - IRQ resource not defined\n");
1526 status = -ENODEV;
1527 goto err_no_irqres;
1528 }
Kay Sievers6c7377a2009-03-24 16:38:21 -07001529 status = request_irq(irq, spi_int, IRQF_DISABLED,
1530 dev_name(dev), drv_data);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001531 if (status < 0) {
1532 dev_err(&pdev->dev, "probe - cannot get IRQ (%d)\n", status);
1533 goto err_no_irqres;
1534 }
1535
1536 /* Setup DMA if requested */
1537 drv_data->tx_channel = -1;
1538 drv_data->rx_channel = -1;
1539 if (platform_info->enable_dma) {
1540 /* Get rx DMA channel */
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +01001541 drv_data->rx_channel = imx_dma_request_by_prio("spi_imx_rx",
1542 DMA_PRIO_HIGH);
1543 if (drv_data->rx_channel < 0) {
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001544 dev_err(dev,
1545 "probe - problem (%d) requesting rx channel\n",
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +01001546 drv_data->rx_channel);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001547 goto err_no_rxdma;
1548 } else
1549 imx_dma_setup_handlers(drv_data->rx_channel, NULL,
1550 dma_err_handler, drv_data);
1551
1552 /* Get tx DMA channel */
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +01001553 drv_data->tx_channel = imx_dma_request_by_prio("spi_imx_tx",
1554 DMA_PRIO_MEDIUM);
1555 if (drv_data->tx_channel < 0) {
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001556 dev_err(dev,
1557 "probe - problem (%d) requesting tx channel\n",
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +01001558 drv_data->tx_channel);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001559 imx_dma_free(drv_data->rx_channel);
1560 goto err_no_txdma;
1561 } else
1562 imx_dma_setup_handlers(drv_data->tx_channel,
1563 dma_tx_handler, dma_err_handler,
1564 drv_data);
1565
1566 /* Set request source and burst length for allocated channels */
1567 switch (drv_data->pdev->id) {
1568 case 1:
1569 /* Using SPI1 */
1570 RSSR(drv_data->rx_channel) = DMA_REQ_SPI1_R;
1571 RSSR(drv_data->tx_channel) = DMA_REQ_SPI1_T;
1572 break;
1573 case 2:
1574 /* Using SPI2 */
1575 RSSR(drv_data->rx_channel) = DMA_REQ_SPI2_R;
1576 RSSR(drv_data->tx_channel) = DMA_REQ_SPI2_T;
1577 break;
1578 default:
1579 dev_err(dev, "probe - bad SPI Id\n");
1580 imx_dma_free(drv_data->rx_channel);
1581 imx_dma_free(drv_data->tx_channel);
1582 status = -ENODEV;
1583 goto err_no_devid;
1584 }
1585 BLR(drv_data->rx_channel) = SPI_DMA_BLR;
1586 BLR(drv_data->tx_channel) = SPI_DMA_BLR;
1587 }
1588
1589 /* Load default SPI configuration */
1590 writel(SPI_RESET_START, drv_data->regs + SPI_RESET);
1591 writel(0, drv_data->regs + SPI_RESET);
1592 writel(SPI_DEFAULT_CONTROL, drv_data->regs + SPI_CONTROL);
1593
1594 /* Initial and start queue */
1595 status = init_queue(drv_data);
1596 if (status != 0) {
1597 dev_err(&pdev->dev, "probe - problem initializing queue\n");
1598 goto err_init_queue;
1599 }
1600 status = start_queue(drv_data);
1601 if (status != 0) {
1602 dev_err(&pdev->dev, "probe - problem starting queue\n");
1603 goto err_start_queue;
1604 }
1605
1606 /* Register with the SPI framework */
1607 platform_set_drvdata(pdev, drv_data);
1608 status = spi_register_master(master);
1609 if (status != 0) {
1610 dev_err(&pdev->dev, "probe - problem registering spi master\n");
1611 goto err_spi_register;
1612 }
1613
1614 dev_dbg(dev, "probe succeded\n");
1615 return 0;
1616
1617err_init_queue:
1618err_start_queue:
1619err_spi_register:
1620 destroy_queue(drv_data);
1621
1622err_no_rxdma:
1623err_no_txdma:
1624err_no_devid:
1625 free_irq(irq, drv_data);
1626
1627err_no_irqres:
1628 iounmap(drv_data->regs);
1629
1630err_no_iomap:
1631 release_resource(drv_data->ioarea);
1632 kfree(drv_data->ioarea);
1633
1634err_no_iores:
Julien Boibessot6a010b52008-12-01 13:13:55 -08001635 clk_disable(drv_data->clk);
1636 clk_put(drv_data->clk);
1637
1638err_no_clk:
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001639 spi_master_put(master);
1640
1641err_no_pdata:
1642err_no_mem:
1643 return status;
1644}
1645
David Brownelld1e44d92007-10-16 01:27:46 -07001646static int __exit spi_imx_remove(struct platform_device *pdev)
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001647{
1648 struct driver_data *drv_data = platform_get_drvdata(pdev);
1649 int irq;
1650 int status = 0;
1651
1652 if (!drv_data)
1653 return 0;
1654
1655 tasklet_kill(&drv_data->pump_transfers);
1656
1657 /* Remove the queue */
1658 status = destroy_queue(drv_data);
1659 if (status != 0) {
1660 dev_err(&pdev->dev, "queue remove failed (%d)\n", status);
1661 return status;
1662 }
1663
1664 /* Reset SPI */
1665 writel(SPI_RESET_START, drv_data->regs + SPI_RESET);
1666 writel(0, drv_data->regs + SPI_RESET);
1667
1668 /* Release DMA */
1669 if (drv_data->master_info->enable_dma) {
1670 RSSR(drv_data->rx_channel) = 0;
1671 RSSR(drv_data->tx_channel) = 0;
1672 imx_dma_free(drv_data->tx_channel);
1673 imx_dma_free(drv_data->rx_channel);
1674 }
1675
1676 /* Release IRQ */
1677 irq = platform_get_irq(pdev, 0);
1678 if (irq >= 0)
1679 free_irq(irq, drv_data);
1680
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001681 clk_disable(drv_data->clk);
1682 clk_put(drv_data->clk);
1683
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001684 /* Release map resources */
1685 iounmap(drv_data->regs);
1686 release_resource(drv_data->ioarea);
1687 kfree(drv_data->ioarea);
1688
1689 /* Disconnect from the SPI framework */
1690 spi_unregister_master(drv_data->master);
1691 spi_master_put(drv_data->master);
1692
1693 /* Prevent double remove */
1694 platform_set_drvdata(pdev, NULL);
1695
1696 dev_dbg(&pdev->dev, "remove succeded\n");
1697
1698 return 0;
1699}
1700
1701static void spi_imx_shutdown(struct platform_device *pdev)
1702{
1703 struct driver_data *drv_data = platform_get_drvdata(pdev);
1704
1705 /* Reset SPI */
1706 writel(SPI_RESET_START, drv_data->regs + SPI_RESET);
1707 writel(0, drv_data->regs + SPI_RESET);
1708
1709 dev_dbg(&pdev->dev, "shutdown succeded\n");
1710}
1711
1712#ifdef CONFIG_PM
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001713
1714static int spi_imx_suspend(struct platform_device *pdev, pm_message_t state)
1715{
1716 struct driver_data *drv_data = platform_get_drvdata(pdev);
1717 int status = 0;
1718
1719 status = stop_queue(drv_data);
1720 if (status != 0) {
1721 dev_warn(&pdev->dev, "suspend cannot stop queue\n");
1722 return status;
1723 }
1724
1725 dev_dbg(&pdev->dev, "suspended\n");
1726
1727 return 0;
1728}
1729
1730static int spi_imx_resume(struct platform_device *pdev)
1731{
1732 struct driver_data *drv_data = platform_get_drvdata(pdev);
1733 int status = 0;
1734
1735 /* Start the queue running */
1736 status = start_queue(drv_data);
1737 if (status != 0)
1738 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1739 else
1740 dev_dbg(&pdev->dev, "resumed\n");
1741
1742 return status;
1743}
1744#else
1745#define spi_imx_suspend NULL
1746#define spi_imx_resume NULL
1747#endif /* CONFIG_PM */
1748
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001749/* work with hotplug and coldplug */
1750MODULE_ALIAS("platform:spi_imx");
1751
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001752static struct platform_driver driver = {
1753 .driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001754 .name = "spi_imx",
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001755 .owner = THIS_MODULE,
1756 },
David Brownelld1e44d92007-10-16 01:27:46 -07001757 .remove = __exit_p(spi_imx_remove),
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001758 .shutdown = spi_imx_shutdown,
1759 .suspend = spi_imx_suspend,
1760 .resume = spi_imx_resume,
1761};
1762
1763static int __init spi_imx_init(void)
1764{
David Brownelld1e44d92007-10-16 01:27:46 -07001765 return platform_driver_probe(&driver, spi_imx_probe);
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001766}
1767module_init(spi_imx_init);
1768
1769static void __exit spi_imx_exit(void)
1770{
1771 platform_driver_unregister(&driver);
1772}
1773module_exit(spi_imx_exit);
1774
1775MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
Will Newton8805f232007-12-10 15:49:27 -08001776MODULE_DESCRIPTION("iMX SPI Controller Driver");
Andrea Paterniani69c202a2007-02-12 00:52:39 -08001777MODULE_LICENSE("GPL");