1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Internal SPI driver for NetUP Universal Dual DVB-CI
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
9 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
12 #include "netup_unidvb.h"
13 #include <linux/spi/spi.h>
14 #include <linux/spi/flash.h>
15 #include <linux/mtd/partitions.h>
16 #include <mtd/mtd-abi.h>
18 #define NETUP_SPI_CTRL_IRQ 0x1000
19 #define NETUP_SPI_CTRL_IMASK 0x2000
20 #define NETUP_SPI_CTRL_START 0x8000
21 #define NETUP_SPI_CTRL_LAST_CS 0x4000
23 #define NETUP_SPI_TIMEOUT 6000
25 enum netup_spi_state
{
30 struct netup_spi_regs
{
34 } __packed
__aligned(1);
38 struct spi_master
*master
;
39 struct netup_spi_regs __iomem
*regs
;
42 wait_queue_head_t waitq
;
43 enum netup_spi_state state
;
46 static char netup_spi_name
[64] = "fpga";
48 static struct mtd_partition netup_spi_flash_partitions
= {
49 .name
= netup_spi_name
,
50 .size
= 0x1000000, /* 16MB */
52 .mask_flags
= MTD_CAP_ROM
55 static struct flash_platform_data spi_flash_data
= {
56 .name
= "netup0_m25p128",
57 .parts
= &netup_spi_flash_partitions
,
61 static struct spi_board_info netup_spi_board
= {
62 .modalias
= "m25p128",
63 .max_speed_hz
= 11000000,
66 .platform_data
= &spi_flash_data
,
69 irqreturn_t
netup_spi_interrupt(struct netup_spi
*spi
)
77 spin_lock_irqsave(&spi
->lock
, flags
);
78 reg
= readw(&spi
->regs
->control_stat
);
79 if (!(reg
& NETUP_SPI_CTRL_IRQ
)) {
80 spin_unlock_irqrestore(&spi
->lock
, flags
);
81 dev_dbg(&spi
->master
->dev
,
82 "%s(): not mine interrupt\n", __func__
);
85 writew(reg
| NETUP_SPI_CTRL_IRQ
, &spi
->regs
->control_stat
);
86 reg
= readw(&spi
->regs
->control_stat
);
87 writew(reg
& ~NETUP_SPI_CTRL_IMASK
, &spi
->regs
->control_stat
);
88 spi
->state
= SPI_STATE_DONE
;
90 spin_unlock_irqrestore(&spi
->lock
, flags
);
91 dev_dbg(&spi
->master
->dev
,
92 "%s(): SPI interrupt handled\n", __func__
);
96 static int netup_spi_transfer(struct spi_master
*master
,
97 struct spi_message
*msg
)
99 struct netup_spi
*spi
= spi_master_get_devdata(master
);
100 struct spi_transfer
*t
;
105 writew(NETUP_SPI_CTRL_LAST_CS
, &spi
->regs
->control_stat
);
106 writew(0, &spi
->regs
->control_stat
);
107 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
110 u32 frag_offset
= t
->len
- tr_size
;
111 u32 frag_size
= (tr_size
> sizeof(spi
->regs
->data
)) ?
112 sizeof(spi
->regs
->data
) : tr_size
;
115 if (list_is_last(&t
->transfer_list
,
117 frag_offset
+ frag_size
== t
->len
) {
121 memcpy_toio(spi
->regs
->data
,
122 t
->tx_buf
+ frag_offset
,
125 memset_io(spi
->regs
->data
,
128 spi
->state
= SPI_STATE_START
;
129 writew((frag_size
& 0x3ff) |
130 NETUP_SPI_CTRL_IMASK
|
131 NETUP_SPI_CTRL_START
|
132 (frag_last
? NETUP_SPI_CTRL_LAST_CS
: 0),
133 &spi
->regs
->control_stat
);
134 dev_dbg(&spi
->master
->dev
,
135 "%s(): control_stat 0x%04x\n",
136 __func__
, readw(&spi
->regs
->control_stat
));
137 wait_event_timeout(spi
->waitq
,
138 spi
->state
!= SPI_STATE_START
,
139 msecs_to_jiffies(NETUP_SPI_TIMEOUT
));
140 if (spi
->state
== SPI_STATE_DONE
) {
142 memcpy_fromio(t
->rx_buf
+ frag_offset
,
143 spi
->regs
->data
, frag_size
);
146 if (spi
->state
== SPI_STATE_START
) {
147 dev_dbg(&spi
->master
->dev
,
148 "%s(): transfer timeout\n",
151 dev_dbg(&spi
->master
->dev
,
152 "%s(): invalid state %d\n",
153 __func__
, spi
->state
);
158 tr_size
-= frag_size
;
159 msg
->actual_length
+= frag_size
;
163 msg
->status
= result
;
164 spi_finalize_current_message(master
);
168 static int netup_spi_setup(struct spi_device
*spi
)
173 int netup_spi_init(struct netup_unidvb_dev
*ndev
)
175 struct spi_master
*master
;
176 struct netup_spi
*nspi
;
178 master
= devm_spi_alloc_master(&ndev
->pci_dev
->dev
,
179 sizeof(struct netup_spi
));
181 dev_err(&ndev
->pci_dev
->dev
,
182 "%s(): unable to alloc SPI master\n", __func__
);
185 nspi
= spi_master_get_devdata(master
);
186 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
187 master
->bus_num
= -1;
188 master
->num_chipselect
= 1;
189 master
->transfer_one_message
= netup_spi_transfer
;
190 master
->setup
= netup_spi_setup
;
191 spin_lock_init(&nspi
->lock
);
192 init_waitqueue_head(&nspi
->waitq
);
193 nspi
->master
= master
;
194 nspi
->regs
= (struct netup_spi_regs __iomem
*)(ndev
->bmmio0
+ 0x4000);
195 writew(2, &nspi
->regs
->clock_divider
);
196 writew(NETUP_UNIDVB_IRQ_SPI
, ndev
->bmmio0
+ REG_IMASK_SET
);
198 if (spi_register_master(master
)) {
200 dev_err(&ndev
->pci_dev
->dev
,
201 "%s(): unable to register SPI bus\n", __func__
);
204 snprintf(netup_spi_name
,
205 sizeof(netup_spi_name
),
206 "fpga_%02x:%02x.%01x",
210 if (!spi_new_device(master
, &netup_spi_board
)) {
211 spi_unregister_master(master
);
213 dev_err(&ndev
->pci_dev
->dev
,
214 "%s(): unable to create SPI device\n", __func__
);
217 dev_dbg(&ndev
->pci_dev
->dev
, "%s(): SPI init OK\n", __func__
);
221 void netup_spi_release(struct netup_unidvb_dev
*ndev
)
225 struct netup_spi
*spi
= ndev
->spi
;
230 spi_unregister_master(spi
->master
);
231 spin_lock_irqsave(&spi
->lock
, flags
);
232 reg
= readw(&spi
->regs
->control_stat
);
233 writew(reg
| NETUP_SPI_CTRL_IRQ
, &spi
->regs
->control_stat
);
234 reg
= readw(&spi
->regs
->control_stat
);
235 writew(reg
& ~NETUP_SPI_CTRL_IMASK
, &spi
->regs
->control_stat
);
236 spin_unlock_irqrestore(&spi
->lock
, flags
);