2 * Common fdt based SPI driver front end
4 * Copyright (c) 2013 NVIDIA Corporation
6 * See file CREDITS for list of people who contributed to this
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <asm/arch/clock.h>
29 #include <asm/arch-tegra/clk_rst.h>
30 #include <asm/arch-tegra20/tegra20_sflash.h>
31 #include <asm/arch-tegra20/tegra20_slink.h>
32 #include <asm/arch-tegra114/tegra114_spi.h>
36 DECLARE_GLOBAL_DATA_PTR;
38 struct fdt_spi_driver {
41 int (*init)(int *node_list, int count);
42 int (*claim_bus)(struct spi_slave *slave);
43 int (*release_bus)(struct spi_slave *slave);
44 int (*cs_is_valid)(unsigned int bus, unsigned int cs);
45 struct spi_slave *(*setup_slave)(unsigned int bus, unsigned int cs,
46 unsigned int max_hz, unsigned int mode);
47 void (*free_slave)(struct spi_slave *slave);
48 void (*cs_activate)(struct spi_slave *slave);
49 void (*cs_deactivate)(struct spi_slave *slave);
50 int (*xfer)(struct spi_slave *slave, unsigned int bitlen,
51 const void *data_out, void *data_in, unsigned long flags);
54 static struct fdt_spi_driver fdt_spi_drivers[] = {
55 #ifdef CONFIG_TEGRA20_SFLASH
57 .compat = COMPAT_NVIDIA_TEGRA20_SFLASH,
59 .init = tegra20_spi_init,
60 .claim_bus = tegra20_spi_claim_bus,
61 .cs_is_valid = tegra20_spi_cs_is_valid,
62 .setup_slave = tegra20_spi_setup_slave,
63 .free_slave = tegra20_spi_free_slave,
64 .cs_activate = tegra20_spi_cs_activate,
65 .cs_deactivate = tegra20_spi_cs_deactivate,
66 .xfer = tegra20_spi_xfer,
69 #ifdef CONFIG_TEGRA20_SLINK
71 .compat = COMPAT_NVIDIA_TEGRA20_SLINK,
72 .max_ctrls = CONFIG_TEGRA_SLINK_CTRLS,
73 .init = tegra30_spi_init,
74 .claim_bus = tegra30_spi_claim_bus,
75 .cs_is_valid = tegra30_spi_cs_is_valid,
76 .setup_slave = tegra30_spi_setup_slave,
77 .free_slave = tegra30_spi_free_slave,
78 .cs_activate = tegra30_spi_cs_activate,
79 .cs_deactivate = tegra30_spi_cs_deactivate,
80 .xfer = tegra30_spi_xfer,
83 #ifdef CONFIG_TEGRA114_SPI
85 .compat = COMPAT_NVIDIA_TEGRA114_SPI,
86 .max_ctrls = CONFIG_TEGRA114_SPI_CTRLS,
87 .init = tegra114_spi_init,
88 .claim_bus = tegra114_spi_claim_bus,
89 .cs_is_valid = tegra114_spi_cs_is_valid,
90 .setup_slave = tegra114_spi_setup_slave,
91 .free_slave = tegra114_spi_free_slave,
92 .cs_activate = tegra114_spi_cs_activate,
93 .cs_deactivate = tegra114_spi_cs_deactivate,
94 .xfer = tegra114_spi_xfer,
99 static struct fdt_spi_driver *driver;
101 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
105 else if (!driver->cs_is_valid)
108 return driver->cs_is_valid(bus, cs);
111 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
112 unsigned int max_hz, unsigned int mode)
114 if (!driver || !driver->setup_slave)
117 return driver->setup_slave(bus, cs, max_hz, mode);
120 void spi_free_slave(struct spi_slave *slave)
122 if (driver && driver->free_slave)
123 return driver->free_slave(slave);
126 static int spi_init_driver(struct fdt_spi_driver *driver)
129 int node_list[driver->max_ctrls];
131 count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
135 return driver->init(node_list, count);
142 for (i = 0; i < ARRAY_SIZE(fdt_spi_drivers); i++) {
143 driver = &fdt_spi_drivers[i];
144 if (!spi_init_driver(driver))
147 if (i == ARRAY_SIZE(fdt_spi_drivers))
151 int spi_claim_bus(struct spi_slave *slave)
155 if (!driver->claim_bus)
158 return driver->claim_bus(slave);
161 void spi_release_bus(struct spi_slave *slave)
163 if (driver && driver->release_bus)
164 driver->release_bus(slave);
167 void spi_cs_activate(struct spi_slave *slave)
169 if (driver && driver->cs_activate)
170 driver->cs_activate(slave);
173 void spi_cs_deactivate(struct spi_slave *slave)
175 if (driver && driver->cs_deactivate)
176 driver->cs_deactivate(slave);
179 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
180 const void *data_out, void *data_in, unsigned long flags)
182 if (!driver || !driver->xfer)
185 return driver->xfer(slave, bitlen, data_out, data_in, flags);