mvtwsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. * These flags are ORed to any write to the control register
  165. * They allow global setting of TWSIEN and ACK.
  166. * By default none are set.
  167. * twsi_start() sets TWSIEN (in case the controller was disabled)
  168. * twsi_recv() sets ACK or resets it depending on expected status.
  169. */
  170. static u8 twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
  171. /*
  172. * Assert the START condition, either in a single I2C transaction
  173. * or inside back-to-back ones (repeated starts).
  174. */
  175. static int twsi_start(struct i2c_adapter *adap, int expected_status)
  176. {
  177. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  178. /* globally set TWSIEN in case it was not */
  179. twsi_control_flags |= MVTWSI_CONTROL_TWSIEN;
  180. /* assert START */
  181. writel(twsi_control_flags | MVTWSI_CONTROL_START |
  182. MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  183. /* wait for controller to process START */
  184. return twsi_wait(adap, expected_status);
  185. }
  186. /*
  187. * Send a byte (i2c address or data).
  188. */
  189. static int twsi_send(struct i2c_adapter *adap, u8 byte, int expected_status)
  190. {
  191. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  192. /* put byte in data register for sending */
  193. writel(byte, &twsi->data);
  194. /* clear any pending interrupt -- that'll cause sending */
  195. writel(twsi_control_flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  196. /* wait for controller to receive byte and check ACK */
  197. return twsi_wait(adap, expected_status);
  198. }
  199. /*
  200. * Receive a byte.
  201. * Global mvtwsi_control_flags variable says if we should ack or nak.
  202. */
  203. static int twsi_recv(struct i2c_adapter *adap, u8 *byte)
  204. {
  205. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  206. int expected_status, status;
  207. /* compute expected status based on ACK bit in global control flags */
  208. if (twsi_control_flags & MVTWSI_CONTROL_ACK)
  209. expected_status = MVTWSI_STATUS_DATA_R_ACK;
  210. else
  211. expected_status = MVTWSI_STATUS_DATA_R_NAK;
  212. /* acknowledge *previous state* and launch receive */
  213. writel(twsi_control_flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  214. /* wait for controller to receive byte and assert ACK or NAK */
  215. status = twsi_wait(adap, expected_status);
  216. /* if we did receive expected byte then store it */
  217. if (status == 0)
  218. *byte = readl(&twsi->data);
  219. /* return status */
  220. return status;
  221. }
  222. /*
  223. * Assert the STOP condition.
  224. * This is also used to force the bus back in idle (SDA=SCL=1).
  225. */
  226. static int twsi_stop(struct i2c_adapter *adap, int status)
  227. {
  228. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  229. int control, stop_status;
  230. int timeout = 1000;
  231. /* assert STOP */
  232. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  233. writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
  234. /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
  235. do {
  236. stop_status = readl(&twsi->status);
  237. if (stop_status == MVTWSI_STATUS_IDLE)
  238. break;
  239. udelay(10); /* one clock cycle at 100 kHz */
  240. } while (timeout--);
  241. control = readl(&twsi->control);
  242. if (stop_status != MVTWSI_STATUS_IDLE)
  243. if (status == 0)
  244. status = MVTWSI_ERROR(
  245. MVTWSI_ERROR_TIMEOUT,
  246. control, status, MVTWSI_STATUS_IDLE);
  247. return status;
  248. }
  249. static unsigned int twsi_calc_freq(const int n, const int m)
  250. {
  251. #ifdef CONFIG_SUNXI
  252. return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
  253. #else
  254. return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  255. #endif
  256. }
  257. /*
  258. * Reset controller.
  259. * Controller reset also resets the baud rate and slave address, so
  260. * they must be re-established afterwards.
  261. */
  262. static void twsi_reset(struct i2c_adapter *adap)
  263. {
  264. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  265. /* ensure controller will be enabled by any twsi*() function */
  266. twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
  267. /* reset controller */
  268. writel(0, &twsi->soft_reset);
  269. /* wait 2 ms -- this is what the Marvell LSP does */
  270. udelay(20000);
  271. }
  272. /*
  273. * I2C init called by cmd_i2c when doing 'i2c reset'.
  274. * Sets baud to the highest possible value not exceeding requested one.
  275. */
  276. static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
  277. unsigned int requested_speed)
  278. {
  279. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  280. unsigned int tmp_speed, highest_speed, n, m;
  281. unsigned int baud = 0x44; /* baudrate at controller reset */
  282. /* use actual speed to collect progressively higher values */
  283. highest_speed = 0;
  284. /* compute m, n setting for highest speed not above requested speed */
  285. for (n = 0; n < 8; n++) {
  286. for (m = 0; m < 16; m++) {
  287. tmp_speed = twsi_calc_freq(n, m);
  288. if ((tmp_speed <= requested_speed)
  289. && (tmp_speed > highest_speed)) {
  290. highest_speed = tmp_speed;
  291. baud = (m << 3) | n;
  292. }
  293. }
  294. }
  295. writel(baud, &twsi->baudrate);
  296. return 0;
  297. }
  298. static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  299. {
  300. struct mvtwsi_registers *twsi = twsi_get_base(adap);
  301. /* reset controller */
  302. twsi_reset(adap);
  303. /* set speed */
  304. twsi_i2c_set_bus_speed(adap, speed);
  305. /* set slave address even though we don't use it */
  306. writel(slaveadd, &twsi->slave_address);
  307. writel(0, &twsi->xtnd_slave_addr);
  308. /* assert STOP but don't care for the result */
  309. (void) twsi_stop(adap, 0);
  310. }
  311. /*
  312. * Begin I2C transaction with expected start status, at given address.
  313. * Common to i2c_probe, i2c_read and i2c_write.
  314. * Expected address status will derive from direction bit (bit 0) in addr.
  315. */
  316. static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
  317. u8 addr)
  318. {
  319. int status, expected_addr_status;
  320. /* compute expected address status from direction bit in addr */
  321. if (addr & 1) /* reading */
  322. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  323. else /* writing */
  324. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  325. /* assert START */
  326. status = twsi_start(adap, expected_start_status);
  327. /* send out the address if the start went well */
  328. if (status == 0)
  329. status = twsi_send(adap, addr, expected_addr_status);
  330. /* return ok or status of first failure to caller */
  331. return status;
  332. }
  333. /*
  334. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  335. * Begin read, nak data byte, end.
  336. */
  337. static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
  338. {
  339. u8 dummy_byte;
  340. int status;
  341. /* begin i2c read */
  342. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1);
  343. /* dummy read was accepted: receive byte but NAK it. */
  344. if (status == 0)
  345. status = twsi_recv(adap, &dummy_byte);
  346. /* Stop transaction */
  347. twsi_stop(adap, 0);
  348. /* return 0 or status of first failure */
  349. return status;
  350. }
  351. /*
  352. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  353. * Begin write, send address byte(s), begin read, receive data bytes, end.
  354. *
  355. * NOTE: some EEPROMS want a stop right before the second start, while
  356. * some will choke if it is there. Deciding which we should do is eeprom
  357. * stuff, not i2c, but at the moment the APIs won't let us put it in
  358. * cmd_eeprom, so we have to choose here, and for the moment that'll be
  359. * a repeated start without a preceding stop.
  360. */
  361. static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  362. int alen, uchar *data, int length)
  363. {
  364. int status;
  365. /* begin i2c write to send the address bytes */
  366. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1));
  367. /* send addr bytes */
  368. while ((status == 0) && alen--)
  369. status = twsi_send(adap, addr >> (8*alen),
  370. MVTWSI_STATUS_DATA_W_ACK);
  371. /* begin i2c read to receive eeprom data bytes */
  372. if (status == 0)
  373. status = i2c_begin(adap, MVTWSI_STATUS_REPEATED_START,
  374. (chip << 1) | 1);
  375. /* prepare ACK if at least one byte must be received */
  376. if (length > 0)
  377. twsi_control_flags |= MVTWSI_CONTROL_ACK;
  378. /* now receive actual bytes */
  379. while ((status == 0) && length--) {
  380. /* reset NAK if we if no more to read now */
  381. if (length == 0)
  382. twsi_control_flags &= ~MVTWSI_CONTROL_ACK;
  383. /* read current byte */
  384. status = twsi_recv(adap, data++);
  385. }
  386. /* Stop transaction */
  387. status = twsi_stop(adap, status);
  388. /* return 0 or status of first failure */
  389. return status;
  390. }
  391. /*
  392. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  393. * Begin write, send address byte(s), send data bytes, end.
  394. */
  395. static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  396. int alen, uchar *data, int length)
  397. {
  398. int status;
  399. /* begin i2c write to send the eeprom adress bytes then data bytes */
  400. status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1));
  401. /* send addr bytes */
  402. while ((status == 0) && alen--)
  403. status = twsi_send(adap, addr >> (8*alen),
  404. MVTWSI_STATUS_DATA_W_ACK);
  405. /* send data bytes */
  406. while ((status == 0) && (length-- > 0))
  407. status = twsi_send(adap, *(data++), MVTWSI_STATUS_DATA_W_ACK);
  408. /* Stop transaction */
  409. status = twsi_stop(adap, status);
  410. /* return 0 or status of first failure */
  411. return status;
  412. }
  413. #ifdef CONFIG_I2C_MVTWSI_BASE0
  414. U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
  415. twsi_i2c_read, twsi_i2c_write,
  416. twsi_i2c_set_bus_speed,
  417. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
  418. #endif
  419. #ifdef CONFIG_I2C_MVTWSI_BASE1
  420. U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
  421. twsi_i2c_read, twsi_i2c_write,
  422. twsi_i2c_set_bus_speed,
  423. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
  424. #endif
  425. #ifdef CONFIG_I2C_MVTWSI_BASE2
  426. U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
  427. twsi_i2c_read, twsi_i2c_write,
  428. twsi_i2c_set_bus_speed,
  429. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
  430. #endif
  431. #ifdef CONFIG_I2C_MVTWSI_BASE3
  432. U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
  433. twsi_i2c_read, twsi_i2c_write,
  434. twsi_i2c_set_bus_speed,
  435. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
  436. #endif
  437. #ifdef CONFIG_I2C_MVTWSI_BASE4
  438. U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
  439. twsi_i2c_read, twsi_i2c_write,
  440. twsi_i2c_set_bus_speed,
  441. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
  442. #endif
  443. #ifdef CONFIG_I2C_MVTWSI_BASE5
  444. U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
  445. twsi_i2c_read, twsi_i2c_write,
  446. twsi_i2c_set_bus_speed,
  447. CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
  448. #endif