aspeed-lpc-ctrl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2017 IBM Corporation
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/log2.h>
  7. #include <linux/mfd/syscon.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/poll.h>
  14. #include <linux/regmap.h>
  15. #include <linux/aspeed-lpc-ctrl.h>
  16. #define DEVICE_NAME "aspeed-lpc-ctrl"
  17. #define HICR5 0x80
  18. #define HICR5_ENL2H BIT(8)
  19. #define HICR5_ENFWH BIT(10)
  20. #define HICR6 0x84
  21. #define SW_FWH2AHB BIT(17)
  22. #define HICR7 0x88
  23. #define HICR8 0x8c
  24. struct aspeed_lpc_ctrl {
  25. struct miscdevice miscdev;
  26. struct regmap *regmap;
  27. struct clk *clk;
  28. phys_addr_t mem_base;
  29. resource_size_t mem_size;
  30. u32 pnor_size;
  31. u32 pnor_base;
  32. bool fwh2ahb;
  33. struct regmap *scu;
  34. };
  35. static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file)
  36. {
  37. return container_of(file->private_data, struct aspeed_lpc_ctrl,
  38. miscdev);
  39. }
  40. static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma)
  41. {
  42. struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
  43. unsigned long vsize = vma->vm_end - vma->vm_start;
  44. pgprot_t prot = vma->vm_page_prot;
  45. if (vma->vm_pgoff + vma_pages(vma) > lpc_ctrl->mem_size >> PAGE_SHIFT)
  46. return -EINVAL;
  47. /* ast2400/2500 AHB accesses are not cache coherent */
  48. prot = pgprot_noncached(prot);
  49. if (remap_pfn_range(vma, vma->vm_start,
  50. (lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff,
  51. vsize, prot))
  52. return -EAGAIN;
  53. return 0;
  54. }
  55. static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd,
  56. unsigned long param)
  57. {
  58. struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
  59. struct device *dev = file->private_data;
  60. void __user *p = (void __user *)param;
  61. struct aspeed_lpc_ctrl_mapping map;
  62. u32 addr;
  63. u32 size;
  64. long rc;
  65. if (copy_from_user(&map, p, sizeof(map)))
  66. return -EFAULT;
  67. if (map.flags != 0)
  68. return -EINVAL;
  69. switch (cmd) {
  70. case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
  71. /* The flash windows don't report their size */
  72. if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY)
  73. return -EINVAL;
  74. /* Support more than one window id in the future */
  75. if (map.window_id != 0)
  76. return -EINVAL;
  77. /* If memory-region is not described in device tree */
  78. if (!lpc_ctrl->mem_size) {
  79. dev_dbg(dev, "Didn't find reserved memory\n");
  80. return -ENXIO;
  81. }
  82. map.size = lpc_ctrl->mem_size;
  83. return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0;
  84. case ASPEED_LPC_CTRL_IOCTL_MAP:
  85. /*
  86. * The top half of HICR7 is the MSB of the BMC address of the
  87. * mapping.
  88. * The bottom half of HICR7 is the MSB of the HOST LPC
  89. * firmware space address of the mapping.
  90. *
  91. * The 1 bits in the top of half of HICR8 represent the bits
  92. * (in the requested address) that should be ignored and
  93. * replaced with those from the top half of HICR7.
  94. * The 1 bits in the bottom half of HICR8 represent the bits
  95. * (in the requested address) that should be kept and pass
  96. * into the BMC address space.
  97. */
  98. /*
  99. * It doesn't make sense to talk about a size or offset with
  100. * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
  101. * bits of addresses and sizes.
  102. */
  103. if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff))
  104. return -EINVAL;
  105. /*
  106. * Because of the way the masks work in HICR8 offset has to
  107. * be a multiple of size.
  108. */
  109. if (map.offset & (map.size - 1))
  110. return -EINVAL;
  111. if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) {
  112. if (!lpc_ctrl->pnor_size) {
  113. dev_dbg(dev, "Didn't find host pnor flash\n");
  114. return -ENXIO;
  115. }
  116. addr = lpc_ctrl->pnor_base;
  117. size = lpc_ctrl->pnor_size;
  118. } else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) {
  119. /* If memory-region is not described in device tree */
  120. if (!lpc_ctrl->mem_size) {
  121. dev_dbg(dev, "Didn't find reserved memory\n");
  122. return -ENXIO;
  123. }
  124. addr = lpc_ctrl->mem_base;
  125. size = lpc_ctrl->mem_size;
  126. } else {
  127. return -EINVAL;
  128. }
  129. /* Check overflow first! */
  130. if (map.offset + map.size < map.offset ||
  131. map.offset + map.size > size)
  132. return -EINVAL;
  133. if (map.size == 0 || map.size > size)
  134. return -EINVAL;
  135. addr += map.offset;
  136. /*
  137. * addr (host lpc address) is safe regardless of values. This
  138. * simply changes the address the host has to request on its
  139. * side of the LPC bus. This cannot impact the hosts own
  140. * memory space by surprise as LPC specific accessors are
  141. * required. The only strange thing that could be done is
  142. * setting the lower 16 bits but the shift takes care of that.
  143. */
  144. rc = regmap_write(lpc_ctrl->regmap, HICR7,
  145. (addr | (map.addr >> 16)));
  146. if (rc)
  147. return rc;
  148. rc = regmap_write(lpc_ctrl->regmap, HICR8,
  149. (~(map.size - 1)) | ((map.size >> 16) - 1));
  150. if (rc)
  151. return rc;
  152. /*
  153. * Switch to FWH2AHB mode, AST2600 only.
  154. */
  155. if (lpc_ctrl->fwh2ahb) {
  156. /*
  157. * Enable FWH2AHB in SCU debug control register 2. This
  158. * does not turn it on, but makes it available for it
  159. * to be configured in HICR6.
  160. */
  161. regmap_update_bits(lpc_ctrl->scu, 0x0D8, BIT(2), 0);
  162. /*
  163. * The other bits in this register are interrupt status bits
  164. * that are cleared by writing 1. As we don't want to clear
  165. * them, set only the bit of interest.
  166. */
  167. regmap_write(lpc_ctrl->regmap, HICR6, SW_FWH2AHB);
  168. }
  169. /*
  170. * Enable LPC FHW cycles. This is required for the host to
  171. * access the regions specified.
  172. */
  173. return regmap_update_bits(lpc_ctrl->regmap, HICR5,
  174. HICR5_ENFWH | HICR5_ENL2H,
  175. HICR5_ENFWH | HICR5_ENL2H);
  176. }
  177. return -EINVAL;
  178. }
  179. static const struct file_operations aspeed_lpc_ctrl_fops = {
  180. .owner = THIS_MODULE,
  181. .mmap = aspeed_lpc_ctrl_mmap,
  182. .unlocked_ioctl = aspeed_lpc_ctrl_ioctl,
  183. };
  184. static int aspeed_lpc_ctrl_probe(struct platform_device *pdev)
  185. {
  186. struct aspeed_lpc_ctrl *lpc_ctrl;
  187. struct device_node *node;
  188. struct resource resm;
  189. struct device *dev;
  190. struct device_node *np;
  191. int rc;
  192. dev = &pdev->dev;
  193. lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL);
  194. if (!lpc_ctrl)
  195. return -ENOMEM;
  196. /* If flash is described in device tree then store */
  197. node = of_parse_phandle(dev->of_node, "flash", 0);
  198. if (!node) {
  199. dev_dbg(dev, "Didn't find host pnor flash node\n");
  200. } else {
  201. rc = of_address_to_resource(node, 1, &resm);
  202. of_node_put(node);
  203. if (rc) {
  204. dev_err(dev, "Couldn't address to resource for flash\n");
  205. return rc;
  206. }
  207. lpc_ctrl->pnor_size = resource_size(&resm);
  208. lpc_ctrl->pnor_base = resm.start;
  209. }
  210. dev_set_drvdata(&pdev->dev, lpc_ctrl);
  211. /* If memory-region is described in device tree then store */
  212. node = of_parse_phandle(dev->of_node, "memory-region", 0);
  213. if (!node) {
  214. dev_dbg(dev, "Didn't find reserved memory\n");
  215. } else {
  216. rc = of_address_to_resource(node, 0, &resm);
  217. of_node_put(node);
  218. if (rc) {
  219. dev_err(dev, "Couldn't address to resource for reserved memory\n");
  220. return -ENXIO;
  221. }
  222. lpc_ctrl->mem_size = resource_size(&resm);
  223. lpc_ctrl->mem_base = resm.start;
  224. if (!is_power_of_2(lpc_ctrl->mem_size)) {
  225. dev_err(dev, "Reserved memory size must be a power of 2, got %u\n",
  226. (unsigned int)lpc_ctrl->mem_size);
  227. return -EINVAL;
  228. }
  229. if (!IS_ALIGNED(lpc_ctrl->mem_base, lpc_ctrl->mem_size)) {
  230. dev_err(dev, "Reserved memory must be naturally aligned for size %u\n",
  231. (unsigned int)lpc_ctrl->mem_size);
  232. return -EINVAL;
  233. }
  234. }
  235. np = pdev->dev.parent->of_node;
  236. if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
  237. !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") &&
  238. !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) {
  239. dev_err(dev, "unsupported LPC device binding\n");
  240. return -ENODEV;
  241. }
  242. lpc_ctrl->regmap = syscon_node_to_regmap(np);
  243. if (IS_ERR(lpc_ctrl->regmap)) {
  244. dev_err(dev, "Couldn't get regmap\n");
  245. return -ENODEV;
  246. }
  247. if (of_device_is_compatible(dev->of_node, "aspeed,ast2600-lpc-ctrl")) {
  248. lpc_ctrl->fwh2ahb = true;
  249. lpc_ctrl->scu = syscon_regmap_lookup_by_compatible("aspeed,ast2600-scu");
  250. if (IS_ERR(lpc_ctrl->scu)) {
  251. dev_err(dev, "couldn't find scu\n");
  252. return PTR_ERR(lpc_ctrl->scu);
  253. }
  254. }
  255. lpc_ctrl->clk = devm_clk_get(dev, NULL);
  256. if (IS_ERR(lpc_ctrl->clk))
  257. return dev_err_probe(dev, PTR_ERR(lpc_ctrl->clk),
  258. "couldn't get clock\n");
  259. rc = clk_prepare_enable(lpc_ctrl->clk);
  260. if (rc) {
  261. dev_err(dev, "couldn't enable clock\n");
  262. return rc;
  263. }
  264. lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR;
  265. lpc_ctrl->miscdev.name = DEVICE_NAME;
  266. lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops;
  267. lpc_ctrl->miscdev.parent = dev;
  268. rc = misc_register(&lpc_ctrl->miscdev);
  269. if (rc) {
  270. dev_err(dev, "Unable to register device\n");
  271. goto err;
  272. }
  273. return 0;
  274. err:
  275. clk_disable_unprepare(lpc_ctrl->clk);
  276. return rc;
  277. }
  278. static int aspeed_lpc_ctrl_remove(struct platform_device *pdev)
  279. {
  280. struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev);
  281. misc_deregister(&lpc_ctrl->miscdev);
  282. clk_disable_unprepare(lpc_ctrl->clk);
  283. return 0;
  284. }
  285. static const struct of_device_id aspeed_lpc_ctrl_match[] = {
  286. { .compatible = "aspeed,ast2400-lpc-ctrl" },
  287. { .compatible = "aspeed,ast2500-lpc-ctrl" },
  288. { .compatible = "aspeed,ast2600-lpc-ctrl" },
  289. { },
  290. };
  291. static struct platform_driver aspeed_lpc_ctrl_driver = {
  292. .driver = {
  293. .name = DEVICE_NAME,
  294. .of_match_table = aspeed_lpc_ctrl_match,
  295. },
  296. .probe = aspeed_lpc_ctrl_probe,
  297. .remove = aspeed_lpc_ctrl_remove,
  298. };
  299. module_platform_driver(aspeed_lpc_ctrl_driver);
  300. MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match);
  301. MODULE_LICENSE("GPL");
  302. MODULE_AUTHOR("Cyril Bur <[email protected]>");
  303. MODULE_DESCRIPTION("Control for ASPEED LPC HOST to BMC mappings");