2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Padmavathi Venna <padma.v@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <asm/arch/clk.h>
25 #include <asm/arch/clock.h>
26 #include <asm/arch/cpu.h>
27 #include <asm/arch/gpio.h>
28 #include <asm/arch/pinmux.h>
29 #include <asm/arch-exynos/spi.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 /* Information about each SPI controller */
36 enum periph_id periph_id;
37 s32 frequency; /* Default clock frequency, -1 for none */
38 struct exynos_spi *regs;
39 int inited; /* 1 if this bus is ready for use */
43 /* A list of spi buses that we know about */
44 static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
45 static unsigned int bus_count;
47 struct exynos_spi_slave {
48 struct spi_slave slave;
49 struct exynos_spi *regs;
50 unsigned int freq; /* Default frequency */
52 enum periph_id periph_id; /* Peripheral ID for this device */
53 unsigned int fifo_size;
57 static struct spi_bus *spi_get_bus(unsigned dev_index)
59 if (dev_index < bus_count)
60 return &spi_bus[dev_index];
61 debug("%s: invalid bus %d", __func__, dev_index);
66 static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
68 return container_of(slave, struct exynos_spi_slave, slave);
72 * Setup the driver private data
74 * @param bus ID of the bus that the slave is attached to
75 * @param cs ID of the chip select connected to the slave
76 * @param max_hz Required spi frequency
77 * @param mode Required spi mode (clk polarity, clk phase and
79 * @return new device or NULL
81 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
82 unsigned int max_hz, unsigned int mode)
84 struct exynos_spi_slave *spi_slave;
87 if (!spi_cs_is_valid(busnum, cs)) {
88 debug("%s: Invalid bus/chip select %d, %d\n", __func__,
93 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
95 debug("%s: Could not allocate spi_slave\n", __func__);
99 bus = &spi_bus[busnum];
100 spi_slave->regs = bus->regs;
101 spi_slave->mode = mode;
102 spi_slave->periph_id = bus->periph_id;
103 if (bus->periph_id == PERIPH_ID_SPI1 ||
104 bus->periph_id == PERIPH_ID_SPI2)
105 spi_slave->fifo_size = 64;
107 spi_slave->fifo_size = 256;
109 spi_slave->skip_preamble = 0;
111 spi_slave->freq = bus->frequency;
113 spi_slave->freq = min(max_hz, spi_slave->freq);
115 return &spi_slave->slave;
119 * Free spi controller
121 * @param slave Pointer to spi_slave to which controller has to
124 void spi_free_slave(struct spi_slave *slave)
126 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
132 * Flush spi tx, rx fifos and reset the SPI controller
134 * @param slave Pointer to spi_slave to which controller has to
137 static void spi_flush_fifo(struct spi_slave *slave)
139 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
140 struct exynos_spi *regs = spi_slave->regs;
142 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
143 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
144 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
148 * Initialize the spi base registers, set the required clock frequency and
149 * initialize the gpios
151 * @param slave Pointer to spi_slave to which controller has to
153 * @return zero on success else a negative value
155 int spi_claim_bus(struct spi_slave *slave)
157 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
158 struct exynos_spi *regs = spi_slave->regs;
162 ret = set_spi_clk(spi_slave->periph_id,
165 debug("%s: Failed to setup spi clock\n", __func__);
169 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
171 spi_flush_fifo(slave);
173 reg = readl(®s->ch_cfg);
174 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
176 if (spi_slave->mode & SPI_CPHA)
177 reg |= SPI_CH_CPHA_B;
179 if (spi_slave->mode & SPI_CPOL)
180 reg |= SPI_CH_CPOL_L;
182 writel(reg, ®s->ch_cfg);
183 writel(SPI_FB_DELAY_180, ®s->fb_clk);
189 * Reset the spi H/W and flush the tx and rx fifos
191 * @param slave Pointer to spi_slave to which controller has to
194 void spi_release_bus(struct spi_slave *slave)
196 spi_flush_fifo(slave);
199 static void spi_get_fifo_levels(struct exynos_spi *regs,
200 int *rx_lvl, int *tx_lvl)
202 uint32_t spi_sts = readl(®s->spi_sts);
204 *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
205 *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
209 * If there's something to transfer, do a software reset and set a
212 * @param regs SPI peripheral registers
213 * @param count Number of bytes to transfer
215 static void spi_request_bytes(struct exynos_spi *regs, int count)
217 assert(count && count < (1 << 16));
218 setbits_le32(®s->ch_cfg, SPI_CH_RST);
219 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
220 writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
223 static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
224 void **dinp, void const **doutp, unsigned long flags)
226 struct exynos_spi *regs = spi_slave->regs;
228 const uchar *txp = *doutp;
230 uint out_bytes, in_bytes;
232 unsigned start = get_timer(0);
235 out_bytes = in_bytes = todo;
237 stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
238 !(spi_slave->mode & SPI_SLAVE);
241 * If there's something to send, do a software reset and set a
244 spi_request_bytes(regs, todo);
247 * Bytes are transmitted/received in pairs. Wait to receive all the
248 * data because then transmission will be done as well.
255 /* Keep the fifos full/empty. */
256 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
257 if (tx_lvl < spi_slave->fifo_size && out_bytes) {
258 temp = txp ? *txp++ : 0xff;
259 writel(temp, ®s->tx_data);
263 temp = readl(®s->rx_data);
264 if (spi_slave->skip_preamble) {
265 if (temp == SPI_PREAMBLE_END_BYTE) {
266 spi_slave->skip_preamble = 0;
275 } else if (!toread) {
277 * We have run out of input data, but haven't read
278 * enough bytes after the preamble yet. Read some more,
279 * and make sure that we transmit dummy bytes too, to
283 out_bytes = in_bytes;
286 spi_request_bytes(regs, toread);
288 if (spi_slave->skip_preamble && get_timer(start) > 100) {
289 printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
290 in_bytes, out_bytes);
302 * Transfer and receive data
304 * @param slave Pointer to spi_slave to which controller has to
306 * @param bitlen No of bits to tranfer or receive
307 * @param dout Pointer to transfer buffer
308 * @param din Pointer to receive buffer
309 * @param flags Flags for transfer begin and end
310 * @return zero on success else a negative value
312 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
313 void *din, unsigned long flags)
315 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
320 /* spi core configured to do 8 bit transfers */
322 debug("Non byte aligned SPI transfer.\n");
326 /* Start the transaction, if necessary. */
327 if ((flags & SPI_XFER_BEGIN))
328 spi_cs_activate(slave);
330 /* Exynos SPI limits each transfer to 65535 bytes */
331 bytelen = bitlen / 8;
332 for (upto = 0; !ret && upto < bytelen; upto += todo) {
333 todo = min(bytelen - upto, (1 << 16) - 1);
334 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
339 /* Stop the transaction, if necessary. */
340 if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
341 spi_cs_deactivate(slave);
342 if (spi_slave->skip_preamble) {
343 assert(!spi_slave->skip_preamble);
344 debug("Failed to complete premable transaction\n");
353 * Validates the bus and chip select numbers
355 * @param bus ID of the bus that the slave is attached to
356 * @param cs ID of the chip select connected to the slave
357 * @return one on success else zero
359 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
361 return spi_get_bus(bus) && cs == 0;
365 * Activate the CS by driving it LOW
367 * @param slave Pointer to spi_slave to which controller has to
370 void spi_cs_activate(struct spi_slave *slave)
372 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
374 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
375 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
376 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
380 * Deactivate the CS by driving it HIGH
382 * @param slave Pointer to spi_slave to which controller has to
385 void spi_cs_deactivate(struct spi_slave *slave)
387 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
389 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
390 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
393 static inline struct exynos_spi *get_spi_base(int dev_index)
396 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
398 return (struct exynos_spi *)samsung_get_base_spi_isp() +
403 * Read the SPI config from the device tree node.
405 * @param blob FDT blob to read from
406 * @param node Node offset to read from
407 * @param bus SPI bus structure to fill with information
408 * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
410 #ifdef CONFIG_OF_CONTROL
411 static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
414 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
415 bus->periph_id = pinmux_decode_periph_id(blob, node);
417 if (bus->periph_id == PERIPH_ID_NONE) {
418 debug("%s: Invalid peripheral ID %d\n", __func__,
420 return -FDT_ERR_NOTFOUND;
423 /* Use 500KHz as a suitable default */
424 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
431 * Process a list of nodes, adding them to our list of SPI ports.
433 * @param blob fdt blob
434 * @param node_list list of nodes to process (any <=0 are ignored)
435 * @param count number of nodes to process
436 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
437 * @return 0 if ok, -1 on error
439 static int process_nodes(const void *blob, int node_list[], int count)
443 /* build the i2c_controllers[] for each controller */
444 for (i = 0; i < count; i++) {
445 int node = node_list[i];
452 if (spi_get_config(blob, node, bus)) {
453 printf("exynos spi_init: failed to decode bus %d\n",
458 debug("spi: controller bus %d at %p, periph_id %d\n",
459 i, bus->regs, bus->periph_id);
468 /* Sadly there is no error return from this function */
473 #ifdef CONFIG_OF_CONTROL
474 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
475 const void *blob = gd->fdt_blob;
477 count = fdtdec_find_aliases_for_id(blob, "spi",
478 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
479 EXYNOS5_SPI_NUM_CONTROLLERS);
480 if (process_nodes(blob, node_list, count))
486 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
487 bus = &spi_bus[count];
488 bus->regs = get_spi_base(count);
489 bus->periph_id = PERIPH_ID_SPI0 + count;
491 /* Although Exynos5 supports upto 50Mhz speed,
492 * we are setting it to 10Mhz for safe side
494 bus->frequency = 10000000;
497 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;