fsi-master-hub.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * FSI hub master driver
  3. *
  4. * Copyright (C) IBM Corporation 2016
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/fsi.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. #include "fsi-master.h"
  21. /* Control Registers */
  22. #define FSI_MMODE 0x0 /* R/W: mode */
  23. #define FSI_MDLYR 0x4 /* R/W: delay */
  24. #define FSI_MCRSP 0x8 /* R/W: clock rate */
  25. #define FSI_MENP0 0x10 /* R/W: enable */
  26. #define FSI_MLEVP0 0x18 /* R: plug detect */
  27. #define FSI_MSENP0 0x18 /* S: Set enable */
  28. #define FSI_MCENP0 0x20 /* C: Clear enable */
  29. #define FSI_MAEB 0x70 /* R: Error address */
  30. #define FSI_MVER 0x74 /* R: master version/type */
  31. #define FSI_MRESP0 0xd0 /* W: Port reset */
  32. #define FSI_MESRB0 0x1d0 /* R: Master error status */
  33. #define FSI_MRESB0 0x1d0 /* W: Reset bridge */
  34. #define FSI_MECTRL 0x2e0 /* W: Error control */
  35. /* MMODE: Mode control */
  36. #define FSI_MMODE_EIP 0x80000000 /* Enable interrupt polling */
  37. #define FSI_MMODE_ECRC 0x40000000 /* Enable error recovery */
  38. #define FSI_MMODE_EPC 0x10000000 /* Enable parity checking */
  39. #define FSI_MMODE_P8_TO_LSB 0x00000010 /* Timeout value LSB */
  40. /* MSB=1, LSB=0 is 0.8 ms */
  41. /* MSB=0, LSB=1 is 0.9 ms */
  42. #define FSI_MMODE_CRS0SHFT 18 /* Clk rate selection 0 shift */
  43. #define FSI_MMODE_CRS0MASK 0x3ff /* Clk rate selection 0 mask */
  44. #define FSI_MMODE_CRS1SHFT 8 /* Clk rate selection 1 shift */
  45. #define FSI_MMODE_CRS1MASK 0x3ff /* Clk rate selection 1 mask */
  46. /* MRESB: Reset brindge */
  47. #define FSI_MRESB_RST_GEN 0x80000000 /* General reset */
  48. #define FSI_MRESB_RST_ERR 0x40000000 /* Error Reset */
  49. /* MRESB: Reset port */
  50. #define FSI_MRESP_RST_ALL_MASTER 0x20000000 /* Reset all FSI masters */
  51. #define FSI_MRESP_RST_ALL_LINK 0x10000000 /* Reset all FSI port contr. */
  52. #define FSI_MRESP_RST_MCR 0x08000000 /* Reset FSI master reg. */
  53. #define FSI_MRESP_RST_PYE 0x04000000 /* Reset FSI parity error */
  54. #define FSI_MRESP_RST_ALL 0xfc000000 /* Reset any error */
  55. /* MECTRL: Error control */
  56. #define FSI_MECTRL_EOAE 0x8000 /* Enable machine check when */
  57. /* master 0 in error */
  58. #define FSI_MECTRL_P8_AUTO_TERM 0x4000 /* Auto terminate */
  59. #define FSI_ENGID_HUB_MASTER 0x1c
  60. #define FSI_HUB_LINK_OFFSET 0x80000
  61. #define FSI_HUB_LINK_SIZE 0x80000
  62. #define FSI_HUB_MASTER_MAX_LINKS 8
  63. #define FSI_LINK_ENABLE_SETUP_TIME 10 /* in mS */
  64. /*
  65. * FSI hub master support
  66. *
  67. * A hub master increases the number of potential target devices that the
  68. * primary FSI master can access. For each link a primary master supports,
  69. * each of those links can in turn be chained to a hub master with multiple
  70. * links of its own.
  71. *
  72. * The hub is controlled by a set of control registers exposed as a regular fsi
  73. * device (the hub->upstream device), and provides access to the downstream FSI
  74. * bus as through an address range on the slave itself (->addr and ->size).
  75. *
  76. * [This differs from "cascaded" masters, which expose the entire downstream
  77. * bus entirely through the fsi device address range, and so have a smaller
  78. * accessible address space.]
  79. */
  80. struct fsi_master_hub {
  81. struct fsi_master master;
  82. struct fsi_device *upstream;
  83. uint32_t addr, size; /* slave-relative addr of */
  84. /* master address space */
  85. };
  86. #define to_fsi_master_hub(m) container_of(m, struct fsi_master_hub, master)
  87. static int hub_master_read(struct fsi_master *master, int link,
  88. uint8_t id, uint32_t addr, void *val, size_t size)
  89. {
  90. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  91. if (id != 0)
  92. return -EINVAL;
  93. addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
  94. return fsi_slave_read(hub->upstream->slave, addr, val, size);
  95. }
  96. static int hub_master_write(struct fsi_master *master, int link,
  97. uint8_t id, uint32_t addr, const void *val, size_t size)
  98. {
  99. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  100. if (id != 0)
  101. return -EINVAL;
  102. addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
  103. return fsi_slave_write(hub->upstream->slave, addr, val, size);
  104. }
  105. static int hub_master_break(struct fsi_master *master, int link)
  106. {
  107. uint32_t addr;
  108. __be32 cmd;
  109. addr = 0x4;
  110. cmd = cpu_to_be32(0xc0de0000);
  111. return hub_master_write(master, link, 0, addr, &cmd, sizeof(cmd));
  112. }
  113. static int hub_master_link_enable(struct fsi_master *master, int link)
  114. {
  115. struct fsi_master_hub *hub = to_fsi_master_hub(master);
  116. int idx, bit;
  117. __be32 reg;
  118. int rc;
  119. idx = link / 32;
  120. bit = link % 32;
  121. reg = cpu_to_be32(0x80000000 >> bit);
  122. rc = fsi_device_write(hub->upstream, FSI_MSENP0 + (4 * idx), &reg, 4);
  123. mdelay(FSI_LINK_ENABLE_SETUP_TIME);
  124. fsi_device_read(hub->upstream, FSI_MENP0 + (4 * idx), &reg, 4);
  125. return rc;
  126. }
  127. static void hub_master_release(struct device *dev)
  128. {
  129. struct fsi_master_hub *hub = to_fsi_master_hub(dev_to_fsi_master(dev));
  130. kfree(hub);
  131. }
  132. /* mmode encoders */
  133. static inline u32 fsi_mmode_crs0(u32 x)
  134. {
  135. return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
  136. }
  137. static inline u32 fsi_mmode_crs1(u32 x)
  138. {
  139. return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
  140. }
  141. static int hub_master_init(struct fsi_master_hub *hub)
  142. {
  143. struct fsi_device *dev = hub->upstream;
  144. __be32 reg;
  145. int rc;
  146. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
  147. | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
  148. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  149. if (rc)
  150. return rc;
  151. /* Initialize the MFSI (hub master) engine */
  152. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
  153. | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
  154. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  155. if (rc)
  156. return rc;
  157. reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
  158. rc = fsi_device_write(dev, FSI_MECTRL, &reg, sizeof(reg));
  159. if (rc)
  160. return rc;
  161. reg = cpu_to_be32(FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
  162. | fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
  163. | FSI_MMODE_P8_TO_LSB);
  164. rc = fsi_device_write(dev, FSI_MMODE, &reg, sizeof(reg));
  165. if (rc)
  166. return rc;
  167. reg = cpu_to_be32(0xffff0000);
  168. rc = fsi_device_write(dev, FSI_MDLYR, &reg, sizeof(reg));
  169. if (rc)
  170. return rc;
  171. reg = cpu_to_be32(~0);
  172. rc = fsi_device_write(dev, FSI_MSENP0, &reg, sizeof(reg));
  173. if (rc)
  174. return rc;
  175. /* Leave enabled long enough for master logic to set up */
  176. mdelay(FSI_LINK_ENABLE_SETUP_TIME);
  177. rc = fsi_device_write(dev, FSI_MCENP0, &reg, sizeof(reg));
  178. if (rc)
  179. return rc;
  180. rc = fsi_device_read(dev, FSI_MAEB, &reg, sizeof(reg));
  181. if (rc)
  182. return rc;
  183. reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
  184. rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
  185. if (rc)
  186. return rc;
  187. rc = fsi_device_read(dev, FSI_MLEVP0, &reg, sizeof(reg));
  188. if (rc)
  189. return rc;
  190. /* Reset the master bridge */
  191. reg = cpu_to_be32(FSI_MRESB_RST_GEN);
  192. rc = fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
  193. if (rc)
  194. return rc;
  195. reg = cpu_to_be32(FSI_MRESB_RST_ERR);
  196. return fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
  197. }
  198. static int hub_master_probe(struct device *dev)
  199. {
  200. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  201. struct fsi_master_hub *hub;
  202. uint32_t reg, links;
  203. __be32 __reg;
  204. int rc;
  205. rc = fsi_device_read(fsi_dev, FSI_MVER, &__reg, sizeof(__reg));
  206. if (rc)
  207. return rc;
  208. reg = be32_to_cpu(__reg);
  209. links = (reg >> 8) & 0xff;
  210. dev_dbg(dev, "hub version %08x (%d links)\n", reg, links);
  211. rc = fsi_slave_claim_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
  212. FSI_HUB_LINK_SIZE * links);
  213. if (rc) {
  214. dev_err(dev, "can't claim slave address range for links");
  215. return rc;
  216. }
  217. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  218. if (!hub) {
  219. rc = -ENOMEM;
  220. goto err_release;
  221. }
  222. hub->addr = FSI_HUB_LINK_OFFSET;
  223. hub->size = FSI_HUB_LINK_SIZE * links;
  224. hub->upstream = fsi_dev;
  225. hub->master.dev.parent = dev;
  226. hub->master.dev.release = hub_master_release;
  227. hub->master.dev.of_node = of_node_get(dev_of_node(dev));
  228. hub->master.n_links = links;
  229. hub->master.read = hub_master_read;
  230. hub->master.write = hub_master_write;
  231. hub->master.send_break = hub_master_break;
  232. hub->master.link_enable = hub_master_link_enable;
  233. dev_set_drvdata(dev, hub);
  234. hub_master_init(hub);
  235. rc = fsi_master_register(&hub->master);
  236. if (rc)
  237. goto err_release;
  238. /* At this point, fsi_master_register performs the device_initialize(),
  239. * and holds the sole reference on master.dev. This means the device
  240. * will be freed (via ->release) during any subsequent call to
  241. * fsi_master_unregister. We add our own reference to it here, so we
  242. * can perform cleanup (in _remove()) without it being freed before
  243. * we're ready.
  244. */
  245. get_device(&hub->master.dev);
  246. return 0;
  247. err_release:
  248. fsi_slave_release_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
  249. FSI_HUB_LINK_SIZE * links);
  250. return rc;
  251. }
  252. static int hub_master_remove(struct device *dev)
  253. {
  254. struct fsi_master_hub *hub = dev_get_drvdata(dev);
  255. fsi_master_unregister(&hub->master);
  256. fsi_slave_release_range(hub->upstream->slave, hub->addr, hub->size);
  257. of_node_put(hub->master.dev.of_node);
  258. /*
  259. * master.dev will likely be ->release()ed after this, which free()s
  260. * the hub
  261. */
  262. put_device(&hub->master.dev);
  263. return 0;
  264. }
  265. static struct fsi_device_id hub_master_ids[] = {
  266. {
  267. .engine_type = FSI_ENGID_HUB_MASTER,
  268. .version = FSI_VERSION_ANY,
  269. },
  270. { 0 }
  271. };
  272. static struct fsi_driver hub_master_driver = {
  273. .id_table = hub_master_ids,
  274. .drv = {
  275. .name = "fsi-master-hub",
  276. .bus = &fsi_bus_type,
  277. .probe = hub_master_probe,
  278. .remove = hub_master_remove,
  279. }
  280. };
  281. module_fsi_driver(hub_master_driver);
  282. MODULE_LICENSE("GPL");