1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI bus driver for the Topcliff PCH used by Intel SoCs
5 * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
8 #include <linux/delay.h>
10 #include <linux/wait.h>
11 #include <linux/spi/spi.h>
12 #include <linux/interrupt.h>
13 #include <linux/sched.h>
14 #include <linux/spi/spidev.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
19 #include <linux/dmaengine.h>
20 #include <linux/pch_dma.h>
22 /* Register offsets */
23 #define PCH_SPCR 0x00 /* SPI control register */
24 #define PCH_SPBRR 0x04 /* SPI baud rate register */
25 #define PCH_SPSR 0x08 /* SPI status register */
26 #define PCH_SPDWR 0x0C /* SPI write data register */
27 #define PCH_SPDRR 0x10 /* SPI read data register */
28 #define PCH_SSNXCR 0x18 /* SSN Expand Control Register */
29 #define PCH_SRST 0x1C /* SPI reset register */
30 #define PCH_ADDRESS_SIZE 0x20
32 #define PCH_SPSR_TFD 0x000007C0
33 #define PCH_SPSR_RFD 0x0000F800
35 #define PCH_READABLE(x) (((x) & PCH_SPSR_RFD)>>11)
36 #define PCH_WRITABLE(x) (((x) & PCH_SPSR_TFD)>>6)
38 #define PCH_RX_THOLD 7
39 #define PCH_RX_THOLD_MAX 15
41 #define PCH_TX_THOLD 2
43 #define PCH_MAX_BAUDRATE 5000000
44 #define PCH_MAX_FIFO_DEPTH 16
46 #define STATUS_RUNNING 1
47 #define STATUS_EXITING 2
48 #define PCH_SLEEP_TIME 10
51 #define SSN_HIGH 0x03U
52 #define SSN_NO_CONTROL 0x00U
53 #define PCH_MAX_CS 0xFF
54 #define PCI_DEVICE_ID_GE_SPI 0x8816
56 #define SPCR_SPE_BIT (1 << 0)
57 #define SPCR_MSTR_BIT (1 << 1)
58 #define SPCR_LSBF_BIT (1 << 4)
59 #define SPCR_CPHA_BIT (1 << 5)
60 #define SPCR_CPOL_BIT (1 << 6)
61 #define SPCR_TFIE_BIT (1 << 8)
62 #define SPCR_RFIE_BIT (1 << 9)
63 #define SPCR_FIE_BIT (1 << 10)
64 #define SPCR_ORIE_BIT (1 << 11)
65 #define SPCR_MDFIE_BIT (1 << 12)
66 #define SPCR_FICLR_BIT (1 << 24)
67 #define SPSR_TFI_BIT (1 << 0)
68 #define SPSR_RFI_BIT (1 << 1)
69 #define SPSR_FI_BIT (1 << 2)
70 #define SPSR_ORF_BIT (1 << 3)
71 #define SPBRR_SIZE_BIT (1 << 10)
73 #define PCH_ALL (SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|\
74 SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
76 #define SPCR_RFIC_FIELD 20
77 #define SPCR_TFIC_FIELD 16
79 #define MASK_SPBRR_SPBR_BITS ((1 << 10) - 1)
80 #define MASK_RFIC_SPCR_BITS (0xf << SPCR_RFIC_FIELD)
81 #define MASK_TFIC_SPCR_BITS (0xf << SPCR_TFIC_FIELD)
83 #define PCH_CLOCK_HZ 50000000
84 #define PCH_MAX_SPBR 1023
86 /* Definition for ML7213/ML7223/ML7831 by LAPIS Semiconductor */
87 #define PCI_DEVICE_ID_ML7213_SPI 0x802c
88 #define PCI_DEVICE_ID_ML7223_SPI 0x800F
89 #define PCI_DEVICE_ID_ML7831_SPI 0x8816
92 * Set the number of SPI instance max
93 * Intel EG20T PCH : 1ch
94 * LAPIS Semiconductor ML7213 IOH : 2ch
95 * LAPIS Semiconductor ML7223 IOH : 1ch
96 * LAPIS Semiconductor ML7831 IOH : 1ch
98 #define PCH_SPI_MAX_DEV 2
100 #define PCH_BUF_SIZE 4096
101 #define PCH_DMA_TRANS_SIZE 12
103 static int use_dma
= 1;
105 struct pch_spi_dma_ctrl
{
106 struct dma_async_tx_descriptor
*desc_tx
;
107 struct dma_async_tx_descriptor
*desc_rx
;
108 struct pch_dma_slave param_tx
;
109 struct pch_dma_slave param_rx
;
110 struct dma_chan
*chan_tx
;
111 struct dma_chan
*chan_rx
;
112 struct scatterlist
*sg_tx_p
;
113 struct scatterlist
*sg_rx_p
;
114 struct scatterlist sg_tx
;
115 struct scatterlist sg_rx
;
119 dma_addr_t tx_buf_dma
;
120 dma_addr_t rx_buf_dma
;
123 * struct pch_spi_data - Holds the SPI channel specific details
124 * @io_remap_addr: The remapped PCI base address
125 * @master: Pointer to the SPI master structure
126 * @work: Reference to work queue handler
127 * @wait: Wait queue for waking up upon receiving an
129 * @transfer_complete: Status of SPI Transfer
130 * @bcurrent_msg_processing: Status flag for message processing
131 * @lock: Lock for protecting this structure
132 * @queue: SPI Message queue
133 * @status: Status of the SPI driver
134 * @bpw_len: Length of data to be transferred in bits per
136 * @transfer_active: Flag showing active transfer
137 * @tx_index: Transmit data count; for bookkeeping during
139 * @rx_index: Receive data count; for bookkeeping during
141 * @tx_buff: Buffer for data to be transmitted
142 * @rx_index: Buffer for Received data
143 * @n_curnt_chip: The chip number that this SPI driver currently
145 * @current_chip: Reference to the current chip that this SPI
146 * driver currently operates on
147 * @current_msg: The current message that this SPI driver is
149 * @cur_trans: The current transfer that this SPI driver is
151 * @board_dat: Reference to the SPI device data structure
152 * @plat_dev: platform_device structure
153 * @ch: SPI channel number
154 * @irq_reg_sts: Status of IRQ registration
156 struct pch_spi_data
{
157 void __iomem
*io_remap_addr
;
158 unsigned long io_base_addr
;
159 struct spi_master
*master
;
160 struct work_struct work
;
161 wait_queue_head_t wait
;
162 u8 transfer_complete
;
163 u8 bcurrent_msg_processing
;
165 struct list_head queue
;
174 struct spi_device
*current_chip
;
175 struct spi_message
*current_msg
;
176 struct spi_transfer
*cur_trans
;
177 struct pch_spi_board_data
*board_dat
;
178 struct platform_device
*plat_dev
;
180 struct pch_spi_dma_ctrl dma
;
187 * struct pch_spi_board_data - Holds the SPI device specific details
188 * @pdev: Pointer to the PCI device
189 * @suspend_sts: Status of suspend
190 * @num: The number of SPI device instance
192 struct pch_spi_board_data
{
193 struct pci_dev
*pdev
;
198 struct pch_pd_dev_save
{
200 struct platform_device
*pd_save
[PCH_SPI_MAX_DEV
];
201 struct pch_spi_board_data
*board_dat
;
204 static const struct pci_device_id pch_spi_pcidev_id
[] = {
205 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_GE_SPI
), 1, },
206 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7213_SPI
), 2, },
207 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7223_SPI
), 1, },
208 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7831_SPI
), 1, },
213 * pch_spi_writereg() - Performs register writes
214 * @master: Pointer to struct spi_master.
215 * @idx: Register offset.
216 * @val: Value to be written to register.
218 static inline void pch_spi_writereg(struct spi_master
*master
, int idx
, u32 val
)
220 struct pch_spi_data
*data
= spi_master_get_devdata(master
);
221 iowrite32(val
, (data
->io_remap_addr
+ idx
));
225 * pch_spi_readreg() - Performs register reads
226 * @master: Pointer to struct spi_master.
227 * @idx: Register offset.
229 static inline u32
pch_spi_readreg(struct spi_master
*master
, int idx
)
231 struct pch_spi_data
*data
= spi_master_get_devdata(master
);
232 return ioread32(data
->io_remap_addr
+ idx
);
235 static inline void pch_spi_setclr_reg(struct spi_master
*master
, int idx
,
238 u32 tmp
= pch_spi_readreg(master
, idx
);
239 tmp
= (tmp
& ~clr
) | set
;
240 pch_spi_writereg(master
, idx
, tmp
);
243 static void pch_spi_set_master_mode(struct spi_master
*master
)
245 pch_spi_setclr_reg(master
, PCH_SPCR
, SPCR_MSTR_BIT
, 0);
249 * pch_spi_clear_fifo() - Clears the Transmit and Receive FIFOs
250 * @master: Pointer to struct spi_master.
252 static void pch_spi_clear_fifo(struct spi_master
*master
)
254 pch_spi_setclr_reg(master
, PCH_SPCR
, SPCR_FICLR_BIT
, 0);
255 pch_spi_setclr_reg(master
, PCH_SPCR
, 0, SPCR_FICLR_BIT
);
258 static void pch_spi_handler_sub(struct pch_spi_data
*data
, u32 reg_spsr_val
,
259 void __iomem
*io_remap_addr
)
261 u32 n_read
, tx_index
, rx_index
, bpw_len
;
262 u16
*pkt_rx_buffer
, *pkt_tx_buff
;
269 spsr
= io_remap_addr
+ PCH_SPSR
;
270 iowrite32(reg_spsr_val
, spsr
);
272 if (data
->transfer_active
) {
273 rx_index
= data
->rx_index
;
274 tx_index
= data
->tx_index
;
275 bpw_len
= data
->bpw_len
;
276 pkt_rx_buffer
= data
->pkt_rx_buff
;
277 pkt_tx_buff
= data
->pkt_tx_buff
;
279 spdrr
= io_remap_addr
+ PCH_SPDRR
;
280 spdwr
= io_remap_addr
+ PCH_SPDWR
;
282 n_read
= PCH_READABLE(reg_spsr_val
);
284 for (read_cnt
= 0; (read_cnt
< n_read
); read_cnt
++) {
285 pkt_rx_buffer
[rx_index
++] = ioread32(spdrr
);
286 if (tx_index
< bpw_len
)
287 iowrite32(pkt_tx_buff
[tx_index
++], spdwr
);
290 /* disable RFI if not needed */
291 if ((bpw_len
- rx_index
) <= PCH_MAX_FIFO_DEPTH
) {
292 reg_spcr_val
= ioread32(io_remap_addr
+ PCH_SPCR
);
293 reg_spcr_val
&= ~SPCR_RFIE_BIT
; /* disable RFI */
295 /* reset rx threshold */
296 reg_spcr_val
&= ~MASK_RFIC_SPCR_BITS
;
297 reg_spcr_val
|= (PCH_RX_THOLD_MAX
<< SPCR_RFIC_FIELD
);
299 iowrite32(reg_spcr_val
, (io_remap_addr
+ PCH_SPCR
));
303 data
->tx_index
= tx_index
;
304 data
->rx_index
= rx_index
;
306 /* if transfer complete interrupt */
307 if (reg_spsr_val
& SPSR_FI_BIT
) {
308 if ((tx_index
== bpw_len
) && (rx_index
== tx_index
)) {
309 /* disable interrupts */
310 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, 0,
313 /* transfer is completed;
314 inform pch_spi_process_messages */
315 data
->transfer_complete
= true;
316 data
->transfer_active
= false;
317 wake_up(&data
->wait
);
319 dev_vdbg(&data
->master
->dev
,
320 "%s : Transfer is not completed",
328 * pch_spi_handler() - Interrupt handler
329 * @irq: The interrupt number.
330 * @dev_id: Pointer to struct pch_spi_board_data.
332 static irqreturn_t
pch_spi_handler(int irq
, void *dev_id
)
336 void __iomem
*io_remap_addr
;
337 irqreturn_t ret
= IRQ_NONE
;
338 struct pch_spi_data
*data
= dev_id
;
339 struct pch_spi_board_data
*board_dat
= data
->board_dat
;
341 if (board_dat
->suspend_sts
) {
342 dev_dbg(&board_dat
->pdev
->dev
,
343 "%s returning due to suspend\n", __func__
);
347 io_remap_addr
= data
->io_remap_addr
;
348 spsr
= io_remap_addr
+ PCH_SPSR
;
350 reg_spsr_val
= ioread32(spsr
);
352 if (reg_spsr_val
& SPSR_ORF_BIT
) {
353 dev_err(&board_dat
->pdev
->dev
, "%s Over run error\n", __func__
);
354 if (data
->current_msg
->complete
) {
355 data
->transfer_complete
= true;
356 data
->current_msg
->status
= -EIO
;
357 data
->current_msg
->complete(data
->current_msg
->context
);
358 data
->bcurrent_msg_processing
= false;
359 data
->current_msg
= NULL
;
360 data
->cur_trans
= NULL
;
367 /* Check if the interrupt is for SPI device */
368 if (reg_spsr_val
& (SPSR_FI_BIT
| SPSR_RFI_BIT
)) {
369 pch_spi_handler_sub(data
, reg_spsr_val
, io_remap_addr
);
373 dev_dbg(&board_dat
->pdev
->dev
, "%s EXIT return value=%d\n",
380 * pch_spi_set_baud_rate() - Sets SPBR field in SPBRR
381 * @master: Pointer to struct spi_master.
382 * @speed_hz: Baud rate.
384 static void pch_spi_set_baud_rate(struct spi_master
*master
, u32 speed_hz
)
386 u32 n_spbr
= PCH_CLOCK_HZ
/ (speed_hz
* 2);
388 /* if baud rate is less than we can support limit it */
389 if (n_spbr
> PCH_MAX_SPBR
)
390 n_spbr
= PCH_MAX_SPBR
;
392 pch_spi_setclr_reg(master
, PCH_SPBRR
, n_spbr
, MASK_SPBRR_SPBR_BITS
);
396 * pch_spi_set_bits_per_word() - Sets SIZE field in SPBRR
397 * @master: Pointer to struct spi_master.
398 * @bits_per_word: Bits per word for SPI transfer.
400 static void pch_spi_set_bits_per_word(struct spi_master
*master
,
403 if (bits_per_word
== 8)
404 pch_spi_setclr_reg(master
, PCH_SPBRR
, 0, SPBRR_SIZE_BIT
);
406 pch_spi_setclr_reg(master
, PCH_SPBRR
, SPBRR_SIZE_BIT
, 0);
410 * pch_spi_setup_transfer() - Configures the PCH SPI hardware for transfer
411 * @spi: Pointer to struct spi_device.
413 static void pch_spi_setup_transfer(struct spi_device
*spi
)
417 dev_dbg(&spi
->dev
, "%s SPBRR content =%x setting baud rate=%d\n",
418 __func__
, pch_spi_readreg(spi
->master
, PCH_SPBRR
),
420 pch_spi_set_baud_rate(spi
->master
, spi
->max_speed_hz
);
422 /* set bits per word */
423 pch_spi_set_bits_per_word(spi
->master
, spi
->bits_per_word
);
425 if (!(spi
->mode
& SPI_LSB_FIRST
))
426 flags
|= SPCR_LSBF_BIT
;
427 if (spi
->mode
& SPI_CPOL
)
428 flags
|= SPCR_CPOL_BIT
;
429 if (spi
->mode
& SPI_CPHA
)
430 flags
|= SPCR_CPHA_BIT
;
431 pch_spi_setclr_reg(spi
->master
, PCH_SPCR
, flags
,
432 (SPCR_LSBF_BIT
| SPCR_CPOL_BIT
| SPCR_CPHA_BIT
));
434 /* Clear the FIFO by toggling FICLR to 1 and back to 0 */
435 pch_spi_clear_fifo(spi
->master
);
439 * pch_spi_reset() - Clears SPI registers
440 * @master: Pointer to struct spi_master.
442 static void pch_spi_reset(struct spi_master
*master
)
444 /* write 1 to reset SPI */
445 pch_spi_writereg(master
, PCH_SRST
, 0x1);
448 pch_spi_writereg(master
, PCH_SRST
, 0x0);
451 static int pch_spi_transfer(struct spi_device
*pspi
, struct spi_message
*pmsg
)
454 struct spi_transfer
*transfer
;
455 struct pch_spi_data
*data
= spi_master_get_devdata(pspi
->master
);
459 spin_lock_irqsave(&data
->lock
, flags
);
460 /* validate Tx/Rx buffers and Transfer length */
461 list_for_each_entry(transfer
, &pmsg
->transfers
, transfer_list
) {
462 if (!transfer
->tx_buf
&& !transfer
->rx_buf
) {
464 "%s Tx and Rx buffer NULL\n", __func__
);
466 goto err_return_spinlock
;
469 if (!transfer
->len
) {
470 dev_err(&pspi
->dev
, "%s Transfer length invalid\n",
473 goto err_return_spinlock
;
477 "%s Tx/Rx buffer valid. Transfer length valid\n",
480 spin_unlock_irqrestore(&data
->lock
, flags
);
482 /* We won't process any messages if we have been asked to terminate */
483 if (data
->status
== STATUS_EXITING
) {
484 dev_err(&pspi
->dev
, "%s status = STATUS_EXITING.\n", __func__
);
489 /* If suspended ,return -EINVAL */
490 if (data
->board_dat
->suspend_sts
) {
491 dev_err(&pspi
->dev
, "%s suspend; returning EINVAL\n", __func__
);
496 /* set status of message */
497 pmsg
->actual_length
= 0;
498 dev_dbg(&pspi
->dev
, "%s - pmsg->status =%d\n", __func__
, pmsg
->status
);
500 pmsg
->status
= -EINPROGRESS
;
501 spin_lock_irqsave(&data
->lock
, flags
);
502 /* add message to queue */
503 list_add_tail(&pmsg
->queue
, &data
->queue
);
504 spin_unlock_irqrestore(&data
->lock
, flags
);
506 dev_dbg(&pspi
->dev
, "%s - Invoked list_add_tail\n", __func__
);
508 schedule_work(&data
->work
);
509 dev_dbg(&pspi
->dev
, "%s - Invoked queue work\n", __func__
);
514 dev_dbg(&pspi
->dev
, "%s RETURN=%d\n", __func__
, retval
);
517 dev_dbg(&pspi
->dev
, "%s RETURN=%d\n", __func__
, retval
);
518 spin_unlock_irqrestore(&data
->lock
, flags
);
522 static inline void pch_spi_select_chip(struct pch_spi_data
*data
,
523 struct spi_device
*pspi
)
525 if (data
->current_chip
!= NULL
) {
526 if (pspi
->chip_select
!= data
->n_curnt_chip
) {
527 dev_dbg(&pspi
->dev
, "%s : different slave\n", __func__
);
528 data
->current_chip
= NULL
;
532 data
->current_chip
= pspi
;
534 data
->n_curnt_chip
= data
->current_chip
->chip_select
;
536 dev_dbg(&pspi
->dev
, "%s :Invoking pch_spi_setup_transfer\n", __func__
);
537 pch_spi_setup_transfer(pspi
);
540 static void pch_spi_set_tx(struct pch_spi_data
*data
, int *bpw
)
545 struct spi_message
*pmsg
, *tmp
;
549 /* set baud rate if needed */
550 if (data
->cur_trans
->speed_hz
) {
551 dev_dbg(&data
->master
->dev
, "%s:setting baud rate\n", __func__
);
552 pch_spi_set_baud_rate(data
->master
, data
->cur_trans
->speed_hz
);
555 /* set bits per word if needed */
556 if (data
->cur_trans
->bits_per_word
&&
557 (data
->current_msg
->spi
->bits_per_word
!= data
->cur_trans
->bits_per_word
)) {
558 dev_dbg(&data
->master
->dev
, "%s:set bits per word\n", __func__
);
559 pch_spi_set_bits_per_word(data
->master
,
560 data
->cur_trans
->bits_per_word
);
561 *bpw
= data
->cur_trans
->bits_per_word
;
563 *bpw
= data
->current_msg
->spi
->bits_per_word
;
566 /* reset Tx/Rx index */
570 data
->bpw_len
= data
->cur_trans
->len
/ (*bpw
/ 8);
572 /* find alloc size */
573 size
= data
->cur_trans
->len
* sizeof(*data
->pkt_tx_buff
);
575 /* allocate memory for pkt_tx_buff & pkt_rx_buffer */
576 data
->pkt_tx_buff
= kzalloc(size
, GFP_KERNEL
);
577 if (data
->pkt_tx_buff
!= NULL
) {
578 data
->pkt_rx_buff
= kzalloc(size
, GFP_KERNEL
);
579 if (!data
->pkt_rx_buff
)
580 kfree(data
->pkt_tx_buff
);
583 if (!data
->pkt_rx_buff
) {
584 /* flush queue and set status of all transfers to -ENOMEM */
585 list_for_each_entry_safe(pmsg
, tmp
, data
->queue
.next
, queue
) {
586 pmsg
->status
= -ENOMEM
;
589 pmsg
->complete(pmsg
->context
);
591 /* delete from queue */
592 list_del_init(&pmsg
->queue
);
598 if (data
->cur_trans
->tx_buf
!= NULL
) {
600 tx_buf
= data
->cur_trans
->tx_buf
;
601 for (j
= 0; j
< data
->bpw_len
; j
++)
602 data
->pkt_tx_buff
[j
] = *tx_buf
++;
604 tx_sbuf
= data
->cur_trans
->tx_buf
;
605 for (j
= 0; j
< data
->bpw_len
; j
++)
606 data
->pkt_tx_buff
[j
] = *tx_sbuf
++;
610 /* if len greater than PCH_MAX_FIFO_DEPTH, write 16,else len bytes */
611 n_writes
= data
->bpw_len
;
612 if (n_writes
> PCH_MAX_FIFO_DEPTH
)
613 n_writes
= PCH_MAX_FIFO_DEPTH
;
615 dev_dbg(&data
->master
->dev
,
616 "\n%s:Pulling down SSN low - writing 0x2 to SSNXCR\n",
618 pch_spi_writereg(data
->master
, PCH_SSNXCR
, SSN_LOW
);
620 for (j
= 0; j
< n_writes
; j
++)
621 pch_spi_writereg(data
->master
, PCH_SPDWR
, data
->pkt_tx_buff
[j
]);
623 /* update tx_index */
626 /* reset transfer complete flag */
627 data
->transfer_complete
= false;
628 data
->transfer_active
= true;
631 static void pch_spi_nomore_transfer(struct pch_spi_data
*data
)
633 struct spi_message
*pmsg
, *tmp
;
634 dev_dbg(&data
->master
->dev
, "%s called\n", __func__
);
635 /* Invoke complete callback
636 * [To the spi core..indicating end of transfer] */
637 data
->current_msg
->status
= 0;
639 if (data
->current_msg
->complete
) {
640 dev_dbg(&data
->master
->dev
,
641 "%s:Invoking callback of SPI core\n", __func__
);
642 data
->current_msg
->complete(data
->current_msg
->context
);
645 /* update status in global variable */
646 data
->bcurrent_msg_processing
= false;
648 dev_dbg(&data
->master
->dev
,
649 "%s:data->bcurrent_msg_processing = false\n", __func__
);
651 data
->current_msg
= NULL
;
652 data
->cur_trans
= NULL
;
654 /* check if we have items in list and not suspending
655 * return 1 if list empty */
656 if ((list_empty(&data
->queue
) == 0) &&
657 (!data
->board_dat
->suspend_sts
) &&
658 (data
->status
!= STATUS_EXITING
)) {
659 /* We have some more work to do (either there is more tranint
660 * bpw;sfer requests in the current message or there are
663 dev_dbg(&data
->master
->dev
, "%s:Invoke queue_work\n", __func__
);
664 schedule_work(&data
->work
);
665 } else if (data
->board_dat
->suspend_sts
||
666 data
->status
== STATUS_EXITING
) {
667 dev_dbg(&data
->master
->dev
,
668 "%s suspend/remove initiated, flushing queue\n",
670 list_for_each_entry_safe(pmsg
, tmp
, data
->queue
.next
, queue
) {
674 pmsg
->complete(pmsg
->context
);
676 /* delete from queue */
677 list_del_init(&pmsg
->queue
);
682 static void pch_spi_set_ir(struct pch_spi_data
*data
)
684 /* enable interrupts, set threshold, enable SPI */
685 if ((data
->bpw_len
) > PCH_MAX_FIFO_DEPTH
)
686 /* set receive threshold to PCH_RX_THOLD */
687 pch_spi_setclr_reg(data
->master
, PCH_SPCR
,
688 PCH_RX_THOLD
<< SPCR_RFIC_FIELD
|
689 SPCR_FIE_BIT
| SPCR_RFIE_BIT
|
690 SPCR_ORIE_BIT
| SPCR_SPE_BIT
,
691 MASK_RFIC_SPCR_BITS
| PCH_ALL
);
693 /* set receive threshold to maximum */
694 pch_spi_setclr_reg(data
->master
, PCH_SPCR
,
695 PCH_RX_THOLD_MAX
<< SPCR_RFIC_FIELD
|
696 SPCR_FIE_BIT
| SPCR_ORIE_BIT
|
698 MASK_RFIC_SPCR_BITS
| PCH_ALL
);
700 /* Wait until the transfer completes; go to sleep after
701 initiating the transfer. */
702 dev_dbg(&data
->master
->dev
,
703 "%s:waiting for transfer to get over\n", __func__
);
705 wait_event_interruptible(data
->wait
, data
->transfer_complete
);
707 /* clear all interrupts */
708 pch_spi_writereg(data
->master
, PCH_SPSR
,
709 pch_spi_readreg(data
->master
, PCH_SPSR
));
710 /* Disable interrupts and SPI transfer */
711 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, 0, PCH_ALL
| SPCR_SPE_BIT
);
713 pch_spi_clear_fifo(data
->master
);
716 static void pch_spi_copy_rx_data(struct pch_spi_data
*data
, int bpw
)
723 if (!data
->cur_trans
->rx_buf
)
727 rx_buf
= data
->cur_trans
->rx_buf
;
728 for (j
= 0; j
< data
->bpw_len
; j
++)
729 *rx_buf
++ = data
->pkt_rx_buff
[j
] & 0xFF;
731 rx_sbuf
= data
->cur_trans
->rx_buf
;
732 for (j
= 0; j
< data
->bpw_len
; j
++)
733 *rx_sbuf
++ = data
->pkt_rx_buff
[j
];
737 static void pch_spi_copy_rx_data_for_dma(struct pch_spi_data
*data
, int bpw
)
742 const u8
*rx_dma_buf
;
743 const u16
*rx_dma_sbuf
;
746 if (!data
->cur_trans
->rx_buf
)
750 rx_buf
= data
->cur_trans
->rx_buf
;
751 rx_dma_buf
= data
->dma
.rx_buf_virt
;
752 for (j
= 0; j
< data
->bpw_len
; j
++)
753 *rx_buf
++ = *rx_dma_buf
++ & 0xFF;
754 data
->cur_trans
->rx_buf
= rx_buf
;
756 rx_sbuf
= data
->cur_trans
->rx_buf
;
757 rx_dma_sbuf
= data
->dma
.rx_buf_virt
;
758 for (j
= 0; j
< data
->bpw_len
; j
++)
759 *rx_sbuf
++ = *rx_dma_sbuf
++;
760 data
->cur_trans
->rx_buf
= rx_sbuf
;
764 static int pch_spi_start_transfer(struct pch_spi_data
*data
)
766 struct pch_spi_dma_ctrl
*dma
;
772 spin_lock_irqsave(&data
->lock
, flags
);
774 /* disable interrupts, SPI set enable */
775 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, SPCR_SPE_BIT
, PCH_ALL
);
777 spin_unlock_irqrestore(&data
->lock
, flags
);
779 /* Wait until the transfer completes; go to sleep after
780 initiating the transfer. */
781 dev_dbg(&data
->master
->dev
,
782 "%s:waiting for transfer to get over\n", __func__
);
783 rtn
= wait_event_interruptible_timeout(data
->wait
,
784 data
->transfer_complete
,
785 msecs_to_jiffies(2 * HZ
));
787 dev_err(&data
->master
->dev
,
788 "%s wait-event timeout\n", __func__
);
790 dma_sync_sg_for_cpu(&data
->master
->dev
, dma
->sg_rx_p
, dma
->nent
,
793 dma_sync_sg_for_cpu(&data
->master
->dev
, dma
->sg_tx_p
, dma
->nent
,
795 memset(data
->dma
.tx_buf_virt
, 0, PAGE_SIZE
);
797 async_tx_ack(dma
->desc_rx
);
798 async_tx_ack(dma
->desc_tx
);
802 spin_lock_irqsave(&data
->lock
, flags
);
804 /* clear fifo threshold, disable interrupts, disable SPI transfer */
805 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, 0,
806 MASK_RFIC_SPCR_BITS
| MASK_TFIC_SPCR_BITS
| PCH_ALL
|
808 /* clear all interrupts */
809 pch_spi_writereg(data
->master
, PCH_SPSR
,
810 pch_spi_readreg(data
->master
, PCH_SPSR
));
812 pch_spi_clear_fifo(data
->master
);
814 spin_unlock_irqrestore(&data
->lock
, flags
);
819 static void pch_dma_rx_complete(void *arg
)
821 struct pch_spi_data
*data
= arg
;
823 /* transfer is completed;inform pch_spi_process_messages_dma */
824 data
->transfer_complete
= true;
825 wake_up_interruptible(&data
->wait
);
828 static bool pch_spi_filter(struct dma_chan
*chan
, void *slave
)
830 struct pch_dma_slave
*param
= slave
;
832 if ((chan
->chan_id
== param
->chan_id
) &&
833 (param
->dma_dev
== chan
->device
->dev
)) {
834 chan
->private = param
;
841 static void pch_spi_request_dma(struct pch_spi_data
*data
, int bpw
)
844 struct dma_chan
*chan
;
845 struct pci_dev
*dma_dev
;
846 struct pch_dma_slave
*param
;
847 struct pch_spi_dma_ctrl
*dma
;
851 width
= PCH_DMA_WIDTH_1_BYTE
;
853 width
= PCH_DMA_WIDTH_2_BYTES
;
857 dma_cap_set(DMA_SLAVE
, mask
);
859 /* Get DMA's dev information */
860 dma_dev
= pci_get_slot(data
->board_dat
->pdev
->bus
,
861 PCI_DEVFN(PCI_SLOT(data
->board_dat
->pdev
->devfn
), 0));
864 param
= &dma
->param_tx
;
865 param
->dma_dev
= &dma_dev
->dev
;
866 param
->chan_id
= data
->ch
* 2; /* Tx = 0, 2 */
867 param
->tx_reg
= data
->io_base_addr
+ PCH_SPDWR
;
868 param
->width
= width
;
869 chan
= dma_request_channel(mask
, pch_spi_filter
, param
);
871 dev_err(&data
->master
->dev
,
872 "ERROR: dma_request_channel FAILS(Tx)\n");
879 param
= &dma
->param_rx
;
880 param
->dma_dev
= &dma_dev
->dev
;
881 param
->chan_id
= data
->ch
* 2 + 1; /* Rx = Tx + 1 */
882 param
->rx_reg
= data
->io_base_addr
+ PCH_SPDRR
;
883 param
->width
= width
;
884 chan
= dma_request_channel(mask
, pch_spi_filter
, param
);
886 dev_err(&data
->master
->dev
,
887 "ERROR: dma_request_channel FAILS(Rx)\n");
888 dma_release_channel(dma
->chan_tx
);
896 static void pch_spi_release_dma(struct pch_spi_data
*data
)
898 struct pch_spi_dma_ctrl
*dma
;
902 dma_release_channel(dma
->chan_tx
);
906 dma_release_channel(dma
->chan_rx
);
911 static void pch_spi_handle_dma(struct pch_spi_data
*data
, int *bpw
)
917 struct scatterlist
*sg
;
918 struct dma_async_tx_descriptor
*desc_tx
;
919 struct dma_async_tx_descriptor
*desc_rx
;
926 struct pch_spi_dma_ctrl
*dma
;
930 /* set baud rate if needed */
931 if (data
->cur_trans
->speed_hz
) {
932 dev_dbg(&data
->master
->dev
, "%s:setting baud rate\n", __func__
);
933 spin_lock_irqsave(&data
->lock
, flags
);
934 pch_spi_set_baud_rate(data
->master
, data
->cur_trans
->speed_hz
);
935 spin_unlock_irqrestore(&data
->lock
, flags
);
938 /* set bits per word if needed */
939 if (data
->cur_trans
->bits_per_word
&&
940 (data
->current_msg
->spi
->bits_per_word
!=
941 data
->cur_trans
->bits_per_word
)) {
942 dev_dbg(&data
->master
->dev
, "%s:set bits per word\n", __func__
);
943 spin_lock_irqsave(&data
->lock
, flags
);
944 pch_spi_set_bits_per_word(data
->master
,
945 data
->cur_trans
->bits_per_word
);
946 spin_unlock_irqrestore(&data
->lock
, flags
);
947 *bpw
= data
->cur_trans
->bits_per_word
;
949 *bpw
= data
->current_msg
->spi
->bits_per_word
;
951 data
->bpw_len
= data
->cur_trans
->len
/ (*bpw
/ 8);
953 if (data
->bpw_len
> PCH_BUF_SIZE
) {
954 data
->bpw_len
= PCH_BUF_SIZE
;
955 data
->cur_trans
->len
-= PCH_BUF_SIZE
;
959 if (data
->cur_trans
->tx_buf
!= NULL
) {
961 tx_buf
= data
->cur_trans
->tx_buf
;
962 tx_dma_buf
= dma
->tx_buf_virt
;
963 for (i
= 0; i
< data
->bpw_len
; i
++)
964 *tx_dma_buf
++ = *tx_buf
++;
966 tx_sbuf
= data
->cur_trans
->tx_buf
;
967 tx_dma_sbuf
= dma
->tx_buf_virt
;
968 for (i
= 0; i
< data
->bpw_len
; i
++)
969 *tx_dma_sbuf
++ = *tx_sbuf
++;
973 /* Calculate Rx parameter for DMA transmitting */
974 if (data
->bpw_len
> PCH_DMA_TRANS_SIZE
) {
975 if (data
->bpw_len
% PCH_DMA_TRANS_SIZE
) {
976 num
= data
->bpw_len
/ PCH_DMA_TRANS_SIZE
+ 1;
977 rem
= data
->bpw_len
% PCH_DMA_TRANS_SIZE
;
979 num
= data
->bpw_len
/ PCH_DMA_TRANS_SIZE
;
980 rem
= PCH_DMA_TRANS_SIZE
;
982 size
= PCH_DMA_TRANS_SIZE
;
985 size
= data
->bpw_len
;
988 dev_dbg(&data
->master
->dev
, "%s num=%d size=%d rem=%d\n",
989 __func__
, num
, size
, rem
);
990 spin_lock_irqsave(&data
->lock
, flags
);
992 /* set receive fifo threshold and transmit fifo threshold */
993 pch_spi_setclr_reg(data
->master
, PCH_SPCR
,
994 ((size
- 1) << SPCR_RFIC_FIELD
) |
995 (PCH_TX_THOLD
<< SPCR_TFIC_FIELD
),
996 MASK_RFIC_SPCR_BITS
| MASK_TFIC_SPCR_BITS
);
998 spin_unlock_irqrestore(&data
->lock
, flags
);
1001 dma
->sg_rx_p
= kcalloc(num
, sizeof(*dma
->sg_rx_p
), GFP_ATOMIC
);
1005 sg_init_table(dma
->sg_rx_p
, num
); /* Initialize SG table */
1006 /* offset, length setting */
1008 for (i
= 0; i
< num
; i
++, sg
++) {
1009 if (i
== (num
- 2)) {
1010 sg
->offset
= size
* i
;
1011 sg
->offset
= sg
->offset
* (*bpw
/ 8);
1012 sg_set_page(sg
, virt_to_page(dma
->rx_buf_virt
), rem
,
1014 sg_dma_len(sg
) = rem
;
1015 } else if (i
== (num
- 1)) {
1016 sg
->offset
= size
* (i
- 1) + rem
;
1017 sg
->offset
= sg
->offset
* (*bpw
/ 8);
1018 sg_set_page(sg
, virt_to_page(dma
->rx_buf_virt
), size
,
1020 sg_dma_len(sg
) = size
;
1022 sg
->offset
= size
* i
;
1023 sg
->offset
= sg
->offset
* (*bpw
/ 8);
1024 sg_set_page(sg
, virt_to_page(dma
->rx_buf_virt
), size
,
1026 sg_dma_len(sg
) = size
;
1028 sg_dma_address(sg
) = dma
->rx_buf_dma
+ sg
->offset
;
1031 desc_rx
= dmaengine_prep_slave_sg(dma
->chan_rx
, sg
,
1032 num
, DMA_DEV_TO_MEM
,
1033 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1035 dev_err(&data
->master
->dev
,
1036 "%s:dmaengine_prep_slave_sg Failed\n", __func__
);
1039 dma_sync_sg_for_device(&data
->master
->dev
, sg
, num
, DMA_FROM_DEVICE
);
1040 desc_rx
->callback
= pch_dma_rx_complete
;
1041 desc_rx
->callback_param
= data
;
1043 dma
->desc_rx
= desc_rx
;
1045 /* Calculate Tx parameter for DMA transmitting */
1046 if (data
->bpw_len
> PCH_MAX_FIFO_DEPTH
) {
1047 head
= PCH_MAX_FIFO_DEPTH
- PCH_DMA_TRANS_SIZE
;
1048 if (data
->bpw_len
% PCH_DMA_TRANS_SIZE
> 4) {
1049 num
= data
->bpw_len
/ PCH_DMA_TRANS_SIZE
+ 1;
1050 rem
= data
->bpw_len
% PCH_DMA_TRANS_SIZE
- head
;
1052 num
= data
->bpw_len
/ PCH_DMA_TRANS_SIZE
;
1053 rem
= data
->bpw_len
% PCH_DMA_TRANS_SIZE
+
1054 PCH_DMA_TRANS_SIZE
- head
;
1056 size
= PCH_DMA_TRANS_SIZE
;
1059 size
= data
->bpw_len
;
1060 rem
= data
->bpw_len
;
1064 dma
->sg_tx_p
= kcalloc(num
, sizeof(*dma
->sg_tx_p
), GFP_ATOMIC
);
1068 sg_init_table(dma
->sg_tx_p
, num
); /* Initialize SG table */
1069 /* offset, length setting */
1071 for (i
= 0; i
< num
; i
++, sg
++) {
1074 sg_set_page(sg
, virt_to_page(dma
->tx_buf_virt
), size
+ head
,
1076 sg_dma_len(sg
) = size
+ head
;
1077 } else if (i
== (num
- 1)) {
1078 sg
->offset
= head
+ size
* i
;
1079 sg
->offset
= sg
->offset
* (*bpw
/ 8);
1080 sg_set_page(sg
, virt_to_page(dma
->tx_buf_virt
), rem
,
1082 sg_dma_len(sg
) = rem
;
1084 sg
->offset
= head
+ size
* i
;
1085 sg
->offset
= sg
->offset
* (*bpw
/ 8);
1086 sg_set_page(sg
, virt_to_page(dma
->tx_buf_virt
), size
,
1088 sg_dma_len(sg
) = size
;
1090 sg_dma_address(sg
) = dma
->tx_buf_dma
+ sg
->offset
;
1093 desc_tx
= dmaengine_prep_slave_sg(dma
->chan_tx
,
1094 sg
, num
, DMA_MEM_TO_DEV
,
1095 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1097 dev_err(&data
->master
->dev
,
1098 "%s:dmaengine_prep_slave_sg Failed\n", __func__
);
1101 dma_sync_sg_for_device(&data
->master
->dev
, sg
, num
, DMA_TO_DEVICE
);
1102 desc_tx
->callback
= NULL
;
1103 desc_tx
->callback_param
= data
;
1105 dma
->desc_tx
= desc_tx
;
1107 dev_dbg(&data
->master
->dev
, "%s:Pulling down SSN low - writing 0x2 to SSNXCR\n", __func__
);
1109 spin_lock_irqsave(&data
->lock
, flags
);
1110 pch_spi_writereg(data
->master
, PCH_SSNXCR
, SSN_LOW
);
1111 desc_rx
->tx_submit(desc_rx
);
1112 desc_tx
->tx_submit(desc_tx
);
1113 spin_unlock_irqrestore(&data
->lock
, flags
);
1115 /* reset transfer complete flag */
1116 data
->transfer_complete
= false;
1119 static void pch_spi_process_messages(struct work_struct
*pwork
)
1121 struct spi_message
*pmsg
, *tmp
;
1122 struct pch_spi_data
*data
;
1125 data
= container_of(pwork
, struct pch_spi_data
, work
);
1126 dev_dbg(&data
->master
->dev
, "%s data initialized\n", __func__
);
1128 spin_lock(&data
->lock
);
1129 /* check if suspend has been initiated;if yes flush queue */
1130 if (data
->board_dat
->suspend_sts
|| (data
->status
== STATUS_EXITING
)) {
1131 dev_dbg(&data
->master
->dev
,
1132 "%s suspend/remove initiated, flushing queue\n", __func__
);
1133 list_for_each_entry_safe(pmsg
, tmp
, data
->queue
.next
, queue
) {
1134 pmsg
->status
= -EIO
;
1136 if (pmsg
->complete
) {
1137 spin_unlock(&data
->lock
);
1138 pmsg
->complete(pmsg
->context
);
1139 spin_lock(&data
->lock
);
1142 /* delete from queue */
1143 list_del_init(&pmsg
->queue
);
1146 spin_unlock(&data
->lock
);
1150 data
->bcurrent_msg_processing
= true;
1151 dev_dbg(&data
->master
->dev
,
1152 "%s Set data->bcurrent_msg_processing= true\n", __func__
);
1154 /* Get the message from the queue and delete it from there. */
1155 data
->current_msg
= list_entry(data
->queue
.next
, struct spi_message
,
1158 list_del_init(&data
->current_msg
->queue
);
1160 data
->current_msg
->status
= 0;
1162 pch_spi_select_chip(data
, data
->current_msg
->spi
);
1164 spin_unlock(&data
->lock
);
1167 pch_spi_request_dma(data
,
1168 data
->current_msg
->spi
->bits_per_word
);
1169 pch_spi_writereg(data
->master
, PCH_SSNXCR
, SSN_NO_CONTROL
);
1172 /* If we are already processing a message get the next
1173 transfer structure from the message otherwise retrieve
1174 the 1st transfer request from the message. */
1175 spin_lock(&data
->lock
);
1176 if (data
->cur_trans
== NULL
) {
1178 list_entry(data
->current_msg
->transfers
.next
,
1179 struct spi_transfer
, transfer_list
);
1180 dev_dbg(&data
->master
->dev
,
1181 "%s :Getting 1st transfer message\n",
1185 list_entry(data
->cur_trans
->transfer_list
.next
,
1186 struct spi_transfer
, transfer_list
);
1187 dev_dbg(&data
->master
->dev
,
1188 "%s :Getting next transfer message\n",
1191 spin_unlock(&data
->lock
);
1193 if (!data
->cur_trans
->len
)
1195 cnt
= (data
->cur_trans
->len
- 1) / PCH_BUF_SIZE
+ 1;
1196 data
->save_total_len
= data
->cur_trans
->len
;
1197 if (data
->use_dma
) {
1199 char *save_rx_buf
= data
->cur_trans
->rx_buf
;
1200 for (i
= 0; i
< cnt
; i
++) {
1201 pch_spi_handle_dma(data
, &bpw
);
1202 if (!pch_spi_start_transfer(data
)) {
1203 data
->transfer_complete
= true;
1204 data
->current_msg
->status
= -EIO
;
1205 data
->current_msg
->complete
1206 (data
->current_msg
->context
);
1207 data
->bcurrent_msg_processing
= false;
1208 data
->current_msg
= NULL
;
1209 data
->cur_trans
= NULL
;
1212 pch_spi_copy_rx_data_for_dma(data
, bpw
);
1214 data
->cur_trans
->rx_buf
= save_rx_buf
;
1216 pch_spi_set_tx(data
, &bpw
);
1217 pch_spi_set_ir(data
);
1218 pch_spi_copy_rx_data(data
, bpw
);
1219 kfree(data
->pkt_rx_buff
);
1220 data
->pkt_rx_buff
= NULL
;
1221 kfree(data
->pkt_tx_buff
);
1222 data
->pkt_tx_buff
= NULL
;
1224 /* increment message count */
1225 data
->cur_trans
->len
= data
->save_total_len
;
1226 data
->current_msg
->actual_length
+= data
->cur_trans
->len
;
1228 dev_dbg(&data
->master
->dev
,
1229 "%s:data->current_msg->actual_length=%d\n",
1230 __func__
, data
->current_msg
->actual_length
);
1232 spi_transfer_delay_exec(data
->cur_trans
);
1234 spin_lock(&data
->lock
);
1236 /* No more transfer in this message. */
1237 if ((data
->cur_trans
->transfer_list
.next
) ==
1238 &(data
->current_msg
->transfers
)) {
1239 pch_spi_nomore_transfer(data
);
1242 spin_unlock(&data
->lock
);
1244 } while (data
->cur_trans
!= NULL
);
1247 pch_spi_writereg(data
->master
, PCH_SSNXCR
, SSN_HIGH
);
1249 pch_spi_release_dma(data
);
1252 static void pch_spi_free_resources(struct pch_spi_board_data
*board_dat
,
1253 struct pch_spi_data
*data
)
1255 dev_dbg(&board_dat
->pdev
->dev
, "%s ENTRY\n", __func__
);
1257 flush_work(&data
->work
);
1260 static int pch_spi_get_resources(struct pch_spi_board_data
*board_dat
,
1261 struct pch_spi_data
*data
)
1263 dev_dbg(&board_dat
->pdev
->dev
, "%s ENTRY\n", __func__
);
1265 /* reset PCH SPI h/w */
1266 pch_spi_reset(data
->master
);
1267 dev_dbg(&board_dat
->pdev
->dev
,
1268 "%s pch_spi_reset invoked successfully\n", __func__
);
1270 dev_dbg(&board_dat
->pdev
->dev
, "%s data->irq_reg_sts=true\n", __func__
);
1275 static void pch_free_dma_buf(struct pch_spi_board_data
*board_dat
,
1276 struct pch_spi_data
*data
)
1278 struct pch_spi_dma_ctrl
*dma
;
1281 if (dma
->tx_buf_dma
)
1282 dma_free_coherent(&board_dat
->pdev
->dev
, PCH_BUF_SIZE
,
1283 dma
->tx_buf_virt
, dma
->tx_buf_dma
);
1284 if (dma
->rx_buf_dma
)
1285 dma_free_coherent(&board_dat
->pdev
->dev
, PCH_BUF_SIZE
,
1286 dma
->rx_buf_virt
, dma
->rx_buf_dma
);
1289 static int pch_alloc_dma_buf(struct pch_spi_board_data
*board_dat
,
1290 struct pch_spi_data
*data
)
1292 struct pch_spi_dma_ctrl
*dma
;
1297 /* Get Consistent memory for Tx DMA */
1298 dma
->tx_buf_virt
= dma_alloc_coherent(&board_dat
->pdev
->dev
,
1299 PCH_BUF_SIZE
, &dma
->tx_buf_dma
, GFP_KERNEL
);
1300 if (!dma
->tx_buf_virt
)
1303 /* Get Consistent memory for Rx DMA */
1304 dma
->rx_buf_virt
= dma_alloc_coherent(&board_dat
->pdev
->dev
,
1305 PCH_BUF_SIZE
, &dma
->rx_buf_dma
, GFP_KERNEL
);
1306 if (!dma
->rx_buf_virt
)
1312 static int pch_spi_pd_probe(struct platform_device
*plat_dev
)
1315 struct spi_master
*master
;
1316 struct pch_spi_board_data
*board_dat
= dev_get_platdata(&plat_dev
->dev
);
1317 struct pch_spi_data
*data
;
1319 dev_dbg(&plat_dev
->dev
, "%s:debug\n", __func__
);
1321 master
= spi_alloc_master(&board_dat
->pdev
->dev
,
1322 sizeof(struct pch_spi_data
));
1324 dev_err(&plat_dev
->dev
, "spi_alloc_master[%d] failed.\n",
1329 data
= spi_master_get_devdata(master
);
1330 data
->master
= master
;
1332 platform_set_drvdata(plat_dev
, data
);
1334 /* baseaddress + address offset) */
1335 data
->io_base_addr
= pci_resource_start(board_dat
->pdev
, 1) +
1336 PCH_ADDRESS_SIZE
* plat_dev
->id
;
1337 data
->io_remap_addr
= pci_iomap(board_dat
->pdev
, 1, 0);
1338 if (!data
->io_remap_addr
) {
1339 dev_err(&plat_dev
->dev
, "%s pci_iomap failed\n", __func__
);
1343 data
->io_remap_addr
+= PCH_ADDRESS_SIZE
* plat_dev
->id
;
1345 dev_dbg(&plat_dev
->dev
, "[ch%d] remap_addr=%p\n",
1346 plat_dev
->id
, data
->io_remap_addr
);
1348 /* initialize members of SPI master */
1349 master
->num_chipselect
= PCH_MAX_CS
;
1350 master
->transfer
= pch_spi_transfer
;
1351 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
1352 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
1353 master
->max_speed_hz
= PCH_MAX_BAUDRATE
;
1355 data
->board_dat
= board_dat
;
1356 data
->plat_dev
= plat_dev
;
1357 data
->n_curnt_chip
= 255;
1358 data
->status
= STATUS_RUNNING
;
1359 data
->ch
= plat_dev
->id
;
1360 data
->use_dma
= use_dma
;
1362 INIT_LIST_HEAD(&data
->queue
);
1363 spin_lock_init(&data
->lock
);
1364 INIT_WORK(&data
->work
, pch_spi_process_messages
);
1365 init_waitqueue_head(&data
->wait
);
1367 ret
= pch_spi_get_resources(board_dat
, data
);
1369 dev_err(&plat_dev
->dev
, "%s fail(retval=%d)\n", __func__
, ret
);
1370 goto err_spi_get_resources
;
1373 ret
= request_irq(board_dat
->pdev
->irq
, pch_spi_handler
,
1374 IRQF_SHARED
, KBUILD_MODNAME
, data
);
1376 dev_err(&plat_dev
->dev
,
1377 "%s request_irq failed\n", __func__
);
1378 goto err_request_irq
;
1380 data
->irq_reg_sts
= true;
1382 pch_spi_set_master_mode(master
);
1385 dev_info(&plat_dev
->dev
, "Use DMA for data transfers\n");
1386 ret
= pch_alloc_dma_buf(board_dat
, data
);
1388 goto err_spi_register_master
;
1391 ret
= spi_register_master(master
);
1393 dev_err(&plat_dev
->dev
,
1394 "%s spi_register_master FAILED\n", __func__
);
1395 goto err_spi_register_master
;
1400 err_spi_register_master
:
1401 pch_free_dma_buf(board_dat
, data
);
1402 free_irq(board_dat
->pdev
->irq
, data
);
1404 pch_spi_free_resources(board_dat
, data
);
1405 err_spi_get_resources
:
1406 pci_iounmap(board_dat
->pdev
, data
->io_remap_addr
);
1408 spi_master_put(master
);
1413 static int pch_spi_pd_remove(struct platform_device
*plat_dev
)
1415 struct pch_spi_board_data
*board_dat
= dev_get_platdata(&plat_dev
->dev
);
1416 struct pch_spi_data
*data
= platform_get_drvdata(plat_dev
);
1418 unsigned long flags
;
1420 dev_dbg(&plat_dev
->dev
, "%s:[ch%d] irq=%d\n",
1421 __func__
, plat_dev
->id
, board_dat
->pdev
->irq
);
1424 pch_free_dma_buf(board_dat
, data
);
1426 /* check for any pending messages; no action is taken if the queue
1427 * is still full; but at least we tried. Unload anyway */
1429 spin_lock_irqsave(&data
->lock
, flags
);
1430 data
->status
= STATUS_EXITING
;
1431 while ((list_empty(&data
->queue
) == 0) && --count
) {
1432 dev_dbg(&board_dat
->pdev
->dev
, "%s :queue not empty\n",
1434 spin_unlock_irqrestore(&data
->lock
, flags
);
1435 msleep(PCH_SLEEP_TIME
);
1436 spin_lock_irqsave(&data
->lock
, flags
);
1438 spin_unlock_irqrestore(&data
->lock
, flags
);
1440 pch_spi_free_resources(board_dat
, data
);
1441 /* disable interrupts & free IRQ */
1442 if (data
->irq_reg_sts
) {
1443 /* disable interrupts */
1444 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, 0, PCH_ALL
);
1445 data
->irq_reg_sts
= false;
1446 free_irq(board_dat
->pdev
->irq
, data
);
1449 pci_iounmap(board_dat
->pdev
, data
->io_remap_addr
);
1450 spi_unregister_master(data
->master
);
1455 static int pch_spi_pd_suspend(struct platform_device
*pd_dev
,
1459 struct pch_spi_board_data
*board_dat
= dev_get_platdata(&pd_dev
->dev
);
1460 struct pch_spi_data
*data
= platform_get_drvdata(pd_dev
);
1462 dev_dbg(&pd_dev
->dev
, "%s ENTRY\n", __func__
);
1465 dev_err(&pd_dev
->dev
,
1466 "%s pci_get_drvdata returned NULL\n", __func__
);
1470 /* check if the current message is processed:
1471 Only after thats done the transfer will be suspended */
1473 while ((--count
) > 0) {
1474 if (!(data
->bcurrent_msg_processing
))
1476 msleep(PCH_SLEEP_TIME
);
1480 if (data
->irq_reg_sts
) {
1481 /* disable all interrupts */
1482 pch_spi_setclr_reg(data
->master
, PCH_SPCR
, 0, PCH_ALL
);
1483 pch_spi_reset(data
->master
);
1484 free_irq(board_dat
->pdev
->irq
, data
);
1486 data
->irq_reg_sts
= false;
1487 dev_dbg(&pd_dev
->dev
,
1488 "%s free_irq invoked successfully.\n", __func__
);
1494 static int pch_spi_pd_resume(struct platform_device
*pd_dev
)
1496 struct pch_spi_board_data
*board_dat
= dev_get_platdata(&pd_dev
->dev
);
1497 struct pch_spi_data
*data
= platform_get_drvdata(pd_dev
);
1501 dev_err(&pd_dev
->dev
,
1502 "%s pci_get_drvdata returned NULL\n", __func__
);
1506 if (!data
->irq_reg_sts
) {
1508 retval
= request_irq(board_dat
->pdev
->irq
, pch_spi_handler
,
1509 IRQF_SHARED
, KBUILD_MODNAME
, data
);
1511 dev_err(&pd_dev
->dev
,
1512 "%s request_irq failed\n", __func__
);
1516 /* reset PCH SPI h/w */
1517 pch_spi_reset(data
->master
);
1518 pch_spi_set_master_mode(data
->master
);
1519 data
->irq_reg_sts
= true;
1524 #define pch_spi_pd_suspend NULL
1525 #define pch_spi_pd_resume NULL
1528 static struct platform_driver pch_spi_pd_driver
= {
1532 .probe
= pch_spi_pd_probe
,
1533 .remove
= pch_spi_pd_remove
,
1534 .suspend
= pch_spi_pd_suspend
,
1535 .resume
= pch_spi_pd_resume
1538 static int pch_spi_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1540 struct pch_spi_board_data
*board_dat
;
1541 struct platform_device
*pd_dev
= NULL
;
1544 struct pch_pd_dev_save
*pd_dev_save
;
1546 pd_dev_save
= kzalloc(sizeof(*pd_dev_save
), GFP_KERNEL
);
1550 board_dat
= kzalloc(sizeof(*board_dat
), GFP_KERNEL
);
1556 retval
= pci_request_regions(pdev
, KBUILD_MODNAME
);
1558 dev_err(&pdev
->dev
, "%s request_region failed\n", __func__
);
1559 goto pci_request_regions
;
1562 board_dat
->pdev
= pdev
;
1563 board_dat
->num
= id
->driver_data
;
1564 pd_dev_save
->num
= id
->driver_data
;
1565 pd_dev_save
->board_dat
= board_dat
;
1567 retval
= pci_enable_device(pdev
);
1569 dev_err(&pdev
->dev
, "%s pci_enable_device failed\n", __func__
);
1570 goto pci_enable_device
;
1573 for (i
= 0; i
< board_dat
->num
; i
++) {
1574 pd_dev
= platform_device_alloc("pch-spi", i
);
1576 dev_err(&pdev
->dev
, "platform_device_alloc failed\n");
1578 goto err_platform_device
;
1580 pd_dev_save
->pd_save
[i
] = pd_dev
;
1581 pd_dev
->dev
.parent
= &pdev
->dev
;
1583 retval
= platform_device_add_data(pd_dev
, board_dat
,
1584 sizeof(*board_dat
));
1587 "platform_device_add_data failed\n");
1588 platform_device_put(pd_dev
);
1589 goto err_platform_device
;
1592 retval
= platform_device_add(pd_dev
);
1594 dev_err(&pdev
->dev
, "platform_device_add failed\n");
1595 platform_device_put(pd_dev
);
1596 goto err_platform_device
;
1600 pci_set_drvdata(pdev
, pd_dev_save
);
1604 err_platform_device
:
1606 platform_device_unregister(pd_dev_save
->pd_save
[i
]);
1607 pci_disable_device(pdev
);
1609 pci_release_regions(pdev
);
1610 pci_request_regions
:
1618 static void pch_spi_remove(struct pci_dev
*pdev
)
1621 struct pch_pd_dev_save
*pd_dev_save
= pci_get_drvdata(pdev
);
1623 dev_dbg(&pdev
->dev
, "%s ENTRY:pdev=%p\n", __func__
, pdev
);
1625 for (i
= 0; i
< pd_dev_save
->num
; i
++)
1626 platform_device_unregister(pd_dev_save
->pd_save
[i
]);
1628 pci_disable_device(pdev
);
1629 pci_release_regions(pdev
);
1630 kfree(pd_dev_save
->board_dat
);
1635 static int pch_spi_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1638 struct pch_pd_dev_save
*pd_dev_save
= pci_get_drvdata(pdev
);
1640 dev_dbg(&pdev
->dev
, "%s ENTRY\n", __func__
);
1642 pd_dev_save
->board_dat
->suspend_sts
= true;
1644 /* save config space */
1645 retval
= pci_save_state(pdev
);
1647 pci_enable_wake(pdev
, PCI_D3hot
, 0);
1648 pci_disable_device(pdev
);
1649 pci_set_power_state(pdev
, PCI_D3hot
);
1651 dev_err(&pdev
->dev
, "%s pci_save_state failed\n", __func__
);
1657 static int pch_spi_resume(struct pci_dev
*pdev
)
1660 struct pch_pd_dev_save
*pd_dev_save
= pci_get_drvdata(pdev
);
1661 dev_dbg(&pdev
->dev
, "%s ENTRY\n", __func__
);
1663 pci_set_power_state(pdev
, PCI_D0
);
1664 pci_restore_state(pdev
);
1666 retval
= pci_enable_device(pdev
);
1669 "%s pci_enable_device failed\n", __func__
);
1671 pci_enable_wake(pdev
, PCI_D3hot
, 0);
1673 /* set suspend status to false */
1674 pd_dev_save
->board_dat
->suspend_sts
= false;
1680 #define pch_spi_suspend NULL
1681 #define pch_spi_resume NULL
1685 static struct pci_driver pch_spi_pcidev_driver
= {
1687 .id_table
= pch_spi_pcidev_id
,
1688 .probe
= pch_spi_probe
,
1689 .remove
= pch_spi_remove
,
1690 .suspend
= pch_spi_suspend
,
1691 .resume
= pch_spi_resume
,
1694 static int __init
pch_spi_init(void)
1697 ret
= platform_driver_register(&pch_spi_pd_driver
);
1701 ret
= pci_register_driver(&pch_spi_pcidev_driver
);
1703 platform_driver_unregister(&pch_spi_pd_driver
);
1709 module_init(pch_spi_init
);
1711 static void __exit
pch_spi_exit(void)
1713 pci_unregister_driver(&pch_spi_pcidev_driver
);
1714 platform_driver_unregister(&pch_spi_pd_driver
);
1716 module_exit(pch_spi_exit
);
1718 module_param(use_dma
, int, 0644);
1719 MODULE_PARM_DESC(use_dma
,
1720 "to use DMA for data transfers pass 1 else 0; default 1");
1722 MODULE_LICENSE("GPL");
1723 MODULE_DESCRIPTION("Intel EG20T PCH/LAPIS Semiconductor ML7xxx IOH SPI Driver");
1724 MODULE_DEVICE_TABLE(pci
, pch_spi_pcidev_id
);