mvtwsi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <asm/errno.h>
  13. #include <asm/io.h>
  14. /*
  15. * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
  16. * settings
  17. */
  18. #if defined(CONFIG_ORION5X)
  19. #include <asm/arch/orion5x.h>
  20. #elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
  21. #include <asm/arch/soc.h>
  22. #elif defined(CONFIG_SUNXI)
  23. #include <asm/arch/i2c.h>
  24. #else
  25. #error Driver mvtwsi not supported by SoC or board
  26. #endif
  27. /*
  28. * TWSI register structure
  29. */
  30. #ifdef CONFIG_SUNXI
  31. struct mvtwsi_registers {
  32. u32 slave_address;
  33. u32 xtnd_slave_addr;
  34. u32 data;
  35. u32 control;
  36. u32 status;
  37. u32 baudrate;
  38. u32 soft_reset;
  39. };
  40. #else
  41. struct mvtwsi_registers {
  42. u32 slave_address;
  43. u32 data;
  44. u32 control;
  45. union {
  46. u32 status; /* When reading */
  47. u32 baudrate; /* When writing */
  48. };
  49. u32 xtnd_slave_addr;
  50. u32 reserved[2];
  51. u32 soft_reset;
  52. };
  53. #endif
  54. /*
  55. * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
  56. * register
  57. */
  58. enum mvtwsi_ctrl_register_fields {
  59. /* Acknowledge bit */
  60. MVTWSI_CONTROL_ACK = 0x00000004,
  61. /* Interrupt flag */
  62. MVTWSI_CONTROL_IFLG = 0x00000008,
  63. /* Stop bit */
  64. MVTWSI_CONTROL_STOP = 0x00000010,
  65. /* Start bit */
  66. MVTWSI_CONTROL_START = 0x00000020,
  67. /* I2C enable */
  68. MVTWSI_CONTROL_TWSIEN = 0x00000040,
  69. /* Interrupt enable */
  70. MVTWSI_CONTROL_INTEN = 0x00000080,
  71. };
  72. /*
  73. * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
  74. * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
  75. */
  76. #ifdef CONFIG_SUNXI_GEN_SUN6I
  77. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
  78. #else
  79. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
  80. #endif
  81. /*
  82. * enum mvstwsi_status_values - Possible values of I2C controller's status
  83. * register
  84. *
  85. * Only those statuses expected in normal master operation on
  86. * non-10-bit-address devices are specified.
  87. *
  88. * Every status that's unexpected during normal operation (bus errors,
  89. * arbitration losses, missing ACKs...) is passed back to the caller as an error
  90. * code.
  91. */
  92. enum mvstwsi_status_values {
  93. /* START condition transmitted */
  94. MVTWSI_STATUS_START = 0x08,
  95. /* Repeated START condition transmitted */
  96. MVTWSI_STATUS_REPEATED_START = 0x10,
  97. /* Address + write bit transmitted, ACK received */
  98. MVTWSI_STATUS_ADDR_W_ACK = 0x18,
  99. /* Data transmitted, ACK received */
  100. MVTWSI_STATUS_DATA_W_ACK = 0x28,
  101. /* Address + read bit transmitted, ACK received */
  102. MVTWSI_STATUS_ADDR_R_ACK = 0x40,
  103. /* Address + read bit transmitted, ACK not received */
  104. MVTWSI_STATUS_ADDR_R_NAK = 0x48,
  105. /* Data received, ACK transmitted */
  106. MVTWSI_STATUS_DATA_R_ACK = 0x50,
  107. /* Data received, ACK not transmitted */
  108. MVTWSI_STATUS_DATA_R_NAK = 0x58,
  109. /* No relevant status */
  110. MVTWSI_STATUS_IDLE = 0xF8,
  111. };
  112. /*
  113. * enum mvstwsi_ack_flags - Determine whether a read byte should be
  114. * acknowledged or not.
  115. */
  116. enum mvtwsi_ack_flags {
  117. /* Send NAK after received byte */
  118. MVTWSI_READ_NAK = 0,
  119. /* Send ACK after received byte */
  120. MVTWSI_READ_ACK = 1,
  121. };
  122. /*
  123. * MVTWSI controller base
  124. */
  125. static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
  126. {
  127. switch (adap->hwadapnr) {
  128. #ifdef CONFIG_I2C_MVTWSI_BASE0
  129. case 0:
  130. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
  131. #endif
  132. #ifdef CONFIG_I2C_MVTWSI_BASE1
  133. case 1:
  134. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
  135. #endif
  136. #ifdef CONFIG_I2C_MVTWSI_BASE2
  137. case 2:
  138. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
  139. #endif
  140. #ifdef CONFIG_I2C_MVTWSI_BASE3
  141. case 3:
  142. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
  143. #endif
  144. #ifdef CONFIG_I2C_MVTWSI_BASE4
  145. case 4:
  146. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
  147. #endif
  148. #ifdef CONFIG_I2C_MVTWSI_BASE5
  149. case 5:
  150. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
  151. #endif
  152. default:
  153. printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
  154. break;
  155. }
  156. return NULL;
  157. }
  158. /*
  159. * enum mvtwsi_error_class - types of I2C errors
  160. */
  161. enum mvtwsi_error_class {
  162. /* The controller returned a different status than expected */
  163. MVTWSI_ERROR_WRONG_STATUS = 0x01,
  164. /* The controller timed out */
  165. MVTWSI_ERROR_TIMEOUT = 0x02,
  166. };
  167. /*
  168. * mvtwsi_error() - Build I2C return code from error information
  169. *
  170. * For debugging purposes, this function packs some information of an occurred
  171. * error into a return code. These error codes are returned from I2C API
  172. * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
  173. *
  174. * @ec: The error class of the error (enum mvtwsi_error_class).
  175. * @lc: The last value of the control register.
  176. * @ls: The last value of the status register.
  177. * @es: The expected value of the status register.
  178. * @return The generated error code.
  179. */
  180. inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
  181. {
  182. return ((ec << 24) & 0xFF000000)
  183. | ((lc << 16) & 0x00FF0000)
  184. | ((ls << 8) & 0x0000FF00)
  185. | (es & 0xFF);
  186. }
  187. /*
  188. * Wait for IFLG to raise, or return 'timeout.' Then, if the status is as
  189. * expected, return 0 (ok) or 'wrong status' otherwise.
  190. */
  191. static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status)
  192. {
  193. int control, status;
  194. int timeout = 1000;
  195. do {
  196. control = readl(&twsi->control);
  197. if (control & MVTWSI_CONTROL_IFLG) {
  198. status = readl(&twsi->status);
  199. if (status == expected_status)
  200. return 0;
  201. else
  202. return mvtwsi_error(
  203. MVTWSI_ERROR_WRONG_STATUS,
  204. control, status, expected_status);
  205. }
  206. udelay(10); /* One clock cycle at 100 kHz */
  207. } while (timeout--);
  208. status = readl(&twsi->status);
  209. return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
  210. expected_status);
  211. }
  212. /*
  213. * Assert the START condition, either in a single I2C transaction
  214. * or inside back-to-back ones (repeated starts).
  215. */
  216. static int twsi_start(struct mvtwsi_registers *twsi, int expected_status)
  217. {
  218. /* Assert START */
  219. writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
  220. MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  221. /* Wait for controller to process START */
  222. return twsi_wait(twsi, expected_status);
  223. }
  224. /*
  225. * Send a byte (i2c address or data).
  226. */
  227. static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
  228. int expected_status)
  229. {
  230. /* Write byte to data register for sending */
  231. writel(byte, &twsi->data);
  232. /* Clear any pending interrupt -- that will cause sending */
  233. writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
  234. &twsi->control);
  235. /* Wait for controller to receive byte, and check ACK */
  236. return twsi_wait(twsi, expected_status);
  237. }
  238. /*
  239. * Receive a byte.
  240. */
  241. static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag)
  242. {
  243. int expected_status, status, control;
  244. /* Compute expected status based on passed ACK flag */
  245. expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
  246. MVTWSI_STATUS_DATA_R_NAK;
  247. /* Acknowledge *previous state*, and launch receive */
  248. control = MVTWSI_CONTROL_TWSIEN;
  249. control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
  250. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  251. /* Wait for controller to receive byte, and assert ACK or NAK */
  252. status = twsi_wait(twsi, expected_status);
  253. /* If we did receive the expected byte, store it */
  254. if (status == 0)
  255. *byte = readl(&twsi->data);
  256. return status;
  257. }
  258. /*
  259. * Assert the STOP condition.
  260. * This is also used to force the bus back to idle (SDA = SCL = 1).
  261. */
  262. static int twsi_stop(struct mvtwsi_registers *twsi)
  263. {
  264. int control, stop_status;
  265. int status = 0;
  266. int timeout = 1000;
  267. /* Assert STOP */
  268. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  269. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  270. /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
  271. do {
  272. stop_status = readl(&twsi->status);
  273. if (stop_status == MVTWSI_STATUS_IDLE)
  274. break;
  275. udelay(10); /* One clock cycle at 100 kHz */
  276. } while (timeout--);
  277. control = readl(&twsi->control);
  278. if (stop_status != MVTWSI_STATUS_IDLE)
  279. status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
  280. control, status, MVTWSI_STATUS_IDLE);
  281. return status;
  282. }
  283. static uint twsi_calc_freq(const int n, const int m)
  284. {
  285. #ifdef CONFIG_SUNXI
  286. return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
  287. #else
  288. return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  289. #endif
  290. }
  291. /*
  292. * Reset controller.
  293. * Controller reset also resets the baud rate and slave address, so
  294. * they must be re-established afterwards.
  295. */
  296. static void twsi_reset(struct mvtwsi_registers *twsi)
  297. {
  298. /* Reset controller */
  299. writel(0, &twsi->soft_reset);
  300. /* Wait 2 ms -- this is what the Marvell LSP does */
  301. udelay(20000);
  302. }
  303. /*
  304. * Sets baud to the highest possible value not exceeding the requested one.
  305. */
  306. static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
  307. uint requested_speed)
  308. {
  309. uint tmp_speed, highest_speed, n, m;
  310. uint baud = 0x44; /* Baud rate after controller reset */
  311. highest_speed = 0;
  312. /* Successively try m, n combinations, and use the combination
  313. * resulting in the largest speed that's not above the requested
  314. * speed */
  315. for (n = 0; n < 8; n++) {
  316. for (m = 0; m < 16; m++) {
  317. tmp_speed = twsi_calc_freq(n, m);
  318. if ((tmp_speed <= requested_speed) &&
  319. (tmp_speed > highest_speed)) {
  320. highest_speed = tmp_speed;
  321. baud = (m << 3) | n;
  322. }
  323. }
  324. }
  325. writel(baud, &twsi->baudrate);
  326. return 0;
  327. }
  328. static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
  329. int slaveadd)
  330. {
  331. /* Reset controller */
  332. twsi_reset(twsi);
  333. /* Set speed */
  334. __twsi_i2c_set_bus_speed(twsi, speed);
  335. /* Set slave address; even though we don't use it */
  336. writel(slaveadd, &twsi->slave_address);
  337. writel(0, &twsi->xtnd_slave_addr);
  338. /* Assert STOP, but don't care for the result */
  339. (void) twsi_stop(twsi);
  340. }
  341. /*
  342. * Begin I2C transaction with expected start status, at given address.
  343. * Expected address status will derive from direction bit (bit 0) in addr.
  344. */
  345. static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
  346. u8 addr)
  347. {
  348. int status, expected_addr_status;
  349. /* Compute the expected address status from the direction bit in
  350. * the address byte */
  351. if (addr & 1) /* Reading */
  352. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  353. else /* Writing */
  354. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  355. /* Assert START */
  356. status = twsi_start(twsi, expected_start_status);
  357. /* Send out the address if the start went well */
  358. if (status == 0)
  359. status = twsi_send(twsi, addr, expected_addr_status);
  360. /* Return 0, or the status of the first failure */
  361. return status;
  362. }
  363. /*
  364. * Begin read, nak data byte, end.
  365. */
  366. static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip)
  367. {
  368. u8 dummy_byte;
  369. int status;
  370. /* Begin i2c read */
  371. status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1);
  372. /* Dummy read was accepted: receive byte, but NAK it. */
  373. if (status == 0)
  374. status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK);
  375. /* Stop transaction */
  376. twsi_stop(twsi);
  377. /* Return 0, or the status of the first failure */
  378. return status;
  379. }
  380. /*
  381. * Begin write, send address byte(s), begin read, receive data bytes, end.
  382. *
  383. * NOTE: Some devices want a stop right before the second start, while some
  384. * will choke if it is there. Since deciding this is not yet supported in
  385. * higher level APIs, we need to make a decision here, and for the moment that
  386. * will be a repeated start without a preceding stop.
  387. */
  388. static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
  389. uint addr, int alen, uchar *data, int length)
  390. {
  391. int status = 0;
  392. int stop_status;
  393. /* Begin i2c write to send the address bytes */
  394. status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1));
  395. /* Send address bytes */
  396. while ((status == 0) && alen--)
  397. status = twsi_send(twsi, addr >> (8*alen),
  398. MVTWSI_STATUS_DATA_W_ACK);
  399. /* Begin i2c read to receive data bytes */
  400. if (status == 0)
  401. status = i2c_begin(twsi, MVTWSI_STATUS_REPEATED_START,
  402. (chip << 1) | 1);
  403. /* Receive actual data bytes; set NAK if we if we have nothing more to
  404. * read */
  405. while ((status == 0) && length--)
  406. status = twsi_recv(twsi, data++,
  407. length > 0 ?
  408. MVTWSI_READ_ACK : MVTWSI_READ_NAK);
  409. /* Stop transaction */
  410. stop_status = twsi_stop(twsi);
  411. /* Return 0, or the status of the first failure */
  412. return status != 0 ? status : stop_status;
  413. }
  414. /*
  415. * Begin write, send address byte(s), send data bytes, end.
  416. */
  417. static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
  418. uint addr, int alen, uchar *data, int length)
  419. {
  420. int status, stop_status;
  421. /* Begin i2c write to send first the address bytes, then the
  422. * data bytes */
  423. status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1));
  424. /* Send address bytes */
  425. while ((status == 0) && alen--)
  426. status = twsi_send(twsi, addr >> (8*alen),
  427. MVTWSI_STATUS_DATA_W_ACK);
  428. /* Send data bytes */
  429. while ((status == 0) && (length-- > 0))
  430. status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK);
  431. /* Stop transaction */
  432. stop_status = twsi_stop(twsi);
  433. /* Return 0, or the status of the first failure */
  434. return status != 0 ? status : stop_status;
  435. }
  436. static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
  437. int slaveadd)
  438. {
  439. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  440. __twsi_i2c_init(twsi, speed, slaveadd);
  441. }
  442. static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
  443. uint requested_speed)
  444. {
  445. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  446. return __twsi_i2c_set_bus_speed(twsi, requested_speed);
  447. }
  448. static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
  449. {
  450. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  451. return __twsi_i2c_probe_chip(twsi, chip);
  452. }
  453. static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  454. int alen, uchar *data, int length)
  455. {
  456. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  457. return __twsi_i2c_read(twsi, chip, addr, alen, data, length);
  458. }
  459. static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  460. int alen, uchar *data, int length)
  461. {
  462. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  463. return __twsi_i2c_write(twsi, chip, addr, alen, data, length);
  464. }
  465. #ifdef CONFIG_I2C_MVTWSI_BASE0
  466. U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
  467. twsi_i2c_read, twsi_i2c_write,
  468. twsi_i2c_set_bus_speed,
  469. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  470. #endif
  471. #ifdef CONFIG_I2C_MVTWSI_BASE1
  472. U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
  473. twsi_i2c_read, twsi_i2c_write,
  474. twsi_i2c_set_bus_speed,
  475. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
  476. #endif
  477. #ifdef CONFIG_I2C_MVTWSI_BASE2
  478. U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
  479. twsi_i2c_read, twsi_i2c_write,
  480. twsi_i2c_set_bus_speed,
  481. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
  482. #endif
  483. #ifdef CONFIG_I2C_MVTWSI_BASE3
  484. U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
  485. twsi_i2c_read, twsi_i2c_write,
  486. twsi_i2c_set_bus_speed,
  487. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
  488. #endif
  489. #ifdef CONFIG_I2C_MVTWSI_BASE4
  490. U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
  491. twsi_i2c_read, twsi_i2c_write,
  492. twsi_i2c_set_bus_speed,
  493. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
  494. #endif
  495. #ifdef CONFIG_I2C_MVTWSI_BASE5
  496. U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
  497. twsi_i2c_read, twsi_i2c_write,
  498. twsi_i2c_set_bus_speed,
  499. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
  500. #endif