eeprom_93cx6.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2004 - 2006 rt2x00 SourceForge Project
  4. * <http://rt2x00.serialmonkey.com>
  5. *
  6. * Module: eeprom_93cx6
  7. * Abstract: EEPROM reader routines for 93cx6 chipsets.
  8. * Supported chipsets: 93c46 & 93c66.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/eeprom_93cx6.h>
  14. MODULE_AUTHOR("http://rt2x00.serialmonkey.com");
  15. MODULE_VERSION("1.0");
  16. MODULE_DESCRIPTION("EEPROM 93cx6 chip driver");
  17. MODULE_LICENSE("GPL");
  18. static inline void eeprom_93cx6_pulse_high(struct eeprom_93cx6 *eeprom)
  19. {
  20. eeprom->reg_data_clock = 1;
  21. eeprom->register_write(eeprom);
  22. /*
  23. * Add a short delay for the pulse to work.
  24. * According to the specifications the "maximum minimum"
  25. * time should be 450ns.
  26. */
  27. ndelay(450);
  28. }
  29. static inline void eeprom_93cx6_pulse_low(struct eeprom_93cx6 *eeprom)
  30. {
  31. eeprom->reg_data_clock = 0;
  32. eeprom->register_write(eeprom);
  33. /*
  34. * Add a short delay for the pulse to work.
  35. * According to the specifications the "maximum minimum"
  36. * time should be 450ns.
  37. */
  38. ndelay(450);
  39. }
  40. static void eeprom_93cx6_startup(struct eeprom_93cx6 *eeprom)
  41. {
  42. /*
  43. * Clear all flags, and enable chip select.
  44. */
  45. eeprom->register_read(eeprom);
  46. eeprom->reg_data_in = 0;
  47. eeprom->reg_data_out = 0;
  48. eeprom->reg_data_clock = 0;
  49. eeprom->reg_chip_select = 1;
  50. eeprom->drive_data = 1;
  51. eeprom->register_write(eeprom);
  52. /*
  53. * kick a pulse.
  54. */
  55. eeprom_93cx6_pulse_high(eeprom);
  56. eeprom_93cx6_pulse_low(eeprom);
  57. }
  58. static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
  59. {
  60. /*
  61. * Clear chip_select and data_in flags.
  62. */
  63. eeprom->register_read(eeprom);
  64. eeprom->reg_data_in = 0;
  65. eeprom->reg_chip_select = 0;
  66. eeprom->register_write(eeprom);
  67. /*
  68. * kick a pulse.
  69. */
  70. eeprom_93cx6_pulse_high(eeprom);
  71. eeprom_93cx6_pulse_low(eeprom);
  72. }
  73. static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
  74. const u16 data, const u16 count)
  75. {
  76. unsigned int i;
  77. eeprom->register_read(eeprom);
  78. /*
  79. * Clear data flags.
  80. */
  81. eeprom->reg_data_in = 0;
  82. eeprom->reg_data_out = 0;
  83. eeprom->drive_data = 1;
  84. /*
  85. * Start writing all bits.
  86. */
  87. for (i = count; i > 0; i--) {
  88. /*
  89. * Check if this bit needs to be set.
  90. */
  91. eeprom->reg_data_in = !!(data & (1 << (i - 1)));
  92. /*
  93. * Write the bit to the eeprom register.
  94. */
  95. eeprom->register_write(eeprom);
  96. /*
  97. * Kick a pulse.
  98. */
  99. eeprom_93cx6_pulse_high(eeprom);
  100. eeprom_93cx6_pulse_low(eeprom);
  101. }
  102. eeprom->reg_data_in = 0;
  103. eeprom->register_write(eeprom);
  104. }
  105. static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
  106. u16 *data, const u16 count)
  107. {
  108. unsigned int i;
  109. u16 buf = 0;
  110. eeprom->register_read(eeprom);
  111. /*
  112. * Clear data flags.
  113. */
  114. eeprom->reg_data_in = 0;
  115. eeprom->reg_data_out = 0;
  116. eeprom->drive_data = 0;
  117. /*
  118. * Start reading all bits.
  119. */
  120. for (i = count; i > 0; i--) {
  121. eeprom_93cx6_pulse_high(eeprom);
  122. eeprom->register_read(eeprom);
  123. /*
  124. * Clear data_in flag.
  125. */
  126. eeprom->reg_data_in = 0;
  127. /*
  128. * Read if the bit has been set.
  129. */
  130. if (eeprom->reg_data_out)
  131. buf |= (1 << (i - 1));
  132. eeprom_93cx6_pulse_low(eeprom);
  133. }
  134. *data = buf;
  135. }
  136. /**
  137. * eeprom_93cx6_read - Read a word from eeprom
  138. * @eeprom: Pointer to eeprom structure
  139. * @word: Word index from where we should start reading
  140. * @data: target pointer where the information will have to be stored
  141. *
  142. * This function will read the eeprom data as host-endian word
  143. * into the given data pointer.
  144. */
  145. void eeprom_93cx6_read(struct eeprom_93cx6 *eeprom, const u8 word,
  146. u16 *data)
  147. {
  148. u16 command;
  149. /*
  150. * Initialize the eeprom register
  151. */
  152. eeprom_93cx6_startup(eeprom);
  153. /*
  154. * Select the read opcode and the word to be read.
  155. */
  156. command = (PCI_EEPROM_READ_OPCODE << eeprom->width) | word;
  157. eeprom_93cx6_write_bits(eeprom, command,
  158. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  159. if (has_quirk_extra_read_cycle(eeprom)) {
  160. eeprom_93cx6_pulse_high(eeprom);
  161. eeprom_93cx6_pulse_low(eeprom);
  162. }
  163. /*
  164. * Read the requested 16 bits.
  165. */
  166. eeprom_93cx6_read_bits(eeprom, data, 16);
  167. /*
  168. * Cleanup eeprom register.
  169. */
  170. eeprom_93cx6_cleanup(eeprom);
  171. }
  172. EXPORT_SYMBOL_GPL(eeprom_93cx6_read);
  173. /**
  174. * eeprom_93cx6_multiread - Read multiple words from eeprom
  175. * @eeprom: Pointer to eeprom structure
  176. * @word: Word index from where we should start reading
  177. * @data: target pointer where the information will have to be stored
  178. * @words: Number of words that should be read.
  179. *
  180. * This function will read all requested words from the eeprom,
  181. * this is done by calling eeprom_93cx6_read() multiple times.
  182. * But with the additional change that while the eeprom_93cx6_read
  183. * will return host ordered bytes, this method will return little
  184. * endian words.
  185. */
  186. void eeprom_93cx6_multiread(struct eeprom_93cx6 *eeprom, const u8 word,
  187. __le16 *data, const u16 words)
  188. {
  189. unsigned int i;
  190. u16 tmp;
  191. for (i = 0; i < words; i++) {
  192. tmp = 0;
  193. eeprom_93cx6_read(eeprom, word + i, &tmp);
  194. data[i] = cpu_to_le16(tmp);
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(eeprom_93cx6_multiread);
  198. /**
  199. * eeprom_93cx6_readb - Read a byte from eeprom
  200. * @eeprom: Pointer to eeprom structure
  201. * @byte: Byte index from where we should start reading
  202. * @data: target pointer where the information will have to be stored
  203. *
  204. * This function will read a byte of the eeprom data
  205. * into the given data pointer.
  206. */
  207. void eeprom_93cx6_readb(struct eeprom_93cx6 *eeprom, const u8 byte,
  208. u8 *data)
  209. {
  210. u16 command;
  211. u16 tmp;
  212. /*
  213. * Initialize the eeprom register
  214. */
  215. eeprom_93cx6_startup(eeprom);
  216. /*
  217. * Select the read opcode and the byte to be read.
  218. */
  219. command = (PCI_EEPROM_READ_OPCODE << (eeprom->width + 1)) | byte;
  220. eeprom_93cx6_write_bits(eeprom, command,
  221. PCI_EEPROM_WIDTH_OPCODE + eeprom->width + 1);
  222. if (has_quirk_extra_read_cycle(eeprom)) {
  223. eeprom_93cx6_pulse_high(eeprom);
  224. eeprom_93cx6_pulse_low(eeprom);
  225. }
  226. /*
  227. * Read the requested 8 bits.
  228. */
  229. eeprom_93cx6_read_bits(eeprom, &tmp, 8);
  230. *data = tmp & 0xff;
  231. /*
  232. * Cleanup eeprom register.
  233. */
  234. eeprom_93cx6_cleanup(eeprom);
  235. }
  236. EXPORT_SYMBOL_GPL(eeprom_93cx6_readb);
  237. /**
  238. * eeprom_93cx6_multireadb - Read multiple bytes from eeprom
  239. * @eeprom: Pointer to eeprom structure
  240. * @byte: Index from where we should start reading
  241. * @data: target pointer where the information will have to be stored
  242. * @bytes: Number of bytes that should be read.
  243. *
  244. * This function will read all requested bytes from the eeprom,
  245. * this is done by calling eeprom_93cx6_readb() multiple times.
  246. */
  247. void eeprom_93cx6_multireadb(struct eeprom_93cx6 *eeprom, const u8 byte,
  248. u8 *data, const u16 bytes)
  249. {
  250. unsigned int i;
  251. for (i = 0; i < bytes; i++)
  252. eeprom_93cx6_readb(eeprom, byte + i, &data[i]);
  253. }
  254. EXPORT_SYMBOL_GPL(eeprom_93cx6_multireadb);
  255. /**
  256. * eeprom_93cx6_wren - set the write enable state
  257. * @eeprom: Pointer to eeprom structure
  258. * @enable: true to enable writes, otherwise disable writes
  259. *
  260. * Set the EEPROM write enable state to either allow or deny
  261. * writes depending on the @enable value.
  262. */
  263. void eeprom_93cx6_wren(struct eeprom_93cx6 *eeprom, bool enable)
  264. {
  265. u16 command;
  266. /* start the command */
  267. eeprom_93cx6_startup(eeprom);
  268. /* create command to enable/disable */
  269. command = enable ? PCI_EEPROM_EWEN_OPCODE : PCI_EEPROM_EWDS_OPCODE;
  270. command <<= (eeprom->width - 2);
  271. eeprom_93cx6_write_bits(eeprom, command,
  272. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  273. eeprom_93cx6_cleanup(eeprom);
  274. }
  275. EXPORT_SYMBOL_GPL(eeprom_93cx6_wren);
  276. /**
  277. * eeprom_93cx6_write - write data to the EEPROM
  278. * @eeprom: Pointer to eeprom structure
  279. * @addr: Address to write data to.
  280. * @data: The data to write to address @addr.
  281. *
  282. * Write the @data to the specified @addr in the EEPROM and
  283. * waiting for the device to finish writing.
  284. *
  285. * Note, since we do not expect large number of write operations
  286. * we delay in between parts of the operation to avoid using excessive
  287. * amounts of CPU time busy waiting.
  288. */
  289. void eeprom_93cx6_write(struct eeprom_93cx6 *eeprom, u8 addr, u16 data)
  290. {
  291. int timeout = 100;
  292. u16 command;
  293. /* start the command */
  294. eeprom_93cx6_startup(eeprom);
  295. command = PCI_EEPROM_WRITE_OPCODE << eeprom->width;
  296. command |= addr;
  297. /* send write command */
  298. eeprom_93cx6_write_bits(eeprom, command,
  299. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  300. /* send data */
  301. eeprom_93cx6_write_bits(eeprom, data, 16);
  302. /* get ready to check for busy */
  303. eeprom->drive_data = 0;
  304. eeprom->reg_chip_select = 1;
  305. eeprom->register_write(eeprom);
  306. /* wait at-least 250ns to get DO to be the busy signal */
  307. usleep_range(1000, 2000);
  308. /* wait for DO to go high to signify finish */
  309. while (true) {
  310. eeprom->register_read(eeprom);
  311. if (eeprom->reg_data_out)
  312. break;
  313. usleep_range(1000, 2000);
  314. if (--timeout <= 0) {
  315. printk(KERN_ERR "%s: timeout\n", __func__);
  316. break;
  317. }
  318. }
  319. eeprom_93cx6_cleanup(eeprom);
  320. }
  321. EXPORT_SYMBOL_GPL(eeprom_93cx6_write);