core.c 11 KB

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