HelenOS sources
This source file includes following definitions.
- log_message
- amdm37x_init
- amdm37x_setup_dpll_on_autoidle
- amdm37x_usb_clocks_set
- amdm37x_usb_tll_init
#include "amdm37x.h"
#include <assert.h>
#include <ddi.h>
#include <ddf/log.h>
#include <errno.h>
#include <stdio.h>
static void
log_message(const volatile void *place, uint64_t val, volatile void *base, size_t size,
void *data, bool write)
{
printf("PIO %s: %p(%p) %#" PRIx64 "\n", write ? "WRITE" : "READ",
(place - base) + data, place, val);
}
errno_t amdm37x_init(amdm37x_t *device, bool trace)
{
assert(device);
errno_t ret = EOK;
ret = pio_enable((void *)USBHOST_CM_BASE_ADDRESS, USBHOST_CM_SIZE,
(void **)&device->cm.usbhost);
if (ret != EOK)
return ret;
ret = pio_enable((void *)CORE_CM_BASE_ADDRESS, CORE_CM_SIZE,
(void **)&device->cm.core);
if (ret != EOK)
return ret;
ret = pio_enable((void *)CLOCK_CONTROL_CM_BASE_ADDRESS,
CLOCK_CONTROL_CM_SIZE, (void **)&device->cm.clocks);
if (ret != EOK)
return ret;
ret = pio_enable((void *)MPU_CM_BASE_ADDRESS,
MPU_CM_SIZE, (void **)&device->cm.mpu);
if (ret != EOK)
return ret;
ret = pio_enable((void *)IVA2_CM_BASE_ADDRESS,
IVA2_CM_SIZE, (void **)&device->cm.iva2);
if (ret != EOK)
return ret;
ret = pio_enable((void *)CLOCK_CONTROL_PRM_BASE_ADDRESS,
CLOCK_CONTROL_PRM_SIZE, (void **)&device->prm.clocks);
if (ret != EOK)
return ret;
ret = pio_enable((void *)GLOBAL_REG_PRM_BASE_ADDRESS,
GLOBAL_REG_PRM_SIZE, (void **)&device->prm.global);
if (ret != EOK)
return ret;
ret = pio_enable((void *)AMDM37x_USBTLL_BASE_ADDRESS,
AMDM37x_USBTLL_SIZE, (void **)&device->tll);
if (ret != EOK)
return ret;
ret = pio_enable((void *)AMDM37x_UHH_BASE_ADDRESS,
AMDM37x_UHH_SIZE, (void **)&device->uhh);
if (ret != EOK)
return ret;
if (trace) {
pio_trace_enable(device->tll, AMDM37x_USBTLL_SIZE, log_message, (void *)AMDM37x_USBTLL_BASE_ADDRESS);
pio_trace_enable(device->cm.clocks, CLOCK_CONTROL_CM_SIZE, log_message, (void *)CLOCK_CONTROL_CM_BASE_ADDRESS);
pio_trace_enable(device->cm.core, CORE_CM_SIZE, log_message, (void *)CORE_CM_BASE_ADDRESS);
pio_trace_enable(device->cm.mpu, MPU_CM_SIZE, log_message, (void *)MPU_CM_BASE_ADDRESS);
pio_trace_enable(device->cm.iva2, IVA2_CM_SIZE, log_message, (void *)IVA2_CM_BASE_ADDRESS);
pio_trace_enable(device->cm.usbhost, USBHOST_CM_SIZE, log_message, (void *)USBHOST_CM_BASE_ADDRESS);
pio_trace_enable(device->uhh, AMDM37x_UHH_SIZE, log_message, (void *)AMDM37x_UHH_BASE_ADDRESS);
pio_trace_enable(device->prm.clocks, CLOCK_CONTROL_PRM_SIZE, log_message, (void *)CLOCK_CONTROL_PRM_BASE_ADDRESS);
pio_trace_enable(device->prm.global, GLOBAL_REG_PRM_SIZE, log_message, (void *)GLOBAL_REG_PRM_BASE_ADDRESS);
}
return EOK;
}
void amdm37x_setup_dpll_on_autoidle(amdm37x_t *device)
{
assert(device);
const unsigned osc_clk = pio_read_32(&device->prm.clocks->clksel) &
CLOCK_CONTROL_PRM_CLKSEL_SYS_CLKIN_MASK;
const unsigned clk_reg = pio_read_32(&device->prm.global->clksrc_ctrl);
const unsigned base_freq = sys_clk_freq_kHz(osc_clk) /
GLOBAL_REG_PRM_CLKSRC_CTRL_SYSCLKDIV_GET(clk_reg);
ddf_msg(LVL_NOTE, "Base frequency: %d.%dMhz",
base_freq / 1000, base_freq % 1000);
mpu_cm_regs_t *mpu = device->cm.mpu;
if (pio_read_32(&mpu->clkstst) & MPU_CM_CLKSTST_CLKACTIVITY_MPU_ACTIVE_FLAG) {
if (pio_read_32(&mpu->idlest_pll) & MPU_CM_IDLEST_PLL_ST_MPU_CLK_LOCKED_FLAG) {
const uint32_t reg = pio_read_32(&mpu->clksel1_pll);
const unsigned multiplier =
(reg & MPU_CM_CLKSEL1_PLL_MPU_DPLL_MULT_MASK) >>
MPU_CM_CLKSEL1_PLL_MPU_DPLL_MULT_SHIFT;
const unsigned divisor =
(reg & MPU_CM_CLKSEL1_PLL_MPU_DPLL_DIV_MASK) >>
MPU_CM_CLKSEL1_PLL_MPU_DPLL_DIV_SHIFT;
const unsigned divisor2 =
(pio_read_32(&mpu->clksel2_pll) &
MPU_CM_CLKSEL2_PLL_MPU_DPLL_CLKOUT_DIV_MASK);
if (multiplier && divisor && divisor2) {
const unsigned freq =
((base_freq * multiplier) / (divisor + 1)) /
divisor2;
ddf_msg(LVL_NOTE, "MPU running at %d.%d MHz",
freq / 1000, freq % 1000);
} else {
ddf_msg(LVL_WARN, "Frequency divisor and/or "
"multiplier value invalid: %d %d %d",
multiplier, divisor, divisor2);
}
} else {
const unsigned divisor =
MPU_CM_CLKSEL1_PLL_MPU_CLK_SRC_VAL(
pio_read_32(&mpu->clksel1_pll));
ddf_msg(LVL_NOTE, "MPU DPLL in bypass mode, running at"
" CORE CLK / %d MHz", divisor);
}
} else {
ddf_msg(LVL_WARN, "MPU clock domain is not active, we should not be running...");
}
#if 0
pio_set_32(&mpu->clken_pll, MPU_CM_CLKEN_PLL_EN_MPU_DPLL_LP_MODE_FLAG, 5);
pio_change_32(&mpu->autoidle_pll, MPU_CM_AUTOIDLE_PLL_AUTO_MPU_DPLL_ENABLED, MPU_CM_AUTOIDLE_PLL_AUTO_MPU_DPLL_MASK, 5);
#endif
pio_set_32(&device->cm.iva2->clken_pll, MPU_CM_CLKEN_PLL_EN_MPU_DPLL_LP_MODE_FLAG, 5);
pio_change_32(&device->cm.iva2->autoidle_pll, MPU_CM_AUTOIDLE_PLL_AUTO_MPU_DPLL_ENABLED, MPU_CM_AUTOIDLE_PLL_AUTO_MPU_DPLL_MASK, 5);
if (pio_read_32(&device->cm.clocks->idlest_ckgen) & CLOCK_CONTROL_CM_IDLEST_CKGEN_ST_CORE_CLK_FLAG) {
const uint32_t reg =
pio_read_32(&device->cm.clocks->clksel1_pll);
const unsigned multiplier =
CLOCK_CONTROL_CM_CLKSEL1_PLL_CORE_DPLL_MULT_GET(reg);
const unsigned divisor =
CLOCK_CONTROL_CM_CLKSEL1_PLL_CORE_DPLL_DIV_GET(reg);
const unsigned divisor2 =
CLOCK_CONTROL_CM_CLKSEL1_PLL_CORE_DPLL_CLKOUT_DIV_GET(reg);
if (multiplier && divisor && divisor2) {
const unsigned freq =
((base_freq * multiplier) / (divisor + 1)) / divisor2;
ddf_msg(LVL_NOTE, "CORE CLK running at %d.%d MHz",
freq / 1000, freq % 1000);
const unsigned l3_div =
pio_read_32(&device->cm.core->clksel) &
CORE_CM_CLKSEL_CLKSEL_L3_MASK;
if (l3_div == CORE_CM_CLKSEL_CLKSEL_L3_DIVIDED1 ||
l3_div == CORE_CM_CLKSEL_CLKSEL_L3_DIVIDED2) {
ddf_msg(LVL_NOTE, "L3 interface at %d.%d MHz",
(freq / l3_div) / 1000,
(freq / l3_div) % 1000);
} else {
ddf_msg(LVL_WARN, "L3 interface clock divisor is"
" invalid: %d", l3_div);
}
} else {
ddf_msg(LVL_WARN, "DPLL3 frequency divisor and/or "
"multiplier value invalid: %d %d %d",
multiplier, divisor, divisor2);
}
} else {
ddf_msg(LVL_WARN, "CORE CLK in bypass mode, fruunig at SYS_CLK"
" frreq of %d.%d MHz", base_freq / 1000, base_freq % 1000);
}
pio_change_32(&device->cm.clocks->autoidle_pll,
CLOCK_CONTROL_CM_AUTOIDLE_PLL_AUTO_CORE_DPLL_AUTOMATIC,
CLOCK_CONTROL_CM_AUTOIDLE_PLL_AUTO_CORE_DPLL_MASK, 5);
pio_change_32(&device->cm.clocks->autoidle_pll,
CLOCK_CONTROL_CM_AUTOIDLE_PLL_AUTO_PERIPH_DPLL_AUTOMATIC,
CLOCK_CONTROL_CM_AUTOIDLE_PLL_AUTO_PERIPH_DPLL_MASK, 5);
if ((pio_read_32(&device->cm.clocks->clken2_pll) &
CLOCK_CONTROL_CM_CLKEN2_PLL_EN_PERIPH2_DPLL_MASK) !=
CLOCK_CONTROL_CM_CLKEN2_PLL_EN_PERIPH2_DPLL_LOCK) {
const unsigned mult = 120;
const unsigned div = (base_freq / 1000) - 1;
const unsigned div2 = 1;
if (((base_freq % 1000) != 0) || (div > 127)) {
ddf_msg(LVL_ERROR, "Rounding error, or divisor to big "
"freq: %d, div: %d", base_freq, div);
return;
}
assert(div <= 127);
pio_change_32(&device->cm.clocks->clksel4_pll,
CLOCK_CONTROL_CM_CLKSEL4_PLL_PERIPH2_DPLL_MULT_CREATE(mult),
CLOCK_CONTROL_CM_CLKSEL4_PLL_PERIPH2_DPLL_MULT_MASK, 10);
pio_change_32(&device->cm.clocks->clksel4_pll,
CLOCK_CONTROL_CM_CLKSEL4_PLL_PERIPH2_DPLL_DIV_CREATE(div),
CLOCK_CONTROL_CM_CLKSEL4_PLL_PERIPH2_DPLL_DIV_MASK, 10);
pio_change_32(&device->cm.clocks->clksel5_pll,
CLOCK_CONTROL_CM_CLKSEL5_PLL_DIV120M_CREATE(div2),
CLOCK_CONTROL_CM_CLKSEL5_PLL_DIV120M_MASK, 10);
pio_change_32(&device->cm.clocks->clken2_pll,
CLOCK_CONTROL_CM_CLKEN2_PLL_EN_PERIPH2_DPLL_LOCK,
CLOCK_CONTROL_CM_CLKEN2_PLL_EN_PERIPH2_DPLL_MASK, 10);
}
pio_change_32(&device->cm.clocks->autoidle2_pll,
CLOCK_CONTROL_CM_AUTOIDLE2_PLL_AUTO_PERIPH2_DPLL_AUTOMATIC,
CLOCK_CONTROL_CM_AUTOIDLE2_PLL_AUTO_PERIPH2_DPLL_MASK, 5);
}
void amdm37x_usb_clocks_set(amdm37x_t *device, bool enabled)
{
if (enabled) {
pio_set_32(&device->cm.core->fclken3,
CORE_CM_FCLKEN3_EN_USBTLL_FLAG, 5);
pio_set_32(&device->cm.core->iclken3,
CORE_CM_ICLKEN3_EN_USBTLL_FLAG, 5);
pio_set_32(&device->cm.usbhost->fclken,
USBHOST_CM_FCLKEN_EN_USBHOST1_FLAG |
USBHOST_CM_FCLKEN_EN_USBHOST2_FLAG, 5);
pio_set_32(&device->cm.usbhost->iclken,
USBHOST_CM_ICLKEN_EN_USBHOST, 5);
#if 0
printf("DPLL5 (and everything else) should be on: %"
PRIx32 " %" PRIx32 ".\n",
pio_read_32(&device->cm.clocks->idlest_ckgen),
pio_read_32(&device->cm.clocks->idlest2_ckgen));
#endif
} else {
pio_clear_32(&device->cm.usbhost->iclken,
USBHOST_CM_ICLKEN_EN_USBHOST, 5);
pio_clear_32(&device->cm.usbhost->fclken,
USBHOST_CM_FCLKEN_EN_USBHOST1_FLAG |
USBHOST_CM_FCLKEN_EN_USBHOST2_FLAG, 5);
pio_clear_32(&device->cm.core->iclken3,
CORE_CM_ICLKEN3_EN_USBTLL_FLAG, 5);
pio_clear_32(&device->cm.core->fclken3,
CORE_CM_FCLKEN3_EN_USBTLL_FLAG, 5);
}
}
errno_t amdm37x_usb_tll_init(amdm37x_t *device)
{
if (pio_read_32(&device->cm.core->idlest3) & CORE_CM_IDLEST3_ST_USBTLL_FLAG) {
ddf_msg(LVL_ERROR, "USB TLL is not accessible");
return EIO;
}
pio_set_32(&device->tll->sysconfig, TLL_SYSCONFIG_SOFTRESET_FLAG, 5);
ddf_msg(LVL_DEBUG2, "Waiting for USB TLL reset");
while (!(pio_read_32(&device->tll->sysstatus) & TLL_SYSSTATUS_RESET_DONE_FLAG))
;
ddf_msg(LVL_DEBUG, "USB TLL Reset done.");
pio_change_32(&device->tll->sysconfig,
TLL_SYSCONFIG_CLOCKACTIVITY_FLAG | TLL_SYSCONFIG_AUTOIDLE_FLAG |
TLL_SYSCONFIG_SIDLE_MODE_SMART, TLL_SYSCONFIG_SIDLE_MODE_MASK, 5);
pio_change_32(&device->uhh->sysconfig,
UHH_SYSCONFIG_CLOCKACTIVITY_FLAG | UHH_SYSCONFIG_AUTOIDLE_FLAG |
UHH_SYSCONFIG_SIDLE_MODE_SMART, UHH_SYSCONFIG_SIDLE_MODE_MASK, 5);
pio_set_32(&device->uhh->hostconfig,
UHH_HOSTCONFIG_P1_ULPI_BYPASS_FLAG |
UHH_HOSTCONFIG_P2_ULPI_BYPASS_FLAG |
UHH_HOSTCONFIG_P3_ULPI_BYPASS_FLAG, 5);
pio_set_32(&device->tll->shared_conf, TLL_SHARED_CONF_FCLK_IS_ON_FLAG, 5);
for (unsigned i = 0; i < 3; ++i) {
pio_change_32(&device->tll->channel_conf[i],
TLL_CHANNEL_CONF_CHANMODE_UTMI_SERIAL_MODE |
TLL_CHANNEL_CONF_FSLSMODE_3PIN_BIDI_PHY,
TLL_CHANNEL_CONF_CHANMODE_MASK |
TLL_CHANNEL_CONF_FSLSMODE_MASK, 5);
}
return EOK;
}
HelenOS homepage, sources at GitHub