]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/tegra_slink.c
spi: mxc_spi: Set master mode for all channels
[karo-tx-uboot.git] / drivers / spi / tegra_slink.c
1 /*
2  * NVIDIA Tegra SPI-SLINK controller
3  *
4  * Copyright (c) 2010-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-tegra/tegra_slink.h>
31 #include <spi.h>
32 #include <fdtdec.h>
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 struct tegra_spi_ctrl {
37         struct slink_tegra *regs;
38         unsigned int freq;
39         unsigned int mode;
40         int periph_id;
41         int valid;
42 };
43
44 struct tegra_spi_slave {
45         struct spi_slave slave;
46         struct tegra_spi_ctrl *ctrl;
47 };
48
49 static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
50
51 static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
52 {
53         return container_of(slave, struct tegra_spi_slave, slave);
54 }
55
56 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
57 {
58         if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
59                 return 0;
60         else
61                 return 1;
62 }
63
64 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
65                 unsigned int max_hz, unsigned int mode)
66 {
67         struct tegra_spi_slave *spi;
68
69         debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
70                 bus, cs, max_hz, mode);
71
72         if (!spi_cs_is_valid(bus, cs)) {
73                 printf("SPI error: unsupported bus %d / chip select %d\n",
74                        bus, cs);
75                 return NULL;
76         }
77
78         if (max_hz > TEGRA_SPI_MAX_FREQ) {
79                 printf("SPI error: unsupported frequency %d Hz. Max frequency"
80                         " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
81                 return NULL;
82         }
83
84         spi = malloc(sizeof(struct tegra_spi_slave));
85         if (!spi) {
86                 printf("SPI error: malloc of SPI structure failed\n");
87                 return NULL;
88         }
89         spi->slave.bus = bus;
90         spi->slave.cs = cs;
91         spi->ctrl = &spi_ctrls[bus];
92         if (!spi->ctrl) {
93                 printf("SPI error: could not find controller for bus %d\n",
94                        bus);
95                 return NULL;
96         }
97
98         if (max_hz < spi->ctrl->freq) {
99                 debug("%s: limiting frequency from %u to %u\n", __func__,
100                       spi->ctrl->freq, max_hz);
101                 spi->ctrl->freq = max_hz;
102         }
103         spi->ctrl->mode = mode;
104
105         return &spi->slave;
106 }
107
108 void spi_free_slave(struct spi_slave *slave)
109 {
110         struct tegra_spi_slave *spi = to_tegra_spi(slave);
111
112         free(spi);
113 }
114
115 void spi_init(void)
116 {
117         struct tegra_spi_ctrl *ctrl;
118         int i;
119 #ifdef CONFIG_OF_CONTROL
120         int node = 0;
121         int count;
122         int node_list[CONFIG_TEGRA_SLINK_CTRLS];
123
124         count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
125                                            COMPAT_NVIDIA_TEGRA20_SLINK,
126                                            node_list,
127                                            CONFIG_TEGRA_SLINK_CTRLS);
128         for (i = 0; i < count; i++) {
129                 ctrl = &spi_ctrls[i];
130                 node = node_list[i];
131
132                 ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob,
133                                                                    node, "reg");
134                 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
135                         debug("%s: no slink register found\n", __func__);
136                         continue;
137                 }
138                 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
139                                             "spi-max-frequency", 0);
140                 if (!ctrl->freq) {
141                         debug("%s: no slink max frequency found\n", __func__);
142                         continue;
143                 }
144
145                 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
146                 if (ctrl->periph_id == PERIPH_ID_NONE) {
147                         debug("%s: could not decode periph id\n", __func__);
148                         continue;
149                 }
150                 ctrl->valid = 1;
151
152                 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
153                       __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
154         }
155 #else
156         for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) {
157                 ctrl = &spi_ctrls[i];
158                 u32 base_regs[] = {
159                         NV_PA_SLINK1_BASE,
160                         NV_PA_SLINK2_BASE,
161                         NV_PA_SLINK3_BASE,
162                         NV_PA_SLINK4_BASE,
163                         NV_PA_SLINK5_BASE,
164                         NV_PA_SLINK6_BASE,
165                 };
166                 int periph_ids[] = {
167                         PERIPH_ID_SBC1,
168                         PERIPH_ID_SBC2,
169                         PERIPH_ID_SBC3,
170                         PERIPH_ID_SBC4,
171                         PERIPH_ID_SBC5,
172                         PERIPH_ID_SBC6,
173                 };
174                 ctrl->regs = (struct slink_tegra *)base_regs[i];
175                 ctrl->freq = TEGRA_SPI_MAX_FREQ;
176                 ctrl->periph_id = periph_ids[i];
177                 ctrl->valid = 1;
178
179                 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
180                       __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
181         }
182 #endif
183 }
184
185 int spi_claim_bus(struct spi_slave *slave)
186 {
187         struct tegra_spi_slave *spi = to_tegra_spi(slave);
188         struct slink_tegra *regs = spi->ctrl->regs;
189         u32 reg;
190
191         /* Change SPI clock to correct frequency, PLLP_OUT0 source */
192         clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
193                                spi->ctrl->freq);
194
195         /* Clear stale status here */
196         reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
197                 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
198         writel(reg, &regs->status);
199         debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
200
201         /* Set master mode and sw controlled CS */
202         reg = readl(&regs->command);
203         reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
204         writel(reg, &regs->command);
205         debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
206
207         return 0;
208 }
209
210 void spi_release_bus(struct spi_slave *slave)
211 {
212 }
213
214 void spi_cs_activate(struct spi_slave *slave)
215 {
216         struct tegra_spi_slave *spi = to_tegra_spi(slave);
217         struct slink_tegra *regs = spi->ctrl->regs;
218
219         /* CS is negated on Tegra, so drive a 1 to get a 0 */
220         setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
221 }
222
223 void spi_cs_deactivate(struct spi_slave *slave)
224 {
225         struct tegra_spi_slave *spi = to_tegra_spi(slave);
226         struct slink_tegra *regs = spi->ctrl->regs;
227
228         /* CS is negated on Tegra, so drive a 0 to get a 1 */
229         clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
230 }
231
232 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
233                 const void *data_out, void *data_in, unsigned long flags)
234 {
235         struct tegra_spi_slave *spi = to_tegra_spi(slave);
236         struct slink_tegra *regs = spi->ctrl->regs;
237         u32 reg, tmpdout, tmpdin = 0;
238         const u8 *dout = data_out;
239         u8 *din = data_in;
240         int num_bytes;
241         int ret;
242
243         debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
244               __func__, slave->bus, slave->cs, dout, din, bitlen);
245         if (bitlen % 8)
246                 return -1;
247         num_bytes = bitlen / 8;
248
249         ret = 0;
250
251         reg = readl(&regs->status);
252         writel(reg, &regs->status);     /* Clear all SPI events via R/W */
253         debug("%s entry: STATUS = %08x\n", __func__, reg);
254
255         reg = readl(&regs->status2);
256         writel(reg, &regs->status2);    /* Clear all STATUS2 events via R/W */
257         debug("%s entry: STATUS2 = %08x\n", __func__, reg);
258
259         debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
260
261         clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
262                         SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
263                         (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
264         debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
265
266         if (flags & SPI_XFER_BEGIN)
267                 spi_cs_activate(slave);
268
269         /* handle data in 32-bit chunks */
270         while (num_bytes > 0) {
271                 int bytes;
272                 int is_read = 0;
273                 int tm, i;
274
275                 tmpdout = 0;
276                 bytes = (num_bytes > 4) ?  4 : num_bytes;
277
278                 if (dout != NULL) {
279                         for (i = 0; i < bytes; ++i)
280                                 tmpdout = (tmpdout << 8) | dout[i];
281                         dout += bytes;
282                 }
283
284                 num_bytes -= bytes;
285
286                 clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
287                                 bytes * 8 - 1);
288                 writel(tmpdout, &regs->tx_fifo);
289                 setbits_le32(&regs->command, SLINK_CMD_GO);
290
291                 /*
292                  * Wait for SPI transmit FIFO to empty, or to time out.
293                  * The RX FIFO status will be read and cleared last
294                  */
295                 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
296                         u32 status;
297
298                         status = readl(&regs->status);
299
300                         /* We can exit when we've had both RX and TX activity */
301                         if (is_read && (status & SLINK_STAT_TXF_EMPTY))
302                                 break;
303
304                         if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
305                                         SLINK_STAT_RDY)
306                                 tm++;
307
308                         else if (!(status & SLINK_STAT_RXF_EMPTY)) {
309                                 tmpdin = readl(&regs->rx_fifo);
310                                 is_read = 1;
311
312                                 /* swap bytes read in */
313                                 if (din != NULL) {
314                                         for (i = bytes - 1; i >= 0; --i) {
315                                                 din[i] = tmpdin & 0xff;
316                                                 tmpdin >>= 8;
317                                         }
318                                         din += bytes;
319                                 }
320                         }
321                 }
322
323                 if (tm >= SPI_TIMEOUT)
324                         ret = tm;
325
326                 /* clear ACK RDY, etc. bits */
327                 writel(readl(&regs->status), &regs->status);
328         }
329
330         if (flags & SPI_XFER_END)
331                 spi_cs_deactivate(slave);
332
333         debug("%s: transfer ended. Value=%08x, status = %08x\n",
334               __func__, tmpdin, readl(&regs->status));
335
336         if (ret) {
337                 printf("%s: timeout during SPI transfer, tm %d\n",
338                        __func__, ret);
339                 return -1;
340         }
341
342         return 0;
343 }