mvtwsi.c 28 KB

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