eeprom_93cx6.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2004 - 2006 rt2x00 SourceForge Project
  3. * <http://rt2x00.serialmonkey.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Module: eeprom_93cx6
  16. * Abstract: EEPROM reader routines for 93cx6 chipsets.
  17. * Supported chipsets: 93c46 & 93c66.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/eeprom_93cx6.h>
  23. MODULE_AUTHOR("http://rt2x00.serialmonkey.com");
  24. MODULE_VERSION("1.0");
  25. MODULE_DESCRIPTION("EEPROM 93cx6 chip driver");
  26. MODULE_LICENSE("GPL");
  27. static inline void eeprom_93cx6_pulse_high(struct eeprom_93cx6 *eeprom)
  28. {
  29. eeprom->reg_data_clock = 1;
  30. eeprom->register_write(eeprom);
  31. /*
  32. * Add a short delay for the pulse to work.
  33. * According to the specifications the "maximum minimum"
  34. * time should be 450ns.
  35. */
  36. ndelay(450);
  37. }
  38. static inline void eeprom_93cx6_pulse_low(struct eeprom_93cx6 *eeprom)
  39. {
  40. eeprom->reg_data_clock = 0;
  41. eeprom->register_write(eeprom);
  42. /*
  43. * Add a short delay for the pulse to work.
  44. * According to the specifications the "maximum minimum"
  45. * time should be 450ns.
  46. */
  47. ndelay(450);
  48. }
  49. static void eeprom_93cx6_startup(struct eeprom_93cx6 *eeprom)
  50. {
  51. /*
  52. * Clear all flags, and enable chip select.
  53. */
  54. eeprom->register_read(eeprom);
  55. eeprom->reg_data_in = 0;
  56. eeprom->reg_data_out = 0;
  57. eeprom->reg_data_clock = 0;
  58. eeprom->reg_chip_select = 1;
  59. eeprom->register_write(eeprom);
  60. /*
  61. * kick a pulse.
  62. */
  63. eeprom_93cx6_pulse_high(eeprom);
  64. eeprom_93cx6_pulse_low(eeprom);
  65. }
  66. static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
  67. {
  68. /*
  69. * Clear chip_select and data_in flags.
  70. */
  71. eeprom->register_read(eeprom);
  72. eeprom->reg_data_in = 0;
  73. eeprom->reg_chip_select = 0;
  74. eeprom->register_write(eeprom);
  75. /*
  76. * kick a pulse.
  77. */
  78. eeprom_93cx6_pulse_high(eeprom);
  79. eeprom_93cx6_pulse_low(eeprom);
  80. }
  81. static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
  82. const u16 data, const u16 count)
  83. {
  84. unsigned int i;
  85. eeprom->register_read(eeprom);
  86. /*
  87. * Clear data flags.
  88. */
  89. eeprom->reg_data_in = 0;
  90. eeprom->reg_data_out = 0;
  91. /*
  92. * Start writing all bits.
  93. */
  94. for (i = count; i > 0; i--) {
  95. /*
  96. * Check if this bit needs to be set.
  97. */
  98. eeprom->reg_data_in = !!(data & (1 << (i - 1)));
  99. /*
  100. * Write the bit to the eeprom register.
  101. */
  102. eeprom->register_write(eeprom);
  103. /*
  104. * Kick a pulse.
  105. */
  106. eeprom_93cx6_pulse_high(eeprom);
  107. eeprom_93cx6_pulse_low(eeprom);
  108. }
  109. eeprom->reg_data_in = 0;
  110. eeprom->register_write(eeprom);
  111. }
  112. static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
  113. u16 *data, const u16 count)
  114. {
  115. unsigned int i;
  116. u16 buf = 0;
  117. eeprom->register_read(eeprom);
  118. /*
  119. * Clear data flags.
  120. */
  121. eeprom->reg_data_in = 0;
  122. eeprom->reg_data_out = 0;
  123. /*
  124. * Start reading all bits.
  125. */
  126. for (i = count; i > 0; i--) {
  127. eeprom_93cx6_pulse_high(eeprom);
  128. eeprom->register_read(eeprom);
  129. /*
  130. * Clear data_in flag.
  131. */
  132. eeprom->reg_data_in = 0;
  133. /*
  134. * Read if the bit has been set.
  135. */
  136. if (eeprom->reg_data_out)
  137. buf |= (1 << (i - 1));
  138. eeprom_93cx6_pulse_low(eeprom);
  139. }
  140. *data = buf;
  141. }
  142. /**
  143. * eeprom_93cx6_read - Read multiple words from eeprom
  144. * @eeprom: Pointer to eeprom structure
  145. * @word: Word index from where we should start reading
  146. * @data: target pointer where the information will have to be stored
  147. *
  148. * This function will read the eeprom data as host-endian word
  149. * into the given data pointer.
  150. */
  151. void eeprom_93cx6_read(struct eeprom_93cx6 *eeprom, const u8 word,
  152. u16 *data)
  153. {
  154. u16 command;
  155. /*
  156. * Initialize the eeprom register
  157. */
  158. eeprom_93cx6_startup(eeprom);
  159. /*
  160. * Select the read opcode and the word to be read.
  161. */
  162. command = (PCI_EEPROM_READ_OPCODE << eeprom->width) | word;
  163. eeprom_93cx6_write_bits(eeprom, command,
  164. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  165. /*
  166. * Read the requested 16 bits.
  167. */
  168. eeprom_93cx6_read_bits(eeprom, data, 16);
  169. /*
  170. * Cleanup eeprom register.
  171. */
  172. eeprom_93cx6_cleanup(eeprom);
  173. }
  174. EXPORT_SYMBOL_GPL(eeprom_93cx6_read);
  175. /**
  176. * eeprom_93cx6_multiread - Read multiple words from eeprom
  177. * @eeprom: Pointer to eeprom structure
  178. * @word: Word index from where we should start reading
  179. * @data: target pointer where the information will have to be stored
  180. * @words: Number of words that should be read.
  181. *
  182. * This function will read all requested words from the eeprom,
  183. * this is done by calling eeprom_93cx6_read() multiple times.
  184. * But with the additional change that while the eeprom_93cx6_read
  185. * will return host ordered bytes, this method will return little
  186. * endian words.
  187. */
  188. void eeprom_93cx6_multiread(struct eeprom_93cx6 *eeprom, const u8 word,
  189. __le16 *data, const u16 words)
  190. {
  191. unsigned int i;
  192. u16 tmp;
  193. for (i = 0; i < words; i++) {
  194. tmp = 0;
  195. eeprom_93cx6_read(eeprom, word + i, &tmp);
  196. data[i] = cpu_to_le16(tmp);
  197. }
  198. }
  199. EXPORT_SYMBOL_GPL(eeprom_93cx6_multiread);