reset-simple.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Simple Reset Controller Driver
  3. *
  4. * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
  5. *
  6. * Based on Allwinner SoCs Reset Controller driver
  7. *
  8. * Copyright 2013 Maxime Ripard
  9. *
  10. * Maxime Ripard <maxime.ripard@free-electrons.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reset-controller.h>
  24. #include <linux/spinlock.h>
  25. #include "reset-simple.h"
  26. static inline struct reset_simple_data *
  27. to_reset_simple_data(struct reset_controller_dev *rcdev)
  28. {
  29. return container_of(rcdev, struct reset_simple_data, rcdev);
  30. }
  31. static int reset_simple_update(struct reset_controller_dev *rcdev,
  32. unsigned long id, bool assert)
  33. {
  34. struct reset_simple_data *data = to_reset_simple_data(rcdev);
  35. int reg_width = sizeof(u32);
  36. int bank = id / (reg_width * BITS_PER_BYTE);
  37. int offset = id % (reg_width * BITS_PER_BYTE);
  38. unsigned long flags;
  39. u32 reg;
  40. spin_lock_irqsave(&data->lock, flags);
  41. reg = readl(data->membase + (bank * reg_width));
  42. if (assert ^ data->active_low)
  43. reg |= BIT(offset);
  44. else
  45. reg &= ~BIT(offset);
  46. writel(reg, data->membase + (bank * reg_width));
  47. spin_unlock_irqrestore(&data->lock, flags);
  48. return 0;
  49. }
  50. static int reset_simple_assert(struct reset_controller_dev *rcdev,
  51. unsigned long id)
  52. {
  53. return reset_simple_update(rcdev, id, true);
  54. }
  55. static int reset_simple_deassert(struct reset_controller_dev *rcdev,
  56. unsigned long id)
  57. {
  58. return reset_simple_update(rcdev, id, false);
  59. }
  60. static int reset_simple_status(struct reset_controller_dev *rcdev,
  61. unsigned long id)
  62. {
  63. struct reset_simple_data *data = to_reset_simple_data(rcdev);
  64. int reg_width = sizeof(u32);
  65. int bank = id / (reg_width * BITS_PER_BYTE);
  66. int offset = id % (reg_width * BITS_PER_BYTE);
  67. u32 reg;
  68. reg = readl(data->membase + (bank * reg_width));
  69. return !(reg & BIT(offset)) ^ !data->status_active_low;
  70. }
  71. const struct reset_control_ops reset_simple_ops = {
  72. .assert = reset_simple_assert,
  73. .deassert = reset_simple_deassert,
  74. .status = reset_simple_status,
  75. };
  76. EXPORT_SYMBOL_GPL(reset_simple_ops);
  77. /**
  78. * struct reset_simple_devdata - simple reset controller properties
  79. * @reg_offset: offset between base address and first reset register.
  80. * @nr_resets: number of resets. If not set, default to resource size in bits.
  81. * @active_low: if true, bits are cleared to assert the reset. Otherwise, bits
  82. * are set to assert the reset.
  83. * @status_active_low: if true, bits read back as cleared while the reset is
  84. * asserted. Otherwise, bits read back as set while the
  85. * reset is asserted.
  86. */
  87. struct reset_simple_devdata {
  88. u32 reg_offset;
  89. u32 nr_resets;
  90. bool active_low;
  91. bool status_active_low;
  92. };
  93. #define SOCFPGA_NR_BANKS 8
  94. static const struct reset_simple_devdata reset_simple_socfpga = {
  95. .reg_offset = 0x10,
  96. .nr_resets = SOCFPGA_NR_BANKS * 32,
  97. .status_active_low = true,
  98. };
  99. static const struct reset_simple_devdata reset_simple_active_low = {
  100. .active_low = true,
  101. .status_active_low = true,
  102. };
  103. static const struct of_device_id reset_simple_dt_ids[] = {
  104. { .compatible = "altr,rst-mgr", .data = &reset_simple_socfpga },
  105. { .compatible = "st,stm32-rcc", },
  106. { .compatible = "allwinner,sun6i-a31-clock-reset",
  107. .data = &reset_simple_active_low },
  108. { .compatible = "zte,zx296718-reset",
  109. .data = &reset_simple_active_low },
  110. { .compatible = "aspeed,ast2400-lpc-reset" },
  111. { .compatible = "aspeed,ast2500-lpc-reset" },
  112. { /* sentinel */ },
  113. };
  114. static int reset_simple_probe(struct platform_device *pdev)
  115. {
  116. struct device *dev = &pdev->dev;
  117. const struct reset_simple_devdata *devdata;
  118. struct reset_simple_data *data;
  119. void __iomem *membase;
  120. struct resource *res;
  121. u32 reg_offset = 0;
  122. devdata = of_device_get_match_data(dev);
  123. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  124. if (!data)
  125. return -ENOMEM;
  126. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  127. membase = devm_ioremap_resource(dev, res);
  128. if (IS_ERR(membase))
  129. return PTR_ERR(membase);
  130. spin_lock_init(&data->lock);
  131. data->membase = membase;
  132. data->rcdev.owner = THIS_MODULE;
  133. data->rcdev.nr_resets = resource_size(res) * BITS_PER_BYTE;
  134. data->rcdev.ops = &reset_simple_ops;
  135. data->rcdev.of_node = dev->of_node;
  136. if (devdata) {
  137. reg_offset = devdata->reg_offset;
  138. if (devdata->nr_resets)
  139. data->rcdev.nr_resets = devdata->nr_resets;
  140. data->active_low = devdata->active_low;
  141. data->status_active_low = devdata->status_active_low;
  142. }
  143. if (of_device_is_compatible(dev->of_node, "altr,rst-mgr") &&
  144. of_property_read_u32(dev->of_node, "altr,modrst-offset",
  145. &reg_offset)) {
  146. dev_warn(dev,
  147. "missing altr,modrst-offset property, assuming 0x%x!\n",
  148. reg_offset);
  149. }
  150. data->membase += reg_offset;
  151. return devm_reset_controller_register(dev, &data->rcdev);
  152. }
  153. static struct platform_driver reset_simple_driver = {
  154. .probe = reset_simple_probe,
  155. .driver = {
  156. .name = "simple-reset",
  157. .of_match_table = reset_simple_dt_ids,
  158. },
  159. };
  160. builtin_platform_driver(reset_simple_driver);