qib_twsi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (c) 2012 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/delay.h>
  35. #include <linux/pci.h>
  36. #include <linux/vmalloc.h>
  37. #include "qib.h"
  38. /*
  39. * QLogic_IB "Two Wire Serial Interface" driver.
  40. * Originally written for a not-quite-i2c serial eeprom, which is
  41. * still used on some supported boards. Later boards have added a
  42. * variety of other uses, most board-specific, so the bit-boffing
  43. * part has been split off to this file, while the other parts
  44. * have been moved to chip-specific files.
  45. *
  46. * We have also dropped all pretense of fully generic (e.g. pretend
  47. * we don't know whether '1' is the higher voltage) interface, as
  48. * the restrictions of the generic i2c interface (e.g. no access from
  49. * driver itself) make it unsuitable for this use.
  50. */
  51. #define READ_CMD 1
  52. #define WRITE_CMD 0
  53. /**
  54. * i2c_wait_for_writes - wait for a write
  55. * @dd: the qlogic_ib device
  56. *
  57. * We use this instead of udelay directly, so we can make sure
  58. * that previous register writes have been flushed all the way
  59. * to the chip. Since we are delaying anyway, the cost doesn't
  60. * hurt, and makes the bit twiddling more regular
  61. */
  62. static void i2c_wait_for_writes(struct qib_devdata *dd)
  63. {
  64. /*
  65. * implicit read of EXTStatus is as good as explicit
  66. * read of scratch, if all we want to do is flush
  67. * writes.
  68. */
  69. dd->f_gpio_mod(dd, 0, 0, 0);
  70. rmb(); /* inlined, so prevent compiler reordering */
  71. }
  72. /*
  73. * QSFP modules are allowed to hold SCL low for 500uSec. Allow twice that
  74. * for "almost compliant" modules
  75. */
  76. #define SCL_WAIT_USEC 1000
  77. /* BUF_WAIT is time bus must be free between STOP or ACK and to next START.
  78. * Should be 20, but some chips need more.
  79. */
  80. #define TWSI_BUF_WAIT_USEC 60
  81. static void scl_out(struct qib_devdata *dd, u8 bit)
  82. {
  83. u32 mask;
  84. udelay(1);
  85. mask = 1UL << dd->gpio_scl_num;
  86. /* SCL is meant to be bare-drain, so never set "OUT", just DIR */
  87. dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);
  88. /*
  89. * Allow for slow slaves by simple
  90. * delay for falling edge, sampling on rise.
  91. */
  92. if (!bit)
  93. udelay(2);
  94. else {
  95. int rise_usec;
  96. for (rise_usec = SCL_WAIT_USEC; rise_usec > 0; rise_usec -= 2) {
  97. if (mask & dd->f_gpio_mod(dd, 0, 0, 0))
  98. break;
  99. udelay(2);
  100. }
  101. if (rise_usec <= 0)
  102. qib_dev_err(dd, "SCL interface stuck low > %d uSec\n",
  103. SCL_WAIT_USEC);
  104. }
  105. i2c_wait_for_writes(dd);
  106. }
  107. static void sda_out(struct qib_devdata *dd, u8 bit)
  108. {
  109. u32 mask;
  110. mask = 1UL << dd->gpio_sda_num;
  111. /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
  112. dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);
  113. i2c_wait_for_writes(dd);
  114. udelay(2);
  115. }
  116. static u8 sda_in(struct qib_devdata *dd, int wait)
  117. {
  118. int bnum;
  119. u32 read_val, mask;
  120. bnum = dd->gpio_sda_num;
  121. mask = (1UL << bnum);
  122. /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
  123. dd->f_gpio_mod(dd, 0, 0, mask);
  124. read_val = dd->f_gpio_mod(dd, 0, 0, 0);
  125. if (wait)
  126. i2c_wait_for_writes(dd);
  127. return (read_val & mask) >> bnum;
  128. }
  129. /**
  130. * i2c_ackrcv - see if ack following write is true
  131. * @dd: the qlogic_ib device
  132. */
  133. static int i2c_ackrcv(struct qib_devdata *dd)
  134. {
  135. u8 ack_received;
  136. /* AT ENTRY SCL = LOW */
  137. /* change direction, ignore data */
  138. ack_received = sda_in(dd, 1);
  139. scl_out(dd, 1);
  140. ack_received = sda_in(dd, 1) == 0;
  141. scl_out(dd, 0);
  142. return ack_received;
  143. }
  144. static void stop_cmd(struct qib_devdata *dd);
  145. /**
  146. * rd_byte - read a byte, sending STOP on last, else ACK
  147. * @dd: the qlogic_ib device
  148. * @last: identifies the last read
  149. *
  150. * Returns byte shifted out of device
  151. */
  152. static int rd_byte(struct qib_devdata *dd, int last)
  153. {
  154. int bit_cntr, data;
  155. data = 0;
  156. for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {
  157. data <<= 1;
  158. scl_out(dd, 1);
  159. data |= sda_in(dd, 0);
  160. scl_out(dd, 0);
  161. }
  162. if (last) {
  163. scl_out(dd, 1);
  164. stop_cmd(dd);
  165. } else {
  166. sda_out(dd, 0);
  167. scl_out(dd, 1);
  168. scl_out(dd, 0);
  169. sda_out(dd, 1);
  170. }
  171. return data;
  172. }
  173. /**
  174. * wr_byte - write a byte, one bit at a time
  175. * @dd: the qlogic_ib device
  176. * @data: the byte to write
  177. *
  178. * Returns 0 if we got the following ack, otherwise 1
  179. */
  180. static int wr_byte(struct qib_devdata *dd, u8 data)
  181. {
  182. int bit_cntr;
  183. u8 bit;
  184. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  185. bit = (data >> bit_cntr) & 1;
  186. sda_out(dd, bit);
  187. scl_out(dd, 1);
  188. scl_out(dd, 0);
  189. }
  190. return (!i2c_ackrcv(dd)) ? 1 : 0;
  191. }
  192. /*
  193. * issue TWSI start sequence:
  194. * (both clock/data high, clock high, data low while clock is high)
  195. */
  196. static void start_seq(struct qib_devdata *dd)
  197. {
  198. sda_out(dd, 1);
  199. scl_out(dd, 1);
  200. sda_out(dd, 0);
  201. udelay(1);
  202. scl_out(dd, 0);
  203. }
  204. /**
  205. * stop_seq - transmit the stop sequence
  206. * @dd: the qlogic_ib device
  207. *
  208. * (both clock/data low, clock high, data high while clock is high)
  209. */
  210. static void stop_seq(struct qib_devdata *dd)
  211. {
  212. scl_out(dd, 0);
  213. sda_out(dd, 0);
  214. scl_out(dd, 1);
  215. sda_out(dd, 1);
  216. }
  217. /**
  218. * stop_cmd - transmit the stop condition
  219. * @dd: the qlogic_ib device
  220. *
  221. * (both clock/data low, clock high, data high while clock is high)
  222. */
  223. static void stop_cmd(struct qib_devdata *dd)
  224. {
  225. stop_seq(dd);
  226. udelay(TWSI_BUF_WAIT_USEC);
  227. }
  228. /**
  229. * qib_twsi_reset - reset I2C communication
  230. * @dd: the qlogic_ib device
  231. */
  232. int qib_twsi_reset(struct qib_devdata *dd)
  233. {
  234. int clock_cycles_left = 9;
  235. int was_high = 0;
  236. u32 pins, mask;
  237. /* Both SCL and SDA should be high. If not, there
  238. * is something wrong.
  239. */
  240. mask = (1UL << dd->gpio_scl_num) | (1UL << dd->gpio_sda_num);
  241. /*
  242. * Force pins to desired innocuous state.
  243. * This is the default power-on state with out=0 and dir=0,
  244. * So tri-stated and should be floating high (barring HW problems)
  245. */
  246. dd->f_gpio_mod(dd, 0, 0, mask);
  247. /*
  248. * Clock nine times to get all listeners into a sane state.
  249. * If SDA does not go high at any point, we are wedged.
  250. * One vendor recommends then issuing START followed by STOP.
  251. * we cannot use our "normal" functions to do that, because
  252. * if SCL drops between them, another vendor's part will
  253. * wedge, dropping SDA and keeping it low forever, at the end of
  254. * the next transaction (even if it was not the device addressed).
  255. * So our START and STOP take place with SCL held high.
  256. */
  257. while (clock_cycles_left--) {
  258. scl_out(dd, 0);
  259. scl_out(dd, 1);
  260. /* Note if SDA is high, but keep clocking to sync slave */
  261. was_high |= sda_in(dd, 0);
  262. }
  263. if (was_high) {
  264. /*
  265. * We saw a high, which we hope means the slave is sync'd.
  266. * Issue START, STOP, pause for T_BUF.
  267. */
  268. pins = dd->f_gpio_mod(dd, 0, 0, 0);
  269. if ((pins & mask) != mask)
  270. qib_dev_err(dd, "GPIO pins not at rest: %d\n",
  271. pins & mask);
  272. /* Drop SDA to issue START */
  273. udelay(1); /* Guarantee .6 uSec setup */
  274. sda_out(dd, 0);
  275. udelay(1); /* Guarantee .6 uSec hold */
  276. /* At this point, SCL is high, SDA low. Raise SDA for STOP */
  277. sda_out(dd, 1);
  278. udelay(TWSI_BUF_WAIT_USEC);
  279. }
  280. return !was_high;
  281. }
  282. #define QIB_TWSI_START 0x100
  283. #define QIB_TWSI_STOP 0x200
  284. /* Write byte to TWSI, optionally prefixed with START or suffixed with
  285. * STOP.
  286. * returns 0 if OK (ACK received), else != 0
  287. */
  288. static int qib_twsi_wr(struct qib_devdata *dd, int data, int flags)
  289. {
  290. int ret = 1;
  291. if (flags & QIB_TWSI_START)
  292. start_seq(dd);
  293. ret = wr_byte(dd, data); /* Leaves SCL low (from i2c_ackrcv()) */
  294. if (flags & QIB_TWSI_STOP)
  295. stop_cmd(dd);
  296. return ret;
  297. }
  298. /* Added functionality for IBA7220-based cards */
  299. #define QIB_TEMP_DEV 0x98
  300. /*
  301. * qib_twsi_blk_rd
  302. * Formerly called qib_eeprom_internal_read, and only used for eeprom,
  303. * but now the general interface for data transfer from twsi devices.
  304. * One vestige of its former role is that it recognizes a device
  305. * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
  306. * which responded to all TWSI device codes, interpreting them as
  307. * address within device. On all other devices found on board handled by
  308. * this driver, the device is followed by a one-byte "address" which selects
  309. * the "register" or "offset" within the device from which data should
  310. * be read.
  311. */
  312. int qib_twsi_blk_rd(struct qib_devdata *dd, int dev, int addr,
  313. void *buffer, int len)
  314. {
  315. int ret;
  316. u8 *bp = buffer;
  317. ret = 1;
  318. if (dev == QIB_TWSI_NO_DEV) {
  319. /* legacy not-really-I2C */
  320. addr = (addr << 1) | READ_CMD;
  321. ret = qib_twsi_wr(dd, addr, QIB_TWSI_START);
  322. } else {
  323. /* Actual I2C */
  324. ret = qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START);
  325. if (ret) {
  326. stop_cmd(dd);
  327. ret = 1;
  328. goto bail;
  329. }
  330. /*
  331. * SFF spec claims we do _not_ stop after the addr
  332. * but simply issue a start with the "read" dev-addr.
  333. * Since we are implicitely waiting for ACK here,
  334. * we need t_buf (nominally 20uSec) before that start,
  335. * and cannot rely on the delay built in to the STOP
  336. */
  337. ret = qib_twsi_wr(dd, addr, 0);
  338. udelay(TWSI_BUF_WAIT_USEC);
  339. if (ret) {
  340. qib_dev_err(dd,
  341. "Failed to write interface read addr %02X\n",
  342. addr);
  343. ret = 1;
  344. goto bail;
  345. }
  346. ret = qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START);
  347. }
  348. if (ret) {
  349. stop_cmd(dd);
  350. ret = 1;
  351. goto bail;
  352. }
  353. /*
  354. * block devices keeps clocking data out as long as we ack,
  355. * automatically incrementing the address. Some have "pages"
  356. * whose boundaries will not be crossed, but the handling
  357. * of these is left to the caller, who is in a better
  358. * position to know.
  359. */
  360. while (len-- > 0) {
  361. /*
  362. * Get and store data, sending ACK if length remaining,
  363. * else STOP
  364. */
  365. *bp++ = rd_byte(dd, !len);
  366. }
  367. ret = 0;
  368. bail:
  369. return ret;
  370. }
  371. /*
  372. * qib_twsi_blk_wr
  373. * Formerly called qib_eeprom_internal_write, and only used for eeprom,
  374. * but now the general interface for data transfer to twsi devices.
  375. * One vestige of its former role is that it recognizes a device
  376. * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
  377. * which responded to all TWSI device codes, interpreting them as
  378. * address within device. On all other devices found on board handled by
  379. * this driver, the device is followed by a one-byte "address" which selects
  380. * the "register" or "offset" within the device to which data should
  381. * be written.
  382. */
  383. int qib_twsi_blk_wr(struct qib_devdata *dd, int dev, int addr,
  384. const void *buffer, int len)
  385. {
  386. int sub_len;
  387. const u8 *bp = buffer;
  388. int max_wait_time, i;
  389. int ret = 1;
  390. while (len > 0) {
  391. if (dev == QIB_TWSI_NO_DEV) {
  392. if (qib_twsi_wr(dd, (addr << 1) | WRITE_CMD,
  393. QIB_TWSI_START)) {
  394. goto failed_write;
  395. }
  396. } else {
  397. /* Real I2C */
  398. if (qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START))
  399. goto failed_write;
  400. ret = qib_twsi_wr(dd, addr, 0);
  401. if (ret) {
  402. qib_dev_err(dd,
  403. "Failed to write interface write addr %02X\n",
  404. addr);
  405. goto failed_write;
  406. }
  407. }
  408. sub_len = min(len, 4);
  409. addr += sub_len;
  410. len -= sub_len;
  411. for (i = 0; i < sub_len; i++)
  412. if (qib_twsi_wr(dd, *bp++, 0))
  413. goto failed_write;
  414. stop_cmd(dd);
  415. /*
  416. * Wait for write complete by waiting for a successful
  417. * read (the chip replies with a zero after the write
  418. * cmd completes, and before it writes to the eeprom.
  419. * The startcmd for the read will fail the ack until
  420. * the writes have completed. We do this inline to avoid
  421. * the debug prints that are in the real read routine
  422. * if the startcmd fails.
  423. * We also use the proper device address, so it doesn't matter
  424. * whether we have real eeprom_dev. Legacy likes any address.
  425. */
  426. max_wait_time = 100;
  427. while (qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START)) {
  428. stop_cmd(dd);
  429. if (!--max_wait_time)
  430. goto failed_write;
  431. }
  432. /* now read (and ignore) the resulting byte */
  433. rd_byte(dd, 1);
  434. }
  435. ret = 0;
  436. goto bail;
  437. failed_write:
  438. stop_cmd(dd);
  439. ret = 1;
  440. bail:
  441. return ret;
  442. }