blob: 551bea774fd8f87be218d5d8613bb56082dd2002 [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
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/delay.h>
15#include <linux/platform_device.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/spi/spi.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040019#include <linux/module.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>
Shadi Ammouri60cadec2008-08-05 13:01:09 -070022#include <asm/unaligned.h>
23
24#define DRIVER_NAME "orion_spi"
25
26#define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
27#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
28
29#define ORION_SPI_IF_CTRL_REG 0x00
30#define ORION_SPI_IF_CONFIG_REG 0x04
31#define ORION_SPI_DATA_OUT_REG 0x08
32#define ORION_SPI_DATA_IN_REG 0x0c
33#define ORION_SPI_INT_CAUSE_REG 0x10
34
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070035#define ORION_SPI_MODE_CPOL (1 << 11)
36#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070037#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
38#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070039#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
40 ORION_SPI_MODE_CPHA)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070041
42struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070043 struct spi_master *master;
44 void __iomem *base;
45 unsigned int max_speed;
46 unsigned int min_speed;
Andrew Lunn4574b882012-04-06 17:17:26 +020047 struct clk *clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070048};
49
Shadi Ammouri60cadec2008-08-05 13:01:09 -070050static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
51{
52 return orion_spi->base + reg;
53}
54
55static inline void
56orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
57{
58 void __iomem *reg_addr = spi_reg(orion_spi, reg);
59 u32 val;
60
61 val = readl(reg_addr);
62 val |= mask;
63 writel(val, reg_addr);
64}
65
66static inline void
67orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
68{
69 void __iomem *reg_addr = spi_reg(orion_spi, reg);
70 u32 val;
71
72 val = readl(reg_addr);
73 val &= ~mask;
74 writel(val, reg_addr);
75}
76
77static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
78{
79 if (size == 16) {
80 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
81 ORION_SPI_IF_8_16_BIT_MODE);
82 } else if (size == 8) {
83 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
84 ORION_SPI_IF_8_16_BIT_MODE);
85 } else {
86 pr_debug("Bad bits per word value %d (only 8 or 16 are "
87 "allowed).\n", size);
88 return -EINVAL;
89 }
90
91 return 0;
92}
93
94static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
95{
96 u32 tclk_hz;
97 u32 rate;
98 u32 prescale;
99 u32 reg;
100 struct orion_spi *orion_spi;
101
102 orion_spi = spi_master_get_devdata(spi->master);
103
Andrew Lunn4574b882012-04-06 17:17:26 +0200104 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700105
106 /*
107 * the supported rates are: 4,6,8...30
108 * round up as we look for equal or less speed
109 */
110 rate = DIV_ROUND_UP(tclk_hz, speed);
111 rate = roundup(rate, 2);
112
113 /* check if requested speed is too small */
114 if (rate > 30)
115 return -EINVAL;
116
117 if (rate < 4)
118 rate = 4;
119
120 /* Convert the rate to SPI clock divisor value. */
121 prescale = 0x10 + rate/2;
122
123 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
124 reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
125 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
126
127 return 0;
128}
129
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700130static void
131orion_spi_mode_set(struct spi_device *spi)
132{
133 u32 reg;
134 struct orion_spi *orion_spi;
135
136 orion_spi = spi_master_get_devdata(spi->master);
137
138 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
139 reg &= ~ORION_SPI_MODE_MASK;
140 if (spi->mode & SPI_CPOL)
141 reg |= ORION_SPI_MODE_CPOL;
142 if (spi->mode & SPI_CPHA)
143 reg |= ORION_SPI_MODE_CPHA;
144 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
145}
146
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700147/*
148 * called only when no transfer is active on the bus
149 */
150static int
151orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
152{
153 struct orion_spi *orion_spi;
154 unsigned int speed = spi->max_speed_hz;
155 unsigned int bits_per_word = spi->bits_per_word;
156 int rc;
157
158 orion_spi = spi_master_get_devdata(spi->master);
159
160 if ((t != NULL) && t->speed_hz)
161 speed = t->speed_hz;
162
163 if ((t != NULL) && t->bits_per_word)
164 bits_per_word = t->bits_per_word;
165
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700166 orion_spi_mode_set(spi);
167
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700168 rc = orion_spi_baudrate_set(spi, speed);
169 if (rc)
170 return rc;
171
172 return orion_spi_set_transfer_size(orion_spi, bits_per_word);
173}
174
175static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
176{
177 if (enable)
178 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
179 else
180 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
181}
182
183static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
184{
185 int i;
186
187 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
188 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
189 return 1;
190 else
191 udelay(1);
192 }
193
194 return -1;
195}
196
197static inline int
198orion_spi_write_read_8bit(struct spi_device *spi,
199 const u8 **tx_buf, u8 **rx_buf)
200{
201 void __iomem *tx_reg, *rx_reg, *int_reg;
202 struct orion_spi *orion_spi;
203
204 orion_spi = spi_master_get_devdata(spi->master);
205 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
206 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
207 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
208
209 /* clear the interrupt cause register */
210 writel(0x0, int_reg);
211
212 if (tx_buf && *tx_buf)
213 writel(*(*tx_buf)++, tx_reg);
214 else
215 writel(0, tx_reg);
216
217 if (orion_spi_wait_till_ready(orion_spi) < 0) {
218 dev_err(&spi->dev, "TXS timed out\n");
219 return -1;
220 }
221
222 if (rx_buf && *rx_buf)
223 *(*rx_buf)++ = readl(rx_reg);
224
225 return 1;
226}
227
228static inline int
229orion_spi_write_read_16bit(struct spi_device *spi,
230 const u16 **tx_buf, u16 **rx_buf)
231{
232 void __iomem *tx_reg, *rx_reg, *int_reg;
233 struct orion_spi *orion_spi;
234
235 orion_spi = spi_master_get_devdata(spi->master);
236 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
237 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
238 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
239
240 /* clear the interrupt cause register */
241 writel(0x0, int_reg);
242
243 if (tx_buf && *tx_buf)
244 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
245 else
246 writel(0, tx_reg);
247
248 if (orion_spi_wait_till_ready(orion_spi) < 0) {
249 dev_err(&spi->dev, "TXS timed out\n");
250 return -1;
251 }
252
253 if (rx_buf && *rx_buf)
254 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
255
256 return 1;
257}
258
259static unsigned int
260orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
261{
262 struct orion_spi *orion_spi;
263 unsigned int count;
264 int word_len;
265
266 orion_spi = spi_master_get_devdata(spi->master);
267 word_len = spi->bits_per_word;
268 count = xfer->len;
269
270 if (word_len == 8) {
271 const u8 *tx = xfer->tx_buf;
272 u8 *rx = xfer->rx_buf;
273
274 do {
275 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
276 goto out;
277 count--;
278 } while (count);
279 } else if (word_len == 16) {
280 const u16 *tx = xfer->tx_buf;
281 u16 *rx = xfer->rx_buf;
282
283 do {
284 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
285 goto out;
286 count -= 2;
287 } while (count);
288 }
289
290out:
291 return xfer->len - count;
292}
293
294
Andrew Lunnba59a802012-07-23 13:16:55 +0200295static int orion_spi_transfer_one_message(struct spi_master *master,
296 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700297{
Andrew Lunnba59a802012-07-23 13:16:55 +0200298 struct orion_spi *orion_spi = spi_master_get_devdata(master);
299 struct spi_device *spi = m->spi;
300 struct spi_transfer *t = NULL;
301 int par_override = 0;
302 int status = 0;
303 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700304
Andrew Lunnba59a802012-07-23 13:16:55 +0200305 /* Load defaults */
306 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700307
Andrew Lunnba59a802012-07-23 13:16:55 +0200308 if (status < 0)
309 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700310
Andrew Lunnba59a802012-07-23 13:16:55 +0200311 list_for_each_entry(t, &m->transfers, transfer_list) {
312 /* make sure buffer length is even when working in 16
313 * bit mode*/
314 if ((t->bits_per_word == 16) && (t->len & 1)) {
315 dev_err(&spi->dev,
316 "message rejected : "
317 "odd data length %d while in 16 bit mode\n",
318 t->len);
319 status = -EIO;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700320 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700321 }
322
Andrew Lunnba59a802012-07-23 13:16:55 +0200323 if (t->speed_hz && t->speed_hz < orion_spi->min_speed) {
324 dev_err(&spi->dev,
325 "message rejected : "
326 "device min speed (%d Hz) exceeds "
327 "required transfer speed (%d Hz)\n",
328 orion_spi->min_speed, t->speed_hz);
329 status = -EIO;
330 goto msg_done;
331 }
332
333 if (par_override || t->speed_hz || t->bits_per_word) {
334 par_override = 1;
335 status = orion_spi_setup_transfer(spi, t);
336 if (status < 0)
337 break;
338 if (!t->speed_hz && !t->bits_per_word)
339 par_override = 0;
340 }
341
342 if (!cs_active) {
343 orion_spi_set_cs(orion_spi, 1);
344 cs_active = 1;
345 }
346
347 if (t->len)
348 m->actual_length += orion_spi_write_read(spi, t);
349
350 if (t->delay_usecs)
351 udelay(t->delay_usecs);
352
353 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700354 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200355 cs_active = 0;
356 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700357 }
358
Andrew Lunnba59a802012-07-23 13:16:55 +0200359msg_done:
360 if (cs_active)
361 orion_spi_set_cs(orion_spi, 0);
362
363 m->status = status;
364 spi_finalize_current_message(master);
365
366 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700367}
368
Grant Likely2deff8d2013-02-05 13:27:35 +0000369static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700370{
371 /* Verify that the CS is deasserted */
372 orion_spi_set_cs(orion_spi, 0);
373
374 return 0;
375}
376
377static int orion_spi_setup(struct spi_device *spi)
378{
379 struct orion_spi *orion_spi;
380
381 orion_spi = spi_master_get_devdata(spi->master);
382
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700383 if ((spi->max_speed_hz == 0)
384 || (spi->max_speed_hz > orion_spi->max_speed))
385 spi->max_speed_hz = orion_spi->max_speed;
386
387 if (spi->max_speed_hz < orion_spi->min_speed) {
388 dev_err(&spi->dev, "setup: requested speed too low %d Hz\n",
389 spi->max_speed_hz);
390 return -EINVAL;
391 }
392
393 /*
394 * baudrate & width will be set orion_spi_setup_transfer
395 */
396 return 0;
397}
398
Grant Likely2deff8d2013-02-05 13:27:35 +0000399static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700400{
401 struct spi_master *master;
402 struct orion_spi *spi;
403 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200404 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700405 int status = 0;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200406 const u32 *iprop;
407 int size;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700408
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700409 master = spi_alloc_master(&pdev->dev, sizeof *spi);
410 if (master == NULL) {
411 dev_dbg(&pdev->dev, "master allocation failed\n");
412 return -ENOMEM;
413 }
414
415 if (pdev->id != -1)
416 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200417 if (pdev->dev.of_node) {
418 iprop = of_get_property(pdev->dev.of_node, "cell-index",
419 &size);
420 if (iprop && size == sizeof(*iprop))
421 master->bus_num = *iprop;
422 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700423
David Brownelle7db06b2009-06-17 16:26:04 -0700424 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700425 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700426
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700427 master->setup = orion_spi_setup;
Andrew Lunnba59a802012-07-23 13:16:55 +0200428 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700429 master->num_chipselect = ORION_NUM_CHIPSELECTS;
430
Jingoo Han24b5a822013-05-23 19:20:40 +0900431 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700432
433 spi = spi_master_get_devdata(master);
434 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700435
Andrew Lunn4574b882012-04-06 17:17:26 +0200436 spi->clk = clk_get(&pdev->dev, NULL);
437 if (IS_ERR(spi->clk)) {
438 status = PTR_ERR(spi->clk);
439 goto out;
440 }
441
442 clk_prepare(spi->clk);
443 clk_enable(spi->clk);
444 tclk_hz = clk_get_rate(spi->clk);
445 spi->max_speed = DIV_ROUND_UP(tclk_hz, 4);
446 spi->min_speed = DIV_ROUND_UP(tclk_hz, 30);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700447
448 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100449 spi->base = devm_ioremap_resource(&pdev->dev, r);
450 if (IS_ERR(spi->base)) {
451 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200452 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700453 }
454
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700455 if (orion_spi_reset(spi) < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100456 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700457
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200458 master->dev.of_node = pdev->dev.of_node;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700459 status = spi_register_master(master);
460 if (status < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100461 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700462
463 return status;
464
Andrew Lunn4574b882012-04-06 17:17:26 +0200465out_rel_clk:
466 clk_disable_unprepare(spi->clk);
467 clk_put(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700468out:
469 spi_master_put(master);
470 return status;
471}
472
473
Grant Likely2deff8d2013-02-05 13:27:35 +0000474static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700475{
476 struct spi_master *master;
Andrew Lunnba59a802012-07-23 13:16:55 +0200477 struct orion_spi *spi;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700478
Jingoo Han24b5a822013-05-23 19:20:40 +0900479 master = platform_get_drvdata(pdev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700480 spi = spi_master_get_devdata(master);
481
Andrew Lunn4574b882012-04-06 17:17:26 +0200482 clk_disable_unprepare(spi->clk);
483 clk_put(spi->clk);
484
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700485 spi_unregister_master(master);
486
487 return 0;
488}
489
490MODULE_ALIAS("platform:" DRIVER_NAME);
491
Grant Likelyfd4a3192012-12-07 16:57:14 +0000492static const struct of_device_id orion_spi_of_match_table[] = {
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200493 { .compatible = "marvell,orion-spi", },
494 {}
495};
496MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
497
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700498static struct platform_driver orion_spi_driver = {
499 .driver = {
500 .name = DRIVER_NAME,
501 .owner = THIS_MODULE,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200502 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700503 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300504 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000505 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700506};
507
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300508module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700509
510MODULE_DESCRIPTION("Orion SPI driver");
511MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
512MODULE_LICENSE("GPL");