reset-sunxi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/module.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 bank = id / BITS_PER_LONG;
  35. int offset = id % BITS_PER_LONG;
  36. unsigned long flags;
  37. u32 reg;
  38. spin_lock_irqsave(&data->lock, flags);
  39. reg = readl(data->membase + (bank * 4));
  40. writel(reg & ~BIT(offset), data->membase + (bank * 4));
  41. spin_unlock_irqrestore(&data->lock, flags);
  42. return 0;
  43. }
  44. static int sunxi_reset_deassert(struct reset_controller_dev *rcdev,
  45. unsigned long id)
  46. {
  47. struct sunxi_reset_data *data = container_of(rcdev,
  48. struct sunxi_reset_data,
  49. rcdev);
  50. int bank = id / BITS_PER_LONG;
  51. int offset = id % BITS_PER_LONG;
  52. unsigned long flags;
  53. u32 reg;
  54. spin_lock_irqsave(&data->lock, flags);
  55. reg = readl(data->membase + (bank * 4));
  56. writel(reg | BIT(offset), data->membase + (bank * 4));
  57. spin_unlock_irqrestore(&data->lock, flags);
  58. return 0;
  59. }
  60. static struct reset_control_ops sunxi_reset_ops = {
  61. .assert = sunxi_reset_assert,
  62. .deassert = sunxi_reset_deassert,
  63. };
  64. static int sunxi_reset_init(struct device_node *np)
  65. {
  66. struct sunxi_reset_data *data;
  67. struct resource res;
  68. resource_size_t size;
  69. int ret;
  70. data = kzalloc(sizeof(*data), GFP_KERNEL);
  71. if (!data)
  72. return -ENOMEM;
  73. ret = of_address_to_resource(np, 0, &res);
  74. if (ret)
  75. goto err_alloc;
  76. size = resource_size(&res);
  77. if (!request_mem_region(res.start, size, np->name)) {
  78. ret = -EBUSY;
  79. goto err_alloc;
  80. }
  81. data->membase = ioremap(res.start, size);
  82. if (!data->membase) {
  83. ret = -ENOMEM;
  84. goto err_alloc;
  85. }
  86. spin_lock_init(&data->lock);
  87. data->rcdev.owner = THIS_MODULE;
  88. data->rcdev.nr_resets = size * 32;
  89. data->rcdev.ops = &sunxi_reset_ops;
  90. data->rcdev.of_node = np;
  91. reset_controller_register(&data->rcdev);
  92. return 0;
  93. err_alloc:
  94. kfree(data);
  95. return ret;
  96. };
  97. /*
  98. * These are the reset controller we need to initialize early on in
  99. * our system, before we can even think of using a regular device
  100. * driver for it.
  101. */
  102. static const struct of_device_id sunxi_early_reset_dt_ids[] __initdata = {
  103. { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
  104. { /* sentinel */ },
  105. };
  106. void __init sun6i_reset_init(void)
  107. {
  108. struct device_node *np;
  109. for_each_matching_node(np, sunxi_early_reset_dt_ids)
  110. sunxi_reset_init(np);
  111. }
  112. /*
  113. * And these are the controllers we can register through the regular
  114. * device model.
  115. */
  116. static const struct of_device_id sunxi_reset_dt_ids[] = {
  117. { .compatible = "allwinner,sun6i-a31-clock-reset", },
  118. { /* sentinel */ },
  119. };
  120. MODULE_DEVICE_TABLE(of, sunxi_reset_dt_ids);
  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) * 32;
  135. data->rcdev.ops = &sunxi_reset_ops;
  136. data->rcdev.of_node = pdev->dev.of_node;
  137. return reset_controller_register(&data->rcdev);
  138. }
  139. static int sunxi_reset_remove(struct platform_device *pdev)
  140. {
  141. struct sunxi_reset_data *data = platform_get_drvdata(pdev);
  142. reset_controller_unregister(&data->rcdev);
  143. return 0;
  144. }
  145. static struct platform_driver sunxi_reset_driver = {
  146. .probe = sunxi_reset_probe,
  147. .remove = sunxi_reset_remove,
  148. .driver = {
  149. .name = "sunxi-reset",
  150. .of_match_table = sunxi_reset_dt_ids,
  151. },
  152. };
  153. module_platform_driver(sunxi_reset_driver);
  154. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  155. MODULE_DESCRIPTION("Allwinner SoCs Reset Controller Driver");
  156. MODULE_LICENSE("GPL");