11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * Interface for the 93C66/56/46/26/06 serial eeprom parts.
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (c) 1995, 1996 Daniel M. Eischen
51da177e4SLinus Torvalds * All rights reserved.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without
81da177e4SLinus Torvalds * modification, are permitted provided that the following conditions
91da177e4SLinus Torvalds * are met:
101da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright
111da177e4SLinus Torvalds * notice, this list of conditions, and the following disclaimer,
121da177e4SLinus Torvalds * without modification.
131da177e4SLinus Torvalds * 2. The name of the author may not be used to endorse or promote products
141da177e4SLinus Torvalds * derived from this software without specific prior written permission.
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * Alternatively, this software may be distributed under the terms of the
171da177e4SLinus Torvalds * GNU General Public License ("GPL").
181da177e4SLinus Torvalds *
191da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
201da177e4SLinus Torvalds * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211da177e4SLinus Torvalds * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221da177e4SLinus Torvalds * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
231da177e4SLinus Torvalds * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241da177e4SLinus Torvalds * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251da177e4SLinus Torvalds * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261da177e4SLinus Torvalds * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271da177e4SLinus Torvalds * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281da177e4SLinus Torvalds * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291da177e4SLinus Torvalds * SUCH DAMAGE.
301da177e4SLinus Torvalds *
3179778a27SJames Bottomley * $Id: //depot/aic7xxx/aic7xxx/aic7xxx_93cx6.c#19 $
321da177e4SLinus Torvalds */
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds * The instruction set of the 93C66/56/46/26/06 chips are as follows:
361da177e4SLinus Torvalds *
371da177e4SLinus Torvalds * Start OP *
381da177e4SLinus Torvalds * Function Bit Code Address** Data Description
391da177e4SLinus Torvalds * -------------------------------------------------------------------
401da177e4SLinus Torvalds * READ 1 10 A5 - A0 Reads data stored in memory,
411da177e4SLinus Torvalds * starting at specified address
421da177e4SLinus Torvalds * EWEN 1 00 11XXXX Write enable must precede
431da177e4SLinus Torvalds * all programming modes
441da177e4SLinus Torvalds * ERASE 1 11 A5 - A0 Erase register A5A4A3A2A1A0
451da177e4SLinus Torvalds * WRITE 1 01 A5 - A0 D15 - D0 Writes register
461da177e4SLinus Torvalds * ERAL 1 00 10XXXX Erase all registers
471da177e4SLinus Torvalds * WRAL 1 00 01XXXX D15 - D0 Writes to all registers
481da177e4SLinus Torvalds * EWDS 1 00 00XXXX Disables all programming
491da177e4SLinus Torvalds * instructions
501da177e4SLinus Torvalds * *Note: A value of X for address is a don't care condition.
511da177e4SLinus Torvalds * **Note: There are 8 address bits for the 93C56/66 chips unlike
521da177e4SLinus Torvalds * the 93C46/26/06 chips which have 6 address bits.
531da177e4SLinus Torvalds *
541da177e4SLinus Torvalds * The 93C46 has a four wire interface: clock, chip select, data in, and
551da177e4SLinus Torvalds * data out. In order to perform one of the above functions, you need
561da177e4SLinus Torvalds * to enable the chip select for a clock period (typically a minimum of
571da177e4SLinus Torvalds * 1 usec, with the clock high and low a minimum of 750 and 250 nsec
581da177e4SLinus Torvalds * respectively). While the chip select remains high, you can clock in
591da177e4SLinus Torvalds * the instructions (above) starting with the start bit, followed by the
601da177e4SLinus Torvalds * OP code, Address, and Data (if needed). For the READ instruction, the
611da177e4SLinus Torvalds * requested 16-bit register contents is read from the data out line but
621da177e4SLinus Torvalds * is preceded by an initial zero (leading 0, followed by 16-bits, MSB
631da177e4SLinus Torvalds * first). The clock cycling from low to high initiates the next data
641da177e4SLinus Torvalds * bit to be sent from the chip.
651da177e4SLinus Torvalds */
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds #include "aic7xxx_osm.h"
681da177e4SLinus Torvalds #include "aic7xxx_inline.h"
691da177e4SLinus Torvalds #include "aic7xxx_93cx6.h"
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds * Right now, we only have to read the SEEPROM. But we make it easier to
731da177e4SLinus Torvalds * add other 93Cx6 functions.
741da177e4SLinus Torvalds */
7579778a27SJames Bottomley struct seeprom_cmd {
761da177e4SLinus Torvalds uint8_t len;
7779778a27SJames Bottomley uint8_t bits[11];
7879778a27SJames Bottomley };
791da177e4SLinus Torvalds
8079778a27SJames Bottomley /* Short opcodes for the c46 */
81980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
82980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
8379778a27SJames Bottomley
8479778a27SJames Bottomley /* Long opcodes for the C56/C66 */
85980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
86980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
8779778a27SJames Bottomley
8879778a27SJames Bottomley /* Common opcodes */
89980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};
90980b306aSDenys Vlasenko static const struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds * Wait for the SEERDY to go high; about 800 ns.
941da177e4SLinus Torvalds */
951da177e4SLinus Torvalds #define CLOCK_PULSE(sd, rdy) \
961da177e4SLinus Torvalds while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) { \
971da177e4SLinus Torvalds ; /* Do nothing */ \
981da177e4SLinus Torvalds } \
991da177e4SLinus Torvalds (void)SEEPROM_INB(sd); /* Clear clock */
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds * Send a START condition and the given command
1031da177e4SLinus Torvalds */
1041da177e4SLinus Torvalds static void
send_seeprom_cmd(struct seeprom_descriptor * sd,const struct seeprom_cmd * cmd)105980b306aSDenys Vlasenko send_seeprom_cmd(struct seeprom_descriptor *sd, const struct seeprom_cmd *cmd)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds uint8_t temp;
1081da177e4SLinus Torvalds int i = 0;
1091da177e4SLinus Torvalds
1101da177e4SLinus Torvalds /* Send chip select for one clock cycle. */
1111da177e4SLinus Torvalds temp = sd->sd_MS ^ sd->sd_CS;
1121da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1131da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds for (i = 0; i < cmd->len; i++) {
1161da177e4SLinus Torvalds if (cmd->bits[i] != 0)
1171da177e4SLinus Torvalds temp ^= sd->sd_DO;
1181da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
1191da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1201da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1211da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1221da177e4SLinus Torvalds if (cmd->bits[i] != 0)
1231da177e4SLinus Torvalds temp ^= sd->sd_DO;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds
1271da177e4SLinus Torvalds /*
1281da177e4SLinus Torvalds * Clear CS put the chip in the reset state, where it can wait for new commands.
1291da177e4SLinus Torvalds */
1301da177e4SLinus Torvalds static void
reset_seeprom(struct seeprom_descriptor * sd)1311da177e4SLinus Torvalds reset_seeprom(struct seeprom_descriptor *sd)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds uint8_t temp;
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds temp = sd->sd_MS;
1361da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
1371da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1381da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1391da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1401da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
1411da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds * Read the serial EEPROM and returns 1 if successful and 0 if
1461da177e4SLinus Torvalds * not successful.
1471da177e4SLinus Torvalds */
1481da177e4SLinus Torvalds int
ahc_read_seeprom(struct seeprom_descriptor * sd,uint16_t * buf,u_int start_addr,u_int count)1491da177e4SLinus Torvalds ahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
1501da177e4SLinus Torvalds u_int start_addr, u_int count)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds int i = 0;
1531da177e4SLinus Torvalds u_int k = 0;
1541da177e4SLinus Torvalds uint16_t v;
1551da177e4SLinus Torvalds uint8_t temp;
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds * Read the requested registers of the seeprom. The loop
1591da177e4SLinus Torvalds * will range from 0 to count-1.
1601da177e4SLinus Torvalds */
1611da177e4SLinus Torvalds for (k = start_addr; k < count + start_addr; k++) {
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds * Now we're ready to send the read command followed by the
1641da177e4SLinus Torvalds * address of the 16-bit register we want to read.
1651da177e4SLinus Torvalds */
1661da177e4SLinus Torvalds send_seeprom_cmd(sd, &seeprom_read);
1671da177e4SLinus Torvalds
1681da177e4SLinus Torvalds /* Send the 6 or 8 bit address (MSB first, LSB last). */
1691da177e4SLinus Torvalds temp = sd->sd_MS ^ sd->sd_CS;
1701da177e4SLinus Torvalds for (i = (sd->sd_chip - 1); i >= 0; i--) {
1711da177e4SLinus Torvalds if ((k & (1 << i)) != 0)
1721da177e4SLinus Torvalds temp ^= sd->sd_DO;
1731da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
1741da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1751da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1761da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1771da177e4SLinus Torvalds if ((k & (1 << i)) != 0)
1781da177e4SLinus Torvalds temp ^= sd->sd_DO;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds * Now read the 16 bit register. An initial 0 precedes the
1831da177e4SLinus Torvalds * register contents which begins with bit 15 (MSB) and ends
1841da177e4SLinus Torvalds * with bit 0 (LSB). The initial 0 will be shifted off the
1851da177e4SLinus Torvalds * top of our word as we let the loop run from 0 to 16.
1861da177e4SLinus Torvalds */
1871da177e4SLinus Torvalds v = 0;
1881da177e4SLinus Torvalds for (i = 16; i >= 0; i--) {
1891da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
1901da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1911da177e4SLinus Torvalds v <<= 1;
1921da177e4SLinus Torvalds if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
1931da177e4SLinus Torvalds v |= 1;
1941da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
1951da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds buf[k - start_addr] = v;
1991da177e4SLinus Torvalds
2001da177e4SLinus Torvalds /* Reset the chip select for the next command cycle. */
2011da177e4SLinus Torvalds reset_seeprom(sd);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds #ifdef AHC_DUMP_EEPROM
204*48813cf9SPekka Enberg printk("\nSerial EEPROM:\n\t");
2051da177e4SLinus Torvalds for (k = 0; k < count; k = k + 1) {
2061da177e4SLinus Torvalds if (((k % 8) == 0) && (k != 0)) {
207*48813cf9SPekka Enberg printk(KERN_CONT "\n\t");
2081da177e4SLinus Torvalds }
209*48813cf9SPekka Enberg printk(KERN_CONT " 0x%x", buf[k]);
2101da177e4SLinus Torvalds }
211*48813cf9SPekka Enberg printk(KERN_CONT "\n");
2121da177e4SLinus Torvalds #endif
2131da177e4SLinus Torvalds return (1);
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds * Write the serial EEPROM and return 1 if successful and 0 if
2181da177e4SLinus Torvalds * not successful.
2191da177e4SLinus Torvalds */
2201da177e4SLinus Torvalds int
ahc_write_seeprom(struct seeprom_descriptor * sd,uint16_t * buf,u_int start_addr,u_int count)2211da177e4SLinus Torvalds ahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
2221da177e4SLinus Torvalds u_int start_addr, u_int count)
2231da177e4SLinus Torvalds {
224980b306aSDenys Vlasenko const struct seeprom_cmd *ewen, *ewds;
2251da177e4SLinus Torvalds uint16_t v;
2261da177e4SLinus Torvalds uint8_t temp;
2271da177e4SLinus Torvalds int i, k;
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds /* Place the chip into write-enable mode */
23079778a27SJames Bottomley if (sd->sd_chip == C46) {
23179778a27SJames Bottomley ewen = &seeprom_ewen;
23279778a27SJames Bottomley ewds = &seeprom_ewds;
23379778a27SJames Bottomley } else if (sd->sd_chip == C56_66) {
23479778a27SJames Bottomley ewen = &seeprom_long_ewen;
23579778a27SJames Bottomley ewds = &seeprom_long_ewds;
23679778a27SJames Bottomley } else {
237*48813cf9SPekka Enberg printk("ahc_write_seeprom: unsupported seeprom type %d\n",
23879778a27SJames Bottomley sd->sd_chip);
23979778a27SJames Bottomley return (0);
24079778a27SJames Bottomley }
24179778a27SJames Bottomley
24279778a27SJames Bottomley send_seeprom_cmd(sd, ewen);
2431da177e4SLinus Torvalds reset_seeprom(sd);
2441da177e4SLinus Torvalds
2451da177e4SLinus Torvalds /* Write all requested data out to the seeprom. */
2461da177e4SLinus Torvalds temp = sd->sd_MS ^ sd->sd_CS;
2471da177e4SLinus Torvalds for (k = start_addr; k < count + start_addr; k++) {
2481da177e4SLinus Torvalds /* Send the write command */
2491da177e4SLinus Torvalds send_seeprom_cmd(sd, &seeprom_write);
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds /* Send the 6 or 8 bit address (MSB first). */
2521da177e4SLinus Torvalds for (i = (sd->sd_chip - 1); i >= 0; i--) {
2531da177e4SLinus Torvalds if ((k & (1 << i)) != 0)
2541da177e4SLinus Torvalds temp ^= sd->sd_DO;
2551da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
2561da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2571da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
2581da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2591da177e4SLinus Torvalds if ((k & (1 << i)) != 0)
2601da177e4SLinus Torvalds temp ^= sd->sd_DO;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds /* Write the 16 bit value, MSB first */
2641da177e4SLinus Torvalds v = buf[k - start_addr];
2651da177e4SLinus Torvalds for (i = 15; i >= 0; i--) {
2661da177e4SLinus Torvalds if ((v & (1 << i)) != 0)
2671da177e4SLinus Torvalds temp ^= sd->sd_DO;
2681da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
2691da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2701da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
2711da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2721da177e4SLinus Torvalds if ((v & (1 << i)) != 0)
2731da177e4SLinus Torvalds temp ^= sd->sd_DO;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds
2761da177e4SLinus Torvalds /* Wait for the chip to complete the write */
2771da177e4SLinus Torvalds temp = sd->sd_MS;
2781da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
2791da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2801da177e4SLinus Torvalds temp = sd->sd_MS ^ sd->sd_CS;
2811da177e4SLinus Torvalds do {
2821da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp);
2831da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2841da177e4SLinus Torvalds SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
2851da177e4SLinus Torvalds CLOCK_PULSE(sd, sd->sd_RDY);
2861da177e4SLinus Torvalds } while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);
2871da177e4SLinus Torvalds
2881da177e4SLinus Torvalds reset_seeprom(sd);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds
2911da177e4SLinus Torvalds /* Put the chip back into write-protect mode */
29279778a27SJames Bottomley send_seeprom_cmd(sd, ewds);
2931da177e4SLinus Torvalds reset_seeprom(sd);
2941da177e4SLinus Torvalds
2951da177e4SLinus Torvalds return (1);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds
2981da177e4SLinus Torvalds int
ahc_verify_cksum(struct seeprom_config * sc)2991da177e4SLinus Torvalds ahc_verify_cksum(struct seeprom_config *sc)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds int i;
3021da177e4SLinus Torvalds int maxaddr;
3031da177e4SLinus Torvalds uint32_t checksum;
3041da177e4SLinus Torvalds uint16_t *scarray;
3051da177e4SLinus Torvalds
3061da177e4SLinus Torvalds maxaddr = (sizeof(*sc)/2) - 1;
3071da177e4SLinus Torvalds checksum = 0;
3081da177e4SLinus Torvalds scarray = (uint16_t *)sc;
3091da177e4SLinus Torvalds
3101da177e4SLinus Torvalds for (i = 0; i < maxaddr; i++)
3111da177e4SLinus Torvalds checksum = checksum + scarray[i];
3121da177e4SLinus Torvalds if (checksum == 0
3131da177e4SLinus Torvalds || (checksum & 0xFFFF) != sc->checksum) {
3141da177e4SLinus Torvalds return (0);
3151da177e4SLinus Torvalds } else {
3161da177e4SLinus Torvalds return(1);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds }
319