blob: b341235d29479919aa1d130fd49ec5ce7ed56ae4 [file] [log] [blame]
Shadi Ammouri60cadec2008-08-05 13:01:09 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Marvell Orion SPI controller driver
Shadi Ammouri60cadec2008-08-05 13:01:09 -07003 *
4 * Author: Shadi Ammouri <shadi@marvell.com>
5 * Copyright (C) 2007-2008 Marvell Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Shadi Ammouri60cadec2008-08-05 13:01:09 -070012#include <linux/interrupt.h>
13#include <linux/delay.h>
14#include <linux/platform_device.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/spi/spi.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040018#include <linux/module.h>
Russell King5c678692014-06-21 12:22:37 +010019#include <linux/pm_runtime.h>
Andrew Lunnf814f9a2012-07-23 12:08:09 +020020#include <linux/of.h>
Stefan Roeseb3c195b2016-05-19 09:07:05 +020021#include <linux/of_address.h>
Greg Ungererdf59fa72014-09-28 23:24:04 +100022#include <linux/of_device.h>
Andrew Lunn4574b882012-04-06 17:17:26 +020023#include <linux/clk.h>
Mark Brown895248f2013-07-29 05:10:21 +010024#include <linux/sizes.h>
Chris Packhamb28b9142017-05-23 16:03:21 +120025#include <linux/gpio.h>
Shadi Ammouri60cadec2008-08-05 13:01:09 -070026#include <asm/unaligned.h>
27
28#define DRIVER_NAME "orion_spi"
29
Russell King5c678692014-06-21 12:22:37 +010030/* Runtime PM autosuspend timeout: PM is fairly light on this driver */
31#define SPI_AUTOSUSPEND_TIMEOUT 200
32
Ken Wilson23244402015-01-16 13:10:47 +100033/* Some SoCs using this driver support up to 8 chip selects.
34 * It is up to the implementer to only use the chip selects
35 * that are available.
36 */
37#define ORION_NUM_CHIPSELECTS 8
38
Shadi Ammouri60cadec2008-08-05 13:01:09 -070039#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
40
41#define ORION_SPI_IF_CTRL_REG 0x00
42#define ORION_SPI_IF_CONFIG_REG 0x04
Bastian Stender1017f422017-04-07 15:52:33 +020043#define ORION_SPI_IF_RXLSBF BIT(14)
44#define ORION_SPI_IF_TXLSBF BIT(13)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070045#define ORION_SPI_DATA_OUT_REG 0x08
46#define ORION_SPI_DATA_IN_REG 0x0c
47#define ORION_SPI_INT_CAUSE_REG 0x10
Nadav Haklai38d62112015-08-11 11:58:47 +020048#define ORION_SPI_TIMING_PARAMS_REG 0x18
49
Stefan Roeseb3c195b2016-05-19 09:07:05 +020050/* Register for the "Direct Mode" */
51#define SPI_DIRECT_WRITE_CONFIG_REG 0x20
52
Nadav Haklai38d62112015-08-11 11:58:47 +020053#define ORION_SPI_TMISO_SAMPLE_MASK (0x3 << 6)
54#define ORION_SPI_TMISO_SAMPLE_1 (1 << 6)
55#define ORION_SPI_TMISO_SAMPLE_2 (2 << 6)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070056
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070057#define ORION_SPI_MODE_CPOL (1 << 11)
58#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070059#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
60#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Greg Ungererdf59fa72014-09-28 23:24:04 +100061#define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070062#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
63 ORION_SPI_MODE_CPHA)
Ken Wilson23244402015-01-16 13:10:47 +100064#define ORION_SPI_CS_MASK 0x1C
65#define ORION_SPI_CS_SHIFT 2
66#define ORION_SPI_CS(cs) ((cs << ORION_SPI_CS_SHIFT) & \
67 ORION_SPI_CS_MASK)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070068
Greg Ungererdf59fa72014-09-28 23:24:04 +100069enum orion_spi_type {
70 ORION_SPI,
71 ARMADA_SPI,
72};
73
74struct orion_spi_dev {
75 enum orion_spi_type typ;
Gregory CLEMENTce2f6ea2015-05-26 11:44:42 +020076 /*
77 * min_divisor and max_hz should be exclusive, the only we can
78 * have both is for managing the armada-370-spi case with old
79 * device tree
80 */
81 unsigned long max_hz;
Greg Ungererdf59fa72014-09-28 23:24:04 +100082 unsigned int min_divisor;
83 unsigned int max_divisor;
84 u32 prescale_mask;
Nadav Haklai38d62112015-08-11 11:58:47 +020085 bool is_errata_50mhz_ac;
Greg Ungererdf59fa72014-09-28 23:24:04 +100086};
87
Stefan Roeseb3c195b2016-05-19 09:07:05 +020088struct orion_direct_acc {
89 void __iomem *vaddr;
90 u32 size;
91};
92
Shadi Ammouri60cadec2008-08-05 13:01:09 -070093struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070094 struct spi_master *master;
95 void __iomem *base;
Andrew Lunn4574b882012-04-06 17:17:26 +020096 struct clk *clk;
Gregory CLEMENT92ae1122018-01-12 11:42:33 +010097 struct clk *axi_clk;
Greg Ungererdf59fa72014-09-28 23:24:04 +100098 const struct orion_spi_dev *devdata;
Jan Kundrát544248622018-01-26 23:56:10 +010099 int unused_hw_gpio;
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200100
101 struct orion_direct_acc direct_access[ORION_NUM_CHIPSELECTS];
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700102};
103
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700104static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
105{
106 return orion_spi->base + reg;
107}
108
109static inline void
110orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
111{
112 void __iomem *reg_addr = spi_reg(orion_spi, reg);
113 u32 val;
114
115 val = readl(reg_addr);
116 val |= mask;
117 writel(val, reg_addr);
118}
119
120static inline void
121orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
122{
123 void __iomem *reg_addr = spi_reg(orion_spi, reg);
124 u32 val;
125
126 val = readl(reg_addr);
127 val &= ~mask;
128 writel(val, reg_addr);
129}
130
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700131static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
132{
133 u32 tclk_hz;
134 u32 rate;
135 u32 prescale;
136 u32 reg;
137 struct orion_spi *orion_spi;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000138 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700139
140 orion_spi = spi_master_get_devdata(spi->master);
Greg Ungererdf59fa72014-09-28 23:24:04 +1000141 devdata = orion_spi->devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700142
Andrew Lunn4574b882012-04-06 17:17:26 +0200143 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700144
Greg Ungererdf59fa72014-09-28 23:24:04 +1000145 if (devdata->typ == ARMADA_SPI) {
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100146 /*
147 * Given the core_clk (tclk_hz) and the target rate (speed) we
148 * determine the best values for SPR (in [0 .. 15]) and SPPR (in
149 * [0..7]) such that
150 *
151 * core_clk / (SPR * 2 ** SPPR)
152 *
153 * is as big as possible but not bigger than speed.
154 */
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700155
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100156 /* best integer divider: */
157 unsigned divider = DIV_ROUND_UP(tclk_hz, speed);
158 unsigned spr, sppr;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700159
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100160 if (divider < 16) {
161 /* This is the easy case, divider is less than 16 */
162 spr = divider;
163 sppr = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700164
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100165 } else {
166 unsigned two_pow_sppr;
167 /*
168 * Find the highest bit set in divider. This and the
169 * three next bits define SPR (apart from rounding).
170 * SPPR is then the number of zero bits that must be
171 * appended:
172 */
173 sppr = fls(divider) - 4;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000174
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100175 /*
176 * As SPR only has 4 bits, we have to round divider up
177 * to the next multiple of 2 ** sppr.
178 */
179 two_pow_sppr = 1 << sppr;
180 divider = (divider + two_pow_sppr - 1) & -two_pow_sppr;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000181
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100182 /*
183 * recalculate sppr as rounding up divider might have
184 * increased it enough to change the position of the
185 * highest set bit. In this case the bit that now
186 * doesn't make it into SPR is 0, so there is no need to
187 * round again.
188 */
189 sppr = fls(divider) - 4;
190 spr = divider >> sppr;
191
192 /*
193 * Now do range checking. SPR is constructed to have a
194 * width of 4 bits, so this is fine for sure. So we
195 * still need to check for sppr to fit into 3 bits:
196 */
197 if (sppr > 7)
198 return -EINVAL;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000199 }
200
Uwe Kleine-König7243e0b2016-12-08 17:37:08 +0100201 prescale = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000202 } else {
203 /*
204 * the supported rates are: 4,6,8...30
205 * round up as we look for equal or less speed
206 */
207 rate = DIV_ROUND_UP(tclk_hz, speed);
208 rate = roundup(rate, 2);
209
210 /* check if requested speed is too small */
211 if (rate > 30)
212 return -EINVAL;
213
214 if (rate < 4)
215 rate = 4;
216
217 /* Convert the rate to SPI clock divisor value. */
218 prescale = 0x10 + rate/2;
219 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700220
221 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
Greg Ungererdf59fa72014-09-28 23:24:04 +1000222 reg = ((reg & ~devdata->prescale_mask) | prescale);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700223 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
224
225 return 0;
226}
227
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700228static void
229orion_spi_mode_set(struct spi_device *spi)
230{
231 u32 reg;
232 struct orion_spi *orion_spi;
233
234 orion_spi = spi_master_get_devdata(spi->master);
235
236 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
237 reg &= ~ORION_SPI_MODE_MASK;
238 if (spi->mode & SPI_CPOL)
239 reg |= ORION_SPI_MODE_CPOL;
240 if (spi->mode & SPI_CPHA)
241 reg |= ORION_SPI_MODE_CPHA;
Bastian Stender1017f422017-04-07 15:52:33 +0200242 if (spi->mode & SPI_LSB_FIRST)
243 reg |= ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF;
244 else
245 reg &= ~(ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF);
246
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700247 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
248}
249
Nadav Haklai38d62112015-08-11 11:58:47 +0200250static void
251orion_spi_50mhz_ac_timing_erratum(struct spi_device *spi, unsigned int speed)
252{
253 u32 reg;
254 struct orion_spi *orion_spi;
255
256 orion_spi = spi_master_get_devdata(spi->master);
257
258 /*
259 * Erratum description: (Erratum NO. FE-9144572) The device
260 * SPI interface supports frequencies of up to 50 MHz.
261 * However, due to this erratum, when the device core clock is
262 * 250 MHz and the SPI interfaces is configured for 50MHz SPI
263 * clock and CPOL=CPHA=1 there might occur data corruption on
264 * reads from the SPI device.
265 * Erratum Workaround:
266 * Work in one of the following configurations:
267 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
268 * Register".
269 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
270 * Register" before setting the interface.
271 */
272 reg = readl(spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
273 reg &= ~ORION_SPI_TMISO_SAMPLE_MASK;
274
275 if (clk_get_rate(orion_spi->clk) == 250000000 &&
276 speed == 50000000 && spi->mode & SPI_CPOL &&
277 spi->mode & SPI_CPHA)
278 reg |= ORION_SPI_TMISO_SAMPLE_2;
279 else
280 reg |= ORION_SPI_TMISO_SAMPLE_1; /* This is the default value */
281
282 writel(reg, spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
283}
284
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700285/*
286 * called only when no transfer is active on the bus
287 */
288static int
289orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
290{
291 struct orion_spi *orion_spi;
292 unsigned int speed = spi->max_speed_hz;
293 unsigned int bits_per_word = spi->bits_per_word;
294 int rc;
295
296 orion_spi = spi_master_get_devdata(spi->master);
297
298 if ((t != NULL) && t->speed_hz)
299 speed = t->speed_hz;
300
301 if ((t != NULL) && t->bits_per_word)
302 bits_per_word = t->bits_per_word;
303
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700304 orion_spi_mode_set(spi);
305
Nadav Haklai38d62112015-08-11 11:58:47 +0200306 if (orion_spi->devdata->is_errata_50mhz_ac)
307 orion_spi_50mhz_ac_timing_erratum(spi, speed);
308
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700309 rc = orion_spi_baudrate_set(spi, speed);
310 if (rc)
311 return rc;
312
Axel Lin495b3352014-02-11 20:51:36 +0800313 if (bits_per_word == 16)
314 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
315 ORION_SPI_IF_8_16_BIT_MODE);
316 else
317 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
318 ORION_SPI_IF_8_16_BIT_MODE);
319
320 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700321}
322
Ken Wilson75872eb2015-01-12 13:13:59 +1000323static void orion_spi_set_cs(struct spi_device *spi, bool enable)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700324{
Ken Wilson75872eb2015-01-12 13:13:59 +1000325 struct orion_spi *orion_spi;
Chris Packhamb28b9142017-05-23 16:03:21 +1200326 int cs;
327
Jan Kundrát544248622018-01-26 23:56:10 +0100328 orion_spi = spi_master_get_devdata(spi->master);
329
Chris Packhamb28b9142017-05-23 16:03:21 +1200330 if (gpio_is_valid(spi->cs_gpio))
Jan Kundrát544248622018-01-26 23:56:10 +0100331 cs = orion_spi->unused_hw_gpio;
Chris Packhamb28b9142017-05-23 16:03:21 +1200332 else
333 cs = spi->chip_select;
Ken Wilson75872eb2015-01-12 13:13:59 +1000334
Ken Wilson23244402015-01-16 13:10:47 +1000335 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, ORION_SPI_CS_MASK);
336 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG,
Chris Packhamb28b9142017-05-23 16:03:21 +1200337 ORION_SPI_CS(cs));
Ken Wilson23244402015-01-16 13:10:47 +1000338
Ken Wilson75872eb2015-01-12 13:13:59 +1000339 /* Chip select logic is inverted from spi_set_cs */
340 if (!enable)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700341 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
342 else
343 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
344}
345
346static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
347{
348 int i;
349
350 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
351 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
352 return 1;
Jingoo Hanb8434042014-09-02 11:51:39 +0900353
354 udelay(1);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700355 }
356
357 return -1;
358}
359
360static inline int
361orion_spi_write_read_8bit(struct spi_device *spi,
362 const u8 **tx_buf, u8 **rx_buf)
363{
364 void __iomem *tx_reg, *rx_reg, *int_reg;
365 struct orion_spi *orion_spi;
366
367 orion_spi = spi_master_get_devdata(spi->master);
368 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
369 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
370 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
371
372 /* clear the interrupt cause register */
373 writel(0x0, int_reg);
374
375 if (tx_buf && *tx_buf)
376 writel(*(*tx_buf)++, tx_reg);
377 else
378 writel(0, tx_reg);
379
380 if (orion_spi_wait_till_ready(orion_spi) < 0) {
381 dev_err(&spi->dev, "TXS timed out\n");
382 return -1;
383 }
384
385 if (rx_buf && *rx_buf)
386 *(*rx_buf)++ = readl(rx_reg);
387
388 return 1;
389}
390
391static inline int
392orion_spi_write_read_16bit(struct spi_device *spi,
393 const u16 **tx_buf, u16 **rx_buf)
394{
395 void __iomem *tx_reg, *rx_reg, *int_reg;
396 struct orion_spi *orion_spi;
397
398 orion_spi = spi_master_get_devdata(spi->master);
399 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
400 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
401 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
402
403 /* clear the interrupt cause register */
404 writel(0x0, int_reg);
405
406 if (tx_buf && *tx_buf)
407 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
408 else
409 writel(0, tx_reg);
410
411 if (orion_spi_wait_till_ready(orion_spi) < 0) {
412 dev_err(&spi->dev, "TXS timed out\n");
413 return -1;
414 }
415
416 if (rx_buf && *rx_buf)
417 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
418
419 return 1;
420}
421
422static unsigned int
423orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
424{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700425 unsigned int count;
426 int word_len;
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200427 struct orion_spi *orion_spi;
428 int cs = spi->chip_select;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700429
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700430 word_len = spi->bits_per_word;
431 count = xfer->len;
432
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200433 orion_spi = spi_master_get_devdata(spi->master);
434
435 /*
436 * Use SPI direct write mode if base address is available. Otherwise
437 * fall back to PIO mode for this transfer.
438 */
439 if ((orion_spi->direct_access[cs].vaddr) && (xfer->tx_buf) &&
440 (word_len == 8)) {
441 unsigned int cnt = count / 4;
442 unsigned int rem = count % 4;
443
444 /*
445 * Send the TX-data to the SPI device via the direct
446 * mapped address window
447 */
448 iowrite32_rep(orion_spi->direct_access[cs].vaddr,
449 xfer->tx_buf, cnt);
450 if (rem) {
451 u32 *buf = (u32 *)xfer->tx_buf;
452
453 iowrite8_rep(orion_spi->direct_access[cs].vaddr,
454 &buf[cnt], rem);
455 }
456
457 return count;
458 }
459
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700460 if (word_len == 8) {
461 const u8 *tx = xfer->tx_buf;
462 u8 *rx = xfer->rx_buf;
463
464 do {
465 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
466 goto out;
467 count--;
468 } while (count);
469 } else if (word_len == 16) {
470 const u16 *tx = xfer->tx_buf;
471 u16 *rx = xfer->rx_buf;
472
473 do {
474 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
475 goto out;
476 count -= 2;
477 } while (count);
478 }
479
480out:
481 return xfer->len - count;
482}
483
Ken Wilson75872eb2015-01-12 13:13:59 +1000484static int orion_spi_transfer_one(struct spi_master *master,
485 struct spi_device *spi,
486 struct spi_transfer *t)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700487{
Andrew Lunnba59a802012-07-23 13:16:55 +0200488 int status = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700489
Ken Wilson75872eb2015-01-12 13:13:59 +1000490 status = orion_spi_setup_transfer(spi, t);
Andrew Lunnba59a802012-07-23 13:16:55 +0200491 if (status < 0)
Ken Wilson75872eb2015-01-12 13:13:59 +1000492 return status;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700493
Ken Wilson75872eb2015-01-12 13:13:59 +1000494 if (t->len)
495 orion_spi_write_read(spi, t);
Andrew Lunnba59a802012-07-23 13:16:55 +0200496
Ken Wilson75872eb2015-01-12 13:13:59 +1000497 return status;
498}
Andrew Lunnba59a802012-07-23 13:16:55 +0200499
Ken Wilson75872eb2015-01-12 13:13:59 +1000500static int orion_spi_setup(struct spi_device *spi)
501{
Jan Kundrát544248622018-01-26 23:56:10 +0100502 if (gpio_is_valid(spi->cs_gpio)) {
503 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
504 }
Ken Wilson75872eb2015-01-12 13:13:59 +1000505 return orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700506}
507
Grant Likely2deff8d2013-02-05 13:27:35 +0000508static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700509{
510 /* Verify that the CS is deasserted */
Ken Wilson75872eb2015-01-12 13:13:59 +1000511 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200512
513 /* Don't deassert CS between the direct mapped SPI transfers */
514 writel(0, spi_reg(orion_spi, SPI_DIRECT_WRITE_CONFIG_REG));
515
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700516 return 0;
517}
518
Greg Ungererdf59fa72014-09-28 23:24:04 +1000519static const struct orion_spi_dev orion_spi_dev_data = {
520 .typ = ORION_SPI,
521 .min_divisor = 4,
522 .max_divisor = 30,
523 .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
524};
525
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200526static const struct orion_spi_dev armada_370_spi_dev_data = {
Greg Ungererdf59fa72014-09-28 23:24:04 +1000527 .typ = ARMADA_SPI,
Gregory CLEMENTce2f6ea2015-05-26 11:44:42 +0200528 .min_divisor = 4,
Greg Ungererdf59fa72014-09-28 23:24:04 +1000529 .max_divisor = 1920,
Gregory CLEMENTce2f6ea2015-05-26 11:44:42 +0200530 .max_hz = 50000000,
Greg Ungererdf59fa72014-09-28 23:24:04 +1000531 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
532};
533
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200534static const struct orion_spi_dev armada_xp_spi_dev_data = {
535 .typ = ARMADA_SPI,
536 .max_hz = 50000000,
537 .max_divisor = 1920,
538 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
539};
540
541static const struct orion_spi_dev armada_375_spi_dev_data = {
542 .typ = ARMADA_SPI,
543 .min_divisor = 15,
544 .max_divisor = 1920,
545 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
546};
547
Nadav Haklai38d62112015-08-11 11:58:47 +0200548static const struct orion_spi_dev armada_380_spi_dev_data = {
549 .typ = ARMADA_SPI,
550 .max_hz = 50000000,
551 .max_divisor = 1920,
552 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
553 .is_errata_50mhz_ac = true,
554};
555
Greg Ungererdf59fa72014-09-28 23:24:04 +1000556static const struct of_device_id orion_spi_of_match_table[] = {
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200557 {
558 .compatible = "marvell,orion-spi",
559 .data = &orion_spi_dev_data,
560 },
561 {
562 .compatible = "marvell,armada-370-spi",
563 .data = &armada_370_spi_dev_data,
564 },
565 {
566 .compatible = "marvell,armada-375-spi",
567 .data = &armada_375_spi_dev_data,
568 },
569 {
570 .compatible = "marvell,armada-380-spi",
Nadav Haklai38d62112015-08-11 11:58:47 +0200571 .data = &armada_380_spi_dev_data,
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200572 },
573 {
574 .compatible = "marvell,armada-390-spi",
575 .data = &armada_xp_spi_dev_data,
576 },
577 {
578 .compatible = "marvell,armada-xp-spi",
579 .data = &armada_xp_spi_dev_data,
580 },
581
Greg Ungererdf59fa72014-09-28 23:24:04 +1000582 {}
583};
584MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
585
Grant Likely2deff8d2013-02-05 13:27:35 +0000586static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700587{
Greg Ungererdf59fa72014-09-28 23:24:04 +1000588 const struct of_device_id *of_id;
589 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700590 struct spi_master *master;
591 struct orion_spi *spi;
592 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200593 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700594 int status = 0;
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200595 struct device_node *np;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700596
Jingoo Han3fed8062013-10-14 10:35:08 +0900597 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700598 if (master == NULL) {
599 dev_dbg(&pdev->dev, "master allocation failed\n");
600 return -ENOMEM;
601 }
602
603 if (pdev->id != -1)
604 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200605 if (pdev->dev.of_node) {
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200606 u32 cell_index;
Jingoo Hanb8434042014-09-02 11:51:39 +0900607
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200608 if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
609 &cell_index))
610 master->bus_num = cell_index;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200611 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700612
Bastian Stender1017f422017-04-07 15:52:33 +0200613 /* we support all 4 SPI modes and LSB first option */
614 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
Ken Wilson75872eb2015-01-12 13:13:59 +1000615 master->set_cs = orion_spi_set_cs;
616 master->transfer_one = orion_spi_transfer_one;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700617 master->num_chipselect = ORION_NUM_CHIPSELECTS;
Ken Wilson75872eb2015-01-12 13:13:59 +1000618 master->setup = orion_spi_setup;
Axel Lin495b3352014-02-11 20:51:36 +0800619 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Russell King5c678692014-06-21 12:22:37 +0100620 master->auto_runtime_pm = true;
Chris Packhamb28b9142017-05-23 16:03:21 +1200621 master->flags = SPI_MASTER_GPIO_SS;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700622
Jingoo Han24b5a822013-05-23 19:20:40 +0900623 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700624
625 spi = spi_master_get_devdata(master);
626 spi->master = master;
Jan Kundrát544248622018-01-26 23:56:10 +0100627 spi->unused_hw_gpio = -1;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700628
Greg Ungererdf59fa72014-09-28 23:24:04 +1000629 of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
Greg Ungerer9a2d3632014-10-21 15:57:48 +1000630 devdata = (of_id) ? of_id->data : &orion_spi_dev_data;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000631 spi->devdata = devdata;
632
Jingoo Hanbb489842013-12-09 19:21:22 +0900633 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200634 if (IS_ERR(spi->clk)) {
635 status = PTR_ERR(spi->clk);
636 goto out;
637 }
638
Russell Kingc85012a2014-06-21 11:32:23 +0100639 status = clk_prepare_enable(spi->clk);
640 if (status)
641 goto out;
642
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100643 /* The following clock is only used by some SoCs */
644 spi->axi_clk = devm_clk_get(&pdev->dev, "axi");
645 if (IS_ERR(spi->axi_clk) &&
Christophe Jaillet479c03a2018-01-25 21:16:17 +0100646 PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) {
647 status = -EPROBE_DEFER;
648 goto out_rel_clk;
649 }
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100650 if (!IS_ERR(spi->axi_clk))
651 clk_prepare_enable(spi->axi_clk);
652
Andrew Lunn4574b882012-04-06 17:17:26 +0200653 tclk_hz = clk_get_rate(spi->clk);
Gregory CLEMENTce2f6ea2015-05-26 11:44:42 +0200654
655 /*
656 * With old device tree, armada-370-spi could be used with
657 * Armada XP, however for this SoC the maximum frequency is
658 * 50MHz instead of tclk/4. On Armada 370, tclk cannot be
659 * higher than 200MHz. So, in order to be able to handle both
660 * SoCs, we can take the minimum of 50MHz and tclk/4.
661 */
662 if (of_device_is_compatible(pdev->dev.of_node,
663 "marvell,armada-370-spi"))
664 master->max_speed_hz = min(devdata->max_hz,
665 DIV_ROUND_UP(tclk_hz, devdata->min_divisor));
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200666 else if (devdata->min_divisor)
Gregory CLEMENTce2f6ea2015-05-26 11:44:42 +0200667 master->max_speed_hz =
668 DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
Gregory CLEMENT4dacccf2015-05-26 11:44:43 +0200669 else
670 master->max_speed_hz = devdata->max_hz;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000671 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700672
673 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100674 spi->base = devm_ioremap_resource(&pdev->dev, r);
675 if (IS_ERR(spi->base)) {
676 status = PTR_ERR(spi->base);
Christophe Jaillet479c03a2018-01-25 21:16:17 +0100677 goto out_rel_axi_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700678 }
679
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200680 /* Scan all SPI devices of this controller for direct mapped devices */
681 for_each_available_child_of_node(pdev->dev.of_node, np) {
682 u32 cs;
683
684 /* Get chip-select number from the "reg" property */
685 status = of_property_read_u32(np, "reg", &cs);
686 if (status) {
687 dev_err(&pdev->dev,
Rob Herring25c56c82017-07-18 16:43:31 -0500688 "%pOF has no valid 'reg' property (%d)\n",
689 np, status);
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200690 continue;
691 }
692
693 /*
694 * Check if an address is configured for this SPI device. If
695 * not, the MBus mapping via the 'ranges' property in the 'soc'
696 * node is not configured and this device should not use the
697 * direct mode. In this case, just continue with the next
698 * device.
699 */
700 status = of_address_to_resource(pdev->dev.of_node, cs + 1, r);
701 if (status)
702 continue;
703
704 /*
705 * Only map one page for direct access. This is enough for the
706 * simple TX transfer which only writes to the first word.
707 * This needs to get extended for the direct SPI-NOR / SPI-NAND
708 * support, once this gets implemented.
709 */
710 spi->direct_access[cs].vaddr = devm_ioremap(&pdev->dev,
711 r->start,
712 PAGE_SIZE);
Wei Yongjun57c624a2016-06-13 14:32:23 +0000713 if (!spi->direct_access[cs].vaddr) {
714 status = -ENOMEM;
Christophe Jaillet479c03a2018-01-25 21:16:17 +0100715 goto out_rel_axi_clk;
Stefan Roeseb3c195b2016-05-19 09:07:05 +0200716 }
717 spi->direct_access[cs].size = PAGE_SIZE;
718
719 dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
720 }
721
Russell King5c678692014-06-21 12:22:37 +0100722 pm_runtime_set_active(&pdev->dev);
723 pm_runtime_use_autosuspend(&pdev->dev);
724 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
725 pm_runtime_enable(&pdev->dev);
726
Wei Yongjun14033812014-07-20 22:03:14 +0800727 status = orion_spi_reset(spi);
728 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100729 goto out_rel_pm;
730
731 pm_runtime_mark_last_busy(&pdev->dev);
732 pm_runtime_put_autosuspend(&pdev->dev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700733
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200734 master->dev.of_node = pdev->dev.of_node;
Russell King5c678692014-06-21 12:22:37 +0100735 status = spi_register_master(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700736 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100737 goto out_rel_pm;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700738
Jan Kundrát544248622018-01-26 23:56:10 +0100739 if (master->cs_gpios) {
740 int i;
741 for (i = 0; i < master->num_chipselect; ++i) {
742 char *gpio_name;
743
744 if (!gpio_is_valid(master->cs_gpios[i])) {
745 continue;
746 }
747
748 gpio_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
749 "%s-CS%d", dev_name(&pdev->dev), i);
750 if (!gpio_name) {
751 status = -ENOMEM;
752 goto out_rel_master;
753 }
754
755 status = devm_gpio_request(&pdev->dev,
756 master->cs_gpios[i], gpio_name);
757 if (status) {
758 dev_err(&pdev->dev,
759 "Can't request GPIO for CS %d\n",
760 master->cs_gpios[i]);
761 goto out_rel_master;
762 }
763 if (spi->unused_hw_gpio == -1) {
764 dev_info(&pdev->dev,
765 "Selected unused HW CS#%d "
766 "for any GPIO CSes\n", i);
767 spi->unused_hw_gpio = i;
768 }
769 }
770 }
771
772
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700773 return status;
774
Jan Kundrát544248622018-01-26 23:56:10 +0100775out_rel_master:
776 spi_unregister_master(master);
Russell King5c678692014-06-21 12:22:37 +0100777out_rel_pm:
778 pm_runtime_disable(&pdev->dev);
Christophe Jaillet479c03a2018-01-25 21:16:17 +0100779out_rel_axi_clk:
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100780 clk_disable_unprepare(spi->axi_clk);
Christophe Jaillet479c03a2018-01-25 21:16:17 +0100781out_rel_clk:
Andrew Lunn4574b882012-04-06 17:17:26 +0200782 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700783out:
784 spi_master_put(master);
785 return status;
786}
787
788
Grant Likely2deff8d2013-02-05 13:27:35 +0000789static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700790{
Russell King5c678692014-06-21 12:22:37 +0100791 struct spi_master *master = platform_get_drvdata(pdev);
792 struct orion_spi *spi = spi_master_get_devdata(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700793
Russell King5c678692014-06-21 12:22:37 +0100794 pm_runtime_get_sync(&pdev->dev);
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100795 clk_disable_unprepare(spi->axi_clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200796 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200797
Russell King5c678692014-06-21 12:22:37 +0100798 spi_unregister_master(master);
799 pm_runtime_disable(&pdev->dev);
800
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700801 return 0;
802}
803
804MODULE_ALIAS("platform:" DRIVER_NAME);
805
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100806#ifdef CONFIG_PM
Russell King5c678692014-06-21 12:22:37 +0100807static int orion_spi_runtime_suspend(struct device *dev)
808{
809 struct spi_master *master = dev_get_drvdata(dev);
810 struct orion_spi *spi = spi_master_get_devdata(master);
811
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100812 clk_disable_unprepare(spi->axi_clk);
Russell King5c678692014-06-21 12:22:37 +0100813 clk_disable_unprepare(spi->clk);
814 return 0;
815}
816
817static int orion_spi_runtime_resume(struct device *dev)
818{
819 struct spi_master *master = dev_get_drvdata(dev);
820 struct orion_spi *spi = spi_master_get_devdata(master);
821
Gregory CLEMENT92ae1122018-01-12 11:42:33 +0100822 if (!IS_ERR(spi->axi_clk))
823 clk_prepare_enable(spi->axi_clk);
Russell King5c678692014-06-21 12:22:37 +0100824 return clk_prepare_enable(spi->clk);
825}
826#endif
827
828static const struct dev_pm_ops orion_spi_pm_ops = {
829 SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
830 orion_spi_runtime_resume,
831 NULL)
832};
833
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700834static struct platform_driver orion_spi_driver = {
835 .driver = {
836 .name = DRIVER_NAME,
Russell King5c678692014-06-21 12:22:37 +0100837 .pm = &orion_spi_pm_ops,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200838 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700839 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300840 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000841 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700842};
843
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300844module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700845
846MODULE_DESCRIPTION("Orion SPI driver");
847MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
848MODULE_LICENSE("GPL");