Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * w1_ds2431.c - w1 family 2d (DS2431) driver
0004  *
0005  * Copyright (c) 2008 Bernhard Weirich <bernhard.weirich@riedel.net>
0006  *
0007  * Heavily inspired by w1_DS2433 driver from Ben Gardner <bgardner@wabtec.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/device.h>
0014 #include <linux/types.h>
0015 #include <linux/delay.h>
0016 
0017 #include <linux/w1.h>
0018 
0019 #define W1_EEPROM_DS2431    0x2D
0020 
0021 #define W1_F2D_EEPROM_SIZE      128
0022 #define W1_F2D_PAGE_COUNT       4
0023 #define W1_F2D_PAGE_BITS        5
0024 #define W1_F2D_PAGE_SIZE        (1<<W1_F2D_PAGE_BITS)
0025 #define W1_F2D_PAGE_MASK        0x1F
0026 
0027 #define W1_F2D_SCRATCH_BITS  3
0028 #define W1_F2D_SCRATCH_SIZE  (1<<W1_F2D_SCRATCH_BITS)
0029 #define W1_F2D_SCRATCH_MASK  (W1_F2D_SCRATCH_SIZE-1)
0030 
0031 #define W1_F2D_READ_EEPROM  0xF0
0032 #define W1_F2D_WRITE_SCRATCH    0x0F
0033 #define W1_F2D_READ_SCRATCH 0xAA
0034 #define W1_F2D_COPY_SCRATCH 0x55
0035 
0036 
0037 #define W1_F2D_TPROG_MS     11
0038 
0039 #define W1_F2D_READ_RETRIES     10
0040 #define W1_F2D_READ_MAXLEN      8
0041 
0042 /*
0043  * Check the file size bounds and adjusts count as needed.
0044  * This would not be needed if the file size didn't reset to 0 after a write.
0045  */
0046 static inline size_t w1_f2d_fix_count(loff_t off, size_t count, size_t size)
0047 {
0048     if (off > size)
0049         return 0;
0050 
0051     if ((off + count) > size)
0052         return size - off;
0053 
0054     return count;
0055 }
0056 
0057 /*
0058  * Read a block from W1 ROM two times and compares the results.
0059  * If they are equal they are returned, otherwise the read
0060  * is repeated W1_F2D_READ_RETRIES times.
0061  *
0062  * count must not exceed W1_F2D_READ_MAXLEN.
0063  */
0064 static int w1_f2d_readblock(struct w1_slave *sl, int off, int count, char *buf)
0065 {
0066     u8 wrbuf[3];
0067     u8 cmp[W1_F2D_READ_MAXLEN];
0068     int tries = W1_F2D_READ_RETRIES;
0069 
0070     do {
0071         wrbuf[0] = W1_F2D_READ_EEPROM;
0072         wrbuf[1] = off & 0xff;
0073         wrbuf[2] = off >> 8;
0074 
0075         if (w1_reset_select_slave(sl))
0076             return -1;
0077 
0078         w1_write_block(sl->master, wrbuf, 3);
0079         w1_read_block(sl->master, buf, count);
0080 
0081         if (w1_reset_select_slave(sl))
0082             return -1;
0083 
0084         w1_write_block(sl->master, wrbuf, 3);
0085         w1_read_block(sl->master, cmp, count);
0086 
0087         if (!memcmp(cmp, buf, count))
0088             return 0;
0089     } while (--tries);
0090 
0091     dev_err(&sl->dev, "proof reading failed %d times\n",
0092             W1_F2D_READ_RETRIES);
0093 
0094     return -1;
0095 }
0096 
0097 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
0098                struct bin_attribute *bin_attr, char *buf,
0099                loff_t off, size_t count)
0100 {
0101     struct w1_slave *sl = kobj_to_w1_slave(kobj);
0102     int todo = count;
0103 
0104     count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
0105     if (count == 0)
0106         return 0;
0107 
0108     mutex_lock(&sl->master->bus_mutex);
0109 
0110     /* read directly from the EEPROM in chunks of W1_F2D_READ_MAXLEN */
0111     while (todo > 0) {
0112         int block_read;
0113 
0114         if (todo >= W1_F2D_READ_MAXLEN)
0115             block_read = W1_F2D_READ_MAXLEN;
0116         else
0117             block_read = todo;
0118 
0119         if (w1_f2d_readblock(sl, off, block_read, buf) < 0)
0120             count = -EIO;
0121 
0122         todo -= W1_F2D_READ_MAXLEN;
0123         buf += W1_F2D_READ_MAXLEN;
0124         off += W1_F2D_READ_MAXLEN;
0125     }
0126 
0127     mutex_unlock(&sl->master->bus_mutex);
0128 
0129     return count;
0130 }
0131 
0132 /*
0133  * Writes to the scratchpad and reads it back for verification.
0134  * Then copies the scratchpad to EEPROM.
0135  * The data must be aligned at W1_F2D_SCRATCH_SIZE bytes and
0136  * must be W1_F2D_SCRATCH_SIZE bytes long.
0137  * The master must be locked.
0138  *
0139  * @param sl    The slave structure
0140  * @param addr  Address for the write
0141  * @param len   length must be <= (W1_F2D_PAGE_SIZE - (addr & W1_F2D_PAGE_MASK))
0142  * @param data  The data to write
0143  * @return  0=Success -1=failure
0144  */
0145 static int w1_f2d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
0146 {
0147     int tries = W1_F2D_READ_RETRIES;
0148     u8 wrbuf[4];
0149     u8 rdbuf[W1_F2D_SCRATCH_SIZE + 3];
0150     u8 es = (addr + len - 1) % W1_F2D_SCRATCH_SIZE;
0151 
0152 retry:
0153 
0154     /* Write the data to the scratchpad */
0155     if (w1_reset_select_slave(sl))
0156         return -1;
0157 
0158     wrbuf[0] = W1_F2D_WRITE_SCRATCH;
0159     wrbuf[1] = addr & 0xff;
0160     wrbuf[2] = addr >> 8;
0161 
0162     w1_write_block(sl->master, wrbuf, 3);
0163     w1_write_block(sl->master, data, len);
0164 
0165     /* Read the scratchpad and verify */
0166     if (w1_reset_select_slave(sl))
0167         return -1;
0168 
0169     w1_write_8(sl->master, W1_F2D_READ_SCRATCH);
0170     w1_read_block(sl->master, rdbuf, len + 3);
0171 
0172     /* Compare what was read against the data written */
0173     if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
0174         (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) {
0175 
0176         if (--tries)
0177             goto retry;
0178 
0179         dev_err(&sl->dev,
0180             "could not write to eeprom, scratchpad compare failed %d times\n",
0181             W1_F2D_READ_RETRIES);
0182 
0183         return -1;
0184     }
0185 
0186     /* Copy the scratchpad to EEPROM */
0187     if (w1_reset_select_slave(sl))
0188         return -1;
0189 
0190     wrbuf[0] = W1_F2D_COPY_SCRATCH;
0191     wrbuf[3] = es;
0192     w1_write_block(sl->master, wrbuf, 4);
0193 
0194     /* Sleep for tprog ms to wait for the write to complete */
0195     msleep(W1_F2D_TPROG_MS);
0196 
0197     /* Reset the bus to wake up the EEPROM  */
0198     w1_reset_bus(sl->master);
0199 
0200     return 0;
0201 }
0202 
0203 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
0204                 struct bin_attribute *bin_attr, char *buf,
0205                 loff_t off, size_t count)
0206 {
0207     struct w1_slave *sl = kobj_to_w1_slave(kobj);
0208     int addr, len;
0209     int copy;
0210 
0211     count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
0212     if (count == 0)
0213         return 0;
0214 
0215     mutex_lock(&sl->master->bus_mutex);
0216 
0217     /* Can only write data in blocks of the size of the scratchpad */
0218     addr = off;
0219     len = count;
0220     while (len > 0) {
0221 
0222         /* if len too short or addr not aligned */
0223         if (len < W1_F2D_SCRATCH_SIZE || addr & W1_F2D_SCRATCH_MASK) {
0224             char tmp[W1_F2D_SCRATCH_SIZE];
0225 
0226             /* read the block and update the parts to be written */
0227             if (w1_f2d_readblock(sl, addr & ~W1_F2D_SCRATCH_MASK,
0228                     W1_F2D_SCRATCH_SIZE, tmp)) {
0229                 count = -EIO;
0230                 goto out_up;
0231             }
0232 
0233             /* copy at most to the boundary of the PAGE or len */
0234             copy = W1_F2D_SCRATCH_SIZE -
0235                 (addr & W1_F2D_SCRATCH_MASK);
0236 
0237             if (copy > len)
0238                 copy = len;
0239 
0240             memcpy(&tmp[addr & W1_F2D_SCRATCH_MASK], buf, copy);
0241             if (w1_f2d_write(sl, addr & ~W1_F2D_SCRATCH_MASK,
0242                     W1_F2D_SCRATCH_SIZE, tmp) < 0) {
0243                 count = -EIO;
0244                 goto out_up;
0245             }
0246         } else {
0247 
0248             copy = W1_F2D_SCRATCH_SIZE;
0249             if (w1_f2d_write(sl, addr, copy, buf) < 0) {
0250                 count = -EIO;
0251                 goto out_up;
0252             }
0253         }
0254         buf += copy;
0255         addr += copy;
0256         len -= copy;
0257     }
0258 
0259 out_up:
0260     mutex_unlock(&sl->master->bus_mutex);
0261 
0262     return count;
0263 }
0264 
0265 static BIN_ATTR_RW(eeprom, W1_F2D_EEPROM_SIZE);
0266 
0267 static struct bin_attribute *w1_f2d_bin_attrs[] = {
0268     &bin_attr_eeprom,
0269     NULL,
0270 };
0271 
0272 static const struct attribute_group w1_f2d_group = {
0273     .bin_attrs = w1_f2d_bin_attrs,
0274 };
0275 
0276 static const struct attribute_group *w1_f2d_groups[] = {
0277     &w1_f2d_group,
0278     NULL,
0279 };
0280 
0281 static const struct w1_family_ops w1_f2d_fops = {
0282     .groups     = w1_f2d_groups,
0283 };
0284 
0285 static struct w1_family w1_family_2d = {
0286     .fid = W1_EEPROM_DS2431,
0287     .fops = &w1_f2d_fops,
0288 };
0289 module_w1_family(w1_family_2d);
0290 
0291 MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
0292 MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
0293 MODULE_LICENSE("GPL");
0294 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431));