lpc18xx_eeprom.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP LPC18xx/LPC43xx EEPROM memory NVMEM driver
  4. *
  5. * Copyright (c) 2015 Ariel D'Alessandro <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/nvmem-provider.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset.h>
  17. /* Registers */
  18. #define LPC18XX_EEPROM_AUTOPROG 0x00c
  19. #define LPC18XX_EEPROM_AUTOPROG_WORD 0x1
  20. #define LPC18XX_EEPROM_CLKDIV 0x014
  21. #define LPC18XX_EEPROM_PWRDWN 0x018
  22. #define LPC18XX_EEPROM_PWRDWN_NO 0x0
  23. #define LPC18XX_EEPROM_PWRDWN_YES 0x1
  24. #define LPC18XX_EEPROM_INTSTAT 0xfe0
  25. #define LPC18XX_EEPROM_INTSTAT_END_OF_PROG BIT(2)
  26. #define LPC18XX_EEPROM_INTSTATCLR 0xfe8
  27. #define LPC18XX_EEPROM_INTSTATCLR_PROG_CLR_ST BIT(2)
  28. /* Fixed page size (bytes) */
  29. #define LPC18XX_EEPROM_PAGE_SIZE 0x80
  30. /* EEPROM device requires a ~1500 kHz clock (min 800 kHz, max 1600 kHz) */
  31. #define LPC18XX_EEPROM_CLOCK_HZ 1500000
  32. /* EEPROM requires 3 ms of erase/program time between each writing */
  33. #define LPC18XX_EEPROM_PROGRAM_TIME 3
  34. struct lpc18xx_eeprom_dev {
  35. struct clk *clk;
  36. void __iomem *reg_base;
  37. void __iomem *mem_base;
  38. struct nvmem_device *nvmem;
  39. unsigned reg_bytes;
  40. unsigned val_bytes;
  41. int size;
  42. };
  43. static inline void lpc18xx_eeprom_writel(struct lpc18xx_eeprom_dev *eeprom,
  44. u32 reg, u32 val)
  45. {
  46. writel(val, eeprom->reg_base + reg);
  47. }
  48. static inline u32 lpc18xx_eeprom_readl(struct lpc18xx_eeprom_dev *eeprom,
  49. u32 reg)
  50. {
  51. return readl(eeprom->reg_base + reg);
  52. }
  53. static int lpc18xx_eeprom_busywait_until_prog(struct lpc18xx_eeprom_dev *eeprom)
  54. {
  55. unsigned long end;
  56. u32 val;
  57. /* Wait until EEPROM program operation has finished */
  58. end = jiffies + msecs_to_jiffies(LPC18XX_EEPROM_PROGRAM_TIME * 10);
  59. while (time_is_after_jiffies(end)) {
  60. val = lpc18xx_eeprom_readl(eeprom, LPC18XX_EEPROM_INTSTAT);
  61. if (val & LPC18XX_EEPROM_INTSTAT_END_OF_PROG) {
  62. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_INTSTATCLR,
  63. LPC18XX_EEPROM_INTSTATCLR_PROG_CLR_ST);
  64. return 0;
  65. }
  66. usleep_range(LPC18XX_EEPROM_PROGRAM_TIME * USEC_PER_MSEC,
  67. (LPC18XX_EEPROM_PROGRAM_TIME + 1) * USEC_PER_MSEC);
  68. }
  69. return -ETIMEDOUT;
  70. }
  71. static int lpc18xx_eeprom_gather_write(void *context, unsigned int reg,
  72. void *val, size_t bytes)
  73. {
  74. struct lpc18xx_eeprom_dev *eeprom = context;
  75. unsigned int offset = reg;
  76. int ret;
  77. /*
  78. * The last page contains the EEPROM initialization data and is not
  79. * writable.
  80. */
  81. if ((reg > eeprom->size - LPC18XX_EEPROM_PAGE_SIZE) ||
  82. (reg + bytes > eeprom->size - LPC18XX_EEPROM_PAGE_SIZE))
  83. return -EINVAL;
  84. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  85. LPC18XX_EEPROM_PWRDWN_NO);
  86. /* Wait 100 us while the EEPROM wakes up */
  87. usleep_range(100, 200);
  88. while (bytes) {
  89. writel(*(u32 *)val, eeprom->mem_base + offset);
  90. ret = lpc18xx_eeprom_busywait_until_prog(eeprom);
  91. if (ret < 0)
  92. return ret;
  93. bytes -= eeprom->val_bytes;
  94. val += eeprom->val_bytes;
  95. offset += eeprom->val_bytes;
  96. }
  97. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  98. LPC18XX_EEPROM_PWRDWN_YES);
  99. return 0;
  100. }
  101. static int lpc18xx_eeprom_read(void *context, unsigned int offset,
  102. void *val, size_t bytes)
  103. {
  104. struct lpc18xx_eeprom_dev *eeprom = context;
  105. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  106. LPC18XX_EEPROM_PWRDWN_NO);
  107. /* Wait 100 us while the EEPROM wakes up */
  108. usleep_range(100, 200);
  109. while (bytes) {
  110. *(u32 *)val = readl(eeprom->mem_base + offset);
  111. bytes -= eeprom->val_bytes;
  112. val += eeprom->val_bytes;
  113. offset += eeprom->val_bytes;
  114. }
  115. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  116. LPC18XX_EEPROM_PWRDWN_YES);
  117. return 0;
  118. }
  119. static struct nvmem_config lpc18xx_nvmem_config = {
  120. .name = "lpc18xx-eeprom",
  121. .stride = 4,
  122. .word_size = 4,
  123. .reg_read = lpc18xx_eeprom_read,
  124. .reg_write = lpc18xx_eeprom_gather_write,
  125. };
  126. static int lpc18xx_eeprom_probe(struct platform_device *pdev)
  127. {
  128. struct lpc18xx_eeprom_dev *eeprom;
  129. struct device *dev = &pdev->dev;
  130. struct reset_control *rst;
  131. unsigned long clk_rate;
  132. struct resource *res;
  133. int ret;
  134. eeprom = devm_kzalloc(dev, sizeof(*eeprom), GFP_KERNEL);
  135. if (!eeprom)
  136. return -ENOMEM;
  137. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  138. eeprom->reg_base = devm_ioremap_resource(dev, res);
  139. if (IS_ERR(eeprom->reg_base))
  140. return PTR_ERR(eeprom->reg_base);
  141. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  142. eeprom->mem_base = devm_ioremap_resource(dev, res);
  143. if (IS_ERR(eeprom->mem_base))
  144. return PTR_ERR(eeprom->mem_base);
  145. eeprom->clk = devm_clk_get(&pdev->dev, "eeprom");
  146. if (IS_ERR(eeprom->clk)) {
  147. dev_err(&pdev->dev, "failed to get eeprom clock\n");
  148. return PTR_ERR(eeprom->clk);
  149. }
  150. ret = clk_prepare_enable(eeprom->clk);
  151. if (ret < 0) {
  152. dev_err(dev, "failed to prepare/enable eeprom clk: %d\n", ret);
  153. return ret;
  154. }
  155. rst = devm_reset_control_get_exclusive(dev, NULL);
  156. if (IS_ERR(rst)) {
  157. dev_err(dev, "failed to get reset: %ld\n", PTR_ERR(rst));
  158. ret = PTR_ERR(rst);
  159. goto err_clk;
  160. }
  161. ret = reset_control_assert(rst);
  162. if (ret < 0) {
  163. dev_err(dev, "failed to assert reset: %d\n", ret);
  164. goto err_clk;
  165. }
  166. eeprom->val_bytes = 4;
  167. eeprom->reg_bytes = 4;
  168. /*
  169. * Clock rate is generated by dividing the system bus clock by the
  170. * division factor, contained in the divider register (minus 1 encoded).
  171. */
  172. clk_rate = clk_get_rate(eeprom->clk);
  173. clk_rate = DIV_ROUND_UP(clk_rate, LPC18XX_EEPROM_CLOCK_HZ) - 1;
  174. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_CLKDIV, clk_rate);
  175. /*
  176. * Writing a single word to the page will start the erase/program cycle
  177. * automatically
  178. */
  179. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_AUTOPROG,
  180. LPC18XX_EEPROM_AUTOPROG_WORD);
  181. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  182. LPC18XX_EEPROM_PWRDWN_YES);
  183. eeprom->size = resource_size(res);
  184. lpc18xx_nvmem_config.size = resource_size(res);
  185. lpc18xx_nvmem_config.dev = dev;
  186. lpc18xx_nvmem_config.priv = eeprom;
  187. eeprom->nvmem = devm_nvmem_register(dev, &lpc18xx_nvmem_config);
  188. if (IS_ERR(eeprom->nvmem)) {
  189. ret = PTR_ERR(eeprom->nvmem);
  190. goto err_clk;
  191. }
  192. platform_set_drvdata(pdev, eeprom);
  193. return 0;
  194. err_clk:
  195. clk_disable_unprepare(eeprom->clk);
  196. return ret;
  197. }
  198. static int lpc18xx_eeprom_remove(struct platform_device *pdev)
  199. {
  200. struct lpc18xx_eeprom_dev *eeprom = platform_get_drvdata(pdev);
  201. clk_disable_unprepare(eeprom->clk);
  202. return 0;
  203. }
  204. static const struct of_device_id lpc18xx_eeprom_of_match[] = {
  205. { .compatible = "nxp,lpc1857-eeprom" },
  206. { },
  207. };
  208. MODULE_DEVICE_TABLE(of, lpc18xx_eeprom_of_match);
  209. static struct platform_driver lpc18xx_eeprom_driver = {
  210. .probe = lpc18xx_eeprom_probe,
  211. .remove = lpc18xx_eeprom_remove,
  212. .driver = {
  213. .name = "lpc18xx-eeprom",
  214. .of_match_table = lpc18xx_eeprom_of_match,
  215. },
  216. };
  217. module_platform_driver(lpc18xx_eeprom_driver);
  218. MODULE_AUTHOR("Ariel D'Alessandro <[email protected]>");
  219. MODULE_DESCRIPTION("NXP LPC18xx EEPROM memory Driver");
  220. MODULE_LICENSE("GPL v2");