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_MODE_CPOL (1 << 11)
36 #define ORION_SPI_MODE_CPHA (1 << 12)
37 #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
38 #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
39 #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
43 struct spi_master
*master
;
45 unsigned int max_speed
;
46 unsigned int min_speed
;
50 static inline void __iomem
*spi_reg(struct orion_spi
*orion_spi
, u32 reg
)
52 return orion_spi
->base
+ reg
;
56 orion_spi_setbits(struct orion_spi
*orion_spi
, u32 reg
, u32 mask
)
58 void __iomem
*reg_addr
= spi_reg(orion_spi
, reg
);
61 val
= readl(reg_addr
);
63 writel(val
, reg_addr
);
67 orion_spi_clrbits(struct orion_spi
*orion_spi
, u32 reg
, u32 mask
)
69 void __iomem
*reg_addr
= spi_reg(orion_spi
, reg
);
72 val
= readl(reg_addr
);
74 writel(val
, reg_addr
);
77 static int orion_spi_set_transfer_size(struct orion_spi
*orion_spi
, int size
)
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
);
86 pr_debug("Bad bits per word value %d (only 8 or 16 are "
94 static int orion_spi_baudrate_set(struct spi_device
*spi
, unsigned int speed
)
100 struct orion_spi
*orion_spi
;
102 orion_spi
= spi_master_get_devdata(spi
->master
);
104 tclk_hz
= clk_get_rate(orion_spi
->clk
);
107 * the supported rates are: 4,6,8...30
108 * round up as we look for equal or less speed
110 rate
= DIV_ROUND_UP(tclk_hz
, speed
);
111 rate
= roundup(rate
, 2);
113 /* check if requested speed is too small */
120 /* Convert the rate to SPI clock divisor value. */
121 prescale
= 0x10 + rate
/2;
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
));
131 orion_spi_mode_set(struct spi_device
*spi
)
134 struct orion_spi
*orion_spi
;
136 orion_spi
= spi_master_get_devdata(spi
->master
);
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
));
148 * called only when no transfer is active on the bus
151 orion_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
153 struct orion_spi
*orion_spi
;
154 unsigned int speed
= spi
->max_speed_hz
;
155 unsigned int bits_per_word
= spi
->bits_per_word
;
158 orion_spi
= spi_master_get_devdata(spi
->master
);
160 if ((t
!= NULL
) && t
->speed_hz
)
163 if ((t
!= NULL
) && t
->bits_per_word
)
164 bits_per_word
= t
->bits_per_word
;
166 orion_spi_mode_set(spi
);
168 rc
= orion_spi_baudrate_set(spi
, speed
);
172 return orion_spi_set_transfer_size(orion_spi
, bits_per_word
);
175 static void orion_spi_set_cs(struct orion_spi
*orion_spi
, int enable
)
178 orion_spi_setbits(orion_spi
, ORION_SPI_IF_CTRL_REG
, 0x1);
180 orion_spi_clrbits(orion_spi
, ORION_SPI_IF_CTRL_REG
, 0x1);
183 static inline int orion_spi_wait_till_ready(struct orion_spi
*orion_spi
)
187 for (i
= 0; i
< ORION_SPI_WAIT_RDY_MAX_LOOP
; i
++) {
188 if (readl(spi_reg(orion_spi
, ORION_SPI_INT_CAUSE_REG
)))
198 orion_spi_write_read_8bit(struct spi_device
*spi
,
199 const u8
**tx_buf
, u8
**rx_buf
)
201 void __iomem
*tx_reg
, *rx_reg
, *int_reg
;
202 struct orion_spi
*orion_spi
;
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
);
209 /* clear the interrupt cause register */
210 writel(0x0, int_reg
);
212 if (tx_buf
&& *tx_buf
)
213 writel(*(*tx_buf
)++, tx_reg
);
217 if (orion_spi_wait_till_ready(orion_spi
) < 0) {
218 dev_err(&spi
->dev
, "TXS timed out\n");
222 if (rx_buf
&& *rx_buf
)
223 *(*rx_buf
)++ = readl(rx_reg
);
229 orion_spi_write_read_16bit(struct spi_device
*spi
,
230 const u16
**tx_buf
, u16
**rx_buf
)
232 void __iomem
*tx_reg
, *rx_reg
, *int_reg
;
233 struct orion_spi
*orion_spi
;
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
);
240 /* clear the interrupt cause register */
241 writel(0x0, int_reg
);
243 if (tx_buf
&& *tx_buf
)
244 writel(__cpu_to_le16(get_unaligned((*tx_buf
)++)), tx_reg
);
248 if (orion_spi_wait_till_ready(orion_spi
) < 0) {
249 dev_err(&spi
->dev
, "TXS timed out\n");
253 if (rx_buf
&& *rx_buf
)
254 put_unaligned(__le16_to_cpu(readl(rx_reg
)), (*rx_buf
)++);
260 orion_spi_write_read(struct spi_device
*spi
, struct spi_transfer
*xfer
)
262 struct orion_spi
*orion_spi
;
266 orion_spi
= spi_master_get_devdata(spi
->master
);
267 word_len
= spi
->bits_per_word
;
271 const u8
*tx
= xfer
->tx_buf
;
272 u8
*rx
= xfer
->rx_buf
;
275 if (orion_spi_write_read_8bit(spi
, &tx
, &rx
) < 0)
279 } else if (word_len
== 16) {
280 const u16
*tx
= xfer
->tx_buf
;
281 u16
*rx
= xfer
->rx_buf
;
284 if (orion_spi_write_read_16bit(spi
, &tx
, &rx
) < 0)
291 return xfer
->len
- count
;
295 static int orion_spi_transfer_one_message(struct spi_master
*master
,
296 struct spi_message
*m
)
298 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;
306 status
= orion_spi_setup_transfer(spi
, NULL
);
311 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
312 /* make sure buffer length is even when working in 16
314 if ((t
->bits_per_word
== 16) && (t
->len
& 1)) {
316 "message rejected : "
317 "odd data length %d while in 16 bit mode\n",
323 if (t
->speed_hz
&& t
->speed_hz
< orion_spi
->min_speed
) {
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
);
333 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
335 status
= orion_spi_setup_transfer(spi
, t
);
338 if (!t
->speed_hz
&& !t
->bits_per_word
)
343 orion_spi_set_cs(orion_spi
, 1);
348 m
->actual_length
+= orion_spi_write_read(spi
, t
);
351 udelay(t
->delay_usecs
);
354 orion_spi_set_cs(orion_spi
, 0);
361 orion_spi_set_cs(orion_spi
, 0);
364 spi_finalize_current_message(master
);
369 static int __init
orion_spi_reset(struct orion_spi
*orion_spi
)
371 /* Verify that the CS is deasserted */
372 orion_spi_set_cs(orion_spi
, 0);
377 static int orion_spi_setup(struct spi_device
*spi
)
379 struct orion_spi
*orion_spi
;
381 orion_spi
= spi_master_get_devdata(spi
->master
);
383 if ((spi
->max_speed_hz
== 0)
384 || (spi
->max_speed_hz
> orion_spi
->max_speed
))
385 spi
->max_speed_hz
= orion_spi
->max_speed
;
387 if (spi
->max_speed_hz
< orion_spi
->min_speed
) {
388 dev_err(&spi
->dev
, "setup: requested speed too low %d Hz\n",
394 * baudrate & width will be set orion_spi_setup_transfer
399 static int __init
orion_spi_probe(struct platform_device
*pdev
)
401 struct spi_master
*master
;
402 struct orion_spi
*spi
;
404 unsigned long tclk_hz
;
409 master
= spi_alloc_master(&pdev
->dev
, sizeof *spi
);
410 if (master
== NULL
) {
411 dev_dbg(&pdev
->dev
, "master allocation failed\n");
416 master
->bus_num
= pdev
->id
;
417 if (pdev
->dev
.of_node
) {
418 iprop
= of_get_property(pdev
->dev
.of_node
, "cell-index",
420 if (iprop
&& size
== sizeof(*iprop
))
421 master
->bus_num
= *iprop
;
424 /* we support only mode 0, and no options */
425 master
->mode_bits
= SPI_CPHA
| SPI_CPOL
;
427 master
->setup
= orion_spi_setup
;
428 master
->transfer_one_message
= orion_spi_transfer_one_message
;
429 master
->num_chipselect
= ORION_NUM_CHIPSELECTS
;
431 dev_set_drvdata(&pdev
->dev
, master
);
433 spi
= spi_master_get_devdata(master
);
434 spi
->master
= master
;
436 spi
->clk
= clk_get(&pdev
->dev
, NULL
);
437 if (IS_ERR(spi
->clk
)) {
438 status
= PTR_ERR(spi
->clk
);
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);
448 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
454 if (!request_mem_region(r
->start
, resource_size(r
),
455 dev_name(&pdev
->dev
))) {
459 spi
->base
= ioremap(r
->start
, SZ_1K
);
461 if (orion_spi_reset(spi
) < 0)
464 master
->dev
.of_node
= pdev
->dev
.of_node
;
465 status
= spi_register_master(master
);
472 release_mem_region(r
->start
, resource_size(r
));
474 clk_disable_unprepare(spi
->clk
);
477 spi_master_put(master
);
482 static int __exit
orion_spi_remove(struct platform_device
*pdev
)
484 struct spi_master
*master
;
486 struct orion_spi
*spi
;
488 master
= dev_get_drvdata(&pdev
->dev
);
489 spi
= spi_master_get_devdata(master
);
491 clk_disable_unprepare(spi
->clk
);
494 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
495 release_mem_region(r
->start
, resource_size(r
));
497 spi_unregister_master(master
);
502 MODULE_ALIAS("platform:" DRIVER_NAME
);
504 static const struct of_device_id orion_spi_of_match_table
[] = {
505 { .compatible
= "marvell,orion-spi", },
508 MODULE_DEVICE_TABLE(of
, orion_spi_of_match_table
);
510 static struct platform_driver orion_spi_driver
= {
513 .owner
= THIS_MODULE
,
514 .of_match_table
= of_match_ptr(orion_spi_of_match_table
),
516 .remove
= __exit_p(orion_spi_remove
),
519 static int __init
orion_spi_init(void)
521 return platform_driver_probe(&orion_spi_driver
, orion_spi_probe
);
523 module_init(orion_spi_init
);
525 static void __exit
orion_spi_exit(void)
527 platform_driver_unregister(&orion_spi_driver
);
529 module_exit(orion_spi_exit
);
531 MODULE_DESCRIPTION("Orion SPI driver");
532 MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
533 MODULE_LICENSE("GPL");