eeprom_93cx6.c 8.4 KB

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