aic7xxx_93cx6.c 9.6 KB

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