stm32_hwspinlock.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #define STM32_MUTEX_COREID BIT(8)
  12. #define STM32_MUTEX_LOCK_BIT BIT(31)
  13. #define STM32_MUTEX_NUM_LOCKS 32
  14. struct stm32mp1_hws_priv {
  15. fdt_addr_t base;
  16. };
  17. static int stm32mp1_lock(struct udevice *dev, int index)
  18. {
  19. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  20. u32 status;
  21. if (index >= STM32_MUTEX_NUM_LOCKS)
  22. return -EINVAL;
  23. status = readl(priv->base + index * sizeof(u32));
  24. if (status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
  25. return -EBUSY;
  26. writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID,
  27. priv->base + index * sizeof(u32));
  28. status = readl(priv->base + index * sizeof(u32));
  29. if (status != (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
  30. return -EINVAL;
  31. return 0;
  32. }
  33. static int stm32mp1_unlock(struct udevice *dev, int index)
  34. {
  35. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  36. if (index >= STM32_MUTEX_NUM_LOCKS)
  37. return -EINVAL;
  38. writel(STM32_MUTEX_COREID, priv->base + index * sizeof(u32));
  39. return 0;
  40. }
  41. static int stm32mp1_hwspinlock_probe(struct udevice *dev)
  42. {
  43. struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
  44. struct clk clk;
  45. int ret;
  46. priv->base = dev_read_addr(dev);
  47. if (priv->base == FDT_ADDR_T_NONE)
  48. return -EINVAL;
  49. ret = clk_get_by_index(dev, 0, &clk);
  50. if (ret)
  51. return ret;
  52. ret = clk_enable(&clk);
  53. if (ret)
  54. clk_free(&clk);
  55. return ret;
  56. }
  57. static const struct hwspinlock_ops stm32mp1_hwspinlock_ops = {
  58. .lock = stm32mp1_lock,
  59. .unlock = stm32mp1_unlock,
  60. };
  61. static const struct udevice_id stm32mp1_hwspinlock_ids[] = {
  62. { .compatible = "st,stm32-hwspinlock" },
  63. {}
  64. };
  65. U_BOOT_DRIVER(hwspinlock_stm32mp1) = {
  66. .name = "hwspinlock_stm32mp1",
  67. .id = UCLASS_HWSPINLOCK,
  68. .of_match = stm32mp1_hwspinlock_ids,
  69. .ops = &stm32mp1_hwspinlock_ops,
  70. .probe = stm32mp1_hwspinlock_probe,
  71. .priv_auto_alloc_size = sizeof(struct stm32mp1_hws_priv),
  72. };