lpc18xx_eeprom.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. };
  128. static int lpc18xx_eeprom_probe(struct platform_device *pdev)
  129. {
  130. struct lpc18xx_eeprom_dev *eeprom;
  131. struct device *dev = &pdev->dev;
  132. struct reset_control *rst;
  133. unsigned long clk_rate;
  134. struct resource *res;
  135. int ret;
  136. eeprom = devm_kzalloc(dev, sizeof(*eeprom), GFP_KERNEL);
  137. if (!eeprom)
  138. return -ENOMEM;
  139. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  140. eeprom->reg_base = devm_ioremap_resource(dev, res);
  141. if (IS_ERR(eeprom->reg_base))
  142. return PTR_ERR(eeprom->reg_base);
  143. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  144. eeprom->mem_base = devm_ioremap_resource(dev, res);
  145. if (IS_ERR(eeprom->mem_base))
  146. return PTR_ERR(eeprom->mem_base);
  147. eeprom->clk = devm_clk_get(&pdev->dev, "eeprom");
  148. if (IS_ERR(eeprom->clk)) {
  149. dev_err(&pdev->dev, "failed to get eeprom clock\n");
  150. return PTR_ERR(eeprom->clk);
  151. }
  152. ret = clk_prepare_enable(eeprom->clk);
  153. if (ret < 0) {
  154. dev_err(dev, "failed to prepare/enable eeprom clk: %d\n", ret);
  155. return ret;
  156. }
  157. rst = devm_reset_control_get_exclusive(dev, NULL);
  158. if (IS_ERR(rst)) {
  159. dev_err(dev, "failed to get reset: %ld\n", PTR_ERR(rst));
  160. ret = PTR_ERR(rst);
  161. goto err_clk;
  162. }
  163. ret = reset_control_assert(rst);
  164. if (ret < 0) {
  165. dev_err(dev, "failed to assert reset: %d\n", ret);
  166. goto err_clk;
  167. }
  168. eeprom->val_bytes = 4;
  169. eeprom->reg_bytes = 4;
  170. /*
  171. * Clock rate is generated by dividing the system bus clock by the
  172. * division factor, contained in the divider register (minus 1 encoded).
  173. */
  174. clk_rate = clk_get_rate(eeprom->clk);
  175. clk_rate = DIV_ROUND_UP(clk_rate, LPC18XX_EEPROM_CLOCK_HZ) - 1;
  176. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_CLKDIV, clk_rate);
  177. /*
  178. * Writing a single word to the page will start the erase/program cycle
  179. * automatically
  180. */
  181. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_AUTOPROG,
  182. LPC18XX_EEPROM_AUTOPROG_WORD);
  183. lpc18xx_eeprom_writel(eeprom, LPC18XX_EEPROM_PWRDWN,
  184. LPC18XX_EEPROM_PWRDWN_YES);
  185. eeprom->size = resource_size(res);
  186. lpc18xx_nvmem_config.size = resource_size(res);
  187. lpc18xx_nvmem_config.dev = dev;
  188. lpc18xx_nvmem_config.priv = eeprom;
  189. eeprom->nvmem = nvmem_register(&lpc18xx_nvmem_config);
  190. if (IS_ERR(eeprom->nvmem)) {
  191. ret = PTR_ERR(eeprom->nvmem);
  192. goto err_clk;
  193. }
  194. platform_set_drvdata(pdev, eeprom);
  195. return 0;
  196. err_clk:
  197. clk_disable_unprepare(eeprom->clk);
  198. return ret;
  199. }
  200. static int lpc18xx_eeprom_remove(struct platform_device *pdev)
  201. {
  202. struct lpc18xx_eeprom_dev *eeprom = platform_get_drvdata(pdev);
  203. int ret;
  204. ret = nvmem_unregister(eeprom->nvmem);
  205. if (ret < 0)
  206. return ret;
  207. clk_disable_unprepare(eeprom->clk);
  208. return 0;
  209. }
  210. static const struct of_device_id lpc18xx_eeprom_of_match[] = {
  211. { .compatible = "nxp,lpc1857-eeprom" },
  212. { },
  213. };
  214. MODULE_DEVICE_TABLE(of, lpc18xx_eeprom_of_match);
  215. static struct platform_driver lpc18xx_eeprom_driver = {
  216. .probe = lpc18xx_eeprom_probe,
  217. .remove = lpc18xx_eeprom_remove,
  218. .driver = {
  219. .name = "lpc18xx-eeprom",
  220. .of_match_table = lpc18xx_eeprom_of_match,
  221. },
  222. };
  223. module_platform_driver(lpc18xx_eeprom_driver);
  224. MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
  225. MODULE_DESCRIPTION("NXP LPC18xx EEPROM memory Driver");
  226. MODULE_LICENSE("GPL v2");