]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/exynos_spi.c
cros: exynos: add SPI support for cros_ec
[karo-tx-uboot.git] / drivers / spi / exynos_spi.c
1 /*
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>
23 #include <fdtdec.h>
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>
30 #include <asm/io.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 /* Information about each SPI controller */
35 struct 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 */
40         int node;
41 };
42
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;
46
47 struct 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;
54         int skip_preamble;
55 };
56
57 static struct spi_bus *spi_get_bus(unsigned dev_index)
58 {
59         if (dev_index < bus_count)
60                 return &spi_bus[dev_index];
61         debug("%s: invalid bus %d", __func__, dev_index);
62
63         return NULL;
64 }
65
66 static 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  */
81 struct 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
93         spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
94         if (!spi_slave) {
95                 debug("%s: Could not allocate spi_slave\n", __func__);
96                 return NULL;
97         }
98
99         bus = &spi_bus[busnum];
100         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
109         spi_slave->skip_preamble = 0;
110
111         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  */
124 void 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  */
137 static 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  */
155 int 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  */
194 void spi_release_bus(struct spi_slave *slave)
195 {
196         spi_flush_fifo(slave);
197 }
198
199 static 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  */
215 static 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
223 static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
224                         void **dinp, void const **doutp, unsigned long flags)
225 {
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;
231         int toread;
232         unsigned start = get_timer(0);
233         int stopping;
234
235         out_bytes = in_bytes = todo;
236
237         stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
238                                         !(spi_slave->mode & SPI_SLAVE);
239
240         /*
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          */
250         toread = in_bytes;
251
252         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                 }
262                 if (rx_lvl > 0) {
263                         temp = readl(&regs->rx_data);
264                         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;
292                 }
293         }
294
295         *dinp = rxp;
296         *doutp = txp;
297
298         return 0;
299 }
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  */
312 int 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;
318         int ret = 0;
319
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;
332         for (upto = 0; !ret && upto < bytelen; upto += todo) {
333                 todo = min(bytelen - upto, (1 << 16) - 1);
334                 ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
335                 if (ret)
336                         break;
337         }
338
339         /* Stop the transaction, if necessary. */
340         if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
341                 spi_cs_deactivate(slave);
342                 if (spi_slave->skip_preamble) {
343                         assert(!spi_slave->skip_preamble);
344                         debug("Failed to complete premable transaction\n");
345                         ret = -1;
346                 }
347         }
348
349         return ret;
350 }
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  */
359 int 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  */
370 void 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);
376         spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
377 }
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  */
385 void 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
393 static 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
402 /*
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  */
410 #ifdef CONFIG_OF_CONTROL
411 static 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  */
439 static 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 }
466 #endif
467
468 /**
469  * Set up a new SPI slave for an fdt node
470  *
471  * @param blob          Device tree blob
472  * @param node          SPI peripheral node to use
473  * @return 0 if ok, -1 on error
474  */
475 struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
476                 unsigned int cs, unsigned int max_hz, unsigned int mode)
477 {
478         struct spi_bus *bus;
479         unsigned int i;
480
481         for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
482                 if (bus->node == node)
483                         return spi_setup_slave(i, cs, max_hz, mode);
484         }
485
486         debug("%s: Failed to find bus node %d\n", __func__, node);
487         return NULL;
488 }
489
490 /* Sadly there is no error return from this function */
491 void spi_init(void)
492 {
493         int count;
494
495 #ifdef CONFIG_OF_CONTROL
496         int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
497         const void *blob = gd->fdt_blob;
498
499         count = fdtdec_find_aliases_for_id(blob, "spi",
500                         COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
501                         EXYNOS5_SPI_NUM_CONTROLLERS);
502         if (process_nodes(blob, node_list, count))
503                 return;
504
505 #else
506         struct spi_bus *bus;
507
508         for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
509                 bus = &spi_bus[count];
510                 bus->regs = get_spi_base(count);
511                 bus->periph_id = PERIPH_ID_SPI0 + count;
512
513                 /* Although Exynos5 supports upto 50Mhz speed,
514                  * we are setting it to 10Mhz for safe side
515                  */
516                 bus->frequency = 10000000;
517                 bus->inited = 1;
518                 bus->node = 0;
519                 bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
520         }
521 #endif
522 }