blob: 7407d6cc1287b05e535befbc8ded08a6cc2aa857 [file] [log] [blame]
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +00001/*
2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Padmavathi Venna <padma.v@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +00006 */
7
8#include <common.h>
9#include <malloc.h>
10#include <spi.h>
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +000011#include <fdtdec.h>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000012#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 Shinde64ef8a32012-12-26 20:03:23 +000020DECLARE_GLOBAL_DATA_PTR;
21
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000022/* Information about each SPI controller */
23struct 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 Shinde64ef8a32012-12-26 20:03:23 +000028 int node;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +053029 uint deactivate_delay_us; /* Delay to wait after deactivate */
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000030};
31
32/* A list of spi buses that we know about */
33static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +000034static unsigned int bus_count;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000035
36struct 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 Shinde813637c2013-05-28 20:10:38 +000043 int skip_preamble;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +053044 struct spi_bus *bus; /* Pointer to our SPI bus info */
45 ulong last_transaction_us; /* Time of last transaction end */
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000046};
47
48static struct spi_bus *spi_get_bus(unsigned dev_index)
49{
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +000050 if (dev_index < bus_count)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000051 return &spi_bus[dev_index];
52 debug("%s: invalid bus %d", __func__, dev_index);
53
54 return NULL;
55}
56
57static 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 */
72struct 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 Glassd034a952013-03-18 19:23:40 +000084 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000085 if (!spi_slave) {
86 debug("%s: Could not allocate spi_slave\n", __func__);
87 return NULL;
88 }
89
90 bus = &spi_bus[busnum];
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +053091 spi_slave->bus = bus;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000092 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 Shinde813637c2013-05-28 20:10:38 +0000101 spi_slave->skip_preamble = 0;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530102 spi_slave->last_transaction_us = timer_get_us();
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000103
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000104 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 */
117void 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 */
130static 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(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
136 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
137 setbits_le32(&regs->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 */
148int 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(&regs->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, &regs->ch_cfg);
176 writel(SPI_FB_DELAY_180, &regs->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 */
187void spi_release_bus(struct spi_slave *slave)
188{
189 spi_flush_fifo(slave);
190}
191
192static void spi_get_fifo_levels(struct exynos_spi *regs,
193 int *rx_lvl, int *tx_lvl)
194{
195 uint32_t spi_sts = readl(&regs->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
207 */
208static void spi_request_bytes(struct exynos_spi *regs, int count)
209{
210 assert(count && count < (1 << 16));
211 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
212 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
213 writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
214}
215
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000216static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
217 void **dinp, void const **doutp, unsigned long flags)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000218{
219 struct exynos_spi *regs = spi_slave->regs;
220 uchar *rxp = *dinp;
221 const uchar *txp = *doutp;
222 int rx_lvl, tx_lvl;
223 uint out_bytes, in_bytes;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000224 int toread;
225 unsigned start = get_timer(0);
226 int stopping;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000227
228 out_bytes = in_bytes = todo;
229
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000230 stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
231 !(spi_slave->mode & SPI_SLAVE);
232
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000233 /*
234 * If there's something to send, do a software reset and set a
235 * transaction size.
236 */
237 spi_request_bytes(regs, todo);
238
239 /*
240 * Bytes are transmitted/received in pairs. Wait to receive all the
241 * data because then transmission will be done as well.
242 */
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000243 toread = in_bytes;
244
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000245 while (in_bytes) {
246 int temp;
247
248 /* Keep the fifos full/empty. */
249 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530250 while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000251 temp = txp ? *txp++ : 0xff;
252 writel(temp, &regs->tx_data);
253 out_bytes--;
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530254 tx_lvl++;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000255 }
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000256 if (rx_lvl > 0) {
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530257 while (rx_lvl > 0) {
258 temp = readl(&regs->rx_data);
259 if (spi_slave->skip_preamble) {
260 if (temp == SPI_PREAMBLE_END_BYTE) {
261 spi_slave->skip_preamble = 0;
262 stopping = 0;
263 }
264 } else {
265 if (rxp || stopping)
266 *rxp++ = temp;
267 in_bytes--;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000268 }
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530269 toread--;
270 rx_lvl--;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000271 } else if (!toread) {
272 /*
273 * We have run out of input data, but haven't read
274 * enough bytes after the preamble yet. Read some more,
275 * and make sure that we transmit dummy bytes too, to
276 * keep things going.
277 */
278 assert(!out_bytes);
279 out_bytes = in_bytes;
280 toread = in_bytes;
281 txp = NULL;
282 spi_request_bytes(regs, toread);
283 }
284 if (spi_slave->skip_preamble && get_timer(start) > 100) {
285 printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
286 in_bytes, out_bytes);
287 return -1;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000288 }
289 }
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000290
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000291 *dinp = rxp;
292 *doutp = txp;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000293
294 return 0;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000295}
296
297/**
298 * Transfer and receive data
299 *
300 * @param slave Pointer to spi_slave to which controller has to
301 * communicate with
302 * @param bitlen No of bits to tranfer or receive
303 * @param dout Pointer to transfer buffer
304 * @param din Pointer to receive buffer
305 * @param flags Flags for transfer begin and end
306 * @return zero on success else a negative value
307 */
308int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
309 void *din, unsigned long flags)
310{
311 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
312 int upto, todo;
313 int bytelen;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000314 int ret = 0;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000315
316 /* spi core configured to do 8 bit transfers */
317 if (bitlen % 8) {
318 debug("Non byte aligned SPI transfer.\n");
319 return -1;
320 }
321
322 /* Start the transaction, if necessary. */
323 if ((flags & SPI_XFER_BEGIN))
324 spi_cs_activate(slave);
325
326 /* Exynos SPI limits each transfer to 65535 bytes */
327 bytelen = bitlen / 8;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000328 for (upto = 0; !ret && upto < bytelen; upto += todo) {
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000329 todo = min(bytelen - upto, (1 << 16) - 1);
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000330 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
331 if (ret)
332 break;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000333 }
334
335 /* Stop the transaction, if necessary. */
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000336 if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000337 spi_cs_deactivate(slave);
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000338 if (spi_slave->skip_preamble) {
339 assert(!spi_slave->skip_preamble);
340 debug("Failed to complete premable transaction\n");
341 ret = -1;
342 }
343 }
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000344
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000345 return ret;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000346}
347
348/**
349 * Validates the bus and chip select numbers
350 *
351 * @param bus ID of the bus that the slave is attached to
352 * @param cs ID of the chip select connected to the slave
353 * @return one on success else zero
354 */
355int spi_cs_is_valid(unsigned int bus, unsigned int cs)
356{
357 return spi_get_bus(bus) && cs == 0;
358}
359
360/**
361 * Activate the CS by driving it LOW
362 *
363 * @param slave Pointer to spi_slave to which controller has to
364 * communicate with
365 */
366void spi_cs_activate(struct spi_slave *slave)
367{
368 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
369
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530370 /* If it's too soon to do another transaction, wait */
371 if (spi_slave->bus->deactivate_delay_us &&
372 spi_slave->last_transaction_us) {
373 ulong delay_us; /* The delay completed so far */
374 delay_us = timer_get_us() - spi_slave->last_transaction_us;
375 if (delay_us < spi_slave->bus->deactivate_delay_us)
376 udelay(spi_slave->bus->deactivate_delay_us - delay_us);
377 }
378
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000379 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
380 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000381 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530382
383 /* Remember time of this transaction so we can honour the bus delay */
384 if (spi_slave->bus->deactivate_delay_us)
385 spi_slave->last_transaction_us = timer_get_us();
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000386}
387
388/**
389 * Deactivate the CS by driving it HIGH
390 *
391 * @param slave Pointer to spi_slave to which controller has to
392 * communicate with
393 */
394void spi_cs_deactivate(struct spi_slave *slave)
395{
396 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
397
398 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
399 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
400}
401
402static inline struct exynos_spi *get_spi_base(int dev_index)
403{
404 if (dev_index < 3)
405 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
406 else
407 return (struct exynos_spi *)samsung_get_base_spi_isp() +
408 (dev_index - 3);
409}
410
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000411/*
412 * Read the SPI config from the device tree node.
413 *
414 * @param blob FDT blob to read from
415 * @param node Node offset to read from
416 * @param bus SPI bus structure to fill with information
417 * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
418 */
Vivek Gautam602c9112013-03-05 03:49:57 +0000419#ifdef CONFIG_OF_CONTROL
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000420static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
421{
422 bus->node = node;
423 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
424 bus->periph_id = pinmux_decode_periph_id(blob, node);
425
426 if (bus->periph_id == PERIPH_ID_NONE) {
427 debug("%s: Invalid peripheral ID %d\n", __func__,
428 bus->periph_id);
429 return -FDT_ERR_NOTFOUND;
430 }
431
432 /* Use 500KHz as a suitable default */
433 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
434 500000);
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530435 bus->deactivate_delay_us = fdtdec_get_int(blob, node,
436 "spi-deactivate-delay", 0);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000437
438 return 0;
439}
440
441/*
442 * Process a list of nodes, adding them to our list of SPI ports.
443 *
444 * @param blob fdt blob
445 * @param node_list list of nodes to process (any <=0 are ignored)
446 * @param count number of nodes to process
447 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
448 * @return 0 if ok, -1 on error
449 */
450static int process_nodes(const void *blob, int node_list[], int count)
451{
452 int i;
453
454 /* build the i2c_controllers[] for each controller */
455 for (i = 0; i < count; i++) {
456 int node = node_list[i];
457 struct spi_bus *bus;
458
459 if (node <= 0)
460 continue;
461
462 bus = &spi_bus[i];
463 if (spi_get_config(blob, node, bus)) {
464 printf("exynos spi_init: failed to decode bus %d\n",
465 i);
466 return -1;
467 }
468
469 debug("spi: controller bus %d at %p, periph_id %d\n",
470 i, bus->regs, bus->periph_id);
471 bus->inited = 1;
472 bus_count++;
473 }
474
475 return 0;
476}
Vivek Gautam602c9112013-03-05 03:49:57 +0000477#endif
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000478
Hung-ying Tyan00391232013-05-15 18:27:30 +0800479/**
480 * Set up a new SPI slave for an fdt node
481 *
482 * @param blob Device tree blob
483 * @param node SPI peripheral node to use
484 * @return 0 if ok, -1 on error
485 */
486struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
487 unsigned int cs, unsigned int max_hz, unsigned int mode)
488{
489 struct spi_bus *bus;
490 unsigned int i;
491
492 for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
493 if (bus->node == node)
494 return spi_setup_slave(i, cs, max_hz, mode);
495 }
496
497 debug("%s: Failed to find bus node %d\n", __func__, node);
498 return NULL;
499}
500
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000501/* Sadly there is no error return from this function */
502void spi_init(void)
503{
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000504 int count;
505
506#ifdef CONFIG_OF_CONTROL
507 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
508 const void *blob = gd->fdt_blob;
509
510 count = fdtdec_find_aliases_for_id(blob, "spi",
511 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
512 EXYNOS5_SPI_NUM_CONTROLLERS);
513 if (process_nodes(blob, node_list, count))
514 return;
515
516#else
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000517 struct spi_bus *bus;
518
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000519 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
520 bus = &spi_bus[count];
521 bus->regs = get_spi_base(count);
522 bus->periph_id = PERIPH_ID_SPI0 + count;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000523
524 /* Although Exynos5 supports upto 50Mhz speed,
525 * we are setting it to 10Mhz for safe side
526 */
527 bus->frequency = 10000000;
528 bus->inited = 1;
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000529 bus->node = 0;
530 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000531 }
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000532#endif
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000533}