| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <device/mmio.h> |
| 5 |
#include <gpio.h> |
| 6 |
#include <soc/addressmap.h> |
| 7 |
#include <soc/pll.h> |
| 8 |
#include <soc/pmif.h> |
| 9 |
#include <soc/pmif_spmi.h> |
| 10 |
#include <soc/spmi.h> |
| 11 |
|
| 12 |
#define SPMI_RCS_CTRL_BASE 0x140084 |
| 13 |
|
| 14 |
|
| 15 |
DEFINE_BITFIELD(CLK_SPMI_MST_SEL, 10, 8) |
| 16 |
DEFINE_BIT(CLK_SPMI_MST_INT, 12) |
| 17 |
DEFINE_BIT(PDN_SPMI_MST, 15) |
| 18 |
|
| 19 |
|
| 20 |
DEFINE_BIT(SPMI_MST_CK_UPDATE, 5) |
| 21 |
|
| 22 |
|
| 23 |
DEFINE_BIT(SAMPL_CK_POL, 0) |
| 24 |
DEFINE_BITFIELD(SAMPL_CK_DLY, 3, 1) |
| 25 |
DEFINE_BITFIELD(SAMPL_CK_DLY_ARB, 6, 4) |
| 26 |
|
| 27 |
const struct spmi_device spmi_dev[] = { |
| 28 |
{ |
| 29 |
.slvid = SPMI_SLAVE_4, |
| 30 |
.mstid = SPMI_MASTER_1, |
| 31 |
.type = MAIN_PMIC, |
| 32 |
.type_id = MAIN_PMIC_ID, |
| 33 |
.hwcid_addr = 0x09, |
| 34 |
.hwcid_val = 0x63, |
| 35 |
.hwcid_mask = 0xF0, |
| 36 |
}, |
| 37 |
{ |
| 38 |
.slvid = SPMI_SLAVE_9, |
| 39 |
.mstid = SPMI_MASTER_1, |
| 40 |
.type = CLOCK_PMIC, |
| 41 |
.type_id = CLOCK_PMIC_ID, |
| 42 |
.hwcid_addr = 0x09, |
| 43 |
.hwcid_val = 0x85, |
| 44 |
.hwcid_mask = 0xF0, |
| 45 |
}, |
| 46 |
{ |
| 47 |
.slvid = SPMI_SLAVE_5, |
| 48 |
.mstid = SPMI_MASTER_1, |
| 49 |
.type = SECOND_PMIC, |
| 50 |
.type_id = SECOND_PMIC_ID, |
| 51 |
.hwcid_addr = 0x09, |
| 52 |
.hwcid_val = 0x73, |
| 53 |
.hwcid_mask = 0xF0, |
| 54 |
}, |
| 55 |
{ |
| 56 |
.slvid = SPMI_SLAVE_6, |
| 57 |
.mstid = SPMI_MASTER_0, |
| 58 |
.type = SUB_PMIC, |
| 59 |
.type_id = SUB_PMIC_ID, |
| 60 |
.hwcid_addr = 0x209, |
| 61 |
.hwcid_val = 0x16, |
| 62 |
.hwcid_mask = 0xf0, |
| 63 |
}, |
| 64 |
{ |
| 65 |
.slvid = SPMI_SLAVE_7, |
| 66 |
.mstid = SPMI_MASTER_0, |
| 67 |
.type = SUB_PMIC, |
| 68 |
.type_id = SUB_PMIC_ID, |
| 69 |
.hwcid_addr = 0x209, |
| 70 |
.hwcid_val = 0x16, |
| 71 |
.hwcid_mask = 0xf0, |
| 72 |
}, |
| 73 |
{ |
| 74 |
.slvid = SPMI_SLAVE_8, |
| 75 |
.mstid = SPMI_MASTER_0, |
| 76 |
.type = SUB_PMIC, |
| 77 |
.type_id = SUB_PMIC_ID, |
| 78 |
.hwcid_addr = 0x209, |
| 79 |
.hwcid_val = 0x16, |
| 80 |
.hwcid_mask = 0xf0, |
| 81 |
}, |
| 82 |
{ |
| 83 |
.slvid = SPMI_SLAVE_15, |
| 84 |
.mstid = SPMI_MASTER_0, |
| 85 |
.type = SUB_PMIC, |
| 86 |
.type_id = SUB_PMIC_ID, |
| 87 |
.hwcid_addr = 0x209, |
| 88 |
.hwcid_val = 0x16, |
| 89 |
.hwcid_mask = 0xf0, |
| 90 |
}, |
| 91 |
}; |
| 92 |
|
| 93 |
static const uint32_t chan_perm[2][4] = { |
| 94 |
[SPMI_MASTER_0] = { 0x8888888A, 0x88888888, 0x88888888, 0x88888888 }, |
| 95 |
[SPMI_MASTER_1] = { 0x888888AA, 0x88888888, 0x88888888, 0x88888888 }, |
| 96 |
}; |
| 97 |
|
| 98 |
static const uint32_t lat_limit[2][23] = { |
| 99 |
[SPMI_MASTER_0] = { 0x3e6, 0x3e6, 0x9, 0x9, 0x3e6, 0x3e6, 0x3e6, 0x3e6, 0x9, 0x3e6, |
| 100 |
0x0, 0x3e6, 0x9, 0x0, 0x0, 0x3e6, 0x3e6, 0x3e6, 0x3e6, 0x0, |
| 101 |
0x3e6, 0x0, 0x3e6 }, |
| 102 |
[SPMI_MASTER_1] = { 0x3e6, 0x0, 0x3e6, 0x3e6, 0x26, 0x37, 0x3e6, 0x10, 0x9, 0x23, |
| 103 |
0x0, 0x39, 0x9, 0x0, 0x0, 0x12, 0x3e6, 0x3e6, 0x3e6, 0x0, |
| 104 |
0x1e, 0x0, 0x3e6 }, |
| 105 |
}; |
| 106 |
|
| 107 |
static struct mtk_spmi_mst_reg *get_mst_reg(struct pmif *arb) |
| 108 |
{ |
| 109 |
if (arb->mstid == SPMI_MASTER_1) |
| 110 |
return mtk_spmi_mst; |
| 111 |
else if (arb->mstid == SPMI_MASTER_0 || arb->mstid == SPMI_MASTER_2) |
| 112 |
return mtk_spmi_mst_p; |
| 113 |
else |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
static int pmif_spmi_config_master(struct pmif *arb) |
| 118 |
{ |
| 119 |
struct mtk_spmi_mst_reg *reg = get_mst_reg(arb); |
| 120 |
if (!reg) { |
| 121 |
printk(BIOS_ERR, "%s: get_mst_reg get null pointer\n", __func__); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
write32(®->mst_req_en, 1); |
| 126 |
write32(®->grp_id_en, 0xC00); |
| 127 |
|
| 128 |
write32(®->rcs_ctrl, SPMI_RCS_CTRL_BASE + arb->mstid); |
| 129 |
printk(BIOS_INFO, "%s: mstid=%d done\n", __func__, arb->mstid); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
void pmif_spmi_config(struct pmif *arb) |
| 134 |
{ |
| 135 |
_Static_assert(ARRAY_SIZE(chan_perm[0]) == ARRAY_SIZE(arb->mtk_pmif->inf_cmd_per), |
| 136 |
"Inconsistent array size for chan_perm and inf_cmd_per"); |
| 137 |
const uint32_t *perm = chan_perm[arb->mstid % 2]; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
for (int i = 0; i < ARRAY_SIZE(arb->mtk_pmif->inf_cmd_per); i++) |
| 147 |
write32(&arb->mtk_pmif->inf_cmd_per[i], perm[i]); |
| 148 |
} |
| 149 |
|
| 150 |
void pmif_spmi_iocfg(void) |
| 151 |
{ |
| 152 |
|
| 153 |
gpio_set_driving(GPIO(SPMI_M_SCL), GPIO_DRV_10_MA); |
| 154 |
gpio_set_driving(GPIO(SPMI_M_SDA), GPIO_DRV_10_MA); |
| 155 |
|
| 156 |
gpio_set_driving(GPIO(SPMI_P_SCL), GPIO_DRV_16_MA); |
| 157 |
gpio_set_driving(GPIO(SPMI_P_SDA), GPIO_DRV_16_MA); |
| 158 |
|
| 159 |
gpio_set_pull(GPIO(SPMI_P_SCL), GPIO_PULL_ENABLE, GPIO_PULL_DOWN); |
| 160 |
gpio_set_pull(GPIO(SPMI_P_SDA), GPIO_PULL_ENABLE, GPIO_PULL_DOWN); |
| 161 |
printk(BIOS_INFO, "%s done\n", __func__); |
| 162 |
} |
| 163 |
|
| 164 |
static int spmi_read_check(struct pmif *arb, const struct spmi_device *dev) |
| 165 |
{ |
| 166 |
u32 rdata = 0; |
| 167 |
|
| 168 |
arb->read(arb, dev->slvid, dev->hwcid_addr, &rdata); |
| 169 |
if ((rdata & dev->hwcid_mask) != (dev->hwcid_val & dev->hwcid_mask)) { |
| 170 |
printk(BIOS_WARNING, "%s next, slvid:%d rdata = 0x%x\n", |
| 171 |
__func__, dev->slvid, rdata); |
| 172 |
return -E_NODEV; |
| 173 |
} |
| 174 |
|
| 175 |
printk(BIOS_INFO, "%s next, slvid:%d rdata = 0x%x.\n", __func__, dev->slvid, rdata); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static int spmi_cali_rd_clock_polarity(struct pmif *arb, const struct spmi_device *dev) |
| 180 |
{ |
| 181 |
size_t i; |
| 182 |
bool success = false; |
| 183 |
const struct cali cali_data[] = { |
| 184 |
{SPMI_CK_DLY_1T, SPMI_CK_POL_NEG}, |
| 185 |
{SPMI_CK_DLY_1T, SPMI_CK_POL_POS}, |
| 186 |
{SPMI_CK_NO_DLY, SPMI_CK_POL_POS}, |
| 187 |
{SPMI_CK_NO_DLY, SPMI_CK_POL_NEG}, |
| 188 |
{SPMI_CK_DLY_2T, SPMI_CK_POL_POS}, |
| 189 |
{SPMI_CK_DLY_2T, SPMI_CK_POL_NEG}, |
| 190 |
}; |
| 191 |
|
| 192 |
struct mtk_spmi_mst_reg *reg = get_mst_reg(arb); |
| 193 |
if (!reg) { |
| 194 |
printk(BIOS_ERR, "%s: get_mst_reg get null pointer\n", __func__); |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
for (i = 0; i < ARRAY_SIZE(cali_data); i++) { |
| 200 |
SET32_BITFIELDS(®->mst_sampl, SAMPL_CK_DLY, cali_data[i].dly, |
| 201 |
SAMPL_CK_POL, cali_data[i].pol); |
| 202 |
SET32_BITFIELDS(®->mst_sampl, SAMPL_CK_DLY_ARB, cali_data[i].dly + 1); |
| 203 |
printk(BIOS_DEBUG, "dly:%d, pol:%d, sampl:0x%x\n", |
| 204 |
cali_data[i].dly, cali_data[i].pol, read32(®->mst_sampl)); |
| 205 |
if (spmi_read_check(arb, dev) == 0) { |
| 206 |
success = true; |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (!success) |
| 212 |
die("ERROR - calibration fail for SPMI clk"); |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static int spmi_config_slave(struct pmif *arb, const struct spmi_device *dev) |
| 218 |
{ |
| 219 |
u32 wdata; |
| 220 |
|
| 221 |
|
| 222 |
wdata = dev->slvid; |
| 223 |
arb->write(arb, dev->slvid, 0x419, wdata); |
| 224 |
|
| 225 |
|
| 226 |
wdata = 0x5 | (dev->mstid << 4); |
| 227 |
arb->write(arb, dev->slvid, 0x418, wdata); |
| 228 |
|
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
static int spmi_mst_init(struct pmif *arb) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
|
| 236 |
if (!arb) { |
| 237 |
printk(BIOS_ERR, "%s: null pointer for pmif dev\n", __func__); |
| 238 |
return -E_INVAL; |
| 239 |
} |
| 240 |
|
| 241 |
if (!CONFIG(PMIF_SPMI_IOCFG_DEFAULT_SETTING)) |
| 242 |
pmif_spmi_iocfg(); |
| 243 |
|
| 244 |
if (pmif_spmi_config_master(arb)) |
| 245 |
return -1; |
| 246 |
|
| 247 |
for (i = 0; i < spmi_dev_cnt(); i++) { |
| 248 |
if ((arb->mstid % 2) == spmi_dev[i].mstid) { |
| 249 |
spmi_cali_rd_clock_polarity(arb, &spmi_dev[i]); |
| 250 |
spmi_config_slave(arb, &spmi_dev[i]); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
static void pmif_spmi_enable_swinf(struct pmif *arb) |
| 258 |
{ |
| 259 |
write32(&arb->mtk_pmif->inf_en, PMIF_SPMI_SW_CHAN); |
| 260 |
write32(&arb->mtk_pmif->arb_en, PMIF_SPMI_SW_CHAN); |
| 261 |
printk(BIOS_INFO, "%s done\n", __func__); |
| 262 |
} |
| 263 |
|
| 264 |
static void pmif_spmi_enable_cmd_issue(struct pmif *arb, bool en) |
| 265 |
{ |
| 266 |
|
| 267 |
write32(&arb->mtk_pmif->cmdissue_en, en); |
| 268 |
printk(BIOS_INFO, "%s done\n", __func__); |
| 269 |
} |
| 270 |
|
| 271 |
static void pmif_spmi_enable(struct pmif *arb) |
| 272 |
{ |
| 273 |
_Static_assert(ARRAY_SIZE(lat_limit[0]) == ARRAY_SIZE(arb->mtk_pmif->lat_limit), |
| 274 |
"Inconsistent array size for lat_limit and arb->mtk_pmif->lat_limit"); |
| 275 |
const uint32_t *lat_limit_value = lat_limit[arb->mstid % 2]; |
| 276 |
u32 pmif_spmi_inf; |
| 277 |
int i; |
| 278 |
|
| 279 |
if (arb->mstid % 2) |
| 280 |
pmif_spmi_inf = PMIF_SPMI_INF; |
| 281 |
else |
| 282 |
pmif_spmi_inf = PMIF_SPMI_INF_P; |
| 283 |
|
| 284 |
pmif_spmi_config(arb); |
| 285 |
|
| 286 |
|
| 287 |
for (i = 0; i < ARRAY_SIZE(arb->mtk_pmif->inf_max_bytecnt_per); i++) |
| 288 |
write32(&arb->mtk_pmif->inf_max_bytecnt_per[i], 0x11111111); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
write32(&arb->mtk_pmif->lat_cnter_ctrl, 0x32); |
| 293 |
if (arb->mstid % 2) |
| 294 |
write32(&arb->mtk_pmif->lat_cnter_en, pmif_spmi_inf); |
| 295 |
for (i = 0; i < ARRAY_SIZE(arb->mtk_pmif->lat_limit); i++) |
| 296 |
write32(&arb->mtk_pmif->lat_limit[i], lat_limit_value[i]); |
| 297 |
write32(&arb->mtk_pmif->lat_limit_loading, pmif_spmi_inf); |
| 298 |
|
| 299 |
|
| 300 |
write32(&arb->mtk_pmif->staupd_ctrl, 0x1030000); |
| 301 |
|
| 302 |
write32(&arb->mtk_pmif->inf_en, pmif_spmi_inf); |
| 303 |
write32(&arb->mtk_pmif->arb_en, pmif_spmi_inf); |
| 304 |
write32(&arb->mtk_pmif->timer_ctrl, 0x3); |
| 305 |
write32(&arb->mtk_pmif->init_done, 1); |
| 306 |
printk(BIOS_INFO, "%s init done: 0x%x\n", __func__, read32(&arb->mtk_pmif->init_done)); |
| 307 |
} |
| 308 |
|
| 309 |
int pmif_spmi_init(struct pmif *arb) |
| 310 |
{ |
| 311 |
if (arb->check_init_done(arb) != 0) { |
| 312 |
pmif_spmi_force_normal_mode(arb); |
| 313 |
pmif_spmi_enable_swinf(arb); |
| 314 |
pmif_spmi_enable_cmd_issue(arb, true); |
| 315 |
pmif_spmi_enable(arb); |
| 316 |
if (arb->check_init_done(arb)) |
| 317 |
return -E_NODEV; |
| 318 |
} |
| 319 |
|
| 320 |
if (spmi_mst_init(arb)) { |
| 321 |
printk(BIOS_ERR, "[%s] failed to init SPMI master\n", __func__); |
| 322 |
return -E_NODEV; |
| 323 |
} |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
size_t spmi_dev_cnt(void) |
| 329 |
{ |
| 330 |
return ARRAY_SIZE(spmi_dev); |
| 331 |
} |