eeprom_93cx6.c 5.4 KB

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