i2c_core.c 9.9 KB

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