physmap-versatile.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Versatile OF physmap driver add-on
  4. *
  5. * Copyright (c) 2016, Linaro Limited
  6. * Author: Linus Walleij <[email protected]>
  7. */
  8. #include <linux/export.h>
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_device.h>
  13. #include <linux/mtd/map.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include <linux/bitops.h>
  17. #include "physmap-versatile.h"
  18. static struct regmap *syscon_regmap;
  19. enum versatile_flashprot {
  20. INTEGRATOR_AP_FLASHPROT,
  21. INTEGRATOR_CP_FLASHPROT,
  22. VERSATILE_FLASHPROT,
  23. REALVIEW_FLASHPROT,
  24. };
  25. static const struct of_device_id syscon_match[] = {
  26. {
  27. .compatible = "arm,integrator-ap-syscon",
  28. .data = (void *)INTEGRATOR_AP_FLASHPROT,
  29. },
  30. {
  31. .compatible = "arm,integrator-cp-syscon",
  32. .data = (void *)INTEGRATOR_CP_FLASHPROT,
  33. },
  34. {
  35. .compatible = "arm,core-module-versatile",
  36. .data = (void *)VERSATILE_FLASHPROT,
  37. },
  38. {
  39. .compatible = "arm,realview-eb-syscon",
  40. .data = (void *)REALVIEW_FLASHPROT,
  41. },
  42. {
  43. .compatible = "arm,realview-pb1176-syscon",
  44. .data = (void *)REALVIEW_FLASHPROT,
  45. },
  46. {
  47. .compatible = "arm,realview-pb11mp-syscon",
  48. .data = (void *)REALVIEW_FLASHPROT,
  49. },
  50. {
  51. .compatible = "arm,realview-pba8-syscon",
  52. .data = (void *)REALVIEW_FLASHPROT,
  53. },
  54. {
  55. .compatible = "arm,realview-pbx-syscon",
  56. .data = (void *)REALVIEW_FLASHPROT,
  57. },
  58. {},
  59. };
  60. /*
  61. * Flash protection handling for the Integrator/AP
  62. */
  63. #define INTEGRATOR_SC_CTRLS_OFFSET 0x08
  64. #define INTEGRATOR_SC_CTRLC_OFFSET 0x0C
  65. #define INTEGRATOR_SC_CTRL_FLVPPEN BIT(1)
  66. #define INTEGRATOR_SC_CTRL_FLWP BIT(2)
  67. #define INTEGRATOR_EBI_CSR1_OFFSET 0x04
  68. /* The manual says bit 2, the code says bit 3, trust the code */
  69. #define INTEGRATOR_EBI_WRITE_ENABLE BIT(3)
  70. #define INTEGRATOR_EBI_LOCK_OFFSET 0x20
  71. #define INTEGRATOR_EBI_LOCK_VAL 0xA05F
  72. static const struct of_device_id ebi_match[] = {
  73. { .compatible = "arm,external-bus-interface"},
  74. { },
  75. };
  76. static int ap_flash_init(struct platform_device *pdev)
  77. {
  78. struct device_node *ebi;
  79. void __iomem *ebi_base;
  80. u32 val;
  81. int ret;
  82. /* Look up the EBI */
  83. ebi = of_find_matching_node(NULL, ebi_match);
  84. if (!ebi) {
  85. return -ENODEV;
  86. }
  87. ebi_base = of_iomap(ebi, 0);
  88. of_node_put(ebi);
  89. if (!ebi_base)
  90. return -ENODEV;
  91. /* Clear VPP and write protection bits */
  92. ret = regmap_write(syscon_regmap,
  93. INTEGRATOR_SC_CTRLC_OFFSET,
  94. INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
  95. if (ret)
  96. dev_err(&pdev->dev, "error clearing Integrator VPP/WP\n");
  97. /* Unlock the EBI */
  98. writel(INTEGRATOR_EBI_LOCK_VAL, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
  99. /* Enable write cycles on the EBI, CSR1 (flash) */
  100. val = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
  101. val |= INTEGRATOR_EBI_WRITE_ENABLE;
  102. writel(val, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
  103. /* Lock the EBI again */
  104. writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
  105. iounmap(ebi_base);
  106. return 0;
  107. }
  108. static void ap_flash_set_vpp(struct map_info *map, int on)
  109. {
  110. int ret;
  111. if (on) {
  112. ret = regmap_write(syscon_regmap,
  113. INTEGRATOR_SC_CTRLS_OFFSET,
  114. INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
  115. if (ret)
  116. pr_err("error enabling AP VPP\n");
  117. } else {
  118. ret = regmap_write(syscon_regmap,
  119. INTEGRATOR_SC_CTRLC_OFFSET,
  120. INTEGRATOR_SC_CTRL_FLVPPEN | INTEGRATOR_SC_CTRL_FLWP);
  121. if (ret)
  122. pr_err("error disabling AP VPP\n");
  123. }
  124. }
  125. /*
  126. * Flash protection handling for the Integrator/CP
  127. */
  128. #define INTCP_FLASHPROG_OFFSET 0x04
  129. #define CINTEGRATOR_FLVPPEN BIT(0)
  130. #define CINTEGRATOR_FLWREN BIT(1)
  131. #define CINTEGRATOR_FLMASK BIT(0)|BIT(1)
  132. static void cp_flash_set_vpp(struct map_info *map, int on)
  133. {
  134. int ret;
  135. if (on) {
  136. ret = regmap_update_bits(syscon_regmap,
  137. INTCP_FLASHPROG_OFFSET,
  138. CINTEGRATOR_FLMASK,
  139. CINTEGRATOR_FLVPPEN | CINTEGRATOR_FLWREN);
  140. if (ret)
  141. pr_err("error setting CP VPP\n");
  142. } else {
  143. ret = regmap_update_bits(syscon_regmap,
  144. INTCP_FLASHPROG_OFFSET,
  145. CINTEGRATOR_FLMASK,
  146. 0);
  147. if (ret)
  148. pr_err("error setting CP VPP\n");
  149. }
  150. }
  151. /*
  152. * Flash protection handling for the Versatiles and RealViews
  153. */
  154. #define VERSATILE_SYS_FLASH_OFFSET 0x4C
  155. static void versatile_flash_set_vpp(struct map_info *map, int on)
  156. {
  157. int ret;
  158. ret = regmap_update_bits(syscon_regmap, VERSATILE_SYS_FLASH_OFFSET,
  159. 0x01, !!on);
  160. if (ret)
  161. pr_err("error setting Versatile VPP\n");
  162. }
  163. int of_flash_probe_versatile(struct platform_device *pdev,
  164. struct device_node *np,
  165. struct map_info *map)
  166. {
  167. struct device_node *sysnp;
  168. const struct of_device_id *devid;
  169. struct regmap *rmap;
  170. static enum versatile_flashprot versatile_flashprot;
  171. int ret;
  172. /* Not all flash chips use this protection line */
  173. if (!of_device_is_compatible(np, "arm,versatile-flash"))
  174. return 0;
  175. /* For first chip probed, look up the syscon regmap */
  176. if (!syscon_regmap) {
  177. sysnp = of_find_matching_node_and_match(NULL,
  178. syscon_match,
  179. &devid);
  180. if (!sysnp)
  181. return -ENODEV;
  182. versatile_flashprot = (enum versatile_flashprot)devid->data;
  183. rmap = syscon_node_to_regmap(sysnp);
  184. of_node_put(sysnp);
  185. if (IS_ERR(rmap))
  186. return PTR_ERR(rmap);
  187. syscon_regmap = rmap;
  188. }
  189. switch (versatile_flashprot) {
  190. case INTEGRATOR_AP_FLASHPROT:
  191. ret = ap_flash_init(pdev);
  192. if (ret)
  193. return ret;
  194. map->set_vpp = ap_flash_set_vpp;
  195. dev_info(&pdev->dev, "Integrator/AP flash protection\n");
  196. break;
  197. case INTEGRATOR_CP_FLASHPROT:
  198. map->set_vpp = cp_flash_set_vpp;
  199. dev_info(&pdev->dev, "Integrator/CP flash protection\n");
  200. break;
  201. case VERSATILE_FLASHPROT:
  202. case REALVIEW_FLASHPROT:
  203. map->set_vpp = versatile_flash_set_vpp;
  204. dev_info(&pdev->dev, "versatile/realview flash protection\n");
  205. break;
  206. default:
  207. dev_info(&pdev->dev, "device marked as Versatile flash "
  208. "but no system controller was found\n");
  209. break;
  210. }
  211. return 0;
  212. }