wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Basic I2C functions |
| 3 | * |
| 4 | * Copyright (c) 2004 Texas Instruments |
| 5 | * |
| 6 | * This package is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the license found in the file |
| 8 | * named COPYING that should have accompanied this file. |
| 9 | * |
| 10 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 11 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 12 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Author: Jian Zhang jzhang@ti.com, Texas Instruments |
| 15 | * |
| 16 | * Copyright (c) 2003 Wolfgang Denk, wd@denx.de |
| 17 | * Rewritten to fit into the current U-Boot framework |
| 18 | * |
| 19 | * Adapted for OMAP2420 I2C, r-woodruff2@ti.com |
| 20 | * |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 21 | * Copyright (c) 2013 Lubomir Popov <lpopov@mm-sol.com>, MM Solutions |
| 22 | * New i2c_read, i2c_write and i2c_probe functions, tested on OMAP4 |
| 23 | * (4430/60/70), OMAP5 (5430) and AM335X (3359); should work on older |
| 24 | * OMAPs and derivatives as well. The only anticipated exception would |
| 25 | * be the OMAP2420, which shall require driver modification. |
| 26 | * - Rewritten i2c_read to operate correctly with all types of chips |
| 27 | * (old function could not read consistent data from some I2C slaves). |
| 28 | * - Optimized i2c_write. |
| 29 | * - New i2c_probe, performs write access vs read. The old probe could |
| 30 | * hang the system under certain conditions (e.g. unconfigured pads). |
| 31 | * - The read/write/probe functions try to identify unconfigured bus. |
| 32 | * - Status functions now read irqstatus_raw as per TRM guidelines |
| 33 | * (except for OMAP243X and OMAP34XX). |
| 34 | * - Driver now supports up to I2C5 (OMAP5). |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 35 | * |
Hannes Schmelzer | 4c302b9 | 2015-05-28 15:41:12 +0200 | [diff] [blame] | 36 | * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 37 | * - Added support for set_speed |
| 38 | * |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <common.h> |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 42 | #include <dm.h> |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 43 | #include <i2c.h> |
wdenk | 289f932 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 44 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 45 | #include <asm/io.h> |
| 46 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 47 | /* |
| 48 | * Provide access to architecture-specific I2C header files for platforms |
| 49 | * that are NOT yet solely relying on CONFIG_DM_I2C, CONFIG_OF_CONTROL, and |
| 50 | * the defaults provided in 'omap24xx_i2c.h' for all U-Boot stages where I2C |
| 51 | * access is desired. |
| 52 | */ |
| 53 | #ifndef CONFIG_ARCH_K3 |
| 54 | #include <asm/arch/i2c.h> |
| 55 | #endif |
| 56 | |
Steve Sakoman | 938717c | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 57 | #include "omap24xx_i2c.h" |
| 58 | |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 59 | #define I2C_TIMEOUT 1000 |
Steve Sakoman | d708395 | 2010-07-19 20:31:55 -0700 | [diff] [blame] | 60 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 61 | /* Absolutely safe for status update at 100 kHz I2C: */ |
| 62 | #define I2C_WAIT 200 |
| 63 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 64 | enum { |
| 65 | OMAP_I2C_REV_V1 = 0, |
| 66 | OMAP_I2C_REV_V2 = 1, |
| 67 | }; |
| 68 | |
| 69 | enum { |
| 70 | OMAP_I2C_REV_REG = 0, /* Only on IP V1 (OMAP34XX) */ |
| 71 | OMAP_I2C_IE_REG, /* Only on IP V1 (OMAP34XX) */ |
| 72 | OMAP_I2C_STAT_REG, |
| 73 | OMAP_I2C_WE_REG, |
| 74 | OMAP_I2C_SYSS_REG, |
| 75 | OMAP_I2C_BUF_REG, |
| 76 | OMAP_I2C_CNT_REG, |
| 77 | OMAP_I2C_DATA_REG, |
| 78 | OMAP_I2C_SYSC_REG, |
| 79 | OMAP_I2C_CON_REG, |
| 80 | OMAP_I2C_OA_REG, |
| 81 | OMAP_I2C_SA_REG, |
| 82 | OMAP_I2C_PSC_REG, |
| 83 | OMAP_I2C_SCLL_REG, |
| 84 | OMAP_I2C_SCLH_REG, |
| 85 | OMAP_I2C_SYSTEST_REG, |
| 86 | OMAP_I2C_BUFSTAT_REG, |
| 87 | /* Only on IP V2 (OMAP4430, etc.) */ |
| 88 | OMAP_I2C_IP_V2_REVNB_LO, |
| 89 | OMAP_I2C_IP_V2_REVNB_HI, |
| 90 | OMAP_I2C_IP_V2_IRQSTATUS_RAW, |
| 91 | OMAP_I2C_IP_V2_IRQENABLE_SET, |
| 92 | OMAP_I2C_IP_V2_IRQENABLE_CLR, |
| 93 | }; |
| 94 | |
| 95 | static const u8 __maybe_unused reg_map_ip_v1[] = { |
| 96 | [OMAP_I2C_REV_REG] = 0x00, |
| 97 | [OMAP_I2C_IE_REG] = 0x04, |
| 98 | [OMAP_I2C_STAT_REG] = 0x08, |
| 99 | [OMAP_I2C_WE_REG] = 0x0c, |
| 100 | [OMAP_I2C_SYSS_REG] = 0x10, |
| 101 | [OMAP_I2C_BUF_REG] = 0x14, |
| 102 | [OMAP_I2C_CNT_REG] = 0x18, |
| 103 | [OMAP_I2C_DATA_REG] = 0x1c, |
| 104 | [OMAP_I2C_SYSC_REG] = 0x20, |
| 105 | [OMAP_I2C_CON_REG] = 0x24, |
| 106 | [OMAP_I2C_OA_REG] = 0x28, |
| 107 | [OMAP_I2C_SA_REG] = 0x2c, |
| 108 | [OMAP_I2C_PSC_REG] = 0x30, |
| 109 | [OMAP_I2C_SCLL_REG] = 0x34, |
| 110 | [OMAP_I2C_SCLH_REG] = 0x38, |
| 111 | [OMAP_I2C_SYSTEST_REG] = 0x3c, |
| 112 | [OMAP_I2C_BUFSTAT_REG] = 0x40, |
| 113 | }; |
| 114 | |
| 115 | static const u8 __maybe_unused reg_map_ip_v2[] = { |
| 116 | [OMAP_I2C_STAT_REG] = 0x28, |
| 117 | [OMAP_I2C_WE_REG] = 0x34, |
| 118 | [OMAP_I2C_SYSS_REG] = 0x90, |
| 119 | [OMAP_I2C_BUF_REG] = 0x94, |
| 120 | [OMAP_I2C_CNT_REG] = 0x98, |
| 121 | [OMAP_I2C_DATA_REG] = 0x9c, |
| 122 | [OMAP_I2C_SYSC_REG] = 0x10, |
| 123 | [OMAP_I2C_CON_REG] = 0xa4, |
| 124 | [OMAP_I2C_OA_REG] = 0xa8, |
| 125 | [OMAP_I2C_SA_REG] = 0xac, |
| 126 | [OMAP_I2C_PSC_REG] = 0xb0, |
| 127 | [OMAP_I2C_SCLL_REG] = 0xb4, |
| 128 | [OMAP_I2C_SCLH_REG] = 0xb8, |
| 129 | [OMAP_I2C_SYSTEST_REG] = 0xbc, |
| 130 | [OMAP_I2C_BUFSTAT_REG] = 0xc0, |
| 131 | [OMAP_I2C_IP_V2_REVNB_LO] = 0x00, |
| 132 | [OMAP_I2C_IP_V2_REVNB_HI] = 0x04, |
| 133 | [OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24, |
| 134 | [OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c, |
| 135 | [OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30, |
| 136 | }; |
| 137 | |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 138 | struct omap_i2c { |
| 139 | struct udevice *clk; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 140 | int ip_rev; |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 141 | struct i2c *regs; |
| 142 | unsigned int speed; |
| 143 | int waitdelay; |
| 144 | int clk_id; |
| 145 | }; |
| 146 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 147 | static inline const u8 *omap_i2c_get_ip_reg_map(int ip_rev) |
| 148 | { |
| 149 | switch (ip_rev) { |
| 150 | case OMAP_I2C_REV_V1: |
| 151 | return reg_map_ip_v1; |
| 152 | case OMAP_I2C_REV_V2: |
| 153 | /* Fall through... */ |
| 154 | default: |
| 155 | return reg_map_ip_v2; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static inline void omap_i2c_write_reg(void __iomem *base, int ip_rev, |
| 160 | u16 val, int reg) |
| 161 | { |
| 162 | writew(val, base + omap_i2c_get_ip_reg_map(ip_rev)[reg]); |
| 163 | } |
| 164 | |
| 165 | static inline u16 omap_i2c_read_reg(void __iomem *base, int ip_rev, int reg) |
| 166 | { |
| 167 | return readw(base + omap_i2c_get_ip_reg_map(ip_rev)[reg]); |
| 168 | } |
| 169 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 170 | static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed) |
| 171 | { |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 172 | unsigned long internal_clk = 0, fclk; |
| 173 | unsigned int prescaler; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 174 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 175 | /* |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 176 | * This method is only called for Standard and Fast Mode speeds |
| 177 | * |
| 178 | * For some TI SoCs it is explicitly written in TRM (e,g, SPRUHZ6G, |
| 179 | * page 5685, Table 24-7) |
| 180 | * that the internal I2C clock (after prescaler) should be between |
| 181 | * 7-12 MHz (at least for Fast Mode (FS)). |
| 182 | * |
| 183 | * Such approach is used in v4.9 Linux kernel in: |
| 184 | * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function). |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 185 | */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 186 | |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 187 | speed /= 1000; /* convert speed to kHz */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 188 | |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 189 | if (speed > 100) |
| 190 | internal_clk = 9600; |
| 191 | else |
| 192 | internal_clk = 4000; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 193 | |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 194 | fclk = I2C_IP_CLK / 1000; |
| 195 | prescaler = fclk / internal_clk; |
| 196 | prescaler = prescaler - 1; |
| 197 | |
| 198 | if (speed > 100) { |
| 199 | unsigned long scl; |
| 200 | |
| 201 | /* Fast mode */ |
| 202 | scl = internal_clk / speed; |
| 203 | *pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM; |
| 204 | *psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM; |
| 205 | } else { |
| 206 | /* Standard mode */ |
| 207 | *pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM; |
| 208 | *psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 209 | } |
Lukasz Majewski | b52a3fa | 2017-03-15 16:59:23 +0100 | [diff] [blame] | 210 | |
| 211 | debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n", |
| 212 | __func__, speed, prescaler, *pscl, *psch); |
| 213 | |
| 214 | if (*pscl <= 0 || *psch <= 0 || prescaler <= 0) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | return prescaler; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 218 | } |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Wait for the bus to be free by checking the Bus Busy (BB) |
| 222 | * bit to become clear |
| 223 | */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 224 | static int wait_for_bb(void __iomem *i2c_base, int ip_rev, int waitdelay) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 225 | { |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 226 | int timeout = I2C_TIMEOUT; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 227 | int irq_stat_reg; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 228 | u16 stat; |
| 229 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 230 | irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ? |
| 231 | OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW; |
| 232 | |
| 233 | /* clear current interrupts */ |
| 234 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
| 235 | |
| 236 | while ((stat = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg) & |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 237 | I2C_STAT_BB) && timeout--) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 238 | omap_i2c_write_reg(i2c_base, ip_rev, stat, OMAP_I2C_STAT_REG); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 239 | udelay(waitdelay); |
| 240 | } |
| 241 | |
| 242 | if (timeout <= 0) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 243 | printf("Timed out in %s: status=%04x\n", __func__, stat); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 244 | return 1; |
| 245 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 246 | |
| 247 | /* clear delayed stuff */ |
| 248 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Wait for the I2C controller to complete current action |
| 254 | * and update status |
| 255 | */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 256 | static u16 wait_for_event(void __iomem *i2c_base, int ip_rev, int waitdelay) |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 257 | { |
| 258 | u16 status; |
| 259 | int timeout = I2C_TIMEOUT; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 260 | int irq_stat_reg; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 261 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 262 | irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ? |
| 263 | OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 264 | do { |
| 265 | udelay(waitdelay); |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 266 | status = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 267 | } while (!(status & |
| 268 | (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY | |
| 269 | I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK | |
| 270 | I2C_STAT_AL)) && timeout--); |
| 271 | |
| 272 | if (timeout <= 0) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 273 | printf("Timed out in %s: status=%04x\n", __func__, status); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 274 | /* |
| 275 | * If status is still 0 here, probably the bus pads have |
| 276 | * not been configured for I2C, and/or pull-ups are missing. |
| 277 | */ |
| 278 | printf("Check if pads/pull-ups of bus are properly configured\n"); |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 279 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 280 | status = 0; |
| 281 | } |
| 282 | |
| 283 | return status; |
| 284 | } |
| 285 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 286 | static void flush_fifo(void __iomem *i2c_base, int ip_rev) |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 287 | { |
| 288 | u16 stat; |
| 289 | |
| 290 | /* |
| 291 | * note: if you try and read data when its not there or ready |
| 292 | * you get a bus error |
| 293 | */ |
| 294 | while (1) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 295 | stat = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_STAT_REG); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 296 | if (stat == I2C_STAT_RRDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 297 | omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_DATA_REG); |
| 298 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 299 | I2C_STAT_RRDY, OMAP_I2C_STAT_REG); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 300 | udelay(1000); |
| 301 | } else |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 306 | static int __omap24_i2c_setspeed(void __iomem *i2c_base, int ip_rev, uint speed, |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 307 | int *waitdelay) |
| 308 | { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 309 | int psc, fsscll = 0, fssclh = 0; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 310 | int hsscll = 0, hssclh = 0; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 311 | u32 scll = 0, sclh = 0; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 312 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 313 | if (speed >= OMAP_I2C_HIGH_SPEED) { |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 314 | /* High speed */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 315 | psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK; |
| 316 | psc -= 1; |
| 317 | if (psc < I2C_PSC_MIN) { |
| 318 | printf("Error : I2C unsupported prescaler %d\n", psc); |
| 319 | return -1; |
| 320 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 321 | |
| 322 | /* For first phase of HS mode */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 323 | fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 324 | |
| 325 | fssclh = fsscll; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 326 | |
| 327 | fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM; |
| 328 | fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM; |
| 329 | if (((fsscll < 0) || (fssclh < 0)) || |
| 330 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 331 | puts("Error : I2C initializing first phase clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 332 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* For second phase of HS mode */ |
| 336 | hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed); |
| 337 | |
| 338 | hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM; |
| 339 | hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM; |
| 340 | if (((fsscll < 0) || (fssclh < 0)) || |
| 341 | ((fsscll > 255) || (fssclh > 255))) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 342 | puts("Error : I2C initializing second phase clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 343 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll; |
| 347 | sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh; |
| 348 | |
| 349 | } else { |
| 350 | /* Standard and fast speed */ |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 351 | psc = omap24_i2c_findpsc(&scll, &sclh, speed); |
| 352 | if (0 > psc) { |
Andreas Müller | 49e9b4b | 2012-01-04 15:26:19 +0000 | [diff] [blame] | 353 | puts("Error : I2C initializing clock\n"); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 354 | return -1; |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 355 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 356 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 357 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 358 | /* wait for 20 clkperiods */ |
| 359 | *waitdelay = (10000000 / speed) * 2; |
| 360 | |
| 361 | omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG); |
| 362 | omap_i2c_write_reg(i2c_base, ip_rev, psc, OMAP_I2C_PSC_REG); |
| 363 | omap_i2c_write_reg(i2c_base, ip_rev, scll, OMAP_I2C_SCLL_REG); |
| 364 | omap_i2c_write_reg(i2c_base, ip_rev, sclh, OMAP_I2C_SCLH_REG); |
| 365 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG); |
| 366 | |
| 367 | /* clear all pending status */ |
| 368 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | } |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 372 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 373 | static void omap24_i2c_deblock(void __iomem *i2c_base, int ip_rev) |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 374 | { |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 375 | int i; |
| 376 | u16 systest; |
| 377 | u16 orgsystest; |
| 378 | |
| 379 | /* set test mode ST_EN = 1 */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 380 | orgsystest = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 381 | systest = orgsystest; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 382 | |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 383 | /* enable testmode */ |
| 384 | systest |= I2C_SYSTEST_ST_EN; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 385 | omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 386 | systest &= ~I2C_SYSTEST_TMODE_MASK; |
| 387 | systest |= 3 << I2C_SYSTEST_TMODE_SHIFT; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 388 | omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 389 | |
| 390 | /* set SCL, SDA = 1 */ |
| 391 | systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 392 | omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 393 | udelay(10); |
| 394 | |
| 395 | /* toggle scl 9 clocks */ |
| 396 | for (i = 0; i < 9; i++) { |
| 397 | /* SCL = 0 */ |
| 398 | systest &= ~I2C_SYSTEST_SCL_O; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 399 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 400 | systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 401 | udelay(10); |
| 402 | /* SCL = 1 */ |
| 403 | systest |= I2C_SYSTEST_SCL_O; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 404 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 405 | systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 406 | udelay(10); |
| 407 | } |
| 408 | |
| 409 | /* send stop */ |
| 410 | systest &= ~I2C_SYSTEST_SDA_O; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 411 | omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 412 | udelay(10); |
| 413 | systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 414 | omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 415 | udelay(10); |
| 416 | |
| 417 | /* restore original mode */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 418 | omap_i2c_write_reg(i2c_base, ip_rev, orgsystest, OMAP_I2C_SYSTEST_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 421 | static void __omap24_i2c_init(void __iomem *i2c_base, int ip_rev, int speed, |
| 422 | int slaveadd, int *waitdelay) |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 423 | { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 424 | int timeout = I2C_TIMEOUT; |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 425 | int deblock = 1; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 426 | |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 427 | retry: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 428 | if (omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_CON_REG) & |
| 429 | I2C_CON_EN) { |
| 430 | omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG); |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 431 | udelay(50000); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 434 | /* for ES2 after soft reset */ |
| 435 | omap_i2c_write_reg(i2c_base, ip_rev, 0x2, OMAP_I2C_SYSC_REG); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 436 | udelay(1000); |
| 437 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 438 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG); |
| 439 | while (!(omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSS_REG) & |
| 440 | I2C_SYSS_RDONE) && timeout--) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 441 | if (timeout <= 0) { |
| 442 | puts("ERROR: Timeout in soft-reset\n"); |
| 443 | return; |
| 444 | } |
| 445 | udelay(1000); |
| 446 | } |
| 447 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 448 | if (__omap24_i2c_setspeed(i2c_base, ip_rev, speed, waitdelay)) { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 449 | printf("ERROR: failed to setup I2C bus-speed!\n"); |
| 450 | return; |
| 451 | } |
Tom Rix | 7f79dfb | 2009-06-28 12:52:27 -0500 | [diff] [blame] | 452 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 453 | /* own address */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 454 | omap_i2c_write_reg(i2c_base, ip_rev, slaveadd, OMAP_I2C_OA_REG); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 455 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 456 | if (ip_rev == OMAP_I2C_REV_V1) { |
| 457 | /* |
| 458 | * Have to enable interrupts for OMAP2/3, these IPs don't have |
| 459 | * an 'irqstatus_raw' register and we shall have to poll 'stat' |
| 460 | */ |
| 461 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_IE_XRDY_IE | |
| 462 | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | |
| 463 | I2C_IE_NACK_IE | I2C_IE_AL_IE, |
| 464 | OMAP_I2C_IE_REG); |
| 465 | } |
| 466 | |
Michael Jones | 89677b2 | 2011-07-27 14:01:55 -0400 | [diff] [blame] | 467 | udelay(1000); |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 468 | flush_fifo(i2c_base, ip_rev); |
| 469 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 470 | |
| 471 | /* Handle possible failed I2C state */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 472 | if (wait_for_bb(i2c_base, ip_rev, *waitdelay)) |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 473 | if (deblock == 1) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 474 | omap24_i2c_deblock(i2c_base, ip_rev); |
Heiko Schocher | f7c1053 | 2014-06-30 09:12:09 +0200 | [diff] [blame] | 475 | deblock = 0; |
| 476 | goto retry; |
| 477 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 480 | /* |
| 481 | * i2c_probe: Use write access. Allows to identify addresses that are |
| 482 | * write-only (like the config register of dual-port EEPROMs) |
| 483 | */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 484 | static int __omap24_i2c_probe(void __iomem *i2c_base, int ip_rev, int waitdelay, |
| 485 | uchar chip) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 486 | { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 487 | u16 status; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 488 | int res = 1; /* default = fail */ |
| 489 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 490 | if (chip == omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_OA_REG)) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 491 | return res; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 492 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 493 | /* Wait until bus is free */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 494 | if (wait_for_bb(i2c_base, ip_rev, waitdelay)) |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 495 | return res; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 496 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 497 | /* No data transfer, slave addr only */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 498 | omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 499 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 500 | /* Stop bit needed here */ |
| 501 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 502 | I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP, |
| 503 | OMAP_I2C_CON_REG); |
| 504 | |
| 505 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 506 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 507 | if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) { |
| 508 | /* |
| 509 | * With current high-level command implementation, notifying |
| 510 | * the user shall flood the console with 127 messages. If |
| 511 | * silent exit is desired upon unconfigured bus, remove the |
| 512 | * following 'if' section: |
| 513 | */ |
| 514 | if (status == I2C_STAT_XRDY) |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 515 | printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n", |
| 516 | status); |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 517 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 518 | goto pr_exit; |
Tom Rini | 168a5ac | 2012-05-21 06:46:29 +0000 | [diff] [blame] | 519 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 520 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 521 | /* Check for ACK (!NAK) */ |
| 522 | if (!(status & I2C_STAT_NACK)) { |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 523 | res = 0; /* Device found */ |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 524 | udelay(waitdelay);/* Required by AM335X in SPL */ |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 525 | /* Abort transfer (force idle state) */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 526 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_MST | I2C_CON_TRX, |
| 527 | OMAP_I2C_CON_REG); /* Reset */ |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 528 | udelay(1000); |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 529 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 530 | I2C_CON_TRX | I2C_CON_STP, |
| 531 | OMAP_I2C_CON_REG); /* STP */ |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 532 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 533 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 534 | pr_exit: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 535 | flush_fifo(i2c_base, ip_rev); |
| 536 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 537 | return res; |
| 538 | } |
| 539 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 540 | /* |
| 541 | * i2c_read: Function now uses a single I2C read transaction with bulk transfer |
| 542 | * of the requested number of bytes (note that the 'i2c md' command |
| 543 | * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is |
| 544 | * defined in the board config header, this transaction shall be with |
| 545 | * Repeated Start (Sr) between the address and data phases; otherwise |
| 546 | * Stop-Start (P-S) shall be used (some I2C chips do require a P-S). |
| 547 | * The address (reg offset) may be 0, 1 or 2 bytes long. |
| 548 | * Function now reads correctly from chips that return more than one |
| 549 | * byte of data per addressed register (like TI temperature sensors), |
| 550 | * or that do not need a register address at all (such as some clock |
| 551 | * distributors). |
| 552 | */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 553 | static int __omap24_i2c_read(void __iomem *i2c_base, int ip_rev, int waitdelay, |
| 554 | uchar chip, uint addr, int alen, uchar *buffer, |
| 555 | int len) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 556 | { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 557 | int i2c_error = 0; |
| 558 | u16 status; |
| 559 | |
| 560 | if (alen < 0) { |
| 561 | puts("I2C read: addr len < 0\n"); |
| 562 | return 1; |
| 563 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 564 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 565 | if (len < 0) { |
| 566 | puts("I2C read: data len < 0\n"); |
| 567 | return 1; |
| 568 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 569 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 570 | if (buffer == NULL) { |
| 571 | puts("I2C read: NULL pointer passed\n"); |
| 572 | return 1; |
| 573 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 574 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 575 | if (alen > 2) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 576 | printf("I2C read: addr len %d not supported\n", alen); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 577 | return 1; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 578 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 579 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 580 | if (addr + len > (1 << 16)) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 581 | puts("I2C read: address out of range\n"); |
| 582 | return 1; |
| 583 | } |
| 584 | |
Guy Thouret | 32b9b55 | 2016-03-11 16:23:41 +0000 | [diff] [blame] | 585 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 586 | /* |
| 587 | * EEPROM chips that implement "address overflow" are ones |
| 588 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 589 | * address and the extra bits end up in the "chip address" |
| 590 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 591 | * four 256 byte chips. |
| 592 | * |
| 593 | * Note that we consider the length of the address field to |
| 594 | * still be one byte because the extra address bits are |
| 595 | * hidden in the chip address. |
| 596 | */ |
| 597 | if (alen > 0) |
| 598 | chip |= ((addr >> (alen * 8)) & |
| 599 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 600 | #endif |
| 601 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 602 | /* Wait until bus not busy */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 603 | if (wait_for_bb(i2c_base, ip_rev, waitdelay)) |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 604 | return 1; |
| 605 | |
| 606 | /* Zero, one or two bytes reg address (offset) */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 607 | omap_i2c_write_reg(i2c_base, ip_rev, alen, OMAP_I2C_CNT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 608 | /* Set slave address */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 609 | omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 610 | |
| 611 | if (alen) { |
| 612 | /* Must write reg offset first */ |
| 613 | #ifdef CONFIG_I2C_REPEATED_START |
| 614 | /* No stop bit, use Repeated Start (Sr) */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 615 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 616 | I2C_CON_STT | I2C_CON_TRX, OMAP_I2C_CON_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 617 | #else |
| 618 | /* Stop - Start (P-S) */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 619 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 620 | I2C_CON_STT | I2C_CON_STP | I2C_CON_TRX, |
| 621 | OMAP_I2C_CON_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 622 | #endif |
| 623 | /* Send register offset */ |
| 624 | while (1) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 625 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 626 | /* Try to identify bus that is not padconf'd for I2C */ |
| 627 | if (status == I2C_STAT_XRDY) { |
| 628 | i2c_error = 2; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 629 | printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n", |
| 630 | status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 631 | goto rd_exit; |
| 632 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 633 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 634 | i2c_error = 1; |
| 635 | printf("i2c_read: error waiting for addr ACK (status=0x%x)\n", |
| 636 | status); |
| 637 | goto rd_exit; |
| 638 | } |
| 639 | if (alen) { |
| 640 | if (status & I2C_STAT_XRDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 641 | u8 addr_byte; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 642 | alen--; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 643 | addr_byte = (addr >> (8 * alen)) & 0xff; |
| 644 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 645 | addr_byte, |
| 646 | OMAP_I2C_DATA_REG); |
| 647 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 648 | I2C_STAT_XRDY, |
| 649 | OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | if (status & I2C_STAT_ARDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 653 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 654 | I2C_STAT_ARDY, |
| 655 | OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | } |
| 659 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 660 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 661 | /* Set slave address */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 662 | omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 663 | /* Read len bytes from slave */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 664 | omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 665 | /* Need stop bit here */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 666 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 667 | I2C_CON_STT | I2C_CON_STP, OMAP_I2C_CON_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 668 | |
| 669 | /* Receive data */ |
| 670 | while (1) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 671 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 672 | /* |
| 673 | * Try to identify bus that is not padconf'd for I2C. This |
| 674 | * state could be left over from previous transactions if |
| 675 | * the address phase is skipped due to alen=0. |
| 676 | */ |
| 677 | if (status == I2C_STAT_XRDY) { |
| 678 | i2c_error = 2; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 679 | printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n", |
| 680 | status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 681 | goto rd_exit; |
| 682 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 683 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 684 | i2c_error = 1; |
| 685 | goto rd_exit; |
| 686 | } |
| 687 | if (status & I2C_STAT_RRDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 688 | *buffer++ = omap_i2c_read_reg(i2c_base, ip_rev, |
| 689 | OMAP_I2C_DATA_REG); |
| 690 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 691 | I2C_STAT_RRDY, OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 692 | } |
| 693 | if (status & I2C_STAT_ARDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 694 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 695 | I2C_STAT_ARDY, OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 696 | break; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 700 | rd_exit: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 701 | flush_fifo(i2c_base, ip_rev); |
| 702 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 703 | return i2c_error; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 706 | /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 707 | static int __omap24_i2c_write(void __iomem *i2c_base, int ip_rev, int waitdelay, |
| 708 | uchar chip, uint addr, int alen, uchar *buffer, |
| 709 | int len) |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 710 | { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 711 | int i; |
| 712 | u16 status; |
| 713 | int i2c_error = 0; |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 714 | int timeout = I2C_TIMEOUT; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 715 | |
| 716 | if (alen < 0) { |
| 717 | puts("I2C write: addr len < 0\n"); |
| 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | if (len < 0) { |
| 722 | puts("I2C write: data len < 0\n"); |
| 723 | return 1; |
| 724 | } |
| 725 | |
| 726 | if (buffer == NULL) { |
| 727 | puts("I2C write: NULL pointer passed\n"); |
| 728 | return 1; |
| 729 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 730 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 731 | if (alen > 2) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 732 | printf("I2C write: addr len %d not supported\n", alen); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 733 | return 1; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 734 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 735 | |
Ilya Yanok | 55faa58 | 2012-06-08 03:12:09 +0000 | [diff] [blame] | 736 | if (addr + len > (1 << 16)) { |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 737 | printf("I2C write: address 0x%x + 0x%x out of range\n", |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 738 | addr, len); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 739 | return 1; |
| 740 | } |
| 741 | |
Guy Thouret | 32b9b55 | 2016-03-11 16:23:41 +0000 | [diff] [blame] | 742 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 743 | /* |
| 744 | * EEPROM chips that implement "address overflow" are ones |
| 745 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 746 | * address and the extra bits end up in the "chip address" |
| 747 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 748 | * four 256 byte chips. |
| 749 | * |
| 750 | * Note that we consider the length of the address field to |
| 751 | * still be one byte because the extra address bits are |
| 752 | * hidden in the chip address. |
| 753 | */ |
| 754 | if (alen > 0) |
| 755 | chip |= ((addr >> (alen * 8)) & |
| 756 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 757 | #endif |
| 758 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 759 | /* Wait until bus not busy */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 760 | if (wait_for_bb(i2c_base, ip_rev, waitdelay)) |
Vincent Stehlé | febc4cd | 2012-12-03 05:23:16 +0000 | [diff] [blame] | 761 | return 1; |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 762 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 763 | /* Start address phase - will write regoffset + len bytes data */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 764 | omap_i2c_write_reg(i2c_base, ip_rev, alen + len, OMAP_I2C_CNT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 765 | /* Set slave address */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 766 | omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 767 | /* Stop bit needed here */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 768 | omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST | |
| 769 | I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP, |
| 770 | OMAP_I2C_CON_REG); |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 771 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 772 | while (alen) { |
| 773 | /* Must write reg offset (one or two bytes) */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 774 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 775 | /* Try to identify bus that is not padconf'd for I2C */ |
| 776 | if (status == I2C_STAT_XRDY) { |
| 777 | i2c_error = 2; |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 778 | printf("i2c_write: pads on bus probably not configured (status=0x%x)\n", |
| 779 | status); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 780 | goto wr_exit; |
| 781 | } |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 782 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Michael Jones | 0607e2b | 2011-09-04 14:01:55 -0400 | [diff] [blame] | 783 | i2c_error = 1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 784 | printf("i2c_write: error waiting for addr ACK (status=0x%x)\n", |
| 785 | status); |
| 786 | goto wr_exit; |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 787 | } |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 788 | if (status & I2C_STAT_XRDY) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 789 | alen--; |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 790 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 791 | (addr >> (8 * alen)) & 0xff, |
| 792 | OMAP_I2C_DATA_REG); |
| 793 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 794 | I2C_STAT_XRDY, OMAP_I2C_STAT_REG); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 795 | } else { |
| 796 | i2c_error = 1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 797 | printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n", |
| 798 | status); |
| 799 | goto wr_exit; |
| 800 | } |
| 801 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 802 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 803 | /* Address phase is over, now write data */ |
| 804 | for (i = 0; i < len; i++) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 805 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 806 | if (status == 0 || (status & I2C_STAT_NACK)) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 807 | i2c_error = 1; |
| 808 | printf("i2c_write: error waiting for data ACK (status=0x%x)\n", |
| 809 | status); |
| 810 | goto wr_exit; |
| 811 | } |
| 812 | if (status & I2C_STAT_XRDY) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 813 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 814 | buffer[i], OMAP_I2C_DATA_REG); |
| 815 | omap_i2c_write_reg(i2c_base, ip_rev, |
| 816 | I2C_STAT_XRDY, OMAP_I2C_STAT_REG); |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 817 | } else { |
| 818 | i2c_error = 1; |
| 819 | printf("i2c_write: bus not ready for data Tx (i=%d)\n", |
| 820 | i); |
| 821 | goto wr_exit; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 824 | |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 825 | /* |
| 826 | * poll ARDY bit for making sure that last byte really has been |
| 827 | * transferred on the bus. |
| 828 | */ |
| 829 | do { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 830 | status = wait_for_event(i2c_base, ip_rev, waitdelay); |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 831 | } while (!(status & I2C_STAT_ARDY) && timeout--); |
| 832 | if (timeout <= 0) |
| 833 | printf("i2c_write: timed out writig last byte!\n"); |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 834 | |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 835 | wr_exit: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 836 | flush_fifo(i2c_base, ip_rev); |
| 837 | omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG); |
Tom Rini | cec487a | 2012-02-20 18:49:16 +0000 | [diff] [blame] | 838 | return i2c_error; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 841 | #ifndef CONFIG_DM_I2C |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 842 | /* |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 843 | * The legacy I2C functions. These need to get removed once |
| 844 | * all users of this driver are converted to DM. |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 845 | */ |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 846 | static void __iomem *omap24_get_base(struct i2c_adapter *adap) |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 847 | { |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 848 | switch (adap->hwadapnr) { |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 849 | case 0: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 850 | return (void __iomem *)I2C_BASE1; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 851 | break; |
| 852 | case 1: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 853 | return (void __iomem *)I2C_BASE2; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 854 | break; |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 855 | #if (CONFIG_SYS_I2C_BUS_MAX > 2) |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 856 | case 2: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 857 | return (void __iomem *)I2C_BASE3; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 858 | break; |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 859 | #if (CONFIG_SYS_I2C_BUS_MAX > 3) |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 860 | case 3: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 861 | return (void __iomem *)I2C_BASE4; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 862 | break; |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 863 | #if (CONFIG_SYS_I2C_BUS_MAX > 4) |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 864 | case 4: |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 865 | return (void __iomem *)I2C_BASE5; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 866 | break; |
| 867 | #endif |
| 868 | #endif |
| 869 | #endif |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 870 | default: |
| 871 | printf("wrong hwadapnr: %d\n", adap->hwadapnr); |
| 872 | break; |
Lubomir Popov | 960187f | 2013-06-01 06:44:38 +0000 | [diff] [blame] | 873 | } |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 874 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 875 | return NULL; |
Dirk Behme | 1d2e96d | 2009-11-02 20:36:26 +0100 | [diff] [blame] | 876 | } |
Steve Sakoman | 938717c | 2010-06-12 06:42:57 -0700 | [diff] [blame] | 877 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 878 | static int omap24_get_ip_rev(void) |
| 879 | { |
| 880 | #ifdef CONFIG_OMAP34XX |
| 881 | return OMAP_I2C_REV_V1; |
| 882 | #else |
| 883 | return OMAP_I2C_REV_V2; |
| 884 | #endif |
| 885 | } |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 886 | |
| 887 | static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 888 | int alen, uchar *buffer, int len) |
| 889 | { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 890 | void __iomem *i2c_base = omap24_get_base(adap); |
| 891 | int ip_rev = omap24_get_ip_rev(); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 892 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 893 | return __omap24_i2c_read(i2c_base, ip_rev, adap->waitdelay, chip, addr, |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 894 | alen, buffer, len); |
| 895 | } |
| 896 | |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 897 | static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 898 | int alen, uchar *buffer, int len) |
| 899 | { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 900 | void __iomem *i2c_base = omap24_get_base(adap); |
| 901 | int ip_rev = omap24_get_ip_rev(); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 902 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 903 | return __omap24_i2c_write(i2c_base, ip_rev, adap->waitdelay, chip, addr, |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 904 | alen, buffer, len); |
| 905 | } |
| 906 | |
| 907 | static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed) |
| 908 | { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 909 | void __iomem *i2c_base = omap24_get_base(adap); |
| 910 | int ip_rev = omap24_get_ip_rev(); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 911 | int ret; |
| 912 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 913 | ret = __omap24_i2c_setspeed(i2c_base, ip_rev, speed, &adap->waitdelay); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 914 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 915 | pr_err("%s: set i2c speed failed\n", __func__); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | adap->speed = speed; |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 925 | { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 926 | void __iomem *i2c_base = omap24_get_base(adap); |
| 927 | int ip_rev = omap24_get_ip_rev(); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 928 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 929 | return __omap24_i2c_init(i2c_base, ip_rev, speed, slaveadd, |
| 930 | &adap->waitdelay); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip) |
| 934 | { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 935 | void __iomem *i2c_base = omap24_get_base(adap); |
| 936 | int ip_rev = omap24_get_ip_rev(); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 937 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 938 | return __omap24_i2c_probe(i2c_base, ip_rev, adap->waitdelay, chip); |
Mugunthan V N | be243e4 | 2016-07-18 15:11:00 +0530 | [diff] [blame] | 939 | } |
| 940 | |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 941 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1) |
| 942 | #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED |
| 943 | #endif |
| 944 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1) |
| 945 | #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 946 | #endif |
| 947 | |
| 948 | U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe, |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 949 | omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed, |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 950 | CONFIG_SYS_OMAP24_I2C_SPEED, |
| 951 | CONFIG_SYS_OMAP24_I2C_SLAVE, |
| 952 | 0) |
| 953 | U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe, |
Hannes Petermaier | d524335 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 954 | omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed, |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 955 | CONFIG_SYS_OMAP24_I2C_SPEED1, |
| 956 | CONFIG_SYS_OMAP24_I2C_SLAVE1, |
| 957 | 1) |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 958 | |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 959 | #if (CONFIG_SYS_I2C_BUS_MAX > 2) |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 960 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2) |
| 961 | #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED |
| 962 | #endif |
| 963 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2) |
| 964 | #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 965 | #endif |
| 966 | |
| 967 | U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe, |
| 968 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 969 | CONFIG_SYS_OMAP24_I2C_SPEED2, |
| 970 | CONFIG_SYS_OMAP24_I2C_SLAVE2, |
| 971 | 2) |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 972 | #if (CONFIG_SYS_I2C_BUS_MAX > 3) |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 973 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3) |
| 974 | #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED |
| 975 | #endif |
| 976 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3) |
| 977 | #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 978 | #endif |
| 979 | |
| 980 | U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe, |
| 981 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 982 | CONFIG_SYS_OMAP24_I2C_SPEED3, |
| 983 | CONFIG_SYS_OMAP24_I2C_SLAVE3, |
| 984 | 3) |
Adam Ford | ac1d8ac | 2017-08-11 06:39:13 -0500 | [diff] [blame] | 985 | #if (CONFIG_SYS_I2C_BUS_MAX > 4) |
Heiko Schocher | 6789e84 | 2013-10-22 11:03:18 +0200 | [diff] [blame] | 986 | #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4) |
| 987 | #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED |
| 988 | #endif |
| 989 | #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4) |
| 990 | #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE |
| 991 | #endif |
| 992 | |
| 993 | U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe, |
| 994 | omap24_i2c_read, omap24_i2c_write, NULL, |
| 995 | CONFIG_SYS_OMAP24_I2C_SPEED4, |
| 996 | CONFIG_SYS_OMAP24_I2C_SLAVE4, |
| 997 | 4) |
| 998 | #endif |
| 999 | #endif |
| 1000 | #endif |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1001 | |
| 1002 | #else /* CONFIG_DM_I2C */ |
| 1003 | |
| 1004 | static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 1005 | { |
| 1006 | struct omap_i2c *priv = dev_get_priv(bus); |
| 1007 | int ret; |
| 1008 | |
| 1009 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 1010 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 1011 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 1012 | if (msg->flags & I2C_M_RD) { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1013 | ret = __omap24_i2c_read(priv->regs, priv->ip_rev, |
| 1014 | priv->waitdelay, |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1015 | msg->addr, 0, 0, msg->buf, |
| 1016 | msg->len); |
| 1017 | } else { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1018 | ret = __omap24_i2c_write(priv->regs, priv->ip_rev, |
| 1019 | priv->waitdelay, |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1020 | msg->addr, 0, 0, msg->buf, |
| 1021 | msg->len); |
| 1022 | } |
| 1023 | if (ret) { |
| 1024 | debug("i2c_write: error sending\n"); |
| 1025 | return -EREMOTEIO; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 1033 | { |
| 1034 | struct omap_i2c *priv = dev_get_priv(bus); |
| 1035 | |
| 1036 | priv->speed = speed; |
| 1037 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1038 | return __omap24_i2c_setspeed(priv->regs, priv->ip_rev, speed, |
| 1039 | &priv->waitdelay); |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 1043 | uint chip_flags) |
| 1044 | { |
| 1045 | struct omap_i2c *priv = dev_get_priv(bus); |
| 1046 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1047 | return __omap24_i2c_probe(priv->regs, priv->ip_rev, priv->waitdelay, |
| 1048 | chip_addr); |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static int omap_i2c_probe(struct udevice *bus) |
| 1052 | { |
| 1053 | struct omap_i2c *priv = dev_get_priv(bus); |
| 1054 | |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1055 | priv->ip_rev = dev_get_driver_data(bus); |
| 1056 | |
| 1057 | __omap24_i2c_init(priv->regs, priv->ip_rev, priv->speed, 0, |
| 1058 | &priv->waitdelay); |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Adam Ford | 410c505 | 2018-08-20 20:24:35 -0500 | [diff] [blame] | 1063 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1064 | static int omap_i2c_ofdata_to_platdata(struct udevice *bus) |
| 1065 | { |
| 1066 | struct omap_i2c *priv = dev_get_priv(bus); |
| 1067 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 1068 | priv->regs = map_physmem(devfdt_get_addr(bus), sizeof(void *), |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1069 | MAP_NOCACHE); |
| 1070 | priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED; |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Adam Ford | 410c505 | 2018-08-20 20:24:35 -0500 | [diff] [blame] | 1075 | static const struct udevice_id omap_i2c_ids[] = { |
Vignesh R | bca09ce | 2018-12-07 14:50:41 +0100 | [diff] [blame^] | 1076 | { .compatible = "ti,omap3-i2c", .data = OMAP_I2C_REV_V1 }, |
| 1077 | { .compatible = "ti,omap4-i2c", .data = OMAP_I2C_REV_V2 }, |
Adam Ford | 410c505 | 2018-08-20 20:24:35 -0500 | [diff] [blame] | 1078 | { } |
| 1079 | }; |
| 1080 | #endif |
| 1081 | |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1082 | static const struct dm_i2c_ops omap_i2c_ops = { |
| 1083 | .xfer = omap_i2c_xfer, |
| 1084 | .probe_chip = omap_i2c_probe_chip, |
| 1085 | .set_bus_speed = omap_i2c_set_bus_speed, |
| 1086 | }; |
| 1087 | |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1088 | U_BOOT_DRIVER(i2c_omap) = { |
| 1089 | .name = "i2c_omap", |
| 1090 | .id = UCLASS_I2C, |
Adam Ford | 410c505 | 2018-08-20 20:24:35 -0500 | [diff] [blame] | 1091 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1092 | .of_match = omap_i2c_ids, |
| 1093 | .ofdata_to_platdata = omap_i2c_ofdata_to_platdata, |
Adam Ford | 410c505 | 2018-08-20 20:24:35 -0500 | [diff] [blame] | 1094 | #endif |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1095 | .probe = omap_i2c_probe, |
| 1096 | .priv_auto_alloc_size = sizeof(struct omap_i2c), |
| 1097 | .ops = &omap_i2c_ops, |
Bin Meng | e0cfc20 | 2018-10-24 06:36:31 -0700 | [diff] [blame] | 1098 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1099 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | e0cfc20 | 2018-10-24 06:36:31 -0700 | [diff] [blame] | 1100 | #endif |
Mugunthan V N | daa69ff | 2016-07-18 15:11:01 +0530 | [diff] [blame] | 1101 | }; |
| 1102 | |
| 1103 | #endif /* CONFIG_DM_I2C */ |