2 * au1550 psc spi controller driver
3 * may work also with au1200, au1210, au1250
4 * will not work on au1000, au1100 and au1500 (no full spi controller there)
6 * Copyright (c) 2006 ATRON electronic GmbH
7 * Author: Jan Nikitenko <jan.nikitenko@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/platform_device.h>
31 #include <linux/resource.h>
32 #include <linux/spi/spi.h>
33 #include <linux/spi/spi_bitbang.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/completion.h>
36 #include <asm/mach-au1x00/au1000.h>
37 #include <asm/mach-au1x00/au1xxx_psc.h>
38 #include <asm/mach-au1x00/au1xxx_dbdma.h>
40 #include <asm/mach-au1x00/au1550_spi.h>
42 static unsigned usedma
= 1;
43 module_param(usedma
, uint
, 0644);
46 #define AU1550_SPI_DEBUG_LOOPBACK
50 #define AU1550_SPI_DBDMA_DESCRIPTORS 1
51 #define AU1550_SPI_DMA_RXTMP_MINSIZE 2048U
54 struct spi_bitbang bitbang
;
56 volatile psc_spi_t __iomem
*regs
;
67 void (*rx_word
)(struct au1550_spi
*hw
);
68 void (*tx_word
)(struct au1550_spi
*hw
);
69 int (*txrx_bufs
)(struct spi_device
*spi
, struct spi_transfer
*t
);
70 irqreturn_t (*irq_callback
)(struct au1550_spi
*hw
);
72 struct completion master_done
;
81 unsigned dma_rx_tmpbuf_size
;
82 u32 dma_rx_tmpbuf_addr
;
84 struct spi_master
*master
;
86 struct au1550_spi_info
*pdata
;
87 struct resource
*ioarea
;
91 /* we use an 8-bit memory device for dma transfers to/from spi fifo */
92 static dbdev_tab_t au1550_spi_mem_dbdev
=
94 .dev_id
= DBDMA_MEM_CHAN
,
95 .dev_flags
= DEV_FLAGS_ANYUSE
|DEV_FLAGS_SYNC
,
98 .dev_physaddr
= 0x00000000,
103 static int ddma_memid
; /* id to above mem dma device */
105 static void au1550_spi_bits_handlers_set(struct au1550_spi
*hw
, int bpw
);
109 * compute BRG and DIV bits to setup spi clock based on main input clock rate
110 * that was specified in platform data structure
111 * according to au1550 datasheet:
112 * psc_tempclk = psc_mainclk / (2 << DIV)
113 * spiclk = psc_tempclk / (2 * (BRG + 1))
114 * BRG valid range is 4..63
115 * DIV valid range is 0..3
117 static u32
au1550_spi_baudcfg(struct au1550_spi
*hw
, unsigned speed_hz
)
119 u32 mainclk_hz
= hw
->pdata
->mainclk_hz
;
122 for (div
= 0; div
< 4; div
++) {
123 brg
= mainclk_hz
/ speed_hz
/ (4 << div
);
124 /* now we have BRG+1 in brg, so count with that */
126 brg
= (4 + 1); /* speed_hz too big */
127 break; /* set lowest brg (div is == 0) */
130 break; /* we have valid brg and div */
133 div
= 3; /* speed_hz too small */
134 brg
= (63 + 1); /* set highest brg and div */
137 return PSC_SPICFG_SET_BAUD(brg
) | PSC_SPICFG_SET_DIV(div
);
140 static inline void au1550_spi_mask_ack_all(struct au1550_spi
*hw
)
142 hw
->regs
->psc_spimsk
=
143 PSC_SPIMSK_MM
| PSC_SPIMSK_RR
| PSC_SPIMSK_RO
144 | PSC_SPIMSK_RU
| PSC_SPIMSK_TR
| PSC_SPIMSK_TO
145 | PSC_SPIMSK_TU
| PSC_SPIMSK_SD
| PSC_SPIMSK_MD
;
148 hw
->regs
->psc_spievent
=
149 PSC_SPIEVNT_MM
| PSC_SPIEVNT_RR
| PSC_SPIEVNT_RO
150 | PSC_SPIEVNT_RU
| PSC_SPIEVNT_TR
| PSC_SPIEVNT_TO
151 | PSC_SPIEVNT_TU
| PSC_SPIEVNT_SD
| PSC_SPIEVNT_MD
;
155 static void au1550_spi_reset_fifos(struct au1550_spi
*hw
)
159 hw
->regs
->psc_spipcr
= PSC_SPIPCR_RC
| PSC_SPIPCR_TC
;
162 pcr
= hw
->regs
->psc_spipcr
;
168 * dma transfers are used for the most common spi word size of 8-bits
169 * we cannot easily change already set up dma channels' width, so if we wanted
170 * dma support for more than 8-bit words (up to 24 bits), we would need to
171 * setup dma channels from scratch on each spi transfer, based on bits_per_word
172 * instead we have pre set up 8 bit dma channels supporting spi 4 to 8 bits
173 * transfers, and 9 to 24 bits spi transfers will be done in pio irq based mode
174 * callbacks to handle dma or pio are set up in au1550_spi_bits_handlers_set()
176 static void au1550_spi_chipsel(struct spi_device
*spi
, int value
)
178 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
179 unsigned cspol
= spi
->mode
& SPI_CS_HIGH
? 1 : 0;
183 case BITBANG_CS_INACTIVE
:
184 if (hw
->pdata
->deactivate_cs
)
185 hw
->pdata
->deactivate_cs(hw
->pdata
, spi
->chip_select
,
189 case BITBANG_CS_ACTIVE
:
190 au1550_spi_bits_handlers_set(hw
, spi
->bits_per_word
);
192 cfg
= hw
->regs
->psc_spicfg
;
194 hw
->regs
->psc_spicfg
= cfg
& ~PSC_SPICFG_DE_ENABLE
;
197 if (spi
->mode
& SPI_CPOL
)
198 cfg
|= PSC_SPICFG_BI
;
200 cfg
&= ~PSC_SPICFG_BI
;
201 if (spi
->mode
& SPI_CPHA
)
202 cfg
&= ~PSC_SPICFG_CDE
;
204 cfg
|= PSC_SPICFG_CDE
;
206 if (spi
->mode
& SPI_LSB_FIRST
)
207 cfg
|= PSC_SPICFG_MLF
;
209 cfg
&= ~PSC_SPICFG_MLF
;
211 if (hw
->usedma
&& spi
->bits_per_word
<= 8)
212 cfg
&= ~PSC_SPICFG_DD_DISABLE
;
214 cfg
|= PSC_SPICFG_DD_DISABLE
;
215 cfg
= PSC_SPICFG_CLR_LEN(cfg
);
216 cfg
|= PSC_SPICFG_SET_LEN(spi
->bits_per_word
);
218 cfg
= PSC_SPICFG_CLR_BAUD(cfg
);
219 cfg
&= ~PSC_SPICFG_SET_DIV(3);
220 cfg
|= au1550_spi_baudcfg(hw
, spi
->max_speed_hz
);
222 hw
->regs
->psc_spicfg
= cfg
| PSC_SPICFG_DE_ENABLE
;
225 stat
= hw
->regs
->psc_spistat
;
227 } while ((stat
& PSC_SPISTAT_DR
) == 0);
229 if (hw
->pdata
->activate_cs
)
230 hw
->pdata
->activate_cs(hw
->pdata
, spi
->chip_select
,
236 static int au1550_spi_setupxfer(struct spi_device
*spi
, struct spi_transfer
*t
)
238 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
242 bpw
= spi
->bits_per_word
;
243 hz
= spi
->max_speed_hz
;
245 if (t
->bits_per_word
)
246 bpw
= t
->bits_per_word
;
251 if (bpw
< 4 || bpw
> 24) {
252 dev_err(&spi
->dev
, "setupxfer: invalid bits_per_word=%d\n",
256 if (hz
> spi
->max_speed_hz
|| hz
> hw
->freq_max
|| hz
< hw
->freq_min
) {
257 dev_err(&spi
->dev
, "setupxfer: clock rate=%d out of range\n",
262 au1550_spi_bits_handlers_set(hw
, spi
->bits_per_word
);
264 cfg
= hw
->regs
->psc_spicfg
;
266 hw
->regs
->psc_spicfg
= cfg
& ~PSC_SPICFG_DE_ENABLE
;
269 if (hw
->usedma
&& bpw
<= 8)
270 cfg
&= ~PSC_SPICFG_DD_DISABLE
;
272 cfg
|= PSC_SPICFG_DD_DISABLE
;
273 cfg
= PSC_SPICFG_CLR_LEN(cfg
);
274 cfg
|= PSC_SPICFG_SET_LEN(bpw
);
276 cfg
= PSC_SPICFG_CLR_BAUD(cfg
);
277 cfg
&= ~PSC_SPICFG_SET_DIV(3);
278 cfg
|= au1550_spi_baudcfg(hw
, hz
);
280 hw
->regs
->psc_spicfg
= cfg
;
283 if (cfg
& PSC_SPICFG_DE_ENABLE
) {
285 stat
= hw
->regs
->psc_spistat
;
287 } while ((stat
& PSC_SPISTAT_DR
) == 0);
290 au1550_spi_reset_fifos(hw
);
291 au1550_spi_mask_ack_all(hw
);
295 static int au1550_spi_setup(struct spi_device
*spi
)
297 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
299 if (spi
->bits_per_word
< 4 || spi
->bits_per_word
> 24) {
300 dev_err(&spi
->dev
, "setup: invalid bits_per_word=%d\n",
305 if (spi
->max_speed_hz
== 0)
306 spi
->max_speed_hz
= hw
->freq_max
;
307 if (spi
->max_speed_hz
> hw
->freq_max
308 || spi
->max_speed_hz
< hw
->freq_min
)
311 * NOTE: cannot change speed and other hw settings immediately,
312 * otherwise sharing of spi bus is not possible,
313 * so do not call setupxfer(spi, NULL) here
319 * for dma spi transfers, we have to setup rx channel, otherwise there is
320 * no reliable way how to recognize that spi transfer is done
321 * dma complete callbacks are called before real spi transfer is finished
322 * and if only tx dma channel is set up (and rx fifo overflow event masked)
323 * spi master done event irq is not generated unless rx fifo is empty (emptied)
324 * so we need rx tmp buffer to use for rx dma if user does not provide one
326 static int au1550_spi_dma_rxtmp_alloc(struct au1550_spi
*hw
, unsigned size
)
328 hw
->dma_rx_tmpbuf
= kmalloc(size
, GFP_KERNEL
);
329 if (!hw
->dma_rx_tmpbuf
)
331 hw
->dma_rx_tmpbuf_size
= size
;
332 hw
->dma_rx_tmpbuf_addr
= dma_map_single(hw
->dev
, hw
->dma_rx_tmpbuf
,
333 size
, DMA_FROM_DEVICE
);
334 if (dma_mapping_error(hw
->dev
, hw
->dma_rx_tmpbuf_addr
)) {
335 kfree(hw
->dma_rx_tmpbuf
);
336 hw
->dma_rx_tmpbuf
= 0;
337 hw
->dma_rx_tmpbuf_size
= 0;
343 static void au1550_spi_dma_rxtmp_free(struct au1550_spi
*hw
)
345 dma_unmap_single(hw
->dev
, hw
->dma_rx_tmpbuf_addr
,
346 hw
->dma_rx_tmpbuf_size
, DMA_FROM_DEVICE
);
347 kfree(hw
->dma_rx_tmpbuf
);
348 hw
->dma_rx_tmpbuf
= 0;
349 hw
->dma_rx_tmpbuf_size
= 0;
352 static int au1550_spi_dma_txrxb(struct spi_device
*spi
, struct spi_transfer
*t
)
354 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
355 dma_addr_t dma_tx_addr
;
356 dma_addr_t dma_rx_addr
;
365 dma_tx_addr
= t
->tx_dma
;
366 dma_rx_addr
= t
->rx_dma
;
369 * check if buffers are already dma mapped, map them otherwise:
370 * - first map the TX buffer, so cache data gets written to memory
371 * - then map the RX buffer, so that cache entries (with
372 * soon-to-be-stale data) get removed
373 * use rx buffer in place of tx if tx buffer was not provided
374 * use temp rx buffer (preallocated or realloc to fit) for rx dma
377 if (t
->tx_dma
== 0) { /* if DMA_ADDR_INVALID, map it */
378 dma_tx_addr
= dma_map_single(hw
->dev
,
380 t
->len
, DMA_TO_DEVICE
);
381 if (dma_mapping_error(hw
->dev
, dma_tx_addr
))
382 dev_err(hw
->dev
, "tx dma map error\n");
387 if (t
->rx_dma
== 0) { /* if DMA_ADDR_INVALID, map it */
388 dma_rx_addr
= dma_map_single(hw
->dev
,
390 t
->len
, DMA_FROM_DEVICE
);
391 if (dma_mapping_error(hw
->dev
, dma_rx_addr
))
392 dev_err(hw
->dev
, "rx dma map error\n");
395 if (t
->len
> hw
->dma_rx_tmpbuf_size
) {
398 au1550_spi_dma_rxtmp_free(hw
);
399 ret
= au1550_spi_dma_rxtmp_alloc(hw
, max(t
->len
,
400 AU1550_SPI_DMA_RXTMP_MINSIZE
));
404 hw
->rx
= hw
->dma_rx_tmpbuf
;
405 dma_rx_addr
= hw
->dma_rx_tmpbuf_addr
;
406 dma_sync_single_for_device(hw
->dev
, dma_rx_addr
,
407 t
->len
, DMA_FROM_DEVICE
);
411 dma_sync_single_for_device(hw
->dev
, dma_rx_addr
,
412 t
->len
, DMA_BIDIRECTIONAL
);
416 /* put buffers on the ring */
417 res
= au1xxx_dbdma_put_dest(hw
->dma_rx_ch
, virt_to_phys(hw
->rx
),
418 t
->len
, DDMA_FLAGS_IE
);
420 dev_err(hw
->dev
, "rx dma put dest error\n");
422 res
= au1xxx_dbdma_put_source(hw
->dma_tx_ch
, virt_to_phys(hw
->tx
),
423 t
->len
, DDMA_FLAGS_IE
);
425 dev_err(hw
->dev
, "tx dma put source error\n");
427 au1xxx_dbdma_start(hw
->dma_rx_ch
);
428 au1xxx_dbdma_start(hw
->dma_tx_ch
);
430 /* by default enable nearly all events interrupt */
431 hw
->regs
->psc_spimsk
= PSC_SPIMSK_SD
;
434 /* start the transfer */
435 hw
->regs
->psc_spipcr
= PSC_SPIPCR_MS
;
438 wait_for_completion(&hw
->master_done
);
440 au1xxx_dbdma_stop(hw
->dma_tx_ch
);
441 au1xxx_dbdma_stop(hw
->dma_rx_ch
);
444 /* using the temporal preallocated and premapped buffer */
445 dma_sync_single_for_cpu(hw
->dev
, dma_rx_addr
, t
->len
,
448 /* unmap buffers if mapped above */
449 if (t
->rx_buf
&& t
->rx_dma
== 0 )
450 dma_unmap_single(hw
->dev
, dma_rx_addr
, t
->len
,
452 if (t
->tx_buf
&& t
->tx_dma
== 0 )
453 dma_unmap_single(hw
->dev
, dma_tx_addr
, t
->len
,
456 return hw
->rx_count
< hw
->tx_count
? hw
->rx_count
: hw
->tx_count
;
459 static irqreturn_t
au1550_spi_dma_irq_callback(struct au1550_spi
*hw
)
463 stat
= hw
->regs
->psc_spistat
;
464 evnt
= hw
->regs
->psc_spievent
;
466 if ((stat
& PSC_SPISTAT_DI
) == 0) {
467 dev_err(hw
->dev
, "Unexpected IRQ!\n");
471 if ((evnt
& (PSC_SPIEVNT_MM
| PSC_SPIEVNT_RO
472 | PSC_SPIEVNT_RU
| PSC_SPIEVNT_TO
473 | PSC_SPIEVNT_TU
| PSC_SPIEVNT_SD
))
476 * due to an spi error we consider transfer as done,
477 * so mask all events until before next transfer start
478 * and stop the possibly running dma immediately
480 au1550_spi_mask_ack_all(hw
);
481 au1xxx_dbdma_stop(hw
->dma_rx_ch
);
482 au1xxx_dbdma_stop(hw
->dma_tx_ch
);
484 /* get number of transferred bytes */
485 hw
->rx_count
= hw
->len
- au1xxx_get_dma_residue(hw
->dma_rx_ch
);
486 hw
->tx_count
= hw
->len
- au1xxx_get_dma_residue(hw
->dma_tx_ch
);
488 au1xxx_dbdma_reset(hw
->dma_rx_ch
);
489 au1xxx_dbdma_reset(hw
->dma_tx_ch
);
490 au1550_spi_reset_fifos(hw
);
492 if (evnt
== PSC_SPIEVNT_RO
)
494 "dma transfer: receive FIFO overflow!\n");
497 "dma transfer: unexpected SPI error "
498 "(event=0x%x stat=0x%x)!\n", evnt
, stat
);
500 complete(&hw
->master_done
);
504 if ((evnt
& PSC_SPIEVNT_MD
) != 0) {
505 /* transfer completed successfully */
506 au1550_spi_mask_ack_all(hw
);
507 hw
->rx_count
= hw
->len
;
508 hw
->tx_count
= hw
->len
;
509 complete(&hw
->master_done
);
515 /* routines to handle different word sizes in pio mode */
516 #define AU1550_SPI_RX_WORD(size, mask) \
517 static void au1550_spi_rx_word_##size(struct au1550_spi *hw) \
519 u32 fifoword = hw->regs->psc_spitxrx & (u32)(mask); \
522 *(u##size *)hw->rx = (u##size)fifoword; \
523 hw->rx += (size) / 8; \
525 hw->rx_count += (size) / 8; \
528 #define AU1550_SPI_TX_WORD(size, mask) \
529 static void au1550_spi_tx_word_##size(struct au1550_spi *hw) \
533 fifoword = *(u##size *)hw->tx & (u32)(mask); \
534 hw->tx += (size) / 8; \
536 hw->tx_count += (size) / 8; \
537 if (hw->tx_count >= hw->len) \
538 fifoword |= PSC_SPITXRX_LC; \
539 hw->regs->psc_spitxrx = fifoword; \
543 AU1550_SPI_RX_WORD(8,0xff)
544 AU1550_SPI_RX_WORD(16,0xffff)
545 AU1550_SPI_RX_WORD(32,0xffffff)
546 AU1550_SPI_TX_WORD(8,0xff)
547 AU1550_SPI_TX_WORD(16,0xffff)
548 AU1550_SPI_TX_WORD(32,0xffffff)
550 static int au1550_spi_pio_txrxb(struct spi_device
*spi
, struct spi_transfer
*t
)
553 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
561 /* by default enable nearly all events after filling tx fifo */
562 mask
= PSC_SPIMSK_SD
;
564 /* fill the transmit FIFO */
565 while (hw
->tx_count
< hw
->len
) {
569 if (hw
->tx_count
>= hw
->len
) {
570 /* mask tx fifo request interrupt as we are done */
571 mask
|= PSC_SPIMSK_TR
;
574 stat
= hw
->regs
->psc_spistat
;
576 if (stat
& PSC_SPISTAT_TF
)
580 /* enable event interrupts */
581 hw
->regs
->psc_spimsk
= mask
;
584 /* start the transfer */
585 hw
->regs
->psc_spipcr
= PSC_SPIPCR_MS
;
588 wait_for_completion(&hw
->master_done
);
590 return hw
->rx_count
< hw
->tx_count
? hw
->rx_count
: hw
->tx_count
;
593 static irqreturn_t
au1550_spi_pio_irq_callback(struct au1550_spi
*hw
)
598 stat
= hw
->regs
->psc_spistat
;
599 evnt
= hw
->regs
->psc_spievent
;
601 if ((stat
& PSC_SPISTAT_DI
) == 0) {
602 dev_err(hw
->dev
, "Unexpected IRQ!\n");
606 if ((evnt
& (PSC_SPIEVNT_MM
| PSC_SPIEVNT_RO
607 | PSC_SPIEVNT_RU
| PSC_SPIEVNT_TO
611 * due to an error we consider transfer as done,
612 * so mask all events until before next transfer start
614 au1550_spi_mask_ack_all(hw
);
615 au1550_spi_reset_fifos(hw
);
617 "pio transfer: unexpected SPI error "
618 "(event=0x%x stat=0x%x)!\n", evnt
, stat
);
619 complete(&hw
->master_done
);
624 * while there is something to read from rx fifo
625 * or there is a space to write to tx fifo:
629 stat
= hw
->regs
->psc_spistat
;
633 * Take care to not let the Rx FIFO overflow.
635 * We only write a byte if we have read one at least. Initially,
636 * the write fifo is full, so we should read from the read fifo
638 * In case we miss a word from the read fifo, we should get a
639 * RO event and should back out.
641 if (!(stat
& PSC_SPISTAT_RE
) && hw
->rx_count
< hw
->len
) {
645 if (!(stat
& PSC_SPISTAT_TF
) && hw
->tx_count
< hw
->len
)
650 hw
->regs
->psc_spievent
= PSC_SPIEVNT_RR
| PSC_SPIEVNT_TR
;
654 * Restart the SPI transmission in case of a transmit underflow.
655 * This seems to work despite the notes in the Au1550 data book
656 * of Figure 8-4 with flowchart for SPI master operation:
658 * """Note 1: An XFR Error Interrupt occurs, unless masked,
659 * for any of the following events: Tx FIFO Underflow,
660 * Rx FIFO Overflow, or Multiple-master Error
661 * Note 2: In case of a Tx Underflow Error, all zeroes are
664 * By simply restarting the spi transfer on Tx Underflow Error,
665 * we assume that spi transfer was paused instead of zeroes
666 * transmittion mentioned in the Note 2 of Au1550 data book.
668 if (evnt
& PSC_SPIEVNT_TU
) {
669 hw
->regs
->psc_spievent
= PSC_SPIEVNT_TU
| PSC_SPIEVNT_MD
;
671 hw
->regs
->psc_spipcr
= PSC_SPIPCR_MS
;
675 if (hw
->rx_count
>= hw
->len
) {
676 /* transfer completed successfully */
677 au1550_spi_mask_ack_all(hw
);
678 complete(&hw
->master_done
);
683 static int au1550_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
685 struct au1550_spi
*hw
= spi_master_get_devdata(spi
->master
);
686 return hw
->txrx_bufs(spi
, t
);
689 static irqreturn_t
au1550_spi_irq(int irq
, void *dev
)
691 struct au1550_spi
*hw
= dev
;
692 return hw
->irq_callback(hw
);
695 static void au1550_spi_bits_handlers_set(struct au1550_spi
*hw
, int bpw
)
699 hw
->txrx_bufs
= &au1550_spi_dma_txrxb
;
700 hw
->irq_callback
= &au1550_spi_dma_irq_callback
;
702 hw
->rx_word
= &au1550_spi_rx_word_8
;
703 hw
->tx_word
= &au1550_spi_tx_word_8
;
704 hw
->txrx_bufs
= &au1550_spi_pio_txrxb
;
705 hw
->irq_callback
= &au1550_spi_pio_irq_callback
;
707 } else if (bpw
<= 16) {
708 hw
->rx_word
= &au1550_spi_rx_word_16
;
709 hw
->tx_word
= &au1550_spi_tx_word_16
;
710 hw
->txrx_bufs
= &au1550_spi_pio_txrxb
;
711 hw
->irq_callback
= &au1550_spi_pio_irq_callback
;
713 hw
->rx_word
= &au1550_spi_rx_word_32
;
714 hw
->tx_word
= &au1550_spi_tx_word_32
;
715 hw
->txrx_bufs
= &au1550_spi_pio_txrxb
;
716 hw
->irq_callback
= &au1550_spi_pio_irq_callback
;
720 static void __init
au1550_spi_setup_psc_as_spi(struct au1550_spi
*hw
)
724 /* set up the PSC for SPI mode */
725 hw
->regs
->psc_ctrl
= PSC_CTRL_DISABLE
;
727 hw
->regs
->psc_sel
= PSC_SEL_PS_SPIMODE
;
730 hw
->regs
->psc_spicfg
= 0;
733 hw
->regs
->psc_ctrl
= PSC_CTRL_ENABLE
;
737 stat
= hw
->regs
->psc_spistat
;
739 } while ((stat
& PSC_SPISTAT_SR
) == 0);
742 cfg
= hw
->usedma
? 0 : PSC_SPICFG_DD_DISABLE
;
743 cfg
|= PSC_SPICFG_SET_LEN(8);
744 cfg
|= PSC_SPICFG_RT_FIFO8
| PSC_SPICFG_TT_FIFO8
;
745 /* use minimal allowed brg and div values as initial setting: */
746 cfg
|= PSC_SPICFG_SET_BAUD(4) | PSC_SPICFG_SET_DIV(0);
748 #ifdef AU1550_SPI_DEBUG_LOOPBACK
749 cfg
|= PSC_SPICFG_LB
;
752 hw
->regs
->psc_spicfg
= cfg
;
755 au1550_spi_mask_ack_all(hw
);
757 hw
->regs
->psc_spicfg
|= PSC_SPICFG_DE_ENABLE
;
761 stat
= hw
->regs
->psc_spistat
;
763 } while ((stat
& PSC_SPISTAT_DR
) == 0);
765 au1550_spi_reset_fifos(hw
);
769 static int __init
au1550_spi_probe(struct platform_device
*pdev
)
771 struct au1550_spi
*hw
;
772 struct spi_master
*master
;
776 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct au1550_spi
));
777 if (master
== NULL
) {
778 dev_err(&pdev
->dev
, "No memory for spi_master\n");
783 /* the spi->mode bits understood by this driver: */
784 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
| SPI_LSB_FIRST
;
786 hw
= spi_master_get_devdata(master
);
788 hw
->master
= spi_master_get(master
);
789 hw
->pdata
= pdev
->dev
.platform_data
;
790 hw
->dev
= &pdev
->dev
;
792 if (hw
->pdata
== NULL
) {
793 dev_err(&pdev
->dev
, "No platform data supplied\n");
798 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
800 dev_err(&pdev
->dev
, "no IRQ\n");
807 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
809 hw
->dma_tx_id
= r
->start
;
810 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
812 hw
->dma_rx_id
= r
->start
;
813 if (usedma
&& ddma_memid
) {
814 if (pdev
->dev
.dma_mask
== NULL
)
815 dev_warn(&pdev
->dev
, "no dma mask\n");
822 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
824 dev_err(&pdev
->dev
, "no mmio resource\n");
829 hw
->ioarea
= request_mem_region(r
->start
, sizeof(psc_spi_t
),
832 dev_err(&pdev
->dev
, "Cannot reserve iomem region\n");
837 hw
->regs
= (psc_spi_t __iomem
*)ioremap(r
->start
, sizeof(psc_spi_t
));
839 dev_err(&pdev
->dev
, "cannot ioremap\n");
844 platform_set_drvdata(pdev
, hw
);
846 init_completion(&hw
->master_done
);
848 hw
->bitbang
.master
= hw
->master
;
849 hw
->bitbang
.setup_transfer
= au1550_spi_setupxfer
;
850 hw
->bitbang
.chipselect
= au1550_spi_chipsel
;
851 hw
->bitbang
.master
->setup
= au1550_spi_setup
;
852 hw
->bitbang
.txrx_bufs
= au1550_spi_txrx_bufs
;
855 hw
->dma_tx_ch
= au1xxx_dbdma_chan_alloc(ddma_memid
,
856 hw
->dma_tx_id
, NULL
, (void *)hw
);
857 if (hw
->dma_tx_ch
== 0) {
859 "Cannot allocate tx dma channel\n");
863 au1xxx_dbdma_set_devwidth(hw
->dma_tx_ch
, 8);
864 if (au1xxx_dbdma_ring_alloc(hw
->dma_tx_ch
,
865 AU1550_SPI_DBDMA_DESCRIPTORS
) == 0) {
867 "Cannot allocate tx dma descriptors\n");
869 goto err_no_txdma_descr
;
873 hw
->dma_rx_ch
= au1xxx_dbdma_chan_alloc(hw
->dma_rx_id
,
874 ddma_memid
, NULL
, (void *)hw
);
875 if (hw
->dma_rx_ch
== 0) {
877 "Cannot allocate rx dma channel\n");
881 au1xxx_dbdma_set_devwidth(hw
->dma_rx_ch
, 8);
882 if (au1xxx_dbdma_ring_alloc(hw
->dma_rx_ch
,
883 AU1550_SPI_DBDMA_DESCRIPTORS
) == 0) {
885 "Cannot allocate rx dma descriptors\n");
887 goto err_no_rxdma_descr
;
890 err
= au1550_spi_dma_rxtmp_alloc(hw
,
891 AU1550_SPI_DMA_RXTMP_MINSIZE
);
894 "Cannot allocate initial rx dma tmp buffer\n");
895 goto err_dma_rxtmp_alloc
;
899 au1550_spi_bits_handlers_set(hw
, 8);
901 err
= request_irq(hw
->irq
, au1550_spi_irq
, 0, pdev
->name
, hw
);
903 dev_err(&pdev
->dev
, "Cannot claim IRQ\n");
907 master
->bus_num
= pdev
->id
;
908 master
->num_chipselect
= hw
->pdata
->num_chipselect
;
911 * precompute valid range for spi freq - from au1550 datasheet:
912 * psc_tempclk = psc_mainclk / (2 << DIV)
913 * spiclk = psc_tempclk / (2 * (BRG + 1))
914 * BRG valid range is 4..63
915 * DIV valid range is 0..3
916 * round the min and max frequencies to values that would still
917 * produce valid brg and div
920 int min_div
= (2 << 0) * (2 * (4 + 1));
921 int max_div
= (2 << 3) * (2 * (63 + 1));
922 hw
->freq_max
= hw
->pdata
->mainclk_hz
/ min_div
;
923 hw
->freq_min
= hw
->pdata
->mainclk_hz
/ (max_div
+ 1) + 1;
926 au1550_spi_setup_psc_as_spi(hw
);
928 err
= spi_bitbang_start(&hw
->bitbang
);
930 dev_err(&pdev
->dev
, "Failed to register SPI master\n");
935 "spi master registered: bus_num=%d num_chipselect=%d\n",
936 master
->bus_num
, master
->num_chipselect
);
941 free_irq(hw
->irq
, hw
);
944 au1550_spi_dma_rxtmp_free(hw
);
949 au1xxx_dbdma_chan_free(hw
->dma_rx_ch
);
954 au1xxx_dbdma_chan_free(hw
->dma_tx_ch
);
957 iounmap((void __iomem
*)hw
->regs
);
960 release_resource(hw
->ioarea
);
965 spi_master_put(hw
->master
);
971 static int __exit
au1550_spi_remove(struct platform_device
*pdev
)
973 struct au1550_spi
*hw
= platform_get_drvdata(pdev
);
975 dev_info(&pdev
->dev
, "spi master remove: bus_num=%d\n",
976 hw
->master
->bus_num
);
978 spi_bitbang_stop(&hw
->bitbang
);
979 free_irq(hw
->irq
, hw
);
980 iounmap((void __iomem
*)hw
->regs
);
981 release_resource(hw
->ioarea
);
985 au1550_spi_dma_rxtmp_free(hw
);
986 au1xxx_dbdma_chan_free(hw
->dma_rx_ch
);
987 au1xxx_dbdma_chan_free(hw
->dma_tx_ch
);
990 platform_set_drvdata(pdev
, NULL
);
992 spi_master_put(hw
->master
);
996 /* work with hotplug and coldplug */
997 MODULE_ALIAS("platform:au1550-spi");
999 static struct platform_driver au1550_spi_drv
= {
1000 .remove
= __exit_p(au1550_spi_remove
),
1002 .name
= "au1550-spi",
1003 .owner
= THIS_MODULE
,
1007 static int __init
au1550_spi_init(void)
1010 * create memory device with 8 bits dev_devwidth
1011 * needed for proper byte ordering to spi fifo
1014 ddma_memid
= au1xxx_ddma_add_device(&au1550_spi_mem_dbdev
);
1016 printk(KERN_ERR
"au1550-spi: cannot add memory"
1019 return platform_driver_probe(&au1550_spi_drv
, au1550_spi_probe
);
1021 module_init(au1550_spi_init
);
1023 static void __exit
au1550_spi_exit(void)
1025 if (usedma
&& ddma_memid
)
1026 au1xxx_ddma_del_device(ddma_memid
);
1027 platform_driver_unregister(&au1550_spi_drv
);
1029 module_exit(au1550_spi_exit
);
1031 MODULE_DESCRIPTION("Au1550 PSC SPI Driver");
1032 MODULE_AUTHOR("Jan Nikitenko <jan.nikitenko@gmail.com>");
1033 MODULE_LICENSE("GPL");