lpc18xx_eeprom.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/nvmem-provider.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reset.h>
  19. /* Registers */
  20. #define LPC18XX_EEPROM_AUTOPROG 0x00c
  21. #define LPC18XX_EEPROM_AUTOPROG_WORD 0x1
  22. #define LPC18XX_EEPROM_CLKDIV 0x014
  23. #define LPC18XX_EEPROM_PWRDWN 0x018
  24. #define LPC18XX_EEPROM_PWRDWN_NO 0x0
  25. #define LPC18XX_EEPROM_PWRDWN_YES 0x1
  26. #define LPC18XX_EEPROM_INTSTAT 0xfe0
  27. #define LPC18XX_EEPROM_INTSTAT_END_OF_PROG BIT(2)
  28. #define LPC18XX_EEPROM_INTSTATCLR 0xfe8
  29. #define LPC18XX_EEPROM_INTSTATCLR_PROG_CLR_ST BIT(2)
  30. /* Fixed page size (bytes) */
  31. #define LPC18XX_EEPROM_PAGE_SIZE 0x80
  32. /* EEPROM device requires a ~1500 kHz clock (min 800 kHz, max 1600 kHz) */
  33. #define LPC18XX_EEPROM_CLOCK_HZ 1500000
  34. /* EEPROM requires 3 ms of erase/program time between each writing */
  35. #define LPC18XX_EEPROM_PROGRAM_TIME 3
  36. struct lpc18xx_eeprom_dev {
  37. struct clk *clk;
  38. void __iomem *reg_base;
  39. void __iomem *mem_base;
  40. struct nvmem_device *nvmem;
  41. unsigned reg_bytes;
  42. unsigned val_bytes;
  43. int size;
  44. };
  45. static inline void lpc18xx_eeprom_writel(struct lpc18xx_eeprom_dev *eeprom,
  46. u32 reg, u32 val)
  47. {
  48. writel(val, eeprom->reg_base + reg);
  49. }
  50. static inline u32 lpc18xx_eeprom_readl(struct lpc18xx_eeprom_dev *eeprom,
  51. u32 reg)
  52. {
  53. return readl(eeprom->reg_base + reg);
  54. }
  55. static int lpc18xx_eeprom_busywait_until_prog(struct lpc18xx_eeprom_dev *eeprom)
  56. {
  57. unsigned long end;
  58. u32 val;
  59. /* Wait until EEPROM program operation has finished */
  60. end = jiffies + msecs_to_jiffies(LPC18XX_EEPROM_PROGRAM_TIME * 10);
  61. while (time_is_after_jiffies(end)) {
  62. val = lpc18xx_eeprom_readl(eeprom, LPC18XX_EEPROM_INTSTAT);
  63. if (val & LPC18XX_EEPROM_INTSTAT_END_OF_PROG) {
  64. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_INTSTATCLR,
  65. LPC18XX_EEPROM_INTSTATCLR_PROG_CLR_ST);
  66. return 0;
  67. }
  68. usleep_range(LPC18XX_EEPROM_PROGRAM_TIME * USEC_PER_MSEC,
  69. (LPC18XX_EEPROM_PROGRAM_TIME + 1) * USEC_PER_MSEC);
  70. }
  71. return -ETIMEDOUT;
  72. }
  73. static int lpc18xx_eeprom_gather_write(void *context, unsigned int reg,
  74. void *val, size_t bytes)
  75. {
  76. struct lpc18xx_eeprom_dev *eeprom = context;
  77. unsigned int offset = reg;
  78. int ret;
  79. /*
  80. * The last page contains the EEPROM initialization data and is not
  81. * writable.
  82. */
  83. if ((reg > eeprom->size - LPC18XX_EEPROM_PAGE_SIZE) ||
  84. (reg + bytes > eeprom->size - LPC18XX_EEPROM_PAGE_SIZE))
  85. return -EINVAL;
  86. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  87. LPC18XX_EEPROM_PWRDWN_NO);
  88. /* Wait 100 us while the EEPROM wakes up */
  89. usleep_range(100, 200);
  90. while (bytes) {
  91. writel(*(u32 *)val, eeprom->mem_base + offset);
  92. ret = lpc18xx_eeprom_busywait_until_prog(eeprom);
  93. if (ret < 0)
  94. return ret;
  95. bytes -= eeprom->val_bytes;
  96. val += eeprom->val_bytes;
  97. offset += eeprom->val_bytes;
  98. }
  99. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  100. LPC18XX_EEPROM_PWRDWN_YES);
  101. return 0;
  102. }
  103. static int lpc18xx_eeprom_read(void *context, unsigned int offset,
  104. void *val, size_t bytes)
  105. {
  106. struct lpc18xx_eeprom_dev *eeprom = context;
  107. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  108. LPC18XX_EEPROM_PWRDWN_NO);
  109. /* Wait 100 us while the EEPROM wakes up */
  110. usleep_range(100, 200);
  111. while (bytes) {
  112. *(u32 *)val = readl(eeprom->mem_base + offset);
  113. bytes -= eeprom->val_bytes;
  114. val += eeprom->val_bytes;
  115. offset += eeprom->val_bytes;
  116. }
  117. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  118. LPC18XX_EEPROM_PWRDWN_YES);
  119. return 0;
  120. }
  121. static struct nvmem_config lpc18xx_nvmem_config = {
  122. .name = "lpc18xx-eeprom",
  123. .stride = 4,
  124. .word_size = 4,
  125. .reg_read = lpc18xx_eeprom_read,
  126. .reg_write = lpc18xx_eeprom_gather_write,
  127. .owner = THIS_MODULE,
  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(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");