aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/mrvl-loki.c
blob: 51609284c49319bafa2feb95a92da3e3ec0f6b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: GPL-2.0
/* Marvell Loki driver
 *
 * Copyright (C) 2018 Marvell International Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/pci.h>

#define PCI_DEVICE_ID_BPHY	0xA089

#define PSM_GPINT0_SUM_W1C	0x0ULL
#define PSM_GPINT0_SUM_W1S	0x40ULL
#define PSM_GPINT0_ENA_W1C	0x80ULL
#define PSM_GPINT0_ENA_W1S	0xC0ULL

#define CPRI_INT_MASK		0x1F

#define CPRI_IP_AXI_INT_STATUS(a)	(0x100ULL | a << 10)
#define CPRI_IP_AXI_INT(a)		(0x108ULL | a << 10)

struct mrvl_loki {
	struct pci_dev *pdev;
	struct msix_entry msix_ent;
	void __iomem *psm_gpint;
	void __iomem *cpri_axi[3];
	int intr_num;

	int (*irq_cb)(uint32_t instance, uint32_t pss_int);
};

struct mrvl_loki *g_ml;

int mrvl_loki_register_irq_cb(int (*func)(uint32_t instance, uint32_t pss_int))
{
	if (!g_ml) {
		pr_err("Error: mrvl_loki is NULL\n");
		return -ENOENT;
	}

	if (func)
		g_ml->irq_cb = func;
	else
		return -EIO;

	return 0;
}
EXPORT_SYMBOL(mrvl_loki_register_irq_cb);

void mrvl_loki_unregister_irq_cb(void)
{
	g_ml->irq_cb = NULL;
}
EXPORT_SYMBOL(mrvl_loki_unregister_irq_cb);

static irqreturn_t mrvl_loki_handler(int irq, void *dev)
{
	struct mrvl_loki *ml =
		platform_get_drvdata((struct platform_device *)dev);
	uint32_t instance, pss_int, val;
	uint8_t cpri, mac;
	int ret;

	val = readq_relaxed(ml->psm_gpint + PSM_GPINT0_SUM_W1C) & CPRI_INT_MASK;

	instance = ffs(val) - 1;
	cpri = instance / 2;
	mac = instance % 2;
	pss_int = (u32)readq_relaxed(ml->cpri_axi[cpri] +
				CPRI_IP_AXI_INT_STATUS(mac));
	if (ml->irq_cb) {
		ret = ml->irq_cb(instance, pss_int);
		if (ret < 0)
			dev_err(dev, "Error %d from loki CPRI callback\n", ret);
	}

	writeq_relaxed(val, ml->psm_gpint + PSM_GPINT0_SUM_W1C);
	writeq_relaxed((u64)pss_int, ml->cpri_axi[cpri] + CPRI_IP_AXI_INT(mac));

	return IRQ_HANDLED;
};

static inline void msix_enable_ctrl(struct pci_dev *dev)
{
	u16 control;

	pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
	control |= PCI_MSIX_FLAGS_ENABLE;
	pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
}

static int mrvl_loki_probe(struct platform_device *pdev)
{
	struct mrvl_loki *ml;
	struct device *dev = &pdev->dev;
	struct pci_dev *bphy_pdev;
	struct resource *res;
	int ret = 0;

	ml = devm_kzalloc(dev, sizeof(*ml), GFP_KERNEL);
	if (!ml)
		return -ENOMEM;

	platform_set_drvdata(pdev, ml);

	/*
	 * BPHY is a PCI device and the kernel resets the MSIXEN bit during
	 * enumeration. So enable it back for interrupts to be generated.
	 */
	bphy_pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_BPHY,
				  NULL);
	if (!bphy_pdev) {
		dev_err(dev, "Couldn't find BPHY PCI device %x\n",
			PCI_DEVICE_ID_BPHY);
		ret = -ENODEV;
		goto err;
	}

	ml->pdev = bphy_pdev;
	ml->msix_ent.entry = 0;

	msix_enable_ctrl(bphy_pdev);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	ml->psm_gpint = ioremap(res->start, resource_size(res));
	if (IS_ERR(ml->psm_gpint)) {
		dev_err(dev, "error in ioremap PSM GPINT\n");
		return PTR_ERR(ml->psm_gpint);
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	ml->cpri_axi[0] = ioremap(res->start, resource_size(res));
	if (IS_ERR(ml->cpri_axi[0])) {
		dev_err(dev, "error in ioremap CPRI AXI0\n");
		return PTR_ERR(ml->cpri_axi[0]);
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
	ml->cpri_axi[1] = ioremap(res->start, resource_size(res));
	if (IS_ERR(ml->cpri_axi[1])) {
		dev_err(dev, "error in ioremap CPRI AXI1\n");
		return PTR_ERR(ml->cpri_axi[1]);
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
	ml->cpri_axi[2] = ioremap(res->start, resource_size(res));
	if (IS_ERR(ml->cpri_axi[2])) {
		dev_err(dev, "error in ioremap CPRI AXI2\n");
		return PTR_ERR(ml->cpri_axi[2]);
	}

	/* register interrupt */
	ml->intr_num = irq_of_parse_and_map(dev->of_node, 0);

	if (request_irq(ml->intr_num, mrvl_loki_handler, 0,
			"mrvl loki handler", pdev)) {
		dev_err(dev, "failed to register irq handler\n");
		ret = -ENOMEM;
		goto err;
	}

	g_ml = ml;
	dev_info(dev, "Registered interrupt handler for %d\n", ml->intr_num);

	return 0;

err:
	devm_kfree(&pdev->dev, ml);
	return ret;
}

static int mrvl_loki_remove(struct platform_device *pdev)
{
	struct mrvl_loki *ml = platform_get_drvdata(pdev);

	free_irq(ml->intr_num, pdev);
	devm_kfree(&pdev->dev, ml);

	return 0;
}

static const struct of_device_id mrvl_loki_of_match[] = {
	{ .compatible = "marvell,loki", },
	{},
};
MODULE_DEVICE_TABLE(of, mrvl_loki_of_match);

static struct platform_driver mrvl_loki_driver = {
	.probe = mrvl_loki_probe,
	.remove = mrvl_loki_remove,
	.driver = {
		.name = "mrvl-loki",
		.of_match_table = of_match_ptr(mrvl_loki_of_match),
	},
};

module_platform_driver(mrvl_loki_driver);

MODULE_DESCRIPTION("Marvell Loki Driver");
MODULE_AUTHOR("Radha Mohan Chintakuntla");
MODULE_LICENSE("GPL v2");