-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-spibsc-rza2-driver.patch
More file actions
767 lines (765 loc) · 19.9 KB
/
add-spibsc-rza2-driver.patch
File metadata and controls
767 lines (765 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
diff --git a/arch/arm/boot/dts/r7s9210-rza2mevb.dts b/arch/arm/boot/dts/r7s9210-rza2mevb.dts
index 9007f9dd8..aa65dfd5c 100644
--- a/arch/arm/boot/dts/r7s9210-rza2mevb.dts
+++ b/arch/arm/boot/dts/r7s9210-rza2mevb.dts
@@ -952,3 +952,107 @@
//binary_cntmd = <1>; /* Operate in custom Binary Count mode */
};
#endif
+
+/* ========== SPIBSC (QSPI) Example ========== */
+#if 1
+/* Booted as uImage (rootfs is on USB Flash drive or SD card)
+ *
+ * * Requires:
+ * CONFIG_MTD=y
+ * CONFIG_MTD_OF_PARTS=y
+ * CONFIG_MTD_M25P80=y
+ *
+ * NOTE: When booting, do NOT leave the QSPI in XIP mode (ie, do not run the 'qspi single' command)
+ *
+ * When booting, you should see:
+ * m25p80 spi3.0: mx66l51235l (65536 Kbytes)
+ * 4 fixed-partitions partitions found on MTD device spi3.0
+ * Creating 4 MTD partitions on "spi3.0":
+ * 0x000000000000-0x000000080000 : "u-boot"
+ * 0x000000080000-0x0000000c0000 : "env"
+ * 0x0000000c0000-0x000000100000 : "dtb"
+ * 0x000000800000-0x000001800000 : "rootfs"
+ * spibsc 1f800000.spi: probed
+ *
+ * After boot:
+ * $ cat /proc/mtd
+ * dev: size erasesize name
+ * mtd0: 00080000 00040000 "u-boot"
+ * mtd1: 00040000 00040000 "env"
+ * mtd2: 00040000 00040000 "dtb"
+ * mtd3: 01000000 00040000 "rootfs"
+ *
+ *
+ * You can do things like erase and mount a JFFS2 file system:
+ * $ flash_eraseall -j /dev/mtd3
+ * $ mount -t jffs2 /dev/mtdblock3 /mnt
+ * $ echo "hello" > /mnt/hello.txt
+ * $ sync
+ */
+
+
+/* Change our Root File System to USB Flash or SD Card */
+/ {
+ chosen {
+ //bootargs = "ignore_loglevel earlycon rootwait root=/dev/mmcblk0p1"; // SD card (partition 1)
+ bootargs = "ignore_loglevel earlycon rootwait root=/dev/mmcblk0p2"; // SD card (partition 2)
+ //bootargs = "ignore_loglevel earlycon rootwait root=/dev/sda1"; // USB Flash drive (partition 1)
+ //bootargs = "ignore_loglevel earlycon rootwait root=/dev/sda2"; // USB Flash drive (partition 2)
+ stdout-path = "serial0:115200n8";
+ };
+};
+
+/* Delete our XIP QSPI partitions from the top of this file. You would not have these for non-XIP systems */
+/ {
+ /delete-node/ qspi@20000000;
+};
+
+/* Enable QSPI (SPIBSC) and define our partitions */
+&spibsc {
+ status = "okay";
+
+ flash@0 {
+ compatible = "jedec,spi-nor";
+ reg = <0>;
+ spi-max-frequency = <30000000>;
+
+ spi-tx-bus-width = <4>;
+ spi-rx-bus-width = <4>;
+
+ /* SPI Mode 3 */
+ spi-cpol;
+ spi-cpha;
+ m25p,fast-read;
+
+ partitions {
+ compatible = "fixed-partitions";
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ partition@0000000 {
+ label = "u-boot";
+ reg = <0x00000000 0x80000>;
+ read-only;
+ };
+ partition@80000 {
+ label = "env";
+ reg = <0x00080000 0x40000>;
+ read-only;
+ };
+ partition@C0000 {
+ label = "dtb";
+ reg = <0x000C0000 0x40000>;
+ read-only;
+ };
+ partition@800000 {
+ label = "rootfs";
+ reg = <0x00800000 0x01000000>; /* 16MB @ 8MB offset in SPI Flash */
+ };
+ partition@2000000 {
+ label = "test-area";
+ reg = <0x02000000 0x00100000>;
+ };
+ };
+ };
+};
+#endif
diff --git a/arch/arm/boot/dts/r7s9210.dtsi b/arch/arm/boot/dts/r7s9210.dtsi
index ab6c5b2da..4a2cb8853 100644
--- a/arch/arm/boot/dts/r7s9210.dtsi
+++ b/arch/arm/boot/dts/r7s9210.dtsi
@@ -68,6 +68,17 @@
cache-level = <2>;
};
+ spibsc: spi@1f800000 {
+ compatible = "renesas,r7s9210-spibsc";
+ reg = <0x1f800000 0x100>, <0x20000000 0x10000000>;
+ clocks = <&cpg CPG_MOD 83>;
+ power-domains = <&cpg>;
+ interrupts = <GIC_SPI 445 IRQ_TYPE_LEVEL_HIGH>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+ status = "disabled";
+ };
+
scif0: serial@e8007000 {
compatible = "renesas,scif-r7s9210";
reg = <0xe8007000 0x18>;
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 5a94668a4..0ee0e65bb 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -530,6 +530,16 @@ config SPI_SPIBSC
While the BSC supports dual SPI flash connections, this driver
only supports a single SPI flash being connected.
+config SPI_SPIBSC_RZA2
+ tristate "Renesas SPIBSC controller for RZA2"
+ depends on ARCH_R7S9210
+ help
+ SPI Bus space Controller (BSC), otherwise known as the
+ Multi I/O Bus Controller in the hardware manual.
+ This controller was designed for accessing QSPI flash devices.
+ While the BSC supports dual SPI flash connections, this driver
+ only supports a single SPI flash being connected.
+
config SPI_QUP
tristate "Qualcomm SPI controller with QUP interface"
depends on ARCH_QCOM || (ARM && COMPILE_TEST)
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 973f16f65..20a9f8c6b 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -79,6 +79,7 @@ obj-$(CONFIG_SPI_ROCKCHIP) += spi-rockchip.o
obj-$(CONFIG_SPI_RB4XX) += spi-rb4xx.o
obj-$(CONFIG_SPI_RSPI) += spi-rspi.o
obj-$(CONFIG_SPI_SPIBSC) += spi-spibsc.o
+obj-$(CONFIG_SPI_SPIBSC_RZA2) += spi-spibsc-rza2.o
obj-$(CONFIG_SPI_S3C24XX) += spi-s3c24xx-hw.o
spi-s3c24xx-hw-y := spi-s3c24xx.o
spi-s3c24xx-hw-$(CONFIG_SPI_S3C24XX_FIQ) += spi-s3c24xx-fiq.o
diff --git a/drivers/spi/spi-spibsc-rza2.c b/drivers/spi/spi-spibsc-rza2.c
new file mode 100644
index 000000000..05d5ffbc3
--- /dev/null
+++ b/drivers/spi/spi-spibsc-rza2.c
@@ -0,0 +1,594 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// SPI Bus Space Controller (SPIBSC) bus driver
+// Otherwise known as a SPI Multi I/O Bus Controller
+//
+// Copyright (C) 2019 Renesas Electronics
+// Copyright (C) 2019 Chris Brandt
+//
+#include <linux/platform_device.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/clk.h>
+#include <linux/io.h>
+#include <linux/delay.h>
+#include <linux/spi/spi.h>
+
+/* SPIBSC registers */
+#define CMNCR 0x00
+#define SSLDR 0x04
+#define SPBCR 0x08
+#define DRCR 0x0c
+#define DRCMR 0x10
+#define DREAR 0x14
+#define DROPR 0x18
+#define DRENR 0x1c
+#define SMCR 0x20
+#define SMCMR 0x24
+#define SMADR 0x28
+#define SMOPR 0x2c
+#define SMENR 0x30
+#define SMRDR0 0x38
+#define SMRDR1 0x3c
+#define SMWDR0 0x40
+#define SMWDR1 0x44
+#define CMNSR 0x48
+#define SMDMCR 0x60
+#define SMDRENR 0x64
+
+/* CMNCR */
+#define CMNCR_MD BIT(31)
+#define CMNCR_SFDE BIT(24)
+#define CMNCR_MOIIO3(x) (((u32)(x) & 0x3) << 22)
+#define CMNCR_MOIIO2(x) (((u32)(x) & 0x3) << 20)
+#define CMNCR_MOIIO1(x) (((u32)(x) & 0x3) << 18)
+#define CMNCR_MOIIO0(x) (((u32)(x) & 0x3) << 16)
+#define CMNCR_IO3FV(x) (((u32)(x) & 0x3) << 14)
+#define CMNCR_IO2FV(x) (((u32)(x) & 0x3) << 12)
+#define CMNCR_IO0FV(x) (((u32)(x) & 0x3) << 8)
+#define CMNCR_CPHAT BIT(6)
+#define CMNCR_CPHAR BIT(5)
+#define CMNCR_SSLP BIT(4)
+#define CMNCR_CPOL BIT(3)
+#define CMNCR_BSZ(n) (((u32)(n) & 0x3) << 0)
+#define OUT_0 (0u)
+#define OUT_1 (1u)
+#define OUT_REV (2u)
+#define OUT_HIZ (3u)
+#define BSZ_SINGLE (0)
+#define BSZ_DUAL (1)
+#define CMNCR_INIT (CMNCR_MD | \
+ CMNCR_SFDE | \
+ CMNCR_MOIIO3(OUT_HIZ) | \
+ CMNCR_MOIIO2(OUT_HIZ) | \
+ CMNCR_MOIIO1(OUT_HIZ) | \
+ CMNCR_MOIIO0(OUT_HIZ) | \
+ CMNCR_IO3FV(OUT_HIZ) | \
+ CMNCR_IO2FV(OUT_HIZ) | \
+ CMNCR_IO0FV(OUT_HIZ) | \
+ CMNCR_CPHAR | \
+ CMNCR_BSZ(BSZ_SINGLE))
+
+/* SSLDR */
+#define SSLDR_SPNDL(x) (((u32)(x) & 0x7) << 16)
+#define SSLDR_SLNDL(x) (((u32)(x) & 0x7) << 8)
+#define SSLDR_SCKDL(x) (((u32)(x) & 0x7) << 0)
+#define SSLDR_INIT (SSLDR_SPNDL(3) | SSLDR_SLNDL(3) | SSLDR_SCKDL(3))
+
+/* SPBCR */
+#define SPBCR_SPBR(x) (((u32)(x) & 0xff) << 8)
+#define SPBCR_BRDV(x) (((u32)(x) & 0x3) << 0)
+
+/* DRCR (read mode) */
+#define DRCR_SSLN BIT(24)
+#define DRCR_RBURST(x) (((u32)(x) & 0xf) << 16)
+#define DRCR_RCF BIT(9)
+#define DRCR_RBE BIT(8)
+#define DRCR_SSLE BIT(0)
+
+/* DROPR (read mode) */
+#define DROPR_OPD3(o) (((u32)(o) & 0xff) << 24)
+#define DROPR_OPD2(o) (((u32)(o) & 0xff) << 16)
+#define DROPR_OPD1(o) (((u32)(o) & 0xff) << 8)
+#define DROPR_OPD0(o) (((u32)(o) & 0xff) << 0)
+
+/* DRENR (read mode) */
+#define DRENR_CDE BIT(14)
+#define DRENR_OCDE BIT(12)
+#define DRENR_OPDE(o) (((u32)(o) & 0xf) << 4)
+
+/* SMCR (spi mode) */
+#define SMCR_SSLKP BIT(8)
+#define SMCR_SPIRE BIT(2)
+#define SMCR_SPIWE BIT(1)
+#define SMCR_SPIE BIT(0)
+
+/* SMCMR (spi mode) */
+#define SMCMR_CMD(c) (((u32)(c) & 0xff) << 16)
+#define SMCMR_OCMD(o) (((u32)(o) & 0xff) << 0)
+
+/* SMENR (spi mode) */
+#define SMENR_SPIDE(b) (((u32)(b) & 0xf) << 0)
+#define SPIDE_8BITS (0x8)
+#define SPIDE_16BITS (0xc)
+#define SPIDE_32BITS (0xf)
+
+/* CMNSR (spi mode) */
+#define CMNSR_SSLF BIT(1)
+#define CMNSR_TEND BIT(0)
+
+#define MAX_CMD_LEN 6
+
+/* HW Option Flags */
+#define HAS_SPBCR BIT(0)
+
+struct spibsc_priv {
+ void __iomem *base; /* register base */
+ void __iomem *mmio; /* memory mapped io space of SPI Flash */
+ int mmio_size;
+ struct spi_controller *master;
+ struct device *dev;
+ struct clk *clk;
+ int last_xfer;
+ int tx_only;
+ u32 flags;
+};
+
+static void spibsc_write(struct spibsc_priv *sbsc, int reg, u32 val)
+{
+ iowrite32(val, sbsc->base + reg);
+}
+
+static void spibsc_write8(struct spibsc_priv *sbsc, int reg, u8 val)
+{
+ iowrite8(val, sbsc->base + reg);
+}
+
+static void spibsc_write16(struct spibsc_priv *sbsc, int reg, u16 val)
+{
+ iowrite16(val, sbsc->base + reg);
+}
+
+static u32 spibsc_read(struct spibsc_priv *sbsc, int reg)
+{
+ return ioread32(sbsc->base + reg);
+}
+
+static int spibsc_wait_trans_completion(struct spibsc_priv *sbsc)
+{
+ int t = 320; /* 32us = 4 bytes at 1MHz */
+
+ while (t--) {
+ if (spibsc_read(sbsc, CMNSR) & CMNSR_TEND)
+ return 0;
+ ndelay(100);
+ }
+
+ dev_err(sbsc->dev, "Timeout waiting for TEND\n");
+ return -ETIMEDOUT;
+}
+
+/*
+ * This function sends data using 'SPI operation mode'. It is intended to be
+ * used only with SPI Flash commands that do not require any reading back from
+ * the SPI flash.
+ */
+static int spibsc_send_data(struct spibsc_priv *sbsc, const u8 *data, int len)
+{
+ u32 smcr, smenr, smwdr0;
+ int ret, unit, sslkp;
+
+ /* print data (for debugging) */
+#if defined(DEBUG_PRINT_DATA)
+ print_hex_dump_debug("TX data: ", DUMP_PREFIX_NONE, 16, 1, data, len,
+ false);
+#endif
+
+ while (len > 0) {
+ if (len >= 4) {
+ unit = 4;
+ smenr = SMENR_SPIDE(SPIDE_32BITS);
+ } else {
+ unit = len;
+ if (unit == 3)
+ unit = 2;
+
+ if (unit >= 2)
+ smenr = SMENR_SPIDE(SPIDE_16BITS);
+ else
+ smenr = SMENR_SPIDE(SPIDE_8BITS);
+ }
+
+ /* set 4bytes data, bit stream */
+ smwdr0 = *data++;
+ if (unit >= 2)
+ smwdr0 |= (u32)(*data++ << 8);
+ if (unit >= 3)
+ smwdr0 |= (u32)(*data++ << 16);
+ if (unit >= 4)
+ smwdr0 |= (u32)(*data++ << 24);
+
+ /* mask unwrite area */
+ if (unit == 3)
+ smwdr0 |= 0xFF000000;
+ else if (unit == 2)
+ smwdr0 |= 0xFFFF0000;
+ else if (unit == 1)
+ smwdr0 |= 0xFFFFFF00;
+
+ /* write send data. */
+ if (unit == 2)
+ spibsc_write16(sbsc, SMWDR0, (u16)smwdr0);
+ else if (unit == 1)
+ spibsc_write8(sbsc, SMWDR0, (u8)smwdr0);
+ else
+ spibsc_write(sbsc, SMWDR0, smwdr0);
+
+ len -= unit;
+ if ((len <= 0) && sbsc->last_xfer)
+ sslkp = 0;
+ else
+ sslkp = 1;
+
+ /* set params */
+ spibsc_write(sbsc, SMCMR, 0);
+ spibsc_write(sbsc, SMADR, 0);
+ spibsc_write(sbsc, SMOPR, 0);
+ spibsc_write(sbsc, SMENR, smenr);
+
+ /* start spi transfer */
+ smcr = SMCR_SPIE|SMCR_SPIWE;
+ if (sslkp)
+ smcr |= SMCR_SSLKP;
+ spibsc_write(sbsc, SMCR, smcr);
+
+ ret = spibsc_wait_trans_completion(sbsc);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+/*
+ * This function uses 'Data Read' mode to send the command (and address) then
+ * read data back out.
+ * The HW was designed such that you program the registers with the command,
+ * base address, additional command data, etc... But, that makes things too
+ * difficult because it means this driver has to pick out those parameters from
+ * the data stream that was passed.
+ * Instead, we will ignore how the HW was 'supposed' to be used and just
+ * blindly put the Tx data (commands) to send in the registers in the order
+ * in which we know they will be transmitted:
+ *
+ * [DRCMR.CMD]+[DRCMR.OCMD]+[DROPR.OPD3]+[DROPR.OPD2]+[DROPR.OPD1]+[DROPR.OPD0]
+ *
+ * We can send up to 6 bytes this way.
+ * We will tell the HW to skip sending the 'address' because we are secretly
+ * including it in the "command" and that way we can leave the address registers
+ * blank.
+ *
+ * Note that when reading data, the HW will read in 8-byte units which usually
+ * is not an issue for SPI Flash devices.
+ */
+static int spibsc_send_recv_data(struct spibsc_priv *sbsc, u8 *tx_data,
+ int tx_len, u8 *rx_data, int rx_len)
+{
+ u32 drcmr, drenr, dropr;
+ u8 opde;
+
+ dev_dbg(sbsc->dev, "%s (tx=%d, rx=%d)\n", __func__, tx_len, rx_len);
+
+ if (tx_len > MAX_CMD_LEN) {
+ dev_err(sbsc->dev, "Command length too long (%d)", tx_len);
+ return -EIO;
+ }
+
+ if (rx_len > sbsc->mmio_size) {
+ dev_err(sbsc->dev, "Receive length too long (%d)", rx_len);
+ return -EIO;
+ }
+
+ /* Setup Data Read mode
+ * Flush read cache and enable burst mode. Burst mode is required
+ * in order to keep chip select low between read transfers, but it
+ * also means data is read in 8-byte intervals.
+ */
+ spibsc_write(sbsc, DRCR, DRCR_SSLN | DRCR_RCF | DRCR_RBE | DRCR_SSLE);
+ spibsc_read(sbsc, DRCR);
+
+ /* Enter Data Read mode */
+ spibsc_write(sbsc, CMNCR, 0x01FFF300);
+ drcmr = 0;
+ drenr = 0;
+ dropr = 0;
+ opde = 0;
+
+ if (tx_len >= 1) {
+ /* Use 'Command' register for the 1st byte */
+ drcmr |= SMCMR_CMD(tx_data[0]);
+ drenr |= DRENR_CDE;
+ }
+
+ if (tx_len >= 2) {
+ /* Use 'Optional Command' register for the 2nd byte */
+ drcmr |= SMCMR_OCMD(tx_data[1]);
+ drenr |= DRENR_OCDE;
+ }
+
+ /* Use 'Option Data' for 3rd-6th bytes */
+ switch (tx_len) {
+ case 6:
+ dropr |= DROPR_OPD0(tx_data[5]);
+ opde |= (1 << 0);
+ /* fall through */
+ case 5:
+ dropr |= DROPR_OPD1(tx_data[4]);
+ opde |= (1 << 1);
+ /* fall through */
+ case 4:
+ dropr |= DROPR_OPD2(tx_data[3]);
+ opde |= (1 << 2);
+ /* fall through */
+ case 3:
+ dropr |= DROPR_OPD3(tx_data[2]);
+ opde |= (1 << 3);
+ drenr |= DRENR_OPDE(opde);
+ }
+
+ spibsc_write(sbsc, DRCMR, drcmr);
+ spibsc_write(sbsc, DROPR, dropr);
+ spibsc_write(sbsc, DRENR, drenr);
+
+ /* This internal bus access is what will trigger the actual Tx/Rx
+ * operation. Remember, we don't care about the address.
+ */
+ memcpy(rx_data, sbsc->mmio, rx_len);
+
+ /* Deactivate chip select */
+ spibsc_write(sbsc, DRCR, DRCR_SSLN);
+
+ /* Print data (for debugging) */
+#if defined(DEBUG_PRINT_DATA)
+ print_hex_dump_debug("TX data: ", DUMP_PREFIX_NONE, 16, 1, tx_data,
+ tx_len, false);
+ print_hex_dump_debug("RX data: ", DUMP_PREFIX_NONE, 16, 1, rx_data,
+ rx_len, false);
+#endif
+ return 0;
+}
+
+static int spibsc_prepare_message(struct spi_controller *ctlr,
+ struct spi_message *msg)
+{
+ struct spibsc_priv *sbsc = spi_controller_get_devdata(ctlr);
+ struct spi_transfer *t, *t_last;
+
+ sbsc->last_xfer = 0;
+ sbsc->tx_only = 1;
+
+ t_last = list_last_entry(&msg->transfers, struct spi_transfer,
+ transfer_list);
+
+ t = list_first_entry(&msg->transfers, struct spi_transfer,
+ transfer_list);
+
+ if (t->rx_buf) {
+ dev_err(sbsc->dev, "Cannot Rx without Tx first!\n");
+ return -EINVAL;
+ }
+
+ list_for_each_entry(t, &msg->transfers, transfer_list) {
+
+ if (t->rx_buf) {
+ sbsc->tx_only = 0;
+ if (t != t_last) {
+ dev_err(sbsc->dev, "RX transaction is not the last transaction!\n");
+ return -EINVAL;
+ }
+ }
+ if (t->cs_change) {
+ dev_err(sbsc->dev, "cs_change not supported");
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+static int spibsc_transfer_one_message(struct spi_controller *master,
+ struct spi_message *msg)
+{
+ struct spibsc_priv *sbsc = spi_controller_get_devdata(master);
+ struct spi_transfer *t, *t_last;
+ u8 tx_data[MAX_CMD_LEN];
+ u8 tx_len;
+ int ret;
+
+ t_last = list_last_entry(&msg->transfers, struct spi_transfer,
+ transfer_list);
+
+ /* Tx Only (SPI Mode is used) */
+ if (sbsc->tx_only) {
+
+ dev_dbg(sbsc->dev, "%s: TX only\n", __func__);
+
+ /* Initialize for SPI Mode */
+ spibsc_write(sbsc, CMNCR, CMNCR_INIT);
+
+ /* Send messages */
+ list_for_each_entry(t, &msg->transfers, transfer_list) {
+
+ if (t == t_last)
+ sbsc->last_xfer = 1;
+
+ ret = spibsc_send_data(sbsc, t->tx_buf, t->len);
+ if (ret)
+ break;
+
+ msg->actual_length += t->len;
+ }
+
+ goto done;
+ }
+
+ /* Tx, then RX (Data Read Mode is used) */
+ dev_dbg(sbsc->dev, "%s: Tx then Rx\n", __func__);
+
+ /* Buffer up the transmit portion (cmd + addr) so we can send it all at
+ * once
+ */
+ tx_len = 0;
+ list_for_each_entry(t, &msg->transfers, transfer_list) {
+ if (t->tx_buf) {
+ if ((tx_len + t->len) > sizeof(tx_data)) {
+ dev_err(sbsc->dev, "Command too big (%d)\n",
+ tx_len + t->len);
+ return -EINVAL;
+ }
+ memcpy(tx_data + tx_len, t->tx_buf, t->len);
+ tx_len += t->len;
+ }
+
+ if (t->rx_buf)
+ ret = spibsc_send_recv_data(sbsc, tx_data, tx_len,
+ t->rx_buf, t->len);
+
+ msg->actual_length += t->len;
+ }
+
+done:
+ msg->status = ret;
+ spi_finalize_current_message(master);
+
+ return ret;
+}
+
+static int spibsc_setup(struct spi_device *spi)
+{
+ struct spibsc_priv *sbsc = spi_controller_get_devdata(spi->master);
+ u32 max_speed_hz;
+ unsigned long rate;
+ u8 spbr;
+ u8 div_ratio;
+
+ if (sbsc->flags & HAS_SPBCR) {
+ max_speed_hz = spi->max_speed_hz;
+ rate = clk_get_rate(sbsc->clk);
+
+ div_ratio = 2;
+ spbr = 1;
+ while ((rate / div_ratio) > max_speed_hz) {
+ spbr++;
+ div_ratio += 2;
+ if (spbr == 255)
+ break;
+ }
+ spibsc_write(sbsc, SPBCR, SPBCR_SPBR(spbr) | SPBCR_BRDV(0));
+ dev_dbg(sbsc->dev, "Clock set to %ld Hz\n", rate/div_ratio);
+ }
+
+ return 0;
+}
+
+static int spibsc_probe(struct platform_device *pdev)
+{
+ struct spi_controller *master;
+ struct spibsc_priv *sbsc;
+ struct resource *res;
+ int ret;
+
+ master = spi_alloc_master(&pdev->dev, sizeof(*sbsc));
+ if (!master)
+ return -ENOMEM;
+
+ sbsc = spi_controller_get_devdata(master);
+ dev_set_drvdata(&pdev->dev, sbsc);
+ sbsc->master = master;
+ sbsc->dev = &pdev->dev;
+
+ sbsc->clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(sbsc->clk)) {
+ dev_err(&pdev->dev, "cannot get clock\n");
+ ret = PTR_ERR(sbsc->clk);
+ goto error;
+ }
+ clk_prepare_enable(sbsc->clk);
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ sbsc->base = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(sbsc->base)) {
+ ret = PTR_ERR(sbsc->base);
+ goto error;
+ }
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+ sbsc->mmio_size = resource_size(res);
+ sbsc->mmio = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(sbsc->mmio)) {
+ ret = PTR_ERR(sbsc->mmio);
+ goto error;
+ }
+
+ sbsc->flags = (u32) of_device_get_match_data(&pdev->dev);
+
+ master->bus_num = pdev->id;
+ master->num_chipselect = 1;
+ master->mode_bits = SPI_CPOL | SPI_CPHA;
+ master->bits_per_word_mask = SPI_BPW_MASK(8);
+ master->setup = spibsc_setup;
+ master->prepare_message = spibsc_prepare_message;
+ master->transfer_one_message = spibsc_transfer_one_message;
+ master->dev.of_node = pdev->dev.of_node;
+
+ /* Initialize HW */
+ spibsc_write(sbsc, CMNCR, CMNCR_INIT);
+ spibsc_write(sbsc, DRCR, DRCR_RCF);
+ spibsc_write(sbsc, SSLDR, SSLDR_INIT);
+
+ ret = devm_spi_register_controller(&pdev->dev, master);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "spi_register_controller error.\n");
+ goto error;
+ }
+
+ return 0;
+
+error:
+ if (sbsc->clk && !IS_ERR(sbsc->clk))
+ clk_disable_unprepare(sbsc->clk);
+
+ spi_controller_put(master);
+
+ return ret;
+}
+
+static int spibsc_remove(struct platform_device *pdev)
+{
+ struct spibsc_priv *sbsc = dev_get_drvdata(&pdev->dev);
+
+ clk_disable_unprepare(sbsc->clk);
+
+ return 0;
+}
+
+static const struct of_device_id of_spibsc_match[] = {
+ { .compatible = "renesas,r7s72100-spibsc", .data = (void *)HAS_SPBCR},
+ { .compatible = "renesas,r7s9210-spibsc"},
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, of_spibsc_match);
+
+static struct platform_driver spibsc_driver = {
+ .probe = spibsc_probe,
+ .remove = spibsc_remove,
+ .driver = {
+ .name = "spibsc",
+ .owner = THIS_MODULE,
+ .of_match_table = of_spibsc_match,
+ },
+};
+module_platform_driver(spibsc_driver);
+
+MODULE_DESCRIPTION("Renesas SPIBSC SPI Flash driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Chris Brandt");