blob: 07f11b17bf206bde7167b481935cc97becad9a24 [file] [log] [blame]
Baolin Wang7e2903c2017-09-15 15:29:16 +08001/*
2 * Copyright (C) 2017 Spreadtrum Communications Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
Baolin Wangac177502018-03-20 10:42:14 +08007#include <linux/delay.h>
Baolin Wang7e2903c2017-09-15 15:29:16 +08008#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>
Baolin Wangac177502018-03-20 10:42:14 +080016#include <linux/reboot.h>
Baolin Wang7e2903c2017-09-15 15:29:16 +080017#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
Baolin Wangac177502018-03-20 10:42:14 +080072/* 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)
Lingling Xu1d00a672019-10-28 18:10:31 +080080#define BIT_WDG_NEW BIT(2)
Baolin Wangac177502018-03-20 10:42:14 +080081#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 */
Chenxu Weicc6b3432019-07-26 15:20:49 +080090#define HWRST_STATUS_SECURITY 0x02
Baolin Wangac177502018-03-20 10:42:14 +080091#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
Sherry Zong9d9aa1c2019-07-26 15:20:50 +0800102#define HWRST_STATUS_FACTORYTEST 0xe0
Sherry Zonge6d722c2019-07-26 15:20:51 +0800103#define HWRST_STATUS_WATCHDOG 0xf0
Baolin Wangac177502018-03-20 10:42:14 +0800104
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
Baolin Wang7e2903c2017-09-15 15:29:16 +0800110struct 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;
Baolin Wangac177502018-03-20 10:42:14 +0800117 struct notifier_block restart_handler;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800118};
119
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800120static int sprd_adi_check_addr(struct sprd_adi *sadi, u32 reg)
Baolin Wang7e2903c2017-09-15 15:29:16 +0800121{
Chunyan Zhang2b961c52021-08-24 15:02:11 +0800122 if (reg >= ADI_SLAVE_ADDR_SIZE) {
Baolin Wang7e2903c2017-09-15 15:29:16 +0800123 dev_err(sadi->dev,
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800124 "slave address offset is incorrect, reg = 0x%x\n",
125 reg);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800126 return -EINVAL;
127 }
128
129 return 0;
130}
131
Baolin Wang7e2903c2017-09-15 15:29:16 +0800132static 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
153static 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
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800158static int sprd_adi_read(struct sprd_adi *sadi, u32 reg, u32 *read_val)
Baolin Wang7e2903c2017-09-15 15:29:16 +0800159{
160 int read_timeout = ADI_READ_TIMEOUT;
Baolin Wanga61aa682018-03-20 10:42:13 +0800161 unsigned long flags;
Chunyan Zhangf674aac2021-08-24 15:02:12 +0800162 u32 val, rd_addr;
Baolin Wangf9adf612019-07-26 15:20:52 +0800163 int ret = 0;
Baolin Wanga61aa682018-03-20 10:42:13 +0800164
Baolin Wangf9adf612019-07-26 15:20:52 +0800165 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 }
Baolin Wanga61aa682018-03-20 10:42:13 +0800173 }
Baolin Wang7e2903c2017-09-15 15:29:16 +0800174
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800175 ret = sprd_adi_check_addr(sadi, reg);
176 if (ret)
177 goto out;
178
Baolin Wang7e2903c2017-09-15 15:29:16 +0800179 /*
Chunyan Zhangf674aac2021-08-24 15:02:12 +0800180 * Set the slave address offset need to read into RD_CMD register,
Baolin Wang7e2903c2017-09-15 15:29:16 +0800181 * then ADI controller will start to transfer automatically.
182 */
Chunyan Zhangf674aac2021-08-24 15:02:12 +0800183 writel_relaxed(reg, sadi->base + REG_ADI_RD_CMD);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800184
185 /*
186 * Wait read operation complete, the BIT_RD_CMD_BUSY will be set
187 * simultaneously when writing read command to register, and the
188 * BIT_RD_CMD_BUSY will be cleared after the read operation is
189 * completed.
190 */
191 do {
192 val = readl_relaxed(sadi->base + REG_ADI_RD_DATA);
193 if (!(val & BIT_RD_CMD_BUSY))
194 break;
195
196 cpu_relax();
197 } while (--read_timeout);
198
199 if (read_timeout == 0) {
200 dev_err(sadi->dev, "ADI read timeout\n");
Baolin Wanga61aa682018-03-20 10:42:13 +0800201 ret = -EBUSY;
202 goto out;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800203 }
204
205 /*
206 * The return value includes data and read register address, from bit 0
207 * to bit 15 are data, and from bit 16 to bit 30 are read register
208 * address. Then we can check the returned register address to validate
209 * data.
210 */
Jay Fange13a8702021-03-24 14:16:33 +0800211 rd_addr = (val & RD_ADDR_MASK) >> RD_ADDR_SHIFT;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800212
Chunyan Zhangf674aac2021-08-24 15:02:12 +0800213 if (rd_addr != (reg & REG_ADDR_LOW_MASK)) {
Baolin Wang7e2903c2017-09-15 15:29:16 +0800214 dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x\n",
Chunyan Zhangf674aac2021-08-24 15:02:12 +0800215 reg, val);
Baolin Wanga61aa682018-03-20 10:42:13 +0800216 ret = -EIO;
217 goto out;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800218 }
219
220 *read_val = val & RD_VALUE_MASK;
Baolin Wanga61aa682018-03-20 10:42:13 +0800221
222out:
Baolin Wangf9adf612019-07-26 15:20:52 +0800223 if (sadi->hwlock)
224 hwspin_unlock_irqrestore(sadi->hwlock, &flags);
Baolin Wanga61aa682018-03-20 10:42:13 +0800225 return ret;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800226}
227
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800228static int sprd_adi_write(struct sprd_adi *sadi, u32 reg, u32 val)
Baolin Wang7e2903c2017-09-15 15:29:16 +0800229{
230 u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
Baolin Wanga61aa682018-03-20 10:42:13 +0800231 unsigned long flags;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800232 int ret;
233
Baolin Wangf9adf612019-07-26 15:20:52 +0800234 if (sadi->hwlock) {
235 ret = hwspin_lock_timeout_irqsave(sadi->hwlock,
236 ADI_HWSPINLOCK_TIMEOUT,
237 &flags);
238 if (ret) {
239 dev_err(sadi->dev, "get the hw lock failed\n");
240 return ret;
241 }
Baolin Wanga61aa682018-03-20 10:42:13 +0800242 }
243
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800244 ret = sprd_adi_check_addr(sadi, reg);
245 if (ret)
246 goto out;
247
Baolin Wang7e2903c2017-09-15 15:29:16 +0800248 ret = sprd_adi_drain_fifo(sadi);
249 if (ret < 0)
Baolin Wanga61aa682018-03-20 10:42:13 +0800250 goto out;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800251
252 /*
253 * we should wait for write fifo is empty before writing data to PMIC
254 * registers.
255 */
256 do {
257 if (!sprd_adi_fifo_is_full(sadi)) {
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800258 /* we need virtual register address to write. */
259 writel_relaxed(val, (void __iomem *)(sadi->slave_vbase + reg));
Baolin Wang7e2903c2017-09-15 15:29:16 +0800260 break;
261 }
262
263 cpu_relax();
264 } while (--timeout);
265
266 if (timeout == 0) {
267 dev_err(sadi->dev, "write fifo is full\n");
Baolin Wanga61aa682018-03-20 10:42:13 +0800268 ret = -EBUSY;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800269 }
270
Baolin Wanga61aa682018-03-20 10:42:13 +0800271out:
Baolin Wangf9adf612019-07-26 15:20:52 +0800272 if (sadi->hwlock)
273 hwspin_unlock_irqrestore(sadi->hwlock, &flags);
Baolin Wanga61aa682018-03-20 10:42:13 +0800274 return ret;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800275}
276
277static int sprd_adi_transfer_one(struct spi_controller *ctlr,
278 struct spi_device *spi_dev,
279 struct spi_transfer *t)
280{
281 struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800282 u32 reg, val;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800283 int ret;
284
285 if (t->rx_buf) {
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800286 reg = *(u32 *)t->rx_buf;
287 ret = sprd_adi_read(sadi, reg, &val);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800288 *(u32 *)t->rx_buf = val;
289 } else if (t->tx_buf) {
290 u32 *p = (u32 *)t->tx_buf;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800291 reg = *p++;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800292 val = *p;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800293 ret = sprd_adi_write(sadi, reg, val);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800294 } else {
295 dev_err(sadi->dev, "no buffer for transfer\n");
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800296 ret = -EINVAL;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800297 }
298
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800299 return ret;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800300}
301
Sherry Zonge6d722c2019-07-26 15:20:51 +0800302static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi)
303{
Baolin Wangbb4bf8d2020-04-13 14:30:25 +0800304#if IS_ENABLED(CONFIG_SPRD_WATCHDOG)
Sherry Zonge6d722c2019-07-26 15:20:51 +0800305 u32 val;
306
307 /* Set default watchdog reboot mode */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800308 sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
Sherry Zonge6d722c2019-07-26 15:20:51 +0800309 val |= HWRST_STATUS_WATCHDOG;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800310 sprd_adi_write(sadi, PMIC_RST_STATUS, val);
Sherry Zonge6d722c2019-07-26 15:20:51 +0800311#endif
312}
313
Baolin Wangac177502018-03-20 10:42:14 +0800314static int sprd_adi_restart_handler(struct notifier_block *this,
315 unsigned long mode, void *cmd)
316{
317 struct sprd_adi *sadi = container_of(this, struct sprd_adi,
318 restart_handler);
319 u32 val, reboot_mode = 0;
320
321 if (!cmd)
322 reboot_mode = HWRST_STATUS_NORMAL;
323 else if (!strncmp(cmd, "recovery", 8))
324 reboot_mode = HWRST_STATUS_RECOVERY;
325 else if (!strncmp(cmd, "alarm", 5))
326 reboot_mode = HWRST_STATUS_ALARM;
327 else if (!strncmp(cmd, "fastsleep", 9))
328 reboot_mode = HWRST_STATUS_SLEEP;
329 else if (!strncmp(cmd, "bootloader", 10))
330 reboot_mode = HWRST_STATUS_FASTBOOT;
331 else if (!strncmp(cmd, "panic", 5))
332 reboot_mode = HWRST_STATUS_PANIC;
333 else if (!strncmp(cmd, "special", 7))
334 reboot_mode = HWRST_STATUS_SPECIAL;
335 else if (!strncmp(cmd, "cftreboot", 9))
336 reboot_mode = HWRST_STATUS_CFTREBOOT;
337 else if (!strncmp(cmd, "autodloader", 11))
338 reboot_mode = HWRST_STATUS_AUTODLOADER;
339 else if (!strncmp(cmd, "iqmode", 6))
340 reboot_mode = HWRST_STATUS_IQMODE;
341 else if (!strncmp(cmd, "sprdisk", 7))
342 reboot_mode = HWRST_STATUS_SPRDISK;
Chenxu Weicc6b3432019-07-26 15:20:49 +0800343 else if (!strncmp(cmd, "tospanic", 8))
344 reboot_mode = HWRST_STATUS_SECURITY;
Sherry Zong9d9aa1c2019-07-26 15:20:50 +0800345 else if (!strncmp(cmd, "factorytest", 11))
346 reboot_mode = HWRST_STATUS_FACTORYTEST;
Baolin Wangac177502018-03-20 10:42:14 +0800347 else
348 reboot_mode = HWRST_STATUS_NORMAL;
349
350 /* Record the reboot mode */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800351 sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
Sherry Zonge6d722c2019-07-26 15:20:51 +0800352 val &= ~HWRST_STATUS_WATCHDOG;
Baolin Wangac177502018-03-20 10:42:14 +0800353 val |= reboot_mode;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800354 sprd_adi_write(sadi, PMIC_RST_STATUS, val);
Baolin Wangac177502018-03-20 10:42:14 +0800355
356 /* Enable the interface clock of the watchdog */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800357 sprd_adi_read(sadi, PMIC_MODULE_EN, &val);
Baolin Wangac177502018-03-20 10:42:14 +0800358 val |= BIT_WDG_EN;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800359 sprd_adi_write(sadi, PMIC_MODULE_EN, val);
Baolin Wangac177502018-03-20 10:42:14 +0800360
361 /* Enable the work clock of the watchdog */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800362 sprd_adi_read(sadi, PMIC_CLK_EN, &val);
Baolin Wangac177502018-03-20 10:42:14 +0800363 val |= BIT_WDG_EN;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800364 sprd_adi_write(sadi, PMIC_CLK_EN, val);
Baolin Wangac177502018-03-20 10:42:14 +0800365
366 /* Unlock the watchdog */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800367 sprd_adi_write(sadi, REG_WDG_LOCK, WDG_UNLOCK_KEY);
Baolin Wangac177502018-03-20 10:42:14 +0800368
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800369 sprd_adi_read(sadi, REG_WDG_CTRL, &val);
Lingling Xu1d00a672019-10-28 18:10:31 +0800370 val |= BIT_WDG_NEW;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800371 sprd_adi_write(sadi, REG_WDG_CTRL, val);
Lingling Xu1d00a672019-10-28 18:10:31 +0800372
Baolin Wangac177502018-03-20 10:42:14 +0800373 /* Load the watchdog timeout value, 50ms is always enough. */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800374 sprd_adi_write(sadi, REG_WDG_LOAD_HIGH, 0);
375 sprd_adi_write(sadi, REG_WDG_LOAD_LOW,
Baolin Wangac177502018-03-20 10:42:14 +0800376 WDG_LOAD_VAL & WDG_LOAD_MASK);
Baolin Wangac177502018-03-20 10:42:14 +0800377
378 /* Start the watchdog to reset system */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800379 sprd_adi_read(sadi, REG_WDG_CTRL, &val);
Baolin Wangac177502018-03-20 10:42:14 +0800380 val |= BIT_WDG_RUN | BIT_WDG_RST;
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800381 sprd_adi_write(sadi, REG_WDG_CTRL, val);
Baolin Wangac177502018-03-20 10:42:14 +0800382
Lingling Xu91ea1d72019-10-28 18:10:30 +0800383 /* Lock the watchdog */
Chunyan Zhang5dc349e2021-08-24 15:02:10 +0800384 sprd_adi_write(sadi, REG_WDG_LOCK, ~WDG_UNLOCK_KEY);
Lingling Xu91ea1d72019-10-28 18:10:30 +0800385
Baolin Wangac177502018-03-20 10:42:14 +0800386 mdelay(1000);
387
388 dev_emerg(sadi->dev, "Unable to restart system\n");
389 return NOTIFY_DONE;
390}
391
Baolin Wang7e2903c2017-09-15 15:29:16 +0800392static void sprd_adi_hw_init(struct sprd_adi *sadi)
393{
394 struct device_node *np = sadi->dev->of_node;
395 int i, size, chn_cnt;
396 const __be32 *list;
397 u32 tmp;
398
Baolin Wang7e2903c2017-09-15 15:29:16 +0800399 /* Set all channels as default priority */
400 writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIL);
401 writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIH);
402
403 /* Set clock auto gate mode */
404 tmp = readl_relaxed(sadi->base + REG_ADI_GSSI_CFG0);
405 tmp &= ~BIT_CLK_ALL_ON;
406 writel_relaxed(tmp, sadi->base + REG_ADI_GSSI_CFG0);
407
408 /* Set hardware channels setting */
409 list = of_get_property(np, "sprd,hw-channels", &size);
Dan Carpenterb0d6e092017-09-22 23:48:08 +0300410 if (!list || !size) {
Baolin Wang7e2903c2017-09-15 15:29:16 +0800411 dev_info(sadi->dev, "no hw channels setting in node\n");
412 return;
413 }
414
415 chn_cnt = size / 8;
416 for (i = 0; i < chn_cnt; i++) {
417 u32 value;
418 u32 chn_id = be32_to_cpu(*list++);
419 u32 chn_config = be32_to_cpu(*list++);
420
421 /* Channel 0 and 1 are software channels */
422 if (chn_id < 2)
423 continue;
424
425 writel_relaxed(chn_config, sadi->base +
426 REG_ADI_CHN_ADDR(chn_id));
427
Baolin Wang54e2fc22017-10-25 19:25:09 +0800428 if (chn_id < 32) {
Baolin Wang7e2903c2017-09-15 15:29:16 +0800429 value = readl_relaxed(sadi->base + REG_ADI_CHN_EN);
430 value |= BIT(chn_id);
431 writel_relaxed(value, sadi->base + REG_ADI_CHN_EN);
432 } else if (chn_id < ADI_HW_CHNS) {
433 value = readl_relaxed(sadi->base + REG_ADI_CHN_EN1);
434 value |= BIT(chn_id - 32);
435 writel_relaxed(value, sadi->base + REG_ADI_CHN_EN1);
436 }
437 }
438}
439
440static int sprd_adi_probe(struct platform_device *pdev)
441{
442 struct device_node *np = pdev->dev.of_node;
443 struct spi_controller *ctlr;
444 struct sprd_adi *sadi;
445 struct resource *res;
446 u32 num_chipselect;
447 int ret;
448
449 if (!np) {
450 dev_err(&pdev->dev, "can not find the adi bus node\n");
451 return -ENODEV;
452 }
453
454 pdev->id = of_alias_get_id(np, "spi");
455 num_chipselect = of_get_child_count(np);
456
457 ctlr = spi_alloc_master(&pdev->dev, sizeof(struct sprd_adi));
458 if (!ctlr)
459 return -ENOMEM;
460
461 dev_set_drvdata(&pdev->dev, ctlr);
462 sadi = spi_controller_get_devdata(ctlr);
463
464 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
465 sadi->base = devm_ioremap_resource(&pdev->dev, res);
Dan Carpenter04063a02017-09-25 13:21:33 +0300466 if (IS_ERR(sadi->base)) {
467 ret = PTR_ERR(sadi->base);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800468 goto put_ctlr;
469 }
470
471 sadi->slave_vbase = (unsigned long)sadi->base + ADI_SLAVE_OFFSET;
472 sadi->slave_pbase = res->start + ADI_SLAVE_OFFSET;
473 sadi->ctlr = ctlr;
474 sadi->dev = &pdev->dev;
Baolin Wangf9adf612019-07-26 15:20:52 +0800475 ret = of_hwspin_lock_get_id(np, 0);
476 if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
477 sadi->hwlock =
478 devm_hwspin_lock_request_specific(&pdev->dev, ret);
479 if (!sadi->hwlock) {
480 ret = -ENXIO;
481 goto put_ctlr;
482 }
483 } else {
484 switch (ret) {
485 case -ENOENT:
486 dev_info(&pdev->dev, "no hardware spinlock supplied\n");
487 break;
488 default:
Krzysztof Kozlowski9d99e552020-09-10 18:07:06 +0200489 dev_err_probe(&pdev->dev, ret, "failed to find hwlock id\n");
Baolin Wangf9adf612019-07-26 15:20:52 +0800490 goto put_ctlr;
491 }
Baolin Wang7e2903c2017-09-15 15:29:16 +0800492 }
493
494 sprd_adi_hw_init(sadi);
Sherry Zonge6d722c2019-07-26 15:20:51 +0800495 sprd_adi_set_wdt_rst_mode(sadi);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800496
497 ctlr->dev.of_node = pdev->dev.of_node;
498 ctlr->bus_num = pdev->id;
499 ctlr->num_chipselect = num_chipselect;
500 ctlr->flags = SPI_MASTER_HALF_DUPLEX;
501 ctlr->bits_per_word_mask = 0;
502 ctlr->transfer_one = sprd_adi_transfer_one;
503
504 ret = devm_spi_register_controller(&pdev->dev, ctlr);
505 if (ret) {
506 dev_err(&pdev->dev, "failed to register SPI controller\n");
Baolin Wangc8d04982018-06-22 16:09:05 +0800507 goto put_ctlr;
Baolin Wang7e2903c2017-09-15 15:29:16 +0800508 }
509
Baolin Wangac177502018-03-20 10:42:14 +0800510 sadi->restart_handler.notifier_call = sprd_adi_restart_handler;
511 sadi->restart_handler.priority = 128;
512 ret = register_restart_handler(&sadi->restart_handler);
513 if (ret) {
514 dev_err(&pdev->dev, "can not register restart handler\n");
Baolin Wangc8d04982018-06-22 16:09:05 +0800515 goto put_ctlr;
Baolin Wangac177502018-03-20 10:42:14 +0800516 }
517
Baolin Wang7e2903c2017-09-15 15:29:16 +0800518 return 0;
519
Baolin Wang7e2903c2017-09-15 15:29:16 +0800520put_ctlr:
521 spi_controller_put(ctlr);
522 return ret;
523}
524
525static int sprd_adi_remove(struct platform_device *pdev)
526{
527 struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
528 struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
529
Baolin Wangac177502018-03-20 10:42:14 +0800530 unregister_restart_handler(&sadi->restart_handler);
Baolin Wang7e2903c2017-09-15 15:29:16 +0800531 return 0;
532}
533
534static const struct of_device_id sprd_adi_of_match[] = {
535 {
536 .compatible = "sprd,sc9860-adi",
537 },
538 { },
539};
540MODULE_DEVICE_TABLE(of, sprd_adi_of_match);
541
542static struct platform_driver sprd_adi_driver = {
543 .driver = {
544 .name = "sprd-adi",
Baolin Wang7e2903c2017-09-15 15:29:16 +0800545 .of_match_table = sprd_adi_of_match,
546 },
547 .probe = sprd_adi_probe,
548 .remove = sprd_adi_remove,
549};
550module_platform_driver(sprd_adi_driver);
551
552MODULE_DESCRIPTION("Spreadtrum ADI Controller Driver");
553MODULE_AUTHOR("Baolin Wang <Baolin.Wang@spreadtrum.com>");
554MODULE_LICENSE("GPL v2");