Jorge Ramirez-Ortiz | e1868f9 | 2018-09-23 09:38:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame^] | 7 | #include <common/debug.h> |
| 8 | #include <lib/mmio.h> |
| 9 | #include <lib/utils_def.h> |
| 10 | |
Jorge Ramirez-Ortiz | e1868f9 | 2018-09-23 09:38:56 +0200 | [diff] [blame] | 11 | #include "cpg_registers.h" |
| 12 | #include "avs_driver.h" |
| 13 | #include "rcar_def.h" |
| 14 | #include "rcar_private.h" |
| 15 | |
| 16 | #if (AVS_SETTING_ENABLE == 1) |
| 17 | #if PMIC_ROHM_BD9571 |
| 18 | /* Read PMIC register for debug. 1:enable / 0:disable */ |
| 19 | #define AVS_READ_PMIC_REG_ENABLE 0 |
| 20 | /* The re-try number of times of the AVS setting. */ |
| 21 | #define AVS_RETRY_NUM (1U) |
| 22 | #endif /* PMIC_ROHM_BD9571 */ |
| 23 | |
| 24 | /* Base address of Adaptive Voltage Scaling module registers*/ |
| 25 | #define AVS_BASE (0xE60A0000U) |
| 26 | /* Adaptive Dynamic Voltage ADJust Parameter2 registers */ |
| 27 | #define ADVADJP2 (AVS_BASE + 0x013CU) |
| 28 | |
| 29 | /* Mask VOLCOND bit in ADVADJP2 registers */ |
| 30 | #define ADVADJP2_VOLCOND_MASK (0x000001FFU) /* VOLCOND[8:0] */ |
| 31 | |
| 32 | #if PMIC_ROHM_BD9571 |
| 33 | /* I2C for DVFS bit in CPG registers for module standby and software reset*/ |
| 34 | #define CPG_SYS_DVFS_BIT (0x04000000U) |
| 35 | #endif /* PMIC_ROHM_BD9571 */ |
| 36 | /* ADVFS Module bit in CPG registers for module standby and software reset*/ |
| 37 | #define CPG_SYS_ADVFS_BIT (0x02000000U) |
| 38 | |
| 39 | #if PMIC_ROHM_BD9571 |
| 40 | /* Base address of IICDVFS registers*/ |
| 41 | #define IIC_DVFS_BASE (0xE60B0000U) |
| 42 | /* IIC bus data register */ |
| 43 | #define IIC_ICDR (IIC_DVFS_BASE + 0x0000U) |
| 44 | /* IIC bus control register */ |
| 45 | #define IIC_ICCR (IIC_DVFS_BASE + 0x0004U) |
| 46 | /* IIC bus status register */ |
| 47 | #define IIC_ICSR (IIC_DVFS_BASE + 0x0008U) |
| 48 | /* IIC interrupt control register */ |
| 49 | #define IIC_ICIC (IIC_DVFS_BASE + 0x000CU) |
| 50 | /* IIC clock control register low */ |
| 51 | #define IIC_ICCL (IIC_DVFS_BASE + 0x0010U) |
| 52 | /* IIC clock control register high */ |
| 53 | #define IIC_ICCH (IIC_DVFS_BASE + 0x0014U) |
| 54 | |
| 55 | /* Bit in ICSR register */ |
| 56 | #define ICSR_BUSY (0x10U) |
| 57 | #define ICSR_AL (0x08U) |
| 58 | #define ICSR_TACK (0x04U) |
| 59 | #define ICSR_WAIT (0x02U) |
| 60 | #define ICSR_DTE (0x01U) |
| 61 | |
| 62 | /* Bit in ICIC register */ |
| 63 | #define ICIC_TACKE (0x04U) |
| 64 | #define ICIC_WAITE (0x02U) |
| 65 | #define ICIC_DTEE (0x01U) |
| 66 | |
| 67 | /* I2C bus interface enable */ |
| 68 | #define ICCR_ENABLE (0x80U) |
| 69 | /* Start condition */ |
| 70 | #define ICCR_START (0x94U) |
| 71 | /* Stop condition */ |
| 72 | #define ICCR_STOP (0x90U) |
| 73 | /* Restart condition with change to receive mode change */ |
| 74 | #define ICCR_START_RECV (0x81U) |
| 75 | /* Stop condition for receive mode */ |
| 76 | #define ICCR_STOP_RECV (0xC0U) |
| 77 | |
| 78 | /* Low-level period of SCL */ |
| 79 | #define ICCL_FREQ_8p33M (0x07U) /* for CP Phy 8.3333MHz */ |
| 80 | #define ICCL_FREQ_10M (0x09U) /* for CP Phy 10MHz */ |
| 81 | #define ICCL_FREQ_12p5M (0x0BU) /* for CP Phy 12.5MHz */ |
| 82 | #define ICCL_FREQ_16p66M (0x0EU) /* for CP Phy 16.6666MHz */ |
| 83 | /* High-level period of SCL */ |
| 84 | #define ICCH_FREQ_8p33M (0x01U) /* for CP Phy 8.3333MHz */ |
| 85 | #define ICCH_FREQ_10M (0x02U) /* for CP Phy 10MHz */ |
| 86 | #define ICCH_FREQ_12p5M (0x03U) /* for CP Phy 12.5MHz */ |
| 87 | #define ICCH_FREQ_16p66M (0x05U) /* for CP Phy 16.6666MHz */ |
| 88 | |
| 89 | /* PMIC */ |
| 90 | #define PMIC_W_SLAVE_ADDRESS (0x60U) /* ROHM BD9571 slave address + (W) */ |
| 91 | #define PMIC_R_SLAVE_ADDRESS (0x61U) /* ROHM BD9571 slave address + (R) */ |
| 92 | #define PMIC_DVFS_SETVID (0x54U) /* ROHM BD9571 DVFS SetVID register */ |
| 93 | #endif /* PMIC_ROHM_BD9571 */ |
| 94 | |
| 95 | /* Individual information */ |
| 96 | #define EFUSE_AVS0 (0U) |
| 97 | #define EFUSE_AVS_NUM ARRAY_SIZE(init_vol_tbl) |
| 98 | |
| 99 | typedef struct { |
| 100 | uint32_t avs; /* AVS code */ |
| 101 | uint8_t vol; /* Voltage */ |
| 102 | } initial_voltage_t; |
| 103 | |
| 104 | static const initial_voltage_t init_vol_tbl[] = { |
| 105 | /* AVS code, RHOM BD9571 DVFS SetVID register */ |
| 106 | {0x00U, 0x53U}, /* AVS0, 0.83V */ |
| 107 | {0x01U, 0x52U}, /* AVS1, 0.82V */ |
| 108 | {0x02U, 0x51U}, /* AVS2, 0.81V */ |
| 109 | {0x04U, 0x50U}, /* AVS3, 0.80V */ |
| 110 | {0x08U, 0x4FU}, /* AVS4, 0.79V */ |
| 111 | {0x10U, 0x4EU}, /* AVS5, 0.78V */ |
| 112 | {0x20U, 0x4DU}, /* AVS6, 0.77V */ |
| 113 | {0x40U, 0x4CU} /* AVS7, 0.76V */ |
| 114 | }; |
| 115 | |
| 116 | #if PMIC_ROHM_BD9571 |
| 117 | /* Kind of AVS settings status */ |
| 118 | typedef enum { |
| 119 | avs_status_none = 0, |
| 120 | avs_status_init, |
| 121 | avs_status_start_condition, |
| 122 | avs_status_set_slave_addr, |
| 123 | avs_status_write_reg_addr, |
| 124 | avs_status_write_reg_data, |
| 125 | avs_status_stop_condition, |
| 126 | avs_status_end, |
| 127 | avs_status_complete, |
| 128 | avs_status_al_start, |
| 129 | avs_status_al_transfer, |
| 130 | avs_status_nack, |
| 131 | avs_status_error_stop, |
| 132 | ave_status_error_end |
| 133 | } avs_status_t; |
| 134 | |
| 135 | /* Kind of AVS error */ |
| 136 | typedef enum { |
| 137 | avs_error_none = 0, |
| 138 | avs_error_al, |
| 139 | avs_error_nack |
| 140 | } avs_error_t; |
| 141 | |
| 142 | static avs_status_t avs_status; |
| 143 | static uint32_t avs_retry; |
| 144 | #endif /* PMIC_ROHM_BD9571 */ |
| 145 | static uint32_t efuse_avs = EFUSE_AVS0; |
| 146 | |
| 147 | #if PMIC_ROHM_BD9571 |
| 148 | /* prototype */ |
| 149 | static avs_error_t avs_check_error(void); |
| 150 | static void avs_set_iic_clock(void); |
| 151 | #if AVS_READ_PMIC_REG_ENABLE == 1 |
| 152 | static uint8_t avs_read_pmic_reg(uint8_t addr); |
| 153 | static void avs_poll(uint8_t bit_pos, uint8_t val); |
| 154 | #endif |
| 155 | #endif /* PMIC_ROHM_BD9571 */ |
| 156 | #endif /* (AVS_SETTING_ENABLE==1) */ |
| 157 | |
| 158 | /* |
| 159 | * Initialize to enable the AVS setting. |
| 160 | */ |
| 161 | void rcar_avs_init(void) |
| 162 | { |
| 163 | #if (AVS_SETTING_ENABLE == 1) |
| 164 | uint32_t val; |
| 165 | |
| 166 | #if PMIC_ROHM_BD9571 |
| 167 | /* Initialize AVS status */ |
| 168 | avs_status = avs_status_init; |
| 169 | #endif /* PMIC_ROHM_BD9571 */ |
| 170 | |
| 171 | /* Enable clock supply to ADVFS. */ |
| 172 | mstpcr_write(CPG_SMSTPCR9, CPG_MSTPSR9, CPG_SYS_ADVFS_BIT); |
| 173 | |
| 174 | /* Read AVS code (Initial values are derived from eFuse) */ |
| 175 | val = mmio_read_32(ADVADJP2) & ADVADJP2_VOLCOND_MASK; |
| 176 | |
| 177 | for (efuse_avs = 0U; efuse_avs < EFUSE_AVS_NUM; efuse_avs++) { |
| 178 | if (val == init_vol_tbl[efuse_avs].avs) |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | if (efuse_avs >= EFUSE_AVS_NUM) |
| 183 | efuse_avs = EFUSE_AVS0; /* Not applicable */ |
| 184 | #if PMIC_ROHM_BD9571 |
| 185 | /* Enable clock supply to DVFS. */ |
| 186 | mstpcr_write(CPG_SMSTPCR9, CPG_MSTPSR9, CPG_SYS_DVFS_BIT); |
| 187 | |
| 188 | /* Disable I2C module and All internal registers initialized. */ |
| 189 | mmio_write_8(IIC_ICCR, 0x00U); |
| 190 | while ((mmio_read_8(IIC_ICCR) & ICCR_ENABLE) != 0U) { |
| 191 | /* Disable I2C module and All internal registers initialized. */ |
| 192 | mmio_write_8(IIC_ICCR, 0x00U); |
| 193 | } |
| 194 | |
| 195 | /* Set next status */ |
| 196 | avs_status = avs_status_start_condition; |
| 197 | |
| 198 | #endif /* PMIC_ROHM_BD9571 */ |
| 199 | #endif /* (AVS_SETTING_ENABLE==1) */ |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Set the value of register corresponding to the voltage |
| 204 | * by transfer of I2C to PIMC. |
| 205 | */ |
| 206 | void rcar_avs_setting(void) |
| 207 | { |
| 208 | #if (AVS_SETTING_ENABLE == 1) |
| 209 | #if PMIC_ROHM_BD9571 |
| 210 | avs_error_t err; |
| 211 | |
| 212 | switch (avs_status) { |
| 213 | case avs_status_start_condition: |
| 214 | /* Set ICCR.ICE=1 to activate the I2C module. */ |
| 215 | mmio_write_8(IIC_ICCR, mmio_read_8(IIC_ICCR) | ICCR_ENABLE); |
| 216 | /* Set frequency of 400kHz */ |
| 217 | avs_set_iic_clock(); |
| 218 | /* Set ICIC.TACKE=1, ICIC.WAITE=1, ICIC.DTEE=1 to */ |
| 219 | /* enable interrupt control. */ |
| 220 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) |
| 221 | | ICIC_TACKE | ICIC_WAITE | ICIC_DTEE); |
| 222 | /* Write H'94 in ICCR to issue start condition */ |
| 223 | mmio_write_8(IIC_ICCR, ICCR_START); |
| 224 | /* Set next status */ |
| 225 | avs_status = avs_status_set_slave_addr; |
| 226 | break; |
| 227 | case avs_status_set_slave_addr: |
| 228 | /* Check error. */ |
| 229 | err = avs_check_error(); |
| 230 | if (err == avs_error_al) { |
| 231 | /* Recovery sequence of just after start. */ |
| 232 | avs_status = avs_status_al_start; |
| 233 | } else if (err == avs_error_nack) { |
| 234 | /* Recovery sequence of detected NACK */ |
| 235 | avs_status = avs_status_nack; |
| 236 | } else { |
| 237 | /* Was data transmission enabled ? */ |
| 238 | if ((mmio_read_8(IIC_ICSR) & ICSR_DTE) == ICSR_DTE) { |
| 239 | /* Clear ICIC.DTEE to disable a DTE interrupt */ |
| 240 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) |
| 241 | & (uint8_t) (~ICIC_DTEE)); |
| 242 | /* Send PMIC slave address + (W) */ |
| 243 | mmio_write_8(IIC_ICDR, PMIC_W_SLAVE_ADDRESS); |
| 244 | /* Set next status */ |
| 245 | avs_status = avs_status_write_reg_addr; |
| 246 | } |
| 247 | } |
| 248 | break; |
| 249 | case avs_status_write_reg_addr: |
| 250 | /* Check error. */ |
| 251 | err = avs_check_error(); |
| 252 | if (err == avs_error_al) { |
| 253 | /* Recovery sequence of during data transfer. */ |
| 254 | avs_status = avs_status_al_transfer; |
| 255 | } else if (err == avs_error_nack) { |
| 256 | /* Recovery sequence of detected NACK */ |
| 257 | avs_status = avs_status_nack; |
| 258 | } else { |
| 259 | /* If wait state after data transmission. */ |
| 260 | if ((mmio_read_8(IIC_ICSR) & ICSR_WAIT) == ICSR_WAIT) { |
| 261 | /* Write PMIC DVFS_SetVID address */ |
| 262 | mmio_write_8(IIC_ICDR, PMIC_DVFS_SETVID); |
| 263 | /* Clear ICSR.WAIT to exit from wait state. */ |
| 264 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) |
| 265 | & (uint8_t) (~ICSR_WAIT)); |
| 266 | /* Set next status */ |
| 267 | avs_status = avs_status_write_reg_data; |
| 268 | } |
| 269 | } |
| 270 | break; |
| 271 | case avs_status_write_reg_data: |
| 272 | /* Check error. */ |
| 273 | err = avs_check_error(); |
| 274 | if (err == avs_error_al) { |
| 275 | /* Recovery sequence of during data transfer. */ |
| 276 | avs_status = avs_status_al_transfer; |
| 277 | } else if (err == avs_error_nack) { |
| 278 | /* Recovery sequence of detected NACK */ |
| 279 | avs_status = avs_status_nack; |
| 280 | } else { |
| 281 | /* If wait state after data transmission. */ |
| 282 | if ((mmio_read_8(IIC_ICSR) & ICSR_WAIT) == ICSR_WAIT) { |
| 283 | /* Dose efuse_avs exceed the number of */ |
| 284 | /* the tables? */ |
| 285 | if (efuse_avs >= EFUSE_AVS_NUM) { |
| 286 | ERROR("AVS number of eFuse is out " |
| 287 | "of a range. number=%u\n", |
| 288 | efuse_avs); |
| 289 | /* Infinite loop */ |
| 290 | panic(); |
| 291 | } |
| 292 | /* Write PMIC DVFS_SetVID value */ |
| 293 | mmio_write_8(IIC_ICDR, |
| 294 | init_vol_tbl[efuse_avs].vol); |
| 295 | /* Clear ICSR.WAIT to exit from wait state. */ |
| 296 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) |
| 297 | & (uint8_t) (~ICSR_WAIT)); |
| 298 | /* Set next status */ |
| 299 | avs_status = avs_status_stop_condition; |
| 300 | } |
| 301 | } |
| 302 | break; |
| 303 | case avs_status_stop_condition: |
| 304 | err = avs_check_error(); |
| 305 | if (err == avs_error_al) { |
| 306 | /* Recovery sequence of during data transfer. */ |
| 307 | avs_status = avs_status_al_transfer; |
| 308 | } else if (err == avs_error_nack) { |
| 309 | /* Recovery sequence of detected NACK */ |
| 310 | avs_status = avs_status_nack; |
| 311 | } else { |
| 312 | /* If wait state after data transmission. */ |
| 313 | if ((mmio_read_8(IIC_ICSR) & ICSR_WAIT) == ICSR_WAIT) { |
| 314 | /* Write H'90 in ICCR to issue stop condition */ |
| 315 | mmio_write_8(IIC_ICCR, ICCR_STOP); |
| 316 | /* Clear ICSR.WAIT to exit from wait state. */ |
| 317 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) |
| 318 | & (uint8_t) (~ICSR_WAIT)); |
| 319 | /* Set next status */ |
| 320 | avs_status = avs_status_end; |
| 321 | } |
| 322 | } |
| 323 | break; |
| 324 | case avs_status_end: |
| 325 | /* Is this module not busy?. */ |
| 326 | if ((mmio_read_8(IIC_ICSR) & ICSR_BUSY) == 0U) { |
| 327 | /* Set ICCR=H'00 to disable the I2C module. */ |
| 328 | mmio_write_8(IIC_ICCR, 0x00U); |
| 329 | /* Set next status */ |
| 330 | avs_status = avs_status_complete; |
| 331 | } |
| 332 | break; |
| 333 | case avs_status_al_start: |
| 334 | /* Clear ICSR.AL bit */ |
| 335 | mmio_write_8(IIC_ICSR, (mmio_read_8(IIC_ICSR) |
| 336 | & (uint8_t) (~ICSR_AL))); |
| 337 | /* Transmit a clock pulse */ |
| 338 | mmio_write_8(IIC_ICDR, init_vol_tbl[EFUSE_AVS0].vol); |
| 339 | /* Set next status */ |
| 340 | avs_status = avs_status_error_stop; |
| 341 | break; |
| 342 | case avs_status_al_transfer: |
| 343 | /* Clear ICSR.AL bit */ |
| 344 | mmio_write_8(IIC_ICSR, (mmio_read_8(IIC_ICSR) |
| 345 | & (uint8_t) (~ICSR_AL))); |
| 346 | /* Set next status */ |
| 347 | avs_status = avs_status_error_stop; |
| 348 | break; |
| 349 | case avs_status_nack: |
| 350 | /* Write H'90 in ICCR to issue stop condition */ |
| 351 | mmio_write_8(IIC_ICCR, ICCR_STOP); |
| 352 | /* Disable a WAIT and DTEE interrupt. */ |
| 353 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) |
| 354 | & (uint8_t) (~(ICIC_WAITE | ICIC_DTEE))); |
| 355 | /* Clear ICSR.TACK bit */ |
| 356 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) |
| 357 | & (uint8_t) (~ICSR_TACK)); |
| 358 | /* Set next status */ |
| 359 | avs_status = ave_status_error_end; |
| 360 | break; |
| 361 | case avs_status_error_stop: |
| 362 | /* If wait state after data transmission. */ |
| 363 | if ((mmio_read_8(IIC_ICSR) & ICSR_WAIT) == ICSR_WAIT) { |
| 364 | /* Write H'90 in ICCR to issue stop condition */ |
| 365 | mmio_write_8(IIC_ICCR, ICCR_STOP); |
| 366 | /* Clear ICSR.WAIT to exit from wait state. */ |
| 367 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) |
| 368 | & (uint8_t) (~ICSR_WAIT)); |
| 369 | /* Set next status */ |
| 370 | avs_status = ave_status_error_end; |
| 371 | } |
| 372 | break; |
| 373 | case ave_status_error_end: |
| 374 | /* Is this module not busy?. */ |
| 375 | if ((mmio_read_8(IIC_ICSR) & ICSR_BUSY) == 0U) { |
| 376 | /* Set ICCR=H'00 to disable the I2C module. */ |
| 377 | mmio_write_8(IIC_ICCR, 0x00U); |
| 378 | /* Increment the re-try number of times. */ |
| 379 | avs_retry++; |
| 380 | /* Set start a re-try to status. */ |
| 381 | avs_status = avs_status_start_condition; |
| 382 | } |
| 383 | break; |
| 384 | case avs_status_complete: |
| 385 | /* After "avs_status" became the "avs_status_complete", */ |
| 386 | /* "avs_setting()" function may be called. */ |
| 387 | break; |
| 388 | default: |
| 389 | /* This case is not possible. */ |
| 390 | ERROR("AVS setting is in invalid status. status=%u\n", |
| 391 | avs_status); |
| 392 | /* Infinite loop */ |
| 393 | panic(); |
| 394 | break; |
| 395 | } |
| 396 | #endif /* PMIC_ROHM_BD9571 */ |
| 397 | #endif /* (AVS_SETTING_ENABLE==1) */ |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Finish the AVS setting. |
| 402 | */ |
| 403 | void rcar_avs_end(void) |
| 404 | { |
| 405 | #if (AVS_SETTING_ENABLE == 1) |
| 406 | uint32_t mstp; |
| 407 | |
| 408 | #if PMIC_ROHM_BD9571 |
| 409 | /* While status is not completion, be repeated. */ |
| 410 | while (avs_status != avs_status_complete) |
| 411 | rcar_avs_setting(); |
| 412 | |
| 413 | NOTICE("AVS setting succeeded. DVFS_SetVID=0x%x\n", |
| 414 | init_vol_tbl[efuse_avs].vol); |
| 415 | |
| 416 | #if AVS_READ_PMIC_REG_ENABLE == 1 |
| 417 | { |
| 418 | uint8_t addr = PMIC_DVFS_SETVID; |
| 419 | uint8_t value = avs_read_pmic_reg(addr); |
| 420 | NOTICE("Read PMIC register. address=0x%x value=0x%x \n", |
| 421 | addr, value); |
| 422 | } |
| 423 | #endif |
| 424 | |
| 425 | /* Bit of the module which wants to disable clock supply. */ |
| 426 | mstp = CPG_SYS_DVFS_BIT; |
| 427 | /* Disables the supply of clock signal to a module. */ |
| 428 | cpg_write(CPG_SMSTPCR9, mmio_read_32(CPG_SMSTPCR9) | mstp); |
| 429 | #endif /* PMIC_ROHM_BD9571 */ |
| 430 | |
| 431 | /* Bit of the module which wants to disable clock supply. */ |
| 432 | mstp = CPG_SYS_ADVFS_BIT; |
| 433 | /* Disables the supply of clock signal to a module. */ |
| 434 | cpg_write(CPG_SMSTPCR9, mmio_read_32(CPG_SMSTPCR9) | mstp); |
| 435 | |
| 436 | #endif /* (AVS_SETTING_ENABLE==1) */ |
| 437 | } |
| 438 | |
| 439 | #if (AVS_SETTING_ENABLE == 1) |
| 440 | #if PMIC_ROHM_BD9571 |
| 441 | /* |
| 442 | * Check error and judge re-try. |
| 443 | */ |
| 444 | static avs_error_t avs_check_error(void) |
| 445 | { |
| 446 | avs_error_t ret; |
| 447 | |
| 448 | if ((mmio_read_8(IIC_ICSR) & ICSR_AL) == ICSR_AL) { |
| 449 | NOTICE("Loss of arbitration is detected. " |
| 450 | "AVS status=%d Retry=%u\n", avs_status, avs_retry); |
| 451 | /* Check of retry number of times */ |
| 452 | if (avs_retry >= AVS_RETRY_NUM) { |
| 453 | ERROR("AVS setting failed in retry. max=%u\n", |
| 454 | AVS_RETRY_NUM); |
| 455 | /* Infinite loop */ |
| 456 | panic(); |
| 457 | } |
| 458 | /* Set the error detected to error status. */ |
| 459 | ret = avs_error_al; |
| 460 | } else if ((mmio_read_8(IIC_ICSR) & ICSR_TACK) == ICSR_TACK) { |
| 461 | NOTICE("Non-acknowledge is detected. " |
| 462 | "AVS status=%d Retry=%u\n", avs_status, avs_retry); |
| 463 | /* Check of retry number of times */ |
| 464 | if (avs_retry >= AVS_RETRY_NUM) { |
| 465 | ERROR("AVS setting failed in retry. max=%u\n", |
| 466 | AVS_RETRY_NUM); |
| 467 | /* Infinite loop */ |
| 468 | panic(); |
| 469 | } |
| 470 | /* Set the error detected to error status. */ |
| 471 | ret = avs_error_nack; |
| 472 | } else { |
| 473 | /* Not error. */ |
| 474 | ret = avs_error_none; |
| 475 | } |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Set I2C for DVFS clock. |
| 481 | */ |
| 482 | static void avs_set_iic_clock(void) |
| 483 | { |
| 484 | uint32_t md_pin; |
| 485 | |
| 486 | /* Read Mode pin register. */ |
| 487 | md_pin = mmio_read_32(RCAR_MODEMR) & CHECK_MD13_MD14; |
| 488 | /* Set the module clock (CP phy) for the IIC-DVFS. */ |
| 489 | /* CP phy is EXTAL / 2. */ |
| 490 | switch (md_pin) { |
| 491 | case MD14_MD13_TYPE_0: /* EXTAL = 16.6666MHz */ |
| 492 | mmio_write_8(IIC_ICCL, ICCL_FREQ_8p33M); |
| 493 | mmio_write_8(IIC_ICCH, ICCH_FREQ_8p33M); |
| 494 | break; |
| 495 | case MD14_MD13_TYPE_1: /* EXTAL = 20MHz */ |
| 496 | mmio_write_8(IIC_ICCL, ICCL_FREQ_10M); |
| 497 | mmio_write_8(IIC_ICCH, ICCH_FREQ_10M); |
| 498 | break; |
| 499 | case MD14_MD13_TYPE_2: /* EXTAL = 25MHz (H3/M3) */ |
| 500 | mmio_write_8(IIC_ICCL, ICCL_FREQ_12p5M); |
| 501 | mmio_write_8(IIC_ICCH, ICCH_FREQ_12p5M); |
| 502 | break; |
| 503 | case MD14_MD13_TYPE_3: /* EXTAL = 33.3333MHz */ |
| 504 | mmio_write_8(IIC_ICCL, ICCL_FREQ_16p66M); |
| 505 | mmio_write_8(IIC_ICCH, ICCH_FREQ_16p66M); |
| 506 | break; |
| 507 | default: /* This case is not possible. */ |
| 508 | /* CP Phy frequency is to be set for the 16.66MHz */ |
| 509 | mmio_write_8(IIC_ICCL, ICCL_FREQ_16p66M); |
| 510 | mmio_write_8(IIC_ICCH, ICCH_FREQ_16p66M); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | #if AVS_READ_PMIC_REG_ENABLE == 1 |
| 516 | /* |
| 517 | * Read the value of the register of PMIC. |
| 518 | */ |
| 519 | static uint8_t avs_read_pmic_reg(uint8_t addr) |
| 520 | { |
| 521 | uint8_t reg; |
| 522 | |
| 523 | /* Set ICCR.ICE=1 to activate the I2C module. */ |
| 524 | mmio_write_8(IIC_ICCR, mmio_read_8(IIC_ICCR) | ICCR_ENABLE); |
| 525 | |
| 526 | /* Set frequency of 400kHz */ |
| 527 | avs_set_iic_clock(); |
| 528 | |
| 529 | /* Set ICIC.WAITE=1, ICIC.DTEE=1 to enable data transmission */ |
| 530 | /* interrupt and wait interrupt. */ |
| 531 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) | ICIC_WAITE | ICIC_DTEE); |
| 532 | |
| 533 | /* Write H'94 in ICCR to issue start condition */ |
| 534 | mmio_write_8(IIC_ICCR, ICCR_START); |
| 535 | |
| 536 | /* Wait for a until ICSR.DTE becomes 1. */ |
| 537 | avs_poll(ICSR_DTE, 1U); |
| 538 | |
| 539 | /* Clear ICIC.DTEE to disable a DTE interrupt. */ |
| 540 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) & (uint8_t) (~ICIC_DTEE)); |
| 541 | /* Send slave address of PMIC */ |
| 542 | mmio_write_8(IIC_ICDR, PMIC_W_SLAVE_ADDRESS); |
| 543 | |
| 544 | /* Wait for a until ICSR.WAIT becomes 1. */ |
| 545 | avs_poll(ICSR_WAIT, 1U); |
| 546 | |
| 547 | /* write PMIC address */ |
| 548 | mmio_write_8(IIC_ICDR, addr); |
| 549 | /* Clear ICSR.WAIT to exit from WAIT status. */ |
| 550 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) & (uint8_t) (~ICSR_WAIT)); |
| 551 | |
| 552 | /* Wait for a until ICSR.WAIT becomes 1. */ |
| 553 | avs_poll(ICSR_WAIT, 1U); |
| 554 | |
| 555 | /* Write H'94 in ICCR to issue restart condition */ |
| 556 | mmio_write_8(IIC_ICCR, ICCR_START); |
| 557 | /* Clear ICSR.WAIT to exit from WAIT status. */ |
| 558 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) & (uint8_t) (~ICSR_WAIT)); |
| 559 | /* Set ICIC.DTEE=1 to enable data transmission interrupt. */ |
| 560 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) | ICIC_DTEE); |
| 561 | |
| 562 | /* Wait for a until ICSR.DTE becomes 1. */ |
| 563 | avs_poll(ICSR_DTE, 1U); |
| 564 | |
| 565 | /* Clear ICIC.DTEE to disable a DTE interrupt. */ |
| 566 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) & (uint8_t) (~ICIC_DTEE)); |
| 567 | /* Send slave address of PMIC */ |
| 568 | mmio_write_8(IIC_ICDR, PMIC_R_SLAVE_ADDRESS); |
| 569 | |
| 570 | /* Wait for a until ICSR.WAIT becomes 1. */ |
| 571 | avs_poll(ICSR_WAIT, 1U); |
| 572 | |
| 573 | /* Write H'81 to ICCR to issue the repeated START condition */ |
| 574 | /* for changing the transmission mode to the receive mode. */ |
| 575 | mmio_write_8(IIC_ICCR, ICCR_START_RECV); |
| 576 | /* Clear ICSR.WAIT to exit from WAIT status. */ |
| 577 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) & (uint8_t) (~ICSR_WAIT)); |
| 578 | |
| 579 | /* Wait for a until ICSR.WAIT becomes 1. */ |
| 580 | avs_poll(ICSR_WAIT, 1U); |
| 581 | |
| 582 | /* Set ICCR to H'C0 for the STOP condition */ |
| 583 | mmio_write_8(IIC_ICCR, ICCR_STOP_RECV); |
| 584 | /* Clear ICSR.WAIT to exit from WAIT status. */ |
| 585 | mmio_write_8(IIC_ICSR, mmio_read_8(IIC_ICSR) & (uint8_t) (~ICSR_WAIT)); |
| 586 | /* Set ICIC.DTEE=1 to enable data transmission interrupt. */ |
| 587 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) | ICIC_DTEE); |
| 588 | |
| 589 | /* Wait for a until ICSR.DTE becomes 1. */ |
| 590 | avs_poll(ICSR_DTE, 1U); |
| 591 | |
| 592 | /* Receive DVFS SetVID register */ |
| 593 | /* Clear ICIC.DTEE to disable a DTE interrupt. */ |
| 594 | mmio_write_8(IIC_ICIC, mmio_read_8(IIC_ICIC) & (uint8_t) (~ICIC_DTEE)); |
| 595 | /* Receive DVFS SetVID register */ |
| 596 | reg = mmio_read_8(IIC_ICDR); |
| 597 | |
| 598 | /* Wait until ICSR.BUSY is cleared. */ |
| 599 | avs_poll(ICSR_BUSY, 0U); |
| 600 | |
| 601 | /* Set ICCR=H'00 to disable the I2C module. */ |
| 602 | mmio_write_8(IIC_ICCR, 0x00U); |
| 603 | |
| 604 | return reg; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Wait processing by the polling. |
| 609 | */ |
| 610 | static void avs_poll(uint8_t bit_pos, uint8_t val) |
| 611 | { |
| 612 | uint8_t bit_val = 0U; |
| 613 | |
| 614 | if (val != 0U) |
| 615 | bit_val = bit_pos; |
| 616 | |
| 617 | while (1) { |
| 618 | if ((mmio_read_8(IIC_ICSR) & bit_pos) == bit_val) |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | #endif /* AVS_READ_PMIC_REG_ENABLE */ |
| 623 | #endif /* PMIC_ROHM_BD9571 */ |
| 624 | #endif /* (AVS_SETTING_ENABLE==1) */ |