OP-TEE / optee_os

Trusted side of the TEE
Other
1.58k stars 1.07k forks source link

stm32mp1 RCC clock and CRYP peripheral #4882

Closed janoslim closed 3 years ago

janoslim commented 3 years ago

Hello,

I am currently making driver on optee for CRYP of stm32mp157c-dk2 board which is crypto peripheral on cpu bus. And I refer to linux driver source code already implemented. And I notice that clock enabling is required and It seems that it is related to RCC.

line 2080 of link here.

cryp->clk = devm_clk_get(dev, NULL);
if (IS_ERR(cryp->clk)) {
if (PTR_ERR(cryp->clk) != -EPROBE_DEFER)
    dev_err(dev, "Could not get clock\n");

return PTR_ERR(cryp->clk);
}

ret = clk_prepare_enable(cryp->clk);
if (ret) {
dev_err(cryp->dev, "Failed to enable clock\n");
return ret;
}

and in stm32mp15xc.dtsi, it is defined as using clock and reset of RCC,

/ {
    soc {
        cryp1: cryp@54001000 {
            compatible = "st,stm32mp1-cryp";
            reg = <0x54001000 0x400>;
            interrupts = <GIC_SPI 79 IRQ_TYPE_LEVEL_HIGH>;
            clocks = <&rcc CRYP1>;
            resets = <&rcc CRYP1_R>;
            status = "disabled";
        };
    };
};

And what i tried to do is... io_setbits32(rcc + RCC_MC_AHB5ENSETR, RCC_MC_AHB5ENSETR_CRYP1EN_POS);

And I wonder what I need to do for enable / disable this peripheral in the point of RCC. As I understand, just writing value to specific register(RCC_AHB5RSTCLRR) is needed according to this TRM page 778. However due to I do not have many background on stm system, I wonder It would works.

Thank you for your great help!

etienne-lms commented 3 years ago

To manipulate the cryp clock, you should use the clock driver interface, from #include <stm32_util.h>:

void stm32_clock_enable(unsigned long id);
void stm32_clock_disable(unsigned long id);
unsigned long stm32_clock_get_rate(unsigned long id);
bool stm32_clock_is_enabled(unsigned long id);

To get the clock, use DT support. Something like:

const void *fdt = get_embedded_dt();
int node = fdt_node_offset_by_compatible(fdt, -1, "st,stm32mp1-cryp");
struct dt_node_info dt_info = { };
unsigned long clock = 0;

if (node < 0) {
    // node node or FDT not found
}

_fdt_fill_device_info(fdt, &dt_info, node);
if (dt_info.status == DT_STATUS_DISABLED) {
    // ensure cryp has status = "okay"
}
if (dt_info.clock == DT_INFO_INVALID_CLOCK) {
    // no clock defined...
} else {
    // use can use dt_info.clock as clock reference
    clock = (unsigned long)dt_info.clock;
    // try it
    EMSG("cryp clock: %lu Hz, %s", stm32_clock_get_rate(clock),
         stm32_clock_is_enabled(clock) ? "enabled" : "disabled");
}

Note that a clock framework is likely to be merged in OP-TEE soon (https://github.com/OP-TEE/optee_os/pull/4705). Stm32mp1 clock driver will move to that framework when possible. Stay tuned.

janoslim commented 3 years ago

Thank you!!

etienne-lms commented 3 years ago

Hello @janoslim, @ToromanoSTM has created a P-R for stm32_cryp support and plug in drvcrypt framework: https://github.com/OP-TEE/optee_os/pull/4939. You may be interested in this.