stm32_hwspinlock.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <dm.h>
  8. #include <hwspinlock.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <linux/bitops.h>
  12. #define STM32_MUTEX_COREID BIT(8)
  13. #define STM32_MUTEX_LOCK_BIT BIT(31)
  14. #define STM32_MUTEX_NUM_LOCKS 32
  15. struct stm32mp1_hws_priv {
  16. fdt_addr_t base;
  17. };
  18. static int stm32mp1_lock(struct udevice *dev, int index)
  19. {
  20. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  21. u32 status;
  22. if (index >= STM32_MUTEX_NUM_LOCKS)
  23. return -EINVAL;
  24. status = readl(priv->base + index * sizeof(u32));
  25. if (status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
  26. return -EBUSY;
  27. writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID,
  28. priv->base + index * sizeof(u32));
  29. status = readl(priv->base + index * sizeof(u32));
  30. if (status != (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
  31. return -EINVAL;
  32. return 0;
  33. }
  34. static int stm32mp1_unlock(struct udevice *dev, int index)
  35. {
  36. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  37. if (index >= STM32_MUTEX_NUM_LOCKS)
  38. return -EINVAL;
  39. writel(STM32_MUTEX_COREID, priv->base + index * sizeof(u32));
  40. return 0;
  41. }
  42. static int stm32mp1_hwspinlock_probe(struct udevice *dev)
  43. {
  44. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  45. struct clk clk;
  46. int ret;
  47. priv->base = dev_read_addr(dev);
  48. if (priv->base == FDT_ADDR_T_NONE)
  49. return -EINVAL;
  50. ret = clk_get_by_index(dev, 0, &clk);
  51. if (ret)
  52. return ret;
  53. ret = clk_enable(&clk);
  54. if (ret)
  55. clk_free(&clk);
  56. return ret;
  57. }
  58. static const struct hwspinlock_ops stm32mp1_hwspinlock_ops = {
  59. .lock = stm32mp1_lock,
  60. .unlock = stm32mp1_unlock,
  61. };
  62. static const struct udevice_id stm32mp1_hwspinlock_ids[] = {
  63. { .compatible = "st,stm32-hwspinlock" },
  64. {}
  65. };
  66. U_BOOT_DRIVER(hwspinlock_stm32mp1) = {
  67. .name = "hwspinlock_stm32mp1",
  68. .id = UCLASS_HWSPINLOCK,
  69. .of_match = stm32mp1_hwspinlock_ids,
  70. .ops = &stm32mp1_hwspinlock_ops,
  71. .probe = stm32mp1_hwspinlock_probe,
  72. .priv_auto_alloc_size = sizeof(struct stm32mp1_hws_priv),
  73. };