mvtwsi.c 13 KB

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