chip.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2021
  4. * Köry Maincent, Bootlin, <kory.maincent@bootlin.com>
  5. * Based on initial code from Maxime Ripard
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <dm.h>
  10. #include <w1.h>
  11. #include <w1-eeprom.h>
  12. #include <dm/device-internal.h>
  13. #include <asm/arch/gpio.h>
  14. #include <extension_board.h>
  15. #define for_each_w1_device(b, d) \
  16. for (device_find_first_child(b, d); *d; device_find_next_child(d))
  17. #define dip_convert(field) \
  18. ( \
  19. (sizeof(field) == 1) ? field : \
  20. (sizeof(field) == 2) ? be16_to_cpu(field) : \
  21. (sizeof(field) == 4) ? be32_to_cpu(field) : \
  22. -1 \
  23. )
  24. #define DIP_MAGIC 0x50494843 /* CHIP */
  25. struct dip_w1_header {
  26. u32 magic; /* CHIP */
  27. u8 version; /* spec version */
  28. u32 vendor_id;
  29. u16 product_id;
  30. u8 product_version;
  31. char vendor_name[32];
  32. char product_name[32];
  33. u8 rsvd[36]; /* rsvd for future spec versions */
  34. u8 data[16]; /* user data, per-dip specific */
  35. } __packed;
  36. int extension_board_scan(struct list_head *extension_list)
  37. {
  38. struct extension *dip;
  39. struct dip_w1_header w1_header;
  40. struct udevice *bus, *dev;
  41. u32 vid;
  42. u16 pid;
  43. int ret;
  44. int num_dip = 0;
  45. sunxi_gpio_set_pull(SUNXI_GPD(2), SUNXI_GPIO_PULL_UP);
  46. ret = w1_get_bus(0, &bus);
  47. if (ret) {
  48. printf("one wire interface not found\n");
  49. return 0;
  50. }
  51. for_each_w1_device(bus, &dev) {
  52. if (w1_get_device_family(dev) != W1_FAMILY_DS2431)
  53. continue;
  54. ret = device_probe(dev);
  55. if (ret) {
  56. printf("Couldn't probe device %s: error %d",
  57. dev->name, ret);
  58. continue;
  59. }
  60. w1_eeprom_read_buf(dev, 0, (u8 *)&w1_header, sizeof(w1_header));
  61. if (w1_header.magic != DIP_MAGIC)
  62. continue;
  63. vid = dip_convert(w1_header.vendor_id);
  64. pid = dip_convert(w1_header.product_id);
  65. printf("DIP: %s (0x%x) from %s (0x%x)\n",
  66. w1_header.product_name, pid,
  67. w1_header.vendor_name, vid);
  68. dip = calloc(1, sizeof(struct extension));
  69. if (!dip) {
  70. printf("Error in memory allocation\n");
  71. return num_dip;
  72. }
  73. snprintf(dip->overlay, sizeof(dip->overlay), "dip-%x-%x.dtbo",
  74. vid, pid);
  75. strncpy(dip->name, w1_header.product_name, 32);
  76. strncpy(dip->owner, w1_header.vendor_name, 32);
  77. list_add_tail(&dip->list, extension_list);
  78. num_dip++;
  79. }
  80. return num_dip;
  81. }