blob: 01378d098e53cf7065425bd4d83a5a38415ba4d5 [file] [log] [blame]
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +00001/*
2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Padmavathi Venna <padma.v@samsung.com>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include <common.h>
21#include <malloc.h>
22#include <spi.h>
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +000023#include <fdtdec.h>
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000024#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>
30#include <asm/io.h>
31
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +000032DECLARE_GLOBAL_DATA_PTR;
33
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000034/* Information about each SPI controller */
35struct spi_bus {
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 */
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +000040 int node;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000041};
42
43/* A list of spi buses that we know about */
44static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +000045static unsigned int bus_count;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000046
47struct exynos_spi_slave {
48 struct spi_slave slave;
49 struct exynos_spi *regs;
50 unsigned int freq; /* Default frequency */
51 unsigned int mode;
52 enum periph_id periph_id; /* Peripheral ID for this device */
53 unsigned int fifo_size;
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +000054 int skip_preamble;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000055};
56
57static struct spi_bus *spi_get_bus(unsigned dev_index)
58{
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +000059 if (dev_index < bus_count)
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000060 return &spi_bus[dev_index];
61 debug("%s: invalid bus %d", __func__, dev_index);
62
63 return NULL;
64}
65
66static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
67{
68 return container_of(slave, struct exynos_spi_slave, slave);
69}
70
71/**
72 * Setup the driver private data
73 *
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
78 * master or slave)
79 * @return new device or NULL
80 */
81struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
82 unsigned int max_hz, unsigned int mode)
83{
84 struct exynos_spi_slave *spi_slave;
85 struct spi_bus *bus;
86
87 if (!spi_cs_is_valid(busnum, cs)) {
88 debug("%s: Invalid bus/chip select %d, %d\n", __func__,
89 busnum, cs);
90 return NULL;
91 }
92
Simon Glassd3504fe2013-03-18 19:23:40 +000093 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +000094 if (!spi_slave) {
95 debug("%s: Could not allocate spi_slave\n", __func__);
96 return NULL;
97 }
98
99 bus = &spi_bus[busnum];
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000100 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;
106 else
107 spi_slave->fifo_size = 256;
108
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000109 spi_slave->skip_preamble = 0;
110
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000111 spi_slave->freq = bus->frequency;
112 if (max_hz)
113 spi_slave->freq = min(max_hz, spi_slave->freq);
114
115 return &spi_slave->slave;
116}
117
118/**
119 * Free spi controller
120 *
121 * @param slave Pointer to spi_slave to which controller has to
122 * communicate with
123 */
124void spi_free_slave(struct spi_slave *slave)
125{
126 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
127
128 free(spi_slave);
129}
130
131/**
132 * Flush spi tx, rx fifos and reset the SPI controller
133 *
134 * @param slave Pointer to spi_slave to which controller has to
135 * communicate with
136 */
137static void spi_flush_fifo(struct spi_slave *slave)
138{
139 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
140 struct exynos_spi *regs = spi_slave->regs;
141
142 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
143 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
144 setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
145}
146
147/**
148 * Initialize the spi base registers, set the required clock frequency and
149 * initialize the gpios
150 *
151 * @param slave Pointer to spi_slave to which controller has to
152 * communicate with
153 * @return zero on success else a negative value
154 */
155int spi_claim_bus(struct spi_slave *slave)
156{
157 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
158 struct exynos_spi *regs = spi_slave->regs;
159 u32 reg = 0;
160 int ret;
161
162 ret = set_spi_clk(spi_slave->periph_id,
163 spi_slave->freq);
164 if (ret < 0) {
165 debug("%s: Failed to setup spi clock\n", __func__);
166 return ret;
167 }
168
169 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
170
171 spi_flush_fifo(slave);
172
173 reg = readl(&regs->ch_cfg);
174 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
175
176 if (spi_slave->mode & SPI_CPHA)
177 reg |= SPI_CH_CPHA_B;
178
179 if (spi_slave->mode & SPI_CPOL)
180 reg |= SPI_CH_CPOL_L;
181
182 writel(reg, &regs->ch_cfg);
183 writel(SPI_FB_DELAY_180, &regs->fb_clk);
184
185 return 0;
186}
187
188/**
189 * Reset the spi H/W and flush the tx and rx fifos
190 *
191 * @param slave Pointer to spi_slave to which controller has to
192 * communicate with
193 */
194void spi_release_bus(struct spi_slave *slave)
195{
196 spi_flush_fifo(slave);
197}
198
199static void spi_get_fifo_levels(struct exynos_spi *regs,
200 int *rx_lvl, int *tx_lvl)
201{
202 uint32_t spi_sts = readl(&regs->spi_sts);
203
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;
206}
207
208/**
209 * If there's something to transfer, do a software reset and set a
210 * transaction size.
211 *
212 * @param regs SPI peripheral registers
213 * @param count Number of bytes to transfer
214 */
215static void spi_request_bytes(struct exynos_spi *regs, int count)
216{
217 assert(count && count < (1 << 16));
218 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
219 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
220 writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
221}
222
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000223static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
224 void **dinp, void const **doutp, unsigned long flags)
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000225{
226 struct exynos_spi *regs = spi_slave->regs;
227 uchar *rxp = *dinp;
228 const uchar *txp = *doutp;
229 int rx_lvl, tx_lvl;
230 uint out_bytes, in_bytes;
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000231 int toread;
232 unsigned start = get_timer(0);
233 int stopping;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000234
235 out_bytes = in_bytes = todo;
236
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000237 stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
238 !(spi_slave->mode & SPI_SLAVE);
239
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000240 /*
241 * If there's something to send, do a software reset and set a
242 * transaction size.
243 */
244 spi_request_bytes(regs, todo);
245
246 /*
247 * Bytes are transmitted/received in pairs. Wait to receive all the
248 * data because then transmission will be done as well.
249 */
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000250 toread = in_bytes;
251
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000252 while (in_bytes) {
253 int temp;
254
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, &regs->tx_data);
260 out_bytes--;
261 }
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000262 if (rx_lvl > 0) {
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000263 temp = readl(&regs->rx_data);
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000264 if (spi_slave->skip_preamble) {
265 if (temp == SPI_PREAMBLE_END_BYTE) {
266 spi_slave->skip_preamble = 0;
267 stopping = 0;
268 }
269 } else {
270 if (rxp || stopping)
271 *rxp++ = temp;
272 in_bytes--;
273 }
274 toread--;
275 } else if (!toread) {
276 /*
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
280 * keep things going.
281 */
282 assert(!out_bytes);
283 out_bytes = in_bytes;
284 toread = in_bytes;
285 txp = NULL;
286 spi_request_bytes(regs, toread);
287 }
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);
291 return -1;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000292 }
293 }
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000294
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000295 *dinp = rxp;
296 *doutp = txp;
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000297
298 return 0;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000299}
300
301/**
302 * Transfer and receive data
303 *
304 * @param slave Pointer to spi_slave to which controller has to
305 * communicate with
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
311 */
312int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
313 void *din, unsigned long flags)
314{
315 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
316 int upto, todo;
317 int bytelen;
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000318 int ret = 0;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000319
320 /* spi core configured to do 8 bit transfers */
321 if (bitlen % 8) {
322 debug("Non byte aligned SPI transfer.\n");
323 return -1;
324 }
325
326 /* Start the transaction, if necessary. */
327 if ((flags & SPI_XFER_BEGIN))
328 spi_cs_activate(slave);
329
330 /* Exynos SPI limits each transfer to 65535 bytes */
331 bytelen = bitlen / 8;
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000332 for (upto = 0; !ret && upto < bytelen; upto += todo) {
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000333 todo = min(bytelen - upto, (1 << 16) - 1);
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000334 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
335 if (ret)
336 break;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000337 }
338
339 /* Stop the transaction, if necessary. */
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000340 if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000341 spi_cs_deactivate(slave);
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000342 if (spi_slave->skip_preamble) {
343 assert(!spi_slave->skip_preamble);
344 debug("Failed to complete premable transaction\n");
345 ret = -1;
346 }
347 }
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000348
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000349 return ret;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000350}
351
352/**
353 * Validates the bus and chip select numbers
354 *
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
358 */
359int spi_cs_is_valid(unsigned int bus, unsigned int cs)
360{
361 return spi_get_bus(bus) && cs == 0;
362}
363
364/**
365 * Activate the CS by driving it LOW
366 *
367 * @param slave Pointer to spi_slave to which controller has to
368 * communicate with
369 */
370void spi_cs_activate(struct spi_slave *slave)
371{
372 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
373
374 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
375 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
Rajeshwari Shindee4eaef82013-05-28 20:10:38 +0000376 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000377}
378
379/**
380 * Deactivate the CS by driving it HIGH
381 *
382 * @param slave Pointer to spi_slave to which controller has to
383 * communicate with
384 */
385void spi_cs_deactivate(struct spi_slave *slave)
386{
387 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
388
389 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
390 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
391}
392
393static inline struct exynos_spi *get_spi_base(int dev_index)
394{
395 if (dev_index < 3)
396 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
397 else
398 return (struct exynos_spi *)samsung_get_base_spi_isp() +
399 (dev_index - 3);
400}
401
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000402/*
403 * Read the SPI config from the device tree node.
404 *
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
409 */
Vivek Gautam7d179582013-03-05 03:49:57 +0000410#ifdef CONFIG_OF_CONTROL
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000411static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
412{
413 bus->node = node;
414 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
415 bus->periph_id = pinmux_decode_periph_id(blob, node);
416
417 if (bus->periph_id == PERIPH_ID_NONE) {
418 debug("%s: Invalid peripheral ID %d\n", __func__,
419 bus->periph_id);
420 return -FDT_ERR_NOTFOUND;
421 }
422
423 /* Use 500KHz as a suitable default */
424 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
425 500000);
426
427 return 0;
428}
429
430/*
431 * Process a list of nodes, adding them to our list of SPI ports.
432 *
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
438 */
439static int process_nodes(const void *blob, int node_list[], int count)
440{
441 int i;
442
443 /* build the i2c_controllers[] for each controller */
444 for (i = 0; i < count; i++) {
445 int node = node_list[i];
446 struct spi_bus *bus;
447
448 if (node <= 0)
449 continue;
450
451 bus = &spi_bus[i];
452 if (spi_get_config(blob, node, bus)) {
453 printf("exynos spi_init: failed to decode bus %d\n",
454 i);
455 return -1;
456 }
457
458 debug("spi: controller bus %d at %p, periph_id %d\n",
459 i, bus->regs, bus->periph_id);
460 bus->inited = 1;
461 bus_count++;
462 }
463
464 return 0;
465}
Vivek Gautam7d179582013-03-05 03:49:57 +0000466#endif
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000467
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000468/* Sadly there is no error return from this function */
469void spi_init(void)
470{
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000471 int count;
472
473#ifdef CONFIG_OF_CONTROL
474 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
475 const void *blob = gd->fdt_blob;
476
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))
481 return;
482
483#else
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000484 struct spi_bus *bus;
485
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000486 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;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000490
491 /* Although Exynos5 supports upto 50Mhz speed,
492 * we are setting it to 10Mhz for safe side
493 */
494 bus->frequency = 10000000;
495 bus->inited = 1;
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000496 bus->node = 0;
497 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000498 }
Rajeshwari Shinde4d3acb92012-12-26 20:03:23 +0000499#endif
Rajeshwari Shinde1bf43b82012-11-02 01:15:36 +0000500}