xref: /OK3568_Linux_fs/kernel/drivers/scsi/aic7xxx/aic7xxx_93cx6.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Interface for the 93C66/56/46/26/06 serial eeprom parts.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 1995, 1996 Daniel M. Eischen
5*4882a593Smuzhiyun  * All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
9*4882a593Smuzhiyun  * are met:
10*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
11*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
12*4882a593Smuzhiyun  *    without modification.
13*4882a593Smuzhiyun  * 2. The name of the author may not be used to endorse or promote products
14*4882a593Smuzhiyun  *    derived from this software without specific prior written permission.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Alternatively, this software may be distributed under the terms of the
17*4882a593Smuzhiyun  * GNU General Public License ("GPL").
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*4882a593Smuzhiyun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23*4882a593Smuzhiyun  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*4882a593Smuzhiyun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*4882a593Smuzhiyun  * SUCH DAMAGE.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * $Id: //depot/aic7xxx/aic7xxx/aic7xxx_93cx6.c#19 $
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  *   The instruction set of the 93C66/56/46/26/06 chips are as follows:
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  *               Start  OP	    *
38*4882a593Smuzhiyun  *     Function   Bit  Code  Address**  Data     Description
39*4882a593Smuzhiyun  *     -------------------------------------------------------------------
40*4882a593Smuzhiyun  *     READ        1    10   A5 - A0             Reads data stored in memory,
41*4882a593Smuzhiyun  *                                               starting at specified address
42*4882a593Smuzhiyun  *     EWEN        1    00   11XXXX              Write enable must precede
43*4882a593Smuzhiyun  *                                               all programming modes
44*4882a593Smuzhiyun  *     ERASE       1    11   A5 - A0             Erase register A5A4A3A2A1A0
45*4882a593Smuzhiyun  *     WRITE       1    01   A5 - A0   D15 - D0  Writes register
46*4882a593Smuzhiyun  *     ERAL        1    00   10XXXX              Erase all registers
47*4882a593Smuzhiyun  *     WRAL        1    00   01XXXX    D15 - D0  Writes to all registers
48*4882a593Smuzhiyun  *     EWDS        1    00   00XXXX              Disables all programming
49*4882a593Smuzhiyun  *                                               instructions
50*4882a593Smuzhiyun  *     *Note: A value of X for address is a don't care condition.
51*4882a593Smuzhiyun  *    **Note: There are 8 address bits for the 93C56/66 chips unlike
52*4882a593Smuzhiyun  *	      the 93C46/26/06 chips which have 6 address bits.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  *   The 93C46 has a four wire interface: clock, chip select, data in, and
55*4882a593Smuzhiyun  *   data out.  In order to perform one of the above functions, you need
56*4882a593Smuzhiyun  *   to enable the chip select for a clock period (typically a minimum of
57*4882a593Smuzhiyun  *   1 usec, with the clock high and low a minimum of 750 and 250 nsec
58*4882a593Smuzhiyun  *   respectively).  While the chip select remains high, you can clock in
59*4882a593Smuzhiyun  *   the instructions (above) starting with the start bit, followed by the
60*4882a593Smuzhiyun  *   OP code, Address, and Data (if needed).  For the READ instruction, the
61*4882a593Smuzhiyun  *   requested 16-bit register contents is read from the data out line but
62*4882a593Smuzhiyun  *   is preceded by an initial zero (leading 0, followed by 16-bits, MSB
63*4882a593Smuzhiyun  *   first).  The clock cycling from low to high initiates the next data
64*4882a593Smuzhiyun  *   bit to be sent from the chip.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #include "aic7xxx_osm.h"
68*4882a593Smuzhiyun #include "aic7xxx_inline.h"
69*4882a593Smuzhiyun #include "aic7xxx_93cx6.h"
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * Right now, we only have to read the SEEPROM.  But we make it easier to
73*4882a593Smuzhiyun  * add other 93Cx6 functions.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun struct seeprom_cmd {
76*4882a593Smuzhiyun   	uint8_t len;
77*4882a593Smuzhiyun  	uint8_t bits[11];
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /* Short opcodes for the c46 */
81*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
82*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* Long opcodes for the C56/C66 */
85*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
86*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /* Common opcodes */
89*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};
90*4882a593Smuzhiyun static const struct seeprom_cmd seeprom_read  = {3, {1, 1, 0}};
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * Wait for the SEERDY to go high; about 800 ns.
94*4882a593Smuzhiyun  */
95*4882a593Smuzhiyun #define CLOCK_PULSE(sd, rdy)				\
96*4882a593Smuzhiyun 	while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) {	\
97*4882a593Smuzhiyun 		;  /* Do nothing */			\
98*4882a593Smuzhiyun 	}						\
99*4882a593Smuzhiyun 	(void)SEEPROM_INB(sd);	/* Clear clock */
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * Send a START condition and the given command
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun static void
send_seeprom_cmd(struct seeprom_descriptor * sd,const struct seeprom_cmd * cmd)105*4882a593Smuzhiyun send_seeprom_cmd(struct seeprom_descriptor *sd, const struct seeprom_cmd *cmd)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	uint8_t temp;
108*4882a593Smuzhiyun 	int i = 0;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* Send chip select for one clock cycle. */
111*4882a593Smuzhiyun 	temp = sd->sd_MS ^ sd->sd_CS;
112*4882a593Smuzhiyun 	SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
113*4882a593Smuzhiyun 	CLOCK_PULSE(sd, sd->sd_RDY);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	for (i = 0; i < cmd->len; i++) {
116*4882a593Smuzhiyun 		if (cmd->bits[i] != 0)
117*4882a593Smuzhiyun 			temp ^= sd->sd_DO;
118*4882a593Smuzhiyun 		SEEPROM_OUTB(sd, temp);
119*4882a593Smuzhiyun 		CLOCK_PULSE(sd, sd->sd_RDY);
120*4882a593Smuzhiyun 		SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
121*4882a593Smuzhiyun 		CLOCK_PULSE(sd, sd->sd_RDY);
122*4882a593Smuzhiyun 		if (cmd->bits[i] != 0)
123*4882a593Smuzhiyun 			temp ^= sd->sd_DO;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun  * Clear CS put the chip in the reset state, where it can wait for new commands.
129*4882a593Smuzhiyun  */
130*4882a593Smuzhiyun static void
reset_seeprom(struct seeprom_descriptor * sd)131*4882a593Smuzhiyun reset_seeprom(struct seeprom_descriptor *sd)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	uint8_t temp;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	temp = sd->sd_MS;
136*4882a593Smuzhiyun 	SEEPROM_OUTB(sd, temp);
137*4882a593Smuzhiyun 	CLOCK_PULSE(sd, sd->sd_RDY);
138*4882a593Smuzhiyun 	SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
139*4882a593Smuzhiyun 	CLOCK_PULSE(sd, sd->sd_RDY);
140*4882a593Smuzhiyun 	SEEPROM_OUTB(sd, temp);
141*4882a593Smuzhiyun 	CLOCK_PULSE(sd, sd->sd_RDY);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun  * Read the serial EEPROM and returns 1 if successful and 0 if
146*4882a593Smuzhiyun  * not successful.
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun int
ahc_read_seeprom(struct seeprom_descriptor * sd,uint16_t * buf,u_int start_addr,u_int count)149*4882a593Smuzhiyun ahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
150*4882a593Smuzhiyun 		 u_int start_addr, u_int count)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int i = 0;
153*4882a593Smuzhiyun 	u_int k = 0;
154*4882a593Smuzhiyun 	uint16_t v;
155*4882a593Smuzhiyun 	uint8_t temp;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/*
158*4882a593Smuzhiyun 	 * Read the requested registers of the seeprom.  The loop
159*4882a593Smuzhiyun 	 * will range from 0 to count-1.
160*4882a593Smuzhiyun 	 */
161*4882a593Smuzhiyun 	for (k = start_addr; k < count + start_addr; k++) {
162*4882a593Smuzhiyun 		/*
163*4882a593Smuzhiyun 		 * Now we're ready to send the read command followed by the
164*4882a593Smuzhiyun 		 * address of the 16-bit register we want to read.
165*4882a593Smuzhiyun 		 */
166*4882a593Smuzhiyun 		send_seeprom_cmd(sd, &seeprom_read);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 		/* Send the 6 or 8 bit address (MSB first, LSB last). */
169*4882a593Smuzhiyun 		temp = sd->sd_MS ^ sd->sd_CS;
170*4882a593Smuzhiyun 		for (i = (sd->sd_chip - 1); i >= 0; i--) {
171*4882a593Smuzhiyun 			if ((k & (1 << i)) != 0)
172*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
173*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp);
174*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
175*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
176*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
177*4882a593Smuzhiyun 			if ((k & (1 << i)) != 0)
178*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		/*
182*4882a593Smuzhiyun 		 * Now read the 16 bit register.  An initial 0 precedes the
183*4882a593Smuzhiyun 		 * register contents which begins with bit 15 (MSB) and ends
184*4882a593Smuzhiyun 		 * with bit 0 (LSB).  The initial 0 will be shifted off the
185*4882a593Smuzhiyun 		 * top of our word as we let the loop run from 0 to 16.
186*4882a593Smuzhiyun 		 */
187*4882a593Smuzhiyun 		v = 0;
188*4882a593Smuzhiyun 		for (i = 16; i >= 0; i--) {
189*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp);
190*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
191*4882a593Smuzhiyun 			v <<= 1;
192*4882a593Smuzhiyun 			if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
193*4882a593Smuzhiyun 				v |= 1;
194*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
195*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 		buf[k - start_addr] = v;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		/* Reset the chip select for the next command cycle. */
201*4882a593Smuzhiyun 		reset_seeprom(sd);
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun #ifdef AHC_DUMP_EEPROM
204*4882a593Smuzhiyun 	printk("\nSerial EEPROM:\n\t");
205*4882a593Smuzhiyun 	for (k = 0; k < count; k = k + 1) {
206*4882a593Smuzhiyun 		if (((k % 8) == 0) && (k != 0)) {
207*4882a593Smuzhiyun 			printk(KERN_CONT "\n\t");
208*4882a593Smuzhiyun 		}
209*4882a593Smuzhiyun 		printk(KERN_CONT " 0x%x", buf[k]);
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	printk(KERN_CONT "\n");
212*4882a593Smuzhiyun #endif
213*4882a593Smuzhiyun 	return (1);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * Write the serial EEPROM and return 1 if successful and 0 if
218*4882a593Smuzhiyun  * not successful.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun int
ahc_write_seeprom(struct seeprom_descriptor * sd,uint16_t * buf,u_int start_addr,u_int count)221*4882a593Smuzhiyun ahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
222*4882a593Smuzhiyun 		  u_int start_addr, u_int count)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	const struct seeprom_cmd *ewen, *ewds;
225*4882a593Smuzhiyun 	uint16_t v;
226*4882a593Smuzhiyun 	uint8_t temp;
227*4882a593Smuzhiyun 	int i, k;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/* Place the chip into write-enable mode */
230*4882a593Smuzhiyun 	if (sd->sd_chip == C46) {
231*4882a593Smuzhiyun 		ewen = &seeprom_ewen;
232*4882a593Smuzhiyun 		ewds = &seeprom_ewds;
233*4882a593Smuzhiyun 	} else if (sd->sd_chip == C56_66) {
234*4882a593Smuzhiyun 		ewen = &seeprom_long_ewen;
235*4882a593Smuzhiyun 		ewds = &seeprom_long_ewds;
236*4882a593Smuzhiyun 	} else {
237*4882a593Smuzhiyun 		printk("ahc_write_seeprom: unsupported seeprom type %d\n",
238*4882a593Smuzhiyun 		       sd->sd_chip);
239*4882a593Smuzhiyun 		return (0);
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	send_seeprom_cmd(sd, ewen);
243*4882a593Smuzhiyun 	reset_seeprom(sd);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/* Write all requested data out to the seeprom. */
246*4882a593Smuzhiyun 	temp = sd->sd_MS ^ sd->sd_CS;
247*4882a593Smuzhiyun 	for (k = start_addr; k < count + start_addr; k++) {
248*4882a593Smuzhiyun 		/* Send the write command */
249*4882a593Smuzhiyun 		send_seeprom_cmd(sd, &seeprom_write);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		/* Send the 6 or 8 bit address (MSB first). */
252*4882a593Smuzhiyun 		for (i = (sd->sd_chip - 1); i >= 0; i--) {
253*4882a593Smuzhiyun 			if ((k & (1 << i)) != 0)
254*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
255*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp);
256*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
257*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
258*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
259*4882a593Smuzhiyun 			if ((k & (1 << i)) != 0)
260*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 		/* Write the 16 bit value, MSB first */
264*4882a593Smuzhiyun 		v = buf[k - start_addr];
265*4882a593Smuzhiyun 		for (i = 15; i >= 0; i--) {
266*4882a593Smuzhiyun 			if ((v & (1 << i)) != 0)
267*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
268*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp);
269*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
270*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
271*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
272*4882a593Smuzhiyun 			if ((v & (1 << i)) != 0)
273*4882a593Smuzhiyun 				temp ^= sd->sd_DO;
274*4882a593Smuzhiyun 		}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 		/* Wait for the chip to complete the write */
277*4882a593Smuzhiyun 		temp = sd->sd_MS;
278*4882a593Smuzhiyun 		SEEPROM_OUTB(sd, temp);
279*4882a593Smuzhiyun 		CLOCK_PULSE(sd, sd->sd_RDY);
280*4882a593Smuzhiyun 		temp = sd->sd_MS ^ sd->sd_CS;
281*4882a593Smuzhiyun 		do {
282*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp);
283*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
284*4882a593Smuzhiyun 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
285*4882a593Smuzhiyun 			CLOCK_PULSE(sd, sd->sd_RDY);
286*4882a593Smuzhiyun 		} while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		reset_seeprom(sd);
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* Put the chip back into write-protect mode */
292*4882a593Smuzhiyun 	send_seeprom_cmd(sd, ewds);
293*4882a593Smuzhiyun 	reset_seeprom(sd);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	return (1);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun int
ahc_verify_cksum(struct seeprom_config * sc)299*4882a593Smuzhiyun ahc_verify_cksum(struct seeprom_config *sc)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	int i;
302*4882a593Smuzhiyun 	int maxaddr;
303*4882a593Smuzhiyun 	uint32_t checksum;
304*4882a593Smuzhiyun 	uint16_t *scarray;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	maxaddr = (sizeof(*sc)/2) - 1;
307*4882a593Smuzhiyun 	checksum = 0;
308*4882a593Smuzhiyun 	scarray = (uint16_t *)sc;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	for (i = 0; i < maxaddr; i++)
311*4882a593Smuzhiyun 		checksum = checksum + scarray[i];
312*4882a593Smuzhiyun 	if (checksum == 0
313*4882a593Smuzhiyun 	 || (checksum & 0xFFFF) != sc->checksum) {
314*4882a593Smuzhiyun 		return (0);
315*4882a593Smuzhiyun 	} else {
316*4882a593Smuzhiyun 		return(1);
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun }
319