blob: 923ff6f3114e1c08a29f1413249323b151e04bac [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewdec61c72009-06-30 14:09:47 +00002/*
3 *
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * Copyright (C) 2004-2009 Freescale Semiconductor, Inc.
8 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
Angelo Dureghello5ea37662019-03-13 21:46:47 +01009 *
10 * Support for DM and DT, non-DM code removed.
11 * Copyright (C) 2018 Angelo Dureghello <angelo@sysam.it>
12 *
13 * TODO: fsl_dspi.c should work as a driver for the DSPI module.
TsiChung Liewdec61c72009-06-30 14:09:47 +000014 */
15
16#include <common.h>
Angelo Dureghello5ea37662019-03-13 21:46:47 +010017#include <dm.h>
18#include <dm/platform_data/spi_coldfire.h>
TsiChung Liewdec61c72009-06-30 14:09:47 +000019#include <spi.h>
20#include <malloc.h>
Angelo Dureghello5ea37662019-03-13 21:46:47 +010021#include <asm/coldfire/dspi.h>
22#include <asm/io.h>
TsiChung Liewdec61c72009-06-30 14:09:47 +000023
Angelo Dureghello5ea37662019-03-13 21:46:47 +010024struct coldfire_spi_priv {
25 struct dspi *regs;
TsiChung Liewdec61c72009-06-30 14:09:47 +000026 uint baudrate;
Angelo Dureghello5ea37662019-03-13 21:46:47 +010027 int mode;
TsiChung Liewdec61c72009-06-30 14:09:47 +000028 int charbit;
29};
30
TsiChung Liewdec61c72009-06-30 14:09:47 +000031DECLARE_GLOBAL_DATA_PTR;
32
Wolfgang Wegnerb97e0cd2010-04-23 05:43:12 +000033#ifndef CONFIG_SPI_IDLE_VAL
34#if defined(CONFIG_SPI_MMC)
35#define CONFIG_SPI_IDLE_VAL 0xFFFF
36#else
37#define CONFIG_SPI_IDLE_VAL 0x0
38#endif
39#endif
40
Angelo Dureghello5ea37662019-03-13 21:46:47 +010041/*
42 * DSPI specific mode
43 *
44 * bit 31 - 28: Transfer size 3 to 16 bits
45 * 27 - 26: PCS to SCK delay prescaler
46 * 25 - 24: After SCK delay prescaler
47 * 23 - 22: Delay after transfer prescaler
48 * 21 : Allow overwrite for bit 31-22 and bit 20-8
49 * 20 : Double baud rate
50 * 19 - 16: PCS to SCK delay scaler
51 * 15 - 12: After SCK delay scaler
52 * 11 - 8: Delay after transfer scaler
53 * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
54 */
55#define SPI_MODE_MOD 0x00200000
56#define SPI_MODE_DBLRATE 0x00100000
TsiChung Liewdec61c72009-06-30 14:09:47 +000057
Angelo Dureghello5ea37662019-03-13 21:46:47 +010058#define SPI_MODE_XFER_SZ_MASK 0xf0000000
59#define SPI_MODE_DLY_PRE_MASK 0x0fc00000
60#define SPI_MODE_DLY_SCA_MASK 0x000fff00
61
62#define MCF_FRM_SZ_16BIT DSPI_CTAR_TRSZ(0xf)
63#define MCF_DSPI_SPEED_BESTMATCH 0x7FFFFFFF
64#define MCF_DSPI_MAX_CTAR_REGS 8
65
66/* Default values */
67#define MCF_DSPI_DEFAULT_SCK_FREQ 10000000
68#define MCF_DSPI_DEFAULT_MAX_CS 4
69#define MCF_DSPI_DEFAULT_MODE 0
70
71#define MCF_DSPI_DEFAULT_CTAR (DSPI_CTAR_TRSZ(7) | \
72 DSPI_CTAR_PCSSCK_1CLK | \
73 DSPI_CTAR_PASC(0) | \
74 DSPI_CTAR_PDT(0) | \
75 DSPI_CTAR_CSSCK(0) | \
76 DSPI_CTAR_ASC(0) | \
77 DSPI_CTAR_DT(1) | \
78 DSPI_CTAR_BR(6))
79
80#define MCF_CTAR_MODE_MASK (MCF_FRM_SZ_16BIT | \
81 DSPI_CTAR_PCSSCK(3) | \
82 DSPI_CTAR_PASC_7CLK | \
83 DSPI_CTAR_PDT(3) | \
84 DSPI_CTAR_CSSCK(0x0f) | \
85 DSPI_CTAR_ASC(0x0f) | \
86 DSPI_CTAR_DT(0x0f))
87
88#define setup_ctrl(ctrl, cs) ((ctrl & 0xFF000000) | ((1 << cs) << 16))
89
90static inline void cfspi_tx(struct coldfire_spi_priv *cfspi,
91 u32 ctrl, u16 data)
Axel Linbb166272015-02-21 00:17:47 +080092{
Angelo Dureghello5ea37662019-03-13 21:46:47 +010093 /*
94 * Need to check fifo level here
95 */
96 while ((readl(&cfspi->regs->sr) & 0x0000F000) >= 0x4000)
97 ;
98
99 writel(ctrl | data, &cfspi->regs->tfr);
Axel Linbb166272015-02-21 00:17:47 +0800100}
101
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100102static inline u16 cfspi_rx(struct coldfire_spi_priv *cfspi)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000103{
TsiChung Liewdec61c72009-06-30 14:09:47 +0000104
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100105 while ((readl(&cfspi->regs->sr) & 0x000000F0) == 0)
106 ;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000107
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100108 return readw(&cfspi->regs->rfr);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000109}
110
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100111static int coldfire_spi_claim_bus(struct udevice *dev)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000112{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100113 struct udevice *bus = dev->parent;
114 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
115 struct dspi *dspi = cfspi->regs;
116 struct dm_spi_slave_platdata *slave_plat =
117 dev_get_parent_platdata(dev);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000118
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100119 if ((in_be32(&dspi->sr) & DSPI_SR_TXRXS) != DSPI_SR_TXRXS)
120 return -1;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000121
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100122 /* Clear FIFO and resume transfer */
123 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
124
125 dspi_chip_select(slave_plat->cs);
126
127 return 0;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000128}
129
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100130static int coldfire_spi_release_bus(struct udevice *dev)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000131{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100132 struct udevice *bus = dev->parent;
133 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
134 struct dspi *dspi = cfspi->regs;
135 struct dm_spi_slave_platdata *slave_plat =
136 dev_get_parent_platdata(dev);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000137
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100138 /* Clear FIFO */
139 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000140
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100141 dspi_chip_unselect(slave_plat->cs);
142
143 return 0;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000144}
145
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100146static int coldfire_spi_xfer(struct udevice *dev, unsigned int bitlen,
147 const void *dout, void *din,
148 unsigned long flags)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000149{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100150 struct udevice *bus = dev_get_parent(dev);
151 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
152 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000153 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
154 u8 *spi_rd = NULL, *spi_wr = NULL;
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100155 static u32 ctrl;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000156 uint len = bitlen >> 3;
157
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100158 if (cfspi->charbit == 16) {
TsiChung Liewdec61c72009-06-30 14:09:47 +0000159 bitlen >>= 1;
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100160 spi_wr16 = (u16 *)dout;
161 spi_rd16 = (u16 *)din;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000162 } else {
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100163 spi_wr = (u8 *)dout;
164 spi_rd = (u8 *)din;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000165 }
166
167 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
168 ctrl |= DSPI_TFR_CONT;
169
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100170 ctrl = setup_ctrl(ctrl, slave_plat->cs);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000171
172 if (len > 1) {
173 int tmp_len = len - 1;
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100174
TsiChung Liewdec61c72009-06-30 14:09:47 +0000175 while (tmp_len--) {
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100176 if (dout) {
177 if (cfspi->charbit == 16)
178 cfspi_tx(cfspi, ctrl, *spi_wr16++);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000179 else
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100180 cfspi_tx(cfspi, ctrl, *spi_wr++);
181 cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000182 }
183
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100184 if (din) {
185 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
186 if (cfspi->charbit == 16)
187 *spi_rd16++ = cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000188 else
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100189 *spi_rd++ = cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000190 }
191 }
192
193 len = 1; /* remaining byte */
194 }
195
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100196 if (flags & SPI_XFER_END)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000197 ctrl &= ~DSPI_TFR_CONT;
198
199 if (len) {
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100200 if (dout) {
201 if (cfspi->charbit == 16)
202 cfspi_tx(cfspi, ctrl, *spi_wr16);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000203 else
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100204 cfspi_tx(cfspi, ctrl, *spi_wr);
205 cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000206 }
207
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100208 if (din) {
209 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
210 if (cfspi->charbit == 16)
211 *spi_rd16 = cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000212 else
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100213 *spi_rd = cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000214 }
215 } else {
216 /* dummy read */
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100217 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
218 cfspi_rx(cfspi);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000219 }
220
221 return 0;
222}
223
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100224static int coldfire_spi_set_speed(struct udevice *bus, uint max_hz)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000225{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100226 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
227 struct dspi *dspi = cfspi->regs;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000228 int prescaler[] = { 2, 3, 5, 7 };
229 int scaler[] = {
230 2, 4, 6, 8,
231 16, 32, 64, 128,
232 256, 512, 1024, 2048,
233 4096, 8192, 16384, 32768
234 };
235 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100236 int best_i, best_j, bestmatch = MCF_DSPI_SPEED_BESTMATCH, baud_speed;
237 u32 bus_setup;
238
239 cfspi->baudrate = max_hz;
240
241 /* Read current setup */
242 bus_setup = readl(&dspi->ctar[bus->seq]);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000243
244 tmp = (prescaler[3] * scaler[15]);
245 /* Maximum and minimum baudrate it can handle */
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100246 if ((cfspi->baudrate > (gd->bus_clk >> 1)) ||
247 (cfspi->baudrate < (gd->bus_clk / tmp))) {
TsiChung Liewdec61c72009-06-30 14:09:47 +0000248 printf("Exceed baudrate limitation: Max %d - Min %d\n",
249 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100250 return -1;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000251 }
252
253 /* Activate Double Baud when it exceed 1/4 the bus clk */
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100254 if ((bus_setup & DSPI_CTAR_DBR) ||
255 (cfspi->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
TsiChung Liewdec61c72009-06-30 14:09:47 +0000256 bus_setup |= DSPI_CTAR_DBR;
257 dbr = 1;
258 }
259
TsiChung Liewdec61c72009-06-30 14:09:47 +0000260 /* Overwrite default value set in platform configuration file */
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100261 if (cfspi->mode & SPI_MODE_MOD) {
TsiChung Liewdec61c72009-06-30 14:09:47 +0000262 /*
263 * Check to see if it is enabled by default in platform
264 * config, or manual setting passed by mode parameter
265 */
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100266 if (cfspi->mode & SPI_MODE_DBLRATE) {
TsiChung Liewdec61c72009-06-30 14:09:47 +0000267 bus_setup |= DSPI_CTAR_DBR;
268 dbr = 1;
269 }
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100270 }
TsiChung Liewdec61c72009-06-30 14:09:47 +0000271
272 pbrcnt = sizeof(prescaler) / sizeof(int);
273 brcnt = sizeof(scaler) / sizeof(int);
274
275 /* baudrate calculation - to closer value, may not be exact match */
276 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
277 baud_speed = gd->bus_clk / prescaler[i];
278 for (j = 0; j < brcnt; j++) {
279 tmp = (baud_speed / scaler[j]) * (1 + dbr);
280
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100281 if (tmp > cfspi->baudrate)
282 diff = tmp - cfspi->baudrate;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000283 else
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100284 diff = cfspi->baudrate - tmp;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000285
286 if (diff < bestmatch) {
287 bestmatch = diff;
288 best_i = i;
289 best_j = j;
290 }
291 }
292 }
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100293
294 bus_setup &= ~(DSPI_CTAR_PBR(0x03) | DSPI_CTAR_BR(0x0f));
TsiChung Liewdec61c72009-06-30 14:09:47 +0000295 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100296 writel(bus_setup, &dspi->ctar[bus->seq]);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000297
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100298 return 0;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000299}
TsiChung Liewdec61c72009-06-30 14:09:47 +0000300
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100301static int coldfire_spi_set_mode(struct udevice *bus, uint mode)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000302{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100303 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
304 struct dspi *dspi = cfspi->regs;
305 u32 bus_setup = 0;
306
307 cfspi->mode = mode;
308
309 if (cfspi->mode & SPI_CPOL)
310 bus_setup |= DSPI_CTAR_CPOL;
311 if (cfspi->mode & SPI_CPHA)
312 bus_setup |= DSPI_CTAR_CPHA;
313 if (cfspi->mode & SPI_LSB_FIRST)
314 bus_setup |= DSPI_CTAR_LSBFE;
315
316 /* Overwrite default value set in platform configuration file */
317 if (cfspi->mode & SPI_MODE_MOD) {
318 if ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) == 0)
319 bus_setup |=
320 readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT;
321 else
322 bus_setup |=
323 ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) >> 1);
324
325 /* PSCSCK, PASC, PDT */
326 bus_setup |= (cfspi->mode & SPI_MODE_DLY_PRE_MASK) >> 4;
327 /* CSSCK, ASC, DT */
328 bus_setup |= (cfspi->mode & SPI_MODE_DLY_SCA_MASK) >> 4;
329 } else {
330 bus_setup |=
331 (readl(&dspi->ctar[bus->seq]) & MCF_CTAR_MODE_MASK);
332 }
333
334 cfspi->charbit =
335 ((readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT) ==
336 MCF_FRM_SZ_16BIT) ? 16 : 8;
337
338 setbits_be32(&dspi->ctar[bus->seq], bus_setup);
339
340 return 0;
341}
342
343static int coldfire_spi_probe(struct udevice *bus)
344{
345 struct coldfire_spi_platdata *plat = dev_get_platdata(bus);
346 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
347 struct dspi *dspi = cfspi->regs;
348 int i;
349
350 cfspi->regs = (struct dspi *)plat->regs_addr;
351
352 cfspi->baudrate = plat->speed_hz;
353 cfspi->mode = plat->mode;
354
355 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++) {
356 unsigned int ctar = 0;
357
358 if (plat->ctar[i][0] == 0)
359 break;
360
361 ctar = DSPI_CTAR_TRSZ(plat->ctar[i][0]) |
362 DSPI_CTAR_PCSSCK(plat->ctar[i][1]) |
363 DSPI_CTAR_PASC(plat->ctar[i][2]) |
364 DSPI_CTAR_PDT(plat->ctar[i][3]) |
365 DSPI_CTAR_CSSCK(plat->ctar[i][4]) |
366 DSPI_CTAR_ASC(plat->ctar[i][5]) |
367 DSPI_CTAR_DT(plat->ctar[i][6]) |
368 DSPI_CTAR_BR(plat->ctar[i][7]);
369
370 writel(ctar, &cfspi->regs->ctar[i]);
371 }
372
373 /* Default CTARs */
374 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++)
375 writel(MCF_DSPI_DEFAULT_CTAR, &dspi->ctar[i]);
376
377 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
378 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
379 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
380 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
381
382 return 0;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000383}
384
TsiChung Liewdec61c72009-06-30 14:09:47 +0000385void spi_init(void)
386{
TsiChung Liewdec61c72009-06-30 14:09:47 +0000387}
388
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100389#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
390static int coldfire_dspi_ofdata_to_platdata(struct udevice *bus)
TsiChung Liewdec61c72009-06-30 14:09:47 +0000391{
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100392 fdt_addr_t addr;
393 struct coldfire_spi_platdata *plat = bus->platdata;
394 const void *blob = gd->fdt_blob;
395 int node = dev_of_offset(bus);
396 int *ctar, len;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000397
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100398 addr = devfdt_get_addr(bus);
399 if (addr == FDT_ADDR_T_NONE)
400 return -ENOMEM;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000401
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100402 plat->regs_addr = addr;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000403
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100404 plat->num_cs = fdtdec_get_int(blob, node, "num-cs",
405 MCF_DSPI_DEFAULT_MAX_CS);
TsiChung Liewdec61c72009-06-30 14:09:47 +0000406
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100407 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
408 MCF_DSPI_DEFAULT_SCK_FREQ);
409
410 plat->mode = fdtdec_get_int(blob, node, "spi-mode",
411 MCF_DSPI_DEFAULT_MODE);
412
413 memset(plat->ctar, 0, sizeof(plat->ctar));
414
415 ctar = (int *)fdt_getprop(blob, node, "ctar-params", &len);
416
417 if (ctar && len) {
418 int i, q, ctar_regs;
419
420 ctar_regs = len / sizeof(unsigned int) / MAX_CTAR_FIELDS;
421
422 if (ctar_regs > MAX_CTAR_REGS)
423 ctar_regs = MAX_CTAR_REGS;
424
425 for (i = 0; i < ctar_regs; i++) {
426 for (q = 0; q < MAX_CTAR_FIELDS; q++)
427 plat->ctar[i][q] = *ctar++;
428 }
429 }
430
431 debug("DSPI: regs=%pa, max-frequency=%d, num-cs=%d, mode=%d\n",
432 (void *)plat->regs_addr,
433 plat->speed_hz, plat->num_cs, plat->mode);
434
435 return 0;
TsiChung Liewdec61c72009-06-30 14:09:47 +0000436}
437
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100438static const struct udevice_id coldfire_spi_ids[] = {
439 { .compatible = "fsl,mcf-dspi" },
440 { }
441};
442#endif
Axel Linbb166272015-02-21 00:17:47 +0800443
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100444static const struct dm_spi_ops coldfire_spi_ops = {
445 .claim_bus = coldfire_spi_claim_bus,
446 .release_bus = coldfire_spi_release_bus,
447 .xfer = coldfire_spi_xfer,
448 .set_speed = coldfire_spi_set_speed,
449 .set_mode = coldfire_spi_set_mode,
450};
TsiChung Liewdec61c72009-06-30 14:09:47 +0000451
Angelo Dureghello5ea37662019-03-13 21:46:47 +0100452U_BOOT_DRIVER(coldfire_spi) = {
453 .name = "spi_coldfire",
454 .id = UCLASS_SPI,
455#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
456 .of_match = coldfire_spi_ids,
457 .ofdata_to_platdata = coldfire_dspi_ofdata_to_platdata,
458 .platdata_auto_alloc_size = sizeof(struct coldfire_spi_platdata),
459#endif
460 .probe = coldfire_spi_probe,
461 .ops = &coldfire_spi_ops,
462 .priv_auto_alloc_size = sizeof(struct coldfire_spi_priv),
463};