core.c 13 KB

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