eeprom_93cx6.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/bits.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/eeprom_93cx6.h>
  15. MODULE_AUTHOR("http://rt2x00.serialmonkey.com");
  16. MODULE_VERSION("1.0");
  17. MODULE_DESCRIPTION("EEPROM 93cx6 chip driver");
  18. MODULE_LICENSE("GPL");
  19. static inline void eeprom_93cx6_pulse_high(struct eeprom_93cx6 *eeprom)
  20. {
  21. eeprom->reg_data_clock = 1;
  22. eeprom->register_write(eeprom);
  23. /*
  24. * Add a short delay for the pulse to work.
  25. * According to the specifications the "maximum minimum"
  26. * time should be 450ns.
  27. */
  28. ndelay(450);
  29. }
  30. static inline void eeprom_93cx6_pulse_low(struct eeprom_93cx6 *eeprom)
  31. {
  32. eeprom->reg_data_clock = 0;
  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 void eeprom_93cx6_startup(struct eeprom_93cx6 *eeprom)
  42. {
  43. /*
  44. * Clear all flags, and enable chip select.
  45. */
  46. eeprom->register_read(eeprom);
  47. eeprom->reg_data_in = 0;
  48. eeprom->reg_data_out = 0;
  49. eeprom->reg_data_clock = 0;
  50. eeprom->reg_chip_select = 1;
  51. eeprom->drive_data = 1;
  52. eeprom->register_write(eeprom);
  53. /*
  54. * kick a pulse.
  55. */
  56. eeprom_93cx6_pulse_high(eeprom);
  57. eeprom_93cx6_pulse_low(eeprom);
  58. }
  59. static void eeprom_93cx6_cleanup(struct eeprom_93cx6 *eeprom)
  60. {
  61. /*
  62. * Clear chip_select and data_in flags.
  63. */
  64. eeprom->register_read(eeprom);
  65. eeprom->reg_data_in = 0;
  66. eeprom->reg_chip_select = 0;
  67. eeprom->register_write(eeprom);
  68. /*
  69. * kick a pulse.
  70. */
  71. eeprom_93cx6_pulse_high(eeprom);
  72. eeprom_93cx6_pulse_low(eeprom);
  73. }
  74. static void eeprom_93cx6_write_bits(struct eeprom_93cx6 *eeprom,
  75. const u16 data, const u16 count)
  76. {
  77. unsigned int i;
  78. eeprom->register_read(eeprom);
  79. /*
  80. * Clear data flags.
  81. */
  82. eeprom->reg_data_in = 0;
  83. eeprom->reg_data_out = 0;
  84. eeprom->drive_data = 1;
  85. /*
  86. * Start writing all bits.
  87. */
  88. for (i = count; i > 0; i--) {
  89. /*
  90. * Check if this bit needs to be set.
  91. */
  92. eeprom->reg_data_in = !!(data & BIT(i - 1));
  93. /*
  94. * Write the bit to the eeprom register.
  95. */
  96. eeprom->register_write(eeprom);
  97. /*
  98. * Kick a pulse.
  99. */
  100. eeprom_93cx6_pulse_high(eeprom);
  101. eeprom_93cx6_pulse_low(eeprom);
  102. }
  103. eeprom->reg_data_in = 0;
  104. eeprom->register_write(eeprom);
  105. }
  106. static void eeprom_93cx6_read_bits(struct eeprom_93cx6 *eeprom,
  107. u16 *data, const u16 count)
  108. {
  109. unsigned int i;
  110. u16 buf = 0;
  111. eeprom->register_read(eeprom);
  112. /*
  113. * Clear data flags.
  114. */
  115. eeprom->reg_data_in = 0;
  116. eeprom->reg_data_out = 0;
  117. eeprom->drive_data = 0;
  118. /*
  119. * Start reading all bits.
  120. */
  121. for (i = count; i > 0; i--) {
  122. eeprom_93cx6_pulse_high(eeprom);
  123. eeprom->register_read(eeprom);
  124. /*
  125. * Clear data_in flag.
  126. */
  127. eeprom->reg_data_in = 0;
  128. /*
  129. * Read if the bit has been set.
  130. */
  131. if (eeprom->reg_data_out)
  132. buf |= BIT(i - 1);
  133. eeprom_93cx6_pulse_low(eeprom);
  134. }
  135. *data = buf;
  136. }
  137. /**
  138. * eeprom_93cx6_read - Read a word from eeprom
  139. * @eeprom: Pointer to eeprom structure
  140. * @word: Word index from where we should start reading
  141. * @data: target pointer where the information will have to be stored
  142. *
  143. * This function will read the eeprom data as host-endian word
  144. * into the given data pointer.
  145. */
  146. void eeprom_93cx6_read(struct eeprom_93cx6 *eeprom, const u8 word,
  147. u16 *data)
  148. {
  149. u16 command;
  150. /*
  151. * Initialize the eeprom register
  152. */
  153. eeprom_93cx6_startup(eeprom);
  154. /*
  155. * Select the read opcode and the word to be read.
  156. */
  157. command = (PCI_EEPROM_READ_OPCODE << eeprom->width) | word;
  158. eeprom_93cx6_write_bits(eeprom, command,
  159. PCI_EEPROM_WIDTH_OPCODE + eeprom->width);
  160. if (has_quirk_extra_read_cycle(eeprom)) {
  161. eeprom_93cx6_pulse_high(eeprom);
  162. eeprom_93cx6_pulse_low(eeprom);
  163. }
  164. /*
  165. * Read the requested 16 bits.
  166. */
  167. eeprom_93cx6_read_bits(eeprom, data, 16);
  168. /*
  169. * Cleanup eeprom register.
  170. */
  171. eeprom_93cx6_cleanup(eeprom);
  172. }
  173. EXPORT_SYMBOL_GPL(eeprom_93cx6_read);
  174. /**
  175. * eeprom_93cx6_multiread - Read multiple words from eeprom
  176. * @eeprom: Pointer to eeprom structure
  177. * @word: Word index from where we should start reading
  178. * @data: target pointer where the information will have to be stored
  179. * @words: Number of words that should be read.
  180. *
  181. * This function will read all requested words from the eeprom,
  182. * this is done by calling eeprom_93cx6_read() multiple times.
  183. * But with the additional change that while the eeprom_93cx6_read
  184. * will return host ordered bytes, this method will return little
  185. * endian words.
  186. */
  187. void eeprom_93cx6_multiread(struct eeprom_93cx6 *eeprom, const u8 word,
  188. __le16 *data, const u16 words)
  189. {
  190. unsigned int i;
  191. u16 tmp;
  192. for (i = 0; i < words; i++) {
  193. tmp = 0;
  194. eeprom_93cx6_read(eeprom, word + i, &tmp);
  195. data[i] = cpu_to_le16(tmp);
  196. }
  197. }
  198. EXPORT_SYMBOL_GPL(eeprom_93cx6_multiread);
  199. /**
  200. * eeprom_93cx6_readb - Read a byte from eeprom
  201. * @eeprom: Pointer to eeprom structure
  202. * @byte: Byte index from where we should start reading
  203. * @data: target pointer where the information will have to be stored
  204. *
  205. * This function will read a byte of the eeprom data
  206. * into the given data pointer.
  207. */
  208. void eeprom_93cx6_readb(struct eeprom_93cx6 *eeprom, const u8 byte,
  209. u8 *data)
  210. {
  211. u16 command;
  212. u16 tmp;
  213. /*
  214. * Initialize the eeprom register
  215. */
  216. eeprom_93cx6_startup(eeprom);
  217. /*
  218. * Select the read opcode and the byte to be read.
  219. */
  220. command = (PCI_EEPROM_READ_OPCODE << (eeprom->width + 1)) | byte;
  221. eeprom_93cx6_write_bits(eeprom, command,
  222. PCI_EEPROM_WIDTH_OPCODE + eeprom->width + 1);
  223. if (has_quirk_extra_read_cycle(eeprom)) {
  224. eeprom_93cx6_pulse_high(eeprom);
  225. eeprom_93cx6_pulse_low(eeprom);
  226. }
  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. * @bytes: 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);