blob: 1bcc3ad318db2fc3fa7f54acb6dc560706b1d70d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Padmavathi Venna <padma.v@samsung.com>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +00005 */
6
7#include <common.h>
Simon Glassca7eafe2014-10-13 23:42:01 -06008#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000011#include <malloc.h>
12#include <spi.h>
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +000013#include <fdtdec.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000015#include <asm/arch/clk.h>
16#include <asm/arch/clock.h>
17#include <asm/arch/cpu.h>
18#include <asm/arch/gpio.h>
19#include <asm/arch/pinmux.h>
Thomas Abraham74f84862015-08-03 17:58:00 +053020#include <asm/arch/spi.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000022#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000024
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +000025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glassb75b15b2020-12-03 16:55:23 -070027struct exynos_spi_plat {
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000028 enum periph_id periph_id;
29 s32 frequency; /* Default clock frequency, -1 for none */
30 struct exynos_spi *regs;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +053031 uint deactivate_delay_us; /* Delay to wait after deactivate */
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000032};
33
Simon Glassca7eafe2014-10-13 23:42:01 -060034struct exynos_spi_priv {
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000035 struct exynos_spi *regs;
36 unsigned int freq; /* Default frequency */
37 unsigned int mode;
38 enum periph_id periph_id; /* Peripheral ID for this device */
39 unsigned int fifo_size;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +000040 int skip_preamble;
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +053041 ulong last_transaction_us; /* Time of last transaction end */
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000042};
43
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000044/**
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000045 * Flush spi tx, rx fifos and reset the SPI controller
46 *
Simon Glassca7eafe2014-10-13 23:42:01 -060047 * @param regs Pointer to SPI registers
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000048 */
Simon Glassca7eafe2014-10-13 23:42:01 -060049static void spi_flush_fifo(struct exynos_spi *regs)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000050{
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000051 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
52 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
53 setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
54}
55
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000056static void spi_get_fifo_levels(struct exynos_spi *regs,
57 int *rx_lvl, int *tx_lvl)
58{
59 uint32_t spi_sts = readl(&regs->spi_sts);
60
61 *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
62 *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
63}
64
65/**
66 * If there's something to transfer, do a software reset and set a
67 * transaction size.
68 *
69 * @param regs SPI peripheral registers
70 * @param count Number of bytes to transfer
Rajeshwari Shindeffc74612013-10-08 16:20:06 +053071 * @param step Number of bytes to transfer in each packet (1 or 4)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000072 */
Rajeshwari Shindeffc74612013-10-08 16:20:06 +053073static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000074{
Simon Glassca7eafe2014-10-13 23:42:01 -060075 debug("%s: regs=%p, count=%d, step=%d\n", __func__, regs, count, step);
76
Rajeshwari Shindeffc74612013-10-08 16:20:06 +053077 /* For word address we need to swap bytes */
78 if (step == 4) {
79 setbits_le32(&regs->mode_cfg,
80 SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
81 count /= 4;
82 setbits_le32(&regs->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN |
83 SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP |
84 SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP);
85 } else {
86 /* Select byte access and clear the swap configuration */
87 clrbits_le32(&regs->mode_cfg,
88 SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
89 writel(0, &regs->swap_cfg);
90 }
91
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000092 assert(count && count < (1 << 16));
93 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
94 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
Rajeshwari Shindeffc74612013-10-08 16:20:06 +053095
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +000096 writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
97}
98
Simon Glassca7eafe2014-10-13 23:42:01 -060099static int spi_rx_tx(struct exynos_spi_priv *priv, int todo,
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000100 void **dinp, void const **doutp, unsigned long flags)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000101{
Simon Glassca7eafe2014-10-13 23:42:01 -0600102 struct exynos_spi *regs = priv->regs;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000103 uchar *rxp = *dinp;
104 const uchar *txp = *doutp;
105 int rx_lvl, tx_lvl;
106 uint out_bytes, in_bytes;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000107 int toread;
108 unsigned start = get_timer(0);
109 int stopping;
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530110 int step;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000111
112 out_bytes = in_bytes = todo;
113
Simon Glassca7eafe2014-10-13 23:42:01 -0600114 stopping = priv->skip_preamble && (flags & SPI_XFER_END) &&
115 !(priv->mode & SPI_SLAVE);
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000116
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000117 /*
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530118 * Try to transfer words if we can. This helps read performance at
119 * SPI clock speeds above about 20MHz.
120 */
121 step = 1;
122 if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
Simon Glassca7eafe2014-10-13 23:42:01 -0600123 !priv->skip_preamble)
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530124 step = 4;
125
126 /*
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000127 * If there's something to send, do a software reset and set a
128 * transaction size.
129 */
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530130 spi_request_bytes(regs, todo, step);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000131
132 /*
133 * Bytes are transmitted/received in pairs. Wait to receive all the
134 * data because then transmission will be done as well.
135 */
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000136 toread = in_bytes;
137
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000138 while (in_bytes) {
139 int temp;
140
141 /* Keep the fifos full/empty. */
142 spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530143
144 /*
145 * Don't completely fill the txfifo, since we don't want our
146 * rxfifo to overflow, and it may already contain data.
147 */
Simon Glassca7eafe2014-10-13 23:42:01 -0600148 while (tx_lvl < priv->fifo_size/2 && out_bytes) {
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530149 if (!txp)
150 temp = -1;
151 else if (step == 4)
152 temp = *(uint32_t *)txp;
153 else
154 temp = *txp;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000155 writel(temp, &regs->tx_data);
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530156 out_bytes -= step;
157 if (txp)
158 txp += step;
159 tx_lvl += step;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000160 }
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530161 if (rx_lvl >= step) {
162 while (rx_lvl >= step) {
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530163 temp = readl(&regs->rx_data);
Simon Glassca7eafe2014-10-13 23:42:01 -0600164 if (priv->skip_preamble) {
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530165 if (temp == SPI_PREAMBLE_END_BYTE) {
Simon Glassca7eafe2014-10-13 23:42:01 -0600166 priv->skip_preamble = 0;
Rajeshwari Shinde0c0faef2013-10-08 16:20:05 +0530167 stopping = 0;
168 }
169 } else {
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530170 if (rxp || stopping) {
Akshay Saraswat8613bed2014-06-18 17:52:41 +0530171 if (step == 4)
172 *(uint32_t *)rxp = temp;
173 else
174 *rxp = temp;
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530175 rxp += step;
176 }
177 in_bytes -= step;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000178 }
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530179 toread -= step;
180 rx_lvl -= step;
181 }
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000182 } else if (!toread) {
183 /*
184 * We have run out of input data, but haven't read
185 * enough bytes after the preamble yet. Read some more,
186 * and make sure that we transmit dummy bytes too, to
187 * keep things going.
188 */
189 assert(!out_bytes);
190 out_bytes = in_bytes;
191 toread = in_bytes;
192 txp = NULL;
Rajeshwari Shindeffc74612013-10-08 16:20:06 +0530193 spi_request_bytes(regs, toread, step);
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000194 }
Simon Glassca7eafe2014-10-13 23:42:01 -0600195 if (priv->skip_preamble && get_timer(start) > 100) {
Simon Glass905ed0b2015-07-02 18:16:11 -0600196 debug("SPI timeout: in_bytes=%d, out_bytes=%d, ",
197 in_bytes, out_bytes);
198 return -ETIMEDOUT;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000199 }
200 }
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000201
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000202 *dinp = rxp;
203 *doutp = txp;
Rajeshwari Shinde813637c2013-05-28 20:10:38 +0000204
205 return 0;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000206}
207
208/**
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000209 * Activate the CS by driving it LOW
210 *
211 * @param slave Pointer to spi_slave to which controller has to
212 * communicate with
213 */
Simon Glassca7eafe2014-10-13 23:42:01 -0600214static void spi_cs_activate(struct udevice *dev)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000215{
Simon Glassca7eafe2014-10-13 23:42:01 -0600216 struct udevice *bus = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700217 struct exynos_spi_plat *pdata = dev_get_plat(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600218 struct exynos_spi_priv *priv = dev_get_priv(bus);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000219
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530220 /* If it's too soon to do another transaction, wait */
Simon Glassca7eafe2014-10-13 23:42:01 -0600221 if (pdata->deactivate_delay_us &&
222 priv->last_transaction_us) {
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530223 ulong delay_us; /* The delay completed so far */
Simon Glassca7eafe2014-10-13 23:42:01 -0600224 delay_us = timer_get_us() - priv->last_transaction_us;
225 if (delay_us < pdata->deactivate_delay_us)
226 udelay(pdata->deactivate_delay_us - delay_us);
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530227 }
228
Simon Glassca7eafe2014-10-13 23:42:01 -0600229 clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
230 debug("Activate CS, bus '%s'\n", bus->name);
231 priv->skip_preamble = priv->mode & SPI_PREAMBLE;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000232}
233
234/**
235 * Deactivate the CS by driving it HIGH
236 *
237 * @param slave Pointer to spi_slave to which controller has to
238 * communicate with
239 */
Simon Glassca7eafe2014-10-13 23:42:01 -0600240static void spi_cs_deactivate(struct udevice *dev)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000241{
Simon Glassca7eafe2014-10-13 23:42:01 -0600242 struct udevice *bus = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700243 struct exynos_spi_plat *pdata = dev_get_plat(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600244 struct exynos_spi_priv *priv = dev_get_priv(bus);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000245
Simon Glassca7eafe2014-10-13 23:42:01 -0600246 setbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
Simon Glassa193ed02014-07-07 10:16:38 -0600247
248 /* Remember time of this transaction so we can honour the bus delay */
Simon Glassca7eafe2014-10-13 23:42:01 -0600249 if (pdata->deactivate_delay_us)
250 priv->last_transaction_us = timer_get_us();
Simon Glassa193ed02014-07-07 10:16:38 -0600251
Simon Glassca7eafe2014-10-13 23:42:01 -0600252 debug("Deactivate CS, bus '%s'\n", bus->name);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000253}
254
Simon Glassaad29ae2020-12-03 16:55:21 -0700255static int exynos_spi_of_to_plat(struct udevice *bus)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000256{
Simon Glass95588622020-12-22 19:30:28 -0700257 struct exynos_spi_plat *plat = dev_get_plat(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600258 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700259 int node = dev_of_offset(bus);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000260
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900261 plat->regs = dev_read_addr_ptr(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600262 plat->periph_id = pinmux_decode_periph_id(blob, node);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000263
Simon Glassca7eafe2014-10-13 23:42:01 -0600264 if (plat->periph_id == PERIPH_ID_NONE) {
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000265 debug("%s: Invalid peripheral ID %d\n", __func__,
Simon Glassca7eafe2014-10-13 23:42:01 -0600266 plat->periph_id);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000267 return -FDT_ERR_NOTFOUND;
268 }
269
270 /* Use 500KHz as a suitable default */
Simon Glassca7eafe2014-10-13 23:42:01 -0600271 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000272 500000);
Simon Glassca7eafe2014-10-13 23:42:01 -0600273 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
Rajeshwari Shindeab46adb2013-10-08 16:20:04 +0530274 "spi-deactivate-delay", 0);
Simon Glassca7eafe2014-10-13 23:42:01 -0600275 debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
276 __func__, plat->regs, plat->periph_id, plat->frequency,
277 plat->deactivate_delay_us);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000278
279 return 0;
280}
281
Simon Glassca7eafe2014-10-13 23:42:01 -0600282static int exynos_spi_probe(struct udevice *bus)
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000283{
Simon Glassb75b15b2020-12-03 16:55:23 -0700284 struct exynos_spi_plat *plat = dev_get_plat(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600285 struct exynos_spi_priv *priv = dev_get_priv(bus);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000286
Simon Glassca7eafe2014-10-13 23:42:01 -0600287 priv->regs = plat->regs;
288 if (plat->periph_id == PERIPH_ID_SPI1 ||
289 plat->periph_id == PERIPH_ID_SPI2)
290 priv->fifo_size = 64;
291 else
292 priv->fifo_size = 256;
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000293
Simon Glassca7eafe2014-10-13 23:42:01 -0600294 priv->skip_preamble = 0;
295 priv->last_transaction_us = timer_get_us();
296 priv->freq = plat->frequency;
297 priv->periph_id = plat->periph_id;
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000298
Simon Glassca7eafe2014-10-13 23:42:01 -0600299 return 0;
300}
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000301
Simon Glass5c74fba2015-04-19 09:05:40 -0600302static int exynos_spi_claim_bus(struct udevice *dev)
Simon Glassca7eafe2014-10-13 23:42:01 -0600303{
Simon Glass5c74fba2015-04-19 09:05:40 -0600304 struct udevice *bus = dev->parent;
Simon Glassca7eafe2014-10-13 23:42:01 -0600305 struct exynos_spi_priv *priv = dev_get_priv(bus);
306
307 exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
308 spi_flush_fifo(priv->regs);
309
310 writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000311
312 return 0;
313}
314
Simon Glass5c74fba2015-04-19 09:05:40 -0600315static int exynos_spi_release_bus(struct udevice *dev)
Hung-ying Tyan00391232013-05-15 18:27:30 +0800316{
Simon Glass5c74fba2015-04-19 09:05:40 -0600317 struct udevice *bus = dev->parent;
Simon Glassca7eafe2014-10-13 23:42:01 -0600318 struct exynos_spi_priv *priv = dev_get_priv(bus);
Hung-ying Tyan00391232013-05-15 18:27:30 +0800319
Simon Glassca7eafe2014-10-13 23:42:01 -0600320 spi_flush_fifo(priv->regs);
321
322 return 0;
323}
324
325static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
326 const void *dout, void *din, unsigned long flags)
327{
328 struct udevice *bus = dev->parent;
329 struct exynos_spi_priv *priv = dev_get_priv(bus);
330 int upto, todo;
331 int bytelen;
332 int ret = 0;
333
334 /* spi core configured to do 8 bit transfers */
335 if (bitlen % 8) {
336 debug("Non byte aligned SPI transfer.\n");
337 return -1;
Hung-ying Tyan00391232013-05-15 18:27:30 +0800338 }
339
Simon Glassca7eafe2014-10-13 23:42:01 -0600340 /* Start the transaction, if necessary. */
341 if ((flags & SPI_XFER_BEGIN))
342 spi_cs_activate(dev);
343
344 /*
345 * Exynos SPI limits each transfer to 65535 transfers. To keep
346 * things simple, allow a maximum of 65532 bytes. We could allow
347 * more in word mode, but the performance difference is small.
348 */
349 bytelen = bitlen / 8;
350 for (upto = 0; !ret && upto < bytelen; upto += todo) {
351 todo = min(bytelen - upto, (1 << 16) - 4);
352 ret = spi_rx_tx(priv, todo, &din, &dout, flags);
353 if (ret)
354 break;
355 }
356
357 /* Stop the transaction, if necessary. */
358 if ((flags & SPI_XFER_END) && !(priv->mode & SPI_SLAVE)) {
359 spi_cs_deactivate(dev);
360 if (priv->skip_preamble) {
361 assert(!priv->skip_preamble);
362 debug("Failed to complete premable transaction\n");
363 ret = -1;
364 }
365 }
366
367 return ret;
368}
369
370static int exynos_spi_set_speed(struct udevice *bus, uint speed)
371{
Simon Glass95588622020-12-22 19:30:28 -0700372 struct exynos_spi_plat *plat = dev_get_plat(bus);
Simon Glassca7eafe2014-10-13 23:42:01 -0600373 struct exynos_spi_priv *priv = dev_get_priv(bus);
374 int ret;
375
376 if (speed > plat->frequency)
377 speed = plat->frequency;
378 ret = set_spi_clk(priv->periph_id, speed);
379 if (ret)
380 return ret;
381 priv->freq = speed;
382 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
383
384 return 0;
Hung-ying Tyan00391232013-05-15 18:27:30 +0800385}
386
Simon Glassca7eafe2014-10-13 23:42:01 -0600387static int exynos_spi_set_mode(struct udevice *bus, uint mode)
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000388{
Simon Glassca7eafe2014-10-13 23:42:01 -0600389 struct exynos_spi_priv *priv = dev_get_priv(bus);
390 uint32_t reg;
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000391
Simon Glassca7eafe2014-10-13 23:42:01 -0600392 reg = readl(&priv->regs->ch_cfg);
393 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000394
Simon Glassca7eafe2014-10-13 23:42:01 -0600395 if (mode & SPI_CPHA)
396 reg |= SPI_CH_CPHA_B;
Rajeshwari Shinde64ef8a32012-12-26 20:03:23 +0000397
Simon Glassca7eafe2014-10-13 23:42:01 -0600398 if (mode & SPI_CPOL)
399 reg |= SPI_CH_CPOL_L;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000400
Simon Glassca7eafe2014-10-13 23:42:01 -0600401 writel(reg, &priv->regs->ch_cfg);
402 priv->mode = mode;
403 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000404
Simon Glassca7eafe2014-10-13 23:42:01 -0600405 return 0;
Rajeshwari Shindeba3b8932012-11-02 01:15:36 +0000406}
Simon Glassca7eafe2014-10-13 23:42:01 -0600407
408static const struct dm_spi_ops exynos_spi_ops = {
409 .claim_bus = exynos_spi_claim_bus,
410 .release_bus = exynos_spi_release_bus,
411 .xfer = exynos_spi_xfer,
412 .set_speed = exynos_spi_set_speed,
413 .set_mode = exynos_spi_set_mode,
414 /*
415 * cs_info is not needed, since we require all chip selects to be
416 * in the device tree explicitly
417 */
418};
419
420static const struct udevice_id exynos_spi_ids[] = {
421 { .compatible = "samsung,exynos-spi" },
422 { }
423};
424
425U_BOOT_DRIVER(exynos_spi) = {
426 .name = "exynos_spi",
427 .id = UCLASS_SPI,
428 .of_match = exynos_spi_ids,
429 .ops = &exynos_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700430 .of_to_plat = exynos_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700431 .plat_auto = sizeof(struct exynos_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700432 .priv_auto = sizeof(struct exynos_spi_priv),
Simon Glassca7eafe2014-10-13 23:42:01 -0600433 .probe = exynos_spi_probe,
434};