1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2017 IBM Corporation
7 #include <linux/log2.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/poll.h>
15 #include <linux/regmap.h>
17 #include <linux/aspeed-lpc-ctrl.h>
19 #define DEVICE_NAME "aspeed-lpc-ctrl"
22 #define HICR5_ENL2H BIT(8)
23 #define HICR5_ENFWH BIT(10)
26 #define SW_FWH2AHB BIT(17)
31 struct aspeed_lpc_ctrl
{
32 struct miscdevice miscdev
;
33 struct regmap
*regmap
;
36 resource_size_t mem_size
;
42 static struct aspeed_lpc_ctrl
*file_aspeed_lpc_ctrl(struct file
*file
)
44 return container_of(file
->private_data
, struct aspeed_lpc_ctrl
,
48 static int aspeed_lpc_ctrl_mmap(struct file
*file
, struct vm_area_struct
*vma
)
50 struct aspeed_lpc_ctrl
*lpc_ctrl
= file_aspeed_lpc_ctrl(file
);
51 unsigned long vsize
= vma
->vm_end
- vma
->vm_start
;
52 pgprot_t prot
= vma
->vm_page_prot
;
54 if (vma
->vm_pgoff
+ vsize
> lpc_ctrl
->mem_base
+ lpc_ctrl
->mem_size
)
57 /* ast2400/2500 AHB accesses are not cache coherent */
58 prot
= pgprot_noncached(prot
);
60 if (remap_pfn_range(vma
, vma
->vm_start
,
61 (lpc_ctrl
->mem_base
>> PAGE_SHIFT
) + vma
->vm_pgoff
,
68 static long aspeed_lpc_ctrl_ioctl(struct file
*file
, unsigned int cmd
,
71 struct aspeed_lpc_ctrl
*lpc_ctrl
= file_aspeed_lpc_ctrl(file
);
72 struct device
*dev
= file
->private_data
;
73 void __user
*p
= (void __user
*)param
;
74 struct aspeed_lpc_ctrl_mapping map
;
79 if (copy_from_user(&map
, p
, sizeof(map
)))
86 case ASPEED_LPC_CTRL_IOCTL_GET_SIZE
:
87 /* The flash windows don't report their size */
88 if (map
.window_type
!= ASPEED_LPC_CTRL_WINDOW_MEMORY
)
91 /* Support more than one window id in the future */
92 if (map
.window_id
!= 0)
95 /* If memory-region is not described in device tree */
96 if (!lpc_ctrl
->mem_size
) {
97 dev_dbg(dev
, "Didn't find reserved memory\n");
101 map
.size
= lpc_ctrl
->mem_size
;
103 return copy_to_user(p
, &map
, sizeof(map
)) ? -EFAULT
: 0;
104 case ASPEED_LPC_CTRL_IOCTL_MAP
:
107 * The top half of HICR7 is the MSB of the BMC address of the
109 * The bottom half of HICR7 is the MSB of the HOST LPC
110 * firmware space address of the mapping.
112 * The 1 bits in the top of half of HICR8 represent the bits
113 * (in the requested address) that should be ignored and
114 * replaced with those from the top half of HICR7.
115 * The 1 bits in the bottom half of HICR8 represent the bits
116 * (in the requested address) that should be kept and pass
117 * into the BMC address space.
121 * It doesn't make sense to talk about a size or offset with
122 * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
123 * bits of addresses and sizes.
126 if ((map
.size
& 0x0000ffff) || (map
.offset
& 0x0000ffff))
130 * Because of the way the masks work in HICR8 offset has to
131 * be a multiple of size.
133 if (map
.offset
& (map
.size
- 1))
136 if (map
.window_type
== ASPEED_LPC_CTRL_WINDOW_FLASH
) {
137 if (!lpc_ctrl
->pnor_size
) {
138 dev_dbg(dev
, "Didn't find host pnor flash\n");
141 addr
= lpc_ctrl
->pnor_base
;
142 size
= lpc_ctrl
->pnor_size
;
143 } else if (map
.window_type
== ASPEED_LPC_CTRL_WINDOW_MEMORY
) {
144 /* If memory-region is not described in device tree */
145 if (!lpc_ctrl
->mem_size
) {
146 dev_dbg(dev
, "Didn't find reserved memory\n");
149 addr
= lpc_ctrl
->mem_base
;
150 size
= lpc_ctrl
->mem_size
;
155 /* Check overflow first! */
156 if (map
.offset
+ map
.size
< map
.offset
||
157 map
.offset
+ map
.size
> size
)
160 if (map
.size
== 0 || map
.size
> size
)
166 * addr (host lpc address) is safe regardless of values. This
167 * simply changes the address the host has to request on its
168 * side of the LPC bus. This cannot impact the hosts own
169 * memory space by surprise as LPC specific accessors are
170 * required. The only strange thing that could be done is
171 * setting the lower 16 bits but the shift takes care of that.
174 rc
= regmap_write(lpc_ctrl
->regmap
, HICR7
,
175 (addr
| (map
.addr
>> 16)));
179 rc
= regmap_write(lpc_ctrl
->regmap
, HICR8
,
180 (~(map
.size
- 1)) | ((map
.size
>> 16) - 1));
185 * Switch to FWH2AHB mode, AST2600 only.
187 * The other bits in this register are interrupt status bits
188 * that are cleared by writing 1. As we don't want to clear
189 * them, set only the bit of interest.
191 if (lpc_ctrl
->fwh2ahb
)
192 regmap_write(lpc_ctrl
->regmap
, HICR6
, SW_FWH2AHB
);
195 * Enable LPC FHW cycles. This is required for the host to
196 * access the regions specified.
198 return regmap_update_bits(lpc_ctrl
->regmap
, HICR5
,
199 HICR5_ENFWH
| HICR5_ENL2H
,
200 HICR5_ENFWH
| HICR5_ENL2H
);
206 static const struct file_operations aspeed_lpc_ctrl_fops
= {
207 .owner
= THIS_MODULE
,
208 .mmap
= aspeed_lpc_ctrl_mmap
,
209 .unlocked_ioctl
= aspeed_lpc_ctrl_ioctl
,
212 static int aspeed_lpc_ctrl_probe(struct platform_device
*pdev
)
214 struct aspeed_lpc_ctrl
*lpc_ctrl
;
215 struct device_node
*node
;
216 struct resource resm
;
222 lpc_ctrl
= devm_kzalloc(dev
, sizeof(*lpc_ctrl
), GFP_KERNEL
);
226 /* If flash is described in device tree then store */
227 node
= of_parse_phandle(dev
->of_node
, "flash", 0);
229 dev_dbg(dev
, "Didn't find host pnor flash node\n");
231 rc
= of_address_to_resource(node
, 1, &resm
);
234 dev_err(dev
, "Couldn't address to resource for flash\n");
238 lpc_ctrl
->pnor_size
= resource_size(&resm
);
239 lpc_ctrl
->pnor_base
= resm
.start
;
243 dev_set_drvdata(&pdev
->dev
, lpc_ctrl
);
245 /* If memory-region is described in device tree then store */
246 node
= of_parse_phandle(dev
->of_node
, "memory-region", 0);
248 dev_dbg(dev
, "Didn't find reserved memory\n");
250 rc
= of_address_to_resource(node
, 0, &resm
);
253 dev_err(dev
, "Couldn't address to resource for reserved memory\n");
257 lpc_ctrl
->mem_size
= resource_size(&resm
);
258 lpc_ctrl
->mem_base
= resm
.start
;
260 if (!is_power_of_2(lpc_ctrl
->mem_size
)) {
261 dev_err(dev
, "Reserved memory size must be a power of 2, got %u\n",
262 (unsigned int)lpc_ctrl
->mem_size
);
266 if (!IS_ALIGNED(lpc_ctrl
->mem_base
, lpc_ctrl
->mem_size
)) {
267 dev_err(dev
, "Reserved memory must be naturally aligned for size %u\n",
268 (unsigned int)lpc_ctrl
->mem_size
);
273 lpc_ctrl
->regmap
= syscon_node_to_regmap(
274 pdev
->dev
.parent
->of_node
);
275 if (IS_ERR(lpc_ctrl
->regmap
)) {
276 dev_err(dev
, "Couldn't get regmap\n");
280 lpc_ctrl
->clk
= devm_clk_get(dev
, NULL
);
281 if (IS_ERR(lpc_ctrl
->clk
)) {
282 dev_err(dev
, "couldn't get clock\n");
283 return PTR_ERR(lpc_ctrl
->clk
);
285 rc
= clk_prepare_enable(lpc_ctrl
->clk
);
287 dev_err(dev
, "couldn't enable clock\n");
291 if (of_device_is_compatible(dev
->of_node
, "aspeed,ast2600-lpc-ctrl"))
292 lpc_ctrl
->fwh2ahb
= true;
294 lpc_ctrl
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
295 lpc_ctrl
->miscdev
.name
= DEVICE_NAME
;
296 lpc_ctrl
->miscdev
.fops
= &aspeed_lpc_ctrl_fops
;
297 lpc_ctrl
->miscdev
.parent
= dev
;
298 rc
= misc_register(&lpc_ctrl
->miscdev
);
300 dev_err(dev
, "Unable to register device\n");
307 clk_disable_unprepare(lpc_ctrl
->clk
);
311 static int aspeed_lpc_ctrl_remove(struct platform_device
*pdev
)
313 struct aspeed_lpc_ctrl
*lpc_ctrl
= dev_get_drvdata(&pdev
->dev
);
315 misc_deregister(&lpc_ctrl
->miscdev
);
316 clk_disable_unprepare(lpc_ctrl
->clk
);
321 static const struct of_device_id aspeed_lpc_ctrl_match
[] = {
322 { .compatible
= "aspeed,ast2400-lpc-ctrl" },
323 { .compatible
= "aspeed,ast2500-lpc-ctrl" },
324 { .compatible
= "aspeed,ast2600-lpc-ctrl" },
328 static struct platform_driver aspeed_lpc_ctrl_driver
= {
331 .of_match_table
= aspeed_lpc_ctrl_match
,
333 .probe
= aspeed_lpc_ctrl_probe
,
334 .remove
= aspeed_lpc_ctrl_remove
,
337 module_platform_driver(aspeed_lpc_ctrl_driver
);
339 MODULE_DEVICE_TABLE(of
, aspeed_lpc_ctrl_match
);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
342 MODULE_DESCRIPTION("Control for ASPEED LPC HOST to BMC mappings");