2015-12-17 16:37:15 +03:00
|
|
|
/*
|
|
|
|
* IMX31 Clock Control Module
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 NICTA
|
|
|
|
* Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
* To get the timer frequencies right, we need to emulate at least part of
|
|
|
|
* the i.MX31 CCM.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2015-12-17 16:37:15 +03:00
|
|
|
#include "hw/misc/imx31_ccm.h"
|
2015-12-15 15:16:16 +03:00
|
|
|
#include "qemu/log.h"
|
2015-12-17 16:37:15 +03:00
|
|
|
|
|
|
|
#define CKIH_FREQ 26000000 /* 26MHz crystal input */
|
|
|
|
|
|
|
|
#ifndef DEBUG_IMX31_CCM
|
|
|
|
#define DEBUG_IMX31_CCM 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DPRINTF(fmt, args...) \
|
|
|
|
do { \
|
|
|
|
if (DEBUG_IMX31_CCM) { \
|
|
|
|
fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX31_CCM, \
|
|
|
|
__func__, ##args); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static char const *imx31_ccm_reg_name(uint32_t reg)
|
|
|
|
{
|
2016-01-11 18:52:18 +03:00
|
|
|
static char unknown[20];
|
|
|
|
|
2015-12-17 16:37:15 +03:00
|
|
|
switch (reg) {
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CCMR_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "CCMR";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PDR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "PDR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PDR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "PDR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_RCSR_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "RCSR";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_MPCTL_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "MPCTL";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_UPCTL_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "UPCTL";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_SPCTL_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "SPCTL";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_COSR_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "COSR";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "CGR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "CGR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR2_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "CGR2";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_WIMR_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "WIMR";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LDC_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LDC";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_DCVR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "DCVR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_DCVR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "DCVR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_DCVR2_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "DCVR2";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_DCVR3_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "DCVR3";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTR2_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTR2";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTR3_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTR3";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTBR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTBR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_LTBR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "LTBR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PMCR0_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "PMCR0";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PMCR1_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "PMCR1";
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PDR2_REG:
|
2015-12-17 16:37:15 +03:00
|
|
|
return "PDR2";
|
|
|
|
default:
|
2016-01-11 18:52:18 +03:00
|
|
|
sprintf(unknown, "[%d ?]", reg);
|
|
|
|
return unknown;
|
2015-12-17 16:37:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_imx31_ccm = {
|
|
|
|
.name = TYPE_IMX31_CCM,
|
2016-01-11 18:52:18 +03:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2015-12-17 16:37:15 +03:00
|
|
|
.fields = (VMStateField[]) {
|
2016-01-11 18:52:18 +03:00
|
|
|
VMSTATE_UINT32_ARRAY(reg, IMX31CCMState, IMX31_CCM_MAX_REG),
|
2015-12-17 16:37:15 +03:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_pll_ref_clk(IMXCCMState *dev)
|
|
|
|
{
|
|
|
|
uint32_t freq = 0;
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
if ((s->reg[IMX31_CCM_CCMR_REG] & CCMR_PRCS) == 2) {
|
|
|
|
if (s->reg[IMX31_CCM_CCMR_REG] & CCMR_FPME) {
|
2015-12-17 16:37:15 +03:00
|
|
|
freq = CKIL_FREQ;
|
2016-01-11 18:52:18 +03:00
|
|
|
if (s->reg[IMX31_CCM_CCMR_REG] & CCMR_FPMF) {
|
2015-12-17 16:37:15 +03:00
|
|
|
freq *= 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
freq = CKIH_FREQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF("freq = %d\n", freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_mpll_clk(IMXCCMState *dev)
|
|
|
|
{
|
|
|
|
uint32_t freq;
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
freq = imx_ccm_calc_pll(s->reg[IMX31_CCM_MPCTL_REG],
|
|
|
|
imx31_ccm_get_pll_ref_clk(dev));
|
2015-12-17 16:37:15 +03:00
|
|
|
|
|
|
|
DPRINTF("freq = %d\n", freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_mcu_main_clk(IMXCCMState *dev)
|
|
|
|
{
|
|
|
|
uint32_t freq;
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
if ((s->reg[IMX31_CCM_CCMR_REG] & CCMR_MDS) ||
|
|
|
|
!(s->reg[IMX31_CCM_CCMR_REG] & CCMR_MPE)) {
|
2015-12-17 16:37:15 +03:00
|
|
|
freq = imx31_ccm_get_pll_ref_clk(dev);
|
|
|
|
} else {
|
|
|
|
freq = imx31_ccm_get_mpll_clk(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF("freq = %d\n", freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_hclk_clk(IMXCCMState *dev)
|
|
|
|
{
|
|
|
|
uint32_t freq;
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
freq = imx31_ccm_get_mcu_main_clk(dev)
|
|
|
|
/ (1 + EXTRACT(s->reg[IMX31_CCM_PDR0_REG], MAX));
|
2015-12-17 16:37:15 +03:00
|
|
|
|
|
|
|
DPRINTF("freq = %d\n", freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_ipg_clk(IMXCCMState *dev)
|
|
|
|
{
|
|
|
|
uint32_t freq;
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
freq = imx31_ccm_get_hclk_clk(dev)
|
|
|
|
/ (1 + EXTRACT(s->reg[IMX31_CCM_PDR0_REG], IPG));
|
2015-12-17 16:37:15 +03:00
|
|
|
|
|
|
|
DPRINTF("freq = %d\n", freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t imx31_ccm_get_clock_frequency(IMXCCMState *dev, IMXClk clock)
|
|
|
|
{
|
|
|
|
uint32_t freq = 0;
|
|
|
|
|
|
|
|
switch (clock) {
|
2016-03-16 20:05:59 +03:00
|
|
|
case CLK_NONE:
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
|
|
|
case CLK_IPG:
|
2016-03-16 20:06:00 +03:00
|
|
|
case CLK_IPG_HIGH:
|
2015-12-17 16:37:15 +03:00
|
|
|
freq = imx31_ccm_get_ipg_clk(dev);
|
|
|
|
break;
|
|
|
|
case CLK_32k:
|
|
|
|
freq = CKIL_FREQ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: unsupported clock %d\n",
|
|
|
|
TYPE_IMX31_CCM, __func__, clock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF("Clock = %d) = %d\n", clock, freq);
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void imx31_ccm_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
IMX31CCMState *s = IMX31_CCM(dev);
|
|
|
|
|
|
|
|
DPRINTF("()\n");
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
memset(s->reg, 0, sizeof(uint32_t) * IMX31_CCM_MAX_REG);
|
|
|
|
|
|
|
|
s->reg[IMX31_CCM_CCMR_REG] = 0x074b0b7d;
|
|
|
|
s->reg[IMX31_CCM_PDR0_REG] = 0xff870b48;
|
|
|
|
s->reg[IMX31_CCM_PDR1_REG] = 0x49fcfe7f;
|
|
|
|
s->reg[IMX31_CCM_RCSR_REG] = 0x007f0000;
|
|
|
|
s->reg[IMX31_CCM_MPCTL_REG] = 0x04001800;
|
|
|
|
s->reg[IMX31_CCM_UPCTL_REG] = 0x04051c03;
|
|
|
|
s->reg[IMX31_CCM_SPCTL_REG] = 0x04043001;
|
|
|
|
s->reg[IMX31_CCM_COSR_REG] = 0x00000280;
|
|
|
|
s->reg[IMX31_CCM_CGR0_REG] = 0xffffffff;
|
|
|
|
s->reg[IMX31_CCM_CGR1_REG] = 0xffffffff;
|
|
|
|
s->reg[IMX31_CCM_CGR2_REG] = 0xffffffff;
|
|
|
|
s->reg[IMX31_CCM_WIMR_REG] = 0xffffffff;
|
|
|
|
s->reg[IMX31_CCM_LTR1_REG] = 0x00004040;
|
|
|
|
s->reg[IMX31_CCM_PMCR0_REG] = 0x80209828;
|
|
|
|
s->reg[IMX31_CCM_PMCR1_REG] = 0x00aa0000;
|
|
|
|
s->reg[IMX31_CCM_PDR2_REG] = 0x00000285;
|
2015-12-17 16:37:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t imx31_ccm_read(void *opaque, hwaddr offset, unsigned size)
|
|
|
|
{
|
2016-01-22 18:09:21 +03:00
|
|
|
uint32_t value = 0;
|
2015-12-17 16:37:15 +03:00
|
|
|
IMX31CCMState *s = (IMX31CCMState *)opaque;
|
|
|
|
|
2016-01-11 18:52:18 +03:00
|
|
|
if ((offset >> 2) < IMX31_CCM_MAX_REG) {
|
|
|
|
value = s->reg[offset >> 2];
|
|
|
|
} else {
|
2015-12-17 16:37:15 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
|
|
|
|
HWADDR_PRIx "\n", TYPE_IMX31_CCM, __func__, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF("reg[%s] => 0x%" PRIx32 "\n", imx31_ccm_reg_name(offset >> 2),
|
|
|
|
value);
|
|
|
|
|
|
|
|
return (uint64_t)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void imx31_ccm_write(void *opaque, hwaddr offset, uint64_t value,
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
IMX31CCMState *s = (IMX31CCMState *)opaque;
|
|
|
|
|
|
|
|
DPRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx31_ccm_reg_name(offset >> 2),
|
|
|
|
(uint32_t)value);
|
|
|
|
|
|
|
|
switch (offset >> 2) {
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CCMR_REG:
|
|
|
|
s->reg[IMX31_CCM_CCMR_REG] = CCMR_FPMF | (value & 0x3b6fdfff);
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PDR0_REG:
|
|
|
|
s->reg[IMX31_CCM_PDR0_REG] = value & 0xff9f3fff;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_PDR1_REG:
|
|
|
|
s->reg[IMX31_CCM_PDR1_REG] = value;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_MPCTL_REG:
|
|
|
|
s->reg[IMX31_CCM_MPCTL_REG] = value & 0xbfff3fff;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_SPCTL_REG:
|
|
|
|
s->reg[IMX31_CCM_SPCTL_REG] = value & 0xbfff3fff;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR0_REG:
|
|
|
|
s->reg[IMX31_CCM_CGR0_REG] = value;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR1_REG:
|
|
|
|
s->reg[IMX31_CCM_CGR1_REG] = value;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
2016-01-11 18:52:18 +03:00
|
|
|
case IMX31_CCM_CGR2_REG:
|
|
|
|
s->reg[IMX31_CCM_CGR2_REG] = value;
|
2015-12-17 16:37:15 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
|
|
|
|
HWADDR_PRIx "\n", TYPE_IMX31_CCM, __func__, offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct MemoryRegionOps imx31_ccm_ops = {
|
|
|
|
.read = imx31_ccm_read,
|
|
|
|
.write = imx31_ccm_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
/*
|
|
|
|
* Our device would not work correctly if the guest was doing
|
|
|
|
* unaligned access. This might not be a limitation on the real
|
|
|
|
* device but in practice there is no reason for a guest to access
|
|
|
|
* this device unaligned.
|
|
|
|
*/
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
.unaligned = false,
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static void imx31_ccm_init(Object *obj)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
SysBusDevice *sd = SYS_BUS_DEVICE(obj);
|
|
|
|
IMX31CCMState *s = IMX31_CCM(obj);
|
|
|
|
|
|
|
|
memory_region_init_io(&s->iomem, OBJECT(dev), &imx31_ccm_ops, s,
|
|
|
|
TYPE_IMX31_CCM, 0x1000);
|
|
|
|
sysbus_init_mmio(sd, &s->iomem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void imx31_ccm_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
IMXCCMClass *ccm = IMX_CCM_CLASS(klass);
|
|
|
|
|
|
|
|
dc->reset = imx31_ccm_reset;
|
|
|
|
dc->vmsd = &vmstate_imx31_ccm;
|
|
|
|
dc->desc = "i.MX31 Clock Control Module";
|
|
|
|
|
|
|
|
ccm->get_clock_frequency = imx31_ccm_get_clock_frequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo imx31_ccm_info = {
|
|
|
|
.name = TYPE_IMX31_CCM,
|
|
|
|
.parent = TYPE_IMX_CCM,
|
|
|
|
.instance_size = sizeof(IMX31CCMState),
|
|
|
|
.instance_init = imx31_ccm_init,
|
|
|
|
.class_init = imx31_ccm_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void imx31_ccm_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&imx31_ccm_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(imx31_ccm_register_types)
|