i2c-octeon-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * (C) Copyright 2009-2010
  3. * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
  4. *
  5. * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
  6. *
  7. * This file contains the shared part of the driver for the i2c adapter in
  8. * Cavium Networks' OCTEON processors and ThunderX SOCs.
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include "i2c-octeon-core.h"
  20. /* interrupt service routine */
  21. irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
  22. {
  23. struct octeon_i2c *i2c = dev_id;
  24. i2c->int_disable(i2c);
  25. wake_up(&i2c->queue);
  26. return IRQ_HANDLED;
  27. }
  28. static bool octeon_i2c_test_iflg(struct octeon_i2c *i2c)
  29. {
  30. return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
  31. }
  32. /**
  33. * octeon_i2c_wait - wait for the IFLG to be set
  34. * @i2c: The struct octeon_i2c
  35. *
  36. * Returns 0 on success, otherwise a negative errno.
  37. */
  38. static int octeon_i2c_wait(struct octeon_i2c *i2c)
  39. {
  40. long time_left;
  41. /*
  42. * Some chip revisions don't assert the irq in the interrupt
  43. * controller. So we must poll for the IFLG change.
  44. */
  45. if (i2c->broken_irq_mode) {
  46. u64 end = get_jiffies_64() + i2c->adap.timeout;
  47. while (!octeon_i2c_test_iflg(i2c) &&
  48. time_before64(get_jiffies_64(), end))
  49. usleep_range(I2C_OCTEON_EVENT_WAIT / 2, I2C_OCTEON_EVENT_WAIT);
  50. return octeon_i2c_test_iflg(i2c) ? 0 : -ETIMEDOUT;
  51. }
  52. i2c->int_enable(i2c);
  53. time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
  54. i2c->adap.timeout);
  55. i2c->int_disable(i2c);
  56. if (i2c->broken_irq_check && !time_left &&
  57. octeon_i2c_test_iflg(i2c)) {
  58. dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n");
  59. i2c->broken_irq_mode = true;
  60. return 0;
  61. }
  62. if (!time_left)
  63. return -ETIMEDOUT;
  64. return 0;
  65. }
  66. static bool octeon_i2c_hlc_test_valid(struct octeon_i2c *i2c)
  67. {
  68. return (__raw_readq(i2c->twsi_base + SW_TWSI(i2c)) & SW_TWSI_V) == 0;
  69. }
  70. static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c)
  71. {
  72. /* clear ST/TS events, listen for neither */
  73. octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT);
  74. }
  75. /*
  76. * Cleanup low-level state & enable high-level controller.
  77. */
  78. static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c)
  79. {
  80. int try = 0;
  81. u64 val;
  82. if (i2c->hlc_enabled)
  83. return;
  84. i2c->hlc_enabled = true;
  85. while (1) {
  86. val = octeon_i2c_ctl_read(i2c);
  87. if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP)))
  88. break;
  89. /* clear IFLG event */
  90. if (val & TWSI_CTL_IFLG)
  91. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  92. if (try++ > 100) {
  93. pr_err("%s: giving up\n", __func__);
  94. break;
  95. }
  96. /* spin until any start/stop has finished */
  97. udelay(10);
  98. }
  99. octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB);
  100. }
  101. static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c)
  102. {
  103. if (!i2c->hlc_enabled)
  104. return;
  105. i2c->hlc_enabled = false;
  106. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  107. }
  108. /**
  109. * octeon_i2c_hlc_wait - wait for an HLC operation to complete
  110. * @i2c: The struct octeon_i2c
  111. *
  112. * Returns 0 on success, otherwise -ETIMEDOUT.
  113. */
  114. static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c)
  115. {
  116. int time_left;
  117. /*
  118. * Some cn38xx boards don't assert the irq in the interrupt
  119. * controller. So we must poll for the valid bit change.
  120. */
  121. if (i2c->broken_irq_mode) {
  122. u64 end = get_jiffies_64() + i2c->adap.timeout;
  123. while (!octeon_i2c_hlc_test_valid(i2c) &&
  124. time_before64(get_jiffies_64(), end))
  125. usleep_range(I2C_OCTEON_EVENT_WAIT / 2, I2C_OCTEON_EVENT_WAIT);
  126. return octeon_i2c_hlc_test_valid(i2c) ? 0 : -ETIMEDOUT;
  127. }
  128. i2c->hlc_int_enable(i2c);
  129. time_left = wait_event_timeout(i2c->queue,
  130. octeon_i2c_hlc_test_valid(i2c),
  131. i2c->adap.timeout);
  132. i2c->hlc_int_disable(i2c);
  133. if (!time_left)
  134. octeon_i2c_hlc_int_clear(i2c);
  135. if (i2c->broken_irq_check && !time_left &&
  136. octeon_i2c_hlc_test_valid(i2c)) {
  137. dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n");
  138. i2c->broken_irq_mode = true;
  139. return 0;
  140. }
  141. if (!time_left)
  142. return -ETIMEDOUT;
  143. return 0;
  144. }
  145. static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
  146. {
  147. u8 stat;
  148. /*
  149. * This is ugly... in HLC mode the status is not in the status register
  150. * but in the lower 8 bits of SW_TWSI.
  151. */
  152. if (i2c->hlc_enabled)
  153. stat = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  154. else
  155. stat = octeon_i2c_stat_read(i2c);
  156. switch (stat) {
  157. /* Everything is fine */
  158. case STAT_IDLE:
  159. case STAT_AD2W_ACK:
  160. case STAT_RXADDR_ACK:
  161. case STAT_TXADDR_ACK:
  162. case STAT_TXDATA_ACK:
  163. return 0;
  164. /* ACK allowed on pre-terminal bytes only */
  165. case STAT_RXDATA_ACK:
  166. if (!final_read)
  167. return 0;
  168. return -EIO;
  169. /* NAK allowed on terminal byte only */
  170. case STAT_RXDATA_NAK:
  171. if (final_read)
  172. return 0;
  173. return -EIO;
  174. /* Arbitration lost */
  175. case STAT_LOST_ARB_38:
  176. case STAT_LOST_ARB_68:
  177. case STAT_LOST_ARB_78:
  178. case STAT_LOST_ARB_B0:
  179. return -EAGAIN;
  180. /* Being addressed as slave, should back off & listen */
  181. case STAT_SLAVE_60:
  182. case STAT_SLAVE_70:
  183. case STAT_GENDATA_ACK:
  184. case STAT_GENDATA_NAK:
  185. return -EOPNOTSUPP;
  186. /* Core busy as slave */
  187. case STAT_SLAVE_80:
  188. case STAT_SLAVE_88:
  189. case STAT_SLAVE_A0:
  190. case STAT_SLAVE_A8:
  191. case STAT_SLAVE_LOST:
  192. case STAT_SLAVE_NAK:
  193. case STAT_SLAVE_ACK:
  194. return -EOPNOTSUPP;
  195. case STAT_TXDATA_NAK:
  196. case STAT_BUS_ERROR:
  197. return -EIO;
  198. case STAT_TXADDR_NAK:
  199. case STAT_RXADDR_NAK:
  200. case STAT_AD2W_NAK:
  201. return -ENXIO;
  202. default:
  203. dev_err(i2c->dev, "unhandled state: %d\n", stat);
  204. return -EIO;
  205. }
  206. }
  207. static int octeon_i2c_recovery(struct octeon_i2c *i2c)
  208. {
  209. int ret;
  210. ret = i2c_recover_bus(&i2c->adap);
  211. if (ret)
  212. /* recover failed, try hardware re-init */
  213. ret = octeon_i2c_init_lowlevel(i2c);
  214. return ret;
  215. }
  216. /**
  217. * octeon_i2c_start - send START to the bus
  218. * @i2c: The struct octeon_i2c
  219. *
  220. * Returns 0 on success, otherwise a negative errno.
  221. */
  222. static int octeon_i2c_start(struct octeon_i2c *i2c)
  223. {
  224. int ret;
  225. u8 stat;
  226. octeon_i2c_hlc_disable(i2c);
  227. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
  228. ret = octeon_i2c_wait(i2c);
  229. if (ret)
  230. goto error;
  231. stat = octeon_i2c_stat_read(i2c);
  232. if (stat == STAT_START || stat == STAT_REP_START)
  233. /* START successful, bail out */
  234. return 0;
  235. error:
  236. /* START failed, try to recover */
  237. ret = octeon_i2c_recovery(i2c);
  238. return (ret) ? ret : -EAGAIN;
  239. }
  240. /* send STOP to the bus */
  241. static void octeon_i2c_stop(struct octeon_i2c *i2c)
  242. {
  243. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
  244. }
  245. /**
  246. * octeon_i2c_read - receive data from the bus via low-level controller
  247. * @i2c: The struct octeon_i2c
  248. * @target: Target address
  249. * @data: Pointer to the location to store the data
  250. * @rlength: Length of the data
  251. * @recv_len: flag for length byte
  252. *
  253. * The address is sent over the bus, then the data is read.
  254. *
  255. * Returns 0 on success, otherwise a negative errno.
  256. */
  257. static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
  258. u8 *data, u16 *rlength, bool recv_len)
  259. {
  260. int i, result, length = *rlength;
  261. bool final_read = false;
  262. octeon_i2c_data_write(i2c, (target << 1) | 1);
  263. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  264. result = octeon_i2c_wait(i2c);
  265. if (result)
  266. return result;
  267. /* address OK ? */
  268. result = octeon_i2c_check_status(i2c, false);
  269. if (result)
  270. return result;
  271. for (i = 0; i < length; i++) {
  272. /*
  273. * For the last byte to receive TWSI_CTL_AAK must not be set.
  274. *
  275. * A special case is I2C_M_RECV_LEN where we don't know the
  276. * additional length yet. If recv_len is set we assume we're
  277. * not reading the final byte and therefore need to set
  278. * TWSI_CTL_AAK.
  279. */
  280. if ((i + 1 == length) && !(recv_len && i == 0))
  281. final_read = true;
  282. /* clear iflg to allow next event */
  283. if (final_read)
  284. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  285. else
  286. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
  287. result = octeon_i2c_wait(i2c);
  288. if (result)
  289. return result;
  290. data[i] = octeon_i2c_data_read(i2c, &result);
  291. if (result)
  292. return result;
  293. if (recv_len && i == 0) {
  294. if (data[i] > I2C_SMBUS_BLOCK_MAX + 1)
  295. return -EPROTO;
  296. length += data[i];
  297. }
  298. result = octeon_i2c_check_status(i2c, final_read);
  299. if (result)
  300. return result;
  301. }
  302. *rlength = length;
  303. return 0;
  304. }
  305. /**
  306. * octeon_i2c_write - send data to the bus via low-level controller
  307. * @i2c: The struct octeon_i2c
  308. * @target: Target address
  309. * @data: Pointer to the data to be sent
  310. * @length: Length of the data
  311. *
  312. * The address is sent over the bus, then the data.
  313. *
  314. * Returns 0 on success, otherwise a negative errno.
  315. */
  316. static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
  317. const u8 *data, int length)
  318. {
  319. int i, result;
  320. octeon_i2c_data_write(i2c, target << 1);
  321. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  322. result = octeon_i2c_wait(i2c);
  323. if (result)
  324. return result;
  325. for (i = 0; i < length; i++) {
  326. result = octeon_i2c_check_status(i2c, false);
  327. if (result)
  328. return result;
  329. octeon_i2c_data_write(i2c, data[i]);
  330. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  331. result = octeon_i2c_wait(i2c);
  332. if (result)
  333. return result;
  334. }
  335. return 0;
  336. }
  337. /* high-level-controller pure read of up to 8 bytes */
  338. static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
  339. {
  340. int i, j, ret = 0;
  341. u64 cmd;
  342. octeon_i2c_hlc_enable(i2c);
  343. octeon_i2c_hlc_int_clear(i2c);
  344. cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
  345. /* SIZE */
  346. cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
  347. /* A */
  348. cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
  349. if (msgs[0].flags & I2C_M_TEN)
  350. cmd |= SW_TWSI_OP_10;
  351. else
  352. cmd |= SW_TWSI_OP_7;
  353. octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c));
  354. ret = octeon_i2c_hlc_wait(i2c);
  355. if (ret)
  356. goto err;
  357. cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  358. if ((cmd & SW_TWSI_R) == 0)
  359. return octeon_i2c_check_status(i2c, false);
  360. for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
  361. msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
  362. if (msgs[0].len > 4) {
  363. cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT(i2c));
  364. for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
  365. msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
  366. }
  367. err:
  368. return ret;
  369. }
  370. /* high-level-controller pure write of up to 8 bytes */
  371. static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
  372. {
  373. int i, j, ret = 0;
  374. u64 cmd;
  375. octeon_i2c_hlc_enable(i2c);
  376. octeon_i2c_hlc_int_clear(i2c);
  377. cmd = SW_TWSI_V | SW_TWSI_SOVR;
  378. /* SIZE */
  379. cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
  380. /* A */
  381. cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
  382. if (msgs[0].flags & I2C_M_TEN)
  383. cmd |= SW_TWSI_OP_10;
  384. else
  385. cmd |= SW_TWSI_OP_7;
  386. for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
  387. cmd |= (u64)msgs[0].buf[j] << (8 * i);
  388. if (msgs[0].len > 4) {
  389. u64 ext = 0;
  390. for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
  391. ext |= (u64)msgs[0].buf[j] << (8 * i);
  392. octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c));
  393. }
  394. octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c));
  395. ret = octeon_i2c_hlc_wait(i2c);
  396. if (ret)
  397. goto err;
  398. cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  399. if ((cmd & SW_TWSI_R) == 0)
  400. return octeon_i2c_check_status(i2c, false);
  401. err:
  402. return ret;
  403. }
  404. /* high-level-controller composite write+read, msg0=addr, msg1=data */
  405. static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
  406. {
  407. int i, j, ret = 0;
  408. u64 cmd;
  409. octeon_i2c_hlc_enable(i2c);
  410. cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
  411. /* SIZE */
  412. cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
  413. /* A */
  414. cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
  415. if (msgs[0].flags & I2C_M_TEN)
  416. cmd |= SW_TWSI_OP_10_IA;
  417. else
  418. cmd |= SW_TWSI_OP_7_IA;
  419. if (msgs[0].len == 2) {
  420. u64 ext = 0;
  421. cmd |= SW_TWSI_EIA;
  422. ext = (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
  423. cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
  424. octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c));
  425. } else {
  426. cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
  427. }
  428. octeon_i2c_hlc_int_clear(i2c);
  429. octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c));
  430. ret = octeon_i2c_hlc_wait(i2c);
  431. if (ret)
  432. goto err;
  433. cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  434. if ((cmd & SW_TWSI_R) == 0)
  435. return octeon_i2c_check_status(i2c, false);
  436. for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
  437. msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
  438. if (msgs[1].len > 4) {
  439. cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT(i2c));
  440. for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
  441. msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
  442. }
  443. err:
  444. return ret;
  445. }
  446. /* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */
  447. static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
  448. {
  449. bool set_ext = false;
  450. int i, j, ret = 0;
  451. u64 cmd, ext = 0;
  452. octeon_i2c_hlc_enable(i2c);
  453. cmd = SW_TWSI_V | SW_TWSI_SOVR;
  454. /* SIZE */
  455. cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
  456. /* A */
  457. cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
  458. if (msgs[0].flags & I2C_M_TEN)
  459. cmd |= SW_TWSI_OP_10_IA;
  460. else
  461. cmd |= SW_TWSI_OP_7_IA;
  462. if (msgs[0].len == 2) {
  463. cmd |= SW_TWSI_EIA;
  464. ext |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
  465. set_ext = true;
  466. cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
  467. } else {
  468. cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
  469. }
  470. for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
  471. cmd |= (u64)msgs[1].buf[j] << (8 * i);
  472. if (msgs[1].len > 4) {
  473. for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
  474. ext |= (u64)msgs[1].buf[j] << (8 * i);
  475. set_ext = true;
  476. }
  477. if (set_ext)
  478. octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c));
  479. octeon_i2c_hlc_int_clear(i2c);
  480. octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c));
  481. ret = octeon_i2c_hlc_wait(i2c);
  482. if (ret)
  483. goto err;
  484. cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c));
  485. if ((cmd & SW_TWSI_R) == 0)
  486. return octeon_i2c_check_status(i2c, false);
  487. err:
  488. return ret;
  489. }
  490. /**
  491. * octeon_i2c_xfer - The driver's master_xfer function
  492. * @adap: Pointer to the i2c_adapter structure
  493. * @msgs: Pointer to the messages to be processed
  494. * @num: Length of the MSGS array
  495. *
  496. * Returns the number of messages processed, or a negative errno on failure.
  497. */
  498. int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  499. {
  500. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  501. int i, ret = 0;
  502. if (num == 1) {
  503. if (msgs[0].len > 0 && msgs[0].len <= 8) {
  504. if (msgs[0].flags & I2C_M_RD)
  505. ret = octeon_i2c_hlc_read(i2c, msgs);
  506. else
  507. ret = octeon_i2c_hlc_write(i2c, msgs);
  508. goto out;
  509. }
  510. } else if (num == 2) {
  511. if ((msgs[0].flags & I2C_M_RD) == 0 &&
  512. (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
  513. msgs[0].len > 0 && msgs[0].len <= 2 &&
  514. msgs[1].len > 0 && msgs[1].len <= 8 &&
  515. msgs[0].addr == msgs[1].addr) {
  516. if (msgs[1].flags & I2C_M_RD)
  517. ret = octeon_i2c_hlc_comp_read(i2c, msgs);
  518. else
  519. ret = octeon_i2c_hlc_comp_write(i2c, msgs);
  520. goto out;
  521. }
  522. }
  523. for (i = 0; ret == 0 && i < num; i++) {
  524. struct i2c_msg *pmsg = &msgs[i];
  525. /* zero-length messages are not supported */
  526. if (!pmsg->len) {
  527. ret = -EOPNOTSUPP;
  528. break;
  529. }
  530. ret = octeon_i2c_start(i2c);
  531. if (ret)
  532. return ret;
  533. if (pmsg->flags & I2C_M_RD)
  534. ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
  535. &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
  536. else
  537. ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
  538. pmsg->len);
  539. }
  540. octeon_i2c_stop(i2c);
  541. out:
  542. return (ret != 0) ? ret : num;
  543. }
  544. /* calculate and set clock divisors */
  545. void octeon_i2c_set_clock(struct octeon_i2c *i2c)
  546. {
  547. int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
  548. int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
  549. for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
  550. /*
  551. * An mdiv value of less than 2 seems to not work well
  552. * with ds1337 RTCs, so we constrain it to larger values.
  553. */
  554. for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
  555. /*
  556. * For given ndiv and mdiv values check the
  557. * two closest thp values.
  558. */
  559. tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
  560. tclk *= (1 << ndiv_idx);
  561. thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
  562. for (inc = 0; inc <= 1; inc++) {
  563. thp_idx = thp_base + inc;
  564. if (thp_idx < 5 || thp_idx > 0xff)
  565. continue;
  566. foscl = i2c->sys_freq / (2 * (thp_idx + 1));
  567. foscl = foscl / (1 << ndiv_idx);
  568. foscl = foscl / (mdiv_idx + 1) / 10;
  569. diff = abs(foscl - i2c->twsi_freq);
  570. if (diff < delta_hz) {
  571. delta_hz = diff;
  572. thp = thp_idx;
  573. mdiv = mdiv_idx;
  574. ndiv = ndiv_idx;
  575. }
  576. }
  577. }
  578. }
  579. octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
  580. octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
  581. }
  582. int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
  583. {
  584. u8 status = 0;
  585. int tries;
  586. /* reset controller */
  587. octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
  588. for (tries = 10; tries && status != STAT_IDLE; tries--) {
  589. udelay(1);
  590. status = octeon_i2c_stat_read(i2c);
  591. if (status == STAT_IDLE)
  592. break;
  593. }
  594. if (status != STAT_IDLE) {
  595. dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n",
  596. __func__, status);
  597. return -EIO;
  598. }
  599. /* toggle twice to force both teardowns */
  600. octeon_i2c_hlc_enable(i2c);
  601. octeon_i2c_hlc_disable(i2c);
  602. return 0;
  603. }
  604. static int octeon_i2c_get_scl(struct i2c_adapter *adap)
  605. {
  606. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  607. u64 state;
  608. state = octeon_i2c_read_int(i2c);
  609. return state & TWSI_INT_SCL;
  610. }
  611. static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
  612. {
  613. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  614. octeon_i2c_write_int(i2c, val ? 0 : TWSI_INT_SCL_OVR);
  615. }
  616. static int octeon_i2c_get_sda(struct i2c_adapter *adap)
  617. {
  618. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  619. u64 state;
  620. state = octeon_i2c_read_int(i2c);
  621. return state & TWSI_INT_SDA;
  622. }
  623. static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
  624. {
  625. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  626. octeon_i2c_hlc_disable(i2c);
  627. octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
  628. /* wait for software reset to settle */
  629. udelay(5);
  630. /*
  631. * Bring control register to a good state regardless
  632. * of HLC state.
  633. */
  634. octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
  635. octeon_i2c_write_int(i2c, 0);
  636. }
  637. static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
  638. {
  639. struct octeon_i2c *i2c = i2c_get_adapdata(adap);
  640. /*
  641. * Generate STOP to finish the unfinished transaction.
  642. * Can't generate STOP via the TWSI CTL register
  643. * since it could bring the TWSI controller into an inoperable state.
  644. */
  645. octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
  646. udelay(5);
  647. octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
  648. udelay(5);
  649. octeon_i2c_write_int(i2c, 0);
  650. }
  651. struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
  652. .recover_bus = i2c_generic_scl_recovery,
  653. .get_scl = octeon_i2c_get_scl,
  654. .set_scl = octeon_i2c_set_scl,
  655. .get_sda = octeon_i2c_get_sda,
  656. .prepare_recovery = octeon_i2c_prepare_recovery,
  657. .unprepare_recovery = octeon_i2c_unprepare_recovery,
  658. };