hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/spi/spi-fsl-spi.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Freescale SPI controller driver.
34 *
....@@ -13,16 +14,11 @@
1314 * GRLIB support:
1415 * Copyright (c) 2012 Aeroflex Gaisler AB.
1516 * Author: Andreas Larsson <andreas@gaisler.com>
16
- *
17
- * This program is free software; you can redistribute it and/or modify it
18
- * under the terms of the GNU General Public License as published by the
19
- * Free Software Foundation; either version 2 of the License, or (at your
20
- * option) any later version.
2117 */
2218 #include <linux/delay.h>
2319 #include <linux/dma-mapping.h>
2420 #include <linux/fsl_devices.h>
25
-#include <linux/gpio.h>
21
+#include <linux/gpio/consumer.h>
2622 #include <linux/interrupt.h>
2723 #include <linux/irq.h>
2824 #include <linux/kernel.h>
....@@ -32,12 +28,19 @@
3228 #include <linux/of.h>
3329 #include <linux/of_address.h>
3430 #include <linux/of_irq.h>
35
-#include <linux/of_gpio.h>
3631 #include <linux/of_platform.h>
3732 #include <linux/platform_device.h>
3833 #include <linux/spi/spi.h>
3934 #include <linux/spi/spi_bitbang.h>
4035 #include <linux/types.h>
36
+
37
+#ifdef CONFIG_FSL_SOC
38
+#include <sysdev/fsl_soc.h>
39
+#endif
40
+
41
+/* Specific to the MPC8306/MPC8309 */
42
+#define IMMR_SPI_CS_OFFSET 0x14c
43
+#define SPI_BOOT_SEL_BIT 0x80000000
4144
4245 #include "spi-fsl-lib.h"
4346 #include "spi-fsl-cpm.h"
....@@ -87,7 +90,7 @@
8790 {
8891 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
8992 struct spi_mpc8xxx_cs *cs = spi->controller_state;
90
- struct fsl_spi_reg *reg_base = mspi->reg_base;
93
+ struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
9194 __be32 __iomem *mode = &reg_base->mode;
9295 unsigned long flags;
9396
....@@ -112,14 +115,13 @@
112115 {
113116 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
114117 struct fsl_spi_platform_data *pdata;
115
- bool pol = spi->mode & SPI_CS_HIGH;
116118 struct spi_mpc8xxx_cs *cs = spi->controller_state;
117119
118120 pdata = spi->dev.parent->parent->platform_data;
119121
120122 if (value == BITBANG_CS_INACTIVE) {
121123 if (pdata->cs_control)
122
- pdata->cs_control(spi, !pol);
124
+ pdata->cs_control(spi, false);
123125 }
124126
125127 if (value == BITBANG_CS_ACTIVE) {
....@@ -131,7 +133,7 @@
131133 fsl_spi_change_mode(spi);
132134
133135 if (pdata->cs_control)
134
- pdata->cs_control(spi, pol);
136
+ pdata->cs_control(spi, true);
135137 }
136138 }
137139
....@@ -288,7 +290,7 @@
288290 struct spi_transfer *t, unsigned int len)
289291 {
290292 u32 word;
291
- struct fsl_spi_reg *reg_base = mspi->reg_base;
293
+ struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
292294
293295 mspi->count = len;
294296
....@@ -306,7 +308,7 @@
306308 bool is_dma_mapped)
307309 {
308310 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
309
- struct fsl_spi_reg *reg_base;
311
+ struct fsl_spi_reg __iomem *reg_base;
310312 unsigned int len = t->len;
311313 u8 bits_per_word;
312314 int ret;
....@@ -355,33 +357,50 @@
355357 static int fsl_spi_do_one_msg(struct spi_master *master,
356358 struct spi_message *m)
357359 {
360
+ struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
358361 struct spi_device *spi = m->spi;
359362 struct spi_transfer *t, *first;
360363 unsigned int cs_change;
361364 const int nsecs = 50;
362
- int status;
365
+ int status, last_bpw;
366
+
367
+ /*
368
+ * In CPU mode, optimize large byte transfers to use larger
369
+ * bits_per_word values to reduce number of interrupts taken.
370
+ */
371
+ if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) {
372
+ list_for_each_entry(t, &m->transfers, transfer_list) {
373
+ if (t->len < 256 || t->bits_per_word != 8)
374
+ continue;
375
+ if ((t->len & 3) == 0)
376
+ t->bits_per_word = 32;
377
+ else if ((t->len & 1) == 0)
378
+ t->bits_per_word = 16;
379
+ }
380
+ }
363381
364382 /* Don't allow changes if CS is active */
365
- first = list_first_entry(&m->transfers, struct spi_transfer,
366
- transfer_list);
383
+ cs_change = 1;
367384 list_for_each_entry(t, &m->transfers, transfer_list) {
368
- if ((first->bits_per_word != t->bits_per_word) ||
369
- (first->speed_hz != t->speed_hz)) {
385
+ if (cs_change)
386
+ first = t;
387
+ cs_change = t->cs_change;
388
+ if (first->speed_hz != t->speed_hz) {
370389 dev_err(&spi->dev,
371
- "bits_per_word/speed_hz should be same for the same SPI transfer\n");
390
+ "speed_hz cannot change while CS is active\n");
372391 return -EINVAL;
373392 }
374393 }
375394
395
+ last_bpw = -1;
376396 cs_change = 1;
377397 status = -EINVAL;
378398 list_for_each_entry(t, &m->transfers, transfer_list) {
379
- if (t->bits_per_word || t->speed_hz) {
380
- if (cs_change)
381
- status = fsl_spi_setup_transfer(spi, t);
382
- if (status < 0)
383
- break;
384
- }
399
+ if (cs_change || last_bpw != t->bits_per_word)
400
+ status = fsl_spi_setup_transfer(spi, t);
401
+ if (status < 0)
402
+ break;
403
+ last_bpw = t->bits_per_word;
385404
386405 if (cs_change) {
387406 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
....@@ -396,8 +415,7 @@
396415 }
397416 m->actual_length += t->len;
398417
399
- if (t->delay_usecs)
400
- udelay(t->delay_usecs);
418
+ spi_transfer_delay_exec(t);
401419
402420 if (cs_change) {
403421 ndelay(nsecs);
....@@ -421,7 +439,8 @@
421439 static int fsl_spi_setup(struct spi_device *spi)
422440 {
423441 struct mpc8xxx_spi *mpc8xxx_spi;
424
- struct fsl_spi_reg *reg_base;
442
+ struct fsl_spi_reg __iomem *reg_base;
443
+ bool initial_setup = false;
425444 int retval;
426445 u32 hw_mode;
427446 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
....@@ -434,6 +453,7 @@
434453 if (!cs)
435454 return -ENOMEM;
436455 spi_set_ctldata(spi, cs);
456
+ initial_setup = true;
437457 }
438458 mpc8xxx_spi = spi_master_get_devdata(spi->master);
439459
....@@ -457,33 +477,9 @@
457477 retval = fsl_spi_setup_transfer(spi, NULL);
458478 if (retval < 0) {
459479 cs->hw_mode = hw_mode; /* Restore settings */
480
+ if (initial_setup)
481
+ kfree(cs);
460482 return retval;
461
- }
462
-
463
- if (mpc8xxx_spi->type == TYPE_GRLIB) {
464
- if (gpio_is_valid(spi->cs_gpio)) {
465
- int desel;
466
-
467
- retval = gpio_request(spi->cs_gpio,
468
- dev_name(&spi->dev));
469
- if (retval)
470
- return retval;
471
-
472
- desel = !(spi->mode & SPI_CS_HIGH);
473
- retval = gpio_direction_output(spi->cs_gpio, desel);
474
- if (retval) {
475
- gpio_free(spi->cs_gpio);
476
- return retval;
477
- }
478
- } else if (spi->cs_gpio != -ENOENT) {
479
- if (spi->cs_gpio < 0)
480
- return spi->cs_gpio;
481
- return -EINVAL;
482
- }
483
- /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
484
- * indicates to use native chipselect if present, or allow for
485
- * an always selected chip
486
- */
487483 }
488484
489485 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
....@@ -494,11 +490,7 @@
494490
495491 static void fsl_spi_cleanup(struct spi_device *spi)
496492 {
497
- struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
498493 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
499
-
500
- if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
501
- gpio_free(spi->cs_gpio);
502494
503495 kfree(cs);
504496 spi_set_ctldata(spi, NULL);
....@@ -506,7 +498,7 @@
506498
507499 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
508500 {
509
- struct fsl_spi_reg *reg_base = mspi->reg_base;
501
+ struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
510502
511503 /* We need handle RX first */
512504 if (events & SPIE_NE) {
....@@ -541,7 +533,7 @@
541533 struct mpc8xxx_spi *mspi = context_data;
542534 irqreturn_t ret = IRQ_NONE;
543535 u32 events;
544
- struct fsl_spi_reg *reg_base = mspi->reg_base;
536
+ struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
545537
546538 /* Get interrupt events(tx/rx) */
547539 events = mpc8xxx_spi_read_reg(&reg_base->event);
....@@ -561,12 +553,12 @@
561553 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
562554 {
563555 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
564
- struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
556
+ struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base;
565557 u32 slvsel;
566558 u16 cs = spi->chip_select;
567559
568
- if (gpio_is_valid(spi->cs_gpio)) {
569
- gpio_set_value(spi->cs_gpio, on);
560
+ if (spi->cs_gpiod) {
561
+ gpiod_set_value(spi->cs_gpiod, on);
570562 } else if (cs < mpc8xxx_spi->native_chipselects) {
571563 slvsel = mpc8xxx_spi_read_reg(&reg_base->slvsel);
572564 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
....@@ -579,7 +571,7 @@
579571 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
580572 struct spi_master *master = dev_get_drvdata(dev);
581573 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
582
- struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
574
+ struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base;
583575 int mbits;
584576 u32 capabilities;
585577
....@@ -599,13 +591,13 @@
599591 pdata->cs_control = fsl_spi_grlib_cs_control;
600592 }
601593
602
-static struct spi_master * fsl_spi_probe(struct device *dev,
594
+static struct spi_master *fsl_spi_probe(struct device *dev,
603595 struct resource *mem, unsigned int irq)
604596 {
605597 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
606598 struct spi_master *master;
607599 struct mpc8xxx_spi *mpc8xxx_spi;
608
- struct fsl_spi_reg *reg_base;
600
+ struct fsl_spi_reg __iomem *reg_base;
609601 u32 regval;
610602 int ret = 0;
611603
....@@ -622,6 +614,7 @@
622614 master->setup = fsl_spi_setup;
623615 master->cleanup = fsl_spi_cleanup;
624616 master->transfer_one_message = fsl_spi_do_one_msg;
617
+ master->use_gpio_descriptors = true;
625618
626619 mpc8xxx_spi = spi_master_get_devdata(master);
627620 mpc8xxx_spi->max_bits_per_word = 32;
....@@ -697,115 +690,17 @@
697690
698691 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
699692 {
700
- struct device *dev = spi->dev.parent->parent;
701
- struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
702
- struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
703
- u16 cs = spi->chip_select;
704
- int gpio = pinfo->gpios[cs];
705
- bool alow = pinfo->alow_flags[cs];
693
+ if (spi->cs_gpiod) {
694
+ gpiod_set_value(spi->cs_gpiod, on);
695
+ } else {
696
+ struct device *dev = spi->dev.parent->parent;
697
+ struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
698
+ struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
706699
707
- gpio_set_value(gpio, on ^ alow);
708
-}
709
-
710
-static int of_fsl_spi_get_chipselects(struct device *dev)
711
-{
712
- struct device_node *np = dev->of_node;
713
- struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
714
- struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
715
- int ngpios;
716
- int i = 0;
717
- int ret;
718
-
719
- ngpios = of_gpio_count(np);
720
- if (ngpios <= 0) {
721
- /*
722
- * SPI w/o chip-select line. One SPI device is still permitted
723
- * though.
724
- */
725
- pdata->max_chipselect = 1;
726
- return 0;
700
+ if (WARN_ON_ONCE(!pinfo->immr_spi_cs))
701
+ return;
702
+ iowrite32be(on ? 0 : SPI_BOOT_SEL_BIT, pinfo->immr_spi_cs);
727703 }
728
-
729
- pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
730
- GFP_KERNEL);
731
- if (!pinfo->gpios)
732
- return -ENOMEM;
733
- memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
734
-
735
- pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
736
- GFP_KERNEL);
737
- if (!pinfo->alow_flags) {
738
- ret = -ENOMEM;
739
- goto err_alloc_flags;
740
- }
741
-
742
- for (; i < ngpios; i++) {
743
- int gpio;
744
- enum of_gpio_flags flags;
745
-
746
- gpio = of_get_gpio_flags(np, i, &flags);
747
- if (!gpio_is_valid(gpio)) {
748
- dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
749
- ret = gpio;
750
- goto err_loop;
751
- }
752
-
753
- ret = gpio_request(gpio, dev_name(dev));
754
- if (ret) {
755
- dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
756
- goto err_loop;
757
- }
758
-
759
- pinfo->gpios[i] = gpio;
760
- pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
761
-
762
- ret = gpio_direction_output(pinfo->gpios[i],
763
- pinfo->alow_flags[i]);
764
- if (ret) {
765
- dev_err(dev,
766
- "can't set output direction for gpio #%d: %d\n",
767
- i, ret);
768
- goto err_loop;
769
- }
770
- }
771
-
772
- pdata->max_chipselect = ngpios;
773
- pdata->cs_control = fsl_spi_cs_control;
774
-
775
- return 0;
776
-
777
-err_loop:
778
- while (i >= 0) {
779
- if (gpio_is_valid(pinfo->gpios[i]))
780
- gpio_free(pinfo->gpios[i]);
781
- i--;
782
- }
783
-
784
- kfree(pinfo->alow_flags);
785
- pinfo->alow_flags = NULL;
786
-err_alloc_flags:
787
- kfree(pinfo->gpios);
788
- pinfo->gpios = NULL;
789
- return ret;
790
-}
791
-
792
-static int of_fsl_spi_free_chipselects(struct device *dev)
793
-{
794
- struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
795
- struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
796
- int i;
797
-
798
- if (!pinfo->gpios)
799
- return 0;
800
-
801
- for (i = 0; i < pdata->max_chipselect; i++) {
802
- if (gpio_is_valid(pinfo->gpios[i]))
803
- gpio_free(pinfo->gpios[i]);
804
- }
805
-
806
- kfree(pinfo->gpios);
807
- kfree(pinfo->alow_flags);
808
- return 0;
809704 }
810705
811706 static int of_fsl_spi_probe(struct platform_device *ofdev)
....@@ -814,8 +709,13 @@
814709 struct device_node *np = ofdev->dev.of_node;
815710 struct spi_master *master;
816711 struct resource mem;
817
- int irq = 0, type;
818
- int ret = -ENOMEM;
712
+ int irq, type;
713
+ int ret;
714
+ bool spisel_boot = false;
715
+#if IS_ENABLED(CONFIG_FSL_SOC)
716
+ struct mpc8xxx_spi_probe_info *pinfo = NULL;
717
+#endif
718
+
819719
820720 ret = of_mpc8xxx_spi_probe(ofdev);
821721 if (ret)
....@@ -823,32 +723,54 @@
823723
824724 type = fsl_spi_get_type(&ofdev->dev);
825725 if (type == TYPE_FSL) {
826
- ret = of_fsl_spi_get_chipselects(dev);
827
- if (ret)
828
- goto err;
726
+ struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
727
+#if IS_ENABLED(CONFIG_FSL_SOC)
728
+ pinfo = to_of_pinfo(pdata);
729
+
730
+ spisel_boot = of_property_read_bool(np, "fsl,spisel_boot");
731
+ if (spisel_boot) {
732
+ pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);
733
+ if (!pinfo->immr_spi_cs)
734
+ return -ENOMEM;
735
+ }
736
+#endif
737
+ /*
738
+ * Handle the case where we have one hardwired (always selected)
739
+ * device on the first "chipselect". Else we let the core code
740
+ * handle any GPIOs or native chip selects and assign the
741
+ * appropriate callback for dealing with the CS lines. This isn't
742
+ * supported on the GRLIB variant.
743
+ */
744
+ ret = gpiod_count(dev, "cs");
745
+ if (ret < 0)
746
+ ret = 0;
747
+ if (ret == 0 && !spisel_boot) {
748
+ pdata->max_chipselect = 1;
749
+ } else {
750
+ pdata->max_chipselect = ret + spisel_boot;
751
+ pdata->cs_control = fsl_spi_cs_control;
752
+ }
829753 }
830754
831755 ret = of_address_to_resource(np, 0, &mem);
832756 if (ret)
833
- goto err;
757
+ goto unmap_out;
834758
835759 irq = platform_get_irq(ofdev, 0);
836760 if (irq < 0) {
837761 ret = irq;
838
- goto err;
762
+ goto unmap_out;
839763 }
840764
841765 master = fsl_spi_probe(dev, &mem, irq);
842
- if (IS_ERR(master)) {
843
- ret = PTR_ERR(master);
844
- goto err;
845
- }
846766
847
- return 0;
767
+ return PTR_ERR_OR_ZERO(master);
848768
849
-err:
850
- if (type == TYPE_FSL)
851
- of_fsl_spi_free_chipselects(dev);
769
+unmap_out:
770
+#if IS_ENABLED(CONFIG_FSL_SOC)
771
+ if (spisel_boot)
772
+ iounmap(pinfo->immr_spi_cs);
773
+#endif
852774 return ret;
853775 }
854776
....@@ -858,8 +780,6 @@
858780 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
859781
860782 fsl_spi_cpm_free(mpc8xxx_spi);
861
- if (mpc8xxx_spi->type == TYPE_FSL)
862
- of_fsl_spi_free_chipselects(&ofdev->dev);
863783 return 0;
864784 }
865785