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