From: Rick Chen <r...@andestech.com>

atcspi200_spi now support dt along with platform data.

Signed-off-by: Rick Chen <r...@andestech.com>
Signed-off-by: Rick Chen <rickche...@gmail.com>
Signed-off-by: Greentime Hu <green...@gmail.com>
---
 drivers/spi/atcspi200_spi.c              |  134 ++++++------------------------
 include/dm/platform_data/spi_atcspi200.h |   15 ++++
 2 files changed, 42 insertions(+), 107 deletions(-)
 create mode 100644 include/dm/platform_data/spi_atcspi200.h

diff --git a/drivers/spi/atcspi200_spi.c b/drivers/spi/atcspi200_spi.c
index 5b2e9d6..0c39c80 100644
--- a/drivers/spi/atcspi200_spi.c
+++ b/drivers/spi/atcspi200_spi.c
@@ -13,6 +13,7 @@
 #include <spi.h>
 #include <asm/io.h>
 #include <dm.h>
+#include <dm/platform_data/spi_atcspi200.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -75,9 +76,6 @@ struct atcspi200_spi_regs {
 };
 
 struct nds_spi_slave {
-#ifndef CONFIG_DM_SPI
-       struct spi_slave slave;
-#endif
        volatile struct atcspi200_spi_regs *regs;
        int             to;
        unsigned int    freq;
@@ -286,89 +284,6 @@ static int __atcspi200_spi_xfer(struct nds_spi_slave *ns,
                return ret;
 }
 
-#ifndef CONFIG_DM_SPI
-#define to_nds_spi_slave(s) container_of(s, struct nds_spi_slave, slave)
-struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
-               unsigned int max_hz, unsigned int mode)
-{
-       struct nds_spi_slave *ns;
-
-       if (!spi_cs_is_valid(bus, cs))
-               return NULL;
-
-       ns = spi_alloc_slave(struct nds_spi_slave, bus, cs);
-       if (!ns)
-               return NULL;
-
-       switch (bus) {
-       case SPI0_BUS:
-                       ns->regs = (struct atcspi200_spi_regs *)SPI0_BASE;
-                       break;
-
-               case SPI1_BUS:
-                       ns->regs = (struct atcspi200_spi_regs *)SPI1_BASE;
-                       break;
-
-               default:
-                       return NULL;
-       }
-
-       ns->freq= max_hz;
-       ns->mode = mode;
-       ns->to = SPI_TIMEOUT;
-       ns->max_transfer_length = MAX_TRANSFER_LEN;
-       ns->slave.max_write_size = MAX_TRANSFER_LEN;
-
-       return &ns->slave;
-}
-
-void spi_free_slave(struct spi_slave *slave)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       free(ns);
-}
-
-void spi_init(void)
-{
-       /* do nothing */
-}
-
-int spi_claim_bus(struct spi_slave *slave)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       return __atcspi200_spi_claim_bus(ns);
-}
-
-void spi_release_bus(struct spi_slave *slave)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       __atcspi200_spi_release_bus(ns);
-}
-
-int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void 
*data_out,
-               void *data_in, unsigned long flags)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       return __atcspi200_spi_xfer(ns, bitlen, data_out, data_in, flags);
-}
-
-int spi_cs_is_valid(unsigned int bus, unsigned int cs)
-{
-       return bus == 0 && cs < NSPI_MAX_CS_NUM;
-}
-
-void spi_cs_activate(struct spi_slave *slave)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       __atcspi200_spi_start(ns);
-}
-
-void spi_cs_deactivate(struct spi_slave *slave)
-{
-       struct nds_spi_slave *ns = to_nds_spi_slave(slave);
-       __atcspi200_spi_stop(ns);
-}
-#else
 static int atcspi200_spi_set_speed(struct udevice *bus, uint max_hz)
 {
        struct nds_spi_slave *ns = dev_get_priv(bus);
@@ -447,7 +362,10 @@ static int atcspi200_spi_get_clk(struct udevice *bus)
 static int atcspi200_spi_probe(struct udevice *bus)
 {
        struct nds_spi_slave *ns = dev_get_priv(bus);
+       struct atcspi200_spi_platdata *plat = bus->platdata;
 
+       ns->regs = plat->regs;
+       ns->num_cs = plat->num_cs;
        ns->to = SPI_TIMEOUT;
        ns->max_transfer_length = MAX_TRANSFER_LEN;
        ns->mtiming = ns->regs->timing;
@@ -456,24 +374,6 @@ static int atcspi200_spi_probe(struct udevice *bus)
        return 0;
 }
 
-static int atcspi200_ofdata_to_platadata(struct udevice *bus)
-{
-       struct nds_spi_slave *ns = dev_get_priv(bus);
-       const void *blob = gd->fdt_blob;
-       int node = dev_of_offset(bus);
-
-       ns->regs = map_physmem(devfdt_get_addr(bus),
-                                sizeof(struct atcspi200_spi_regs),
-                                MAP_NOCACHE);
-       if (!ns->regs) {
-               printf("%s: could not map device address\n", __func__);
-               return -EINVAL;
-       }
-       ns->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
-
-       return 0;
-}
-
 static const struct dm_spi_ops atcspi200_spi_ops = {
        .claim_bus      = atcspi200_spi_claim_bus,
        .release_bus    = atcspi200_spi_release_bus,
@@ -482,18 +382,38 @@ static const struct dm_spi_ops atcspi200_spi_ops = {
        .set_mode       = atcspi200_spi_set_mode,
 };
 
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+static int atcspi200_ofdata_to_platadata(struct udevice *bus)
+{
+       struct atcspi200_spi_platdata *plat = bus->platdata;
+       fdt_addr_t addr;
+
+       addr = devfdt_get_addr(bus);
+
+       if (addr == FDT_ADDR_T_NONE)
+               return -EINVAL;
+
+       plat->regs = (struct atcspi200_spi_regs *)addr;
+       plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), 
"num-cs", 4);
+
+       return 0;
+}
+
 static const struct udevice_id atcspi200_spi_ids[] = {
        { .compatible = "andestech,atcspi200" },
        { }
 };
+#endif
 
 U_BOOT_DRIVER(atcspi200_spi) = {
        .name = "atcspi200_spi",
        .id = UCLASS_SPI,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
        .of_match = atcspi200_spi_ids,
-       .ops = &atcspi200_spi_ops,
        .ofdata_to_platdata = atcspi200_ofdata_to_platadata,
-       .priv_auto_alloc_size = sizeof(struct nds_spi_slave),
+       .platdata_auto_alloc_size = sizeof(struct atcspi200_spi_platdata),
+#endif
        .probe = atcspi200_spi_probe,
+       .ops = &atcspi200_spi_ops,
+       .priv_auto_alloc_size = sizeof(struct nds_spi_slave),
 };
-#endif
diff --git a/include/dm/platform_data/spi_atcspi200.h 
b/include/dm/platform_data/spi_atcspi200.h
new file mode 100644
index 0000000..d09e74b
--- /dev/null
+++ b/include/dm/platform_data/spi_atcspi200.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2018 Rick Chen <r...@andestech.com>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#ifndef __spi_atcspi200_h
+#define __spi_atcspi200_h
+
+struct atcspi200_spi_platdata {
+       struct atcspi200_spi_regs *regs;
+       u8 num_cs;
+};
+
+#endif /* __spi_atcspi200_h */
-- 
1.7.1

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to