Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 1 | /* |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2010-2013 NVIDIA Corporation |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 3 | * With help from the mpc8xxx SPI driver |
| 4 | * With more help from omap3_spi SPI driver |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 10 | #include <malloc.h> |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/gpio.h> |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 13 | #include <asm/arch/clock.h> |
| 14 | #include <asm/arch/pinmux.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 15 | #include <asm/arch-tegra/clk_rst.h> |
Allen Martin | ff1da6f | 2013-03-16 18:58:03 +0000 | [diff] [blame] | 16 | #include <asm/arch-tegra20/tegra20_sflash.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 17 | #include <spi.h> |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 18 | #include <fdtdec.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 21 | |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 22 | #define SPI_CMD_GO (1 << 30) |
| 23 | #define SPI_CMD_ACTIVE_SCLK_SHIFT 26 |
| 24 | #define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT) |
| 25 | #define SPI_CMD_CK_SDA (1 << 21) |
| 26 | #define SPI_CMD_ACTIVE_SDA_SHIFT 18 |
| 27 | #define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT) |
| 28 | #define SPI_CMD_CS_POL (1 << 16) |
| 29 | #define SPI_CMD_TXEN (1 << 15) |
| 30 | #define SPI_CMD_RXEN (1 << 14) |
| 31 | #define SPI_CMD_CS_VAL (1 << 13) |
| 32 | #define SPI_CMD_CS_SOFT (1 << 12) |
| 33 | #define SPI_CMD_CS_DELAY (1 << 9) |
| 34 | #define SPI_CMD_CS3_EN (1 << 8) |
| 35 | #define SPI_CMD_CS2_EN (1 << 7) |
| 36 | #define SPI_CMD_CS1_EN (1 << 6) |
| 37 | #define SPI_CMD_CS0_EN (1 << 5) |
| 38 | #define SPI_CMD_BIT_LENGTH (1 << 4) |
| 39 | #define SPI_CMD_BIT_LENGTH_MASK 0x0000001F |
| 40 | |
| 41 | #define SPI_STAT_BSY (1 << 31) |
| 42 | #define SPI_STAT_RDY (1 << 30) |
| 43 | #define SPI_STAT_RXF_FLUSH (1 << 29) |
| 44 | #define SPI_STAT_TXF_FLUSH (1 << 28) |
| 45 | #define SPI_STAT_RXF_UNR (1 << 27) |
| 46 | #define SPI_STAT_TXF_OVF (1 << 26) |
| 47 | #define SPI_STAT_RXF_EMPTY (1 << 25) |
| 48 | #define SPI_STAT_RXF_FULL (1 << 24) |
| 49 | #define SPI_STAT_TXF_EMPTY (1 << 23) |
| 50 | #define SPI_STAT_TXF_FULL (1 << 22) |
| 51 | #define SPI_STAT_SEL_TXRX_N (1 << 16) |
| 52 | #define SPI_STAT_CUR_BLKCNT (1 << 15) |
| 53 | |
| 54 | #define SPI_TIMEOUT 1000 |
| 55 | #define TEGRA_SPI_MAX_FREQ 52000000 |
| 56 | |
| 57 | struct spi_regs { |
| 58 | u32 command; /* SPI_COMMAND_0 register */ |
| 59 | u32 status; /* SPI_STATUS_0 register */ |
| 60 | u32 rx_cmp; /* SPI_RX_CMP_0 register */ |
| 61 | u32 dma_ctl; /* SPI_DMA_CTL_0 register */ |
| 62 | u32 tx_fifo; /* SPI_TX_FIFO_0 register */ |
| 63 | u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */ |
| 64 | u32 rx_fifo; /* SPI_RX_FIFO_0 register */ |
| 65 | }; |
| 66 | |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 67 | struct tegra_spi_ctrl { |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 68 | struct spi_regs *regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 69 | unsigned int freq; |
| 70 | unsigned int mode; |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 71 | int periph_id; |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 72 | int valid; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 75 | struct tegra_spi_slave { |
| 76 | struct spi_slave slave; |
| 77 | struct tegra_spi_ctrl *ctrl; |
| 78 | }; |
| 79 | |
| 80 | /* tegra20 only supports one SFLASH controller */ |
| 81 | static struct tegra_spi_ctrl spi_ctrls[1]; |
| 82 | |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 83 | static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave) |
| 84 | { |
| 85 | return container_of(slave, struct tegra_spi_slave, slave); |
| 86 | } |
| 87 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 88 | int tegra20_spi_cs_is_valid(unsigned int bus, unsigned int cs) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 89 | { |
Allen Martin | 00a2749 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 90 | /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */ |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 91 | if (bus != 0 || cs != 0) |
| 92 | return 0; |
| 93 | else |
| 94 | return 1; |
| 95 | } |
| 96 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 97 | struct spi_slave *tegra20_spi_setup_slave(unsigned int bus, unsigned int cs, |
| 98 | unsigned int max_hz, unsigned int mode) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 99 | { |
| 100 | struct tegra_spi_slave *spi; |
| 101 | |
| 102 | if (!spi_cs_is_valid(bus, cs)) { |
| 103 | printf("SPI error: unsupported bus %d / chip select %d\n", |
| 104 | bus, cs); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
Tom Warren | 29f3e3f | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 108 | if (max_hz > TEGRA_SPI_MAX_FREQ) { |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 109 | printf("SPI error: unsupported frequency %d Hz. Max frequency" |
Tom Warren | 29f3e3f | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 110 | " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
Albert ARIBAUD | 009d75c | 2013-03-28 18:50:01 +0100 | [diff] [blame] | 114 | spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 115 | if (!spi) { |
| 116 | printf("SPI error: malloc of SPI structure failed\n"); |
| 117 | return NULL; |
| 118 | } |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 119 | spi->ctrl = &spi_ctrls[bus]; |
| 120 | if (!spi->ctrl) { |
| 121 | printf("SPI error: could not find controller for bus %d\n", |
| 122 | bus); |
| 123 | return NULL; |
| 124 | } |
Allen Martin | 2a3c5bc | 2013-03-16 18:58:04 +0000 | [diff] [blame] | 125 | |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 126 | if (max_hz < spi->ctrl->freq) { |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 127 | debug("%s: limiting frequency from %u to %u\n", __func__, |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 128 | spi->ctrl->freq, max_hz); |
| 129 | spi->ctrl->freq = max_hz; |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 130 | } |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 131 | spi->ctrl->mode = mode; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 132 | |
| 133 | return &spi->slave; |
| 134 | } |
| 135 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 136 | void tegra20_spi_free_slave(struct spi_slave *slave) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 137 | { |
| 138 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 139 | |
| 140 | free(spi); |
| 141 | } |
| 142 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 143 | int tegra20_spi_init(int *node_list, int count) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 144 | { |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 145 | struct tegra_spi_ctrl *ctrl; |
| 146 | int i; |
| 147 | int node = 0; |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 148 | int found = 0; |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 149 | |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 150 | for (i = 0; i < count; i++) { |
| 151 | ctrl = &spi_ctrls[i]; |
| 152 | node = node_list[i]; |
| 153 | |
| 154 | ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob, |
| 155 | node, "reg"); |
| 156 | if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) { |
| 157 | debug("%s: no slink register found\n", __func__); |
| 158 | continue; |
| 159 | } |
| 160 | ctrl->freq = fdtdec_get_int(gd->fdt_blob, node, |
| 161 | "spi-max-frequency", 0); |
| 162 | if (!ctrl->freq) { |
| 163 | debug("%s: no slink max frequency found\n", __func__); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node); |
| 168 | if (ctrl->periph_id == PERIPH_ID_NONE) { |
| 169 | debug("%s: could not decode periph id\n", __func__); |
| 170 | continue; |
| 171 | } |
| 172 | ctrl->valid = 1; |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 173 | found = 1; |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 174 | |
| 175 | debug("%s: found controller at %p, freq = %u, periph_id = %d\n", |
| 176 | __func__, ctrl->regs, ctrl->freq, ctrl->periph_id); |
| 177 | } |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 178 | return !found; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 181 | int tegra20_spi_claim_bus(struct spi_slave *slave) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 182 | { |
| 183 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 184 | struct spi_regs *regs = spi->ctrl->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 185 | u32 reg; |
| 186 | |
| 187 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 188 | clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH, |
| 189 | spi->ctrl->freq); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 190 | |
| 191 | /* Clear stale status here */ |
| 192 | reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \ |
| 193 | SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF; |
| 194 | writel(reg, ®s->status); |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 195 | debug("%s: STATUS = %08x\n", __func__, readl(®s->status)); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * Use sw-controlled CS, so we can clock in data after ReadID, etc. |
| 199 | */ |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 200 | reg = (spi->ctrl->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT; |
| 201 | if (spi->ctrl->mode & 2) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 202 | reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT; |
| 203 | clrsetbits_le32(®s->command, SPI_CMD_ACTIVE_SCLK_MASK | |
| 204 | SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg); |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 205 | debug("%s: COMMAND = %08x\n", __func__, readl(®s->command)); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 206 | |
| 207 | /* |
Allen Martin | 00a2749 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 208 | * SPI pins on Tegra20 are muxed - change pinmux later due to UART |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 209 | * issue. |
| 210 | */ |
| 211 | pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH); |
| 212 | pinmux_tristate_disable(PINGRP_LSPI); |
Allen Martin | 9000652 | 2013-03-16 18:58:02 +0000 | [diff] [blame] | 213 | pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); |
Simon Glass | 4560c7d | 2011-11-05 04:46:50 +0000 | [diff] [blame] | 214 | |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 218 | void tegra20_spi_cs_activate(struct spi_slave *slave) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 219 | { |
| 220 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 221 | struct spi_regs *regs = spi->ctrl->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 222 | |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 223 | /* CS is negated on Tegra, so drive a 1 to get a 0 */ |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 224 | setbits_le32(®s->command, SPI_CMD_CS_VAL); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 227 | void tegra20_spi_cs_deactivate(struct spi_slave *slave) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 228 | { |
| 229 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 230 | struct spi_regs *regs = spi->ctrl->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 231 | |
| 232 | /* CS is negated on Tegra, so drive a 0 to get a 1 */ |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 233 | clrbits_le32(®s->command, SPI_CMD_CS_VAL); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 236 | int tegra20_spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 237 | const void *data_out, void *data_in, unsigned long flags) |
| 238 | { |
| 239 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 240 | struct spi_regs *regs = spi->ctrl->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 241 | u32 reg, tmpdout, tmpdin = 0; |
| 242 | const u8 *dout = data_out; |
| 243 | u8 *din = data_in; |
| 244 | int num_bytes; |
| 245 | int ret; |
| 246 | |
| 247 | debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", |
| 248 | slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen); |
| 249 | if (bitlen % 8) |
| 250 | return -1; |
| 251 | num_bytes = bitlen / 8; |
| 252 | |
| 253 | ret = 0; |
| 254 | |
| 255 | reg = readl(®s->status); |
| 256 | writel(reg, ®s->status); /* Clear all SPI events via R/W */ |
| 257 | debug("spi_xfer entry: STATUS = %08x\n", reg); |
| 258 | |
| 259 | reg = readl(®s->command); |
| 260 | reg |= SPI_CMD_TXEN | SPI_CMD_RXEN; |
| 261 | writel(reg, ®s->command); |
| 262 | debug("spi_xfer: COMMAND = %08x\n", readl(®s->command)); |
| 263 | |
| 264 | if (flags & SPI_XFER_BEGIN) |
| 265 | spi_cs_activate(slave); |
| 266 | |
| 267 | /* handle data in 32-bit chunks */ |
| 268 | while (num_bytes > 0) { |
| 269 | int bytes; |
| 270 | int is_read = 0; |
| 271 | int tm, i; |
| 272 | |
| 273 | tmpdout = 0; |
| 274 | bytes = (num_bytes > 4) ? 4 : num_bytes; |
| 275 | |
| 276 | if (dout != NULL) { |
| 277 | for (i = 0; i < bytes; ++i) |
| 278 | tmpdout = (tmpdout << 8) | dout[i]; |
| 279 | } |
| 280 | |
| 281 | num_bytes -= bytes; |
| 282 | if (dout) |
| 283 | dout += bytes; |
| 284 | |
| 285 | clrsetbits_le32(®s->command, SPI_CMD_BIT_LENGTH_MASK, |
| 286 | bytes * 8 - 1); |
| 287 | writel(tmpdout, ®s->tx_fifo); |
| 288 | setbits_le32(®s->command, SPI_CMD_GO); |
| 289 | |
| 290 | /* |
| 291 | * Wait for SPI transmit FIFO to empty, or to time out. |
| 292 | * The RX FIFO status will be read and cleared last |
| 293 | */ |
| 294 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
| 295 | u32 status; |
| 296 | |
| 297 | status = readl(®s->status); |
| 298 | |
| 299 | /* We can exit when we've had both RX and TX activity */ |
| 300 | if (is_read && (status & SPI_STAT_TXF_EMPTY)) |
| 301 | break; |
| 302 | |
| 303 | if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) != |
| 304 | SPI_STAT_RDY) |
| 305 | tm++; |
| 306 | |
| 307 | else if (!(status & SPI_STAT_RXF_EMPTY)) { |
| 308 | tmpdin = readl(®s->rx_fifo); |
| 309 | is_read = 1; |
| 310 | |
| 311 | /* swap bytes read in */ |
| 312 | if (din != NULL) { |
| 313 | for (i = bytes - 1; i >= 0; --i) { |
| 314 | din[i] = tmpdin & 0xff; |
| 315 | tmpdin >>= 8; |
| 316 | } |
| 317 | din += bytes; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (tm >= SPI_TIMEOUT) |
| 323 | ret = tm; |
| 324 | |
| 325 | /* clear ACK RDY, etc. bits */ |
| 326 | writel(readl(®s->status), ®s->status); |
| 327 | } |
| 328 | |
| 329 | if (flags & SPI_XFER_END) |
| 330 | spi_cs_deactivate(slave); |
| 331 | |
| 332 | debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n", |
| 333 | tmpdin, readl(®s->status)); |
| 334 | |
| 335 | if (ret) { |
| 336 | printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |