w1-uclass.c 4.7 KB

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