]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/fdt_spi.c
spi: add common fdt SPI driver interface
[karo-tx-uboot.git] / drivers / spi / fdt_spi.c
1 /*
2  * Common fdt based SPI driver front end
3  *
4  * Copyright (c) 2013 NVIDIA Corporation
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <malloc.h>
26 #include <asm/io.h>
27 #include <asm/gpio.h>
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 <spi.h>
33 #include <fdtdec.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 struct fdt_spi_driver {
38         int compat;
39         int max_ctrls;
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);
51 };
52
53 static struct fdt_spi_driver fdt_spi_drivers[] = {
54 #ifdef CONFIG_TEGRA20_SFLASH
55         {
56                 .compat         = COMPAT_NVIDIA_TEGRA20_SFLASH,
57                 .max_ctrls      = 1,
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,
66         },
67 #endif
68 #ifdef CONFIG_TEGRA20_SLINK
69         {
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,
80         },
81 #endif
82 };
83
84 static struct fdt_spi_driver *driver;
85
86 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
87 {
88         if (!driver)
89                 return 0;
90         else if (!driver->cs_is_valid)
91                 return 1;
92         else
93                 return driver->cs_is_valid(bus, cs);
94 }
95
96 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
97                 unsigned int max_hz, unsigned int mode)
98 {
99         if (!driver || !driver->setup_slave)
100                 return NULL;
101
102         return driver->setup_slave(bus, cs, max_hz, mode);
103 }
104
105 void spi_free_slave(struct spi_slave *slave)
106 {
107         if (driver && driver->free_slave)
108                 return driver->free_slave(slave);
109 }
110
111 static int spi_init_driver(struct fdt_spi_driver *driver)
112 {
113         int count;
114         int node_list[driver->max_ctrls];
115
116         count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
117                                            driver->compat,
118                                            node_list,
119                                            driver->max_ctrls);
120         return driver->init(node_list, count);
121 }
122
123 void spi_init(void)
124 {
125         int i;
126
127         for (i = 0; i < ARRAY_SIZE(fdt_spi_drivers); i++) {
128                 driver = &fdt_spi_drivers[i];
129                 if (!spi_init_driver(driver))
130                         break;
131         }
132         if (i == ARRAY_SIZE(fdt_spi_drivers))
133                 driver = NULL;
134 }
135
136 int spi_claim_bus(struct spi_slave *slave)
137 {
138         if (!driver)
139                 return 1;
140         if (!driver->claim_bus)
141                 return 0;
142
143         return driver->claim_bus(slave);
144 }
145
146 void spi_release_bus(struct spi_slave *slave)
147 {
148         if (driver && driver->release_bus)
149                 driver->release_bus(slave);
150 }
151
152 void spi_cs_activate(struct spi_slave *slave)
153 {
154         if (driver && driver->cs_activate)
155                 driver->cs_activate(slave);
156 }
157
158 void spi_cs_deactivate(struct spi_slave *slave)
159 {
160         if (driver && driver->cs_deactivate)
161                 driver->cs_deactivate(slave);
162 }
163
164 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
165              const void *data_out, void *data_in, unsigned long flags)
166 {
167         if (!driver || !driver->xfer)
168                 return -1;
169
170         return driver->xfer(slave, bitlen, data_out, data_in, flags);
171 }