qib_twsi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. *
  149. * Returns byte shifted out of device
  150. */
  151. static int rd_byte(struct qib_devdata *dd, int last)
  152. {
  153. int bit_cntr, data;
  154. data = 0;
  155. for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {
  156. data <<= 1;
  157. scl_out(dd, 1);
  158. data |= sda_in(dd, 0);
  159. scl_out(dd, 0);
  160. }
  161. if (last) {
  162. scl_out(dd, 1);
  163. stop_cmd(dd);
  164. } else {
  165. sda_out(dd, 0);
  166. scl_out(dd, 1);
  167. scl_out(dd, 0);
  168. sda_out(dd, 1);
  169. }
  170. return data;
  171. }
  172. /**
  173. * wr_byte - write a byte, one bit at a time
  174. * @dd: the qlogic_ib device
  175. * @data: the byte to write
  176. *
  177. * Returns 0 if we got the following ack, otherwise 1
  178. */
  179. static int wr_byte(struct qib_devdata *dd, u8 data)
  180. {
  181. int bit_cntr;
  182. u8 bit;
  183. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  184. bit = (data >> bit_cntr) & 1;
  185. sda_out(dd, bit);
  186. scl_out(dd, 1);
  187. scl_out(dd, 0);
  188. }
  189. return (!i2c_ackrcv(dd)) ? 1 : 0;
  190. }
  191. /*
  192. * issue TWSI start sequence:
  193. * (both clock/data high, clock high, data low while clock is high)
  194. */
  195. static void start_seq(struct qib_devdata *dd)
  196. {
  197. sda_out(dd, 1);
  198. scl_out(dd, 1);
  199. sda_out(dd, 0);
  200. udelay(1);
  201. scl_out(dd, 0);
  202. }
  203. /**
  204. * stop_seq - transmit the stop sequence
  205. * @dd: the qlogic_ib device
  206. *
  207. * (both clock/data low, clock high, data high while clock is high)
  208. */
  209. static void stop_seq(struct qib_devdata *dd)
  210. {
  211. scl_out(dd, 0);
  212. sda_out(dd, 0);
  213. scl_out(dd, 1);
  214. sda_out(dd, 1);
  215. }
  216. /**
  217. * stop_cmd - transmit the stop condition
  218. * @dd: the qlogic_ib device
  219. *
  220. * (both clock/data low, clock high, data high while clock is high)
  221. */
  222. static void stop_cmd(struct qib_devdata *dd)
  223. {
  224. stop_seq(dd);
  225. udelay(TWSI_BUF_WAIT_USEC);
  226. }
  227. /**
  228. * qib_twsi_reset - reset I2C communication
  229. * @dd: the qlogic_ib device
  230. */
  231. int qib_twsi_reset(struct qib_devdata *dd)
  232. {
  233. int clock_cycles_left = 9;
  234. int was_high = 0;
  235. u32 pins, mask;
  236. /* Both SCL and SDA should be high. If not, there
  237. * is something wrong.
  238. */
  239. mask = (1UL << dd->gpio_scl_num) | (1UL << dd->gpio_sda_num);
  240. /*
  241. * Force pins to desired innocuous state.
  242. * This is the default power-on state with out=0 and dir=0,
  243. * So tri-stated and should be floating high (barring HW problems)
  244. */
  245. dd->f_gpio_mod(dd, 0, 0, mask);
  246. /*
  247. * Clock nine times to get all listeners into a sane state.
  248. * If SDA does not go high at any point, we are wedged.
  249. * One vendor recommends then issuing START followed by STOP.
  250. * we cannot use our "normal" functions to do that, because
  251. * if SCL drops between them, another vendor's part will
  252. * wedge, dropping SDA and keeping it low forever, at the end of
  253. * the next transaction (even if it was not the device addressed).
  254. * So our START and STOP take place with SCL held high.
  255. */
  256. while (clock_cycles_left--) {
  257. scl_out(dd, 0);
  258. scl_out(dd, 1);
  259. /* Note if SDA is high, but keep clocking to sync slave */
  260. was_high |= sda_in(dd, 0);
  261. }
  262. if (was_high) {
  263. /*
  264. * We saw a high, which we hope means the slave is sync'd.
  265. * Issue START, STOP, pause for T_BUF.
  266. */
  267. pins = dd->f_gpio_mod(dd, 0, 0, 0);
  268. if ((pins & mask) != mask)
  269. qib_dev_err(dd, "GPIO pins not at rest: %d\n",
  270. pins & mask);
  271. /* Drop SDA to issue START */
  272. udelay(1); /* Guarantee .6 uSec setup */
  273. sda_out(dd, 0);
  274. udelay(1); /* Guarantee .6 uSec hold */
  275. /* At this point, SCL is high, SDA low. Raise SDA for STOP */
  276. sda_out(dd, 1);
  277. udelay(TWSI_BUF_WAIT_USEC);
  278. }
  279. return !was_high;
  280. }
  281. #define QIB_TWSI_START 0x100
  282. #define QIB_TWSI_STOP 0x200
  283. /* Write byte to TWSI, optionally prefixed with START or suffixed with
  284. * STOP.
  285. * returns 0 if OK (ACK received), else != 0
  286. */
  287. static int qib_twsi_wr(struct qib_devdata *dd, int data, int flags)
  288. {
  289. int ret = 1;
  290. if (flags & QIB_TWSI_START)
  291. start_seq(dd);
  292. ret = wr_byte(dd, data); /* Leaves SCL low (from i2c_ackrcv()) */
  293. if (flags & QIB_TWSI_STOP)
  294. stop_cmd(dd);
  295. return ret;
  296. }
  297. /* Added functionality for IBA7220-based cards */
  298. #define QIB_TEMP_DEV 0x98
  299. /*
  300. * qib_twsi_blk_rd
  301. * Formerly called qib_eeprom_internal_read, and only used for eeprom,
  302. * but now the general interface for data transfer from twsi devices.
  303. * One vestige of its former role is that it recognizes a device
  304. * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
  305. * which responded to all TWSI device codes, interpreting them as
  306. * address within device. On all other devices found on board handled by
  307. * this driver, the device is followed by a one-byte "address" which selects
  308. * the "register" or "offset" within the device from which data should
  309. * be read.
  310. */
  311. int qib_twsi_blk_rd(struct qib_devdata *dd, int dev, int addr,
  312. void *buffer, int len)
  313. {
  314. int ret;
  315. u8 *bp = buffer;
  316. ret = 1;
  317. if (dev == QIB_TWSI_NO_DEV) {
  318. /* legacy not-really-I2C */
  319. addr = (addr << 1) | READ_CMD;
  320. ret = qib_twsi_wr(dd, addr, QIB_TWSI_START);
  321. } else {
  322. /* Actual I2C */
  323. ret = qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START);
  324. if (ret) {
  325. stop_cmd(dd);
  326. ret = 1;
  327. goto bail;
  328. }
  329. /*
  330. * SFF spec claims we do _not_ stop after the addr
  331. * but simply issue a start with the "read" dev-addr.
  332. * Since we are implicitely waiting for ACK here,
  333. * we need t_buf (nominally 20uSec) before that start,
  334. * and cannot rely on the delay built in to the STOP
  335. */
  336. ret = qib_twsi_wr(dd, addr, 0);
  337. udelay(TWSI_BUF_WAIT_USEC);
  338. if (ret) {
  339. qib_dev_err(dd,
  340. "Failed to write interface read addr %02X\n",
  341. addr);
  342. ret = 1;
  343. goto bail;
  344. }
  345. ret = qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START);
  346. }
  347. if (ret) {
  348. stop_cmd(dd);
  349. ret = 1;
  350. goto bail;
  351. }
  352. /*
  353. * block devices keeps clocking data out as long as we ack,
  354. * automatically incrementing the address. Some have "pages"
  355. * whose boundaries will not be crossed, but the handling
  356. * of these is left to the caller, who is in a better
  357. * position to know.
  358. */
  359. while (len-- > 0) {
  360. /*
  361. * Get and store data, sending ACK if length remaining,
  362. * else STOP
  363. */
  364. *bp++ = rd_byte(dd, !len);
  365. }
  366. ret = 0;
  367. bail:
  368. return ret;
  369. }
  370. /*
  371. * qib_twsi_blk_wr
  372. * Formerly called qib_eeprom_internal_write, and only used for eeprom,
  373. * but now the general interface for data transfer to twsi devices.
  374. * One vestige of its former role is that it recognizes a device
  375. * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
  376. * which responded to all TWSI device codes, interpreting them as
  377. * address within device. On all other devices found on board handled by
  378. * this driver, the device is followed by a one-byte "address" which selects
  379. * the "register" or "offset" within the device to which data should
  380. * be written.
  381. */
  382. int qib_twsi_blk_wr(struct qib_devdata *dd, int dev, int addr,
  383. const void *buffer, int len)
  384. {
  385. int sub_len;
  386. const u8 *bp = buffer;
  387. int max_wait_time, i;
  388. int ret = 1;
  389. while (len > 0) {
  390. if (dev == QIB_TWSI_NO_DEV) {
  391. if (qib_twsi_wr(dd, (addr << 1) | WRITE_CMD,
  392. QIB_TWSI_START)) {
  393. goto failed_write;
  394. }
  395. } else {
  396. /* Real I2C */
  397. if (qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START))
  398. goto failed_write;
  399. ret = qib_twsi_wr(dd, addr, 0);
  400. if (ret) {
  401. qib_dev_err(dd,
  402. "Failed to write interface write addr %02X\n",
  403. addr);
  404. goto failed_write;
  405. }
  406. }
  407. sub_len = min(len, 4);
  408. addr += sub_len;
  409. len -= sub_len;
  410. for (i = 0; i < sub_len; i++)
  411. if (qib_twsi_wr(dd, *bp++, 0))
  412. goto failed_write;
  413. stop_cmd(dd);
  414. /*
  415. * Wait for write complete by waiting for a successful
  416. * read (the chip replies with a zero after the write
  417. * cmd completes, and before it writes to the eeprom.
  418. * The startcmd for the read will fail the ack until
  419. * the writes have completed. We do this inline to avoid
  420. * the debug prints that are in the real read routine
  421. * if the startcmd fails.
  422. * We also use the proper device address, so it doesn't matter
  423. * whether we have real eeprom_dev. Legacy likes any address.
  424. */
  425. max_wait_time = 100;
  426. while (qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START)) {
  427. stop_cmd(dd);
  428. if (!--max_wait_time)
  429. goto failed_write;
  430. }
  431. /* now read (and ignore) the resulting byte */
  432. rd_byte(dd, 1);
  433. }
  434. ret = 0;
  435. goto bail;
  436. failed_write:
  437. stop_cmd(dd);
  438. ret = 1;
  439. bail:
  440. return ret;
  441. }