s3c24x0_i2c.c 7.9 KB

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