qfprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/device.h>
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/nvmem-provider.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/property.h>
  17. #include <linux/regulator/consumer.h>
  18. /* Blow timer clock frequency in Mhz */
  19. #define QFPROM_BLOW_TIMER_OFFSET 0x03c
  20. /* Amount of time required to hold charge to blow fuse in micro-seconds */
  21. #define QFPROM_FUSE_BLOW_POLL_US 100
  22. #define QFPROM_FUSE_BLOW_TIMEOUT_US 10000
  23. #define QFPROM_BLOW_STATUS_OFFSET 0x048
  24. #define QFPROM_BLOW_STATUS_BUSY 0x1
  25. #define QFPROM_BLOW_STATUS_READY 0x0
  26. #define QFPROM_ACCEL_OFFSET 0x044
  27. #define QFPROM_VERSION_OFFSET 0x0
  28. #define QFPROM_MAJOR_VERSION_SHIFT 28
  29. #define QFPROM_MAJOR_VERSION_MASK GENMASK(31, QFPROM_MAJOR_VERSION_SHIFT)
  30. #define QFPROM_MINOR_VERSION_SHIFT 16
  31. #define QFPROM_MINOR_VERSION_MASK GENMASK(27, QFPROM_MINOR_VERSION_SHIFT)
  32. static bool read_raw_data;
  33. module_param(read_raw_data, bool, 0644);
  34. MODULE_PARM_DESC(read_raw_data, "Read raw instead of corrected data");
  35. /**
  36. * struct qfprom_soc_data - config that varies from SoC to SoC.
  37. *
  38. * @accel_value: Should contain qfprom accel value.
  39. * @qfprom_blow_timer_value: The timer value of qfprom when doing efuse blow.
  40. * @qfprom_blow_set_freq: The frequency required to set when we start the
  41. * fuse blowing.
  42. * @qfprom_blow_uV: LDO voltage to be set when doing efuse blow
  43. */
  44. struct qfprom_soc_data {
  45. u32 accel_value;
  46. u32 qfprom_blow_timer_value;
  47. u32 qfprom_blow_set_freq;
  48. int qfprom_blow_uV;
  49. };
  50. /**
  51. * struct qfprom_priv - structure holding qfprom attributes
  52. *
  53. * @qfpraw: iomapped memory space for qfprom-efuse raw address space.
  54. * @qfpconf: iomapped memory space for qfprom-efuse configuration address
  55. * space.
  56. * @qfpcorrected: iomapped memory space for qfprom corrected address space.
  57. * @qfpsecurity: iomapped memory space for qfprom security control space.
  58. * @dev: qfprom device structure.
  59. * @secclk: Clock supply.
  60. * @vcc: Regulator supply.
  61. * @soc_data: Data that for things that varies from SoC to SoC.
  62. */
  63. struct qfprom_priv {
  64. void __iomem *qfpraw;
  65. void __iomem *qfpconf;
  66. void __iomem *qfpcorrected;
  67. void __iomem *qfpsecurity;
  68. struct device *dev;
  69. struct clk *secclk;
  70. struct regulator *vcc;
  71. const struct qfprom_soc_data *soc_data;
  72. };
  73. /**
  74. * struct qfprom_touched_values - saved values to restore after blowing
  75. *
  76. * @clk_rate: The rate the clock was at before blowing.
  77. * @accel_val: The value of the accel reg before blowing.
  78. * @timer_val: The value of the timer before blowing.
  79. */
  80. struct qfprom_touched_values {
  81. unsigned long clk_rate;
  82. u32 accel_val;
  83. u32 timer_val;
  84. };
  85. /**
  86. * struct qfprom_soc_compatible_data - Data matched against the SoC
  87. * compatible string.
  88. *
  89. * @keepout: Array of keepout regions for this SoC.
  90. * @nkeepout: Number of elements in the keepout array.
  91. */
  92. struct qfprom_soc_compatible_data {
  93. const struct nvmem_keepout *keepout;
  94. unsigned int nkeepout;
  95. };
  96. static const struct nvmem_keepout sc7180_qfprom_keepout[] = {
  97. {.start = 0x128, .end = 0x148},
  98. {.start = 0x220, .end = 0x228}
  99. };
  100. static const struct qfprom_soc_compatible_data sc7180_qfprom = {
  101. .keepout = sc7180_qfprom_keepout,
  102. .nkeepout = ARRAY_SIZE(sc7180_qfprom_keepout)
  103. };
  104. static const struct nvmem_keepout sc7280_qfprom_keepout[] = {
  105. {.start = 0x128, .end = 0x148},
  106. {.start = 0x238, .end = 0x248}
  107. };
  108. static const struct qfprom_soc_compatible_data sc7280_qfprom = {
  109. .keepout = sc7280_qfprom_keepout,
  110. .nkeepout = ARRAY_SIZE(sc7280_qfprom_keepout)
  111. };
  112. /**
  113. * qfprom_disable_fuse_blowing() - Undo enabling of fuse blowing.
  114. * @priv: Our driver data.
  115. * @old: The data that was stashed from before fuse blowing.
  116. *
  117. * Resets the value of the blow timer, accel register and the clock
  118. * and voltage settings.
  119. *
  120. * Prints messages if there are errors but doesn't return an error code
  121. * since there's not much we can do upon failure.
  122. */
  123. static void qfprom_disable_fuse_blowing(const struct qfprom_priv *priv,
  124. const struct qfprom_touched_values *old)
  125. {
  126. int ret;
  127. writel(old->timer_val, priv->qfpconf + QFPROM_BLOW_TIMER_OFFSET);
  128. writel(old->accel_val, priv->qfpconf + QFPROM_ACCEL_OFFSET);
  129. dev_pm_genpd_set_performance_state(priv->dev, 0);
  130. pm_runtime_put(priv->dev);
  131. /*
  132. * This may be a shared rail and may be able to run at a lower rate
  133. * when we're not blowing fuses. At the moment, the regulator framework
  134. * applies voltage constraints even on disabled rails, so remove our
  135. * constraints and allow the rail to be adjusted by other users.
  136. */
  137. ret = regulator_set_voltage(priv->vcc, 0, INT_MAX);
  138. if (ret)
  139. dev_warn(priv->dev, "Failed to set 0 voltage (ignoring)\n");
  140. ret = regulator_disable(priv->vcc);
  141. if (ret)
  142. dev_warn(priv->dev, "Failed to disable regulator (ignoring)\n");
  143. ret = clk_set_rate(priv->secclk, old->clk_rate);
  144. if (ret)
  145. dev_warn(priv->dev,
  146. "Failed to set clock rate for disable (ignoring)\n");
  147. clk_disable_unprepare(priv->secclk);
  148. }
  149. /**
  150. * qfprom_enable_fuse_blowing() - Enable fuse blowing.
  151. * @priv: Our driver data.
  152. * @old: We'll stash stuff here to use when disabling.
  153. *
  154. * Sets the value of the blow timer, accel register and the clock
  155. * and voltage settings.
  156. *
  157. * Prints messages if there are errors so caller doesn't need to.
  158. *
  159. * Return: 0 or -err.
  160. */
  161. static int qfprom_enable_fuse_blowing(const struct qfprom_priv *priv,
  162. struct qfprom_touched_values *old)
  163. {
  164. int ret;
  165. int qfprom_blow_uV = priv->soc_data->qfprom_blow_uV;
  166. ret = clk_prepare_enable(priv->secclk);
  167. if (ret) {
  168. dev_err(priv->dev, "Failed to enable clock\n");
  169. return ret;
  170. }
  171. old->clk_rate = clk_get_rate(priv->secclk);
  172. ret = clk_set_rate(priv->secclk, priv->soc_data->qfprom_blow_set_freq);
  173. if (ret) {
  174. dev_err(priv->dev, "Failed to set clock rate for enable\n");
  175. goto err_clk_prepared;
  176. }
  177. /*
  178. * Hardware requires a minimum voltage for fuse blowing.
  179. * This may be a shared rail so don't specify a maximum.
  180. * Regulator constraints will cap to the actual maximum.
  181. */
  182. ret = regulator_set_voltage(priv->vcc, qfprom_blow_uV, INT_MAX);
  183. if (ret) {
  184. dev_err(priv->dev, "Failed to set %duV\n", qfprom_blow_uV);
  185. goto err_clk_rate_set;
  186. }
  187. ret = regulator_enable(priv->vcc);
  188. if (ret) {
  189. dev_err(priv->dev, "Failed to enable regulator\n");
  190. goto err_clk_rate_set;
  191. }
  192. ret = pm_runtime_resume_and_get(priv->dev);
  193. if (ret < 0) {
  194. dev_err(priv->dev, "Failed to enable power-domain\n");
  195. goto err_reg_enable;
  196. }
  197. dev_pm_genpd_set_performance_state(priv->dev, INT_MAX);
  198. old->timer_val = readl(priv->qfpconf + QFPROM_BLOW_TIMER_OFFSET);
  199. old->accel_val = readl(priv->qfpconf + QFPROM_ACCEL_OFFSET);
  200. writel(priv->soc_data->qfprom_blow_timer_value,
  201. priv->qfpconf + QFPROM_BLOW_TIMER_OFFSET);
  202. writel(priv->soc_data->accel_value,
  203. priv->qfpconf + QFPROM_ACCEL_OFFSET);
  204. return 0;
  205. err_reg_enable:
  206. regulator_disable(priv->vcc);
  207. err_clk_rate_set:
  208. clk_set_rate(priv->secclk, old->clk_rate);
  209. err_clk_prepared:
  210. clk_disable_unprepare(priv->secclk);
  211. return ret;
  212. }
  213. /**
  214. * qfprom_reg_write() - Write to fuses.
  215. * @context: Our driver data.
  216. * @reg: The offset to write at.
  217. * @_val: Pointer to data to write.
  218. * @bytes: The number of bytes to write.
  219. *
  220. * Writes to fuses. WARNING: THIS IS PERMANENT.
  221. *
  222. * Return: 0 or -err.
  223. */
  224. static int qfprom_reg_write(void *context, unsigned int reg, void *_val,
  225. size_t bytes)
  226. {
  227. struct qfprom_priv *priv = context;
  228. struct qfprom_touched_values old;
  229. int words = bytes / 4;
  230. u32 *value = _val;
  231. u32 blow_status;
  232. int ret;
  233. int i;
  234. dev_dbg(priv->dev,
  235. "Writing to raw qfprom region : %#010x of size: %zu\n",
  236. reg, bytes);
  237. /*
  238. * The hardware only allows us to write word at a time, but we can
  239. * read byte at a time. Until the nvmem framework allows a separate
  240. * word_size and stride for reading vs. writing, we'll enforce here.
  241. */
  242. if (bytes % 4) {
  243. dev_err(priv->dev,
  244. "%zu is not an integral number of words\n", bytes);
  245. return -EINVAL;
  246. }
  247. if (reg % 4) {
  248. dev_err(priv->dev,
  249. "Invalid offset: %#x. Must be word aligned\n", reg);
  250. return -EINVAL;
  251. }
  252. ret = qfprom_enable_fuse_blowing(priv, &old);
  253. if (ret)
  254. return ret;
  255. ret = readl_relaxed_poll_timeout(
  256. priv->qfpconf + QFPROM_BLOW_STATUS_OFFSET,
  257. blow_status, blow_status == QFPROM_BLOW_STATUS_READY,
  258. QFPROM_FUSE_BLOW_POLL_US, QFPROM_FUSE_BLOW_TIMEOUT_US);
  259. if (ret) {
  260. dev_err(priv->dev,
  261. "Timeout waiting for initial ready; aborting.\n");
  262. goto exit_enabled_fuse_blowing;
  263. }
  264. for (i = 0; i < words; i++)
  265. writel(value[i], priv->qfpraw + reg + (i * 4));
  266. ret = readl_relaxed_poll_timeout(
  267. priv->qfpconf + QFPROM_BLOW_STATUS_OFFSET,
  268. blow_status, blow_status == QFPROM_BLOW_STATUS_READY,
  269. QFPROM_FUSE_BLOW_POLL_US, QFPROM_FUSE_BLOW_TIMEOUT_US);
  270. /* Give an error, but not much we can do in this case */
  271. if (ret)
  272. dev_err(priv->dev, "Timeout waiting for finish.\n");
  273. exit_enabled_fuse_blowing:
  274. qfprom_disable_fuse_blowing(priv, &old);
  275. return ret;
  276. }
  277. static int qfprom_reg_read(void *context,
  278. unsigned int reg, void *_val, size_t bytes)
  279. {
  280. struct qfprom_priv *priv = context;
  281. u32 *val = _val;
  282. void __iomem *base = priv->qfpcorrected;
  283. int words = DIV_ROUND_UP(bytes, sizeof(u32));
  284. int i;
  285. if (read_raw_data && priv->qfpraw)
  286. base = priv->qfpraw;
  287. for (i = 0; i < words; i++)
  288. *val++ = readl(base + reg + i * sizeof(u32));
  289. return 0;
  290. }
  291. /* Align reads to word boundary */
  292. static void qfprom_fixup_dt_cell_info(struct nvmem_device *nvmem,
  293. struct nvmem_cell_info *cell)
  294. {
  295. unsigned int byte_offset = cell->offset % sizeof(u32);
  296. cell->bit_offset += byte_offset * BITS_PER_BYTE;
  297. cell->offset -= byte_offset;
  298. if (byte_offset && !cell->nbits)
  299. cell->nbits = cell->bytes * BITS_PER_BYTE;
  300. }
  301. static void qfprom_runtime_disable(void *data)
  302. {
  303. pm_runtime_disable(data);
  304. }
  305. static const struct qfprom_soc_data qfprom_7_8_data = {
  306. .accel_value = 0xD10,
  307. .qfprom_blow_timer_value = 25,
  308. .qfprom_blow_set_freq = 4800000,
  309. .qfprom_blow_uV = 1800000,
  310. };
  311. static const struct qfprom_soc_data qfprom_7_15_data = {
  312. .accel_value = 0xD08,
  313. .qfprom_blow_timer_value = 24,
  314. .qfprom_blow_set_freq = 4800000,
  315. .qfprom_blow_uV = 1900000,
  316. };
  317. static int qfprom_probe(struct platform_device *pdev)
  318. {
  319. struct nvmem_config econfig = {
  320. .name = "qfprom",
  321. .add_legacy_fixed_of_cells = true,
  322. .stride = 4,
  323. .word_size = 4,
  324. .id = NVMEM_DEVID_AUTO,
  325. .reg_read = qfprom_reg_read,
  326. .fixup_dt_cell_info = qfprom_fixup_dt_cell_info,
  327. };
  328. struct device *dev = &pdev->dev;
  329. struct resource *res;
  330. struct nvmem_device *nvmem;
  331. const struct qfprom_soc_compatible_data *soc_data;
  332. struct qfprom_priv *priv;
  333. int ret;
  334. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  335. if (!priv)
  336. return -ENOMEM;
  337. /* The corrected section is always provided */
  338. priv->qfpcorrected = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  339. if (IS_ERR(priv->qfpcorrected))
  340. return PTR_ERR(priv->qfpcorrected);
  341. econfig.size = resource_size(res);
  342. econfig.dev = dev;
  343. econfig.priv = priv;
  344. priv->dev = dev;
  345. soc_data = device_get_match_data(dev);
  346. if (soc_data) {
  347. econfig.keepout = soc_data->keepout;
  348. econfig.nkeepout = soc_data->nkeepout;
  349. }
  350. /*
  351. * If more than one region is provided then the OS has the ability
  352. * to write.
  353. */
  354. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  355. if (res) {
  356. u32 version;
  357. int major_version, minor_version;
  358. priv->qfpraw = devm_ioremap_resource(dev, res);
  359. if (IS_ERR(priv->qfpraw))
  360. return PTR_ERR(priv->qfpraw);
  361. priv->qfpconf = devm_platform_ioremap_resource(pdev, 2);
  362. if (IS_ERR(priv->qfpconf))
  363. return PTR_ERR(priv->qfpconf);
  364. priv->qfpsecurity = devm_platform_ioremap_resource(pdev, 3);
  365. if (IS_ERR(priv->qfpsecurity))
  366. return PTR_ERR(priv->qfpsecurity);
  367. version = readl(priv->qfpsecurity + QFPROM_VERSION_OFFSET);
  368. major_version = (version & QFPROM_MAJOR_VERSION_MASK) >>
  369. QFPROM_MAJOR_VERSION_SHIFT;
  370. minor_version = (version & QFPROM_MINOR_VERSION_MASK) >>
  371. QFPROM_MINOR_VERSION_SHIFT;
  372. if (major_version == 7 && minor_version == 8)
  373. priv->soc_data = &qfprom_7_8_data;
  374. else if (major_version == 7 && minor_version == 15)
  375. priv->soc_data = &qfprom_7_15_data;
  376. priv->vcc = devm_regulator_get(&pdev->dev, "vcc");
  377. if (IS_ERR(priv->vcc))
  378. return PTR_ERR(priv->vcc);
  379. priv->secclk = devm_clk_get_optional(dev, "core");
  380. if (IS_ERR(priv->secclk))
  381. return dev_err_probe(dev, PTR_ERR(priv->secclk), "Error getting clock\n");
  382. /* Only enable writing if we have SoC data and a valid clock */
  383. if (priv->soc_data && priv->secclk)
  384. econfig.reg_write = qfprom_reg_write;
  385. }
  386. pm_runtime_enable(dev);
  387. ret = devm_add_action_or_reset(dev, qfprom_runtime_disable, dev);
  388. if (ret)
  389. return ret;
  390. nvmem = devm_nvmem_register(dev, &econfig);
  391. return PTR_ERR_OR_ZERO(nvmem);
  392. }
  393. static const struct of_device_id qfprom_of_match[] = {
  394. { .compatible = "qcom,qfprom",},
  395. { .compatible = "qcom,sc7180-qfprom", .data = &sc7180_qfprom},
  396. { .compatible = "qcom,sc7280-qfprom", .data = &sc7280_qfprom},
  397. {/* sentinel */},
  398. };
  399. MODULE_DEVICE_TABLE(of, qfprom_of_match);
  400. static struct platform_driver qfprom_driver = {
  401. .probe = qfprom_probe,
  402. .driver = {
  403. .name = "qcom,qfprom",
  404. .of_match_table = qfprom_of_match,
  405. },
  406. };
  407. module_platform_driver(qfprom_driver);
  408. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
  409. MODULE_DESCRIPTION("Qualcomm QFPROM driver");
  410. MODULE_LICENSE("GPL v2");