blob: 8c1a62ee2801a065e00f1806740fd4053225d2c1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewf6b243a2009-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 Dureghelloaa86be72019-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 Liewf6b243a2009-06-30 14:09:47 +000014 */
15
16#include <common.h>
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010017#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010019#include <dm/platform_data/spi_coldfire.h>
TsiChung Liewf6b243a2009-06-30 14:09:47 +000020#include <spi.h>
21#include <malloc.h>
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010022#include <asm/coldfire/dspi.h>
23#include <asm/io.h>
TsiChung Liewf6b243a2009-06-30 14:09:47 +000024
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010025struct coldfire_spi_priv {
26 struct dspi *regs;
TsiChung Liewf6b243a2009-06-30 14:09:47 +000027 uint baudrate;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010028 int mode;
TsiChung Liewf6b243a2009-06-30 14:09:47 +000029 int charbit;
30};
31
TsiChung Liewf6b243a2009-06-30 14:09:47 +000032DECLARE_GLOBAL_DATA_PTR;
33
Wolfgang Wegnerfaba5012010-04-23 05:43:12 +000034#ifndef CONFIG_SPI_IDLE_VAL
35#if defined(CONFIG_SPI_MMC)
36#define CONFIG_SPI_IDLE_VAL 0xFFFF
37#else
38#define CONFIG_SPI_IDLE_VAL 0x0
39#endif
40#endif
41
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010042/*
43 * DSPI specific mode
44 *
45 * bit 31 - 28: Transfer size 3 to 16 bits
46 * 27 - 26: PCS to SCK delay prescaler
47 * 25 - 24: After SCK delay prescaler
48 * 23 - 22: Delay after transfer prescaler
49 * 21 : Allow overwrite for bit 31-22 and bit 20-8
50 * 20 : Double baud rate
51 * 19 - 16: PCS to SCK delay scaler
52 * 15 - 12: After SCK delay scaler
53 * 11 - 8: Delay after transfer scaler
54 * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
55 */
56#define SPI_MODE_MOD 0x00200000
57#define SPI_MODE_DBLRATE 0x00100000
58
59#define SPI_MODE_XFER_SZ_MASK 0xf0000000
60#define SPI_MODE_DLY_PRE_MASK 0x0fc00000
61#define SPI_MODE_DLY_SCA_MASK 0x000fff00
62
63#define MCF_FRM_SZ_16BIT DSPI_CTAR_TRSZ(0xf)
64#define MCF_DSPI_SPEED_BESTMATCH 0x7FFFFFFF
65#define MCF_DSPI_MAX_CTAR_REGS 8
TsiChung Liewf6b243a2009-06-30 14:09:47 +000066
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010067/* Default values */
68#define MCF_DSPI_DEFAULT_SCK_FREQ 10000000
69#define MCF_DSPI_DEFAULT_MAX_CS 4
70#define MCF_DSPI_DEFAULT_MODE 0
71
72#define MCF_DSPI_DEFAULT_CTAR (DSPI_CTAR_TRSZ(7) | \
73 DSPI_CTAR_PCSSCK_1CLK | \
74 DSPI_CTAR_PASC(0) | \
75 DSPI_CTAR_PDT(0) | \
76 DSPI_CTAR_CSSCK(0) | \
77 DSPI_CTAR_ASC(0) | \
78 DSPI_CTAR_DT(1) | \
79 DSPI_CTAR_BR(6))
80
81#define MCF_CTAR_MODE_MASK (MCF_FRM_SZ_16BIT | \
82 DSPI_CTAR_PCSSCK(3) | \
83 DSPI_CTAR_PASC_7CLK | \
84 DSPI_CTAR_PDT(3) | \
85 DSPI_CTAR_CSSCK(0x0f) | \
86 DSPI_CTAR_ASC(0x0f) | \
87 DSPI_CTAR_DT(0x0f))
88
89#define setup_ctrl(ctrl, cs) ((ctrl & 0xFF000000) | ((1 << cs) << 16))
90
91static inline void cfspi_tx(struct coldfire_spi_priv *cfspi,
92 u32 ctrl, u16 data)
Axel Lin13e17092015-02-21 00:17:47 +080093{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +010094 /*
95 * Need to check fifo level here
96 */
97 while ((readl(&cfspi->regs->sr) & 0x0000F000) >= 0x4000)
98 ;
99
100 writel(ctrl | data, &cfspi->regs->tfr);
Axel Lin13e17092015-02-21 00:17:47 +0800101}
102
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100103static inline u16 cfspi_rx(struct coldfire_spi_priv *cfspi)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000104{
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000105
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100106 while ((readl(&cfspi->regs->sr) & 0x000000F0) == 0)
107 ;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000108
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100109 return readw(&cfspi->regs->rfr);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000110}
111
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100112static int coldfire_spi_claim_bus(struct udevice *dev)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000113{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100114 struct udevice *bus = dev->parent;
115 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
116 struct dspi *dspi = cfspi->regs;
117 struct dm_spi_slave_platdata *slave_plat =
Simon Glass71fa5b42020-12-03 16:55:18 -0700118 dev_get_parent_plat(dev);
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100119
120 if ((in_be32(&dspi->sr) & DSPI_SR_TXRXS) != DSPI_SR_TXRXS)
121 return -1;
122
123 /* Clear FIFO and resume transfer */
124 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000125
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100126 dspi_chip_select(slave_plat->cs);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000127
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100128 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000129}
130
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100131static int coldfire_spi_release_bus(struct udevice *dev)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000132{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100133 struct udevice *bus = dev->parent;
134 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
135 struct dspi *dspi = cfspi->regs;
136 struct dm_spi_slave_platdata *slave_plat =
Simon Glass71fa5b42020-12-03 16:55:18 -0700137 dev_get_parent_plat(dev);
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100138
139 /* Clear FIFO */
140 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000141
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100142 dspi_chip_unselect(slave_plat->cs);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000143
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100144 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000145}
146
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100147static int coldfire_spi_xfer(struct udevice *dev, unsigned int bitlen,
148 const void *dout, void *din,
149 unsigned long flags)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000150{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100151 struct udevice *bus = dev_get_parent(dev);
152 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
Simon Glass71fa5b42020-12-03 16:55:18 -0700153 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000154 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
155 u8 *spi_rd = NULL, *spi_wr = NULL;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100156 static u32 ctrl;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000157 uint len = bitlen >> 3;
158
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100159 if (cfspi->charbit == 16) {
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000160 bitlen >>= 1;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100161 spi_wr16 = (u16 *)dout;
162 spi_rd16 = (u16 *)din;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000163 } else {
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100164 spi_wr = (u8 *)dout;
165 spi_rd = (u8 *)din;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000166 }
167
168 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
169 ctrl |= DSPI_TFR_CONT;
170
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100171 ctrl = setup_ctrl(ctrl, slave_plat->cs);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000172
173 if (len > 1) {
174 int tmp_len = len - 1;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100175
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000176 while (tmp_len--) {
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100177 if (dout) {
178 if (cfspi->charbit == 16)
179 cfspi_tx(cfspi, ctrl, *spi_wr16++);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000180 else
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100181 cfspi_tx(cfspi, ctrl, *spi_wr++);
182 cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000183 }
184
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100185 if (din) {
186 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
187 if (cfspi->charbit == 16)
188 *spi_rd16++ = cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000189 else
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100190 *spi_rd++ = cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000191 }
192 }
193
194 len = 1; /* remaining byte */
195 }
196
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100197 if (flags & SPI_XFER_END)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000198 ctrl &= ~DSPI_TFR_CONT;
199
200 if (len) {
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100201 if (dout) {
202 if (cfspi->charbit == 16)
203 cfspi_tx(cfspi, ctrl, *spi_wr16);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000204 else
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100205 cfspi_tx(cfspi, ctrl, *spi_wr);
206 cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000207 }
208
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100209 if (din) {
210 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
211 if (cfspi->charbit == 16)
212 *spi_rd16 = cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000213 else
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100214 *spi_rd = cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000215 }
216 } else {
217 /* dummy read */
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100218 cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL);
219 cfspi_rx(cfspi);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000220 }
221
222 return 0;
223}
224
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100225static int coldfire_spi_set_speed(struct udevice *bus, uint max_hz)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000226{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100227 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
228 struct dspi *dspi = cfspi->regs;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000229 int prescaler[] = { 2, 3, 5, 7 };
230 int scaler[] = {
231 2, 4, 6, 8,
232 16, 32, 64, 128,
233 256, 512, 1024, 2048,
234 4096, 8192, 16384, 32768
235 };
236 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100237 int best_i, best_j, bestmatch = MCF_DSPI_SPEED_BESTMATCH, baud_speed;
238 u32 bus_setup;
239
240 cfspi->baudrate = max_hz;
241
242 /* Read current setup */
243 bus_setup = readl(&dspi->ctar[bus->seq]);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000244
245 tmp = (prescaler[3] * scaler[15]);
246 /* Maximum and minimum baudrate it can handle */
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100247 if ((cfspi->baudrate > (gd->bus_clk >> 1)) ||
248 (cfspi->baudrate < (gd->bus_clk / tmp))) {
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000249 printf("Exceed baudrate limitation: Max %d - Min %d\n",
250 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100251 return -1;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000252 }
253
254 /* Activate Double Baud when it exceed 1/4 the bus clk */
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100255 if ((bus_setup & DSPI_CTAR_DBR) ||
256 (cfspi->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000257 bus_setup |= DSPI_CTAR_DBR;
258 dbr = 1;
259 }
260
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000261 /* Overwrite default value set in platform configuration file */
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100262 if (cfspi->mode & SPI_MODE_MOD) {
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000263 /*
264 * Check to see if it is enabled by default in platform
265 * config, or manual setting passed by mode parameter
266 */
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100267 if (cfspi->mode & SPI_MODE_DBLRATE) {
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000268 bus_setup |= DSPI_CTAR_DBR;
269 dbr = 1;
270 }
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100271 }
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000272
273 pbrcnt = sizeof(prescaler) / sizeof(int);
274 brcnt = sizeof(scaler) / sizeof(int);
275
276 /* baudrate calculation - to closer value, may not be exact match */
277 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
278 baud_speed = gd->bus_clk / prescaler[i];
279 for (j = 0; j < brcnt; j++) {
280 tmp = (baud_speed / scaler[j]) * (1 + dbr);
281
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100282 if (tmp > cfspi->baudrate)
283 diff = tmp - cfspi->baudrate;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000284 else
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100285 diff = cfspi->baudrate - tmp;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000286
287 if (diff < bestmatch) {
288 bestmatch = diff;
289 best_i = i;
290 best_j = j;
291 }
292 }
293 }
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100294
295 bus_setup &= ~(DSPI_CTAR_PBR(0x03) | DSPI_CTAR_BR(0x0f));
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000296 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100297 writel(bus_setup, &dspi->ctar[bus->seq]);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000298
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100299 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000300}
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000301
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100302static int coldfire_spi_set_mode(struct udevice *bus, uint mode)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000303{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100304 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
305 struct dspi *dspi = cfspi->regs;
306 u32 bus_setup = 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000307
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100308 cfspi->mode = mode;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000309
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100310 if (cfspi->mode & SPI_CPOL)
311 bus_setup |= DSPI_CTAR_CPOL;
312 if (cfspi->mode & SPI_CPHA)
313 bus_setup |= DSPI_CTAR_CPHA;
314 if (cfspi->mode & SPI_LSB_FIRST)
315 bus_setup |= DSPI_CTAR_LSBFE;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000316
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100317 /* Overwrite default value set in platform configuration file */
318 if (cfspi->mode & SPI_MODE_MOD) {
319 if ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) == 0)
320 bus_setup |=
321 readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT;
322 else
323 bus_setup |=
324 ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) >> 1);
325
326 /* PSCSCK, PASC, PDT */
327 bus_setup |= (cfspi->mode & SPI_MODE_DLY_PRE_MASK) >> 4;
328 /* CSSCK, ASC, DT */
329 bus_setup |= (cfspi->mode & SPI_MODE_DLY_SCA_MASK) >> 4;
330 } else {
331 bus_setup |=
332 (readl(&dspi->ctar[bus->seq]) & MCF_CTAR_MODE_MASK);
333 }
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000334
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100335 cfspi->charbit =
336 ((readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT) ==
337 MCF_FRM_SZ_16BIT) ? 16 : 8;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000338
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100339 setbits_be32(&dspi->ctar[bus->seq], bus_setup);
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000340
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100341 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000342}
343
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100344static int coldfire_spi_probe(struct udevice *bus)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000345{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100346 struct coldfire_spi_platdata *plat = dev_get_platdata(bus);
347 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
348 struct dspi *dspi = cfspi->regs;
349 int i;
Axel Lin13e17092015-02-21 00:17:47 +0800350
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100351 cfspi->regs = (struct dspi *)plat->regs_addr;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000352
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100353 cfspi->baudrate = plat->speed_hz;
354 cfspi->mode = plat->mode;
355
356 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++) {
357 unsigned int ctar = 0;
358
359 if (plat->ctar[i][0] == 0)
360 break;
361
362 ctar = DSPI_CTAR_TRSZ(plat->ctar[i][0]) |
363 DSPI_CTAR_PCSSCK(plat->ctar[i][1]) |
364 DSPI_CTAR_PASC(plat->ctar[i][2]) |
365 DSPI_CTAR_PDT(plat->ctar[i][3]) |
366 DSPI_CTAR_CSSCK(plat->ctar[i][4]) |
367 DSPI_CTAR_ASC(plat->ctar[i][5]) |
368 DSPI_CTAR_DT(plat->ctar[i][6]) |
369 DSPI_CTAR_BR(plat->ctar[i][7]);
370
371 writel(ctar, &cfspi->regs->ctar[i]);
372 }
373
374 /* Default CTARs */
375 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++)
376 writel(MCF_DSPI_DEFAULT_CTAR, &dspi->ctar[i]);
377
378 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
379 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
380 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
381 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
382
383 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000384}
385
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100386#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
387static int coldfire_dspi_ofdata_to_platdata(struct udevice *bus)
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000388{
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100389 fdt_addr_t addr;
Simon Glass71fa5b42020-12-03 16:55:18 -0700390 struct coldfire_spi_platdata *plat = bus->plat;
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100391 const void *blob = gd->fdt_blob;
392 int node = dev_of_offset(bus);
393 int *ctar, len;
394
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900395 addr = dev_read_addr(bus);
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100396 if (addr == FDT_ADDR_T_NONE)
397 return -ENOMEM;
398
399 plat->regs_addr = addr;
400
401 plat->num_cs = fdtdec_get_int(blob, node, "num-cs",
402 MCF_DSPI_DEFAULT_MAX_CS);
403
404 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
405 MCF_DSPI_DEFAULT_SCK_FREQ);
406
407 plat->mode = fdtdec_get_int(blob, node, "spi-mode",
408 MCF_DSPI_DEFAULT_MODE);
409
410 memset(plat->ctar, 0, sizeof(plat->ctar));
411
412 ctar = (int *)fdt_getprop(blob, node, "ctar-params", &len);
413
414 if (ctar && len) {
415 int i, q, ctar_regs;
416
417 ctar_regs = len / sizeof(unsigned int) / MAX_CTAR_FIELDS;
418
419 if (ctar_regs > MAX_CTAR_REGS)
420 ctar_regs = MAX_CTAR_REGS;
421
422 for (i = 0; i < ctar_regs; i++) {
423 for (q = 0; q < MAX_CTAR_FIELDS; q++)
424 plat->ctar[i][q] = *ctar++;
425 }
426 }
427
428 debug("DSPI: regs=%pa, max-frequency=%d, num-cs=%d, mode=%d\n",
429 (void *)plat->regs_addr,
430 plat->speed_hz, plat->num_cs, plat->mode);
431
432 return 0;
TsiChung Liewf6b243a2009-06-30 14:09:47 +0000433}
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100434
435static const struct udevice_id coldfire_spi_ids[] = {
436 { .compatible = "fsl,mcf-dspi" },
437 { }
438};
439#endif
440
441static const struct dm_spi_ops coldfire_spi_ops = {
442 .claim_bus = coldfire_spi_claim_bus,
443 .release_bus = coldfire_spi_release_bus,
444 .xfer = coldfire_spi_xfer,
445 .set_speed = coldfire_spi_set_speed,
446 .set_mode = coldfire_spi_set_mode,
447};
448
449U_BOOT_DRIVER(coldfire_spi) = {
450 .name = "spi_coldfire",
451 .id = UCLASS_SPI,
452#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
453 .of_match = coldfire_spi_ids,
454 .ofdata_to_platdata = coldfire_dspi_ofdata_to_platdata,
Simon Glass71fa5b42020-12-03 16:55:18 -0700455 .plat_auto = sizeof(struct coldfire_spi_platdata),
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100456#endif
457 .probe = coldfire_spi_probe,
458 .ops = &coldfire_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700459 .priv_auto = sizeof(struct coldfire_spi_priv),
Angelo Dureghelloaa86be72019-03-13 21:46:47 +0100460};