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;
56 static struct spi_bus *spi_get_bus(unsigned dev_index)
58 if (dev_index < bus_count)
59 return &spi_bus[dev_index];
60 debug("%s: invalid bus %d", __func__, dev_index);
65 static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
67 return container_of(slave, struct exynos_spi_slave, slave);
71 * Setup the driver private data
73 * @param bus ID of the bus that the slave is attached to
74 * @param cs ID of the chip select connected to the slave
75 * @param max_hz Required spi frequency
76 * @param mode Required spi mode (clk polarity, clk phase and
78 * @return new device or NULL
80 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
81 unsigned int max_hz, unsigned int mode)
83 struct exynos_spi_slave *spi_slave;
86 if (!spi_cs_is_valid(busnum, cs)) {
87 debug("%s: Invalid bus/chip select %d, %d\n", __func__,
92 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
94 debug("%s: Could not allocate spi_slave\n", __func__);
98 bus = &spi_bus[busnum];
99 spi_slave->regs = bus->regs;
100 spi_slave->mode = mode;
101 spi_slave->periph_id = bus->periph_id;
102 if (bus->periph_id == PERIPH_ID_SPI1 ||
103 bus->periph_id == PERIPH_ID_SPI2)
104 spi_slave->fifo_size = 64;
106 spi_slave->fifo_size = 256;
108 spi_slave->freq = bus->frequency;
110 spi_slave->freq = min(max_hz, spi_slave->freq);
112 return &spi_slave->slave;
116 * Free spi controller
118 * @param slave Pointer to spi_slave to which controller has to
121 void spi_free_slave(struct spi_slave *slave)
123 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
129 * Flush spi tx, rx fifos and reset the SPI controller
131 * @param slave Pointer to spi_slave to which controller has to
134 static void spi_flush_fifo(struct spi_slave *slave)
136 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
137 struct exynos_spi *regs = spi_slave->regs;
139 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
140 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
141 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
145 * Initialize the spi base registers, set the required clock frequency and
146 * initialize the gpios
148 * @param slave Pointer to spi_slave to which controller has to
150 * @return zero on success else a negative value
152 int spi_claim_bus(struct spi_slave *slave)
154 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
155 struct exynos_spi *regs = spi_slave->regs;
159 ret = set_spi_clk(spi_slave->periph_id,
162 debug("%s: Failed to setup spi clock\n", __func__);
166 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
168 spi_flush_fifo(slave);
170 reg = readl(®s->ch_cfg);
171 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
173 if (spi_slave->mode & SPI_CPHA)
174 reg |= SPI_CH_CPHA_B;
176 if (spi_slave->mode & SPI_CPOL)
177 reg |= SPI_CH_CPOL_L;
179 writel(reg, ®s->ch_cfg);
180 writel(SPI_FB_DELAY_180, ®s->fb_clk);
186 * Reset the spi H/W and flush the tx and rx fifos
188 * @param slave Pointer to spi_slave to which controller has to
191 void spi_release_bus(struct spi_slave *slave)
193 spi_flush_fifo(slave);
196 static void spi_get_fifo_levels(struct exynos_spi *regs,
197 int *rx_lvl, int *tx_lvl)
199 uint32_t spi_sts = readl(®s->spi_sts);
201 *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
202 *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
206 * If there's something to transfer, do a software reset and set a
209 * @param regs SPI peripheral registers
210 * @param count Number of bytes to transfer
212 static void spi_request_bytes(struct exynos_spi *regs, int count)
214 assert(count && count < (1 << 16));
215 setbits_le32(®s->ch_cfg, SPI_CH_RST);
216 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
217 writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
220 static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
221 void **dinp, void const **doutp)
223 struct exynos_spi *regs = spi_slave->regs;
225 const uchar *txp = *doutp;
227 uint out_bytes, in_bytes;
229 out_bytes = in_bytes = todo;
232 * If there's something to send, do a software reset and set a
235 spi_request_bytes(regs, todo);
238 * Bytes are transmitted/received in pairs. Wait to receive all the
239 * data because then transmission will be done as well.
244 /* Keep the fifos full/empty. */
245 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
246 if (tx_lvl < spi_slave->fifo_size && out_bytes) {
247 temp = txp ? *txp++ : 0xff;
248 writel(temp, ®s->tx_data);
251 if (rx_lvl > 0 && in_bytes) {
252 temp = readl(®s->rx_data);
263 * Transfer and receive data
265 * @param slave Pointer to spi_slave to which controller has to
267 * @param bitlen No of bits to tranfer or receive
268 * @param dout Pointer to transfer buffer
269 * @param din Pointer to receive buffer
270 * @param flags Flags for transfer begin and end
271 * @return zero on success else a negative value
273 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
274 void *din, unsigned long flags)
276 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
280 /* spi core configured to do 8 bit transfers */
282 debug("Non byte aligned SPI transfer.\n");
286 /* Start the transaction, if necessary. */
287 if ((flags & SPI_XFER_BEGIN))
288 spi_cs_activate(slave);
290 /* Exynos SPI limits each transfer to 65535 bytes */
291 bytelen = bitlen / 8;
292 for (upto = 0; upto < bytelen; upto += todo) {
293 todo = min(bytelen - upto, (1 << 16) - 1);
294 spi_rx_tx(spi_slave, todo, &din, &dout);
297 /* Stop the transaction, if necessary. */
298 if ((flags & SPI_XFER_END))
299 spi_cs_deactivate(slave);
305 * Validates the bus and chip select numbers
307 * @param bus ID of the bus that the slave is attached to
308 * @param cs ID of the chip select connected to the slave
309 * @return one on success else zero
311 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
313 return spi_get_bus(bus) && cs == 0;
317 * Activate the CS by driving it LOW
319 * @param slave Pointer to spi_slave to which controller has to
322 void spi_cs_activate(struct spi_slave *slave)
324 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
326 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
327 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
331 * Deactivate the CS by driving it HIGH
333 * @param slave Pointer to spi_slave to which controller has to
336 void spi_cs_deactivate(struct spi_slave *slave)
338 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
340 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
341 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
344 static inline struct exynos_spi *get_spi_base(int dev_index)
347 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
349 return (struct exynos_spi *)samsung_get_base_spi_isp() +
354 * Read the SPI config from the device tree node.
356 * @param blob FDT blob to read from
357 * @param node Node offset to read from
358 * @param bus SPI bus structure to fill with information
359 * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
361 static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
364 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
365 bus->periph_id = pinmux_decode_periph_id(blob, node);
367 if (bus->periph_id == PERIPH_ID_NONE) {
368 debug("%s: Invalid peripheral ID %d\n", __func__,
370 return -FDT_ERR_NOTFOUND;
373 /* Use 500KHz as a suitable default */
374 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
381 * Process a list of nodes, adding them to our list of SPI ports.
383 * @param blob fdt blob
384 * @param node_list list of nodes to process (any <=0 are ignored)
385 * @param count number of nodes to process
386 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
387 * @return 0 if ok, -1 on error
389 static int process_nodes(const void *blob, int node_list[], int count)
393 /* build the i2c_controllers[] for each controller */
394 for (i = 0; i < count; i++) {
395 int node = node_list[i];
402 if (spi_get_config(blob, node, bus)) {
403 printf("exynos spi_init: failed to decode bus %d\n",
408 debug("spi: controller bus %d at %p, periph_id %d\n",
409 i, bus->regs, bus->periph_id);
417 /* Sadly there is no error return from this function */
422 #ifdef CONFIG_OF_CONTROL
423 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
424 const void *blob = gd->fdt_blob;
426 count = fdtdec_find_aliases_for_id(blob, "spi",
427 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
428 EXYNOS5_SPI_NUM_CONTROLLERS);
429 if (process_nodes(blob, node_list, count))
435 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
436 bus = &spi_bus[count];
437 bus->regs = get_spi_base(count);
438 bus->periph_id = PERIPH_ID_SPI0 + count;
440 /* Although Exynos5 supports upto 50Mhz speed,
441 * we are setting it to 10Mhz for safe side
443 bus->frequency = 10000000;
446 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;