blob: a62034e2a7cbfeeee8bbcff188f9ef4d68ce2ea0 [file] [log] [blame]
Wolfram Sang9135bac2018-08-22 00:02:23 +02001// SPDX-License-Identifier: GPL-2.0
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -08002/*
3 * SuperH HSPI bus driver
4 *
5 * Copyright (C) 2011 Kuninori Morimoto
6 *
7 * Based on spi-sh.c:
8 * Based on pxa2xx_spi.c:
9 * Copyright (C) 2011 Renesas Solutions Corp.
10 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080011 */
Kuninori Morimoto49e599b82012-03-14 02:48:05 -070012
13#include <linux/clk.h>
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/timer.h>
17#include <linux/delay.h>
18#include <linux/list.h>
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080019#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/pm_runtime.h>
22#include <linux/io.h>
23#include <linux/spi/spi.h>
24#include <linux/spi/sh_hspi.h>
25
26#define SPCR 0x00
27#define SPSR 0x04
28#define SPSCR 0x08
29#define SPTBR 0x0C
30#define SPRBR 0x10
31#define SPCR2 0x14
32
33/* SPSR */
34#define RXFL (1 << 2)
35
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080036struct hspi_priv {
37 void __iomem *addr;
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +010038 struct spi_controller *ctlr;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080039 struct device *dev;
Kuninori Morimoto49e599b82012-03-14 02:48:05 -070040 struct clk *clk;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080041};
42
43/*
44 * basic function
45 */
46static void hspi_write(struct hspi_priv *hspi, int reg, u32 val)
47{
48 iowrite32(val, hspi->addr + reg);
49}
50
51static u32 hspi_read(struct hspi_priv *hspi, int reg)
52{
53 return ioread32(hspi->addr + reg);
54}
55
Phil Edworthyce329302012-11-22 14:37:27 +000056static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set)
57{
58 u32 val = hspi_read(hspi, reg);
59
60 val &= ~mask;
61 val |= set & mask;
62
63 hspi_write(hspi, reg, val);
64}
65
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080066/*
67 * transfer function
68 */
69static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
70{
71 int t = 256;
72
73 while (t--) {
74 if ((mask & hspi_read(hspi, SPSR)) == val)
75 return 0;
76
Kuninori Morimotobc2bfff2013-05-26 17:59:20 -070077 udelay(10);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -080078 }
79
80 dev_err(hspi->dev, "timeout\n");
81 return -ETIMEDOUT;
82}
83
Kuninori Morimotoec139b62012-03-14 02:47:40 -070084/*
85 * spi master function
86 */
Kuninori Morimotoec139b62012-03-14 02:47:40 -070087
Phil Edworthyce329302012-11-22 14:37:27 +000088#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)
90static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi)
91{
92 hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6);
93}
94
Kuninori Morimoto49e599b82012-03-14 02:48:05 -070095static void hspi_hw_setup(struct hspi_priv *hspi,
96 struct spi_message *msg,
97 struct spi_transfer *t)
98{
99 struct spi_device *spi = msg->spi;
100 struct device *dev = hspi->dev;
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700101 u32 spcr, idiv_clk;
102 u32 rate, best_rate, min, tmp;
103
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700104 /*
105 * find best IDIV/CLKCx settings
106 */
107 min = ~0;
108 best_rate = 0;
109 spcr = 0;
110 for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
111 rate = clk_get_rate(hspi->clk);
112
113 /* IDIV calculation */
114 if (idiv_clk & (1 << 5))
115 rate /= 128;
116 else
117 rate /= 16;
118
119 /* CLKCx calculation */
Jingoo Hana29c8ae2013-10-14 10:35:42 +0900120 rate /= (((idiv_clk & 0x1F) + 1) * 2);
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700121
122 /* save best settings */
Axel Line428a422014-03-02 23:01:50 +0800123 tmp = abs(t->speed_hz - rate);
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700124 if (tmp < min) {
125 min = tmp;
126 spcr = idiv_clk;
127 best_rate = rate;
128 }
129 }
130
131 if (spi->mode & SPI_CPHA)
132 spcr |= 1 << 7;
133 if (spi->mode & SPI_CPOL)
134 spcr |= 1 << 6;
135
Axel Line428a422014-03-02 23:01:50 +0800136 dev_dbg(dev, "speed %d/%d\n", t->speed_hz, best_rate);
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700137
138 hspi_write(hspi, SPCR, spcr);
139 hspi_write(hspi, SPSR, 0x0);
Phil Edworthyce329302012-11-22 14:37:27 +0000140 hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700141}
142
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100143static int hspi_transfer_one_message(struct spi_controller *ctlr,
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700144 struct spi_message *msg)
145{
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100146 struct hspi_priv *hspi = spi_controller_get_devdata(ctlr);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800147 struct spi_transfer *t;
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700148 u32 tx;
149 u32 rx;
150 int ret, i;
Phil Edworthyce329302012-11-22 14:37:27 +0000151 unsigned int cs_change;
152 const int nsecs = 50;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800153
154 dev_dbg(hspi->dev, "%s\n", __func__);
155
Phil Edworthyce329302012-11-22 14:37:27 +0000156 cs_change = 1;
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700157 ret = 0;
158 list_for_each_entry(t, &msg->transfers, transfer_list) {
Phil Edworthyce329302012-11-22 14:37:27 +0000159
160 if (cs_change) {
161 hspi_hw_setup(hspi, msg, t);
162 hspi_hw_cs_enable(hspi);
163 ndelay(nsecs);
164 }
165 cs_change = t->cs_change;
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700166
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700167 for (i = 0; i < t->len; i++) {
168
169 /* wait remains */
170 ret = hspi_status_check_timeout(hspi, 0x1, 0);
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700171 if (ret < 0)
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700172 break;
173
174 tx = 0;
175 if (t->tx_buf)
176 tx = (u32)((u8 *)t->tx_buf)[i];
177
178 hspi_write(hspi, SPTBR, tx);
179
Geert Uytterhoevenc6c07b42014-01-12 14:03:38 +0100180 /* wait receive */
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700181 ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700182 if (ret < 0)
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700183 break;
184
185 rx = hspi_read(hspi, SPRBR);
186 if (t->rx_buf)
187 ((u8 *)t->rx_buf)[i] = (u8)rx;
188
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700189 }
Kuninori Morimotobb9c5682012-03-14 02:48:25 -0700190
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700191 msg->actual_length += t->len;
Phil Edworthyce329302012-11-22 14:37:27 +0000192
Alexandru Ardeleane74dc5c2019-09-26 13:51:37 +0300193 spi_transfer_delay_exec(t);
Phil Edworthyce329302012-11-22 14:37:27 +0000194
195 if (cs_change) {
196 ndelay(nsecs);
197 hspi_hw_cs_disable(hspi);
198 ndelay(nsecs);
199 }
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700200 }
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700201
202 msg->status = ret;
Phil Edworthyce329302012-11-22 14:37:27 +0000203 if (!cs_change) {
204 ndelay(nsecs);
205 hspi_hw_cs_disable(hspi);
206 }
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100207 spi_finalize_current_message(ctlr);
Kuninori Morimotoec139b62012-03-14 02:47:40 -0700208
209 return ret;
210}
211
Grant Likelyfd4a3192012-12-07 16:57:14 +0000212static int hspi_probe(struct platform_device *pdev)
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800213{
214 struct resource *res;
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100215 struct spi_controller *ctlr;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800216 struct hspi_priv *hspi;
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700217 struct clk *clk;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800218 int ret;
219
220 /* get base addr */
221 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
222 if (!res) {
223 dev_err(&pdev->dev, "invalid resource\n");
224 return -EINVAL;
225 }
226
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100227 ctlr = spi_alloc_master(&pdev->dev, sizeof(*hspi));
228 if (!ctlr)
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800229 return -ENOMEM;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800230
Simon Horman4a4dd7d2014-04-14 10:41:38 +0900231 clk = clk_get(&pdev->dev, NULL);
Cyril Roelandtd3601e52012-12-12 01:24:54 +0100232 if (IS_ERR(clk)) {
Simon Horman4a4dd7d2014-04-14 10:41:38 +0900233 dev_err(&pdev->dev, "couldn't get clock\n");
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700234 ret = -EINVAL;
235 goto error0;
236 }
237
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100238 hspi = spi_controller_get_devdata(ctlr);
Jingoo Han24b5a822013-05-23 19:20:40 +0900239 platform_set_drvdata(pdev, hspi);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800240
241 /* init hspi */
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100242 hspi->ctlr = ctlr;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800243 hspi->dev = &pdev->dev;
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700244 hspi->clk = clk;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800245 hspi->addr = devm_ioremap(hspi->dev,
246 res->start, resource_size(res));
247 if (!hspi->addr) {
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800248 ret = -ENOMEM;
249 goto error1;
250 }
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800251
Kuninori Morimoto268d7642013-10-03 02:15:50 -0700252 pm_runtime_enable(&pdev->dev);
253
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100254 ctlr->bus_num = pdev->id;
255 ctlr->mode_bits = SPI_CPOL | SPI_CPHA;
256 ctlr->dev.of_node = pdev->dev.of_node;
257 ctlr->auto_runtime_pm = true;
258 ctlr->transfer_one_message = hspi_transfer_one_message;
259 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
Axel Lin45221932014-02-12 22:09:52 +0800260
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100261 ret = devm_spi_register_controller(&pdev->dev, ctlr);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800262 if (ret < 0) {
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100263 dev_err(&pdev->dev, "devm_spi_register_controller error.\n");
Geert Uytterhoeven3abf0ed2014-03-11 10:59:10 +0100264 goto error2;
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800265 }
266
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800267 return 0;
268
Geert Uytterhoeven3abf0ed2014-03-11 10:59:10 +0100269 error2:
270 pm_runtime_disable(&pdev->dev);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800271 error1:
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700272 clk_put(clk);
273 error0:
Geert Uytterhoeven5a0e5772019-02-08 10:09:08 +0100274 spi_controller_put(ctlr);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800275
276 return ret;
277}
278
Grant Likelyfd4a3192012-12-07 16:57:14 +0000279static int hspi_remove(struct platform_device *pdev)
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800280{
Jingoo Han24b5a822013-05-23 19:20:40 +0900281 struct hspi_priv *hspi = platform_get_drvdata(pdev);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800282
283 pm_runtime_disable(&pdev->dev);
284
Kuninori Morimoto49e599b82012-03-14 02:48:05 -0700285 clk_put(hspi->clk);
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800286
287 return 0;
288}
289
Jingoo Han8e3489f2014-06-03 21:04:59 +0900290static const struct of_device_id hspi_of_match[] = {
Kuninori Morimotoe5f78252013-10-24 21:53:29 -0700291 { .compatible = "renesas,hspi", },
292 { /* sentinel */ }
293};
294MODULE_DEVICE_TABLE(of, hspi_of_match);
295
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800296static struct platform_driver hspi_driver = {
297 .probe = hspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000298 .remove = hspi_remove,
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800299 .driver = {
300 .name = "sh-hspi",
Kuninori Morimotoe5f78252013-10-24 21:53:29 -0700301 .of_match_table = hspi_of_match,
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800302 },
303};
304module_platform_driver(hspi_driver);
305
306MODULE_DESCRIPTION("SuperH HSPI bus driver");
Wolfram Sang9135bac2018-08-22 00:02:23 +0200307MODULE_LICENSE("GPL v2");
Kuninori Morimotod1c8bbd2012-03-01 17:10:17 -0800308MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
Axel Lincaedb992014-01-08 18:52:40 +0800309MODULE_ALIAS("platform:sh-hspi");