w1.c 2.7 KB

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