blob: 20f174ee06ec468b03aebf4d0c51e0a1c8686a62 [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
76static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
77{
78 if (size == 16) {
79 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
80 ORION_SPI_IF_8_16_BIT_MODE);
81 } else if (size == 8) {
82 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
83 ORION_SPI_IF_8_16_BIT_MODE);
84 } else {
Jingoo Han3fed8062013-10-14 10:35:08 +090085 pr_debug("Bad bits per word value %d (only 8 or 16 are allowed).\n",
86 size);
Shadi Ammouri60cadec2008-08-05 13:01:09 -070087 return -EINVAL;
88 }
89
90 return 0;
91}
92
93static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
94{
95 u32 tclk_hz;
96 u32 rate;
97 u32 prescale;
98 u32 reg;
99 struct orion_spi *orion_spi;
100
101 orion_spi = spi_master_get_devdata(spi->master);
102
Andrew Lunn4574b882012-04-06 17:17:26 +0200103 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700104
105 /*
106 * the supported rates are: 4,6,8...30
107 * round up as we look for equal or less speed
108 */
109 rate = DIV_ROUND_UP(tclk_hz, speed);
110 rate = roundup(rate, 2);
111
112 /* check if requested speed is too small */
113 if (rate > 30)
114 return -EINVAL;
115
116 if (rate < 4)
117 rate = 4;
118
119 /* Convert the rate to SPI clock divisor value. */
120 prescale = 0x10 + rate/2;
121
122 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
123 reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
124 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
125
126 return 0;
127}
128
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700129static void
130orion_spi_mode_set(struct spi_device *spi)
131{
132 u32 reg;
133 struct orion_spi *orion_spi;
134
135 orion_spi = spi_master_get_devdata(spi->master);
136
137 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
138 reg &= ~ORION_SPI_MODE_MASK;
139 if (spi->mode & SPI_CPOL)
140 reg |= ORION_SPI_MODE_CPOL;
141 if (spi->mode & SPI_CPHA)
142 reg |= ORION_SPI_MODE_CPHA;
143 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
144}
145
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700146/*
147 * called only when no transfer is active on the bus
148 */
149static int
150orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
151{
152 struct orion_spi *orion_spi;
153 unsigned int speed = spi->max_speed_hz;
154 unsigned int bits_per_word = spi->bits_per_word;
155 int rc;
156
157 orion_spi = spi_master_get_devdata(spi->master);
158
159 if ((t != NULL) && t->speed_hz)
160 speed = t->speed_hz;
161
162 if ((t != NULL) && t->bits_per_word)
163 bits_per_word = t->bits_per_word;
164
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700165 orion_spi_mode_set(spi);
166
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700167 rc = orion_spi_baudrate_set(spi, speed);
168 if (rc)
169 return rc;
170
171 return orion_spi_set_transfer_size(orion_spi, bits_per_word);
172}
173
174static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
175{
176 if (enable)
177 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
178 else
179 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
180}
181
182static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
183{
184 int i;
185
186 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
187 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
188 return 1;
189 else
190 udelay(1);
191 }
192
193 return -1;
194}
195
196static inline int
197orion_spi_write_read_8bit(struct spi_device *spi,
198 const u8 **tx_buf, u8 **rx_buf)
199{
200 void __iomem *tx_reg, *rx_reg, *int_reg;
201 struct orion_spi *orion_spi;
202
203 orion_spi = spi_master_get_devdata(spi->master);
204 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
205 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
206 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
207
208 /* clear the interrupt cause register */
209 writel(0x0, int_reg);
210
211 if (tx_buf && *tx_buf)
212 writel(*(*tx_buf)++, tx_reg);
213 else
214 writel(0, tx_reg);
215
216 if (orion_spi_wait_till_ready(orion_spi) < 0) {
217 dev_err(&spi->dev, "TXS timed out\n");
218 return -1;
219 }
220
221 if (rx_buf && *rx_buf)
222 *(*rx_buf)++ = readl(rx_reg);
223
224 return 1;
225}
226
227static inline int
228orion_spi_write_read_16bit(struct spi_device *spi,
229 const u16 **tx_buf, u16 **rx_buf)
230{
231 void __iomem *tx_reg, *rx_reg, *int_reg;
232 struct orion_spi *orion_spi;
233
234 orion_spi = spi_master_get_devdata(spi->master);
235 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
236 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
237 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
238
239 /* clear the interrupt cause register */
240 writel(0x0, int_reg);
241
242 if (tx_buf && *tx_buf)
243 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
244 else
245 writel(0, tx_reg);
246
247 if (orion_spi_wait_till_ready(orion_spi) < 0) {
248 dev_err(&spi->dev, "TXS timed out\n");
249 return -1;
250 }
251
252 if (rx_buf && *rx_buf)
253 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
254
255 return 1;
256}
257
258static unsigned int
259orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
260{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700261 unsigned int count;
262 int word_len;
263
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700264 word_len = spi->bits_per_word;
265 count = xfer->len;
266
267 if (word_len == 8) {
268 const u8 *tx = xfer->tx_buf;
269 u8 *rx = xfer->rx_buf;
270
271 do {
272 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
273 goto out;
274 count--;
275 } while (count);
276 } else if (word_len == 16) {
277 const u16 *tx = xfer->tx_buf;
278 u16 *rx = xfer->rx_buf;
279
280 do {
281 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
282 goto out;
283 count -= 2;
284 } while (count);
285 }
286
287out:
288 return xfer->len - count;
289}
290
291
Andrew Lunnba59a802012-07-23 13:16:55 +0200292static int orion_spi_transfer_one_message(struct spi_master *master,
293 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700294{
Andrew Lunnba59a802012-07-23 13:16:55 +0200295 struct orion_spi *orion_spi = spi_master_get_devdata(master);
296 struct spi_device *spi = m->spi;
297 struct spi_transfer *t = NULL;
298 int par_override = 0;
299 int status = 0;
300 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700301
Andrew Lunnba59a802012-07-23 13:16:55 +0200302 /* Load defaults */
303 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700304
Andrew Lunnba59a802012-07-23 13:16:55 +0200305 if (status < 0)
306 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700307
Andrew Lunnba59a802012-07-23 13:16:55 +0200308 list_for_each_entry(t, &m->transfers, transfer_list) {
309 /* make sure buffer length is even when working in 16
310 * bit mode*/
311 if ((t->bits_per_word == 16) && (t->len & 1)) {
312 dev_err(&spi->dev,
313 "message rejected : "
314 "odd data length %d while in 16 bit mode\n",
315 t->len);
316 status = -EIO;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700317 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700318 }
319
Andrew Lunnba59a802012-07-23 13:16:55 +0200320 if (par_override || t->speed_hz || t->bits_per_word) {
321 par_override = 1;
322 status = orion_spi_setup_transfer(spi, t);
323 if (status < 0)
324 break;
325 if (!t->speed_hz && !t->bits_per_word)
326 par_override = 0;
327 }
328
329 if (!cs_active) {
330 orion_spi_set_cs(orion_spi, 1);
331 cs_active = 1;
332 }
333
334 if (t->len)
335 m->actual_length += orion_spi_write_read(spi, t);
336
337 if (t->delay_usecs)
338 udelay(t->delay_usecs);
339
340 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700341 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200342 cs_active = 0;
343 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700344 }
345
Andrew Lunnba59a802012-07-23 13:16:55 +0200346msg_done:
347 if (cs_active)
348 orion_spi_set_cs(orion_spi, 0);
349
350 m->status = status;
351 spi_finalize_current_message(master);
352
353 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700354}
355
Grant Likely2deff8d2013-02-05 13:27:35 +0000356static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700357{
358 /* Verify that the CS is deasserted */
359 orion_spi_set_cs(orion_spi, 0);
360
361 return 0;
362}
363
Grant Likely2deff8d2013-02-05 13:27:35 +0000364static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700365{
366 struct spi_master *master;
367 struct orion_spi *spi;
368 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200369 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700370 int status = 0;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200371 const u32 *iprop;
372 int size;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700373
Jingoo Han3fed8062013-10-14 10:35:08 +0900374 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700375 if (master == NULL) {
376 dev_dbg(&pdev->dev, "master allocation failed\n");
377 return -ENOMEM;
378 }
379
380 if (pdev->id != -1)
381 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200382 if (pdev->dev.of_node) {
383 iprop = of_get_property(pdev->dev.of_node, "cell-index",
384 &size);
385 if (iprop && size == sizeof(*iprop))
386 master->bus_num = *iprop;
387 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700388
David Brownelle7db06b2009-06-17 16:26:04 -0700389 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700390 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700391
Andrew Lunnba59a802012-07-23 13:16:55 +0200392 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700393 master->num_chipselect = ORION_NUM_CHIPSELECTS;
394
Jingoo Han24b5a822013-05-23 19:20:40 +0900395 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700396
397 spi = spi_master_get_devdata(master);
398 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700399
Jingoo Hanbb489842013-12-09 19:21:22 +0900400 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200401 if (IS_ERR(spi->clk)) {
402 status = PTR_ERR(spi->clk);
403 goto out;
404 }
405
406 clk_prepare(spi->clk);
407 clk_enable(spi->clk);
408 tclk_hz = clk_get_rate(spi->clk);
Axel Linb52a37f2014-02-04 11:05:50 +0800409 master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4);
410 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700411
412 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100413 spi->base = devm_ioremap_resource(&pdev->dev, r);
414 if (IS_ERR(spi->base)) {
415 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200416 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700417 }
418
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700419 if (orion_spi_reset(spi) < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100420 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700421
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200422 master->dev.of_node = pdev->dev.of_node;
Jingoo Han4bd3d8e2013-09-24 13:43:09 +0900423 status = devm_spi_register_master(&pdev->dev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700424 if (status < 0)
Mark Brown1729ce32013-07-28 14:38:06 +0100425 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700426
427 return status;
428
Andrew Lunn4574b882012-04-06 17:17:26 +0200429out_rel_clk:
430 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700431out:
432 spi_master_put(master);
433 return status;
434}
435
436
Grant Likely2deff8d2013-02-05 13:27:35 +0000437static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700438{
439 struct spi_master *master;
Andrew Lunnba59a802012-07-23 13:16:55 +0200440 struct orion_spi *spi;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700441
Jingoo Han24b5a822013-05-23 19:20:40 +0900442 master = platform_get_drvdata(pdev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700443 spi = spi_master_get_devdata(master);
444
Andrew Lunn4574b882012-04-06 17:17:26 +0200445 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200446
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700447 return 0;
448}
449
450MODULE_ALIAS("platform:" DRIVER_NAME);
451
Grant Likelyfd4a3192012-12-07 16:57:14 +0000452static const struct of_device_id orion_spi_of_match_table[] = {
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200453 { .compatible = "marvell,orion-spi", },
454 {}
455};
456MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
457
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700458static struct platform_driver orion_spi_driver = {
459 .driver = {
460 .name = DRIVER_NAME,
461 .owner = THIS_MODULE,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200462 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700463 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300464 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000465 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700466};
467
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300468module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700469
470MODULE_DESCRIPTION("Orion SPI driver");
471MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
472MODULE_LICENSE("GPL");