mvtwsi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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*
  16. * and possibly other 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. * MVTWSI controller base
  114. */
  115. static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
  116. {
  117. switch (adap->hwadapnr) {
  118. #ifdef CONFIG_I2C_MVTWSI_BASE0
  119. case 0:
  120. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
  121. #endif
  122. #ifdef CONFIG_I2C_MVTWSI_BASE1
  123. case 1:
  124. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
  125. #endif
  126. #ifdef CONFIG_I2C_MVTWSI_BASE2
  127. case 2:
  128. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
  129. #endif
  130. #ifdef CONFIG_I2C_MVTWSI_BASE3
  131. case 3:
  132. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
  133. #endif
  134. #ifdef CONFIG_I2C_MVTWSI_BASE4
  135. case 4:
  136. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
  137. #endif
  138. #ifdef CONFIG_I2C_MVTWSI_BASE5
  139. case 5:
  140. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
  141. #endif
  142. default:
  143. printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
  144. break;
  145. }
  146. return NULL;
  147. }
  148. /*
  149. * enum mvtwsi_error_class - types of I2C errors
  150. */
  151. enum mvtwsi_error_class {
  152. /* The controller returned a different status than expected */
  153. MVTWSI_ERROR_WRONG_STATUS = 0x01,
  154. /* The controller timed out */
  155. MVTWSI_ERROR_TIMEOUT = 0x02,
  156. };
  157. /*
  158. * mvtwsi_error() - Build I2C return code from error information
  159. *
  160. * For debugging purposes, this function packs some information of an occurred
  161. * error into a return code. These error codes are returned from I2C API
  162. * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
  163. *
  164. * @ec: The error class of the error (enum mvtwsi_error_class).
  165. * @lc: The last value of the control register.
  166. * @ls: The last value of the status register.
  167. * @es: The expected value of the status register.
  168. * @return The generated error code.
  169. */
  170. inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
  171. {
  172. return ((ec << 24) & 0xFF000000)
  173. | ((lc << 16) & 0x00FF0000)
  174. | ((ls << 8) & 0x0000FF00)
  175. | (es & 0xFF);
  176. }
  177. /*
  178. * Wait for IFLG to raise, or return 'timeout'; then if status is as expected,
  179. * return 0 (ok) or return 'wrong status'.
  180. */
  181. static int twsi_wait(struct i2c_adapter *adap, int expected_status)
  182. {
  183. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  184. int control, status;
  185. int timeout = 1000;
  186. do {
  187. control = readl(&twsi->control);
  188. if (control & MVTWSI_CONTROL_IFLG) {
  189. status = readl(&twsi->status);
  190. if (status == expected_status)
  191. return 0;
  192. else
  193. return mvtwsi_error(
  194. MVTWSI_ERROR_WRONG_STATUS,
  195. control, status, expected_status);
  196. }
  197. udelay(10); /* one clock cycle at 100 kHz */
  198. } while (timeout--);
  199. status = readl(&twsi->status);
  200. return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
  201. expected_status);
  202. }
  203. /*
  204. * Assert the START condition, either in a single I2C transaction
  205. * or inside back-to-back ones (repeated starts).
  206. */
  207. static int twsi_start(struct i2c_adapter *adap, int expected_status, u8 *flags)
  208. {
  209. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  210. /* globally set TWSIEN in case it was not */
  211. *flags |= MVTWSI_CONTROL_TWSIEN;
  212. /* assert START */
  213. writel(*flags | MVTWSI_CONTROL_START |
  214. MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  215. /* wait for controller to process START */
  216. return twsi_wait(adap, expected_status);
  217. }
  218. /*
  219. * Send a byte (i2c address or data).
  220. */
  221. static int twsi_send(struct i2c_adapter *adap, u8 byte, int expected_status,
  222. u8 *flags)
  223. {
  224. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  225. /* put byte in data register for sending */
  226. writel(byte, &twsi->data);
  227. /* clear any pending interrupt -- that'll cause sending */
  228. writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  229. /* wait for controller to receive byte and check ACK */
  230. return twsi_wait(adap, expected_status);
  231. }
  232. /*
  233. * Receive a byte.
  234. * Global mvtwsi_control_flags variable says if we should ack or nak.
  235. */
  236. static int twsi_recv(struct i2c_adapter *adap, u8 *byte, u8 *flags)
  237. {
  238. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  239. int expected_status, status;
  240. /* compute expected status based on ACK bit in global control flags */
  241. if (*flags & MVTWSI_CONTROL_ACK)
  242. expected_status = MVTWSI_STATUS_DATA_R_ACK;
  243. else
  244. expected_status = MVTWSI_STATUS_DATA_R_NAK;
  245. /* acknowledge *previous state* and launch receive */
  246. writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  247. /* wait for controller to receive byte and assert ACK or NAK */
  248. status = twsi_wait(adap, expected_status);
  249. /* if we did receive expected byte then store it */
  250. if (status == 0)
  251. *byte = readl(&twsi->data);
  252. /* return status */
  253. return status;
  254. }
  255. /*
  256. * Assert the STOP condition.
  257. * This is also used to force the bus back in idle (SDA=SCL=1).
  258. */
  259. static int twsi_stop(struct i2c_adapter *adap, int status)
  260. {
  261. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  262. int control, stop_status;
  263. int timeout = 1000;
  264. /* assert STOP */
  265. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  266. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  267. /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
  268. do {
  269. stop_status = readl(&twsi->status);
  270. if (stop_status == MVTWSI_STATUS_IDLE)
  271. break;
  272. udelay(10); /* one clock cycle at 100 kHz */
  273. } while (timeout--);
  274. control = readl(&twsi->control);
  275. if (stop_status != MVTWSI_STATUS_IDLE)
  276. if (status == 0)
  277. status = mvtwsi_error(
  278. MVTWSI_ERROR_TIMEOUT,
  279. control, status, MVTWSI_STATUS_IDLE);
  280. return status;
  281. }
  282. static unsigned int twsi_calc_freq(const int n, const int m)
  283. {
  284. #ifdef CONFIG_SUNXI
  285. return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
  286. #else
  287. return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  288. #endif
  289. }
  290. /*
  291. * Reset controller.
  292. * Controller reset also resets the baud rate and slave address, so
  293. * they must be re-established afterwards.
  294. */
  295. static void twsi_reset(struct i2c_adapter *adap)
  296. {
  297. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  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. * I2C init called by cmd_i2c when doing 'i2c reset'.
  305. * Sets baud to the highest possible value not exceeding requested one.
  306. */
  307. static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
  308. unsigned int requested_speed)
  309. {
  310. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  311. unsigned int tmp_speed, highest_speed, n, m;
  312. unsigned int baud = 0x44; /* baudrate at controller reset */
  313. /* use actual speed to collect progressively higher values */
  314. highest_speed = 0;
  315. /* compute m, n setting for highest speed not above requested speed */
  316. for (n = 0; n < 8; n++) {
  317. for (m = 0; m < 16; m++) {
  318. tmp_speed = twsi_calc_freq(n, m);
  319. if ((tmp_speed <= requested_speed) &&
  320. (tmp_speed > highest_speed)) {
  321. highest_speed = tmp_speed;
  322. baud = (m << 3) | n;
  323. }
  324. }
  325. }
  326. writel(baud, &twsi->baudrate);
  327. return 0;
  328. }
  329. static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  330. {
  331. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  332. /* reset controller */
  333. twsi_reset(adap);
  334. /* set speed */
  335. twsi_i2c_set_bus_speed(adap, speed);
  336. /* set slave address even though we don't use it */
  337. writel(slaveadd, &twsi->slave_address);
  338. writel(0, &twsi->xtnd_slave_addr);
  339. /* assert STOP but don't care for the result */
  340. (void) twsi_stop(adap, 0);
  341. }
  342. /*
  343. * Begin I2C transaction with expected start status, at given address.
  344. * Common to i2c_probe, i2c_read and i2c_write.
  345. * Expected address status will derive from direction bit (bit 0) in addr.
  346. */
  347. static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
  348. u8 addr, u8 *flags)
  349. {
  350. int status, expected_addr_status;
  351. /* compute expected address status from direction bit in addr */
  352. if (addr & 1) /* reading */
  353. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  354. else /* writing */
  355. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  356. /* assert START */
  357. status = twsi_start(adap, expected_start_status, flags);
  358. /* send out the address if the start went well */
  359. if (status == 0)
  360. status = twsi_send(adap, addr, expected_addr_status,
  361. flags);
  362. /* return ok or status of first failure to caller */
  363. return status;
  364. }
  365. /*
  366. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  367. * Begin read, nak data byte, end.
  368. */
  369. static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
  370. {
  371. u8 dummy_byte;
  372. u8 flags = 0;
  373. int status;
  374. /* begin i2c read */
  375. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1, &flags);
  376. /* dummy read was accepted: receive byte but NAK it. */
  377. if (status == 0)
  378. status = twsi_recv(adap, &dummy_byte, &flags);
  379. /* Stop transaction */
  380. twsi_stop(adap, 0);
  381. /* return 0 or status of first failure */
  382. return status;
  383. }
  384. /*
  385. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  386. * Begin write, send address byte(s), begin read, receive data bytes, end.
  387. *
  388. * NOTE: some EEPROMS want a stop right before the second start, while
  389. * some will choke if it is there. Deciding which we should do is eeprom
  390. * stuff, not i2c, but at the moment the APIs won't let us put it in
  391. * cmd_eeprom, so we have to choose here, and for the moment that'll be
  392. * a repeated start without a preceding stop.
  393. */
  394. static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  395. int alen, uchar *data, int length)
  396. {
  397. int status;
  398. u8 flags = 0;
  399. /* begin i2c write to send the address bytes */
  400. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
  401. /* send addr bytes */
  402. while ((status == 0) && alen--)
  403. status = twsi_send(adap, addr >> (8*alen),
  404. MVTWSI_STATUS_DATA_W_ACK, &flags);
  405. /* begin i2c read to receive eeprom data bytes */
  406. if (status == 0)
  407. status = i2c_begin(adap, MVTWSI_STATUS_REPEATED_START,
  408. (chip << 1) | 1, &flags);
  409. /* prepare ACK if at least one byte must be received */
  410. if (length > 0)
  411. flags |= MVTWSI_CONTROL_ACK;
  412. /* now receive actual bytes */
  413. while ((status == 0) && length--) {
  414. /* reset NAK if we if no more to read now */
  415. if (length == 0)
  416. flags &= ~MVTWSI_CONTROL_ACK;
  417. /* read current byte */
  418. status = twsi_recv(adap, data++, &flags);
  419. }
  420. /* Stop transaction */
  421. status = twsi_stop(adap, status);
  422. /* return 0 or status of first failure */
  423. return status;
  424. }
  425. /*
  426. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  427. * Begin write, send address byte(s), send data bytes, end.
  428. */
  429. static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  430. int alen, uchar *data, int length)
  431. {
  432. int status;
  433. u8 flags = 0;
  434. /* begin i2c write to send the eeprom adress bytes then data bytes */
  435. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
  436. /* send addr bytes */
  437. while ((status == 0) && alen--)
  438. status = twsi_send(adap, addr >> (8*alen),
  439. MVTWSI_STATUS_DATA_W_ACK, &flags);
  440. /* send data bytes */
  441. while ((status == 0) && (length-- > 0))
  442. status = twsi_send(adap, *(data++), MVTWSI_STATUS_DATA_W_ACK,
  443. &flags);
  444. /* Stop transaction */
  445. status = twsi_stop(adap, status);
  446. /* return 0 or status of first failure */
  447. return status;
  448. }
  449. #ifdef CONFIG_I2C_MVTWSI_BASE0
  450. U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
  451. twsi_i2c_read, twsi_i2c_write,
  452. twsi_i2c_set_bus_speed,
  453. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  454. #endif
  455. #ifdef CONFIG_I2C_MVTWSI_BASE1
  456. U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
  457. twsi_i2c_read, twsi_i2c_write,
  458. twsi_i2c_set_bus_speed,
  459. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
  460. #endif
  461. #ifdef CONFIG_I2C_MVTWSI_BASE2
  462. U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
  463. twsi_i2c_read, twsi_i2c_write,
  464. twsi_i2c_set_bus_speed,
  465. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
  466. #endif
  467. #ifdef CONFIG_I2C_MVTWSI_BASE3
  468. U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
  469. twsi_i2c_read, twsi_i2c_write,
  470. twsi_i2c_set_bus_speed,
  471. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
  472. #endif
  473. #ifdef CONFIG_I2C_MVTWSI_BASE4
  474. U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
  475. twsi_i2c_read, twsi_i2c_write,
  476. twsi_i2c_set_bus_speed,
  477. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
  478. #endif
  479. #ifdef CONFIG_I2C_MVTWSI_BASE5
  480. U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
  481. twsi_i2c_read, twsi_i2c_write,
  482. twsi_i2c_set_bus_speed,
  483. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
  484. #endif