2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Padmavathi Venna <padma.v@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
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>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* Information about each SPI controller */
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 */
31 /* A list of spi buses that we know about */
32 static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
33 static unsigned int bus_count;
35 struct exynos_spi_slave {
36 struct spi_slave slave;
37 struct exynos_spi *regs;
38 unsigned int freq; /* Default frequency */
40 enum periph_id periph_id; /* Peripheral ID for this device */
41 unsigned int fifo_size;
45 static struct spi_bus *spi_get_bus(unsigned dev_index)
47 if (dev_index < bus_count)
48 return &spi_bus[dev_index];
49 debug("%s: invalid bus %d", __func__, dev_index);
54 static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
56 return container_of(slave, struct exynos_spi_slave, slave);
60 * Setup the driver private data
62 * @param bus ID of the bus that the slave is attached to
63 * @param cs ID of the chip select connected to the slave
64 * @param max_hz Required spi frequency
65 * @param mode Required spi mode (clk polarity, clk phase and
67 * @return new device or NULL
69 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
70 unsigned int max_hz, unsigned int mode)
72 struct exynos_spi_slave *spi_slave;
75 if (!spi_cs_is_valid(busnum, cs)) {
76 debug("%s: Invalid bus/chip select %d, %d\n", __func__,
81 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
83 debug("%s: Could not allocate spi_slave\n", __func__);
87 bus = &spi_bus[busnum];
88 spi_slave->regs = bus->regs;
89 spi_slave->mode = mode;
90 spi_slave->periph_id = bus->periph_id;
91 if (bus->periph_id == PERIPH_ID_SPI1 ||
92 bus->periph_id == PERIPH_ID_SPI2)
93 spi_slave->fifo_size = 64;
95 spi_slave->fifo_size = 256;
97 spi_slave->skip_preamble = 0;
99 spi_slave->freq = bus->frequency;
101 spi_slave->freq = min(max_hz, spi_slave->freq);
103 return &spi_slave->slave;
107 * Free spi controller
109 * @param slave Pointer to spi_slave to which controller has to
112 void spi_free_slave(struct spi_slave *slave)
114 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
120 * Flush spi tx, rx fifos and reset the SPI controller
122 * @param slave Pointer to spi_slave to which controller has to
125 static void spi_flush_fifo(struct spi_slave *slave)
127 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
128 struct exynos_spi *regs = spi_slave->regs;
130 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
131 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
132 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
136 * Initialize the spi base registers, set the required clock frequency and
137 * initialize the gpios
139 * @param slave Pointer to spi_slave to which controller has to
141 * @return zero on success else a negative value
143 int spi_claim_bus(struct spi_slave *slave)
145 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
146 struct exynos_spi *regs = spi_slave->regs;
150 ret = set_spi_clk(spi_slave->periph_id,
153 debug("%s: Failed to setup spi clock\n", __func__);
157 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
159 spi_flush_fifo(slave);
161 reg = readl(®s->ch_cfg);
162 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
164 if (spi_slave->mode & SPI_CPHA)
165 reg |= SPI_CH_CPHA_B;
167 if (spi_slave->mode & SPI_CPOL)
168 reg |= SPI_CH_CPOL_L;
170 writel(reg, ®s->ch_cfg);
171 writel(SPI_FB_DELAY_180, ®s->fb_clk);
177 * Reset the spi H/W and flush the tx and rx fifos
179 * @param slave Pointer to spi_slave to which controller has to
182 void spi_release_bus(struct spi_slave *slave)
184 spi_flush_fifo(slave);
187 static void spi_get_fifo_levels(struct exynos_spi *regs,
188 int *rx_lvl, int *tx_lvl)
190 uint32_t spi_sts = readl(®s->spi_sts);
192 *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
193 *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
197 * If there's something to transfer, do a software reset and set a
200 * @param regs SPI peripheral registers
201 * @param count Number of bytes to transfer
203 static void spi_request_bytes(struct exynos_spi *regs, int count)
205 assert(count && count < (1 << 16));
206 setbits_le32(®s->ch_cfg, SPI_CH_RST);
207 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
208 writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
211 static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
212 void **dinp, void const **doutp, unsigned long flags)
214 struct exynos_spi *regs = spi_slave->regs;
216 const uchar *txp = *doutp;
218 uint out_bytes, in_bytes;
220 unsigned start = get_timer(0);
223 out_bytes = in_bytes = todo;
225 stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
226 !(spi_slave->mode & SPI_SLAVE);
229 * If there's something to send, do a software reset and set a
232 spi_request_bytes(regs, todo);
235 * Bytes are transmitted/received in pairs. Wait to receive all the
236 * data because then transmission will be done as well.
243 /* Keep the fifos full/empty. */
244 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
245 if (tx_lvl < spi_slave->fifo_size && out_bytes) {
246 temp = txp ? *txp++ : 0xff;
247 writel(temp, ®s->tx_data);
251 temp = readl(®s->rx_data);
252 if (spi_slave->skip_preamble) {
253 if (temp == SPI_PREAMBLE_END_BYTE) {
254 spi_slave->skip_preamble = 0;
263 } else if (!toread) {
265 * We have run out of input data, but haven't read
266 * enough bytes after the preamble yet. Read some more,
267 * and make sure that we transmit dummy bytes too, to
271 out_bytes = in_bytes;
274 spi_request_bytes(regs, toread);
276 if (spi_slave->skip_preamble && get_timer(start) > 100) {
277 printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
278 in_bytes, out_bytes);
290 * Transfer and receive data
292 * @param slave Pointer to spi_slave to which controller has to
294 * @param bitlen No of bits to tranfer or receive
295 * @param dout Pointer to transfer buffer
296 * @param din Pointer to receive buffer
297 * @param flags Flags for transfer begin and end
298 * @return zero on success else a negative value
300 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
301 void *din, unsigned long flags)
303 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
308 /* spi core configured to do 8 bit transfers */
310 debug("Non byte aligned SPI transfer.\n");
314 /* Start the transaction, if necessary. */
315 if ((flags & SPI_XFER_BEGIN))
316 spi_cs_activate(slave);
318 /* Exynos SPI limits each transfer to 65535 bytes */
319 bytelen = bitlen / 8;
320 for (upto = 0; !ret && upto < bytelen; upto += todo) {
321 todo = min(bytelen - upto, (1 << 16) - 1);
322 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
327 /* Stop the transaction, if necessary. */
328 if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
329 spi_cs_deactivate(slave);
330 if (spi_slave->skip_preamble) {
331 assert(!spi_slave->skip_preamble);
332 debug("Failed to complete premable transaction\n");
341 * Validates the bus and chip select numbers
343 * @param bus ID of the bus that the slave is attached to
344 * @param cs ID of the chip select connected to the slave
345 * @return one on success else zero
347 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
349 return spi_get_bus(bus) && cs == 0;
353 * Activate the CS by driving it LOW
355 * @param slave Pointer to spi_slave to which controller has to
358 void spi_cs_activate(struct spi_slave *slave)
360 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
362 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
363 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
364 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
368 * Deactivate the CS by driving it HIGH
370 * @param slave Pointer to spi_slave to which controller has to
373 void spi_cs_deactivate(struct spi_slave *slave)
375 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
377 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
378 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
381 static inline struct exynos_spi *get_spi_base(int dev_index)
384 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
386 return (struct exynos_spi *)samsung_get_base_spi_isp() +
391 * Read the SPI config from the device tree node.
393 * @param blob FDT blob to read from
394 * @param node Node offset to read from
395 * @param bus SPI bus structure to fill with information
396 * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
398 #ifdef CONFIG_OF_CONTROL
399 static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
402 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
403 bus->periph_id = pinmux_decode_periph_id(blob, node);
405 if (bus->periph_id == PERIPH_ID_NONE) {
406 debug("%s: Invalid peripheral ID %d\n", __func__,
408 return -FDT_ERR_NOTFOUND;
411 /* Use 500KHz as a suitable default */
412 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
419 * Process a list of nodes, adding them to our list of SPI ports.
421 * @param blob fdt blob
422 * @param node_list list of nodes to process (any <=0 are ignored)
423 * @param count number of nodes to process
424 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
425 * @return 0 if ok, -1 on error
427 static int process_nodes(const void *blob, int node_list[], int count)
431 /* build the i2c_controllers[] for each controller */
432 for (i = 0; i < count; i++) {
433 int node = node_list[i];
440 if (spi_get_config(blob, node, bus)) {
441 printf("exynos spi_init: failed to decode bus %d\n",
446 debug("spi: controller bus %d at %p, periph_id %d\n",
447 i, bus->regs, bus->periph_id);
457 * Set up a new SPI slave for an fdt node
459 * @param blob Device tree blob
460 * @param node SPI peripheral node to use
461 * @return 0 if ok, -1 on error
463 struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
464 unsigned int cs, unsigned int max_hz, unsigned int mode)
469 for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
470 if (bus->node == node)
471 return spi_setup_slave(i, cs, max_hz, mode);
474 debug("%s: Failed to find bus node %d\n", __func__, node);
478 /* Sadly there is no error return from this function */
483 #ifdef CONFIG_OF_CONTROL
484 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
485 const void *blob = gd->fdt_blob;
487 count = fdtdec_find_aliases_for_id(blob, "spi",
488 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
489 EXYNOS5_SPI_NUM_CONTROLLERS);
490 if (process_nodes(blob, node_list, count))
496 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
497 bus = &spi_bus[count];
498 bus->regs = get_spi_base(count);
499 bus->periph_id = PERIPH_ID_SPI0 + count;
501 /* Although Exynos5 supports upto 50Mhz speed,
502 * we are setting it to 10Mhz for safe side
504 bus->frequency = 10000000;
507 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;