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>
35 DECLARE_GLOBAL_DATA_PTR;
37 struct fdt_spi_driver {
40 int (*init)(int *node_list, int count);
41 int (*claim_bus)(struct spi_slave *slave);
42 int (*release_bus)(struct spi_slave *slave);
43 int (*cs_is_valid)(unsigned int bus, unsigned int cs);
44 struct spi_slave *(*setup_slave)(unsigned int bus, unsigned int cs,
45 unsigned int max_hz, unsigned int mode);
46 void (*free_slave)(struct spi_slave *slave);
47 void (*cs_activate)(struct spi_slave *slave);
48 void (*cs_deactivate)(struct spi_slave *slave);
49 int (*xfer)(struct spi_slave *slave, unsigned int bitlen,
50 const void *data_out, void *data_in, unsigned long flags);
53 static struct fdt_spi_driver fdt_spi_drivers[] = {
54 #ifdef CONFIG_TEGRA20_SFLASH
56 .compat = COMPAT_NVIDIA_TEGRA20_SFLASH,
58 .init = tegra20_spi_init,
59 .claim_bus = tegra20_spi_claim_bus,
60 .cs_is_valid = tegra20_spi_cs_is_valid,
61 .setup_slave = tegra20_spi_setup_slave,
62 .free_slave = tegra20_spi_free_slave,
63 .cs_activate = tegra20_spi_cs_activate,
64 .cs_deactivate = tegra20_spi_cs_deactivate,
65 .xfer = tegra20_spi_xfer,
68 #ifdef CONFIG_TEGRA20_SLINK
70 .compat = COMPAT_NVIDIA_TEGRA20_SLINK,
71 .max_ctrls = CONFIG_TEGRA_SLINK_CTRLS,
72 .init = tegra30_spi_init,
73 .claim_bus = tegra30_spi_claim_bus,
74 .cs_is_valid = tegra30_spi_cs_is_valid,
75 .setup_slave = tegra30_spi_setup_slave,
76 .free_slave = tegra30_spi_free_slave,
77 .cs_activate = tegra30_spi_cs_activate,
78 .cs_deactivate = tegra30_spi_cs_deactivate,
79 .xfer = tegra30_spi_xfer,
84 static struct fdt_spi_driver *driver;
86 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
90 else if (!driver->cs_is_valid)
93 return driver->cs_is_valid(bus, cs);
96 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
97 unsigned int max_hz, unsigned int mode)
99 if (!driver || !driver->setup_slave)
102 return driver->setup_slave(bus, cs, max_hz, mode);
105 void spi_free_slave(struct spi_slave *slave)
107 if (driver && driver->free_slave)
108 return driver->free_slave(slave);
111 static int spi_init_driver(struct fdt_spi_driver *driver)
114 int node_list[driver->max_ctrls];
116 count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
120 return driver->init(node_list, count);
127 for (i = 0; i < ARRAY_SIZE(fdt_spi_drivers); i++) {
128 driver = &fdt_spi_drivers[i];
129 if (!spi_init_driver(driver))
132 if (i == ARRAY_SIZE(fdt_spi_drivers))
136 int spi_claim_bus(struct spi_slave *slave)
140 if (!driver->claim_bus)
143 return driver->claim_bus(slave);
146 void spi_release_bus(struct spi_slave *slave)
148 if (driver && driver->release_bus)
149 driver->release_bus(slave);
152 void spi_cs_activate(struct spi_slave *slave)
154 if (driver && driver->cs_activate)
155 driver->cs_activate(slave);
158 void spi_cs_deactivate(struct spi_slave *slave)
160 if (driver && driver->cs_deactivate)
161 driver->cs_deactivate(slave);
164 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
165 const void *data_out, void *data_in, unsigned long flags)
167 if (!driver || !driver->xfer)
170 return driver->xfer(slave, bitlen, data_out, data_in, flags);