eep_sandbox.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* SPDX-License-Identifier: GPL-2.0+
  2. *
  3. * Copyright (c) 2018 Microchip Technology, Inc.
  4. *
  5. */
  6. #include <common.h>
  7. #include <linux/err.h>
  8. #include <dm.h>
  9. #include <w1-eeprom.h>
  10. #include <w1.h>
  11. #define W1_F2D_READ_EEPROM 0xf0
  12. #define EEP_SANDBOX_SAMPLE_MEM "this is a sample EEPROM memory string."
  13. static int eep_sandbox_read_buf(struct udevice *dev, unsigned int offset,
  14. u8 *buf, unsigned int count)
  15. {
  16. /* do not allow to copy more than our maximum sample string */
  17. if (offset + count < strlen(EEP_SANDBOX_SAMPLE_MEM)) {
  18. offset = 0;
  19. count = strlen(EEP_SANDBOX_SAMPLE_MEM);
  20. }
  21. strncpy((char *)buf, EEP_SANDBOX_SAMPLE_MEM, count);
  22. /*
  23. * in case the w1 subsystem uses some different kind of sandbox testing,
  24. * like randomized gpio values , we take the buffer from there
  25. */
  26. w1_reset_select(dev);
  27. w1_write_byte(dev, W1_F2D_READ_EEPROM);
  28. w1_write_byte(dev, offset & 0xff);
  29. w1_write_byte(dev, offset >> 8);
  30. w1_read_buf(dev, buf, count);
  31. /*
  32. * even if read buf from w1 fails, return success as we hardcoded
  33. * the buffer.
  34. */
  35. return 0;
  36. }
  37. static const struct w1_eeprom_ops eep_sandbox_ops = {
  38. .read_buf = eep_sandbox_read_buf,
  39. };
  40. static const struct udevice_id eep_sandbox_id[] = {
  41. { .compatible = "sandbox,w1-eeprom", .data = W1_FAMILY_EEP_SANDBOX },
  42. { },
  43. };
  44. U_BOOT_DRIVER(eep_sandbox) = {
  45. .name = "eep_sandbox",
  46. .id = UCLASS_W1_EEPROM,
  47. .of_match = eep_sandbox_id,
  48. .ops = &eep_sandbox_ops,
  49. };