Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Broadcom |
| 4 | * |
| 5 | */ |
| 6 | |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <common.h> |
| 10 | #include <config.h> |
| 11 | #include <dm.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 12 | #include <linux/printk.h> |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 13 | #include "errno.h" |
| 14 | #include <i2c.h> |
| 15 | #include "iproc_i2c.h" |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct iproc_i2c_regs { |
| 20 | u32 cfg_reg; |
| 21 | u32 timg_cfg; |
| 22 | u32 addr_reg; |
| 23 | u32 mstr_fifo_ctrl; |
| 24 | u32 slv_fifo_ctrl; |
| 25 | u32 bitbng_ctrl; |
| 26 | u32 blnks[6]; /* Not to be used */ |
| 27 | u32 mstr_cmd; |
| 28 | u32 slv_cmd; |
| 29 | u32 evt_en; |
| 30 | u32 evt_sts; |
| 31 | u32 mstr_datawr; |
| 32 | u32 mstr_datard; |
| 33 | u32 slv_datawr; |
| 34 | u32 slv_datard; |
| 35 | }; |
| 36 | |
| 37 | struct iproc_i2c { |
| 38 | struct iproc_i2c_regs __iomem *base; /* register base */ |
| 39 | int bus_speed; |
| 40 | int i2c_init_done; |
| 41 | }; |
| 42 | |
| 43 | /* Function to read a value from specified register. */ |
| 44 | static unsigned int iproc_i2c_reg_read(u32 *reg_addr) |
| 45 | { |
| 46 | unsigned int val; |
| 47 | |
| 48 | val = readl((void *)(reg_addr)); |
| 49 | return cpu_to_le32(val); |
| 50 | } |
| 51 | |
| 52 | /* Function to write a value ('val') in to a specified register. */ |
| 53 | static int iproc_i2c_reg_write(u32 *reg_addr, unsigned int val) |
| 54 | { |
| 55 | val = cpu_to_le32(val); |
| 56 | writel(val, (void *)(reg_addr)); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | #if defined(DEBUG) |
| 61 | static int iproc_dump_i2c_regs(struct iproc_i2c *bus_prvdata) |
| 62 | { |
| 63 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 64 | unsigned int regval; |
| 65 | |
| 66 | debug("\n----------------------------------------------\n"); |
| 67 | debug("%s: Dumping SMBus registers...\n", __func__); |
| 68 | |
| 69 | regval = iproc_i2c_reg_read(&base->cfg_reg); |
| 70 | debug("CCB_SMB_CFG_REG=0x%08X\n", regval); |
| 71 | |
| 72 | regval = iproc_i2c_reg_read(&base->timg_cfg); |
| 73 | debug("CCB_SMB_TIMGCFG_REG=0x%08X\n", regval); |
| 74 | |
| 75 | regval = iproc_i2c_reg_read(&base->addr_reg); |
| 76 | debug("CCB_SMB_ADDR_REG=0x%08X\n", regval); |
| 77 | |
| 78 | regval = iproc_i2c_reg_read(&base->mstr_fifo_ctrl); |
| 79 | debug("CCB_SMB_MSTRFIFOCTL_REG=0x%08X\n", regval); |
| 80 | |
| 81 | regval = iproc_i2c_reg_read(&base->slv_fifo_ctrl); |
| 82 | debug("CCB_SMB_SLVFIFOCTL_REG=0x%08X\n", regval); |
| 83 | |
| 84 | regval = iproc_i2c_reg_read(&base->bitbng_ctrl); |
| 85 | debug("CCB_SMB_BITBANGCTL_REG=0x%08X\n", regval); |
| 86 | |
| 87 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 88 | debug("CCB_SMB_MSTRCMD_REG=0x%08X\n", regval); |
| 89 | |
| 90 | regval = iproc_i2c_reg_read(&base->slv_cmd); |
| 91 | debug("CCB_SMB_SLVCMD_REG=0x%08X\n", regval); |
| 92 | |
| 93 | regval = iproc_i2c_reg_read(&base->evt_en); |
| 94 | debug("CCB_SMB_EVTEN_REG=0x%08X\n", regval); |
| 95 | |
| 96 | regval = iproc_i2c_reg_read(&base->evt_sts); |
| 97 | debug("CCB_SMB_EVTSTS_REG=0x%08X\n", regval); |
| 98 | |
| 99 | regval = iproc_i2c_reg_read(&base->mstr_datawr); |
| 100 | debug("CCB_SMB_MSTRDATAWR_REG=0x%08X\n", regval); |
| 101 | |
| 102 | regval = iproc_i2c_reg_read(&base->mstr_datard); |
| 103 | debug("CCB_SMB_MSTRDATARD_REG=0x%08X\n", regval); |
| 104 | |
| 105 | regval = iproc_i2c_reg_read(&base->slv_datawr); |
| 106 | debug("CCB_SMB_SLVDATAWR_REG=0x%08X\n", regval); |
| 107 | |
| 108 | regval = iproc_i2c_reg_read(&base->slv_datard); |
| 109 | debug("CCB_SMB_SLVDATARD_REG=0x%08X\n", regval); |
| 110 | |
| 111 | debug("----------------------------------------------\n\n"); |
| 112 | return 0; |
| 113 | } |
| 114 | #else |
| 115 | static int iproc_dump_i2c_regs(struct iproc_i2c *bus_prvdata) |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | /* |
| 122 | * Function to ensure that the previous transaction was completed before |
| 123 | * initiating a new transaction. It can also be used in polling mode to |
| 124 | * check status of completion of a command |
| 125 | */ |
| 126 | static int iproc_i2c_startbusy_wait(struct iproc_i2c *bus_prvdata) |
| 127 | { |
| 128 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 129 | unsigned int regval; |
| 130 | |
| 131 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 132 | |
| 133 | /* Check if an operation is in progress. During probe it won't be. |
| 134 | * But when shutdown/remove was called we want to make sure that |
| 135 | * the transaction in progress completed |
| 136 | */ |
| 137 | if (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) { |
| 138 | unsigned int i = 0; |
| 139 | |
| 140 | do { |
| 141 | mdelay(10); |
| 142 | i++; |
| 143 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 144 | |
| 145 | /* If start-busy bit cleared, exit the loop */ |
| 146 | } while ((regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) && |
| 147 | (i < IPROC_SMB_MAX_RETRIES)); |
| 148 | |
| 149 | if (i >= IPROC_SMB_MAX_RETRIES) { |
| 150 | pr_err("%s: START_BUSY bit didn't clear, exiting\n", |
| 151 | __func__); |
| 152 | return -ETIMEDOUT; |
| 153 | } |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * This function set clock frequency for SMBus block. As per hardware |
| 160 | * engineering, the clock frequency can be changed dynamically. |
| 161 | */ |
| 162 | static int iproc_i2c_set_clk_freq(struct iproc_i2c *bus_prvdata) |
| 163 | { |
| 164 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 165 | unsigned int regval; |
| 166 | |
| 167 | regval = iproc_i2c_reg_read(&base->timg_cfg); |
| 168 | |
| 169 | switch (bus_prvdata->bus_speed) { |
| 170 | case I2C_SPEED_STANDARD_RATE: |
| 171 | regval &= ~CCB_SMB_TIMGCFG_MODE400_MASK; |
| 172 | break; |
| 173 | |
| 174 | case I2C_SPEED_FAST_RATE: |
| 175 | regval |= CCB_SMB_TIMGCFG_MODE400_MASK; |
| 176 | break; |
| 177 | |
| 178 | default: |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | iproc_i2c_reg_write(&base->timg_cfg, regval); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int iproc_i2c_init(struct udevice *bus) |
| 187 | { |
| 188 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 189 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 190 | unsigned int regval; |
| 191 | |
| 192 | debug("\nEntering %s\n", __func__); |
| 193 | |
| 194 | /* Put controller in reset */ |
| 195 | regval = iproc_i2c_reg_read(&base->cfg_reg); |
| 196 | regval |= CCB_SMB_CFG_RST_MASK; |
| 197 | regval &= ~CCB_SMB_CFG_SMBEN_MASK; |
| 198 | iproc_i2c_reg_write(&base->cfg_reg, regval); |
| 199 | |
| 200 | /* Wait 100 usec as per spec */ |
| 201 | udelay(100); |
| 202 | |
| 203 | /* bring controller out of reset */ |
| 204 | regval &= ~CCB_SMB_CFG_RST_MASK; |
| 205 | iproc_i2c_reg_write(&base->cfg_reg, regval); |
| 206 | |
| 207 | /* Flush Tx, Rx FIFOs. Note we are setting the Rx FIFO threshold to 0. |
| 208 | * May be OK since we are setting RX_EVENT and RX_FIFO_FULL interrupts |
| 209 | */ |
| 210 | regval = CCB_SMB_MSTRRXFIFOFLSH_MASK | CCB_SMB_MSTRTXFIFOFLSH_MASK; |
| 211 | iproc_i2c_reg_write(&base->mstr_fifo_ctrl, regval); |
| 212 | |
| 213 | /* Enable SMbus block. Note, we are setting MASTER_RETRY_COUNT to zero |
| 214 | * since there will be only one master |
| 215 | */ |
| 216 | regval = iproc_i2c_reg_read(&base->cfg_reg); |
| 217 | regval |= CCB_SMB_CFG_SMBEN_MASK; |
| 218 | iproc_i2c_reg_write(&base->cfg_reg, regval); |
| 219 | |
| 220 | /* Set default clock frequency */ |
| 221 | iproc_i2c_set_clk_freq(bus_prvdata); |
| 222 | |
| 223 | /* Disable intrs */ |
| 224 | iproc_i2c_reg_write(&base->evt_en, 0); |
| 225 | |
| 226 | /* Clear intrs (W1TC) */ |
| 227 | regval = iproc_i2c_reg_read(&base->evt_sts); |
| 228 | iproc_i2c_reg_write(&base->evt_sts, regval); |
| 229 | |
| 230 | bus_prvdata->i2c_init_done = 1; |
| 231 | |
| 232 | iproc_dump_i2c_regs(bus_prvdata); |
| 233 | debug("%s: Init successful\n", __func__); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * This function copies data to SMBus's Tx FIFO. Valid for write transactions |
| 240 | * only |
| 241 | * |
| 242 | * base_addr: Mapped address of this SMBus instance |
| 243 | * dev_addr: SMBus (I2C) device address. We are assuming 7-bit addresses |
| 244 | * initially |
| 245 | * info: Data to copy in to Tx FIFO. For read commands, the size should be |
| 246 | * set to zero by the caller |
| 247 | * |
| 248 | */ |
| 249 | static void iproc_i2c_write_trans_data(struct iproc_i2c *bus_prvdata, |
| 250 | unsigned short dev_addr, |
| 251 | struct iproc_xact_info *info) |
| 252 | { |
| 253 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 254 | unsigned int regval; |
| 255 | unsigned int i; |
| 256 | unsigned int num_data_bytes = 0; |
| 257 | |
| 258 | debug("%s: dev_addr=0x%X cmd_valid=%d cmd=0x%02x size=%u proto=%d buf[] %x\n", |
| 259 | __func__, dev_addr, info->cmd_valid, |
| 260 | info->command, info->size, info->smb_proto, info->data[0]); |
| 261 | |
| 262 | /* Write SMBus device address first */ |
| 263 | /* Note, we are assuming 7-bit addresses for now. For 10-bit addresses, |
| 264 | * we may have one more write to send the upper 3 bits of 10-bit addr |
| 265 | */ |
| 266 | iproc_i2c_reg_write(&base->mstr_datawr, dev_addr); |
| 267 | |
| 268 | /* If the protocol needs command code, copy it */ |
| 269 | if (info->cmd_valid) |
| 270 | iproc_i2c_reg_write(&base->mstr_datawr, info->command); |
| 271 | |
| 272 | /* Depending on the SMBus protocol, we need to write additional |
| 273 | * transaction data in to Tx FIFO. Refer to section 5.5 of SMBus |
| 274 | * spec for sequence for a transaction |
| 275 | */ |
| 276 | switch (info->smb_proto) { |
| 277 | case SMBUS_PROT_RECV_BYTE: |
| 278 | /* No additional data to be written */ |
| 279 | num_data_bytes = 0; |
| 280 | break; |
| 281 | |
| 282 | case SMBUS_PROT_SEND_BYTE: |
| 283 | num_data_bytes = info->size; |
| 284 | break; |
| 285 | |
| 286 | case SMBUS_PROT_RD_BYTE: |
| 287 | case SMBUS_PROT_RD_WORD: |
| 288 | case SMBUS_PROT_BLK_RD: |
| 289 | /* Write slave address with R/W~ set (bit #0) */ |
| 290 | iproc_i2c_reg_write(&base->mstr_datawr, |
| 291 | dev_addr | 0x1); |
| 292 | num_data_bytes = 0; |
| 293 | break; |
| 294 | |
| 295 | case SMBUS_PROT_BLK_WR_BLK_RD_PROC_CALL: |
| 296 | iproc_i2c_reg_write(&base->mstr_datawr, |
| 297 | dev_addr | 0x1 | |
| 298 | CCB_SMB_MSTRWRSTS_MASK); |
| 299 | num_data_bytes = 0; |
| 300 | break; |
| 301 | |
| 302 | case SMBUS_PROT_WR_BYTE: |
| 303 | case SMBUS_PROT_WR_WORD: |
| 304 | /* No additional bytes to be written. |
| 305 | * Data portion is written in the |
| 306 | * 'for' loop below |
| 307 | */ |
| 308 | num_data_bytes = info->size; |
| 309 | break; |
| 310 | |
| 311 | case SMBUS_PROT_BLK_WR: |
| 312 | /* 3rd byte is byte count */ |
| 313 | iproc_i2c_reg_write(&base->mstr_datawr, info->size); |
| 314 | num_data_bytes = info->size; |
| 315 | break; |
| 316 | |
| 317 | default: |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | /* Copy actual data from caller, next. In general, for reads, |
| 322 | * no data is copied |
| 323 | */ |
| 324 | for (i = 0; num_data_bytes; --num_data_bytes, i++) { |
| 325 | /* For the last byte, set MASTER_WR_STATUS bit */ |
| 326 | regval = (num_data_bytes == 1) ? |
| 327 | info->data[i] | CCB_SMB_MSTRWRSTS_MASK : |
| 328 | info->data[i]; |
| 329 | |
| 330 | iproc_i2c_reg_write(&base->mstr_datawr, regval); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static int iproc_i2c_data_send(struct iproc_i2c *bus_prvdata, |
| 335 | unsigned short addr, |
| 336 | struct iproc_xact_info *info) |
| 337 | { |
| 338 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 339 | int rc, retry = 3; |
| 340 | unsigned int regval; |
| 341 | |
| 342 | /* Make sure the previous transaction completed */ |
| 343 | rc = iproc_i2c_startbusy_wait(bus_prvdata); |
| 344 | |
| 345 | if (rc < 0) { |
| 346 | pr_err("%s: Send: bus is busy, exiting\n", __func__); |
| 347 | return rc; |
| 348 | } |
| 349 | |
| 350 | /* Write transaction bytes to Tx FIFO */ |
| 351 | iproc_i2c_write_trans_data(bus_prvdata, addr, info); |
| 352 | |
| 353 | /* Program master command register (0x30) with protocol type and set |
| 354 | * start_busy_command bit to initiate the write transaction |
| 355 | */ |
| 356 | regval = (info->smb_proto << CCB_SMB_MSTRSMBUSPROTO_SHIFT) | |
| 357 | CCB_SMB_MSTRSTARTBUSYCMD_MASK; |
| 358 | |
| 359 | iproc_i2c_reg_write(&base->mstr_cmd, regval); |
| 360 | |
| 361 | /* Check for Master status */ |
| 362 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 363 | while (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) { |
| 364 | mdelay(10); |
| 365 | if (retry-- <= 0) |
| 366 | break; |
| 367 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 368 | } |
| 369 | |
| 370 | /* If start_busy bit cleared, check if there are any errors */ |
| 371 | if (!(regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK)) { |
| 372 | /* start_busy bit cleared, check master_status field now */ |
| 373 | regval &= CCB_SMB_MSTRSTS_MASK; |
| 374 | regval >>= CCB_SMB_MSTRSTS_SHIFT; |
| 375 | |
| 376 | if (regval != MSTR_STS_XACT_SUCCESS) { |
| 377 | /* Error We can flush Tx FIFO here */ |
| 378 | pr_err("%s: ERROR: Error in transaction %u, exiting\n", |
| 379 | __func__, regval); |
| 380 | return -EREMOTEIO; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int iproc_i2c_data_recv(struct iproc_i2c *bus_prvdata, |
| 388 | unsigned short addr, |
| 389 | struct iproc_xact_info *info, |
| 390 | unsigned int *num_bytes_read) |
| 391 | { |
| 392 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 393 | int rc, retry = 3; |
| 394 | unsigned int regval; |
| 395 | |
| 396 | /* Make sure the previous transaction completed */ |
| 397 | rc = iproc_i2c_startbusy_wait(bus_prvdata); |
| 398 | |
| 399 | if (rc < 0) { |
| 400 | pr_err("%s: Receive: Bus is busy, exiting\n", __func__); |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | /* Program all transaction bytes into master Tx FIFO */ |
| 405 | iproc_i2c_write_trans_data(bus_prvdata, addr, info); |
| 406 | |
| 407 | /* Program master command register (0x30) with protocol type and set |
| 408 | * start_busy_command bit to initiate the write transaction |
| 409 | */ |
| 410 | regval = (info->smb_proto << CCB_SMB_MSTRSMBUSPROTO_SHIFT) | |
| 411 | CCB_SMB_MSTRSTARTBUSYCMD_MASK | info->size; |
| 412 | |
| 413 | iproc_i2c_reg_write(&base->mstr_cmd, regval); |
| 414 | |
| 415 | /* Check for Master status */ |
| 416 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 417 | while (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) { |
| 418 | udelay(1000); |
| 419 | if (retry-- <= 0) |
| 420 | break; |
| 421 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 422 | } |
| 423 | |
| 424 | /* If start_busy bit cleared, check if there are any errors */ |
| 425 | if (!(regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK)) { |
| 426 | /* start_busy bit cleared, check master_status field now */ |
| 427 | regval &= CCB_SMB_MSTRSTS_MASK; |
| 428 | regval >>= CCB_SMB_MSTRSTS_SHIFT; |
| 429 | |
| 430 | if (regval != MSTR_STS_XACT_SUCCESS) { |
| 431 | /* We can flush Tx FIFO here */ |
| 432 | pr_err("%s: Error in transaction %d, exiting\n", |
| 433 | __func__, regval); |
| 434 | return -EREMOTEIO; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* Read received byte(s), after TX out address etc */ |
| 439 | regval = iproc_i2c_reg_read(&base->mstr_datard); |
| 440 | |
| 441 | /* For block read, protocol (hw) returns byte count, |
| 442 | * as the first byte |
| 443 | */ |
| 444 | if (info->smb_proto == SMBUS_PROT_BLK_RD) { |
| 445 | int i; |
| 446 | |
| 447 | *num_bytes_read = regval & CCB_SMB_MSTRRDDATA_MASK; |
| 448 | |
| 449 | /* Limit to reading a max of 32 bytes only; just a safeguard. |
| 450 | * If # bytes read is a number > 32, check transaction set up, |
| 451 | * and contact hw engg. Assumption: PEC is disabled |
| 452 | */ |
| 453 | for (i = 0; |
| 454 | (i < *num_bytes_read) && (i < I2C_SMBUS_BLOCK_MAX); |
| 455 | i++) { |
| 456 | /* Read Rx FIFO for data bytes */ |
| 457 | regval = iproc_i2c_reg_read(&base->mstr_datard); |
| 458 | info->data[i] = regval & CCB_SMB_MSTRRDDATA_MASK; |
| 459 | } |
| 460 | } else { |
| 461 | /* 1 Byte data */ |
| 462 | *info->data = regval & CCB_SMB_MSTRRDDATA_MASK; |
| 463 | *num_bytes_read = 1; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int i2c_write_byte(struct iproc_i2c *bus_prvdata, |
| 470 | u8 devaddr, u8 regoffset, u8 value) |
| 471 | { |
| 472 | int rc; |
| 473 | struct iproc_xact_info info; |
| 474 | |
| 475 | devaddr <<= 1; |
| 476 | |
| 477 | info.cmd_valid = 1; |
| 478 | info.command = (unsigned char)regoffset; |
| 479 | info.data = &value; |
| 480 | info.size = 1; |
| 481 | info.flags = 0; |
| 482 | info.smb_proto = SMBUS_PROT_WR_BYTE; |
| 483 | /* Refer to i2c_smbus_write_byte params passed. */ |
| 484 | rc = iproc_i2c_data_send(bus_prvdata, devaddr, &info); |
| 485 | |
| 486 | if (rc < 0) { |
| 487 | pr_err("%s: %s error accessing device 0x%X\n", |
| 488 | __func__, "Write", devaddr); |
| 489 | return -EREMOTEIO; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | int i2c_write(struct udevice *bus, |
| 496 | uchar chip, uint regaddr, int alen, uchar *buffer, int len) |
| 497 | { |
| 498 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 499 | int i, data_len; |
| 500 | u8 *data; |
| 501 | |
| 502 | if (len > 256) { |
| 503 | pr_err("I2C write: address out of range\n"); |
| 504 | return 1; |
| 505 | } |
| 506 | |
| 507 | if (len < 1) { |
| 508 | pr_err("I2C write: Need offset addr and value\n"); |
| 509 | return 1; |
| 510 | } |
| 511 | |
| 512 | /* buffer contains offset addr followed by value to be written */ |
| 513 | regaddr = buffer[0]; |
| 514 | data = &buffer[1]; |
| 515 | data_len = len - 1; |
| 516 | |
| 517 | for (i = 0; i < data_len; i++) { |
| 518 | if (i2c_write_byte(bus_prvdata, chip, regaddr + i, data[i])) { |
| 519 | pr_err("I2C write (%d): I/O error\n", i); |
| 520 | iproc_i2c_init(bus); |
| 521 | return 1; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int i2c_read_byte(struct iproc_i2c *bus_prvdata, |
| 529 | u8 devaddr, u8 regoffset, u8 *value) |
| 530 | { |
| 531 | int rc; |
| 532 | struct iproc_xact_info info; |
| 533 | unsigned int num_bytes_read = 0; |
| 534 | |
| 535 | devaddr <<= 1; |
| 536 | |
| 537 | info.cmd_valid = 1; |
| 538 | info.command = (unsigned char)regoffset; |
| 539 | info.data = value; |
| 540 | info.size = 1; |
| 541 | info.flags = 0; |
| 542 | info.smb_proto = SMBUS_PROT_RD_BYTE; |
| 543 | /* Refer to i2c_smbus_read_byte for params passed. */ |
| 544 | rc = iproc_i2c_data_recv(bus_prvdata, devaddr, &info, &num_bytes_read); |
| 545 | |
| 546 | if (rc < 0) { |
| 547 | pr_err("%s: %s error accessing device 0x%X\n", |
| 548 | __func__, "Read", devaddr); |
| 549 | return -EREMOTEIO; |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | int i2c_read(struct udevice *bus, |
| 556 | uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 557 | { |
| 558 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 559 | int i; |
| 560 | |
| 561 | if (len > 256) { |
| 562 | pr_err("I2C read: address out of range\n"); |
| 563 | return 1; |
| 564 | } |
| 565 | |
| 566 | for (i = 0; i < len; i++) { |
| 567 | if (i2c_read_byte(bus_prvdata, chip, addr + i, &buffer[i])) { |
| 568 | pr_err("I2C read: I/O error\n"); |
| 569 | iproc_i2c_init(bus); |
| 570 | return 1; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int iproc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 578 | { |
| 579 | int ret = 0; |
| 580 | |
| 581 | debug("%s: %d messages\n", __func__, nmsgs); |
| 582 | |
| 583 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 584 | if (msg->flags & I2C_M_RD) |
| 585 | ret = i2c_read(bus, msg->addr, 0, 0, |
| 586 | msg->buf, msg->len); |
| 587 | else |
| 588 | ret = i2c_write(bus, msg->addr, 0, 0, |
| 589 | msg->buf, msg->len); |
| 590 | } |
| 591 | |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | static int iproc_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 596 | uint chip_flags) |
| 597 | { |
| 598 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 599 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 600 | u32 regval; |
| 601 | |
| 602 | debug("\n%s: Entering chip probe\n", __func__); |
| 603 | |
| 604 | /* Init internal regs, disable intrs (and then clear intrs), set fifo |
| 605 | * thresholds, etc. |
| 606 | */ |
| 607 | if (!bus_prvdata->i2c_init_done) |
| 608 | iproc_i2c_init(bus); |
| 609 | |
| 610 | regval = (chip_addr << 1); |
| 611 | iproc_i2c_reg_write(&base->mstr_datawr, regval); |
| 612 | regval = ((SMBUS_PROT_QUICK_CMD << CCB_SMB_MSTRSMBUSPROTO_SHIFT) | |
| 613 | (1 << CCB_SMB_MSTRSTARTBUSYCMD_SHIFT)); |
| 614 | iproc_i2c_reg_write(&base->mstr_cmd, regval); |
| 615 | |
| 616 | do { |
| 617 | udelay(100); |
| 618 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 619 | regval &= CCB_SMB_MSTRSTARTBUSYCMD_MASK; |
| 620 | } while (regval); |
| 621 | |
| 622 | regval = iproc_i2c_reg_read(&base->mstr_cmd); |
| 623 | |
| 624 | if ((regval & CCB_SMB_MSTRSTS_MASK) != 0) |
| 625 | return -1; |
| 626 | |
| 627 | iproc_dump_i2c_regs(bus_prvdata); |
| 628 | debug("%s: chip probe successful\n", __func__); |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static int iproc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 634 | { |
| 635 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 636 | |
| 637 | bus_prvdata->bus_speed = speed; |
| 638 | return iproc_i2c_set_clk_freq(bus_prvdata); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * i2c_get_bus_speed - get i2c bus speed |
| 643 | * |
| 644 | * This function returns the speed of operation in Hz |
| 645 | */ |
| 646 | int iproc_i2c_get_bus_speed(struct udevice *bus) |
| 647 | { |
| 648 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 649 | struct iproc_i2c_regs *base = bus_prvdata->base; |
| 650 | unsigned int regval; |
| 651 | int ret = 0; |
| 652 | |
| 653 | regval = iproc_i2c_reg_read(&base->timg_cfg); |
| 654 | regval = (regval & CCB_SMB_TIMGCFG_MODE400_MASK) >> |
| 655 | CCB_SMB_TIMGCFG_MODE400_SHIFT; |
| 656 | |
| 657 | switch (regval) { |
| 658 | case 0: |
| 659 | ret = I2C_SPEED_STANDARD_RATE; |
| 660 | break; |
| 661 | case 1: |
| 662 | ret = I2C_SPEED_FAST_RATE; |
| 663 | break; |
| 664 | default: |
| 665 | ret = -EINVAL; |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | static int iproc_i2c_probe(struct udevice *bus) |
| 673 | { |
| 674 | return iproc_i2c_init(bus); |
| 675 | } |
| 676 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 677 | static int iproc_i2c_of_to_plat(struct udevice *bus) |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 678 | { |
| 679 | struct iproc_i2c *bus_prvdata = dev_get_priv(bus); |
| 680 | int node = dev_of_offset(bus); |
| 681 | const void *blob = gd->fdt_blob; |
| 682 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 683 | bus_prvdata->base = map_physmem(dev_read_addr(bus), |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 684 | sizeof(void *), |
| 685 | MAP_NOCACHE); |
| 686 | |
| 687 | bus_prvdata->bus_speed = |
| 688 | fdtdec_get_int(blob, node, "bus-frequency", |
| 689 | I2C_SPEED_STANDARD_RATE); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static const struct dm_i2c_ops iproc_i2c_ops = { |
| 695 | .xfer = iproc_i2c_xfer, |
| 696 | .probe_chip = iproc_i2c_probe_chip, |
| 697 | .set_bus_speed = iproc_i2c_set_bus_speed, |
| 698 | .get_bus_speed = iproc_i2c_get_bus_speed, |
| 699 | }; |
| 700 | |
| 701 | static const struct udevice_id iproc_i2c_ids[] = { |
| 702 | { .compatible = "brcm,iproc-i2c" }, |
| 703 | { } |
| 704 | }; |
| 705 | |
| 706 | U_BOOT_DRIVER(iproc_i2c) = { |
| 707 | .name = "iproc_i2c", |
| 708 | .id = UCLASS_I2C, |
| 709 | .of_match = iproc_i2c_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 710 | .of_to_plat = iproc_i2c_of_to_plat, |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 711 | .probe = iproc_i2c_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 712 | .priv_auto = sizeof(struct iproc_i2c), |
Rayagonda Kokatanur | 956d57a | 2020-04-08 11:12:27 +0530 | [diff] [blame] | 713 | .ops = &iproc_i2c_ops, |
| 714 | .flags = DM_FLAG_PRE_RELOC, |
| 715 | }; |