xref: /onnv-gate/usr/src/uts/common/io/rtw/smc93cx6.c (revision 10448:fe6b44e693c2)
14689Sql147931 /*
2*10448SMikore.Li@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
34689Sql147931  * Use is subject to license terms.
44689Sql147931  */
54689Sql147931 
64689Sql147931 /*
74689Sql147931  * Interface for the 93C66/56/46/26/06 serial eeprom parts.
84689Sql147931  *
94689Sql147931  * Copyright (c) 1995, 1996 Daniel M. Eischen
104689Sql147931  * All rights reserved.
114689Sql147931  *
124689Sql147931  * Redistribution and use in source and binary forms, with or without
134689Sql147931  * modification, are permitted provided that the following conditions
144689Sql147931  * are met:
154689Sql147931  * 1. Redistributions of source code must retain the above copyright
164689Sql147931  *    notice immediately at the beginning of the file, without modification,
174689Sql147931  *    this list of conditions, and the following disclaimer.
184689Sql147931  * 2. Redistributions in binary form must reproduce the above copyright
194689Sql147931  *    notice, this list of conditions and the following disclaimer in the
204689Sql147931  *    documentation and/or other materials provided with the distribution.
214689Sql147931  * 3. Absolutely no warranty of function or purpose is made by the author
224689Sql147931  *    Daniel M. Eischen.
234689Sql147931  * 4. Modifications may be freely made to this file if the above conditions
244689Sql147931  *    are met.
254689Sql147931  *
264689Sql147931  * $FreeBSD: src/sys/dev/aic7xxx/93cx6.c,v 1.5 2000/01/07 23:08:17 gibbs Exp $
274689Sql147931  */
284689Sql147931 
294689Sql147931 /*
304689Sql147931  *   The instruction set of the 93C66/56/46/26/06 chips are as follows:
314689Sql147931  *
324689Sql147931  *               Start  OP	    *
334689Sql147931  *     Function   Bit  Code  Address**  Data     Description
344689Sql147931  *     -------------------------------------------------------------------
354689Sql147931  *     READ        1    10   A5 - A0             Reads data stored in memory,
364689Sql147931  *                                               starting at specified address
374689Sql147931  *     EWEN        1    00   11XXXX              Write enable must precede
384689Sql147931  *                                               all programming modes
394689Sql147931  *     ERASE       1    11   A5 - A0             Erase register A5A4A3A2A1A0
404689Sql147931  *     WRITE       1    01   A5 - A0   D15 - D0  Writes register
414689Sql147931  *     ERAL        1    00   10XXXX              Erase all registers
424689Sql147931  *     WRAL        1    00   01XXXX    D15 - D0  Writes to all registers
434689Sql147931  *     EWDS        1    00   00XXXX              Disables all programming
444689Sql147931  *                                               instructions
454689Sql147931  *     *Note: A value of X for address is a don't care condition.
464689Sql147931  *    **Note: There are 8 address bits for the 93C56/66 chips unlike
474689Sql147931  *	      the 93C46/26/06 chips which have 6 address bits.
484689Sql147931  *
494689Sql147931  *   The 93C46 has a four wire interface: clock, chip select, data in, and
504689Sql147931  *   data out.  In order to perform one of the above functions, you need
514689Sql147931  *   to enable the chip select for a clock period (typically a minimum of
524689Sql147931  *   1 usec, with the clock high and low a minimum of 750 and 250 nsec
534689Sql147931  *   respectively).  While the chip select remains high, you can clock in
544689Sql147931  *   the instructions (above) starting with the start bit, followed by the
554689Sql147931  *   OP code, Address, and Data (if needed).  For the READ instruction, the
564689Sql147931  *   requested 16-bit register contents is read from the data out line but
574689Sql147931  *   is preceded by an initial zero (leading 0, followed by 16-bits, MSB
584689Sql147931  *   first).  The clock cycling from low to high initiates the next data
594689Sql147931  *   bit to be sent from the chip.
604689Sql147931  *
614689Sql147931  */
624689Sql147931 #include <sys/sunddi.h>
634689Sql147931 #include "smc93cx6var.h"
644689Sql147931 /*
654689Sql147931  * Right now, we only have to read the SEEPROM.  But we make it easier to
664689Sql147931  * add other 93Cx6 functions.
674689Sql147931  */
684689Sql147931 static struct seeprom_cmd {
694689Sql147931 	unsigned char len;
704689Sql147931 	unsigned char bits[3];
714689Sql147931 } seeprom_read = {3, {1, 1, 0}};
724689Sql147931 
734689Sql147931 #define	CLOCK_PULSE(sd, rdy) {					\
744689Sql147931 	/*								\
754689Sql147931 	 * Wait for the SEERDY to go high; about 800 ns.		\
764689Sql147931 	 */								\
774689Sql147931 	int cpi = 1000;							\
784689Sql147931 	if (rdy == 0) {							\
794689Sql147931 		DELAY(4); /* more than long enough */			\
804689Sql147931 	} else {							\
814689Sql147931 		while ((SEEPROM_STATUS_INB(sd) & rdy) == 0 &&		\
824689Sql147931 		    cpi-- > 0) {					\
834689Sql147931 			cpi = cpi;	/* for lint */			\
844689Sql147931 		}							\
854689Sql147931 		(void) SEEPROM_INB(sd);	/* Clear clock */		\
864689Sql147931 	}								\
874689Sql147931 }
884689Sql147931 
894689Sql147931 /*
904689Sql147931  * Read the serial EEPROM and returns 1 if successful and 0 if
914689Sql147931  * not successful.
924689Sql147931  */
934689Sql147931 int
read_seeprom(sd,buf,start_addr,count)944689Sql147931 read_seeprom(sd, buf, start_addr, count)
954689Sql147931 	struct seeprom_descriptor *sd;
964689Sql147931 	uint16_t *buf;
974689Sql147931 	size_t start_addr;
984689Sql147931 	size_t count;
994689Sql147931 {
1004689Sql147931 	int i = 0;
1014689Sql147931 	size_t k = 0;
1024689Sql147931 	uint16_t v;
1034689Sql147931 	uint32_t temp;
1044689Sql147931 
1054689Sql147931 	/*
1064689Sql147931 	 * Read the requested registers of the seeprom.  The loop
1074689Sql147931 	 * will range from 0 to count-1.
1084689Sql147931 	 */
1094689Sql147931 	for (k = start_addr; k < count + start_addr; k++) {
1104689Sql147931 		/* Send chip select for one clock cycle. */
1114689Sql147931 		temp = sd->sd_MS ^ sd->sd_CS;
1124689Sql147931 		SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1134689Sql147931 		CLOCK_PULSE(sd, sd->sd_RDY);
1144689Sql147931 
1154689Sql147931 		/*
1164689Sql147931 		 * Now we're ready to send the read command followed by the
1174689Sql147931 		 * address of the 16-bit register we want to read.
1184689Sql147931 		 */
1194689Sql147931 		for (i = 0; i < seeprom_read.len; i++) {
1204689Sql147931 			if (seeprom_read.bits[i] != 0)
1214689Sql147931 				temp ^= sd->sd_DO;
1224689Sql147931 			SEEPROM_OUTB(sd, temp);
1234689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1244689Sql147931 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1254689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1264689Sql147931 			if (seeprom_read.bits[i] != 0)
1274689Sql147931 				temp ^= sd->sd_DO;
1284689Sql147931 		}
1294689Sql147931 		/* Send the 6 or 8 bit address (MSB first, LSB last). */
1304689Sql147931 		for (i = (sd->sd_chip - 1); i >= 0; i--) {
1314689Sql147931 			if ((k & (1 << i)) != 0)
1324689Sql147931 				temp ^= sd->sd_DO;
1334689Sql147931 			SEEPROM_OUTB(sd, temp);
1344689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1354689Sql147931 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1364689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1374689Sql147931 			if ((k & (1 << i)) != 0)
1384689Sql147931 				temp ^= sd->sd_DO;
1394689Sql147931 		}
1404689Sql147931 
1414689Sql147931 		/*
1424689Sql147931 		 * Now read the 16 bit register.  An initial 0 precedes the
1434689Sql147931 		 * register contents which begins with bit 15 (MSB) and ends
1444689Sql147931 		 * with bit 0 (LSB).  The initial 0 will be shifted off the
1454689Sql147931 		 * top of our word as we let the loop run from 0 to 16.
1464689Sql147931 		 */
1474689Sql147931 		v = 0;
1484689Sql147931 		for (i = 16; i >= 0; i--) {
1494689Sql147931 			SEEPROM_OUTB(sd, temp);
1504689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1514689Sql147931 			v <<= 1;
1524689Sql147931 			if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
1534689Sql147931 				v |= 1;
1544689Sql147931 			SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1554689Sql147931 			CLOCK_PULSE(sd, sd->sd_RDY);
1564689Sql147931 		}
1574689Sql147931 
1584689Sql147931 		buf[k - start_addr] = v;
1594689Sql147931 
1604689Sql147931 		/* Reset the chip select for the next command cycle. */
1614689Sql147931 		temp = sd->sd_MS;
1624689Sql147931 		SEEPROM_OUTB(sd, temp);
1634689Sql147931 		CLOCK_PULSE(sd, sd->sd_RDY);
1644689Sql147931 		SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1654689Sql147931 		CLOCK_PULSE(sd, sd->sd_RDY);
1664689Sql147931 		SEEPROM_OUTB(sd, temp);
1674689Sql147931 		CLOCK_PULSE(sd, sd->sd_RDY);
1684689Sql147931 	}
1694689Sql147931 #ifdef AHC_DUMP_EEPROM
1704689Sql147931 	cmn_err(CE_NOTE, "\nSerial EEPROM:\n\t");
1714689Sql147931 	for (k = 0; k < count; k = k + 1) {
1724689Sql147931 		cmn_err(CE_NOTE, " 0x%x", buf[k]);
1734689Sql147931 	}
1744689Sql147931 #endif
1754689Sql147931 	return (1);
1764689Sql147931 }
177