w1-uclass.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. * Copyright (c) 2021 Bootlin
  8. *
  9. * Maxime Ripard <maxime.ripard@free-electrons.com>
  10. * Eugen Hristev <eugen.hristev@microchip.com>
  11. * Kory Maincent <kory.maincent@bootlin.com>
  12. *
  13. */
  14. #define LOG_CATEGORY UCLASS_W1
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <log.h>
  18. #include <w1.h>
  19. #include <w1-eeprom.h>
  20. #include <dm/device-internal.h>
  21. #define W1_MATCH_ROM 0x55
  22. #define W1_SKIP_ROM 0xcc
  23. #define W1_SEARCH 0xf0
  24. struct w1_bus {
  25. u64 search_id;
  26. };
  27. int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice
  28. **devp)
  29. {
  30. struct udevice *dev;
  31. u8 family = id & 0xff;
  32. int ret;
  33. for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev);
  34. !ret && dev;
  35. uclass_next_device(&dev)) {
  36. if (ret || !dev) {
  37. debug("cannot find w1 eeprom dev\n");
  38. return -ENODEV;
  39. }
  40. if (dev_get_driver_data(dev) == family) {
  41. *devp = dev;
  42. return 0;
  43. }
  44. }
  45. return -ENODEV;
  46. }
  47. int w1_register_new_device(u64 id, struct udevice *bus)
  48. {
  49. u8 family = id & 0xff;
  50. int n_ents, ret = 0;
  51. struct udevice *dev;
  52. struct w1_driver_entry *start, *entry;
  53. start = ll_entry_start(struct w1_driver_entry, w1_driver_entry);
  54. n_ents = ll_entry_count(struct w1_driver_entry, w1_driver_entry);
  55. for (entry = start; entry != start + n_ents; entry++) {
  56. const u8 *match_family;
  57. const struct driver *drv;
  58. struct w1_device *w1;
  59. for (match_family = entry->family; match_family;
  60. match_family++) {
  61. if (*match_family != family)
  62. continue;
  63. ret = w1_bus_find_dev(bus, id, &dev);
  64. /* If nothing in the device tree, bind a device */
  65. if (ret == -ENODEV) {
  66. drv = entry->driver;
  67. ret = device_bind(bus, drv, drv->name,
  68. NULL, ofnode_null(), &dev);
  69. if (ret)
  70. return ret;
  71. }
  72. device_probe(dev);
  73. w1 = dev_get_parent_plat(dev);
  74. w1->id = id;
  75. return 0;
  76. }
  77. }
  78. debug("%s: No matches found: error %d\n", __func__, ret);
  79. return ret;
  80. }
  81. static int w1_enumerate(struct udevice *bus)
  82. {
  83. const struct w1_ops *ops = device_get_ops(bus);
  84. struct w1_bus *w1 = dev_get_uclass_priv(bus);
  85. u64 last_rn, rn = w1->search_id, tmp64;
  86. bool last_device = false;
  87. int search_bit, desc_bit = 64;
  88. int last_zero = -1;
  89. u8 triplet_ret = 0;
  90. int i;
  91. if (!ops->reset || !ops->write_byte || !ops->triplet)
  92. return -ENOSYS;
  93. while (!last_device) {
  94. last_rn = rn;
  95. rn = 0;
  96. /*
  97. * Reset bus and all 1-wire device state machines
  98. * so they can respond to our requests.
  99. *
  100. * Return 0 - device(s) present, 1 - no devices present.
  101. */
  102. if (ops->reset(bus)) {
  103. debug("%s: No devices present on the wire.\n",
  104. __func__);
  105. break;
  106. }
  107. /* Start the search */
  108. ops->write_byte(bus, W1_SEARCH);
  109. for (i = 0; i < 64; ++i) {
  110. /* Determine the direction/search bit */
  111. if (i == desc_bit)
  112. /* took the 0 path last time, so take the 1 path */
  113. search_bit = 1;
  114. else if (i > desc_bit)
  115. /* take the 0 path on the next branch */
  116. search_bit = 0;
  117. else
  118. search_bit = ((last_rn >> i) & 0x1);
  119. /* Read two bits and write one bit */
  120. triplet_ret = ops->triplet(bus, search_bit);
  121. /* quit if no device responded */
  122. if ((triplet_ret & 0x03) == 0x03)
  123. break;
  124. /* If both directions were valid, and we took the 0 path... */
  125. if (triplet_ret == 0)
  126. last_zero = i;
  127. /* extract the direction taken & update the device number */
  128. tmp64 = (triplet_ret >> 2);
  129. rn |= (tmp64 << i);
  130. }
  131. if ((triplet_ret & 0x03) != 0x03) {
  132. if (desc_bit == last_zero || last_zero < 0) {
  133. last_device = 1;
  134. w1->search_id = 0;
  135. } else {
  136. w1->search_id = rn;
  137. }
  138. desc_bit = last_zero;
  139. debug("%s: Detected new device 0x%llx (family 0x%x)\n",
  140. bus->name, rn, (u8)(rn & 0xff));
  141. /* attempt to register as w1 device */
  142. w1_register_new_device(rn, bus);
  143. }
  144. }
  145. return 0;
  146. }
  147. int w1_get_bus(int busnum, struct udevice **busp)
  148. {
  149. int ret, i = 0;
  150. struct udevice *dev;
  151. for (ret = uclass_first_device(UCLASS_W1, &dev);
  152. dev && !ret;
  153. ret = uclass_next_device(&dev), i++) {
  154. if (i == busnum) {
  155. *busp = dev;
  156. return 0;
  157. }
  158. }
  159. if (!ret) {
  160. debug("Cannot find w1 bus %d\n", busnum);
  161. ret = -ENODEV;
  162. }
  163. return ret;
  164. }
  165. u8 w1_get_device_family(struct udevice *dev)
  166. {
  167. struct w1_device *w1 = dev_get_parent_plat(dev);
  168. return w1->id & 0xff;
  169. }
  170. int w1_reset_select(struct udevice *dev)
  171. {
  172. struct w1_device *w1 = dev_get_parent_plat(dev);
  173. struct udevice *bus = dev_get_parent(dev);
  174. const struct w1_ops *ops = device_get_ops(bus);
  175. int i;
  176. if (!ops->reset || !ops->write_byte)
  177. return -ENOSYS;
  178. ops->reset(bus);
  179. ops->write_byte(bus, W1_MATCH_ROM);
  180. for (i = 0; i < sizeof(w1->id); i++)
  181. ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
  182. return 0;
  183. }
  184. int w1_read_byte(struct udevice *dev)
  185. {
  186. struct udevice *bus = dev_get_parent(dev);
  187. const struct w1_ops *ops = device_get_ops(bus);
  188. if (!ops->read_byte)
  189. return -ENOSYS;
  190. return ops->read_byte(bus);
  191. }
  192. int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
  193. {
  194. int i, ret;
  195. for (i = 0; i < count; i++) {
  196. ret = w1_read_byte(dev);
  197. if (ret < 0)
  198. return ret;
  199. buf[i] = ret & 0xff;
  200. }
  201. return 0;
  202. }
  203. int w1_write_byte(struct udevice *dev, u8 byte)
  204. {
  205. struct udevice *bus = dev_get_parent(dev);
  206. const struct w1_ops *ops = device_get_ops(bus);
  207. if (!ops->write_byte)
  208. return -ENOSYS;
  209. ops->write_byte(bus, byte);
  210. return 0;
  211. }
  212. static int w1_post_probe(struct udevice *bus)
  213. {
  214. w1_enumerate(bus);
  215. return 0;
  216. }
  217. int w1_init(void)
  218. {
  219. struct udevice *bus;
  220. struct uclass *uc;
  221. int ret;
  222. ret = uclass_get(UCLASS_W1, &uc);
  223. if (ret)
  224. return ret;
  225. uclass_foreach_dev(bus, uc) {
  226. ret = device_probe(bus);
  227. if (ret == -ENODEV) { /* No such device. */
  228. printf("W1 controller not available.\n");
  229. continue;
  230. }
  231. if (ret) { /* Other error. */
  232. printf("W1 controller probe failed.\n");
  233. continue;
  234. }
  235. }
  236. return 0;
  237. }
  238. UCLASS_DRIVER(w1) = {
  239. .name = "w1",
  240. .id = UCLASS_W1,
  241. .flags = DM_UC_FLAG_SEQ_ALIAS,
  242. .per_device_auto = sizeof(struct w1_bus),
  243. .post_probe = w1_post_probe,
  244. #if CONFIG_IS_ENABLED(OF_CONTROL)
  245. .post_bind = dm_scan_fdt_dev,
  246. #endif
  247. .per_child_plat_auto = sizeof(struct w1_device),
  248. };