qnap-mcu-eeprom.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ee1004 - driver for DDR4 SPD EEPROMs
  4. *
  5. * Copyright (C) 2017-2019 Jean Delvare
  6. *
  7. * Based on the at24 driver:
  8. * Copyright (C) 2005-2007 David Brownell
  9. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  10. */
  11. #include <linux/mfd/qnap-mcu.h>
  12. #include <linux/module.h>
  13. #include <linux/nvmem-provider.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. /* Determined by trial and error until read anomalies appeared */
  17. #define QNAP_MCU_EEPROM_SIZE 256
  18. #define QNAP_MCU_EEPROM_BLOCK_SIZE 32
  19. static int qnap_mcu_eeprom_read_block(struct qnap_mcu *mcu, unsigned int offset,
  20. void *val, size_t bytes)
  21. {
  22. const u8 cmd[] = { 0xf7, 0xa1, offset, bytes };
  23. u8 *reply;
  24. int ret = 0;
  25. reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
  26. if (!reply)
  27. return -ENOMEM;
  28. ret = qnap_mcu_exec(mcu, cmd, sizeof(cmd), reply, bytes + sizeof(cmd));
  29. if (ret)
  30. goto out;
  31. /* First bytes must mirror the sent command */
  32. if (memcmp(cmd, reply, sizeof(cmd))) {
  33. ret = -EIO;
  34. goto out;
  35. }
  36. memcpy(val, reply + sizeof(cmd), bytes);
  37. out:
  38. kfree(reply);
  39. return ret;
  40. }
  41. static int qnap_mcu_eeprom_read(void *priv, unsigned int offset, void *val, size_t bytes)
  42. {
  43. struct qnap_mcu *mcu = priv;
  44. int pos = 0, ret;
  45. u8 *buf = val;
  46. if (unlikely(!bytes))
  47. return 0;
  48. while (bytes > 0) {
  49. size_t to_read = (bytes > QNAP_MCU_EEPROM_BLOCK_SIZE) ?
  50. QNAP_MCU_EEPROM_BLOCK_SIZE : bytes;
  51. ret = qnap_mcu_eeprom_read_block(mcu, offset + pos, &buf[pos], to_read);
  52. if (ret < 0)
  53. return ret;
  54. pos += to_read;
  55. bytes -= to_read;
  56. }
  57. return 0;
  58. }
  59. static int qnap_mcu_eeprom_probe(struct platform_device *pdev)
  60. {
  61. struct qnap_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
  62. struct nvmem_config nvcfg = {};
  63. struct nvmem_device *ndev;
  64. nvcfg.dev = &pdev->dev;
  65. nvcfg.of_node = pdev->dev.parent->of_node;
  66. nvcfg.name = dev_name(&pdev->dev);
  67. nvcfg.id = NVMEM_DEVID_NONE;
  68. nvcfg.owner = THIS_MODULE;
  69. nvcfg.type = NVMEM_TYPE_EEPROM;
  70. nvcfg.read_only = true;
  71. nvcfg.root_only = false;
  72. nvcfg.reg_read = qnap_mcu_eeprom_read;
  73. nvcfg.size = QNAP_MCU_EEPROM_SIZE,
  74. nvcfg.word_size = 1,
  75. nvcfg.stride = 1,
  76. nvcfg.priv = mcu,
  77. ndev = devm_nvmem_register(&pdev->dev, &nvcfg);
  78. if (IS_ERR(ndev))
  79. return PTR_ERR(ndev);
  80. return 0;
  81. }
  82. static struct platform_driver qnap_mcu_eeprom_driver = {
  83. .probe = qnap_mcu_eeprom_probe,
  84. .driver = {
  85. .name = "qnap-mcu-eeprom",
  86. },
  87. };
  88. module_platform_driver(qnap_mcu_eeprom_driver);
  89. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  90. MODULE_DESCRIPTION("QNAP MCU EEPROM driver");
  91. MODULE_LICENSE("GPL");