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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/types.h>
27 #include "pmcc4_sysdep.h"
28 #include "sbecom_inline_linux.h"
30 #include "sbe_promformat.h"
37 /*------------------------------------------------------------------------
38 * EEPROM address definitions
39 *------------------------------------------------------------------------
41 * The offset in the definitions below allows the test to skip over
42 * areas of the EEPROM that other programs (such a VxWorks) are
46 #define EE_MFG (long)0 /* Index to manufacturing record */
47 #define EE_FIRST 0x28 /* Index to start testing at */
48 #define EE_LIMIT 128 /* Index to end testing at */
51 /* Bit Ordering for Instructions
53 ** A0, A1, A2, A3, A4, A5, A6, OP0, OP1, SB (lsb, or 1st bit out)
57 #define EPROM_EWEN 0x0019 /* Erase/Write enable (reversed) */
58 #define EPROM_EWDS 0x0001 /* Erase/Write disable (reversed) */
59 #define EPROM_READ 0x0003 /* Read (reversed) */
60 #define EPROM_WRITE 0x0005 /* Write (reversed) */
61 #define EPROM_ERASE 0x0007 /* Erase (reversed) */
62 #define EPROM_ERAL 0x0009 /* Erase All (reversed) */
63 #define EPROM_WRAL 0x0011 /* Write All (reversed) */
65 #define EPROM_ADR_SZ 7 /* Number of bits in offset address */
66 #define EPROM_OP_SZ 3 /* Number of bits in command */
67 #define SIZE_ADDR_OP (EPROM_ADR_SZ + EPROM_OP_SZ)
68 #define LC46A_MAX_OPS 10 /* Number of bits in Instruction */
69 #define NUM_OF_BITS 8 /* Number of bits in data */
72 /* EEPROM signal bits */
73 #define EPROM_ACTIVE_OUT_BIT 0x0001 /* Out data bit */
74 #define EPROM_ACTIVE_IN_BIT 0x0002 /* In data bit */
75 #define ACTIVE_IN_BIT_SHIFT 0x0001 /* Shift In data bit to LSB */
76 #define EPROM_ENCS 0x0004 /* Set EEPROM CS during operation */
79 /*------------------------------------------------------------------------
80 * The ByteReverse table is used to reverses the 8 bits within a byte
81 *------------------------------------------------------------------------
84 static unsigned char ByteReverse[256];
85 static int ByteReverseBuilt = FALSE;
88 /*------------------------------------------------------------------------
89 * mfg_template - initial serial EEPROM data structure
90 *------------------------------------------------------------------------
93 short mfg_template[sizeof (FLD_TYPE2)] =
95 PROM_FORMAT_TYPE2, /* type; */
96 0x00, 0x1A, /* length[2]; */
97 0x00, 0x00, 0x00, 0x00, /* Crc32[4]; */
98 0x11, 0x76, /* Id[2]; */
99 0x07, 0x05, /* SubId[2] E1; */
100 0x00, 0xA0, 0xD6, 0x00, 0x00, 0x00, /* Serial[6]; */
101 0x00, 0x00, 0x00, 0x00, /* CreateTime[4]; */
102 0x00, 0x00, 0x00, 0x00, /* HeatRunTime[4]; */
103 0x00, 0x00, 0x00, 0x00, /* HeatRunIterations[4]; */
104 0x00, 0x00, 0x00, 0x00, /* HeatRunErrors[4]; */
108 /*------------------------------------------------------------------------
109 * BuildByteReverse - build the 8-bit reverse table
110 *------------------------------------------------------------------------
112 * The 'ByteReverse' table reverses the 8 bits within a byte
113 * (the MSB becomes the LSB etc.).
117 BuildByteReverse (void)
119 long half; /* Used to build by powers to 2 */
124 for (half = 1; half < sizeof (ByteReverse); half <<= 1)
125 for (i = 0; i < half; i++)
126 ByteReverse[half + i] = (char) (ByteReverse[i] | (0x80 / half));
128 ByteReverseBuilt = TRUE;
132 /*------------------------------------------------------------------------
133 * eeprom_delay - small delay for EEPROM timing
134 *------------------------------------------------------------------------
142 for (timeout = 20; timeout; --timeout)
149 /*------------------------------------------------------------------------
150 * eeprom_put_byte - Send a byte to the EEPROM serially
151 *------------------------------------------------------------------------
153 * Given the PCI address and the data, this routine serially sends
154 * the data to the EEPROM.
158 eeprom_put_byte (long addr, long data, int count)
164 output = (data & EPROM_ACTIVE_OUT_BIT) ? 1 : 0; /* Get next data bit */
165 output |= EPROM_ENCS; /* Add Chip Select */
169 pci_write_32 ((u_int32_t *) addr, output); /* Output it */
174 /*------------------------------------------------------------------------
175 * eeprom_get_byte - Receive a byte from the EEPROM serially
176 *------------------------------------------------------------------------
178 * Given the PCI address, this routine serially fetches the data
183 eeprom_get_byte (long addr)
189 /* Start the Reading of DATA
191 ** The first read is a dummy as the data is latched in the
192 ** EPLD and read on the next read access to the EEPROM.
195 input = pci_read_32 ((u_int32_t *) addr);
202 input = pci_read_32 ((u_int32_t *) addr);
204 data <<= 1; /* Shift data over */
205 data |= (input & EPROM_ACTIVE_IN_BIT) ? 1 : 0;
213 /*------------------------------------------------------------------------
214 * disable_pmc_eeprom - Disable writes to the EEPROM
215 *------------------------------------------------------------------------
217 * Issue the EEPROM command to disable writes.
221 disable_pmc_eeprom (long addr)
223 eeprom_put_byte (addr, EPROM_EWDS, SIZE_ADDR_OP);
225 pci_write_32 ((u_int32_t *) addr, 0); /* this removes Chip Select
230 /*------------------------------------------------------------------------
231 * enable_pmc_eeprom - Enable writes to the EEPROM
232 *------------------------------------------------------------------------
234 * Issue the EEPROM command to enable writes.
238 enable_pmc_eeprom (long addr)
240 eeprom_put_byte (addr, EPROM_EWEN, SIZE_ADDR_OP);
242 pci_write_32 ((u_int32_t *) addr, 0); /* this removes Chip Select
247 /*------------------------------------------------------------------------
248 * pmc_eeprom_read - EEPROM location read
249 *------------------------------------------------------------------------
251 * Given a EEPROM PCI address and location offset, this routine returns
252 * the contents of the specified location to the calling routine.
256 pmc_eeprom_read (long addr, long mem_offset)
258 u_int32_t data; /* Data from chip */
260 if (!ByteReverseBuilt)
263 mem_offset = ByteReverse[0x7F & mem_offset]; /* Reverse address */
265 * NOTE: The max offset address is 128 or half the reversal table. So the
266 * LSB is always zero and counts as a built in shift of one bit. So even
267 * though we need to shift 3 bits to make room for the command, we only
268 * need to shift twice more because of the built in shift.
270 mem_offset <<= 2; /* Shift for command */
271 mem_offset |= EPROM_READ; /* Add command */
273 eeprom_put_byte (addr, mem_offset, SIZE_ADDR_OP); /* Output chip address */
275 data = eeprom_get_byte (addr); /* Read chip data */
277 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select from
280 return (data & 0x000000FF);
284 /*------------------------------------------------------------------------
285 * pmc_eeprom_write - EEPROM location write
286 *------------------------------------------------------------------------
288 * Given a EEPROM PCI address, location offset and value, this
289 * routine writes the value to the specified location.
291 * Note: it is up to the caller to determine if the write
292 * operation succeeded.
296 pmc_eeprom_write (long addr, long mem_offset, u_int32_t data)
298 volatile u_int32_t temp;
301 if (!ByteReverseBuilt)
304 mem_offset = ByteReverse[0x7F & mem_offset]; /* Reverse address */
306 * NOTE: The max offset address is 128 or half the reversal table. So the
307 * LSB is always zero and counts as a built in shift of one bit. So even
308 * though we need to shift 3 bits to make room for the command, we only
309 * need to shift twice more because of the built in shift.
311 mem_offset <<= 2; /* Shift for command */
312 mem_offset |= EPROM_WRITE; /* Add command */
314 eeprom_put_byte (addr, mem_offset, SIZE_ADDR_OP); /* Output chip address */
316 data = ByteReverse[0xFF & data];/* Reverse data */
317 eeprom_put_byte (addr, data, NUM_OF_BITS); /* Output chip data */
319 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select from
323 ** Must see Data In at a low state before completing this transaction.
325 ** Afterwards, the data bit will return to a high state, ~6 ms, terminating
328 pci_write_32 ((u_int32_t *) addr, EPROM_ENCS); /* Re-enable Chip Select */
329 temp = pci_read_32 ((u_int32_t *) addr); /* discard first read */
330 temp = pci_read_32 ((u_int32_t *) addr);
331 if (temp & EPROM_ACTIVE_IN_BIT)
333 temp = pci_read_32 ((u_int32_t *) addr);
334 if (temp & EPROM_ACTIVE_IN_BIT)
336 pci_write_32 ((u_int32_t *) addr, 0); /* Remove Chip Select
344 for (temp = 0; temp < 0x10; temp++)
347 if (pci_read_32 ((u_int32_t *) addr) & EPROM_ACTIVE_IN_BIT)
358 /*------------------------------------------------------------------------
359 * pmcGetBuffValue - read the specified value from buffer
360 *------------------------------------------------------------------------
364 pmcGetBuffValue (char *ptr, int size)
369 for (index = 0; index < size; ++index)
372 value |= ptr[index] & 0xFF;
379 /*------------------------------------------------------------------------
380 * pmcSetBuffValue - save the specified value to buffer
381 *------------------------------------------------------------------------
385 pmcSetBuffValue (char *ptr, long value, int size)
391 ptr[index] = (char) (value & 0xFF);
397 /*------------------------------------------------------------------------
398 * pmc_eeprom_read_buffer - read EEPROM data into specified buffer
399 *------------------------------------------------------------------------
403 pmc_eeprom_read_buffer (long addr, long mem_offset, char *dest_ptr, int size)
406 *dest_ptr++ = (char) pmc_eeprom_read (addr, mem_offset++);
410 /*------------------------------------------------------------------------
411 * pmc_eeprom_write_buffer - write EEPROM data from specified buffer
412 *------------------------------------------------------------------------
416 pmc_eeprom_write_buffer (long addr, long mem_offset, char *dest_ptr, int size)
418 enable_pmc_eeprom (addr);
421 pmc_eeprom_write (addr, mem_offset++, *dest_ptr++);
423 disable_pmc_eeprom (addr);
427 /*------------------------------------------------------------------------
428 * pmcCalcCrc - calculate the CRC for the serial EEPROM structure
429 *------------------------------------------------------------------------
433 pmcCalcCrc_T01 (void *bufp)
435 FLD_TYPE2 *buf = bufp;
436 u_int32_t crc; /* CRC of the structure */
438 /* Calc CRC for type and length fields */
440 (u_int8_t *) &buf->type,
441 (u_int32_t) STRUCT_OFFSET (FLD_TYPE1, Crc32),
445 #ifdef EEPROM_TYPE_DEBUG
446 pr_info("sbeCrc: crc 1 calculated as %08x\n", crc); /* RLD DEBUG */
452 pmcCalcCrc_T02 (void *bufp)
454 FLD_TYPE2 *buf = bufp;
455 u_int32_t crc; /* CRC of the structure */
457 /* Calc CRC for type and length fields */
459 (u_int8_t *) &buf->type,
460 (u_int32_t) STRUCT_OFFSET (FLD_TYPE2, Crc32),
464 /* Calc CRC for remaining fields */
466 (u_int8_t *) &buf->Id[0],
467 (u_int32_t) (sizeof (FLD_TYPE2) - STRUCT_OFFSET (FLD_TYPE2, Id)),
471 #ifdef EEPROM_TYPE_DEBUG
472 pr_info("sbeCrc: crc 2 calculated as %08x\n", crc); /* RLD DEBUG */
478 /*------------------------------------------------------------------------
479 * pmc_init_seeprom - initialize the serial EEPROM structure
480 *------------------------------------------------------------------------
482 * At the front of the serial EEPROM there is a record that contains
483 * manufacturing information. If the info does not already exist, it
484 * is created. The only field modifiable by the operator is the
485 * serial number field.
489 pmc_init_seeprom (u_int32_t addr, u_int32_t serialNum)
491 PROMFORMAT buffer; /* Memory image of structure */
492 u_int32_t crc; /* CRC of structure */
496 createTime = get_seconds ();
498 /* use template data */
499 for (i = 0; i < sizeof (FLD_TYPE2); ++i)
500 buffer.bytes[i] = mfg_template[i];
502 /* Update serial number field in buffer */
503 pmcSetBuffValue (&buffer.fldType2.Serial[3], serialNum, 3);
505 /* Update create time field in buffer */
506 pmcSetBuffValue (&buffer.fldType2.CreateTime[0], createTime, 4);
508 /* Update CRC field in buffer */
509 crc = pmcCalcCrc_T02 (&buffer);
510 pmcSetBuffValue (&buffer.fldType2.Crc32[0], crc, 4);
513 for (i = 0; i < sizeof (FLD_TYPE2); ++i)
514 pr_info("[%02X] = %02X\n", i, buffer.bytes[i] & 0xFF);
517 /* Write structure to serial EEPROM */
518 pmc_eeprom_write_buffer (addr, EE_MFG, (char *) &buffer, sizeof (FLD_TYPE2));
523 pmc_verify_cksum (void *bufp)
525 FLD_TYPE1 *buf1 = bufp;
526 FLD_TYPE2 *buf2 = bufp;
527 u_int32_t crc1, crc2; /* CRC read from EEPROM */
529 /* Retrieve contents of CRC field */
530 crc1 = pmcGetBuffValue (&buf1->Crc32[0], sizeof (buf1->Crc32));
531 #ifdef EEPROM_TYPE_DEBUG
532 pr_info("EEPROM: chksum 1 reads as %08x\n", crc1); /* RLD DEBUG */
534 if ((buf1->type == PROM_FORMAT_TYPE1) &&
535 (pmcCalcCrc_T01 ((void *) buf1) == crc1))
536 return PROM_FORMAT_TYPE1; /* checksum type 1 verified */
538 crc2 = pmcGetBuffValue (&buf2->Crc32[0], sizeof (buf2->Crc32));
539 #ifdef EEPROM_TYPE_DEBUG
540 pr_info("EEPROM: chksum 2 reads as %08x\n", crc2); /* RLD DEBUG */
542 if ((buf2->type == PROM_FORMAT_TYPE2) &&
543 (pmcCalcCrc_T02 ((void *) buf2) == crc2))
544 return PROM_FORMAT_TYPE2; /* checksum type 2 verified */
546 return PROM_FORMAT_Unk; /* failed to validate */
550 /*** End-of-File ***/