Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 SAMSUNG Electronics |
| 3 | * Padmavathi Venna <padma.v@samsung.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <spi.h> |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 11 | #include <fdtdec.h> |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 12 | #include <asm/arch/clk.h> |
| 13 | #include <asm/arch/clock.h> |
| 14 | #include <asm/arch/cpu.h> |
| 15 | #include <asm/arch/gpio.h> |
| 16 | #include <asm/arch/pinmux.h> |
| 17 | #include <asm/arch-exynos/spi.h> |
| 18 | #include <asm/io.h> |
| 19 | |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 22 | /* Information about each SPI controller */ |
| 23 | struct spi_bus { |
| 24 | enum periph_id periph_id; |
| 25 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 26 | struct exynos_spi *regs; |
| 27 | int inited; /* 1 if this bus is ready for use */ |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 28 | int node; |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 29 | uint deactivate_delay_us; /* Delay to wait after deactivate */ |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* A list of spi buses that we know about */ |
| 33 | static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS]; |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 34 | static unsigned int bus_count; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 35 | |
| 36 | struct exynos_spi_slave { |
| 37 | struct spi_slave slave; |
| 38 | struct exynos_spi *regs; |
| 39 | unsigned int freq; /* Default frequency */ |
| 40 | unsigned int mode; |
| 41 | enum periph_id periph_id; /* Peripheral ID for this device */ |
| 42 | unsigned int fifo_size; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 43 | int skip_preamble; |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 44 | struct spi_bus *bus; /* Pointer to our SPI bus info */ |
| 45 | ulong last_transaction_us; /* Time of last transaction end */ |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static struct spi_bus *spi_get_bus(unsigned dev_index) |
| 49 | { |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 50 | if (dev_index < bus_count) |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 51 | return &spi_bus[dev_index]; |
| 52 | debug("%s: invalid bus %d", __func__, dev_index); |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave) |
| 58 | { |
| 59 | return container_of(slave, struct exynos_spi_slave, slave); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Setup the driver private data |
| 64 | * |
| 65 | * @param bus ID of the bus that the slave is attached to |
| 66 | * @param cs ID of the chip select connected to the slave |
| 67 | * @param max_hz Required spi frequency |
| 68 | * @param mode Required spi mode (clk polarity, clk phase and |
| 69 | * master or slave) |
| 70 | * @return new device or NULL |
| 71 | */ |
| 72 | struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, |
| 73 | unsigned int max_hz, unsigned int mode) |
| 74 | { |
| 75 | struct exynos_spi_slave *spi_slave; |
| 76 | struct spi_bus *bus; |
| 77 | |
| 78 | if (!spi_cs_is_valid(busnum, cs)) { |
| 79 | debug("%s: Invalid bus/chip select %d, %d\n", __func__, |
| 80 | busnum, cs); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 84 | spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs); |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 85 | if (!spi_slave) { |
| 86 | debug("%s: Could not allocate spi_slave\n", __func__); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | bus = &spi_bus[busnum]; |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 91 | spi_slave->bus = bus; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 92 | spi_slave->regs = bus->regs; |
| 93 | spi_slave->mode = mode; |
| 94 | spi_slave->periph_id = bus->periph_id; |
| 95 | if (bus->periph_id == PERIPH_ID_SPI1 || |
| 96 | bus->periph_id == PERIPH_ID_SPI2) |
| 97 | spi_slave->fifo_size = 64; |
| 98 | else |
| 99 | spi_slave->fifo_size = 256; |
| 100 | |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 101 | spi_slave->skip_preamble = 0; |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 102 | spi_slave->last_transaction_us = timer_get_us(); |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 103 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 104 | spi_slave->freq = bus->frequency; |
| 105 | if (max_hz) |
| 106 | spi_slave->freq = min(max_hz, spi_slave->freq); |
| 107 | |
| 108 | return &spi_slave->slave; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Free spi controller |
| 113 | * |
| 114 | * @param slave Pointer to spi_slave to which controller has to |
| 115 | * communicate with |
| 116 | */ |
| 117 | void spi_free_slave(struct spi_slave *slave) |
| 118 | { |
| 119 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 120 | |
| 121 | free(spi_slave); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Flush spi tx, rx fifos and reset the SPI controller |
| 126 | * |
| 127 | * @param slave Pointer to spi_slave to which controller has to |
| 128 | * communicate with |
| 129 | */ |
| 130 | static void spi_flush_fifo(struct spi_slave *slave) |
| 131 | { |
| 132 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 133 | struct exynos_spi *regs = spi_slave->regs; |
| 134 | |
| 135 | clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST); |
| 136 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 137 | setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Initialize the spi base registers, set the required clock frequency and |
| 142 | * initialize the gpios |
| 143 | * |
| 144 | * @param slave Pointer to spi_slave to which controller has to |
| 145 | * communicate with |
| 146 | * @return zero on success else a negative value |
| 147 | */ |
| 148 | int spi_claim_bus(struct spi_slave *slave) |
| 149 | { |
| 150 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 151 | struct exynos_spi *regs = spi_slave->regs; |
| 152 | u32 reg = 0; |
| 153 | int ret; |
| 154 | |
| 155 | ret = set_spi_clk(spi_slave->periph_id, |
| 156 | spi_slave->freq); |
| 157 | if (ret < 0) { |
| 158 | debug("%s: Failed to setup spi clock\n", __func__); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE); |
| 163 | |
| 164 | spi_flush_fifo(slave); |
| 165 | |
| 166 | reg = readl(®s->ch_cfg); |
| 167 | reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L); |
| 168 | |
| 169 | if (spi_slave->mode & SPI_CPHA) |
| 170 | reg |= SPI_CH_CPHA_B; |
| 171 | |
| 172 | if (spi_slave->mode & SPI_CPOL) |
| 173 | reg |= SPI_CH_CPOL_L; |
| 174 | |
| 175 | writel(reg, ®s->ch_cfg); |
| 176 | writel(SPI_FB_DELAY_180, ®s->fb_clk); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Reset the spi H/W and flush the tx and rx fifos |
| 183 | * |
| 184 | * @param slave Pointer to spi_slave to which controller has to |
| 185 | * communicate with |
| 186 | */ |
| 187 | void spi_release_bus(struct spi_slave *slave) |
| 188 | { |
| 189 | spi_flush_fifo(slave); |
| 190 | } |
| 191 | |
| 192 | static void spi_get_fifo_levels(struct exynos_spi *regs, |
| 193 | int *rx_lvl, int *tx_lvl) |
| 194 | { |
| 195 | uint32_t spi_sts = readl(®s->spi_sts); |
| 196 | |
| 197 | *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK; |
| 198 | *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * If there's something to transfer, do a software reset and set a |
| 203 | * transaction size. |
| 204 | * |
| 205 | * @param regs SPI peripheral registers |
| 206 | * @param count Number of bytes to transfer |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 207 | * @param step Number of bytes to transfer in each packet (1 or 4) |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 208 | */ |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 209 | static void spi_request_bytes(struct exynos_spi *regs, int count, int step) |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 210 | { |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 211 | /* For word address we need to swap bytes */ |
| 212 | if (step == 4) { |
| 213 | setbits_le32(®s->mode_cfg, |
| 214 | SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 215 | count /= 4; |
| 216 | setbits_le32(®s->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN | |
| 217 | SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP | |
| 218 | SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP); |
| 219 | } else { |
| 220 | /* Select byte access and clear the swap configuration */ |
| 221 | clrbits_le32(®s->mode_cfg, |
| 222 | SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 223 | writel(0, ®s->swap_cfg); |
| 224 | } |
| 225 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 226 | assert(count && count < (1 << 16)); |
| 227 | setbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 228 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 229 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 230 | writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt); |
| 231 | } |
| 232 | |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 233 | static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, |
| 234 | void **dinp, void const **doutp, unsigned long flags) |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 235 | { |
| 236 | struct exynos_spi *regs = spi_slave->regs; |
| 237 | uchar *rxp = *dinp; |
| 238 | const uchar *txp = *doutp; |
| 239 | int rx_lvl, tx_lvl; |
| 240 | uint out_bytes, in_bytes; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 241 | int toread; |
| 242 | unsigned start = get_timer(0); |
| 243 | int stopping; |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 244 | int step; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 245 | |
| 246 | out_bytes = in_bytes = todo; |
| 247 | |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 248 | stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) && |
| 249 | !(spi_slave->mode & SPI_SLAVE); |
| 250 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 251 | /* |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 252 | * Try to transfer words if we can. This helps read performance at |
| 253 | * SPI clock speeds above about 20MHz. |
| 254 | */ |
| 255 | step = 1; |
| 256 | if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) && |
| 257 | !spi_slave->skip_preamble) |
| 258 | step = 4; |
| 259 | |
| 260 | /* |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 261 | * If there's something to send, do a software reset and set a |
| 262 | * transaction size. |
| 263 | */ |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 264 | spi_request_bytes(regs, todo, step); |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * Bytes are transmitted/received in pairs. Wait to receive all the |
| 268 | * data because then transmission will be done as well. |
| 269 | */ |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 270 | toread = in_bytes; |
| 271 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 272 | while (in_bytes) { |
| 273 | int temp; |
| 274 | |
| 275 | /* Keep the fifos full/empty. */ |
| 276 | spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl); |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Don't completely fill the txfifo, since we don't want our |
| 280 | * rxfifo to overflow, and it may already contain data. |
| 281 | */ |
Rajeshwari Shinde | 120af15 | 2013-10-08 16:20:05 +0530 | [diff] [blame] | 282 | while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) { |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 283 | if (!txp) |
| 284 | temp = -1; |
| 285 | else if (step == 4) |
| 286 | temp = *(uint32_t *)txp; |
| 287 | else |
| 288 | temp = *txp; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 289 | writel(temp, ®s->tx_data); |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 290 | out_bytes -= step; |
| 291 | if (txp) |
| 292 | txp += step; |
| 293 | tx_lvl += step; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 294 | } |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 295 | if (rx_lvl >= step) { |
| 296 | while (rx_lvl >= step) { |
Rajeshwari Shinde | 120af15 | 2013-10-08 16:20:05 +0530 | [diff] [blame] | 297 | temp = readl(®s->rx_data); |
| 298 | if (spi_slave->skip_preamble) { |
| 299 | if (temp == SPI_PREAMBLE_END_BYTE) { |
| 300 | spi_slave->skip_preamble = 0; |
| 301 | stopping = 0; |
| 302 | } |
| 303 | } else { |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 304 | if (rxp || stopping) { |
Akshay Saraswat | e76d2a8 | 2014-06-18 17:52:41 +0530 | [diff] [blame] | 305 | if (step == 4) |
| 306 | *(uint32_t *)rxp = temp; |
| 307 | else |
| 308 | *rxp = temp; |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 309 | rxp += step; |
| 310 | } |
| 311 | in_bytes -= step; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 312 | } |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 313 | toread -= step; |
| 314 | rx_lvl -= step; |
| 315 | } |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 316 | } else if (!toread) { |
| 317 | /* |
| 318 | * We have run out of input data, but haven't read |
| 319 | * enough bytes after the preamble yet. Read some more, |
| 320 | * and make sure that we transmit dummy bytes too, to |
| 321 | * keep things going. |
| 322 | */ |
| 323 | assert(!out_bytes); |
| 324 | out_bytes = in_bytes; |
| 325 | toread = in_bytes; |
| 326 | txp = NULL; |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 327 | spi_request_bytes(regs, toread, step); |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 328 | } |
| 329 | if (spi_slave->skip_preamble && get_timer(start) > 100) { |
| 330 | printf("SPI timeout: in_bytes=%d, out_bytes=%d, ", |
| 331 | in_bytes, out_bytes); |
| 332 | return -1; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 335 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 336 | *dinp = rxp; |
| 337 | *doutp = txp; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 338 | |
| 339 | return 0; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Transfer and receive data |
| 344 | * |
| 345 | * @param slave Pointer to spi_slave to which controller has to |
| 346 | * communicate with |
| 347 | * @param bitlen No of bits to tranfer or receive |
| 348 | * @param dout Pointer to transfer buffer |
| 349 | * @param din Pointer to receive buffer |
| 350 | * @param flags Flags for transfer begin and end |
| 351 | * @return zero on success else a negative value |
| 352 | */ |
| 353 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 354 | void *din, unsigned long flags) |
| 355 | { |
| 356 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 357 | int upto, todo; |
| 358 | int bytelen; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 359 | int ret = 0; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 360 | |
| 361 | /* spi core configured to do 8 bit transfers */ |
| 362 | if (bitlen % 8) { |
| 363 | debug("Non byte aligned SPI transfer.\n"); |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | /* Start the transaction, if necessary. */ |
| 368 | if ((flags & SPI_XFER_BEGIN)) |
| 369 | spi_cs_activate(slave); |
| 370 | |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 371 | /* |
| 372 | * Exynos SPI limits each transfer to 65535 transfers. To keep |
| 373 | * things simple, allow a maximum of 65532 bytes. We could allow |
| 374 | * more in word mode, but the performance difference is small. |
| 375 | */ |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 376 | bytelen = bitlen / 8; |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 377 | for (upto = 0; !ret && upto < bytelen; upto += todo) { |
Rajeshwari Shinde | c4a7963 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 378 | todo = min(bytelen - upto, (1 << 16) - 4); |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 379 | ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags); |
| 380 | if (ret) |
| 381 | break; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Stop the transaction, if necessary. */ |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 385 | if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) { |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 386 | spi_cs_deactivate(slave); |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 387 | if (spi_slave->skip_preamble) { |
| 388 | assert(!spi_slave->skip_preamble); |
| 389 | debug("Failed to complete premable transaction\n"); |
| 390 | ret = -1; |
| 391 | } |
| 392 | } |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 393 | |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 394 | return ret; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Validates the bus and chip select numbers |
| 399 | * |
| 400 | * @param bus ID of the bus that the slave is attached to |
| 401 | * @param cs ID of the chip select connected to the slave |
| 402 | * @return one on success else zero |
| 403 | */ |
| 404 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 405 | { |
| 406 | return spi_get_bus(bus) && cs == 0; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Activate the CS by driving it LOW |
| 411 | * |
| 412 | * @param slave Pointer to spi_slave to which controller has to |
| 413 | * communicate with |
| 414 | */ |
| 415 | void spi_cs_activate(struct spi_slave *slave) |
| 416 | { |
| 417 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 418 | |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 419 | /* If it's too soon to do another transaction, wait */ |
| 420 | if (spi_slave->bus->deactivate_delay_us && |
| 421 | spi_slave->last_transaction_us) { |
| 422 | ulong delay_us; /* The delay completed so far */ |
| 423 | delay_us = timer_get_us() - spi_slave->last_transaction_us; |
| 424 | if (delay_us < spi_slave->bus->deactivate_delay_us) |
| 425 | udelay(spi_slave->bus->deactivate_delay_us - delay_us); |
| 426 | } |
| 427 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 428 | clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT); |
| 429 | debug("Activate CS, bus %d\n", spi_slave->slave.bus); |
Rajeshwari Shinde | e4eaef8 | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 430 | spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Deactivate the CS by driving it HIGH |
| 435 | * |
| 436 | * @param slave Pointer to spi_slave to which controller has to |
| 437 | * communicate with |
| 438 | */ |
| 439 | void spi_cs_deactivate(struct spi_slave *slave) |
| 440 | { |
| 441 | struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); |
| 442 | |
| 443 | setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT); |
Simon Glass | a4e29db | 2014-07-07 10:16:38 -0600 | [diff] [blame] | 444 | |
| 445 | /* Remember time of this transaction so we can honour the bus delay */ |
| 446 | if (spi_slave->bus->deactivate_delay_us) |
| 447 | spi_slave->last_transaction_us = timer_get_us(); |
| 448 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 449 | debug("Deactivate CS, bus %d\n", spi_slave->slave.bus); |
| 450 | } |
| 451 | |
| 452 | static inline struct exynos_spi *get_spi_base(int dev_index) |
| 453 | { |
| 454 | if (dev_index < 3) |
| 455 | return (struct exynos_spi *)samsung_get_base_spi() + dev_index; |
| 456 | else |
| 457 | return (struct exynos_spi *)samsung_get_base_spi_isp() + |
| 458 | (dev_index - 3); |
| 459 | } |
| 460 | |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 461 | /* |
| 462 | * Read the SPI config from the device tree node. |
| 463 | * |
| 464 | * @param blob FDT blob to read from |
| 465 | * @param node Node offset to read from |
| 466 | * @param bus SPI bus structure to fill with information |
| 467 | * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing |
| 468 | */ |
Vivek Gautam | 7d17958 | 2013-03-05 03:49:57 +0000 | [diff] [blame] | 469 | #ifdef CONFIG_OF_CONTROL |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 470 | static int spi_get_config(const void *blob, int node, struct spi_bus *bus) |
| 471 | { |
| 472 | bus->node = node; |
| 473 | bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg"); |
| 474 | bus->periph_id = pinmux_decode_periph_id(blob, node); |
| 475 | |
| 476 | if (bus->periph_id == PERIPH_ID_NONE) { |
| 477 | debug("%s: Invalid peripheral ID %d\n", __func__, |
| 478 | bus->periph_id); |
| 479 | return -FDT_ERR_NOTFOUND; |
| 480 | } |
| 481 | |
| 482 | /* Use 500KHz as a suitable default */ |
| 483 | bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 484 | 500000); |
Rajeshwari Shinde | 8d203af | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 485 | bus->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 486 | "spi-deactivate-delay", 0); |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Process a list of nodes, adding them to our list of SPI ports. |
| 493 | * |
| 494 | * @param blob fdt blob |
| 495 | * @param node_list list of nodes to process (any <=0 are ignored) |
| 496 | * @param count number of nodes to process |
| 497 | * @param is_dvc 1 if these are DVC ports, 0 if standard I2C |
| 498 | * @return 0 if ok, -1 on error |
| 499 | */ |
| 500 | static int process_nodes(const void *blob, int node_list[], int count) |
| 501 | { |
| 502 | int i; |
| 503 | |
| 504 | /* build the i2c_controllers[] for each controller */ |
| 505 | for (i = 0; i < count; i++) { |
| 506 | int node = node_list[i]; |
| 507 | struct spi_bus *bus; |
| 508 | |
| 509 | if (node <= 0) |
| 510 | continue; |
| 511 | |
| 512 | bus = &spi_bus[i]; |
| 513 | if (spi_get_config(blob, node, bus)) { |
| 514 | printf("exynos spi_init: failed to decode bus %d\n", |
| 515 | i); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | debug("spi: controller bus %d at %p, periph_id %d\n", |
| 520 | i, bus->regs, bus->periph_id); |
| 521 | bus->inited = 1; |
| 522 | bus_count++; |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
Vivek Gautam | 7d17958 | 2013-03-05 03:49:57 +0000 | [diff] [blame] | 527 | #endif |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 528 | |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 529 | /** |
| 530 | * Set up a new SPI slave for an fdt node |
| 531 | * |
| 532 | * @param blob Device tree blob |
| 533 | * @param node SPI peripheral node to use |
| 534 | * @return 0 if ok, -1 on error |
| 535 | */ |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 536 | struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node, |
| 537 | int spi_node) |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 538 | { |
| 539 | struct spi_bus *bus; |
| 540 | unsigned int i; |
| 541 | |
| 542 | for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) { |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 543 | if (bus->node == spi_node) |
| 544 | return spi_base_setup_slave_fdt(blob, i, slave_node); |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 545 | } |
| 546 | |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 547 | debug("%s: Failed to find bus node %d\n", __func__, spi_node); |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 548 | return NULL; |
| 549 | } |
| 550 | |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 551 | /* Sadly there is no error return from this function */ |
| 552 | void spi_init(void) |
| 553 | { |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 554 | int count; |
| 555 | |
| 556 | #ifdef CONFIG_OF_CONTROL |
| 557 | int node_list[EXYNOS5_SPI_NUM_CONTROLLERS]; |
| 558 | const void *blob = gd->fdt_blob; |
| 559 | |
| 560 | count = fdtdec_find_aliases_for_id(blob, "spi", |
| 561 | COMPAT_SAMSUNG_EXYNOS_SPI, node_list, |
| 562 | EXYNOS5_SPI_NUM_CONTROLLERS); |
| 563 | if (process_nodes(blob, node_list, count)) |
| 564 | return; |
| 565 | |
| 566 | #else |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 567 | struct spi_bus *bus; |
| 568 | |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 569 | for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) { |
| 570 | bus = &spi_bus[count]; |
| 571 | bus->regs = get_spi_base(count); |
| 572 | bus->periph_id = PERIPH_ID_SPI0 + count; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 573 | |
| 574 | /* Although Exynos5 supports upto 50Mhz speed, |
| 575 | * we are setting it to 10Mhz for safe side |
| 576 | */ |
| 577 | bus->frequency = 10000000; |
| 578 | bus->inited = 1; |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 579 | bus->node = 0; |
| 580 | bus_count = EXYNOS5_SPI_NUM_CONTROLLERS; |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 581 | } |
Rajeshwari Shinde | 4d3acb9 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 582 | #endif |
Rajeshwari Shinde | 1bf43b8 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 583 | } |