stm32_hwspinlock.c 2.1 KB

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