Blame view

drivers/spi/spi-ath79.c 6.14 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
2
3
4
5
6
7
8
  /*
   * SPI controller driver for the Atheros AR71XX/AR724X/AR913X SoCs
   *
   * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
   *
   * This driver has been based on the spi-gpio.c:
   *	Copyright (C) 2006,2008 David Brownell
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
9
10
11
   */
  
  #include <linux/kernel.h>
807cc4b12   Gabor Juhos   spi/ath79: fix co...
12
  #include <linux/module.h>
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
13
14
  #include <linux/delay.h>
  #include <linux/spinlock.h>
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
15
16
17
18
19
  #include <linux/platform_device.h>
  #include <linux/io.h>
  #include <linux/spi/spi.h>
  #include <linux/spi/spi_bitbang.h>
  #include <linux/bitops.h>
440114fdb   Gabor Juhos   spi/ath79: add de...
20
21
  #include <linux/clk.h>
  #include <linux/err.h>
b172fd0c8   Alban Bedel   spi: ath79: Enabl...
22
  #include <linux/platform_data/spi-ath79.h>
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
23
24
  
  #define DRV_NAME	"ath79-spi"
440114fdb   Gabor Juhos   spi/ath79: add de...
25
26
  #define ATH79_SPI_RRW_DELAY_FACTOR	12000
  #define MHZ				(1000 * 1000)
b172fd0c8   Alban Bedel   spi: ath79: Enabl...
27
28
29
30
31
32
33
34
35
36
  #define AR71XX_SPI_REG_FS		0x00	/* Function Select */
  #define AR71XX_SPI_REG_CTRL		0x04	/* SPI Control */
  #define AR71XX_SPI_REG_IOC		0x08	/* SPI I/O Control */
  #define AR71XX_SPI_REG_RDS		0x0c	/* Read Data Shift */
  
  #define AR71XX_SPI_FS_GPIO		BIT(0)	/* Enable GPIO mode */
  
  #define AR71XX_SPI_IOC_DO		BIT(0)	/* Data Out pin */
  #define AR71XX_SPI_IOC_CLK		BIT(8)	/* CLK pin */
  #define AR71XX_SPI_IOC_CS(n)		BIT(16 + (n))
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
37
38
39
40
41
  struct ath79_spi {
  	struct spi_bitbang	bitbang;
  	u32			ioc_base;
  	u32			reg_ctrl;
  	void __iomem		*base;
440114fdb   Gabor Juhos   spi/ath79: add de...
42
  	struct clk		*clk;
da470d6ab   Aravind Thokala   spi/ath79: Fix ch...
43
  	unsigned int		rrw_delay;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
44
  };
da470d6ab   Aravind Thokala   spi/ath79: Fix ch...
45
  static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned int reg)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
46
47
48
  {
  	return ioread32(sp->base + reg);
  }
da470d6ab   Aravind Thokala   spi/ath79: Fix ch...
49
  static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned int reg, u32 val)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
50
51
52
53
54
55
56
57
  {
  	iowrite32(val, sp->base + reg);
  }
  
  static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
  {
  	return spi_master_get_devdata(spi->master);
  }
da470d6ab   Aravind Thokala   spi/ath79: Fix ch...
58
  static inline void ath79_spi_delay(struct ath79_spi *sp, unsigned int nsecs)
440114fdb   Gabor Juhos   spi/ath79: add de...
59
60
61
62
  {
  	if (nsecs > sp->rrw_delay)
  		ndelay(nsecs - sp->rrw_delay);
  }
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
63
64
65
66
  static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
  {
  	struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  	int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
797622d7a   Alban Bedel   spi: ath79: Simpl...
67
  	u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
68

797622d7a   Alban Bedel   spi: ath79: Simpl...
69
70
71
72
  	if (cs_high)
  		sp->ioc_base |= cs_bit;
  	else
  		sp->ioc_base &= ~cs_bit;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
73

797622d7a   Alban Bedel   spi: ath79: Simpl...
74
  	ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
75
  }
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
76
  static void ath79_spi_enable(struct ath79_spi *sp)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
77
  {
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
78
79
80
81
82
83
  	/* enable GPIO mode */
  	ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  
  	/* save CTRL register */
  	sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
  	sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
797622d7a   Alban Bedel   spi: ath79: Simpl...
84
85
  	/* clear clk and mosi in the base state */
  	sp->ioc_base &= ~(AR71XX_SPI_IOC_DO | AR71XX_SPI_IOC_CLK);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
86
87
  	/* TODO: setup speed? */
  	ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
88
89
90
91
92
93
94
95
96
  }
  
  static void ath79_spi_disable(struct ath79_spi *sp)
  {
  	/* restore CTRL register */
  	ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
  	/* disable GPIO mode */
  	ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
  }
da470d6ab   Aravind Thokala   spi/ath79: Fix ch...
97
  static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned int nsecs,
304d34360   Lorenzo Bianconi   spi: add flags pa...
98
  			       u32 word, u8 bits, unsigned flags)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  {
  	struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  	u32 ioc = sp->ioc_base;
  
  	/* clock starts at inactive polarity */
  	for (word <<= (32 - bits); likely(bits); bits--) {
  		u32 out;
  
  		if (word & (1 << 31))
  			out = ioc | AR71XX_SPI_IOC_DO;
  		else
  			out = ioc & ~AR71XX_SPI_IOC_DO;
  
  		/* setup MSB (to slave) on trailing edge */
  		ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
440114fdb   Gabor Juhos   spi/ath79: add de...
114
  		ath79_spi_delay(sp, nsecs);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
115
  		ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
440114fdb   Gabor Juhos   spi/ath79: add de...
116
  		ath79_spi_delay(sp, nsecs);
72611db0e   Gabor Juhos   spi/ath79: add mi...
117
118
  		if (bits == 1)
  			ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
119
120
121
122
123
124
  
  		word <<= 1;
  	}
  
  	return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
125
  static int ath79_spi_probe(struct platform_device *pdev)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
126
127
128
129
  {
  	struct spi_master *master;
  	struct ath79_spi *sp;
  	struct ath79_spi_platform_data *pdata;
440114fdb   Gabor Juhos   spi/ath79: add de...
130
  	unsigned long rate;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
131
132
133
134
135
136
137
138
139
140
  	int ret;
  
  	master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  	if (master == NULL) {
  		dev_err(&pdev->dev, "failed to allocate spi master
  ");
  		return -ENOMEM;
  	}
  
  	sp = spi_master_get_devdata(master);
85f62476f   Alban Bedel   spi: spi-ath79: A...
141
  	master->dev.of_node = pdev->dev.of_node;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
142
  	platform_set_drvdata(pdev, sp);
8074cf063   Jingoo Han   spi: use dev_get_...
143
  	pdata = dev_get_platdata(&pdev->dev);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
144

8db79547e   Linus Walleij   spi: ath79: Conve...
145
  	master->use_gpio_descriptors = true;
24778be20   Stephen Warren   spi: convert driv...
146
  	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
f1b2c1c84   Alban Bedel   spi: ath79: Remov...
147
148
  	master->setup = spi_bitbang_setup;
  	master->cleanup = spi_bitbang_cleanup;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
149
150
151
  	if (pdata) {
  		master->bus_num = pdata->bus_num;
  		master->num_chipselect = pdata->num_chipselect;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
152
  	}
94c69f765   Axel Lin   spi: bitbang: Let...
153
  	sp->bitbang.master = master;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
154
155
  	sp->bitbang.chipselect = ath79_spi_chipselect;
  	sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
156
  	sp->bitbang.flags = SPI_CS_HIGH;
bf3484190   YueHaibing   spi: ath79: use d...
157
  	sp->base = devm_platform_ioremap_resource(pdev, 0);
b7a2a1c0b   Heiner Kallweit   spi: ath79: simpl...
158
159
  	if (IS_ERR(sp->base)) {
  		ret = PTR_ERR(sp->base);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
160
161
  		goto err_put_master;
  	}
a6f4c8e06   Jingoo Han   spi: ath79: Use d...
162
  	sp->clk = devm_clk_get(&pdev->dev, "ahb");
440114fdb   Gabor Juhos   spi/ath79: add de...
163
164
  	if (IS_ERR(sp->clk)) {
  		ret = PTR_ERR(sp->clk);
a6f4c8e06   Jingoo Han   spi: ath79: Use d...
165
  		goto err_put_master;
440114fdb   Gabor Juhos   spi/ath79: add de...
166
  	}
3e19acdc5   Alban Bedel   spi: spi-ath79: U...
167
  	ret = clk_prepare_enable(sp->clk);
440114fdb   Gabor Juhos   spi/ath79: add de...
168
  	if (ret)
a6f4c8e06   Jingoo Han   spi: ath79: Use d...
169
  		goto err_put_master;
440114fdb   Gabor Juhos   spi/ath79: add de...
170
171
172
173
174
175
176
177
178
179
180
  
  	rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
  	if (!rate) {
  		ret = -EINVAL;
  		goto err_clk_disable;
  	}
  
  	sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
  	dev_dbg(&pdev->dev, "register read/write delay is %u nsecs
  ",
  		sp->rrw_delay);
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
181
  	ath79_spi_enable(sp);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
182
183
  	ret = spi_bitbang_start(&sp->bitbang);
  	if (ret)
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
184
  		goto err_disable;
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
185
186
  
  	return 0;
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
187
188
  err_disable:
  	ath79_spi_disable(sp);
440114fdb   Gabor Juhos   spi/ath79: add de...
189
  err_clk_disable:
3e19acdc5   Alban Bedel   spi: spi-ath79: U...
190
  	clk_disable_unprepare(sp->clk);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
191
  err_put_master:
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
192
193
194
195
  	spi_master_put(sp->bitbang.master);
  
  	return ret;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
196
  static int ath79_spi_remove(struct platform_device *pdev)
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
197
198
199
200
  {
  	struct ath79_spi *sp = platform_get_drvdata(pdev);
  
  	spi_bitbang_stop(&sp->bitbang);
c4a31f430   Gabor Juhos   spi/ath79: avoid ...
201
  	ath79_spi_disable(sp);
3e19acdc5   Alban Bedel   spi: spi-ath79: U...
202
  	clk_disable_unprepare(sp->clk);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
203
204
205
206
  	spi_master_put(sp->bitbang.master);
  
  	return 0;
  }
7410e8485   Gabor Juhos   spi/ath79: add sh...
207
208
209
210
  static void ath79_spi_shutdown(struct platform_device *pdev)
  {
  	ath79_spi_remove(pdev);
  }
85f62476f   Alban Bedel   spi: spi-ath79: A...
211
212
213
214
  static const struct of_device_id ath79_spi_of_match[] = {
  	{ .compatible = "qca,ar7100-spi", },
  	{ },
  };
d7a32394e   Javier Martinez Canillas   spi: ath79: Fix m...
215
  MODULE_DEVICE_TABLE(of, ath79_spi_of_match);
85f62476f   Alban Bedel   spi: spi-ath79: A...
216

8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
217
218
  static struct platform_driver ath79_spi_driver = {
  	.probe		= ath79_spi_probe,
fd4a319bc   Grant Likely   spi: Remove HOTPL...
219
  	.remove		= ath79_spi_remove,
7410e8485   Gabor Juhos   spi/ath79: add sh...
220
  	.shutdown	= ath79_spi_shutdown,
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
221
222
  	.driver		= {
  		.name	= DRV_NAME,
85f62476f   Alban Bedel   spi: spi-ath79: A...
223
  		.of_match_table = ath79_spi_of_match,
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
224
225
  	},
  };
940ab8896   Grant Likely   drivercore: Add h...
226
  module_platform_driver(ath79_spi_driver);
8efaef4dc   Gabor Juhos   SPI: Add SPI cont...
227
228
229
230
231
  
  MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
  MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  MODULE_LICENSE("GPL v2");
  MODULE_ALIAS("platform:" DRV_NAME);