reset-ark.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Arkmidro Reset Controller Driver
  3. * Author: Sim
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/property.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_platform.h>
  25. struct ark_reset {
  26. struct reset_controller_dev rcdev;
  27. void __iomem *base;
  28. spinlock_t lock;
  29. };
  30. static int ark_reset_update(struct reset_controller_dev *rcdev,
  31. unsigned long id, bool assert)
  32. {
  33. struct ark_reset *ark_reset =
  34. container_of(rcdev, struct ark_reset, rcdev);
  35. unsigned long flags;
  36. u32 reg;
  37. u32 offset;
  38. u32 val;
  39. reg = (id >> 8) & 0xfff;
  40. offset = id & 0xff;
  41. if (offset > 31) {
  42. printk(KERN_ALERT "ark ret wrong bit offset %u.\n", offset);
  43. return -EINVAL;
  44. }
  45. spin_lock_irqsave(&ark_reset->lock, flags);
  46. val = readl(ark_reset->base + reg);
  47. if (assert)
  48. val &= ~BIT(offset);
  49. else
  50. val |= BIT(offset);
  51. writel(val, ark_reset->base + reg);
  52. spin_unlock_irqrestore(&ark_reset->lock, flags);
  53. return 0;
  54. }
  55. static int ark_reset_assert(struct reset_controller_dev *rcdev,
  56. unsigned long id)
  57. {
  58. return ark_reset_update(rcdev, id, true);
  59. }
  60. static int ark_reset_deassert(struct reset_controller_dev *rcdev,
  61. unsigned long id)
  62. {
  63. return ark_reset_update(rcdev, id, false);
  64. }
  65. static int ark_reset_status(struct reset_controller_dev *rcdev,
  66. unsigned long id)
  67. {
  68. struct ark_reset *ark_reset =
  69. container_of(rcdev, struct ark_reset, rcdev);
  70. u32 val;
  71. val = readl(ark_reset->base);
  72. return !!(val & BIT(id));
  73. }
  74. static const struct reset_control_ops ark_reset_ops = {
  75. .assert = ark_reset_assert,
  76. .deassert = ark_reset_deassert,
  77. .status = ark_reset_status,
  78. };
  79. static int ark_reset_xlate(struct reset_controller_dev *rcdev,
  80. const struct of_phandle_args *reset_spec)
  81. {
  82. unsigned int reg, offset;
  83. reg = reset_spec->args[0];
  84. offset = reset_spec->args[1];
  85. return (reg << 8) | offset;
  86. }
  87. static int ark_reset_probe(struct platform_device *pdev)
  88. {
  89. struct ark_reset *ark_reset;
  90. struct resource *res;
  91. int err;
  92. ark_reset = devm_kzalloc(&pdev->dev,
  93. sizeof(*ark_reset), GFP_KERNEL);
  94. if (!ark_reset)
  95. return -ENOMEM;
  96. platform_set_drvdata(pdev, ark_reset);
  97. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  98. ark_reset->base = ioremap(res->start, resource_size(res)); /* baseaddr conflict */
  99. if (IS_ERR(ark_reset->base)) {
  100. return PTR_ERR(ark_reset->base);
  101. }
  102. spin_lock_init(&ark_reset->lock);
  103. ark_reset->rcdev.ops = &ark_reset_ops;
  104. ark_reset->rcdev.owner = THIS_MODULE;
  105. ark_reset->rcdev.of_node = pdev->dev.of_node;
  106. ark_reset->rcdev.of_reset_n_cells = 2;
  107. ark_reset->rcdev.of_xlate = ark_reset_xlate;
  108. err = devm_reset_controller_register(&pdev->dev, &ark_reset->rcdev);
  109. if (err)
  110. return err;
  111. return 0;
  112. }
  113. static const struct of_device_id ark_reset_dt_ids[] = {
  114. {
  115. .compatible = "arkmicro,ark-reset",
  116. } , {
  117. /* sentinel */
  118. }
  119. };
  120. MODULE_DEVICE_TABLE(of, ark_reset_dt_ids);
  121. static struct platform_driver ark_reset_driver = {
  122. .probe = ark_reset_probe,
  123. .driver = {
  124. .name = "ark-reset",
  125. .of_match_table = of_match_ptr(ark_reset_dt_ids),
  126. },
  127. };
  128. static int __init ark_reset_init(void)
  129. {
  130. int ret;
  131. ret = platform_driver_register(&ark_reset_driver);
  132. if (ret)
  133. return ret;
  134. return 0;
  135. }
  136. postcore_initcall(ark_reset_init);