reset-microchip-sparx5.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Microchip Sparx5 Switch Reset driver
  3. *
  4. * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
  5. *
  6. * The Sparx5 Chip Register Model can be browsed at this location:
  7. * https://github.com/microchip-ung/sparx-5_reginfo
  8. */
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset-controller.h>
  17. struct reset_props {
  18. u32 protect_reg;
  19. u32 protect_bit;
  20. u32 reset_reg;
  21. u32 reset_bit;
  22. };
  23. struct mchp_reset_context {
  24. struct regmap *cpu_ctrl;
  25. struct regmap *gcb_ctrl;
  26. struct reset_controller_dev rcdev;
  27. const struct reset_props *props;
  28. };
  29. static struct regmap_config sparx5_reset_regmap_config = {
  30. .reg_bits = 32,
  31. .val_bits = 32,
  32. .reg_stride = 4,
  33. };
  34. static int sparx5_switch_reset(struct mchp_reset_context *ctx)
  35. {
  36. u32 val;
  37. /* Make sure the core is PROTECTED from reset */
  38. regmap_update_bits(ctx->cpu_ctrl, ctx->props->protect_reg,
  39. ctx->props->protect_bit, ctx->props->protect_bit);
  40. /* Start soft reset */
  41. regmap_write(ctx->gcb_ctrl, ctx->props->reset_reg,
  42. ctx->props->reset_bit);
  43. /* Wait for soft reset done */
  44. return regmap_read_poll_timeout(ctx->gcb_ctrl, ctx->props->reset_reg, val,
  45. (val & ctx->props->reset_bit) == 0,
  46. 1, 100);
  47. }
  48. static int sparx5_reset_noop(struct reset_controller_dev *rcdev,
  49. unsigned long id)
  50. {
  51. return 0;
  52. }
  53. static const struct reset_control_ops sparx5_reset_ops = {
  54. .reset = sparx5_reset_noop,
  55. };
  56. static const struct regmap_config mchp_lan966x_syscon_regmap_config = {
  57. .reg_bits = 32,
  58. .val_bits = 32,
  59. .reg_stride = 4,
  60. };
  61. static struct regmap *mchp_lan966x_syscon_to_regmap(struct device *dev,
  62. struct device_node *syscon_np)
  63. {
  64. struct regmap_config regmap_config = mchp_lan966x_syscon_regmap_config;
  65. struct resource res;
  66. void __iomem *base;
  67. int err;
  68. err = of_address_to_resource(syscon_np, 0, &res);
  69. if (err)
  70. return ERR_PTR(err);
  71. /* It is not possible to use devm_of_iomap because this resource is
  72. * shared with other drivers.
  73. */
  74. base = devm_ioremap(dev, res.start, resource_size(&res));
  75. if (!base)
  76. return ERR_PTR(-ENOMEM);
  77. regmap_config.max_register = resource_size(&res) - 4;
  78. return devm_regmap_init_mmio(dev, base, &regmap_config);
  79. }
  80. static int mchp_sparx5_map_syscon(struct platform_device *pdev, char *name,
  81. struct regmap **target)
  82. {
  83. struct device_node *syscon_np;
  84. struct regmap *regmap;
  85. int err;
  86. syscon_np = of_parse_phandle(pdev->dev.of_node, name, 0);
  87. if (!syscon_np)
  88. return -ENODEV;
  89. /*
  90. * The syscon API doesn't support syscon device removal.
  91. * When used in LAN966x PCI device, the cpu-syscon device needs to be
  92. * removed when the PCI device is removed.
  93. * In case of LAN966x, map the syscon device locally to support the
  94. * device removal.
  95. */
  96. if (of_device_is_compatible(pdev->dev.of_node, "microchip,lan966x-switch-reset"))
  97. regmap = mchp_lan966x_syscon_to_regmap(&pdev->dev, syscon_np);
  98. else
  99. regmap = syscon_node_to_regmap(syscon_np);
  100. of_node_put(syscon_np);
  101. if (IS_ERR(regmap)) {
  102. err = PTR_ERR(regmap);
  103. dev_err(&pdev->dev, "No '%s' map: %d\n", name, err);
  104. return err;
  105. }
  106. *target = regmap;
  107. return 0;
  108. }
  109. static int mchp_sparx5_map_io(struct platform_device *pdev, int index,
  110. struct regmap **target)
  111. {
  112. struct resource *res;
  113. struct regmap *map;
  114. void __iomem *mem;
  115. mem = devm_platform_get_and_ioremap_resource(pdev, index, &res);
  116. if (IS_ERR(mem)) {
  117. dev_err(&pdev->dev, "Could not map resource %d\n", index);
  118. return PTR_ERR(mem);
  119. }
  120. sparx5_reset_regmap_config.name = res->name;
  121. map = devm_regmap_init_mmio(&pdev->dev, mem, &sparx5_reset_regmap_config);
  122. if (IS_ERR(map))
  123. return PTR_ERR(map);
  124. *target = map;
  125. return 0;
  126. }
  127. static int mchp_sparx5_reset_probe(struct platform_device *pdev)
  128. {
  129. struct device_node *dn = pdev->dev.of_node;
  130. struct mchp_reset_context *ctx;
  131. int err;
  132. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  133. if (!ctx)
  134. return -ENOMEM;
  135. err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);
  136. if (err)
  137. return err;
  138. err = mchp_sparx5_map_io(pdev, 0, &ctx->gcb_ctrl);
  139. if (err)
  140. return err;
  141. ctx->rcdev.owner = THIS_MODULE;
  142. ctx->rcdev.dev = &pdev->dev;
  143. ctx->rcdev.nr_resets = 1;
  144. ctx->rcdev.ops = &sparx5_reset_ops;
  145. ctx->rcdev.of_node = dn;
  146. ctx->props = device_get_match_data(&pdev->dev);
  147. /* Issue the reset very early, our actual reset callback is a noop. */
  148. err = sparx5_switch_reset(ctx);
  149. if (err)
  150. return err;
  151. return devm_reset_controller_register(&pdev->dev, &ctx->rcdev);
  152. }
  153. static const struct reset_props reset_props_sparx5 = {
  154. .protect_reg = 0x84,
  155. .protect_bit = BIT(10),
  156. .reset_reg = 0x0,
  157. .reset_bit = BIT(1),
  158. };
  159. static const struct reset_props reset_props_lan966x = {
  160. .protect_reg = 0x88,
  161. .protect_bit = BIT(5),
  162. .reset_reg = 0x0,
  163. .reset_bit = BIT(1),
  164. };
  165. static const struct of_device_id mchp_sparx5_reset_of_match[] = {
  166. {
  167. .compatible = "microchip,sparx5-switch-reset",
  168. .data = &reset_props_sparx5,
  169. }, {
  170. .compatible = "microchip,lan966x-switch-reset",
  171. .data = &reset_props_lan966x,
  172. },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(of, mchp_sparx5_reset_of_match);
  176. static struct platform_driver mchp_sparx5_reset_driver = {
  177. .probe = mchp_sparx5_reset_probe,
  178. .driver = {
  179. .name = "sparx5-switch-reset",
  180. .of_match_table = mchp_sparx5_reset_of_match,
  181. },
  182. };
  183. static int __init mchp_sparx5_reset_init(void)
  184. {
  185. return platform_driver_register(&mchp_sparx5_reset_driver);
  186. }
  187. /*
  188. * Because this is a global reset, keep this postcore_initcall() to issue the
  189. * reset as early as possible during the kernel startup.
  190. */
  191. postcore_initcall(mchp_sparx5_reset_init);
  192. MODULE_DESCRIPTION("Microchip Sparx5 switch reset driver");
  193. MODULE_AUTHOR("Steen Hegelund <steen.hegelund@microchip.com>");
  194. MODULE_LICENSE("GPL");