ihs_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * (C) Copyright 2013
  3. * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #ifdef CONFIG_DM_I2C
  10. #include <dm.h>
  11. #include <fpgamap.h>
  12. #include "../misc/gdsys_soc.h"
  13. #else
  14. #include <gdsys_fpga.h>
  15. #endif
  16. #include <asm/unaligned.h>
  17. #ifdef CONFIG_DM_I2C
  18. struct ihs_i2c_priv {
  19. uint speed;
  20. phys_addr_t addr;
  21. };
  22. enum {
  23. REG_INTERRUPT_STATUS = 0x00,
  24. REG_INTERRUPT_ENABLE_CONTROL = 0x02,
  25. REG_WRITE_MAILBOX_EXT = 0x04,
  26. REG_WRITE_MAILBOX = 0x06,
  27. REG_READ_MAILBOX_EXT = 0x08,
  28. REG_READ_MAILBOX = 0x0A,
  29. };
  30. #else /* !CONFIG_DM_I2C */
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  33. #define I2C_SET_REG(fld, val) \
  34. do { \
  35. if (I2C_ADAP_HWNR & 0x10) \
  36. FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
  37. else \
  38. FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
  39. } while (0)
  40. #else
  41. #define I2C_SET_REG(fld, val) \
  42. FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
  43. #endif
  44. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  45. #define I2C_GET_REG(fld, val) \
  46. do { \
  47. if (I2C_ADAP_HWNR & 0x10) \
  48. FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
  49. else \
  50. FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
  51. } while (0)
  52. #else
  53. #define I2C_GET_REG(fld, val) \
  54. FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
  55. #endif
  56. #endif /* CONFIG_DM_I2C */
  57. enum {
  58. I2CINT_ERROR_EV = BIT(13),
  59. I2CINT_TRANSMIT_EV = BIT(14),
  60. I2CINT_RECEIVE_EV = BIT(15),
  61. };
  62. enum {
  63. I2CMB_READ = 0 << 10,
  64. I2CMB_WRITE = 1 << 10,
  65. I2CMB_1BYTE = 0 << 11,
  66. I2CMB_2BYTE = 1 << 11,
  67. I2CMB_DONT_HOLD_BUS = 0 << 13,
  68. I2CMB_HOLD_BUS = 1 << 13,
  69. I2CMB_NATIVE = 2 << 14,
  70. };
  71. enum {
  72. I2COP_WRITE = 0,
  73. I2COP_READ = 1,
  74. };
  75. #ifdef CONFIG_DM_I2C
  76. static int wait_for_int(struct udevice *dev, int read)
  77. #else
  78. static int wait_for_int(bool read)
  79. #endif
  80. {
  81. u16 val;
  82. uint ctr = 0;
  83. #ifdef CONFIG_DM_I2C
  84. struct ihs_i2c_priv *priv = dev_get_priv(dev);
  85. struct udevice *fpga;
  86. gdsys_soc_get_fpga(dev, &fpga);
  87. #endif
  88. #ifdef CONFIG_DM_I2C
  89. fpgamap_read16(fpga, priv->addr + REG_INTERRUPT_STATUS, &val);
  90. #else
  91. I2C_GET_REG(interrupt_status, &val);
  92. #endif
  93. /* Wait until error or receive/transmit interrupt was raised */
  94. while (!(val & (I2CINT_ERROR_EV
  95. | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
  96. udelay(10);
  97. if (ctr++ > 5000)
  98. return 1;
  99. #ifdef CONFIG_DM_I2C
  100. fpgamap_read16(fpga, priv->addr + REG_INTERRUPT_STATUS, &val);
  101. #else
  102. I2C_GET_REG(interrupt_status, &val);
  103. #endif
  104. }
  105. return (val & I2CINT_ERROR_EV) ? 1 : 0;
  106. }
  107. #ifdef CONFIG_DM_I2C
  108. static int ihs_i2c_transfer(struct udevice *dev, uchar chip,
  109. uchar *buffer, int len, int read, bool is_last)
  110. #else
  111. static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
  112. bool is_last)
  113. #endif
  114. {
  115. u16 val;
  116. #ifdef CONFIG_DM_I2C
  117. struct ihs_i2c_priv *priv = dev_get_priv(dev);
  118. struct udevice *fpga;
  119. gdsys_soc_get_fpga(dev, &fpga);
  120. #endif
  121. /* Clear interrupt status */
  122. #ifdef CONFIG_DM_I2C
  123. fpgamap_write16(fpga, priv->addr + REG_INTERRUPT_STATUS,
  124. I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV);
  125. fpgamap_read16(fpga, priv->addr + REG_INTERRUPT_STATUS, &val);
  126. #else
  127. I2C_SET_REG(interrupt_status, I2CINT_ERROR_EV
  128. | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV);
  129. I2C_GET_REG(interrupt_status, &val);
  130. #endif
  131. /* If we want to write and have data, write the bytes to the mailbox */
  132. if (!read && len) {
  133. val = buffer[0];
  134. if (len > 1)
  135. val |= buffer[1] << 8;
  136. #ifdef CONFIG_DM_I2C
  137. fpgamap_write16(fpga, priv->addr + REG_WRITE_MAILBOX_EXT, val);
  138. #else
  139. I2C_SET_REG(write_mailbox_ext, val);
  140. #endif
  141. }
  142. #ifdef CONFIG_DM_I2C
  143. fpgamap_write16(fpga, priv->addr + REG_WRITE_MAILBOX,
  144. I2CMB_NATIVE
  145. | (read ? I2CMB_READ : I2CMB_WRITE)
  146. | (chip << 1)
  147. | ((len > 1) ? I2CMB_2BYTE : I2CMB_1BYTE)
  148. | (!is_last ? I2CMB_HOLD_BUS : I2CMB_DONT_HOLD_BUS));
  149. #else
  150. I2C_SET_REG(write_mailbox,
  151. I2CMB_NATIVE
  152. | (read ? 0 : I2CMB_WRITE)
  153. | (chip << 1)
  154. | ((len > 1) ? I2CMB_2BYTE : 0)
  155. | (is_last ? 0 : I2CMB_HOLD_BUS));
  156. #endif
  157. #ifdef CONFIG_DM_I2C
  158. if (wait_for_int(dev, read))
  159. #else
  160. if (wait_for_int(read))
  161. #endif
  162. return 1;
  163. /* If we want to read, get the bytes from the mailbox */
  164. if (read) {
  165. #ifdef CONFIG_DM_I2C
  166. fpgamap_read16(fpga, priv->addr + REG_READ_MAILBOX_EXT, &val);
  167. #else
  168. I2C_GET_REG(read_mailbox_ext, &val);
  169. #endif
  170. buffer[0] = val & 0xff;
  171. if (len > 1)
  172. buffer[1] = val >> 8;
  173. }
  174. return 0;
  175. }
  176. #ifdef CONFIG_DM_I2C
  177. static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
  178. #else
  179. static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus,
  180. int read)
  181. #endif
  182. {
  183. while (len) {
  184. int transfer = min(len, 2);
  185. bool is_last = len <= transfer;
  186. #ifdef CONFIG_DM_I2C
  187. if (ihs_i2c_transfer(dev, chip, data, transfer, read,
  188. hold_bus ? false : is_last))
  189. return 1;
  190. #else
  191. if (ihs_i2c_transfer(chip, data, transfer, read,
  192. hold_bus ? false : is_last))
  193. return 1;
  194. #endif
  195. data += transfer;
  196. len -= transfer;
  197. }
  198. return 0;
  199. }
  200. #ifdef CONFIG_DM_I2C
  201. static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
  202. bool hold_bus)
  203. #else
  204. static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
  205. #endif
  206. {
  207. #ifdef CONFIG_DM_I2C
  208. return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
  209. #else
  210. return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE);
  211. #endif
  212. }
  213. #ifdef CONFIG_DM_I2C
  214. static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
  215. int alen, uchar *buffer, int len, int read)
  216. #else
  217. static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
  218. int alen, uchar *buffer, int len, int read)
  219. #endif
  220. {
  221. /* Don't hold the bus if length of data to send/receive is zero */
  222. #ifdef CONFIG_DM_I2C
  223. if (len <= 0 || ihs_i2c_address(dev, chip, addr, alen, len))
  224. return 1;
  225. #else
  226. if (len <= 0 || ihs_i2c_address(chip, addr, alen, len))
  227. return 1;
  228. #endif
  229. #ifdef CONFIG_DM_I2C
  230. return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
  231. #else
  232. return ihs_i2c_send_buffer(chip, buffer, len, false, read);
  233. #endif
  234. }
  235. #ifdef CONFIG_DM_I2C
  236. int ihs_i2c_probe(struct udevice *bus)
  237. {
  238. struct ihs_i2c_priv *priv = dev_get_priv(bus);
  239. int addr;
  240. addr = dev_read_u32_default(bus, "reg", -1);
  241. priv->addr = addr;
  242. return 0;
  243. }
  244. static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed)
  245. {
  246. struct ihs_i2c_priv *priv = dev_get_priv(bus);
  247. if (speed != priv->speed && priv->speed != 0)
  248. return 1;
  249. priv->speed = speed;
  250. return 0;
  251. }
  252. static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  253. {
  254. struct i2c_msg *dmsg, *omsg, dummy;
  255. memset(&dummy, 0, sizeof(struct i2c_msg));
  256. /* We expect either two messages (one with an offset and one with the
  257. * actucal data) or one message (just data)
  258. */
  259. if (nmsgs > 2 || nmsgs == 0) {
  260. debug("%s: Only one or two messages are supported.", __func__);
  261. return -1;
  262. }
  263. omsg = nmsgs == 1 ? &dummy : msg;
  264. dmsg = nmsgs == 1 ? msg : msg + 1;
  265. if (dmsg->flags & I2C_M_RD)
  266. return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
  267. omsg->len, dmsg->buf, dmsg->len,
  268. I2COP_READ);
  269. else
  270. return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
  271. omsg->len, dmsg->buf, dmsg->len,
  272. I2COP_WRITE);
  273. }
  274. static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  275. u32 chip_flags)
  276. {
  277. uchar buffer[2];
  278. if (ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true))
  279. return 1;
  280. return 0;
  281. }
  282. static const struct dm_i2c_ops ihs_i2c_ops = {
  283. .xfer = ihs_i2c_xfer,
  284. .probe_chip = ihs_i2c_probe_chip,
  285. .set_bus_speed = ihs_i2c_set_bus_speed,
  286. };
  287. static const struct udevice_id ihs_i2c_ids[] = {
  288. { .compatible = "gdsys,ihs_i2cmaster", },
  289. { /* sentinel */ }
  290. };
  291. U_BOOT_DRIVER(i2c_ihs) = {
  292. .name = "i2c_ihs",
  293. .id = UCLASS_I2C,
  294. .of_match = ihs_i2c_ids,
  295. .probe = ihs_i2c_probe,
  296. .priv_auto_alloc_size = sizeof(struct ihs_i2c_priv),
  297. .ops = &ihs_i2c_ops,
  298. };
  299. #else /* CONFIG_DM_I2C */
  300. static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  301. {
  302. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  303. /*
  304. * Call board specific i2c bus reset routine before accessing the
  305. * environment, which might be in a chip on that bus. For details
  306. * about this problem see doc/I2C_Edge_Conditions.
  307. */
  308. i2c_init_board();
  309. #endif
  310. }
  311. static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
  312. {
  313. uchar buffer[2];
  314. if (ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true))
  315. return 1;
  316. return 0;
  317. }
  318. static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  319. int alen, uchar *buffer, int len)
  320. {
  321. u8 addr_bytes[4];
  322. put_unaligned_le32(addr, addr_bytes);
  323. return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
  324. I2COP_READ);
  325. }
  326. static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  327. int alen, uchar *buffer, int len)
  328. {
  329. u8 addr_bytes[4];
  330. put_unaligned_le32(addr, addr_bytes);
  331. return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
  332. I2COP_WRITE);
  333. }
  334. static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
  335. unsigned int speed)
  336. {
  337. if (speed != adap->speed)
  338. return 1;
  339. return speed;
  340. }
  341. /*
  342. * Register IHS i2c adapters
  343. */
  344. #ifdef CONFIG_SYS_I2C_IHS_CH0
  345. U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
  346. ihs_i2c_read, ihs_i2c_write,
  347. ihs_i2c_set_bus_speed,
  348. CONFIG_SYS_I2C_IHS_SPEED_0,
  349. CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
  350. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  351. U_BOOT_I2C_ADAP_COMPLETE(ihs0_1, ihs_i2c_init, ihs_i2c_probe,
  352. ihs_i2c_read, ihs_i2c_write,
  353. ihs_i2c_set_bus_speed,
  354. CONFIG_SYS_I2C_IHS_SPEED_0_1,
  355. CONFIG_SYS_I2C_IHS_SLAVE_0_1, 16)
  356. #endif
  357. #endif
  358. #ifdef CONFIG_SYS_I2C_IHS_CH1
  359. U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
  360. ihs_i2c_read, ihs_i2c_write,
  361. ihs_i2c_set_bus_speed,
  362. CONFIG_SYS_I2C_IHS_SPEED_1,
  363. CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
  364. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  365. U_BOOT_I2C_ADAP_COMPLETE(ihs1_1, ihs_i2c_init, ihs_i2c_probe,
  366. ihs_i2c_read, ihs_i2c_write,
  367. ihs_i2c_set_bus_speed,
  368. CONFIG_SYS_I2C_IHS_SPEED_1_1,
  369. CONFIG_SYS_I2C_IHS_SLAVE_1_1, 17)
  370. #endif
  371. #endif
  372. #ifdef CONFIG_SYS_I2C_IHS_CH2
  373. U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
  374. ihs_i2c_read, ihs_i2c_write,
  375. ihs_i2c_set_bus_speed,
  376. CONFIG_SYS_I2C_IHS_SPEED_2,
  377. CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
  378. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  379. U_BOOT_I2C_ADAP_COMPLETE(ihs2_1, ihs_i2c_init, ihs_i2c_probe,
  380. ihs_i2c_read, ihs_i2c_write,
  381. ihs_i2c_set_bus_speed,
  382. CONFIG_SYS_I2C_IHS_SPEED_2_1,
  383. CONFIG_SYS_I2C_IHS_SLAVE_2_1, 18)
  384. #endif
  385. #endif
  386. #ifdef CONFIG_SYS_I2C_IHS_CH3
  387. U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
  388. ihs_i2c_read, ihs_i2c_write,
  389. ihs_i2c_set_bus_speed,
  390. CONFIG_SYS_I2C_IHS_SPEED_3,
  391. CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
  392. #ifdef CONFIG_SYS_I2C_IHS_DUAL
  393. U_BOOT_I2C_ADAP_COMPLETE(ihs3_1, ihs_i2c_init, ihs_i2c_probe,
  394. ihs_i2c_read, ihs_i2c_write,
  395. ihs_i2c_set_bus_speed,
  396. CONFIG_SYS_I2C_IHS_SPEED_3_1,
  397. CONFIG_SYS_I2C_IHS_SLAVE_3_1, 19)
  398. #endif
  399. #endif
  400. #endif /* CONFIG_DM_I2C */