Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: clock_control: nrf: Use onoff service #24334

Merged

Conversation

nordic-krch
Copy link
Contributor

@nordic-krch nordic-krch commented Apr 14, 2020

Ported nRF clock control driver to use onoff service.
Clock_control API no longer tracks number of users but only enables or disables clock. It should not be used directly for clocks which have multiple user.

Clock control driver implements onoff service which can be used to track clock users. Clock control driver implements error detection mechanism which detects cases when direct API and onoff API is used for the same clock subsystem.

Nordic specific API (nrf_clock_control.h) provides function for getting onoff manager associated with given clock subsystem: z_nrf_clock_control_get_onoff().

Since low frequency clock is typically used in enable once mode, nordic specific API provides a function for enabling LF clock without a need to maintain onoff client (z_nrf_clock_control_lf_on()). This is currently used by components which uses LF clock: system timer, bluetooth or counter driver.

PR contains port to use onoff service whenever HF clock is used.

PR is based on #23898 thus DNM.

Fixes #20712

@zephyrbot
Copy link
Collaborator

zephyrbot commented Apr 14, 2020

All checks are passing now.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

@nordic-krch nordic-krch force-pushed the clock_control_calib_onoff branch 2 times, most recently from fce8975 to c234836 Compare April 15, 2020 09:45
@pabigot pabigot self-requested a review April 15, 2020 12:09
@nordic-krch
Copy link
Contributor Author

except for the significant degradation in performance.

@cvinayak thanks for checking this. I will take a look what is the onoff overhead and what can be done about it.

@nordic-krch nordic-krch force-pushed the clock_control_calib_onoff branch 2 times, most recently from 3b1552e to bcc244b Compare July 16, 2020 12:31
@nordic-krch
Copy link
Contributor Author

@cvinayak can you take one more look? I've added void z_nrf_clock_bt_ctrl_hf_request(void) and void z_nrf_clock_bt_ctrl_hf_release(void) to be used by the controller.

@anangl can you take another look at the driver implementation as i had to add some hacks&flags to support out of band hf clock requesting. I've also added a test suite where i tried to mix ble and onoff API and stress test it.

Overall, that didn't make clock control driver prettier so I hope that this backdoor was opened for only one guest.

@nordic-krch nordic-krch force-pushed the clock_control_calib_onoff branch from bcc244b to 8e3ce05 Compare July 16, 2020 12:40
LOG_INF("setup done");
}

static void tear_down_instance(const char *dev_name,
clock_control_subsys_t subsys)
{
struct device *dev = device_get_binding(dev_name);
#if DT_NODE_HAS_STATUS(DT_INST(0, nordic_nrf_clock), okay)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if DT_NODE_HAS_STATUS(DT_INST(0, nordic_nrf_clock), okay)
#if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_clock)

err = clock_control_off(dev, subsys);
zassert_equal(0, err, "%s: Unexpected err (%d)", dev_name, err);
zassert_equal(-EBUSY, err, "%s: Unexpected err (%d)", dev_name, err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error code is not specified by the clock_control API, so this check should be performed only for nRF clocks. Same applies to double start/stop test cases.

Comment on lines 25 to 28
/* Used only by HF clock */
#define HF_BT_ACTIVE BIT(8)
#define HF_GENERIC_ACTIVE BIT(9)
#define HF_USER_MASK (HF_BT_ACTIVE | HF_GENERIC_ACTIVE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is only for HFCLK, why not use a dedicated variable for it? Something like:

static atomic_t hfclk_users;

You would then avoid the need of this weird masking of flags with HF_USER_MASK in set_off_state():
https://github.com/zephyrproject-rtos/zephyr/blob/a7fbf5e80f3f702024c261dd63899f368f39028d/drivers/clock_control/nrf_power_clock.c#L158
and in set_starting_state():
https://github.com/zephyrproject-rtos/zephyr/blob/a7fbf5e80f3f702024c261dd63899f368f39028d/drivers/clock_control/nrf_power_clock.c#L173-L175
Also accesses to such variable should be faster than to flags for which you need to get the pointer via get_hf_flags().

* ensure that every z_nrf_clock_bt_ctrl_hf_request matches
* z_nrf_clock_bt_ctrl_hf_release call.
*/
void z_nrf_clock_bt_ctrl_hf_request(void);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should rather use *_bt_ctlr_*, for consistency with other related stuff.


*flags |= HF_GENERIC_ACTIVE;
if (*flags & HF_BT_ACTIVE) {
nrf_clock_is_running(NRF_CLOCK, NRF_CLOCK_DOMAIN_HFCLK, &type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value returned by nrf_clock_is_running() is ignored here, so (void) could help make it clear (and perhaps also avoid warnings from some linters).

@nordic-krch nordic-krch force-pushed the clock_control_calib_onoff branch from 8e3ce05 to f263f18 Compare July 20, 2020 07:50
/* Used only by HF clock */
#define HF_USER_BT BIT(0)
#define HF_USER_GENERIC BIT(1)
#define HF_USER_MASK (HF_USER_BT | HF_USER_GENERIC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

@nordic-krch
Copy link
Contributor Author

@cvinayak can you check bluetooth part?

@cvinayak
Copy link
Contributor

PR:
image

Master 98bcc51:
image

The performance is now comparable.

Copy link
Contributor

@cvinayak cvinayak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for BLE controller changes by analyzing performance changes.

Updated nrf clock control driver to use onoff service for managing
multiple users.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Added shell command to clock driver to get clock status.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Added suite to test clock control with onoff.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Nordic platforms exposes API for controlling HF clock along
onoff API. This API is dedicated to be used by the bluetooth
controller and must not be used elsewhere.

Test suite is validating stability of HF clock control using
onoff API and specialized API.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Adapted driver to use onoff service for clock control.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Adapted driver to clock_control changes (usage of onoff manager).
Since timer is permanenty requesting the clock, it is using API
dedicated for that: z_nrf_clock_control_lf_on().

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Adapted nrfx_rtc counter to request clock through onoff API.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Adapt link layer to use onoff service when controlling clock.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Change to use onoff service when controlling HFXO.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Removed HFXO request. It is a qemu target thus HF clock source
should not matter.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Adapted to use onoff service for clock control

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
nrf_hw_models require extension to support nrf_clock_is_running call.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nRF clock_control_on() is nonblocking