2 * NVIDIA Tegra SPI-SLINK controller
4 * Copyright (c) 2010-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-tegra/tegra_slink.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 struct tegra_spi_ctrl {
37 struct slink_tegra *regs;
44 struct tegra_spi_slave {
45 struct spi_slave slave;
46 struct tegra_spi_ctrl *ctrl;
49 static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
51 static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
53 return container_of(slave, struct tegra_spi_slave, slave);
56 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
58 if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
64 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
65 unsigned int max_hz, unsigned int mode)
67 struct tegra_spi_slave *spi;
69 debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
70 bus, cs, max_hz, mode);
72 if (!spi_cs_is_valid(bus, cs)) {
73 printf("SPI error: unsupported bus %d / chip select %d\n",
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);
84 spi = malloc(sizeof(struct tegra_spi_slave));
86 printf("SPI error: malloc of SPI structure failed\n");
91 spi->ctrl = &spi_ctrls[bus];
93 printf("SPI error: could not find controller for bus %d\n",
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;
103 spi->ctrl->mode = mode;
108 void spi_free_slave(struct spi_slave *slave)
110 struct tegra_spi_slave *spi = to_tegra_spi(slave);
117 struct tegra_spi_ctrl *ctrl;
119 #ifdef CONFIG_OF_CONTROL
122 int node_list[CONFIG_TEGRA_SLINK_CTRLS];
124 count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
125 COMPAT_NVIDIA_TEGRA20_SLINK,
127 CONFIG_TEGRA_SLINK_CTRLS);
128 for (i = 0; i < count; i++) {
129 ctrl = &spi_ctrls[i];
132 ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob,
134 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
135 debug("%s: no slink register found\n", __func__);
138 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
139 "spi-max-frequency", 0);
141 debug("%s: no slink max frequency found\n", __func__);
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__);
152 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
153 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
156 for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) {
157 ctrl = &spi_ctrls[i];
174 ctrl->regs = (struct slink_tegra *)base_regs[i];
175 ctrl->freq = TEGRA_SPI_MAX_FREQ;
176 ctrl->periph_id = periph_ids[i];
179 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
180 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
185 int spi_claim_bus(struct spi_slave *slave)
187 struct tegra_spi_slave *spi = to_tegra_spi(slave);
188 struct slink_tegra *regs = spi->ctrl->regs;
191 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
192 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
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, ®s->status);
199 debug("%s: STATUS = %08x\n", __func__, readl(®s->status));
201 /* Set master mode and sw controlled CS */
202 reg = readl(®s->command);
203 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
204 writel(reg, ®s->command);
205 debug("%s: COMMAND = %08x\n", __func__, readl(®s->command));
210 void spi_release_bus(struct spi_slave *slave)
214 void spi_cs_activate(struct spi_slave *slave)
216 struct tegra_spi_slave *spi = to_tegra_spi(slave);
217 struct slink_tegra *regs = spi->ctrl->regs;
219 /* CS is negated on Tegra, so drive a 1 to get a 0 */
220 setbits_le32(®s->command, SLINK_CMD_CS_VAL);
223 void spi_cs_deactivate(struct spi_slave *slave)
225 struct tegra_spi_slave *spi = to_tegra_spi(slave);
226 struct slink_tegra *regs = spi->ctrl->regs;
228 /* CS is negated on Tegra, so drive a 0 to get a 1 */
229 clrbits_le32(®s->command, SLINK_CMD_CS_VAL);
232 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
233 const void *data_out, void *data_in, unsigned long flags)
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;
243 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
244 __func__, slave->bus, slave->cs, dout, din, bitlen);
247 num_bytes = bitlen / 8;
251 reg = readl(®s->status);
252 writel(reg, ®s->status); /* Clear all SPI events via R/W */
253 debug("%s entry: STATUS = %08x\n", __func__, reg);
255 reg = readl(®s->status2);
256 writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */
257 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
259 debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command));
261 clrsetbits_le32(®s->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(®s->command2));
266 if (flags & SPI_XFER_BEGIN)
267 spi_cs_activate(slave);
269 /* handle data in 32-bit chunks */
270 while (num_bytes > 0) {
276 bytes = (num_bytes > 4) ? 4 : num_bytes;
279 for (i = 0; i < bytes; ++i)
280 tmpdout = (tmpdout << 8) | dout[i];
286 clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK,
288 writel(tmpdout, ®s->tx_fifo);
289 setbits_le32(®s->command, SLINK_CMD_GO);
292 * Wait for SPI transmit FIFO to empty, or to time out.
293 * The RX FIFO status will be read and cleared last
295 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
298 status = readl(®s->status);
300 /* We can exit when we've had both RX and TX activity */
301 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
304 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
308 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
309 tmpdin = readl(®s->rx_fifo);
312 /* swap bytes read in */
314 for (i = bytes - 1; i >= 0; --i) {
315 din[i] = tmpdin & 0xff;
323 if (tm >= SPI_TIMEOUT)
326 /* clear ACK RDY, etc. bits */
327 writel(readl(®s->status), ®s->status);
330 if (flags & SPI_XFER_END)
331 spi_cs_deactivate(slave);
333 debug("%s: transfer ended. Value=%08x, status = %08x\n",
334 __func__, tmpdin, readl(®s->status));
337 printf("%s: timeout during SPI transfer, tm %d\n",