xref: /linux/drivers/platform/mips/rs780e-acpi.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
10cfd2440SJiaxun Yang // SPDX-License-Identifier: GPL-2.0
20cfd2440SJiaxun Yang #include <linux/io.h>
30cfd2440SJiaxun Yang #include <linux/init.h>
40cfd2440SJiaxun Yang #include <linux/ioport.h>
50cfd2440SJiaxun Yang #include <linux/export.h>
60cfd2440SJiaxun Yang #include <linux/of.h>
70cfd2440SJiaxun Yang #include <linux/platform_device.h>
80cfd2440SJiaxun Yang 
90cfd2440SJiaxun Yang static unsigned long acpi_iobase;
100cfd2440SJiaxun Yang 
110cfd2440SJiaxun Yang #define ACPI_PM_EVT_BLK         (acpi_iobase + 0x00) /* 4 bytes */
120cfd2440SJiaxun Yang #define ACPI_PM_CNT_BLK         (acpi_iobase + 0x04) /* 2 bytes */
130cfd2440SJiaxun Yang #define ACPI_PMA_CNT_BLK        (acpi_iobase + 0x0F) /* 1 byte */
140cfd2440SJiaxun Yang #define ACPI_PM_TMR_BLK         (acpi_iobase + 0x18) /* 4 bytes */
150cfd2440SJiaxun Yang #define ACPI_GPE0_BLK           (acpi_iobase + 0x10) /* 8 bytes */
160cfd2440SJiaxun Yang #define ACPI_END                (acpi_iobase + 0x80)
170cfd2440SJiaxun Yang 
180cfd2440SJiaxun Yang #define PM_INDEX        0xCD6
190cfd2440SJiaxun Yang #define PM_DATA         0xCD7
200cfd2440SJiaxun Yang #define PM2_INDEX       0xCD0
210cfd2440SJiaxun Yang #define PM2_DATA        0xCD1
220cfd2440SJiaxun Yang 
pmio_write_index(u16 index,u8 reg,u8 value)230cfd2440SJiaxun Yang static void pmio_write_index(u16 index, u8 reg, u8 value)
240cfd2440SJiaxun Yang {
250cfd2440SJiaxun Yang 	outb(reg, index);
260cfd2440SJiaxun Yang 	outb(value, index + 1);
270cfd2440SJiaxun Yang }
280cfd2440SJiaxun Yang 
pmio_read_index(u16 index,u8 reg)290cfd2440SJiaxun Yang static u8 pmio_read_index(u16 index, u8 reg)
300cfd2440SJiaxun Yang {
310cfd2440SJiaxun Yang 	outb(reg, index);
320cfd2440SJiaxun Yang 	return inb(index + 1);
330cfd2440SJiaxun Yang }
340cfd2440SJiaxun Yang 
pm_iowrite(u8 reg,u8 value)35*2657bc63SArnd Bergmann static void pm_iowrite(u8 reg, u8 value)
360cfd2440SJiaxun Yang {
370cfd2440SJiaxun Yang 	pmio_write_index(PM_INDEX, reg, value);
380cfd2440SJiaxun Yang }
390cfd2440SJiaxun Yang 
pm_ioread(u8 reg)40*2657bc63SArnd Bergmann static u8 pm_ioread(u8 reg)
410cfd2440SJiaxun Yang {
420cfd2440SJiaxun Yang 	return pmio_read_index(PM_INDEX, reg);
430cfd2440SJiaxun Yang }
440cfd2440SJiaxun Yang 
pm2_iowrite(u8 reg,u8 value)45*2657bc63SArnd Bergmann static void pm2_iowrite(u8 reg, u8 value)
460cfd2440SJiaxun Yang {
470cfd2440SJiaxun Yang 	pmio_write_index(PM2_INDEX, reg, value);
480cfd2440SJiaxun Yang }
490cfd2440SJiaxun Yang 
pm2_ioread(u8 reg)50*2657bc63SArnd Bergmann static u8 pm2_ioread(u8 reg)
510cfd2440SJiaxun Yang {
520cfd2440SJiaxun Yang 	return pmio_read_index(PM2_INDEX, reg);
530cfd2440SJiaxun Yang }
540cfd2440SJiaxun Yang 
acpi_hw_clear_status(void)550cfd2440SJiaxun Yang static void acpi_hw_clear_status(void)
560cfd2440SJiaxun Yang {
570cfd2440SJiaxun Yang 	u16 value;
580cfd2440SJiaxun Yang 
590cfd2440SJiaxun Yang 	/* PMStatus: Clear WakeStatus/PwrBtnStatus */
600cfd2440SJiaxun Yang 	value = inw(ACPI_PM_EVT_BLK);
610cfd2440SJiaxun Yang 	value |= (1 << 8 | 1 << 15);
620cfd2440SJiaxun Yang 	outw(value, ACPI_PM_EVT_BLK);
630cfd2440SJiaxun Yang 
640cfd2440SJiaxun Yang 	/* GPEStatus: Clear all generated events */
650cfd2440SJiaxun Yang 	outl(inl(ACPI_GPE0_BLK), ACPI_GPE0_BLK);
660cfd2440SJiaxun Yang }
670cfd2440SJiaxun Yang 
acpi_registers_setup(void)6852001066SWei Yongjun static void acpi_registers_setup(void)
690cfd2440SJiaxun Yang {
700cfd2440SJiaxun Yang 	u32 value;
710cfd2440SJiaxun Yang 
720cfd2440SJiaxun Yang 	/* PM Status Base */
730cfd2440SJiaxun Yang 	pm_iowrite(0x20, ACPI_PM_EVT_BLK & 0xff);
740cfd2440SJiaxun Yang 	pm_iowrite(0x21, ACPI_PM_EVT_BLK >> 8);
750cfd2440SJiaxun Yang 
760cfd2440SJiaxun Yang 	/* PM Control Base */
770cfd2440SJiaxun Yang 	pm_iowrite(0x22, ACPI_PM_CNT_BLK & 0xff);
780cfd2440SJiaxun Yang 	pm_iowrite(0x23, ACPI_PM_CNT_BLK >> 8);
790cfd2440SJiaxun Yang 
800cfd2440SJiaxun Yang 	/* GPM Base */
810cfd2440SJiaxun Yang 	pm_iowrite(0x28, ACPI_GPE0_BLK & 0xff);
820cfd2440SJiaxun Yang 	pm_iowrite(0x29, ACPI_GPE0_BLK >> 8);
830cfd2440SJiaxun Yang 
840cfd2440SJiaxun Yang 	/* ACPI End */
850cfd2440SJiaxun Yang 	pm_iowrite(0x2e, ACPI_END & 0xff);
860cfd2440SJiaxun Yang 	pm_iowrite(0x2f, ACPI_END >> 8);
870cfd2440SJiaxun Yang 
880cfd2440SJiaxun Yang 	/* IO Decode: When AcpiDecodeEnable set, South-Bridge uses the contents
890cfd2440SJiaxun Yang 	 * of the PM registers at index 0x20~0x2B to decode ACPI I/O address. */
900cfd2440SJiaxun Yang 	pm_iowrite(0x0e, 1 << 3);
910cfd2440SJiaxun Yang 
920cfd2440SJiaxun Yang 	/* SCI_EN set */
930cfd2440SJiaxun Yang 	outw(1, ACPI_PM_CNT_BLK);
940cfd2440SJiaxun Yang 
950cfd2440SJiaxun Yang 	/* Enable to generate SCI */
960cfd2440SJiaxun Yang 	pm_iowrite(0x10, pm_ioread(0x10) | 1);
970cfd2440SJiaxun Yang 
980cfd2440SJiaxun Yang 	/* GPM3/GPM9 enable */
990cfd2440SJiaxun Yang 	value = inl(ACPI_GPE0_BLK + 4);
1000cfd2440SJiaxun Yang 	outl(value | (1 << 14) | (1 << 22), ACPI_GPE0_BLK + 4);
1010cfd2440SJiaxun Yang 
1020cfd2440SJiaxun Yang 	/* Set GPM9 as input */
1030cfd2440SJiaxun Yang 	pm_iowrite(0x8d, pm_ioread(0x8d) & (~(1 << 1)));
1040cfd2440SJiaxun Yang 
1050cfd2440SJiaxun Yang 	/* Set GPM9 as non-output */
1060cfd2440SJiaxun Yang 	pm_iowrite(0x94, pm_ioread(0x94) | (1 << 3));
1070cfd2440SJiaxun Yang 
1080cfd2440SJiaxun Yang 	/* GPM3 config ACPI trigger SCIOUT */
1090cfd2440SJiaxun Yang 	pm_iowrite(0x33, pm_ioread(0x33) & (~(3 << 4)));
1100cfd2440SJiaxun Yang 
1110cfd2440SJiaxun Yang 	/* GPM9 config ACPI trigger SCIOUT */
1120cfd2440SJiaxun Yang 	pm_iowrite(0x3d, pm_ioread(0x3d) & (~(3 << 2)));
1130cfd2440SJiaxun Yang 
1140cfd2440SJiaxun Yang 	/* GPM3 config falling edge trigger */
1150cfd2440SJiaxun Yang 	pm_iowrite(0x37, pm_ioread(0x37) & (~(1 << 6)));
1160cfd2440SJiaxun Yang 
1170cfd2440SJiaxun Yang 	/* No wait for STPGNT# in ACPI Sx state */
1180cfd2440SJiaxun Yang 	pm_iowrite(0x7c, pm_ioread(0x7c) | (1 << 6));
1190cfd2440SJiaxun Yang 
1200cfd2440SJiaxun Yang 	/* Set GPM3 pull-down enable */
1210cfd2440SJiaxun Yang 	value = pm2_ioread(0xf6);
1220cfd2440SJiaxun Yang 	value |= ((1 << 7) | (1 << 3));
1230cfd2440SJiaxun Yang 	pm2_iowrite(0xf6, value);
1240cfd2440SJiaxun Yang 
1250cfd2440SJiaxun Yang 	/* Set GPM9 pull-down enable */
1260cfd2440SJiaxun Yang 	value = pm2_ioread(0xf8);
1270cfd2440SJiaxun Yang 	value |= ((1 << 5) | (1 << 1));
1280cfd2440SJiaxun Yang 	pm2_iowrite(0xf8, value);
1290cfd2440SJiaxun Yang }
1300cfd2440SJiaxun Yang 
rs780e_acpi_probe(struct platform_device * pdev)1310cfd2440SJiaxun Yang static int rs780e_acpi_probe(struct platform_device *pdev)
1320cfd2440SJiaxun Yang {
1330cfd2440SJiaxun Yang 	struct resource *res;
1340cfd2440SJiaxun Yang 
1350cfd2440SJiaxun Yang 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1360cfd2440SJiaxun Yang 	if (!res)
1370cfd2440SJiaxun Yang 		return -ENODEV;
1380cfd2440SJiaxun Yang 
1390cfd2440SJiaxun Yang 	/* SCI interrupt need acpi space, allocate here */
1400cfd2440SJiaxun Yang 	if (!request_region(res->start, resource_size(res), "acpi")) {
1410cfd2440SJiaxun Yang 		pr_err("RS780E-ACPI: Failed to request IO Region\n");
1420cfd2440SJiaxun Yang 		return -EBUSY;
1430cfd2440SJiaxun Yang 	}
1440cfd2440SJiaxun Yang 
1450cfd2440SJiaxun Yang 	acpi_iobase = res->start;
1460cfd2440SJiaxun Yang 
1470cfd2440SJiaxun Yang 	acpi_registers_setup();
1480cfd2440SJiaxun Yang 	acpi_hw_clear_status();
1490cfd2440SJiaxun Yang 
1500cfd2440SJiaxun Yang 	return 0;
1510cfd2440SJiaxun Yang }
1520cfd2440SJiaxun Yang 
1530cfd2440SJiaxun Yang static const struct of_device_id rs780e_acpi_match[] = {
1540cfd2440SJiaxun Yang 	{ .compatible = "loongson,rs780e-acpi" },
1550cfd2440SJiaxun Yang 	{},
1560cfd2440SJiaxun Yang };
1570cfd2440SJiaxun Yang 
1580cfd2440SJiaxun Yang static struct platform_driver rs780e_acpi_driver = {
1590cfd2440SJiaxun Yang 	.probe = rs780e_acpi_probe,
1600cfd2440SJiaxun Yang 	.driver = {
1610cfd2440SJiaxun Yang 		.name = "RS780E-ACPI",
1620cfd2440SJiaxun Yang 		.of_match_table = rs780e_acpi_match,
1630cfd2440SJiaxun Yang 	},
1640cfd2440SJiaxun Yang };
1650cfd2440SJiaxun Yang builtin_platform_driver(rs780e_acpi_driver);
166