i2c_core.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
  3. *
  4. * (C) Copyright 2012
  5. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  6. *
  7. * Multibus/multiadapter I2C core functions (wrappers)
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <i2c.h>
  13. struct i2c_adapter *i2c_get_adapter(int index)
  14. {
  15. struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
  16. i2c);
  17. int max = ll_entry_count(struct i2c_adapter, i2c);
  18. int i;
  19. if (index >= max) {
  20. printf("Error, wrong i2c adapter %d max %d possible\n",
  21. index, max);
  22. return i2c_adap_p;
  23. }
  24. if (index == 0)
  25. return i2c_adap_p;
  26. for (i = 0; i < index; i++)
  27. i2c_adap_p++;
  28. return i2c_adap_p;
  29. }
  30. #if !defined(CONFIG_SYS_I2C_DIRECT_BUS)
  31. struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] =
  32. CONFIG_SYS_I2C_BUSES;
  33. #endif
  34. DECLARE_GLOBAL_DATA_PTR;
  35. void i2c_reloc_fixup(void)
  36. {
  37. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  38. struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter,
  39. i2c);
  40. struct i2c_adapter *tmp = i2c_adap_p;
  41. int max = ll_entry_count(struct i2c_adapter, i2c);
  42. int i;
  43. unsigned long addr;
  44. if (gd->reloc_off == 0)
  45. return;
  46. for (i = 0; i < max; i++) {
  47. /* adapter itself */
  48. addr = (unsigned long)i2c_adap_p;
  49. addr += gd->reloc_off;
  50. i2c_adap_p = (struct i2c_adapter *)addr;
  51. /* i2c_init() */
  52. addr = (unsigned long)i2c_adap_p->init;
  53. addr += gd->reloc_off;
  54. i2c_adap_p->init = (void (*)(int, int))addr;
  55. /* i2c_probe() */
  56. addr = (unsigned long)i2c_adap_p->probe;
  57. addr += gd->reloc_off;
  58. i2c_adap_p->probe = (int (*)(uint8_t))addr;
  59. /* i2c_read() */
  60. addr = (unsigned long)i2c_adap_p->read;
  61. addr += gd->reloc_off;
  62. i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *,
  63. int))addr;
  64. /* i2c_write() */
  65. addr = (unsigned long)i2c_adap_p->write;
  66. addr += gd->reloc_off;
  67. i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *,
  68. int))addr;
  69. /* i2c_set_bus_speed() */
  70. addr = (unsigned long)i2c_adap_p->set_bus_speed;
  71. addr += gd->reloc_off;
  72. i2c_adap_p->set_bus_speed = (uint (*)(uint))addr;
  73. /* name */
  74. addr = (unsigned long)i2c_adap_p->name;
  75. addr += gd->reloc_off;
  76. i2c_adap_p->name = (char *)addr;
  77. tmp++;
  78. i2c_adap_p = tmp;
  79. }
  80. #endif
  81. }
  82. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  83. /*
  84. * i2c_mux_set()
  85. * -------------
  86. *
  87. * This turns on the given channel on I2C multiplexer chip connected to
  88. * a given I2C adapter directly or via other multiplexers. In the latter
  89. * case the entire multiplexer chain must be initialized first starting
  90. * with the one connected directly to the adapter. When disabling a chain
  91. * muxes must be programmed in reverse order, starting with the one
  92. * farthest from the adapter.
  93. *
  94. * mux_id is the multiplexer chip type from defined in i2c.h. So far only
  95. * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT
  96. * supported (anybody uses them?)
  97. */
  98. static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip,
  99. int channel)
  100. {
  101. uint8_t buf;
  102. int ret;
  103. /* channel < 0 - turn off the mux */
  104. if (channel < 0) {
  105. buf = 0;
  106. ret = adap->write(adap, chip, 0, 0, &buf, 1);
  107. if (ret)
  108. printf("%s: Could not turn off the mux.\n", __func__);
  109. return ret;
  110. }
  111. switch (mux_id) {
  112. case I2C_MUX_PCA9540_ID:
  113. case I2C_MUX_PCA9542_ID:
  114. if (channel > 1)
  115. return -1;
  116. buf = (uint8_t)((channel & 0x01) | (1 << 2));
  117. break;
  118. case I2C_MUX_PCA9544_ID:
  119. if (channel > 3)
  120. return -1;
  121. buf = (uint8_t)((channel & 0x03) | (1 << 2));
  122. break;
  123. case I2C_MUX_PCA9547_ID:
  124. if (channel > 7)
  125. return -1;
  126. buf = (uint8_t)((channel & 0x07) | (1 << 3));
  127. break;
  128. case I2C_MUX_PCA9548_ID:
  129. if (channel > 7)
  130. return -1;
  131. buf = (uint8_t)(0x01 << channel);
  132. break;
  133. default:
  134. printf("%s: wrong mux id: %d\n", __func__, mux_id);
  135. return -1;
  136. }
  137. ret = adap->write(adap, chip, 0, 0, &buf, 1);
  138. if (ret)
  139. printf("%s: could not set mux: id: %d chip: %x channel: %d\n",
  140. __func__, mux_id, chip, channel);
  141. return ret;
  142. }
  143. static int i2c_mux_set_all(void)
  144. {
  145. struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
  146. int i;
  147. /* Connect requested bus if behind muxes */
  148. if (i2c_bus_tmp->next_hop[0].chip != 0) {
  149. /* Set all muxes along the path to that bus */
  150. for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) {
  151. int ret;
  152. if (i2c_bus_tmp->next_hop[i].chip == 0)
  153. break;
  154. ret = i2c_mux_set(I2C_ADAP,
  155. i2c_bus_tmp->next_hop[i].mux.id,
  156. i2c_bus_tmp->next_hop[i].chip,
  157. i2c_bus_tmp->next_hop[i].channel);
  158. if (ret != 0)
  159. return ret;
  160. }
  161. }
  162. return 0;
  163. }
  164. static int i2c_mux_disconnet_all(void)
  165. {
  166. struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS];
  167. int i;
  168. uint8_t buf;
  169. if (I2C_ADAP->init_done == 0)
  170. return 0;
  171. /* Disconnect current bus (turn off muxes if any) */
  172. if ((i2c_bus_tmp->next_hop[0].chip != 0) &&
  173. (I2C_ADAP->init_done != 0)) {
  174. i = CONFIG_SYS_I2C_MAX_HOPS;
  175. do {
  176. uint8_t chip;
  177. int ret;
  178. chip = i2c_bus_tmp->next_hop[--i].chip;
  179. if (chip == 0)
  180. continue;
  181. ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1);
  182. if (ret != 0) {
  183. printf("i2c: mux diconnect error\n");
  184. return ret;
  185. }
  186. } while (i > 0);
  187. }
  188. return 0;
  189. }
  190. #endif
  191. /*
  192. * i2c_init_bus():
  193. * ---------------
  194. *
  195. * Initializes one bus. Will initialize the parent adapter. No current bus
  196. * changes, no mux (if any) setup.
  197. */
  198. static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr)
  199. {
  200. if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES)
  201. return;
  202. I2C_ADAP->init(I2C_ADAP, speed, slaveaddr);
  203. if (gd->flags & GD_FLG_RELOC) {
  204. I2C_ADAP->init_done = 1;
  205. I2C_ADAP->speed = speed;
  206. I2C_ADAP->slaveaddr = slaveaddr;
  207. }
  208. }
  209. /* implement possible board specific board init */
  210. static void __def_i2c_init_board(void)
  211. {
  212. }
  213. void i2c_init_board(void)
  214. __attribute__((weak, alias("__def_i2c_init_board")));
  215. /*
  216. * i2c_init_all():
  217. *
  218. * not longer needed, will deleted. Actual init the SPD_BUS
  219. * for compatibility.
  220. * i2c_adap[] must be initialized beforehead with function pointers and
  221. * data, including speed and slaveaddr.
  222. */
  223. void i2c_init_all(void)
  224. {
  225. i2c_init_board();
  226. i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
  227. return;
  228. }
  229. /*
  230. * i2c_get_bus_num():
  231. * ------------------
  232. *
  233. * Returns index of currently active I2C bus. Zero-based.
  234. */
  235. unsigned int i2c_get_bus_num(void)
  236. {
  237. return gd->cur_i2c_bus;
  238. }
  239. /*
  240. * i2c_set_bus_num():
  241. * ------------------
  242. *
  243. * Change the active I2C bus. Subsequent read/write calls will
  244. * go to this one. Sets all of the muxes in a proper condition
  245. * if that bus is behind muxes.
  246. * If previously selected bus is behind the muxes turns off all the
  247. * muxes along the path to that bus.
  248. *
  249. * bus - bus index, zero based
  250. *
  251. * Returns: 0 on success, not 0 on failure
  252. */
  253. int i2c_set_bus_num(unsigned int bus)
  254. {
  255. int max = ll_entry_count(struct i2c_adapter, i2c);
  256. if (I2C_ADAPTER(bus) >= max) {
  257. printf("Error, wrong i2c adapter %d max %d possible\n",
  258. I2C_ADAPTER(bus), max);
  259. return -2;
  260. }
  261. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  262. if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
  263. return -1;
  264. #endif
  265. if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
  266. return 0;
  267. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  268. i2c_mux_disconnet_all();
  269. #endif
  270. gd->cur_i2c_bus = bus;
  271. if (I2C_ADAP->init_done == 0)
  272. i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr);
  273. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  274. i2c_mux_set_all();
  275. #endif
  276. return 0;
  277. }
  278. /*
  279. * Probe the given I2C chip address. Returns 0 if a chip responded,
  280. * not 0 on failure.
  281. */
  282. int i2c_probe(uint8_t chip)
  283. {
  284. return I2C_ADAP->probe(I2C_ADAP, chip);
  285. }
  286. /*
  287. * Read/Write interface:
  288. * chip: I2C chip address, range 0..127
  289. * addr: Memory (register) address within the chip
  290. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  291. * memories, 0 for register type devices with only one
  292. * register)
  293. * buffer: Where to read/write the data
  294. * len: How many bytes to read/write
  295. *
  296. * Returns: 0 on success, not 0 on failure
  297. */
  298. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  299. uint8_t *buffer, int len)
  300. {
  301. return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len);
  302. }
  303. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  304. uint8_t *buffer, int len)
  305. {
  306. return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len);
  307. }
  308. unsigned int i2c_set_bus_speed(unsigned int speed)
  309. {
  310. unsigned int ret;
  311. if (I2C_ADAP->set_bus_speed == NULL)
  312. return 0;
  313. ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed);
  314. if (gd->flags & GD_FLG_RELOC)
  315. I2C_ADAP->speed = ret;
  316. return ret;
  317. }
  318. unsigned int i2c_get_bus_speed(void)
  319. {
  320. struct i2c_adapter *cur = I2C_ADAP;
  321. return cur->speed;
  322. }
  323. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg)
  324. {
  325. uint8_t buf;
  326. #ifdef CONFIG_8xx
  327. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  328. /* maybe it is now the time for it ... */
  329. i2c_set_bus_num(i2c_get_bus_num());
  330. #endif
  331. i2c_read(addr, reg, 1, &buf, 1);
  332. #ifdef DEBUG
  333. printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  334. __func__, i2c_get_bus_num(), addr, reg, buf);
  335. #endif
  336. return buf;
  337. }
  338. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
  339. {
  340. #ifdef CONFIG_8xx
  341. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  342. /* maybe it is now the time for it ... */
  343. i2c_set_bus_num(i2c_get_bus_num());
  344. #endif
  345. #ifdef DEBUG
  346. printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  347. __func__, i2c_get_bus_num(), addr, reg, val);
  348. #endif
  349. i2c_write(addr, reg, 1, &val, 1);
  350. }
  351. void __i2c_init(int speed, int slaveaddr)
  352. {
  353. i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
  354. }
  355. void i2c_init(int speed, int slaveaddr)
  356. __attribute__((weak, alias("__i2c_init")));