mvtwsi.c 25 KB

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