Commit c7d9cc1f authored by Marouen Ghodhbane's avatar Marouen Ghodhbane Committed by Benjamin Cabé
Browse files

drivers: counter: tpm: fix init sequence

The current code is enabling the IRQ before calling the TPM_Init()
function which (beside being incorrect by design) opens the door to a
spurious irq to cause the isr function call before the init.  This
corner case can be easily observed for example when running the code
inside a hypervisor/jailhouse where restarting the cell without
previous proper exit (device deinit) can cause a crash (null pointer
dereference) when an IRQ is triggered before the init.

Signed-off-by: default avatarMarouen Ghodhbane <marouen.ghodhbane@nxp.com>
parent 8cff70a9
Loading
Loading
Loading
Loading
+7 −4
Changes for drivers/counter/counter_mcux_tpm.c: 7 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ struct mcux_tpm_config {

	tpm_clock_source_t tpm_clock_source;
	tpm_clock_prescale_t prescale;
	void (*irq_config_func)(void);
};

struct mcux_tpm_data {
@@ -248,6 +249,8 @@ static int mcux_tpm_init(const struct device *dev)
	/* Set the modulo to max value. */
	base->MOD = TPM_MAX_COUNTER_VALUE(base);

	config->irq_config_func();

	return 0;
}

@@ -267,6 +270,7 @@ static DEVICE_API(counter, mcux_tpm_driver_api) = {

#define TPM_DEVICE_INIT_MCUX(n)							\
	static struct mcux_tpm_data mcux_tpm_data_ ## n;			\
	static void mcux_tpm_irq_config_ ## n(void);				\
										\
	static const struct mcux_tpm_config mcux_tpm_config_ ## n = {		\
		DEVICE_MMIO_NAMED_ROM_INIT(tpm_mmio, DT_DRV_INST(n)),		\
@@ -281,11 +285,11 @@ static DEVICE_API(counter, mcux_tpm_driver_api) = {
			.channels = 1,						\
			.flags = COUNTER_CONFIG_INFO_COUNT_UP,			\
		},								\
		.irq_config_func = mcux_tpm_irq_config_ ## n,			\
	};									\
										\
	static int mcux_tpm_## n ##_init(const struct device *dev);		\
	DEVICE_DT_INST_DEFINE(n,						\
			mcux_tpm_## n ##_init,					\
			mcux_tpm_init,						\
			NULL,							\
			&mcux_tpm_data_ ## n,					\
			&mcux_tpm_config_ ## n,					\
@@ -293,13 +297,12 @@ static DEVICE_API(counter, mcux_tpm_driver_api) = {
			CONFIG_COUNTER_INIT_PRIORITY,				\
			&mcux_tpm_driver_api);					\
										\
	static int mcux_tpm_## n ##_init(const struct device *dev)		\
	static void mcux_tpm_irq_config_ ## n(void)				\
	{									\
		IRQ_CONNECT(DT_INST_IRQN(n),					\
			DT_INST_IRQ(n, priority),				\
			mcux_tpm_isr, DEVICE_DT_INST_GET(n), 0);		\
		irq_enable(DT_INST_IRQN(n));					\
		return mcux_tpm_init(dev);					\
	}									\

DT_INST_FOREACH_STATUS_OKAY(TPM_DEVICE_INIT_MCUX)