2 * Copyright (C) 2017 Hisilicon Limited, All Rights Reserved.
3 * Author: Zhichang Yuan <yuanzhichang@hisilicon.com>
4 * Author: Zou Rongrong <zourongrong@huawei.com>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/acpi.h>
20 #include <linux/console.h>
21 #include <linux/delay.h>
23 #include <linux/libio.h>
24 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_platform.h>
28 #include <linux/pci.h>
29 #include <linux/serial_8250.h>
30 #include <linux/slab.h>
33 * Setting this bit means each IO operation will target to a
34 * different port address:
35 * 0 means repeatedly IO operations will stick on the same port,
38 #define FG_INCRADDR_LPC 0x02
40 struct lpc_cycle_para
{
42 unsigned int csize
; /* the data length of each operation */
46 spinlock_t cycle_lock
;
47 void __iomem
*membase
;
48 struct libio_range
*io_host
;
51 /* bounds of the LPC bus address range */
52 #define LPC_MIN_BUS_RANGE 0x0
55 * The default maximal IO size for Hip06/Hip07 LPC bus.
56 * Defining the I/O range size as 0x400 here should be sufficient for
57 * all peripherals under the bus.
59 #define LPC_BUS_IO_SIZE 0x400
61 /* The maximum continuous cycles per burst */
62 #define LPC_MAX_BURST 16
63 /* The IO cycle counts supported is four per operation at maximum */
64 #define LPC_MAX_DULEN 4
65 #if LPC_MAX_DULEN > LPC_MAX_BURST
66 #error "LPC.. MAX_DULEN must be not bigger than MAX_OPCNT!"
69 #if LPC_MAX_BURST % LPC_MAX_DULEN
70 #error "LPC.. LPC_MAX_BURST must be multiple of LPC_MAX_DULEN!"
73 #define LPC_REG_START 0x00 /* start a new LPC cycle */
74 #define LPC_REG_OP_STATUS 0x04 /* the current LPC status */
75 #define LPC_REG_IRQ_ST 0x08 /* interrupt enable&status */
76 #define LPC_REG_OP_LEN 0x10 /* how many LPC cycles each start */
77 #define LPC_REG_CMD 0x14 /* command for the required LPC cycle */
78 #define LPC_REG_ADDR 0x20 /* LPC target address */
79 #define LPC_REG_WDATA 0x24 /* data to be written */
80 #define LPC_REG_RDATA 0x28 /* data coming from peer */
83 /* The command register fields */
84 #define LPC_CMD_SAMEADDR 0x08
85 #define LPC_CMD_TYPE_IO 0x00
86 #define LPC_CMD_WRITE 0x01
87 #define LPC_CMD_READ 0x00
88 /* the bit attribute is W1C. 1 represents OK. */
89 #define LPC_STAT_BYIRQ 0x02
91 #define LPC_STATUS_IDLE 0x01
92 #define LPC_OP_FINISHED 0x02
94 #define START_WORK 0x01
97 * The minimal nanosecond interval for each query on LPC cycle status.
99 #define LPC_NSEC_PERWAIT 100
101 * The maximum waiting time is about 128us.
102 * It is specific for stream I/O, such as ins.
103 * The fastest IO cycle time is about 390ns, but the worst case will wait
104 * for extra 256 lpc clocks, so (256 + 13) * 30ns = 8 us. The maximum
105 * burst cycles is 16. So, the maximum waiting time is about 128us under
107 * choose 1300 as the maximum.
109 #define LPC_MAX_WAITCNT 1300
110 /* About 10us. This is specific for single IO operation, such as inb. */
111 #define LPC_PEROP_WAITCNT 100
114 static inline int wait_lpc_idle(unsigned char *mbase
,
115 unsigned int waitcnt
) {
119 ndelay(LPC_NSEC_PERWAIT
);
120 opstatus
= readl(mbase
+ LPC_REG_OP_STATUS
);
121 if (opstatus
& LPC_STATUS_IDLE
)
122 return (opstatus
& LPC_OP_FINISHED
) ? 0 : (-EIO
);
128 * hisilpc_target_in - trigger a series of lpc cycles to read required data
129 * from target peripheral.
130 * @pdev: pointer to hisi lpc device
131 * @para: some parameters used to control the lpc I/O operations
132 * @ptaddr: the lpc I/O target port address
133 * @buf: where the read back data is stored
134 * @opcnt: how many I/O operations required in this calling
136 * Only one byte data is read each I/O operation.
138 * Returns 0 on success, non-zero on fail.
142 hisilpc_target_in(struct hisilpc_dev
*lpcdev
, struct lpc_cycle_para
*para
,
143 unsigned long ptaddr
, unsigned char *buf
,
146 unsigned long cnt_per_trans
;
147 unsigned int cmd_word
;
148 unsigned int waitcnt
;
151 if (!buf
|| !opcnt
|| !para
|| !para
->csize
|| !lpcdev
)
154 cmd_word
= LPC_CMD_TYPE_IO
| LPC_CMD_READ
;
155 waitcnt
= LPC_PEROP_WAITCNT
;
156 if (!(para
->opflags
& FG_INCRADDR_LPC
)) {
157 cmd_word
|= LPC_CMD_SAMEADDR
;
158 waitcnt
= LPC_MAX_WAITCNT
;
162 cnt_per_trans
= (para
->csize
== 1) ? opcnt
: para
->csize
;
163 for (; opcnt
&& !ret
; cnt_per_trans
= para
->csize
) {
166 /* whole operation must be atomic */
167 spin_lock_irqsave(&lpcdev
->cycle_lock
, flags
);
169 writel_relaxed(cnt_per_trans
, lpcdev
->membase
+ LPC_REG_OP_LEN
);
171 writel_relaxed(cmd_word
, lpcdev
->membase
+ LPC_REG_CMD
);
173 writel_relaxed(ptaddr
, lpcdev
->membase
+ LPC_REG_ADDR
);
175 writel(START_WORK
, lpcdev
->membase
+ LPC_REG_START
);
177 /* whether the operation is finished */
178 ret
= wait_lpc_idle(lpcdev
->membase
, waitcnt
);
180 opcnt
-= cnt_per_trans
;
181 for (cnt_per_trans
--; cnt_per_trans
--; buf
++)
182 *buf
= readb_relaxed(lpcdev
->membase
+
184 *buf
= readb(lpcdev
->membase
+ LPC_REG_RDATA
);
187 spin_unlock_irqrestore(&lpcdev
->cycle_lock
, flags
);
194 * hisilpc_target_out - trigger a series of lpc cycles to write required
195 * data to target peripheral.
196 * @pdev: pointer to hisi lpc device
197 * @para: some parameters used to control the lpc I/O operations
198 * @ptaddr: the lpc I/O target port address
199 * @buf: where the data to be written is stored
200 * @opcnt: how many I/O operations required
202 * Only one byte data is read each I/O operation.
204 * Returns 0 on success, non-zero on fail.
208 hisilpc_target_out(struct hisilpc_dev
*lpcdev
, struct lpc_cycle_para
*para
,
209 unsigned long ptaddr
, const unsigned char *buf
,
212 unsigned long cnt_per_trans
;
213 unsigned int cmd_word
;
214 unsigned int waitcnt
;
217 if (!buf
|| !opcnt
|| !para
|| !lpcdev
)
220 /* default is increasing address */
221 cmd_word
= LPC_CMD_TYPE_IO
| LPC_CMD_WRITE
;
222 waitcnt
= LPC_PEROP_WAITCNT
;
223 if (!(para
->opflags
& FG_INCRADDR_LPC
)) {
224 cmd_word
|= LPC_CMD_SAMEADDR
;
225 waitcnt
= LPC_MAX_WAITCNT
;
229 cnt_per_trans
= (para
->csize
== 1) ? opcnt
: para
->csize
;
230 for (; opcnt
&& !ret
; cnt_per_trans
= para
->csize
) {
233 spin_lock_irqsave(&lpcdev
->cycle_lock
, flags
);
235 writel_relaxed(cnt_per_trans
, lpcdev
->membase
+ LPC_REG_OP_LEN
);
236 writel_relaxed(cmd_word
, lpcdev
->membase
+ LPC_REG_CMD
);
237 writel_relaxed(ptaddr
, lpcdev
->membase
+ LPC_REG_ADDR
);
239 opcnt
-= cnt_per_trans
;
240 for (; cnt_per_trans
--; buf
++)
241 writeb_relaxed(*buf
, lpcdev
->membase
+ LPC_REG_WDATA
);
243 writel(START_WORK
, lpcdev
->membase
+ LPC_REG_START
);
245 /* whether the operation is finished */
246 ret
= wait_lpc_idle(lpcdev
->membase
, waitcnt
);
248 spin_unlock_irqrestore(&lpcdev
->cycle_lock
, flags
);
254 static inline unsigned long
255 hisi_lpc_pio_to_addr(struct hisilpc_dev
*lpcdev
, unsigned long pio
)
257 return pio
- lpcdev
->io_host
->io_start
+ lpcdev
->io_host
->hw_start
;
262 * hisilpc_comm_in - read/input the data from the I/O peripheral
264 * @devobj: pointer to the device information relevant to LPC controller.
265 * @pio: the target I/O port address.
266 * @dlen: the data length required to read from the target I/O port.
268 * when succeed, the data read back is stored in buffer pointed by inbuf.
269 * For inb, return the data read from I/O or -1 when error occur.
271 static u32
hisilpc_comm_in(void *devobj
, unsigned long pio
, size_t dlen
)
275 unsigned long ptaddr
;
276 unsigned char *newbuf
;
277 struct lpc_cycle_para iopara
;
278 struct hisilpc_dev
*lpcdev
= devobj
;
280 if (!lpcdev
|| !dlen
|| dlen
> LPC_MAX_DULEN
|| (dlen
& (dlen
- 1)))
283 /* the local buffer must be enough for one data unit */
284 if (sizeof(rd_data
) < dlen
)
287 newbuf
= (unsigned char *)&rd_data
;
289 ptaddr
= hisi_lpc_pio_to_addr(lpcdev
, pio
);
291 iopara
.opflags
= FG_INCRADDR_LPC
;
294 ret
= hisilpc_target_in(lpcdev
, &iopara
, ptaddr
, newbuf
, dlen
);
298 return le32_to_cpu(rd_data
);
302 * hisilpc_comm_out - output the data whose maximum length is four bytes
303 to the I/O peripheral through the LPC host.
304 * @devobj: pointer to the device information relevant to LPC controller.
305 * @outval: a value to be outputted from caller, maximum is four bytes.
306 * @pio: the target I/O port address.
307 * @dlen: the data length required writing to the target I/O port.
309 * This function is corresponding to out(b,w,l) only
312 static void hisilpc_comm_out(void *devobj
, unsigned long pio
,
313 u32 outval
, size_t dlen
)
315 unsigned long ptaddr
;
316 struct hisilpc_dev
*lpcdev
= devobj
;
317 struct lpc_cycle_para iopara
;
318 const unsigned char *newbuf
;
320 if (!lpcdev
|| !dlen
|| dlen
> LPC_MAX_DULEN
)
323 if (sizeof(outval
) < dlen
)
326 outval
= cpu_to_le32(outval
);
328 newbuf
= (const unsigned char *)&outval
;
329 ptaddr
= hisi_lpc_pio_to_addr(lpcdev
, pio
);
331 iopara
.opflags
= FG_INCRADDR_LPC
;
334 hisilpc_target_out(lpcdev
, &iopara
, ptaddr
, newbuf
, dlen
);
338 * hisilpc_comm_ins - read/input the data in buffer to the I/O
339 * peripheral through LPC, it corresponds to ins(b,w,l)
340 * @devobj: pointer to the device information relevant to LPC controller.
341 * @pio: the target I/O port address.
342 * @inbuf: a buffer where read/input data bytes are stored.
343 * @dlen: the data length required writing to the target I/O port.
344 * @count: how many data units whose length is dlen will be read.
348 hisilpc_comm_ins(void *devobj
, unsigned long pio
, void *inbuf
,
349 size_t dlen
, unsigned int count
)
351 struct hisilpc_dev
*lpcdev
= devobj
;
352 struct lpc_cycle_para iopara
;
353 unsigned char *newbuf
;
354 unsigned int loopcnt
, cntleft
;
355 unsigned long ptaddr
;
357 if (!lpcdev
|| !inbuf
|| !count
|| !dlen
||
358 dlen
> LPC_MAX_DULEN
|| (dlen
& (dlen
- 1)) || count
% dlen
)
363 iopara
.opflags
|= FG_INCRADDR_LPC
;
366 ptaddr
= hisi_lpc_pio_to_addr(lpcdev
, pio
);
367 newbuf
= (unsigned char *)inbuf
;
369 * ensure data stream whose length is multiple of dlen to be processed
372 cntleft
= count
* dlen
;
376 loopcnt
= (cntleft
>= LPC_MAX_BURST
) ? LPC_MAX_BURST
: cntleft
;
377 ret
= hisilpc_target_in(lpcdev
, &iopara
, ptaddr
,
389 * hisilpc_comm_outs - write/output the data in buffer to the I/O
390 * peripheral through LPC, it corresponds to outs(b,w,l)
391 * @devobj: pointer to the device information relevant to LPC controller.
392 * @pio: the target I/O port address.
393 * @outbuf: a buffer where write/output data bytes are stored.
394 * @dlen: the data length required writing to the target I/O port .
395 * @count: how many data units whose length is dlen will be written.
399 hisilpc_comm_outs(void *devobj
, unsigned long pio
, const void *outbuf
,
400 size_t dlen
, unsigned int count
)
402 struct hisilpc_dev
*lpcdev
= devobj
;
403 struct lpc_cycle_para iopara
;
404 const unsigned char *newbuf
;
405 unsigned int loopcnt
, cntleft
;
406 unsigned long ptaddr
;
408 if (!lpcdev
|| !outbuf
|| !count
|| !dlen
||
409 dlen
> LPC_MAX_DULEN
|| (dlen
& (dlen
- 1)) || count
% dlen
)
414 iopara
.opflags
|= FG_INCRADDR_LPC
;
417 ptaddr
= hisi_lpc_pio_to_addr(lpcdev
, pio
);
418 newbuf
= (unsigned char *)outbuf
;
420 * ensure data stream whose length is multiple of dlen to be processed
423 cntleft
= count
* dlen
;
425 loopcnt
= (cntleft
>= LPC_MAX_BURST
) ? LPC_MAX_BURST
: cntleft
;
426 if (hisilpc_target_out(lpcdev
, &iopara
, ptaddr
, newbuf
,
434 static struct libio_ops hisi_lpc_ops
= {
435 .pfin
= hisilpc_comm_in
,
436 .pfout
= hisilpc_comm_out
,
437 .pfins
= hisilpc_comm_ins
,
438 .pfouts
= hisilpc_comm_outs
,
442 static int hisilpc_host_io_register(struct device
*dev
)
444 struct libio_range
*range
, *tmprange
;
447 * indirectIO bus was detected, time to request the linux virtual
450 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
453 range
->node
= dev
->fwnode
;
454 range
->flags
= IO_HOST_INDIRECT
;
455 range
->size
= LPC_BUS_IO_SIZE
;
456 range
->hw_start
= LPC_MIN_BUS_RANGE
;
458 tmprange
= register_libio_range(range
);
459 if (tmprange
!= range
) {
461 if (IS_ERR(tmprange
))
466 * For ACPI children, translate the bus-local I/O range to logical
467 * I/O range and set it as the current resource before the children
470 if (has_acpi_companion(dev
)) {
471 struct acpi_device
*root
, *child
;
473 root
= to_acpi_device_node(dev
->fwnode
);
474 /* For hisilpc, only care about the sons of host. */
475 list_for_each_entry(child
, &root
->children
, node
) {
478 ret
= acpi_set_libio_resource(&child
->dev
, &root
->dev
);
480 dev_err(&child
->dev
, "set resource failed..\n");
490 * hisilpc_probe - the probe callback function for hisi lpc device,
491 * will finish all the initialization.
492 * @pdev: the platform device corresponding to hisi lpc
494 * Returns 0 on success, non-zero on fail.
497 static int hisilpc_probe(struct platform_device
*pdev
)
499 struct device
*dev
= &pdev
->dev
;
500 struct resource
*res
;
501 struct hisilpc_dev
*lpcdev
;
504 dev_info(dev
, "probing...\n");
506 lpcdev
= devm_kzalloc(dev
, sizeof(struct hisilpc_dev
), GFP_KERNEL
);
510 spin_lock_init(&lpcdev
->cycle_lock
);
512 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
514 dev_err(dev
, "no MEM resource\n");
518 lpcdev
->membase
= devm_ioremap_resource(dev
, res
);
519 if (IS_ERR(lpcdev
->membase
)) {
520 dev_err(dev
, "remap failed\n");
521 return PTR_ERR(lpcdev
->membase
);
524 lpcdev
->io_host
= find_io_range_from_fwnode(dev
->fwnode
);
525 if (!lpcdev
->io_host
) {
526 dev_err(dev
, "Hisilpc IO hasn't registered!\n");
530 lpcdev
->io_host
->devpara
= lpcdev
;
531 lpcdev
->io_host
->ops
= &hisi_lpc_ops
;
533 platform_set_drvdata(pdev
, lpcdev
);
536 * It is time to start the children scannings....
538 if (!has_acpi_companion(dev
)) {
539 ret
= of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
541 dev_err(dev
, "OF: enumerate LPC bus fail(%d)\n", ret
);
545 dev_info(dev
, "hslpc end probing. range[%pa - sz:%pa]\n",
546 &lpcdev
->io_host
->io_start
,
547 &lpcdev
->io_host
->size
);
549 dev_info(dev
, "hslpc probing is fail(%d)\n", ret
);
551 * When LPC probing is not completely successful, set 'devpara'
552 * as NULL. This will make all the LPC I/O return failure
553 * directly without any hardware operations. It will block
554 * some peripherals which had not finished the initialization
555 * manipulate I/O for safety.
557 lpcdev
->io_host
->devpara
= NULL
;
563 static const struct of_device_id hisilpc_of_match
[] = {
564 { .compatible
= "hisilicon,hip06-lpc", },
565 { .compatible
= "hisilicon,hip07-lpc", },
569 static const struct acpi_device_id hisilpc_acpi_match
[] = {
574 static struct platform_driver hisilpc_driver
= {
577 .of_match_table
= hisilpc_of_match
,
578 .acpi_match_table
= ACPI_PTR(hisilpc_acpi_match
),
580 .probe
= hisilpc_probe
,
584 * hisilpc_bus_platform_notify -- notify callback function specific for
585 * hisi-lpc bus. Here, will register linux virtual
586 * PIO for the bus detected, then the bus children
587 * can translate their bus-local IO to linux PIO.
589 static int hisilpc_bus_platform_notify(struct notifier_block
*nb
,
590 unsigned long action
, void *data
)
592 struct device
*dev
= data
;
595 if (!is_of_node(dev
->fwnode
) && !is_acpi_node(dev
->fwnode
))
598 if (action
!= BUS_NOTIFY_ADD_DEVICE
)
601 /* whether the device notified is hisi-lpc? */
602 if (has_acpi_companion(dev
)) {
603 if (!acpi_match_device(hisilpc_acpi_match
, dev
))
606 if (!of_match_node(hisilpc_of_match
, dev
->of_node
))
610 /* register the LPC host PIO resources */
611 ret
= hisilpc_host_io_register(dev
);
613 dev_err(dev
, "host PIO registration failed!\n");
617 dev_info(dev
, "hisilpc notifier processes OK!\n");
621 static struct notifier_block hisilpc_preinit_nb
= {
622 .notifier_call
= hisilpc_bus_platform_notify
,
625 static int __init
hisilpc_init(void)
629 ret
= bus_register_notifier(&platform_bus_type
, &hisilpc_preinit_nb
);
631 ret
= platform_driver_register(&hisilpc_driver
);
637 * This initial funtion must be called before the platform bus scanning to make
638 * the lpc-relevant I/O resource is ready for the device enumeration.
640 arch_initcall(hisilpc_init
);