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 */
16 #include <../board/gth/ee_dev.h>
18 /* We dont have kernel functions */
34 * lookup table ripped from DS app note 17, understanding and using
35 * cyclic redundancy checks...
38 static u8 crc_lookup[256] = {
39 0, 94, 188, 226, 97, 63, 221, 131,
40 194, 156, 126, 32, 163, 253, 31, 65,
41 157, 195, 33, 127, 252, 162, 64, 30,
42 95, 1, 227, 189, 62, 96, 130, 220,
43 35, 125, 159, 193, 66, 28, 254, 160,
44 225, 191, 93, 3, 128, 222, 60, 98,
45 190, 224, 2, 92, 223, 129, 99, 61,
46 124, 34, 192, 158, 29, 67, 161, 255,
47 70, 24, 250, 164, 39, 121, 155, 197,
48 132, 218, 56, 102, 229, 187, 89, 7,
49 219, 133, 103, 57, 186, 228, 6, 88,
50 25, 71, 165, 251, 120, 38, 196, 154,
51 101, 59, 217, 135, 4, 90, 184, 230,
52 167, 249, 27, 69, 198, 152, 122, 36,
53 248, 166, 68, 26, 153, 199, 37, 123,
54 58, 100, 134, 216, 91, 5, 231, 185,
55 140, 210, 48, 110, 237, 179, 81, 15,
56 78, 16, 242, 172, 47, 113, 147, 205,
57 17, 79, 173, 243, 112, 46, 204, 146,
58 211, 141, 111, 49, 178, 236, 14, 80,
59 175, 241, 19, 77, 206, 144, 114, 44,
60 109, 51, 209, 143, 12, 82, 176, 238,
61 50, 108, 142, 208, 83, 13, 239, 177,
62 240, 174, 76, 18, 145, 207, 45, 115,
63 202, 148, 118, 40, 171, 245, 23, 73,
64 8, 86, 180, 234, 105, 55, 213, 139,
65 87, 9, 235, 181, 54, 104, 138, 212,
66 149, 203, 41, 119, 244, 170, 72, 22,
67 233, 183, 85, 11, 136, 214, 52, 106,
68 43, 117, 151, 201, 74, 20, 246, 168,
69 116, 42, 200, 150, 21, 75, 169, 247,
70 182, 232, 10, 84, 215, 137, 107, 53
73 static u8 make_new_crc( u8 Old_crc, u8 New_value ){
74 /* Compute a new checksum with new byte, using previous checksum as input
75 See DS app note 17, understanding and using cyclic redundancy checks...
76 Also see DS2438, page 11 */
77 return( crc_lookup[Old_crc ^ New_value ]);
80 int ee_crc_ok( u8 *Buffer, int Len, u8 Crc ){
81 /* Check if the checksum for this buffer is correct */
84 u8 *Curr_byte = Buffer;
87 Curr_crc = make_new_crc( Curr_crc, *Curr_byte);
90 E_DEBUG("Calculated CRC = 0x%x, read = 0x%x\n", Curr_crc, Crc);
96 printk(KERN_ERR"EE checksum error, Calculated CRC = 0x%x, read = 0x%x\n",
103 /* Send idle and keep start time
104 Continous 1 is idle */
110 /* Release reset and verify that chip responds with presence pulse */
113 udelay(RESET_LOW_TIME);
117 udelay(RESET_LOW_TIME);
122 /* Wait for EEPROM to drive output */
123 udelay(PRESENCE_TIMEOUT);
125 /* Ok, EEPROM is driving a 0 */
126 E_DEBUG("Presence detected\n");
128 E_DEBUG("Retries %d\n",Retries);
130 /* Make sure chip releases pin */
131 udelay(PRESENCE_LOW_TIME);
137 printk(KERN_ERR"EEPROM did not respond when releasing reset\n");
139 /* Make sure chip releases pin */
140 udelay(PRESENCE_LOW_TIME);
142 /* Set to idle again */
150 /* Read a single byte from EEPROM
159 E_DEBUG("Reading byte\n");
162 /* Small delay between pulses */
171 /* Pull down pin short time to start read
172 See page 26 in data sheet */
178 /* Wait for chip to drive pin */
179 udelay(READ_TIMEOUT);
187 restore_flags(Flags);
190 /* Wait for chip to release pin */
191 udelay(TOTAL_READ_LOW-READ_TIMEOUT);
197 E_DEBUG("Read byte 0x%x\n",Result);
204 /* Write a single byte to EEPROM
212 E_DEBUG("Writing byte 0x%x\n",Byte);
215 /* Small delay between pulses */
225 /* Pull down pin short time for a 1, long time for a 0
226 See page 26 in data sheet */
242 restore_flags(Flags);
246 /* Wait for chip to read the 1 */
247 udelay(TOTAL_WRITE_LOW-WRITE_1_LOW);
252 int ee_do_command( u8 *Tx, int Tx_len, u8 *Rx, int Rx_len, int Send_skip ){
253 /* Execute this command string, including
254 giving reset and setting to idle after command
255 if Rx_len is set, we read out data from EEPROM */
258 E_DEBUG("Command, Tx_len %d, Rx_len %d\n", Tx_len, Rx_len );
266 /* Always send SKIP_ROM first to tell chip we are sending a command,
267 except when we read out rom data for chip */
268 write_byte(SKIP_ROM);
270 /* Always have Tx data */
271 for(i=0;i<Tx_len;i++){
276 for(i=0;i<Rx_len;i++){
283 E_DEBUG("Command done\n");
288 int ee_init_data(void){
292 volatile immap_t *immap = (immap_t *)CFG_IMMR;
297 immap->im_ioport.iop_padat &= ~PA_FRONT_LED;
299 immap->im_ioport.iop_padat |= PA_FRONT_LED;
303 /* Set port to open drain to be able to read data from
304 port without setting it to input */
305 PORT_B_PAR &= ~PB_EEPROM;
306 PORT_B_ODR |= PB_EEPROM;
307 SET_PORT_B_OUTPUT(PB_EEPROM);
312 /* Copy all User EEPROM data to scratchpad */
313 for(i=0;i<USER_PAGES;i++){
315 Tx[1]=EE_USER_PAGE_0+i;
316 if(ee_do_command(Tx,2,NULL,0,TRUE)) return(-EIO);
319 /* Make sure chip doesnt store measurements in NVRAM */
320 Tx[0]=WRITE_SCRATCHPAD;
323 if(ee_do_command(Tx,3,NULL,0,TRUE)) return(-EIO);
325 Tx[0]=COPY_SCRATCHPAD;
326 if(ee_do_command(Tx,2,NULL,0,TRUE)) return(-EIO);
328 /* FIXME check status bit instead
329 Could take 10 ms to store in EEPROM */