162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Interface for the 93C66/56/46/26/06 serial eeprom parts.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 1995, 1996 Daniel M. Eischen
562306a36Sopenharmony_ci * All rights reserved.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
862306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
962306a36Sopenharmony_ci * are met:
1062306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1162306a36Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
1262306a36Sopenharmony_ci *    without modification.
1362306a36Sopenharmony_ci * 2. The name of the author may not be used to endorse or promote products
1462306a36Sopenharmony_ci *    derived from this software without specific prior written permission.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
1762306a36Sopenharmony_ci * GNU General Public License ("GPL").
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2062306a36Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2162306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2262306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2362306a36Sopenharmony_ci * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2462306a36Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2562306a36Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2662306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2762306a36Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2862306a36Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2962306a36Sopenharmony_ci * SUCH DAMAGE.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * $Id: //depot/aic7xxx/aic7xxx/aic7xxx_93cx6.c#19 $
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci *   The instruction set of the 93C66/56/46/26/06 chips are as follows:
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci *               Start  OP	    *
3862306a36Sopenharmony_ci *     Function   Bit  Code  Address**  Data     Description
3962306a36Sopenharmony_ci *     -------------------------------------------------------------------
4062306a36Sopenharmony_ci *     READ        1    10   A5 - A0             Reads data stored in memory,
4162306a36Sopenharmony_ci *                                               starting at specified address
4262306a36Sopenharmony_ci *     EWEN        1    00   11XXXX              Write enable must precede
4362306a36Sopenharmony_ci *                                               all programming modes
4462306a36Sopenharmony_ci *     ERASE       1    11   A5 - A0             Erase register A5A4A3A2A1A0
4562306a36Sopenharmony_ci *     WRITE       1    01   A5 - A0   D15 - D0  Writes register
4662306a36Sopenharmony_ci *     ERAL        1    00   10XXXX              Erase all registers
4762306a36Sopenharmony_ci *     WRAL        1    00   01XXXX    D15 - D0  Writes to all registers
4862306a36Sopenharmony_ci *     EWDS        1    00   00XXXX              Disables all programming
4962306a36Sopenharmony_ci *                                               instructions
5062306a36Sopenharmony_ci *     *Note: A value of X for address is a don't care condition.
5162306a36Sopenharmony_ci *    **Note: There are 8 address bits for the 93C56/66 chips unlike
5262306a36Sopenharmony_ci *	      the 93C46/26/06 chips which have 6 address bits.
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci *   The 93C46 has a four wire interface: clock, chip select, data in, and
5562306a36Sopenharmony_ci *   data out.  In order to perform one of the above functions, you need
5662306a36Sopenharmony_ci *   to enable the chip select for a clock period (typically a minimum of
5762306a36Sopenharmony_ci *   1 usec, with the clock high and low a minimum of 750 and 250 nsec
5862306a36Sopenharmony_ci *   respectively).  While the chip select remains high, you can clock in
5962306a36Sopenharmony_ci *   the instructions (above) starting with the start bit, followed by the
6062306a36Sopenharmony_ci *   OP code, Address, and Data (if needed).  For the READ instruction, the
6162306a36Sopenharmony_ci *   requested 16-bit register contents is read from the data out line but
6262306a36Sopenharmony_ci *   is preceded by an initial zero (leading 0, followed by 16-bits, MSB
6362306a36Sopenharmony_ci *   first).  The clock cycling from low to high initiates the next data
6462306a36Sopenharmony_ci *   bit to be sent from the chip.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#include "aic7xxx_osm.h"
6862306a36Sopenharmony_ci#include "aic7xxx_inline.h"
6962306a36Sopenharmony_ci#include "aic7xxx_93cx6.h"
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/*
7262306a36Sopenharmony_ci * Right now, we only have to read the SEEPROM.  But we make it easier to
7362306a36Sopenharmony_ci * add other 93Cx6 functions.
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_cistruct seeprom_cmd {
7662306a36Sopenharmony_ci	uint8_t len;
7762306a36Sopenharmony_ci	uint8_t bits[11];
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/* Short opcodes for the c46 */
8162306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
8262306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* Long opcodes for the C56/C66 */
8562306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
8662306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Common opcodes */
8962306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};
9062306a36Sopenharmony_cistatic const struct seeprom_cmd seeprom_read  = {3, {1, 1, 0}};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/*
9362306a36Sopenharmony_ci * Wait for the SEERDY to go high; about 800 ns.
9462306a36Sopenharmony_ci */
9562306a36Sopenharmony_ci#define CLOCK_PULSE(sd, rdy)				\
9662306a36Sopenharmony_ci	while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) {	\
9762306a36Sopenharmony_ci		;  /* Do nothing */			\
9862306a36Sopenharmony_ci	}						\
9962306a36Sopenharmony_ci	(void)SEEPROM_INB(sd);	/* Clear clock */
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/*
10262306a36Sopenharmony_ci * Send a START condition and the given command
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_cistatic void
10562306a36Sopenharmony_cisend_seeprom_cmd(struct seeprom_descriptor *sd, const struct seeprom_cmd *cmd)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	uint8_t temp;
10862306a36Sopenharmony_ci	int i = 0;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	/* Send chip select for one clock cycle. */
11162306a36Sopenharmony_ci	temp = sd->sd_MS ^ sd->sd_CS;
11262306a36Sopenharmony_ci	SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
11362306a36Sopenharmony_ci	CLOCK_PULSE(sd, sd->sd_RDY);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	for (i = 0; i < cmd->len; i++) {
11662306a36Sopenharmony_ci		if (cmd->bits[i] != 0)
11762306a36Sopenharmony_ci			temp ^= sd->sd_DO;
11862306a36Sopenharmony_ci		SEEPROM_OUTB(sd, temp);
11962306a36Sopenharmony_ci		CLOCK_PULSE(sd, sd->sd_RDY);
12062306a36Sopenharmony_ci		SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
12162306a36Sopenharmony_ci		CLOCK_PULSE(sd, sd->sd_RDY);
12262306a36Sopenharmony_ci		if (cmd->bits[i] != 0)
12362306a36Sopenharmony_ci			temp ^= sd->sd_DO;
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/*
12862306a36Sopenharmony_ci * Clear CS put the chip in the reset state, where it can wait for new commands.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_cistatic void
13162306a36Sopenharmony_cireset_seeprom(struct seeprom_descriptor *sd)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	uint8_t temp;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	temp = sd->sd_MS;
13662306a36Sopenharmony_ci	SEEPROM_OUTB(sd, temp);
13762306a36Sopenharmony_ci	CLOCK_PULSE(sd, sd->sd_RDY);
13862306a36Sopenharmony_ci	SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
13962306a36Sopenharmony_ci	CLOCK_PULSE(sd, sd->sd_RDY);
14062306a36Sopenharmony_ci	SEEPROM_OUTB(sd, temp);
14162306a36Sopenharmony_ci	CLOCK_PULSE(sd, sd->sd_RDY);
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Read the serial EEPROM and returns 1 if successful and 0 if
14662306a36Sopenharmony_ci * not successful.
14762306a36Sopenharmony_ci */
14862306a36Sopenharmony_ciint
14962306a36Sopenharmony_ciahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
15062306a36Sopenharmony_ci		 u_int start_addr, u_int count)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	int i = 0;
15362306a36Sopenharmony_ci	u_int k = 0;
15462306a36Sopenharmony_ci	uint16_t v;
15562306a36Sopenharmony_ci	uint8_t temp;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/*
15862306a36Sopenharmony_ci	 * Read the requested registers of the seeprom.  The loop
15962306a36Sopenharmony_ci	 * will range from 0 to count-1.
16062306a36Sopenharmony_ci	 */
16162306a36Sopenharmony_ci	for (k = start_addr; k < count + start_addr; k++) {
16262306a36Sopenharmony_ci		/*
16362306a36Sopenharmony_ci		 * Now we're ready to send the read command followed by the
16462306a36Sopenharmony_ci		 * address of the 16-bit register we want to read.
16562306a36Sopenharmony_ci		 */
16662306a36Sopenharmony_ci		send_seeprom_cmd(sd, &seeprom_read);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci		/* Send the 6 or 8 bit address (MSB first, LSB last). */
16962306a36Sopenharmony_ci		temp = sd->sd_MS ^ sd->sd_CS;
17062306a36Sopenharmony_ci		for (i = (sd->sd_chip - 1); i >= 0; i--) {
17162306a36Sopenharmony_ci			if ((k & (1 << i)) != 0)
17262306a36Sopenharmony_ci				temp ^= sd->sd_DO;
17362306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp);
17462306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
17562306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
17662306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
17762306a36Sopenharmony_ci			if ((k & (1 << i)) != 0)
17862306a36Sopenharmony_ci				temp ^= sd->sd_DO;
17962306a36Sopenharmony_ci		}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci		/*
18262306a36Sopenharmony_ci		 * Now read the 16 bit register.  An initial 0 precedes the
18362306a36Sopenharmony_ci		 * register contents which begins with bit 15 (MSB) and ends
18462306a36Sopenharmony_ci		 * with bit 0 (LSB).  The initial 0 will be shifted off the
18562306a36Sopenharmony_ci		 * top of our word as we let the loop run from 0 to 16.
18662306a36Sopenharmony_ci		 */
18762306a36Sopenharmony_ci		v = 0;
18862306a36Sopenharmony_ci		for (i = 16; i >= 0; i--) {
18962306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp);
19062306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
19162306a36Sopenharmony_ci			v <<= 1;
19262306a36Sopenharmony_ci			if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
19362306a36Sopenharmony_ci				v |= 1;
19462306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
19562306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
19662306a36Sopenharmony_ci		}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci		buf[k - start_addr] = v;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci		/* Reset the chip select for the next command cycle. */
20162306a36Sopenharmony_ci		reset_seeprom(sd);
20262306a36Sopenharmony_ci	}
20362306a36Sopenharmony_ci#ifdef AHC_DUMP_EEPROM
20462306a36Sopenharmony_ci	printk("\nSerial EEPROM:\n\t");
20562306a36Sopenharmony_ci	for (k = 0; k < count; k = k + 1) {
20662306a36Sopenharmony_ci		if (((k % 8) == 0) && (k != 0)) {
20762306a36Sopenharmony_ci			printk(KERN_CONT "\n\t");
20862306a36Sopenharmony_ci		}
20962306a36Sopenharmony_ci		printk(KERN_CONT " 0x%x", buf[k]);
21062306a36Sopenharmony_ci	}
21162306a36Sopenharmony_ci	printk(KERN_CONT "\n");
21262306a36Sopenharmony_ci#endif
21362306a36Sopenharmony_ci	return (1);
21462306a36Sopenharmony_ci}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci/*
21762306a36Sopenharmony_ci * Write the serial EEPROM and return 1 if successful and 0 if
21862306a36Sopenharmony_ci * not successful.
21962306a36Sopenharmony_ci */
22062306a36Sopenharmony_ciint
22162306a36Sopenharmony_ciahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
22262306a36Sopenharmony_ci		  u_int start_addr, u_int count)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	const struct seeprom_cmd *ewen, *ewds;
22562306a36Sopenharmony_ci	uint16_t v;
22662306a36Sopenharmony_ci	uint8_t temp;
22762306a36Sopenharmony_ci	int i, k;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	/* Place the chip into write-enable mode */
23062306a36Sopenharmony_ci	if (sd->sd_chip == C46) {
23162306a36Sopenharmony_ci		ewen = &seeprom_ewen;
23262306a36Sopenharmony_ci		ewds = &seeprom_ewds;
23362306a36Sopenharmony_ci	} else if (sd->sd_chip == C56_66) {
23462306a36Sopenharmony_ci		ewen = &seeprom_long_ewen;
23562306a36Sopenharmony_ci		ewds = &seeprom_long_ewds;
23662306a36Sopenharmony_ci	} else {
23762306a36Sopenharmony_ci		printk("ahc_write_seeprom: unsupported seeprom type %d\n",
23862306a36Sopenharmony_ci		       sd->sd_chip);
23962306a36Sopenharmony_ci		return (0);
24062306a36Sopenharmony_ci	}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	send_seeprom_cmd(sd, ewen);
24362306a36Sopenharmony_ci	reset_seeprom(sd);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* Write all requested data out to the seeprom. */
24662306a36Sopenharmony_ci	temp = sd->sd_MS ^ sd->sd_CS;
24762306a36Sopenharmony_ci	for (k = start_addr; k < count + start_addr; k++) {
24862306a36Sopenharmony_ci		/* Send the write command */
24962306a36Sopenharmony_ci		send_seeprom_cmd(sd, &seeprom_write);
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci		/* Send the 6 or 8 bit address (MSB first). */
25262306a36Sopenharmony_ci		for (i = (sd->sd_chip - 1); i >= 0; i--) {
25362306a36Sopenharmony_ci			if ((k & (1 << i)) != 0)
25462306a36Sopenharmony_ci				temp ^= sd->sd_DO;
25562306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp);
25662306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
25762306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
25862306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
25962306a36Sopenharmony_ci			if ((k & (1 << i)) != 0)
26062306a36Sopenharmony_ci				temp ^= sd->sd_DO;
26162306a36Sopenharmony_ci		}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci		/* Write the 16 bit value, MSB first */
26462306a36Sopenharmony_ci		v = buf[k - start_addr];
26562306a36Sopenharmony_ci		for (i = 15; i >= 0; i--) {
26662306a36Sopenharmony_ci			if ((v & (1 << i)) != 0)
26762306a36Sopenharmony_ci				temp ^= sd->sd_DO;
26862306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp);
26962306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
27062306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
27162306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
27262306a36Sopenharmony_ci			if ((v & (1 << i)) != 0)
27362306a36Sopenharmony_ci				temp ^= sd->sd_DO;
27462306a36Sopenharmony_ci		}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci		/* Wait for the chip to complete the write */
27762306a36Sopenharmony_ci		temp = sd->sd_MS;
27862306a36Sopenharmony_ci		SEEPROM_OUTB(sd, temp);
27962306a36Sopenharmony_ci		CLOCK_PULSE(sd, sd->sd_RDY);
28062306a36Sopenharmony_ci		temp = sd->sd_MS ^ sd->sd_CS;
28162306a36Sopenharmony_ci		do {
28262306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp);
28362306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
28462306a36Sopenharmony_ci			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
28562306a36Sopenharmony_ci			CLOCK_PULSE(sd, sd->sd_RDY);
28662306a36Sopenharmony_ci		} while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci		reset_seeprom(sd);
28962306a36Sopenharmony_ci	}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	/* Put the chip back into write-protect mode */
29262306a36Sopenharmony_ci	send_seeprom_cmd(sd, ewds);
29362306a36Sopenharmony_ci	reset_seeprom(sd);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	return (1);
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ciint
29962306a36Sopenharmony_ciahc_verify_cksum(struct seeprom_config *sc)
30062306a36Sopenharmony_ci{
30162306a36Sopenharmony_ci	int i;
30262306a36Sopenharmony_ci	int maxaddr;
30362306a36Sopenharmony_ci	uint32_t checksum;
30462306a36Sopenharmony_ci	uint16_t *scarray;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	maxaddr = (sizeof(*sc)/2) - 1;
30762306a36Sopenharmony_ci	checksum = 0;
30862306a36Sopenharmony_ci	scarray = (uint16_t *)sc;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	for (i = 0; i < maxaddr; i++)
31162306a36Sopenharmony_ci		checksum = checksum + scarray[i];
31262306a36Sopenharmony_ci	if (checksum == 0
31362306a36Sopenharmony_ci	 || (checksum & 0xFFFF) != sc->checksum) {
31462306a36Sopenharmony_ci		return (0);
31562306a36Sopenharmony_ci	} else {
31662306a36Sopenharmony_ci		return(1);
31762306a36Sopenharmony_ci	}
31862306a36Sopenharmony_ci}
319