blob: cb3d44cb0f63f002e6e80548f5753b405753d07a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +08002/*
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
7 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8 * Chao Fu (B44548@freescale.com)
9 * Haikun Wang (B53464@freescale.com)
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +080010 */
Simon Glass51a3ec32017-05-17 17:18:07 -060011
12#include <common.h>
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +080013#include <dm.h>
14#include <errno.h>
15#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +080017#include <spi.h>
18#include <malloc.h>
19#include <asm/io.h>
20#include <fdtdec.h>
21#ifndef CONFIG_M68K
22#include <asm/arch/clock.h>
23#endif
24#include <fsl_dspi.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +080026
27DECLARE_GLOBAL_DATA_PTR;
28
29/* fsl_dspi_platdata flags */
Jagan Tekic97ca922015-10-23 01:37:18 +053030#define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +080031
32/* idle data value */
33#define DSPI_IDLE_VAL 0x0
34
35/* max chipselect signals number */
36#define FSL_DSPI_MAX_CHIPSELECT 6
37
38/* default SCK frequency, unit: HZ */
39#define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
40
41/* tx/rx data wait timeout value, unit: us */
42#define DSPI_TXRX_WAIT_TIMEOUT 1000000
43
44/* CTAR register pre-configure value */
45#define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
46 DSPI_CTAR_PCSSCK_1CLK | \
47 DSPI_CTAR_PASC(0) | \
48 DSPI_CTAR_PDT(0) | \
49 DSPI_CTAR_CSSCK(0) | \
50 DSPI_CTAR_ASC(0) | \
51 DSPI_CTAR_DT(0))
52
53/* CTAR register pre-configure mask */
54#define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
55 DSPI_CTAR_PCSSCK(3) | \
56 DSPI_CTAR_PASC(3) | \
57 DSPI_CTAR_PDT(3) | \
58 DSPI_CTAR_CSSCK(15) | \
59 DSPI_CTAR_ASC(15) | \
60 DSPI_CTAR_DT(15))
61
62/**
63 * struct fsl_dspi_platdata - platform data for Freescale DSPI
64 *
65 * @flags: Flags for DSPI DSPI_FLAG_...
66 * @speed_hz: Default SCK frequency
67 * @num_chipselect: Number of DSPI chipselect signals
68 * @regs_addr: Base address of DSPI registers
69 */
70struct fsl_dspi_platdata {
71 uint flags;
72 uint speed_hz;
73 uint num_chipselect;
74 fdt_addr_t regs_addr;
75};
76
77/**
78 * struct fsl_dspi_priv - private data for Freescale DSPI
79 *
80 * @flags: Flags for DSPI DSPI_FLAG_...
81 * @mode: SPI mode to use for slave device (see SPI mode flags)
82 * @mcr_val: MCR register configure value
83 * @bus_clk: DSPI input clk frequency
84 * @speed_hz: Default SCK frequency
85 * @charbit: How many bits in every transfer
86 * @num_chipselect: Number of DSPI chipselect signals
87 * @ctar_val: CTAR register configure value of per chipselect slave device
88 * @regs: Point to DSPI register structure for I/O access
89 */
90struct fsl_dspi_priv {
91 uint flags;
92 uint mode;
93 uint mcr_val;
94 uint bus_clk;
95 uint speed_hz;
96 uint charbit;
97 uint num_chipselect;
98 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
99 struct dspi *regs;
100};
101
102#ifndef CONFIG_DM_SPI
103struct fsl_dspi {
104 struct spi_slave slave;
105 struct fsl_dspi_priv priv;
106};
107#endif
108
109__weak void cpu_dspi_port_conf(void)
110{
111}
112
113__weak int cpu_dspi_claim_bus(uint bus, uint cs)
114{
115 return 0;
116}
117
118__weak void cpu_dspi_release_bus(uint bus, uint cs)
119{
120}
121
122static uint dspi_read32(uint flags, uint *addr)
123{
124 return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
125 in_be32(addr) : in_le32(addr);
126}
127
128static void dspi_write32(uint flags, uint *addr, uint val)
129{
130 flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
131 out_be32(addr, val) : out_le32(addr, val);
132}
133
134static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
135{
136 uint mcr_val;
137
138 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
139
140 if (halt)
141 mcr_val |= DSPI_MCR_HALT;
142 else
143 mcr_val &= ~DSPI_MCR_HALT;
144
145 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
146}
147
148static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
149{
150 /* halt DSPI module */
151 dspi_halt(priv, 1);
152
153 dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
154
155 /* resume module */
156 dspi_halt(priv, 0);
157
158 priv->mcr_val = cfg_val;
159}
160
161static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
162 uint cs, uint state)
163{
164 uint mcr_val;
165
166 dspi_halt(priv, 1);
167
168 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
169 if (state & SPI_CS_HIGH)
170 /* CSx inactive state is low */
171 mcr_val &= ~DSPI_MCR_PCSIS(cs);
172 else
173 /* CSx inactive state is high */
174 mcr_val |= DSPI_MCR_PCSIS(cs);
175 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
176
177 dspi_halt(priv, 0);
178}
179
180static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
181 uint cs, uint mode)
182{
183 uint bus_setup;
184
185 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
186
187 bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
188 bus_setup |= priv->ctar_val[cs];
189 bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
190
191 if (mode & SPI_CPOL)
192 bus_setup |= DSPI_CTAR_CPOL;
193 if (mode & SPI_CPHA)
194 bus_setup |= DSPI_CTAR_CPHA;
195 if (mode & SPI_LSB_FIRST)
196 bus_setup |= DSPI_CTAR_LSBFE;
197
198 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
199
200 priv->charbit =
201 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
202 DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
203
204 return 0;
205}
206
207static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
208{
209 uint mcr_val;
210
211 dspi_halt(priv, 1);
212 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
213 /* flush RX and TX FIFO */
214 mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
215 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
216 dspi_halt(priv, 0);
217}
218
219static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
220{
221 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
222
223 /* wait for empty entries in TXFIFO or timeout */
224 while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
225 timeout--)
226 udelay(1);
227
228 if (timeout >= 0)
229 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
230 else
231 debug("dspi_tx: waiting timeout!\n");
232}
233
234static u16 dspi_rx(struct fsl_dspi_priv *priv)
235{
236 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
237
238 /* wait for valid entries in RXFIFO or timeout */
239 while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
240 timeout--)
241 udelay(1);
242
243 if (timeout >= 0)
244 return (u16)DSPI_RFR_RXDATA(
245 dspi_read32(priv->flags, &priv->regs->rfr));
246 else {
247 debug("dspi_rx: waiting timeout!\n");
248 return (u16)(~0);
249 }
250}
251
252static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
253 const void *dout, void *din, unsigned long flags)
254{
255 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
256 u8 *spi_rd = NULL, *spi_wr = NULL;
257 static u32 ctrl;
258 uint len = bitlen >> 3;
259
260 if (priv->charbit == 16) {
261 bitlen >>= 1;
262 spi_wr16 = (u16 *)dout;
263 spi_rd16 = (u16 *)din;
264 } else {
265 spi_wr = (u8 *)dout;
266 spi_rd = (u8 *)din;
267 }
268
269 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
270 ctrl |= DSPI_TFR_CONT;
271
272 ctrl = ctrl & DSPI_TFR_CONT;
273 ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
274
275 if (len > 1) {
276 int tmp_len = len - 1;
277 while (tmp_len--) {
Jared Bents0d8cdcb2019-03-22 09:46:52 -0500278 if ((dout != NULL) && (din != NULL)) {
279 if (priv->charbit == 16) {
280 dspi_tx(priv, ctrl, *spi_wr16++);
281 *spi_rd16++ = dspi_rx(priv);
282 }
283 else {
284 dspi_tx(priv, ctrl, *spi_wr++);
285 *spi_rd++ = dspi_rx(priv);
286 }
287 }
288
289 else if (dout != NULL) {
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800290 if (priv->charbit == 16)
291 dspi_tx(priv, ctrl, *spi_wr16++);
292 else
293 dspi_tx(priv, ctrl, *spi_wr++);
294 dspi_rx(priv);
295 }
296
Jared Bents0d8cdcb2019-03-22 09:46:52 -0500297 else if (din != NULL) {
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800298 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
299 if (priv->charbit == 16)
300 *spi_rd16++ = dspi_rx(priv);
301 else
302 *spi_rd++ = dspi_rx(priv);
303 }
304 }
305
306 len = 1; /* remaining byte */
307 }
308
309 if ((flags & SPI_XFER_END) == SPI_XFER_END)
310 ctrl &= ~DSPI_TFR_CONT;
311
312 if (len) {
Jared Bents0d8cdcb2019-03-22 09:46:52 -0500313 if ((dout != NULL) && (din != NULL)) {
314 if (priv->charbit == 16) {
315 dspi_tx(priv, ctrl, *spi_wr16++);
316 *spi_rd16++ = dspi_rx(priv);
317 }
318 else {
319 dspi_tx(priv, ctrl, *spi_wr++);
320 *spi_rd++ = dspi_rx(priv);
321 }
322 }
323
324 else if (dout != NULL) {
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800325 if (priv->charbit == 16)
326 dspi_tx(priv, ctrl, *spi_wr16);
327 else
328 dspi_tx(priv, ctrl, *spi_wr);
329 dspi_rx(priv);
330 }
331
Jared Bents0d8cdcb2019-03-22 09:46:52 -0500332 else if (din != NULL) {
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800333 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
334 if (priv->charbit == 16)
335 *spi_rd16 = dspi_rx(priv);
336 else
337 *spi_rd = dspi_rx(priv);
338 }
339 } else {
340 /* dummy read */
341 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
342 dspi_rx(priv);
343 }
344
345 return 0;
346}
347
348/**
349 * Calculate the divide value between input clk frequency and expected SCK frequency
350 * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
351 * Dbr: use default value 0
352 *
353 * @pbr: return Baud Rate Prescaler value
354 * @br: return Baud Rate Scaler value
355 * @speed_hz: expected SCK frequency
356 * @clkrate: input clk frequency
357 */
358static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
359 int speed_hz, uint clkrate)
360{
361 /* Valid baud rate pre-scaler values */
362 int pbr_tbl[4] = {2, 3, 5, 7};
363 int brs[16] = {2, 4, 6, 8,
364 16, 32, 64, 128,
365 256, 512, 1024, 2048,
366 4096, 8192, 16384, 32768};
367 int temp, i = 0, j = 0;
368
369 temp = clkrate / speed_hz;
370
371 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
372 for (j = 0; j < ARRAY_SIZE(brs); j++) {
373 if (pbr_tbl[i] * brs[j] >= temp) {
374 *pbr = i;
375 *br = j;
376 return 0;
377 }
378 }
379
380 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
381 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
382
383 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
384 *br = ARRAY_SIZE(brs) - 1;
385 return -EINVAL;
386}
387
388static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
389{
390 int ret;
391 uint bus_setup;
392 int best_i, best_j, bus_clk;
393
394 bus_clk = priv->bus_clk;
395
396 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
397 speed, bus_clk);
398
399 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
400 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
401
402 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
403 if (ret) {
404 speed = priv->speed_hz;
405 debug("DSPI set_speed use default SCK rate %u.\n", speed);
406 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
407 }
408
409 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
410 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
411
412 priv->speed_hz = speed;
413
414 return 0;
415}
416#ifndef CONFIG_DM_SPI
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800417int spi_cs_is_valid(unsigned int bus, unsigned int cs)
418{
419 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
420 return 1;
421 else
422 return 0;
423}
424
425struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
426 unsigned int max_hz, unsigned int mode)
427{
428 struct fsl_dspi *dspi;
429 uint mcr_cfg_val;
430
431 dspi = spi_alloc_slave(struct fsl_dspi, bus, cs);
432 if (!dspi)
433 return NULL;
434
435 cpu_dspi_port_conf();
436
437#ifdef CONFIG_SYS_FSL_DSPI_BE
438 dspi->priv.flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
439#endif
440
441 dspi->priv.regs = (struct dspi *)MMAP_DSPI;
442
443#ifdef CONFIG_M68K
444 dspi->priv.bus_clk = gd->bus_clk;
445#else
446 dspi->priv.bus_clk = mxc_get_clock(MXC_DSPI_CLK);
447#endif
448 dspi->priv.speed_hz = FSL_DSPI_DEFAULT_SCK_FREQ;
449
450 /* default: all CS signals inactive state is high */
451 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
452 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
453 fsl_dspi_init_mcr(&dspi->priv, mcr_cfg_val);
454
455 for (i = 0; i < FSL_DSPI_MAX_CHIPSELECT; i++)
456 dspi->priv.ctar_val[i] = DSPI_CTAR_DEFAULT_VALUE;
457
458#ifdef CONFIG_SYS_DSPI_CTAR0
459 if (FSL_DSPI_MAX_CHIPSELECT > 0)
460 dspi->priv.ctar_val[0] = CONFIG_SYS_DSPI_CTAR0;
461#endif
462#ifdef CONFIG_SYS_DSPI_CTAR1
463 if (FSL_DSPI_MAX_CHIPSELECT > 1)
464 dspi->priv.ctar_val[1] = CONFIG_SYS_DSPI_CTAR1;
465#endif
466#ifdef CONFIG_SYS_DSPI_CTAR2
467 if (FSL_DSPI_MAX_CHIPSELECT > 2)
468 dspi->priv.ctar_val[2] = CONFIG_SYS_DSPI_CTAR2;
469#endif
470#ifdef CONFIG_SYS_DSPI_CTAR3
471 if (FSL_DSPI_MAX_CHIPSELECT > 3)
472 dspi->priv.ctar_val[3] = CONFIG_SYS_DSPI_CTAR3;
473#endif
474#ifdef CONFIG_SYS_DSPI_CTAR4
475 if (FSL_DSPI_MAX_CHIPSELECT > 4)
476 dspi->priv.ctar_val[4] = CONFIG_SYS_DSPI_CTAR4;
477#endif
478#ifdef CONFIG_SYS_DSPI_CTAR5
479 if (FSL_DSPI_MAX_CHIPSELECT > 5)
480 dspi->priv.ctar_val[5] = CONFIG_SYS_DSPI_CTAR5;
481#endif
482#ifdef CONFIG_SYS_DSPI_CTAR6
483 if (FSL_DSPI_MAX_CHIPSELECT > 6)
484 dspi->priv.ctar_val[6] = CONFIG_SYS_DSPI_CTAR6;
485#endif
486#ifdef CONFIG_SYS_DSPI_CTAR7
487 if (FSL_DSPI_MAX_CHIPSELECT > 7)
488 dspi->priv.ctar_val[7] = CONFIG_SYS_DSPI_CTAR7;
489#endif
490
491 fsl_dspi_cfg_speed(&dspi->priv, max_hz);
492
493 /* configure transfer mode */
494 fsl_dspi_cfg_ctar_mode(&dspi->priv, cs, mode);
495
496 /* configure active state of CSX */
497 fsl_dspi_cfg_cs_active_state(&dspi->priv, cs, mode);
498
499 return &dspi->slave;
500}
501
502void spi_free_slave(struct spi_slave *slave)
503{
504 free(slave);
505}
506
507int spi_claim_bus(struct spi_slave *slave)
508{
509 uint sr_val;
510 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
511
512 cpu_dspi_claim_bus(slave->bus, slave->cs);
513
514 fsl_dspi_clr_fifo(&dspi->priv);
515
516 /* check module TX and RX status */
517 sr_val = dspi_read32(dspi->priv.flags, &dspi->priv.regs->sr);
518 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
519 debug("DSPI RX/TX not ready!\n");
520 return -EIO;
521 }
522
523 return 0;
524}
525
526void spi_release_bus(struct spi_slave *slave)
527{
528 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
529
530 dspi_halt(&dspi->priv, 1);
531 cpu_dspi_release_bus(slave->bus.slave->cs);
532}
533
534int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
535 void *din, unsigned long flags)
536{
537 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
538 return dspi_xfer(&dspi->priv, slave->cs, bitlen, dout, din, flags);
539}
540#else
541static int fsl_dspi_child_pre_probe(struct udevice *dev)
542{
543 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
544 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
545
546 if (slave_plat->cs >= priv->num_chipselect) {
547 debug("DSPI invalid chipselect number %d(max %d)!\n",
548 slave_plat->cs, priv->num_chipselect - 1);
549 return -EINVAL;
550 }
551
552 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
553
554 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
555 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
556
557 return 0;
558}
559
560static int fsl_dspi_probe(struct udevice *bus)
561{
562 struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
563 struct fsl_dspi_priv *priv = dev_get_priv(bus);
564 struct dm_spi_bus *dm_spi_bus;
565 uint mcr_cfg_val;
566
567 dm_spi_bus = bus->uclass_priv;
568
569 /* cpu speical pin muxing configure */
570 cpu_dspi_port_conf();
571
572 /* get input clk frequency */
573 priv->regs = (struct dspi *)plat->regs_addr;
574 priv->flags = plat->flags;
575#ifdef CONFIG_M68K
576 priv->bus_clk = gd->bus_clk;
577#else
578 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
579#endif
580 priv->num_chipselect = plat->num_chipselect;
581 priv->speed_hz = plat->speed_hz;
582 /* frame data length in bits, default 8bits */
583 priv->charbit = 8;
584
585 dm_spi_bus->max_hz = plat->speed_hz;
586
587 /* default: all CS signals inactive state is high */
588 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
589 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
590 fsl_dspi_init_mcr(priv, mcr_cfg_val);
591
592 debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
593
594 return 0;
595}
596
597static int fsl_dspi_claim_bus(struct udevice *dev)
598{
599 uint sr_val;
600 struct fsl_dspi_priv *priv;
601 struct udevice *bus = dev->parent;
602 struct dm_spi_slave_platdata *slave_plat =
603 dev_get_parent_platdata(dev);
604
605 priv = dev_get_priv(bus);
606
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400607 /* processor special preparation work */
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800608 cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
609
610 /* configure transfer mode */
611 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
612
613 /* configure active state of CSX */
614 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
615 priv->mode);
616
617 fsl_dspi_clr_fifo(priv);
618
619 /* check module TX and RX status */
620 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
621 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
622 debug("DSPI RX/TX not ready!\n");
623 return -EIO;
624 }
625
626 return 0;
627}
628
629static int fsl_dspi_release_bus(struct udevice *dev)
630{
631 struct udevice *bus = dev->parent;
632 struct fsl_dspi_priv *priv = dev_get_priv(bus);
633 struct dm_spi_slave_platdata *slave_plat =
634 dev_get_parent_platdata(dev);
635
636 /* halt module */
637 dspi_halt(priv, 1);
638
639 /* processor special release work */
640 cpu_dspi_release_bus(bus->seq, slave_plat->cs);
641
642 return 0;
643}
644
645/**
646 * This function doesn't do anything except help with debugging
647 */
648static int fsl_dspi_bind(struct udevice *bus)
649{
650 debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
651 return 0;
652}
653
654static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
655{
656 fdt_addr_t addr;
657 struct fsl_dspi_platdata *plat = bus->platdata;
658 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700659 int node = dev_of_offset(bus);
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800660
661 if (fdtdec_get_bool(blob, node, "big-endian"))
662 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
663
664 plat->num_chipselect =
665 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
666
Simon Glassba1dea42017-05-17 17:18:05 -0600667 addr = devfdt_get_addr(bus);
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800668 if (addr == FDT_ADDR_T_NONE) {
669 debug("DSPI: Can't get base address or size\n");
670 return -ENOMEM;
671 }
672 plat->regs_addr = addr;
673
674 plat->speed_hz = fdtdec_get_int(blob,
675 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
676
York Sunaa5b66c2015-08-03 12:02:05 -0700677 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
678 &plat->regs_addr, plat->speed_hz,
Haikun.Wang@freescale.coma28e2912015-03-24 22:03:58 +0800679 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
680 plat->num_chipselect);
681
682 return 0;
683}
684
685static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
686 const void *dout, void *din, unsigned long flags)
687{
688 struct fsl_dspi_priv *priv;
689 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
690 struct udevice *bus;
691
692 bus = dev->parent;
693 priv = dev_get_priv(bus);
694
695 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
696}
697
698static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
699{
700 struct fsl_dspi_priv *priv = dev_get_priv(bus);
701
702 return fsl_dspi_cfg_speed(priv, speed);
703}
704
705static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
706{
707 struct fsl_dspi_priv *priv = dev_get_priv(bus);
708
709 debug("DSPI set_mode: mode 0x%x.\n", mode);
710
711 /*
712 * We store some chipselect special configure value in priv->ctar_val,
713 * and we can't get the correct chipselect number here,
714 * so just store mode value.
715 * Do really configuration when claim_bus.
716 */
717 priv->mode = mode;
718
719 return 0;
720}
721
722static const struct dm_spi_ops fsl_dspi_ops = {
723 .claim_bus = fsl_dspi_claim_bus,
724 .release_bus = fsl_dspi_release_bus,
725 .xfer = fsl_dspi_xfer,
726 .set_speed = fsl_dspi_set_speed,
727 .set_mode = fsl_dspi_set_mode,
728};
729
730static const struct udevice_id fsl_dspi_ids[] = {
731 { .compatible = "fsl,vf610-dspi" },
732 { }
733};
734
735U_BOOT_DRIVER(fsl_dspi) = {
736 .name = "fsl_dspi",
737 .id = UCLASS_SPI,
738 .of_match = fsl_dspi_ids,
739 .ops = &fsl_dspi_ops,
740 .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
741 .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
742 .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
743 .probe = fsl_dspi_probe,
744 .child_pre_probe = fsl_dspi_child_pre_probe,
745 .bind = fsl_dspi_bind,
746};
747#endif