i2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
  3. * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
  4. * Changes for multibus/multiadapter I2C support.
  5. *
  6. * (C) Copyright 2001
  7. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. *
  11. * The original I2C interface was
  12. * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
  13. * AIRVENT SAM s.p.a - RIMINI(ITALY)
  14. * but has been changed substantially.
  15. */
  16. #ifndef _I2C_H_
  17. #define _I2C_H_
  18. /*
  19. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  20. *
  21. * The implementation MUST NOT use static or global variables if the
  22. * I2C routines are used to read SDRAM configuration information
  23. * because this is done before the memories are initialized. Limited
  24. * use of stack-based variables are OK (the initial stack size is
  25. * limited).
  26. *
  27. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  28. */
  29. /*
  30. * Configuration items.
  31. */
  32. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  33. #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
  34. /* no muxes used bus = i2c adapters */
  35. #define CONFIG_SYS_I2C_DIRECT_BUS 1
  36. #define CONFIG_SYS_I2C_MAX_HOPS 0
  37. #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
  38. #else
  39. /* we use i2c muxes */
  40. #undef CONFIG_SYS_I2C_DIRECT_BUS
  41. #endif
  42. /* define the I2C bus number for RTC and DTT if not already done */
  43. #if !defined(CONFIG_SYS_RTC_BUS_NUM)
  44. #define CONFIG_SYS_RTC_BUS_NUM 0
  45. #endif
  46. #if !defined(CONFIG_SYS_DTT_BUS_NUM)
  47. #define CONFIG_SYS_DTT_BUS_NUM 0
  48. #endif
  49. #if !defined(CONFIG_SYS_SPD_BUS_NUM)
  50. #define CONFIG_SYS_SPD_BUS_NUM 0
  51. #endif
  52. struct i2c_adapter {
  53. void (*init)(struct i2c_adapter *adap, int speed,
  54. int slaveaddr);
  55. int (*probe)(struct i2c_adapter *adap, uint8_t chip);
  56. int (*read)(struct i2c_adapter *adap, uint8_t chip,
  57. uint addr, int alen, uint8_t *buffer,
  58. int len);
  59. int (*write)(struct i2c_adapter *adap, uint8_t chip,
  60. uint addr, int alen, uint8_t *buffer,
  61. int len);
  62. uint (*set_bus_speed)(struct i2c_adapter *adap,
  63. uint speed);
  64. int speed;
  65. int slaveaddr;
  66. int init_done;
  67. int hwadapnr;
  68. char *name;
  69. };
  70. #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  71. _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
  72. { \
  73. .init = _init, \
  74. .probe = _probe, \
  75. .read = _read, \
  76. .write = _write, \
  77. .set_bus_speed = _set_speed, \
  78. .speed = _speed, \
  79. .slaveaddr = _slaveaddr, \
  80. .init_done = 0, \
  81. .hwadapnr = _hwadapnr, \
  82. .name = #_name \
  83. };
  84. #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
  85. _set_speed, _speed, _slaveaddr, _hwadapnr) \
  86. ll_entry_declare(struct i2c_adapter, _name, i2c) = \
  87. U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  88. _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
  89. struct i2c_adapter *i2c_get_adapter(int index);
  90. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  91. struct i2c_mux {
  92. int id;
  93. char name[16];
  94. };
  95. struct i2c_next_hop {
  96. struct i2c_mux mux;
  97. uint8_t chip;
  98. uint8_t channel;
  99. };
  100. struct i2c_bus_hose {
  101. int adapter;
  102. struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
  103. };
  104. #define I2C_NULL_HOP {{-1, ""}, 0, 0}
  105. extern struct i2c_bus_hose i2c_bus[];
  106. #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
  107. #else
  108. #define I2C_ADAPTER(bus) bus
  109. #endif
  110. #define I2C_BUS gd->cur_i2c_bus
  111. #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
  112. #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
  113. #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
  114. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  115. #define I2C_MUX_PCA9540_ID 1
  116. #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
  117. #define I2C_MUX_PCA9542_ID 2
  118. #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
  119. #define I2C_MUX_PCA9544_ID 3
  120. #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
  121. #define I2C_MUX_PCA9547_ID 4
  122. #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
  123. #define I2C_MUX_PCA9548_ID 5
  124. #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
  125. #endif
  126. #ifndef I2C_SOFT_DECLARATIONS
  127. # if defined(CONFIG_MPC8260)
  128. # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
  129. # elif defined(CONFIG_8xx)
  130. # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  131. # elif (defined(CONFIG_AT91RM9200) || \
  132. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  133. defined(CONFIG_AT91SAM9263))
  134. # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  135. # else
  136. # define I2C_SOFT_DECLARATIONS
  137. # endif
  138. #endif
  139. #ifdef CONFIG_8xx
  140. /* Set default value for the I2C bus speed on 8xx. In the
  141. * future, we'll define these in all 8xx board config files.
  142. */
  143. #ifndef CONFIG_SYS_I2C_SPEED
  144. #define CONFIG_SYS_I2C_SPEED 50000
  145. #endif
  146. #endif
  147. /*
  148. * Many boards/controllers/drivers don't support an I2C slave interface so
  149. * provide a default slave address for them for use in common code. A real
  150. * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
  151. * support a slave interface.
  152. */
  153. #ifndef CONFIG_SYS_I2C_SLAVE
  154. #define CONFIG_SYS_I2C_SLAVE 0xfe
  155. #endif
  156. /*
  157. * Initialization, must be called once on start up, may be called
  158. * repeatedly to change the speed and slave addresses.
  159. */
  160. void i2c_init(int speed, int slaveaddr);
  161. void i2c_init_board(void);
  162. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  163. void i2c_board_late_init(void);
  164. #endif
  165. #ifdef CONFIG_SYS_I2C
  166. /*
  167. * i2c_get_bus_num:
  168. *
  169. * Returns index of currently active I2C bus. Zero-based.
  170. */
  171. unsigned int i2c_get_bus_num(void);
  172. /*
  173. * i2c_set_bus_num:
  174. *
  175. * Change the active I2C bus. Subsequent read/write calls will
  176. * go to this one.
  177. *
  178. * bus - bus index, zero based
  179. *
  180. * Returns: 0 on success, not 0 on failure
  181. *
  182. */
  183. int i2c_set_bus_num(unsigned int bus);
  184. /*
  185. * i2c_init_all():
  186. *
  187. * Initializes all I2C adapters in the system. All i2c_adap structures must
  188. * be initialized beforehead with function pointers and data, including
  189. * speed and slaveaddr. Returns 0 on success, non-0 on failure.
  190. */
  191. void i2c_init_all(void);
  192. /*
  193. * Probe the given I2C chip address. Returns 0 if a chip responded,
  194. * not 0 on failure.
  195. */
  196. int i2c_probe(uint8_t chip);
  197. /*
  198. * Read/Write interface:
  199. * chip: I2C chip address, range 0..127
  200. * addr: Memory (register) address within the chip
  201. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  202. * memories, 0 for register type devices with only one
  203. * register)
  204. * buffer: Where to read/write the data
  205. * len: How many bytes to read/write
  206. *
  207. * Returns: 0 on success, not 0 on failure
  208. */
  209. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  210. uint8_t *buffer, int len);
  211. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  212. uint8_t *buffer, int len);
  213. /*
  214. * Utility routines to read/write registers.
  215. */
  216. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  217. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  218. /*
  219. * i2c_set_bus_speed:
  220. *
  221. * Change the speed of the active I2C bus
  222. *
  223. * speed - bus speed in Hz
  224. *
  225. * Returns: new bus speed
  226. *
  227. */
  228. unsigned int i2c_set_bus_speed(unsigned int speed);
  229. /*
  230. * i2c_get_bus_speed:
  231. *
  232. * Returns speed of currently active I2C bus in Hz
  233. */
  234. unsigned int i2c_get_bus_speed(void);
  235. /*
  236. * i2c_reloc_fixup:
  237. *
  238. * Adjusts I2C pointers after U-Boot is relocated to DRAM
  239. */
  240. void i2c_reloc_fixup(void);
  241. #if defined(CONFIG_SYS_I2C_SOFT)
  242. void i2c_soft_init(void);
  243. void i2c_soft_active(void);
  244. void i2c_soft_tristate(void);
  245. int i2c_soft_read(void);
  246. void i2c_soft_sda(int bit);
  247. void i2c_soft_scl(int bit);
  248. void i2c_soft_delay(void);
  249. #endif
  250. #else
  251. /*
  252. * Probe the given I2C chip address. Returns 0 if a chip responded,
  253. * not 0 on failure.
  254. */
  255. int i2c_probe(uchar chip);
  256. /*
  257. * Read/Write interface:
  258. * chip: I2C chip address, range 0..127
  259. * addr: Memory (register) address within the chip
  260. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  261. * memories, 0 for register type devices with only one
  262. * register)
  263. * buffer: Where to read/write the data
  264. * len: How many bytes to read/write
  265. *
  266. * Returns: 0 on success, not 0 on failure
  267. */
  268. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
  269. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
  270. /*
  271. * Utility routines to read/write registers.
  272. */
  273. static inline u8 i2c_reg_read(u8 addr, u8 reg)
  274. {
  275. u8 buf;
  276. #ifdef CONFIG_8xx
  277. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  278. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  279. #endif
  280. #ifdef DEBUG
  281. printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
  282. #endif
  283. i2c_read(addr, reg, 1, &buf, 1);
  284. return buf;
  285. }
  286. static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
  287. {
  288. #ifdef CONFIG_8xx
  289. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  290. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  291. #endif
  292. #ifdef DEBUG
  293. printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  294. __func__, addr, reg, val);
  295. #endif
  296. i2c_write(addr, reg, 1, &val, 1);
  297. }
  298. /*
  299. * Functions for setting the current I2C bus and its speed
  300. */
  301. /*
  302. * i2c_set_bus_num:
  303. *
  304. * Change the active I2C bus. Subsequent read/write calls will
  305. * go to this one.
  306. *
  307. * bus - bus index, zero based
  308. *
  309. * Returns: 0 on success, not 0 on failure
  310. *
  311. */
  312. int i2c_set_bus_num(unsigned int bus);
  313. /*
  314. * i2c_get_bus_num:
  315. *
  316. * Returns index of currently active I2C bus. Zero-based.
  317. */
  318. unsigned int i2c_get_bus_num(void);
  319. /*
  320. * i2c_set_bus_speed:
  321. *
  322. * Change the speed of the active I2C bus
  323. *
  324. * speed - bus speed in Hz
  325. *
  326. * Returns: 0 on success, not 0 on failure
  327. *
  328. */
  329. int i2c_set_bus_speed(unsigned int);
  330. /*
  331. * i2c_get_bus_speed:
  332. *
  333. * Returns speed of currently active I2C bus in Hz
  334. */
  335. unsigned int i2c_get_bus_speed(void);
  336. #endif /* CONFIG_SYS_I2C */
  337. /*
  338. * only for backwardcompatibility, should go away if we switched
  339. * completely to new multibus support.
  340. */
  341. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  342. # if !defined(CONFIG_SYS_MAX_I2C_BUS)
  343. # define CONFIG_SYS_MAX_I2C_BUS 2
  344. # endif
  345. # define I2C_MULTI_BUS 1
  346. #else
  347. # define CONFIG_SYS_MAX_I2C_BUS 1
  348. # define I2C_MULTI_BUS 0
  349. #endif
  350. /* NOTE: These two functions MUST be always_inline to avoid code growth! */
  351. static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
  352. static inline unsigned int I2C_GET_BUS(void)
  353. {
  354. return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
  355. }
  356. static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
  357. static inline void I2C_SET_BUS(unsigned int bus)
  358. {
  359. if (I2C_MULTI_BUS)
  360. i2c_set_bus_num(bus);
  361. }
  362. /* Multi I2C definitions */
  363. enum {
  364. I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
  365. I2C_8, I2C_9, I2C_10,
  366. };
  367. /* Multi I2C busses handling */
  368. #ifdef CONFIG_SOFT_I2C_MULTI_BUS
  369. extern int get_multi_scl_pin(void);
  370. extern int get_multi_sda_pin(void);
  371. extern int multi_i2c_init(void);
  372. #endif
  373. /**
  374. * Get FDT values for i2c bus.
  375. *
  376. * @param blob Device tree blbo
  377. * @return the number of I2C bus
  378. */
  379. void board_i2c_init(const void *blob);
  380. /**
  381. * Find the I2C bus number by given a FDT I2C node.
  382. *
  383. * @param blob Device tree blbo
  384. * @param node FDT I2C node to find
  385. * @return the number of I2C bus (zero based), or -1 on error
  386. */
  387. int i2c_get_bus_num_fdt(int node);
  388. /**
  389. * Reset the I2C bus represented by the given a FDT I2C node.
  390. *
  391. * @param blob Device tree blbo
  392. * @param node FDT I2C node to find
  393. * @return 0 if port was reset, -1 if not found
  394. */
  395. int i2c_reset_port_fdt(const void *blob, int node);
  396. #endif /* _I2C_H_ */