Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Spreadtrum Communications Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0 |
| 5 | */ |
| 6 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 7 | #include <linux/delay.h> |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 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> |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 16 | #include <linux/reboot.h> |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 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 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 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) |
Lingling Xu | 1d00a67 | 2019-10-28 18:10:31 +0800 | [diff] [blame] | 80 | #define BIT_WDG_NEW BIT(2) |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 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 */ |
Chenxu Wei | cc6b343 | 2019-07-26 15:20:49 +0800 | [diff] [blame] | 90 | #define HWRST_STATUS_SECURITY 0x02 |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 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 |
Sherry Zong | 9d9aa1c | 2019-07-26 15:20:50 +0800 | [diff] [blame] | 102 | #define HWRST_STATUS_FACTORYTEST 0xe0 |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 103 | #define HWRST_STATUS_WATCHDOG 0xf0 |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 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 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 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; |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 117 | struct notifier_block restart_handler; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 118 | }; |
| 119 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 120 | static int sprd_adi_check_addr(struct sprd_adi *sadi, u32 reg) |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 121 | { |
Chunyan Zhang | 2b961c5 | 2021-08-24 15:02:11 +0800 | [diff] [blame] | 122 | if (reg >= ADI_SLAVE_ADDR_SIZE) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 123 | dev_err(sadi->dev, |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 124 | "slave address offset is incorrect, reg = 0x%x\n", |
| 125 | reg); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 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 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 158 | static int sprd_adi_read(struct sprd_adi *sadi, u32 reg, u32 *read_val) |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 159 | { |
| 160 | int read_timeout = ADI_READ_TIMEOUT; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 161 | unsigned long flags; |
Chunyan Zhang | f674aac | 2021-08-24 15:02:12 +0800 | [diff] [blame^] | 162 | u32 val, rd_addr; |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 163 | int ret = 0; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 164 | |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 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 | } |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 173 | } |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 174 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 175 | ret = sprd_adi_check_addr(sadi, reg); |
| 176 | if (ret) |
| 177 | goto out; |
| 178 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 179 | /* |
Chunyan Zhang | f674aac | 2021-08-24 15:02:12 +0800 | [diff] [blame^] | 180 | * Set the slave address offset need to read into RD_CMD register, |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 181 | * then ADI controller will start to transfer automatically. |
| 182 | */ |
Chunyan Zhang | f674aac | 2021-08-24 15:02:12 +0800 | [diff] [blame^] | 183 | writel_relaxed(reg, sadi->base + REG_ADI_RD_CMD); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 184 | |
| 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 Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 201 | ret = -EBUSY; |
| 202 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 203 | } |
| 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 Fang | e13a870 | 2021-03-24 14:16:33 +0800 | [diff] [blame] | 211 | rd_addr = (val & RD_ADDR_MASK) >> RD_ADDR_SHIFT; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 212 | |
Chunyan Zhang | f674aac | 2021-08-24 15:02:12 +0800 | [diff] [blame^] | 213 | if (rd_addr != (reg & REG_ADDR_LOW_MASK)) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 214 | dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x\n", |
Chunyan Zhang | f674aac | 2021-08-24 15:02:12 +0800 | [diff] [blame^] | 215 | reg, val); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 216 | ret = -EIO; |
| 217 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | *read_val = val & RD_VALUE_MASK; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 221 | |
| 222 | out: |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 223 | if (sadi->hwlock) |
| 224 | hwspin_unlock_irqrestore(sadi->hwlock, &flags); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 225 | return ret; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 228 | static int sprd_adi_write(struct sprd_adi *sadi, u32 reg, u32 val) |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 229 | { |
| 230 | u32 timeout = ADI_FIFO_DRAIN_TIMEOUT; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 231 | unsigned long flags; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 232 | int ret; |
| 233 | |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 234 | 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 Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 244 | ret = sprd_adi_check_addr(sadi, reg); |
| 245 | if (ret) |
| 246 | goto out; |
| 247 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 248 | ret = sprd_adi_drain_fifo(sadi); |
| 249 | if (ret < 0) |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 250 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 251 | |
| 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 Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 258 | /* we need virtual register address to write. */ |
| 259 | writel_relaxed(val, (void __iomem *)(sadi->slave_vbase + reg)); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 260 | 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 Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 268 | ret = -EBUSY; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 269 | } |
| 270 | |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 271 | out: |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 272 | if (sadi->hwlock) |
| 273 | hwspin_unlock_irqrestore(sadi->hwlock, &flags); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame] | 274 | return ret; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static 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 Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 282 | u32 reg, val; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 283 | int ret; |
| 284 | |
| 285 | if (t->rx_buf) { |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 286 | reg = *(u32 *)t->rx_buf; |
| 287 | ret = sprd_adi_read(sadi, reg, &val); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 288 | *(u32 *)t->rx_buf = val; |
| 289 | } else if (t->tx_buf) { |
| 290 | u32 *p = (u32 *)t->tx_buf; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 291 | reg = *p++; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 292 | val = *p; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 293 | ret = sprd_adi_write(sadi, reg, val); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 294 | } else { |
| 295 | dev_err(sadi->dev, "no buffer for transfer\n"); |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 296 | ret = -EINVAL; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 297 | } |
| 298 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 299 | return ret; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 302 | static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi) |
| 303 | { |
Baolin Wang | bb4bf8d | 2020-04-13 14:30:25 +0800 | [diff] [blame] | 304 | #if IS_ENABLED(CONFIG_SPRD_WATCHDOG) |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 305 | u32 val; |
| 306 | |
| 307 | /* Set default watchdog reboot mode */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 308 | sprd_adi_read(sadi, PMIC_RST_STATUS, &val); |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 309 | val |= HWRST_STATUS_WATCHDOG; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 310 | sprd_adi_write(sadi, PMIC_RST_STATUS, val); |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 311 | #endif |
| 312 | } |
| 313 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 314 | static 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 Wei | cc6b343 | 2019-07-26 15:20:49 +0800 | [diff] [blame] | 343 | else if (!strncmp(cmd, "tospanic", 8)) |
| 344 | reboot_mode = HWRST_STATUS_SECURITY; |
Sherry Zong | 9d9aa1c | 2019-07-26 15:20:50 +0800 | [diff] [blame] | 345 | else if (!strncmp(cmd, "factorytest", 11)) |
| 346 | reboot_mode = HWRST_STATUS_FACTORYTEST; |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 347 | else |
| 348 | reboot_mode = HWRST_STATUS_NORMAL; |
| 349 | |
| 350 | /* Record the reboot mode */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 351 | sprd_adi_read(sadi, PMIC_RST_STATUS, &val); |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 352 | val &= ~HWRST_STATUS_WATCHDOG; |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 353 | val |= reboot_mode; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 354 | sprd_adi_write(sadi, PMIC_RST_STATUS, val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 355 | |
| 356 | /* Enable the interface clock of the watchdog */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 357 | sprd_adi_read(sadi, PMIC_MODULE_EN, &val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 358 | val |= BIT_WDG_EN; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 359 | sprd_adi_write(sadi, PMIC_MODULE_EN, val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 360 | |
| 361 | /* Enable the work clock of the watchdog */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 362 | sprd_adi_read(sadi, PMIC_CLK_EN, &val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 363 | val |= BIT_WDG_EN; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 364 | sprd_adi_write(sadi, PMIC_CLK_EN, val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 365 | |
| 366 | /* Unlock the watchdog */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 367 | sprd_adi_write(sadi, REG_WDG_LOCK, WDG_UNLOCK_KEY); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 368 | |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 369 | sprd_adi_read(sadi, REG_WDG_CTRL, &val); |
Lingling Xu | 1d00a67 | 2019-10-28 18:10:31 +0800 | [diff] [blame] | 370 | val |= BIT_WDG_NEW; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 371 | sprd_adi_write(sadi, REG_WDG_CTRL, val); |
Lingling Xu | 1d00a67 | 2019-10-28 18:10:31 +0800 | [diff] [blame] | 372 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 373 | /* Load the watchdog timeout value, 50ms is always enough. */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 374 | sprd_adi_write(sadi, REG_WDG_LOAD_HIGH, 0); |
| 375 | sprd_adi_write(sadi, REG_WDG_LOAD_LOW, |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 376 | WDG_LOAD_VAL & WDG_LOAD_MASK); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 377 | |
| 378 | /* Start the watchdog to reset system */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 379 | sprd_adi_read(sadi, REG_WDG_CTRL, &val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 380 | val |= BIT_WDG_RUN | BIT_WDG_RST; |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 381 | sprd_adi_write(sadi, REG_WDG_CTRL, val); |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 382 | |
Lingling Xu | 91ea1d7 | 2019-10-28 18:10:30 +0800 | [diff] [blame] | 383 | /* Lock the watchdog */ |
Chunyan Zhang | 5dc349e | 2021-08-24 15:02:10 +0800 | [diff] [blame] | 384 | sprd_adi_write(sadi, REG_WDG_LOCK, ~WDG_UNLOCK_KEY); |
Lingling Xu | 91ea1d7 | 2019-10-28 18:10:30 +0800 | [diff] [blame] | 385 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 386 | mdelay(1000); |
| 387 | |
| 388 | dev_emerg(sadi->dev, "Unable to restart system\n"); |
| 389 | return NOTIFY_DONE; |
| 390 | } |
| 391 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 392 | static 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 Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 399 | /* 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 Carpenter | b0d6e09 | 2017-09-22 23:48:08 +0300 | [diff] [blame] | 410 | if (!list || !size) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 411 | 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 Wang | 54e2fc2 | 2017-10-25 19:25:09 +0800 | [diff] [blame] | 428 | if (chn_id < 32) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 429 | 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 | |
| 440 | static 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 Carpenter | 04063a0 | 2017-09-25 13:21:33 +0300 | [diff] [blame] | 466 | if (IS_ERR(sadi->base)) { |
| 467 | ret = PTR_ERR(sadi->base); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 468 | 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 Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 475 | 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 Kozlowski | 9d99e55 | 2020-09-10 18:07:06 +0200 | [diff] [blame] | 489 | dev_err_probe(&pdev->dev, ret, "failed to find hwlock id\n"); |
Baolin Wang | f9adf61 | 2019-07-26 15:20:52 +0800 | [diff] [blame] | 490 | goto put_ctlr; |
| 491 | } |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | sprd_adi_hw_init(sadi); |
Sherry Zong | e6d722c | 2019-07-26 15:20:51 +0800 | [diff] [blame] | 495 | sprd_adi_set_wdt_rst_mode(sadi); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 496 | |
| 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 Wang | c8d0498 | 2018-06-22 16:09:05 +0800 | [diff] [blame] | 507 | goto put_ctlr; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 508 | } |
| 509 | |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 510 | 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 Wang | c8d0498 | 2018-06-22 16:09:05 +0800 | [diff] [blame] | 515 | goto put_ctlr; |
Baolin Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 516 | } |
| 517 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 518 | return 0; |
| 519 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 520 | put_ctlr: |
| 521 | spi_controller_put(ctlr); |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | static 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 Wang | ac17750 | 2018-03-20 10:42:14 +0800 | [diff] [blame] | 530 | unregister_restart_handler(&sadi->restart_handler); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct of_device_id sprd_adi_of_match[] = { |
| 535 | { |
| 536 | .compatible = "sprd,sc9860-adi", |
| 537 | }, |
| 538 | { }, |
| 539 | }; |
| 540 | MODULE_DEVICE_TABLE(of, sprd_adi_of_match); |
| 541 | |
| 542 | static struct platform_driver sprd_adi_driver = { |
| 543 | .driver = { |
| 544 | .name = "sprd-adi", |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 545 | .of_match_table = sprd_adi_of_match, |
| 546 | }, |
| 547 | .probe = sprd_adi_probe, |
| 548 | .remove = sprd_adi_remove, |
| 549 | }; |
| 550 | module_platform_driver(sprd_adi_driver); |
| 551 | |
| 552 | MODULE_DESCRIPTION("Spreadtrum ADI Controller Driver"); |
| 553 | MODULE_AUTHOR("Baolin Wang <Baolin.Wang@spreadtrum.com>"); |
| 554 | MODULE_LICENSE("GPL v2"); |