xref: /linux/arch/mips/loongson2ef/lemote-2f/pm.c (revision c95baf12f5077419db01313ab61c2aac007d40cd)
1*71e2f4ddSJiaxun Yang // SPDX-License-Identifier: GPL-2.0-or-later
2*71e2f4ddSJiaxun Yang /*
3*71e2f4ddSJiaxun Yang  *  Lemote loongson2f family machines' specific suspend support
4*71e2f4ddSJiaxun Yang  *
5*71e2f4ddSJiaxun Yang  *  Copyright (C) 2009 Lemote Inc.
6*71e2f4ddSJiaxun Yang  *  Author: Wu Zhangjin <wuzhangjin@gmail.com>
7*71e2f4ddSJiaxun Yang  */
8*71e2f4ddSJiaxun Yang 
9*71e2f4ddSJiaxun Yang #include <linux/suspend.h>
10*71e2f4ddSJiaxun Yang #include <linux/interrupt.h>
11*71e2f4ddSJiaxun Yang #include <linux/pm.h>
12*71e2f4ddSJiaxun Yang #include <linux/i8042.h>
13*71e2f4ddSJiaxun Yang #include <linux/export.h>
14*71e2f4ddSJiaxun Yang 
15*71e2f4ddSJiaxun Yang #include <asm/i8259.h>
16*71e2f4ddSJiaxun Yang #include <asm/mipsregs.h>
17*71e2f4ddSJiaxun Yang #include <asm/bootinfo.h>
18*71e2f4ddSJiaxun Yang 
19*71e2f4ddSJiaxun Yang #include <loongson.h>
20*71e2f4ddSJiaxun Yang 
21*71e2f4ddSJiaxun Yang #include <cs5536/cs5536_mfgpt.h>
22*71e2f4ddSJiaxun Yang #include "ec_kb3310b.h"
23*71e2f4ddSJiaxun Yang 
24*71e2f4ddSJiaxun Yang #define I8042_KBD_IRQ		1
25*71e2f4ddSJiaxun Yang #define I8042_CTR_KBDINT	0x01
26*71e2f4ddSJiaxun Yang #define I8042_CTR_KBDDIS	0x10
27*71e2f4ddSJiaxun Yang 
28*71e2f4ddSJiaxun Yang static unsigned char i8042_ctr;
29*71e2f4ddSJiaxun Yang 
i8042_enable_kbd_port(void)30*71e2f4ddSJiaxun Yang static int i8042_enable_kbd_port(void)
31*71e2f4ddSJiaxun Yang {
32*71e2f4ddSJiaxun Yang 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
33*71e2f4ddSJiaxun Yang 		pr_err("i8042.c: Can't read CTR while enabling i8042 kbd port."
34*71e2f4ddSJiaxun Yang 		       "\n");
35*71e2f4ddSJiaxun Yang 		return -EIO;
36*71e2f4ddSJiaxun Yang 	}
37*71e2f4ddSJiaxun Yang 
38*71e2f4ddSJiaxun Yang 	i8042_ctr &= ~I8042_CTR_KBDDIS;
39*71e2f4ddSJiaxun Yang 	i8042_ctr |= I8042_CTR_KBDINT;
40*71e2f4ddSJiaxun Yang 
41*71e2f4ddSJiaxun Yang 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
42*71e2f4ddSJiaxun Yang 		i8042_ctr &= ~I8042_CTR_KBDINT;
43*71e2f4ddSJiaxun Yang 		i8042_ctr |= I8042_CTR_KBDDIS;
44*71e2f4ddSJiaxun Yang 		pr_err("i8042.c: Failed to enable KBD port.\n");
45*71e2f4ddSJiaxun Yang 
46*71e2f4ddSJiaxun Yang 		return -EIO;
47*71e2f4ddSJiaxun Yang 	}
48*71e2f4ddSJiaxun Yang 
49*71e2f4ddSJiaxun Yang 	return 0;
50*71e2f4ddSJiaxun Yang }
51*71e2f4ddSJiaxun Yang 
setup_wakeup_events(void)52*71e2f4ddSJiaxun Yang void setup_wakeup_events(void)
53*71e2f4ddSJiaxun Yang {
54*71e2f4ddSJiaxun Yang 	int irq_mask;
55*71e2f4ddSJiaxun Yang 
56*71e2f4ddSJiaxun Yang 	switch (mips_machtype) {
57*71e2f4ddSJiaxun Yang 	case MACH_LEMOTE_ML2F7:
58*71e2f4ddSJiaxun Yang 	case MACH_LEMOTE_YL2F89:
59*71e2f4ddSJiaxun Yang 		/* open the keyboard irq in i8259A */
60*71e2f4ddSJiaxun Yang 		outb((0xff & ~(1 << I8042_KBD_IRQ)), PIC_MASTER_IMR);
61*71e2f4ddSJiaxun Yang 		irq_mask = inb(PIC_MASTER_IMR);
62*71e2f4ddSJiaxun Yang 
63*71e2f4ddSJiaxun Yang 		/* enable keyboard port */
64*71e2f4ddSJiaxun Yang 		i8042_enable_kbd_port();
65*71e2f4ddSJiaxun Yang 
66*71e2f4ddSJiaxun Yang 		/* Wakeup CPU via SCI lid open event */
67*71e2f4ddSJiaxun Yang 		outb(irq_mask & ~(1 << PIC_CASCADE_IR), PIC_MASTER_IMR);
68*71e2f4ddSJiaxun Yang 		inb(PIC_MASTER_IMR);
69*71e2f4ddSJiaxun Yang 		outb(0xff & ~(1 << (SCI_IRQ_NUM - 8)), PIC_SLAVE_IMR);
70*71e2f4ddSJiaxun Yang 		inb(PIC_SLAVE_IMR);
71*71e2f4ddSJiaxun Yang 
72*71e2f4ddSJiaxun Yang 		break;
73*71e2f4ddSJiaxun Yang 
74*71e2f4ddSJiaxun Yang 	default:
75*71e2f4ddSJiaxun Yang 		break;
76*71e2f4ddSJiaxun Yang 	}
77*71e2f4ddSJiaxun Yang }
78*71e2f4ddSJiaxun Yang 
79*71e2f4ddSJiaxun Yang static struct delayed_work lid_task;
80*71e2f4ddSJiaxun Yang static int initialized;
81*71e2f4ddSJiaxun Yang /* yeeloong_report_lid_status will be implemented in yeeloong_laptop.c */
82*71e2f4ddSJiaxun Yang sci_handler yeeloong_report_lid_status;
83*71e2f4ddSJiaxun Yang EXPORT_SYMBOL(yeeloong_report_lid_status);
yeeloong_lid_update_task(struct work_struct * work)84*71e2f4ddSJiaxun Yang static void yeeloong_lid_update_task(struct work_struct *work)
85*71e2f4ddSJiaxun Yang {
86*71e2f4ddSJiaxun Yang 	if (yeeloong_report_lid_status)
87*71e2f4ddSJiaxun Yang 		yeeloong_report_lid_status(BIT_LID_DETECT_ON);
88*71e2f4ddSJiaxun Yang }
89*71e2f4ddSJiaxun Yang 
wakeup_loongson(void)90*71e2f4ddSJiaxun Yang int wakeup_loongson(void)
91*71e2f4ddSJiaxun Yang {
92*71e2f4ddSJiaxun Yang 	int irq;
93*71e2f4ddSJiaxun Yang 
94*71e2f4ddSJiaxun Yang 	/* query the interrupt number */
95*71e2f4ddSJiaxun Yang 	irq = mach_i8259_irq();
96*71e2f4ddSJiaxun Yang 	if (irq < 0)
97*71e2f4ddSJiaxun Yang 		return 0;
98*71e2f4ddSJiaxun Yang 
99*71e2f4ddSJiaxun Yang 	printk(KERN_INFO "%s: irq = %d\n", __func__, irq);
100*71e2f4ddSJiaxun Yang 
101*71e2f4ddSJiaxun Yang 	if (irq == I8042_KBD_IRQ)
102*71e2f4ddSJiaxun Yang 		return 1;
103*71e2f4ddSJiaxun Yang 	else if (irq == SCI_IRQ_NUM) {
104*71e2f4ddSJiaxun Yang 		int ret, sci_event;
105*71e2f4ddSJiaxun Yang 		/* query the event number */
106*71e2f4ddSJiaxun Yang 		ret = ec_query_seq(CMD_GET_EVENT_NUM);
107*71e2f4ddSJiaxun Yang 		if (ret < 0)
108*71e2f4ddSJiaxun Yang 			return 0;
109*71e2f4ddSJiaxun Yang 		sci_event = ec_get_event_num();
110*71e2f4ddSJiaxun Yang 		if (sci_event < 0)
111*71e2f4ddSJiaxun Yang 			return 0;
112*71e2f4ddSJiaxun Yang 		if (sci_event == EVENT_LID) {
113*71e2f4ddSJiaxun Yang 			int lid_status;
114*71e2f4ddSJiaxun Yang 			/* check the LID status */
115*71e2f4ddSJiaxun Yang 			lid_status = ec_read(REG_LID_DETECT);
116*71e2f4ddSJiaxun Yang 			/* wakeup cpu when people open the LID */
117*71e2f4ddSJiaxun Yang 			if (lid_status == BIT_LID_DETECT_ON) {
118*71e2f4ddSJiaxun Yang 				/* If we call it directly here, the WARNING
119*71e2f4ddSJiaxun Yang 				 * will be sent out by getnstimeofday
120*71e2f4ddSJiaxun Yang 				 * via "WARN_ON(timekeeping_suspended);"
121*71e2f4ddSJiaxun Yang 				 * because we can not schedule in suspend mode.
122*71e2f4ddSJiaxun Yang 				 */
123*71e2f4ddSJiaxun Yang 				if (initialized == 0) {
124*71e2f4ddSJiaxun Yang 					INIT_DELAYED_WORK(&lid_task,
125*71e2f4ddSJiaxun Yang 						yeeloong_lid_update_task);
126*71e2f4ddSJiaxun Yang 					initialized = 1;
127*71e2f4ddSJiaxun Yang 				}
128*71e2f4ddSJiaxun Yang 				schedule_delayed_work(&lid_task, 1);
129*71e2f4ddSJiaxun Yang 				return 1;
130*71e2f4ddSJiaxun Yang 			}
131*71e2f4ddSJiaxun Yang 		}
132*71e2f4ddSJiaxun Yang 	}
133*71e2f4ddSJiaxun Yang 
134*71e2f4ddSJiaxun Yang 	return 0;
135*71e2f4ddSJiaxun Yang }
136*71e2f4ddSJiaxun Yang 
mach_suspend(void)137*71e2f4ddSJiaxun Yang void __weak mach_suspend(void)
138*71e2f4ddSJiaxun Yang {
139*71e2f4ddSJiaxun Yang 	disable_mfgpt0_counter();
140*71e2f4ddSJiaxun Yang }
141*71e2f4ddSJiaxun Yang 
mach_resume(void)142*71e2f4ddSJiaxun Yang void __weak mach_resume(void)
143*71e2f4ddSJiaxun Yang {
144*71e2f4ddSJiaxun Yang 	enable_mfgpt0_counter();
145*71e2f4ddSJiaxun Yang }
146