reset-socfpga.c 4.0 KB

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