1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2011 Renesas Solutions Corp.
7 * Based on pxa2xx_spi.c:
8 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/errno.h>
15 #include <linux/timer.h>
16 #include <linux/delay.h>
17 #include <linux/list.h>
18 #include <linux/workqueue.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
22 #include <linux/spi/spi.h>
24 #define SPI_SH_TBR 0x00
25 #define SPI_SH_RBR 0x00
26 #define SPI_SH_CR1 0x08
27 #define SPI_SH_CR2 0x10
28 #define SPI_SH_CR3 0x18
29 #define SPI_SH_CR4 0x20
30 #define SPI_SH_CR5 0x28
33 #define SPI_SH_TBE 0x80
34 #define SPI_SH_TBF 0x40
35 #define SPI_SH_RBE 0x20
36 #define SPI_SH_RBF 0x10
37 #define SPI_SH_PFONRD 0x08
38 #define SPI_SH_SSDB 0x04
39 #define SPI_SH_SSD 0x02
40 #define SPI_SH_SSA 0x01
43 #define SPI_SH_RSTF 0x80
44 #define SPI_SH_LOOPBK 0x40
45 #define SPI_SH_CPOL 0x20
46 #define SPI_SH_CPHA 0x10
47 #define SPI_SH_L1M0 0x08
50 #define SPI_SH_MAX_BYTE 0xFF
53 #define SPI_SH_TBEI 0x80
54 #define SPI_SH_TBFI 0x40
55 #define SPI_SH_RBEI 0x20
56 #define SPI_SH_RBFI 0x10
57 #define SPI_SH_WPABRT 0x04
58 #define SPI_SH_SSS 0x01
61 #define SPI_SH_P1L0 0x80
62 #define SPI_SH_PP1L0 0x40
63 #define SPI_SH_MUXI 0x20
64 #define SPI_SH_MUXIRQ 0x10
66 #define SPI_SH_FIFO_SIZE 32
67 #define SPI_SH_SEND_TIMEOUT (3 * HZ)
68 #define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
75 struct spi_master *master;
77 wait_queue_head_t wait;
81 static void spi_sh_write(struct spi_sh_data *ss, unsigned long data,
85 iowrite8(data, ss->addr + (offset >> 2));
86 else if (ss->width == 32)
87 iowrite32(data, ss->addr + offset);
90 static unsigned long spi_sh_read(struct spi_sh_data *ss, unsigned long offset)
93 return ioread8(ss->addr + (offset >> 2));
94 else if (ss->width == 32)
95 return ioread32(ss->addr + offset);
100 static void spi_sh_set_bit(struct spi_sh_data *ss, unsigned long val,
101 unsigned long offset)
105 tmp = spi_sh_read(ss, offset);
107 spi_sh_write(ss, tmp, offset);
110 static void spi_sh_clear_bit(struct spi_sh_data *ss, unsigned long val,
111 unsigned long offset)
115 tmp = spi_sh_read(ss, offset);
117 spi_sh_write(ss, tmp, offset);
120 static void clear_fifo(struct spi_sh_data *ss)
122 spi_sh_set_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
123 spi_sh_clear_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
126 static int spi_sh_wait_receive_buffer(struct spi_sh_data *ss)
128 int timeout = 100000;
130 while (spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
138 static int spi_sh_wait_write_buffer_empty(struct spi_sh_data *ss)
140 int timeout = 100000;
142 while (!(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBE)) {
150 static int spi_sh_send(struct spi_sh_data *ss, struct spi_message *mesg,
151 struct spi_transfer *t)
160 spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
162 data = (unsigned char *)t->tx_buf;
164 cur_len = min(SPI_SH_FIFO_SIZE, remain);
165 for (i = 0; i < cur_len &&
166 !(spi_sh_read(ss, SPI_SH_CR4) &
168 !(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBF);
170 spi_sh_write(ss, (unsigned long)data[i], SPI_SH_TBR);
172 if (spi_sh_read(ss, SPI_SH_CR4) & SPI_SH_WPABRT) {
173 /* Abort SPI operation */
174 spi_sh_set_bit(ss, SPI_SH_WPABRT, SPI_SH_CR4);
185 ss->cr1 &= ~SPI_SH_TBE;
186 spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
187 ret = wait_event_interruptible_timeout(ss->wait,
188 ss->cr1 & SPI_SH_TBE,
189 SPI_SH_SEND_TIMEOUT);
190 if (ret == 0 && !(ss->cr1 & SPI_SH_TBE)) {
191 printk(KERN_ERR "%s: timeout\n", __func__);
197 if (list_is_last(&t->transfer_list, &mesg->transfers)) {
198 spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
199 spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
201 ss->cr1 &= ~SPI_SH_TBE;
202 spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
203 ret = wait_event_interruptible_timeout(ss->wait,
204 ss->cr1 & SPI_SH_TBE,
205 SPI_SH_SEND_TIMEOUT);
206 if (ret == 0 && (ss->cr1 & SPI_SH_TBE)) {
207 printk(KERN_ERR "%s: timeout\n", __func__);
215 static int spi_sh_receive(struct spi_sh_data *ss, struct spi_message *mesg,
216 struct spi_transfer *t)
224 if (t->len > SPI_SH_MAX_BYTE)
225 spi_sh_write(ss, SPI_SH_MAX_BYTE, SPI_SH_CR3);
227 spi_sh_write(ss, t->len, SPI_SH_CR3);
229 spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
230 spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
232 spi_sh_wait_write_buffer_empty(ss);
234 data = (unsigned char *)t->rx_buf;
236 if (remain >= SPI_SH_FIFO_SIZE) {
237 ss->cr1 &= ~SPI_SH_RBF;
238 spi_sh_set_bit(ss, SPI_SH_RBF, SPI_SH_CR4);
239 ret = wait_event_interruptible_timeout(ss->wait,
240 ss->cr1 & SPI_SH_RBF,
241 SPI_SH_RECEIVE_TIMEOUT);
243 spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
244 printk(KERN_ERR "%s: timeout\n", __func__);
249 cur_len = min(SPI_SH_FIFO_SIZE, remain);
250 for (i = 0; i < cur_len; i++) {
251 if (spi_sh_wait_receive_buffer(ss))
253 data[i] = (unsigned char)spi_sh_read(ss, SPI_SH_RBR);
260 /* deassert CS when SPI is receiving. */
261 if (t->len > SPI_SH_MAX_BYTE) {
263 spi_sh_write(ss, 1, SPI_SH_CR3);
265 spi_sh_write(ss, 0, SPI_SH_CR3);
271 static int spi_sh_transfer_one_message(struct spi_controller *ctlr,
272 struct spi_message *mesg)
274 struct spi_sh_data *ss = spi_controller_get_devdata(ctlr);
275 struct spi_transfer *t;
278 pr_debug("%s: enter\n", __func__);
280 spi_sh_clear_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
282 list_for_each_entry(t, &mesg->transfers, transfer_list) {
283 pr_debug("tx_buf = %p, rx_buf = %p\n",
284 t->tx_buf, t->rx_buf);
285 pr_debug("len = %d, delay.value = %d\n",
286 t->len, t->delay.value);
289 ret = spi_sh_send(ss, mesg, t);
294 ret = spi_sh_receive(ss, mesg, t);
298 mesg->actual_length += t->len;
302 spi_finalize_current_message(ctlr);
305 spi_sh_set_bit(ss, SPI_SH_SSD, SPI_SH_CR1);
308 spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
317 spi_finalize_current_message(ctlr);
319 mesg->complete(mesg->context);
321 spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
328 static int spi_sh_setup(struct spi_device *spi)
330 struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
332 pr_debug("%s: enter\n", __func__);
334 spi_sh_write(ss, 0xfe, SPI_SH_CR1); /* SPI sycle stop */
335 spi_sh_write(ss, 0x00, SPI_SH_CR1); /* CR1 init */
336 spi_sh_write(ss, 0x00, SPI_SH_CR3); /* CR3 init */
341 spi_sh_write(ss, spi_sh_read(ss, SPI_SH_CR2) | 0x07, SPI_SH_CR2);
347 static void spi_sh_cleanup(struct spi_device *spi)
349 struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
351 pr_debug("%s: enter\n", __func__);
353 spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
357 static irqreturn_t spi_sh_irq(int irq, void *_ss)
359 struct spi_sh_data *ss = (struct spi_sh_data *)_ss;
362 cr1 = spi_sh_read(ss, SPI_SH_CR1);
363 if (cr1 & SPI_SH_TBE)
364 ss->cr1 |= SPI_SH_TBE;
365 if (cr1 & SPI_SH_TBF)
366 ss->cr1 |= SPI_SH_TBF;
367 if (cr1 & SPI_SH_RBE)
368 ss->cr1 |= SPI_SH_RBE;
369 if (cr1 & SPI_SH_RBF)
370 ss->cr1 |= SPI_SH_RBF;
373 spi_sh_clear_bit(ss, ss->cr1, SPI_SH_CR4);
380 static void spi_sh_remove(struct platform_device *pdev)
382 struct spi_sh_data *ss = platform_get_drvdata(pdev);
384 spi_unregister_master(ss->master);
385 free_irq(ss->irq, ss);
388 static int spi_sh_probe(struct platform_device *pdev)
390 struct resource *res;
391 struct spi_master *master;
392 struct spi_sh_data *ss;
396 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
397 if (unlikely(res == NULL)) {
398 dev_err(&pdev->dev, "invalid resource\n");
402 irq = platform_get_irq(pdev, 0);
406 master = devm_spi_alloc_master(&pdev->dev, sizeof(struct spi_sh_data));
407 if (master == NULL) {
408 dev_err(&pdev->dev, "spi_alloc_master error.\n");
412 ss = spi_master_get_devdata(master);
413 platform_set_drvdata(pdev, ss);
415 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
416 case IORESOURCE_MEM_8BIT:
419 case IORESOURCE_MEM_32BIT:
423 dev_err(&pdev->dev, "No support width\n");
428 ss->addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
429 if (ss->addr == NULL) {
430 dev_err(&pdev->dev, "ioremap error.\n");
433 init_waitqueue_head(&ss->wait);
435 ret = request_irq(irq, spi_sh_irq, 0, "spi_sh", ss);
437 dev_err(&pdev->dev, "request_irq error\n");
441 master->num_chipselect = 2;
442 master->bus_num = pdev->id;
443 master->setup = spi_sh_setup;
444 master->transfer_one_message = spi_sh_transfer_one_message;
445 master->cleanup = spi_sh_cleanup;
447 ret = spi_register_master(master);
449 printk(KERN_ERR "spi_register_master error.\n");
460 static struct platform_driver spi_sh_driver = {
461 .probe = spi_sh_probe,
462 .remove_new = spi_sh_remove,
467 module_platform_driver(spi_sh_driver);
469 MODULE_DESCRIPTION("SH SPI bus driver");
470 MODULE_LICENSE("GPL v2");
471 MODULE_AUTHOR("Yoshihiro Shimoda");
472 MODULE_ALIAS("platform:sh_spi");