1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Board-specific reboot/shutdown routines
3  *
4  * Copyright (c) 2009 Philippe Vachon <philippe@cowpig.ca>
5  *
6  * Copyright (C) 2009 Lemote Inc.
7  * Author: Wu Zhangjin, wuzhangjin@gmail.com
8  */
9 
10 #include <linux/io.h>
11 #include <linux/delay.h>
12 #include <linux/types.h>
13 
14 #include <asm/bootinfo.h>
15 
16 #include <loongson.h>
17 
18 #include <cs5536/cs5536.h>
19 #include "ec_kb3310b.h"
20 
21 static void reset_cpu(void)
22 {
23 	/*
24 	 * reset cpu to full speed, this is needed when enabling cpu frequency
25 	 * scalling
26 	 */
27 	writel(readl(LOONGSON_CHIPCFG) | 0x7, LOONGSON_CHIPCFG);
28 }
29 
30 /* reset support for fuloong2f */
31 
32 static void fl2f_reboot(void)
33 {
34 	reset_cpu();
35 
36 	/* send a reset signal to south bridge.
37 	 *
38 	 * NOTE: if enable "Power Management" in kernel, rtl8169 will not reset
39 	 * normally with this reset operation and it will not work in PMON, but
40 	 * you can type halt command and then reboot, seems the hardware reset
41 	 * logic not work normally.
42 	 */
43 	{
44 		u32 hi, lo;
45 		_rdmsr(DIVIL_MSR_REG(DIVIL_SOFT_RESET), &hi, &lo);
46 		lo |= 0x00000001;
47 		_wrmsr(DIVIL_MSR_REG(DIVIL_SOFT_RESET), hi, lo);
48 	}
49 }
50 
51 static void fl2f_shutdown(void)
52 {
53 	u32 hi, lo, val;
54 	int gpio_base;
55 
56 	/* get gpio base */
57 	_rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_GPIO), &hi, &lo);
58 	gpio_base = lo & 0xff00;
59 
60 	/* make cs5536 gpio13 output enable */
61 	val = inl(gpio_base + GPIOL_OUT_EN);
62 	val &= ~(1 << (16 + 13));
63 	val |= (1 << 13);
64 	outl(val, gpio_base + GPIOL_OUT_EN);
65 	mmiowb();
66 	/* make cs5536 gpio13 output low level voltage. */
67 	val = inl(gpio_base + GPIOL_OUT_VAL) & ~(1 << (13));
68 	val |= (1 << (16 + 13));
69 	outl(val, gpio_base + GPIOL_OUT_VAL);
70 	mmiowb();
71 }
72 
73 /* reset support for yeeloong2f and mengloong2f notebook */
74 
75 static void ml2f_reboot(void)
76 {
77 	reset_cpu();
78 
79 	/* sending an reset signal to EC(embedded controller) */
80 	ec_write(REG_RESET, BIT_RESET_ON);
81 }
82 
83 #define yl2f89_reboot ml2f_reboot
84 
85 /* menglong(7inches) laptop has different shutdown logic from 8.9inches */
86 #define EC_SHUTDOWN_IO_PORT_HIGH 0xff2d
87 #define EC_SHUTDOWN_IO_PORT_LOW	 0xff2e
88 #define EC_SHUTDOWN_IO_PORT_DATA 0xff2f
89 #define REG_SHUTDOWN_HIGH	 0xFC
90 #define REG_SHUTDOWN_LOW	 0x29
91 #define BIT_SHUTDOWN_ON		 (1 << 1)
92 
93 static void ml2f_shutdown(void)
94 {
95 	u8 val;
96 	u64 i;
97 
98 	outb(REG_SHUTDOWN_HIGH, EC_SHUTDOWN_IO_PORT_HIGH);
99 	outb(REG_SHUTDOWN_LOW, EC_SHUTDOWN_IO_PORT_LOW);
100 	mmiowb();
101 	val = inb(EC_SHUTDOWN_IO_PORT_DATA);
102 	outb(val & (~BIT_SHUTDOWN_ON), EC_SHUTDOWN_IO_PORT_DATA);
103 	mmiowb();
104 	/* need enough wait here... how many microseconds needs? */
105 	for (i = 0; i < 0x10000; i++)
106 		delay();
107 	outb(val | BIT_SHUTDOWN_ON, EC_SHUTDOWN_IO_PORT_DATA);
108 	mmiowb();
109 }
110 
111 static void yl2f89_shutdown(void)
112 {
113 	/* cpu-gpio0 output low */
114 	LOONGSON_GPIODATA &= ~0x00000001;
115 	/* cpu-gpio0 as output */
116 	LOONGSON_GPIOIE &= ~0x00000001;
117 }
118 
119 void mach_prepare_reboot(void)
120 {
121 	switch (mips_machtype) {
122 	case MACH_LEMOTE_FL2F:
123 	case MACH_LEMOTE_NAS:
124 	case MACH_LEMOTE_LL2F:
125 		fl2f_reboot();
126 		break;
127 	case MACH_LEMOTE_ML2F7:
128 		ml2f_reboot();
129 		break;
130 	case MACH_LEMOTE_YL2F89:
131 		yl2f89_reboot();
132 		break;
133 	default:
134 		break;
135 	}
136 }
137 
138 void mach_prepare_shutdown(void)
139 {
140 	switch (mips_machtype) {
141 	case MACH_LEMOTE_FL2F:
142 	case MACH_LEMOTE_NAS:
143 	case MACH_LEMOTE_LL2F:
144 		fl2f_shutdown();
145 		break;
146 	case MACH_LEMOTE_ML2F7:
147 		ml2f_shutdown();
148 		break;
149 	case MACH_LEMOTE_YL2F89:
150 		yl2f89_shutdown();
151 		break;
152 	default:
153 		break;
154 	}
155 }
156