blob: 75871d444ff49e66288a13937f8cd25d906e8f98 [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>
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
27#define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
28#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
29
30#define ORION_SPI_IF_CTRL_REG 0x00
31#define ORION_SPI_IF_CONFIG_REG 0x04
32#define ORION_SPI_DATA_OUT_REG 0x08
33#define ORION_SPI_DATA_IN_REG 0x0c
34#define ORION_SPI_INT_CAUSE_REG 0x10
35
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070036#define ORION_SPI_MODE_CPOL (1 << 11)
37#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070038#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
39#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070040#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
41 ORION_SPI_MODE_CPHA)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070042
43struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070044 struct spi_master *master;
45 void __iomem *base;
46 unsigned int max_speed;
47 unsigned int min_speed;
Andrew Lunn4574b882012-04-06 17:17:26 +020048 struct clk *clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070049};
50
Shadi Ammouri60cadec2008-08-05 13:01:09 -070051static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
52{
53 return orion_spi->base + reg;
54}
55
56static inline void
57orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
58{
59 void __iomem *reg_addr = spi_reg(orion_spi, reg);
60 u32 val;
61
62 val = readl(reg_addr);
63 val |= mask;
64 writel(val, reg_addr);
65}
66
67static inline void
68orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
69{
70 void __iomem *reg_addr = spi_reg(orion_spi, reg);
71 u32 val;
72
73 val = readl(reg_addr);
74 val &= ~mask;
75 writel(val, reg_addr);
76}
77
78static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
79{
80 if (size == 16) {
81 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
82 ORION_SPI_IF_8_16_BIT_MODE);
83 } else if (size == 8) {
84 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
85 ORION_SPI_IF_8_16_BIT_MODE);
86 } else {
Jingoo Han3fed8062013-10-14 10:35:08 +090087 pr_debug("Bad bits per word value %d (only 8 or 16 are allowed).\n",
88 size);
Shadi Ammouri60cadec2008-08-05 13:01:09 -070089 return -EINVAL;
90 }
91
92 return 0;
93}
94
95static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
96{
97 u32 tclk_hz;
98 u32 rate;
99 u32 prescale;
100 u32 reg;
101 struct orion_spi *orion_spi;
102
103 orion_spi = spi_master_get_devdata(spi->master);
104
Andrew Lunn4574b882012-04-06 17:17:26 +0200105 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700106
107 /*
108 * the supported rates are: 4,6,8...30
109 * round up as we look for equal or less speed
110 */
111 rate = DIV_ROUND_UP(tclk_hz, speed);
112 rate = roundup(rate, 2);
113
114 /* check if requested speed is too small */
115 if (rate > 30)
116 return -EINVAL;
117
118 if (rate < 4)
119 rate = 4;
120
121 /* Convert the rate to SPI clock divisor value. */
122 prescale = 0x10 + rate/2;
123
124 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
125 reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
126 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
127
128 return 0;
129}
130
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700131static void
132orion_spi_mode_set(struct spi_device *spi)
133{
134 u32 reg;
135 struct orion_spi *orion_spi;
136
137 orion_spi = spi_master_get_devdata(spi->master);
138
139 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
140 reg &= ~ORION_SPI_MODE_MASK;
141 if (spi->mode & SPI_CPOL)
142 reg |= ORION_SPI_MODE_CPOL;
143 if (spi->mode & SPI_CPHA)
144 reg |= ORION_SPI_MODE_CPHA;
145 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
146}
147
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700148/*
149 * called only when no transfer is active on the bus
150 */
151static int
152orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
153{
154 struct orion_spi *orion_spi;
155 unsigned int speed = spi->max_speed_hz;
156 unsigned int bits_per_word = spi->bits_per_word;
157 int rc;
158
159 orion_spi = spi_master_get_devdata(spi->master);
160
161 if ((t != NULL) && t->speed_hz)
162 speed = t->speed_hz;
163
164 if ((t != NULL) && t->bits_per_word)
165 bits_per_word = t->bits_per_word;
166
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700167 orion_spi_mode_set(spi);
168
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700169 rc = orion_spi_baudrate_set(spi, speed);
170 if (rc)
171 return rc;
172
173 return orion_spi_set_transfer_size(orion_spi, bits_per_word);
174}
175
176static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
177{
178 if (enable)
179 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
180 else
181 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
182}
183
184static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
185{
186 int i;
187
188 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
189 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
190 return 1;
191 else
192 udelay(1);
193 }
194
195 return -1;
196}
197
198static inline int
199orion_spi_write_read_8bit(struct spi_device *spi,
200 const u8 **tx_buf, u8 **rx_buf)
201{
202 void __iomem *tx_reg, *rx_reg, *int_reg;
203 struct orion_spi *orion_spi;
204
205 orion_spi = spi_master_get_devdata(spi->master);
206 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
207 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
208 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
209
210 /* clear the interrupt cause register */
211 writel(0x0, int_reg);
212
213 if (tx_buf && *tx_buf)
214 writel(*(*tx_buf)++, tx_reg);
215 else
216 writel(0, tx_reg);
217
218 if (orion_spi_wait_till_ready(orion_spi) < 0) {
219 dev_err(&spi->dev, "TXS timed out\n");
220 return -1;
221 }
222
223 if (rx_buf && *rx_buf)
224 *(*rx_buf)++ = readl(rx_reg);
225
226 return 1;
227}
228
229static inline int
230orion_spi_write_read_16bit(struct spi_device *spi,
231 const u16 **tx_buf, u16 **rx_buf)
232{
233 void __iomem *tx_reg, *rx_reg, *int_reg;
234 struct orion_spi *orion_spi;
235
236 orion_spi = spi_master_get_devdata(spi->master);
237 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
238 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
239 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
240
241 /* clear the interrupt cause register */
242 writel(0x0, int_reg);
243
244 if (tx_buf && *tx_buf)
245 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
246 else
247 writel(0, tx_reg);
248
249 if (orion_spi_wait_till_ready(orion_spi) < 0) {
250 dev_err(&spi->dev, "TXS timed out\n");
251 return -1;
252 }
253
254 if (rx_buf && *rx_buf)
255 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
256
257 return 1;
258}
259
260static unsigned int
261orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
262{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700263 unsigned int count;
264 int word_len;
265
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700266 word_len = spi->bits_per_word;
267 count = xfer->len;
268
269 if (word_len == 8) {
270 const u8 *tx = xfer->tx_buf;
271 u8 *rx = xfer->rx_buf;
272
273 do {
274 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
275 goto out;
276 count--;
277 } while (count);
278 } else if (word_len == 16) {
279 const u16 *tx = xfer->tx_buf;
280 u16 *rx = xfer->rx_buf;
281
282 do {
283 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
284 goto out;
285 count -= 2;
286 } while (count);
287 }
288
289out:
290 return xfer->len - count;
291}
292
293
Andrew Lunnba59a802012-07-23 13:16:55 +0200294static int orion_spi_transfer_one_message(struct spi_master *master,
295 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700296{
Andrew Lunnba59a802012-07-23 13:16:55 +0200297 struct orion_spi *orion_spi = spi_master_get_devdata(master);
298 struct spi_device *spi = m->spi;
299 struct spi_transfer *t = NULL;
300 int par_override = 0;
301 int status = 0;
302 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700303
Andrew Lunnba59a802012-07-23 13:16:55 +0200304 /* Load defaults */
305 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700306
Andrew Lunnba59a802012-07-23 13:16:55 +0200307 if (status < 0)
308 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700309
Andrew Lunnba59a802012-07-23 13:16:55 +0200310 list_for_each_entry(t, &m->transfers, transfer_list) {
311 /* make sure buffer length is even when working in 16
312 * bit mode*/
313 if ((t->bits_per_word == 16) && (t->len & 1)) {
314 dev_err(&spi->dev,
315 "message rejected : "
316 "odd data length %d while in 16 bit mode\n",
317 t->len);
318 status = -EIO;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700319 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700320 }
321
Andrew Lunnba59a802012-07-23 13:16:55 +0200322 if (t->speed_hz && t->speed_hz < orion_spi->min_speed) {
323 dev_err(&spi->dev,
324 "message rejected : "
325 "device min speed (%d Hz) exceeds "
326 "required transfer speed (%d Hz)\n",
327 orion_spi->min_speed, t->speed_hz);
328 status = -EIO;
329 goto msg_done;
330 }
331
332 if (par_override || t->speed_hz || t->bits_per_word) {
333 par_override = 1;
334 status = orion_spi_setup_transfer(spi, t);
335 if (status < 0)
336 break;
337 if (!t->speed_hz && !t->bits_per_word)
338 par_override = 0;
339 }
340
341 if (!cs_active) {
342 orion_spi_set_cs(orion_spi, 1);
343 cs_active = 1;
344 }
345
346 if (t->len)
347 m->actual_length += orion_spi_write_read(spi, t);
348
349 if (t->delay_usecs)
350 udelay(t->delay_usecs);
351
352 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700353 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200354 cs_active = 0;
355 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700356 }
357
Andrew Lunnba59a802012-07-23 13:16:55 +0200358msg_done:
359 if (cs_active)
360 orion_spi_set_cs(orion_spi, 0);
361
362 m->status = status;
363 spi_finalize_current_message(master);
364
365 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700366}
367
Grant Likely2deff8d2013-02-05 13:27:35 +0000368static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700369{
370 /* Verify that the CS is deasserted */
371 orion_spi_set_cs(orion_spi, 0);
372
373 return 0;
374}
375
376static int orion_spi_setup(struct spi_device *spi)
377{
378 struct orion_spi *orion_spi;
379
380 orion_spi = spi_master_get_devdata(spi->master);
381
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700382 if ((spi->max_speed_hz == 0)
383 || (spi->max_speed_hz > orion_spi->max_speed))
384 spi->max_speed_hz = orion_spi->max_speed;
385
386 if (spi->max_speed_hz < orion_spi->min_speed) {
387 dev_err(&spi->dev, "setup: requested speed too low %d Hz\n",
388 spi->max_speed_hz);
389 return -EINVAL;
390 }
391
392 /*
393 * baudrate & width will be set orion_spi_setup_transfer
394 */
395 return 0;
396}
397
Grant Likely2deff8d2013-02-05 13:27:35 +0000398static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700399{
400 struct spi_master *master;
401 struct orion_spi *spi;
402 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200403 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700404 int status = 0;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200405 const u32 *iprop;
406 int size;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700407
Jingoo Han3fed8062013-10-14 10:35:08 +0900408 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700409 if (master == NULL) {
410 dev_dbg(&pdev->dev, "master allocation failed\n");
411 return -ENOMEM;
412 }
413
414 if (pdev->id != -1)
415 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200416 if (pdev->dev.of_node) {
417 iprop = of_get_property(pdev->dev.of_node, "cell-index",
418 &size);
419 if (iprop && size == sizeof(*iprop))
420 master->bus_num = *iprop;
421 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700422
David Brownelle7db06b2009-06-17 16:26:04 -0700423 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700424 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700425
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700426 master->setup = orion_spi_setup;
Andrew Lunnba59a802012-07-23 13:16:55 +0200427 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700428 master->num_chipselect = ORION_NUM_CHIPSELECTS;
429
Jingoo Han24b5a822013-05-23 19:20:40 +0900430 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700431
432 spi = spi_master_get_devdata(master);
433 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700434
Jingoo Hanbb489842013-12-09 19:21:22 +0900435 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200436 if (IS_ERR(spi->clk)) {
437 status = PTR_ERR(spi->clk);
438 goto out;
439 }
440
441 clk_prepare(spi->clk);
442 clk_enable(spi->clk);
443 tclk_hz = clk_get_rate(spi->clk);
444 spi->max_speed = DIV_ROUND_UP(tclk_hz, 4);
445 spi->min_speed = DIV_ROUND_UP(tclk_hz, 30);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700446
447 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100448 spi->base = devm_ioremap_resource(&pdev->dev, r);
449 if (IS_ERR(spi->base)) {
450 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200451 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700452 }
453
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700454 if (orion_spi_reset(spi) < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100455 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700456
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200457 master->dev.of_node = pdev->dev.of_node;
Jingoo Han4bd3d8e2013-09-24 13:43:09 +0900458 status = devm_spi_register_master(&pdev->dev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700459 if (status < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100460 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700461
462 return status;
463
Andrew Lunn4574b882012-04-06 17:17:26 +0200464out_rel_clk:
465 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700466out:
467 spi_master_put(master);
468 return status;
469}
470
471
Grant Likely2deff8d2013-02-05 13:27:35 +0000472static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700473{
474 struct spi_master *master;
Andrew Lunnba59a802012-07-23 13:16:55 +0200475 struct orion_spi *spi;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700476
Jingoo Han24b5a822013-05-23 19:20:40 +0900477 master = platform_get_drvdata(pdev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700478 spi = spi_master_get_devdata(master);
479
Andrew Lunn4574b882012-04-06 17:17:26 +0200480 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200481
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700482 return 0;
483}
484
485MODULE_ALIAS("platform:" DRIVER_NAME);
486
Grant Likelyfd4a3192012-12-07 16:57:14 +0000487static const struct of_device_id orion_spi_of_match_table[] = {
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200488 { .compatible = "marvell,orion-spi", },
489 {}
490};
491MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
492
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700493static struct platform_driver orion_spi_driver = {
494 .driver = {
495 .name = DRIVER_NAME,
496 .owner = THIS_MODULE,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200497 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700498 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300499 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000500 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700501};
502
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300503module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700504
505MODULE_DESCRIPTION("Orion SPI driver");
506MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
507MODULE_LICENSE("GPL");