cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

sja1000_platform.c (7600B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * Copyright (C) 2005 Sascha Hauer, Pengutronix
      4 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
      5 */
      6
      7#include <linux/kernel.h>
      8#include <linux/module.h>
      9#include <linux/interrupt.h>
     10#include <linux/netdevice.h>
     11#include <linux/delay.h>
     12#include <linux/pci.h>
     13#include <linux/platform_device.h>
     14#include <linux/irq.h>
     15#include <linux/can/dev.h>
     16#include <linux/can/platform/sja1000.h>
     17#include <linux/io.h>
     18#include <linux/of.h>
     19#include <linux/of_device.h>
     20
     21#include "sja1000.h"
     22
     23#define DRV_NAME "sja1000_platform"
     24#define SP_CAN_CLOCK  (16000000 / 2)
     25
     26MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
     27MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
     28MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
     29MODULE_ALIAS("platform:" DRV_NAME);
     30MODULE_LICENSE("GPL v2");
     31
     32struct sja1000_of_data {
     33	size_t  priv_sz;
     34	int     (*init)(struct sja1000_priv *priv, struct device_node *of);
     35};
     36
     37struct technologic_priv {
     38	spinlock_t      io_lock;
     39};
     40
     41static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
     42{
     43	return ioread8(priv->reg_base + reg);
     44}
     45
     46static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
     47{
     48	iowrite8(val, priv->reg_base + reg);
     49}
     50
     51static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
     52{
     53	return ioread8(priv->reg_base + reg * 2);
     54}
     55
     56static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
     57{
     58	iowrite8(val, priv->reg_base + reg * 2);
     59}
     60
     61static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
     62{
     63	return ioread8(priv->reg_base + reg * 4);
     64}
     65
     66static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
     67{
     68	iowrite8(val, priv->reg_base + reg * 4);
     69}
     70
     71static u8 sp_technologic_read_reg16(const struct sja1000_priv *priv, int reg)
     72{
     73	struct technologic_priv *tp = priv->priv;
     74	unsigned long flags;
     75	u8 val;
     76
     77	spin_lock_irqsave(&tp->io_lock, flags);
     78	iowrite16(reg, priv->reg_base + 0);
     79	val = ioread16(priv->reg_base + 2);
     80	spin_unlock_irqrestore(&tp->io_lock, flags);
     81
     82	return val;
     83}
     84
     85static void sp_technologic_write_reg16(const struct sja1000_priv *priv,
     86				       int reg, u8 val)
     87{
     88	struct technologic_priv *tp = priv->priv;
     89	unsigned long flags;
     90
     91	spin_lock_irqsave(&tp->io_lock, flags);
     92	iowrite16(reg, priv->reg_base + 0);
     93	iowrite16(val, priv->reg_base + 2);
     94	spin_unlock_irqrestore(&tp->io_lock, flags);
     95}
     96
     97static int sp_technologic_init(struct sja1000_priv *priv, struct device_node *of)
     98{
     99	struct technologic_priv *tp = priv->priv;
    100
    101	priv->read_reg = sp_technologic_read_reg16;
    102	priv->write_reg = sp_technologic_write_reg16;
    103	spin_lock_init(&tp->io_lock);
    104
    105	return 0;
    106}
    107
    108static void sp_populate(struct sja1000_priv *priv,
    109			struct sja1000_platform_data *pdata,
    110			unsigned long resource_mem_flags)
    111{
    112	/* The CAN clock frequency is half the oscillator clock frequency */
    113	priv->can.clock.freq = pdata->osc_freq / 2;
    114	priv->ocr = pdata->ocr;
    115	priv->cdr = pdata->cdr;
    116
    117	switch (resource_mem_flags & IORESOURCE_MEM_TYPE_MASK) {
    118	case IORESOURCE_MEM_32BIT:
    119		priv->read_reg = sp_read_reg32;
    120		priv->write_reg = sp_write_reg32;
    121		break;
    122	case IORESOURCE_MEM_16BIT:
    123		priv->read_reg = sp_read_reg16;
    124		priv->write_reg = sp_write_reg16;
    125		break;
    126	case IORESOURCE_MEM_8BIT:
    127	default:
    128		priv->read_reg = sp_read_reg8;
    129		priv->write_reg = sp_write_reg8;
    130		break;
    131	}
    132}
    133
    134static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
    135{
    136	int err;
    137	u32 prop;
    138
    139	err = of_property_read_u32(of, "reg-io-width", &prop);
    140	if (err)
    141		prop = 1; /* 8 bit is default */
    142
    143	switch (prop) {
    144	case 4:
    145		priv->read_reg = sp_read_reg32;
    146		priv->write_reg = sp_write_reg32;
    147		break;
    148	case 2:
    149		priv->read_reg = sp_read_reg16;
    150		priv->write_reg = sp_write_reg16;
    151		break;
    152	case 1:
    153	default:
    154		priv->read_reg = sp_read_reg8;
    155		priv->write_reg = sp_write_reg8;
    156	}
    157
    158	err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
    159	if (!err)
    160		priv->can.clock.freq = prop / 2;
    161	else
    162		priv->can.clock.freq = SP_CAN_CLOCK; /* default */
    163
    164	err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
    165	if (!err)
    166		priv->ocr |= prop & OCR_MODE_MASK;
    167	else
    168		priv->ocr |= OCR_MODE_NORMAL; /* default */
    169
    170	err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
    171	if (!err)
    172		priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
    173	else
    174		priv->ocr |= OCR_TX0_PULLDOWN; /* default */
    175
    176	err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
    177	if (!err && prop) {
    178		u32 divider = priv->can.clock.freq * 2 / prop;
    179
    180		if (divider > 1)
    181			priv->cdr |= divider / 2 - 1;
    182		else
    183			priv->cdr |= CDR_CLKOUT_MASK;
    184	} else {
    185		priv->cdr |= CDR_CLK_OFF; /* default */
    186	}
    187
    188	if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
    189		priv->cdr |= CDR_CBP; /* default */
    190}
    191
    192static struct sja1000_of_data technologic_data = {
    193	.priv_sz = sizeof(struct technologic_priv),
    194	.init = sp_technologic_init,
    195};
    196
    197static const struct of_device_id sp_of_table[] = {
    198	{ .compatible = "nxp,sja1000", .data = NULL, },
    199	{ .compatible = "technologic,sja1000", .data = &technologic_data, },
    200	{ /* sentinel */ },
    201};
    202MODULE_DEVICE_TABLE(of, sp_of_table);
    203
    204static int sp_probe(struct platform_device *pdev)
    205{
    206	int err, irq = 0;
    207	void __iomem *addr;
    208	struct net_device *dev;
    209	struct sja1000_priv *priv;
    210	struct resource *res_mem, *res_irq = NULL;
    211	struct sja1000_platform_data *pdata;
    212	struct device_node *of = pdev->dev.of_node;
    213	const struct of_device_id *of_id;
    214	const struct sja1000_of_data *of_data = NULL;
    215	size_t priv_sz = 0;
    216
    217	pdata = dev_get_platdata(&pdev->dev);
    218	if (!pdata && !of) {
    219		dev_err(&pdev->dev, "No platform data provided!\n");
    220		return -ENODEV;
    221	}
    222
    223	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    224	if (!res_mem)
    225		return -ENODEV;
    226
    227	if (!devm_request_mem_region(&pdev->dev, res_mem->start,
    228				     resource_size(res_mem), DRV_NAME))
    229		return -EBUSY;
    230
    231	addr = devm_ioremap(&pdev->dev, res_mem->start,
    232				    resource_size(res_mem));
    233	if (!addr)
    234		return -ENOMEM;
    235
    236	if (of) {
    237		irq = platform_get_irq(pdev, 0);
    238		if (irq < 0)
    239			return irq;
    240	} else {
    241		res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
    242		if (!res_irq)
    243			return -ENODEV;
    244	}
    245
    246	of_id = of_match_device(sp_of_table, &pdev->dev);
    247	if (of_id && of_id->data) {
    248		of_data = of_id->data;
    249		priv_sz = of_data->priv_sz;
    250	}
    251
    252	dev = alloc_sja1000dev(priv_sz);
    253	if (!dev)
    254		return -ENOMEM;
    255	priv = netdev_priv(dev);
    256
    257	if (res_irq) {
    258		irq = res_irq->start;
    259		priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
    260		if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
    261			priv->irq_flags |= IRQF_SHARED;
    262	} else {
    263		priv->irq_flags = IRQF_SHARED;
    264	}
    265
    266	dev->irq = irq;
    267	priv->reg_base = addr;
    268
    269	if (of) {
    270		sp_populate_of(priv, of);
    271
    272		if (of_data && of_data->init) {
    273			err = of_data->init(priv, of);
    274			if (err)
    275				goto exit_free;
    276		}
    277	} else {
    278		sp_populate(priv, pdata, res_mem->flags);
    279	}
    280
    281	platform_set_drvdata(pdev, dev);
    282	SET_NETDEV_DEV(dev, &pdev->dev);
    283
    284	err = register_sja1000dev(dev);
    285	if (err) {
    286		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
    287			DRV_NAME, err);
    288		goto exit_free;
    289	}
    290
    291	dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
    292		 DRV_NAME, priv->reg_base, dev->irq);
    293	return 0;
    294
    295 exit_free:
    296	free_sja1000dev(dev);
    297	return err;
    298}
    299
    300static int sp_remove(struct platform_device *pdev)
    301{
    302	struct net_device *dev = platform_get_drvdata(pdev);
    303
    304	unregister_sja1000dev(dev);
    305	free_sja1000dev(dev);
    306
    307	return 0;
    308}
    309
    310static struct platform_driver sp_driver = {
    311	.probe = sp_probe,
    312	.remove = sp_remove,
    313	.driver = {
    314		.name = DRV_NAME,
    315		.of_match_table = sp_of_table,
    316	},
    317};
    318
    319module_platform_driver(sp_driver);