mvtwsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for the TWSI (i2c) controller found on the Marvell
  4. * orion5x and kirkwood SoC families.
  5. *
  6. * Author: Albert Aribaud <albert.u.boot@aribaud.net>
  7. * Copyright (c) 2010 Albert Aribaud.
  8. */
  9. #include <common.h>
  10. #include <i2c.h>
  11. #include <linux/errno.h>
  12. #include <asm/io.h>
  13. #include <linux/bitops.h>
  14. #include <linux/compat.h>
  15. #ifdef CONFIG_DM_I2C
  16. #include <dm.h>
  17. #endif
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /*
  20. * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
  21. * settings
  22. */
  23. #ifndef CONFIG_DM_I2C
  24. #if defined(CONFIG_ORION5X)
  25. #include <asm/arch/orion5x.h>
  26. #elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
  27. #include <asm/arch/soc.h>
  28. #elif defined(CONFIG_ARCH_SUNXI)
  29. #include <asm/arch/i2c.h>
  30. #else
  31. #error Driver mvtwsi not supported by SoC or board
  32. #endif
  33. #endif /* CONFIG_DM_I2C */
  34. /*
  35. * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
  36. * always have it.
  37. */
  38. #if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
  39. #include <asm/arch/i2c.h>
  40. #endif
  41. /*
  42. * TWSI register structure
  43. */
  44. #ifdef CONFIG_ARCH_SUNXI
  45. struct mvtwsi_registers {
  46. u32 slave_address;
  47. u32 xtnd_slave_addr;
  48. u32 data;
  49. u32 control;
  50. u32 status;
  51. u32 baudrate;
  52. u32 soft_reset;
  53. u32 debug; /* Dummy field for build compatibility with mvebu */
  54. };
  55. #else
  56. struct mvtwsi_registers {
  57. u32 slave_address;
  58. u32 data;
  59. u32 control;
  60. union {
  61. u32 status; /* When reading */
  62. u32 baudrate; /* When writing */
  63. };
  64. u32 xtnd_slave_addr;
  65. u32 reserved0[2];
  66. u32 soft_reset;
  67. u32 reserved1[27];
  68. u32 debug;
  69. };
  70. #endif
  71. #ifdef CONFIG_DM_I2C
  72. struct mvtwsi_i2c_dev {
  73. /* TWSI Register base for the device */
  74. struct mvtwsi_registers *base;
  75. /* Number of the device (determined from cell-index property) */
  76. int index;
  77. /* The I2C slave address for the device */
  78. u8 slaveadd;
  79. /* The configured I2C speed in Hz */
  80. uint speed;
  81. /* The current length of a clock period (depending on speed) */
  82. uint tick;
  83. };
  84. #endif /* CONFIG_DM_I2C */
  85. /*
  86. * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
  87. * register
  88. */
  89. enum mvtwsi_ctrl_register_fields {
  90. /* Acknowledge bit */
  91. MVTWSI_CONTROL_ACK = 0x00000004,
  92. /* Interrupt flag */
  93. MVTWSI_CONTROL_IFLG = 0x00000008,
  94. /* Stop bit */
  95. MVTWSI_CONTROL_STOP = 0x00000010,
  96. /* Start bit */
  97. MVTWSI_CONTROL_START = 0x00000020,
  98. /* I2C enable */
  99. MVTWSI_CONTROL_TWSIEN = 0x00000040,
  100. /* Interrupt enable */
  101. MVTWSI_CONTROL_INTEN = 0x00000080,
  102. };
  103. /*
  104. * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
  105. * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
  106. */
  107. #ifdef CONFIG_SUNXI_GEN_SUN6I
  108. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
  109. #else
  110. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
  111. #endif
  112. /*
  113. * enum mvstwsi_status_values - Possible values of I2C controller's status
  114. * register
  115. *
  116. * Only those statuses expected in normal master operation on
  117. * non-10-bit-address devices are specified.
  118. *
  119. * Every status that's unexpected during normal operation (bus errors,
  120. * arbitration losses, missing ACKs...) is passed back to the caller as an error
  121. * code.
  122. */
  123. enum mvstwsi_status_values {
  124. /* START condition transmitted */
  125. MVTWSI_STATUS_START = 0x08,
  126. /* Repeated START condition transmitted */
  127. MVTWSI_STATUS_REPEATED_START = 0x10,
  128. /* Address + write bit transmitted, ACK received */
  129. MVTWSI_STATUS_ADDR_W_ACK = 0x18,
  130. /* Data transmitted, ACK received */
  131. MVTWSI_STATUS_DATA_W_ACK = 0x28,
  132. /* Address + read bit transmitted, ACK received */
  133. MVTWSI_STATUS_ADDR_R_ACK = 0x40,
  134. /* Address + read bit transmitted, ACK not received */
  135. MVTWSI_STATUS_ADDR_R_NAK = 0x48,
  136. /* Data received, ACK transmitted */
  137. MVTWSI_STATUS_DATA_R_ACK = 0x50,
  138. /* Data received, ACK not transmitted */
  139. MVTWSI_STATUS_DATA_R_NAK = 0x58,
  140. /* No relevant status */
  141. MVTWSI_STATUS_IDLE = 0xF8,
  142. };
  143. /*
  144. * enum mvstwsi_ack_flags - Determine whether a read byte should be
  145. * acknowledged or not.
  146. */
  147. enum mvtwsi_ack_flags {
  148. /* Send NAK after received byte */
  149. MVTWSI_READ_NAK = 0,
  150. /* Send ACK after received byte */
  151. MVTWSI_READ_ACK = 1,
  152. };
  153. /*
  154. * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
  155. *
  156. * @speed: The speed in Hz to calculate the clock cycle duration for.
  157. * @return The duration of a clock cycle in ns.
  158. */
  159. inline uint calc_tick(uint speed)
  160. {
  161. /* One tick = the duration of a period at the specified speed in ns (we
  162. * add 100 ns to be on the safe side) */
  163. return (1000000000u / speed) + 100;
  164. }
  165. #ifndef CONFIG_DM_I2C
  166. /*
  167. * twsi_get_base() - Get controller register base for specified adapter
  168. *
  169. * @adap: Adapter to get the register base for.
  170. * @return Register base for the specified adapter.
  171. */
  172. static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
  173. {
  174. switch (adap->hwadapnr) {
  175. #ifdef CONFIG_I2C_MVTWSI_BASE0
  176. case 0:
  177. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
  178. #endif
  179. #ifdef CONFIG_I2C_MVTWSI_BASE1
  180. case 1:
  181. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
  182. #endif
  183. #ifdef CONFIG_I2C_MVTWSI_BASE2
  184. case 2:
  185. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
  186. #endif
  187. #ifdef CONFIG_I2C_MVTWSI_BASE3
  188. case 3:
  189. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
  190. #endif
  191. #ifdef CONFIG_I2C_MVTWSI_BASE4
  192. case 4:
  193. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
  194. #endif
  195. #ifdef CONFIG_I2C_MVTWSI_BASE5
  196. case 5:
  197. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
  198. #endif
  199. default:
  200. printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
  201. break;
  202. }
  203. return NULL;
  204. }
  205. #endif
  206. /*
  207. * enum mvtwsi_error_class - types of I2C errors
  208. */
  209. enum mvtwsi_error_class {
  210. /* The controller returned a different status than expected */
  211. MVTWSI_ERROR_WRONG_STATUS = 0x01,
  212. /* The controller timed out */
  213. MVTWSI_ERROR_TIMEOUT = 0x02,
  214. };
  215. /*
  216. * mvtwsi_error() - Build I2C return code from error information
  217. *
  218. * For debugging purposes, this function packs some information of an occurred
  219. * error into a return code. These error codes are returned from I2C API
  220. * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
  221. *
  222. * @ec: The error class of the error (enum mvtwsi_error_class).
  223. * @lc: The last value of the control register.
  224. * @ls: The last value of the status register.
  225. * @es: The expected value of the status register.
  226. * @return The generated error code.
  227. */
  228. inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
  229. {
  230. return ((ec << 24) & 0xFF000000)
  231. | ((lc << 16) & 0x00FF0000)
  232. | ((ls << 8) & 0x0000FF00)
  233. | (es & 0xFF);
  234. }
  235. /*
  236. * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
  237. *
  238. * @return Zero if status is as expected, or a non-zero code if either a time
  239. * out occurred, or the status was not the expected one.
  240. */
  241. static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
  242. uint tick)
  243. {
  244. int control, status;
  245. int timeout = 1000;
  246. do {
  247. control = readl(&twsi->control);
  248. if (control & MVTWSI_CONTROL_IFLG) {
  249. status = readl(&twsi->status);
  250. if (status == expected_status)
  251. return 0;
  252. else
  253. return mvtwsi_error(
  254. MVTWSI_ERROR_WRONG_STATUS,
  255. control, status, expected_status);
  256. }
  257. ndelay(tick); /* One clock cycle */
  258. } while (timeout--);
  259. status = readl(&twsi->status);
  260. return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
  261. expected_status);
  262. }
  263. /*
  264. * twsi_start() - Assert a START condition on the bus.
  265. *
  266. * This function is used in both single I2C transactions and inside
  267. * back-to-back transactions (repeated starts).
  268. *
  269. * @twsi: The MVTWSI register structure to use.
  270. * @expected_status: The I2C bus status expected to be asserted after the
  271. * operation completion.
  272. * @tick: The duration of a clock cycle at the current I2C speed.
  273. * @return Zero if status is as expected, or a non-zero code if either a time
  274. * out occurred or the status was not the expected one.
  275. */
  276. static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
  277. uint tick)
  278. {
  279. /* Assert START */
  280. writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
  281. MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  282. /* Wait for controller to process START */
  283. return twsi_wait(twsi, expected_status, tick);
  284. }
  285. /*
  286. * twsi_send() - Send a byte on the I2C bus.
  287. *
  288. * The byte may be part of an address byte or data.
  289. *
  290. * @twsi: The MVTWSI register structure to use.
  291. * @byte: The byte to send.
  292. * @expected_status: The I2C bus status expected to be asserted after the
  293. * operation completion.
  294. * @tick: The duration of a clock cycle at the current I2C speed.
  295. * @return Zero if status is as expected, or a non-zero code if either a time
  296. * out occurred or the status was not the expected one.
  297. */
  298. static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
  299. int expected_status, uint tick)
  300. {
  301. /* Write byte to data register for sending */
  302. writel(byte, &twsi->data);
  303. /* Clear any pending interrupt -- that will cause sending */
  304. writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
  305. &twsi->control);
  306. /* Wait for controller to receive byte, and check ACK */
  307. return twsi_wait(twsi, expected_status, tick);
  308. }
  309. /*
  310. * twsi_recv() - Receive a byte on the I2C bus.
  311. *
  312. * The static variable mvtwsi_control_flags controls whether we ack or nak.
  313. *
  314. * @twsi: The MVTWSI register structure to use.
  315. * @byte: The byte to send.
  316. * @ack_flag: Flag that determines whether the received byte should
  317. * be acknowledged by the controller or not (sent ACK/NAK).
  318. * @tick: The duration of a clock cycle at the current I2C speed.
  319. * @return Zero if status is as expected, or a non-zero code if either a time
  320. * out occurred or the status was not the expected one.
  321. */
  322. static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
  323. uint tick)
  324. {
  325. int expected_status, status, control;
  326. /* Compute expected status based on passed ACK flag */
  327. expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
  328. MVTWSI_STATUS_DATA_R_NAK;
  329. /* Acknowledge *previous state*, and launch receive */
  330. control = MVTWSI_CONTROL_TWSIEN;
  331. control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
  332. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  333. /* Wait for controller to receive byte, and assert ACK or NAK */
  334. status = twsi_wait(twsi, expected_status, tick);
  335. /* If we did receive the expected byte, store it */
  336. if (status == 0)
  337. *byte = readl(&twsi->data);
  338. return status;
  339. }
  340. /*
  341. * twsi_stop() - Assert a STOP condition on the bus.
  342. *
  343. * This function is also used to force the bus back to idle state (SDA =
  344. * SCL = 1).
  345. *
  346. * @twsi: The MVTWSI register structure to use.
  347. * @tick: The duration of a clock cycle at the current I2C speed.
  348. * @return Zero if the operation succeeded, or a non-zero code if a time out
  349. * occurred.
  350. */
  351. static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
  352. {
  353. int control, stop_status;
  354. int status = 0;
  355. int timeout = 1000;
  356. /* Assert STOP */
  357. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  358. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  359. /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
  360. do {
  361. stop_status = readl(&twsi->status);
  362. if (stop_status == MVTWSI_STATUS_IDLE)
  363. break;
  364. ndelay(tick); /* One clock cycle */
  365. } while (timeout--);
  366. control = readl(&twsi->control);
  367. if (stop_status != MVTWSI_STATUS_IDLE)
  368. status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
  369. control, status, MVTWSI_STATUS_IDLE);
  370. return status;
  371. }
  372. /*
  373. * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
  374. *
  375. * @n: Parameter 'n' for the frequency calculation algorithm.
  376. * @m: Parameter 'm' for the frequency calculation algorithm.
  377. * @return The I2C frequency corresponding to the passed m and n parameters.
  378. */
  379. static uint twsi_calc_freq(const int n, const int m)
  380. {
  381. #ifdef CONFIG_ARCH_SUNXI
  382. return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
  383. #else
  384. return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  385. #endif
  386. }
  387. /*
  388. * twsi_reset() - Reset the I2C controller.
  389. *
  390. * Resetting the controller also resets the baud rate and slave address, hence
  391. * they must be re-established after the reset.
  392. *
  393. * @twsi: The MVTWSI register structure to use.
  394. */
  395. static void twsi_reset(struct mvtwsi_registers *twsi)
  396. {
  397. /* Reset controller */
  398. writel(0, &twsi->soft_reset);
  399. /* Wait 2 ms -- this is what the Marvell LSP does */
  400. udelay(20000);
  401. }
  402. /*
  403. * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
  404. *
  405. * This function sets baud rate to the highest possible value that does not
  406. * exceed the requested rate.
  407. *
  408. * @twsi: The MVTWSI register structure to use.
  409. * @requested_speed: The desired frequency the controller should run at
  410. * in Hz.
  411. * @return The actual frequency the controller was configured to.
  412. */
  413. static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
  414. uint requested_speed)
  415. {
  416. uint tmp_speed, highest_speed, n, m;
  417. uint baud = 0x44; /* Baud rate after controller reset */
  418. highest_speed = 0;
  419. /* Successively try m, n combinations, and use the combination
  420. * resulting in the largest speed that's not above the requested
  421. * speed */
  422. for (n = 0; n < 8; n++) {
  423. for (m = 0; m < 16; m++) {
  424. tmp_speed = twsi_calc_freq(n, m);
  425. if ((tmp_speed <= requested_speed) &&
  426. (tmp_speed > highest_speed)) {
  427. highest_speed = tmp_speed;
  428. baud = (m << 3) | n;
  429. }
  430. }
  431. }
  432. writel(baud, &twsi->baudrate);
  433. /* Wait for controller for one tick */
  434. #ifdef CONFIG_DM_I2C
  435. ndelay(calc_tick(highest_speed));
  436. #else
  437. ndelay(10000);
  438. #endif
  439. return highest_speed;
  440. }
  441. /*
  442. * __twsi_i2c_init() - Initialize the I2C controller.
  443. *
  444. * @twsi: The MVTWSI register structure to use.
  445. * @speed: The initial frequency the controller should run at
  446. * in Hz.
  447. * @slaveadd: The I2C address to be set for the I2C master.
  448. * @actual_speed: A output parameter that receives the actual frequency
  449. * in Hz the controller was set to by the function.
  450. * @return Zero if the operation succeeded, or a non-zero code if a time out
  451. * occurred.
  452. */
  453. static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
  454. int slaveadd, uint *actual_speed)
  455. {
  456. uint tmp_speed;
  457. /* Reset controller */
  458. twsi_reset(twsi);
  459. /* Set speed */
  460. tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
  461. if (actual_speed)
  462. *actual_speed = tmp_speed;
  463. /* Set slave address; even though we don't use it */
  464. writel(slaveadd, &twsi->slave_address);
  465. writel(0, &twsi->xtnd_slave_addr);
  466. /* Assert STOP, but don't care for the result */
  467. #ifdef CONFIG_DM_I2C
  468. (void) twsi_stop(twsi, calc_tick(*actual_speed));
  469. #else
  470. (void) twsi_stop(twsi, 10000);
  471. #endif
  472. }
  473. /*
  474. * i2c_begin() - Start a I2C transaction.
  475. *
  476. * Begin a I2C transaction with a given expected start status and chip address.
  477. * A START is asserted, and the address byte is sent to the I2C controller. The
  478. * expected address status will be derived from the direction bit (bit 0) of
  479. * the address byte.
  480. *
  481. * @twsi: The MVTWSI register structure to use.
  482. * @expected_start_status: The I2C status the controller is expected to
  483. * assert after the address byte was sent.
  484. * @addr: The address byte to be sent.
  485. * @tick: The duration of a clock cycle at the current
  486. * I2C speed.
  487. * @return Zero if the operation succeeded, or a non-zero code if a time out or
  488. * unexpected I2C status occurred.
  489. */
  490. static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
  491. u8 addr, uint tick)
  492. {
  493. int status, expected_addr_status;
  494. /* Compute the expected address status from the direction bit in
  495. * the address byte */
  496. if (addr & 1) /* Reading */
  497. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  498. else /* Writing */
  499. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  500. /* Assert START */
  501. status = twsi_start(twsi, expected_start_status, tick);
  502. /* Send out the address if the start went well */
  503. if (status == 0)
  504. status = twsi_send(twsi, addr, expected_addr_status, tick);
  505. /* Return 0, or the status of the first failure */
  506. return status;
  507. }
  508. /*
  509. * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
  510. *
  511. * This function begins a I2C read transaction, does a dummy read and NAKs; if
  512. * the procedure succeeds, the chip is considered to be present.
  513. *
  514. * @twsi: The MVTWSI register structure to use.
  515. * @chip: The chip address to probe.
  516. * @tick: The duration of a clock cycle at the current I2C speed.
  517. * @return Zero if the operation succeeded, or a non-zero code if a time out or
  518. * unexpected I2C status occurred.
  519. */
  520. static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
  521. uint tick)
  522. {
  523. u8 dummy_byte;
  524. int status;
  525. /* Begin i2c read */
  526. status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
  527. /* Dummy read was accepted: receive byte, but NAK it. */
  528. if (status == 0)
  529. status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
  530. /* Stop transaction */
  531. twsi_stop(twsi, tick);
  532. /* Return 0, or the status of the first failure */
  533. return status;
  534. }
  535. /*
  536. * __twsi_i2c_read() - Read data from a I2C chip.
  537. *
  538. * This function begins a I2C write transaction, and transmits the address
  539. * bytes; then begins a I2C read transaction, and receives the data bytes.
  540. *
  541. * NOTE: Some devices want a stop right before the second start, while some
  542. * will choke if it is there. Since deciding this is not yet supported in
  543. * higher level APIs, we need to make a decision here, and for the moment that
  544. * will be a repeated start without a preceding stop.
  545. *
  546. * @twsi: The MVTWSI register structure to use.
  547. * @chip: The chip address to read from.
  548. * @addr: The address bytes to send.
  549. * @alen: The length of the address bytes in bytes.
  550. * @data: The buffer to receive the data read from the chip (has to have
  551. * a size of at least 'length' bytes).
  552. * @length: The amount of data to be read from the chip in bytes.
  553. * @tick: The duration of a clock cycle at the current I2C speed.
  554. * @return Zero if the operation succeeded, or a non-zero code if a time out or
  555. * unexpected I2C status occurred.
  556. */
  557. static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
  558. u8 *addr, int alen, uchar *data, int length,
  559. uint tick)
  560. {
  561. int status = 0;
  562. int stop_status;
  563. int expected_start = MVTWSI_STATUS_START;
  564. if (alen > 0) {
  565. /* Begin i2c write to send the address bytes */
  566. status = i2c_begin(twsi, expected_start, (chip << 1), tick);
  567. /* Send address bytes */
  568. while ((status == 0) && alen--)
  569. status = twsi_send(twsi, addr[alen],
  570. MVTWSI_STATUS_DATA_W_ACK, tick);
  571. /* Send repeated STARTs after the initial START */
  572. expected_start = MVTWSI_STATUS_REPEATED_START;
  573. }
  574. /* Begin i2c read to receive data bytes */
  575. if (status == 0)
  576. status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
  577. /* Receive actual data bytes; set NAK if we if we have nothing more to
  578. * read */
  579. while ((status == 0) && length--)
  580. status = twsi_recv(twsi, data++,
  581. length > 0 ?
  582. MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
  583. /* Stop transaction */
  584. stop_status = twsi_stop(twsi, tick);
  585. /* Return 0, or the status of the first failure */
  586. return status != 0 ? status : stop_status;
  587. }
  588. /*
  589. * __twsi_i2c_write() - Send data to a I2C chip.
  590. *
  591. * This function begins a I2C write transaction, and transmits the address
  592. * bytes; then begins a new I2C write transaction, and sends the data bytes.
  593. *
  594. * @twsi: The MVTWSI register structure to use.
  595. * @chip: The chip address to read from.
  596. * @addr: The address bytes to send.
  597. * @alen: The length of the address bytes in bytes.
  598. * @data: The buffer containing the data to be sent to the chip.
  599. * @length: The length of data to be sent to the chip in bytes.
  600. * @tick: The duration of a clock cycle at the current I2C speed.
  601. * @return Zero if the operation succeeded, or a non-zero code if a time out or
  602. * unexpected I2C status occurred.
  603. */
  604. static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
  605. u8 *addr, int alen, uchar *data, int length,
  606. uint tick)
  607. {
  608. int status, stop_status;
  609. /* Begin i2c write to send first the address bytes, then the
  610. * data bytes */
  611. status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
  612. /* Send address bytes */
  613. while ((status == 0) && (alen-- > 0))
  614. status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
  615. tick);
  616. /* Send data bytes */
  617. while ((status == 0) && (length-- > 0))
  618. status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
  619. tick);
  620. /* Stop transaction */
  621. stop_status = twsi_stop(twsi, tick);
  622. /* Return 0, or the status of the first failure */
  623. return status != 0 ? status : stop_status;
  624. }
  625. #ifndef CONFIG_DM_I2C
  626. static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
  627. int slaveadd)
  628. {
  629. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  630. __twsi_i2c_init(twsi, speed, slaveadd, NULL);
  631. }
  632. static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
  633. uint requested_speed)
  634. {
  635. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  636. __twsi_i2c_set_bus_speed(twsi, requested_speed);
  637. return 0;
  638. }
  639. static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
  640. {
  641. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  642. return __twsi_i2c_probe_chip(twsi, chip, 10000);
  643. }
  644. static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  645. int alen, uchar *data, int length)
  646. {
  647. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  648. u8 addr_bytes[4];
  649. addr_bytes[0] = (addr >> 0) & 0xFF;
  650. addr_bytes[1] = (addr >> 8) & 0xFF;
  651. addr_bytes[2] = (addr >> 16) & 0xFF;
  652. addr_bytes[3] = (addr >> 24) & 0xFF;
  653. return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
  654. 10000);
  655. }
  656. static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  657. int alen, uchar *data, int length)
  658. {
  659. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  660. u8 addr_bytes[4];
  661. addr_bytes[0] = (addr >> 0) & 0xFF;
  662. addr_bytes[1] = (addr >> 8) & 0xFF;
  663. addr_bytes[2] = (addr >> 16) & 0xFF;
  664. addr_bytes[3] = (addr >> 24) & 0xFF;
  665. return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
  666. 10000);
  667. }
  668. #ifdef CONFIG_I2C_MVTWSI_BASE0
  669. U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
  670. twsi_i2c_read, twsi_i2c_write,
  671. twsi_i2c_set_bus_speed,
  672. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  673. #endif
  674. #ifdef CONFIG_I2C_MVTWSI_BASE1
  675. U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
  676. twsi_i2c_read, twsi_i2c_write,
  677. twsi_i2c_set_bus_speed,
  678. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
  679. #endif
  680. #ifdef CONFIG_I2C_MVTWSI_BASE2
  681. U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
  682. twsi_i2c_read, twsi_i2c_write,
  683. twsi_i2c_set_bus_speed,
  684. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
  685. #endif
  686. #ifdef CONFIG_I2C_MVTWSI_BASE3
  687. U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
  688. twsi_i2c_read, twsi_i2c_write,
  689. twsi_i2c_set_bus_speed,
  690. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
  691. #endif
  692. #ifdef CONFIG_I2C_MVTWSI_BASE4
  693. U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
  694. twsi_i2c_read, twsi_i2c_write,
  695. twsi_i2c_set_bus_speed,
  696. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
  697. #endif
  698. #ifdef CONFIG_I2C_MVTWSI_BASE5
  699. U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
  700. twsi_i2c_read, twsi_i2c_write,
  701. twsi_i2c_set_bus_speed,
  702. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
  703. #endif
  704. #else /* CONFIG_DM_I2C */
  705. static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  706. u32 chip_flags)
  707. {
  708. struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  709. return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
  710. }
  711. static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
  712. {
  713. struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  714. dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
  715. dev->tick = calc_tick(dev->speed);
  716. return 0;
  717. }
  718. static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
  719. {
  720. struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  721. dev->base = devfdt_get_addr_ptr(bus);
  722. if (!dev->base)
  723. return -ENOMEM;
  724. dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
  725. "cell-index", -1);
  726. dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
  727. "u-boot,i2c-slave-addr", 0x0);
  728. dev->speed = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
  729. "clock-frequency", 100000);
  730. return 0;
  731. }
  732. static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
  733. {
  734. clrbits_le32(&twsi->debug, BIT(18));
  735. }
  736. static int mvtwsi_i2c_bind(struct udevice *bus)
  737. {
  738. struct mvtwsi_registers *twsi = devfdt_get_addr_ptr(bus);
  739. /* Disable the hidden slave in i2c0 of these platforms */
  740. if ((IS_ENABLED(CONFIG_ARMADA_38X) || IS_ENABLED(CONFIG_KIRKWOOD))
  741. && bus->req_seq == 0)
  742. twsi_disable_i2c_slave(twsi);
  743. return 0;
  744. }
  745. static int mvtwsi_i2c_probe(struct udevice *bus)
  746. {
  747. struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  748. uint actual_speed;
  749. __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
  750. dev->speed = actual_speed;
  751. dev->tick = calc_tick(dev->speed);
  752. return 0;
  753. }
  754. static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  755. {
  756. struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  757. struct i2c_msg *dmsg, *omsg, dummy;
  758. memset(&dummy, 0, sizeof(struct i2c_msg));
  759. /* We expect either two messages (one with an offset and one with the
  760. * actual data) or one message (just data or offset/data combined) */
  761. if (nmsgs > 2 || nmsgs == 0) {
  762. debug("%s: Only one or two messages are supported.", __func__);
  763. return -1;
  764. }
  765. omsg = nmsgs == 1 ? &dummy : msg;
  766. dmsg = nmsgs == 1 ? msg : msg + 1;
  767. if (dmsg->flags & I2C_M_RD)
  768. return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
  769. omsg->len, dmsg->buf, dmsg->len,
  770. dev->tick);
  771. else
  772. return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
  773. omsg->len, dmsg->buf, dmsg->len,
  774. dev->tick);
  775. }
  776. static const struct dm_i2c_ops mvtwsi_i2c_ops = {
  777. .xfer = mvtwsi_i2c_xfer,
  778. .probe_chip = mvtwsi_i2c_probe_chip,
  779. .set_bus_speed = mvtwsi_i2c_set_bus_speed,
  780. };
  781. static const struct udevice_id mvtwsi_i2c_ids[] = {
  782. { .compatible = "marvell,mv64xxx-i2c", },
  783. { .compatible = "marvell,mv78230-i2c", },
  784. { .compatible = "allwinner,sun6i-a31-i2c", },
  785. { /* sentinel */ }
  786. };
  787. U_BOOT_DRIVER(i2c_mvtwsi) = {
  788. .name = "i2c_mvtwsi",
  789. .id = UCLASS_I2C,
  790. .of_match = mvtwsi_i2c_ids,
  791. .bind = mvtwsi_i2c_bind,
  792. .probe = mvtwsi_i2c_probe,
  793. .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata,
  794. .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev),
  795. .ops = &mvtwsi_i2c_ops,
  796. };
  797. #endif /* CONFIG_DM_I2C */