mvtwsi.c 11 KB

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