1 --- a/drivers/spi/spi-rb4xx.c
2 +++ b/drivers/spi/spi-rb4xx.c
7 +#include <linux/clk.h>
8 +#include <linux/err.h>
9 #include <linux/kernel.h>
10 +#include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/spinlock.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
18 -#include <asm/mach-ar71xx/ar71xx.h>
19 +#include <asm/mach-ath79/ar71xx_regs.h>
20 +#include <asm/mach-ath79/ath79.h>
22 #define DRV_NAME "rb4xx-spi"
23 #define DRV_DESC "Mikrotik RB4xx SPI controller driver"
24 @@ -41,13 +45,16 @@ struct rb4xx_spi {
25 unsigned spi_ctrl_flash;
26 unsigned spi_ctrl_fread;
28 + struct clk *ahb_clk;
29 + unsigned long ahb_freq;
32 struct list_head queue;
37 -static unsigned spi_clk_low = SPI_IOC_CS1;
38 +static unsigned spi_clk_low = AR71XX_SPI_IOC_CS1;
40 #ifdef RB4XX_SPI_DEBUG
41 static inline void do_spi_delay(void)
42 @@ -60,10 +67,11 @@ static inline void do_spi_delay(void) {
44 static inline void do_spi_init(struct spi_device *spi)
46 - unsigned cs = SPI_IOC_CS0 | SPI_IOC_CS1;
47 + unsigned cs = AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1;
49 if (!(spi->mode & SPI_CS_HIGH))
50 - cs ^= (spi->chip_select == 2) ? SPI_IOC_CS1 : SPI_IOC_CS0;
51 + cs ^= (spi->chip_select == 2) ? AR71XX_SPI_IOC_CS1 :
56 @@ -71,17 +79,18 @@ static inline void do_spi_init(struct sp
57 static inline void do_spi_finish(void __iomem *base)
60 - __raw_writel(SPI_IOC_CS0 | SPI_IOC_CS1, base + SPI_REG_IOC);
61 + __raw_writel(AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1,
62 + base + AR71XX_SPI_REG_IOC);
65 static inline void do_spi_clk(void __iomem *base, int bit)
67 - unsigned bval = spi_clk_low | ((bit & 1) ? SPI_IOC_DO : 0);
68 + unsigned bval = spi_clk_low | ((bit & 1) ? AR71XX_SPI_IOC_DO : 0);
71 - __raw_writel(bval, base + SPI_REG_IOC);
72 + __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
74 - __raw_writel(bval | SPI_IOC_CLK, base + SPI_REG_IOC);
75 + __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
78 static void do_spi_byte(void __iomem *base, unsigned char byte)
79 @@ -97,19 +106,19 @@ static void do_spi_byte(void __iomem *ba
81 pr_debug("spi_byte sent 0x%02x got 0x%02x\n",
83 - (unsigned char)__raw_readl(base + SPI_REG_RDS));
84 + (unsigned char)__raw_readl(base + AR71XX_SPI_REG_RDS));
87 static inline void do_spi_clk_fast(void __iomem *base, unsigned bit1,
90 unsigned bval = (spi_clk_low |
91 - ((bit1 & 1) ? SPI_IOC_DO : 0) |
92 - ((bit2 & 1) ? SPI_IOC_CS2 : 0));
93 + ((bit1 & 1) ? AR71XX_SPI_IOC_DO : 0) |
94 + ((bit2 & 1) ? AR71XX_SPI_IOC_CS2 : 0));
96 - __raw_writel(bval, base + SPI_REG_IOC);
97 + __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
99 - __raw_writel(bval | SPI_IOC_CLK, base + SPI_REG_IOC);
100 + __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
103 static void do_spi_byte_fast(void __iomem *base, unsigned char byte)
104 @@ -121,7 +130,7 @@ static void do_spi_byte_fast(void __iome
106 pr_debug("spi_byte_fast sent 0x%02x got 0x%02x\n",
108 - (unsigned char) __raw_readl(base + SPI_REG_RDS));
109 + (unsigned char) __raw_readl(base + AR71XX_SPI_REG_RDS));
112 static int rb4xx_spi_txrx(void __iomem *base, struct spi_transfer *t)
113 @@ -150,9 +159,9 @@ static int rb4xx_spi_txrx(void __iomem *
114 do_spi_byte(base, sdata);
117 - rx_ptr[i] = __raw_readl(base + SPI_REG_RDS) & 0xff;
118 + rx_ptr[i] = __raw_readl(base + AR71XX_SPI_REG_RDS) & 0xff;
119 } else if (rxv_ptr) {
120 - unsigned char c = __raw_readl(base + SPI_REG_RDS);
121 + unsigned char c = __raw_readl(base + AR71XX_SPI_REG_RDS);
125 @@ -201,9 +210,9 @@ static int rb4xx_spi_read_fast(struct rb
126 if (t->tx_buf && !t->verify)
129 - __raw_writel(SPI_FS_GPIO, base + SPI_REG_FS);
130 - __raw_writel(rbspi->spi_ctrl_fread, base + SPI_REG_CTRL);
131 - __raw_writel(0, base + SPI_REG_FS);
132 + __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
133 + __raw_writel(rbspi->spi_ctrl_fread, base + AR71XX_SPI_REG_CTRL);
134 + __raw_writel(0, base + AR71XX_SPI_REG_FS);
137 memcpy(t->rx_buf, (const void *)addr, t->len);
138 @@ -216,9 +225,9 @@ static int rb4xx_spi_read_fast(struct rb
139 m->actual_length += t->len;
141 if (rbspi->spi_ctrl_flash != rbspi->spi_ctrl_fread) {
142 - __raw_writel(SPI_FS_GPIO, base + SPI_REG_FS);
143 - __raw_writel(rbspi->spi_ctrl_flash, base + SPI_REG_CTRL);
144 - __raw_writel(0, base + SPI_REG_FS);
145 + __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
146 + __raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
147 + __raw_writel(0, base + AR71XX_SPI_REG_FS);
151 @@ -237,8 +246,8 @@ static int rb4xx_spi_msg(struct rb4xx_sp
152 if (rb4xx_spi_read_fast(rbspi, m) == 0)
155 - __raw_writel(SPI_FS_GPIO, base + SPI_REG_FS);
156 - __raw_writel(SPI_CTRL_FASTEST, base + SPI_REG_CTRL);
157 + __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
158 + __raw_writel(SPI_CTRL_FASTEST, base + AR71XX_SPI_REG_CTRL);
161 list_for_each_entry(t, &m->transfers, transfer_list) {
162 @@ -262,8 +271,8 @@ static int rb4xx_spi_msg(struct rb4xx_sp
166 - __raw_writel(rbspi->spi_ctrl_flash, base + SPI_REG_CTRL);
167 - __raw_writel(0, base + SPI_REG_FS);
168 + __raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
169 + __raw_writel(0, base + AR71XX_SPI_REG_FS);
173 @@ -352,11 +361,12 @@ static int rb4xx_spi_setup(struct spi_de
177 -static unsigned get_spi_ctrl(unsigned hz_max, const char *name)
178 +static unsigned get_spi_ctrl(struct rb4xx_spi *rbspi, unsigned hz_max,
183 - div = (ar71xx_ahb_freq - 1) / (2 * hz_max);
184 + div = (rbspi->ahb_freq - 1) / (2 * hz_max);
187 * CPU has a bug at (div == 0) - first bit read is random
188 @@ -365,7 +375,7 @@ static unsigned get_spi_ctrl(unsigned hz
192 - unsigned ahb_khz = (ar71xx_ahb_freq + 500) / 1000;
193 + unsigned ahb_khz = (rbspi->ahb_freq + 500) / 1000;
194 unsigned div_real = 2 * (div + 1);
195 pr_debug("rb4xx: %s SPI clock %u kHz (AHB %u kHz / %u)\n",
197 @@ -396,23 +406,40 @@ static int rb4xx_spi_probe(struct platfo
198 master->transfer = rb4xx_spi_transfer;
200 rbspi = spi_master_get_devdata(master);
202 + rbspi->ahb_clk = clk_get(&pdev->dev, "AHB");
203 + if (IS_ERR(rbspi->ahb_clk)) {
204 + err = PTR_ERR(rbspi->ahb_clk);
205 + goto err_put_master;
208 + err = clk_enable(rbspi->ahb_clk);
212 + rbspi->ahb_freq = clk_get_rate(rbspi->ahb_clk);
213 + if (!rbspi->ahb_freq) {
215 + goto err_clk_disable;
218 platform_set_drvdata(pdev, rbspi);
220 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
223 - goto err_put_master;
224 + goto err_clk_disable;
227 rbspi->base = ioremap(r->start, r->end - r->start + 1);
230 - goto err_put_master;
231 + goto err_clk_disable;
234 rbspi->master = master;
235 - rbspi->spi_ctrl_flash = get_spi_ctrl(SPI_FLASH_HZ, "FLASH");
236 - rbspi->spi_ctrl_fread = get_spi_ctrl(SPI_CPLD_HZ, "CPLD");
237 + rbspi->spi_ctrl_flash = get_spi_ctrl(rbspi, SPI_FLASH_HZ, "FLASH");
238 + rbspi->spi_ctrl_fread = get_spi_ctrl(rbspi, SPI_CPLD_HZ, "CPLD");
241 spin_lock_init(&rbspi->lock);
242 @@ -428,6 +455,10 @@ static int rb4xx_spi_probe(struct platfo
245 iounmap(rbspi->base);
247 + clk_disable(rbspi->ahb_clk);
249 + clk_put(rbspi->ahb_clk);
251 platform_set_drvdata(pdev, NULL);
252 spi_master_put(master);
253 @@ -440,6 +471,8 @@ static int rb4xx_spi_remove(struct platf
254 struct rb4xx_spi *rbspi = platform_get_drvdata(pdev);
256 iounmap(rbspi->base);
257 + clk_disable(rbspi->ahb_clk);
258 + clk_put(rbspi->ahb_clk);
259 platform_set_drvdata(pdev, NULL);
260 spi_master_put(rbspi->master);