qib_twsi.c 13 KB

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