11f045217Swdenk /* 21f045217Swdenk * (C) Copyright 2001 31f045217Swdenk * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. 41f045217Swdenk * 51f045217Swdenk * See file CREDITS for list of people who contributed to this 61f045217Swdenk * project. 71f045217Swdenk * 81f045217Swdenk * This program is free software; you can redistribute it and/or 91f045217Swdenk * modify it under the terms of the GNU General Public License as 101f045217Swdenk * published by the Free Software Foundation; either version 2 of 111f045217Swdenk * the License, or (at your option) any later version. 121f045217Swdenk * 131f045217Swdenk * This program is distributed in the hope that it will be useful, 141f045217Swdenk * but WITHOUT ANY WARRANTY; without even the implied warranty of 151f045217Swdenk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161f045217Swdenk * GNU General Public License for more details. 171f045217Swdenk * 181f045217Swdenk * You should have received a copy of the GNU General Public License 191f045217Swdenk * along with this program; if not, write to the Free Software 201f045217Swdenk * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 211f045217Swdenk * MA 02111-1307 USA 221f045217Swdenk * 231f045217Swdenk * The original I2C interface was 241f045217Swdenk * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) 251f045217Swdenk * AIRVENT SAM s.p.a - RIMINI(ITALY) 261f045217Swdenk * but has been changed substantially. 271f045217Swdenk */ 281f045217Swdenk 291f045217Swdenk #ifndef _I2C_H_ 301f045217Swdenk #define _I2C_H_ 311f045217Swdenk 321f045217Swdenk /* 331f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 341f045217Swdenk * 351f045217Swdenk * The implementation MUST NOT use static or global variables if the 361f045217Swdenk * I2C routines are used to read SDRAM configuration information 371f045217Swdenk * because this is done before the memories are initialized. Limited 381f045217Swdenk * use of stack-based variables are OK (the initial stack size is 391f045217Swdenk * limited). 401f045217Swdenk * 411f045217Swdenk * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 421f045217Swdenk */ 431f045217Swdenk 441f045217Swdenk /* 451f045217Swdenk * Configuration items. 461f045217Swdenk */ 471f045217Swdenk #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ 481f045217Swdenk 4979b2d0bbSStefan Roese #if defined(CONFIG_I2C_MULTI_BUS) 50*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_MAX_I2C_BUS 2 5179b2d0bbSStefan Roese #define I2C_GET_BUS() i2c_get_bus_num() 5279b2d0bbSStefan Roese #define I2C_SET_BUS(a) i2c_set_bus_num(a) 5379b2d0bbSStefan Roese #else 54*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_MAX_I2C_BUS 1 5579b2d0bbSStefan Roese #define I2C_GET_BUS() 0 5679b2d0bbSStefan Roese #define I2C_SET_BUS(a) 5779b2d0bbSStefan Roese #endif 5879b2d0bbSStefan Roese 598c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */ 60*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM) 61*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM 0 628c12045aSStefan Roese #endif 63*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM) 64*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM 0 658c12045aSStefan Roese #endif 66*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM) 67*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM 0 68d8a8ea5cSMatthias Fuchs #endif 698c12045aSStefan Roese 7098aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS 7198aed379SHeiko Schocher # if defined(CONFIG_MPC8260) 72*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); 7398aed379SHeiko Schocher # elif defined(CONFIG_8xx) 74*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; 7598aed379SHeiko Schocher # else 7698aed379SHeiko Schocher # define I2C_SOFT_DECLARATIONS 7798aed379SHeiko Schocher # endif 7898aed379SHeiko Schocher #endif 791f045217Swdenk /* 801f045217Swdenk * Initialization, must be called once on start up, may be called 811f045217Swdenk * repeatedly to change the speed and slave addresses. 821f045217Swdenk */ 831f045217Swdenk void i2c_init(int speed, int slaveaddr); 84*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_I2C_INIT_BOARD 8506d01dbeSwdenk void i2c_init_board(void); 8606d01dbeSwdenk #endif 871f045217Swdenk 8867b23a32SHeiko Schocher #if defined(CONFIG_I2C_MUX) 8967b23a32SHeiko Schocher 9067b23a32SHeiko Schocher typedef struct _mux { 9167b23a32SHeiko Schocher uchar chip; 9267b23a32SHeiko Schocher uchar channel; 9367b23a32SHeiko Schocher char *name; 9467b23a32SHeiko Schocher struct _mux *next; 9567b23a32SHeiko Schocher } I2C_MUX; 9667b23a32SHeiko Schocher 9767b23a32SHeiko Schocher typedef struct _mux_device { 9867b23a32SHeiko Schocher int busid; 9967b23a32SHeiko Schocher I2C_MUX *mux; /* List of muxes, to reach the device */ 10067b23a32SHeiko Schocher struct _mux_device *next; 10167b23a32SHeiko Schocher } I2C_MUX_DEVICE; 10267b23a32SHeiko Schocher 10367b23a32SHeiko Schocher int i2c_mux_add_device(I2C_MUX_DEVICE *dev); 10467b23a32SHeiko Schocher 10567b23a32SHeiko Schocher I2C_MUX_DEVICE *i2c_mux_search_device(int id); 10667b23a32SHeiko Schocher I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf); 10767b23a32SHeiko Schocher int i2x_mux_select_mux(int bus); 10867b23a32SHeiko Schocher int i2c_mux_ident_muxstring_f (uchar *buf); 10967b23a32SHeiko Schocher #endif 11067b23a32SHeiko Schocher 1111f045217Swdenk /* 1121f045217Swdenk * Probe the given I2C chip address. Returns 0 if a chip responded, 1131f045217Swdenk * not 0 on failure. 1141f045217Swdenk */ 1151f045217Swdenk int i2c_probe(uchar chip); 1161f045217Swdenk 1171f045217Swdenk /* 1181f045217Swdenk * Read/Write interface: 1191f045217Swdenk * chip: I2C chip address, range 0..127 1201f045217Swdenk * addr: Memory (register) address within the chip 1211f045217Swdenk * alen: Number of bytes to use for addr (typically 1, 2 for larger 1221f045217Swdenk * memories, 0 for register type devices with only one 1231f045217Swdenk * register) 1241f045217Swdenk * buffer: Where to read/write the data 1251f045217Swdenk * len: How many bytes to read/write 1261f045217Swdenk * 1271f045217Swdenk * Returns: 0 on success, not 0 on failure 1281f045217Swdenk */ 1291f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); 1301f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); 1311f045217Swdenk 1321f045217Swdenk /* 1331f045217Swdenk * Utility routines to read/write registers. 1341f045217Swdenk */ 1351f045217Swdenk uchar i2c_reg_read (uchar chip, uchar reg); 1361f045217Swdenk void i2c_reg_write(uchar chip, uchar reg, uchar val); 1371f045217Swdenk 138bb99ad6dSBen Warren /* 139bb99ad6dSBen Warren * Functions for setting the current I2C bus and its speed 140bb99ad6dSBen Warren */ 141bb99ad6dSBen Warren 142bb99ad6dSBen Warren /* 143bb99ad6dSBen Warren * i2c_set_bus_num: 144bb99ad6dSBen Warren * 145bb99ad6dSBen Warren * Change the active I2C bus. Subsequent read/write calls will 146bb99ad6dSBen Warren * go to this one. 147bb99ad6dSBen Warren * 148bb99ad6dSBen Warren * bus - bus index, zero based 149bb99ad6dSBen Warren * 150bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 151bb99ad6dSBen Warren * 152bb99ad6dSBen Warren */ 1539ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus); 154bb99ad6dSBen Warren 155bb99ad6dSBen Warren /* 156bb99ad6dSBen Warren * i2c_get_bus_num: 157bb99ad6dSBen Warren * 158bb99ad6dSBen Warren * Returns index of currently active I2C bus. Zero-based. 159bb99ad6dSBen Warren */ 160bb99ad6dSBen Warren 1619ca880a2STimur Tabi unsigned int i2c_get_bus_num(void); 162bb99ad6dSBen Warren 163bb99ad6dSBen Warren /* 164bb99ad6dSBen Warren * i2c_set_bus_speed: 165bb99ad6dSBen Warren * 166bb99ad6dSBen Warren * Change the speed of the active I2C bus 167bb99ad6dSBen Warren * 168bb99ad6dSBen Warren * speed - bus speed in Hz 169bb99ad6dSBen Warren * 170bb99ad6dSBen Warren * Returns: 0 on success, not 0 on failure 171bb99ad6dSBen Warren * 172bb99ad6dSBen Warren */ 1739ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int); 174bb99ad6dSBen Warren 175bb99ad6dSBen Warren /* 176bb99ad6dSBen Warren * i2c_get_bus_speed: 177bb99ad6dSBen Warren * 178bb99ad6dSBen Warren * Returns speed of currently active I2C bus in Hz 179bb99ad6dSBen Warren */ 180bb99ad6dSBen Warren 1819ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void); 182bb99ad6dSBen Warren 1831f045217Swdenk #endif /* _I2C_H_ */ 184