eeprom_93cx6.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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->drive_data = 1;
  60. eeprom->register_write(eeprom);
  61. /*
  62. * kick a pulse.
  63. */
  64. eeprom_93cx6_pulse_high(eeprom);
  65. eeprom_93cx6_pulse_low(eeprom);
  66. }
  67. static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
  68. {
  69. /*
  70. * Clear chip_select and data_in flags.
  71. */
  72. eeprom->register_read(eeprom);
  73. eeprom->reg_data_in = 0;
  74. eeprom->reg_chip_select = 0;
  75. eeprom->register_write(eeprom);
  76. /*
  77. * kick a pulse.
  78. */
  79. eeprom_93cx6_pulse_high(eeprom);
  80. eeprom_93cx6_pulse_low(eeprom);
  81. }
  82. static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
  83. const u16 data, const u16 count)
  84. {
  85. unsigned int i;
  86. eeprom->register_read(eeprom);
  87. /*
  88. * Clear data flags.
  89. */
  90. eeprom->reg_data_in = 0;
  91. eeprom->reg_data_out = 0;
  92. eeprom->drive_data = 1;
  93. /*
  94. * Start writing all bits.
  95. */
  96. for (i = count; i > 0; i--) {
  97. /*
  98. * Check if this bit needs to be set.
  99. */
  100. eeprom->reg_data_in = !!(data & (1 << (i - 1)));
  101. /*
  102. * Write the bit to the eeprom register.
  103. */
  104. eeprom->register_write(eeprom);
  105. /*
  106. * Kick a pulse.
  107. */
  108. eeprom_93cx6_pulse_high(eeprom);
  109. eeprom_93cx6_pulse_low(eeprom);
  110. }
  111. eeprom->reg_data_in = 0;
  112. eeprom->register_write(eeprom);
  113. }
  114. static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
  115. u16 *data, const u16 count)
  116. {
  117. unsigned int i;
  118. u16 buf = 0;
  119. eeprom->register_read(eeprom);
  120. /*
  121. * Clear data flags.
  122. */
  123. eeprom->reg_data_in = 0;
  124. eeprom->reg_data_out = 0;
  125. eeprom->drive_data = 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);
  203. /**
  204. * eeprom_93cx6_wren - set the write enable state
  205. * @eeprom: Pointer to eeprom structure
  206. * @enable: true to enable writes, otherwise disable writes
  207. *
  208. * Set the EEPROM write enable state to either allow or deny
  209. * writes depending on the @enable value.
  210. */
  211. void eeprom_93cx6_wren(struct eeprom_93cx6 *eeprom, bool enable)
  212. {
  213. u16 command;
  214. /* start the command */
  215. eeprom_93cx6_startup(eeprom);
  216. /* create command to enable/disable */
  217. command = enable ? PCI_EEPROM_EWEN_OPCODE : PCI_EEPROM_EWDS_OPCODE;
  218. command <<= (eeprom->width - 2);
  219. eeprom_93cx6_write_bits(eeprom, command,
  220. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  221. eeprom_93cx6_cleanup(eeprom);
  222. }
  223. EXPORT_SYMBOL_GPL(eeprom_93cx6_wren);
  224. /**
  225. * eeprom_93cx6_write - write data to the EEPROM
  226. * @eeprom: Pointer to eeprom structure
  227. * @addr: Address to write data to.
  228. * @data: The data to write to address @addr.
  229. *
  230. * Write the @data to the specified @addr in the EEPROM and
  231. * waiting for the device to finish writing.
  232. *
  233. * Note, since we do not expect large number of write operations
  234. * we delay in between parts of the operation to avoid using excessive
  235. * amounts of CPU time busy waiting.
  236. */
  237. void eeprom_93cx6_write(struct eeprom_93cx6 *eeprom, u8 addr, u16 data)
  238. {
  239. int timeout = 100;
  240. u16 command;
  241. /* start the command */
  242. eeprom_93cx6_startup(eeprom);
  243. command = PCI_EEPROM_WRITE_OPCODE << eeprom->width;
  244. command |= addr;
  245. /* send write command */
  246. eeprom_93cx6_write_bits(eeprom, command,
  247. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  248. /* send data */
  249. eeprom_93cx6_write_bits(eeprom, data, 16);
  250. /* get ready to check for busy */
  251. eeprom->drive_data = 0;
  252. eeprom->reg_chip_select = 1;
  253. eeprom->register_write(eeprom);
  254. /* wait at-least 250ns to get DO to be the busy signal */
  255. usleep_range(1000, 2000);
  256. /* wait for DO to go high to signify finish */
  257. while (true) {
  258. eeprom->register_read(eeprom);
  259. if (eeprom->reg_data_out)
  260. break;
  261. usleep_range(1000, 2000);
  262. if (--timeout <= 0) {
  263. printk(KERN_ERR "%s: timeout\n", __func__);
  264. break;
  265. }
  266. }
  267. eeprom_93cx6_cleanup(eeprom);
  268. }
  269. EXPORT_SYMBOL_GPL(eeprom_93cx6_write);