reset-sunxi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Allwinner SoCs Reset Controller driver
  3. *
  4. * Copyright 2013 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. struct sunxi_reset_data {
  24. spinlock_t lock;
  25. void __iomem *membase;
  26. struct reset_controller_dev rcdev;
  27. };
  28. static int sunxi_reset_assert(struct reset_controller_dev *rcdev,
  29. unsigned long id)
  30. {
  31. struct sunxi_reset_data *data = container_of(rcdev,
  32. struct sunxi_reset_data,
  33. rcdev);
  34. int reg_width = sizeof(u32);
  35. int bank = id / (reg_width * BITS_PER_BYTE);
  36. int offset = id % (reg_width * BITS_PER_BYTE);
  37. unsigned long flags;
  38. u32 reg;
  39. spin_lock_irqsave(&data->lock, flags);
  40. reg = readl(data->membase + (bank * reg_width));
  41. writel(reg & ~BIT(offset), data->membase + (bank * reg_width));
  42. spin_unlock_irqrestore(&data->lock, flags);
  43. return 0;
  44. }
  45. static int sunxi_reset_deassert(struct reset_controller_dev *rcdev,
  46. unsigned long id)
  47. {
  48. struct sunxi_reset_data *data = container_of(rcdev,
  49. struct sunxi_reset_data,
  50. rcdev);
  51. int reg_width = sizeof(u32);
  52. int bank = id / (reg_width * BITS_PER_BYTE);
  53. int offset = id % (reg_width * BITS_PER_BYTE);
  54. unsigned long flags;
  55. u32 reg;
  56. spin_lock_irqsave(&data->lock, flags);
  57. reg = readl(data->membase + (bank * reg_width));
  58. writel(reg | BIT(offset), data->membase + (bank * reg_width));
  59. spin_unlock_irqrestore(&data->lock, flags);
  60. return 0;
  61. }
  62. static const struct reset_control_ops sunxi_reset_ops = {
  63. .assert = sunxi_reset_assert,
  64. .deassert = sunxi_reset_deassert,
  65. };
  66. static int sunxi_reset_init(struct device_node *np)
  67. {
  68. struct sunxi_reset_data *data;
  69. struct resource res;
  70. resource_size_t size;
  71. int ret;
  72. data = kzalloc(sizeof(*data), GFP_KERNEL);
  73. if (!data)
  74. return -ENOMEM;
  75. ret = of_address_to_resource(np, 0, &res);
  76. if (ret)
  77. goto err_alloc;
  78. size = resource_size(&res);
  79. if (!request_mem_region(res.start, size, np->name)) {
  80. ret = -EBUSY;
  81. goto err_alloc;
  82. }
  83. data->membase = ioremap(res.start, size);
  84. if (!data->membase) {
  85. ret = -ENOMEM;
  86. goto err_alloc;
  87. }
  88. spin_lock_init(&data->lock);
  89. data->rcdev.owner = THIS_MODULE;
  90. data->rcdev.nr_resets = size * 8;
  91. data->rcdev.ops = &sunxi_reset_ops;
  92. data->rcdev.of_node = np;
  93. return reset_controller_register(&data->rcdev);
  94. err_alloc:
  95. kfree(data);
  96. return ret;
  97. };
  98. /*
  99. * These are the reset controller we need to initialize early on in
  100. * our system, before we can even think of using a regular device
  101. * driver for it.
  102. */
  103. static const struct of_device_id sunxi_early_reset_dt_ids[] __initconst = {
  104. { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
  105. { /* sentinel */ },
  106. };
  107. void __init sun6i_reset_init(void)
  108. {
  109. struct device_node *np;
  110. for_each_matching_node(np, sunxi_early_reset_dt_ids)
  111. sunxi_reset_init(np);
  112. }
  113. /*
  114. * And these are the controllers we can register through the regular
  115. * device model.
  116. */
  117. static const struct of_device_id sunxi_reset_dt_ids[] = {
  118. { .compatible = "allwinner,sun6i-a31-clock-reset", },
  119. { /* sentinel */ },
  120. };
  121. static int sunxi_reset_probe(struct platform_device *pdev)
  122. {
  123. struct sunxi_reset_data *data;
  124. struct resource *res;
  125. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  126. if (!data)
  127. return -ENOMEM;
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. data->membase = devm_ioremap_resource(&pdev->dev, res);
  130. if (IS_ERR(data->membase))
  131. return PTR_ERR(data->membase);
  132. spin_lock_init(&data->lock);
  133. data->rcdev.owner = THIS_MODULE;
  134. data->rcdev.nr_resets = resource_size(res) * 8;
  135. data->rcdev.ops = &sunxi_reset_ops;
  136. data->rcdev.of_node = pdev->dev.of_node;
  137. return devm_reset_controller_register(&pdev->dev, &data->rcdev);
  138. }
  139. static struct platform_driver sunxi_reset_driver = {
  140. .probe = sunxi_reset_probe,
  141. .driver = {
  142. .name = "sunxi-reset",
  143. .of_match_table = sunxi_reset_dt_ids,
  144. },
  145. };
  146. builtin_platform_driver(sunxi_reset_driver);