xref: /openbmc/linux/drivers/slimbus/core.c (revision 6d99a79c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2017, The Linux Foundation
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/idr.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/slimbus.h>
15 #include "slimbus.h"
16 
17 static DEFINE_IDA(ctrl_ida);
18 
19 static const struct slim_device_id *slim_match(const struct slim_device_id *id,
20 					       const struct slim_device *sbdev)
21 {
22 	while (id->manf_id != 0 || id->prod_code != 0) {
23 		if (id->manf_id == sbdev->e_addr.manf_id &&
24 		    id->prod_code == sbdev->e_addr.prod_code)
25 			return id;
26 		id++;
27 	}
28 	return NULL;
29 }
30 
31 static int slim_device_match(struct device *dev, struct device_driver *drv)
32 {
33 	struct slim_device *sbdev = to_slim_device(dev);
34 	struct slim_driver *sbdrv = to_slim_driver(drv);
35 
36 	/* Attempt an OF style match first */
37 	if (of_driver_match_device(dev, drv))
38 		return 1;
39 
40 	return !!slim_match(sbdrv->id_table, sbdev);
41 }
42 
43 static int slim_device_probe(struct device *dev)
44 {
45 	struct slim_device	*sbdev = to_slim_device(dev);
46 	struct slim_driver	*sbdrv = to_slim_driver(dev->driver);
47 	int ret;
48 
49 	ret = sbdrv->probe(sbdev);
50 	if (ret)
51 		return ret;
52 
53 	/* try getting the logical address after probe */
54 	ret = slim_get_logical_addr(sbdev);
55 	if (!ret) {
56 		if (sbdrv->device_status)
57 			sbdrv->device_status(sbdev, sbdev->status);
58 	} else {
59 		dev_err(&sbdev->dev, "Failed to get logical address\n");
60 		ret = -EPROBE_DEFER;
61 	}
62 
63 	return ret;
64 }
65 
66 static int slim_device_remove(struct device *dev)
67 {
68 	struct slim_device *sbdev = to_slim_device(dev);
69 	struct slim_driver *sbdrv;
70 
71 	if (dev->driver) {
72 		sbdrv = to_slim_driver(dev->driver);
73 		if (sbdrv->remove)
74 			sbdrv->remove(sbdev);
75 	}
76 
77 	return 0;
78 }
79 
80 static int slim_device_uevent(struct device *dev, struct kobj_uevent_env *env)
81 {
82 	struct slim_device *sbdev = to_slim_device(dev);
83 	int ret;
84 
85 	ret = of_device_uevent_modalias(dev, env);
86 	if (ret != -ENODEV)
87 		return ret;
88 
89 	return add_uevent_var(env, "MODALIAS=slim:%s", dev_name(&sbdev->dev));
90 }
91 
92 struct bus_type slimbus_bus = {
93 	.name		= "slimbus",
94 	.match		= slim_device_match,
95 	.probe		= slim_device_probe,
96 	.remove		= slim_device_remove,
97 	.uevent		= slim_device_uevent,
98 };
99 EXPORT_SYMBOL_GPL(slimbus_bus);
100 
101 /*
102  * __slim_driver_register() - Client driver registration with SLIMbus
103  *
104  * @drv:Client driver to be associated with client-device.
105  * @owner: owning module/driver
106  *
107  * This API will register the client driver with the SLIMbus
108  * It is called from the driver's module-init function.
109  */
110 int __slim_driver_register(struct slim_driver *drv, struct module *owner)
111 {
112 	/* ID table and probe are mandatory */
113 	if (!(drv->driver.of_match_table || drv->id_table) || !drv->probe)
114 		return -EINVAL;
115 
116 	drv->driver.bus = &slimbus_bus;
117 	drv->driver.owner = owner;
118 
119 	return driver_register(&drv->driver);
120 }
121 EXPORT_SYMBOL_GPL(__slim_driver_register);
122 
123 /*
124  * slim_driver_unregister() - Undo effect of slim_driver_register
125  *
126  * @drv: Client driver to be unregistered
127  */
128 void slim_driver_unregister(struct slim_driver *drv)
129 {
130 	driver_unregister(&drv->driver);
131 }
132 EXPORT_SYMBOL_GPL(slim_driver_unregister);
133 
134 static void slim_dev_release(struct device *dev)
135 {
136 	struct slim_device *sbdev = to_slim_device(dev);
137 
138 	kfree(sbdev);
139 }
140 
141 static int slim_add_device(struct slim_controller *ctrl,
142 			   struct slim_device *sbdev,
143 			   struct device_node *node)
144 {
145 	sbdev->dev.bus = &slimbus_bus;
146 	sbdev->dev.parent = ctrl->dev;
147 	sbdev->dev.release = slim_dev_release;
148 	sbdev->dev.driver = NULL;
149 	sbdev->ctrl = ctrl;
150 	INIT_LIST_HEAD(&sbdev->stream_list);
151 	spin_lock_init(&sbdev->stream_list_lock);
152 
153 	if (node)
154 		sbdev->dev.of_node = of_node_get(node);
155 
156 	dev_set_name(&sbdev->dev, "%x:%x:%x:%x",
157 				  sbdev->e_addr.manf_id,
158 				  sbdev->e_addr.prod_code,
159 				  sbdev->e_addr.dev_index,
160 				  sbdev->e_addr.instance);
161 
162 	return device_register(&sbdev->dev);
163 }
164 
165 static struct slim_device *slim_alloc_device(struct slim_controller *ctrl,
166 					     struct slim_eaddr *eaddr,
167 					     struct device_node *node)
168 {
169 	struct slim_device *sbdev;
170 	int ret;
171 
172 	sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL);
173 	if (!sbdev)
174 		return NULL;
175 
176 	sbdev->e_addr = *eaddr;
177 	ret = slim_add_device(ctrl, sbdev, node);
178 	if (ret) {
179 		put_device(&sbdev->dev);
180 		return NULL;
181 	}
182 
183 	return sbdev;
184 }
185 
186 static void of_register_slim_devices(struct slim_controller *ctrl)
187 {
188 	struct device *dev = ctrl->dev;
189 	struct device_node *node;
190 
191 	if (!ctrl->dev->of_node)
192 		return;
193 
194 	for_each_child_of_node(ctrl->dev->of_node, node) {
195 		struct slim_device *sbdev;
196 		struct slim_eaddr e_addr;
197 		const char *compat = NULL;
198 		int reg[2], ret;
199 		int manf_id, prod_code;
200 
201 		compat = of_get_property(node, "compatible", NULL);
202 		if (!compat)
203 			continue;
204 
205 		ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code);
206 		if (ret != 2) {
207 			dev_err(dev, "Manf ID & Product code not found %s\n",
208 				compat);
209 			continue;
210 		}
211 
212 		ret = of_property_read_u32_array(node, "reg", reg, 2);
213 		if (ret) {
214 			dev_err(dev, "Device and Instance id not found:%d\n",
215 				ret);
216 			continue;
217 		}
218 
219 		e_addr.dev_index = reg[0];
220 		e_addr.instance = reg[1];
221 		e_addr.manf_id = manf_id;
222 		e_addr.prod_code = prod_code;
223 
224 		sbdev = slim_alloc_device(ctrl, &e_addr, node);
225 		if (!sbdev)
226 			continue;
227 	}
228 }
229 
230 /*
231  * slim_register_controller() - Controller bring-up and registration.
232  *
233  * @ctrl: Controller to be registered.
234  *
235  * A controller is registered with the framework using this API.
236  * If devices on a controller were registered before controller,
237  * this will make sure that they get probed when controller is up
238  */
239 int slim_register_controller(struct slim_controller *ctrl)
240 {
241 	int id;
242 
243 	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
244 	if (id < 0)
245 		return id;
246 
247 	ctrl->id = id;
248 
249 	if (!ctrl->min_cg)
250 		ctrl->min_cg = SLIM_MIN_CLK_GEAR;
251 	if (!ctrl->max_cg)
252 		ctrl->max_cg = SLIM_MAX_CLK_GEAR;
253 
254 	ida_init(&ctrl->laddr_ida);
255 	idr_init(&ctrl->tid_idr);
256 	mutex_init(&ctrl->lock);
257 	mutex_init(&ctrl->sched.m_reconf);
258 	init_completion(&ctrl->sched.pause_comp);
259 
260 	dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n",
261 		ctrl->name, ctrl->dev);
262 
263 	of_register_slim_devices(ctrl);
264 
265 	return 0;
266 }
267 EXPORT_SYMBOL_GPL(slim_register_controller);
268 
269 /* slim_remove_device: Remove the effect of slim_add_device() */
270 static void slim_remove_device(struct slim_device *sbdev)
271 {
272 	device_unregister(&sbdev->dev);
273 }
274 
275 static int slim_ctrl_remove_device(struct device *dev, void *null)
276 {
277 	slim_remove_device(to_slim_device(dev));
278 	return 0;
279 }
280 
281 /**
282  * slim_unregister_controller() - Controller tear-down.
283  *
284  * @ctrl: Controller to tear-down.
285  */
286 int slim_unregister_controller(struct slim_controller *ctrl)
287 {
288 	/* Remove all clients */
289 	device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device);
290 	/* Enter Clock Pause */
291 	slim_ctrl_clk_pause(ctrl, false, 0);
292 	ida_simple_remove(&ctrl_ida, ctrl->id);
293 
294 	return 0;
295 }
296 EXPORT_SYMBOL_GPL(slim_unregister_controller);
297 
298 static void slim_device_update_status(struct slim_device *sbdev,
299 				      enum slim_device_status status)
300 {
301 	struct slim_driver *sbdrv;
302 
303 	if (sbdev->status == status)
304 		return;
305 
306 	sbdev->status = status;
307 	if (!sbdev->dev.driver)
308 		return;
309 
310 	sbdrv = to_slim_driver(sbdev->dev.driver);
311 	if (sbdrv->device_status)
312 		sbdrv->device_status(sbdev, sbdev->status);
313 }
314 
315 /**
316  * slim_report_absent() - Controller calls this function when a device
317  *	reports absent, OR when the device cannot be communicated with
318  *
319  * @sbdev: Device that cannot be reached, or sent report absent
320  */
321 void slim_report_absent(struct slim_device *sbdev)
322 {
323 	struct slim_controller *ctrl = sbdev->ctrl;
324 
325 	if (!ctrl)
326 		return;
327 
328 	/* invalidate logical addresses */
329 	mutex_lock(&ctrl->lock);
330 	sbdev->is_laddr_valid = false;
331 	mutex_unlock(&ctrl->lock);
332 
333 	ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr);
334 	slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN);
335 }
336 EXPORT_SYMBOL_GPL(slim_report_absent);
337 
338 static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b)
339 {
340 	return (a->manf_id == b->manf_id &&
341 		a->prod_code == b->prod_code &&
342 		a->dev_index == b->dev_index &&
343 		a->instance == b->instance);
344 }
345 
346 static int slim_match_dev(struct device *dev, void *data)
347 {
348 	struct slim_eaddr *e_addr = data;
349 	struct slim_device *sbdev = to_slim_device(dev);
350 
351 	return slim_eaddr_equal(&sbdev->e_addr, e_addr);
352 }
353 
354 static struct slim_device *find_slim_device(struct slim_controller *ctrl,
355 					    struct slim_eaddr *eaddr)
356 {
357 	struct slim_device *sbdev;
358 	struct device *dev;
359 
360 	dev = device_find_child(ctrl->dev, eaddr, slim_match_dev);
361 	if (dev) {
362 		sbdev = to_slim_device(dev);
363 		return sbdev;
364 	}
365 
366 	return NULL;
367 }
368 
369 /**
370  * slim_get_device() - get handle to a device.
371  *
372  * @ctrl: Controller on which this device will be added/queried
373  * @e_addr: Enumeration address of the device to be queried
374  *
375  * Return: pointer to a device if it has already reported. Creates a new
376  * device and returns pointer to it if the device has not yet enumerated.
377  */
378 struct slim_device *slim_get_device(struct slim_controller *ctrl,
379 				    struct slim_eaddr *e_addr)
380 {
381 	struct slim_device *sbdev;
382 
383 	sbdev = find_slim_device(ctrl, e_addr);
384 	if (!sbdev) {
385 		sbdev = slim_alloc_device(ctrl, e_addr, NULL);
386 		if (!sbdev)
387 			return ERR_PTR(-ENOMEM);
388 	}
389 
390 	return sbdev;
391 }
392 EXPORT_SYMBOL_GPL(slim_get_device);
393 
394 static int of_slim_match_dev(struct device *dev, void *data)
395 {
396 	struct device_node *np = data;
397 	struct slim_device *sbdev = to_slim_device(dev);
398 
399 	return (sbdev->dev.of_node == np);
400 }
401 
402 static struct slim_device *of_find_slim_device(struct slim_controller *ctrl,
403 					       struct device_node *np)
404 {
405 	struct slim_device *sbdev;
406 	struct device *dev;
407 
408 	dev = device_find_child(ctrl->dev, np, of_slim_match_dev);
409 	if (dev) {
410 		sbdev = to_slim_device(dev);
411 		return sbdev;
412 	}
413 
414 	return NULL;
415 }
416 
417 /**
418  * of_slim_get_device() - get handle to a device using dt node.
419  *
420  * @ctrl: Controller on which this device will be added/queried
421  * @np: node pointer to device
422  *
423  * Return: pointer to a device if it has already reported. Creates a new
424  * device and returns pointer to it if the device has not yet enumerated.
425  */
426 struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
427 				       struct device_node *np)
428 {
429 	return of_find_slim_device(ctrl, np);
430 }
431 EXPORT_SYMBOL_GPL(of_slim_get_device);
432 
433 static int slim_device_alloc_laddr(struct slim_device *sbdev,
434 				   bool report_present)
435 {
436 	struct slim_controller *ctrl = sbdev->ctrl;
437 	u8 laddr;
438 	int ret;
439 
440 	mutex_lock(&ctrl->lock);
441 	if (ctrl->get_laddr) {
442 		ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr);
443 		if (ret < 0)
444 			goto err;
445 	} else if (report_present) {
446 		ret = ida_simple_get(&ctrl->laddr_ida,
447 				     0, SLIM_LA_MANAGER - 1, GFP_KERNEL);
448 		if (ret < 0)
449 			goto err;
450 
451 		laddr = ret;
452 	} else {
453 		ret = -EINVAL;
454 		goto err;
455 	}
456 
457 	if (ctrl->set_laddr) {
458 		ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr);
459 		if (ret) {
460 			ret = -EINVAL;
461 			goto err;
462 		}
463 	}
464 
465 	sbdev->laddr = laddr;
466 	sbdev->is_laddr_valid = true;
467 
468 	slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
469 
470 	dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
471 		laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code,
472 		sbdev->e_addr.dev_index, sbdev->e_addr.instance);
473 
474 err:
475 	mutex_unlock(&ctrl->lock);
476 	return ret;
477 
478 }
479 
480 /**
481  * slim_device_report_present() - Report enumerated device.
482  *
483  * @ctrl: Controller with which device is enumerated.
484  * @e_addr: Enumeration address of the device.
485  * @laddr: Return logical address (if valid flag is false)
486  *
487  * Called by controller in response to REPORT_PRESENT. Framework will assign
488  * a logical address to this enumeration address.
489  * Function returns -EXFULL to indicate that all logical addresses are already
490  * taken.
491  */
492 int slim_device_report_present(struct slim_controller *ctrl,
493 			       struct slim_eaddr *e_addr, u8 *laddr)
494 {
495 	struct slim_device *sbdev;
496 	int ret;
497 
498 	ret = pm_runtime_get_sync(ctrl->dev);
499 
500 	if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
501 		dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n",
502 				    ctrl->sched.clk_state, ret);
503 		goto slimbus_not_active;
504 	}
505 
506 	sbdev = slim_get_device(ctrl, e_addr);
507 	if (IS_ERR(sbdev))
508 		return -ENODEV;
509 
510 	if (sbdev->is_laddr_valid) {
511 		*laddr = sbdev->laddr;
512 		return 0;
513 	}
514 
515 	ret = slim_device_alloc_laddr(sbdev, true);
516 
517 slimbus_not_active:
518 	pm_runtime_mark_last_busy(ctrl->dev);
519 	pm_runtime_put_autosuspend(ctrl->dev);
520 	return ret;
521 }
522 EXPORT_SYMBOL_GPL(slim_device_report_present);
523 
524 /**
525  * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
526  *
527  * @sbdev: client handle requesting the address.
528  *
529  * Return: zero if a logical address is valid or a new logical address
530  * has been assigned. error code in case of error.
531  */
532 int slim_get_logical_addr(struct slim_device *sbdev)
533 {
534 	if (!sbdev->is_laddr_valid)
535 		return slim_device_alloc_laddr(sbdev, false);
536 
537 	return 0;
538 }
539 EXPORT_SYMBOL_GPL(slim_get_logical_addr);
540 
541 static void __exit slimbus_exit(void)
542 {
543 	bus_unregister(&slimbus_bus);
544 }
545 module_exit(slimbus_exit);
546 
547 static int __init slimbus_init(void)
548 {
549 	return bus_register(&slimbus_bus);
550 }
551 postcore_initcall(slimbus_init);
552 
553 MODULE_LICENSE("GPL v2");
554 MODULE_DESCRIPTION("SLIMbus core");
555