1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH HSPI bus driver
5 * Copyright (C) 2011 Kuninori Morimoto
8 * Based on pxa2xx_spi.c:
9 * Copyright (C) 2011 Renesas Solutions Corp.
10 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/timer.h>
17 #include <linux/delay.h>
18 #include <linux/list.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
23 #include <linux/spi/spi.h>
24 #include <linux/spi/sh_hspi.h>
38 struct spi_controller
*ctlr
;
46 static void hspi_write(struct hspi_priv
*hspi
, int reg
, u32 val
)
48 iowrite32(val
, hspi
->addr
+ reg
);
51 static u32
hspi_read(struct hspi_priv
*hspi
, int reg
)
53 return ioread32(hspi
->addr
+ reg
);
56 static void hspi_bit_set(struct hspi_priv
*hspi
, int reg
, u32 mask
, u32 set
)
58 u32 val
= hspi_read(hspi
, reg
);
63 hspi_write(hspi
, reg
, val
);
69 static int hspi_status_check_timeout(struct hspi_priv
*hspi
, u32 mask
, u32 val
)
74 if ((mask
& hspi_read(hspi
, SPSR
)) == val
)
80 dev_err(hspi
->dev
, "timeout\n");
88 #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0)
89 #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1)
90 static void hspi_hw_cs_ctrl(struct hspi_priv
*hspi
, int hi
)
92 hspi_bit_set(hspi
, SPSCR
, (1 << 6), (hi
) << 6);
95 static void hspi_hw_setup(struct hspi_priv
*hspi
,
96 struct spi_message
*msg
,
97 struct spi_transfer
*t
)
99 struct spi_device
*spi
= msg
->spi
;
100 struct device
*dev
= hspi
->dev
;
102 u32 rate
, best_rate
, min
, tmp
;
105 * find best IDIV/CLKCx settings
110 for (idiv_clk
= 0x00; idiv_clk
<= 0x3F; idiv_clk
++) {
111 rate
= clk_get_rate(hspi
->clk
);
113 /* IDIV calculation */
114 if (idiv_clk
& (1 << 5))
119 /* CLKCx calculation */
120 rate
/= (((idiv_clk
& 0x1F) + 1) * 2);
122 /* save best settings */
123 tmp
= abs(t
->speed_hz
- rate
);
131 if (spi
->mode
& SPI_CPHA
)
133 if (spi
->mode
& SPI_CPOL
)
136 dev_dbg(dev
, "speed %d/%d\n", t
->speed_hz
, best_rate
);
138 hspi_write(hspi
, SPCR
, spcr
);
139 hspi_write(hspi
, SPSR
, 0x0);
140 hspi_write(hspi
, SPSCR
, 0x21); /* master mode / CS control */
143 static int hspi_transfer_one_message(struct spi_controller
*ctlr
,
144 struct spi_message
*msg
)
146 struct hspi_priv
*hspi
= spi_controller_get_devdata(ctlr
);
147 struct spi_transfer
*t
;
151 unsigned int cs_change
;
152 const int nsecs
= 50;
154 dev_dbg(hspi
->dev
, "%s\n", __func__
);
158 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
161 hspi_hw_setup(hspi
, msg
, t
);
162 hspi_hw_cs_enable(hspi
);
165 cs_change
= t
->cs_change
;
167 for (i
= 0; i
< t
->len
; i
++) {
170 ret
= hspi_status_check_timeout(hspi
, 0x1, 0);
176 tx
= (u32
)((u8
*)t
->tx_buf
)[i
];
178 hspi_write(hspi
, SPTBR
, tx
);
181 ret
= hspi_status_check_timeout(hspi
, 0x4, 0x4);
185 rx
= hspi_read(hspi
, SPRBR
);
187 ((u8
*)t
->rx_buf
)[i
] = (u8
)rx
;
191 msg
->actual_length
+= t
->len
;
194 udelay(t
->delay_usecs
);
198 hspi_hw_cs_disable(hspi
);
206 hspi_hw_cs_disable(hspi
);
208 spi_finalize_current_message(ctlr
);
213 static int hspi_probe(struct platform_device
*pdev
)
215 struct resource
*res
;
216 struct spi_controller
*ctlr
;
217 struct hspi_priv
*hspi
;
222 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
224 dev_err(&pdev
->dev
, "invalid resource\n");
228 ctlr
= spi_alloc_master(&pdev
->dev
, sizeof(*hspi
));
232 clk
= clk_get(&pdev
->dev
, NULL
);
234 dev_err(&pdev
->dev
, "couldn't get clock\n");
239 hspi
= spi_controller_get_devdata(ctlr
);
240 platform_set_drvdata(pdev
, hspi
);
244 hspi
->dev
= &pdev
->dev
;
246 hspi
->addr
= devm_ioremap(hspi
->dev
,
247 res
->start
, resource_size(res
));
253 pm_runtime_enable(&pdev
->dev
);
255 ctlr
->bus_num
= pdev
->id
;
256 ctlr
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
257 ctlr
->dev
.of_node
= pdev
->dev
.of_node
;
258 ctlr
->auto_runtime_pm
= true;
259 ctlr
->transfer_one_message
= hspi_transfer_one_message
;
260 ctlr
->bits_per_word_mask
= SPI_BPW_MASK(8);
262 ret
= devm_spi_register_controller(&pdev
->dev
, ctlr
);
264 dev_err(&pdev
->dev
, "devm_spi_register_controller error.\n");
271 pm_runtime_disable(&pdev
->dev
);
275 spi_controller_put(ctlr
);
280 static int hspi_remove(struct platform_device
*pdev
)
282 struct hspi_priv
*hspi
= platform_get_drvdata(pdev
);
284 pm_runtime_disable(&pdev
->dev
);
291 static const struct of_device_id hspi_of_match
[] = {
292 { .compatible
= "renesas,hspi", },
295 MODULE_DEVICE_TABLE(of
, hspi_of_match
);
297 static struct platform_driver hspi_driver
= {
299 .remove
= hspi_remove
,
302 .of_match_table
= hspi_of_match
,
305 module_platform_driver(hspi_driver
);
307 MODULE_DESCRIPTION("SuperH HSPI bus driver");
308 MODULE_LICENSE("GPL v2");
309 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
310 MODULE_ALIAS("platform:sh-hspi");