Skip to content

Commit c1b17ea

Browse files
scosumarckleinebudde
authored andcommitted
can: tcan4x5x: Check size of mram configuration
To reduce debugging effort in case the mram is misconfigured, add this size check of the DT configuration. Currently if the mram configuration doesn't fit into the available MRAM it just overwrites other areas of the MRAM. Signed-off-by: Markus Schneider-Pargmann <[email protected]> Reviewed-by: Michal Kubiak <[email protected]> Link: https://lore.kernel.org/all/[email protected] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent fbe534f commit c1b17ea

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

drivers/net/can/m_can/m_can.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,22 @@ static int register_m_can_dev(struct net_device *dev)
19131913
return register_candev(dev);
19141914
}
19151915

1916+
int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size)
1917+
{
1918+
u32 total_size;
1919+
1920+
total_size = cdev->mcfg[MRAM_TXB].off - cdev->mcfg[MRAM_SIDF].off +
1921+
cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1922+
if (total_size > mram_max_size) {
1923+
dev_err(cdev->dev, "Total size of mram config(%u) exceeds mram(%u)\n",
1924+
total_size, mram_max_size);
1925+
return -EINVAL;
1926+
}
1927+
1928+
return 0;
1929+
}
1930+
EXPORT_SYMBOL_GPL(m_can_check_mram_cfg);
1931+
19161932
static void m_can_of_parse_mram(struct m_can_classdev *cdev,
19171933
const u32 *mram_config_vals)
19181934
{

drivers/net/can/m_can/m_can.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int m_can_class_register(struct m_can_classdev *cdev);
103103
void m_can_class_unregister(struct m_can_classdev *cdev);
104104
int m_can_class_get_clocks(struct m_can_classdev *cdev);
105105
int m_can_init_ram(struct m_can_classdev *priv);
106+
int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size);
106107

107108
int m_can_class_suspend(struct device *dev);
108109
int m_can_class_resume(struct device *dev);

drivers/net/can/m_can/tcan4x5x-core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
TCAN4X5X_MCAN_IR_RF1F)
8181

8282
#define TCAN4X5X_MRAM_START 0x8000
83+
#define TCAN4X5X_MRAM_SIZE 0x800
8384
#define TCAN4X5X_MCAN_OFFSET 0x1000
8485

8586
#define TCAN4X5X_CLEAR_ALL_INT 0xffffffff
@@ -307,6 +308,10 @@ static int tcan4x5x_can_probe(struct spi_device *spi)
307308
if (!mcan_class)
308309
return -ENOMEM;
309310

311+
ret = m_can_check_mram_cfg(mcan_class, TCAN4X5X_MRAM_SIZE);
312+
if (ret)
313+
goto out_m_can_class_free_dev;
314+
310315
priv = cdev_to_priv(mcan_class);
311316

312317
priv->power = devm_regulator_get_optional(&spi->dev, "vsup");

0 commit comments

Comments
 (0)