2 * Marvell Orion SPI controller driver
4 * Author: Shadi Ammouri <shadi@marvell.com>
5 * Copyright (C) 2007-2008 Marvell Ltd.
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.
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>
18 #include <linux/spi/spi.h>
19 #include <linux/module.h>
21 #include <linux/clk.h>
22 #include <asm/unaligned.h>
24 #define DRIVER_NAME "orion_spi"
26 #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
27 #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
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
35 #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
36 #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
39 struct spi_master
*master
;
41 unsigned int max_speed
;
42 unsigned int min_speed
;
46 static inline void __iomem
*spi_reg(struct orion_spi
*orion_spi
, u32 reg
)
48 return orion_spi
->base
+ reg
;
52 orion_spi_setbits(struct orion_spi
*orion_spi
, u32 reg
, u32 mask
)
54 void __iomem
*reg_addr
= spi_reg(orion_spi
, reg
);
57 val
= readl(reg_addr
);
59 writel(val
, reg_addr
);
63 orion_spi_clrbits(struct orion_spi
*orion_spi
, u32 reg
, u32 mask
)
65 void __iomem
*reg_addr
= spi_reg(orion_spi
, reg
);
68 val
= readl(reg_addr
);
70 writel(val
, reg_addr
);
73 static int orion_spi_set_transfer_size(struct orion_spi
*orion_spi
, int size
)
76 orion_spi_setbits(orion_spi
, ORION_SPI_IF_CONFIG_REG
,
77 ORION_SPI_IF_8_16_BIT_MODE
);
78 } else if (size
== 8) {
79 orion_spi_clrbits(orion_spi
, ORION_SPI_IF_CONFIG_REG
,
80 ORION_SPI_IF_8_16_BIT_MODE
);
82 pr_debug("Bad bits per word value %d (only 8 or 16 are "
90 static int orion_spi_baudrate_set(struct spi_device
*spi
, unsigned int speed
)
96 struct orion_spi
*orion_spi
;
98 orion_spi
= spi_master_get_devdata(spi
->master
);
100 tclk_hz
= clk_get_rate(orion_spi
->clk
);
103 * the supported rates are: 4,6,8...30
104 * round up as we look for equal or less speed
106 rate
= DIV_ROUND_UP(tclk_hz
, speed
);
107 rate
= roundup(rate
, 2);
109 /* check if requested speed is too small */
116 /* Convert the rate to SPI clock divisor value. */
117 prescale
= 0x10 + rate
/2;
119 reg
= readl(spi_reg(orion_spi
, ORION_SPI_IF_CONFIG_REG
));
120 reg
= ((reg
& ~ORION_SPI_CLK_PRESCALE_MASK
) | prescale
);
121 writel(reg
, spi_reg(orion_spi
, ORION_SPI_IF_CONFIG_REG
));
127 * called only when no transfer is active on the bus
130 orion_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
132 struct orion_spi
*orion_spi
;
133 unsigned int speed
= spi
->max_speed_hz
;
134 unsigned int bits_per_word
= spi
->bits_per_word
;
137 orion_spi
= spi_master_get_devdata(spi
->master
);
139 if ((t
!= NULL
) && t
->speed_hz
)
142 if ((t
!= NULL
) && t
->bits_per_word
)
143 bits_per_word
= t
->bits_per_word
;
145 rc
= orion_spi_baudrate_set(spi
, speed
);
149 return orion_spi_set_transfer_size(orion_spi
, bits_per_word
);
152 static void orion_spi_set_cs(struct orion_spi
*orion_spi
, int enable
)
155 orion_spi_setbits(orion_spi
, ORION_SPI_IF_CTRL_REG
, 0x1);
157 orion_spi_clrbits(orion_spi
, ORION_SPI_IF_CTRL_REG
, 0x1);
160 static inline int orion_spi_wait_till_ready(struct orion_spi
*orion_spi
)
164 for (i
= 0; i
< ORION_SPI_WAIT_RDY_MAX_LOOP
; i
++) {
165 if (readl(spi_reg(orion_spi
, ORION_SPI_INT_CAUSE_REG
)))
175 orion_spi_write_read_8bit(struct spi_device
*spi
,
176 const u8
**tx_buf
, u8
**rx_buf
)
178 void __iomem
*tx_reg
, *rx_reg
, *int_reg
;
179 struct orion_spi
*orion_spi
;
181 orion_spi
= spi_master_get_devdata(spi
->master
);
182 tx_reg
= spi_reg(orion_spi
, ORION_SPI_DATA_OUT_REG
);
183 rx_reg
= spi_reg(orion_spi
, ORION_SPI_DATA_IN_REG
);
184 int_reg
= spi_reg(orion_spi
, ORION_SPI_INT_CAUSE_REG
);
186 /* clear the interrupt cause register */
187 writel(0x0, int_reg
);
189 if (tx_buf
&& *tx_buf
)
190 writel(*(*tx_buf
)++, tx_reg
);
194 if (orion_spi_wait_till_ready(orion_spi
) < 0) {
195 dev_err(&spi
->dev
, "TXS timed out\n");
199 if (rx_buf
&& *rx_buf
)
200 *(*rx_buf
)++ = readl(rx_reg
);
206 orion_spi_write_read_16bit(struct spi_device
*spi
,
207 const u16
**tx_buf
, u16
**rx_buf
)
209 void __iomem
*tx_reg
, *rx_reg
, *int_reg
;
210 struct orion_spi
*orion_spi
;
212 orion_spi
= spi_master_get_devdata(spi
->master
);
213 tx_reg
= spi_reg(orion_spi
, ORION_SPI_DATA_OUT_REG
);
214 rx_reg
= spi_reg(orion_spi
, ORION_SPI_DATA_IN_REG
);
215 int_reg
= spi_reg(orion_spi
, ORION_SPI_INT_CAUSE_REG
);
217 /* clear the interrupt cause register */
218 writel(0x0, int_reg
);
220 if (tx_buf
&& *tx_buf
)
221 writel(__cpu_to_le16(get_unaligned((*tx_buf
)++)), tx_reg
);
225 if (orion_spi_wait_till_ready(orion_spi
) < 0) {
226 dev_err(&spi
->dev
, "TXS timed out\n");
230 if (rx_buf
&& *rx_buf
)
231 put_unaligned(__le16_to_cpu(readl(rx_reg
)), (*rx_buf
)++);
237 orion_spi_write_read(struct spi_device
*spi
, struct spi_transfer
*xfer
)
239 struct orion_spi
*orion_spi
;
243 orion_spi
= spi_master_get_devdata(spi
->master
);
244 word_len
= spi
->bits_per_word
;
248 const u8
*tx
= xfer
->tx_buf
;
249 u8
*rx
= xfer
->rx_buf
;
252 if (orion_spi_write_read_8bit(spi
, &tx
, &rx
) < 0)
256 } else if (word_len
== 16) {
257 const u16
*tx
= xfer
->tx_buf
;
258 u16
*rx
= xfer
->rx_buf
;
261 if (orion_spi_write_read_16bit(spi
, &tx
, &rx
) < 0)
268 return xfer
->len
- count
;
272 static int orion_spi_transfer_one_message(struct spi_master
*master
,
273 struct spi_message
*m
)
275 struct orion_spi
*orion_spi
= spi_master_get_devdata(master
);
276 struct spi_device
*spi
= m
->spi
;
277 struct spi_transfer
*t
= NULL
;
278 int par_override
= 0;
283 status
= orion_spi_setup_transfer(spi
, NULL
);
288 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
289 /* make sure buffer length is even when working in 16
291 if ((t
->bits_per_word
== 16) && (t
->len
& 1)) {
293 "message rejected : "
294 "odd data length %d while in 16 bit mode\n",
300 if (t
->speed_hz
&& t
->speed_hz
< orion_spi
->min_speed
) {
302 "message rejected : "
303 "device min speed (%d Hz) exceeds "
304 "required transfer speed (%d Hz)\n",
305 orion_spi
->min_speed
, t
->speed_hz
);
310 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
312 status
= orion_spi_setup_transfer(spi
, t
);
315 if (!t
->speed_hz
&& !t
->bits_per_word
)
320 orion_spi_set_cs(orion_spi
, 1);
325 m
->actual_length
+= orion_spi_write_read(spi
, t
);
328 udelay(t
->delay_usecs
);
331 orion_spi_set_cs(orion_spi
, 0);
338 orion_spi_set_cs(orion_spi
, 0);
341 spi_finalize_current_message(master
);
346 static int __init
orion_spi_reset(struct orion_spi
*orion_spi
)
348 /* Verify that the CS is deasserted */
349 orion_spi_set_cs(orion_spi
, 0);
354 static int orion_spi_setup(struct spi_device
*spi
)
356 struct orion_spi
*orion_spi
;
358 orion_spi
= spi_master_get_devdata(spi
->master
);
360 if ((spi
->max_speed_hz
== 0)
361 || (spi
->max_speed_hz
> orion_spi
->max_speed
))
362 spi
->max_speed_hz
= orion_spi
->max_speed
;
364 if (spi
->max_speed_hz
< orion_spi
->min_speed
) {
365 dev_err(&spi
->dev
, "setup: requested speed too low %d Hz\n",
371 * baudrate & width will be set orion_spi_setup_transfer
376 static int __init
orion_spi_probe(struct platform_device
*pdev
)
378 struct spi_master
*master
;
379 struct orion_spi
*spi
;
381 unsigned long tclk_hz
;
386 master
= spi_alloc_master(&pdev
->dev
, sizeof *spi
);
387 if (master
== NULL
) {
388 dev_dbg(&pdev
->dev
, "master allocation failed\n");
393 master
->bus_num
= pdev
->id
;
394 if (pdev
->dev
.of_node
) {
395 iprop
= of_get_property(pdev
->dev
.of_node
, "cell-index",
397 if (iprop
&& size
== sizeof(*iprop
))
398 master
->bus_num
= *iprop
;
401 /* we support only mode 0, and no options */
402 master
->mode_bits
= 0;
404 master
->setup
= orion_spi_setup
;
405 master
->transfer_one_message
= orion_spi_transfer_one_message
;
406 master
->num_chipselect
= ORION_NUM_CHIPSELECTS
;
408 dev_set_drvdata(&pdev
->dev
, master
);
410 spi
= spi_master_get_devdata(master
);
411 spi
->master
= master
;
413 spi
->clk
= clk_get(&pdev
->dev
, NULL
);
414 if (IS_ERR(spi
->clk
)) {
415 status
= PTR_ERR(spi
->clk
);
419 clk_prepare(spi
->clk
);
420 clk_enable(spi
->clk
);
421 tclk_hz
= clk_get_rate(spi
->clk
);
422 spi
->max_speed
= DIV_ROUND_UP(tclk_hz
, 4);
423 spi
->min_speed
= DIV_ROUND_UP(tclk_hz
, 30);
425 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
431 if (!request_mem_region(r
->start
, resource_size(r
),
432 dev_name(&pdev
->dev
))) {
436 spi
->base
= ioremap(r
->start
, SZ_1K
);
438 if (orion_spi_reset(spi
) < 0)
441 master
->dev
.of_node
= pdev
->dev
.of_node
;
442 status
= spi_register_master(master
);
449 release_mem_region(r
->start
, resource_size(r
));
451 clk_disable_unprepare(spi
->clk
);
454 spi_master_put(master
);
459 static int __exit
orion_spi_remove(struct platform_device
*pdev
)
461 struct spi_master
*master
;
463 struct orion_spi
*spi
;
465 master
= dev_get_drvdata(&pdev
->dev
);
466 spi
= spi_master_get_devdata(master
);
468 clk_disable_unprepare(spi
->clk
);
471 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
472 release_mem_region(r
->start
, resource_size(r
));
474 spi_unregister_master(master
);
479 MODULE_ALIAS("platform:" DRIVER_NAME
);
481 static const struct of_device_id orion_spi_of_match_table
[] __devinitdata
= {
482 { .compatible
= "marvell,orion-spi", },
485 MODULE_DEVICE_TABLE(of
, orion_spi_of_match_table
);
487 static struct platform_driver orion_spi_driver
= {
490 .owner
= THIS_MODULE
,
491 .of_match_table
= of_match_ptr(orion_spi_of_match_table
),
493 .remove
= __exit_p(orion_spi_remove
),
496 static int __init
orion_spi_init(void)
498 return platform_driver_probe(&orion_spi_driver
, orion_spi_probe
);
500 module_init(orion_spi_init
);
502 static void __exit
orion_spi_exit(void)
504 platform_driver_unregister(&orion_spi_driver
);
506 module_exit(orion_spi_exit
);
508 MODULE_DESCRIPTION("Orion SPI driver");
509 MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
510 MODULE_LICENSE("GPL");