Lines Matching +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Internal SPI driver for NetUP Universal Dual DVB-CI
13 #include <linux/spi/spi.h>
14 #include <linux/spi/flash.h>
16 #include <mtd/mtd-abi.h>
69 irqreturn_t netup_spi_interrupt(struct netup_spi *spi) in netup_spi_interrupt() argument
74 if (!spi) in netup_spi_interrupt()
77 spin_lock_irqsave(&spi->lock, flags); in netup_spi_interrupt()
78 reg = readw(&spi->regs->control_stat); in netup_spi_interrupt()
80 spin_unlock_irqrestore(&spi->lock, flags); in netup_spi_interrupt()
81 dev_dbg(&spi->ctlr->dev, in netup_spi_interrupt()
85 writew(reg | NETUP_SPI_CTRL_IRQ, &spi->regs->control_stat); in netup_spi_interrupt()
86 reg = readw(&spi->regs->control_stat); in netup_spi_interrupt()
87 writew(reg & ~NETUP_SPI_CTRL_IMASK, &spi->regs->control_stat); in netup_spi_interrupt()
88 spi->state = SPI_STATE_DONE; in netup_spi_interrupt()
89 wake_up(&spi->waitq); in netup_spi_interrupt()
90 spin_unlock_irqrestore(&spi->lock, flags); in netup_spi_interrupt()
91 dev_dbg(&spi->ctlr->dev, in netup_spi_interrupt()
92 "%s(): SPI interrupt handled\n", __func__); in netup_spi_interrupt()
99 struct netup_spi *spi = spi_controller_get_devdata(ctlr); in netup_spi_transfer() local
105 writew(NETUP_SPI_CTRL_LAST_CS, &spi->regs->control_stat); in netup_spi_transfer()
106 writew(0, &spi->regs->control_stat); in netup_spi_transfer()
107 list_for_each_entry(t, &msg->transfers, transfer_list) { in netup_spi_transfer()
108 tr_size = t->len; in netup_spi_transfer()
110 u32 frag_offset = t->len - tr_size; in netup_spi_transfer()
111 u32 frag_size = (tr_size > sizeof(spi->regs->data)) ? in netup_spi_transfer()
112 sizeof(spi->regs->data) : tr_size; in netup_spi_transfer()
115 if (list_is_last(&t->transfer_list, in netup_spi_transfer()
116 &msg->transfers) && in netup_spi_transfer()
117 frag_offset + frag_size == t->len) { in netup_spi_transfer()
120 if (t->tx_buf) { in netup_spi_transfer()
121 memcpy_toio(spi->regs->data, in netup_spi_transfer()
122 t->tx_buf + frag_offset, in netup_spi_transfer()
125 memset_io(spi->regs->data, in netup_spi_transfer()
128 spi->state = SPI_STATE_START; in netup_spi_transfer()
133 &spi->regs->control_stat); in netup_spi_transfer()
134 dev_dbg(&spi->ctlr->dev, in netup_spi_transfer()
136 __func__, readw(&spi->regs->control_stat)); in netup_spi_transfer()
137 wait_event_timeout(spi->waitq, in netup_spi_transfer()
138 spi->state != SPI_STATE_START, in netup_spi_transfer()
140 if (spi->state == SPI_STATE_DONE) { in netup_spi_transfer()
141 if (t->rx_buf) { in netup_spi_transfer()
142 memcpy_fromio(t->rx_buf + frag_offset, in netup_spi_transfer()
143 spi->regs->data, frag_size); in netup_spi_transfer()
146 if (spi->state == SPI_STATE_START) { in netup_spi_transfer()
147 dev_dbg(&spi->ctlr->dev, in netup_spi_transfer()
151 dev_dbg(&spi->ctlr->dev, in netup_spi_transfer()
153 __func__, spi->state); in netup_spi_transfer()
155 result = -EIO; in netup_spi_transfer()
158 tr_size -= frag_size; in netup_spi_transfer()
159 msg->actual_length += frag_size; in netup_spi_transfer()
163 msg->status = result; in netup_spi_transfer()
168 static int netup_spi_setup(struct spi_device *spi) in netup_spi_setup() argument
178 ctlr = devm_spi_alloc_master(&ndev->pci_dev->dev, in netup_spi_init()
181 dev_err(&ndev->pci_dev->dev, in netup_spi_init()
182 "%s(): unable to alloc SPI master\n", __func__); in netup_spi_init()
183 return -EINVAL; in netup_spi_init()
186 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; in netup_spi_init()
187 ctlr->bus_num = -1; in netup_spi_init()
188 ctlr->num_chipselect = 1; in netup_spi_init()
189 ctlr->transfer_one_message = netup_spi_transfer; in netup_spi_init()
190 ctlr->setup = netup_spi_setup; in netup_spi_init()
191 spin_lock_init(&nspi->lock); in netup_spi_init()
192 init_waitqueue_head(&nspi->waitq); in netup_spi_init()
193 nspi->ctlr = ctlr; in netup_spi_init()
194 nspi->regs = (struct netup_spi_regs __iomem *)(ndev->bmmio0 + 0x4000); in netup_spi_init()
195 writew(2, &nspi->regs->clock_divider); in netup_spi_init()
196 writew(NETUP_UNIDVB_IRQ_SPI, ndev->bmmio0 + REG_IMASK_SET); in netup_spi_init()
197 ndev->spi = nspi; in netup_spi_init()
199 ndev->spi = NULL; in netup_spi_init()
200 dev_err(&ndev->pci_dev->dev, in netup_spi_init()
201 "%s(): unable to register SPI bus\n", __func__); in netup_spi_init()
202 return -EINVAL; in netup_spi_init()
207 ndev->pci_bus, in netup_spi_init()
208 ndev->pci_slot, in netup_spi_init()
209 ndev->pci_func); in netup_spi_init()
212 ndev->spi = NULL; in netup_spi_init()
213 dev_err(&ndev->pci_dev->dev, in netup_spi_init()
214 "%s(): unable to create SPI device\n", __func__); in netup_spi_init()
215 return -EINVAL; in netup_spi_init()
217 dev_dbg(&ndev->pci_dev->dev, "%s(): SPI init OK\n", __func__); in netup_spi_init()
225 struct netup_spi *spi = ndev->spi; in netup_spi_release() local
227 if (!spi) in netup_spi_release()
230 spi_unregister_controller(spi->ctlr); in netup_spi_release()
231 spin_lock_irqsave(&spi->lock, flags); in netup_spi_release()
232 reg = readw(&spi->regs->control_stat); in netup_spi_release()
233 writew(reg | NETUP_SPI_CTRL_IRQ, &spi->regs->control_stat); in netup_spi_release()
234 reg = readw(&spi->regs->control_stat); in netup_spi_release()
235 writew(reg & ~NETUP_SPI_CTRL_IMASK, &spi->regs->control_stat); in netup_spi_release()
236 spin_unlock_irqrestore(&spi->lock, flags); in netup_spi_release()
237 ndev->spi = NULL; in netup_spi_release()