mtk-infracfg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2015 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/export.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/regmap.h>
  16. #include <linux/soc/mediatek/infracfg.h>
  17. #include <asm/processor.h>
  18. #define INFRA_TOPAXI_PROTECTEN 0x0220
  19. #define INFRA_TOPAXI_PROTECTSTA1 0x0228
  20. #define INFRA_TOPAXI_PROTECTEN_SET 0x0260
  21. #define INFRA_TOPAXI_PROTECTEN_CLR 0x0264
  22. /**
  23. * mtk_infracfg_set_bus_protection - enable bus protection
  24. * @regmap: The infracfg regmap
  25. * @mask: The mask containing the protection bits to be enabled.
  26. * @reg_update: The boolean flag determines to set the protection bits
  27. * by regmap_update_bits with enable register(PROTECTEN) or
  28. * by regmap_write with set register(PROTECTEN_SET).
  29. *
  30. * This function enables the bus protection bits for disabled power
  31. * domains so that the system does not hang when some unit accesses the
  32. * bus while in power down.
  33. */
  34. int mtk_infracfg_set_bus_protection(struct regmap *infracfg, u32 mask,
  35. bool reg_update)
  36. {
  37. unsigned long expired;
  38. u32 val;
  39. int ret;
  40. if (reg_update)
  41. regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask,
  42. mask);
  43. else
  44. regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_SET, mask);
  45. expired = jiffies + HZ;
  46. while (1) {
  47. ret = regmap_read(infracfg, INFRA_TOPAXI_PROTECTSTA1, &val);
  48. if (ret)
  49. return ret;
  50. if ((val & mask) == mask)
  51. break;
  52. cpu_relax();
  53. if (time_after(jiffies, expired))
  54. return -EIO;
  55. }
  56. return 0;
  57. }
  58. /**
  59. * mtk_infracfg_clear_bus_protection - disable bus protection
  60. * @regmap: The infracfg regmap
  61. * @mask: The mask containing the protection bits to be disabled.
  62. * @reg_update: The boolean flag determines to clear the protection bits
  63. * by regmap_update_bits with enable register(PROTECTEN) or
  64. * by regmap_write with clear register(PROTECTEN_CLR).
  65. *
  66. * This function disables the bus protection bits previously enabled with
  67. * mtk_infracfg_set_bus_protection.
  68. */
  69. int mtk_infracfg_clear_bus_protection(struct regmap *infracfg, u32 mask,
  70. bool reg_update)
  71. {
  72. unsigned long expired;
  73. int ret;
  74. if (reg_update)
  75. regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask, 0);
  76. else
  77. regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_CLR, mask);
  78. expired = jiffies + HZ;
  79. while (1) {
  80. u32 val;
  81. ret = regmap_read(infracfg, INFRA_TOPAXI_PROTECTSTA1, &val);
  82. if (ret)
  83. return ret;
  84. if (!(val & mask))
  85. break;
  86. cpu_relax();
  87. if (time_after(jiffies, expired))
  88. return -EIO;
  89. }
  90. return 0;
  91. }