core.c 13 KB

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