1 /* pmc93x6_eeprom.c - PMC's 93LC46 EEPROM Device
3 * The 93LC46 is a low-power, serial Electrically Erasable and
4 * Programmable Read Only Memory organized as 128 8-bit bytes.
6 * Accesses to the 93LC46 are done in a bit serial stream, organized
7 * in a 3 wire format. Writes are internally timed by the device
8 * (the In data bit is pulled low until the write is complete and
9 * then is pulled high) and take about 6 milliseconds.
11 * Copyright (C) 2003-2005 SBE, Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/types.h>
25 #include "pmcc4_sysdep.h"
26 #include "sbecom_inline_linux.h"
28 #include "sbe_promformat.h"
35 #ifdef SBE_INCLUDE_SYMBOLS
42 /*------------------------------------------------------------------------
43 * EEPROM address definitions
44 *------------------------------------------------------------------------
46 * The offset in the definitions below allows the test to skip over
47 * areas of the EEPROM that other programs (such a VxWorks) are
51 #define EE_MFG (long)0 /* Index to manufacturing record */
52 #define EE_FIRST 0x28 /* Index to start testing at */
53 #define EE_LIMIT 128 /* Index to end testing at */
56 /* Bit Ordering for Instructions
58 ** A0, A1, A2, A3, A4, A5, A6, OP0, OP1, SB (lsb, or 1st bit out)
62 #define EPROM_EWEN 0x0019 /* Erase/Write enable (reversed) */
63 #define EPROM_EWDS 0x0001 /* Erase/Write disable (reversed) */
64 #define EPROM_READ 0x0003 /* Read (reversed) */
65 #define EPROM_WRITE 0x0005 /* Write (reversed) */
66 #define EPROM_ERASE 0x0007 /* Erase (reversed) */
67 #define EPROM_ERAL 0x0009 /* Erase All (reversed) */
68 #define EPROM_WRAL 0x0011 /* Write All (reversed) */
70 #define EPROM_ADR_SZ 7 /* Number of bits in offset address */
71 #define EPROM_OP_SZ 3 /* Number of bits in command */
72 #define SIZE_ADDR_OP (EPROM_ADR_SZ + EPROM_OP_SZ)
73 #define LC46A_MAX_OPS 10 /* Number of bits in Instruction */
74 #define NUM_OF_BITS 8 /* Number of bits in data */
77 /* EEPROM signal bits */
78 #define EPROM_ACTIVE_OUT_BIT 0x0001 /* Out data bit */
79 #define EPROM_ACTIVE_IN_BIT 0x0002 /* In data bit */
80 #define ACTIVE_IN_BIT_SHIFT 0x0001 /* Shift In data bit to LSB */
81 #define EPROM_ENCS 0x0004 /* Set EEPROM CS during operation */
84 /*------------------------------------------------------------------------
85 * The ByteReverse table is used to reverses the 8 bits within a byte
86 *------------------------------------------------------------------------
89 static unsigned char ByteReverse[256];
90 static int ByteReverseBuilt = FALSE;
93 /*------------------------------------------------------------------------
94 * mfg_template - initial serial EEPROM data structure
95 *------------------------------------------------------------------------
98 short mfg_template[sizeof (FLD_TYPE2)] =
100 PROM_FORMAT_TYPE2, /* type; */
101 0x00, 0x1A, /* length[2]; */
102 0x00, 0x00, 0x00, 0x00, /* Crc32[4]; */
103 0x11, 0x76, /* Id[2]; */
104 0x07, 0x05, /* SubId[2] E1; */
105 0x00, 0xA0, 0xD6, 0x00, 0x00, 0x00, /* Serial[6]; */
106 0x00, 0x00, 0x00, 0x00, /* CreateTime[4]; */
107 0x00, 0x00, 0x00, 0x00, /* HeatRunTime[4]; */
108 0x00, 0x00, 0x00, 0x00, /* HeatRunIterations[4]; */
109 0x00, 0x00, 0x00, 0x00, /* HeatRunErrors[4]; */
113 /*------------------------------------------------------------------------
114 * BuildByteReverse - build the 8-bit reverse table
115 *------------------------------------------------------------------------
117 * The 'ByteReverse' table reverses the 8 bits within a byte
118 * (the MSB becomes the LSB etc.).
122 BuildByteReverse (void)
124 long half; /* Used to build by powers to 2 */
129 for (half = 1; half < sizeof (ByteReverse); half <<= 1)
130 for (i = 0; i < half; i++)
131 ByteReverse[half + i] = (char) (ByteReverse[i] | (0x80 / half));
133 ByteReverseBuilt = TRUE;
137 /*------------------------------------------------------------------------
138 * eeprom_delay - small delay for EEPROM timing
139 *------------------------------------------------------------------------
147 for (timeout = 20; timeout; --timeout)
154 /*------------------------------------------------------------------------
155 * eeprom_put_byte - Send a byte to the EEPROM serially
156 *------------------------------------------------------------------------
158 * Given the PCI address and the data, this routine serially sends
159 * the data to the EEPROM.
163 eeprom_put_byte (long addr, long data, int count)
169 output = (data & EPROM_ACTIVE_OUT_BIT) ? 1 : 0; /* Get next data bit */
170 output |= EPROM_ENCS; /* Add Chip Select */
174 pci_write_32 ((u_int32_t *) addr, output); /* Output it */
179 /*------------------------------------------------------------------------
180 * eeprom_get_byte - Receive a byte from the EEPROM serially
181 *------------------------------------------------------------------------
183 * Given the PCI address, this routine serially fetches the data
188 eeprom_get_byte (long addr)
194 /* Start the Reading of DATA
196 ** The first read is a dummy as the data is latched in the
197 ** EPLD and read on the next read access to the EEPROM.
200 input = pci_read_32 ((u_int32_t *) addr);
207 input = pci_read_32 ((u_int32_t *) addr);
209 data <<= 1; /* Shift data over */
210 data |= (input & EPROM_ACTIVE_IN_BIT) ? 1 : 0;
218 /*------------------------------------------------------------------------
219 * disable_pmc_eeprom - Disable writes to the EEPROM
220 *------------------------------------------------------------------------
222 * Issue the EEPROM command to disable writes.
226 disable_pmc_eeprom (long addr)
228 eeprom_put_byte (addr, EPROM_EWDS, SIZE_ADDR_OP);
230 pci_write_32 ((u_int32_t *) addr, 0); /* this removes Chip Select
235 /*------------------------------------------------------------------------
236 * enable_pmc_eeprom - Enable writes to the EEPROM
237 *------------------------------------------------------------------------
239 * Issue the EEPROM command to enable writes.
243 enable_pmc_eeprom (long addr)
245 eeprom_put_byte (addr, EPROM_EWEN, SIZE_ADDR_OP);
247 pci_write_32 ((u_int32_t *) addr, 0); /* this removes Chip Select
252 /*------------------------------------------------------------------------
253 * pmc_eeprom_read - EEPROM location read
254 *------------------------------------------------------------------------
256 * Given a EEPROM PCI address and location offset, this routine returns
257 * the contents of the specified location to the calling routine.
261 pmc_eeprom_read (long addr, long mem_offset)
263 u_int32_t data; /* Data from chip */
265 if (!ByteReverseBuilt)
268 mem_offset = ByteReverse[0x7F & mem_offset]; /* Reverse address */
270 * NOTE: The max offset address is 128 or half the reversal table. So the
271 * LSB is always zero and counts as a built in shift of one bit. So even
272 * though we need to shift 3 bits to make room for the command, we only
273 * need to shift twice more because of the built in shift.
275 mem_offset <<= 2; /* Shift for command */
276 mem_offset |= EPROM_READ; /* Add command */
278 eeprom_put_byte (addr, mem_offset, SIZE_ADDR_OP); /* Output chip address */
280 data = eeprom_get_byte (addr); /* Read chip data */
282 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select from
285 return (data & 0x000000FF);
289 /*------------------------------------------------------------------------
290 * pmc_eeprom_write - EEPROM location write
291 *------------------------------------------------------------------------
293 * Given a EEPROM PCI address, location offset and value, this
294 * routine writes the value to the specified location.
296 * Note: it is up to the caller to determine if the write
297 * operation succeeded.
301 pmc_eeprom_write (long addr, long mem_offset, u_int32_t data)
303 volatile u_int32_t temp;
306 if (!ByteReverseBuilt)
309 mem_offset = ByteReverse[0x7F & mem_offset]; /* Reverse address */
311 * NOTE: The max offset address is 128 or half the reversal table. So the
312 * LSB is always zero and counts as a built in shift of one bit. So even
313 * though we need to shift 3 bits to make room for the command, we only
314 * need to shift twice more because of the built in shift.
316 mem_offset <<= 2; /* Shift for command */
317 mem_offset |= EPROM_WRITE; /* Add command */
319 eeprom_put_byte (addr, mem_offset, SIZE_ADDR_OP); /* Output chip address */
321 data = ByteReverse[0xFF & data];/* Reverse data */
322 eeprom_put_byte (addr, data, NUM_OF_BITS); /* Output chip data */
324 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select from
328 ** Must see Data In at a low state before completing this transaction.
330 ** Afterwards, the data bit will return to a high state, ~6 ms, terminating
333 pci_write_32 ((u_int32_t *) addr, EPROM_ENCS); /* Re-enable Chip Select */
334 temp = pci_read_32 ((u_int32_t *) addr); /* discard first read */
335 temp = pci_read_32 ((u_int32_t *) addr);
336 if (temp & EPROM_ACTIVE_IN_BIT)
338 temp = pci_read_32 ((u_int32_t *) addr);
339 if (temp & EPROM_ACTIVE_IN_BIT)
341 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select
349 for (temp = 0; temp < 0x10; temp++)
352 if (pci_read_32 ((u_int32_t *) addr) & EPROM_ACTIVE_IN_BIT)
363 /*------------------------------------------------------------------------
364 * pmcGetBuffValue - read the specified value from buffer
365 *------------------------------------------------------------------------
369 pmcGetBuffValue (char *ptr, int size)
374 for (index = 0; index < size; ++index)
377 value |= ptr[index] & 0xFF;
384 /*------------------------------------------------------------------------
385 * pmcSetBuffValue - save the specified value to buffer
386 *------------------------------------------------------------------------
390 pmcSetBuffValue (char *ptr, long value, int size)
396 ptr[index] = (char) (value & 0xFF);
402 /*------------------------------------------------------------------------
403 * pmc_eeprom_read_buffer - read EEPROM data into specified buffer
404 *------------------------------------------------------------------------
408 pmc_eeprom_read_buffer (long addr, long mem_offset, char *dest_ptr, int size)
411 *dest_ptr++ = (char) pmc_eeprom_read (addr, mem_offset++);
415 /*------------------------------------------------------------------------
416 * pmc_eeprom_write_buffer - write EEPROM data from specified buffer
417 *------------------------------------------------------------------------
421 pmc_eeprom_write_buffer (long addr, long mem_offset, char *dest_ptr, int size)
423 enable_pmc_eeprom (addr);
426 pmc_eeprom_write (addr, mem_offset++, *dest_ptr++);
428 disable_pmc_eeprom (addr);
432 /*------------------------------------------------------------------------
433 * pmcCalcCrc - calculate the CRC for the serial EEPROM structure
434 *------------------------------------------------------------------------
438 pmcCalcCrc_T01 (void *bufp)
440 FLD_TYPE2 *buf = bufp;
441 u_int32_t crc; /* CRC of the structure */
443 /* Calc CRC for type and length fields */
445 (u_int8_t *) &buf->type,
446 (u_int32_t) STRUCT_OFFSET (FLD_TYPE1, Crc32),
450 #ifdef EEPROM_TYPE_DEBUG
451 printk ("sbeCrc: crc 1 calculated as %08x\n", crc); /* RLD DEBUG */
457 pmcCalcCrc_T02 (void *bufp)
459 FLD_TYPE2 *buf = bufp;
460 u_int32_t crc; /* CRC of the structure */
462 /* Calc CRC for type and length fields */
464 (u_int8_t *) &buf->type,
465 (u_int32_t) STRUCT_OFFSET (FLD_TYPE2, Crc32),
469 /* Calc CRC for remaining fields */
471 (u_int8_t *) &buf->Id[0],
472 (u_int32_t) (sizeof (FLD_TYPE2) - STRUCT_OFFSET (FLD_TYPE2, Id)),
476 #ifdef EEPROM_TYPE_DEBUG
477 printk ("sbeCrc: crc 2 calculated as %08x\n", crc); /* RLD DEBUG */
483 /*------------------------------------------------------------------------
484 * pmc_init_seeprom - initialize the serial EEPROM structure
485 *------------------------------------------------------------------------
487 * At the front of the serial EEPROM there is a record that contains
488 * manufacturing information. If the info does not already exist, it
489 * is created. The only field modifiable by the operator is the
490 * serial number field.
494 pmc_init_seeprom (u_int32_t addr, u_int32_t serialNum)
496 PROMFORMAT buffer; /* Memory image of structure */
497 u_int32_t crc; /* CRC of structure */
501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
502 createTime = CURRENT_TIME;
504 createTime = get_seconds ();
507 /* use template data */
508 for (i = 0; i < sizeof (FLD_TYPE2); ++i)
509 buffer.bytes[i] = mfg_template[i];
511 /* Update serial number field in buffer */
512 pmcSetBuffValue (&buffer.fldType2.Serial[3], serialNum, 3);
514 /* Update create time field in buffer */
515 pmcSetBuffValue (&buffer.fldType2.CreateTime[0], createTime, 4);
517 /* Update CRC field in buffer */
518 crc = pmcCalcCrc_T02 (&buffer);
519 pmcSetBuffValue (&buffer.fldType2.Crc32[0], crc, 4);
522 for (i = 0; i < sizeof (FLD_TYPE2); ++i)
523 printk ("[%02X] = %02X\n", i, buffer.bytes[i] & 0xFF);
526 /* Write structure to serial EEPROM */
527 pmc_eeprom_write_buffer (addr, EE_MFG, (char *) &buffer, sizeof (FLD_TYPE2));
532 pmc_verify_cksum (void *bufp)
534 FLD_TYPE1 *buf1 = bufp;
535 FLD_TYPE2 *buf2 = bufp;
536 u_int32_t crc1, crc2; /* CRC read from EEPROM */
538 /* Retrieve contents of CRC field */
539 crc1 = pmcGetBuffValue (&buf1->Crc32[0], sizeof (buf1->Crc32));
540 #ifdef EEPROM_TYPE_DEBUG
541 printk ("EEPROM: chksum 1 reads as %08x\n", crc1); /* RLD DEBUG */
543 if ((buf1->type == PROM_FORMAT_TYPE1) &&
544 (pmcCalcCrc_T01 ((void *) buf1) == crc1))
545 return PROM_FORMAT_TYPE1; /* checksum type 1 verified */
547 crc2 = pmcGetBuffValue (&buf2->Crc32[0], sizeof (buf2->Crc32));
548 #ifdef EEPROM_TYPE_DEBUG
549 printk ("EEPROM: chksum 2 reads as %08x\n", crc2); /* RLD DEBUG */
551 if ((buf2->type == PROM_FORMAT_TYPE2) &&
552 (pmcCalcCrc_T02 ((void *) buf2) == crc2))
553 return PROM_FORMAT_TYPE2; /* checksum type 2 verified */
555 return PROM_FORMAT_Unk; /* failed to validate */
559 /*** End-of-File ***/