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 = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
86 printf("SPI error: malloc of SPI structure failed\n");
89 spi->ctrl = &spi_ctrls[bus];
91 printf("SPI error: could not find controller for bus %d\n",
96 if (max_hz < spi->ctrl->freq) {
97 debug("%s: limiting frequency from %u to %u\n", __func__,
98 spi->ctrl->freq, max_hz);
99 spi->ctrl->freq = max_hz;
101 spi->ctrl->mode = mode;
106 void spi_free_slave(struct spi_slave *slave)
108 struct tegra_spi_slave *spi = to_tegra_spi(slave);
115 struct tegra_spi_ctrl *ctrl;
117 #ifdef CONFIG_OF_CONTROL
120 int node_list[CONFIG_TEGRA_SLINK_CTRLS];
122 count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
123 COMPAT_NVIDIA_TEGRA20_SLINK,
125 CONFIG_TEGRA_SLINK_CTRLS);
126 for (i = 0; i < count; i++) {
127 ctrl = &spi_ctrls[i];
130 ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob,
132 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
133 debug("%s: no slink register found\n", __func__);
136 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
137 "spi-max-frequency", 0);
139 debug("%s: no slink max frequency found\n", __func__);
143 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
144 if (ctrl->periph_id == PERIPH_ID_NONE) {
145 debug("%s: could not decode periph id\n", __func__);
150 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
151 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
154 for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) {
155 ctrl = &spi_ctrls[i];
172 ctrl->regs = (struct slink_tegra *)base_regs[i];
173 ctrl->freq = TEGRA_SPI_MAX_FREQ;
174 ctrl->periph_id = periph_ids[i];
177 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
178 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
183 int spi_claim_bus(struct spi_slave *slave)
185 struct tegra_spi_slave *spi = to_tegra_spi(slave);
186 struct slink_tegra *regs = spi->ctrl->regs;
189 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
190 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
193 /* Clear stale status here */
194 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
195 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
196 writel(reg, ®s->status);
197 debug("%s: STATUS = %08x\n", __func__, readl(®s->status));
199 /* Set master mode and sw controlled CS */
200 reg = readl(®s->command);
201 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
202 writel(reg, ®s->command);
203 debug("%s: COMMAND = %08x\n", __func__, readl(®s->command));
208 void spi_release_bus(struct spi_slave *slave)
212 void spi_cs_activate(struct spi_slave *slave)
214 struct tegra_spi_slave *spi = to_tegra_spi(slave);
215 struct slink_tegra *regs = spi->ctrl->regs;
217 /* CS is negated on Tegra, so drive a 1 to get a 0 */
218 setbits_le32(®s->command, SLINK_CMD_CS_VAL);
221 void spi_cs_deactivate(struct spi_slave *slave)
223 struct tegra_spi_slave *spi = to_tegra_spi(slave);
224 struct slink_tegra *regs = spi->ctrl->regs;
226 /* CS is negated on Tegra, so drive a 0 to get a 1 */
227 clrbits_le32(®s->command, SLINK_CMD_CS_VAL);
230 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
231 const void *data_out, void *data_in, unsigned long flags)
233 struct tegra_spi_slave *spi = to_tegra_spi(slave);
234 struct slink_tegra *regs = spi->ctrl->regs;
235 u32 reg, tmpdout, tmpdin = 0;
236 const u8 *dout = data_out;
241 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
242 __func__, slave->bus, slave->cs, dout, din, bitlen);
245 num_bytes = bitlen / 8;
249 reg = readl(®s->status);
250 writel(reg, ®s->status); /* Clear all SPI events via R/W */
251 debug("%s entry: STATUS = %08x\n", __func__, reg);
253 reg = readl(®s->status2);
254 writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */
255 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
257 debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command));
259 clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK,
260 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
261 (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
262 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2));
264 if (flags & SPI_XFER_BEGIN)
265 spi_cs_activate(slave);
267 /* handle data in 32-bit chunks */
268 while (num_bytes > 0) {
274 bytes = (num_bytes > 4) ? 4 : num_bytes;
277 for (i = 0; i < bytes; ++i)
278 tmpdout = (tmpdout << 8) | dout[i];
284 clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK,
286 writel(tmpdout, ®s->tx_fifo);
287 setbits_le32(®s->command, SLINK_CMD_GO);
290 * Wait for SPI transmit FIFO to empty, or to time out.
291 * The RX FIFO status will be read and cleared last
293 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
296 status = readl(®s->status);
298 /* We can exit when we've had both RX and TX activity */
299 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
302 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
306 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
307 tmpdin = readl(®s->rx_fifo);
310 /* swap bytes read in */
312 for (i = bytes - 1; i >= 0; --i) {
313 din[i] = tmpdin & 0xff;
321 if (tm >= SPI_TIMEOUT)
324 /* clear ACK RDY, etc. bits */
325 writel(readl(®s->status), ®s->status);
328 if (flags & SPI_XFER_END)
329 spi_cs_deactivate(slave);
331 debug("%s: transfer ended. Value=%08x, status = %08x\n",
332 __func__, tmpdin, readl(®s->status));
335 printf("%s: timeout during SPI transfer, tm %d\n",