Blame view

drivers/macintosh/via-pmu.c 63.5 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
c16a85a5a   Finn Thain   macintosh/via-pmu...
3
   * Device driver for the PMU in Apple PowerBooks and PowerMacs.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
   *
   * The VIA (versatile interface adapter) interfaces to the PMU,
   * a 6805 microprocessor core whose primary function is to control
   * battery charging and system power on the PowerBook 3400 and 2400.
   * The PMU also controls the ADB (Apple Desktop Bus) which connects
   * to the keyboard and mouse, as well as the non-volatile RAM
   * and the RTC (real time clock) chip.
   *
   * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
   * Copyright (C) 2001-2002 Benjamin Herrenschmidt
f91266edb   Johannes Berg   [POWERPC] powerma...
14
   * Copyright (C) 2006-2007 Johannes Berg
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
   *
   * THIS DRIVER IS BECOMING A TOTAL MESS !
   *  - Cleanup atomically disabling reply to PMU events after
   *    a sleep or a freq. switch
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
   *
   */
  #include <stdarg.h>
d851b6e04   Arnd Bergmann   mac: autoconvert ...
22
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/delay.h>
174cd4b1e   Ingo Molnar   sched/headers: Pr...
27
  #include <linux/sched/signal.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
  #include <linux/miscdevice.h>
  #include <linux/blkdev.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
  #include <linux/poll.h>
  #include <linux/adb.h>
  #include <linux/pmu.h>
  #include <linux/cuda.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
  #include <linux/module.h>
  #include <linux/spinlock.h>
  #include <linux/pm.h>
  #include <linux/proc_fs.h>
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
40
  #include <linux/seq_file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/device.h>
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
44
  #include <linux/syscore_ops.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
45
  #include <linux/freezer.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  #include <linux/syscalls.h>
6002f544c   Dave Jones   [PATCH] Fix impli...
47
  #include <linux/suspend.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
  #include <linux/cpu.h>
4cc4587fb   Andreas Schwab   via-pmu: Add comp...
49
  #include <linux/compat.h>
5af507300   Rob Herring   drivers: clean-up...
50
51
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
c16a85a5a   Finn Thain   macintosh/via-pmu...
52
  #include <linux/uaccess.h>
65fddcfca   Mike Rapoport   mm: reorder inclu...
53
  #include <linux/pgtable.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  #include <asm/machdep.h>
  #include <asm/io.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
  #include <asm/sections.h>
  #include <asm/irq.h>
c16a85a5a   Finn Thain   macintosh/via-pmu...
58
  #ifdef CONFIG_PPC_PMAC
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  #include <asm/pmac_feature.h>
5b9ca5269   Benjamin Herrenschmidt   [PATCH] 3/5 power...
60
61
  #include <asm/pmac_pfunc.h>
  #include <asm/pmac_low_i2c.h>
c16a85a5a   Finn Thain   macintosh/via-pmu...
62
  #include <asm/prom.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
  #include <asm/mmu_context.h>
  #include <asm/cputable.h>
  #include <asm/time.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  #include <asm/backlight.h>
c16a85a5a   Finn Thain   macintosh/via-pmu...
67
68
69
70
71
  #else
  #include <asm/macintosh.h>
  #include <asm/macints.h>
  #include <asm/mac_via.h>
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72

9e8e30a0c   Johannes Berg   Input: via-pmu - ...
73
  #include "via-pmu-event.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  /* Some compile options */
f91266edb   Johannes Berg   [POWERPC] powerma...
75
  #undef DEBUG_SLEEP
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  /* How many iterations between battery polls */
  #define BATTERY_POLLING_COUNT	2
d851b6e04   Arnd Bergmann   mac: autoconvert ...
79
  static DEFINE_MUTEX(pmu_info_proc_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  
  /* VIA registers - spaced 0x200 bytes apart */
  #define RS		0x200		/* skip between registers */
  #define B		0		/* B-side data */
  #define A		RS		/* A-side data */
  #define DIRB		(2*RS)		/* B-side direction (1=output) */
  #define DIRA		(3*RS)		/* A-side direction (1=output) */
  #define T1CL		(4*RS)		/* Timer 1 ctr/latch (low 8 bits) */
  #define T1CH		(5*RS)		/* Timer 1 counter (high 8 bits) */
  #define T1LL		(6*RS)		/* Timer 1 latch (low 8 bits) */
  #define T1LH		(7*RS)		/* Timer 1 latch (high 8 bits) */
  #define T2CL		(8*RS)		/* Timer 2 ctr/latch (low 8 bits) */
  #define T2CH		(9*RS)		/* Timer 2 counter (high 8 bits) */
  #define SR		(10*RS)		/* Shift register */
  #define ACR		(11*RS)		/* Auxiliary control register */
  #define PCR		(12*RS)		/* Peripheral control register */
  #define IFR		(13*RS)		/* Interrupt flag register */
  #define IER		(14*RS)		/* Interrupt enable register */
  #define ANH		(15*RS)		/* A-side data, no handshake */
  
  /* Bits in B data register: both active low */
c16a85a5a   Finn Thain   macintosh/via-pmu...
101
  #ifdef CONFIG_PPC_PMAC
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
  #define TACK		0x08		/* Transfer acknowledge (input) */
  #define TREQ		0x10		/* Transfer request (output) */
c16a85a5a   Finn Thain   macintosh/via-pmu...
104
105
106
107
  #else
  #define TACK		0x02
  #define TREQ		0x04
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  
  /* Bits in ACR */
  #define SR_CTRL		0x1c		/* Shift register control bits */
  #define SR_EXT		0x0c		/* Shift on external clock */
  #define SR_OUT		0x10		/* Shift out if 1 */
  
  /* Bits in IFR and IER */
  #define IER_SET		0x80		/* set bits in IER */
  #define IER_CLR		0		/* clear bits in IER */
  #define SR_INT		0x04		/* Shift register full/empty */
  #define CB2_INT		0x08
  #define CB1_INT		0x10		/* transition on CB1 input */
  
  static volatile enum pmu_state {
c57902d52   Finn Thain   macintosh/via-pmu...
122
  	uninitialized = 0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	idle,
  	sending,
  	intack,
  	reading,
  	reading_intr,
  	locked,
  } pmu_state;
  
  static volatile enum int_data_state {
  	int_data_empty,
  	int_data_fill,
  	int_data_ready,
  	int_data_flush
  } int_data_state[2] = { int_data_empty, int_data_empty };
  
  static struct adb_request *current_req;
  static struct adb_request *last_req;
  static struct adb_request *req_awaiting_reply;
  static unsigned char interrupt_data[2][32];
  static int interrupt_data_len[2];
  static int int_data_last;
  static unsigned char *reply_ptr;
  static int data_index;
  static int data_len;
  static volatile int adb_int_pending;
  static volatile int disable_poll;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  static int pmu_kind = PMU_UNKNOWN;
872758563   Olaf Hering   [POWERPC] move va...
150
  static int pmu_fully_inited;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  static int pmu_has_adb;
c16a85a5a   Finn Thain   macintosh/via-pmu...
152
  #ifdef CONFIG_PPC_PMAC
c70c35da5   Finn Thain   macintosh/via-pmu...
153
154
  static volatile unsigned char __iomem *via1;
  static volatile unsigned char __iomem *via2;
c16a85a5a   Finn Thain   macintosh/via-pmu...
155
  static struct device_node *vias;
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
156
  static struct device_node *gpio_node;
c16a85a5a   Finn Thain   macintosh/via-pmu...
157
  #endif
872758563   Olaf Hering   [POWERPC] move va...
158
  static unsigned char __iomem *gpio_reg;
ef24ba709   Michael Ellerman   powerpc: Remove a...
159
  static int gpio_irq = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  static int gpio_irq_enabled = -1;
872758563   Olaf Hering   [POWERPC] move va...
161
  static volatile int pmu_suspended;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
165
  static spinlock_t pmu_lock;
  static u8 pmu_intr_mask;
  static int pmu_version;
  static int drop_interrupts;
f91266edb   Johannes Berg   [POWERPC] powerma...
166
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
  static int option_lid_wakeup = 1;
f91266edb   Johannes Berg   [POWERPC] powerma...
168
  #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
  static unsigned long async_req_locks;
6edc22fc9   Finn Thain   macintosh/via-pmu...
170
171
172
  
  #define NUM_IRQ_STATS 13
  static unsigned int pmu_irq_stats[NUM_IRQ_STATS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
177
178
  
  static struct proc_dir_entry *proc_pmu_root;
  static struct proc_dir_entry *proc_pmu_info;
  static struct proc_dir_entry *proc_pmu_irqstats;
  static struct proc_dir_entry *proc_pmu_options;
  static int option_server_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
  int pmu_battery_count;
  int pmu_cur_battery;
a334bdbdd   Olaf Hering   [POWERPC] Correct...
181
  unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
  struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
  static int query_batt_timer = BATTERY_POLLING_COUNT;
  static struct adb_request batt_req;
  static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
  int __fake_sleep;
  int asleep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
  
  #ifdef CONFIG_ADB
872758563   Olaf Hering   [POWERPC] move va...
191
  static int adb_dev_map;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
195
196
197
198
199
200
201
  static int pmu_adb_flags;
  
  static int pmu_probe(void);
  static int pmu_init(void);
  static int pmu_send_request(struct adb_request *req, int sync);
  static int pmu_adb_autopoll(int devs);
  static int pmu_adb_reset_bus(void);
  #endif /* CONFIG_ADB */
  
  static int init_pmu(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  static void pmu_start(void);
7d12e780e   David Howells   IRQ: Maintain reg...
203
204
  static irqreturn_t via_pmu_interrupt(int irq, void *arg);
  static irqreturn_t gpio1_interrupt(int irq, void *arg);
3f3942aca   Christoph Hellwig   proc: introduce p...
205
206
207
  static int pmu_info_proc_show(struct seq_file *m, void *v);
  static int pmu_irqstats_proc_show(struct seq_file *m, void *v);
  static int pmu_battery_proc_show(struct seq_file *m, void *v);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  static void pmu_pass_intr(unsigned char *data, int len);
97a32539b   Alexey Dobriyan   proc: convert eve...
209
  static const struct proc_ops pmu_options_proc_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
  
  #ifdef CONFIG_ADB
58935176a   Mathieu Malaterre   powerpc/via-pmu: ...
212
  const struct adb_driver via_pmu_driver = {
3a52f6f98   Finn Thain   macintosh/adb: Us...
213
214
215
216
217
218
219
  	.name         = "PMU",
  	.probe        = pmu_probe,
  	.init         = pmu_init,
  	.send_request = pmu_send_request,
  	.autopoll     = pmu_adb_autopoll,
  	.poll         = pmu_poll_adb,
  	.reset_bus    = pmu_adb_reset_bus,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
227
228
  };
  #endif /* CONFIG_ADB */
  
  extern void low_sleep_handler(void);
  extern void enable_kernel_altivec(void);
  extern void enable_kernel_fp(void);
  
  #ifdef DEBUG_SLEEP
  int pmu_polled_request(struct adb_request *req);
f91266edb   Johannes Berg   [POWERPC] powerma...
229
  void pmu_blink(int n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
233
234
235
236
237
238
  #endif
  
  /*
   * This table indicates for each PMU opcode:
   * - the number of data bytes to be sent with the command, or -1
   *   if a length byte should be sent,
   * - the number of response bytes which the PMU will return, or
   *   -1 if it will send a length byte.
   */
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
239
  static const s8 pmu_data_len[256][2] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  /*	   0	   1	   2	   3	   4	   5	   6	   7  */
  /*00*/	{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*08*/	{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*10*/	{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*18*/	{ 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
  /*20*/	{-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*28*/	{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
  /*30*/	{ 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*38*/	{ 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
  /*40*/	{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*48*/	{ 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
  /*50*/	{ 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
  /*58*/	{ 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
  /*60*/	{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*68*/	{ 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
  /*70*/	{ 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*78*/	{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
  /*80*/	{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*88*/	{ 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*90*/	{ 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*98*/	{ 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*a0*/	{ 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
  /*a8*/	{ 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*b0*/	{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*b8*/	{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*c0*/	{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*c8*/	{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  /*d0*/	{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*d8*/	{ 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
  /*e0*/	{-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
  /*e8*/	{ 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
  /*f0*/	{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
  /*f8*/	{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
  };
  
  static char *pbook_type[] = {
  	"Unknown PowerBook",
  	"PowerBook 2400/3400/3500(G3)",
  	"PowerBook G3 Series",
  	"1999 PowerBook G3",
  	"Core99"
  };
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
282
  int __init find_via_pmu(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  {
c16a85a5a   Finn Thain   macintosh/via-pmu...
284
  #ifdef CONFIG_PPC_PMAC
cc5d0189b   Benjamin Herrenschmidt   [PATCH] powerpc: ...
285
  	u64 taddr;
018a3d1db   Jeremy Kerr   [POWERPC] powerma...
286
  	const u32 *reg;
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
287

c57902d52   Finn Thain   macintosh/via-pmu...
288
  	if (pmu_state != uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  		return 1;
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
290
291
  	vias = of_find_node_by_name(NULL, "via-pmu");
  	if (vias == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293

01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
294
  	reg = of_get_property(vias, "reg", NULL);
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
295
296
297
298
299
300
  	if (reg == NULL) {
  		printk(KERN_ERR "via-pmu: No \"reg\" property !
  ");
  		goto fail;
  	}
  	taddr = of_translate_address(vias, reg);
bb6b9b28d   Benjamin Herrenschmidt   [PATCH] powerpc: ...
301
  	if (taddr == OF_BAD_ADDR) {
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
302
303
304
  		printk(KERN_ERR "via-pmu: Can't translate address !
  ");
  		goto fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
307
308
309
310
311
312
313
314
315
  	}
  
  	spin_lock_init(&pmu_lock);
  
  	pmu_has_adb = 1;
  
  	pmu_intr_mask =	PMU_INT_PCEJECT |
  			PMU_INT_SNDBRT |
  			PMU_INT_ADB |
  			PMU_INT_TICK;
  	
f1e0addca   Rob Herring   macintosh: Use of...
316
317
  	if (of_node_name_eq(vias->parent, "ohare") ||
  	    of_device_is_compatible(vias->parent, "ohare"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  		pmu_kind = PMU_OHARE_BASED;
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
319
  	else if (of_device_is_compatible(vias->parent, "paddington"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  		pmu_kind = PMU_PADDINGTON_BASED;
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
321
  	else if (of_device_is_compatible(vias->parent, "heathrow"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
  		pmu_kind = PMU_HEATHROW_BASED;
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
323
324
  	else if (of_device_is_compatible(vias->parent, "Keylargo")
  		 || of_device_is_compatible(vias->parent, "K2-Keylargo")) {
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
325
  		struct device_node *gpiop;
1658ab667   Stephen Rothwell   [POWERPC] Remove ...
326
  		struct device_node *adbp;
cc5d0189b   Benjamin Herrenschmidt   [PATCH] powerpc: ...
327
  		u64 gaddr = OF_BAD_ADDR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
  
  		pmu_kind = PMU_KEYLARGO_BASED;
1658ab667   Stephen Rothwell   [POWERPC] Remove ...
330
331
332
  		adbp = of_find_node_by_type(NULL, "adb");
  		pmu_has_adb = (adbp != NULL);
  		of_node_put(adbp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
334
335
336
337
338
  		pmu_intr_mask =	PMU_INT_PCEJECT |
  				PMU_INT_SNDBRT |
  				PMU_INT_ADB |
  				PMU_INT_TICK |
  				PMU_INT_ENVIRONMENT;
  		
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
339
340
  		gpiop = of_find_node_by_name(NULL, "gpio");
  		if (gpiop) {
01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
341
  			reg = of_get_property(gpiop, "reg", NULL);
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
342
343
  			if (reg)
  				gaddr = of_translate_address(gpiop, reg);
cc5d0189b   Benjamin Herrenschmidt   [PATCH] powerpc: ...
344
  			if (gaddr != OF_BAD_ADDR)
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
345
  				gpio_reg = ioremap(gaddr, 0x10);
e702240e8   Phil Carmody   powerpc/via-pmu: ...
346
  			of_node_put(gpiop);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
  		}
61e37ca22   Olaf Hering   [POWERPC] Avoid N...
348
  		if (gpio_reg == NULL) {
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
349
350
  			printk(KERN_ERR "via-pmu: Can't find GPIO reg !
  ");
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
351
  			goto fail;
61e37ca22   Olaf Hering   [POWERPC] Avoid N...
352
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
  	} else
  		pmu_kind = PMU_UNKNOWN;
c70c35da5   Finn Thain   macintosh/via-pmu...
355
356
  	via1 = via2 = ioremap(taddr, 0x2000);
  	if (via1 == NULL) {
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
357
358
  		printk(KERN_ERR "via-pmu: Can't map address !
  ");
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
359
  		goto fail_via_remap;
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
360
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
  	
c70c35da5   Finn Thain   macintosh/via-pmu...
362
363
  	out_8(&via1[IER], IER_CLR | 0x7f);	/* disable all intrs */
  	out_8(&via1[IFR], 0x7f);			/* clear IFR */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
  
  	pmu_state = idle;
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
366
367
  	if (!init_pmu())
  		goto fail_init;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
  	sys_ctrler = SYS_CTRLER_PMU;
  	
  	return 1;
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
372
373
  
   fail_init:
c70c35da5   Finn Thain   macintosh/via-pmu...
374
375
  	iounmap(via1);
  	via1 = via2 = NULL;
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
376
   fail_via_remap:
61e37ca22   Olaf Hering   [POWERPC] Avoid N...
377
378
  	iounmap(gpio_reg);
  	gpio_reg = NULL;
ffa3eb010   Phil Carmody   powerpc/via-pmu: ...
379
380
   fail:
  	of_node_put(vias);
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
381
  	vias = NULL;
c57902d52   Finn Thain   macintosh/via-pmu...
382
  	pmu_state = uninitialized;
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
383
  	return 0;
c16a85a5a   Finn Thain   macintosh/via-pmu...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  #else
  	if (macintosh_config->adb_type != MAC_ADB_PB2)
  		return 0;
  
  	pmu_kind = PMU_UNKNOWN;
  
  	spin_lock_init(&pmu_lock);
  
  	pmu_has_adb = 1;
  
  	pmu_intr_mask =	PMU_INT_PCEJECT |
  			PMU_INT_SNDBRT |
  			PMU_INT_ADB |
  			PMU_INT_TICK;
  
  	pmu_state = idle;
  
  	if (!init_pmu()) {
  		pmu_state = uninitialized;
  		return 0;
  	}
  
  	return 1;
  #endif /* !CONFIG_PPC_PMAC */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
  }
  
  #ifdef CONFIG_ADB
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
411
  static int pmu_probe(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
  {
c57902d52   Finn Thain   macintosh/via-pmu...
413
  	return pmu_state == uninitialized ? -ENODEV : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  }
73f4447d4   Finn Thain   macintosh/via-pmu...
415
  static int pmu_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
  {
c57902d52   Finn Thain   macintosh/via-pmu...
417
  	return pmu_state == uninitialized ? -ENODEV : 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
420
421
422
423
424
425
426
427
428
429
  }
  #endif /* CONFIG_ADB */
  
  /*
   * We can't wait until pmu_init gets called, that happens too late.
   * It happens after IDE and SCSI initialization, which can take a few
   * seconds, and by that time the PMU could have given up on us and
   * turned us off.
   * Thus this is called with arch_initcall rather than device_initcall.
   */
  static int __init via_pmu_start(void)
  {
c16a85a5a   Finn Thain   macintosh/via-pmu...
430
  	unsigned int __maybe_unused irq;
0ebfff149   Benjamin Herrenschmidt   [POWERPC] Add new...
431

c57902d52   Finn Thain   macintosh/via-pmu...
432
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
  	batt_req.complete = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435

c16a85a5a   Finn Thain   macintosh/via-pmu...
436
  #ifdef CONFIG_PPC_PMAC
0ebfff149   Benjamin Herrenschmidt   [POWERPC] Add new...
437
  	irq = irq_of_parse_and_map(vias, 0);
ef24ba709   Michael Ellerman   powerpc: Remove a...
438
  	if (!irq) {
7b52b440d   Michael Buesch   [POWERPC] via-pmu...
439
440
  		printk(KERN_ERR "via-pmu: can't map interrupt
  ");
0ebfff149   Benjamin Herrenschmidt   [POWERPC] Add new...
441
442
  		return -ENODEV;
  	}
ba461f094   Ian Campbell   powerpc: Use IRQF...
443
444
445
  	/* We set IRQF_NO_SUSPEND because we don't want the interrupt
  	 * to be disabled between the 2 passes of driver suspend, we
  	 * control our own disabling for that one
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
446
  	 */
ba461f094   Ian Campbell   powerpc: Use IRQF...
447
448
  	if (request_irq(irq, via_pmu_interrupt, IRQF_NO_SUSPEND,
  			"VIA-PMU", (void *)0)) {
0ebfff149   Benjamin Herrenschmidt   [POWERPC] Add new...
449
450
451
  		printk(KERN_ERR "via-pmu: can't request irq %d
  ", irq);
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  	}
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
453
454
455
456
457
  	if (pmu_kind == PMU_KEYLARGO_BASED) {
  		gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
  		if (gpio_node == NULL)
  			gpio_node = of_find_node_by_name(NULL,
  							 "pmu-interrupt");
0ebfff149   Benjamin Herrenschmidt   [POWERPC] Add new...
458
459
  		if (gpio_node)
  			gpio_irq = irq_of_parse_and_map(gpio_node, 0);
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
460

ef24ba709   Michael Ellerman   powerpc: Remove a...
461
  		if (gpio_irq) {
6c3082151   John Ogness   powerpc/powermac:...
462
463
464
  			if (request_irq(gpio_irq, gpio1_interrupt,
  					IRQF_NO_SUSPEND, "GPIO1 ADB",
  					(void *)0))
51d3082fe   Benjamin Herrenschmidt   [PATCH] powerpc: ...
465
466
467
468
469
470
  				printk(KERN_ERR "pmu: can't get irq %d"
  				       " (GPIO1)
  ", gpio_irq);
  			else
  				gpio_irq_enabled = 1;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
  	}
  
  	/* Enable interrupts */
c70c35da5   Finn Thain   macintosh/via-pmu...
474
  	out_8(&via1[IER], IER_SET | SR_INT | CB1_INT);
c16a85a5a   Finn Thain   macintosh/via-pmu...
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  #else
  	if (request_irq(IRQ_MAC_ADB_SR, via_pmu_interrupt, IRQF_NO_SUSPEND,
  			"VIA-PMU-SR", NULL)) {
  		pr_err("%s: couldn't get SR irq
  ", __func__);
  		return -ENODEV;
  	}
  	if (request_irq(IRQ_MAC_ADB_CL, via_pmu_interrupt, IRQF_NO_SUSPEND,
  			"VIA-PMU-CL", NULL)) {
  		pr_err("%s: couldn't get CL irq
  ", __func__);
  		free_irq(IRQ_MAC_ADB_SR, NULL);
  		return -ENODEV;
  	}
  #endif /* !CONFIG_PPC_PMAC */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  
  	pmu_fully_inited = 1;
  
  	/* Make sure PMU settle down before continuing. This is _very_ important
  	 * since the IDE probe may shut interrupts down for quite a bit of time. If
  	 * a PMU communication is pending while this happens, the PMU may timeout
  	 * Not that on Core99 machines, the PMU keeps sending us environement
  	 * messages, we should find a way to either fix IDE or make it call
  	 * pmu_suspend() before masking interrupts. This can also happens while
  	 * scolling with some fbdevs.
  	 */
  	do {
  		pmu_poll();
  	} while (pmu_state != idle);
  
  	return 0;
  }
  
  arch_initcall(via_pmu_start);
  
  /*
   * This has to be done after pci_init, which is a subsys_initcall.
   */
  static int __init via_pmu_dev_init(void)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
515
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
517
  #ifdef CONFIG_PMAC_BACKLIGHT
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
518
  	/* Initialize backlight */
4b755999d   Michael Hanselmann   [PATCH] powermac:...
519
  	pmu_backlight_init();
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
520
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521

8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
522
  #ifdef CONFIG_PPC32
71a157e8e   Grant Likely   of: add 'of_' pre...
523
524
    	if (of_machine_is_compatible("AAPL,3400/2400") ||
    		of_machine_is_compatible("AAPL,3500")) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525
526
527
528
529
530
531
  		int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
  			NULL, PMAC_MB_INFO_MODEL, 0);
  		pmu_battery_count = 1;
  		if (mb == PMAC_TYPE_COMET)
  			pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
  		else
  			pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
71a157e8e   Grant Likely   of: add 'of_' pre...
532
533
  	} else if (of_machine_is_compatible("AAPL,PowerBook1998") ||
  		of_machine_is_compatible("PowerBook1,1")) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534
535
536
537
  		pmu_battery_count = 2;
  		pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
  		pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
  	} else {
30686ba6d   Stephen Rothwell   [POWERPC] Remove ...
538
539
  		struct device_node* prim =
  			of_find_node_by_name(NULL, "power-mgt");
018a3d1db   Jeremy Kerr   [POWERPC] powerma...
540
  		const u32 *prim_info = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  		if (prim)
01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
542
  			prim_info = of_get_property(prim, "prim-info", NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
544
545
546
547
548
549
  		if (prim_info) {
  			/* Other stuffs here yet unknown */
  			pmu_battery_count = (prim_info[6] >> 16) & 0xff;
  			pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
  			if (pmu_battery_count > 1)
  				pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
  		}
30686ba6d   Stephen Rothwell   [POWERPC] Remove ...
550
  		of_node_put(prim);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
  	}
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
552
  #endif /* CONFIG_PPC32 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
554
555
  	/* Create /proc/pmu */
  	proc_pmu_root = proc_mkdir("pmu", NULL);
  	if (proc_pmu_root) {
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
556
  		long i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
  
  		for (i=0; i<pmu_battery_count; i++) {
  			char title[16];
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
560
  			sprintf(title, "battery_%ld", i);
3f3942aca   Christoph Hellwig   proc: introduce p...
561
562
563
  			proc_pmu_batt[i] = proc_create_single_data(title, 0,
  					proc_pmu_root, pmu_battery_proc_show,
  					(void *)i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
565

3f3942aca   Christoph Hellwig   proc: introduce p...
566
567
568
569
  		proc_pmu_info = proc_create_single("info", 0, proc_pmu_root,
  				pmu_info_proc_show);
  		proc_pmu_irqstats = proc_create_single("interrupts", 0,
  				proc_pmu_root, pmu_irqstats_proc_show);
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
570
  		proc_pmu_options = proc_create("options", 0600, proc_pmu_root,
97a32539b   Alexey Dobriyan   proc: convert eve...
571
  						&pmu_options_proc_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
573
574
575
576
  	}
  	return 0;
  }
  
  device_initcall(via_pmu_dev_init);
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
577
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
580
581
  init_pmu(void)
  {
  	int timeout;
  	struct adb_request req;
576d5290d   Finn Thain   macintosh/via-pmu...
582
  	/* Negate TREQ. Set TACK to input and TREQ to output. */
c70c35da5   Finn Thain   macintosh/via-pmu...
583
584
  	out_8(&via2[B], in_8(&via2[B]) | TREQ);
  	out_8(&via2[DIRB], (in_8(&via2[DIRB]) | TREQ) & ~TACK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  
  	pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
  	timeout =  100000;
  	while (!req.complete) {
  		if (--timeout < 0) {
  			printk(KERN_ERR "init_pmu: no response from PMU
  ");
  			return 0;
  		}
  		udelay(10);
  		pmu_poll();
  	}
  
  	/* ack all pending interrupts */
  	timeout = 100000;
  	interrupt_data[0][0] = 1;
  	while (interrupt_data[0][0] || pmu_state != idle) {
  		if (--timeout < 0) {
  			printk(KERN_ERR "init_pmu: timed out acking intrs
  ");
  			return 0;
  		}
  		if (pmu_state == idle)
  			adb_int_pending = 1;
7d12e780e   David Howells   IRQ: Maintain reg...
609
  		via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  		udelay(10);
  	}
  
  	/* Tell PMU we are ready.  */
  	if (pmu_kind == PMU_KEYLARGO_BASED) {
  		pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
  		while (!req.complete)
  			pmu_poll();
  	}
  
  	/* Read PMU version */
  	pmu_request(&req, NULL, 1, PMU_GET_VERSION);
  	pmu_wait_complete(&req);
  	if (req.reply_len > 0)
  		pmu_version = req.reply[0];
  	
  	/* Read server mode setting */
  	if (pmu_kind == PMU_KEYLARGO_BASED) {
  		pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
  			    PMU_PWR_GET_POWERUP_EVENTS);
  		pmu_wait_complete(&req);
  		if (req.reply_len == 2) {
  			if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
  				option_server_mode = 1;
  			printk(KERN_INFO "via-pmu: Server Mode is %s
  ",
  			       option_server_mode ? "enabled" : "disabled");
  		}
  	}
c16a85a5a   Finn Thain   macintosh/via-pmu...
639
640
641
642
  
  	printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x
  ",
  	       PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
644
645
646
647
648
649
650
  	return 1;
  }
  
  int
  pmu_get_model(void)
  {
  	return pmu_kind;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  static void pmu_set_server_mode(int server_mode)
  {
  	struct adb_request req;
  
  	if (pmu_kind != PMU_KEYLARGO_BASED)
  		return;
  
  	option_server_mode = server_mode;
  	pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
  	pmu_wait_complete(&req);
  	if (req.reply_len < 2)
  		return;
  	if (server_mode)
  		pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
  			    PMU_PWR_SET_POWERUP_EVENTS,
  			    req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); 
  	else
  		pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
  			    PMU_PWR_CLR_POWERUP_EVENTS,
  			    req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); 
  	pmu_wait_complete(&req);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
  /* This new version of the code for 2400/3400/3500 powerbooks
   * is inspired from the implementation in gkrellm-pmu
   */
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
676
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
678
  done_battery_state_ohare(struct adb_request* req)
  {
c16a85a5a   Finn Thain   macintosh/via-pmu...
679
  #ifdef CONFIG_PPC_PMAC
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  	/* format:
  	 *  [0]    :  flags
  	 *    0x01 :  AC indicator
  	 *    0x02 :  charging
  	 *    0x04 :  battery exist
  	 *    0x08 :  
  	 *    0x10 :  
  	 *    0x20 :  full charged
  	 *    0x40 :  pcharge reset
  	 *    0x80 :  battery exist
  	 *
  	 *  [1][2] :  battery voltage
  	 *  [3]    :  CPU temperature
  	 *  [4]    :  battery temperature
  	 *  [5]    :  current
  	 *  [6][7] :  pcharge
  	 *              --tkoba
  	 */
  	unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
  	long pcharge, charge, vb, vmax, lmax;
  	long vmax_charging, vmax_charged;
  	long amperage, voltage, time, max;
  	int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
  			NULL, PMAC_MB_INFO_MODEL, 0);
  
  	if (req->reply[0] & 0x01)
  		pmu_power_flags |= PMU_PWR_AC_PRESENT;
  	else
  		pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
  	
  	if (mb == PMAC_TYPE_COMET) {
  		vmax_charged = 189;
  		vmax_charging = 213;
  		lmax = 6500;
  	} else {
  		vmax_charged = 330;
  		vmax_charging = 330;
  		lmax = 6500;
  	}
  	vmax = vmax_charged;
  
  	/* If battery installed */
  	if (req->reply[0] & 0x04) {
  		bat_flags |= PMU_BATT_PRESENT;
  		if (req->reply[0] & 0x02)
  			bat_flags |= PMU_BATT_CHARGING;
  		vb = (req->reply[1] << 8) | req->reply[2];
  		voltage = (vb * 265 + 72665) / 10;
  		amperage = req->reply[5];
  		if ((req->reply[0] & 0x01) == 0) {
  			if (amperage > 200)
  				vb += ((amperage - 200) * 15)/100;
  		} else if (req->reply[0] & 0x02) {
  			vb = (vb * 97) / 100;
  			vmax = vmax_charging;
  		}
  		charge = (100 * vb) / vmax;
  		if (req->reply[0] & 0x40) {
  			pcharge = (req->reply[6] << 8) + req->reply[7];
  			if (pcharge > lmax)
  				pcharge = lmax;
  			pcharge *= 100;
  			pcharge = 100 - pcharge / lmax;
  			if (pcharge < charge)
  				charge = pcharge;
  		}
  		if (amperage > 0)
  			time = (charge * 16440) / amperage;
  		else
  			time = 0;
  		max = 100;
  		amperage = -amperage;
  	} else
  		charge = max = amperage = voltage = time = 0;
  
  	pmu_batteries[pmu_cur_battery].flags = bat_flags;
  	pmu_batteries[pmu_cur_battery].charge = charge;
  	pmu_batteries[pmu_cur_battery].max_charge = max;
  	pmu_batteries[pmu_cur_battery].amperage = amperage;
  	pmu_batteries[pmu_cur_battery].voltage = voltage;
  	pmu_batteries[pmu_cur_battery].time_remaining = time;
c16a85a5a   Finn Thain   macintosh/via-pmu...
761
  #endif /* CONFIG_PPC_PMAC */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
764
  
  	clear_bit(0, &async_req_locks);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
765
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  done_battery_state_smart(struct adb_request* req)
  {
  	/* format:
  	 *  [0] : format of this structure (known: 3,4,5)
  	 *  [1] : flags
  	 *  
  	 *  format 3 & 4:
  	 *  
  	 *  [2] : charge
  	 *  [3] : max charge
  	 *  [4] : current
  	 *  [5] : voltage
  	 *  
  	 *  format 5:
  	 *  
  	 *  [2][3] : charge
  	 *  [4][5] : max charge
  	 *  [6][7] : current
  	 *  [8][9] : voltage
  	 */
  	 
  	unsigned int bat_flags = PMU_BATT_TYPE_SMART;
  	int amperage;
  	unsigned int capa, max, voltage;
  	
  	if (req->reply[1] & 0x01)
  		pmu_power_flags |= PMU_PWR_AC_PRESENT;
  	else
  		pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
  
  
  	capa = max = amperage = voltage = 0;
  	
  	if (req->reply[1] & 0x04) {
  		bat_flags |= PMU_BATT_PRESENT;
  		switch(req->reply[0]) {
  			case 3:
  			case 4: capa = req->reply[2];
  				max = req->reply[3];
  				amperage = *((signed char *)&req->reply[4]);
  				voltage = req->reply[5];
  				break;
  			case 5: capa = (req->reply[2] << 8) | req->reply[3];
  				max = (req->reply[4] << 8) | req->reply[5];
  				amperage = *((signed short *)&req->reply[6]);
  				voltage = (req->reply[8] << 8) | req->reply[9];
  				break;
  			default:
ebd004e4e   Andy Shevchenko   powerpc/pmac/smu:...
814
815
816
817
  				pr_warn("pmu.c: unrecognized battery info, "
  					"len: %d, %4ph
  ", req->reply_len,
  							   req->reply);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  				break;
  		}
  	}
  
  	if ((req->reply[1] & 0x01) && (amperage > 0))
  		bat_flags |= PMU_BATT_CHARGING;
  
  	pmu_batteries[pmu_cur_battery].flags = bat_flags;
  	pmu_batteries[pmu_cur_battery].charge = capa;
  	pmu_batteries[pmu_cur_battery].max_charge = max;
  	pmu_batteries[pmu_cur_battery].amperage = amperage;
  	pmu_batteries[pmu_cur_battery].voltage = voltage;
  	if (amperage) {
  		if ((req->reply[1] & 0x01) && (amperage > 0))
  			pmu_batteries[pmu_cur_battery].time_remaining
  				= ((max-capa) * 3600) / amperage;
  		else
  			pmu_batteries[pmu_cur_battery].time_remaining
  				= (capa * 3600) / (-amperage);
  	} else
  		pmu_batteries[pmu_cur_battery].time_remaining = 0;
  
  	pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
  
  	clear_bit(0, &async_req_locks);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
844
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
847
848
849
850
851
852
853
854
855
  query_battery_state(void)
  {
  	if (test_and_set_bit(0, &async_req_locks))
  		return;
  	if (pmu_kind == PMU_OHARE_BASED)
  		pmu_request(&batt_req, done_battery_state_ohare,
  			1, PMU_BATTERY_STATE);
  	else
  		pmu_request(&batt_req, done_battery_state_smart,
  			2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
  }
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
856
  static int pmu_info_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857
  {
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
858
859
860
861
862
863
  	seq_printf(m, "PMU driver version     : %d
  ", PMU_DRIVER_VERSION);
  	seq_printf(m, "PMU firmware version   : %02x
  ", pmu_version);
  	seq_printf(m, "AC Power               : %d
  ",
63e1fd41c   Benjamin Herrenschmidt   [PATCH] macintosh...
864
  		((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
865
866
867
868
869
  	seq_printf(m, "Battery count          : %d
  ", pmu_battery_count);
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870

9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
871
  static int pmu_irqstats_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872
873
  {
  	int i;
6edc22fc9   Finn Thain   macintosh/via-pmu...
874
875
876
  	static const char *irq_names[NUM_IRQ_STATS] = {
  		"Unknown interrupt (type 0)",
  		"Unknown interrupt (type 1)",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
878
879
880
881
882
883
884
  		"PC-Card eject button",
  		"Sound/Brightness button",
  		"ADB message",
  		"Battery state change",
  		"Environment interrupt",
  		"Tick timer",
  		"Ghost interrupt (zero len)",
  		"Empty interrupt (empty mask)",
6edc22fc9   Finn Thain   macintosh/via-pmu...
885
886
887
  		"Max irqs in a row",
  		"Total CB1 triggered events",
  		"Total GPIO1 triggered events",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
          };
6edc22fc9   Finn Thain   macintosh/via-pmu...
889
  	for (i = 0; i < NUM_IRQ_STATS; i++) {
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
890
891
  		seq_printf(m, " %2u: %10u (%s)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
893
  			     i, pmu_irq_stats[i], irq_names[i]);
  	}
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
894
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
  }
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
896
897
898
  static int pmu_battery_proc_show(struct seq_file *m, void *v)
  {
  	long batnum = (long)m->private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
  	
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
  	seq_putc(m, '
  ');
  	seq_printf(m, "flags      : %08x
  ", pmu_batteries[batnum].flags);
  	seq_printf(m, "charge     : %d
  ", pmu_batteries[batnum].charge);
  	seq_printf(m, "max_charge : %d
  ", pmu_batteries[batnum].max_charge);
  	seq_printf(m, "current    : %d
  ", pmu_batteries[batnum].amperage);
  	seq_printf(m, "voltage    : %d
  ", pmu_batteries[batnum].voltage);
  	seq_printf(m, "time rem.  : %d
  ", pmu_batteries[batnum].time_remaining);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916

9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
917
918
  static int pmu_options_proc_show(struct seq_file *m, void *v)
  {
f91266edb   Johannes Berg   [POWERPC] powerma...
919
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
  	if (pmu_kind == PMU_KEYLARGO_BASED &&
  	    pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
922
923
  		seq_printf(m, "lid_wakeup=%d
  ", option_lid_wakeup);
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
924
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
925
  	if (pmu_kind == PMU_KEYLARGO_BASED)
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
926
927
  		seq_printf(m, "server_mode=%d
  ", option_server_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928

9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
929
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
  }
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
931
932
933
934
935
936
937
938
  
  static int pmu_options_proc_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, pmu_options_proc_show, NULL);
  }
  
  static ssize_t pmu_options_proc_write(struct file *file,
  		const char __user *buffer, size_t count, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
  {
  	char tmp[33];
  	char *label, *val;
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
942
  	size_t fcount = count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
  	
  	if (!count)
  		return -EINVAL;
  	if (count > 32)
  		count = 32;
  	if (copy_from_user(tmp, buffer, count))
  		return -EFAULT;
  	tmp[count] = 0;
  
  	label = tmp;
  	while(*label == ' ')
  		label++;
  	val = label;
  	while(*val && (*val != '=')) {
  		if (*val == ' ')
  			*val = 0;
  		val++;
  	}
  	if ((*val) == 0)
  		return -EINVAL;
  	*(val++) = 0;
  	while(*val == ' ')
  		val++;
f91266edb   Johannes Berg   [POWERPC] powerma...
966
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
968
969
970
  	if (pmu_kind == PMU_KEYLARGO_BASED &&
  	    pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
  		if (!strcmp(label, "lid_wakeup"))
  			option_lid_wakeup = ((*val) == '1');
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
971
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
974
975
976
977
978
979
  	if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
  		int new_value;
  		new_value = ((*val) == '1');
  		if (new_value != option_server_mode)
  			pmu_set_server_mode(new_value);
  	}
  	return fcount;
  }
97a32539b   Alexey Dobriyan   proc: convert eve...
980
981
982
983
984
985
  static const struct proc_ops pmu_options_proc_ops = {
  	.proc_open	= pmu_options_proc_open,
  	.proc_read	= seq_read,
  	.proc_lseek	= seq_lseek,
  	.proc_release	= single_release,
  	.proc_write	= pmu_options_proc_write,
9d2f7342d   Alexey Dobriyan   powerpc/via-pmu: ...
986
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
  #ifdef CONFIG_ADB
  /* Send an ADB command */
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
989
  static int pmu_send_request(struct adb_request *req, int sync)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
990
991
  {
  	int i, ret;
c57902d52   Finn Thain   macintosh/via-pmu...
992
  	if (pmu_state == uninitialized || !pmu_fully_inited) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
  		req->complete = 1;
  		return -ENXIO;
  	}
  
  	ret = -EINVAL;
  
  	switch (req->data[0]) {
  	case PMU_PACKET:
  		for (i = 0; i < req->nbytes - 1; ++i)
  			req->data[i] = req->data[i+1];
  		--req->nbytes;
  		if (pmu_data_len[req->data[0]][1] != 0) {
  			req->reply[0] = ADB_RET_OK;
  			req->reply_len = 1;
  		} else
  			req->reply_len = 0;
  		ret = pmu_queue_request(req);
  		break;
  	case CUDA_PACKET:
  		switch (req->data[1]) {
  		case CUDA_GET_TIME:
  			if (req->nbytes != 2)
  				break;
  			req->data[0] = PMU_READ_RTC;
  			req->nbytes = 1;
  			req->reply_len = 3;
  			req->reply[0] = CUDA_PACKET;
  			req->reply[1] = 0;
  			req->reply[2] = CUDA_GET_TIME;
  			ret = pmu_queue_request(req);
  			break;
  		case CUDA_SET_TIME:
  			if (req->nbytes != 6)
  				break;
  			req->data[0] = PMU_SET_RTC;
  			req->nbytes = 5;
  			for (i = 1; i <= 4; ++i)
  				req->data[i] = req->data[i+1];
  			req->reply_len = 3;
  			req->reply[0] = CUDA_PACKET;
  			req->reply[1] = 0;
  			req->reply[2] = CUDA_SET_TIME;
  			ret = pmu_queue_request(req);
  			break;
  		}
  		break;
  	case ADB_PACKET:
  	    	if (!pmu_has_adb)
      			return -ENXIO;
  		for (i = req->nbytes - 1; i > 1; --i)
  			req->data[i+2] = req->data[i];
  		req->data[3] = req->nbytes - 2;
  		req->data[2] = pmu_adb_flags;
  		/*req->data[1] = req->data[1];*/
  		req->data[0] = PMU_ADB_CMD;
  		req->nbytes += 2;
  		req->reply_expected = 1;
  		req->reply_len = 0;
  		ret = pmu_queue_request(req);
  		break;
  	}
  	if (ret) {
  		req->complete = 1;
  		return ret;
  	}
  
  	if (sync)
  		while (!req->complete)
  			pmu_poll();
  
  	return 0;
  }
  
  /* Enable/disable autopolling */
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1067
  static int __pmu_adb_autopoll(int devs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1068
1069
  {
  	struct adb_request req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
  	if (devs) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
  		pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
  			    adb_dev_map >> 8, adb_dev_map);
  		pmu_adb_flags = 2;
  	} else {
  		pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
  		pmu_adb_flags = 0;
  	}
  	while (!req.complete)
  		pmu_poll();
  	return 0;
  }
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1082
1083
  static int pmu_adb_autopoll(int devs)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
1084
  	if (pmu_state == uninitialized || !pmu_fully_inited || !pmu_has_adb)
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1085
1086
1087
1088
1089
  		return -ENXIO;
  
  	adb_dev_map = devs;
  	return __pmu_adb_autopoll(devs);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1090
  /* Reset the ADB bus */
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1091
  static int pmu_adb_reset_bus(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1092
1093
1094
  {
  	struct adb_request req;
  	int save_autopoll = adb_dev_map;
c57902d52   Finn Thain   macintosh/via-pmu...
1095
  	if (pmu_state == uninitialized || !pmu_fully_inited || !pmu_has_adb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1096
1097
1098
  		return -ENXIO;
  
  	/* anyone got a better idea?? */
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1099
  	__pmu_adb_autopoll(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100

11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1101
  	req.nbytes = 4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
1103
  	req.done = NULL;
  	req.data[0] = PMU_ADB_CMD;
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1104
1105
  	req.data[1] = ADB_BUSRESET;
  	req.data[2] = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  	req.data[3] = 0;
  	req.data[4] = 0;
  	req.reply_len = 0;
  	req.reply_expected = 1;
  	if (pmu_queue_request(&req) != 0) {
  		printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed
  ");
  		return -EIO;
  	}
  	pmu_wait_complete(&req);
  
  	if (save_autopoll != 0)
11a50873e   Benjamin Herrenschmidt   powerpc/pmac: Fix...
1118
  		__pmu_adb_autopoll(save_autopoll);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
1120
1121
1122
1123
1124
  
  	return 0;
  }
  #endif /* CONFIG_ADB */
  
  /* Construct and send a pmu request */
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1125
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1126
1127
1128
1129
1130
  pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
  	    int nbytes, ...)
  {
  	va_list list;
  	int i;
c57902d52   Finn Thain   macintosh/via-pmu...
1131
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  		return -ENXIO;
  
  	if (nbytes < 0 || nbytes > 32) {
  		printk(KERN_ERR "pmu_request: bad nbytes (%d)
  ", nbytes);
  		req->complete = 1;
  		return -EINVAL;
  	}
  	req->nbytes = nbytes;
  	req->done = done;
  	va_start(list, nbytes);
  	for (i = 0; i < nbytes; ++i)
  		req->data[i] = va_arg(list, int);
  	va_end(list);
  	req->reply_len = 0;
  	req->reply_expected = 0;
  	return pmu_queue_request(req);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1150
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1151
1152
1153
1154
  pmu_queue_request(struct adb_request *req)
  {
  	unsigned long flags;
  	int nsend;
c57902d52   Finn Thain   macintosh/via-pmu...
1155
  	if (pmu_state == uninitialized) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
  		req->complete = 1;
  		return -ENXIO;
  	}
  	if (req->nbytes <= 0) {
  		req->complete = 1;
  		return 0;
  	}
  	nsend = pmu_data_len[req->data[0]][0];
  	if (nsend >= 0 && req->nbytes != nsend + 1) {
  		req->complete = 1;
  		return -EINVAL;
  	}
  
  	req->next = NULL;
  	req->sent = 0;
  	req->complete = 0;
  
  	spin_lock_irqsave(&pmu_lock, flags);
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
1174
  	if (current_req) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  		last_req->next = req;
  		last_req = req;
  	} else {
  		current_req = req;
  		last_req = req;
  		if (pmu_state == idle)
  			pmu_start();
  	}
  	spin_unlock_irqrestore(&pmu_lock, flags);
  
  	return 0;
  }
  
  static inline void
  wait_for_ack(void)
  {
  	/* Sightly increased the delay, I had one occurrence of the message
  	 * reported
  	 */
  	int timeout = 4000;
c70c35da5   Finn Thain   macintosh/via-pmu...
1195
  	while ((in_8(&via2[B]) & TACK) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
  		if (--timeout < 0) {
  			printk(KERN_ERR "PMU not responding (!ack)
  ");
  			return;
  		}
  		udelay(10);
  	}
  }
  
  /* New PMU seems to be very sensitive to those timings, so we make sure
   * PCI is flushed immediately */
  static inline void
  send_byte(int x)
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
1210
1211
1212
1213
  	out_8(&via1[ACR], in_8(&via1[ACR]) | SR_OUT | SR_EXT);
  	out_8(&via1[SR], x);
  	out_8(&via2[B], in_8(&via2[B]) & ~TREQ);	/* assert TREQ */
  	(void)in_8(&via2[B]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1214
1215
1216
1217
1218
  }
  
  static inline void
  recv_byte(void)
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
1219
1220
1221
1222
  	out_8(&via1[ACR], (in_8(&via1[ACR]) & ~SR_OUT) | SR_EXT);
  	in_8(&via1[SR]);		/* resets SR */
  	out_8(&via2[B], in_8(&via2[B]) & ~TREQ);
  	(void)in_8(&via2[B]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  }
  
  static inline void
  pmu_done(struct adb_request *req)
  {
  	void (*done)(struct adb_request *) = req->done;
  	mb();
  	req->complete = 1;
      	/* Here, we assume that if the request has a done member, the
      	 * struct request will survive to setting req->complete to 1
      	 */
  	if (done)
  		(*done)(req);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1237
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1238
1239
1240
1241
1242
1243
1244
  pmu_start(void)
  {
  	struct adb_request *req;
  
  	/* assert pmu_state == idle */
  	/* get the packet to send */
  	req = current_req;
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
1245
  	if (!req || pmu_state != idle
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  	    || (/*req->reply_expected && */req_awaiting_reply))
  		return;
  
  	pmu_state = sending;
  	data_index = 1;
  	data_len = pmu_data_len[req->data[0]][0];
  
  	/* Sounds safer to make sure ACK is high before writing. This helped
  	 * kill a problem with ADB and some iBooks
  	 */
  	wait_for_ack();
  	/* set the shift register to shift out and send a byte */
  	send_byte(req->data[0]);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1260
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1261
1262
  pmu_poll(void)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
1263
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
1266
  		return;
  	if (disable_poll)
  		return;
7d12e780e   David Howells   IRQ: Maintain reg...
1267
  	via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1268
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1269
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
  pmu_poll_adb(void)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
1272
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
1275
1276
1277
1278
  		return;
  	if (disable_poll)
  		return;
  	/* Kicks ADB read when PMU is suspended */
  	adb_int_pending = 1;
  	do {
7d12e780e   David Howells   IRQ: Maintain reg...
1279
  		via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1280
1281
1282
  	} while (pmu_suspended && (adb_int_pending || pmu_state != idle
  		|| req_awaiting_reply));
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1283
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1284
1285
  pmu_wait_complete(struct adb_request *req)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
1286
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1287
1288
  		return;
  	while((pmu_state != idle && pmu_state != locked) || !req->complete)
7d12e780e   David Howells   IRQ: Maintain reg...
1289
  		via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
1291
1292
1293
1294
1295
1296
  }
  
  /* This function loops until the PMU is idle and prevents it from
   * anwsering to ADB interrupts. pmu_request can still be called.
   * This is done to avoid spurrious shutdowns when we know we'll have
   * interrupts switched off for a long time
   */
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1297
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1298
1299
1300
  pmu_suspend(void)
  {
  	unsigned long flags;
1b0e9d44e   Johannes Berg   [POWERPC] PMU: Re...
1301

c57902d52   Finn Thain   macintosh/via-pmu...
1302
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
  		return;
  	
  	spin_lock_irqsave(&pmu_lock, flags);
  	pmu_suspended++;
  	if (pmu_suspended > 1) {
  		spin_unlock_irqrestore(&pmu_lock, flags);
  		return;
  	}
  
  	do {
  		spin_unlock_irqrestore(&pmu_lock, flags);
  		if (req_awaiting_reply)
  			adb_int_pending = 1;
7d12e780e   David Howells   IRQ: Maintain reg...
1316
  		via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
1318
  		spin_lock_irqsave(&pmu_lock, flags);
  		if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1319
1320
  			if (gpio_irq >= 0)
  				disable_irq_nosync(gpio_irq);
c70c35da5   Finn Thain   macintosh/via-pmu...
1321
  			out_8(&via1[IER], CB1_INT | IER_CLR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1322
  			spin_unlock_irqrestore(&pmu_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1323
1324
1325
1326
  			break;
  		}
  	} while (1);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1327
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1328
1329
1330
  pmu_resume(void)
  {
  	unsigned long flags;
c57902d52   Finn Thain   macintosh/via-pmu...
1331
  	if (pmu_state == uninitialized || pmu_suspended < 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1332
1333
1334
1335
1336
1337
1338
1339
1340
  		return;
  
  	spin_lock_irqsave(&pmu_lock, flags);
  	pmu_suspended--;
  	if (pmu_suspended > 0) {
  		spin_unlock_irqrestore(&pmu_lock, flags);
  		return;
  	}
  	adb_int_pending = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1341
1342
  	if (gpio_irq >= 0)
  		enable_irq(gpio_irq);
c70c35da5   Finn Thain   macintosh/via-pmu...
1343
  	out_8(&via1[IER], CB1_INT | IER_SET);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
1345
  	spin_unlock_irqrestore(&pmu_lock, flags);
  	pmu_poll();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
1347
1348
  }
  
  /* Interrupt data could be the result data from an ADB cmd */
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1349
  static void
7d12e780e   David Howells   IRQ: Maintain reg...
1350
  pmu_handle_data(unsigned char *data, int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1351
  {
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1352
1353
  	unsigned char ints;
  	int idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
  	int i = 0;
  
  	asleep = 0;
  	if (drop_interrupts || len < 1) {
  		adb_int_pending = 0;
  		pmu_irq_stats[8]++;
  		return;
  	}
  
  	/* Get PMU interrupt mask */
  	ints = data[0];
  
  	/* Record zero interrupts for stats */
  	if (ints == 0)
  		pmu_irq_stats[9]++;
  
  	/* Hack to deal with ADB autopoll flag */
  	if (ints & PMU_INT_ADB)
  		ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
  
  next:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
  	if (ints == 0) {
  		if (i > pmu_irq_stats[10])
  			pmu_irq_stats[10] = i;
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1380
  	i++;
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1381
1382
1383
1384
1385
  
  	idx = ffs(ints) - 1;
  	ints &= ~BIT(idx);
  
  	pmu_irq_stats[idx]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1386
1387
1388
1389
1390
  
  	/* Note: for some reason, we get an interrupt with len=1,
  	 * data[0]==0 after each normal ADB interrupt, at least
  	 * on the Pismo. Still investigating...  --BenH
  	 */
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1391
1392
  	switch (BIT(idx)) {
  	case PMU_INT_ADB:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1393
1394
  		if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
  			struct adb_request *req = req_awaiting_reply;
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
1395
  			if (!req) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
  				printk(KERN_ERR "PMU: extra ADB reply
  ");
  				return;
  			}
  			req_awaiting_reply = NULL;
  			if (len <= 2)
  				req->reply_len = 0;
  			else {
  				memcpy(req->reply, data + 1, len - 1);
  				req->reply_len = len - 1;
  			}
  			pmu_done(req);
  		} else {
c16a85a5a   Finn Thain   macintosh/via-pmu...
1409
  #ifdef CONFIG_XMON
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1410
1411
1412
1413
1414
1415
1416
  			if (len == 4 && data[1] == 0x2c) {
  				extern int xmon_wants_key, xmon_adb_keycode;
  				if (xmon_wants_key) {
  					xmon_adb_keycode = data[2];
  					return;
  				}
  			}
c16a85a5a   Finn Thain   macintosh/via-pmu...
1417
  #endif /* CONFIG_XMON */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
  #ifdef CONFIG_ADB
  			/*
  			 * XXX On the [23]400 the PMU gives us an up
  			 * event for keycodes 0x74 or 0x75 when the PC
  			 * card eject buttons are released, so we
  			 * ignore those events.
  			 */
  			if (!(pmu_kind == PMU_OHARE_BASED && len == 4
  			      && data[1] == 0x2c && data[3] == 0xff
  			      && (data[2] & ~1) == 0xf4))
7d12e780e   David Howells   IRQ: Maintain reg...
1428
  				adb_input(data+1, len-1, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1429
1430
  #endif /* CONFIG_ADB */		
  		}
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1431
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1432
  	/* Sound/brightness button pressed */
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1433
  	case PMU_INT_SNDBRT:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1434
1435
  #ifdef CONFIG_PMAC_BACKLIGHT
  		if (len == 3)
4b755999d   Michael Hanselmann   [PATCH] powermac:...
1436
1437
  			pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
  #endif
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1438
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1439
  	/* Tick interrupt */
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1440
1441
  	case PMU_INT_TICK:
  		/* Environment or tick interrupt, query batteries */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
1443
1444
1445
1446
1447
  		if (pmu_battery_count) {
  			if ((--query_batt_timer) == 0) {
  				query_battery_state();
  				query_batt_timer = BATTERY_POLLING_COUNT;
  			}
  		}
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1448
1449
1450
  		break;
  
  	case PMU_INT_ENVIRONMENT:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
1452
1453
  		if (pmu_battery_count)
  			query_battery_state();
  		pmu_pass_intr(data, len);
9e8e30a0c   Johannes Berg   Input: via-pmu - ...
1454
1455
1456
1457
1458
1459
  		/* len == 6 is probably a bad check. But how do I
  		 * know what PMU versions send what events here? */
  		if (len == 6) {
  			via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
  			via_pmu_event(PMU_EVT_LID, data[1]&1);
  		}
b5c7cccaa   Finn Thain   macintosh/via-pmu...
1460
1461
1462
  		break;
  
  	default:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463
  	       pmu_pass_intr(data, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1464
1465
1466
  	}
  	goto next;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1467
  static struct adb_request*
7d12e780e   David Howells   IRQ: Maintain reg...
1468
  pmu_sr_intr(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1469
1470
1471
  {
  	struct adb_request *req;
  	int bite = 0;
c70c35da5   Finn Thain   macintosh/via-pmu...
1472
1473
1474
  	if (in_8(&via2[B]) & TREQ) {
  		printk(KERN_ERR "PMU: spurious SR intr (%x)
  ", in_8(&via2[B]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
1476
1477
  		return NULL;
  	}
  	/* The ack may not yet be low when we get the interrupt */
c70c35da5   Finn Thain   macintosh/via-pmu...
1478
  	while ((in_8(&via2[B]) & TACK) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1479
1480
1481
1482
  			;
  
  	/* if reading grab the byte, and reset the interrupt */
  	if (pmu_state == reading || pmu_state == reading_intr)
c70c35da5   Finn Thain   macintosh/via-pmu...
1483
  		bite = in_8(&via1[SR]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484
1485
  
  	/* reset TREQ and wait for TACK to go high */
c70c35da5   Finn Thain   macintosh/via-pmu...
1486
  	out_8(&via2[B], in_8(&via2[B]) | TREQ);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  	wait_for_ack();
  
  	switch (pmu_state) {
  	case sending:
  		req = current_req;
  		if (data_len < 0) {
  			data_len = req->nbytes - 1;
  			send_byte(data_len);
  			break;
  		}
  		if (data_index <= data_len) {
  			send_byte(req->data[data_index++]);
  			break;
  		}
  		req->sent = 1;
  		data_len = pmu_data_len[req->data[0]][1];
  		if (data_len == 0) {
  			pmu_state = idle;
  			current_req = req->next;
  			if (req->reply_expected)
  				req_awaiting_reply = req;
  			else
  				return req;
  		} else {
  			pmu_state = reading;
  			data_index = 0;
  			reply_ptr = req->reply + req->reply_len;
  			recv_byte();
  		}
  		break;
  
  	case intack:
  		data_index = 0;
  		data_len = -1;
  		pmu_state = reading_intr;
  		reply_ptr = interrupt_data[int_data_last];
  		recv_byte();
  		if (gpio_irq >= 0 && !gpio_irq_enabled) {
  			enable_irq(gpio_irq);
  			gpio_irq_enabled = 1;
  		}
  		break;
  
  	case reading:
  	case reading_intr:
  		if (data_len == -1) {
  			data_len = bite;
  			if (bite > 32)
  				printk(KERN_ERR "PMU: bad reply len %d
  ", bite);
  		} else if (data_index < 32) {
  			reply_ptr[data_index++] = bite;
  		}
  		if (data_index < data_len) {
  			recv_byte();
  			break;
  		}
  
  		if (pmu_state == reading_intr) {
  			pmu_state = idle;
  			int_data_state[int_data_last] = int_data_ready;
  			interrupt_data_len[int_data_last] = data_len;
  		} else {
  			req = current_req;
  			/* 
  			 * For PMU sleep and freq change requests, we lock the
c03983ac9   Jean Delvare   Spelling fix: exp...
1553
  			 * PMU until it's explicitly unlocked. This avoids any
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
  			 * spurrious event polling getting in
  			 */
  			current_req = req->next;
  			req->reply_len += data_index;
  			if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
  				pmu_state = locked;
  			else
  				pmu_state = idle;
  			return req;
  		}
  		break;
  
  	default:
  		printk(KERN_ERR "via_pmu_interrupt: unknown state %d?
  ",
  		       pmu_state);
  	}
  	return NULL;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1573
  static irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
1574
  via_pmu_interrupt(int irq, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
  {
  	unsigned long flags;
  	int intr;
  	int nloop = 0;
  	int int_data = -1;
  	struct adb_request *req = NULL;
  	int handled = 0;
  
  	/* This is a bit brutal, we can probably do better */
  	spin_lock_irqsave(&pmu_lock, flags);
  	++disable_poll;
  	
  	for (;;) {
c16a85a5a   Finn Thain   macintosh/via-pmu...
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
  		/* On 68k Macs, VIA interrupts are dispatched individually.
  		 * Unless we are polling, the relevant IRQ flag has already
  		 * been cleared.
  		 */
  		intr = 0;
  		if (IS_ENABLED(CONFIG_PPC_PMAC) || !irq) {
  			intr = in_8(&via1[IFR]) & (SR_INT | CB1_INT);
  			out_8(&via1[IFR], intr);
  		}
  #ifndef CONFIG_PPC_PMAC
  		switch (irq) {
  		case IRQ_MAC_ADB_CL:
  			intr = CB1_INT;
  			break;
  		case IRQ_MAC_ADB_SR:
  			intr = SR_INT;
  			break;
  		}
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1607
1608
1609
1610
1611
1612
1613
  		if (intr == 0)
  			break;
  		handled = 1;
  		if (++nloop > 1000) {
  			printk(KERN_DEBUG "PMU: stuck in intr loop, "
  			       "intr=%x, ier=%x pmu_state=%d
  ",
c70c35da5   Finn Thain   macintosh/via-pmu...
1614
  			       intr, in_8(&via1[IER]), pmu_state);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1615
1616
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1617
1618
  		if (intr & CB1_INT) {
  			adb_int_pending = 1;
6edc22fc9   Finn Thain   macintosh/via-pmu...
1619
  			pmu_irq_stats[11]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1620
1621
  		}
  		if (intr & SR_INT) {
7d12e780e   David Howells   IRQ: Maintain reg...
1622
  			req = pmu_sr_intr();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1623
1624
1625
  			if (req)
  				break;
  		}
c16a85a5a   Finn Thain   macintosh/via-pmu...
1626
1627
1628
  #ifndef CONFIG_PPC_PMAC
  		break;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
  	}
  
  recheck:
  	if (pmu_state == idle) {
  		if (adb_int_pending) {
  			if (int_data_state[0] == int_data_empty)
  				int_data_last = 0;
  			else if (int_data_state[1] == int_data_empty)
  				int_data_last = 1;
  			else
  				goto no_free_slot;
  			pmu_state = intack;
  			int_data_state[int_data_last] = int_data_fill;
  			/* Sounds safer to make sure ACK is high before writing.
  			 * This helped kill a problem with ADB and some iBooks
  			 */
  			wait_for_ack();
  			send_byte(PMU_INT_ACK);
  			adb_int_pending = 0;
  		} else if (current_req)
  			pmu_start();
  	}
  no_free_slot:			
  	/* Mark the oldest buffer for flushing */
  	if (int_data_state[!int_data_last] == int_data_ready) {
  		int_data_state[!int_data_last] = int_data_flush;
  		int_data = !int_data_last;
  	} else if (int_data_state[int_data_last] == int_data_ready) {
  		int_data_state[int_data_last] = int_data_flush;
  		int_data = int_data_last;
  	}
  	--disable_poll;
  	spin_unlock_irqrestore(&pmu_lock, flags);
  
  	/* Deal with completed PMU requests outside of the lock */
  	if (req) {
  		pmu_done(req);
  		req = NULL;
  	}
  		
  	/* Deal with interrupt datas outside of the lock */
  	if (int_data >= 0) {
7d12e780e   David Howells   IRQ: Maintain reg...
1671
  		pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
1673
1674
1675
1676
1677
1678
1679
1680
  		spin_lock_irqsave(&pmu_lock, flags);
  		++disable_poll;
  		int_data_state[int_data] = int_data_empty;
  		int_data = -1;
  		goto recheck;
  	}
  
  	return IRQ_RETVAL(handled);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1681
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
  pmu_unlock(void)
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&pmu_lock, flags);
  	if (pmu_state == locked)
  		pmu_state = idle;
  	adb_int_pending = 1;
  	spin_unlock_irqrestore(&pmu_lock, flags);
  }
c16a85a5a   Finn Thain   macintosh/via-pmu...
1692
  static __maybe_unused irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
1693
  gpio1_interrupt(int irq, void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1694
1695
1696
1697
1698
1699
1700
1701
1702
  {
  	unsigned long flags;
  
  	if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
  		spin_lock_irqsave(&pmu_lock, flags);
  		if (gpio_irq_enabled > 0) {
  			disable_irq_nosync(gpio_irq);
  			gpio_irq_enabled = 0;
  		}
6edc22fc9   Finn Thain   macintosh/via-pmu...
1703
  		pmu_irq_stats[12]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1704
1705
  		adb_int_pending = 1;
  		spin_unlock_irqrestore(&pmu_lock, flags);
7d12e780e   David Howells   IRQ: Maintain reg...
1706
  		via_pmu_interrupt(0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1707
1708
1709
1710
  		return IRQ_HANDLED;
  	}
  	return IRQ_NONE;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1711
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1712
1713
1714
  pmu_enable_irled(int on)
  {
  	struct adb_request req;
c57902d52   Finn Thain   macintosh/via-pmu...
1715
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1716
1717
1718
1719
1720
1721
1722
1723
  		return ;
  	if (pmu_kind == PMU_KEYLARGO_BASED)
  		return ;
  
  	pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
  	    (on ? PMU_POW_ON : PMU_POW_OFF));
  	pmu_wait_complete(&req);
  }
0792a2c8e   Finn Thain   macintosh: Use co...
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
  /* Offset between Unix time (1970-based) and Mac time (1904-based) */
  #define RTC_OFFSET	2082844800
  
  time64_t pmu_get_time(void)
  {
  	struct adb_request req;
  	u32 now;
  
  	if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  		return 0;
  	pmu_wait_complete(&req);
  	if (req.reply_len != 4)
  		pr_err("%s: got %d byte reply
  ", __func__, req.reply_len);
  	now = (req.reply[0] << 24) + (req.reply[1] << 16) +
  	      (req.reply[2] << 8) + req.reply[3];
  	return (time64_t)now - RTC_OFFSET;
  }
  
  int pmu_set_rtc_time(struct rtc_time *tm)
  {
  	u32 now;
  	struct adb_request req;
  
  	now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
  	if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  	                now >> 24, now >> 16, now >> 8, now) < 0)
  		return -ENXIO;
  	pmu_wait_complete(&req);
  	if (req.reply_len != 0)
  		pr_err("%s: got %d byte reply
  ", __func__, req.reply_len);
  	return 0;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1758
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1759
1760
1761
  pmu_restart(void)
  {
  	struct adb_request req;
c57902d52   Finn Thain   macintosh/via-pmu...
1762
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
  		return;
  
  	local_irq_disable();
  
  	drop_interrupts = 1;
  	
  	if (pmu_kind != PMU_KEYLARGO_BASED) {
  		pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
  						PMU_INT_TICK );
  		while(!req.complete)
  			pmu_poll();
  	}
  
  	pmu_request(&req, NULL, 1, PMU_RESET);
  	pmu_wait_complete(&req);
  	for (;;)
  		;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1781
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1782
1783
1784
  pmu_shutdown(void)
  {
  	struct adb_request req;
c57902d52   Finn Thain   macintosh/via-pmu...
1785
  	if (pmu_state == uninitialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
  		return;
  
  	local_irq_disable();
  
  	drop_interrupts = 1;
  
  	if (pmu_kind != PMU_KEYLARGO_BASED) {
  		pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
  						PMU_INT_TICK );
  		pmu_wait_complete(&req);
  	} else {
  		/* Disable server mode on shutdown or we'll just
  		 * wake up again
  		 */
  		pmu_set_server_mode(0);
  	}
  
  	pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
  		    'M', 'A', 'T', 'T');
  	pmu_wait_complete(&req);
  	for (;;)
  		;
  }
  
  int
  pmu_present(void)
  {
c57902d52   Finn Thain   macintosh/via-pmu...
1813
  	return pmu_state != uninitialized;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1814
  }
f91266edb   Johannes Berg   [POWERPC] powerma...
1815
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1816
1817
1818
1819
  /*
   * Put the powerbook to sleep.
   */
   
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1820
  static u32 save_via[8];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821

aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1822
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1823
1824
  save_via_state(void)
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
1825
1826
1827
1828
1829
1830
1831
1832
  	save_via[0] = in_8(&via1[ANH]);
  	save_via[1] = in_8(&via1[DIRA]);
  	save_via[2] = in_8(&via1[B]);
  	save_via[3] = in_8(&via1[DIRB]);
  	save_via[4] = in_8(&via1[PCR]);
  	save_via[5] = in_8(&via1[ACR]);
  	save_via[6] = in_8(&via1[T1CL]);
  	save_via[7] = in_8(&via1[T1CH]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1833
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1834
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1835
1836
  restore_via_state(void)
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
  	out_8(&via1[ANH],  save_via[0]);
  	out_8(&via1[DIRA], save_via[1]);
  	out_8(&via1[B],    save_via[2]);
  	out_8(&via1[DIRB], save_via[3]);
  	out_8(&via1[PCR],  save_via[4]);
  	out_8(&via1[ACR],  save_via[5]);
  	out_8(&via1[T1CL], save_via[6]);
  	out_8(&via1[T1CH], save_via[7]);
  	out_8(&via1[IER], IER_CLR | 0x7f);	/* disable all intrs */
  	out_8(&via1[IFR], 0x7f);			/* clear IFR */
  	out_8(&via1[IER], IER_SET | SR_INT | CB1_INT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1848
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1849
1850
1851
1852
  #define	GRACKLE_PM	(1<<7)
  #define GRACKLE_DOZE	(1<<5)
  #define	GRACKLE_NAP	(1<<4)
  #define	GRACKLE_SLEEP	(1<<3)
3bea63136   Olaf Hering   [PATCH] powerpc: ...
1853
  static int powerbook_sleep_grackle(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1854
1855
1856
1857
  {
  	unsigned long save_l2cr;
  	unsigned short pmcr1;
  	struct adb_request req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1858
  	struct pci_dev *grackle;
67c8d3269   Sinan Kaya   powerpc/via-pmu: ...
1859
  	grackle = pci_get_domain_bus_and_slot(0, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1860
1861
  	if (!grackle)
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
  	/* Turn off various things. Darwin does some retry tests here... */
  	pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
  	pmu_wait_complete(&req);
  	pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  		PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
  	pmu_wait_complete(&req);
  
  	/* For 750, save backside cache setting and disable it */
  	save_l2cr = _get_L2CR();	/* (returns -1 if not available) */
  
  	if (!__fake_sleep) {
  		/* Ask the PMU to put us to sleep */
  		pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
  		pmu_wait_complete(&req);
  	}
  
  	/* The VIA is supposed not to be restored correctly*/
  	save_via_state();
  	/* We shut down some HW */
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
  
  	pci_read_config_word(grackle, 0x70, &pmcr1);
  	/* Apparently, MacOS uses NAP mode for Grackle ??? */
  	pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP); 
  	pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
  	pci_write_config_word(grackle, 0x70, pmcr1);
  
  	/* Call low-level ASM sleep handler */
  	if (__fake_sleep)
  		mdelay(5000);
  	else
  		low_sleep_handler();
  
  	/* We're awake again, stop grackle PM */
  	pci_read_config_word(grackle, 0x70, &pmcr1);
  	pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP); 
  	pci_write_config_word(grackle, 0x70, pmcr1);
c78f83054   Alan Cox   [POWERPC] via-pmu...
1899
  	pci_dev_put(grackle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1900
1901
1902
1903
1904
1905
1906
1907
1908
  	/* Make sure the PMU is idle */
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
  	restore_via_state();
  	
  	/* Restore L2 cache */
  	if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
   		_set_L2CR(save_l2cr);
  	
  	/* Restore userland MMU context */
d2adba3fd   Aneesh Kumar K.V   powerpc/mm: Abstr...
1909
  	switch_mmu_context(NULL, current->active_mm, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
  
  	/* Power things up */
  	pmu_unlock();
  	pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
  	pmu_wait_complete(&req);
  	pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
  			PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
  	pmu_wait_complete(&req);
  	pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  			PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
  	pmu_wait_complete(&req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1921
1922
  	return 0;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
1923
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1924
1925
1926
1927
1928
  powerbook_sleep_Core99(void)
  {
  	unsigned long save_l2cr;
  	unsigned long save_l3cr;
  	struct adb_request req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1929
1930
1931
1932
1933
1934
1935
1936
1937
  	
  	if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
  		printk(KERN_ERR "Sleep mode not supported on this machine
  ");
  		return -ENOSYS;
  	}
  
  	if (num_online_cpus() > 1 || cpu_is_offline(0))
  		return -EAGAIN;
b16eeb472   Benjamin Herrenschmidt   [PATCH] ppc32: Fi...
1938
1939
1940
  	/* Stop environment and ADB interrupts */
  	pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
  	pmu_wait_complete(&req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
  
  	/* Tell PMU what events will wake us up */
  	pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
  		0xff, 0xff);
  	pmu_wait_complete(&req);
  	pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
  		0, PMU_PWR_WAKEUP_KEY |
  		(option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
  	pmu_wait_complete(&req);
  
  	/* Save the state of the L2 and L3 caches */
  	save_l3cr = _get_L3CR();	/* (returns -1 if not available) */
  	save_l2cr = _get_L2CR();	/* (returns -1 if not available) */
  
  	if (!__fake_sleep) {
  		/* Ask the PMU to put us to sleep */
  		pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
  		pmu_wait_complete(&req);
  	}
  
  	/* The VIA is supposed not to be restored correctly*/
  	save_via_state();
  
  	/* Shut down various ASICs. There's a chance that we can no longer
  	 * talk to the PMU after this, so I moved it to _after_ sending the
  	 * sleep command to it. Still need to be checked.
  	 */
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
  
  	/* Call low-level ASM sleep handler */
  	if (__fake_sleep)
  		mdelay(5000);
  	else
  		low_sleep_handler();
  
  	/* Restore Apple core ASICs state */
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
  
  	/* Restore VIA */
  	restore_via_state();
0086b5ec7   Benjamin Herrenschmidt   [PATCH] ppc32: Fi...
1981
1982
  	/* tweak LPJ before cpufreq is there */
  	loops_per_jiffy *= 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
  	/* Restore video */
  	pmac_call_early_video_resume();
  
  	/* Restore L2 cache */
  	if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
   		_set_L2CR(save_l2cr);
  	/* Restore L3 cache */
  	if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
   		_set_L3CR(save_l3cr);
  	
  	/* Restore userland MMU context */
d2adba3fd   Aneesh Kumar K.V   powerpc/mm: Abstr...
1994
  	switch_mmu_context(NULL, current->active_mm, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1995
1996
1997
1998
1999
2000
2001
  
  	/* Tell PMU we are ready */
  	pmu_unlock();
  	pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
  	pmu_wait_complete(&req);
  	pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
  	pmu_wait_complete(&req);
0086b5ec7   Benjamin Herrenschmidt   [PATCH] ppc32: Fi...
2002
2003
  	/* Restore LPJ, cpufreq will adjust the cpu frequency */
  	loops_per_jiffy /= 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2004
2005
2006
2007
2008
  	return 0;
  }
  
  #define PB3400_MEM_CTRL		0xf8000000
  #define PB3400_MEM_CTRL_SLEEP	0x70
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
  static void __iomem *pb3400_mem_ctrl;
  
  static void powerbook_sleep_init_3400(void)
  {
  	/* map in the memory controller registers */
  	pb3400_mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
  	if (pb3400_mem_ctrl == NULL)
  		printk(KERN_WARNING "ioremap failed: sleep won't be possible");
  }
  
  static int powerbook_sleep_3400(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2020
  {
f91266edb   Johannes Berg   [POWERPC] powerma...
2021
  	int i, x;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2022
  	unsigned int hid0;
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2023
  	unsigned long msr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2024
  	struct adb_request sleep_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2025
  	unsigned int __iomem *mem_ctrl_sleep;
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2026
  	if (pb3400_mem_ctrl == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2027
  		return -ENOMEM;
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2028
  	mem_ctrl_sleep = pb3400_mem_ctrl + PB3400_MEM_CTRL_SLEEP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2029

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
  	/* Set the memory controller to keep the memory refreshed
  	   while we're asleep */
  	for (i = 0x403f; i >= 0x4000; --i) {
  		out_be32(mem_ctrl_sleep, i);
  		do {
  			x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
  		} while (x == 0);
  		if (x >= 0x100)
  			break;
  	}
  
  	/* Ask the PMU to put us to sleep */
  	pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2043
2044
  	pmu_wait_complete(&sleep_req);
  	pmu_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2045

887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2046
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2047

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2048
2049
2050
  	asleep = 1;
  
  	/* Put the CPU into sleep mode */
21fe3301f   Benjamin Herrenschmidt   [PATCH] ppc: fix ...
2051
  	hid0 = mfspr(SPRN_HID0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2052
  	hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
21fe3301f   Benjamin Herrenschmidt   [PATCH] ppc: fix ...
2053
  	mtspr(SPRN_HID0, hid0);
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2054
2055
2056
2057
2058
2059
2060
2061
  	local_irq_enable();
  	msr = mfmsr() | MSR_POW;
  	while (asleep) {
  		mb();
  		mtmsr(msr);
  		isync();
  	}
  	local_irq_disable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2062
2063
2064
  
  	/* OK, we're awake again, start restoring things */
  	out_be32(mem_ctrl_sleep, 0x3f);
887ef35ae   Paul Mackerras   [POWERPC] Fix sle...
2065
  	pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2066

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2067
2068
  	return 0;
  }
f91266edb   Johannes Berg   [POWERPC] powerma...
2069
  #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2070

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
  /*
   * Support for /dev/pmu device
   */
  #define RB_SIZE		0x10
  struct pmu_private {
  	struct list_head list;
  	int	rb_get;
  	int	rb_put;
  	struct rb_entry {
  		unsigned short len;
  		unsigned char data[16];
  	}	rb_buf[RB_SIZE];
  	wait_queue_head_t wait;
  	spinlock_t lock;
  #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
  	int	backlight_locker;
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2087
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2088
2089
2090
  };
  
  static LIST_HEAD(all_pmu_pvt);
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2091
  static DEFINE_SPINLOCK(all_pvt_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2092

aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2093
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
  pmu_pass_intr(unsigned char *data, int len)
  {
  	struct pmu_private *pp;
  	struct list_head *list;
  	int i;
  	unsigned long flags;
  
  	if (len > sizeof(pp->rb_buf[0].data))
  		len = sizeof(pp->rb_buf[0].data);
  	spin_lock_irqsave(&all_pvt_lock, flags);
  	for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
  		pp = list_entry(list, struct pmu_private, list);
  		spin_lock(&pp->lock);
  		i = pp->rb_put + 1;
  		if (i >= RB_SIZE)
  			i = 0;
  		if (i != pp->rb_get) {
  			struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
  			rp->len = len;
  			memcpy(rp->data, data, len);
  			pp->rb_put = i;
  			wake_up_interruptible(&pp->wait);
  		}
  		spin_unlock(&pp->lock);
  	}
  	spin_unlock_irqrestore(&all_pvt_lock, flags);
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2121
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2122
2123
2124
2125
2126
2127
  pmu_open(struct inode *inode, struct file *file)
  {
  	struct pmu_private *pp;
  	unsigned long flags;
  
  	pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
2128
  	if (!pp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2129
2130
2131
2132
  		return -ENOMEM;
  	pp->rb_get = pp->rb_put = 0;
  	spin_lock_init(&pp->lock);
  	init_waitqueue_head(&pp->wait);
d851b6e04   Arnd Bergmann   mac: autoconvert ...
2133
  	mutex_lock(&pmu_info_proc_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2134
2135
2136
  	spin_lock_irqsave(&all_pvt_lock, flags);
  #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
  	pp->backlight_locker = 0;
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2137
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2138
2139
2140
  	list_add(&pp->list, &all_pmu_pvt);
  	spin_unlock_irqrestore(&all_pvt_lock, flags);
  	file->private_data = pp;
d851b6e04   Arnd Bergmann   mac: autoconvert ...
2141
  	mutex_unlock(&pmu_info_proc_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2142
2143
  	return 0;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2144
  static ssize_t 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2145
2146
2147
2148
2149
2150
2151
  pmu_read(struct file *file, char __user *buf,
  			size_t count, loff_t *ppos)
  {
  	struct pmu_private *pp = file->private_data;
  	DECLARE_WAITQUEUE(wait, current);
  	unsigned long flags;
  	int ret = 0;
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
2152
  	if (count < 1 || !pp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2153
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2154
2155
2156
  
  	spin_lock_irqsave(&pp->lock, flags);
  	add_wait_queue(&pp->wait, &wait);
111fbc68f   Fabian Frederick   powerpc/pmac: rep...
2157
  	set_current_state(TASK_INTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
  
  	for (;;) {
  		ret = -EAGAIN;
  		if (pp->rb_get != pp->rb_put) {
  			int i = pp->rb_get;
  			struct rb_entry *rp = &pp->rb_buf[i];
  			ret = rp->len;
  			spin_unlock_irqrestore(&pp->lock, flags);
  			if (ret > count)
  				ret = count;
  			if (ret > 0 && copy_to_user(buf, rp->data, ret))
  				ret = -EFAULT;
  			if (++i >= RB_SIZE)
  				i = 0;
  			spin_lock_irqsave(&pp->lock, flags);
  			pp->rb_get = i;
  		}
  		if (ret >= 0)
  			break;
  		if (file->f_flags & O_NONBLOCK)
  			break;
  		ret = -ERESTARTSYS;
  		if (signal_pending(current))
  			break;
  		spin_unlock_irqrestore(&pp->lock, flags);
  		schedule();
  		spin_lock_irqsave(&pp->lock, flags);
  	}
111fbc68f   Fabian Frederick   powerpc/pmac: rep...
2186
  	__set_current_state(TASK_RUNNING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2187
2188
2189
2190
2191
  	remove_wait_queue(&pp->wait, &wait);
  	spin_unlock_irqrestore(&pp->lock, flags);
  	
  	return ret;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2192
  static ssize_t
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2193
2194
2195
2196
2197
  pmu_write(struct file *file, const char __user *buf,
  			 size_t count, loff_t *ppos)
  {
  	return 0;
  }
afc9a42b7   Al Viro   the rest of drive...
2198
  static __poll_t
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2199
2200
2201
  pmu_fpoll(struct file *filp, poll_table *wait)
  {
  	struct pmu_private *pp = filp->private_data;
afc9a42b7   Al Viro   the rest of drive...
2202
  	__poll_t mask = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2203
2204
  	unsigned long flags;
  	
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
2205
  	if (!pp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2206
2207
2208
2209
  		return 0;
  	poll_wait(filp, &pp->wait, wait);
  	spin_lock_irqsave(&pp->lock, flags);
  	if (pp->rb_get != pp->rb_put)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
2210
  		mask |= EPOLLIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2211
2212
2213
  	spin_unlock_irqrestore(&pp->lock, flags);
  	return mask;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2214
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2215
2216
2217
2218
  pmu_release(struct inode *inode, struct file *file)
  {
  	struct pmu_private *pp = file->private_data;
  	unsigned long flags;
d8731527a   Mathieu Malaterre   powerpc/sparse: F...
2219
  	if (pp) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2220
2221
2222
2223
  		file->private_data = NULL;
  		spin_lock_irqsave(&all_pvt_lock, flags);
  		list_del(&pp->list);
  		spin_unlock_irqrestore(&all_pvt_lock, flags);
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2224

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2225
  #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2226
2227
2228
  		if (pp->backlight_locker)
  			pmac_backlight_enable();
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229
2230
  		kfree(pp);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2231
2232
  	return 0;
  }
f91266edb   Johannes Berg   [POWERPC] powerma...
2233
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
7ac5dde99   Scott Wood   [POWERPC] Impleme...
2234
  static void pmac_suspend_disable_irqs(void)
f91266edb   Johannes Berg   [POWERPC] powerma...
2235
  {
f91266edb   Johannes Berg   [POWERPC] powerma...
2236
2237
2238
  	/* Call platform functions marked "on sleep" */
  	pmac_pfunc_i2c_suspend();
  	pmac_pfunc_base_suspend();
f91266edb   Johannes Berg   [POWERPC] powerma...
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
  }
  
  static int powerbook_sleep(suspend_state_t state)
  {
  	int error = 0;
  
  	/* Wait for completion of async requests */
  	while (!batt_req.complete)
  		pmu_poll();
  
  	/* Giveup the lazy FPU & vec so we don't have to back them
  	 * up from the low level code
  	 */
  	enable_kernel_fp();
  
  #ifdef CONFIG_ALTIVEC
  	if (cpu_has_feature(CPU_FTR_ALTIVEC))
  		enable_kernel_altivec();
  #endif /* CONFIG_ALTIVEC */
  
  	switch (pmu_kind) {
  	case PMU_OHARE_BASED:
  		error = powerbook_sleep_3400();
  		break;
  	case PMU_HEATHROW_BASED:
  	case PMU_PADDINGTON_BASED:
  		error = powerbook_sleep_grackle();
  		break;
  	case PMU_KEYLARGO_BASED:
  		error = powerbook_sleep_Core99();
  		break;
  	default:
  		return -ENOSYS;
  	}
  
  	if (error)
  		return error;
  
  	mdelay(100);
f91266edb   Johannes Berg   [POWERPC] powerma...
2278
2279
  	return 0;
  }
7ac5dde99   Scott Wood   [POWERPC] Impleme...
2280
  static void pmac_suspend_enable_irqs(void)
f91266edb   Johannes Berg   [POWERPC] powerma...
2281
2282
2283
2284
  {
  	/* Force a poll of ADB interrupts */
  	adb_int_pending = 1;
  	via_pmu_interrupt(0, NULL);
f91266edb   Johannes Berg   [POWERPC] powerma...
2285
  	mdelay(10);
f91266edb   Johannes Berg   [POWERPC] powerma...
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
  
  	/* Call platform functions marked "on wake" */
  	pmac_pfunc_base_resume();
  	pmac_pfunc_i2c_resume();
  }
  
  static int pmu_sleep_valid(suspend_state_t state)
  {
  	return state == PM_SUSPEND_MEM
  		&& (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) >= 0);
  }
2f55ac072   Lionel Debroux   suspend: constify...
2297
  static const struct platform_suspend_ops pmu_pm_ops = {
f91266edb   Johannes Berg   [POWERPC] powerma...
2298
2299
2300
2301
2302
2303
  	.enter = powerbook_sleep,
  	.valid = pmu_sleep_valid,
  };
  
  static int register_pmu_pm_ops(void)
  {
7ac5dde99   Scott Wood   [POWERPC] Impleme...
2304
2305
2306
2307
  	if (pmu_kind == PMU_OHARE_BASED)
  		powerbook_sleep_init_3400();
  	ppc_md.suspend_disable_irqs = pmac_suspend_disable_irqs;
  	ppc_md.suspend_enable_irqs = pmac_suspend_enable_irqs;
f91266edb   Johannes Berg   [POWERPC] powerma...
2308
2309
2310
2311
2312
2313
2314
  	suspend_set_ops(&pmu_pm_ops);
  
  	return 0;
  }
  
  device_initcall(register_pmu_pm_ops);
  #endif
55929332c   Arnd Bergmann   drivers: Push dow...
2315
  static int pmu_ioctl(struct file *filp,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316
2317
  		     u_int cmd, u_long arg)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
  	__u32 __user *argp = (__u32 __user *)arg;
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2319
  	int error = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2320
2321
  
  	switch (cmd) {
c16a85a5a   Finn Thain   macintosh/via-pmu...
2322
  #ifdef CONFIG_PPC_PMAC
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2323
2324
2325
  	case PMU_IOC_SLEEP:
  		if (!capable(CAP_SYS_ADMIN))
  			return -EACCES;
f91266edb   Johannes Berg   [POWERPC] powerma...
2326
  		return pm_suspend(PM_SUSPEND_MEM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2327
  	case PMU_IOC_CAN_SLEEP:
f91266edb   Johannes Berg   [POWERPC] powerma...
2328
  		if (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2329
2330
2331
  			return put_user(0, argp);
  		else
  			return put_user(1, argp);
c16a85a5a   Finn Thain   macintosh/via-pmu...
2332
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2333

5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2334
2335
  #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
  	/* Compatibility ioctl's for backlight */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2336
  	case PMU_IOC_GET_BACKLIGHT:
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2337
2338
  	{
  		int brightness;
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2339
2340
2341
2342
2343
2344
2345
  		brightness = pmac_backlight_get_legacy_brightness();
  		if (brightness < 0)
  			return brightness;
  		else
  			return put_user(brightness, argp);
  
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2346
2347
  	case PMU_IOC_SET_BACKLIGHT:
  	{
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2348
  		int brightness;
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2349
2350
2351
2352
2353
  		error = get_user(brightness, argp);
  		if (error)
  			return error;
  
  		return pmac_backlight_set_legacy_brightness(brightness);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2354
2355
2356
  	}
  #ifdef CONFIG_INPUT_ADBHID
  	case PMU_IOC_GRAB_BACKLIGHT: {
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2357
  		struct pmu_private *pp = filp->private_data;
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2358

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359
2360
  		if (pp->backlight_locker)
  			return 0;
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2361

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2362
  		pp->backlight_locker = 1;
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2363
  		pmac_backlight_disable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2364
2365
2366
  		return 0;
  	}
  #endif /* CONFIG_INPUT_ADBHID */
5474c120a   Michael Hanselmann   [PATCH] Rewritten...
2367
  #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
4b755999d   Michael Hanselmann   [PATCH] powermac:...
2368

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2369
2370
2371
2372
2373
  	case PMU_IOC_GET_MODEL:
  	    	return put_user(pmu_kind, argp);
  	case PMU_IOC_HAS_ADB:
  		return put_user(pmu_has_adb, argp);
  	}
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2374
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2375
  }
55929332c   Arnd Bergmann   drivers: Push dow...
2376
2377
2378
2379
  static long pmu_unlocked_ioctl(struct file *filp,
  			       u_int cmd, u_long arg)
  {
  	int ret;
d851b6e04   Arnd Bergmann   mac: autoconvert ...
2380
  	mutex_lock(&pmu_info_proc_mutex);
55929332c   Arnd Bergmann   drivers: Push dow...
2381
  	ret = pmu_ioctl(filp, cmd, arg);
d851b6e04   Arnd Bergmann   mac: autoconvert ...
2382
  	mutex_unlock(&pmu_info_proc_mutex);
55929332c   Arnd Bergmann   drivers: Push dow...
2383
2384
2385
  
  	return ret;
  }
4cc4587fb   Andreas Schwab   via-pmu: Add comp...
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
  #ifdef CONFIG_COMPAT
  #define PMU_IOC_GET_BACKLIGHT32	_IOR('B', 1, compat_size_t)
  #define PMU_IOC_SET_BACKLIGHT32	_IOW('B', 2, compat_size_t)
  #define PMU_IOC_GET_MODEL32	_IOR('B', 3, compat_size_t)
  #define PMU_IOC_HAS_ADB32	_IOR('B', 4, compat_size_t)
  #define PMU_IOC_CAN_SLEEP32	_IOR('B', 5, compat_size_t)
  #define PMU_IOC_GRAB_BACKLIGHT32 _IOR('B', 6, compat_size_t)
  
  static long compat_pmu_ioctl (struct file *filp, u_int cmd, u_long arg)
  {
  	switch (cmd) {
  	case PMU_IOC_SLEEP:
  		break;
  	case PMU_IOC_GET_BACKLIGHT32:
  		cmd = PMU_IOC_GET_BACKLIGHT;
  		break;
  	case PMU_IOC_SET_BACKLIGHT32:
  		cmd = PMU_IOC_SET_BACKLIGHT;
  		break;
  	case PMU_IOC_GET_MODEL32:
  		cmd = PMU_IOC_GET_MODEL;
  		break;
  	case PMU_IOC_HAS_ADB32:
  		cmd = PMU_IOC_HAS_ADB;
  		break;
  	case PMU_IOC_CAN_SLEEP32:
  		cmd = PMU_IOC_CAN_SLEEP;
  		break;
  	case PMU_IOC_GRAB_BACKLIGHT32:
  		cmd = PMU_IOC_GRAB_BACKLIGHT;
  		break;
  	default:
  		return -ENOIOCTLCMD;
  	}
  	return pmu_unlocked_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  }
  #endif
fa027c2a0   Arjan van de Ven   [PATCH] mark stru...
2423
  static const struct file_operations pmu_device_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2424
2425
2426
  	.read		= pmu_read,
  	.write		= pmu_write,
  	.poll		= pmu_fpoll,
55929332c   Arnd Bergmann   drivers: Push dow...
2427
  	.unlocked_ioctl	= pmu_unlocked_ioctl,
4cc4587fb   Andreas Schwab   via-pmu: Add comp...
2428
2429
2430
  #ifdef CONFIG_COMPAT
  	.compat_ioctl	= compat_pmu_ioctl,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2431
2432
  	.open		= pmu_open,
  	.release	= pmu_release,
6038f373a   Arnd Bergmann   llseek: automatic...
2433
  	.llseek		= noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2434
  };
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2435
  static struct miscdevice pmu_device = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2436
2437
  	PMU_MINOR, "pmu", &pmu_device_fops
  };
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2438
  static int pmu_device_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2439
  {
c57902d52   Finn Thain   macintosh/via-pmu...
2440
  	if (pmu_state == uninitialized)
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2441
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2442
2443
2444
  	if (misc_register(&pmu_device) < 0)
  		printk(KERN_ERR "via-pmu: cannot register misc device.
  ");
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2445
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2446
  }
8c8709334   Benjamin Herrenschmidt   [PATCH] ppc32: Re...
2447
  device_initcall(pmu_device_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2448
2449
  
  #ifdef DEBUG_SLEEP
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2450
  static inline void 
c70c35da5   Finn Thain   macintosh/via-pmu...
2451
  polled_handshake(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2452
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
2453
2454
  	via2[B] &= ~TREQ; eieio();
  	while ((via2[B] & TACK) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2455
  		;
c70c35da5   Finn Thain   macintosh/via-pmu...
2456
2457
  	via2[B] |= TREQ; eieio();
  	while ((via2[B] & TACK) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2458
2459
  		;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2460
  static inline void 
c70c35da5   Finn Thain   macintosh/via-pmu...
2461
  polled_send_byte(int x)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2462
  {
c70c35da5   Finn Thain   macintosh/via-pmu...
2463
2464
2465
  	via1[ACR] |= SR_OUT | SR_EXT; eieio();
  	via1[SR] = x; eieio();
  	polled_handshake();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2466
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2467
  static inline int
c70c35da5   Finn Thain   macintosh/via-pmu...
2468
  polled_recv_byte(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2469
2470
  {
  	int x;
c70c35da5   Finn Thain   macintosh/via-pmu...
2471
2472
2473
2474
  	via1[ACR] = (via1[ACR] & ~SR_OUT) | SR_EXT; eieio();
  	x = via1[SR]; eieio();
  	polled_handshake();
  	x = via1[SR]; eieio();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2475
2476
  	return x;
  }
aacaf9bd9   Jon Loeliger   [PATCH] powerpc: ...
2477
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2478
2479
2480
2481
  pmu_polled_request(struct adb_request *req)
  {
  	unsigned long flags;
  	int i, l, c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
  
  	req->complete = 1;
  	c = req->data[0];
  	l = pmu_data_len[c][0];
  	if (l >= 0 && req->nbytes != l + 1)
  		return -EINVAL;
  
  	local_irq_save(flags);
  	while (pmu_state != idle)
  		pmu_poll();
c70c35da5   Finn Thain   macintosh/via-pmu...
2492
  	while ((via2[B] & TACK) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2493
  		;
c70c35da5   Finn Thain   macintosh/via-pmu...
2494
  	polled_send_byte(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2495
2496
  	if (l < 0) {
  		l = req->nbytes - 1;
c70c35da5   Finn Thain   macintosh/via-pmu...
2497
  		polled_send_byte(l);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2498
2499
  	}
  	for (i = 1; i <= l; ++i)
c70c35da5   Finn Thain   macintosh/via-pmu...
2500
  		polled_send_byte(req->data[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2501
2502
2503
  
  	l = pmu_data_len[c][1];
  	if (l < 0)
c70c35da5   Finn Thain   macintosh/via-pmu...
2504
  		l = polled_recv_byte();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2505
  	for (i = 0; i < l; ++i)
c70c35da5   Finn Thain   macintosh/via-pmu...
2506
  		req->reply[i + req->reply_len] = polled_recv_byte();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2507
2508
2509
2510
2511
2512
2513
  
  	if (req->done)
  		(*req->done)(req);
  
  	local_irq_restore(flags);
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2514

f91266edb   Johannes Berg   [POWERPC] powerma...
2515
2516
2517
2518
  /* N.B. This doesn't work on the 3400 */
  void pmu_blink(int n)
  {
  	struct adb_request req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2519

f91266edb   Johannes Berg   [POWERPC] powerma...
2520
  	memset(&req, 0, sizeof(req));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2521

f91266edb   Johannes Berg   [POWERPC] powerma...
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
  	for (; n > 0; --n) {
  		req.nbytes = 4;
  		req.done = NULL;
  		req.data[0] = 0xee;
  		req.data[1] = 4;
  		req.data[2] = 0;
  		req.data[3] = 1;
  		req.reply[0] = ADB_RET_OK;
  		req.reply_len = 1;
  		req.reply_expected = 0;
  		pmu_polled_request(&req);
  		mdelay(50);
  		req.nbytes = 4;
  		req.done = NULL;
  		req.data[0] = 0xee;
  		req.data[1] = 4;
  		req.data[2] = 0;
  		req.data[3] = 0;
  		req.reply[0] = ADB_RET_OK;
  		req.reply_len = 1;
  		req.reply_expected = 0;
  		pmu_polled_request(&req);
  		mdelay(50);
  	}
  	mdelay(50);
  }
  #endif /* DEBUG_SLEEP */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2549

f91266edb   Johannes Berg   [POWERPC] powerma...
2550
  #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
f596575e8   Johannes Berg   [POWERPC] via-pmu...
2551
  int pmu_sys_suspended;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2552

e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2553
  static int pmu_syscore_suspend(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2554
  {
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2555
  	/* Suspend PMU event interrupts */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2556
  	pmu_suspend();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2557
  	pmu_sys_suspended = 1;
0094f2cdc   Benjamin Herrenschmidt   [POWERPC] Fix for...
2558
2559
2560
2561
2562
  
  #ifdef CONFIG_PMAC_BACKLIGHT
  	/* Tell backlight code not to muck around with the chip anymore */
  	pmu_backlight_set_sleep(1);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2563
2564
  	return 0;
  }
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2565
  static void pmu_syscore_resume(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2566
2567
2568
2569
  {
  	struct adb_request req;
  
  	if (!pmu_sys_suspended)
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2570
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2571
2572
2573
2574
  
  	/* Tell PMU we are ready */
  	pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
  	pmu_wait_complete(&req);
0094f2cdc   Benjamin Herrenschmidt   [POWERPC] Fix for...
2575
2576
2577
2578
  #ifdef CONFIG_PMAC_BACKLIGHT
  	/* Tell backlight code it can use the chip again */
  	pmu_backlight_set_sleep(0);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2579
2580
  	/* Resume PMU event interrupts */
  	pmu_resume();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2581
  	pmu_sys_suspended = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2582
  }
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2583
2584
2585
  static struct syscore_ops pmu_syscore_ops = {
  	.suspend = pmu_syscore_suspend,
  	.resume = pmu_syscore_resume,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2586
  };
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2587
  static int pmu_syscore_register(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2588
  {
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2589
  	register_syscore_ops(&pmu_syscore_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2590

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2591
2592
  	return 0;
  }
e83b906c9   Benjamin Herrenschmidt   powerpc/pmac: Upd...
2593
2594
  subsys_initcall(pmu_syscore_register);
  #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2595
2596
  
  EXPORT_SYMBOL(pmu_request);
730745a5c   Benjamin Herrenschmidt   [PATCH] 1/5 power...
2597
  EXPORT_SYMBOL(pmu_queue_request);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2598
2599
2600
2601
2602
2603
  EXPORT_SYMBOL(pmu_poll);
  EXPORT_SYMBOL(pmu_poll_adb);
  EXPORT_SYMBOL(pmu_wait_complete);
  EXPORT_SYMBOL(pmu_suspend);
  EXPORT_SYMBOL(pmu_resume);
  EXPORT_SYMBOL(pmu_unlock);
620a24597   Guido Guenther   [POWERPC] Fix bui...
2604
  #if defined(CONFIG_PPC32)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2605
2606
2607
2608
  EXPORT_SYMBOL(pmu_enable_irled);
  EXPORT_SYMBOL(pmu_battery_count);
  EXPORT_SYMBOL(pmu_batteries);
  EXPORT_SYMBOL(pmu_power_flags);
f91266edb   Johannes Berg   [POWERPC] powerma...
2609
  #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2610