reset-socfpga.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
  3. *
  4. * based on
  5. * Allwinner SoCs Reset Controller driver
  6. *
  7. * Copyright 2013 Maxime Ripard
  8. *
  9. * Maxime Ripard <maxime.ripard@free-electrons.com>
  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. #define OFFSET_MODRST 0x10
  26. struct socfpga_reset_data {
  27. spinlock_t lock;
  28. void __iomem *membase;
  29. struct reset_controller_dev rcdev;
  30. };
  31. static int socfpga_reset_assert(struct reset_controller_dev *rcdev,
  32. unsigned long id)
  33. {
  34. struct socfpga_reset_data *data = container_of(rcdev,
  35. struct socfpga_reset_data,
  36. rcdev);
  37. int bank = id / BITS_PER_LONG;
  38. int offset = id % BITS_PER_LONG;
  39. unsigned long flags;
  40. u32 reg;
  41. spin_lock_irqsave(&data->lock, flags);
  42. reg = readl(data->membase + OFFSET_MODRST + (bank * NR_BANKS));
  43. writel(reg | BIT(offset), data->membase + OFFSET_MODRST +
  44. (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 + OFFSET_MODRST + (bank * NR_BANKS));
  60. writel(reg & ~BIT(offset), data->membase + OFFSET_MODRST +
  61. (bank * NR_BANKS));
  62. spin_unlock_irqrestore(&data->lock, flags);
  63. return 0;
  64. }
  65. static struct reset_control_ops socfpga_reset_ops = {
  66. .assert = socfpga_reset_assert,
  67. .deassert = socfpga_reset_deassert,
  68. };
  69. static int socfpga_reset_probe(struct platform_device *pdev)
  70. {
  71. struct socfpga_reset_data *data;
  72. struct resource *res;
  73. /*
  74. * The binding was mainlined without the required property.
  75. * Do not continue, when we encounter an old DT.
  76. */
  77. if (!of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
  78. dev_err(&pdev->dev, "%s missing #reset-cells property\n",
  79. pdev->dev.of_node->full_name);
  80. return -EINVAL;
  81. }
  82. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  83. if (!data)
  84. return -ENOMEM;
  85. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. data->membase = devm_ioremap_resource(&pdev->dev, res);
  87. if (IS_ERR(data->membase))
  88. return PTR_ERR(data->membase);
  89. spin_lock_init(&data->lock);
  90. data->rcdev.owner = THIS_MODULE;
  91. data->rcdev.nr_resets = NR_BANKS * BITS_PER_LONG;
  92. data->rcdev.ops = &socfpga_reset_ops;
  93. data->rcdev.of_node = pdev->dev.of_node;
  94. reset_controller_register(&data->rcdev);
  95. return 0;
  96. }
  97. static int socfpga_reset_remove(struct platform_device *pdev)
  98. {
  99. struct socfpga_reset_data *data = platform_get_drvdata(pdev);
  100. reset_controller_unregister(&data->rcdev);
  101. return 0;
  102. }
  103. static const struct of_device_id socfpga_reset_dt_ids[] = {
  104. { .compatible = "altr,rst-mgr", },
  105. { /* sentinel */ },
  106. };
  107. static struct platform_driver socfpga_reset_driver = {
  108. .probe = socfpga_reset_probe,
  109. .remove = socfpga_reset_remove,
  110. .driver = {
  111. .name = "socfpga-reset",
  112. .owner = THIS_MODULE,
  113. .of_match_table = socfpga_reset_dt_ids,
  114. },
  115. };
  116. module_platform_driver(socfpga_reset_driver);
  117. MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de");
  118. MODULE_DESCRIPTION("Socfpga Reset Controller Driver");
  119. MODULE_LICENSE("GPL");