blob: c4675fa8b64552afac670212b3dc8b002c430f7b [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>
Andrew Lunn4574b882012-04-06 17:17:26 +020021#include <linux/clk.h>
Mark Brown895248f2013-07-29 05:10:21 +010022#include <linux/sizes.h>
Shadi Ammouri60cadec2008-08-05 13:01:09 -070023#include <asm/unaligned.h>
24
25#define DRIVER_NAME "orion_spi"
26
Russell King5c678692014-06-21 12:22:37 +010027/* Runtime PM autosuspend timeout: PM is fairly light on this driver */
28#define SPI_AUTOSUSPEND_TIMEOUT 200
29
Shadi Ammouri60cadec2008-08-05 13:01:09 -070030#define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
31#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
32
33#define ORION_SPI_IF_CTRL_REG 0x00
34#define ORION_SPI_IF_CONFIG_REG 0x04
35#define ORION_SPI_DATA_OUT_REG 0x08
36#define ORION_SPI_DATA_IN_REG 0x0c
37#define ORION_SPI_INT_CAUSE_REG 0x10
38
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070039#define ORION_SPI_MODE_CPOL (1 << 11)
40#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070041#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
42#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070043#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
44 ORION_SPI_MODE_CPHA)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070045
46struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070047 struct spi_master *master;
48 void __iomem *base;
Andrew Lunn4574b882012-04-06 17:17:26 +020049 struct clk *clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070050};
51
Shadi Ammouri60cadec2008-08-05 13:01:09 -070052static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
53{
54 return orion_spi->base + reg;
55}
56
57static inline void
58orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
59{
60 void __iomem *reg_addr = spi_reg(orion_spi, reg);
61 u32 val;
62
63 val = readl(reg_addr);
64 val |= mask;
65 writel(val, reg_addr);
66}
67
68static inline void
69orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
70{
71 void __iomem *reg_addr = spi_reg(orion_spi, reg);
72 u32 val;
73
74 val = readl(reg_addr);
75 val &= ~mask;
76 writel(val, reg_addr);
77}
78
Shadi Ammouri60cadec2008-08-05 13:01:09 -070079static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
80{
81 u32 tclk_hz;
82 u32 rate;
83 u32 prescale;
84 u32 reg;
85 struct orion_spi *orion_spi;
86
87 orion_spi = spi_master_get_devdata(spi->master);
88
Andrew Lunn4574b882012-04-06 17:17:26 +020089 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -070090
91 /*
92 * the supported rates are: 4,6,8...30
93 * round up as we look for equal or less speed
94 */
95 rate = DIV_ROUND_UP(tclk_hz, speed);
96 rate = roundup(rate, 2);
97
98 /* check if requested speed is too small */
99 if (rate > 30)
100 return -EINVAL;
101
102 if (rate < 4)
103 rate = 4;
104
105 /* Convert the rate to SPI clock divisor value. */
106 prescale = 0x10 + rate/2;
107
108 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
109 reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
110 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
111
112 return 0;
113}
114
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700115static void
116orion_spi_mode_set(struct spi_device *spi)
117{
118 u32 reg;
119 struct orion_spi *orion_spi;
120
121 orion_spi = spi_master_get_devdata(spi->master);
122
123 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
124 reg &= ~ORION_SPI_MODE_MASK;
125 if (spi->mode & SPI_CPOL)
126 reg |= ORION_SPI_MODE_CPOL;
127 if (spi->mode & SPI_CPHA)
128 reg |= ORION_SPI_MODE_CPHA;
129 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
130}
131
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700132/*
133 * called only when no transfer is active on the bus
134 */
135static int
136orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
137{
138 struct orion_spi *orion_spi;
139 unsigned int speed = spi->max_speed_hz;
140 unsigned int bits_per_word = spi->bits_per_word;
141 int rc;
142
143 orion_spi = spi_master_get_devdata(spi->master);
144
145 if ((t != NULL) && t->speed_hz)
146 speed = t->speed_hz;
147
148 if ((t != NULL) && t->bits_per_word)
149 bits_per_word = t->bits_per_word;
150
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700151 orion_spi_mode_set(spi);
152
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700153 rc = orion_spi_baudrate_set(spi, speed);
154 if (rc)
155 return rc;
156
Axel Lin495b3352014-02-11 20:51:36 +0800157 if (bits_per_word == 16)
158 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
159 ORION_SPI_IF_8_16_BIT_MODE);
160 else
161 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
162 ORION_SPI_IF_8_16_BIT_MODE);
163
164 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700165}
166
167static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
168{
169 if (enable)
170 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
171 else
172 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
173}
174
175static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
176{
177 int i;
178
179 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
180 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
181 return 1;
182 else
183 udelay(1);
184 }
185
186 return -1;
187}
188
189static inline int
190orion_spi_write_read_8bit(struct spi_device *spi,
191 const u8 **tx_buf, u8 **rx_buf)
192{
193 void __iomem *tx_reg, *rx_reg, *int_reg;
194 struct orion_spi *orion_spi;
195
196 orion_spi = spi_master_get_devdata(spi->master);
197 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
198 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
199 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
200
201 /* clear the interrupt cause register */
202 writel(0x0, int_reg);
203
204 if (tx_buf && *tx_buf)
205 writel(*(*tx_buf)++, tx_reg);
206 else
207 writel(0, tx_reg);
208
209 if (orion_spi_wait_till_ready(orion_spi) < 0) {
210 dev_err(&spi->dev, "TXS timed out\n");
211 return -1;
212 }
213
214 if (rx_buf && *rx_buf)
215 *(*rx_buf)++ = readl(rx_reg);
216
217 return 1;
218}
219
220static inline int
221orion_spi_write_read_16bit(struct spi_device *spi,
222 const u16 **tx_buf, u16 **rx_buf)
223{
224 void __iomem *tx_reg, *rx_reg, *int_reg;
225 struct orion_spi *orion_spi;
226
227 orion_spi = spi_master_get_devdata(spi->master);
228 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
229 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
230 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
231
232 /* clear the interrupt cause register */
233 writel(0x0, int_reg);
234
235 if (tx_buf && *tx_buf)
236 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
237 else
238 writel(0, tx_reg);
239
240 if (orion_spi_wait_till_ready(orion_spi) < 0) {
241 dev_err(&spi->dev, "TXS timed out\n");
242 return -1;
243 }
244
245 if (rx_buf && *rx_buf)
246 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
247
248 return 1;
249}
250
251static unsigned int
252orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
253{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700254 unsigned int count;
255 int word_len;
256
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700257 word_len = spi->bits_per_word;
258 count = xfer->len;
259
260 if (word_len == 8) {
261 const u8 *tx = xfer->tx_buf;
262 u8 *rx = xfer->rx_buf;
263
264 do {
265 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
266 goto out;
267 count--;
268 } while (count);
269 } else if (word_len == 16) {
270 const u16 *tx = xfer->tx_buf;
271 u16 *rx = xfer->rx_buf;
272
273 do {
274 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
275 goto out;
276 count -= 2;
277 } while (count);
278 }
279
280out:
281 return xfer->len - count;
282}
283
Andrew Lunnba59a802012-07-23 13:16:55 +0200284static int orion_spi_transfer_one_message(struct spi_master *master,
285 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700286{
Andrew Lunnba59a802012-07-23 13:16:55 +0200287 struct orion_spi *orion_spi = spi_master_get_devdata(master);
288 struct spi_device *spi = m->spi;
289 struct spi_transfer *t = NULL;
290 int par_override = 0;
291 int status = 0;
292 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700293
Andrew Lunnba59a802012-07-23 13:16:55 +0200294 /* Load defaults */
295 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700296
Andrew Lunnba59a802012-07-23 13:16:55 +0200297 if (status < 0)
298 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700299
Andrew Lunnba59a802012-07-23 13:16:55 +0200300 list_for_each_entry(t, &m->transfers, transfer_list) {
Andrew Lunnba59a802012-07-23 13:16:55 +0200301 if (par_override || t->speed_hz || t->bits_per_word) {
302 par_override = 1;
303 status = orion_spi_setup_transfer(spi, t);
304 if (status < 0)
305 break;
306 if (!t->speed_hz && !t->bits_per_word)
307 par_override = 0;
308 }
309
310 if (!cs_active) {
311 orion_spi_set_cs(orion_spi, 1);
312 cs_active = 1;
313 }
314
315 if (t->len)
316 m->actual_length += orion_spi_write_read(spi, t);
317
318 if (t->delay_usecs)
319 udelay(t->delay_usecs);
320
321 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700322 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200323 cs_active = 0;
324 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700325 }
326
Andrew Lunnba59a802012-07-23 13:16:55 +0200327msg_done:
328 if (cs_active)
329 orion_spi_set_cs(orion_spi, 0);
330
331 m->status = status;
332 spi_finalize_current_message(master);
333
334 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700335}
336
Grant Likely2deff8d2013-02-05 13:27:35 +0000337static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700338{
339 /* Verify that the CS is deasserted */
340 orion_spi_set_cs(orion_spi, 0);
341
342 return 0;
343}
344
Grant Likely2deff8d2013-02-05 13:27:35 +0000345static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700346{
347 struct spi_master *master;
348 struct orion_spi *spi;
349 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200350 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700351 int status = 0;
352
Jingoo Han3fed8062013-10-14 10:35:08 +0900353 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700354 if (master == NULL) {
355 dev_dbg(&pdev->dev, "master allocation failed\n");
356 return -ENOMEM;
357 }
358
359 if (pdev->id != -1)
360 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200361 if (pdev->dev.of_node) {
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200362 u32 cell_index;
363 if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
364 &cell_index))
365 master->bus_num = cell_index;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200366 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700367
David Brownelle7db06b2009-06-17 16:26:04 -0700368 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700369 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700370
Andrew Lunnba59a802012-07-23 13:16:55 +0200371 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700372 master->num_chipselect = ORION_NUM_CHIPSELECTS;
Axel Lin495b3352014-02-11 20:51:36 +0800373 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Russell King5c678692014-06-21 12:22:37 +0100374 master->auto_runtime_pm = true;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700375
Jingoo Han24b5a822013-05-23 19:20:40 +0900376 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700377
378 spi = spi_master_get_devdata(master);
379 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700380
Jingoo Hanbb489842013-12-09 19:21:22 +0900381 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200382 if (IS_ERR(spi->clk)) {
383 status = PTR_ERR(spi->clk);
384 goto out;
385 }
386
Russell Kingc85012a2014-06-21 11:32:23 +0100387 status = clk_prepare_enable(spi->clk);
388 if (status)
389 goto out;
390
Andrew Lunn4574b882012-04-06 17:17:26 +0200391 tclk_hz = clk_get_rate(spi->clk);
Axel Linb52a37f2014-02-04 11:05:50 +0800392 master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4);
393 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700394
395 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100396 spi->base = devm_ioremap_resource(&pdev->dev, r);
397 if (IS_ERR(spi->base)) {
398 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200399 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700400 }
401
Russell King5c678692014-06-21 12:22:37 +0100402 pm_runtime_set_active(&pdev->dev);
403 pm_runtime_use_autosuspend(&pdev->dev);
404 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
405 pm_runtime_enable(&pdev->dev);
406
Wei Yongjun14033812014-07-20 22:03:14 +0800407 status = orion_spi_reset(spi);
408 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100409 goto out_rel_pm;
410
411 pm_runtime_mark_last_busy(&pdev->dev);
412 pm_runtime_put_autosuspend(&pdev->dev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700413
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200414 master->dev.of_node = pdev->dev.of_node;
Russell King5c678692014-06-21 12:22:37 +0100415 status = spi_register_master(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700416 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100417 goto out_rel_pm;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700418
419 return status;
420
Russell King5c678692014-06-21 12:22:37 +0100421out_rel_pm:
422 pm_runtime_disable(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200423out_rel_clk:
424 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700425out:
426 spi_master_put(master);
427 return status;
428}
429
430
Grant Likely2deff8d2013-02-05 13:27:35 +0000431static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700432{
Russell King5c678692014-06-21 12:22:37 +0100433 struct spi_master *master = platform_get_drvdata(pdev);
434 struct orion_spi *spi = spi_master_get_devdata(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700435
Russell King5c678692014-06-21 12:22:37 +0100436 pm_runtime_get_sync(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200437 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200438
Russell King5c678692014-06-21 12:22:37 +0100439 spi_unregister_master(master);
440 pm_runtime_disable(&pdev->dev);
441
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700442 return 0;
443}
444
445MODULE_ALIAS("platform:" DRIVER_NAME);
446
Russell King5c678692014-06-21 12:22:37 +0100447#ifdef CONFIG_PM_RUNTIME
448static int orion_spi_runtime_suspend(struct device *dev)
449{
450 struct spi_master *master = dev_get_drvdata(dev);
451 struct orion_spi *spi = spi_master_get_devdata(master);
452
453 clk_disable_unprepare(spi->clk);
454 return 0;
455}
456
457static int orion_spi_runtime_resume(struct device *dev)
458{
459 struct spi_master *master = dev_get_drvdata(dev);
460 struct orion_spi *spi = spi_master_get_devdata(master);
461
462 return clk_prepare_enable(spi->clk);
463}
464#endif
465
466static const struct dev_pm_ops orion_spi_pm_ops = {
467 SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
468 orion_spi_runtime_resume,
469 NULL)
470};
471
Grant Likelyfd4a3192012-12-07 16:57:14 +0000472static const struct of_device_id orion_spi_of_match_table[] = {
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200473 { .compatible = "marvell,orion-spi", },
474 {}
475};
476MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
477
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700478static struct platform_driver orion_spi_driver = {
479 .driver = {
480 .name = DRIVER_NAME,
481 .owner = THIS_MODULE,
Russell King5c678692014-06-21 12:22:37 +0100482 .pm = &orion_spi_pm_ops,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200483 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700484 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300485 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000486 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700487};
488
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300489module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700490
491MODULE_DESCRIPTION("Orion SPI driver");
492MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
493MODULE_LICENSE("GPL");