1
2
3
4
5
6
7
8#include <common.h>
9#include <malloc.h>
10#include <spi.h>
11#include <fdtdec.h>
12#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
20DECLARE_GLOBAL_DATA_PTR;
21
22
23struct spi_bus {
24 enum periph_id periph_id;
25 s32 frequency;
26 struct exynos_spi *regs;
27 int inited;
28 int node;
29 uint deactivate_delay_us;
30};
31
32
33static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
34static unsigned int bus_count;
35
36struct exynos_spi_slave {
37 struct spi_slave slave;
38 struct exynos_spi *regs;
39 unsigned int freq;
40 unsigned int mode;
41 enum periph_id periph_id;
42 unsigned int fifo_size;
43 int skip_preamble;
44 struct spi_bus *bus;
45 ulong last_transaction_us;
46};
47
48static struct spi_bus *spi_get_bus(unsigned dev_index)
49{
50 if (dev_index < bus_count)
51 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
64
65
66
67
68
69
70
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
84 spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
85 if (!spi_slave) {
86 debug("%s: Could not allocate spi_slave\n", __func__);
87 return NULL;
88 }
89
90 bus = &spi_bus[busnum];
91 spi_slave->bus = bus;
92 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
101 spi_slave->skip_preamble = 0;
102 spi_slave->last_transaction_us = timer_get_us();
103
104 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
113
114
115
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
126
127
128
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(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
136 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
137 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
138}
139
140
141
142
143
144
145
146
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(®s->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, ®s->ch_cfg);
176 writel(SPI_FB_DELAY_180, ®s->fb_clk);
177
178 return 0;
179}
180
181
182
183
184
185
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(®s->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
203
204
205
206
207
208
209static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
210{
211
212 if (step == 4) {
213 setbits_le32(®s->mode_cfg,
214 SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
215 count /= 4;
216 setbits_le32(®s->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN |
217 SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP |
218 SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP);
219 } else {
220
221 clrbits_le32(®s->mode_cfg,
222 SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
223 writel(0, ®s->swap_cfg);
224 }
225
226 assert(count && count < (1 << 16));
227 setbits_le32(®s->ch_cfg, SPI_CH_RST);
228 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
229
230 writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
231}
232
233static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
234 void **dinp, void const **doutp, unsigned long flags)
235{
236 struct exynos_spi *regs = spi_slave->regs;
237 uchar *rxp = *dinp;
238 const uchar *txp = *doutp;
239 int rx_lvl, tx_lvl;
240 uint out_bytes, in_bytes;
241 int toread;
242 unsigned start = get_timer(0);
243 int stopping;
244 int step;
245
246 out_bytes = in_bytes = todo;
247
248 stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
249 !(spi_slave->mode & SPI_SLAVE);
250
251
252
253
254
255 step = 1;
256 if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
257 !spi_slave->skip_preamble)
258 step = 4;
259
260
261
262
263
264 spi_request_bytes(regs, todo, step);
265
266
267
268
269
270 toread = in_bytes;
271
272 while (in_bytes) {
273 int temp;
274
275
276 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
277
278
279
280
281
282 while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
283 if (!txp)
284 temp = -1;
285 else if (step == 4)
286 temp = *(uint32_t *)txp;
287 else
288 temp = *txp;
289 writel(temp, ®s->tx_data);
290 out_bytes -= step;
291 if (txp)
292 txp += step;
293 tx_lvl += step;
294 }
295 if (rx_lvl >= step) {
296 while (rx_lvl >= step) {
297 temp = readl(®s->rx_data);
298 if (spi_slave->skip_preamble) {
299 if (temp == SPI_PREAMBLE_END_BYTE) {
300 spi_slave->skip_preamble = 0;
301 stopping = 0;
302 }
303 } else {
304 if (rxp || stopping) {
305 *rxp = temp;
306 rxp += step;
307 }
308 in_bytes -= step;
309 }
310 toread -= step;
311 rx_lvl -= step;
312 }
313 } else if (!toread) {
314
315
316
317
318
319
320 assert(!out_bytes);
321 out_bytes = in_bytes;
322 toread = in_bytes;
323 txp = NULL;
324 spi_request_bytes(regs, toread, step);
325 }
326 if (spi_slave->skip_preamble && get_timer(start) > 100) {
327 printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
328 in_bytes, out_bytes);
329 return -1;
330 }
331 }
332
333 *dinp = rxp;
334 *doutp = txp;
335
336 return 0;
337}
338
339
340
341
342
343
344
345
346
347
348
349
350int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
351 void *din, unsigned long flags)
352{
353 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
354 int upto, todo;
355 int bytelen;
356 int ret = 0;
357
358
359 if (bitlen % 8) {
360 debug("Non byte aligned SPI transfer.\n");
361 return -1;
362 }
363
364
365 if ((flags & SPI_XFER_BEGIN))
366 spi_cs_activate(slave);
367
368
369
370
371
372
373 bytelen = bitlen / 8;
374 for (upto = 0; !ret && upto < bytelen; upto += todo) {
375 todo = min(bytelen - upto, (1 << 16) - 4);
376 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
377 if (ret)
378 break;
379 }
380
381
382 if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
383 spi_cs_deactivate(slave);
384 if (spi_slave->skip_preamble) {
385 assert(!spi_slave->skip_preamble);
386 debug("Failed to complete premable transaction\n");
387 ret = -1;
388 }
389 }
390
391 return ret;
392}
393
394
395
396
397
398
399
400
401int spi_cs_is_valid(unsigned int bus, unsigned int cs)
402{
403 return spi_get_bus(bus) && cs == 0;
404}
405
406
407
408
409
410
411
412void spi_cs_activate(struct spi_slave *slave)
413{
414 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
415
416
417 if (spi_slave->bus->deactivate_delay_us &&
418 spi_slave->last_transaction_us) {
419 ulong delay_us;
420 delay_us = timer_get_us() - spi_slave->last_transaction_us;
421 if (delay_us < spi_slave->bus->deactivate_delay_us)
422 udelay(spi_slave->bus->deactivate_delay_us - delay_us);
423 }
424
425 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
426 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
427 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
428
429
430 if (spi_slave->bus->deactivate_delay_us)
431 spi_slave->last_transaction_us = timer_get_us();
432}
433
434
435
436
437
438
439
440void spi_cs_deactivate(struct spi_slave *slave)
441{
442 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
443
444 setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
445 debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
446}
447
448static inline struct exynos_spi *get_spi_base(int dev_index)
449{
450 if (dev_index < 3)
451 return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
452 else
453 return (struct exynos_spi *)samsung_get_base_spi_isp() +
454 (dev_index - 3);
455}
456
457
458
459
460
461
462
463
464
465#ifdef CONFIG_OF_CONTROL
466static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
467{
468 bus->node = node;
469 bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
470 bus->periph_id = pinmux_decode_periph_id(blob, node);
471
472 if (bus->periph_id == PERIPH_ID_NONE) {
473 debug("%s: Invalid peripheral ID %d\n", __func__,
474 bus->periph_id);
475 return -FDT_ERR_NOTFOUND;
476 }
477
478
479 bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
480 500000);
481 bus->deactivate_delay_us = fdtdec_get_int(blob, node,
482 "spi-deactivate-delay", 0);
483
484 return 0;
485}
486
487
488
489
490
491
492
493
494
495
496static int process_nodes(const void *blob, int node_list[], int count)
497{
498 int i;
499
500
501 for (i = 0; i < count; i++) {
502 int node = node_list[i];
503 struct spi_bus *bus;
504
505 if (node <= 0)
506 continue;
507
508 bus = &spi_bus[i];
509 if (spi_get_config(blob, node, bus)) {
510 printf("exynos spi_init: failed to decode bus %d\n",
511 i);
512 return -1;
513 }
514
515 debug("spi: controller bus %d at %p, periph_id %d\n",
516 i, bus->regs, bus->periph_id);
517 bus->inited = 1;
518 bus_count++;
519 }
520
521 return 0;
522}
523#endif
524
525
526
527
528
529
530
531
532struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
533 int spi_node)
534{
535 struct spi_bus *bus;
536 unsigned int i;
537
538 for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
539 if (bus->node == spi_node)
540 return spi_base_setup_slave_fdt(blob, i, slave_node);
541 }
542
543 debug("%s: Failed to find bus node %d\n", __func__, spi_node);
544 return NULL;
545}
546
547
548void spi_init(void)
549{
550 int count;
551
552#ifdef CONFIG_OF_CONTROL
553 int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
554 const void *blob = gd->fdt_blob;
555
556 count = fdtdec_find_aliases_for_id(blob, "spi",
557 COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
558 EXYNOS5_SPI_NUM_CONTROLLERS);
559 if (process_nodes(blob, node_list, count))
560 return;
561
562#else
563 struct spi_bus *bus;
564
565 for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
566 bus = &spi_bus[count];
567 bus->regs = get_spi_base(count);
568 bus->periph_id = PERIPH_ID_SPI0 + count;
569
570
571
572
573 bus->frequency = 10000000;
574 bus->inited = 1;
575 bus->node = 0;
576 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
577 }
578#endif
579}
580