aspeed-lpc-ctrl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2017 IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/poll.h>
  17. #include <linux/regmap.h>
  18. #include <linux/aspeed-lpc-ctrl.h>
  19. #define DEVICE_NAME "aspeed-lpc-ctrl"
  20. #define HICR5 0x0
  21. #define HICR5_ENL2H BIT(8)
  22. #define HICR5_ENFWH BIT(10)
  23. #define HICR7 0x8
  24. #define HICR8 0xc
  25. struct aspeed_lpc_ctrl {
  26. struct miscdevice miscdev;
  27. struct regmap *regmap;
  28. struct clk *clk;
  29. phys_addr_t mem_base;
  30. resource_size_t mem_size;
  31. u32 pnor_size;
  32. u32 pnor_base;
  33. };
  34. static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file)
  35. {
  36. return container_of(file->private_data, struct aspeed_lpc_ctrl,
  37. miscdev);
  38. }
  39. static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma)
  40. {
  41. struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
  42. unsigned long vsize = vma->vm_end - vma->vm_start;
  43. pgprot_t prot = vma->vm_page_prot;
  44. if (vma->vm_pgoff + vsize > lpc_ctrl->mem_base + lpc_ctrl->mem_size)
  45. return -EINVAL;
  46. /* ast2400/2500 AHB accesses are not cache coherent */
  47. prot = pgprot_noncached(prot);
  48. if (remap_pfn_range(vma, vma->vm_start,
  49. (lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff,
  50. vsize, prot))
  51. return -EAGAIN;
  52. return 0;
  53. }
  54. static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd,
  55. unsigned long param)
  56. {
  57. struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
  58. void __user *p = (void __user *)param;
  59. struct aspeed_lpc_ctrl_mapping map;
  60. u32 addr;
  61. u32 size;
  62. long rc;
  63. if (copy_from_user(&map, p, sizeof(map)))
  64. return -EFAULT;
  65. if (map.flags != 0)
  66. return -EINVAL;
  67. switch (cmd) {
  68. case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
  69. /* The flash windows don't report their size */
  70. if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY)
  71. return -EINVAL;
  72. /* Support more than one window id in the future */
  73. if (map.window_id != 0)
  74. return -EINVAL;
  75. map.size = lpc_ctrl->mem_size;
  76. return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0;
  77. case ASPEED_LPC_CTRL_IOCTL_MAP:
  78. /*
  79. * The top half of HICR7 is the MSB of the BMC address of the
  80. * mapping.
  81. * The bottom half of HICR7 is the MSB of the HOST LPC
  82. * firmware space address of the mapping.
  83. *
  84. * The 1 bits in the top of half of HICR8 represent the bits
  85. * (in the requested address) that should be ignored and
  86. * replaced with those from the top half of HICR7.
  87. * The 1 bits in the bottom half of HICR8 represent the bits
  88. * (in the requested address) that should be kept and pass
  89. * into the BMC address space.
  90. */
  91. /*
  92. * It doesn't make sense to talk about a size or offset with
  93. * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
  94. * bits of addresses and sizes.
  95. */
  96. if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff))
  97. return -EINVAL;
  98. /*
  99. * Because of the way the masks work in HICR8 offset has to
  100. * be a multiple of size.
  101. */
  102. if (map.offset & (map.size - 1))
  103. return -EINVAL;
  104. if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) {
  105. addr = lpc_ctrl->pnor_base;
  106. size = lpc_ctrl->pnor_size;
  107. } else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) {
  108. addr = lpc_ctrl->mem_base;
  109. size = lpc_ctrl->mem_size;
  110. } else {
  111. return -EINVAL;
  112. }
  113. /* Check overflow first! */
  114. if (map.offset + map.size < map.offset ||
  115. map.offset + map.size > size)
  116. return -EINVAL;
  117. if (map.size == 0 || map.size > size)
  118. return -EINVAL;
  119. addr += map.offset;
  120. /*
  121. * addr (host lpc address) is safe regardless of values. This
  122. * simply changes the address the host has to request on its
  123. * side of the LPC bus. This cannot impact the hosts own
  124. * memory space by surprise as LPC specific accessors are
  125. * required. The only strange thing that could be done is
  126. * setting the lower 16 bits but the shift takes care of that.
  127. */
  128. rc = regmap_write(lpc_ctrl->regmap, HICR7,
  129. (addr | (map.addr >> 16)));
  130. if (rc)
  131. return rc;
  132. rc = regmap_write(lpc_ctrl->regmap, HICR8,
  133. (~(map.size - 1)) | ((map.size >> 16) - 1));
  134. if (rc)
  135. return rc;
  136. /*
  137. * Enable LPC FHW cycles. This is required for the host to
  138. * access the regions specified.
  139. */
  140. return regmap_update_bits(lpc_ctrl->regmap, HICR5,
  141. HICR5_ENFWH | HICR5_ENL2H,
  142. HICR5_ENFWH | HICR5_ENL2H);
  143. }
  144. return -EINVAL;
  145. }
  146. static const struct file_operations aspeed_lpc_ctrl_fops = {
  147. .owner = THIS_MODULE,
  148. .mmap = aspeed_lpc_ctrl_mmap,
  149. .unlocked_ioctl = aspeed_lpc_ctrl_ioctl,
  150. };
  151. static int aspeed_lpc_ctrl_probe(struct platform_device *pdev)
  152. {
  153. struct aspeed_lpc_ctrl *lpc_ctrl;
  154. struct device_node *node;
  155. struct resource resm;
  156. struct device *dev;
  157. int rc;
  158. dev = &pdev->dev;
  159. lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL);
  160. if (!lpc_ctrl)
  161. return -ENOMEM;
  162. node = of_parse_phandle(dev->of_node, "flash", 0);
  163. if (!node) {
  164. dev_err(dev, "Didn't find host pnor flash node\n");
  165. return -ENODEV;
  166. }
  167. rc = of_address_to_resource(node, 1, &resm);
  168. of_node_put(node);
  169. if (rc) {
  170. dev_err(dev, "Couldn't address to resource for flash\n");
  171. return rc;
  172. }
  173. lpc_ctrl->pnor_size = resource_size(&resm);
  174. lpc_ctrl->pnor_base = resm.start;
  175. dev_set_drvdata(&pdev->dev, lpc_ctrl);
  176. node = of_parse_phandle(dev->of_node, "memory-region", 0);
  177. if (!node) {
  178. dev_err(dev, "Didn't find reserved memory\n");
  179. return -EINVAL;
  180. }
  181. rc = of_address_to_resource(node, 0, &resm);
  182. of_node_put(node);
  183. if (rc) {
  184. dev_err(dev, "Couldn't address to resource for reserved memory\n");
  185. return -ENOMEM;
  186. }
  187. lpc_ctrl->mem_size = resource_size(&resm);
  188. lpc_ctrl->mem_base = resm.start;
  189. lpc_ctrl->regmap = syscon_node_to_regmap(
  190. pdev->dev.parent->of_node);
  191. if (IS_ERR(lpc_ctrl->regmap)) {
  192. dev_err(dev, "Couldn't get regmap\n");
  193. return -ENODEV;
  194. }
  195. lpc_ctrl->clk = devm_clk_get(dev, NULL);
  196. if (IS_ERR(lpc_ctrl->clk)) {
  197. dev_err(dev, "couldn't get clock\n");
  198. return PTR_ERR(lpc_ctrl->clk);
  199. }
  200. rc = clk_prepare_enable(lpc_ctrl->clk);
  201. if (rc) {
  202. dev_err(dev, "couldn't enable clock\n");
  203. return rc;
  204. }
  205. lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR;
  206. lpc_ctrl->miscdev.name = DEVICE_NAME;
  207. lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops;
  208. lpc_ctrl->miscdev.parent = dev;
  209. rc = misc_register(&lpc_ctrl->miscdev);
  210. if (rc) {
  211. dev_err(dev, "Unable to register device\n");
  212. goto err;
  213. }
  214. dev_info(dev, "Loaded at %pr\n", &resm);
  215. return 0;
  216. err:
  217. clk_disable_unprepare(lpc_ctrl->clk);
  218. return rc;
  219. }
  220. static int aspeed_lpc_ctrl_remove(struct platform_device *pdev)
  221. {
  222. struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev);
  223. misc_deregister(&lpc_ctrl->miscdev);
  224. clk_disable_unprepare(lpc_ctrl->clk);
  225. return 0;
  226. }
  227. static const struct of_device_id aspeed_lpc_ctrl_match[] = {
  228. { .compatible = "aspeed,ast2400-lpc-ctrl" },
  229. { .compatible = "aspeed,ast2500-lpc-ctrl" },
  230. { },
  231. };
  232. static struct platform_driver aspeed_lpc_ctrl_driver = {
  233. .driver = {
  234. .name = DEVICE_NAME,
  235. .of_match_table = aspeed_lpc_ctrl_match,
  236. },
  237. .probe = aspeed_lpc_ctrl_probe,
  238. .remove = aspeed_lpc_ctrl_remove,
  239. };
  240. module_platform_driver(aspeed_lpc_ctrl_driver);
  241. MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match);
  242. MODULE_LICENSE("GPL");
  243. MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
  244. MODULE_DESCRIPTION("Control for aspeed 2400/2500 LPC HOST to BMC mappings");