ihs_i2c.c 11 KB

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