1 // SPDX-License-Identifier: GPL-2.0
2 // IOMapped CAN bus driver for Bosch M_CAN controller
3 // Copyright (C) 2014 Freescale Semiconductor, Inc.
4 // Dong Aisheng <b29396@freescale.com>
6 // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/
8 #include <linux/phy/phy.h>
9 #include <linux/platform_device.h>
13 struct m_can_plat_priv {
14 struct m_can_classdev cdev;
17 void __iomem *mram_base;
20 static inline struct m_can_plat_priv *cdev_to_priv(struct m_can_classdev *cdev)
22 return container_of(cdev, struct m_can_plat_priv, cdev);
25 static u32 iomap_read_reg(struct m_can_classdev *cdev, int reg)
27 struct m_can_plat_priv *priv = cdev_to_priv(cdev);
29 return readl(priv->base + reg);
32 static int iomap_read_fifo(struct m_can_classdev *cdev, int offset, void *val, size_t val_count)
34 struct m_can_plat_priv *priv = cdev_to_priv(cdev);
35 void __iomem *src = priv->mram_base + offset;
38 *(unsigned int *)val = ioread32(src);
46 static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val)
48 struct m_can_plat_priv *priv = cdev_to_priv(cdev);
50 writel(val, priv->base + reg);
55 static int iomap_write_fifo(struct m_can_classdev *cdev, int offset,
56 const void *val, size_t val_count)
58 struct m_can_plat_priv *priv = cdev_to_priv(cdev);
59 void __iomem *dst = priv->mram_base + offset;
62 iowrite32(*(unsigned int *)val, dst);
70 static struct m_can_ops m_can_plat_ops = {
71 .read_reg = iomap_read_reg,
72 .write_reg = iomap_write_reg,
73 .write_fifo = iomap_write_fifo,
74 .read_fifo = iomap_read_fifo,
77 static int m_can_plat_probe(struct platform_device *pdev)
79 struct m_can_classdev *mcan_class;
80 struct m_can_plat_priv *priv;
83 void __iomem *mram_addr;
84 struct phy *transceiver;
87 mcan_class = m_can_class_allocate_dev(&pdev->dev,
88 sizeof(struct m_can_plat_priv));
92 priv = cdev_to_priv(mcan_class);
94 ret = m_can_class_get_clocks(mcan_class);
98 addr = devm_platform_ioremap_resource_byname(pdev, "m_can");
99 irq = platform_get_irq_byname(pdev, "int0");
100 if (IS_ERR(addr) || irq < 0) {
105 /* message ram could be shared */
106 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "message_ram");
112 mram_addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
118 transceiver = devm_phy_optional_get(&pdev->dev, NULL);
119 if (IS_ERR(transceiver)) {
120 ret = PTR_ERR(transceiver);
121 dev_err_probe(&pdev->dev, ret, "failed to get phy\n");
126 mcan_class->can.bitrate_max = transceiver->attrs.max_link_rate;
129 priv->mram_base = mram_addr;
131 mcan_class->net->irq = irq;
132 mcan_class->pm_clock_support = 1;
133 mcan_class->can.clock.freq = clk_get_rate(mcan_class->cclk);
134 mcan_class->dev = &pdev->dev;
135 mcan_class->transceiver = transceiver;
137 mcan_class->ops = &m_can_plat_ops;
139 mcan_class->is_peripheral = false;
141 platform_set_drvdata(pdev, mcan_class);
143 pm_runtime_enable(mcan_class->dev);
144 ret = m_can_class_register(mcan_class);
146 goto out_runtime_disable;
151 pm_runtime_disable(mcan_class->dev);
153 m_can_class_free_dev(mcan_class->net);
157 static __maybe_unused int m_can_suspend(struct device *dev)
159 return m_can_class_suspend(dev);
162 static __maybe_unused int m_can_resume(struct device *dev)
164 return m_can_class_resume(dev);
167 static int m_can_plat_remove(struct platform_device *pdev)
169 struct m_can_plat_priv *priv = platform_get_drvdata(pdev);
170 struct m_can_classdev *mcan_class = &priv->cdev;
172 m_can_class_unregister(mcan_class);
174 m_can_class_free_dev(mcan_class->net);
179 static int __maybe_unused m_can_runtime_suspend(struct device *dev)
181 struct m_can_plat_priv *priv = dev_get_drvdata(dev);
182 struct m_can_classdev *mcan_class = &priv->cdev;
184 clk_disable_unprepare(mcan_class->cclk);
185 clk_disable_unprepare(mcan_class->hclk);
190 static int __maybe_unused m_can_runtime_resume(struct device *dev)
192 struct m_can_plat_priv *priv = dev_get_drvdata(dev);
193 struct m_can_classdev *mcan_class = &priv->cdev;
196 err = clk_prepare_enable(mcan_class->hclk);
200 err = clk_prepare_enable(mcan_class->cclk);
202 clk_disable_unprepare(mcan_class->hclk);
207 static const struct dev_pm_ops m_can_pmops = {
208 SET_RUNTIME_PM_OPS(m_can_runtime_suspend,
209 m_can_runtime_resume, NULL)
210 SET_SYSTEM_SLEEP_PM_OPS(m_can_suspend, m_can_resume)
213 static const struct of_device_id m_can_of_table[] = {
214 { .compatible = "bosch,m_can", .data = NULL },
217 MODULE_DEVICE_TABLE(of, m_can_of_table);
219 static struct platform_driver m_can_plat_driver = {
221 .name = KBUILD_MODNAME,
222 .of_match_table = m_can_of_table,
225 .probe = m_can_plat_probe,
226 .remove = m_can_plat_remove,
229 module_platform_driver(m_can_plat_driver);
231 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
232 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
233 MODULE_LICENSE("GPL v2");
234 MODULE_DESCRIPTION("M_CAN driver for IO Mapped Bosch controllers");