mvtwsi.c 25 KB

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