smc93cx6.c revision 1.1 1 1.1 mycroft /* $NetBSD: smc93cx6.c,v 1.1 1996/05/16 03:59:10 mycroft Exp $ */
2 1.1 mycroft
3 1.1 mycroft /*
4 1.1 mycroft * Interface for the 93C46/26/06 serial eeprom parts.
5 1.1 mycroft *
6 1.1 mycroft * Copyright (c) 1995 Daniel M. Eischen
7 1.1 mycroft * All rights reserved.
8 1.1 mycroft *
9 1.1 mycroft * Redistribution and use in source and binary forms, with or without
10 1.1 mycroft * modification, are permitted provided that the following conditions
11 1.1 mycroft * are met:
12 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
13 1.1 mycroft * notice immediately at the beginning of the file, without modification,
14 1.1 mycroft * this list of conditions, and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.1 mycroft * 3. Absolutely no warranty of function or purpose is made by the author
19 1.1 mycroft * Daniel M. Eischen.
20 1.1 mycroft * 4. Modifications may be freely made to this file if the above conditions
21 1.1 mycroft * are met.
22 1.1 mycroft */
23 1.1 mycroft
24 1.1 mycroft /*
25 1.1 mycroft * The instruction set of the 93C46/26/06 chips are as follows:
26 1.1 mycroft *
27 1.1 mycroft * Start OP
28 1.1 mycroft * Function Bit Code Address Data Description
29 1.1 mycroft * -------------------------------------------------------------------
30 1.1 mycroft * READ 1 10 A5 - A0 Reads data stored in memory,
31 1.1 mycroft * starting at specified address
32 1.1 mycroft * EWEN 1 00 11XXXX Write enable must preceed
33 1.1 mycroft * all programming modes
34 1.1 mycroft * ERASE 1 11 A5 - A0 Erase register A5A4A3A2A1A0
35 1.1 mycroft * WRITE 1 01 A5 - A0 D15 - D0 Writes register
36 1.1 mycroft * ERAL 1 00 10XXXX Erase all registers
37 1.1 mycroft * WRAL 1 00 01XXXX D15 - D0 Writes to all registers
38 1.1 mycroft * EWDS 1 00 00XXXX Disables all programming
39 1.1 mycroft * instructions
40 1.1 mycroft * *Note: A value of X for address is a don't care condition.
41 1.1 mycroft *
42 1.1 mycroft * The 93C46 has a four wire interface: clock, chip select, data in, and
43 1.1 mycroft * data out. In order to perform one of the above functions, you need
44 1.1 mycroft * to enable the chip select for a clock period (typically a minimum of
45 1.1 mycroft * 1 usec, with the clock high and low a minimum of 750 and 250 nsec
46 1.1 mycroft * respectively. While the chip select remains high, you can clock in
47 1.1 mycroft * the instructions (above) starting with the start bit, followed by the
48 1.1 mycroft * OP code, Address, and Data (if needed). For the READ instruction, the
49 1.1 mycroft * requested 16-bit register contents is read from the data out line but
50 1.1 mycroft * is preceded by an initial zero (leading 0, followed by 16-bits, MSB
51 1.1 mycroft * first). The clock cycling from low to high initiates the next data
52 1.1 mycroft * bit to be sent from the chip.
53 1.1 mycroft *
54 1.1 mycroft */
55 1.1 mycroft
56 1.1 mycroft #include <sys/param.h>
57 1.1 mycroft #include <sys/systm.h>
58 1.1 mycroft #if defined(__FreeBSD__)
59 1.1 mycroft #include <machine/clock.h>
60 1.1 mycroft #include <i386/scsi/93cx6.h>
61 1.1 mycroft #elif defined(__NetBSD__)
62 1.1 mycroft #include <machine/bus.h>
63 1.1 mycroft #include <dev/ic/smc93cx6var.h>
64 1.1 mycroft #endif
65 1.1 mycroft
66 1.1 mycroft /*
67 1.1 mycroft * Right now, we only have to read the SEEPROM. But we make it easier to
68 1.1 mycroft * add other 93Cx6 functions.
69 1.1 mycroft */
70 1.1 mycroft static struct seeprom_cmd {
71 1.1 mycroft unsigned char len;
72 1.1 mycroft unsigned char bits[3];
73 1.1 mycroft } seeprom_read = {3, {1, 1, 0}};
74 1.1 mycroft
75 1.1 mycroft #if defined(__FreeBSD__)
76 1.1 mycroft #define SEEPROM_INB(sd) inb(sd->sd_iobase)
77 1.1 mycroft #define SEEPROM_OUTB(sd, value) outb(sd->sd_iobase, value)
78 1.1 mycroft #elif defined(__NetBSD__)
79 1.1 mycroft #define SEEPROM_INB(sd) \
80 1.1 mycroft bus_io_read_1(sd->sd_bc, sd->sd_ioh, sd->sd_offset)
81 1.1 mycroft #define SEEPROM_OUTB(sd, value) \
82 1.1 mycroft bus_io_write_1(sd->sd_bc, sd->sd_ioh, sd->sd_offset, value)
83 1.1 mycroft #endif
84 1.1 mycroft
85 1.1 mycroft /*
86 1.1 mycroft * Wait for the SEERDY to go high; about 800 ns.
87 1.1 mycroft */
88 1.1 mycroft #define CLOCK_PULSE(sd, rdy) \
89 1.1 mycroft while ((SEEPROM_INB(sd) & rdy) == 0) { \
90 1.1 mycroft ; /* Do nothing */ \
91 1.1 mycroft }
92 1.1 mycroft
93 1.1 mycroft /*
94 1.1 mycroft * Read the serial EEPROM and returns 1 if successful and 0 if
95 1.1 mycroft * not successful.
96 1.1 mycroft */
97 1.1 mycroft int
98 1.1 mycroft read_seeprom(sd, buf, start_addr, count)
99 1.1 mycroft struct seeprom_descriptor *sd;
100 1.1 mycroft u_int16_t *buf;
101 1.1 mycroft #if defined(__FreeBSD__)
102 1.1 mycroft u_int start_addr;
103 1.1 mycroft int count;
104 1.1 mycroft #elif defined(__NetBSD__)
105 1.1 mycroft bus_io_size_t start_addr;
106 1.1 mycroft bus_io_size_t count;
107 1.1 mycroft #endif
108 1.1 mycroft {
109 1.1 mycroft int i = 0, k = 0;
110 1.1 mycroft u_int16_t v;
111 1.1 mycroft u_int8_t temp;
112 1.1 mycroft
113 1.1 mycroft /*
114 1.1 mycroft * Read the requested registers of the seeprom. The loop
115 1.1 mycroft * will range from 0 to count-1.
116 1.1 mycroft */
117 1.1 mycroft for (k = start_addr; k < count + start_addr; k++) {
118 1.1 mycroft /* Send chip select for one clock cycle. */
119 1.1 mycroft temp = sd->sd_MS ^ sd->sd_CS;
120 1.1 mycroft SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
121 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
122 1.1 mycroft
123 1.1 mycroft /*
124 1.1 mycroft * Now we're ready to send the read command followed by the
125 1.1 mycroft * address of the 16-bit register we want to read.
126 1.1 mycroft */
127 1.1 mycroft for (i = 0; i < seeprom_read.len; i++) {
128 1.1 mycroft if (seeprom_read.bits[i] != 0)
129 1.1 mycroft temp ^= sd->sd_DO;
130 1.1 mycroft SEEPROM_OUTB(sd, temp);
131 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
132 1.1 mycroft SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
133 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
134 1.1 mycroft if (seeprom_read.bits[i] != 0)
135 1.1 mycroft temp ^= sd->sd_DO;
136 1.1 mycroft }
137 1.1 mycroft /* Send the 6 bit address (MSB first, LSB last). */
138 1.1 mycroft for (i = 5; i >= 0; i--) {
139 1.1 mycroft if ((k & (1 << i)) != 0)
140 1.1 mycroft temp ^= sd->sd_DO;
141 1.1 mycroft SEEPROM_OUTB(sd, temp);
142 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
143 1.1 mycroft SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
144 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
145 1.1 mycroft if ((k & (1 << i)) != 0)
146 1.1 mycroft temp ^= sd->sd_DO;
147 1.1 mycroft }
148 1.1 mycroft
149 1.1 mycroft /*
150 1.1 mycroft * Now read the 16 bit register. An initial 0 precedes the
151 1.1 mycroft * register contents which begins with bit 15 (MSB) and ends
152 1.1 mycroft * with bit 0 (LSB). The initial 0 will be shifted off the
153 1.1 mycroft * top of our word as we let the loop run from 0 to 16.
154 1.1 mycroft */
155 1.1 mycroft v = 0;
156 1.1 mycroft for (i = 16; i >= 0; i--) {
157 1.1 mycroft SEEPROM_OUTB(sd, temp);
158 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
159 1.1 mycroft v <<= 1;
160 1.1 mycroft if (SEEPROM_INB(sd) & sd->sd_DI)
161 1.1 mycroft v |= 1;
162 1.1 mycroft SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
163 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
164 1.1 mycroft }
165 1.1 mycroft
166 1.1 mycroft buf[k - start_addr] = v;
167 1.1 mycroft
168 1.1 mycroft /* Reset the chip select for the next command cycle. */
169 1.1 mycroft temp = sd->sd_MS;
170 1.1 mycroft SEEPROM_OUTB(sd, temp);
171 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
172 1.1 mycroft SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
173 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
174 1.1 mycroft SEEPROM_OUTB(sd, temp);
175 1.1 mycroft CLOCK_PULSE(sd, sd->sd_RDY);
176 1.1 mycroft }
177 1.1 mycroft #if 0
178 1.1 mycroft printf ("Serial EEPROM:");
179 1.1 mycroft for (k = 0; k < count; k = k + 1) {
180 1.1 mycroft if (((k % 8) == 0) && (k != 0))
181 1.1 mycroft {
182 1.1 mycroft printf ("\n ");
183 1.1 mycroft }
184 1.1 mycroft printf (" 0x%x", buf[k]);
185 1.1 mycroft }
186 1.1 mycroft printf ("\n");
187 1.1 mycroft #endif
188 1.1 mycroft return (1);
189 1.1 mycroft }
190 1.1 mycroft
191 1.1 mycroft int
192 1.1 mycroft acquire_seeprom(sd)
193 1.1 mycroft struct seeprom_descriptor *sd;
194 1.1 mycroft {
195 1.1 mycroft int wait;
196 1.1 mycroft
197 1.1 mycroft /*
198 1.1 mycroft * Request access of the memory port. When access is
199 1.1 mycroft * granted, SEERDY will go high. We use a 1 second
200 1.1 mycroft * timeout which should be near 1 second more than
201 1.1 mycroft * is needed. Reason: after the chip reset, there
202 1.1 mycroft * should be no contention.
203 1.1 mycroft */
204 1.1 mycroft SEEPROM_OUTB(sd, sd->sd_MS);
205 1.1 mycroft wait = 1000; /* 1 second timeout in msec */
206 1.1 mycroft while (--wait && ((SEEPROM_INB(sd) & sd->sd_RDY) == 0)) {
207 1.1 mycroft DELAY (1000); /* delay 1 msec */
208 1.1 mycroft }
209 1.1 mycroft if ((SEEPROM_INB(sd) & sd->sd_RDY) == 0) {
210 1.1 mycroft SEEPROM_OUTB(sd, 0);
211 1.1 mycroft return (0);
212 1.1 mycroft }
213 1.1 mycroft return(1);
214 1.1 mycroft }
215 1.1 mycroft
216 1.1 mycroft void
217 1.1 mycroft release_seeprom(sd)
218 1.1 mycroft struct seeprom_descriptor *sd;
219 1.1 mycroft {
220 1.1 mycroft /* Release access to the memory port and the serial EEPROM. */
221 1.1 mycroft SEEPROM_OUTB(sd, 0);
222 1.1 mycroft }
223