1 /* Module for handling DALLAS DS2438, smart battery monitor
2 Chip can store up to 40 bytes of user data in EEPROM,
3 perform temp, voltage and current measurements.
4 Chip also contains a unique serial number.
6 Always read/write LSb first
8 For documentaion, see data sheet for DS2438, 2438.pdf
10 By Thomas.Lange@corelatus.com 001025
12 Copyright (C) 2000-2005 Corelatus AB */
14 /* This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/au1x00.h>
35 #include "ee_access.h"
37 /* static int Debug = 1; */
39 #define E_DEBUG(fmt,args...) /* */
40 /* #define E_DEBUG(fmt,args...) printk("EEA:"fmt,##args); */
42 /* We dont have kernel functions */
55 /* lookup table ripped from DS app note 17, understanding and using cyclic redundancy checks... */
57 static u8 crc_lookup
[256] = {
58 0, 94, 188, 226, 97, 63, 221, 131,
59 194, 156, 126, 32, 163, 253, 31, 65,
60 157, 195, 33, 127, 252, 162, 64, 30,
61 95, 1, 227, 189, 62, 96, 130, 220,
62 35, 125, 159, 193, 66, 28, 254, 160,
63 225, 191, 93, 3, 128, 222, 60, 98,
64 190, 224, 2, 92, 223, 129, 99, 61,
65 124, 34, 192, 158, 29, 67, 161, 255,
66 70, 24, 250, 164, 39, 121, 155, 197,
67 132, 218, 56, 102, 229, 187, 89, 7,
68 219, 133, 103, 57, 186, 228, 6, 88,
69 25, 71, 165, 251, 120, 38, 196, 154,
70 101, 59, 217, 135, 4, 90, 184, 230,
71 167, 249, 27, 69, 198, 152, 122, 36,
72 248, 166, 68, 26, 153, 199, 37, 123,
73 58, 100, 134, 216, 91, 5, 231, 185,
74 140, 210, 48, 110, 237, 179, 81, 15,
75 78, 16, 242, 172, 47, 113, 147, 205,
76 17, 79, 173, 243, 112, 46, 204, 146,
77 211, 141, 111, 49, 178, 236, 14, 80,
78 175, 241, 19, 77, 206, 144, 114, 44,
79 109, 51, 209, 143, 12, 82, 176, 238,
80 50, 108, 142, 208, 83, 13, 239, 177,
81 240, 174, 76, 18, 145, 207, 45, 115,
82 202, 148, 118, 40, 171, 245, 23, 73,
83 8, 86, 180, 234, 105, 55, 213, 139,
84 87, 9, 235, 181, 54, 104, 138, 212,
85 149, 203, 41, 119, 244, 170, 72, 22,
86 233, 183, 85, 11, 136, 214, 52, 106,
87 43, 117, 151, 201, 74, 20, 246, 168,
88 116, 42, 200, 150, 21, 75, 169, 247,
89 182, 232, 10, 84, 215, 137, 107, 53
93 write_gpio_data(int value
){
96 gpio_tristate(GPIO_EEDQ
);
100 gpio_clear(GPIO_EEDQ
);
104 static u8
make_new_crc( u8 Old_crc
, u8 New_value
){
105 /* Compute a new checksum with new byte, using previous checksum as input
106 See DS app note 17, understanding and using cyclic redundancy checks...
107 Also see DS2438, page 11 */
108 return( crc_lookup
[Old_crc
^ New_value
]);
111 int ee_crc_ok( u8
*Buffer
, int Len
, u8 Crc
){
112 /* Check if the checksum for this buffer is correct */
115 u8
*Curr_byte
= Buffer
;
118 Curr_crc
= make_new_crc( Curr_crc
, *Curr_byte
);
121 E_DEBUG("Calculated CRC = 0x%x, read = 0x%x\n", Curr_crc
, Crc
);
127 printk(KERN_ERR
"EE checksum error, Calculated CRC = 0x%x, read = 0x%x\n", Curr_crc
, Crc
);
133 /* Send idle and keep start time
134 Continous 1 is idle */
141 /* Release reset and verify that chip responds with presence pulse */
144 udelay(RESET_LOW_TIME
);
148 udelay(RESET_LOW_TIME
);
153 /* Wait for EEPROM to drive output */
154 udelay(PRESENCE_TIMEOUT
);
156 /* Ok, EEPROM is driving a 0 */
157 E_DEBUG("Presence detected\n");
159 E_DEBUG("Retries %d\n",Retries
);
161 /* Make sure chip releases pin */
162 udelay(PRESENCE_LOW_TIME
);
168 printk(KERN_ERR
"eeprom did not respond when releasing reset\n");
170 /* Make sure chip releases pin */
171 udelay(PRESENCE_LOW_TIME
);
173 /* Set to idle again */
181 /* Read a single byte from EEPROM
188 E_DEBUG("Reading byte\n");
191 /* Small delay between pulses */
200 /* Pull down pin short time to start read
201 See page 26 in data sheet */
207 /* Wait for chip to drive pin */
208 udelay(READ_TIMEOUT
);
216 restore_flags(Flags
);
219 /* Wait for chip to release pin */
220 udelay(TOTAL_READ_LOW
-READ_TIMEOUT
);
224 /* E_DEBUG("Read %d\n",Value); */
228 E_DEBUG("Read byte 0x%x\n",Result
);
234 write_cpu_byte(u8 Byte
){
235 /* Write a single byte to EEPROM
241 E_DEBUG("Writing byte 0x%x\n",Byte
);
244 /* Small delay between pulses */
254 /* Pull down pin short time for a 1, long time for a 0
255 See page 26 in data sheet */
271 restore_flags(Flags
);
275 /* Wait for chip to read the 1 */
276 udelay(TOTAL_WRITE_LOW
-WRITE_1_LOW
);
278 /* E_DEBUG("Wrote %d\n",Value); */
283 int ee_do_cpu_command( u8
*Tx
, int Tx_len
, u8
*Rx
, int Rx_len
, int Send_skip
){
284 /* Execute this command string, including
285 giving reset and setting to idle after command
286 if Rx_len is set, we read out data from EEPROM */
289 E_DEBUG("Command, Tx_len %d, Rx_len %d\n", Tx_len
, Rx_len
);
297 /* Always send SKIP_ROM first to tell chip we are sending a command,
298 except when we read out rom data for chip */
299 write_cpu_byte(SKIP_ROM
);
301 /* Always have Tx data */
302 for(i
=0;i
<Tx_len
;i
++){
303 write_cpu_byte(Tx
[i
]);
307 for(i
=0;i
<Rx_len
;i
++){
308 Rx
[i
]=read_cpu_byte();
314 E_DEBUG("Command done\n");
319 int ee_init_cpu_data(void){
323 /* Leave it floting since altera is driving the same pin */
326 /* Copy all User EEPROM data to scratchpad */
327 for(i
=0;i
<USER_PAGES
;i
++){
329 Tx
[1]=EE_USER_PAGE_0
+i
;
330 if(ee_do_cpu_command(Tx
,2,NULL
,0,TRUE
)) return(-EIO
);
333 /* Make sure chip doesnt store measurements in NVRAM */
334 Tx
[0]=WRITE_SCRATCHPAD
;
337 if(ee_do_cpu_command(Tx
,3,NULL
,0,TRUE
)) return(-EIO
);
339 Tx
[0]=COPY_SCRATCHPAD
;
340 if(ee_do_cpu_command(Tx
,2,NULL
,0,TRUE
)) return(-EIO
);