2 * J-Core SPI controller driver
4 * Copyright (C) 2012-2016 Smart Energy Instruments, Inc.
6 * Current version by Rich Felker
7 * Based loosely on initial version by Oleksandr G Zhadan
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/spi/spi.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
20 #include <linux/delay.h>
22 #define DRV_NAME "jcore_spi"
27 #define JCORE_SPI_CTRL_XMIT 0x02
28 #define JCORE_SPI_STAT_BUSY 0x02
29 #define JCORE_SPI_CTRL_LOOP 0x08
30 #define JCORE_SPI_CTRL_CS_BITS 0x15
32 #define JCORE_SPI_WAIT_RDY_MAX_LOOP 2000000
35 struct spi_master
*master
;
38 unsigned int speed_reg
;
39 unsigned int speed_hz
;
40 unsigned int clock_freq
;
43 static int jcore_spi_wait(void __iomem
*ctrl_reg
)
45 unsigned timeout
= JCORE_SPI_WAIT_RDY_MAX_LOOP
;
48 if (!(readl(ctrl_reg
) & JCORE_SPI_STAT_BUSY
))
56 static void jcore_spi_program(struct jcore_spi
*hw
)
58 void __iomem
*ctrl_reg
= hw
->base
+ CTRL_REG
;
60 if (jcore_spi_wait(ctrl_reg
))
61 dev_err(hw
->master
->dev
.parent
,
62 "timeout waiting to program ctrl reg.\n");
64 writel(hw
->cs_reg
| hw
->speed_reg
, ctrl_reg
);
67 static void jcore_spi_chipsel(struct spi_device
*spi
, bool value
)
69 struct jcore_spi
*hw
= spi_master_get_devdata(spi
->master
);
70 u32 csbit
= 1U << (2 * spi
->chip_select
);
72 dev_dbg(hw
->master
->dev
.parent
, "chipselect %d\n", spi
->chip_select
);
79 jcore_spi_program(hw
);
82 static void jcore_spi_baudrate(struct jcore_spi
*hw
, int speed
)
84 if (speed
== hw
->speed_hz
) return;
86 if (speed
>= hw
->clock_freq
/ 2)
89 hw
->speed_reg
= ((hw
->clock_freq
/ 2 / speed
) - 1) << 27;
90 jcore_spi_program(hw
);
91 dev_dbg(hw
->master
->dev
.parent
, "speed=%d reg=0x%x\n",
92 speed
, hw
->speed_reg
);
95 static int jcore_spi_txrx(struct spi_master
*master
, struct spi_device
*spi
,
96 struct spi_transfer
*t
)
98 struct jcore_spi
*hw
= spi_master_get_devdata(master
);
100 void __iomem
*ctrl_reg
= hw
->base
+ CTRL_REG
;
101 void __iomem
*data_reg
= hw
->base
+ DATA_REG
;
105 const unsigned char *tx
;
110 jcore_spi_baudrate(hw
, t
->speed_hz
);
112 xmit
= hw
->cs_reg
| hw
->speed_reg
| JCORE_SPI_CTRL_XMIT
;
117 for (count
= 0; count
< len
; count
++) {
118 if (jcore_spi_wait(ctrl_reg
))
121 writel(tx
? *tx
++ : 0, data_reg
);
122 writel(xmit
, ctrl_reg
);
124 if (jcore_spi_wait(ctrl_reg
))
128 *rx
++ = readl(data_reg
);
131 spi_finalize_current_transfer(master
);
139 static int jcore_spi_probe(struct platform_device
*pdev
)
141 struct device_node
*node
= pdev
->dev
.of_node
;
142 struct jcore_spi
*hw
;
143 struct spi_master
*master
;
144 struct resource
*res
;
149 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct jcore_spi
));
153 /* Setup the master state. */
154 master
->num_chipselect
= 3;
155 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
156 master
->transfer_one
= jcore_spi_txrx
;
157 master
->set_cs
= jcore_spi_chipsel
;
158 master
->dev
.of_node
= node
;
159 master
->bus_num
= pdev
->id
;
161 hw
= spi_master_get_devdata(master
);
163 platform_set_drvdata(pdev
, hw
);
165 /* Find and map our resources */
166 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
169 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
170 resource_size(res
), pdev
->name
))
172 hw
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
178 * The SPI clock rate controlled via a configurable clock divider
179 * which is applied to the reference clock. A 50 MHz reference is
180 * most suitable for obtaining standard SPI clock rates, but some
181 * designs may have a different reference clock, and the DT must
182 * make the driver aware so that it can properly program the
183 * requested rate. If the clock is omitted, 50 MHz is assumed.
185 clock_freq
= 50000000;
186 clk
= devm_clk_get(&pdev
->dev
, "ref_clk");
188 if (clk_prepare_enable(clk
) == 0) {
189 clock_freq
= clk_get_rate(clk
);
190 clk_disable_unprepare(clk
);
192 dev_warn(&pdev
->dev
, "could not enable ref_clk\n");
194 hw
->clock_freq
= clock_freq
;
196 /* Initialize all CS bits to high. */
197 hw
->cs_reg
= JCORE_SPI_CTRL_CS_BITS
;
198 jcore_spi_baudrate(hw
, 400000);
200 /* Register our spi controller */
201 err
= devm_spi_register_master(&pdev
->dev
, master
);
210 spi_master_put(master
);
214 static const struct of_device_id jcore_spi_of_match
[] = {
215 { .compatible
= "jcore,spi2" },
218 MODULE_DEVICE_TABLE(of
, jcore_spi_of_match
);
220 static struct platform_driver jcore_spi_driver
= {
221 .probe
= jcore_spi_probe
,
224 .of_match_table
= jcore_spi_of_match
,
228 module_platform_driver(jcore_spi_driver
);
230 MODULE_DESCRIPTION("J-Core SPI driver");
231 MODULE_AUTHOR("Rich Felker <dalias@libc.org>");
232 MODULE_LICENSE("GPL");
233 MODULE_ALIAS("platform:" DRV_NAME
);