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