mvtwsi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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)
  21. #include <asm/arch/kirkwood.h>
  22. #else
  23. #error Driver mvtwsi not supported by SoC or board
  24. #endif
  25. /*
  26. * TWSI register structure
  27. */
  28. struct mvtwsi_registers {
  29. u32 slave_address;
  30. u32 data;
  31. u32 control;
  32. union {
  33. u32 status; /* when reading */
  34. u32 baudrate; /* when writing */
  35. };
  36. u32 xtnd_slave_addr;
  37. u32 reserved[2];
  38. u32 soft_reset;
  39. };
  40. /*
  41. * Control register fields
  42. */
  43. #define MVTWSI_CONTROL_ACK 0x00000004
  44. #define MVTWSI_CONTROL_IFLG 0x00000008
  45. #define MVTWSI_CONTROL_STOP 0x00000010
  46. #define MVTWSI_CONTROL_START 0x00000020
  47. #define MVTWSI_CONTROL_TWSIEN 0x00000040
  48. #define MVTWSI_CONTROL_INTEN 0x00000080
  49. /*
  50. * Status register values -- only those expected in normal master
  51. * operation on non-10-bit-address devices; whatever status we don't
  52. * expect in nominal conditions (bus errors, arbitration losses,
  53. * missing ACKs...) we just pass back to the caller as an error
  54. * code.
  55. */
  56. #define MVTWSI_STATUS_START 0x08
  57. #define MVTWSI_STATUS_REPEATED_START 0x10
  58. #define MVTWSI_STATUS_ADDR_W_ACK 0x18
  59. #define MVTWSI_STATUS_DATA_W_ACK 0x28
  60. #define MVTWSI_STATUS_ADDR_R_ACK 0x40
  61. #define MVTWSI_STATUS_ADDR_R_NAK 0x48
  62. #define MVTWSI_STATUS_DATA_R_ACK 0x50
  63. #define MVTWSI_STATUS_DATA_R_NAK 0x58
  64. #define MVTWSI_STATUS_IDLE 0xF8
  65. /*
  66. * The single instance of the controller we'll be dealing with
  67. */
  68. static struct mvtwsi_registers *twsi =
  69. (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE;
  70. /*
  71. * Returned statuses are 0 for success and nonzero otherwise.
  72. * Currently, cmd_i2c and cmd_eeprom do not interpret an error status.
  73. * Thus to ease debugging, the return status contains some debug info:
  74. * - bits 31..24 are error class: 1 is timeout, 2 is 'status mismatch'.
  75. * - bits 23..16 are the last value of the control register.
  76. * - bits 15..8 are the last value of the status register.
  77. * - bits 7..0 are the expected value of the status register.
  78. */
  79. #define MVTWSI_ERROR_WRONG_STATUS 0x01
  80. #define MVTWSI_ERROR_TIMEOUT 0x02
  81. #define MVTWSI_ERROR(ec, lc, ls, es) (((ec << 24) & 0xFF000000) | \
  82. ((lc << 16) & 0x00FF0000) | ((ls<<8) & 0x0000FF00) | (es & 0xFF))
  83. /*
  84. * Wait for IFLG to raise, or return 'timeout'; then if status is as expected,
  85. * return 0 (ok) or return 'wrong status'.
  86. */
  87. static int twsi_wait(int expected_status)
  88. {
  89. int control, status;
  90. int timeout = 1000;
  91. do {
  92. control = readl(&twsi->control);
  93. if (control & MVTWSI_CONTROL_IFLG) {
  94. status = readl(&twsi->status);
  95. if (status == expected_status)
  96. return 0;
  97. else
  98. return MVTWSI_ERROR(
  99. MVTWSI_ERROR_WRONG_STATUS,
  100. control, status, expected_status);
  101. }
  102. udelay(10); /* one clock cycle at 100 kHz */
  103. } while (timeout--);
  104. status = readl(&twsi->status);
  105. return MVTWSI_ERROR(
  106. MVTWSI_ERROR_TIMEOUT, control, status, expected_status);
  107. }
  108. /*
  109. * These flags are ORed to any write to the control register
  110. * They allow global setting of TWSIEN and ACK.
  111. * By default none are set.
  112. * twsi_start() sets TWSIEN (in case the controller was disabled)
  113. * twsi_recv() sets ACK or resets it depending on expected status.
  114. */
  115. static u8 twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
  116. /*
  117. * Assert the START condition, either in a single I2C transaction
  118. * or inside back-to-back ones (repeated starts).
  119. */
  120. static int twsi_start(int expected_status)
  121. {
  122. /* globally set TWSIEN in case it was not */
  123. twsi_control_flags |= MVTWSI_CONTROL_TWSIEN;
  124. /* assert START */
  125. writel(twsi_control_flags | MVTWSI_CONTROL_START, &twsi->control);
  126. /* wait for controller to process START */
  127. return twsi_wait(expected_status);
  128. }
  129. /*
  130. * Send a byte (i2c address or data).
  131. */
  132. static int twsi_send(u8 byte, int expected_status)
  133. {
  134. /* put byte in data register for sending */
  135. writel(byte, &twsi->data);
  136. /* clear any pending interrupt -- that'll cause sending */
  137. writel(twsi_control_flags, &twsi->control);
  138. /* wait for controller to receive byte and check ACK */
  139. return twsi_wait(expected_status);
  140. }
  141. /*
  142. * Receive a byte.
  143. * Global mvtwsi_control_flags variable says if we should ack or nak.
  144. */
  145. static int twsi_recv(u8 *byte)
  146. {
  147. int expected_status, status;
  148. /* compute expected status based on ACK bit in global control flags */
  149. if (twsi_control_flags & MVTWSI_CONTROL_ACK)
  150. expected_status = MVTWSI_STATUS_DATA_R_ACK;
  151. else
  152. expected_status = MVTWSI_STATUS_DATA_R_NAK;
  153. /* acknowledge *previous state* and launch receive */
  154. writel(twsi_control_flags, &twsi->control);
  155. /* wait for controller to receive byte and assert ACK or NAK */
  156. status = twsi_wait(expected_status);
  157. /* if we did receive expected byte then store it */
  158. if (status == 0)
  159. *byte = readl(&twsi->data);
  160. /* return status */
  161. return status;
  162. }
  163. /*
  164. * Assert the STOP condition.
  165. * This is also used to force the bus back in idle (SDA=SCL=1).
  166. */
  167. static int twsi_stop(int status)
  168. {
  169. int control, stop_status;
  170. int timeout = 1000;
  171. /* assert STOP */
  172. control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
  173. writel(control, &twsi->control);
  174. /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
  175. do {
  176. stop_status = readl(&twsi->status);
  177. if (stop_status == MVTWSI_STATUS_IDLE)
  178. break;
  179. udelay(10); /* one clock cycle at 100 kHz */
  180. } while (timeout--);
  181. control = readl(&twsi->control);
  182. if (stop_status != MVTWSI_STATUS_IDLE)
  183. if (status == 0)
  184. status = MVTWSI_ERROR(
  185. MVTWSI_ERROR_TIMEOUT,
  186. control, status, MVTWSI_STATUS_IDLE);
  187. return status;
  188. }
  189. /*
  190. * Ugly formula to convert m and n values to a frequency comes from
  191. * TWSI specifications
  192. */
  193. #define TWSI_FREQUENCY(m, n) \
  194. ((u8) (CONFIG_SYS_TCLK / (10 * (m + 1) * 2 * (1 << n))))
  195. /*
  196. * These are required to be reprogrammed before enabling the controller
  197. * because a reset loses them.
  198. * Default values come from the spec, but a twsi_reset will change them.
  199. * twsi_slave_address left uninitialized lest checkpatch.pl complains.
  200. */
  201. /* Baudrate generator: m (bits 7..4) =4, n (bits 3..0) =4 */
  202. static u8 twsi_baud_rate = 0x44; /* baudrate at controller reset */
  203. /* Default frequency corresponding to default m=4, n=4 */
  204. static u8 twsi_actual_speed = TWSI_FREQUENCY(4, 4);
  205. /* Default slave address is 0 (so is an uninitialized static) */
  206. static u8 twsi_slave_address;
  207. /*
  208. * Reset controller.
  209. * Called at end of i2c_init unsuccessful i2c transactions.
  210. * Controller reset also resets the baud rate and slave address, so
  211. * re-establish them.
  212. */
  213. static void twsi_reset(void)
  214. {
  215. /* ensure controller will be enabled by any twsi*() function */
  216. twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
  217. /* reset controller */
  218. writel(0, &twsi->soft_reset);
  219. /* wait 2 ms -- this is what the Marvell LSP does */
  220. udelay(20000);
  221. /* set baud rate */
  222. writel(twsi_baud_rate, &twsi->baudrate);
  223. /* set slave address even though we don't use it */
  224. writel(twsi_slave_address, &twsi->slave_address);
  225. writel(0, &twsi->xtnd_slave_addr);
  226. /* assert STOP but don't care for the result */
  227. (void) twsi_stop(0);
  228. }
  229. /*
  230. * I2C init called by cmd_i2c when doing 'i2c reset'.
  231. * Sets baud to the highest possible value not exceeding requested one.
  232. */
  233. void i2c_init(int requested_speed, int slaveadd)
  234. {
  235. int tmp_speed, highest_speed, n, m;
  236. int baud = 0x44; /* baudrate at controller reset */
  237. /* use actual speed to collect progressively higher values */
  238. highest_speed = 0;
  239. /* compute m, n setting for highest speed not above requested speed */
  240. for (n = 0; n < 8; n++) {
  241. for (m = 0; m < 16; m++) {
  242. tmp_speed = TWSI_FREQUENCY(m, n);
  243. if ((tmp_speed <= requested_speed)
  244. && (tmp_speed > highest_speed)) {
  245. highest_speed = tmp_speed;
  246. baud = (m << 3) | n;
  247. }
  248. }
  249. }
  250. /* save baud rate and slave for later calls to twsi_reset */
  251. twsi_baud_rate = baud;
  252. twsi_actual_speed = highest_speed;
  253. twsi_slave_address = slaveadd;
  254. /* reset controller */
  255. twsi_reset();
  256. }
  257. /*
  258. * Begin I2C transaction with expected start status, at given address.
  259. * Common to i2c_probe, i2c_read and i2c_write.
  260. * Expected address status will derive from direction bit (bit 0) in addr.
  261. */
  262. static int i2c_begin(int expected_start_status, u8 addr)
  263. {
  264. int status, expected_addr_status;
  265. /* compute expected address status from direction bit in addr */
  266. if (addr & 1) /* reading */
  267. expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
  268. else /* writing */
  269. expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
  270. /* assert START */
  271. status = twsi_start(expected_start_status);
  272. /* send out the address if the start went well */
  273. if (status == 0)
  274. status = twsi_send(addr, expected_addr_status);
  275. /* return ok or status of first failure to caller */
  276. return status;
  277. }
  278. /*
  279. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  280. * Begin read, nak data byte, end.
  281. */
  282. int i2c_probe(uchar chip)
  283. {
  284. u8 dummy_byte;
  285. int status;
  286. /* begin i2c read */
  287. status = i2c_begin(MVTWSI_STATUS_START, (chip << 1) | 1);
  288. /* dummy read was accepted: receive byte but NAK it. */
  289. if (status == 0)
  290. status = twsi_recv(&dummy_byte);
  291. /* Stop transaction */
  292. twsi_stop(0);
  293. /* return 0 or status of first failure */
  294. return status;
  295. }
  296. /*
  297. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  298. * Begin write, send address byte(s), begin read, receive data bytes, end.
  299. *
  300. * NOTE: some EEPROMS want a stop right before the second start, while
  301. * some will choke if it is there. Deciding which we should do is eeprom
  302. * stuff, not i2c, but at the moment the APIs won't let us put it in
  303. * cmd_eeprom, so we have to choose here, and for the moment that'll be
  304. * a repeated start without a preceding stop.
  305. */
  306. int i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
  307. {
  308. int status;
  309. /* begin i2c write to send the address bytes */
  310. status = i2c_begin(MVTWSI_STATUS_START, (dev << 1));
  311. /* send addr bytes */
  312. while ((status == 0) && alen--)
  313. status = twsi_send(addr >> (8*alen),
  314. MVTWSI_STATUS_DATA_W_ACK);
  315. /* begin i2c read to receive eeprom data bytes */
  316. if (status == 0)
  317. status = i2c_begin(
  318. MVTWSI_STATUS_REPEATED_START, (dev << 1) | 1);
  319. /* prepare ACK if at least one byte must be received */
  320. if (length > 0)
  321. twsi_control_flags |= MVTWSI_CONTROL_ACK;
  322. /* now receive actual bytes */
  323. while ((status == 0) && length--) {
  324. /* reset NAK if we if no more to read now */
  325. if (length == 0)
  326. twsi_control_flags &= ~MVTWSI_CONTROL_ACK;
  327. /* read current byte */
  328. status = twsi_recv(data++);
  329. }
  330. /* Stop transaction */
  331. status = twsi_stop(status);
  332. /* return 0 or status of first failure */
  333. return status;
  334. }
  335. /*
  336. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  337. * Begin write, send address byte(s), send data bytes, end.
  338. */
  339. int i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
  340. {
  341. int status;
  342. /* begin i2c write to send the eeprom adress bytes then data bytes */
  343. status = i2c_begin(MVTWSI_STATUS_START, (dev << 1));
  344. /* send addr bytes */
  345. while ((status == 0) && alen--)
  346. status = twsi_send(addr >> (8*alen),
  347. MVTWSI_STATUS_DATA_W_ACK);
  348. /* send data bytes */
  349. while ((status == 0) && (length-- > 0))
  350. status = twsi_send(*(data++), MVTWSI_STATUS_DATA_W_ACK);
  351. /* Stop transaction */
  352. status = twsi_stop(status);
  353. /* return 0 or status of first failure */
  354. return status;
  355. }
  356. /*
  357. * Bus set routine: we only support bus 0.
  358. */
  359. int i2c_set_bus_num(unsigned int bus)
  360. {
  361. if (bus > 0) {
  362. return -1;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * Bus get routine: hard-return bus 0.
  368. */
  369. unsigned int i2c_get_bus_num(void)
  370. {
  371. return 0;
  372. }