w1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SPDX-License-Identifier: GPL-2.0+
  2. *
  3. * (C) Copyright 2018
  4. * Microchip Technology, Inc.
  5. * Eugen Hristev <eugen.hristev@microchip.com>
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <dm.h>
  10. #include <w1.h>
  11. #include <w1-eeprom.h>
  12. #include <dm/device-internal.h>
  13. static int w1_bus(void)
  14. {
  15. struct udevice *bus, *dev;
  16. int ret;
  17. ret = w1_get_bus(0, &bus);
  18. if (ret) {
  19. printf("one wire interface not found\n");
  20. return CMD_RET_FAILURE;
  21. }
  22. printf("Bus %d:\t%s", dev_seq(bus), bus->name);
  23. if (device_active(bus))
  24. printf(" (active)");
  25. printf("\n");
  26. for (device_find_first_child(bus, &dev);
  27. dev;
  28. device_find_next_child(&dev)) {
  29. ret = device_probe(dev);
  30. printf("\t%s (%d) uclass %s : ", dev->name, dev_seq(dev),
  31. dev->uclass->uc_drv->name);
  32. if (ret)
  33. printf("device error\n");
  34. else
  35. printf("family 0x%x\n", w1_get_device_family(dev));
  36. }
  37. return CMD_RET_SUCCESS;
  38. }
  39. static int w1_read(int argc, char *const argv[])
  40. {
  41. int bus_n = 0, dev_n = 0, offset = 0, len = 512;
  42. int i;
  43. struct udevice *bus, *dev;
  44. int ret;
  45. u8 buf[512];
  46. if (argc > 2)
  47. bus_n = dectoul(argv[2], NULL);
  48. if (argc > 3)
  49. dev_n = dectoul(argv[3], NULL);
  50. if (argc > 4)
  51. offset = dectoul(argv[4], NULL);
  52. if (argc > 5)
  53. len = dectoul(argv[5], NULL);
  54. if (len > 512) {
  55. printf("len needs to be <= 512\n");
  56. return CMD_RET_FAILURE;
  57. }
  58. ret = w1_get_bus(bus_n, &bus);
  59. if (ret) {
  60. printf("one wire interface not found\n");
  61. return CMD_RET_FAILURE;
  62. }
  63. for (device_find_first_child(bus, &dev), i = 0;
  64. dev && i <= dev_n;
  65. device_find_next_child(&dev), i++) {
  66. ret = device_probe(dev);
  67. if (!ret && i == dev_n)
  68. break;
  69. }
  70. if (i != dev_n || ret || !dev) {
  71. printf("invalid dev\n");
  72. return CMD_RET_FAILURE;
  73. }
  74. if (strcmp(dev->uclass->uc_drv->name, "w1_eeprom")) {
  75. printf("the device present on the interface is of unknown device class\n");
  76. return CMD_RET_FAILURE;
  77. }
  78. ret = w1_eeprom_read_buf(dev, offset, (u8 *)buf, len);
  79. if (ret) {
  80. printf("error reading device %s\n", dev->name);
  81. return CMD_RET_FAILURE;
  82. }
  83. for (i = 0; i < len; i++)
  84. printf("%x", buf[i]);
  85. printf("\n");
  86. return CMD_RET_SUCCESS;
  87. }
  88. int do_w1(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  89. {
  90. if (argc < 2)
  91. return CMD_RET_USAGE;
  92. if (!strcmp(argv[1], "bus"))
  93. return w1_bus();
  94. if (!strcmp(argv[1], "read"))
  95. return w1_read(argc, argv);
  96. return CMD_RET_SUCCESS;
  97. }
  98. U_BOOT_CMD(w1, 6, 0, do_w1,
  99. "onewire interface utility commands",
  100. "bus - show onewire bus info (all)\n"
  101. "w1 read [<bus> [<dev> [offset [length]]]]"
  102. " - read from onewire device 'dev' on onewire bus 'bus'"
  103. " starting from offset 'offset' and length 'length'\n"
  104. " defaults: bus 0, dev 0, offset 0, length 512 bytes.");