mvtwsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. * Control register fields
  56. */
  57. #define MVTWSI_CONTROL_ACK 0x00000004
  58. #define MVTWSI_CONTROL_IFLG 0x00000008
  59. #define MVTWSI_CONTROL_STOP 0x00000010
  60. #define MVTWSI_CONTROL_START 0x00000020
  61. #define MVTWSI_CONTROL_TWSIEN 0x00000040
  62. #define MVTWSI_CONTROL_INTEN 0x00000080
  63. /*
  64. * On sun6i and newer IFLG is a write-clear bit which is cleared by writing 1,
  65. * on other platforms it is a normal r/w bit which is cleared by writing 0.
  66. */
  67. #ifdef CONFIG_SUNXI_GEN_SUN6I
  68. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
  69. #else
  70. #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
  71. #endif
  72. /*
  73. * Status register values -- only those expected in normal master
  74. * operation on non-10-bit-address devices; whatever status we don't
  75. * expect in nominal conditions (bus errors, arbitration losses,
  76. * missing ACKs...) we just pass back to the caller as an error
  77. * code.
  78. */
  79. #define MVTWSI_STATUS_START 0x08
  80. #define MVTWSI_STATUS_REPEATED_START 0x10
  81. #define MVTWSI_STATUS_ADDR_W_ACK 0x18
  82. #define MVTWSI_STATUS_DATA_W_ACK 0x28
  83. #define MVTWSI_STATUS_ADDR_R_ACK 0x40
  84. #define MVTWSI_STATUS_ADDR_R_NAK 0x48
  85. #define MVTWSI_STATUS_DATA_R_ACK 0x50
  86. #define MVTWSI_STATUS_DATA_R_NAK 0x58
  87. #define MVTWSI_STATUS_IDLE 0xF8
  88. /*
  89. * MVTWSI controller base
  90. */
  91. static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
  92. {
  93. switch (adap->hwadapnr) {
  94. #ifdef CONFIG_I2C_MVTWSI_BASE0
  95. case 0:
  96. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
  97. #endif
  98. #ifdef CONFIG_I2C_MVTWSI_BASE1
  99. case 1:
  100. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
  101. #endif
  102. #ifdef CONFIG_I2C_MVTWSI_BASE2
  103. case 2:
  104. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
  105. #endif
  106. #ifdef CONFIG_I2C_MVTWSI_BASE3
  107. case 3:
  108. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
  109. #endif
  110. #ifdef CONFIG_I2C_MVTWSI_BASE4
  111. case 4:
  112. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
  113. #endif
  114. #ifdef CONFIG_I2C_MVTWSI_BASE5
  115. case 5:
  116. return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
  117. #endif
  118. default:
  119. printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
  120. break;
  121. }
  122. return NULL;
  123. }
  124. /*
  125. * Returned statuses are 0 for success and nonzero otherwise.
  126. * Currently, cmd_i2c and cmd_eeprom do not interpret an error status.
  127. * Thus to ease debugging, the return status contains some debug info:
  128. * - bits 31..24 are error class: 1 is timeout, 2 is 'status mismatch'.
  129. * - bits 23..16 are the last value of the control register.
  130. * - bits 15..8 are the last value of the status register.
  131. * - bits 7..0 are the expected value of the status register.
  132. */
  133. #define MVTWSI_ERROR_WRONG_STATUS 0x01
  134. #define MVTWSI_ERROR_TIMEOUT 0x02
  135. #define MVTWSI_ERROR(ec, lc, ls, es) (((ec << 24) & 0xFF000000) | \
  136. ((lc << 16) & 0x00FF0000) | ((ls<<8) & 0x0000FF00) | (es & 0xFF))
  137. /*
  138. * Wait for IFLG to raise, or return 'timeout'; then if status is as expected,
  139. * return 0 (ok) or return 'wrong status'.
  140. */
  141. static int twsi_wait(struct i2c_adapter *adap, int expected_status)
  142. {
  143. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  144. int control, status;
  145. int timeout = 1000;
  146. do {
  147. control = readl(&twsi->control);
  148. if (control & MVTWSI_CONTROL_IFLG) {
  149. status = readl(&twsi->status);
  150. if (status == expected_status)
  151. return 0;
  152. else
  153. return MVTWSI_ERROR(
  154. MVTWSI_ERROR_WRONG_STATUS,
  155. control, status, expected_status);
  156. }
  157. udelay(10); /* one clock cycle at 100 kHz */
  158. } while (timeout--);
  159. status = readl(&twsi->status);
  160. return MVTWSI_ERROR(
  161. MVTWSI_ERROR_TIMEOUT, control, status, expected_status);
  162. }
  163. /*
  164. * Assert the START condition, either in a single I2C transaction
  165. * or inside back-to-back ones (repeated starts).
  166. */
  167. static int twsi_start(struct i2c_adapter *adap, int expected_status, u8 *flags)
  168. {
  169. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  170. /* globally set TWSIEN in case it was not */
  171. *flags |= MVTWSI_CONTROL_TWSIEN;
  172. /* assert START */
  173. writel(*flags | MVTWSI_CONTROL_START |
  174. MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  175. /* wait for controller to process START */
  176. return twsi_wait(adap, expected_status);
  177. }
  178. /*
  179. * Send a byte (i2c address or data).
  180. */
  181. static int twsi_send(struct i2c_adapter *adap, u8 byte, int expected_status,
  182. u8 *flags)
  183. {
  184. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  185. /* put byte in data register for sending */
  186. writel(byte, &twsi->data);
  187. /* clear any pending interrupt -- that'll cause sending */
  188. writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  189. /* wait for controller to receive byte and check ACK */
  190. return twsi_wait(adap, expected_status);
  191. }
  192. /*
  193. * Receive a byte.
  194. * Global mvtwsi_control_flags variable says if we should ack or nak.
  195. */
  196. static int twsi_recv(struct i2c_adapter *adap, u8 *byte, u8 *flags)
  197. {
  198. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  199. int expected_status, status;
  200. /* compute expected status based on ACK bit in global control flags */
  201. if (*flags & MVTWSI_CONTROL_ACK)
  202. expected_status = MVTWSI_STATUS_DATA_R_ACK;
  203. else
  204. expected_status = MVTWSI_STATUS_DATA_R_NAK;
  205. /* acknowledge *previous state* and launch receive */
  206. writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  207. /* wait for controller to receive byte and assert ACK or NAK */
  208. status = twsi_wait(adap, expected_status);
  209. /* if we did receive expected byte then store it */
  210. if (status == 0)
  211. *byte = readl(&twsi->data);
  212. /* return status */
  213. return status;
  214. }
  215. /*
  216. * Assert the STOP condition.
  217. * This is also used to force the bus back in idle (SDA=SCL=1).
  218. */
  219. static int twsi_stop(struct i2c_adapter *adap, int status)
  220. {
  221. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  222. int control, stop_status;
  223. int timeout = 1000;
  224. /* assert STOP */
  225. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  226. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  227. /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
  228. do {
  229. stop_status = readl(&twsi->status);
  230. if (stop_status == MVTWSI_STATUS_IDLE)
  231. break;
  232. udelay(10); /* one clock cycle at 100 kHz */
  233. } while (timeout--);
  234. control = readl(&twsi->control);
  235. if (stop_status != MVTWSI_STATUS_IDLE)
  236. if (status == 0)
  237. status = MVTWSI_ERROR(
  238. MVTWSI_ERROR_TIMEOUT,
  239. control, status, MVTWSI_STATUS_IDLE);
  240. return status;
  241. }
  242. static unsigned int twsi_calc_freq(const int n, const int m)
  243. {
  244. #ifdef CONFIG_SUNXI
  245. return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
  246. #else
  247. return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  248. #endif
  249. }
  250. /*
  251. * Reset controller.
  252. * Controller reset also resets the baud rate and slave address, so
  253. * they must be re-established afterwards.
  254. */
  255. static void twsi_reset(struct i2c_adapter *adap)
  256. {
  257. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  258. /* reset controller */
  259. writel(0, &twsi->soft_reset);
  260. /* wait 2 ms -- this is what the Marvell LSP does */
  261. udelay(20000);
  262. }
  263. /*
  264. * I2C init called by cmd_i2c when doing 'i2c reset'.
  265. * Sets baud to the highest possible value not exceeding requested one.
  266. */
  267. static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
  268. unsigned int requested_speed)
  269. {
  270. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  271. unsigned int tmp_speed, highest_speed, n, m;
  272. unsigned int baud = 0x44; /* baudrate at controller reset */
  273. /* use actual speed to collect progressively higher values */
  274. highest_speed = 0;
  275. /* compute m, n setting for highest speed not above requested speed */
  276. for (n = 0; n < 8; n++) {
  277. for (m = 0; m < 16; m++) {
  278. tmp_speed = twsi_calc_freq(n, m);
  279. if ((tmp_speed <= requested_speed) &&
  280. (tmp_speed > highest_speed)) {
  281. highest_speed = tmp_speed;
  282. baud = (m << 3) | n;
  283. }
  284. }
  285. }
  286. writel(baud, &twsi->baudrate);
  287. return 0;
  288. }
  289. static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  290. {
  291. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  292. /* reset controller */
  293. twsi_reset(adap);
  294. /* set speed */
  295. twsi_i2c_set_bus_speed(adap, speed);
  296. /* set slave address even though we don't use it */
  297. writel(slaveadd, &twsi->slave_address);
  298. writel(0, &twsi->xtnd_slave_addr);
  299. /* assert STOP but don't care for the result */
  300. (void) twsi_stop(adap, 0);
  301. }
  302. /*
  303. * Begin I2C transaction with expected start status, at given address.
  304. * Common to i2c_probe, i2c_read and i2c_write.
  305. * Expected address status will derive from direction bit (bit 0) in addr.
  306. */
  307. static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
  308. u8 addr, u8 *flags)
  309. {
  310. int status, expected_addr_status;
  311. /* compute expected address status from direction bit in addr */
  312. if (addr & 1) /* reading */
  313. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  314. else /* writing */
  315. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  316. /* assert START */
  317. status = twsi_start(adap, expected_start_status, flags);
  318. /* send out the address if the start went well */
  319. if (status == 0)
  320. status = twsi_send(adap, addr, expected_addr_status,
  321. flags);
  322. /* return ok or status of first failure to caller */
  323. return status;
  324. }
  325. /*
  326. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  327. * Begin read, nak data byte, end.
  328. */
  329. static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
  330. {
  331. u8 dummy_byte;
  332. u8 flags = 0;
  333. int status;
  334. /* begin i2c read */
  335. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1, &flags);
  336. /* dummy read was accepted: receive byte but NAK it. */
  337. if (status == 0)
  338. status = twsi_recv(adap, &dummy_byte, &flags);
  339. /* Stop transaction */
  340. twsi_stop(adap, 0);
  341. /* return 0 or status of first failure */
  342. return status;
  343. }
  344. /*
  345. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  346. * Begin write, send address byte(s), begin read, receive data bytes, end.
  347. *
  348. * NOTE: some EEPROMS want a stop right before the second start, while
  349. * some will choke if it is there. Deciding which we should do is eeprom
  350. * stuff, not i2c, but at the moment the APIs won't let us put it in
  351. * cmd_eeprom, so we have to choose here, and for the moment that'll be
  352. * a repeated start without a preceding stop.
  353. */
  354. static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  355. int alen, uchar *data, int length)
  356. {
  357. int status;
  358. u8 flags = 0;
  359. /* begin i2c write to send the address bytes */
  360. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
  361. /* send addr bytes */
  362. while ((status == 0) && alen--)
  363. status = twsi_send(adap, addr >> (8*alen),
  364. MVTWSI_STATUS_DATA_W_ACK, &flags);
  365. /* begin i2c read to receive eeprom data bytes */
  366. if (status == 0)
  367. status = i2c_begin(adap, MVTWSI_STATUS_REPEATED_START,
  368. (chip << 1) | 1, &flags);
  369. /* prepare ACK if at least one byte must be received */
  370. if (length > 0)
  371. flags |= MVTWSI_CONTROL_ACK;
  372. /* now receive actual bytes */
  373. while ((status == 0) && length--) {
  374. /* reset NAK if we if no more to read now */
  375. if (length == 0)
  376. flags &= ~MVTWSI_CONTROL_ACK;
  377. /* read current byte */
  378. status = twsi_recv(adap, data++, &flags);
  379. }
  380. /* Stop transaction */
  381. status = twsi_stop(adap, status);
  382. /* return 0 or status of first failure */
  383. return status;
  384. }
  385. /*
  386. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  387. * Begin write, send address byte(s), send data bytes, end.
  388. */
  389. static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  390. int alen, uchar *data, int length)
  391. {
  392. int status;
  393. u8 flags = 0;
  394. /* begin i2c write to send the eeprom adress bytes then data bytes */
  395. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
  396. /* send addr bytes */
  397. while ((status == 0) && alen--)
  398. status = twsi_send(adap, addr >> (8*alen),
  399. MVTWSI_STATUS_DATA_W_ACK, &flags);
  400. /* send data bytes */
  401. while ((status == 0) && (length-- > 0))
  402. status = twsi_send(adap, *(data++), MVTWSI_STATUS_DATA_W_ACK,
  403. &flags);
  404. /* Stop transaction */
  405. status = twsi_stop(adap, status);
  406. /* return 0 or status of first failure */
  407. return status;
  408. }
  409. #ifdef CONFIG_I2C_MVTWSI_BASE0
  410. U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
  411. twsi_i2c_read, twsi_i2c_write,
  412. twsi_i2c_set_bus_speed,
  413. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  414. #endif
  415. #ifdef CONFIG_I2C_MVTWSI_BASE1
  416. U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
  417. twsi_i2c_read, twsi_i2c_write,
  418. twsi_i2c_set_bus_speed,
  419. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
  420. #endif
  421. #ifdef CONFIG_I2C_MVTWSI_BASE2
  422. U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
  423. twsi_i2c_read, twsi_i2c_write,
  424. twsi_i2c_set_bus_speed,
  425. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
  426. #endif
  427. #ifdef CONFIG_I2C_MVTWSI_BASE3
  428. U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
  429. twsi_i2c_read, twsi_i2c_write,
  430. twsi_i2c_set_bus_speed,
  431. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
  432. #endif
  433. #ifdef CONFIG_I2C_MVTWSI_BASE4
  434. U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
  435. twsi_i2c_read, twsi_i2c_write,
  436. twsi_i2c_set_bus_speed,
  437. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
  438. #endif
  439. #ifdef CONFIG_I2C_MVTWSI_BASE5
  440. U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
  441. twsi_i2c_read, twsi_i2c_write,
  442. twsi_i2c_set_bus_speed,
  443. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
  444. #endif