s3c24x0_i2c.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * (C) Copyright 2002
  3. * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /* This code should work for both the S3C2400 and the S3C2410
  8. * as they seem to have the same I2C controller inside.
  9. * The different address mapping is handled by the s3c24xx.h files below.
  10. */
  11. #include <common.h>
  12. #include <errno.h>
  13. #include <dm.h>
  14. #include <fdtdec.h>
  15. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  16. #include <asm/arch/clk.h>
  17. #include <asm/arch/cpu.h>
  18. #include <asm/arch/pinmux.h>
  19. #else
  20. #include <asm/arch/s3c24x0_cpu.h>
  21. #endif
  22. #include <asm/io.h>
  23. #include <i2c.h>
  24. #include "s3c24x0_i2c.h"
  25. #ifndef CONFIG_SYS_I2C_S3C24X0_SLAVE
  26. #define SYS_I2C_S3C24X0_SLAVE_ADDR 0
  27. #else
  28. #define SYS_I2C_S3C24X0_SLAVE_ADDR CONFIG_SYS_I2C_S3C24X0_SLAVE
  29. #endif
  30. DECLARE_GLOBAL_DATA_PTR;
  31. /*
  32. * Wait til the byte transfer is completed.
  33. *
  34. * @param i2c- pointer to the appropriate i2c register bank.
  35. * @return I2C_OK, if transmission was ACKED
  36. * I2C_NACK, if transmission was NACKED
  37. * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
  38. */
  39. static int WaitForXfer(struct s3c24x0_i2c *i2c)
  40. {
  41. ulong start_time = get_timer(0);
  42. do {
  43. if (readl(&i2c->iiccon) & I2CCON_IRPND)
  44. return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
  45. I2C_NACK : I2C_OK;
  46. } while (get_timer(start_time) < I2C_TIMEOUT_MS);
  47. return I2C_NOK_TOUT;
  48. }
  49. static void read_write_byte(struct s3c24x0_i2c *i2c)
  50. {
  51. clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
  52. }
  53. static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
  54. {
  55. ulong freq, pres = 16, div;
  56. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  57. freq = get_i2c_clk();
  58. #else
  59. freq = get_PCLK();
  60. #endif
  61. /* calculate prescaler and divisor values */
  62. if ((freq / pres / (16 + 1)) > speed)
  63. /* set prescaler to 512 */
  64. pres = 512;
  65. div = 0;
  66. while ((freq / pres / (div + 1)) > speed)
  67. div++;
  68. /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
  69. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
  70. /* init to SLAVE REVEIVE and set slaveaddr */
  71. writel(0, &i2c->iicstat);
  72. writel(slaveadd, &i2c->iicadd);
  73. /* program Master Transmit (and implicit STOP) */
  74. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  75. }
  76. static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  77. {
  78. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  79. i2c_bus->clock_frequency = speed;
  80. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  81. SYS_I2C_S3C24X0_SLAVE_ADDR);
  82. return 0;
  83. }
  84. /*
  85. * cmd_type is 0 for write, 1 for read.
  86. *
  87. * addr_len can take any value from 0-255, it is only limited
  88. * by the char, we could make it larger if needed. If it is
  89. * 0 we skip the address write cycle.
  90. */
  91. static int i2c_transfer(struct s3c24x0_i2c *i2c,
  92. unsigned char cmd_type,
  93. unsigned char chip,
  94. unsigned char addr[],
  95. unsigned char addr_len,
  96. unsigned char data[],
  97. unsigned short data_len)
  98. {
  99. int i = 0, result;
  100. ulong start_time = get_timer(0);
  101. if (data == 0 || data_len == 0) {
  102. /*Don't support data transfer of no length or to address 0 */
  103. debug("i2c_transfer: bad call\n");
  104. return I2C_NOK;
  105. }
  106. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  107. if (get_timer(start_time) > I2C_TIMEOUT_MS)
  108. return I2C_NOK_TOUT;
  109. }
  110. writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
  111. /* Get the slave chip address going */
  112. writel(chip, &i2c->iicds);
  113. if ((cmd_type == I2C_WRITE) || (addr && addr_len))
  114. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  115. &i2c->iicstat);
  116. else
  117. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  118. &i2c->iicstat);
  119. /* Wait for chip address to transmit. */
  120. result = WaitForXfer(i2c);
  121. if (result != I2C_OK)
  122. goto bailout;
  123. /* If register address needs to be transmitted - do it now. */
  124. if (addr && addr_len) {
  125. while ((i < addr_len) && (result == I2C_OK)) {
  126. writel(addr[i++], &i2c->iicds);
  127. read_write_byte(i2c);
  128. result = WaitForXfer(i2c);
  129. }
  130. i = 0;
  131. if (result != I2C_OK)
  132. goto bailout;
  133. }
  134. switch (cmd_type) {
  135. case I2C_WRITE:
  136. while ((i < data_len) && (result == I2C_OK)) {
  137. writel(data[i++], &i2c->iicds);
  138. read_write_byte(i2c);
  139. result = WaitForXfer(i2c);
  140. }
  141. break;
  142. case I2C_READ:
  143. if (addr && addr_len) {
  144. /*
  145. * Register address has been sent, now send slave chip
  146. * address again to start the actual read transaction.
  147. */
  148. writel(chip, &i2c->iicds);
  149. /* Generate a re-START. */
  150. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  151. &i2c->iicstat);
  152. read_write_byte(i2c);
  153. result = WaitForXfer(i2c);
  154. if (result != I2C_OK)
  155. goto bailout;
  156. }
  157. while ((i < data_len) && (result == I2C_OK)) {
  158. /* disable ACK for final READ */
  159. if (i == data_len - 1)
  160. writel(readl(&i2c->iiccon)
  161. & ~I2CCON_ACKGEN,
  162. &i2c->iiccon);
  163. read_write_byte(i2c);
  164. result = WaitForXfer(i2c);
  165. data[i++] = readl(&i2c->iicds);
  166. }
  167. if (result == I2C_NACK)
  168. result = I2C_OK; /* Normal terminated read. */
  169. break;
  170. default:
  171. debug("i2c_transfer: bad call\n");
  172. result = I2C_NOK;
  173. break;
  174. }
  175. bailout:
  176. /* Send STOP. */
  177. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  178. read_write_byte(i2c);
  179. return result;
  180. }
  181. static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
  182. {
  183. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  184. uchar buf[1];
  185. int ret;
  186. buf[0] = 0;
  187. /*
  188. * What is needed is to send the chip address and verify that the
  189. * address was <ACK>ed (i.e. there was a chip at that address which
  190. * drove the data line low).
  191. */
  192. ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
  193. return ret != I2C_OK;
  194. }
  195. static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
  196. int seq)
  197. {
  198. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  199. bool is_read = msg->flags & I2C_M_RD;
  200. uint status;
  201. uint addr;
  202. int ret, i;
  203. if (!seq)
  204. setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  205. /* Get the slave chip address going */
  206. addr = msg->addr << 1;
  207. writel(addr, &i2c->iicds);
  208. status = I2C_TXRX_ENA | I2C_START_STOP;
  209. if (is_read)
  210. status |= I2C_MODE_MR;
  211. else
  212. status |= I2C_MODE_MT;
  213. writel(status, &i2c->iicstat);
  214. if (seq)
  215. read_write_byte(i2c);
  216. /* Wait for chip address to transmit */
  217. ret = WaitForXfer(i2c);
  218. if (ret)
  219. goto err;
  220. if (is_read) {
  221. for (i = 0; !ret && i < msg->len; i++) {
  222. /* disable ACK for final READ */
  223. if (i == msg->len - 1)
  224. clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  225. read_write_byte(i2c);
  226. ret = WaitForXfer(i2c);
  227. msg->buf[i] = readl(&i2c->iicds);
  228. }
  229. if (ret == I2C_NACK)
  230. ret = I2C_OK; /* Normal terminated read */
  231. } else {
  232. for (i = 0; !ret && i < msg->len; i++) {
  233. writel(msg->buf[i], &i2c->iicds);
  234. read_write_byte(i2c);
  235. ret = WaitForXfer(i2c);
  236. }
  237. }
  238. err:
  239. return ret;
  240. }
  241. static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  242. int nmsgs)
  243. {
  244. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  245. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  246. ulong start_time;
  247. int ret, i;
  248. start_time = get_timer(0);
  249. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  250. if (get_timer(start_time) > I2C_TIMEOUT_MS) {
  251. debug("Timeout\n");
  252. return -ETIMEDOUT;
  253. }
  254. }
  255. for (ret = 0, i = 0; !ret && i < nmsgs; i++)
  256. ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
  257. /* Send STOP */
  258. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  259. read_write_byte(i2c);
  260. return ret ? -EREMOTEIO : 0;
  261. }
  262. static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
  263. {
  264. const void *blob = gd->fdt_blob;
  265. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  266. int node;
  267. node = dev_of_offset(dev);
  268. i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
  269. i2c_bus->id = pinmux_decode_periph_id(blob, node);
  270. i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
  271. "clock-frequency", 100000);
  272. i2c_bus->node = node;
  273. i2c_bus->bus_num = dev->seq;
  274. exynos_pinmux_config(i2c_bus->id, 0);
  275. i2c_bus->active = true;
  276. return 0;
  277. }
  278. static const struct dm_i2c_ops s3c_i2c_ops = {
  279. .xfer = s3c24x0_i2c_xfer,
  280. .probe_chip = s3c24x0_i2c_probe,
  281. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  282. };
  283. static const struct udevice_id s3c_i2c_ids[] = {
  284. { .compatible = "samsung,s3c2440-i2c" },
  285. { }
  286. };
  287. U_BOOT_DRIVER(i2c_s3c) = {
  288. .name = "i2c_s3c",
  289. .id = UCLASS_I2C,
  290. .of_match = s3c_i2c_ids,
  291. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  292. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  293. .ops = &s3c_i2c_ops,
  294. };