sun6i_hwspinlock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * sun6i_hwspinlock.c - hardware spinlock driver for sun6i compatible Allwinner SoCs
  4. * Copyright (C) 2020 Wilken Gottwalt <[email protected]>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/errno.h>
  9. #include <linux/hwspinlock.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/types.h>
  18. #include "hwspinlock_internal.h"
  19. #define DRIVER_NAME "sun6i_hwspinlock"
  20. #define SPINLOCK_BASE_ID 0 /* there is only one hwspinlock device per SoC */
  21. #define SPINLOCK_SYSSTATUS_REG 0x0000
  22. #define SPINLOCK_LOCK_REGN 0x0100
  23. #define SPINLOCK_NOTTAKEN 0
  24. struct sun6i_hwspinlock_data {
  25. struct hwspinlock_device *bank;
  26. struct reset_control *reset;
  27. struct clk *ahb_clk;
  28. struct dentry *debugfs;
  29. int nlocks;
  30. };
  31. #ifdef CONFIG_DEBUG_FS
  32. static int hwlocks_supported_show(struct seq_file *seqf, void *unused)
  33. {
  34. struct sun6i_hwspinlock_data *priv = seqf->private;
  35. seq_printf(seqf, "%d\n", priv->nlocks);
  36. return 0;
  37. }
  38. DEFINE_SHOW_ATTRIBUTE(hwlocks_supported);
  39. static void sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data *priv)
  40. {
  41. priv->debugfs = debugfs_create_dir(DRIVER_NAME, NULL);
  42. debugfs_create_file("supported", 0444, priv->debugfs, priv, &hwlocks_supported_fops);
  43. }
  44. #else
  45. static void sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data *priv)
  46. {
  47. }
  48. #endif
  49. static int sun6i_hwspinlock_trylock(struct hwspinlock *lock)
  50. {
  51. void __iomem *lock_addr = lock->priv;
  52. return (readl(lock_addr) == SPINLOCK_NOTTAKEN);
  53. }
  54. static void sun6i_hwspinlock_unlock(struct hwspinlock *lock)
  55. {
  56. void __iomem *lock_addr = lock->priv;
  57. writel(SPINLOCK_NOTTAKEN, lock_addr);
  58. }
  59. static const struct hwspinlock_ops sun6i_hwspinlock_ops = {
  60. .trylock = sun6i_hwspinlock_trylock,
  61. .unlock = sun6i_hwspinlock_unlock,
  62. };
  63. static void sun6i_hwspinlock_disable(void *data)
  64. {
  65. struct sun6i_hwspinlock_data *priv = data;
  66. debugfs_remove_recursive(priv->debugfs);
  67. clk_disable_unprepare(priv->ahb_clk);
  68. reset_control_assert(priv->reset);
  69. }
  70. static int sun6i_hwspinlock_probe(struct platform_device *pdev)
  71. {
  72. struct sun6i_hwspinlock_data *priv;
  73. struct hwspinlock *hwlock;
  74. void __iomem *io_base;
  75. u32 num_banks;
  76. int err, i;
  77. io_base = devm_platform_ioremap_resource(pdev, SPINLOCK_BASE_ID);
  78. if (IS_ERR(io_base))
  79. return PTR_ERR(io_base);
  80. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  81. if (!priv)
  82. return -ENOMEM;
  83. priv->ahb_clk = devm_clk_get(&pdev->dev, "ahb");
  84. if (IS_ERR(priv->ahb_clk)) {
  85. err = PTR_ERR(priv->ahb_clk);
  86. dev_err(&pdev->dev, "unable to get AHB clock (%d)\n", err);
  87. return err;
  88. }
  89. priv->reset = devm_reset_control_get(&pdev->dev, "ahb");
  90. if (IS_ERR(priv->reset))
  91. return dev_err_probe(&pdev->dev, PTR_ERR(priv->reset),
  92. "unable to get reset control\n");
  93. err = reset_control_deassert(priv->reset);
  94. if (err) {
  95. dev_err(&pdev->dev, "deassert reset control failure (%d)\n", err);
  96. return err;
  97. }
  98. err = clk_prepare_enable(priv->ahb_clk);
  99. if (err) {
  100. dev_err(&pdev->dev, "unable to prepare AHB clk (%d)\n", err);
  101. goto clk_fail;
  102. }
  103. /*
  104. * bit 28 and 29 represents the hwspinlock setup
  105. *
  106. * every datasheet (A64, A80, A83T, H3, H5, H6 ...) says the default value is 0x1 and 0x1
  107. * to 0x4 represent 32, 64, 128 and 256 locks
  108. * but later datasheets (H5, H6) say 00, 01, 10, 11 represent 32, 64, 128 and 256 locks,
  109. * but that would mean H5 and H6 have 64 locks, while their datasheets talk about 32 locks
  110. * all the time, not a single mentioning of 64 locks
  111. * the 0x4 value is also not representable by 2 bits alone, so some datasheets are not
  112. * correct
  113. * one thing have all in common, default value of the sysstatus register is 0x10000000,
  114. * which results in bit 28 being set
  115. * this is the reason 0x1 is considered being 32 locks and bit 30 is taken into account
  116. * verified on H2+ (datasheet 0x1 = 32 locks) and H5 (datasheet 01 = 64 locks)
  117. */
  118. num_banks = readl(io_base + SPINLOCK_SYSSTATUS_REG) >> 28;
  119. switch (num_banks) {
  120. case 1 ... 4:
  121. priv->nlocks = 1 << (4 + num_banks);
  122. break;
  123. default:
  124. err = -EINVAL;
  125. dev_err(&pdev->dev, "unsupported hwspinlock setup (%d)\n", num_banks);
  126. goto bank_fail;
  127. }
  128. priv->bank = devm_kzalloc(&pdev->dev, struct_size(priv->bank, lock, priv->nlocks),
  129. GFP_KERNEL);
  130. if (!priv->bank) {
  131. err = -ENOMEM;
  132. goto bank_fail;
  133. }
  134. for (i = 0; i < priv->nlocks; ++i) {
  135. hwlock = &priv->bank->lock[i];
  136. hwlock->priv = io_base + SPINLOCK_LOCK_REGN + sizeof(u32) * i;
  137. }
  138. /* failure of debugfs is considered non-fatal */
  139. sun6i_hwspinlock_debugfs_init(priv);
  140. if (IS_ERR(priv->debugfs))
  141. priv->debugfs = NULL;
  142. err = devm_add_action_or_reset(&pdev->dev, sun6i_hwspinlock_disable, priv);
  143. if (err) {
  144. dev_err(&pdev->dev, "failed to add hwspinlock disable action\n");
  145. goto bank_fail;
  146. }
  147. platform_set_drvdata(pdev, priv);
  148. return devm_hwspin_lock_register(&pdev->dev, priv->bank, &sun6i_hwspinlock_ops,
  149. SPINLOCK_BASE_ID, priv->nlocks);
  150. bank_fail:
  151. clk_disable_unprepare(priv->ahb_clk);
  152. clk_fail:
  153. reset_control_assert(priv->reset);
  154. return err;
  155. }
  156. static const struct of_device_id sun6i_hwspinlock_ids[] = {
  157. { .compatible = "allwinner,sun6i-a31-hwspinlock", },
  158. {},
  159. };
  160. MODULE_DEVICE_TABLE(of, sun6i_hwspinlock_ids);
  161. static struct platform_driver sun6i_hwspinlock_driver = {
  162. .probe = sun6i_hwspinlock_probe,
  163. .driver = {
  164. .name = DRIVER_NAME,
  165. .of_match_table = sun6i_hwspinlock_ids,
  166. },
  167. };
  168. module_platform_driver(sun6i_hwspinlock_driver);
  169. MODULE_LICENSE("GPL");
  170. MODULE_DESCRIPTION("SUN6I hardware spinlock driver");
  171. MODULE_AUTHOR("Wilken Gottwalt <[email protected]>");