blob: 01fcf6bef5a472d3e62c9a43fafb9e4fe7a7855f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +05302/*
3 * (C) Copyright 2009
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 *
7 * Derived from drivers/spi/mpc8xxx_spi.c
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +05308 */
9
10#include <common.h>
Stefan Roese27139a62015-11-20 13:39:43 +010011#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053013#include <malloc.h>
14#include <spi.h>
Lei Wen298ae912011-10-18 20:11:42 +053015#include <asm/io.h>
Stefan Roesec2437842014-10-22 12:13:06 +020016#include <asm/arch/soc.h>
Trevor Woernerbb7ab072020-05-06 08:02:40 -040017#ifdef CONFIG_ARCH_KIRKWOOD
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053018#include <asm/arch/mpp.h>
Stefan Roese2a88a532014-10-22 12:13:10 +020019#endif
Stefan Roese2da296f2014-10-22 12:13:07 +020020#include <asm/arch-mvebu/spi.h>
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053021
Stefan Roese27139a62015-11-20 13:39:43 +010022static void _spi_cs_activate(struct kwspi_registers *reg)
23{
24 setbits_le32(&reg->ctrl, KWSPI_CSN_ACT);
25}
26
27static void _spi_cs_deactivate(struct kwspi_registers *reg)
28{
29 clrbits_le32(&reg->ctrl, KWSPI_CSN_ACT);
30}
31
32static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen,
33 const void *dout, void *din, unsigned long flags)
34{
35 unsigned int tmpdout, tmpdin;
36 int tm, isread = 0;
37
38 debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen);
39
40 if (flags & SPI_XFER_BEGIN)
41 _spi_cs_activate(reg);
42
43 /*
44 * handle data in 8-bit chunks
45 * TBD: 2byte xfer mode to be enabled
46 */
47 clrsetbits_le32(&reg->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
48
49 while (bitlen > 4) {
50 debug("loopstart bitlen %d\n", bitlen);
51 tmpdout = 0;
52
53 /* Shift data so it's msb-justified */
54 if (dout)
55 tmpdout = *(u32 *)dout & 0xff;
56
57 clrbits_le32(&reg->irq_cause, KWSPI_SMEMRDIRQ);
58 writel(tmpdout, &reg->dout); /* Write the data out */
59 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
60 tmpdout, bitlen);
61
62 /*
63 * Wait for SPI transmit to get out
64 * or time out (1 second = 1000 ms)
65 * The NE event must be read and cleared first
66 */
67 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
68 if (readl(&reg->irq_cause) & KWSPI_SMEMRDIRQ) {
69 isread = 1;
70 tmpdin = readl(&reg->din);
71 debug("spi_xfer: din %p..%08x read\n",
72 din, tmpdin);
73
74 if (din) {
75 *((u8 *)din) = (u8)tmpdin;
76 din += 1;
77 }
78 if (dout)
79 dout += 1;
80 bitlen -= 8;
81 }
82 if (isread)
83 break;
84 }
85 if (tm >= KWSPI_TIMEOUT)
86 printf("*** spi_xfer: Time out during SPI transfer\n");
87
88 debug("loopend bitlen %d\n", bitlen);
89 }
90
91 if (flags & SPI_XFER_END)
92 _spi_cs_deactivate(reg);
93
94 return 0;
95}
96
97#ifndef CONFIG_DM_SPI
98
Stefan Roese1b258d52014-10-22 12:13:12 +020099static struct kwspi_registers *spireg =
100 (struct kwspi_registers *)MVEBU_SPI_BASE;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530101
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400102#ifdef CONFIG_ARCH_KIRKWOOD
Stefan Roese8edd6e02014-09-02 14:02:52 +0200103static u32 cs_spi_mpp_back[2];
Stefan Roese2a88a532014-10-22 12:13:10 +0200104#endif
Valentin Longchamp1ad0acd2012-06-01 01:31:01 +0000105
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530106struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
107 unsigned int max_hz, unsigned int mode)
108{
109 struct spi_slave *slave;
110 u32 data;
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400111#ifdef CONFIG_ARCH_KIRKWOOD
Albert ARIBAUD4d424312012-11-26 11:27:36 +0000112 static const u32 kwspi_mpp_config[2][2] = {
113 { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
114 { MPP7_SPI_SCn, 0 } /* if cs != 0 */
115 };
Stefan Roese2a88a532014-10-22 12:13:10 +0200116#endif
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530117
118 if (!spi_cs_is_valid(bus, cs))
119 return NULL;
120
Simon Glassd034a952013-03-18 19:23:40 +0000121 slave = spi_alloc_slave_base(bus, cs);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530122 if (!slave)
123 return NULL;
124
Stefan Roeseb85f0cb2014-09-02 14:02:51 +0200125 writel(KWSPI_SMEMRDY, &spireg->ctrl);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530126
127 /* calculate spi clock prescaller using max_hz */
Valentin Longchampbaddd9f2012-08-15 05:31:49 +0000128 data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
129 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
130 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530131
132 /* program spi clock prescaller using max_hz */
133 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
Stefan Roese57767df2014-09-02 14:02:53 +0200134 debug("data = 0x%08x\n", data);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530135
136 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
Ian Campbell573b62b2012-01-12 06:10:22 +0000137 writel(KWSPI_IRQMASK, &spireg->irq_mask);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530138
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400139#ifdef CONFIG_ARCH_KIRKWOOD
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530140 /* program mpp registers to select SPI_CSn */
Albert ARIBAUD4d424312012-11-26 11:27:36 +0000141 kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
Stefan Roese2a88a532014-10-22 12:13:10 +0200142#endif
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530143
144 return slave;
145}
146
147void spi_free_slave(struct spi_slave *slave)
148{
Trevor Woernerbb7ab072020-05-06 08:02:40 -0400149#ifdef CONFIG_ARCH_KIRKWOOD
Valentin Longchamp1ad0acd2012-06-01 01:31:01 +0000150 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
Stefan Roese2a88a532014-10-22 12:13:10 +0200151#endif
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530152 free(slave);
153}
154
Valentin Longchamp41f94c42012-06-01 01:31:03 +0000155__attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
156{
157 return 0;
158}
159
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530160int spi_claim_bus(struct spi_slave *slave)
161{
Valentin Longchamp41f94c42012-06-01 01:31:03 +0000162 return board_spi_claim_bus(slave);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530163}
164
Valentin Longchamp41f94c42012-06-01 01:31:03 +0000165__attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
166{
167}
168
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530169void spi_release_bus(struct spi_slave *slave)
170{
Valentin Longchamp41f94c42012-06-01 01:31:03 +0000171 board_spi_release_bus(slave);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530172}
173
174#ifndef CONFIG_SPI_CS_IS_VALID
175/*
176 * you can define this function board specific
177 * define above CONFIG in board specific config file and
178 * provide the function in board specific src file
179 */
180int spi_cs_is_valid(unsigned int bus, unsigned int cs)
181{
Stefan Roese57767df2014-09-02 14:02:53 +0200182 return bus == 0 && (cs == 0 || cs == 1);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530183}
184#endif
185
Michael Walle86ea45b2011-10-18 20:12:00 +0530186void spi_init(void)
187{
Stefan Roeseff74ca42015-11-20 08:44:21 +0100188}
189
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530190void spi_cs_activate(struct spi_slave *slave)
191{
Stefan Roeseff74ca42015-11-20 08:44:21 +0100192 _spi_cs_activate(spireg);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530193}
194
195void spi_cs_deactivate(struct spi_slave *slave)
196{
Stefan Roeseff74ca42015-11-20 08:44:21 +0100197 _spi_cs_deactivate(spireg);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530198}
199
Stefan Roese27139a62015-11-20 13:39:43 +0100200int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
201 const void *dout, void *din, unsigned long flags)
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530202{
Stefan Roese27139a62015-11-20 13:39:43 +0100203 return _spi_xfer(spireg, bitlen, dout, din, flags);
204}
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530205
Stefan Roese27139a62015-11-20 13:39:43 +0100206#else
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530207
Stefan Roese27139a62015-11-20 13:39:43 +0100208/* Here now the DM part */
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530209
Chris Packham479588d2018-01-22 22:44:20 +1300210struct mvebu_spi_dev {
211 bool is_errata_50mhz_ac;
212};
213
Stefan Roese27139a62015-11-20 13:39:43 +0100214struct mvebu_spi_platdata {
215 struct kwspi_registers *spireg;
Jagan Tekif45abaa2018-03-15 17:03:22 +0530216 bool is_errata_50mhz_ac;
Stefan Roese27139a62015-11-20 13:39:43 +0100217};
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530218
Stefan Roese27139a62015-11-20 13:39:43 +0100219struct mvebu_spi_priv {
220 struct kwspi_registers *spireg;
221};
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530222
Stefan Roese27139a62015-11-20 13:39:43 +0100223static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
224{
225 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
226 struct kwspi_registers *reg = plat->spireg;
227 u32 data;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530228
Stefan Roese27139a62015-11-20 13:39:43 +0100229 /* calculate spi clock prescaller using max_hz */
230 data = ((CONFIG_SYS_TCLK / 2) / hz) + 0x10;
231 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
232 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530233
Stefan Roese27139a62015-11-20 13:39:43 +0100234 /* program spi clock prescaler using max_hz */
235 writel(KWSPI_ADRLEN_3BYTE | data, &reg->cfg);
236 debug("data = 0x%08x\n", data);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530237
Stefan Roese27139a62015-11-20 13:39:43 +0100238 return 0;
239}
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530240
Chris Packham479588d2018-01-22 22:44:20 +1300241static void mvebu_spi_50mhz_ac_timing_erratum(struct udevice *bus, uint mode)
242{
243 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
244 struct kwspi_registers *reg = plat->spireg;
245 u32 data;
246
247 /*
248 * Erratum description: (Erratum NO. FE-9144572) The device
249 * SPI interface supports frequencies of up to 50 MHz.
250 * However, due to this erratum, when the device core clock is
251 * 250 MHz and the SPI interfaces is configured for 50MHz SPI
252 * clock and CPOL=CPHA=1 there might occur data corruption on
253 * reads from the SPI device.
254 * Erratum Workaround:
255 * Work in one of the following configurations:
256 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
257 * Register".
258 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
259 * Register" before setting the interface.
260 */
261 data = readl(&reg->timing1);
262 data &= ~KW_SPI_TMISO_SAMPLE_MASK;
263
264 if (CONFIG_SYS_TCLK == 250000000 &&
265 mode & SPI_CPOL &&
266 mode & SPI_CPHA)
267 data |= KW_SPI_TMISO_SAMPLE_2;
268 else
269 data |= KW_SPI_TMISO_SAMPLE_1;
270
271 writel(data, &reg->timing1);
272}
273
Stefan Roese27139a62015-11-20 13:39:43 +0100274static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
275{
Chris Packham21c3dca2016-10-27 21:16:05 +1300276 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
277 struct kwspi_registers *reg = plat->spireg;
278 u32 data = readl(&reg->cfg);
279
280 data &= ~(KWSPI_CPHA | KWSPI_CPOL | KWSPI_RXLSBF | KWSPI_TXLSBF);
281
282 if (mode & SPI_CPHA)
283 data |= KWSPI_CPHA;
284 if (mode & SPI_CPOL)
285 data |= KWSPI_CPOL;
286 if (mode & SPI_LSB_FIRST)
287 data |= (KWSPI_RXLSBF | KWSPI_TXLSBF);
288
289 writel(data, &reg->cfg);
290
Jagan Tekif45abaa2018-03-15 17:03:22 +0530291 if (plat->is_errata_50mhz_ac)
Chris Packham479588d2018-01-22 22:44:20 +1300292 mvebu_spi_50mhz_ac_timing_erratum(bus, mode);
293
Stefan Roese27139a62015-11-20 13:39:43 +0100294 return 0;
295}
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530296
Stefan Roese27139a62015-11-20 13:39:43 +0100297static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
298 const void *dout, void *din, unsigned long flags)
299{
300 struct udevice *bus = dev->parent;
301 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
302
303 return _spi_xfer(plat->spireg, bitlen, dout, din, flags);
304}
305
Pascal Linder96821ba2019-06-18 08:41:01 +0200306__attribute__((weak)) int mvebu_board_spi_claim_bus(struct udevice *dev)
307{
308 return 0;
309}
310
Stefan Roese90b499a2016-02-11 11:37:38 +0100311static int mvebu_spi_claim_bus(struct udevice *dev)
312{
313 struct udevice *bus = dev->parent;
314 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
315
316 /* Configure the chip-select in the CTRL register */
317 clrsetbits_le32(&plat->spireg->ctrl,
318 KWSPI_CS_MASK << KWSPI_CS_SHIFT,
319 spi_chip_select(dev) << KWSPI_CS_SHIFT);
320
Pascal Linder96821ba2019-06-18 08:41:01 +0200321 return mvebu_board_spi_claim_bus(dev);
322}
323
324__attribute__((weak)) int mvebu_board_spi_release_bus(struct udevice *dev)
325{
Stefan Roese90b499a2016-02-11 11:37:38 +0100326 return 0;
327}
328
Pascal Linder96821ba2019-06-18 08:41:01 +0200329static int mvebu_spi_release_bus(struct udevice *dev)
330{
331 return mvebu_board_spi_release_bus(dev);
332}
333
Stefan Roese27139a62015-11-20 13:39:43 +0100334static int mvebu_spi_probe(struct udevice *bus)
335{
336 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
337 struct kwspi_registers *reg = plat->spireg;
338
339 writel(KWSPI_SMEMRDY, &reg->ctrl);
340 writel(KWSPI_SMEMRDIRQ, &reg->irq_cause);
341 writel(KWSPI_IRQMASK, &reg->irq_mask);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530342
343 return 0;
344}
Stefan Roeseff74ca42015-11-20 08:44:21 +0100345
Stefan Roese27139a62015-11-20 13:39:43 +0100346static int mvebu_spi_ofdata_to_platdata(struct udevice *bus)
Stefan Roeseff74ca42015-11-20 08:44:21 +0100347{
Stefan Roese27139a62015-11-20 13:39:43 +0100348 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
Jagan Tekif45abaa2018-03-15 17:03:22 +0530349 const struct mvebu_spi_dev *drvdata =
350 (struct mvebu_spi_dev *)dev_get_driver_data(bus);
Stefan Roese27139a62015-11-20 13:39:43 +0100351
Simon Glassba1dea42017-05-17 17:18:05 -0600352 plat->spireg = (struct kwspi_registers *)devfdt_get_addr(bus);
Jagan Tekif45abaa2018-03-15 17:03:22 +0530353 plat->is_errata_50mhz_ac = drvdata->is_errata_50mhz_ac;
Stefan Roese27139a62015-11-20 13:39:43 +0100354
355 return 0;
Stefan Roeseff74ca42015-11-20 08:44:21 +0100356}
Stefan Roese27139a62015-11-20 13:39:43 +0100357
358static const struct dm_spi_ops mvebu_spi_ops = {
Stefan Roese90b499a2016-02-11 11:37:38 +0100359 .claim_bus = mvebu_spi_claim_bus,
Pascal Linder96821ba2019-06-18 08:41:01 +0200360 .release_bus = mvebu_spi_release_bus,
Stefan Roese27139a62015-11-20 13:39:43 +0100361 .xfer = mvebu_spi_xfer,
362 .set_speed = mvebu_spi_set_speed,
363 .set_mode = mvebu_spi_set_mode,
364 /*
365 * cs_info is not needed, since we require all chip selects to be
366 * in the device tree explicitly
367 */
368};
369
Chris Packham833ff182018-08-01 12:19:26 +0530370static const struct mvebu_spi_dev armada_spi_dev_data = {
371 .is_errata_50mhz_ac = false,
372};
373
Chris Packham479588d2018-01-22 22:44:20 +1300374static const struct mvebu_spi_dev armada_xp_spi_dev_data = {
375 .is_errata_50mhz_ac = false,
376};
377
378static const struct mvebu_spi_dev armada_375_spi_dev_data = {
379 .is_errata_50mhz_ac = false,
380};
381
382static const struct mvebu_spi_dev armada_380_spi_dev_data = {
383 .is_errata_50mhz_ac = true,
384};
385
Stefan Roese27139a62015-11-20 13:39:43 +0100386static const struct udevice_id mvebu_spi_ids[] = {
Chris Packham479588d2018-01-22 22:44:20 +1300387 {
Chris Packham833ff182018-08-01 12:19:26 +0530388 .compatible = "marvell,orion-spi",
389 .data = (ulong)&armada_spi_dev_data,
390 },
391 {
Chris Packham479588d2018-01-22 22:44:20 +1300392 .compatible = "marvell,armada-375-spi",
393 .data = (ulong)&armada_375_spi_dev_data
394 },
395 {
396 .compatible = "marvell,armada-380-spi",
397 .data = (ulong)&armada_380_spi_dev_data
398 },
399 {
400 .compatible = "marvell,armada-xp-spi",
401 .data = (ulong)&armada_xp_spi_dev_data
402 },
Stefan Roese27139a62015-11-20 13:39:43 +0100403 { }
404};
405
406U_BOOT_DRIVER(mvebu_spi) = {
407 .name = "mvebu_spi",
408 .id = UCLASS_SPI,
409 .of_match = mvebu_spi_ids,
410 .ops = &mvebu_spi_ops,
411 .ofdata_to_platdata = mvebu_spi_ofdata_to_platdata,
412 .platdata_auto_alloc_size = sizeof(struct mvebu_spi_platdata),
413 .priv_auto_alloc_size = sizeof(struct mvebu_spi_priv),
414 .probe = mvebu_spi_probe,
415};
416#endif