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