fsi-master-hub.c 9.0 KB

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