reset-sunxi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Allwinner SoCs Reset Controller driver
  3. *
  4. * Copyright 2013 Maxime Ripard
  5. *
  6. * Maxime Ripard <[email protected]>
  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 const 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. return reset_controller_register(&data->rcdev);
  92. err_alloc:
  93. kfree(data);
  94. return ret;
  95. };
  96. /*
  97. * These are the reset controller we need to initialize early on in
  98. * our system, before we can even think of using a regular device
  99. * driver for it.
  100. */
  101. static const struct of_device_id sunxi_early_reset_dt_ids[] __initconst = {
  102. { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
  103. { /* sentinel */ },
  104. };
  105. void __init sun6i_reset_init(void)
  106. {
  107. struct device_node *np;
  108. for_each_matching_node(np, sunxi_early_reset_dt_ids)
  109. sunxi_reset_init(np);
  110. }
  111. /*
  112. * And these are the controllers we can register through the regular
  113. * device model.
  114. */
  115. static const struct of_device_id sunxi_reset_dt_ids[] = {
  116. { .compatible = "allwinner,sun6i-a31-clock-reset", },
  117. { /* sentinel */ },
  118. };
  119. MODULE_DEVICE_TABLE(of, sunxi_reset_dt_ids);
  120. static int sunxi_reset_probe(struct platform_device *pdev)
  121. {
  122. struct sunxi_reset_data *data;
  123. struct resource *res;
  124. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  125. if (!data)
  126. return -ENOMEM;
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. data->membase = devm_ioremap_resource(&pdev->dev, res);
  129. if (IS_ERR(data->membase))
  130. return PTR_ERR(data->membase);
  131. spin_lock_init(&data->lock);
  132. data->rcdev.owner = THIS_MODULE;
  133. data->rcdev.nr_resets = resource_size(res) * 32;
  134. data->rcdev.ops = &sunxi_reset_ops;
  135. data->rcdev.of_node = pdev->dev.of_node;
  136. return devm_reset_controller_register(&pdev->dev, &data->rcdev);
  137. }
  138. static struct platform_driver sunxi_reset_driver = {
  139. .probe = sunxi_reset_probe,
  140. .driver = {
  141. .name = "sunxi-reset",
  142. .of_match_table = sunxi_reset_dt_ids,
  143. },
  144. };
  145. module_platform_driver(sunxi_reset_driver);
  146. MODULE_AUTHOR("Maxime Ripard <[email protected]");
  147. MODULE_DESCRIPTION("Allwinner SoCs Reset Controller Driver");
  148. MODULE_LICENSE("GPL");