spi: sprd: Pass offset instead of physical address to adi_read/_write()
[linux-2.6-microblaze.git] / drivers / spi / spi-sprd-adi.c
1 /*
2  * Copyright (C) 2017 Spreadtrum Communications Inc.
3  *
4  * SPDX-License-Identifier: GPL-2.0
5  */
6
7 #include <linux/delay.h>
8 #include <linux/hwspinlock.h>
9 #include <linux/init.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/reboot.h>
17 #include <linux/spi/spi.h>
18 #include <linux/sizes.h>
19
20 /* Registers definitions for ADI controller */
21 #define REG_ADI_CTRL0                   0x4
22 #define REG_ADI_CHN_PRIL                0x8
23 #define REG_ADI_CHN_PRIH                0xc
24 #define REG_ADI_INT_EN                  0x10
25 #define REG_ADI_INT_RAW                 0x14
26 #define REG_ADI_INT_MASK                0x18
27 #define REG_ADI_INT_CLR                 0x1c
28 #define REG_ADI_GSSI_CFG0               0x20
29 #define REG_ADI_GSSI_CFG1               0x24
30 #define REG_ADI_RD_CMD                  0x28
31 #define REG_ADI_RD_DATA                 0x2c
32 #define REG_ADI_ARM_FIFO_STS            0x30
33 #define REG_ADI_STS                     0x34
34 #define REG_ADI_EVT_FIFO_STS            0x38
35 #define REG_ADI_ARM_CMD_STS             0x3c
36 #define REG_ADI_CHN_EN                  0x40
37 #define REG_ADI_CHN_ADDR(id)            (0x44 + (id - 2) * 4)
38 #define REG_ADI_CHN_EN1                 0x20c
39
40 /* Bits definitions for register REG_ADI_GSSI_CFG0 */
41 #define BIT_CLK_ALL_ON                  BIT(30)
42
43 /* Bits definitions for register REG_ADI_RD_DATA */
44 #define BIT_RD_CMD_BUSY                 BIT(31)
45 #define RD_ADDR_SHIFT                   16
46 #define RD_VALUE_MASK                   GENMASK(15, 0)
47 #define RD_ADDR_MASK                    GENMASK(30, 16)
48
49 /* Bits definitions for register REG_ADI_ARM_FIFO_STS */
50 #define BIT_FIFO_FULL                   BIT(11)
51 #define BIT_FIFO_EMPTY                  BIT(10)
52
53 /*
54  * ADI slave devices include RTC, ADC, regulator, charger, thermal and so on.
55  * The slave devices address offset is always 0x8000 and size is 4K.
56  */
57 #define ADI_SLAVE_ADDR_SIZE             SZ_4K
58 #define ADI_SLAVE_OFFSET                0x8000
59
60 /* Timeout (ms) for the trylock of hardware spinlocks */
61 #define ADI_HWSPINLOCK_TIMEOUT          5000
62 /*
63  * ADI controller has 50 channels including 2 software channels
64  * and 48 hardware channels.
65  */
66 #define ADI_HW_CHNS                     50
67
68 #define ADI_FIFO_DRAIN_TIMEOUT          1000
69 #define ADI_READ_TIMEOUT                2000
70 #define REG_ADDR_LOW_MASK               GENMASK(11, 0)
71
72 /* Registers definitions for PMIC watchdog controller */
73 #define REG_WDG_LOAD_LOW                0x80
74 #define REG_WDG_LOAD_HIGH               0x84
75 #define REG_WDG_CTRL                    0x88
76 #define REG_WDG_LOCK                    0xa0
77
78 /* Bits definitions for register REG_WDG_CTRL */
79 #define BIT_WDG_RUN                     BIT(1)
80 #define BIT_WDG_NEW                     BIT(2)
81 #define BIT_WDG_RST                     BIT(3)
82
83 /* Registers definitions for PMIC */
84 #define PMIC_RST_STATUS                 0xee8
85 #define PMIC_MODULE_EN                  0xc08
86 #define PMIC_CLK_EN                     0xc18
87 #define BIT_WDG_EN                      BIT(2)
88
89 /* Definition of PMIC reset status register */
90 #define HWRST_STATUS_SECURITY           0x02
91 #define HWRST_STATUS_RECOVERY           0x20
92 #define HWRST_STATUS_NORMAL             0x40
93 #define HWRST_STATUS_ALARM              0x50
94 #define HWRST_STATUS_SLEEP              0x60
95 #define HWRST_STATUS_FASTBOOT           0x30
96 #define HWRST_STATUS_SPECIAL            0x70
97 #define HWRST_STATUS_PANIC              0x80
98 #define HWRST_STATUS_CFTREBOOT          0x90
99 #define HWRST_STATUS_AUTODLOADER        0xa0
100 #define HWRST_STATUS_IQMODE             0xb0
101 #define HWRST_STATUS_SPRDISK            0xc0
102 #define HWRST_STATUS_FACTORYTEST        0xe0
103 #define HWRST_STATUS_WATCHDOG           0xf0
104
105 /* Use default timeout 50 ms that converts to watchdog values */
106 #define WDG_LOAD_VAL                    ((50 * 1000) / 32768)
107 #define WDG_LOAD_MASK                   GENMASK(15, 0)
108 #define WDG_UNLOCK_KEY                  0xe551
109
110 struct sprd_adi {
111         struct spi_controller   *ctlr;
112         struct device           *dev;
113         void __iomem            *base;
114         struct hwspinlock       *hwlock;
115         unsigned long           slave_vbase;
116         unsigned long           slave_pbase;
117         struct notifier_block   restart_handler;
118 };
119
120 static int sprd_adi_check_addr(struct sprd_adi *sadi, u32 reg)
121 {
122         if (reg > ADI_SLAVE_ADDR_SIZE) {
123                 dev_err(sadi->dev,
124                         "slave address offset is incorrect, reg = 0x%x\n",
125                         reg);
126                 return -EINVAL;
127         }
128
129         return 0;
130 }
131
132 static int sprd_adi_drain_fifo(struct sprd_adi *sadi)
133 {
134         u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
135         u32 sts;
136
137         do {
138                 sts = readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS);
139                 if (sts & BIT_FIFO_EMPTY)
140                         break;
141
142                 cpu_relax();
143         } while (--timeout);
144
145         if (timeout == 0) {
146                 dev_err(sadi->dev, "drain write fifo timeout\n");
147                 return -EBUSY;
148         }
149
150         return 0;
151 }
152
153 static int sprd_adi_fifo_is_full(struct sprd_adi *sadi)
154 {
155         return readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS) & BIT_FIFO_FULL;
156 }
157
158 static int sprd_adi_read(struct sprd_adi *sadi, u32 reg, u32 *read_val)
159 {
160         int read_timeout = ADI_READ_TIMEOUT;
161         unsigned long flags;
162         u32 val, rd_addr, paddr;
163         int ret = 0;
164
165         if (sadi->hwlock) {
166                 ret = hwspin_lock_timeout_irqsave(sadi->hwlock,
167                                                   ADI_HWSPINLOCK_TIMEOUT,
168                                                   &flags);
169                 if (ret) {
170                         dev_err(sadi->dev, "get the hw lock failed\n");
171                         return ret;
172                 }
173         }
174
175         ret = sprd_adi_check_addr(sadi, reg);
176         if (ret)
177                 goto out;
178
179         /*
180          * Set the physical register address need to read into RD_CMD register,
181          * then ADI controller will start to transfer automatically.
182          */
183         paddr = sadi->slave_pbase + reg;
184         writel_relaxed(paddr, sadi->base + REG_ADI_RD_CMD);
185
186         /*
187          * Wait read operation complete, the BIT_RD_CMD_BUSY will be set
188          * simultaneously when writing read command to register, and the
189          * BIT_RD_CMD_BUSY will be cleared after the read operation is
190          * completed.
191          */
192         do {
193                 val = readl_relaxed(sadi->base + REG_ADI_RD_DATA);
194                 if (!(val & BIT_RD_CMD_BUSY))
195                         break;
196
197                 cpu_relax();
198         } while (--read_timeout);
199
200         if (read_timeout == 0) {
201                 dev_err(sadi->dev, "ADI read timeout\n");
202                 ret = -EBUSY;
203                 goto out;
204         }
205
206         /*
207          * The return value includes data and read register address, from bit 0
208          * to bit 15 are data, and from bit 16 to bit 30 are read register
209          * address. Then we can check the returned register address to validate
210          * data.
211          */
212         rd_addr = (val & RD_ADDR_MASK) >> RD_ADDR_SHIFT;
213
214         if (rd_addr != (paddr & REG_ADDR_LOW_MASK)) {
215                 dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x\n",
216                         paddr, val);
217                 ret = -EIO;
218                 goto out;
219         }
220
221         *read_val = val & RD_VALUE_MASK;
222
223 out:
224         if (sadi->hwlock)
225                 hwspin_unlock_irqrestore(sadi->hwlock, &flags);
226         return ret;
227 }
228
229 static int sprd_adi_write(struct sprd_adi *sadi, u32 reg, u32 val)
230 {
231         u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
232         unsigned long flags;
233         int ret;
234
235         if (sadi->hwlock) {
236                 ret = hwspin_lock_timeout_irqsave(sadi->hwlock,
237                                                   ADI_HWSPINLOCK_TIMEOUT,
238                                                   &flags);
239                 if (ret) {
240                         dev_err(sadi->dev, "get the hw lock failed\n");
241                         return ret;
242                 }
243         }
244
245         ret = sprd_adi_check_addr(sadi, reg);
246         if (ret)
247                 goto out;
248
249         ret = sprd_adi_drain_fifo(sadi);
250         if (ret < 0)
251                 goto out;
252
253         /*
254          * we should wait for write fifo is empty before writing data to PMIC
255          * registers.
256          */
257         do {
258                 if (!sprd_adi_fifo_is_full(sadi)) {
259                         /* we need virtual register address to write. */
260                         writel_relaxed(val, (void __iomem *)(sadi->slave_vbase + reg));
261                         break;
262                 }
263
264                 cpu_relax();
265         } while (--timeout);
266
267         if (timeout == 0) {
268                 dev_err(sadi->dev, "write fifo is full\n");
269                 ret = -EBUSY;
270         }
271
272 out:
273         if (sadi->hwlock)
274                 hwspin_unlock_irqrestore(sadi->hwlock, &flags);
275         return ret;
276 }
277
278 static int sprd_adi_transfer_one(struct spi_controller *ctlr,
279                                  struct spi_device *spi_dev,
280                                  struct spi_transfer *t)
281 {
282         struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
283         u32 reg, val;
284         int ret;
285
286         if (t->rx_buf) {
287                 reg = *(u32 *)t->rx_buf;
288                 ret = sprd_adi_read(sadi, reg, &val);
289                 *(u32 *)t->rx_buf = val;
290         } else if (t->tx_buf) {
291                 u32 *p = (u32 *)t->tx_buf;
292                 reg = *p++;
293                 val = *p;
294                 ret = sprd_adi_write(sadi, reg, val);
295         } else {
296                 dev_err(sadi->dev, "no buffer for transfer\n");
297                 ret = -EINVAL;
298         }
299
300         return ret;
301 }
302
303 static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi)
304 {
305 #if IS_ENABLED(CONFIG_SPRD_WATCHDOG)
306         u32 val;
307
308         /* Set default watchdog reboot mode */
309         sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
310         val |= HWRST_STATUS_WATCHDOG;
311         sprd_adi_write(sadi, PMIC_RST_STATUS, val);
312 #endif
313 }
314
315 static int sprd_adi_restart_handler(struct notifier_block *this,
316                                     unsigned long mode, void *cmd)
317 {
318         struct sprd_adi *sadi = container_of(this, struct sprd_adi,
319                                              restart_handler);
320         u32 val, reboot_mode = 0;
321
322         if (!cmd)
323                 reboot_mode = HWRST_STATUS_NORMAL;
324         else if (!strncmp(cmd, "recovery", 8))
325                 reboot_mode = HWRST_STATUS_RECOVERY;
326         else if (!strncmp(cmd, "alarm", 5))
327                 reboot_mode = HWRST_STATUS_ALARM;
328         else if (!strncmp(cmd, "fastsleep", 9))
329                 reboot_mode = HWRST_STATUS_SLEEP;
330         else if (!strncmp(cmd, "bootloader", 10))
331                 reboot_mode = HWRST_STATUS_FASTBOOT;
332         else if (!strncmp(cmd, "panic", 5))
333                 reboot_mode = HWRST_STATUS_PANIC;
334         else if (!strncmp(cmd, "special", 7))
335                 reboot_mode = HWRST_STATUS_SPECIAL;
336         else if (!strncmp(cmd, "cftreboot", 9))
337                 reboot_mode = HWRST_STATUS_CFTREBOOT;
338         else if (!strncmp(cmd, "autodloader", 11))
339                 reboot_mode = HWRST_STATUS_AUTODLOADER;
340         else if (!strncmp(cmd, "iqmode", 6))
341                 reboot_mode = HWRST_STATUS_IQMODE;
342         else if (!strncmp(cmd, "sprdisk", 7))
343                 reboot_mode = HWRST_STATUS_SPRDISK;
344         else if (!strncmp(cmd, "tospanic", 8))
345                 reboot_mode = HWRST_STATUS_SECURITY;
346         else if (!strncmp(cmd, "factorytest", 11))
347                 reboot_mode = HWRST_STATUS_FACTORYTEST;
348         else
349                 reboot_mode = HWRST_STATUS_NORMAL;
350
351         /* Record the reboot mode */
352         sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
353         val &= ~HWRST_STATUS_WATCHDOG;
354         val |= reboot_mode;
355         sprd_adi_write(sadi, PMIC_RST_STATUS, val);
356
357         /* Enable the interface clock of the watchdog */
358         sprd_adi_read(sadi, PMIC_MODULE_EN, &val);
359         val |= BIT_WDG_EN;
360         sprd_adi_write(sadi, PMIC_MODULE_EN, val);
361
362         /* Enable the work clock of the watchdog */
363         sprd_adi_read(sadi, PMIC_CLK_EN, &val);
364         val |= BIT_WDG_EN;
365         sprd_adi_write(sadi, PMIC_CLK_EN, val);
366
367         /* Unlock the watchdog */
368         sprd_adi_write(sadi, REG_WDG_LOCK, WDG_UNLOCK_KEY);
369
370         sprd_adi_read(sadi, REG_WDG_CTRL, &val);
371         val |= BIT_WDG_NEW;
372         sprd_adi_write(sadi, REG_WDG_CTRL, val);
373
374         /* Load the watchdog timeout value, 50ms is always enough. */
375         sprd_adi_write(sadi, REG_WDG_LOAD_HIGH, 0);
376         sprd_adi_write(sadi, REG_WDG_LOAD_LOW,
377                        WDG_LOAD_VAL & WDG_LOAD_MASK);
378
379         /* Start the watchdog to reset system */
380         sprd_adi_read(sadi, REG_WDG_CTRL, &val);
381         val |= BIT_WDG_RUN | BIT_WDG_RST;
382         sprd_adi_write(sadi, REG_WDG_CTRL, val);
383
384         /* Lock the watchdog */
385         sprd_adi_write(sadi, REG_WDG_LOCK, ~WDG_UNLOCK_KEY);
386
387         mdelay(1000);
388
389         dev_emerg(sadi->dev, "Unable to restart system\n");
390         return NOTIFY_DONE;
391 }
392
393 static void sprd_adi_hw_init(struct sprd_adi *sadi)
394 {
395         struct device_node *np = sadi->dev->of_node;
396         int i, size, chn_cnt;
397         const __be32 *list;
398         u32 tmp;
399
400         /* Set all channels as default priority */
401         writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIL);
402         writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIH);
403
404         /* Set clock auto gate mode */
405         tmp = readl_relaxed(sadi->base + REG_ADI_GSSI_CFG0);
406         tmp &= ~BIT_CLK_ALL_ON;
407         writel_relaxed(tmp, sadi->base + REG_ADI_GSSI_CFG0);
408
409         /* Set hardware channels setting */
410         list = of_get_property(np, "sprd,hw-channels", &size);
411         if (!list || !size) {
412                 dev_info(sadi->dev, "no hw channels setting in node\n");
413                 return;
414         }
415
416         chn_cnt = size / 8;
417         for (i = 0; i < chn_cnt; i++) {
418                 u32 value;
419                 u32 chn_id = be32_to_cpu(*list++);
420                 u32 chn_config = be32_to_cpu(*list++);
421
422                 /* Channel 0 and 1 are software channels */
423                 if (chn_id < 2)
424                         continue;
425
426                 writel_relaxed(chn_config, sadi->base +
427                                REG_ADI_CHN_ADDR(chn_id));
428
429                 if (chn_id < 32) {
430                         value = readl_relaxed(sadi->base + REG_ADI_CHN_EN);
431                         value |= BIT(chn_id);
432                         writel_relaxed(value, sadi->base + REG_ADI_CHN_EN);
433                 } else if (chn_id < ADI_HW_CHNS) {
434                         value = readl_relaxed(sadi->base + REG_ADI_CHN_EN1);
435                         value |= BIT(chn_id - 32);
436                         writel_relaxed(value, sadi->base + REG_ADI_CHN_EN1);
437                 }
438         }
439 }
440
441 static int sprd_adi_probe(struct platform_device *pdev)
442 {
443         struct device_node *np = pdev->dev.of_node;
444         struct spi_controller *ctlr;
445         struct sprd_adi *sadi;
446         struct resource *res;
447         u32 num_chipselect;
448         int ret;
449
450         if (!np) {
451                 dev_err(&pdev->dev, "can not find the adi bus node\n");
452                 return -ENODEV;
453         }
454
455         pdev->id = of_alias_get_id(np, "spi");
456         num_chipselect = of_get_child_count(np);
457
458         ctlr = spi_alloc_master(&pdev->dev, sizeof(struct sprd_adi));
459         if (!ctlr)
460                 return -ENOMEM;
461
462         dev_set_drvdata(&pdev->dev, ctlr);
463         sadi = spi_controller_get_devdata(ctlr);
464
465         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
466         sadi->base = devm_ioremap_resource(&pdev->dev, res);
467         if (IS_ERR(sadi->base)) {
468                 ret = PTR_ERR(sadi->base);
469                 goto put_ctlr;
470         }
471
472         sadi->slave_vbase = (unsigned long)sadi->base + ADI_SLAVE_OFFSET;
473         sadi->slave_pbase = res->start + ADI_SLAVE_OFFSET;
474         sadi->ctlr = ctlr;
475         sadi->dev = &pdev->dev;
476         ret = of_hwspin_lock_get_id(np, 0);
477         if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
478                 sadi->hwlock =
479                         devm_hwspin_lock_request_specific(&pdev->dev, ret);
480                 if (!sadi->hwlock) {
481                         ret = -ENXIO;
482                         goto put_ctlr;
483                 }
484         } else {
485                 switch (ret) {
486                 case -ENOENT:
487                         dev_info(&pdev->dev, "no hardware spinlock supplied\n");
488                         break;
489                 default:
490                         dev_err_probe(&pdev->dev, ret, "failed to find hwlock id\n");
491                         goto put_ctlr;
492                 }
493         }
494
495         sprd_adi_hw_init(sadi);
496         sprd_adi_set_wdt_rst_mode(sadi);
497
498         ctlr->dev.of_node = pdev->dev.of_node;
499         ctlr->bus_num = pdev->id;
500         ctlr->num_chipselect = num_chipselect;
501         ctlr->flags = SPI_MASTER_HALF_DUPLEX;
502         ctlr->bits_per_word_mask = 0;
503         ctlr->transfer_one = sprd_adi_transfer_one;
504
505         ret = devm_spi_register_controller(&pdev->dev, ctlr);
506         if (ret) {
507                 dev_err(&pdev->dev, "failed to register SPI controller\n");
508                 goto put_ctlr;
509         }
510
511         sadi->restart_handler.notifier_call = sprd_adi_restart_handler;
512         sadi->restart_handler.priority = 128;
513         ret = register_restart_handler(&sadi->restart_handler);
514         if (ret) {
515                 dev_err(&pdev->dev, "can not register restart handler\n");
516                 goto put_ctlr;
517         }
518
519         return 0;
520
521 put_ctlr:
522         spi_controller_put(ctlr);
523         return ret;
524 }
525
526 static int sprd_adi_remove(struct platform_device *pdev)
527 {
528         struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
529         struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
530
531         unregister_restart_handler(&sadi->restart_handler);
532         return 0;
533 }
534
535 static const struct of_device_id sprd_adi_of_match[] = {
536         {
537                 .compatible = "sprd,sc9860-adi",
538         },
539         { },
540 };
541 MODULE_DEVICE_TABLE(of, sprd_adi_of_match);
542
543 static struct platform_driver sprd_adi_driver = {
544         .driver = {
545                 .name = "sprd-adi",
546                 .of_match_table = sprd_adi_of_match,
547         },
548         .probe = sprd_adi_probe,
549         .remove = sprd_adi_remove,
550 };
551 module_platform_driver(sprd_adi_driver);
552
553 MODULE_DESCRIPTION("Spreadtrum ADI Controller Driver");
554 MODULE_AUTHOR("Baolin Wang <Baolin.Wang@spreadtrum.com>");
555 MODULE_LICENSE("GPL v2");