1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/gpio.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/of_irq.h>
21#include <linux/of_address.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/spi/spi.h>
25
26
27#define CDNS_SPI_NAME "cdns-spi"
28
29
30#define CDNS_SPI_CR 0x00
31#define CDNS_SPI_ISR 0x04
32#define CDNS_SPI_IER 0x08
33#define CDNS_SPI_IDR 0x0c
34#define CDNS_SPI_IMR 0x10
35#define CDNS_SPI_ER 0x14
36#define CDNS_SPI_DR 0x18
37#define CDNS_SPI_TXD 0x1C
38#define CDNS_SPI_RXD 0x20
39#define CDNS_SPI_SICR 0x24
40#define CDNS_SPI_THLD 0x28
41
42#define SPI_AUTOSUSPEND_TIMEOUT 3000
43
44
45
46
47
48
49#define CDNS_SPI_CR_MANSTRT 0x00010000
50#define CDNS_SPI_CR_CPHA 0x00000004
51#define CDNS_SPI_CR_CPOL 0x00000002
52#define CDNS_SPI_CR_SSCTRL 0x00003C00
53#define CDNS_SPI_CR_PERI_SEL 0x00000200
54#define CDNS_SPI_CR_BAUD_DIV 0x00000038
55#define CDNS_SPI_CR_MSTREN 0x00000001
56#define CDNS_SPI_CR_MANSTRTEN 0x00008000
57#define CDNS_SPI_CR_SSFORCE 0x00004000
58#define CDNS_SPI_CR_BAUD_DIV_4 0x00000008
59#define CDNS_SPI_CR_DEFAULT (CDNS_SPI_CR_MSTREN | \
60 CDNS_SPI_CR_SSCTRL | \
61 CDNS_SPI_CR_SSFORCE | \
62 CDNS_SPI_CR_BAUD_DIV_4)
63
64
65
66
67
68
69
70
71#define CDNS_SPI_BAUD_DIV_MAX 7
72#define CDNS_SPI_BAUD_DIV_MIN 1
73#define CDNS_SPI_BAUD_DIV_SHIFT 3
74#define CDNS_SPI_SS_SHIFT 10
75#define CDNS_SPI_SS0 0x1
76
77
78
79
80
81
82
83#define CDNS_SPI_IXR_TXOW 0x00000004
84#define CDNS_SPI_IXR_MODF 0x00000002
85#define CDNS_SPI_IXR_RXNEMTY 0x00000010
86#define CDNS_SPI_IXR_DEFAULT (CDNS_SPI_IXR_TXOW | \
87 CDNS_SPI_IXR_MODF)
88#define CDNS_SPI_IXR_TXFULL 0x00000008
89#define CDNS_SPI_IXR_ALL 0x0000007F
90
91
92
93
94
95
96#define CDNS_SPI_ER_ENABLE 0x00000001
97#define CDNS_SPI_ER_DISABLE 0x0
98
99
100#define CDNS_SPI_FIFO_DEPTH 128
101
102
103#define CDNS_SPI_DEFAULT_NUM_CS 4
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118struct cdns_spi {
119 void __iomem *regs;
120 struct clk *ref_clk;
121 struct clk *pclk;
122 u32 speed_hz;
123 const u8 *txbuf;
124 u8 *rxbuf;
125 int tx_bytes;
126 int rx_bytes;
127 u8 dev_busy;
128 u32 is_decoded_cs;
129};
130
131struct cdns_spi_device_data {
132 bool gpio_requested;
133};
134
135
136static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset)
137{
138 return readl_relaxed(xspi->regs + offset);
139}
140
141static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)
142{
143 writel_relaxed(val, xspi->regs + offset);
144}
145
146
147
148
149
150
151
152
153
154
155
156
157static void cdns_spi_init_hw(struct cdns_spi *xspi)
158{
159 u32 ctrl_reg = CDNS_SPI_CR_DEFAULT;
160
161 if (xspi->is_decoded_cs)
162 ctrl_reg |= CDNS_SPI_CR_PERI_SEL;
163
164 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
165 cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_ALL);
166
167
168 while (cdns_spi_read(xspi, CDNS_SPI_ISR) & CDNS_SPI_IXR_RXNEMTY)
169 cdns_spi_read(xspi, CDNS_SPI_RXD);
170
171 cdns_spi_write(xspi, CDNS_SPI_ISR, CDNS_SPI_IXR_ALL);
172 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
173 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
174}
175
176
177
178
179
180
181static void cdns_spi_chipselect(struct spi_device *spi, bool is_high)
182{
183 struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
184 u32 ctrl_reg;
185
186 ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
187
188 if (is_high) {
189
190 ctrl_reg |= CDNS_SPI_CR_SSCTRL;
191 } else {
192
193 ctrl_reg &= ~CDNS_SPI_CR_SSCTRL;
194 if (!(xspi->is_decoded_cs))
195 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) <<
196 CDNS_SPI_SS_SHIFT) &
197 CDNS_SPI_CR_SSCTRL;
198 else
199 ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) &
200 CDNS_SPI_CR_SSCTRL;
201 }
202
203 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
204}
205
206
207
208
209
210
211
212static void cdns_spi_config_clock_mode(struct spi_device *spi)
213{
214 struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
215 u32 ctrl_reg, new_ctrl_reg;
216
217 new_ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
218 ctrl_reg = new_ctrl_reg;
219
220
221 new_ctrl_reg &= ~(CDNS_SPI_CR_CPHA | CDNS_SPI_CR_CPOL);
222 if (spi->mode & SPI_CPHA)
223 new_ctrl_reg |= CDNS_SPI_CR_CPHA;
224 if (spi->mode & SPI_CPOL)
225 new_ctrl_reg |= CDNS_SPI_CR_CPOL;
226
227 if (new_ctrl_reg != ctrl_reg) {
228
229
230
231
232
233
234 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
235 cdns_spi_write(xspi, CDNS_SPI_CR, new_ctrl_reg);
236 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
237 }
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254static void cdns_spi_config_clock_freq(struct spi_device *spi,
255 struct spi_transfer *transfer)
256{
257 struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
258 u32 ctrl_reg, baud_rate_val;
259 unsigned long frequency;
260
261 frequency = clk_get_rate(xspi->ref_clk);
262
263 ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
264
265
266 if (xspi->speed_hz != transfer->speed_hz) {
267
268 baud_rate_val = CDNS_SPI_BAUD_DIV_MIN;
269 while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) &&
270 (frequency / (2 << baud_rate_val)) > transfer->speed_hz)
271 baud_rate_val++;
272
273 ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV;
274 ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT;
275
276 xspi->speed_hz = frequency / (2 << baud_rate_val);
277 }
278 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
279}
280
281
282
283
284
285
286
287
288
289
290
291
292static int cdns_spi_setup_transfer(struct spi_device *spi,
293 struct spi_transfer *transfer)
294{
295 struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
296
297 cdns_spi_config_clock_freq(spi, transfer);
298
299 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n",
300 __func__, spi->mode, spi->bits_per_word,
301 xspi->speed_hz);
302
303 return 0;
304}
305
306
307
308
309
310static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi)
311{
312 unsigned long trans_cnt = 0;
313
314 while ((trans_cnt < CDNS_SPI_FIFO_DEPTH) &&
315 (xspi->tx_bytes > 0)) {
316
317
318
319
320 if (cdns_spi_read(xspi, CDNS_SPI_ISR) &
321 CDNS_SPI_IXR_TXFULL)
322 usleep_range(10, 20);
323
324 if (xspi->txbuf)
325 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++);
326 else
327 cdns_spi_write(xspi, CDNS_SPI_TXD, 0);
328
329 xspi->tx_bytes--;
330 trans_cnt++;
331 }
332}
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
349{
350 struct spi_master *master = dev_id;
351 struct cdns_spi *xspi = spi_master_get_devdata(master);
352 u32 intr_status, status;
353
354 status = IRQ_NONE;
355 intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR);
356 cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status);
357
358 if (intr_status & CDNS_SPI_IXR_MODF) {
359
360
361
362
363 cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_DEFAULT);
364 spi_finalize_current_transfer(master);
365 status = IRQ_HANDLED;
366 } else if (intr_status & CDNS_SPI_IXR_TXOW) {
367 unsigned long trans_cnt;
368
369 trans_cnt = xspi->rx_bytes - xspi->tx_bytes;
370
371
372 while (trans_cnt) {
373 u8 data;
374
375 data = cdns_spi_read(xspi, CDNS_SPI_RXD);
376 if (xspi->rxbuf)
377 *xspi->rxbuf++ = data;
378
379 xspi->rx_bytes--;
380 trans_cnt--;
381 }
382
383 if (xspi->tx_bytes) {
384
385 cdns_spi_fill_tx_fifo(xspi);
386 } else {
387
388 cdns_spi_write(xspi, CDNS_SPI_IDR,
389 CDNS_SPI_IXR_DEFAULT);
390 spi_finalize_current_transfer(master);
391 }
392 status = IRQ_HANDLED;
393 }
394
395 return status;
396}
397
398static int cdns_prepare_message(struct spi_master *master,
399 struct spi_message *msg)
400{
401 cdns_spi_config_clock_mode(msg->spi);
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414
415
416
417static int cdns_transfer_one(struct spi_master *master,
418 struct spi_device *spi,
419 struct spi_transfer *transfer)
420{
421 struct cdns_spi *xspi = spi_master_get_devdata(master);
422
423 xspi->txbuf = transfer->tx_buf;
424 xspi->rxbuf = transfer->rx_buf;
425 xspi->tx_bytes = transfer->len;
426 xspi->rx_bytes = transfer->len;
427
428 cdns_spi_setup_transfer(spi, transfer);
429
430 cdns_spi_fill_tx_fifo(xspi);
431
432 cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT);
433 return transfer->len;
434}
435
436
437
438
439
440
441
442
443
444
445static int cdns_prepare_transfer_hardware(struct spi_master *master)
446{
447 struct cdns_spi *xspi = spi_master_get_devdata(master);
448
449 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
450
451 return 0;
452}
453
454
455
456
457
458
459
460
461
462
463static int cdns_unprepare_transfer_hardware(struct spi_master *master)
464{
465 struct cdns_spi *xspi = spi_master_get_devdata(master);
466
467 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
468
469 return 0;
470}
471
472static int cdns_spi_setup(struct spi_device *spi)
473{
474
475 int ret = -EINVAL;
476 struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi);
477
478
479 if (spi->cs_gpio == -ENOENT)
480 return 0;
481
482
483 if (!cdns_spi_data) {
484 cdns_spi_data = kzalloc(sizeof(*cdns_spi_data), GFP_KERNEL);
485 if (!cdns_spi_data)
486 return -ENOMEM;
487 cdns_spi_data->gpio_requested = false;
488 spi_set_ctldata(spi, cdns_spi_data);
489 }
490
491
492 if (!cdns_spi_data->gpio_requested && gpio_is_valid(spi->cs_gpio)) {
493 ret = gpio_request_one(spi->cs_gpio,
494 (spi->mode & SPI_CS_HIGH) ?
495 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
496 dev_name(&spi->dev));
497 if (ret)
498 dev_err(&spi->dev, "can't request chipselect gpio %d\n",
499 spi->cs_gpio);
500 else
501 cdns_spi_data->gpio_requested = true;
502 } else {
503 if (gpio_is_valid(spi->cs_gpio)) {
504 int mode = ((spi->mode & SPI_CS_HIGH) ?
505 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH);
506
507 ret = gpio_direction_output(spi->cs_gpio, mode);
508 if (ret)
509 dev_err(&spi->dev, "chipselect gpio %d setup failed (%d)\n",
510 spi->cs_gpio, ret);
511 }
512 }
513
514 return ret;
515}
516
517static void cdns_spi_cleanup(struct spi_device *spi)
518{
519 struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi);
520
521 if (cdns_spi_data) {
522 if (cdns_spi_data->gpio_requested)
523 gpio_free(spi->cs_gpio);
524 kfree(cdns_spi_data);
525 spi_set_ctldata(spi, NULL);
526 }
527
528}
529
530
531
532
533
534
535
536
537
538static int cdns_spi_probe(struct platform_device *pdev)
539{
540 int ret = 0, irq;
541 struct spi_master *master;
542 struct cdns_spi *xspi;
543 struct resource *res;
544 u32 num_cs;
545
546 master = spi_alloc_master(&pdev->dev, sizeof(*xspi));
547 if (!master)
548 return -ENOMEM;
549
550 xspi = spi_master_get_devdata(master);
551 master->dev.of_node = pdev->dev.of_node;
552 platform_set_drvdata(pdev, master);
553
554 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
555 xspi->regs = devm_ioremap_resource(&pdev->dev, res);
556 if (IS_ERR(xspi->regs)) {
557 ret = PTR_ERR(xspi->regs);
558 goto remove_master;
559 }
560
561 xspi->pclk = devm_clk_get(&pdev->dev, "pclk");
562 if (IS_ERR(xspi->pclk)) {
563 dev_err(&pdev->dev, "pclk clock not found.\n");
564 ret = PTR_ERR(xspi->pclk);
565 goto remove_master;
566 }
567
568 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
569 if (IS_ERR(xspi->ref_clk)) {
570 dev_err(&pdev->dev, "ref_clk clock not found.\n");
571 ret = PTR_ERR(xspi->ref_clk);
572 goto remove_master;
573 }
574
575 ret = clk_prepare_enable(xspi->pclk);
576 if (ret) {
577 dev_err(&pdev->dev, "Unable to enable APB clock.\n");
578 goto remove_master;
579 }
580
581 ret = clk_prepare_enable(xspi->ref_clk);
582 if (ret) {
583 dev_err(&pdev->dev, "Unable to enable device clock.\n");
584 goto clk_dis_apb;
585 }
586
587 pm_runtime_use_autosuspend(&pdev->dev);
588 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
589 pm_runtime_set_active(&pdev->dev);
590 pm_runtime_enable(&pdev->dev);
591
592 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
593 if (ret < 0)
594 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;
595 else
596 master->num_chipselect = num_cs;
597
598 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",
599 &xspi->is_decoded_cs);
600 if (ret < 0)
601 xspi->is_decoded_cs = 0;
602
603
604 cdns_spi_init_hw(xspi);
605
606 pm_runtime_mark_last_busy(&pdev->dev);
607 pm_runtime_put_autosuspend(&pdev->dev);
608
609 irq = platform_get_irq(pdev, 0);
610 if (irq <= 0) {
611 ret = -ENXIO;
612 dev_err(&pdev->dev, "irq number is invalid\n");
613 goto clk_dis_all;
614 }
615
616 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,
617 0, pdev->name, master);
618 if (ret != 0) {
619 ret = -ENXIO;
620 dev_err(&pdev->dev, "request_irq failed\n");
621 goto clk_dis_all;
622 }
623
624 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware;
625 master->prepare_message = cdns_prepare_message;
626 master->transfer_one = cdns_transfer_one;
627 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;
628 master->set_cs = cdns_spi_chipselect;
629 master->setup = cdns_spi_setup;
630 master->cleanup = cdns_spi_cleanup;
631 master->auto_runtime_pm = true;
632 master->mode_bits = SPI_CPOL | SPI_CPHA;
633
634
635 master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4;
636 xspi->speed_hz = master->max_speed_hz;
637
638 master->bits_per_word_mask = SPI_BPW_MASK(8);
639
640 ret = spi_register_master(master);
641 if (ret) {
642 dev_err(&pdev->dev, "spi_register_master failed\n");
643 goto clk_dis_all;
644 }
645
646 return ret;
647
648clk_dis_all:
649 pm_runtime_set_suspended(&pdev->dev);
650 pm_runtime_disable(&pdev->dev);
651 clk_disable_unprepare(xspi->ref_clk);
652clk_dis_apb:
653 clk_disable_unprepare(xspi->pclk);
654remove_master:
655 spi_master_put(master);
656 return ret;
657}
658
659
660
661
662
663
664
665
666
667
668
669static int cdns_spi_remove(struct platform_device *pdev)
670{
671 struct spi_master *master = platform_get_drvdata(pdev);
672 struct cdns_spi *xspi = spi_master_get_devdata(master);
673
674 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
675
676 clk_disable_unprepare(xspi->ref_clk);
677 clk_disable_unprepare(xspi->pclk);
678 pm_runtime_set_suspended(&pdev->dev);
679 pm_runtime_disable(&pdev->dev);
680
681 spi_unregister_master(master);
682
683 return 0;
684}
685
686
687
688
689
690
691
692
693
694
695static int __maybe_unused cdns_spi_suspend(struct device *dev)
696{
697 struct spi_master *master = dev_get_drvdata(dev);
698
699 return spi_master_suspend(master);
700}
701
702
703
704
705
706
707
708
709
710static int __maybe_unused cdns_spi_resume(struct device *dev)
711{
712 struct spi_master *master = dev_get_drvdata(dev);
713 struct cdns_spi *xspi = spi_master_get_devdata(master);
714
715 cdns_spi_init_hw(xspi);
716 return spi_master_resume(master);
717}
718
719
720
721
722
723
724
725
726
727static int __maybe_unused cnds_runtime_resume(struct device *dev)
728{
729 struct spi_master *master = dev_get_drvdata(dev);
730 struct cdns_spi *xspi = spi_master_get_devdata(master);
731 int ret;
732
733 ret = clk_prepare_enable(xspi->pclk);
734 if (ret) {
735 dev_err(dev, "Cannot enable APB clock.\n");
736 return ret;
737 }
738
739 ret = clk_prepare_enable(xspi->ref_clk);
740 if (ret) {
741 dev_err(dev, "Cannot enable device clock.\n");
742 clk_disable(xspi->pclk);
743 return ret;
744 }
745 return 0;
746}
747
748
749
750
751
752
753
754
755
756static int __maybe_unused cnds_runtime_suspend(struct device *dev)
757{
758 struct spi_master *master = dev_get_drvdata(dev);
759 struct cdns_spi *xspi = spi_master_get_devdata(master);
760
761 clk_disable_unprepare(xspi->ref_clk);
762 clk_disable_unprepare(xspi->pclk);
763
764 return 0;
765}
766
767static const struct dev_pm_ops cdns_spi_dev_pm_ops = {
768 SET_RUNTIME_PM_OPS(cnds_runtime_suspend,
769 cnds_runtime_resume, NULL)
770 SET_SYSTEM_SLEEP_PM_OPS(cdns_spi_suspend, cdns_spi_resume)
771};
772
773static const struct of_device_id cdns_spi_of_match[] = {
774 { .compatible = "xlnx,zynq-spi-r1p6" },
775 { .compatible = "cdns,spi-r1p6" },
776 { }
777};
778MODULE_DEVICE_TABLE(of, cdns_spi_of_match);
779
780
781static struct platform_driver cdns_spi_driver = {
782 .probe = cdns_spi_probe,
783 .remove = cdns_spi_remove,
784 .driver = {
785 .name = CDNS_SPI_NAME,
786 .of_match_table = cdns_spi_of_match,
787 .pm = &cdns_spi_dev_pm_ops,
788 },
789};
790
791module_platform_driver(cdns_spi_driver);
792
793MODULE_AUTHOR("Xilinx, Inc.");
794MODULE_DESCRIPTION("Cadence SPI driver");
795MODULE_LICENSE("GPL");
796