reset-socfpga.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Socfpga Reset Controller Driver
  3. *
  4. * Copyright 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
  5. *
  6. * based on
  7. * Allwinner SoCs Reset Controller driver
  8. *
  9. * Copyright 2013 Maxime Ripard
  10. *
  11. * Maxime Ripard <maxime.ripard@free-electrons.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/init.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reset-controller.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. #define NR_BANKS 4
  27. struct socfpga_reset_data {
  28. spinlock_t lock;
  29. void __iomem *membase;
  30. struct reset_controller_dev rcdev;
  31. };
  32. static int socfpga_reset_assert(struct reset_controller_dev *rcdev,
  33. unsigned long id)
  34. {
  35. struct socfpga_reset_data *data = container_of(rcdev,
  36. struct socfpga_reset_data,
  37. rcdev);
  38. int bank = id / BITS_PER_LONG;
  39. int offset = id % BITS_PER_LONG;
  40. unsigned long flags;
  41. u32 reg;
  42. spin_lock_irqsave(&data->lock, flags);
  43. reg = readl(data->membase + (bank * NR_BANKS));
  44. writel(reg | BIT(offset), data->membase + (bank * NR_BANKS));
  45. spin_unlock_irqrestore(&data->lock, flags);
  46. return 0;
  47. }
  48. static int socfpga_reset_deassert(struct reset_controller_dev *rcdev,
  49. unsigned long id)
  50. {
  51. struct socfpga_reset_data *data = container_of(rcdev,
  52. struct socfpga_reset_data,
  53. rcdev);
  54. int bank = id / BITS_PER_LONG;
  55. int offset = id % BITS_PER_LONG;
  56. unsigned long flags;
  57. u32 reg;
  58. spin_lock_irqsave(&data->lock, flags);
  59. reg = readl(data->membase + (bank * NR_BANKS));
  60. writel(reg & ~BIT(offset), data->membase + (bank * NR_BANKS));
  61. spin_unlock_irqrestore(&data->lock, flags);
  62. return 0;
  63. }
  64. static int socfpga_reset_status(struct reset_controller_dev *rcdev,
  65. unsigned long id)
  66. {
  67. struct socfpga_reset_data *data = container_of(rcdev,
  68. struct socfpga_reset_data, rcdev);
  69. int bank = id / BITS_PER_LONG;
  70. int offset = id % BITS_PER_LONG;
  71. u32 reg;
  72. reg = readl(data->membase + (bank * NR_BANKS));
  73. return !(reg & BIT(offset));
  74. }
  75. static const struct reset_control_ops socfpga_reset_ops = {
  76. .assert = socfpga_reset_assert,
  77. .deassert = socfpga_reset_deassert,
  78. .status = socfpga_reset_status,
  79. };
  80. static int socfpga_reset_probe(struct platform_device *pdev)
  81. {
  82. struct socfpga_reset_data *data;
  83. struct resource *res;
  84. struct device *dev = &pdev->dev;
  85. struct device_node *np = dev->of_node;
  86. u32 modrst_offset;
  87. /*
  88. * The binding was mainlined without the required property.
  89. * Do not continue, when we encounter an old DT.
  90. */
  91. if (!of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
  92. dev_err(&pdev->dev, "%s missing #reset-cells property\n",
  93. pdev->dev.of_node->full_name);
  94. return -EINVAL;
  95. }
  96. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  97. if (!data)
  98. return -ENOMEM;
  99. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. data->membase = devm_ioremap_resource(&pdev->dev, res);
  101. if (IS_ERR(data->membase))
  102. return PTR_ERR(data->membase);
  103. if (of_property_read_u32(np, "altr,modrst-offset", &modrst_offset)) {
  104. dev_warn(dev, "missing altr,modrst-offset property, assuming 0x10!\n");
  105. modrst_offset = 0x10;
  106. }
  107. data->membase += modrst_offset;
  108. spin_lock_init(&data->lock);
  109. data->rcdev.owner = THIS_MODULE;
  110. data->rcdev.nr_resets = NR_BANKS * BITS_PER_LONG;
  111. data->rcdev.ops = &socfpga_reset_ops;
  112. data->rcdev.of_node = pdev->dev.of_node;
  113. return devm_reset_controller_register(dev, &data->rcdev);
  114. }
  115. static const struct of_device_id socfpga_reset_dt_ids[] = {
  116. { .compatible = "altr,rst-mgr", },
  117. { /* sentinel */ },
  118. };
  119. static struct platform_driver socfpga_reset_driver = {
  120. .probe = socfpga_reset_probe,
  121. .driver = {
  122. .name = "socfpga-reset",
  123. .of_match_table = socfpga_reset_dt_ids,
  124. },
  125. };
  126. builtin_platform_driver(socfpga_reset_driver);