lpc18xx_eeprom.c 7.2 KB

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