mvtwsi.c 25 KB

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