4 * Internal SPI driver for NetUP Universal Dual DVB-CI
6 * Copyright (C) 2014 NetUP Inc.
7 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
8 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
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.
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.
21 #include "netup_unidvb.h"
22 #include <linux/spi/spi.h>
23 #include <linux/spi/flash.h>
24 #include <linux/mtd/partitions.h>
25 #include <mtd/mtd-abi.h>
27 #define NETUP_SPI_CTRL_IRQ 0x1000
28 #define NETUP_SPI_CTRL_IMASK 0x2000
29 #define NETUP_SPI_CTRL_START 0x8000
30 #define NETUP_SPI_CTRL_LAST_CS 0x4000
32 #define NETUP_SPI_TIMEOUT 6000
34 enum netup_spi_state
{
39 struct netup_spi_regs
{
43 } __packed
__aligned(1);
47 struct spi_master
*master
;
48 struct netup_spi_regs __iomem
*regs
;
51 wait_queue_head_t waitq
;
52 enum netup_spi_state state
;
55 static char netup_spi_name
[64] = "fpga";
57 static struct mtd_partition netup_spi_flash_partitions
= {
58 .name
= netup_spi_name
,
59 .size
= 0x1000000, /* 16MB */
61 .mask_flags
= MTD_CAP_ROM
64 static struct flash_platform_data spi_flash_data
= {
65 .name
= "netup0_m25p128",
66 .parts
= &netup_spi_flash_partitions
,
70 static struct spi_board_info netup_spi_board
= {
71 .modalias
= "m25p128",
72 .max_speed_hz
= 11000000,
75 .platform_data
= &spi_flash_data
,
78 irqreturn_t
netup_spi_interrupt(struct netup_spi
*spi
)
86 spin_lock_irqsave(&spi
->lock
, flags
);
87 reg
= readw(&spi
->regs
->control_stat
);
88 if (!(reg
& NETUP_SPI_CTRL_IRQ
)) {
89 spin_unlock_irqrestore(&spi
->lock
, flags
);
90 dev_dbg(&spi
->master
->dev
,
91 "%s(): not mine interrupt\n", __func__
);
94 writew(reg
| NETUP_SPI_CTRL_IRQ
, &spi
->regs
->control_stat
);
95 reg
= readw(&spi
->regs
->control_stat
);
96 writew(reg
& ~NETUP_SPI_CTRL_IMASK
, &spi
->regs
->control_stat
);
97 spi
->state
= SPI_STATE_DONE
;
99 spin_unlock_irqrestore(&spi
->lock
, flags
);
100 dev_dbg(&spi
->master
->dev
,
101 "%s(): SPI interrupt handled\n", __func__
);
105 static int netup_spi_transfer(struct spi_master
*master
,
106 struct spi_message
*msg
)
108 struct netup_spi
*spi
= spi_master_get_devdata(master
);
109 struct spi_transfer
*t
;
114 writew(NETUP_SPI_CTRL_LAST_CS
, &spi
->regs
->control_stat
);
115 writew(0, &spi
->regs
->control_stat
);
116 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
119 u32 frag_offset
= t
->len
- tr_size
;
120 u32 frag_size
= (tr_size
> sizeof(spi
->regs
->data
)) ?
121 sizeof(spi
->regs
->data
) : tr_size
;
124 if (list_is_last(&t
->transfer_list
,
126 frag_offset
+ frag_size
== t
->len
) {
130 memcpy_toio(spi
->regs
->data
,
131 t
->tx_buf
+ frag_offset
,
134 memset_io(spi
->regs
->data
,
137 spi
->state
= SPI_STATE_START
;
138 writew((frag_size
& 0x3ff) |
139 NETUP_SPI_CTRL_IMASK
|
140 NETUP_SPI_CTRL_START
|
141 (frag_last
? NETUP_SPI_CTRL_LAST_CS
: 0),
142 &spi
->regs
->control_stat
);
143 dev_dbg(&spi
->master
->dev
,
144 "%s(): control_stat 0x%04x\n",
145 __func__
, readw(&spi
->regs
->control_stat
));
146 wait_event_timeout(spi
->waitq
,
147 spi
->state
!= SPI_STATE_START
,
148 msecs_to_jiffies(NETUP_SPI_TIMEOUT
));
149 if (spi
->state
== SPI_STATE_DONE
) {
151 memcpy_fromio(t
->rx_buf
+ frag_offset
,
152 spi
->regs
->data
, frag_size
);
155 if (spi
->state
== SPI_STATE_START
) {
156 dev_dbg(&spi
->master
->dev
,
157 "%s(): transfer timeout\n",
160 dev_dbg(&spi
->master
->dev
,
161 "%s(): invalid state %d\n",
162 __func__
, spi
->state
);
167 tr_size
-= frag_size
;
168 msg
->actual_length
+= frag_size
;
172 msg
->status
= result
;
173 spi_finalize_current_message(master
);
177 static int netup_spi_setup(struct spi_device
*spi
)
182 int netup_spi_init(struct netup_unidvb_dev
*ndev
)
184 struct spi_master
*master
;
185 struct netup_spi
*nspi
;
187 master
= spi_alloc_master(&ndev
->pci_dev
->dev
,
188 sizeof(struct netup_spi
));
190 dev_err(&ndev
->pci_dev
->dev
,
191 "%s(): unable to alloc SPI master\n", __func__
);
194 nspi
= spi_master_get_devdata(master
);
195 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
196 master
->bus_num
= -1;
197 master
->num_chipselect
= 1;
198 master
->transfer_one_message
= netup_spi_transfer
;
199 master
->setup
= netup_spi_setup
;
200 spin_lock_init(&nspi
->lock
);
201 init_waitqueue_head(&nspi
->waitq
);
202 nspi
->master
= master
;
203 nspi
->regs
= (struct netup_spi_regs __iomem
*)(ndev
->bmmio0
+ 0x4000);
204 writew(2, &nspi
->regs
->clock_divider
);
205 writew(NETUP_UNIDVB_IRQ_SPI
, ndev
->bmmio0
+ REG_IMASK_SET
);
207 if (spi_register_master(master
)) {
209 dev_err(&ndev
->pci_dev
->dev
,
210 "%s(): unable to register SPI bus\n", __func__
);
213 snprintf(netup_spi_name
,
214 sizeof(netup_spi_name
),
215 "fpga_%02x:%02x.%01x",
219 if (!spi_new_device(master
, &netup_spi_board
)) {
221 dev_err(&ndev
->pci_dev
->dev
,
222 "%s(): unable to create SPI device\n", __func__
);
225 dev_dbg(&ndev
->pci_dev
->dev
, "%s(): SPI init OK\n", __func__
);
229 void netup_spi_release(struct netup_unidvb_dev
*ndev
)
233 struct netup_spi
*spi
= ndev
->spi
;
238 spin_lock_irqsave(&spi
->lock
, flags
);
239 reg
= readw(&spi
->regs
->control_stat
);
240 writew(reg
| NETUP_SPI_CTRL_IRQ
, &spi
->regs
->control_stat
);
241 reg
= readw(&spi
->regs
->control_stat
);
242 writew(reg
& ~NETUP_SPI_CTRL_IMASK
, &spi
->regs
->control_stat
);
243 spin_unlock_irqrestore(&spi
->lock
, flags
);
244 spi_unregister_master(spi
->master
);