2 * Designware SPI core controller driver (refer pxa2xx_spi.c)
4 * Copyright (c) 2009, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/highmem.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/spi/spi.h>
23 #include <linux/gpio.h>
27 #ifdef CONFIG_DEBUG_FS
28 #include <linux/debugfs.h>
31 /* Slave spi_dev related */
34 u8 cs
; /* chip select pin */
35 u8 n_bytes
; /* current is a 1/2/4 byte op */
36 u8 tmode
; /* TR/TO/RO/EEPROM */
37 u8 type
; /* SPI/SSP/MicroWire */
39 u8 poll_mode
; /* 1 means use poll mode */
46 u16 clk_div
; /* baud rate divider */
47 u32 speed_hz
; /* baud rate */
48 void (*cs_control
)(u32 command
);
51 #ifdef CONFIG_DEBUG_FS
52 #define SPI_REGS_BUFSIZE 1024
53 static ssize_t
dw_spi_show_regs(struct file
*file
, char __user
*user_buf
,
54 size_t count
, loff_t
*ppos
)
56 struct dw_spi
*dws
= file
->private_data
;
61 buf
= kzalloc(SPI_REGS_BUFSIZE
, GFP_KERNEL
);
65 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
66 "%s registers:\n", dev_name(&dws
->master
->dev
));
67 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
68 "=================================\n");
69 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
70 "CTRL0: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_CTRL0
));
71 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
72 "CTRL1: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_CTRL1
));
73 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
74 "SSIENR: \t0x%08x\n", dw_readl(dws
, DW_SPI_SSIENR
));
75 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
76 "SER: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_SER
));
77 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
78 "BAUDR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_BAUDR
));
79 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
80 "TXFTLR: \t0x%08x\n", dw_readl(dws
, DW_SPI_TXFLTR
));
81 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
82 "RXFTLR: \t0x%08x\n", dw_readl(dws
, DW_SPI_RXFLTR
));
83 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
84 "TXFLR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_TXFLR
));
85 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
86 "RXFLR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_RXFLR
));
87 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
88 "SR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_SR
));
89 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
90 "IMR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_IMR
));
91 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
92 "ISR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_ISR
));
93 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
94 "DMACR: \t\t0x%08x\n", dw_readl(dws
, DW_SPI_DMACR
));
95 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
96 "DMATDLR: \t0x%08x\n", dw_readl(dws
, DW_SPI_DMATDLR
));
97 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
98 "DMARDLR: \t0x%08x\n", dw_readl(dws
, DW_SPI_DMARDLR
));
99 len
+= snprintf(buf
+ len
, SPI_REGS_BUFSIZE
- len
,
100 "=================================\n");
102 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
107 static const struct file_operations dw_spi_regs_ops
= {
108 .owner
= THIS_MODULE
,
110 .read
= dw_spi_show_regs
,
111 .llseek
= default_llseek
,
114 static int dw_spi_debugfs_init(struct dw_spi
*dws
)
116 dws
->debugfs
= debugfs_create_dir("dw_spi", NULL
);
120 debugfs_create_file("registers", S_IFREG
| S_IRUGO
,
121 dws
->debugfs
, (void *)dws
, &dw_spi_regs_ops
);
125 static void dw_spi_debugfs_remove(struct dw_spi
*dws
)
127 debugfs_remove_recursive(dws
->debugfs
);
131 static inline int dw_spi_debugfs_init(struct dw_spi
*dws
)
136 static inline void dw_spi_debugfs_remove(struct dw_spi
*dws
)
139 #endif /* CONFIG_DEBUG_FS */
141 static void dw_spi_set_cs(struct spi_device
*spi
, bool enable
)
143 struct dw_spi
*dws
= spi_master_get_devdata(spi
->master
);
144 struct chip_data
*chip
= spi_get_ctldata(spi
);
146 /* Chip select logic is inverted from spi_set_cs() */
147 if (chip
&& chip
->cs_control
)
148 chip
->cs_control(!enable
);
151 dw_writel(dws
, DW_SPI_SER
, BIT(spi
->chip_select
));
154 /* Return the max entries we can fill into tx fifo */
155 static inline u32
tx_max(struct dw_spi
*dws
)
157 u32 tx_left
, tx_room
, rxtx_gap
;
159 tx_left
= (dws
->tx_end
- dws
->tx
) / dws
->n_bytes
;
160 tx_room
= dws
->fifo_len
- dw_readl(dws
, DW_SPI_TXFLR
);
163 * Another concern is about the tx/rx mismatch, we
164 * though to use (dws->fifo_len - rxflr - txflr) as
165 * one maximum value for tx, but it doesn't cover the
166 * data which is out of tx/rx fifo and inside the
167 * shift registers. So a control from sw point of
170 rxtx_gap
= ((dws
->rx_end
- dws
->rx
) - (dws
->tx_end
- dws
->tx
))
173 return min3(tx_left
, tx_room
, (u32
) (dws
->fifo_len
- rxtx_gap
));
176 /* Return the max entries we should read out of rx fifo */
177 static inline u32
rx_max(struct dw_spi
*dws
)
179 u32 rx_left
= (dws
->rx_end
- dws
->rx
) / dws
->n_bytes
;
181 return min_t(u32
, rx_left
, dw_readl(dws
, DW_SPI_RXFLR
));
184 static void dw_writer(struct dw_spi
*dws
)
186 u32 max
= tx_max(dws
);
190 /* Set the tx word if the transfer's original "tx" is not null */
191 if (dws
->tx_end
- dws
->len
) {
192 if (dws
->n_bytes
== 1)
193 txw
= *(u8
*)(dws
->tx
);
195 txw
= *(u16
*)(dws
->tx
);
197 dw_writel(dws
, DW_SPI_DR
, txw
);
198 dws
->tx
+= dws
->n_bytes
;
202 static void dw_reader(struct dw_spi
*dws
)
204 u32 max
= rx_max(dws
);
208 rxw
= dw_readl(dws
, DW_SPI_DR
);
209 /* Care rx only if the transfer's original "rx" is not null */
210 if (dws
->rx_end
- dws
->len
) {
211 if (dws
->n_bytes
== 1)
212 *(u8
*)(dws
->rx
) = rxw
;
214 *(u16
*)(dws
->rx
) = rxw
;
216 dws
->rx
+= dws
->n_bytes
;
220 static void int_error_stop(struct dw_spi
*dws
, const char *msg
)
224 dev_err(&dws
->master
->dev
, "%s\n", msg
);
225 dws
->master
->cur_msg
->status
= -EIO
;
226 spi_finalize_current_transfer(dws
->master
);
229 static irqreturn_t
interrupt_transfer(struct dw_spi
*dws
)
231 u16 irq_status
= dw_readl(dws
, DW_SPI_ISR
);
234 if (irq_status
& (SPI_INT_TXOI
| SPI_INT_RXOI
| SPI_INT_RXUI
)) {
235 dw_readl(dws
, DW_SPI_ICR
);
236 int_error_stop(dws
, "interrupt_transfer: fifo overrun/underrun");
241 if (dws
->rx_end
== dws
->rx
) {
242 spi_mask_intr(dws
, SPI_INT_TXEI
);
243 spi_finalize_current_transfer(dws
->master
);
246 if (irq_status
& SPI_INT_TXEI
) {
247 spi_mask_intr(dws
, SPI_INT_TXEI
);
249 /* Enable TX irq always, it will be disabled when RX finished */
250 spi_umask_intr(dws
, SPI_INT_TXEI
);
256 static irqreturn_t
dw_spi_irq(int irq
, void *dev_id
)
258 struct spi_master
*master
= dev_id
;
259 struct dw_spi
*dws
= spi_master_get_devdata(master
);
260 u16 irq_status
= dw_readl(dws
, DW_SPI_ISR
) & 0x3f;
265 if (!master
->cur_msg
) {
266 spi_mask_intr(dws
, SPI_INT_TXEI
);
270 return dws
->transfer_handler(dws
);
273 /* Must be called inside pump_transfers() */
274 static int poll_transfer(struct dw_spi
*dws
)
280 } while (dws
->rx_end
> dws
->rx
);
285 static int dw_spi_transfer_one(struct spi_master
*master
,
286 struct spi_device
*spi
, struct spi_transfer
*transfer
)
288 struct dw_spi
*dws
= spi_master_get_devdata(master
);
289 struct chip_data
*chip
= spi_get_ctldata(spi
);
298 dws
->n_bytes
= chip
->n_bytes
;
299 dws
->dma_width
= chip
->dma_width
;
301 dws
->tx
= (void *)transfer
->tx_buf
;
302 dws
->tx_end
= dws
->tx
+ transfer
->len
;
303 dws
->rx
= transfer
->rx_buf
;
304 dws
->rx_end
= dws
->rx
+ transfer
->len
;
305 dws
->len
= transfer
->len
;
307 spi_enable_chip(dws
, 0);
311 /* Handle per transfer options for bpw and speed */
312 if (transfer
->speed_hz
) {
313 speed
= chip
->speed_hz
;
315 if ((transfer
->speed_hz
!= speed
) || !chip
->clk_div
) {
316 speed
= transfer
->speed_hz
;
318 /* clk_div doesn't support odd number */
319 clk_div
= (dws
->max_freq
/ speed
+ 1) & 0xfffe;
321 chip
->speed_hz
= speed
;
322 chip
->clk_div
= clk_div
;
324 spi_set_clk(dws
, chip
->clk_div
);
327 if (transfer
->bits_per_word
) {
328 if (transfer
->bits_per_word
== 8) {
331 } else if (transfer
->bits_per_word
== 16) {
335 cr0
= (transfer
->bits_per_word
- 1)
336 | (chip
->type
<< SPI_FRF_OFFSET
)
337 | (spi
->mode
<< SPI_MODE_OFFSET
)
338 | (chip
->tmode
<< SPI_TMOD_OFFSET
);
342 * Adjust transfer mode if necessary. Requires platform dependent
343 * chipselect mechanism.
345 if (chip
->cs_control
) {
346 if (dws
->rx
&& dws
->tx
)
347 chip
->tmode
= SPI_TMOD_TR
;
349 chip
->tmode
= SPI_TMOD_RO
;
351 chip
->tmode
= SPI_TMOD_TO
;
353 cr0
&= ~SPI_TMOD_MASK
;
354 cr0
|= (chip
->tmode
<< SPI_TMOD_OFFSET
);
357 dw_writel(dws
, DW_SPI_CTRL0
, cr0
);
359 /* Check if current transfer is a DMA transaction */
360 if (master
->can_dma
&& master
->can_dma(master
, spi
, transfer
))
361 dws
->dma_mapped
= master
->cur_msg_mapped
;
363 /* For poll mode just disable all interrupts */
364 spi_mask_intr(dws
, 0xff);
368 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
370 if (dws
->dma_mapped
) {
371 ret
= dws
->dma_ops
->dma_setup(dws
, transfer
);
373 spi_enable_chip(dws
, 1);
376 } else if (!chip
->poll_mode
) {
377 txlevel
= min_t(u16
, dws
->fifo_len
/ 2, dws
->len
/ dws
->n_bytes
);
378 dw_writel(dws
, DW_SPI_TXFLTR
, txlevel
);
380 /* Set the interrupt mask */
381 imask
|= SPI_INT_TXEI
| SPI_INT_TXOI
|
382 SPI_INT_RXUI
| SPI_INT_RXOI
;
383 spi_umask_intr(dws
, imask
);
385 dws
->transfer_handler
= interrupt_transfer
;
388 spi_enable_chip(dws
, 1);
390 if (dws
->dma_mapped
) {
391 ret
= dws
->dma_ops
->dma_transfer(dws
, transfer
);
397 return poll_transfer(dws
);
402 static void dw_spi_handle_err(struct spi_master
*master
,
403 struct spi_message
*msg
)
405 struct dw_spi
*dws
= spi_master_get_devdata(master
);
408 dws
->dma_ops
->dma_stop(dws
);
413 /* This may be called twice for each spi dev */
414 static int dw_spi_setup(struct spi_device
*spi
)
416 struct dw_spi_chip
*chip_info
= NULL
;
417 struct chip_data
*chip
;
420 /* Only alloc on first setup */
421 chip
= spi_get_ctldata(spi
);
423 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
426 spi_set_ctldata(spi
, chip
);
430 * Protocol drivers may change the chip settings, so...
431 * if chip_info exists, use it
433 chip_info
= spi
->controller_data
;
435 /* chip_info doesn't always exist */
437 if (chip_info
->cs_control
)
438 chip
->cs_control
= chip_info
->cs_control
;
440 chip
->poll_mode
= chip_info
->poll_mode
;
441 chip
->type
= chip_info
->type
;
443 chip
->rx_threshold
= 0;
444 chip
->tx_threshold
= 0;
447 if (spi
->bits_per_word
== 8) {
450 } else if (spi
->bits_per_word
== 16) {
454 chip
->bits_per_word
= spi
->bits_per_word
;
456 if (!spi
->max_speed_hz
) {
457 dev_err(&spi
->dev
, "No max speed HZ parameter\n");
461 chip
->tmode
= 0; /* Tx & Rx */
462 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
463 chip
->cr0
= (chip
->bits_per_word
- 1)
464 | (chip
->type
<< SPI_FRF_OFFSET
)
465 | (spi
->mode
<< SPI_MODE_OFFSET
)
466 | (chip
->tmode
<< SPI_TMOD_OFFSET
);
468 if (spi
->mode
& SPI_LOOP
)
469 chip
->cr0
|= 1 << SPI_SRL_OFFSET
;
471 if (gpio_is_valid(spi
->cs_gpio
)) {
472 ret
= gpio_direction_output(spi
->cs_gpio
,
473 !(spi
->mode
& SPI_CS_HIGH
));
481 static void dw_spi_cleanup(struct spi_device
*spi
)
483 struct chip_data
*chip
= spi_get_ctldata(spi
);
486 spi_set_ctldata(spi
, NULL
);
489 /* Restart the controller, disable all interrupts, clean rx fifo */
490 static void spi_hw_init(struct device
*dev
, struct dw_spi
*dws
)
495 * Try to detect the FIFO depth if not set by interface driver,
496 * the depth could be from 2 to 256 from HW spec
498 if (!dws
->fifo_len
) {
501 for (fifo
= 1; fifo
< 256; fifo
++) {
502 dw_writel(dws
, DW_SPI_TXFLTR
, fifo
);
503 if (fifo
!= dw_readl(dws
, DW_SPI_TXFLTR
))
506 dw_writel(dws
, DW_SPI_TXFLTR
, 0);
508 dws
->fifo_len
= (fifo
== 1) ? 0 : fifo
;
509 dev_dbg(dev
, "Detected FIFO size: %u bytes\n", dws
->fifo_len
);
513 int dw_spi_add_host(struct device
*dev
, struct dw_spi
*dws
)
515 struct spi_master
*master
;
520 master
= spi_alloc_master(dev
, 0);
524 dws
->master
= master
;
525 dws
->type
= SSI_MOTO_SPI
;
527 dws
->dma_addr
= (dma_addr_t
)(dws
->paddr
+ 0x60);
528 snprintf(dws
->name
, sizeof(dws
->name
), "dw_spi%d", dws
->bus_num
);
530 ret
= devm_request_irq(dev
, dws
->irq
, dw_spi_irq
, IRQF_SHARED
,
533 dev_err(&master
->dev
, "can not get IRQ\n");
534 goto err_free_master
;
537 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LOOP
;
538 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
539 master
->bus_num
= dws
->bus_num
;
540 master
->num_chipselect
= dws
->num_cs
;
541 master
->setup
= dw_spi_setup
;
542 master
->cleanup
= dw_spi_cleanup
;
543 master
->set_cs
= dw_spi_set_cs
;
544 master
->transfer_one
= dw_spi_transfer_one
;
545 master
->handle_err
= dw_spi_handle_err
;
546 master
->max_speed_hz
= dws
->max_freq
;
547 master
->dev
.of_node
= dev
->of_node
;
550 spi_hw_init(dev
, dws
);
552 if (dws
->dma_ops
&& dws
->dma_ops
->dma_init
) {
553 ret
= dws
->dma_ops
->dma_init(dws
);
555 dev_warn(dev
, "DMA init failed\n");
558 master
->can_dma
= dws
->dma_ops
->can_dma
;
562 spi_master_set_devdata(master
, dws
);
563 ret
= devm_spi_register_master(dev
, master
);
565 dev_err(&master
->dev
, "problem registering spi master\n");
569 dw_spi_debugfs_init(dws
);
573 if (dws
->dma_ops
&& dws
->dma_ops
->dma_exit
)
574 dws
->dma_ops
->dma_exit(dws
);
575 spi_enable_chip(dws
, 0);
577 spi_master_put(master
);
580 EXPORT_SYMBOL_GPL(dw_spi_add_host
);
582 void dw_spi_remove_host(struct dw_spi
*dws
)
586 dw_spi_debugfs_remove(dws
);
588 if (dws
->dma_ops
&& dws
->dma_ops
->dma_exit
)
589 dws
->dma_ops
->dma_exit(dws
);
590 spi_enable_chip(dws
, 0);
594 EXPORT_SYMBOL_GPL(dw_spi_remove_host
);
596 int dw_spi_suspend_host(struct dw_spi
*dws
)
600 ret
= spi_master_suspend(dws
->master
);
603 spi_enable_chip(dws
, 0);
607 EXPORT_SYMBOL_GPL(dw_spi_suspend_host
);
609 int dw_spi_resume_host(struct dw_spi
*dws
)
613 spi_hw_init(&dws
->master
->dev
, dws
);
614 ret
= spi_master_resume(dws
->master
);
616 dev_err(&dws
->master
->dev
, "fail to start queue (%d)\n", ret
);
619 EXPORT_SYMBOL_GPL(dw_spi_resume_host
);
621 MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
622 MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
623 MODULE_LICENSE("GPL v2");