summaryrefslogtreecommitdiffstats
path: root/drivers/fsi/fsi-scom.c
blob: 3cba0eb645e1480fb92af3a0757f413798ff15c1 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * SCOM FSI Client device driver
 *
 * Copyright (C) IBM Corporation 2016
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/fsi.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/list.h>
#include <linux/idr.h>

#define FSI_ENGID_SCOM		0x5

/* SCOM engine register set */
#define SCOM_DATA0_REG		0x00
#define SCOM_DATA1_REG		0x04
#define SCOM_CMD_REG		0x08

#define SCOM_WRITE_CMD		0x80000000

struct scom_device {
	struct list_head link;
	struct fsi_device *fsi_dev;
	struct miscdevice mdev;
	struct mutex lock;
	char	name[32];
	int idx;
};

#define to_scom_dev(x)		container_of((x), struct scom_device, mdev)

static struct list_head scom_devices;

static DEFINE_IDA(scom_ida);

static int put_scom(struct scom_device *scom_dev, uint64_t value,
			uint32_t addr)
{
	int rc;
	uint32_t data;

	mutex_lock(&scom_dev->lock);

	data = cpu_to_be32((value >> 32) & 0xffffffff);
	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
				sizeof(uint32_t));
	if (rc)
		goto bail;

	data = cpu_to_be32(value & 0xffffffff);
	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
				sizeof(uint32_t));
	if (rc)
		goto bail;

	data = cpu_to_be32(SCOM_WRITE_CMD | addr);
	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
				sizeof(uint32_t));
 bail:
	mutex_unlock(&scom_dev->lock);
	return rc;
}

static int get_scom(struct scom_device *scom_dev, uint64_t *value,
			uint32_t addr)
{
	uint32_t result, data;
	int rc;


	mutex_lock(&scom_dev->lock);
	*value = 0ULL;
	data = cpu_to_be32(addr);
	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
				sizeof(uint32_t));
	if (rc)
		goto bail;

	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &result,
				sizeof(uint32_t));
	if (rc)
		goto bail;

	*value |= (uint64_t)cpu_to_be32(result) << 32;
	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &result,
				sizeof(uint32_t));
	if (rc)
		goto bail;

	*value |= cpu_to_be32(result);

 bail:
	mutex_unlock(&scom_dev->lock);
	return rc;
}

static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
			loff_t *offset)
{
	int rc;
	struct miscdevice *mdev =
				(struct miscdevice *)filep->private_data;
	struct scom_device *scom = to_scom_dev(mdev);
	struct device *dev = &scom->fsi_dev->dev;
	uint64_t val;

	if (len != sizeof(uint64_t))
		return -EINVAL;

	rc = get_scom(scom, &val, *offset);
	if (rc) {
		dev_dbg(dev, "get_scom fail:%d\n", rc);
		return rc;
	}

	rc = copy_to_user(buf, &val, len);
	if (rc)
		dev_dbg(dev, "copy to user failed:%d\n", rc);

	return rc ? rc : len;
}

static ssize_t scom_write(struct file *filep, const char __user *buf,
			size_t len, loff_t *offset)
{
	int rc;
	struct miscdevice *mdev = filep->private_data;
	struct scom_device *scom = to_scom_dev(mdev);
	struct device *dev = &scom->fsi_dev->dev;
	uint64_t val;

	if (len != sizeof(uint64_t))
		return -EINVAL;

	rc = copy_from_user(&val, buf, len);
	if (rc) {
		dev_dbg(dev, "copy from user failed:%d\n", rc);
		return -EINVAL;
	}

	rc = put_scom(scom, val, *offset);
	if (rc) {
		dev_dbg(dev, "put_scom failed with:%d\n", rc);
		return rc;
	}

	return len;
}

static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
{
	switch (whence) {
	case SEEK_CUR:
		break;
	case SEEK_SET:
		file->f_pos = offset;
		break;
	default:
		return -EINVAL;
	}

	return offset;
}

static const struct file_operations scom_fops = {
	.owner	= THIS_MODULE,
	.llseek	= scom_llseek,
	.read	= scom_read,
	.write	= scom_write,
};

static int scom_probe(struct device *dev)
{
	struct fsi_device *fsi_dev = to_fsi_dev(dev);
	struct scom_device *scom;

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

	mutex_init(&scom->lock);
	scom->idx = ida_simple_get(&scom_ida, 1, INT_MAX, GFP_KERNEL);
	snprintf(scom->name, sizeof(scom->name), "scom%d", scom->idx);
	scom->fsi_dev = fsi_dev;
	scom->mdev.minor = MISC_DYNAMIC_MINOR;
	scom->mdev.fops = &scom_fops;
	scom->mdev.name = scom->name;
	scom->mdev.parent = dev;
	list_add(&scom->link, &scom_devices);

	return misc_register(&scom->mdev);
}

static int scom_remove(struct device *dev)
{
	struct scom_device *scom, *scom_tmp;
	struct fsi_device *fsi_dev = to_fsi_dev(dev);

	list_for_each_entry_safe(scom, scom_tmp, &scom_devices, link) {
		if (scom->fsi_dev == fsi_dev) {
			list_del(&scom->link);
			ida_simple_remove(&scom_ida, scom->idx);
			misc_deregister(&scom->mdev);
		}
	}

	return 0;
}

static struct fsi_device_id scom_ids[] = {
	{
		.engine_type = FSI_ENGID_SCOM,
		.version = FSI_VERSION_ANY,
	},
	{ 0 }
};

static struct fsi_driver scom_drv = {
	.id_table = scom_ids,
	.drv = {
		.name = "scom",
		.bus = &fsi_bus_type,
		.probe = scom_probe,
		.remove = scom_remove,
	}
};

static int scom_init(void)
{
	INIT_LIST_HEAD(&scom_devices);
	return fsi_driver_register(&scom_drv);
}

static void scom_exit(void)
{
	struct list_head *pos;
	struct scom_device *scom;

	list_for_each(pos, &scom_devices) {
		scom = list_entry(pos, struct scom_device, link);
		misc_deregister(&scom->mdev);
		devm_kfree(&scom->fsi_dev->dev, scom);
	}
	fsi_driver_unregister(&scom_drv);
}

module_init(scom_init);
module_exit(scom_exit);
MODULE_LICENSE("GPL");