i2c-octeon-core.c 21 KB

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