Merge branch 'spi-5.1' into spi-5.2
[linux-2.6-microblaze.git] / drivers / spi / spi-mt7621.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // spi-mt7621.c -- MediaTek MT7621 SPI controller driver
4 //
5 // Copyright (C) 2011 Sergiy <piratfm@gmail.com>
6 // Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
7 // Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
8 //
9 // Some parts are based on spi-orion.c:
10 //   Author: Shadi Ammouri <shadi@marvell.com>
11 //   Copyright (C) 2007-2008 Marvell Ltd.
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/reset.h>
19 #include <linux/spi/spi.h>
20
21 #define DRIVER_NAME             "spi-mt7621"
22
23 /* in usec */
24 #define RALINK_SPI_WAIT_MAX_LOOP 2000
25
26 /* SPISTAT register bit field */
27 #define SPISTAT_BUSY            BIT(0)
28
29 #define MT7621_SPI_TRANS        0x00
30 #define SPITRANS_BUSY           BIT(16)
31
32 #define MT7621_SPI_OPCODE       0x04
33 #define MT7621_SPI_DATA0        0x08
34 #define MT7621_SPI_DATA4        0x18
35 #define SPI_CTL_TX_RX_CNT_MASK  0xff
36 #define SPI_CTL_START           BIT(8)
37
38 #define MT7621_SPI_MASTER       0x28
39 #define MASTER_MORE_BUFMODE     BIT(2)
40 #define MASTER_FULL_DUPLEX      BIT(10)
41 #define MASTER_RS_CLK_SEL       GENMASK(27, 16)
42 #define MASTER_RS_CLK_SEL_SHIFT 16
43 #define MASTER_RS_SLAVE_SEL     GENMASK(31, 29)
44
45 #define MT7621_SPI_MOREBUF      0x2c
46 #define MT7621_SPI_POLAR        0x38
47 #define MT7621_SPI_SPACE        0x3c
48
49 #define MT7621_CPHA             BIT(5)
50 #define MT7621_CPOL             BIT(4)
51 #define MT7621_LSB_FIRST        BIT(3)
52
53 struct mt7621_spi {
54         struct spi_controller   *master;
55         void __iomem            *base;
56         unsigned int            sys_freq;
57         unsigned int            speed;
58         struct clk              *clk;
59         int                     pending_write;
60
61         struct mt7621_spi_ops   *ops;
62 };
63
64 static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
65 {
66         return spi_controller_get_devdata(spi->master);
67 }
68
69 static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
70 {
71         return ioread32(rs->base + reg);
72 }
73
74 static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
75 {
76         iowrite32(val, rs->base + reg);
77 }
78
79 static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
80 {
81         struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
82         int cs = spi->chip_select;
83         u32 polar = 0;
84         u32 master;
85
86         /*
87          * Select SPI device 7, enable "more buffer mode" and disable
88          * full-duplex (only half-duplex really works on this chip
89          * reliably)
90          */
91         master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
92         master |= MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE;
93         master &= ~MASTER_FULL_DUPLEX;
94         mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
95
96         rs->pending_write = 0;
97
98         if (enable)
99                 polar = BIT(cs);
100         mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
101 }
102
103 static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
104 {
105         struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
106         u32 rate;
107         u32 reg;
108
109         dev_dbg(&spi->dev, "speed:%u\n", speed);
110
111         rate = DIV_ROUND_UP(rs->sys_freq, speed);
112         dev_dbg(&spi->dev, "rate-1:%u\n", rate);
113
114         if (rate > 4097)
115                 return -EINVAL;
116
117         if (rate < 2)
118                 rate = 2;
119
120         reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
121         reg &= ~MASTER_RS_CLK_SEL;
122         reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
123         rs->speed = speed;
124
125         reg &= ~MT7621_LSB_FIRST;
126         if (spi->mode & SPI_LSB_FIRST)
127                 reg |= MT7621_LSB_FIRST;
128
129         /*
130          * This SPI controller seems to be tested on SPI flash only and some
131          * bits are swizzled under other SPI modes probably due to incorrect
132          * wiring inside the silicon. Only mode 0 works correctly.
133          */
134         reg &= ~(MT7621_CPHA | MT7621_CPOL);
135
136         mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
137
138         return 0;
139 }
140
141 static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
142 {
143         int i;
144
145         for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
146                 u32 status;
147
148                 status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
149                 if ((status & SPITRANS_BUSY) == 0)
150                         return 0;
151                 cpu_relax();
152                 udelay(1);
153         }
154
155         return -ETIMEDOUT;
156 }
157
158 static void mt7621_spi_read_half_duplex(struct mt7621_spi *rs,
159                                         int rx_len, u8 *buf)
160 {
161         int tx_len;
162
163         /*
164          * Combine with any pending write, and perform one or more half-duplex
165          * transactions reading 'len' bytes. Data to be written is already in
166          * MT7621_SPI_DATA.
167          */
168         tx_len = rs->pending_write;
169         rs->pending_write = 0;
170
171         while (rx_len || tx_len) {
172                 int i;
173                 u32 val = (min(tx_len, 4) * 8) << 24;
174                 int rx = min(rx_len, 32);
175
176                 if (tx_len > 4)
177                         val |= (tx_len - 4) * 8;
178                 val |= (rx * 8) << 12;
179                 mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
180
181                 tx_len = 0;
182
183                 val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
184                 val |= SPI_CTL_START;
185                 mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
186
187                 mt7621_spi_wait_till_ready(rs);
188
189                 for (i = 0; i < rx; i++) {
190                         if ((i % 4) == 0)
191                                 val = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
192                         *buf++ = val & 0xff;
193                         val >>= 8;
194                 }
195
196                 rx_len -= i;
197         }
198 }
199
200 static inline void mt7621_spi_flush(struct mt7621_spi *rs)
201 {
202         mt7621_spi_read_half_duplex(rs, 0, NULL);
203 }
204
205 static void mt7621_spi_write_half_duplex(struct mt7621_spi *rs,
206                                          int tx_len, const u8 *buf)
207 {
208         int len = rs->pending_write;
209         int val = 0;
210
211         if (len & 3) {
212                 val = mt7621_spi_read(rs, MT7621_SPI_OPCODE + (len & ~3));
213                 if (len < 4) {
214                         val <<= (4 - len) * 8;
215                         val = swab32(val);
216                 }
217         }
218
219         while (tx_len > 0) {
220                 if (len >= 36) {
221                         rs->pending_write = len;
222                         mt7621_spi_flush(rs);
223                         len = 0;
224                 }
225
226                 val |= *buf++ << (8 * (len & 3));
227                 len++;
228                 if ((len & 3) == 0) {
229                         if (len == 4)
230                                 /* The byte-order of the opcode is weird! */
231                                 val = swab32(val);
232                         mt7621_spi_write(rs, MT7621_SPI_OPCODE + len - 4, val);
233                         val = 0;
234                 }
235                 tx_len -= 1;
236         }
237
238         if (len & 3) {
239                 if (len < 4) {
240                         val = swab32(val);
241                         val >>= (4 - len) * 8;
242                 }
243                 mt7621_spi_write(rs, MT7621_SPI_OPCODE + (len & ~3), val);
244         }
245
246         rs->pending_write = len;
247 }
248
249 static int mt7621_spi_transfer_one_message(struct spi_controller *master,
250                                            struct spi_message *m)
251 {
252         struct mt7621_spi *rs = spi_controller_get_devdata(master);
253         struct spi_device *spi = m->spi;
254         unsigned int speed = spi->max_speed_hz;
255         struct spi_transfer *t = NULL;
256         int status = 0;
257
258         mt7621_spi_wait_till_ready(rs);
259
260         list_for_each_entry(t, &m->transfers, transfer_list)
261                 if (t->speed_hz < speed)
262                         speed = t->speed_hz;
263
264         if (mt7621_spi_prepare(spi, speed)) {
265                 status = -EIO;
266                 goto msg_done;
267         }
268
269         /* Assert CS */
270         mt7621_spi_set_cs(spi, 1);
271
272         m->actual_length = 0;
273         list_for_each_entry(t, &m->transfers, transfer_list) {
274                 if ((t->rx_buf) && (t->tx_buf)) {
275                         /*
276                          * This controller will shift some extra data out
277                          * of spi_opcode if (mosi_bit_cnt > 0) &&
278                          * (cmd_bit_cnt == 0). So the claimed full-duplex
279                          * support is broken since we have no way to read
280                          * the MISO value during that bit.
281                          */
282                         status = -EIO;
283                         goto msg_done;
284                 } else if (t->rx_buf) {
285                         mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf);
286                 } else if (t->tx_buf) {
287                         mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf);
288                 }
289                 m->actual_length += t->len;
290         }
291
292         /* Flush data and deassert CS */
293         mt7621_spi_flush(rs);
294         mt7621_spi_set_cs(spi, 0);
295
296 msg_done:
297         m->status = status;
298         spi_finalize_current_message(master);
299
300         return 0;
301 }
302
303 static int mt7621_spi_setup(struct spi_device *spi)
304 {
305         struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
306
307         if ((spi->max_speed_hz == 0) ||
308             (spi->max_speed_hz > (rs->sys_freq / 2)))
309                 spi->max_speed_hz = (rs->sys_freq / 2);
310
311         if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
312                 dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
313                         spi->max_speed_hz);
314                 return -EINVAL;
315         }
316
317         return 0;
318 }
319
320 static const struct of_device_id mt7621_spi_match[] = {
321         { .compatible = "ralink,mt7621-spi" },
322         {},
323 };
324 MODULE_DEVICE_TABLE(of, mt7621_spi_match);
325
326 static int mt7621_spi_probe(struct platform_device *pdev)
327 {
328         const struct of_device_id *match;
329         struct spi_controller *master;
330         struct mt7621_spi *rs;
331         void __iomem *base;
332         struct resource *r;
333         int status = 0;
334         struct clk *clk;
335         struct mt7621_spi_ops *ops;
336         int ret;
337
338         match = of_match_device(mt7621_spi_match, &pdev->dev);
339         if (!match)
340                 return -EINVAL;
341         ops = (struct mt7621_spi_ops *)match->data;
342
343         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
344         base = devm_ioremap_resource(&pdev->dev, r);
345         if (IS_ERR(base))
346                 return PTR_ERR(base);
347
348         clk = devm_clk_get(&pdev->dev, NULL);
349         if (IS_ERR(clk)) {
350                 dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
351                         status);
352                 return PTR_ERR(clk);
353         }
354
355         status = clk_prepare_enable(clk);
356         if (status)
357                 return status;
358
359         master = spi_alloc_master(&pdev->dev, sizeof(*rs));
360         if (!master) {
361                 dev_info(&pdev->dev, "master allocation failed\n");
362                 return -ENOMEM;
363         }
364
365         master->mode_bits = SPI_LSB_FIRST;
366         master->flags = SPI_CONTROLLER_HALF_DUPLEX;
367         master->setup = mt7621_spi_setup;
368         master->transfer_one_message = mt7621_spi_transfer_one_message;
369         master->bits_per_word_mask = SPI_BPW_MASK(8);
370         master->dev.of_node = pdev->dev.of_node;
371         master->num_chipselect = 2;
372
373         dev_set_drvdata(&pdev->dev, master);
374
375         rs = spi_controller_get_devdata(master);
376         rs->base = base;
377         rs->clk = clk;
378         rs->master = master;
379         rs->sys_freq = clk_get_rate(rs->clk);
380         rs->ops = ops;
381         rs->pending_write = 0;
382         dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
383
384         ret = device_reset(&pdev->dev);
385         if (ret) {
386                 dev_err(&pdev->dev, "SPI reset failed!\n");
387                 return ret;
388         }
389
390         return devm_spi_register_controller(&pdev->dev, master);
391 }
392
393 static int mt7621_spi_remove(struct platform_device *pdev)
394 {
395         struct spi_controller *master;
396         struct mt7621_spi *rs;
397
398         master = dev_get_drvdata(&pdev->dev);
399         rs = spi_controller_get_devdata(master);
400
401         clk_disable_unprepare(rs->clk);
402
403         return 0;
404 }
405
406 MODULE_ALIAS("platform:" DRIVER_NAME);
407
408 static struct platform_driver mt7621_spi_driver = {
409         .driver = {
410                 .name = DRIVER_NAME,
411                 .of_match_table = mt7621_spi_match,
412         },
413         .probe = mt7621_spi_probe,
414         .remove = mt7621_spi_remove,
415 };
416
417 module_platform_driver(mt7621_spi_driver);
418
419 MODULE_DESCRIPTION("MT7621 SPI driver");
420 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
421 MODULE_LICENSE("GPL");