12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2524feb79SPatrick Venture /* 3524feb79SPatrick Venture * Copyright 2017 IBM Corporation 4524feb79SPatrick Venture */ 5524feb79SPatrick Venture 6524feb79SPatrick Venture #include <linux/clk.h> 76bf4ddbeSAndrew Jeffery #include <linux/log2.h> 8524feb79SPatrick Venture #include <linux/mfd/syscon.h> 9524feb79SPatrick Venture #include <linux/miscdevice.h> 10524feb79SPatrick Venture #include <linux/mm.h> 11524feb79SPatrick Venture #include <linux/module.h> 12524feb79SPatrick Venture #include <linux/of_address.h> 13524feb79SPatrick Venture #include <linux/platform_device.h> 14524feb79SPatrick Venture #include <linux/poll.h> 15524feb79SPatrick Venture #include <linux/regmap.h> 16524feb79SPatrick Venture 17524feb79SPatrick Venture #include <linux/aspeed-lpc-ctrl.h> 18524feb79SPatrick Venture 19524feb79SPatrick Venture #define DEVICE_NAME "aspeed-lpc-ctrl" 20524feb79SPatrick Venture 21489774ffSChia-Wei, Wang #define HICR5 0x80 22524feb79SPatrick Venture #define HICR5_ENL2H BIT(8) 23524feb79SPatrick Venture #define HICR5_ENFWH BIT(10) 24524feb79SPatrick Venture 25489774ffSChia-Wei, Wang #define HICR6 0x84 265042d3f2SJoel Stanley #define SW_FWH2AHB BIT(17) 275042d3f2SJoel Stanley 28489774ffSChia-Wei, Wang #define HICR7 0x88 29489774ffSChia-Wei, Wang #define HICR8 0x8c 30524feb79SPatrick Venture 31524feb79SPatrick Venture struct aspeed_lpc_ctrl { 32524feb79SPatrick Venture struct miscdevice miscdev; 33524feb79SPatrick Venture struct regmap *regmap; 34524feb79SPatrick Venture struct clk *clk; 35524feb79SPatrick Venture phys_addr_t mem_base; 36524feb79SPatrick Venture resource_size_t mem_size; 37524feb79SPatrick Venture u32 pnor_size; 38524feb79SPatrick Venture u32 pnor_base; 395042d3f2SJoel Stanley bool fwh2ahb; 402f9b25faSJoel Stanley struct regmap *scu; 41524feb79SPatrick Venture }; 42524feb79SPatrick Venture 43524feb79SPatrick Venture static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file) 44524feb79SPatrick Venture { 45524feb79SPatrick Venture return container_of(file->private_data, struct aspeed_lpc_ctrl, 46524feb79SPatrick Venture miscdev); 47524feb79SPatrick Venture } 48524feb79SPatrick Venture 49524feb79SPatrick Venture static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma) 50524feb79SPatrick Venture { 51524feb79SPatrick Venture struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file); 52524feb79SPatrick Venture unsigned long vsize = vma->vm_end - vma->vm_start; 53524feb79SPatrick Venture pgprot_t prot = vma->vm_page_prot; 54524feb79SPatrick Venture 55b49a0e69SIwona Winiarska if (vma->vm_pgoff + vma_pages(vma) > lpc_ctrl->mem_size >> PAGE_SHIFT) 56524feb79SPatrick Venture return -EINVAL; 57524feb79SPatrick Venture 58524feb79SPatrick Venture /* ast2400/2500 AHB accesses are not cache coherent */ 59524feb79SPatrick Venture prot = pgprot_noncached(prot); 60524feb79SPatrick Venture 61524feb79SPatrick Venture if (remap_pfn_range(vma, vma->vm_start, 62524feb79SPatrick Venture (lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff, 63524feb79SPatrick Venture vsize, prot)) 64524feb79SPatrick Venture return -EAGAIN; 65524feb79SPatrick Venture 66524feb79SPatrick Venture return 0; 67524feb79SPatrick Venture } 68524feb79SPatrick Venture 69524feb79SPatrick Venture static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd, 70524feb79SPatrick Venture unsigned long param) 71524feb79SPatrick Venture { 72524feb79SPatrick Venture struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file); 73e4272af4SVijay Khemka struct device *dev = file->private_data; 74524feb79SPatrick Venture void __user *p = (void __user *)param; 75524feb79SPatrick Venture struct aspeed_lpc_ctrl_mapping map; 76524feb79SPatrick Venture u32 addr; 77524feb79SPatrick Venture u32 size; 78524feb79SPatrick Venture long rc; 79524feb79SPatrick Venture 80524feb79SPatrick Venture if (copy_from_user(&map, p, sizeof(map))) 81524feb79SPatrick Venture return -EFAULT; 82524feb79SPatrick Venture 83524feb79SPatrick Venture if (map.flags != 0) 84524feb79SPatrick Venture return -EINVAL; 85524feb79SPatrick Venture 86524feb79SPatrick Venture switch (cmd) { 87524feb79SPatrick Venture case ASPEED_LPC_CTRL_IOCTL_GET_SIZE: 88524feb79SPatrick Venture /* The flash windows don't report their size */ 89524feb79SPatrick Venture if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY) 90524feb79SPatrick Venture return -EINVAL; 91524feb79SPatrick Venture 92524feb79SPatrick Venture /* Support more than one window id in the future */ 93524feb79SPatrick Venture if (map.window_id != 0) 94524feb79SPatrick Venture return -EINVAL; 95524feb79SPatrick Venture 96e4272af4SVijay Khemka /* If memory-region is not described in device tree */ 97e4272af4SVijay Khemka if (!lpc_ctrl->mem_size) { 98e4272af4SVijay Khemka dev_dbg(dev, "Didn't find reserved memory\n"); 99e4272af4SVijay Khemka return -ENXIO; 100e4272af4SVijay Khemka } 101e4272af4SVijay Khemka 102524feb79SPatrick Venture map.size = lpc_ctrl->mem_size; 103524feb79SPatrick Venture 104524feb79SPatrick Venture return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0; 105524feb79SPatrick Venture case ASPEED_LPC_CTRL_IOCTL_MAP: 106524feb79SPatrick Venture 107524feb79SPatrick Venture /* 108524feb79SPatrick Venture * The top half of HICR7 is the MSB of the BMC address of the 109524feb79SPatrick Venture * mapping. 110524feb79SPatrick Venture * The bottom half of HICR7 is the MSB of the HOST LPC 111524feb79SPatrick Venture * firmware space address of the mapping. 112524feb79SPatrick Venture * 113524feb79SPatrick Venture * The 1 bits in the top of half of HICR8 represent the bits 114524feb79SPatrick Venture * (in the requested address) that should be ignored and 115524feb79SPatrick Venture * replaced with those from the top half of HICR7. 116524feb79SPatrick Venture * The 1 bits in the bottom half of HICR8 represent the bits 117524feb79SPatrick Venture * (in the requested address) that should be kept and pass 118524feb79SPatrick Venture * into the BMC address space. 119524feb79SPatrick Venture */ 120524feb79SPatrick Venture 121524feb79SPatrick Venture /* 122524feb79SPatrick Venture * It doesn't make sense to talk about a size or offset with 123524feb79SPatrick Venture * low 16 bits set. Both HICR7 and HICR8 talk about the top 16 124524feb79SPatrick Venture * bits of addresses and sizes. 125524feb79SPatrick Venture */ 126524feb79SPatrick Venture 127524feb79SPatrick Venture if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff)) 128524feb79SPatrick Venture return -EINVAL; 129524feb79SPatrick Venture 130524feb79SPatrick Venture /* 131524feb79SPatrick Venture * Because of the way the masks work in HICR8 offset has to 132524feb79SPatrick Venture * be a multiple of size. 133524feb79SPatrick Venture */ 134524feb79SPatrick Venture if (map.offset & (map.size - 1)) 135524feb79SPatrick Venture return -EINVAL; 136524feb79SPatrick Venture 137524feb79SPatrick Venture if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) { 138e4272af4SVijay Khemka if (!lpc_ctrl->pnor_size) { 139e4272af4SVijay Khemka dev_dbg(dev, "Didn't find host pnor flash\n"); 140e4272af4SVijay Khemka return -ENXIO; 141e4272af4SVijay Khemka } 142524feb79SPatrick Venture addr = lpc_ctrl->pnor_base; 143524feb79SPatrick Venture size = lpc_ctrl->pnor_size; 144524feb79SPatrick Venture } else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) { 145e4272af4SVijay Khemka /* If memory-region is not described in device tree */ 146e4272af4SVijay Khemka if (!lpc_ctrl->mem_size) { 147e4272af4SVijay Khemka dev_dbg(dev, "Didn't find reserved memory\n"); 148e4272af4SVijay Khemka return -ENXIO; 149e4272af4SVijay Khemka } 150524feb79SPatrick Venture addr = lpc_ctrl->mem_base; 151524feb79SPatrick Venture size = lpc_ctrl->mem_size; 152524feb79SPatrick Venture } else { 153524feb79SPatrick Venture return -EINVAL; 154524feb79SPatrick Venture } 155524feb79SPatrick Venture 156524feb79SPatrick Venture /* Check overflow first! */ 157524feb79SPatrick Venture if (map.offset + map.size < map.offset || 158524feb79SPatrick Venture map.offset + map.size > size) 159524feb79SPatrick Venture return -EINVAL; 160524feb79SPatrick Venture 161524feb79SPatrick Venture if (map.size == 0 || map.size > size) 162524feb79SPatrick Venture return -EINVAL; 163524feb79SPatrick Venture 164524feb79SPatrick Venture addr += map.offset; 165524feb79SPatrick Venture 166524feb79SPatrick Venture /* 167524feb79SPatrick Venture * addr (host lpc address) is safe regardless of values. This 168524feb79SPatrick Venture * simply changes the address the host has to request on its 169524feb79SPatrick Venture * side of the LPC bus. This cannot impact the hosts own 170524feb79SPatrick Venture * memory space by surprise as LPC specific accessors are 171524feb79SPatrick Venture * required. The only strange thing that could be done is 172524feb79SPatrick Venture * setting the lower 16 bits but the shift takes care of that. 173524feb79SPatrick Venture */ 174524feb79SPatrick Venture 175524feb79SPatrick Venture rc = regmap_write(lpc_ctrl->regmap, HICR7, 176524feb79SPatrick Venture (addr | (map.addr >> 16))); 177524feb79SPatrick Venture if (rc) 178524feb79SPatrick Venture return rc; 179524feb79SPatrick Venture 180524feb79SPatrick Venture rc = regmap_write(lpc_ctrl->regmap, HICR8, 181524feb79SPatrick Venture (~(map.size - 1)) | ((map.size >> 16) - 1)); 182524feb79SPatrick Venture if (rc) 183524feb79SPatrick Venture return rc; 184524feb79SPatrick Venture 185524feb79SPatrick Venture /* 1865042d3f2SJoel Stanley * Switch to FWH2AHB mode, AST2600 only. 1872f9b25faSJoel Stanley */ 1882f9b25faSJoel Stanley if (lpc_ctrl->fwh2ahb) { 1892f9b25faSJoel Stanley /* 1902f9b25faSJoel Stanley * Enable FWH2AHB in SCU debug control register 2. This 1912f9b25faSJoel Stanley * does not turn it on, but makes it available for it 1922f9b25faSJoel Stanley * to be configured in HICR6. 1932f9b25faSJoel Stanley */ 1942f9b25faSJoel Stanley regmap_update_bits(lpc_ctrl->scu, 0x0D8, BIT(2), 0); 1952f9b25faSJoel Stanley 1962f9b25faSJoel Stanley /* 1975042d3f2SJoel Stanley * The other bits in this register are interrupt status bits 1985042d3f2SJoel Stanley * that are cleared by writing 1. As we don't want to clear 1995042d3f2SJoel Stanley * them, set only the bit of interest. 2005042d3f2SJoel Stanley */ 2015042d3f2SJoel Stanley regmap_write(lpc_ctrl->regmap, HICR6, SW_FWH2AHB); 2022f9b25faSJoel Stanley } 2035042d3f2SJoel Stanley 2045042d3f2SJoel Stanley /* 205524feb79SPatrick Venture * Enable LPC FHW cycles. This is required for the host to 206524feb79SPatrick Venture * access the regions specified. 207524feb79SPatrick Venture */ 208524feb79SPatrick Venture return regmap_update_bits(lpc_ctrl->regmap, HICR5, 209524feb79SPatrick Venture HICR5_ENFWH | HICR5_ENL2H, 210524feb79SPatrick Venture HICR5_ENFWH | HICR5_ENL2H); 211524feb79SPatrick Venture } 212524feb79SPatrick Venture 213524feb79SPatrick Venture return -EINVAL; 214524feb79SPatrick Venture } 215524feb79SPatrick Venture 216524feb79SPatrick Venture static const struct file_operations aspeed_lpc_ctrl_fops = { 217524feb79SPatrick Venture .owner = THIS_MODULE, 218524feb79SPatrick Venture .mmap = aspeed_lpc_ctrl_mmap, 219524feb79SPatrick Venture .unlocked_ioctl = aspeed_lpc_ctrl_ioctl, 220524feb79SPatrick Venture }; 221524feb79SPatrick Venture 222524feb79SPatrick Venture static int aspeed_lpc_ctrl_probe(struct platform_device *pdev) 223524feb79SPatrick Venture { 224524feb79SPatrick Venture struct aspeed_lpc_ctrl *lpc_ctrl; 225524feb79SPatrick Venture struct device_node *node; 226524feb79SPatrick Venture struct resource resm; 227524feb79SPatrick Venture struct device *dev; 228489774ffSChia-Wei, Wang struct device_node *np; 229524feb79SPatrick Venture int rc; 230524feb79SPatrick Venture 231524feb79SPatrick Venture dev = &pdev->dev; 232524feb79SPatrick Venture 233524feb79SPatrick Venture lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL); 234524feb79SPatrick Venture if (!lpc_ctrl) 235524feb79SPatrick Venture return -ENOMEM; 236524feb79SPatrick Venture 237e4272af4SVijay Khemka /* If flash is described in device tree then store */ 238524feb79SPatrick Venture node = of_parse_phandle(dev->of_node, "flash", 0); 239524feb79SPatrick Venture if (!node) { 240e4272af4SVijay Khemka dev_dbg(dev, "Didn't find host pnor flash node\n"); 241e4272af4SVijay Khemka } else { 242524feb79SPatrick Venture rc = of_address_to_resource(node, 1, &resm); 243524feb79SPatrick Venture of_node_put(node); 244524feb79SPatrick Venture if (rc) { 245524feb79SPatrick Venture dev_err(dev, "Couldn't address to resource for flash\n"); 246524feb79SPatrick Venture return rc; 247524feb79SPatrick Venture } 248524feb79SPatrick Venture 249524feb79SPatrick Venture lpc_ctrl->pnor_size = resource_size(&resm); 250524feb79SPatrick Venture lpc_ctrl->pnor_base = resm.start; 251c8a3b9b5SJoel Stanley } 252c8a3b9b5SJoel Stanley 253524feb79SPatrick Venture 254524feb79SPatrick Venture dev_set_drvdata(&pdev->dev, lpc_ctrl); 255524feb79SPatrick Venture 256e4272af4SVijay Khemka /* If memory-region is described in device tree then store */ 257524feb79SPatrick Venture node = of_parse_phandle(dev->of_node, "memory-region", 0); 258524feb79SPatrick Venture if (!node) { 259e4272af4SVijay Khemka dev_dbg(dev, "Didn't find reserved memory\n"); 260e4272af4SVijay Khemka } else { 261524feb79SPatrick Venture rc = of_address_to_resource(node, 0, &resm); 262524feb79SPatrick Venture of_node_put(node); 263524feb79SPatrick Venture if (rc) { 264524feb79SPatrick Venture dev_err(dev, "Couldn't address to resource for reserved memory\n"); 265e4272af4SVijay Khemka return -ENXIO; 266524feb79SPatrick Venture } 267524feb79SPatrick Venture 268524feb79SPatrick Venture lpc_ctrl->mem_size = resource_size(&resm); 269524feb79SPatrick Venture lpc_ctrl->mem_base = resm.start; 2706bf4ddbeSAndrew Jeffery 2716bf4ddbeSAndrew Jeffery if (!is_power_of_2(lpc_ctrl->mem_size)) { 2726bf4ddbeSAndrew Jeffery dev_err(dev, "Reserved memory size must be a power of 2, got %u\n", 2736bf4ddbeSAndrew Jeffery (unsigned int)lpc_ctrl->mem_size); 2746bf4ddbeSAndrew Jeffery return -EINVAL; 2756bf4ddbeSAndrew Jeffery } 2766bf4ddbeSAndrew Jeffery 2776bf4ddbeSAndrew Jeffery if (!IS_ALIGNED(lpc_ctrl->mem_base, lpc_ctrl->mem_size)) { 2786bf4ddbeSAndrew Jeffery dev_err(dev, "Reserved memory must be naturally aligned for size %u\n", 2796bf4ddbeSAndrew Jeffery (unsigned int)lpc_ctrl->mem_size); 2806bf4ddbeSAndrew Jeffery return -EINVAL; 2816bf4ddbeSAndrew Jeffery } 282e4272af4SVijay Khemka } 283524feb79SPatrick Venture 284489774ffSChia-Wei, Wang np = pdev->dev.parent->of_node; 285489774ffSChia-Wei, Wang if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") && 286489774ffSChia-Wei, Wang !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") && 287489774ffSChia-Wei, Wang !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) { 288489774ffSChia-Wei, Wang dev_err(dev, "unsupported LPC device binding\n"); 289489774ffSChia-Wei, Wang return -ENODEV; 290489774ffSChia-Wei, Wang } 291489774ffSChia-Wei, Wang 292489774ffSChia-Wei, Wang lpc_ctrl->regmap = syscon_node_to_regmap(np); 293524feb79SPatrick Venture if (IS_ERR(lpc_ctrl->regmap)) { 294524feb79SPatrick Venture dev_err(dev, "Couldn't get regmap\n"); 295524feb79SPatrick Venture return -ENODEV; 296524feb79SPatrick Venture } 297524feb79SPatrick Venture 298*51e321feSYang Yingliang if (of_device_is_compatible(dev->of_node, "aspeed,ast2600-lpc-ctrl")) { 299*51e321feSYang Yingliang lpc_ctrl->fwh2ahb = true; 300*51e321feSYang Yingliang 301*51e321feSYang Yingliang lpc_ctrl->scu = syscon_regmap_lookup_by_compatible("aspeed,ast2600-scu"); 302*51e321feSYang Yingliang if (IS_ERR(lpc_ctrl->scu)) { 303*51e321feSYang Yingliang dev_err(dev, "couldn't find scu\n"); 304*51e321feSYang Yingliang return PTR_ERR(lpc_ctrl->scu); 305*51e321feSYang Yingliang } 306*51e321feSYang Yingliang } 307*51e321feSYang Yingliang 308524feb79SPatrick Venture lpc_ctrl->clk = devm_clk_get(dev, NULL); 309524feb79SPatrick Venture if (IS_ERR(lpc_ctrl->clk)) { 310524feb79SPatrick Venture dev_err(dev, "couldn't get clock\n"); 311524feb79SPatrick Venture return PTR_ERR(lpc_ctrl->clk); 312524feb79SPatrick Venture } 313524feb79SPatrick Venture rc = clk_prepare_enable(lpc_ctrl->clk); 314524feb79SPatrick Venture if (rc) { 315524feb79SPatrick Venture dev_err(dev, "couldn't enable clock\n"); 316524feb79SPatrick Venture return rc; 317524feb79SPatrick Venture } 318524feb79SPatrick Venture 319524feb79SPatrick Venture lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR; 320524feb79SPatrick Venture lpc_ctrl->miscdev.name = DEVICE_NAME; 321524feb79SPatrick Venture lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops; 322524feb79SPatrick Venture lpc_ctrl->miscdev.parent = dev; 323524feb79SPatrick Venture rc = misc_register(&lpc_ctrl->miscdev); 324524feb79SPatrick Venture if (rc) { 325524feb79SPatrick Venture dev_err(dev, "Unable to register device\n"); 326524feb79SPatrick Venture goto err; 327524feb79SPatrick Venture } 328524feb79SPatrick Venture 329524feb79SPatrick Venture return 0; 330524feb79SPatrick Venture 331524feb79SPatrick Venture err: 332524feb79SPatrick Venture clk_disable_unprepare(lpc_ctrl->clk); 333524feb79SPatrick Venture return rc; 334524feb79SPatrick Venture } 335524feb79SPatrick Venture 336524feb79SPatrick Venture static int aspeed_lpc_ctrl_remove(struct platform_device *pdev) 337524feb79SPatrick Venture { 338524feb79SPatrick Venture struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev); 339524feb79SPatrick Venture 340524feb79SPatrick Venture misc_deregister(&lpc_ctrl->miscdev); 341524feb79SPatrick Venture clk_disable_unprepare(lpc_ctrl->clk); 342524feb79SPatrick Venture 343524feb79SPatrick Venture return 0; 344524feb79SPatrick Venture } 345524feb79SPatrick Venture 346524feb79SPatrick Venture static const struct of_device_id aspeed_lpc_ctrl_match[] = { 347524feb79SPatrick Venture { .compatible = "aspeed,ast2400-lpc-ctrl" }, 348524feb79SPatrick Venture { .compatible = "aspeed,ast2500-lpc-ctrl" }, 34944ddc4deSBrad Bishop { .compatible = "aspeed,ast2600-lpc-ctrl" }, 350524feb79SPatrick Venture { }, 351524feb79SPatrick Venture }; 352524feb79SPatrick Venture 353524feb79SPatrick Venture static struct platform_driver aspeed_lpc_ctrl_driver = { 354524feb79SPatrick Venture .driver = { 355524feb79SPatrick Venture .name = DEVICE_NAME, 356524feb79SPatrick Venture .of_match_table = aspeed_lpc_ctrl_match, 357524feb79SPatrick Venture }, 358524feb79SPatrick Venture .probe = aspeed_lpc_ctrl_probe, 359524feb79SPatrick Venture .remove = aspeed_lpc_ctrl_remove, 360524feb79SPatrick Venture }; 361524feb79SPatrick Venture 362524feb79SPatrick Venture module_platform_driver(aspeed_lpc_ctrl_driver); 363524feb79SPatrick Venture 364524feb79SPatrick Venture MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match); 365524feb79SPatrick Venture MODULE_LICENSE("GPL"); 366524feb79SPatrick Venture MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>"); 367cd460be0SJoel Stanley MODULE_DESCRIPTION("Control for ASPEED LPC HOST to BMC mappings"); 368