eeprom_93cx6.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 a word 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_readb - Read a byte from eeprom
  205. * @eeprom: Pointer to eeprom structure
  206. * @word: Byte index from where we should start reading
  207. * @data: target pointer where the information will have to be stored
  208. *
  209. * This function will read a byte of the eeprom data
  210. * into the given data pointer.
  211. */
  212. void eeprom_93cx6_readb(struct eeprom_93cx6 *eeprom, const u8 byte,
  213. u8 *data)
  214. {
  215. u16 command;
  216. u16 tmp;
  217. /*
  218. * Initialize the eeprom register
  219. */
  220. eeprom_93cx6_startup(eeprom);
  221. /*
  222. * Select the read opcode and the byte to be read.
  223. */
  224. command = (PCI_EEPROM_READ_OPCODE << (eeprom->width + 1)) | byte;
  225. eeprom_93cx6_write_bits(eeprom, command,
  226. PCI_EEPROM_WIDTH_OPCODE + eeprom->width + 1);
  227. /*
  228. * Read the requested 8 bits.
  229. */
  230. eeprom_93cx6_read_bits(eeprom, &tmp, 8);
  231. *data = tmp & 0xff;
  232. /*
  233. * Cleanup eeprom register.
  234. */
  235. eeprom_93cx6_cleanup(eeprom);
  236. }
  237. EXPORT_SYMBOL_GPL(eeprom_93cx6_readb);
  238. /**
  239. * eeprom_93cx6_multireadb - Read multiple bytes from eeprom
  240. * @eeprom: Pointer to eeprom structure
  241. * @byte: Index from where we should start reading
  242. * @data: target pointer where the information will have to be stored
  243. * @words: Number of bytes that should be read.
  244. *
  245. * This function will read all requested bytes from the eeprom,
  246. * this is done by calling eeprom_93cx6_readb() multiple times.
  247. */
  248. void eeprom_93cx6_multireadb(struct eeprom_93cx6 *eeprom, const u8 byte,
  249. u8 *data, const u16 bytes)
  250. {
  251. unsigned int i;
  252. for (i = 0; i < bytes; i++)
  253. eeprom_93cx6_readb(eeprom, byte + i, &data[i]);
  254. }
  255. EXPORT_SYMBOL_GPL(eeprom_93cx6_multireadb);
  256. /**
  257. * eeprom_93cx6_wren - set the write enable state
  258. * @eeprom: Pointer to eeprom structure
  259. * @enable: true to enable writes, otherwise disable writes
  260. *
  261. * Set the EEPROM write enable state to either allow or deny
  262. * writes depending on the @enable value.
  263. */
  264. void eeprom_93cx6_wren(struct eeprom_93cx6 *eeprom, bool enable)
  265. {
  266. u16 command;
  267. /* start the command */
  268. eeprom_93cx6_startup(eeprom);
  269. /* create command to enable/disable */
  270. command = enable ? PCI_EEPROM_EWEN_OPCODE : PCI_EEPROM_EWDS_OPCODE;
  271. command <<= (eeprom->width - 2);
  272. eeprom_93cx6_write_bits(eeprom, command,
  273. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  274. eeprom_93cx6_cleanup(eeprom);
  275. }
  276. EXPORT_SYMBOL_GPL(eeprom_93cx6_wren);
  277. /**
  278. * eeprom_93cx6_write - write data to the EEPROM
  279. * @eeprom: Pointer to eeprom structure
  280. * @addr: Address to write data to.
  281. * @data: The data to write to address @addr.
  282. *
  283. * Write the @data to the specified @addr in the EEPROM and
  284. * waiting for the device to finish writing.
  285. *
  286. * Note, since we do not expect large number of write operations
  287. * we delay in between parts of the operation to avoid using excessive
  288. * amounts of CPU time busy waiting.
  289. */
  290. void eeprom_93cx6_write(struct eeprom_93cx6 *eeprom, u8 addr, u16 data)
  291. {
  292. int timeout = 100;
  293. u16 command;
  294. /* start the command */
  295. eeprom_93cx6_startup(eeprom);
  296. command = PCI_EEPROM_WRITE_OPCODE << eeprom->width;
  297. command |= addr;
  298. /* send write command */
  299. eeprom_93cx6_write_bits(eeprom, command,
  300. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  301. /* send data */
  302. eeprom_93cx6_write_bits(eeprom, data, 16);
  303. /* get ready to check for busy */
  304. eeprom->drive_data = 0;
  305. eeprom->reg_chip_select = 1;
  306. eeprom->register_write(eeprom);
  307. /* wait at-least 250ns to get DO to be the busy signal */
  308. usleep_range(1000, 2000);
  309. /* wait for DO to go high to signify finish */
  310. while (true) {
  311. eeprom->register_read(eeprom);
  312. if (eeprom->reg_data_out)
  313. break;
  314. usleep_range(1000, 2000);
  315. if (--timeout <= 0) {
  316. printk(KERN_ERR "%s: timeout\n", __func__);
  317. break;
  318. }
  319. }
  320. eeprom_93cx6_cleanup(eeprom);
  321. }
  322. EXPORT_SYMBOL_GPL(eeprom_93cx6_write);