w1-uclass.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * Copyright (c) 2015 Free Electrons
  5. * Copyright (c) 2015 NextThing Co.
  6. * Copyright (c) 2018 Microchip Technology, Inc.
  7. *
  8. * Maxime Ripard <maxime.ripard@free-electrons.com>
  9. * Eugen Hristev <eugen.hristev@microchip.com>
  10. *
  11. */
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <w1.h>
  15. #include <w1-eeprom.h>
  16. #include <dm/device-internal.h>
  17. #define W1_MATCH_ROM 0x55
  18. #define W1_SKIP_ROM 0xcc
  19. #define W1_SEARCH 0xf0
  20. struct w1_bus {
  21. u64 search_id;
  22. };
  23. static int w1_enumerate(struct udevice *bus)
  24. {
  25. const struct w1_ops *ops = device_get_ops(bus);
  26. struct w1_bus *w1 = dev_get_uclass_priv(bus);
  27. u64 last_rn, rn = w1->search_id, tmp64;
  28. bool last_device = false;
  29. int search_bit, desc_bit = 64;
  30. int last_zero = -1;
  31. u8 triplet_ret = 0;
  32. int i;
  33. if (!ops->reset || !ops->write_byte || !ops->triplet)
  34. return -ENOSYS;
  35. while (!last_device) {
  36. last_rn = rn;
  37. rn = 0;
  38. /*
  39. * Reset bus and all 1-wire device state machines
  40. * so they can respond to our requests.
  41. *
  42. * Return 0 - device(s) present, 1 - no devices present.
  43. */
  44. if (ops->reset(bus)) {
  45. debug("%s: No devices present on the wire.\n",
  46. __func__);
  47. break;
  48. }
  49. /* Start the search */
  50. ops->write_byte(bus, W1_SEARCH);
  51. for (i = 0; i < 64; ++i) {
  52. /* Determine the direction/search bit */
  53. if (i == desc_bit)
  54. /* took the 0 path last time, so take the 1 path */
  55. search_bit = 1;
  56. else if (i > desc_bit)
  57. /* take the 0 path on the next branch */
  58. search_bit = 0;
  59. else
  60. search_bit = ((last_rn >> i) & 0x1);
  61. /* Read two bits and write one bit */
  62. triplet_ret = ops->triplet(bus, search_bit);
  63. /* quit if no device responded */
  64. if ((triplet_ret & 0x03) == 0x03)
  65. break;
  66. /* If both directions were valid, and we took the 0 path... */
  67. if (triplet_ret == 0)
  68. last_zero = i;
  69. /* extract the direction taken & update the device number */
  70. tmp64 = (triplet_ret >> 2);
  71. rn |= (tmp64 << i);
  72. }
  73. /* last device or error, aborting here */
  74. if ((triplet_ret & 0x03) == 0x03)
  75. last_device = true;
  76. if ((triplet_ret & 0x03) != 0x03) {
  77. if (desc_bit == last_zero || last_zero < 0) {
  78. last_device = 1;
  79. w1->search_id = 0;
  80. } else {
  81. w1->search_id = rn;
  82. }
  83. desc_bit = last_zero;
  84. debug("%s: Detected new device 0x%llx (family 0x%x)\n",
  85. bus->name, rn, (u8)(rn & 0xff));
  86. /* attempt to register as w1-eeprom device */
  87. w1_eeprom_register_new_device(rn);
  88. }
  89. }
  90. return 0;
  91. }
  92. int w1_get_bus(int busnum, struct udevice **busp)
  93. {
  94. int ret, i = 0;
  95. struct udevice *dev;
  96. for (ret = uclass_first_device(UCLASS_W1, &dev);
  97. !ret;
  98. uclass_next_device(&dev), i++) {
  99. if (ret) {
  100. debug("Cannot find w1 bus %d\n", busnum);
  101. return ret;
  102. }
  103. if (i == busnum) {
  104. *busp = dev;
  105. return 0;
  106. }
  107. }
  108. return ret;
  109. }
  110. u8 w1_get_device_family(struct udevice *dev)
  111. {
  112. struct w1_device *w1 = dev_get_parent_platdata(dev);
  113. return w1->id & 0xff;
  114. }
  115. int w1_reset_select(struct udevice *dev)
  116. {
  117. struct w1_device *w1 = dev_get_parent_platdata(dev);
  118. struct udevice *bus = dev_get_parent(dev);
  119. const struct w1_ops *ops = device_get_ops(bus);
  120. int i;
  121. if (!ops->reset || !ops->write_byte)
  122. return -ENOSYS;
  123. ops->reset(bus);
  124. ops->write_byte(bus, W1_MATCH_ROM);
  125. for (i = 0; i < sizeof(w1->id); i++)
  126. ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
  127. return 0;
  128. }
  129. int w1_read_byte(struct udevice *dev)
  130. {
  131. struct udevice *bus = dev_get_parent(dev);
  132. const struct w1_ops *ops = device_get_ops(bus);
  133. if (!ops->read_byte)
  134. return -ENOSYS;
  135. return ops->read_byte(bus);
  136. }
  137. int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
  138. {
  139. int i, ret;
  140. for (i = 0; i < count; i++) {
  141. ret = w1_read_byte(dev);
  142. if (ret < 0)
  143. return ret;
  144. buf[i] = ret & 0xff;
  145. }
  146. return 0;
  147. }
  148. int w1_write_byte(struct udevice *dev, u8 byte)
  149. {
  150. struct udevice *bus = dev_get_parent(dev);
  151. const struct w1_ops *ops = device_get_ops(bus);
  152. if (!ops->write_byte)
  153. return -ENOSYS;
  154. ops->write_byte(bus, byte);
  155. return 0;
  156. }
  157. static int w1_post_probe(struct udevice *bus)
  158. {
  159. w1_enumerate(bus);
  160. return 0;
  161. }
  162. int w1_init(void)
  163. {
  164. struct udevice *bus;
  165. struct uclass *uc;
  166. int ret;
  167. ret = uclass_get(UCLASS_W1, &uc);
  168. if (ret)
  169. return ret;
  170. uclass_foreach_dev(bus, uc) {
  171. ret = device_probe(bus);
  172. if (ret == -ENODEV) { /* No such device. */
  173. printf("W1 controller not available.\n");
  174. continue;
  175. }
  176. if (ret) { /* Other error. */
  177. printf("W1 controller probe failed.\n");
  178. continue;
  179. }
  180. }
  181. return 0;
  182. }
  183. UCLASS_DRIVER(w1) = {
  184. .name = "w1",
  185. .id = UCLASS_W1,
  186. .flags = DM_UC_FLAG_SEQ_ALIAS,
  187. .per_device_auto_alloc_size = sizeof(struct w1_bus),
  188. .post_probe = w1_post_probe,
  189. #if CONFIG_IS_ENABLED(OF_CONTROL)
  190. .post_bind = dm_scan_fdt_dev,
  191. #endif
  192. .per_child_platdata_auto_alloc_size = sizeof(struct w1_device),
  193. };