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 = malloc(sizeof(*spi_slave));
94 debug("%s: Could not allocate spi_slave\n", __func__);
98 bus = &spi_bus[busnum];
99 spi_slave->slave.bus = busnum;
100 spi_slave->slave.cs = cs;
101 spi_slave->regs = bus->regs;
102 spi_slave->mode = mode;
103 spi_slave->periph_id = bus->periph_id;
104 if (bus->periph_id == PERIPH_ID_SPI1 ||
105 bus->periph_id == PERIPH_ID_SPI2)
106 spi_slave->fifo_size = 64;
108 spi_slave->fifo_size = 256;
110 spi_slave->freq = bus->frequency;
112 spi_slave->freq = min(max_hz, spi_slave->freq);
114 return &spi_slave->slave;
118 * Free spi controller
120 * @param slave Pointer to spi_slave to which controller has to
123 void spi_free_slave(struct spi_slave *slave)
125 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
131 * Flush spi tx, rx fifos and reset the SPI controller
133 * @param slave Pointer to spi_slave to which controller has to
136 static void spi_flush_fifo(struct spi_slave *slave)
138 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
139 struct exynos_spi *regs = spi_slave->regs;
141 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
142 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
143 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
147 * Initialize the spi base registers, set the required clock frequency and
148 * initialize the gpios
150 * @param slave Pointer to spi_slave to which controller has to
152 * @return zero on success else a negative value
154 int spi_claim_bus(struct spi_slave *slave)
156 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
157 struct exynos_spi *regs = spi_slave->regs;
161 ret = set_spi_clk(spi_slave->periph_id,
164 debug("%s: Failed to setup spi clock\n", __func__);
168 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
170 spi_flush_fifo(slave);
172 reg = readl(®s->ch_cfg);
173 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
175 if (spi_slave->mode & SPI_CPHA)
176 reg |= SPI_CH_CPHA_B;
178 if (spi_slave->mode & SPI_CPOL)
179 reg |= SPI_CH_CPOL_L;
181 writel(reg, ®s->ch_cfg);
182 writel(SPI_FB_DELAY_180, ®s->fb_clk);
188 * Reset the spi H/W and flush the tx and rx fifos
190 * @param slave Pointer to spi_slave to which controller has to
193 void spi_release_bus(struct spi_slave *slave)
195 spi_flush_fifo(slave);
198 static void spi_get_fifo_levels(struct exynos_spi *regs,
199 int *rx_lvl, int *tx_lvl)
201 uint32_t spi_sts = readl(®s->spi_sts);
203 *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
204 *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
208 * If there's something to transfer, do a software reset and set a
211 * @param regs SPI peripheral registers
212 * @param count Number of bytes to transfer
214 static void spi_request_bytes(struct exynos_spi *regs, int count)
216 assert(count && count < (1 << 16));
217 setbits_le32(®s->ch_cfg, SPI_CH_RST);
218 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
219 writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
222 static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
223 void **dinp, void const **doutp)
225 struct exynos_spi *regs = spi_slave->regs;
227 const uchar *txp = *doutp;
229 uint out_bytes, in_bytes;
231 out_bytes = in_bytes = todo;
234 * If there's something to send, do a software reset and set a
237 spi_request_bytes(regs, todo);
240 * Bytes are transmitted/received in pairs. Wait to receive all the
241 * data because then transmission will be done as well.
246 /* Keep the fifos full/empty. */
247 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
248 if (tx_lvl < spi_slave->fifo_size && out_bytes) {
249 temp = txp ? *txp++ : 0xff;
250 writel(temp, ®s->tx_data);
253 if (rx_lvl > 0 && in_bytes) {
254 temp = readl(®s->rx_data);
265 * Transfer and receive data
267 * @param slave Pointer to spi_slave to which controller has to
269 * @param bitlen No of bits to tranfer or receive
270 * @param dout Pointer to transfer buffer
271 * @param din Pointer to receive buffer
272 * @param flags Flags for transfer begin and end
273 * @return zero on success else a negative value
275 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
276 void *din, unsigned long flags)
278 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
282 /* spi core configured to do 8 bit transfers */
284 debug("Non byte aligned SPI transfer.\n");
288 /* Start the transaction, if necessary. */
289 if ((flags & SPI_XFER_BEGIN))
290 spi_cs_activate(slave);
292 /* Exynos SPI limits each transfer to 65535 bytes */
293 bytelen = bitlen / 8;
294 for (upto = 0; upto < bytelen; upto += todo) {
295 todo = min(bytelen - upto, (1 << 16) - 1);
296 spi_rx_tx(spi_slave, todo, &din, &dout);
299 /* Stop the transaction, if necessary. */
300 if ((flags & SPI_XFER_END))
301 spi_cs_deactivate(slave);
307 * Validates the bus and chip select numbers
309 * @param bus ID of the bus that the slave is attached to
310 * @param cs ID of the chip select connected to the slave
311 * @return one on success else zero
313 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
315 return spi_get_bus(bus) && cs == 0;
319 * Activate the CS by driving it LOW
321 * @param slave Pointer to spi_slave to which controller has to
324 void spi_cs_activate(struct spi_slave *slave)
326 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
328 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
329 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
333 * Deactivate the CS by driving it HIGH
335 * @param slave Pointer to spi_slave to which controller has to
338 void spi_cs_deactivate(struct spi_slave *slave)
340 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
342 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
343 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
346 static inline struct exynos_spi *get_spi_base(int dev_index)
349 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
351 return (struct exynos_spi *)samsung_get_base_spi_isp() +
356 * Read the SPI config from the device tree node.
358 * @param blob FDT blob to read from
359 * @param node Node offset to read from
360 * @param bus SPI bus structure to fill with information
361 * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
363 static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
366 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
367 bus->periph_id = pinmux_decode_periph_id(blob, node);
369 if (bus->periph_id == PERIPH_ID_NONE) {
370 debug("%s: Invalid peripheral ID %d\n", __func__,
372 return -FDT_ERR_NOTFOUND;
375 /* Use 500KHz as a suitable default */
376 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
383 * Process a list of nodes, adding them to our list of SPI ports.
385 * @param blob fdt blob
386 * @param node_list list of nodes to process (any <=0 are ignored)
387 * @param count number of nodes to process
388 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
389 * @return 0 if ok, -1 on error
391 static int process_nodes(const void *blob, int node_list[], int count)
395 /* build the i2c_controllers[] for each controller */
396 for (i = 0; i < count; i++) {
397 int node = node_list[i];
404 if (spi_get_config(blob, node, bus)) {
405 printf("exynos spi_init: failed to decode bus %d\n",
410 debug("spi: controller bus %d at %p, periph_id %d\n",
411 i, bus->regs, bus->periph_id);
419 /* Sadly there is no error return from this function */
424 #ifdef CONFIG_OF_CONTROL
425 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
426 const void *blob = gd->fdt_blob;
428 count = fdtdec_find_aliases_for_id(blob, "spi",
429 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
430 EXYNOS5_SPI_NUM_CONTROLLERS);
431 if (process_nodes(blob, node_list, count))
437 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
438 bus = &spi_bus[count];
439 bus->regs = get_spi_base(count);
440 bus->periph_id = PERIPH_ID_SPI0 + count;
442 /* Although Exynos5 supports upto 50Mhz speed,
443 * we are setting it to 10Mhz for safe side
445 bus->frequency = 10000000;
448 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;