aic7xxx_93cx6.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /*
  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 Description
  38. * -------------------------------------------------------------------
  39. * READ 1 10 A5 - A0 Reads data stored in memory,
  40. * starting at specified address
  41. * EWEN 1 00 11XXXX Write enable must precede
  42. * all programming modes
  43. * ERASE 1 11 A5 - A0 Erase register A5A4A3A2A1A0
  44. * WRITE 1 01 A5 - A0 D15 - D0 Writes register
  45. * ERAL 1 00 10XXXX Erase all registers
  46. * WRAL 1 00 01XXXX D15 - D0 Writes to all registers
  47. * EWDS 1 00 00XXXX Disables all programming
  48. * instructions
  49. * *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 unlike
  51. * the 93C46/26/06 chips which have 6 address bits.
  52. *
  53. * The 93C46 has a four wire interface: clock, chip select, data in, and
  54. * data out. In order to perform one of the above functions, you need
  55. * to enable the chip select for a clock period (typically a minimum of
  56. * 1 usec, with the clock high and low a minimum of 750 and 250 nsec
  57. * respectively). While the chip select remains high, you can clock in
  58. * the instructions (above) starting with the start bit, followed by the
  59. * OP code, Address, and Data (if needed). For the READ instruction, the
  60. * requested 16-bit register contents is read from the data out line but
  61. * is preceded by an initial zero (leading 0, followed by 16-bits, MSB
  62. * first). The clock cycling from low to high initiates the next data
  63. * bit to be sent from the chip.
  64. */
  65. #include "aic7xxx_osm.h"
  66. #include "aic7xxx_inline.h"
  67. #include "aic7xxx_93cx6.h"
  68. /*
  69. * Right now, we only have to read the SEEPROM. But we make it easier to
  70. * add other 93Cx6 functions.
  71. */
  72. struct seeprom_cmd {
  73. uint8_t len;
  74. uint8_t bits[11];
  75. };
  76. /* Short opcodes for the c46 */
  77. static const struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
  78. static const struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
  79. /* Long opcodes for the C56/C66 */
  80. static const struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
  81. static const struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
  82. /* Common opcodes */
  83. static const struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};
  84. static const struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
  85. /*
  86. * Wait for the SEERDY to go high; about 800 ns.
  87. */
  88. #define CLOCK_PULSE(sd, rdy) \
  89. while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) { \
  90. ; /* Do nothing */ \
  91. } \
  92. (void)SEEPROM_INB(sd); /* Clear clock */
  93. /*
  94. * Send a START condition and the given command
  95. */
  96. static void
  97. send_seeprom_cmd(struct seeprom_descriptor *sd, const struct seeprom_cmd *cmd)
  98. {
  99. uint8_t temp;
  100. int i = 0;
  101. /* Send chip select for one clock cycle. */
  102. temp = sd->sd_MS ^ sd->sd_CS;
  103. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  104. CLOCK_PULSE(sd, sd->sd_RDY);
  105. for (i = 0; i < cmd->len; i++) {
  106. if (cmd->bits[i] != 0)
  107. temp ^= sd->sd_DO;
  108. SEEPROM_OUTB(sd, temp);
  109. CLOCK_PULSE(sd, sd->sd_RDY);
  110. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  111. CLOCK_PULSE(sd, sd->sd_RDY);
  112. if (cmd->bits[i] != 0)
  113. temp ^= sd->sd_DO;
  114. }
  115. }
  116. /*
  117. * Clear CS put the chip in the reset state, where it can wait for new commands.
  118. */
  119. static void
  120. reset_seeprom(struct seeprom_descriptor *sd)
  121. {
  122. uint8_t temp;
  123. temp = sd->sd_MS;
  124. SEEPROM_OUTB(sd, temp);
  125. CLOCK_PULSE(sd, sd->sd_RDY);
  126. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  127. CLOCK_PULSE(sd, sd->sd_RDY);
  128. SEEPROM_OUTB(sd, temp);
  129. CLOCK_PULSE(sd, sd->sd_RDY);
  130. }
  131. /*
  132. * Read the serial EEPROM and returns 1 if successful and 0 if
  133. * not successful.
  134. */
  135. int
  136. ahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
  137. u_int start_addr, u_int count)
  138. {
  139. int i = 0;
  140. u_int k = 0;
  141. uint16_t v;
  142. uint8_t temp;
  143. /*
  144. * Read the requested registers of the seeprom. The loop
  145. * will range from 0 to count-1.
  146. */
  147. for (k = start_addr; k < count + start_addr; k++) {
  148. /*
  149. * Now we're ready to send the read command followed by the
  150. * address of the 16-bit register we want to read.
  151. */
  152. send_seeprom_cmd(sd, &seeprom_read);
  153. /* Send the 6 or 8 bit address (MSB first, LSB last). */
  154. temp = sd->sd_MS ^ sd->sd_CS;
  155. for (i = (sd->sd_chip - 1); i >= 0; i--) {
  156. if ((k & (1 << i)) != 0)
  157. temp ^= sd->sd_DO;
  158. SEEPROM_OUTB(sd, temp);
  159. CLOCK_PULSE(sd, sd->sd_RDY);
  160. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  161. CLOCK_PULSE(sd, sd->sd_RDY);
  162. if ((k & (1 << i)) != 0)
  163. temp ^= sd->sd_DO;
  164. }
  165. /*
  166. * Now read the 16 bit register. An initial 0 precedes the
  167. * register contents which begins with bit 15 (MSB) and ends
  168. * with bit 0 (LSB). The initial 0 will be shifted off the
  169. * top of our word as we let the loop run from 0 to 16.
  170. */
  171. v = 0;
  172. for (i = 16; i >= 0; i--) {
  173. SEEPROM_OUTB(sd, temp);
  174. CLOCK_PULSE(sd, sd->sd_RDY);
  175. v <<= 1;
  176. if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
  177. v |= 1;
  178. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  179. CLOCK_PULSE(sd, sd->sd_RDY);
  180. }
  181. buf[k - start_addr] = v;
  182. /* Reset the chip select for the next command cycle. */
  183. reset_seeprom(sd);
  184. }
  185. #ifdef AHC_DUMP_EEPROM
  186. printk("\nSerial EEPROM:\n\t");
  187. for (k = 0; k < count; k = k + 1) {
  188. if (((k % 8) == 0) && (k != 0)) {
  189. printk(KERN_CONT "\n\t");
  190. }
  191. printk(KERN_CONT " 0x%x", buf[k]);
  192. }
  193. printk(KERN_CONT "\n");
  194. #endif
  195. return (1);
  196. }
  197. /*
  198. * Write the serial EEPROM and return 1 if successful and 0 if
  199. * not successful.
  200. */
  201. int
  202. ahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
  203. u_int start_addr, u_int count)
  204. {
  205. const struct seeprom_cmd *ewen, *ewds;
  206. uint16_t v;
  207. uint8_t temp;
  208. int i, k;
  209. /* Place the chip into write-enable mode */
  210. if (sd->sd_chip == C46) {
  211. ewen = &seeprom_ewen;
  212. ewds = &seeprom_ewds;
  213. } else if (sd->sd_chip == C56_66) {
  214. ewen = &seeprom_long_ewen;
  215. ewds = &seeprom_long_ewds;
  216. } else {
  217. printk("ahc_write_seeprom: unsupported seeprom type %d\n",
  218. sd->sd_chip);
  219. return (0);
  220. }
  221. send_seeprom_cmd(sd, ewen);
  222. reset_seeprom(sd);
  223. /* Write all requested data out to the seeprom. */
  224. temp = sd->sd_MS ^ sd->sd_CS;
  225. for (k = start_addr; k < count + start_addr; k++) {
  226. /* Send the write command */
  227. send_seeprom_cmd(sd, &seeprom_write);
  228. /* Send the 6 or 8 bit address (MSB first). */
  229. for (i = (sd->sd_chip - 1); i >= 0; i--) {
  230. if ((k & (1 << i)) != 0)
  231. temp ^= sd->sd_DO;
  232. SEEPROM_OUTB(sd, temp);
  233. CLOCK_PULSE(sd, sd->sd_RDY);
  234. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  235. CLOCK_PULSE(sd, sd->sd_RDY);
  236. if ((k & (1 << i)) != 0)
  237. temp ^= sd->sd_DO;
  238. }
  239. /* Write the 16 bit value, MSB first */
  240. v = buf[k - start_addr];
  241. for (i = 15; i >= 0; i--) {
  242. if ((v & (1 << i)) != 0)
  243. temp ^= sd->sd_DO;
  244. SEEPROM_OUTB(sd, temp);
  245. CLOCK_PULSE(sd, sd->sd_RDY);
  246. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  247. CLOCK_PULSE(sd, sd->sd_RDY);
  248. if ((v & (1 << i)) != 0)
  249. temp ^= sd->sd_DO;
  250. }
  251. /* Wait for the chip to complete the write */
  252. temp = sd->sd_MS;
  253. SEEPROM_OUTB(sd, temp);
  254. CLOCK_PULSE(sd, sd->sd_RDY);
  255. temp = sd->sd_MS ^ sd->sd_CS;
  256. do {
  257. SEEPROM_OUTB(sd, temp);
  258. CLOCK_PULSE(sd, sd->sd_RDY);
  259. SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
  260. CLOCK_PULSE(sd, sd->sd_RDY);
  261. } while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);
  262. reset_seeprom(sd);
  263. }
  264. /* Put the chip back into write-protect mode */
  265. send_seeprom_cmd(sd, ewds);
  266. reset_seeprom(sd);
  267. return (1);
  268. }
  269. int
  270. ahc_verify_cksum(struct seeprom_config *sc)
  271. {
  272. int i;
  273. int maxaddr;
  274. uint32_t checksum;
  275. uint16_t *scarray;
  276. maxaddr = (sizeof(*sc)/2) - 1;
  277. checksum = 0;
  278. scarray = (uint16_t *)sc;
  279. for (i = 0; i < maxaddr; i++)
  280. checksum = checksum + scarray[i];
  281. if (checksum == 0
  282. || (checksum & 0xFFFF) != sc->checksum) {
  283. return (0);
  284. } else {
  285. return(1);
  286. }
  287. }