blob: caaa53fc68cce55a076834b27eb0f407847093a9 [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;
Andrew Lunn4574b882012-04-06 17:17:26 +020046 struct clk *clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070047};
48
Shadi Ammouri60cadec2008-08-05 13:01:09 -070049static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
50{
51 return orion_spi->base + reg;
52}
53
54static inline void
55orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
56{
57 void __iomem *reg_addr = spi_reg(orion_spi, reg);
58 u32 val;
59
60 val = readl(reg_addr);
61 val |= mask;
62 writel(val, reg_addr);
63}
64
65static inline void
66orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
67{
68 void __iomem *reg_addr = spi_reg(orion_spi, reg);
69 u32 val;
70
71 val = readl(reg_addr);
72 val &= ~mask;
73 writel(val, reg_addr);
74}
75
Shadi Ammouri60cadec2008-08-05 13:01:09 -070076static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
77{
78 u32 tclk_hz;
79 u32 rate;
80 u32 prescale;
81 u32 reg;
82 struct orion_spi *orion_spi;
83
84 orion_spi = spi_master_get_devdata(spi->master);
85
Andrew Lunn4574b882012-04-06 17:17:26 +020086 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -070087
88 /*
89 * the supported rates are: 4,6,8...30
90 * round up as we look for equal or less speed
91 */
92 rate = DIV_ROUND_UP(tclk_hz, speed);
93 rate = roundup(rate, 2);
94
95 /* check if requested speed is too small */
96 if (rate > 30)
97 return -EINVAL;
98
99 if (rate < 4)
100 rate = 4;
101
102 /* Convert the rate to SPI clock divisor value. */
103 prescale = 0x10 + rate/2;
104
105 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
106 reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
107 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
108
109 return 0;
110}
111
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700112static void
113orion_spi_mode_set(struct spi_device *spi)
114{
115 u32 reg;
116 struct orion_spi *orion_spi;
117
118 orion_spi = spi_master_get_devdata(spi->master);
119
120 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
121 reg &= ~ORION_SPI_MODE_MASK;
122 if (spi->mode & SPI_CPOL)
123 reg |= ORION_SPI_MODE_CPOL;
124 if (spi->mode & SPI_CPHA)
125 reg |= ORION_SPI_MODE_CPHA;
126 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
127}
128
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700129/*
130 * called only when no transfer is active on the bus
131 */
132static int
133orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
134{
135 struct orion_spi *orion_spi;
136 unsigned int speed = spi->max_speed_hz;
137 unsigned int bits_per_word = spi->bits_per_word;
138 int rc;
139
140 orion_spi = spi_master_get_devdata(spi->master);
141
142 if ((t != NULL) && t->speed_hz)
143 speed = t->speed_hz;
144
145 if ((t != NULL) && t->bits_per_word)
146 bits_per_word = t->bits_per_word;
147
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700148 orion_spi_mode_set(spi);
149
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700150 rc = orion_spi_baudrate_set(spi, speed);
151 if (rc)
152 return rc;
153
Axel Lin495b3352014-02-11 20:51:36 +0800154 if (bits_per_word == 16)
155 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
156 ORION_SPI_IF_8_16_BIT_MODE);
157 else
158 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
159 ORION_SPI_IF_8_16_BIT_MODE);
160
161 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700162}
163
164static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
165{
166 if (enable)
167 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
168 else
169 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
170}
171
172static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
173{
174 int i;
175
176 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
177 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
178 return 1;
179 else
180 udelay(1);
181 }
182
183 return -1;
184}
185
186static inline int
187orion_spi_write_read_8bit(struct spi_device *spi,
188 const u8 **tx_buf, u8 **rx_buf)
189{
190 void __iomem *tx_reg, *rx_reg, *int_reg;
191 struct orion_spi *orion_spi;
192
193 orion_spi = spi_master_get_devdata(spi->master);
194 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
195 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
196 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
197
198 /* clear the interrupt cause register */
199 writel(0x0, int_reg);
200
201 if (tx_buf && *tx_buf)
202 writel(*(*tx_buf)++, tx_reg);
203 else
204 writel(0, tx_reg);
205
206 if (orion_spi_wait_till_ready(orion_spi) < 0) {
207 dev_err(&spi->dev, "TXS timed out\n");
208 return -1;
209 }
210
211 if (rx_buf && *rx_buf)
212 *(*rx_buf)++ = readl(rx_reg);
213
214 return 1;
215}
216
217static inline int
218orion_spi_write_read_16bit(struct spi_device *spi,
219 const u16 **tx_buf, u16 **rx_buf)
220{
221 void __iomem *tx_reg, *rx_reg, *int_reg;
222 struct orion_spi *orion_spi;
223
224 orion_spi = spi_master_get_devdata(spi->master);
225 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
226 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
227 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
228
229 /* clear the interrupt cause register */
230 writel(0x0, int_reg);
231
232 if (tx_buf && *tx_buf)
233 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
234 else
235 writel(0, tx_reg);
236
237 if (orion_spi_wait_till_ready(orion_spi) < 0) {
238 dev_err(&spi->dev, "TXS timed out\n");
239 return -1;
240 }
241
242 if (rx_buf && *rx_buf)
243 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
244
245 return 1;
246}
247
248static unsigned int
249orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
250{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700251 unsigned int count;
252 int word_len;
253
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700254 word_len = spi->bits_per_word;
255 count = xfer->len;
256
257 if (word_len == 8) {
258 const u8 *tx = xfer->tx_buf;
259 u8 *rx = xfer->rx_buf;
260
261 do {
262 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
263 goto out;
264 count--;
265 } while (count);
266 } else if (word_len == 16) {
267 const u16 *tx = xfer->tx_buf;
268 u16 *rx = xfer->rx_buf;
269
270 do {
271 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
272 goto out;
273 count -= 2;
274 } while (count);
275 }
276
277out:
278 return xfer->len - count;
279}
280
281
Andrew Lunnba59a802012-07-23 13:16:55 +0200282static int orion_spi_transfer_one_message(struct spi_master *master,
283 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700284{
Andrew Lunnba59a802012-07-23 13:16:55 +0200285 struct orion_spi *orion_spi = spi_master_get_devdata(master);
286 struct spi_device *spi = m->spi;
287 struct spi_transfer *t = NULL;
288 int par_override = 0;
289 int status = 0;
290 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700291
Andrew Lunnba59a802012-07-23 13:16:55 +0200292 /* Load defaults */
293 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700294
Andrew Lunnba59a802012-07-23 13:16:55 +0200295 if (status < 0)
296 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700297
Andrew Lunnba59a802012-07-23 13:16:55 +0200298 list_for_each_entry(t, &m->transfers, transfer_list) {
299 /* make sure buffer length is even when working in 16
300 * bit mode*/
301 if ((t->bits_per_word == 16) && (t->len & 1)) {
302 dev_err(&spi->dev,
303 "message rejected : "
304 "odd data length %d while in 16 bit mode\n",
305 t->len);
306 status = -EIO;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700307 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700308 }
309
Andrew Lunnba59a802012-07-23 13:16:55 +0200310 if (par_override || t->speed_hz || t->bits_per_word) {
311 par_override = 1;
312 status = orion_spi_setup_transfer(spi, t);
313 if (status < 0)
314 break;
315 if (!t->speed_hz && !t->bits_per_word)
316 par_override = 0;
317 }
318
319 if (!cs_active) {
320 orion_spi_set_cs(orion_spi, 1);
321 cs_active = 1;
322 }
323
324 if (t->len)
325 m->actual_length += orion_spi_write_read(spi, t);
326
327 if (t->delay_usecs)
328 udelay(t->delay_usecs);
329
330 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700331 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200332 cs_active = 0;
333 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700334 }
335
Andrew Lunnba59a802012-07-23 13:16:55 +0200336msg_done:
337 if (cs_active)
338 orion_spi_set_cs(orion_spi, 0);
339
340 m->status = status;
341 spi_finalize_current_message(master);
342
343 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700344}
345
Grant Likely2deff8d2013-02-05 13:27:35 +0000346static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700347{
348 /* Verify that the CS is deasserted */
349 orion_spi_set_cs(orion_spi, 0);
350
351 return 0;
352}
353
Grant Likely2deff8d2013-02-05 13:27:35 +0000354static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700355{
356 struct spi_master *master;
357 struct orion_spi *spi;
358 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200359 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700360 int status = 0;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200361 const u32 *iprop;
362 int size;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700363
Jingoo Han3fed8062013-10-14 10:35:08 +0900364 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700365 if (master == NULL) {
366 dev_dbg(&pdev->dev, "master allocation failed\n");
367 return -ENOMEM;
368 }
369
370 if (pdev->id != -1)
371 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200372 if (pdev->dev.of_node) {
373 iprop = of_get_property(pdev->dev.of_node, "cell-index",
374 &size);
375 if (iprop && size == sizeof(*iprop))
376 master->bus_num = *iprop;
377 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700378
David Brownelle7db06b2009-06-17 16:26:04 -0700379 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700380 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700381
Andrew Lunnba59a802012-07-23 13:16:55 +0200382 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700383 master->num_chipselect = ORION_NUM_CHIPSELECTS;
Axel Lin495b3352014-02-11 20:51:36 +0800384 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700385
Jingoo Han24b5a822013-05-23 19:20:40 +0900386 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700387
388 spi = spi_master_get_devdata(master);
389 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700390
Jingoo Hanbb489842013-12-09 19:21:22 +0900391 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200392 if (IS_ERR(spi->clk)) {
393 status = PTR_ERR(spi->clk);
394 goto out;
395 }
396
397 clk_prepare(spi->clk);
398 clk_enable(spi->clk);
399 tclk_hz = clk_get_rate(spi->clk);
Axel Linb52a37f2014-02-04 11:05:50 +0800400 master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4);
401 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700402
403 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100404 spi->base = devm_ioremap_resource(&pdev->dev, r);
405 if (IS_ERR(spi->base)) {
406 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200407 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700408 }
409
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700410 if (orion_spi_reset(spi) < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100411 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700412
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200413 master->dev.of_node = pdev->dev.of_node;
Jingoo Han4bd3d8e2013-09-24 13:43:09 +0900414 status = devm_spi_register_master(&pdev->dev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700415 if (status < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100416 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700417
418 return status;
419
Andrew Lunn4574b882012-04-06 17:17:26 +0200420out_rel_clk:
421 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700422out:
423 spi_master_put(master);
424 return status;
425}
426
427
Grant Likely2deff8d2013-02-05 13:27:35 +0000428static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700429{
430 struct spi_master *master;
Andrew Lunnba59a802012-07-23 13:16:55 +0200431 struct orion_spi *spi;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700432
Jingoo Han24b5a822013-05-23 19:20:40 +0900433 master = platform_get_drvdata(pdev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700434 spi = spi_master_get_devdata(master);
435
Andrew Lunn4574b882012-04-06 17:17:26 +0200436 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200437
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700438 return 0;
439}
440
441MODULE_ALIAS("platform:" DRIVER_NAME);
442
Grant Likelyfd4a3192012-12-07 16:57:14 +0000443static const struct of_device_id orion_spi_of_match_table[] = {
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200444 { .compatible = "marvell,orion-spi", },
445 {}
446};
447MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
448
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700449static struct platform_driver orion_spi_driver = {
450 .driver = {
451 .name = DRIVER_NAME,
452 .owner = THIS_MODULE,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200453 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700454 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300455 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000456 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700457};
458
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300459module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700460
461MODULE_DESCRIPTION("Orion SPI driver");
462MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
463MODULE_LICENSE("GPL");