1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Arcx Anybus-S Controller driver
4  *
5  * Copyright (C) 2018 Arcx Inc
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/delay.h>
17 #include <linux/idr.h>
18 #include <linux/mutex.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regmap.h>
22 
23 /* move to <linux/anybuss-controller.h> when taking this out of staging */
24 #include "anybuss-controller.h"
25 
26 #define CPLD_STATUS1		0x80
27 #define CPLD_CONTROL		0x80
28 #define CPLD_CONTROL_CRST	0x40
29 #define CPLD_CONTROL_RST1	0x04
30 #define CPLD_CONTROL_RST2	0x80
31 #define CPLD_STATUS1_AB		0x02
32 #define CPLD_STATUS1_CAN_POWER	0x01
33 #define CPLD_DESIGN_LO		0x81
34 #define CPLD_DESIGN_HI		0x82
35 #define CPLD_CAP		0x83
36 #define CPLD_CAP_COMPAT		0x01
37 #define CPLD_CAP_SEP_RESETS	0x02
38 
39 struct controller_priv {
40 	struct device *class_dev;
41 	bool common_reset;
42 	struct gpio_desc *reset_gpiod;
43 	void __iomem *cpld_base;
44 	struct mutex ctrl_lock; /* protects CONTROL register */
45 	u8 control_reg;
46 	char version[3];
47 	u16 design_no;
48 };
49 
50 static void do_reset(struct controller_priv *cd, u8 rst_bit, bool reset)
51 {
52 	mutex_lock(&cd->ctrl_lock);
53 	/*
54 	 * CPLD_CONTROL is write-only, so cache its value in
55 	 * cd->control_reg
56 	 */
57 	if (reset)
58 		cd->control_reg &= ~rst_bit;
59 	else
60 		cd->control_reg |= rst_bit;
61 	writeb(cd->control_reg, cd->cpld_base + CPLD_CONTROL);
62 	/*
63 	 * h/w work-around:
64 	 * the hardware is 'too fast', so a reset followed by an immediate
65 	 * not-reset will _not_ change the anybus reset line in any way,
66 	 * losing the reset. to prevent this from happening, introduce
67 	 * a minimum reset duration.
68 	 * Verified minimum safe duration required using a scope
69 	 * on 14-June-2018: 100 us.
70 	 */
71 	if (reset)
72 		usleep_range(100, 200);
73 	mutex_unlock(&cd->ctrl_lock);
74 }
75 
76 static int anybuss_reset(struct controller_priv *cd,
77 			 unsigned long id, bool reset)
78 {
79 	if (id >= 2)
80 		return -EINVAL;
81 	if (cd->common_reset)
82 		do_reset(cd, CPLD_CONTROL_CRST, reset);
83 	else
84 		do_reset(cd, id ? CPLD_CONTROL_RST2 : CPLD_CONTROL_RST1, reset);
85 	return 0;
86 }
87 
88 static void export_reset_0(struct device *dev, bool assert)
89 {
90 	struct controller_priv *cd = dev_get_drvdata(dev);
91 
92 	anybuss_reset(cd, 0, assert);
93 }
94 
95 static void export_reset_1(struct device *dev, bool assert)
96 {
97 	struct controller_priv *cd = dev_get_drvdata(dev);
98 
99 	anybuss_reset(cd, 1, assert);
100 }
101 
102 /*
103  * parallel bus limitation:
104  *
105  * the anybus is 8-bit wide. we can't assume that the hardware will translate
106  * word accesses on the parallel bus to multiple byte-accesses on the anybus.
107  *
108  * the imx WEIM bus does not provide this type of translation.
109  *
110  * to be safe, we will limit parallel bus accesses to a single byte
111  * at a time for now.
112  */
113 
114 static const struct regmap_config arcx_regmap_cfg = {
115 	.reg_bits = 16,
116 	.val_bits = 8,
117 	.max_register = 0x7ff,
118 	.use_single_read = true,
119 	.use_single_write = true,
120 	/*
121 	 * single-byte parallel bus accesses are atomic, so don't
122 	 * require any synchronization.
123 	 */
124 	.disable_locking = true,
125 };
126 
127 static struct regmap *create_parallel_regmap(struct platform_device *pdev,
128 					     int idx)
129 {
130 	void __iomem *base;
131 	struct device *dev = &pdev->dev;
132 
133 	base = devm_platform_ioremap_resource(pdev, idx + 1);
134 	if (IS_ERR(base))
135 		return ERR_CAST(base);
136 	return devm_regmap_init_mmio(dev, base, &arcx_regmap_cfg);
137 }
138 
139 static struct anybuss_host *
140 create_anybus_host(struct platform_device *pdev, int idx)
141 {
142 	struct anybuss_ops ops = {};
143 
144 	switch (idx) {
145 	case 0:
146 		ops.reset = export_reset_0;
147 		break;
148 	case 1:
149 		ops.reset = export_reset_1;
150 		break;
151 	default:
152 		return ERR_PTR(-EINVAL);
153 	}
154 	ops.host_idx = idx;
155 	ops.regmap = create_parallel_regmap(pdev, idx);
156 	if (IS_ERR(ops.regmap))
157 		return ERR_CAST(ops.regmap);
158 	ops.irq = platform_get_irq(pdev, idx);
159 	if (ops.irq < 0)
160 		return ERR_PTR(ops.irq);
161 	return devm_anybuss_host_common_probe(&pdev->dev, &ops);
162 }
163 
164 static ssize_t version_show(struct device *dev,
165 			    struct device_attribute *attr, char *buf)
166 {
167 	struct controller_priv *cd = dev_get_drvdata(dev);
168 
169 	return sprintf(buf, "%s\n", cd->version);
170 }
171 static DEVICE_ATTR_RO(version);
172 
173 static ssize_t design_number_show(struct device *dev,
174 				  struct device_attribute *attr, char *buf)
175 {
176 	struct controller_priv *cd = dev_get_drvdata(dev);
177 
178 	return sprintf(buf, "%d\n", cd->design_no);
179 }
180 static DEVICE_ATTR_RO(design_number);
181 
182 static struct attribute *controller_attributes[] = {
183 	&dev_attr_version.attr,
184 	&dev_attr_design_number.attr,
185 	NULL,
186 };
187 
188 static const struct attribute_group controller_attribute_group = {
189 	.attrs = controller_attributes,
190 };
191 
192 static const struct attribute_group *controller_attribute_groups[] = {
193 	&controller_attribute_group,
194 	NULL,
195 };
196 
197 static void controller_device_release(struct device *dev)
198 {
199 	kfree(dev);
200 }
201 
202 static int can_power_is_enabled(struct regulator_dev *rdev)
203 {
204 	struct controller_priv *cd = rdev_get_drvdata(rdev);
205 
206 	return !(readb(cd->cpld_base + CPLD_STATUS1) & CPLD_STATUS1_CAN_POWER);
207 }
208 
209 static const struct regulator_ops can_power_ops = {
210 	.is_enabled = can_power_is_enabled,
211 };
212 
213 static const struct regulator_desc can_power_desc = {
214 	.name = "regulator-can-power",
215 	.id = -1,
216 	.type = REGULATOR_VOLTAGE,
217 	.owner = THIS_MODULE,
218 	.ops = &can_power_ops,
219 };
220 
221 static const struct class controller_class = {
222 	.name = "arcx_anybus_controller",
223 };
224 
225 static DEFINE_IDA(controller_index_ida);
226 
227 static int controller_probe(struct platform_device *pdev)
228 {
229 	struct controller_priv *cd;
230 	struct device *dev = &pdev->dev;
231 	struct regulator_config config = { };
232 	struct regulator_dev *regulator;
233 	int err, id;
234 	struct anybuss_host *host;
235 	u8 status1, cap;
236 
237 	cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL);
238 	if (!cd)
239 		return -ENOMEM;
240 	dev_set_drvdata(dev, cd);
241 	mutex_init(&cd->ctrl_lock);
242 	cd->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
243 	if (IS_ERR(cd->reset_gpiod))
244 		return PTR_ERR(cd->reset_gpiod);
245 
246 	/* CPLD control memory, sits at index 0 */
247 	cd->cpld_base = devm_platform_ioremap_resource(pdev, 0);
248 	if (IS_ERR(cd->cpld_base)) {
249 		dev_err(dev,
250 			"failed to map cpld base address\n");
251 		err = PTR_ERR(cd->cpld_base);
252 		goto out_reset;
253 	}
254 
255 	/* identify cpld */
256 	status1 = readb(cd->cpld_base + CPLD_STATUS1);
257 	cd->design_no = (readb(cd->cpld_base + CPLD_DESIGN_HI) << 8) |
258 				readb(cd->cpld_base + CPLD_DESIGN_LO);
259 	snprintf(cd->version, sizeof(cd->version), "%c%d",
260 		 'A' + ((status1 >> 5) & 0x7),
261 		 (status1 >> 2) & 0x7);
262 	dev_info(dev, "design number %d, revision %s\n",
263 		 cd->design_no,
264 		cd->version);
265 	cap = readb(cd->cpld_base + CPLD_CAP);
266 	if (!(cap & CPLD_CAP_COMPAT)) {
267 		dev_err(dev, "unsupported controller [cap=0x%02X]", cap);
268 		err = -ENODEV;
269 		goto out_reset;
270 	}
271 
272 	if (status1 & CPLD_STATUS1_AB) {
273 		dev_info(dev, "has anybus-S slot(s)");
274 		cd->common_reset = !(cap & CPLD_CAP_SEP_RESETS);
275 		dev_info(dev, "supports %s", cd->common_reset ?
276 			"a common reset" : "separate resets");
277 		for (id = 0; id < 2; id++) {
278 			host = create_anybus_host(pdev, id);
279 			if (!IS_ERR(host))
280 				continue;
281 			err = PTR_ERR(host);
282 			/* -ENODEV is fine, it just means no card detected */
283 			if (err != -ENODEV)
284 				goto out_reset;
285 		}
286 	}
287 
288 	id = ida_alloc(&controller_index_ida, GFP_KERNEL);
289 	if (id < 0) {
290 		err = id;
291 		goto out_reset;
292 	}
293 	/* export can power readout as a regulator */
294 	config.dev = dev;
295 	config.driver_data = cd;
296 	regulator = devm_regulator_register(dev, &can_power_desc, &config);
297 	if (IS_ERR(regulator)) {
298 		err = PTR_ERR(regulator);
299 		goto out_ida;
300 	}
301 	/* make controller info visible to userspace */
302 	cd->class_dev = kzalloc(sizeof(*cd->class_dev), GFP_KERNEL);
303 	if (!cd->class_dev) {
304 		err = -ENOMEM;
305 		goto out_ida;
306 	}
307 	cd->class_dev->class = &controller_class;
308 	cd->class_dev->groups = controller_attribute_groups;
309 	cd->class_dev->parent = dev;
310 	cd->class_dev->id = id;
311 	cd->class_dev->release = controller_device_release;
312 	dev_set_name(cd->class_dev, "%d", cd->class_dev->id);
313 	dev_set_drvdata(cd->class_dev, cd);
314 	err = device_register(cd->class_dev);
315 	if (err)
316 		goto out_dev;
317 	return 0;
318 out_dev:
319 	put_device(cd->class_dev);
320 out_ida:
321 	ida_free(&controller_index_ida, id);
322 out_reset:
323 	gpiod_set_value_cansleep(cd->reset_gpiod, 1);
324 	return err;
325 }
326 
327 static void controller_remove(struct platform_device *pdev)
328 {
329 	struct controller_priv *cd = platform_get_drvdata(pdev);
330 	int id = cd->class_dev->id;
331 
332 	device_unregister(cd->class_dev);
333 	ida_free(&controller_index_ida, id);
334 	gpiod_set_value_cansleep(cd->reset_gpiod, 1);
335 }
336 
337 static const struct of_device_id controller_of_match[] = {
338 	{ .compatible = "arcx,anybus-controller" },
339 	{ }
340 };
341 
342 MODULE_DEVICE_TABLE(of, controller_of_match);
343 
344 static struct platform_driver controller_driver = {
345 	.probe = controller_probe,
346 	.remove_new = controller_remove,
347 	.driver		= {
348 		.name   = "arcx-anybus-controller",
349 		.of_match_table	= controller_of_match,
350 	},
351 };
352 
353 static int __init controller_init(void)
354 {
355 	int err;
356 
357 	err = class_register(&controller_class);
358 	if (err)
359 		return err;
360 	err = platform_driver_register(&controller_driver);
361 	if (err)
362 		class_unregister(&controller_class);
363 
364 	return err;
365 }
366 
367 static void __exit controller_exit(void)
368 {
369 	platform_driver_unregister(&controller_driver);
370 	class_unregister(&controller_class);
371 	ida_destroy(&controller_index_ida);
372 }
373 
374 module_init(controller_init);
375 module_exit(controller_exit);
376 
377 MODULE_DESCRIPTION("Arcx Anybus-S Controller driver");
378 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
379 MODULE_LICENSE("GPL v2");
380