fsi-master-hub.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FSI hub master driver
  4. *
  5. * Copyright (C) IBM Corporation 2016
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/fsi.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/slab.h>
  12. #include "fsi-master.h"
  13. #define FSI_ENGID_HUB_MASTER 0x1c
  14. #define FSI_LINK_ENABLE_SETUP_TIME 10 /* in mS */
  15. /*
  16. * FSI hub master support
  17. *
  18. * A hub master increases the number of potential target devices that the
  19. * primary FSI master can access. For each link a primary master supports,
  20. * each of those links can in turn be chained to a hub master with multiple
  21. * links of its own.
  22. *
  23. * The hub is controlled by a set of control registers exposed as a regular fsi
  24. * device (the hub->upstream device), and provides access to the downstream FSI
  25. * bus as through an address range on the slave itself (->addr and ->size).
  26. *
  27. * [This differs from "cascaded" masters, which expose the entire downstream
  28. * bus entirely through the fsi device address range, and so have a smaller
  29. * accessible address space.]
  30. */
  31. struct fsi_master_hub {
  32. struct fsi_master master;
  33. struct fsi_device *upstream;
  34. uint32_t addr, size; /* slave-relative addr of */
  35. /* master address space */
  36. };
  37. #define to_fsi_master_hub(m) container_of(m, struct fsi_master_hub, master)
  38. static int hub_master_read(struct fsi_master *master, int link,
  39. uint8_t id, uint32_t addr, void *val, size_t size)
  40. {
  41. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  42. if (id != 0)
  43. return -EINVAL;
  44. addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
  45. return fsi_slave_read(hub->upstream->slave, addr, val, size);
  46. }
  47. static int hub_master_write(struct fsi_master *master, int link,
  48. uint8_t id, uint32_t addr, const void *val, size_t size)
  49. {
  50. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  51. if (id != 0)
  52. return -EINVAL;
  53. addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
  54. return fsi_slave_write(hub->upstream->slave, addr, val, size);
  55. }
  56. static int hub_master_break(struct fsi_master *master, int link)
  57. {
  58. uint32_t addr;
  59. __be32 cmd;
  60. addr = 0x4;
  61. cmd = cpu_to_be32(0xc0de0000);
  62. return hub_master_write(master, link, 0, addr, &cmd, sizeof(cmd));
  63. }
  64. static int hub_master_link_enable(struct fsi_master *master, int link,
  65. bool enable)
  66. {
  67. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  68. int idx, bit;
  69. __be32 reg;
  70. int rc;
  71. idx = link / 32;
  72. bit = link % 32;
  73. reg = cpu_to_be32(0x80000000 >> bit);
  74. if (!enable)
  75. return fsi_device_write(hub->upstream, FSI_MCENP0 + (4 * idx),
  76. &reg, 4);
  77. rc = fsi_device_write(hub->upstream, FSI_MSENP0 + (4 * idx), &reg, 4);
  78. if (rc)
  79. return rc;
  80. mdelay(FSI_LINK_ENABLE_SETUP_TIME);
  81. return 0;
  82. }
  83. static void hub_master_release(struct device *dev)
  84. {
  85. struct fsi_master_hub *hub = to_fsi_master_hub(dev_to_fsi_master(dev));
  86. kfree(hub);
  87. }
  88. /* mmode encoders */
  89. static inline u32 fsi_mmode_crs0(u32 x)
  90. {
  91. return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
  92. }
  93. static inline u32 fsi_mmode_crs1(u32 x)
  94. {
  95. return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
  96. }
  97. static int hub_master_init(struct fsi_master_hub *hub)
  98. {
  99. struct fsi_device *dev = hub->upstream;
  100. __be32 reg;
  101. int rc;
  102. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
  103. | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
  104. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  105. if (rc)
  106. return rc;
  107. /* Initialize the MFSI (hub master) engine */
  108. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
  109. | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
  110. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  111. if (rc)
  112. return rc;
  113. reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
  114. rc = fsi_device_write(dev, FSI_MECTRL, &reg, sizeof(reg));
  115. if (rc)
  116. return rc;
  117. reg = cpu_to_be32(FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
  118. | fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
  119. | FSI_MMODE_P8_TO_LSB);
  120. rc = fsi_device_write(dev, FSI_MMODE, &reg, sizeof(reg));
  121. if (rc)
  122. return rc;
  123. reg = cpu_to_be32(0xffff0000);
  124. rc = fsi_device_write(dev, FSI_MDLYR, &reg, sizeof(reg));
  125. if (rc)
  126. return rc;
  127. reg = cpu_to_be32(~0);
  128. rc = fsi_device_write(dev, FSI_MSENP0, &reg, sizeof(reg));
  129. if (rc)
  130. return rc;
  131. /* Leave enabled long enough for master logic to set up */
  132. mdelay(FSI_LINK_ENABLE_SETUP_TIME);
  133. rc = fsi_device_write(dev, FSI_MCENP0, &reg, sizeof(reg));
  134. if (rc)
  135. return rc;
  136. rc = fsi_device_read(dev, FSI_MAEB, &reg, sizeof(reg));
  137. if (rc)
  138. return rc;
  139. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
  140. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  141. if (rc)
  142. return rc;
  143. rc = fsi_device_read(dev, FSI_MLEVP0, &reg, sizeof(reg));
  144. if (rc)
  145. return rc;
  146. /* Reset the master bridge */
  147. reg = cpu_to_be32(FSI_MRESB_RST_GEN);
  148. rc = fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
  149. if (rc)
  150. return rc;
  151. reg = cpu_to_be32(FSI_MRESB_RST_ERR);
  152. return fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
  153. }
  154. static int hub_master_probe(struct device *dev)
  155. {
  156. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  157. struct fsi_master_hub *hub;
  158. uint32_t reg, links;
  159. __be32 __reg;
  160. int rc;
  161. rc = fsi_device_read(fsi_dev, FSI_MVER, &__reg, sizeof(__reg));
  162. if (rc)
  163. return rc;
  164. reg = be32_to_cpu(__reg);
  165. links = (reg >> 8) & 0xff;
  166. dev_dbg(dev, "hub version %08x (%d links)\n", reg, links);
  167. rc = fsi_slave_claim_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
  168. FSI_HUB_LINK_SIZE * links);
  169. if (rc) {
  170. dev_err(dev, "can't claim slave address range for links");
  171. return rc;
  172. }
  173. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  174. if (!hub) {
  175. rc = -ENOMEM;
  176. goto err_release;
  177. }
  178. hub->addr = FSI_HUB_LINK_OFFSET;
  179. hub->size = FSI_HUB_LINK_SIZE * links;
  180. hub->upstream = fsi_dev;
  181. hub->master.dev.parent = dev;
  182. hub->master.dev.release = hub_master_release;
  183. hub->master.dev.of_node = of_node_get(dev_of_node(dev));
  184. hub->master.n_links = links;
  185. hub->master.read = hub_master_read;
  186. hub->master.write = hub_master_write;
  187. hub->master.send_break = hub_master_break;
  188. hub->master.link_enable = hub_master_link_enable;
  189. dev_set_drvdata(dev, hub);
  190. hub_master_init(hub);
  191. rc = fsi_master_register(&hub->master);
  192. if (rc)
  193. goto err_release;
  194. /* At this point, fsi_master_register performs the device_initialize(),
  195. * and holds the sole reference on master.dev. This means the device
  196. * will be freed (via ->release) during any subsequent call to
  197. * fsi_master_unregister. We add our own reference to it here, so we
  198. * can perform cleanup (in _remove()) without it being freed before
  199. * we're ready.
  200. */
  201. get_device(&hub->master.dev);
  202. return 0;
  203. err_release:
  204. fsi_slave_release_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
  205. FSI_HUB_LINK_SIZE * links);
  206. return rc;
  207. }
  208. static int hub_master_remove(struct device *dev)
  209. {
  210. struct fsi_master_hub *hub = dev_get_drvdata(dev);
  211. fsi_master_unregister(&hub->master);
  212. fsi_slave_release_range(hub->upstream->slave, hub->addr, hub->size);
  213. of_node_put(hub->master.dev.of_node);
  214. /*
  215. * master.dev will likely be ->release()ed after this, which free()s
  216. * the hub
  217. */
  218. put_device(&hub->master.dev);
  219. return 0;
  220. }
  221. static const struct fsi_device_id hub_master_ids[] = {
  222. {
  223. .engine_type = FSI_ENGID_HUB_MASTER,
  224. .version = FSI_VERSION_ANY,
  225. },
  226. { 0 }
  227. };
  228. static struct fsi_driver hub_master_driver = {
  229. .id_table = hub_master_ids,
  230. .drv = {
  231. .name = "fsi-master-hub",
  232. .bus = &fsi_bus_type,
  233. .probe = hub_master_probe,
  234. .remove = hub_master_remove,
  235. }
  236. };
  237. module_fsi_driver(hub_master_driver);
  238. MODULE_LICENSE("GPL");