18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <asm/io.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/delay.h> 98c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "w1_internal.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic int w1_delay_parm = 1; 158c2ecf20Sopenharmony_cimodule_param_named(delay_coef, w1_delay_parm, int, 0); 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic int w1_disable_irqs = 0; 188c2ecf20Sopenharmony_cimodule_param_named(disable_irqs, w1_disable_irqs, int, 0); 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistatic u8 w1_crc8_table[] = { 218c2ecf20Sopenharmony_ci 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 228c2ecf20Sopenharmony_ci 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 238c2ecf20Sopenharmony_ci 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 248c2ecf20Sopenharmony_ci 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 258c2ecf20Sopenharmony_ci 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 268c2ecf20Sopenharmony_ci 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 278c2ecf20Sopenharmony_ci 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 288c2ecf20Sopenharmony_ci 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 298c2ecf20Sopenharmony_ci 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 308c2ecf20Sopenharmony_ci 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 318c2ecf20Sopenharmony_ci 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 328c2ecf20Sopenharmony_ci 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 338c2ecf20Sopenharmony_ci 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 348c2ecf20Sopenharmony_ci 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 358c2ecf20Sopenharmony_ci 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, 368c2ecf20Sopenharmony_ci 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic void w1_delay(unsigned long tm) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci udelay(tm * w1_delay_parm); 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic void w1_write_bit(struct w1_master *dev, int bit); 458c2ecf20Sopenharmony_cistatic u8 w1_read_bit(struct w1_master *dev); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/** 488c2ecf20Sopenharmony_ci * w1_touch_bit() - Generates a write-0 or write-1 cycle and samples the level. 498c2ecf20Sopenharmony_ci * @dev: the master device 508c2ecf20Sopenharmony_ci * @bit: 0 - write a 0, 1 - write a 0 read the level 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_ciu8 w1_touch_bit(struct w1_master *dev, int bit) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci if (dev->bus_master->touch_bit) 558c2ecf20Sopenharmony_ci return dev->bus_master->touch_bit(dev->bus_master->data, bit); 568c2ecf20Sopenharmony_ci else if (bit) 578c2ecf20Sopenharmony_ci return w1_read_bit(dev); 588c2ecf20Sopenharmony_ci else { 598c2ecf20Sopenharmony_ci w1_write_bit(dev, 0); 608c2ecf20Sopenharmony_ci return 0; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_touch_bit); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/** 668c2ecf20Sopenharmony_ci * w1_write_bit() - Generates a write-0 or write-1 cycle. 678c2ecf20Sopenharmony_ci * @dev: the master device 688c2ecf20Sopenharmony_ci * @bit: bit to write 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * Only call if dev->bus_master->touch_bit is NULL 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic void w1_write_bit(struct w1_master *dev, int bit) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci unsigned long flags = 0; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if(w1_disable_irqs) local_irq_save(flags); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci if (bit) { 798c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 0); 808c2ecf20Sopenharmony_ci w1_delay(6); 818c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 1); 828c2ecf20Sopenharmony_ci w1_delay(64); 838c2ecf20Sopenharmony_ci } else { 848c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 0); 858c2ecf20Sopenharmony_ci w1_delay(60); 868c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 1); 878c2ecf20Sopenharmony_ci w1_delay(10); 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if(w1_disable_irqs) local_irq_restore(flags); 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci/** 948c2ecf20Sopenharmony_ci * w1_pre_write() - pre-write operations 958c2ecf20Sopenharmony_ci * @dev: the master device 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * Pre-write operation, currently only supporting strong pullups. 988c2ecf20Sopenharmony_ci * Program the hardware for a strong pullup, if one has been requested and 998c2ecf20Sopenharmony_ci * the hardware supports it. 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistatic void w1_pre_write(struct w1_master *dev) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci if (dev->pullup_duration && 1048c2ecf20Sopenharmony_ci dev->enable_pullup && dev->bus_master->set_pullup) { 1058c2ecf20Sopenharmony_ci dev->bus_master->set_pullup(dev->bus_master->data, 1068c2ecf20Sopenharmony_ci dev->pullup_duration); 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci/** 1118c2ecf20Sopenharmony_ci * w1_post_write() - post-write options 1128c2ecf20Sopenharmony_ci * @dev: the master device 1138c2ecf20Sopenharmony_ci * 1148c2ecf20Sopenharmony_ci * Post-write operation, currently only supporting strong pullups. 1158c2ecf20Sopenharmony_ci * If a strong pullup was requested, clear it if the hardware supports 1168c2ecf20Sopenharmony_ci * them, or execute the delay otherwise, in either case clear the request. 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_cistatic void w1_post_write(struct w1_master *dev) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci if (dev->pullup_duration) { 1218c2ecf20Sopenharmony_ci if (dev->enable_pullup && dev->bus_master->set_pullup) 1228c2ecf20Sopenharmony_ci dev->bus_master->set_pullup(dev->bus_master->data, 0); 1238c2ecf20Sopenharmony_ci else 1248c2ecf20Sopenharmony_ci msleep(dev->pullup_duration); 1258c2ecf20Sopenharmony_ci dev->pullup_duration = 0; 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/** 1308c2ecf20Sopenharmony_ci * w1_write_8() - Writes 8 bits. 1318c2ecf20Sopenharmony_ci * @dev: the master device 1328c2ecf20Sopenharmony_ci * @byte: the byte to write 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_civoid w1_write_8(struct w1_master *dev, u8 byte) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci int i; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (dev->bus_master->write_byte) { 1398c2ecf20Sopenharmony_ci w1_pre_write(dev); 1408c2ecf20Sopenharmony_ci dev->bus_master->write_byte(dev->bus_master->data, byte); 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci else 1438c2ecf20Sopenharmony_ci for (i = 0; i < 8; ++i) { 1448c2ecf20Sopenharmony_ci if (i == 7) 1458c2ecf20Sopenharmony_ci w1_pre_write(dev); 1468c2ecf20Sopenharmony_ci w1_touch_bit(dev, (byte >> i) & 0x1); 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci w1_post_write(dev); 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_write_8); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/** 1548c2ecf20Sopenharmony_ci * w1_read_bit() - Generates a write-1 cycle and samples the level. 1558c2ecf20Sopenharmony_ci * @dev: the master device 1568c2ecf20Sopenharmony_ci * 1578c2ecf20Sopenharmony_ci * Only call if dev->bus_master->touch_bit is NULL 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_cistatic u8 w1_read_bit(struct w1_master *dev) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci int result; 1628c2ecf20Sopenharmony_ci unsigned long flags = 0; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* sample timing is critical here */ 1658c2ecf20Sopenharmony_ci local_irq_save(flags); 1668c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 0); 1678c2ecf20Sopenharmony_ci w1_delay(6); 1688c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 1); 1698c2ecf20Sopenharmony_ci w1_delay(9); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci result = dev->bus_master->read_bit(dev->bus_master->data); 1728c2ecf20Sopenharmony_ci local_irq_restore(flags); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci w1_delay(55); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return result & 0x1; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/** 1808c2ecf20Sopenharmony_ci * w1_triplet() - * Does a triplet - used for searching ROM addresses. 1818c2ecf20Sopenharmony_ci * @dev: the master device 1828c2ecf20Sopenharmony_ci * @bdir: the bit to write if both id_bit and comp_bit are 0 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * Return bits: 1858c2ecf20Sopenharmony_ci * bit 0 = id_bit 1868c2ecf20Sopenharmony_ci * bit 1 = comp_bit 1878c2ecf20Sopenharmony_ci * bit 2 = dir_taken 1888c2ecf20Sopenharmony_ci * 1898c2ecf20Sopenharmony_ci * If both bits 0 & 1 are set, the search should be restarted. 1908c2ecf20Sopenharmony_ci * 1918c2ecf20Sopenharmony_ci * Return: bit fields - see above 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_ciu8 w1_triplet(struct w1_master *dev, int bdir) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci if (dev->bus_master->triplet) 1968c2ecf20Sopenharmony_ci return dev->bus_master->triplet(dev->bus_master->data, bdir); 1978c2ecf20Sopenharmony_ci else { 1988c2ecf20Sopenharmony_ci u8 id_bit = w1_touch_bit(dev, 1); 1998c2ecf20Sopenharmony_ci u8 comp_bit = w1_touch_bit(dev, 1); 2008c2ecf20Sopenharmony_ci u8 retval; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (id_bit && comp_bit) 2038c2ecf20Sopenharmony_ci return 0x03; /* error */ 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci if (!id_bit && !comp_bit) { 2068c2ecf20Sopenharmony_ci /* Both bits are valid, take the direction given */ 2078c2ecf20Sopenharmony_ci retval = bdir ? 0x04 : 0; 2088c2ecf20Sopenharmony_ci } else { 2098c2ecf20Sopenharmony_ci /* Only one bit is valid, take that direction */ 2108c2ecf20Sopenharmony_ci bdir = id_bit; 2118c2ecf20Sopenharmony_ci retval = id_bit ? 0x05 : 0x02; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci if (dev->bus_master->touch_bit) 2158c2ecf20Sopenharmony_ci w1_touch_bit(dev, bdir); 2168c2ecf20Sopenharmony_ci else 2178c2ecf20Sopenharmony_ci w1_write_bit(dev, bdir); 2188c2ecf20Sopenharmony_ci return retval; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_triplet); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci/** 2248c2ecf20Sopenharmony_ci * w1_read_8() - Reads 8 bits. 2258c2ecf20Sopenharmony_ci * @dev: the master device 2268c2ecf20Sopenharmony_ci * 2278c2ecf20Sopenharmony_ci * Return: the byte read 2288c2ecf20Sopenharmony_ci */ 2298c2ecf20Sopenharmony_ciu8 w1_read_8(struct w1_master *dev) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci int i; 2328c2ecf20Sopenharmony_ci u8 res = 0; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci if (dev->bus_master->read_byte) 2358c2ecf20Sopenharmony_ci res = dev->bus_master->read_byte(dev->bus_master->data); 2368c2ecf20Sopenharmony_ci else 2378c2ecf20Sopenharmony_ci for (i = 0; i < 8; ++i) 2388c2ecf20Sopenharmony_ci res |= (w1_touch_bit(dev,1) << i); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci return res; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_read_8); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci/** 2458c2ecf20Sopenharmony_ci * w1_write_block() - Writes a series of bytes. 2468c2ecf20Sopenharmony_ci * @dev: the master device 2478c2ecf20Sopenharmony_ci * @buf: pointer to the data to write 2488c2ecf20Sopenharmony_ci * @len: the number of bytes to write 2498c2ecf20Sopenharmony_ci */ 2508c2ecf20Sopenharmony_civoid w1_write_block(struct w1_master *dev, const u8 *buf, int len) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci int i; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (dev->bus_master->write_block) { 2558c2ecf20Sopenharmony_ci w1_pre_write(dev); 2568c2ecf20Sopenharmony_ci dev->bus_master->write_block(dev->bus_master->data, buf, len); 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci else 2598c2ecf20Sopenharmony_ci for (i = 0; i < len; ++i) 2608c2ecf20Sopenharmony_ci w1_write_8(dev, buf[i]); /* calls w1_pre_write */ 2618c2ecf20Sopenharmony_ci w1_post_write(dev); 2628c2ecf20Sopenharmony_ci} 2638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_write_block); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci/** 2668c2ecf20Sopenharmony_ci * w1_touch_block() - Touches a series of bytes. 2678c2ecf20Sopenharmony_ci * @dev: the master device 2688c2ecf20Sopenharmony_ci * @buf: pointer to the data to write 2698c2ecf20Sopenharmony_ci * @len: the number of bytes to write 2708c2ecf20Sopenharmony_ci */ 2718c2ecf20Sopenharmony_civoid w1_touch_block(struct w1_master *dev, u8 *buf, int len) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci int i, j; 2748c2ecf20Sopenharmony_ci u8 tmp; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci for (i = 0; i < len; ++i) { 2778c2ecf20Sopenharmony_ci tmp = 0; 2788c2ecf20Sopenharmony_ci for (j = 0; j < 8; ++j) { 2798c2ecf20Sopenharmony_ci if (j == 7) 2808c2ecf20Sopenharmony_ci w1_pre_write(dev); 2818c2ecf20Sopenharmony_ci tmp |= w1_touch_bit(dev, (buf[i] >> j) & 0x1) << j; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci buf[i] = tmp; 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_touch_block); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci/** 2908c2ecf20Sopenharmony_ci * w1_read_block() - Reads a series of bytes. 2918c2ecf20Sopenharmony_ci * @dev: the master device 2928c2ecf20Sopenharmony_ci * @buf: pointer to the buffer to fill 2938c2ecf20Sopenharmony_ci * @len: the number of bytes to read 2948c2ecf20Sopenharmony_ci * Return: the number of bytes read 2958c2ecf20Sopenharmony_ci */ 2968c2ecf20Sopenharmony_ciu8 w1_read_block(struct w1_master *dev, u8 *buf, int len) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci int i; 2998c2ecf20Sopenharmony_ci u8 ret; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci if (dev->bus_master->read_block) 3028c2ecf20Sopenharmony_ci ret = dev->bus_master->read_block(dev->bus_master->data, buf, len); 3038c2ecf20Sopenharmony_ci else { 3048c2ecf20Sopenharmony_ci for (i = 0; i < len; ++i) 3058c2ecf20Sopenharmony_ci buf[i] = w1_read_8(dev); 3068c2ecf20Sopenharmony_ci ret = len; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci return ret; 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_read_block); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci/** 3148c2ecf20Sopenharmony_ci * w1_reset_bus() - Issues a reset bus sequence. 3158c2ecf20Sopenharmony_ci * @dev: the master device 3168c2ecf20Sopenharmony_ci * Return: 0=Device present, 1=No device present or error 3178c2ecf20Sopenharmony_ci */ 3188c2ecf20Sopenharmony_ciint w1_reset_bus(struct w1_master *dev) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci int result; 3218c2ecf20Sopenharmony_ci unsigned long flags = 0; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci if(w1_disable_irqs) local_irq_save(flags); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (dev->bus_master->reset_bus) 3268c2ecf20Sopenharmony_ci result = dev->bus_master->reset_bus(dev->bus_master->data) & 0x1; 3278c2ecf20Sopenharmony_ci else { 3288c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 0); 3298c2ecf20Sopenharmony_ci /* minimum 480, max ? us 3308c2ecf20Sopenharmony_ci * be nice and sleep, except 18b20 spec lists 960us maximum, 3318c2ecf20Sopenharmony_ci * so until we can sleep with microsecond accuracy, spin. 3328c2ecf20Sopenharmony_ci * Feel free to come up with some other way to give up the 3338c2ecf20Sopenharmony_ci * cpu for such a short amount of time AND get it back in 3348c2ecf20Sopenharmony_ci * the maximum amount of time. 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_ci w1_delay(500); 3378c2ecf20Sopenharmony_ci dev->bus_master->write_bit(dev->bus_master->data, 1); 3388c2ecf20Sopenharmony_ci w1_delay(70); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci result = dev->bus_master->read_bit(dev->bus_master->data) & 0x1; 3418c2ecf20Sopenharmony_ci /* minimum 70 (above) + 430 = 500 us 3428c2ecf20Sopenharmony_ci * There aren't any timing requirements between a reset and 3438c2ecf20Sopenharmony_ci * the following transactions. Sleeping is safe here. 3448c2ecf20Sopenharmony_ci */ 3458c2ecf20Sopenharmony_ci /* w1_delay(430); min required time */ 3468c2ecf20Sopenharmony_ci msleep(1); 3478c2ecf20Sopenharmony_ci } 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci if(w1_disable_irqs) local_irq_restore(flags); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci return result; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_reset_bus); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ciu8 w1_calc_crc8(u8 * data, int len) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci u8 crc = 0; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci while (len--) 3608c2ecf20Sopenharmony_ci crc = w1_crc8_table[crc ^ *data++]; 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci return crc; 3638c2ecf20Sopenharmony_ci} 3648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_calc_crc8); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_civoid w1_search_devices(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb) 3678c2ecf20Sopenharmony_ci{ 3688c2ecf20Sopenharmony_ci dev->attempts++; 3698c2ecf20Sopenharmony_ci if (dev->bus_master->search) 3708c2ecf20Sopenharmony_ci dev->bus_master->search(dev->bus_master->data, dev, 3718c2ecf20Sopenharmony_ci search_type, cb); 3728c2ecf20Sopenharmony_ci else 3738c2ecf20Sopenharmony_ci w1_search(dev, search_type, cb); 3748c2ecf20Sopenharmony_ci} 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci/** 3778c2ecf20Sopenharmony_ci * w1_reset_select_slave() - reset and select a slave 3788c2ecf20Sopenharmony_ci * @sl: the slave to select 3798c2ecf20Sopenharmony_ci * 3808c2ecf20Sopenharmony_ci * Resets the bus and then selects the slave by sending either a skip rom 3818c2ecf20Sopenharmony_ci * or a rom match. A skip rom is issued if there is only one device 3828c2ecf20Sopenharmony_ci * registered on the bus. 3838c2ecf20Sopenharmony_ci * The w1 master lock must be held. 3848c2ecf20Sopenharmony_ci * 3858c2ecf20Sopenharmony_ci * Return: 0=success, anything else=error 3868c2ecf20Sopenharmony_ci */ 3878c2ecf20Sopenharmony_ciint w1_reset_select_slave(struct w1_slave *sl) 3888c2ecf20Sopenharmony_ci{ 3898c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 3908c2ecf20Sopenharmony_ci return -1; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci if (sl->master->slave_count == 1) 3938c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_SKIP_ROM); 3948c2ecf20Sopenharmony_ci else { 3958c2ecf20Sopenharmony_ci u8 match[9] = {W1_MATCH_ROM, }; 3968c2ecf20Sopenharmony_ci u64 rn = le64_to_cpu(*((u64*)&sl->reg_num)); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci memcpy(&match[1], &rn, 8); 3998c2ecf20Sopenharmony_ci w1_write_block(sl->master, match, 9); 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci return 0; 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_reset_select_slave); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci/** 4068c2ecf20Sopenharmony_ci * w1_reset_resume_command() - resume instead of another match ROM 4078c2ecf20Sopenharmony_ci * @dev: the master device 4088c2ecf20Sopenharmony_ci * 4098c2ecf20Sopenharmony_ci * When the workflow with a slave amongst many requires several 4108c2ecf20Sopenharmony_ci * successive commands a reset between each, this function is similar 4118c2ecf20Sopenharmony_ci * to doing a reset then a match ROM for the last matched ROM. The 4128c2ecf20Sopenharmony_ci * advantage being that the matched ROM step is skipped in favor of the 4138c2ecf20Sopenharmony_ci * resume command. The slave must support the command of course. 4148c2ecf20Sopenharmony_ci * 4158c2ecf20Sopenharmony_ci * If the bus has only one slave, traditionnaly the match ROM is skipped 4168c2ecf20Sopenharmony_ci * and a "SKIP ROM" is done for efficiency. On multi-slave busses, this 4178c2ecf20Sopenharmony_ci * doesn't work of course, but the resume command is the next best thing. 4188c2ecf20Sopenharmony_ci * 4198c2ecf20Sopenharmony_ci * The w1 master lock must be held. 4208c2ecf20Sopenharmony_ci */ 4218c2ecf20Sopenharmony_ciint w1_reset_resume_command(struct w1_master *dev) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci if (w1_reset_bus(dev)) 4248c2ecf20Sopenharmony_ci return -1; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci w1_write_8(dev, dev->slave_count > 1 ? W1_RESUME_CMD : W1_SKIP_ROM); 4278c2ecf20Sopenharmony_ci return 0; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_reset_resume_command); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci/** 4328c2ecf20Sopenharmony_ci * w1_next_pullup() - register for a strong pullup 4338c2ecf20Sopenharmony_ci * @dev: the master device 4348c2ecf20Sopenharmony_ci * @delay: time in milliseconds 4358c2ecf20Sopenharmony_ci * 4368c2ecf20Sopenharmony_ci * Put out a strong pull-up of the specified duration after the next write 4378c2ecf20Sopenharmony_ci * operation. Not all hardware supports strong pullups. Hardware that 4388c2ecf20Sopenharmony_ci * doesn't support strong pullups will sleep for the given time after the 4398c2ecf20Sopenharmony_ci * write operation without a strong pullup. This is a one shot request for 4408c2ecf20Sopenharmony_ci * the next write, specifying zero will clear a previous request. 4418c2ecf20Sopenharmony_ci * The w1 master lock must be held. 4428c2ecf20Sopenharmony_ci * 4438c2ecf20Sopenharmony_ci * Return: 0=success, anything else=error 4448c2ecf20Sopenharmony_ci */ 4458c2ecf20Sopenharmony_civoid w1_next_pullup(struct w1_master *dev, int delay) 4468c2ecf20Sopenharmony_ci{ 4478c2ecf20Sopenharmony_ci dev->pullup_duration = delay; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(w1_next_pullup); 450