xref: /OK3568_Linux_fs/kernel/arch/powerpc/include/asm/smu.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _SMU_H
3*4882a593Smuzhiyun #define _SMU_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * Definitions for talking to the SMU chip in newer G5 PowerMacs
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #ifdef __KERNEL__
9*4882a593Smuzhiyun #include <linux/list.h>
10*4882a593Smuzhiyun #endif
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * Known SMU commands
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Most of what is below comes from looking at the Open Firmware driver,
17*4882a593Smuzhiyun  * though this is still incomplete and could use better documentation here
18*4882a593Smuzhiyun  * or there...
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * Partition info commands
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * These commands are used to retrieve the sdb-partition-XX datas from
26*4882a593Smuzhiyun  * the SMU. The length is always 2. First byte is the subcommand code
27*4882a593Smuzhiyun  * and second byte is the partition ID.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * The reply is 6 bytes:
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *  - 0..1 : partition address
32*4882a593Smuzhiyun  *  - 2    : a byte containing the partition ID
33*4882a593Smuzhiyun  *  - 3    : length (maybe other bits are rest of header ?)
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * The data must then be obtained with calls to another command:
36*4882a593Smuzhiyun  * SMU_CMD_MISC_ee_GET_DATABLOCK_REC (described below).
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define SMU_CMD_PARTITION_COMMAND		0x3e
39*4882a593Smuzhiyun #define   SMU_CMD_PARTITION_LATEST		0x01
40*4882a593Smuzhiyun #define   SMU_CMD_PARTITION_BASE		0x02
41*4882a593Smuzhiyun #define   SMU_CMD_PARTITION_UPDATE		0x03
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * Fan control
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * This is a "mux" for fan control commands. The command seem to
48*4882a593Smuzhiyun  * act differently based on the number of arguments. With 1 byte
49*4882a593Smuzhiyun  * of argument, this seem to be queries for fans status, setpoint,
50*4882a593Smuzhiyun  * etc..., while with 0xe arguments, we will set the fans speeds.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * Queries (1 byte arg):
53*4882a593Smuzhiyun  * ---------------------
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * arg=0x01: read RPM fans status
56*4882a593Smuzhiyun  * arg=0x02: read RPM fans setpoint
57*4882a593Smuzhiyun  * arg=0x11: read PWM fans status
58*4882a593Smuzhiyun  * arg=0x12: read PWM fans setpoint
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * the "status" queries return the current speed while the "setpoint" ones
61*4882a593Smuzhiyun  * return the programmed/target speed. It _seems_ that the result is a bit
62*4882a593Smuzhiyun  * mask in the first byte of active/available fans, followed by 6 words (16
63*4882a593Smuzhiyun  * bits) containing the requested speed.
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * Setpoint (14 bytes arg):
66*4882a593Smuzhiyun  * ------------------------
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * first arg byte is 0 for RPM fans and 0x10 for PWM. Second arg byte is the
69*4882a593Smuzhiyun  * mask of fans affected by the command. Followed by 6 words containing the
70*4882a593Smuzhiyun  * setpoint value for selected fans in the mask (or 0 if mask value is 0)
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun #define SMU_CMD_FAN_COMMAND			0x4a
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun  * Battery access
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Same command number as the PMU, could it be same syntax ?
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun #define SMU_CMD_BATTERY_COMMAND			0x6f
81*4882a593Smuzhiyun #define   SMU_CMD_GET_BATTERY_INFO		0x00
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun  * Real time clock control
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * This is a "mux", first data byte contains the "sub" command.
87*4882a593Smuzhiyun  * The "RTC" part of the SMU controls the date, time, powerup
88*4882a593Smuzhiyun  * timer, but also a PRAM
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * Dates are in BCD format on 7 bytes:
91*4882a593Smuzhiyun  * [sec] [min] [hour] [weekday] [month day] [month] [year]
92*4882a593Smuzhiyun  * with month being 1 based and year minus 100
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun #define SMU_CMD_RTC_COMMAND			0x8e
95*4882a593Smuzhiyun #define   SMU_CMD_RTC_SET_PWRUP_TIMER		0x00 /* i: 7 bytes date */
96*4882a593Smuzhiyun #define   SMU_CMD_RTC_GET_PWRUP_TIMER		0x01 /* o: 7 bytes date */
97*4882a593Smuzhiyun #define   SMU_CMD_RTC_STOP_PWRUP_TIMER		0x02
98*4882a593Smuzhiyun #define   SMU_CMD_RTC_SET_PRAM_BYTE_ACC		0x20 /* i: 1 byte (address?) */
99*4882a593Smuzhiyun #define   SMU_CMD_RTC_SET_PRAM_AUTOINC		0x21 /* i: 1 byte (data?) */
100*4882a593Smuzhiyun #define   SMU_CMD_RTC_SET_PRAM_LO_BYTES 	0x22 /* i: 10 bytes */
101*4882a593Smuzhiyun #define   SMU_CMD_RTC_SET_PRAM_HI_BYTES 	0x23 /* i: 10 bytes */
102*4882a593Smuzhiyun #define   SMU_CMD_RTC_GET_PRAM_BYTE		0x28 /* i: 1 bytes (address?) */
103*4882a593Smuzhiyun #define   SMU_CMD_RTC_GET_PRAM_LO_BYTES 	0x29 /* o: 10 bytes */
104*4882a593Smuzhiyun #define   SMU_CMD_RTC_GET_PRAM_HI_BYTES 	0x2a /* o: 10 bytes */
105*4882a593Smuzhiyun #define	  SMU_CMD_RTC_SET_DATETIME		0x80 /* i: 7 bytes date */
106*4882a593Smuzhiyun #define   SMU_CMD_RTC_GET_DATETIME		0x81 /* o: 7 bytes date */
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun  /*
109*4882a593Smuzhiyun   * i2c commands
110*4882a593Smuzhiyun   *
111*4882a593Smuzhiyun   * To issue an i2c command, first is to send a parameter block to
112*4882a593Smuzhiyun   * the SMU. This is a command of type 0x9a with 9 bytes of header
113*4882a593Smuzhiyun   * eventually followed by data for a write:
114*4882a593Smuzhiyun   *
115*4882a593Smuzhiyun   * 0: bus number (from device-tree usually, SMU has lots of busses !)
116*4882a593Smuzhiyun   * 1: transfer type/format (see below)
117*4882a593Smuzhiyun   * 2: device address. For combined and combined4 type transfers, this
118*4882a593Smuzhiyun   *    is the "write" version of the address (bit 0x01 cleared)
119*4882a593Smuzhiyun   * 3: subaddress length (0..3)
120*4882a593Smuzhiyun   * 4: subaddress byte 0 (or only byte for subaddress length 1)
121*4882a593Smuzhiyun   * 5: subaddress byte 1
122*4882a593Smuzhiyun   * 6: subaddress byte 2
123*4882a593Smuzhiyun   * 7: combined address (device address for combined mode data phase)
124*4882a593Smuzhiyun   * 8: data length
125*4882a593Smuzhiyun   *
126*4882a593Smuzhiyun   * The transfer types are the same good old Apple ones it seems,
127*4882a593Smuzhiyun   * that is:
128*4882a593Smuzhiyun   *   - 0x00: Simple transfer
129*4882a593Smuzhiyun   *   - 0x01: Subaddress transfer (addr write + data tx, no restart)
130*4882a593Smuzhiyun   *   - 0x02: Combined transfer (addr write + restart + data tx)
131*4882a593Smuzhiyun   *
132*4882a593Smuzhiyun   * This is then followed by actual data for a write.
133*4882a593Smuzhiyun   *
134*4882a593Smuzhiyun   * At this point, the OF driver seems to have a limitation on transfer
135*4882a593Smuzhiyun   * sizes of 0xd bytes on reads and 0x5 bytes on writes. I do not know
136*4882a593Smuzhiyun   * whether this is just an OF limit due to some temporary buffer size
137*4882a593Smuzhiyun   * or if this is an SMU imposed limit. This driver has the same limitation
138*4882a593Smuzhiyun   * for now as I use a 0x10 bytes temporary buffer as well
139*4882a593Smuzhiyun   *
140*4882a593Smuzhiyun   * Once that is completed, a response is expected from the SMU. This is
141*4882a593Smuzhiyun   * obtained via a command of type 0x9a with a length of 1 byte containing
142*4882a593Smuzhiyun   * 0 as the data byte. OF also fills the rest of the data buffer with 0xff's
143*4882a593Smuzhiyun   * though I can't tell yet if this is actually necessary. Once this command
144*4882a593Smuzhiyun   * is complete, at this point, all I can tell is what OF does. OF tests
145*4882a593Smuzhiyun   * byte 0 of the reply:
146*4882a593Smuzhiyun   *   - on read, 0xfe or 0xfc : bus is busy, wait (see below) or nak ?
147*4882a593Smuzhiyun   *   - on read, 0x00 or 0x01 : reply is in buffer (after the byte 0)
148*4882a593Smuzhiyun   *   - on write, < 0 -> failure (immediate exit)
149*4882a593Smuzhiyun   *   - else, OF just exists (without error, weird)
150*4882a593Smuzhiyun   *
151*4882a593Smuzhiyun   * So on read, there is this wait-for-busy thing when getting a 0xfc or
152*4882a593Smuzhiyun   * 0xfe result. OF does a loop of up to 64 retries, waiting 20ms and
153*4882a593Smuzhiyun   * doing the above again until either the retries expire or the result
154*4882a593Smuzhiyun   * is no longer 0xfe or 0xfc
155*4882a593Smuzhiyun   *
156*4882a593Smuzhiyun   * The Darwin I2C driver is less subtle though. On any non-success status
157*4882a593Smuzhiyun   * from the response command, it waits 5ms and tries again up to 20 times,
158*4882a593Smuzhiyun   * it doesn't differentiate between fatal errors or "busy" status.
159*4882a593Smuzhiyun   *
160*4882a593Smuzhiyun   * This driver provides an asynchronous paramblock based i2c command
161*4882a593Smuzhiyun   * interface to be used either directly by low level code or by a higher
162*4882a593Smuzhiyun   * level driver interfacing to the linux i2c layer. The current
163*4882a593Smuzhiyun   * implementation of this relies on working timers & timer interrupts
164*4882a593Smuzhiyun   * though, so be careful of calling context for now. This may be "fixed"
165*4882a593Smuzhiyun   * in the future by adding a polling facility.
166*4882a593Smuzhiyun   */
167*4882a593Smuzhiyun #define SMU_CMD_I2C_COMMAND			0x9a
168*4882a593Smuzhiyun           /* transfer types */
169*4882a593Smuzhiyun #define   SMU_I2C_TRANSFER_SIMPLE	0x00
170*4882a593Smuzhiyun #define   SMU_I2C_TRANSFER_STDSUB	0x01
171*4882a593Smuzhiyun #define   SMU_I2C_TRANSFER_COMBINED	0x02
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun  * Power supply control
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * The "sub" command is an ASCII string in the data, the
177*4882a593Smuzhiyun  * data length is that of the string.
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * The VSLEW command can be used to get or set the voltage slewing.
180*4882a593Smuzhiyun  *  - length 5 (only "VSLEW") : it returns "DONE" and 3 bytes of
181*4882a593Smuzhiyun  *    reply at data offset 6, 7 and 8.
182*4882a593Smuzhiyun  *  - length 8 ("VSLEWxyz") has 3 additional bytes appended, and is
183*4882a593Smuzhiyun  *    used to set the voltage slewing point. The SMU replies with "DONE"
184*4882a593Smuzhiyun  * I yet have to figure out their exact meaning of those 3 bytes in
185*4882a593Smuzhiyun  * both cases. They seem to be:
186*4882a593Smuzhiyun  *  x = processor mask
187*4882a593Smuzhiyun  *  y = op. point index
188*4882a593Smuzhiyun  *  z = processor freq. step index
189*4882a593Smuzhiyun  * I haven't yet deciphered result codes
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  */
192*4882a593Smuzhiyun #define SMU_CMD_POWER_COMMAND			0xaa
193*4882a593Smuzhiyun #define   SMU_CMD_POWER_RESTART		       	"RESTART"
194*4882a593Smuzhiyun #define   SMU_CMD_POWER_SHUTDOWN		"SHUTDOWN"
195*4882a593Smuzhiyun #define   SMU_CMD_POWER_VOLTAGE_SLEW		"VSLEW"
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun  * Read ADC sensors
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  * This command takes one byte of parameter: the sensor ID (or "reg"
201*4882a593Smuzhiyun  * value in the device-tree) and returns a 16 bits value
202*4882a593Smuzhiyun  */
203*4882a593Smuzhiyun #define SMU_CMD_READ_ADC			0xd8
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /* Misc commands
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * This command seem to be a grab bag of various things
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * Parameters:
211*4882a593Smuzhiyun  *   1: subcommand
212*4882a593Smuzhiyun  */
213*4882a593Smuzhiyun #define SMU_CMD_MISC_df_COMMAND			0xdf
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun  * Sets "system ready" status
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * I did not yet understand how it exactly works or what it does.
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * Guessing from OF code, 0x02 activates the display backlight. Apple uses/used
221*4882a593Smuzhiyun  * the same codebase for all OF versions. On PowerBooks, this command would
222*4882a593Smuzhiyun  * enable the backlight. For the G5s, it only activates the front LED. However,
223*4882a593Smuzhiyun  * don't take this for granted.
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Parameters:
226*4882a593Smuzhiyun  *   2: status [0x00, 0x01 or 0x02]
227*4882a593Smuzhiyun  */
228*4882a593Smuzhiyun #define   SMU_CMD_MISC_df_SET_DISPLAY_LIT	0x02
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun  * Sets mode of power switch.
232*4882a593Smuzhiyun  *
233*4882a593Smuzhiyun  * What this actually does is not yet known. Maybe it enables some interrupt.
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * Parameters:
236*4882a593Smuzhiyun  *   2: enable power switch? [0x00 or 0x01]
237*4882a593Smuzhiyun  *   3 (optional): enable nmi? [0x00 or 0x01]
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  * Returns:
240*4882a593Smuzhiyun  *   If parameter 2 is 0x00 and parameter 3 is not specified, returns whether
241*4882a593Smuzhiyun  *   NMI is enabled. Otherwise unknown.
242*4882a593Smuzhiyun  */
243*4882a593Smuzhiyun #define   SMU_CMD_MISC_df_NMI_OPTION		0x04
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /* Sets LED dimm offset.
246*4882a593Smuzhiyun  *
247*4882a593Smuzhiyun  * The front LED dimms itself during sleep. Its brightness (or, well, the PWM
248*4882a593Smuzhiyun  * frequency) depends on current time. Therefore, the SMU needs to know the
249*4882a593Smuzhiyun  * timezone.
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * Parameters:
252*4882a593Smuzhiyun  *   2-8: unknown (BCD coding)
253*4882a593Smuzhiyun  */
254*4882a593Smuzhiyun #define   SMU_CMD_MISC_df_DIMM_OFFSET		0x99
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun  * Version info commands
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * Parameters:
261*4882a593Smuzhiyun  *   1 (optional): Specifies version part to retrieve
262*4882a593Smuzhiyun  *
263*4882a593Smuzhiyun  * Returns:
264*4882a593Smuzhiyun  *   Version value
265*4882a593Smuzhiyun  */
266*4882a593Smuzhiyun #define SMU_CMD_VERSION_COMMAND			0xea
267*4882a593Smuzhiyun #define   SMU_VERSION_RUNNING			0x00
268*4882a593Smuzhiyun #define   SMU_VERSION_BASE			0x01
269*4882a593Smuzhiyun #define   SMU_VERSION_UPDATE			0x02
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun  * Switches
274*4882a593Smuzhiyun  *
275*4882a593Smuzhiyun  * These are switches whose status seems to be known to the SMU.
276*4882a593Smuzhiyun  *
277*4882a593Smuzhiyun  * Parameters:
278*4882a593Smuzhiyun  *   none
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * Result:
281*4882a593Smuzhiyun  *   Switch bits (ORed, see below)
282*4882a593Smuzhiyun  */
283*4882a593Smuzhiyun #define SMU_CMD_SWITCHES			0xdc
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /* Switches bits */
286*4882a593Smuzhiyun #define SMU_SWITCH_CASE_CLOSED			0x01
287*4882a593Smuzhiyun #define SMU_SWITCH_AC_POWER			0x04
288*4882a593Smuzhiyun #define SMU_SWITCH_POWER_SWITCH			0x08
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun  * Misc commands
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * This command seem to be a grab bag of various things
295*4882a593Smuzhiyun  *
296*4882a593Smuzhiyun  * SMU_CMD_MISC_ee_GET_DATABLOCK_REC is used, among others, to
297*4882a593Smuzhiyun  * transfer blocks of data from the SMU. So far, I've decrypted it's
298*4882a593Smuzhiyun  * usage to retrieve partition data. In order to do that, you have to
299*4882a593Smuzhiyun  * break your transfer in "chunks" since that command cannot transfer
300*4882a593Smuzhiyun  * more than a chunk at a time. The chunk size used by OF is 0xe bytes,
301*4882a593Smuzhiyun  * but it seems that the darwin driver will let you do 0x1e bytes if
302*4882a593Smuzhiyun  * your "PMU" version is >= 0x30. You can get the "PMU" version apparently
303*4882a593Smuzhiyun  * either in the last 16 bits of property "smu-version-pmu" or as the 16
304*4882a593Smuzhiyun  * bytes at offset 1 of "smu-version-info"
305*4882a593Smuzhiyun  *
306*4882a593Smuzhiyun  * For each chunk, the command takes 7 bytes of arguments:
307*4882a593Smuzhiyun  *  byte 0: subcommand code (0x02)
308*4882a593Smuzhiyun  *  byte 1: 0x04 (always, I don't know what it means, maybe the address
309*4882a593Smuzhiyun  *                space to use or some other nicety. It's hard coded in OF)
310*4882a593Smuzhiyun  *  byte 2..5: SMU address of the chunk (big endian 32 bits)
311*4882a593Smuzhiyun  *  byte 6: size to transfer (up to max chunk size)
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * The data is returned directly
314*4882a593Smuzhiyun  */
315*4882a593Smuzhiyun #define SMU_CMD_MISC_ee_COMMAND			0xee
316*4882a593Smuzhiyun #define   SMU_CMD_MISC_ee_GET_DATABLOCK_REC	0x02
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /* Retrieves currently used watts.
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * Parameters:
321*4882a593Smuzhiyun  *   1: 0x03 (Meaning unknown)
322*4882a593Smuzhiyun  */
323*4882a593Smuzhiyun #define   SMU_CMD_MISC_ee_GET_WATTS		0x03
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun #define   SMU_CMD_MISC_ee_LEDS_CTRL		0x04 /* i: 00 (00,01) [00] */
326*4882a593Smuzhiyun #define   SMU_CMD_MISC_ee_GET_DATA		0x05 /* i: 00 , o: ?? */
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun  * Power related commands
331*4882a593Smuzhiyun  *
332*4882a593Smuzhiyun  * Parameters:
333*4882a593Smuzhiyun  *   1: subcommand
334*4882a593Smuzhiyun  */
335*4882a593Smuzhiyun #define SMU_CMD_POWER_EVENTS_COMMAND		0x8f
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /* SMU_POWER_EVENTS subcommands */
338*4882a593Smuzhiyun enum {
339*4882a593Smuzhiyun 	SMU_PWR_GET_POWERUP_EVENTS      = 0x00,
340*4882a593Smuzhiyun 	SMU_PWR_SET_POWERUP_EVENTS      = 0x01,
341*4882a593Smuzhiyun 	SMU_PWR_CLR_POWERUP_EVENTS      = 0x02,
342*4882a593Smuzhiyun 	SMU_PWR_GET_WAKEUP_EVENTS       = 0x03,
343*4882a593Smuzhiyun 	SMU_PWR_SET_WAKEUP_EVENTS       = 0x04,
344*4882a593Smuzhiyun 	SMU_PWR_CLR_WAKEUP_EVENTS       = 0x05,
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	/*
347*4882a593Smuzhiyun 	 * Get last shutdown cause
348*4882a593Smuzhiyun 	 *
349*4882a593Smuzhiyun 	 * Returns:
350*4882a593Smuzhiyun 	 *   1 byte (signed char): Last shutdown cause. Exact meaning unknown.
351*4882a593Smuzhiyun 	 */
352*4882a593Smuzhiyun 	SMU_PWR_LAST_SHUTDOWN_CAUSE	= 0x07,
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/*
355*4882a593Smuzhiyun 	 * Sets or gets server ID. Meaning or use is unknown.
356*4882a593Smuzhiyun 	 *
357*4882a593Smuzhiyun 	 * Parameters:
358*4882a593Smuzhiyun 	 *   2 (optional): Set server ID (1 byte)
359*4882a593Smuzhiyun 	 *
360*4882a593Smuzhiyun 	 * Returns:
361*4882a593Smuzhiyun 	 *   1 byte (server ID?)
362*4882a593Smuzhiyun 	 */
363*4882a593Smuzhiyun 	SMU_PWR_SERVER_ID		= 0x08,
364*4882a593Smuzhiyun };
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /* Power events wakeup bits */
367*4882a593Smuzhiyun enum {
368*4882a593Smuzhiyun 	SMU_PWR_WAKEUP_KEY              = 0x01, /* Wake on key press */
369*4882a593Smuzhiyun 	SMU_PWR_WAKEUP_AC_INSERT        = 0x02, /* Wake on AC adapter plug */
370*4882a593Smuzhiyun 	SMU_PWR_WAKEUP_AC_CHANGE        = 0x04,
371*4882a593Smuzhiyun 	SMU_PWR_WAKEUP_LID_OPEN         = 0x08,
372*4882a593Smuzhiyun 	SMU_PWR_WAKEUP_RING             = 0x10,
373*4882a593Smuzhiyun };
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun  * - Kernel side interface -
378*4882a593Smuzhiyun  */
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun #ifdef __KERNEL__
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun  * Asynchronous SMU commands
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * Fill up this structure and submit it via smu_queue_command(),
386*4882a593Smuzhiyun  * and get notified by the optional done() callback, or because
387*4882a593Smuzhiyun  * status becomes != 1
388*4882a593Smuzhiyun  */
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun struct smu_cmd;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun struct smu_cmd
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	/* public */
395*4882a593Smuzhiyun 	u8			cmd;		/* command */
396*4882a593Smuzhiyun 	int			data_len;	/* data len */
397*4882a593Smuzhiyun 	int			reply_len;	/* reply len */
398*4882a593Smuzhiyun 	void			*data_buf;	/* data buffer */
399*4882a593Smuzhiyun 	void			*reply_buf;	/* reply buffer */
400*4882a593Smuzhiyun 	int			status;		/* command status */
401*4882a593Smuzhiyun 	void			(*done)(struct smu_cmd *cmd, void *misc);
402*4882a593Smuzhiyun 	void			*misc;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/* private */
405*4882a593Smuzhiyun 	struct list_head	link;
406*4882a593Smuzhiyun };
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun /*
409*4882a593Smuzhiyun  * Queues an SMU command, all fields have to be initialized
410*4882a593Smuzhiyun  */
411*4882a593Smuzhiyun extern int smu_queue_cmd(struct smu_cmd *cmd);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun /*
414*4882a593Smuzhiyun  * Simple command wrapper. This structure embeds a small buffer
415*4882a593Smuzhiyun  * to ease sending simple SMU commands from the stack
416*4882a593Smuzhiyun  */
417*4882a593Smuzhiyun struct smu_simple_cmd
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	struct smu_cmd	cmd;
420*4882a593Smuzhiyun 	u8	       	buffer[16];
421*4882a593Smuzhiyun };
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun  * Queues a simple command. All fields will be initialized by that
425*4882a593Smuzhiyun  * function
426*4882a593Smuzhiyun  */
427*4882a593Smuzhiyun extern int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
428*4882a593Smuzhiyun 			    unsigned int data_len,
429*4882a593Smuzhiyun 			    void (*done)(struct smu_cmd *cmd, void *misc),
430*4882a593Smuzhiyun 			    void *misc,
431*4882a593Smuzhiyun 			    ...);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun  * Completion helper. Pass it to smu_queue_simple or as 'done'
435*4882a593Smuzhiyun  * member to smu_queue_cmd, it will call complete() on the struct
436*4882a593Smuzhiyun  * completion passed in the "misc" argument
437*4882a593Smuzhiyun  */
438*4882a593Smuzhiyun extern void smu_done_complete(struct smu_cmd *cmd, void *misc);
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /*
441*4882a593Smuzhiyun  * Synchronous helpers. Will spin-wait for completion of a command
442*4882a593Smuzhiyun  */
443*4882a593Smuzhiyun extern void smu_spinwait_cmd(struct smu_cmd *cmd);
444*4882a593Smuzhiyun 
smu_spinwait_simple(struct smu_simple_cmd * scmd)445*4882a593Smuzhiyun static inline void smu_spinwait_simple(struct smu_simple_cmd *scmd)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun 	smu_spinwait_cmd(&scmd->cmd);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun  * Poll routine to call if blocked with irqs off
452*4882a593Smuzhiyun  */
453*4882a593Smuzhiyun extern void smu_poll(void);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /*
457*4882a593Smuzhiyun  * Init routine, presence check....
458*4882a593Smuzhiyun  */
459*4882a593Smuzhiyun extern int smu_init(void);
460*4882a593Smuzhiyun extern int smu_present(void);
461*4882a593Smuzhiyun struct platform_device;
462*4882a593Smuzhiyun extern struct platform_device *smu_get_ofdev(void);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /*
466*4882a593Smuzhiyun  * Common command wrappers
467*4882a593Smuzhiyun  */
468*4882a593Smuzhiyun extern void smu_shutdown(void);
469*4882a593Smuzhiyun extern void smu_restart(void);
470*4882a593Smuzhiyun struct rtc_time;
471*4882a593Smuzhiyun extern int smu_get_rtc_time(struct rtc_time *time, int spinwait);
472*4882a593Smuzhiyun extern int smu_set_rtc_time(struct rtc_time *time, int spinwait);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun  * Kernel asynchronous i2c interface
476*4882a593Smuzhiyun  */
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun #define SMU_I2C_READ_MAX	0x1d
479*4882a593Smuzhiyun #define SMU_I2C_WRITE_MAX	0x15
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun /* SMU i2c header, exactly matches i2c header on wire */
482*4882a593Smuzhiyun struct smu_i2c_param
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	u8	bus;		/* SMU bus ID (from device tree) */
485*4882a593Smuzhiyun 	u8	type;		/* i2c transfer type */
486*4882a593Smuzhiyun 	u8	devaddr;	/* device address (includes direction) */
487*4882a593Smuzhiyun 	u8	sublen;		/* subaddress length */
488*4882a593Smuzhiyun 	u8	subaddr[3];	/* subaddress */
489*4882a593Smuzhiyun 	u8	caddr;		/* combined address, filled by SMU driver */
490*4882a593Smuzhiyun 	u8	datalen;	/* length of transfer */
491*4882a593Smuzhiyun 	u8	data[SMU_I2C_READ_MAX];	/* data */
492*4882a593Smuzhiyun };
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun struct smu_i2c_cmd
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	/* public */
497*4882a593Smuzhiyun 	struct smu_i2c_param	info;
498*4882a593Smuzhiyun 	void			(*done)(struct smu_i2c_cmd *cmd, void *misc);
499*4882a593Smuzhiyun 	void			*misc;
500*4882a593Smuzhiyun 	int			status; /* 1 = pending, 0 = ok, <0 = fail */
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	/* private */
503*4882a593Smuzhiyun 	struct smu_cmd		scmd;
504*4882a593Smuzhiyun 	int			read;
505*4882a593Smuzhiyun 	int			stage;
506*4882a593Smuzhiyun 	int			retries;
507*4882a593Smuzhiyun 	u8			pdata[32];
508*4882a593Smuzhiyun 	struct list_head	link;
509*4882a593Smuzhiyun };
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /*
512*4882a593Smuzhiyun  * Call this to queue an i2c command to the SMU. You must fill info,
513*4882a593Smuzhiyun  * including info.data for a write, done and misc.
514*4882a593Smuzhiyun  * For now, no polling interface is provided so you have to use completion
515*4882a593Smuzhiyun  * callback.
516*4882a593Smuzhiyun  */
517*4882a593Smuzhiyun extern int smu_queue_i2c(struct smu_i2c_cmd *cmd);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun #endif /* __KERNEL__ */
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun /*
524*4882a593Smuzhiyun  * - SMU "sdb" partitions informations -
525*4882a593Smuzhiyun  */
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /*
529*4882a593Smuzhiyun  * Partition header format
530*4882a593Smuzhiyun  */
531*4882a593Smuzhiyun struct smu_sdbp_header {
532*4882a593Smuzhiyun 	__u8	id;
533*4882a593Smuzhiyun 	__u8	len;
534*4882a593Smuzhiyun 	__u8	version;
535*4882a593Smuzhiyun 	__u8	flags;
536*4882a593Smuzhiyun };
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun  /*
540*4882a593Smuzhiyun  * demangle 16 and 32 bits integer in some SMU partitions
541*4882a593Smuzhiyun  * (currently, afaik, this concerns only the FVT partition
542*4882a593Smuzhiyun  * (0x12)
543*4882a593Smuzhiyun  */
544*4882a593Smuzhiyun #define SMU_U16_MIX(x)	le16_to_cpu(x)
545*4882a593Smuzhiyun #define SMU_U32_MIX(x)  ((((x) & 0xff00ff00u) >> 8)|(((x) & 0x00ff00ffu) << 8))
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun /* This is the definition of the SMU sdb-partition-0x12 table (called
549*4882a593Smuzhiyun  * CPU F/V/T operating points in Darwin). The definition for all those
550*4882a593Smuzhiyun  * SMU tables should be moved to some separate file
551*4882a593Smuzhiyun  */
552*4882a593Smuzhiyun #define SMU_SDB_FVT_ID			0x12
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun struct smu_sdbp_fvt {
555*4882a593Smuzhiyun 	__u32	sysclk;			/* Base SysClk frequency in Hz for
556*4882a593Smuzhiyun 					 * this operating point. Value need to
557*4882a593Smuzhiyun 					 * be unmixed with SMU_U32_MIX()
558*4882a593Smuzhiyun 					 */
559*4882a593Smuzhiyun 	__u8	pad;
560*4882a593Smuzhiyun 	__u8	maxtemp;		/* Max temp. supported by this
561*4882a593Smuzhiyun 					 * operating point
562*4882a593Smuzhiyun 					 */
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	__u16	volts[3];		/* CPU core voltage for the 3
565*4882a593Smuzhiyun 					 * PowerTune modes, a mode with
566*4882a593Smuzhiyun 					 * 0V = not supported. Value need
567*4882a593Smuzhiyun 					 * to be unmixed with SMU_U16_MIX()
568*4882a593Smuzhiyun 					 */
569*4882a593Smuzhiyun };
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun /* This partition contains voltage & current sensor calibration
572*4882a593Smuzhiyun  * informations
573*4882a593Smuzhiyun  */
574*4882a593Smuzhiyun #define SMU_SDB_CPUVCP_ID		0x21
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun struct smu_sdbp_cpuvcp {
577*4882a593Smuzhiyun 	__u16	volt_scale;		/* u4.12 fixed point */
578*4882a593Smuzhiyun 	__s16	volt_offset;		/* s4.12 fixed point */
579*4882a593Smuzhiyun 	__u16	curr_scale;		/* u4.12 fixed point */
580*4882a593Smuzhiyun 	__s16	curr_offset;		/* s4.12 fixed point */
581*4882a593Smuzhiyun 	__s32	power_quads[3];		/* s4.28 fixed point */
582*4882a593Smuzhiyun };
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun /* This partition contains CPU thermal diode calibration
585*4882a593Smuzhiyun  */
586*4882a593Smuzhiyun #define SMU_SDB_CPUDIODE_ID		0x18
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun struct smu_sdbp_cpudiode {
589*4882a593Smuzhiyun 	__u16	m_value;		/* u1.15 fixed point */
590*4882a593Smuzhiyun 	__s16	b_value;		/* s10.6 fixed point */
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun };
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /* This partition contains Slots power calibration
595*4882a593Smuzhiyun  */
596*4882a593Smuzhiyun #define SMU_SDB_SLOTSPOW_ID		0x78
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun struct smu_sdbp_slotspow {
599*4882a593Smuzhiyun 	__u16	pow_scale;		/* u4.12 fixed point */
600*4882a593Smuzhiyun 	__s16	pow_offset;		/* s4.12 fixed point */
601*4882a593Smuzhiyun };
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun /* This partition contains machine specific version information about
604*4882a593Smuzhiyun  * the sensor/control layout
605*4882a593Smuzhiyun  */
606*4882a593Smuzhiyun #define SMU_SDB_SENSORTREE_ID		0x25
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun struct smu_sdbp_sensortree {
609*4882a593Smuzhiyun 	__u8	model_id;
610*4882a593Smuzhiyun 	__u8	unknown[3];
611*4882a593Smuzhiyun };
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /* This partition contains CPU thermal control PID informations. So far
614*4882a593Smuzhiyun  * only single CPU machines have been seen with an SMU, so we assume this
615*4882a593Smuzhiyun  * carries only informations for those
616*4882a593Smuzhiyun  */
617*4882a593Smuzhiyun #define SMU_SDB_CPUPIDDATA_ID		0x17
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun struct smu_sdbp_cpupiddata {
620*4882a593Smuzhiyun 	__u8	unknown1;
621*4882a593Smuzhiyun 	__u8	target_temp_delta;
622*4882a593Smuzhiyun 	__u8	unknown2;
623*4882a593Smuzhiyun 	__u8	history_len;
624*4882a593Smuzhiyun 	__s16	power_adj;
625*4882a593Smuzhiyun 	__u16	max_power;
626*4882a593Smuzhiyun 	__s32	gp,gr,gd;
627*4882a593Smuzhiyun };
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /* Other partitions without known structures */
631*4882a593Smuzhiyun #define SMU_SDB_DEBUG_SWITCHES_ID	0x05
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun #ifdef __KERNEL__
634*4882a593Smuzhiyun /*
635*4882a593Smuzhiyun  * This returns the pointer to an SMU "sdb" partition data or NULL
636*4882a593Smuzhiyun  * if not found. The data format is described below
637*4882a593Smuzhiyun  */
638*4882a593Smuzhiyun extern const struct smu_sdbp_header *smu_get_sdb_partition(int id,
639*4882a593Smuzhiyun 					unsigned int *size);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun /* Get "sdb" partition data from an SMU satellite */
642*4882a593Smuzhiyun extern struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id,
643*4882a593Smuzhiyun 					int id, unsigned int *size);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun #endif /* __KERNEL__ */
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun /*
650*4882a593Smuzhiyun  * - Userland interface -
651*4882a593Smuzhiyun  */
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun /*
654*4882a593Smuzhiyun  * A given instance of the device can be configured for 2 different
655*4882a593Smuzhiyun  * things at the moment:
656*4882a593Smuzhiyun  *
657*4882a593Smuzhiyun  *  - sending SMU commands (default at open() time)
658*4882a593Smuzhiyun  *  - receiving SMU events (not yet implemented)
659*4882a593Smuzhiyun  *
660*4882a593Smuzhiyun  * Commands are written with write() of a command block. They can be
661*4882a593Smuzhiyun  * "driver" commands (for example to switch to event reception mode)
662*4882a593Smuzhiyun  * or real SMU commands. They are made of a header followed by command
663*4882a593Smuzhiyun  * data if any.
664*4882a593Smuzhiyun  *
665*4882a593Smuzhiyun  * For SMU commands (not for driver commands), you can then read() back
666*4882a593Smuzhiyun  * a reply. The reader will be blocked or not depending on how the device
667*4882a593Smuzhiyun  * file is opened. poll() isn't implemented yet. The reply will consist
668*4882a593Smuzhiyun  * of a header as well, followed by the reply data if any. You should
669*4882a593Smuzhiyun  * always provide a buffer large enough for the maximum reply data, I
670*4882a593Smuzhiyun  * recommand one page.
671*4882a593Smuzhiyun  *
672*4882a593Smuzhiyun  * It is illegal to send SMU commands through a file descriptor configured
673*4882a593Smuzhiyun  * for events reception
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  */
676*4882a593Smuzhiyun struct smu_user_cmd_hdr
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	__u32		cmdtype;
679*4882a593Smuzhiyun #define SMU_CMDTYPE_SMU			0	/* SMU command */
680*4882a593Smuzhiyun #define SMU_CMDTYPE_WANTS_EVENTS	1	/* switch fd to events mode */
681*4882a593Smuzhiyun #define SMU_CMDTYPE_GET_PARTITION	2	/* retrieve an sdb partition */
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	__u8		cmd;			/* SMU command byte */
684*4882a593Smuzhiyun 	__u8		pad[3];			/* padding */
685*4882a593Smuzhiyun 	__u32		data_len;		/* Length of data following */
686*4882a593Smuzhiyun };
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun struct smu_user_reply_hdr
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	__u32		status;			/* Command status */
691*4882a593Smuzhiyun 	__u32		reply_len;		/* Length of data follwing */
692*4882a593Smuzhiyun };
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun #endif /*  _SMU_H */
695