/*-1* Interface for the 93C66/56/46/26/06 serial eeprom parts.2*3* Copyright (c) 1995, 1996 Daniel M. Eischen4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions, and the following disclaimer,11* without modification.12* 2. The name of the author may not be used to endorse or promote products13* derived from this software without specific prior written permission.14*15* Alternatively, this software may be distributed under the terms of the16* GNU General Public License ("GPL").17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR22* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* $Id: //depot/aic7xxx/aic7xxx/aic7xxx_93cx6.c#19 $31*/3233/*34* The instruction set of the 93C66/56/46/26/06 chips are as follows:35*36* Start OP *37* Function Bit Code Address** Data Description38* -------------------------------------------------------------------39* READ 1 10 A5 - A0 Reads data stored in memory,40* starting at specified address41* EWEN 1 00 11XXXX Write enable must precede42* all programming modes43* ERASE 1 11 A5 - A0 Erase register A5A4A3A2A1A044* WRITE 1 01 A5 - A0 D15 - D0 Writes register45* ERAL 1 00 10XXXX Erase all registers46* WRAL 1 00 01XXXX D15 - D0 Writes to all registers47* EWDS 1 00 00XXXX Disables all programming48* instructions49* *Note: A value of X for address is a don't care condition.50* **Note: There are 8 address bits for the 93C56/66 chips unlike51* the 93C46/26/06 chips which have 6 address bits.52*53* The 93C46 has a four wire interface: clock, chip select, data in, and54* data out. In order to perform one of the above functions, you need55* to enable the chip select for a clock period (typically a minimum of56* 1 usec, with the clock high and low a minimum of 750 and 250 nsec57* respectively). While the chip select remains high, you can clock in58* the instructions (above) starting with the start bit, followed by the59* OP code, Address, and Data (if needed). For the READ instruction, the60* requested 16-bit register contents is read from the data out line but61* is preceded by an initial zero (leading 0, followed by 16-bits, MSB62* first). The clock cycling from low to high initiates the next data63* bit to be sent from the chip.64*/6566#include <dev/aic7xxx/aic7xxx_osm.h>67#include <dev/aic7xxx/aic7xxx_inline.h>68#include <dev/aic7xxx/aic7xxx_93cx6.h>6970/*71* Right now, we only have to read the SEEPROM. But we make it easier to72* add other 93Cx6 functions.73*/74struct seeprom_cmd {75uint8_t len;76uint8_t bits[11];77};7879/* Short opcodes for the c46 */80static struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};81static struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};8283/* Long opcodes for the C56/C66 */84static struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};85static struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};8687/* Common opcodes */88static struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};89static struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};9091/*92* Wait for the SEERDY to go high; about 800 ns.93*/94#define CLOCK_PULSE(sd, rdy) \95while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) { \96; /* Do nothing */ \97} \98(void)SEEPROM_INB(sd); /* Clear clock */99100/*101* Send a START condition and the given command102*/103static void104send_seeprom_cmd(struct seeprom_descriptor *sd, struct seeprom_cmd *cmd)105{106uint8_t temp;107int i = 0;108109/* Send chip select for one clock cycle. */110temp = sd->sd_MS ^ sd->sd_CS;111SEEPROM_OUTB(sd, temp ^ sd->sd_CK);112CLOCK_PULSE(sd, sd->sd_RDY);113114for (i = 0; i < cmd->len; i++) {115if (cmd->bits[i] != 0)116temp ^= sd->sd_DO;117SEEPROM_OUTB(sd, temp);118CLOCK_PULSE(sd, sd->sd_RDY);119SEEPROM_OUTB(sd, temp ^ sd->sd_CK);120CLOCK_PULSE(sd, sd->sd_RDY);121if (cmd->bits[i] != 0)122temp ^= sd->sd_DO;123}124}125126/*127* Clear CS put the chip in the reset state, where it can wait for new commands.128*/129static void130reset_seeprom(struct seeprom_descriptor *sd)131{132uint8_t temp;133134temp = sd->sd_MS;135SEEPROM_OUTB(sd, temp);136CLOCK_PULSE(sd, sd->sd_RDY);137SEEPROM_OUTB(sd, temp ^ sd->sd_CK);138CLOCK_PULSE(sd, sd->sd_RDY);139SEEPROM_OUTB(sd, temp);140CLOCK_PULSE(sd, sd->sd_RDY);141}142143/*144* Read the serial EEPROM and returns 1 if successful and 0 if145* not successful.146*/147int148ahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,149u_int start_addr, u_int count)150{151int i = 0;152u_int k = 0;153uint16_t v;154uint8_t temp;155156/*157* Read the requested registers of the seeprom. The loop158* will range from 0 to count-1.159*/160for (k = start_addr; k < count + start_addr; k++) {161/*162* Now we're ready to send the read command followed by the163* address of the 16-bit register we want to read.164*/165send_seeprom_cmd(sd, &seeprom_read);166167/* Send the 6 or 8 bit address (MSB first, LSB last). */168temp = sd->sd_MS ^ sd->sd_CS;169for (i = (sd->sd_chip - 1); i >= 0; i--) {170if ((k & (1 << i)) != 0)171temp ^= sd->sd_DO;172SEEPROM_OUTB(sd, temp);173CLOCK_PULSE(sd, sd->sd_RDY);174SEEPROM_OUTB(sd, temp ^ sd->sd_CK);175CLOCK_PULSE(sd, sd->sd_RDY);176if ((k & (1 << i)) != 0)177temp ^= sd->sd_DO;178}179180/*181* Now read the 16 bit register. An initial 0 precedes the182* register contents which begins with bit 15 (MSB) and ends183* with bit 0 (LSB). The initial 0 will be shifted off the184* top of our word as we let the loop run from 0 to 16.185*/186v = 0;187for (i = 16; i >= 0; i--) {188SEEPROM_OUTB(sd, temp);189CLOCK_PULSE(sd, sd->sd_RDY);190v <<= 1;191if (SEEPROM_DATA_INB(sd) & sd->sd_DI)192v |= 1;193SEEPROM_OUTB(sd, temp ^ sd->sd_CK);194CLOCK_PULSE(sd, sd->sd_RDY);195}196197buf[k - start_addr] = v;198199/* Reset the chip select for the next command cycle. */200reset_seeprom(sd);201}202#ifdef AHC_DUMP_EEPROM203printf("\nSerial EEPROM:\n\t");204for (k = 0; k < count; k = k + 1) {205if (((k % 8) == 0) && (k != 0)) {206printf ("\n\t");207}208printf (" 0x%x", buf[k]);209}210printf ("\n");211#endif212return (1);213}214215/*216* Write the serial EEPROM and return 1 if successful and 0 if217* not successful.218*/219int220ahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,221u_int start_addr, u_int count)222{223struct seeprom_cmd *ewen, *ewds;224uint16_t v;225uint8_t temp;226int i, k;227228/* Place the chip into write-enable mode */229if (sd->sd_chip == C46) {230ewen = &seeprom_ewen;231ewds = &seeprom_ewds;232} else if (sd->sd_chip == C56_66) {233ewen = &seeprom_long_ewen;234ewds = &seeprom_long_ewds;235} else {236printf("ahc_write_seeprom: unsupported seeprom type %d\n",237sd->sd_chip);238return (0);239}240241send_seeprom_cmd(sd, ewen);242reset_seeprom(sd);243244/* Write all requested data out to the seeprom. */245temp = sd->sd_MS ^ sd->sd_CS;246for (k = start_addr; k < count + start_addr; k++) {247/* Send the write command */248send_seeprom_cmd(sd, &seeprom_write);249250/* Send the 6 or 8 bit address (MSB first). */251for (i = (sd->sd_chip - 1); i >= 0; i--) {252if ((k & (1 << i)) != 0)253temp ^= sd->sd_DO;254SEEPROM_OUTB(sd, temp);255CLOCK_PULSE(sd, sd->sd_RDY);256SEEPROM_OUTB(sd, temp ^ sd->sd_CK);257CLOCK_PULSE(sd, sd->sd_RDY);258if ((k & (1 << i)) != 0)259temp ^= sd->sd_DO;260}261262/* Write the 16 bit value, MSB first */263v = buf[k - start_addr];264for (i = 15; i >= 0; i--) {265if ((v & (1 << i)) != 0)266temp ^= sd->sd_DO;267SEEPROM_OUTB(sd, temp);268CLOCK_PULSE(sd, sd->sd_RDY);269SEEPROM_OUTB(sd, temp ^ sd->sd_CK);270CLOCK_PULSE(sd, sd->sd_RDY);271if ((v & (1 << i)) != 0)272temp ^= sd->sd_DO;273}274275/* Wait for the chip to complete the write */276temp = sd->sd_MS;277SEEPROM_OUTB(sd, temp);278CLOCK_PULSE(sd, sd->sd_RDY);279temp = sd->sd_MS ^ sd->sd_CS;280do {281SEEPROM_OUTB(sd, temp);282CLOCK_PULSE(sd, sd->sd_RDY);283SEEPROM_OUTB(sd, temp ^ sd->sd_CK);284CLOCK_PULSE(sd, sd->sd_RDY);285} while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);286287reset_seeprom(sd);288}289290/* Put the chip back into write-protect mode */291send_seeprom_cmd(sd, ewds);292reset_seeprom(sd);293294return (1);295}296297int298ahc_verify_cksum(struct seeprom_config *sc)299{300int i;301int maxaddr;302uint32_t checksum;303uint16_t *scarray;304305maxaddr = (sizeof(*sc)/2) - 1;306checksum = 0;307scarray = (uint16_t *)sc;308309for (i = 0; i < maxaddr; i++)310checksum = checksum + scarray[i];311if (checksum == 0312|| (checksum & 0xFFFF) != sc->checksum) {313return (0);314} else {315return(1);316}317}318319320