spi-sprd-adi.c 14 KB

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