core.c 11 KB

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