Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Device driver for the via-pmu on Apple Powermacs. |
| 3 | * |
| 4 | * The VIA (versatile interface adapter) interfaces to the PMU, |
| 5 | * a 6805 microprocessor core whose primary function is to control |
| 6 | * battery charging and system power on the PowerBook 3400 and 2400. |
| 7 | * The PMU also controls the ADB (Apple Desktop Bus) which connects |
| 8 | * to the keyboard and mouse, as well as the non-volatile RAM |
| 9 | * and the RTC (real time clock) chip. |
| 10 | * |
| 11 | * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi. |
| 12 | * Copyright (C) 2001-2002 Benjamin Herrenschmidt |
| 13 | * |
| 14 | * THIS DRIVER IS BECOMING A TOTAL MESS ! |
| 15 | * - Cleanup atomically disabling reply to PMU events after |
| 16 | * a sleep or a freq. switch |
| 17 | * - Move sleep code out of here to pmac_pm, merge into new |
| 18 | * common PM infrastructure |
| 19 | * - Move backlight code out as well |
| 20 | * - Save/Restore PCI space properly |
| 21 | * |
| 22 | */ |
| 23 | #include <stdarg.h> |
| 24 | #include <linux/config.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/miscdevice.h> |
| 31 | #include <linux/blkdev.h> |
| 32 | #include <linux/pci.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/adb.h> |
| 36 | #include <linux/pmu.h> |
| 37 | #include <linux/cuda.h> |
| 38 | #include <linux/smp_lock.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/spinlock.h> |
| 41 | #include <linux/pm.h> |
| 42 | #include <linux/proc_fs.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/device.h> |
| 46 | #include <linux/sysdev.h> |
| 47 | #include <linux/suspend.h> |
| 48 | #include <linux/syscalls.h> |
| 49 | #include <linux/cpu.h> |
| 50 | #include <asm/prom.h> |
| 51 | #include <asm/machdep.h> |
| 52 | #include <asm/io.h> |
| 53 | #include <asm/pgtable.h> |
| 54 | #include <asm/system.h> |
| 55 | #include <asm/sections.h> |
| 56 | #include <asm/irq.h> |
| 57 | #include <asm/pmac_feature.h> |
Benjamin Herrenschmidt | 5b9ca52 | 2006-01-07 11:41:02 +1100 | [diff] [blame] | 58 | #include <asm/pmac_pfunc.h> |
| 59 | #include <asm/pmac_low_i2c.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <asm/uaccess.h> |
| 61 | #include <asm/mmu_context.h> |
| 62 | #include <asm/cputable.h> |
| 63 | #include <asm/time.h> |
| 64 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 65 | #include <asm/backlight.h> |
| 66 | #endif |
| 67 | |
Benjamin Herrenschmidt | e4ee69c | 2005-06-27 14:36:32 -0700 | [diff] [blame] | 68 | #ifdef CONFIG_PPC32 |
| 69 | #include <asm/open_pic.h> |
| 70 | #endif |
| 71 | |
Johannes Berg | 9e8e30a | 2006-06-26 01:49:55 -0400 | [diff] [blame] | 72 | #include "via-pmu-event.h" |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* Some compile options */ |
| 75 | #undef SUSPEND_USES_PMU |
| 76 | #define DEBUG_SLEEP |
| 77 | #undef HACKED_PCI_SAVE |
| 78 | |
| 79 | /* Misc minor number allocated for /dev/pmu */ |
| 80 | #define PMU_MINOR 154 |
| 81 | |
| 82 | /* How many iterations between battery polls */ |
| 83 | #define BATTERY_POLLING_COUNT 2 |
| 84 | |
| 85 | static volatile unsigned char __iomem *via; |
| 86 | |
| 87 | /* VIA registers - spaced 0x200 bytes apart */ |
| 88 | #define RS 0x200 /* skip between registers */ |
| 89 | #define B 0 /* B-side data */ |
| 90 | #define A RS /* A-side data */ |
| 91 | #define DIRB (2*RS) /* B-side direction (1=output) */ |
| 92 | #define DIRA (3*RS) /* A-side direction (1=output) */ |
| 93 | #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ |
| 94 | #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ |
| 95 | #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ |
| 96 | #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ |
| 97 | #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */ |
| 98 | #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */ |
| 99 | #define SR (10*RS) /* Shift register */ |
| 100 | #define ACR (11*RS) /* Auxiliary control register */ |
| 101 | #define PCR (12*RS) /* Peripheral control register */ |
| 102 | #define IFR (13*RS) /* Interrupt flag register */ |
| 103 | #define IER (14*RS) /* Interrupt enable register */ |
| 104 | #define ANH (15*RS) /* A-side data, no handshake */ |
| 105 | |
| 106 | /* Bits in B data register: both active low */ |
| 107 | #define TACK 0x08 /* Transfer acknowledge (input) */ |
| 108 | #define TREQ 0x10 /* Transfer request (output) */ |
| 109 | |
| 110 | /* Bits in ACR */ |
| 111 | #define SR_CTRL 0x1c /* Shift register control bits */ |
| 112 | #define SR_EXT 0x0c /* Shift on external clock */ |
| 113 | #define SR_OUT 0x10 /* Shift out if 1 */ |
| 114 | |
| 115 | /* Bits in IFR and IER */ |
| 116 | #define IER_SET 0x80 /* set bits in IER */ |
| 117 | #define IER_CLR 0 /* clear bits in IER */ |
| 118 | #define SR_INT 0x04 /* Shift register full/empty */ |
| 119 | #define CB2_INT 0x08 |
| 120 | #define CB1_INT 0x10 /* transition on CB1 input */ |
| 121 | |
| 122 | static volatile enum pmu_state { |
| 123 | idle, |
| 124 | sending, |
| 125 | intack, |
| 126 | reading, |
| 127 | reading_intr, |
| 128 | locked, |
| 129 | } pmu_state; |
| 130 | |
| 131 | static volatile enum int_data_state { |
| 132 | int_data_empty, |
| 133 | int_data_fill, |
| 134 | int_data_ready, |
| 135 | int_data_flush |
| 136 | } int_data_state[2] = { int_data_empty, int_data_empty }; |
| 137 | |
| 138 | static struct adb_request *current_req; |
| 139 | static struct adb_request *last_req; |
| 140 | static struct adb_request *req_awaiting_reply; |
| 141 | static unsigned char interrupt_data[2][32]; |
| 142 | static int interrupt_data_len[2]; |
| 143 | static int int_data_last; |
| 144 | static unsigned char *reply_ptr; |
| 145 | static int data_index; |
| 146 | static int data_len; |
| 147 | static volatile int adb_int_pending; |
| 148 | static volatile int disable_poll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static struct device_node *vias; |
| 150 | static int pmu_kind = PMU_UNKNOWN; |
| 151 | static int pmu_fully_inited = 0; |
| 152 | static int pmu_has_adb; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 153 | static struct device_node *gpio_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | static unsigned char __iomem *gpio_reg = NULL; |
| 155 | static int gpio_irq = -1; |
| 156 | static int gpio_irq_enabled = -1; |
| 157 | static volatile int pmu_suspended = 0; |
| 158 | static spinlock_t pmu_lock; |
| 159 | static u8 pmu_intr_mask; |
| 160 | static int pmu_version; |
| 161 | static int drop_interrupts; |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 162 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | static int option_lid_wakeup = 1; |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 164 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 165 | #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY) |
Kristian Mueller | a04c878 | 2005-12-15 12:31:55 +0800 | [diff] [blame] | 166 | static int sleep_in_progress; |
Andrew Morton | 57ae595 | 2006-03-21 23:20:27 -0800 | [diff] [blame] | 167 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static unsigned long async_req_locks; |
| 169 | static unsigned int pmu_irq_stats[11]; |
| 170 | |
| 171 | static struct proc_dir_entry *proc_pmu_root; |
| 172 | static struct proc_dir_entry *proc_pmu_info; |
| 173 | static struct proc_dir_entry *proc_pmu_irqstats; |
| 174 | static struct proc_dir_entry *proc_pmu_options; |
| 175 | static int option_server_mode; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | int pmu_battery_count; |
| 178 | int pmu_cur_battery; |
| 179 | unsigned int pmu_power_flags; |
| 180 | struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES]; |
| 181 | static int query_batt_timer = BATTERY_POLLING_COUNT; |
| 182 | static struct adb_request batt_req; |
| 183 | static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) |
| 186 | extern int disable_kernel_backlight; |
| 187 | #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */ |
| 188 | |
| 189 | int __fake_sleep; |
| 190 | int asleep; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 191 | BLOCKING_NOTIFIER_HEAD(sleep_notifier_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | #ifdef CONFIG_ADB |
| 194 | static int adb_dev_map = 0; |
| 195 | static int pmu_adb_flags; |
| 196 | |
| 197 | static int pmu_probe(void); |
| 198 | static int pmu_init(void); |
| 199 | static int pmu_send_request(struct adb_request *req, int sync); |
| 200 | static int pmu_adb_autopoll(int devs); |
| 201 | static int pmu_adb_reset_bus(void); |
| 202 | #endif /* CONFIG_ADB */ |
| 203 | |
| 204 | static int init_pmu(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static void pmu_start(void); |
| 206 | static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs); |
| 207 | static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs); |
| 208 | static int proc_get_info(char *page, char **start, off_t off, |
| 209 | int count, int *eof, void *data); |
| 210 | static int proc_get_irqstats(char *page, char **start, off_t off, |
| 211 | int count, int *eof, void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static void pmu_pass_intr(unsigned char *data, int len); |
| 213 | static int proc_get_batt(char *page, char **start, off_t off, |
| 214 | int count, int *eof, void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | static int proc_read_options(char *page, char **start, off_t off, |
| 216 | int count, int *eof, void *data); |
| 217 | static int proc_write_options(struct file *file, const char __user *buffer, |
| 218 | unsigned long count, void *data); |
| 219 | |
| 220 | #ifdef CONFIG_ADB |
| 221 | struct adb_driver via_pmu_driver = { |
| 222 | "PMU", |
| 223 | pmu_probe, |
| 224 | pmu_init, |
| 225 | pmu_send_request, |
| 226 | pmu_adb_autopoll, |
| 227 | pmu_poll_adb, |
| 228 | pmu_adb_reset_bus |
| 229 | }; |
| 230 | #endif /* CONFIG_ADB */ |
| 231 | |
| 232 | extern void low_sleep_handler(void); |
| 233 | extern void enable_kernel_altivec(void); |
| 234 | extern void enable_kernel_fp(void); |
| 235 | |
| 236 | #ifdef DEBUG_SLEEP |
| 237 | int pmu_polled_request(struct adb_request *req); |
| 238 | int pmu_wink(struct adb_request *req); |
| 239 | #endif |
| 240 | |
| 241 | /* |
| 242 | * This table indicates for each PMU opcode: |
| 243 | * - the number of data bytes to be sent with the command, or -1 |
| 244 | * if a length byte should be sent, |
| 245 | * - the number of response bytes which the PMU will return, or |
| 246 | * -1 if it will send a length byte. |
| 247 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 248 | static const s8 pmu_data_len[256][2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* 0 1 2 3 4 5 6 7 */ |
| 250 | /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 251 | /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 252 | /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 253 | /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0}, |
| 254 | /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 255 | /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1}, |
| 256 | /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 257 | /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0}, |
| 258 | /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 259 | /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1}, |
| 260 | /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0}, |
| 261 | /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 262 | /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 263 | /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1}, |
| 264 | /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 265 | /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1}, |
| 266 | /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 267 | /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 268 | /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 269 | /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 270 | /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0}, |
| 271 | /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 272 | /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 273 | /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 274 | /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 275 | /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 276 | /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 277 | /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1}, |
| 278 | /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0}, |
| 279 | /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0}, |
| 280 | /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0}, |
| 281 | /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 282 | }; |
| 283 | |
| 284 | static char *pbook_type[] = { |
| 285 | "Unknown PowerBook", |
| 286 | "PowerBook 2400/3400/3500(G3)", |
| 287 | "PowerBook G3 Series", |
| 288 | "1999 PowerBook G3", |
| 289 | "Core99" |
| 290 | }; |
| 291 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 292 | int __init find_via_pmu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 294 | u64 taddr; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 295 | u32 *reg; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | if (via != 0) |
| 298 | return 1; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 299 | vias = of_find_node_by_name(NULL, "via-pmu"); |
| 300 | if (vias == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 303 | reg = (u32 *)get_property(vias, "reg", NULL); |
| 304 | if (reg == NULL) { |
| 305 | printk(KERN_ERR "via-pmu: No \"reg\" property !\n"); |
| 306 | goto fail; |
| 307 | } |
| 308 | taddr = of_translate_address(vias, reg); |
Benjamin Herrenschmidt | bb6b9b2 | 2005-11-30 16:54:12 +1100 | [diff] [blame] | 309 | if (taddr == OF_BAD_ADDR) { |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 310 | printk(KERN_ERR "via-pmu: Can't translate address !\n"); |
| 311 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | spin_lock_init(&pmu_lock); |
| 315 | |
| 316 | pmu_has_adb = 1; |
| 317 | |
| 318 | pmu_intr_mask = PMU_INT_PCEJECT | |
| 319 | PMU_INT_SNDBRT | |
| 320 | PMU_INT_ADB | |
| 321 | PMU_INT_TICK; |
| 322 | |
| 323 | if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0) |
| 324 | || device_is_compatible(vias->parent, "ohare"))) |
| 325 | pmu_kind = PMU_OHARE_BASED; |
| 326 | else if (device_is_compatible(vias->parent, "paddington")) |
| 327 | pmu_kind = PMU_PADDINGTON_BASED; |
| 328 | else if (device_is_compatible(vias->parent, "heathrow")) |
| 329 | pmu_kind = PMU_HEATHROW_BASED; |
| 330 | else if (device_is_compatible(vias->parent, "Keylargo") |
| 331 | || device_is_compatible(vias->parent, "K2-Keylargo")) { |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 332 | struct device_node *gpiop; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 333 | u64 gaddr = OF_BAD_ADDR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | pmu_kind = PMU_KEYLARGO_BASED; |
| 336 | pmu_has_adb = (find_type_devices("adb") != NULL); |
| 337 | pmu_intr_mask = PMU_INT_PCEJECT | |
| 338 | PMU_INT_SNDBRT | |
| 339 | PMU_INT_ADB | |
| 340 | PMU_INT_TICK | |
| 341 | PMU_INT_ENVIRONMENT; |
| 342 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 343 | gpiop = of_find_node_by_name(NULL, "gpio"); |
| 344 | if (gpiop) { |
| 345 | reg = (u32 *)get_property(gpiop, "reg", NULL); |
| 346 | if (reg) |
| 347 | gaddr = of_translate_address(gpiop, reg); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 348 | if (gaddr != OF_BAD_ADDR) |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 349 | gpio_reg = ioremap(gaddr, 0x10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 351 | if (gpio_reg == NULL) |
| 352 | printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } else |
| 354 | pmu_kind = PMU_UNKNOWN; |
| 355 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 356 | via = ioremap(taddr, 0x2000); |
| 357 | if (via == NULL) { |
| 358 | printk(KERN_ERR "via-pmu: Can't map address !\n"); |
| 359 | goto fail; |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */ |
| 363 | out_8(&via[IFR], 0x7f); /* clear IFR */ |
| 364 | |
| 365 | pmu_state = idle; |
| 366 | |
| 367 | if (!init_pmu()) { |
| 368 | via = NULL; |
| 369 | return 0; |
| 370 | } |
| 371 | |
Benjamin Herrenschmidt | bb6b9b2 | 2005-11-30 16:54:12 +1100 | [diff] [blame] | 372 | printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version); |
| 374 | |
| 375 | sys_ctrler = SYS_CTRLER_PMU; |
| 376 | |
| 377 | return 1; |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 378 | fail: |
| 379 | of_node_put(vias); |
| 380 | vias = NULL; |
| 381 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | #ifdef CONFIG_ADB |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 385 | static int pmu_probe(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | return vias == NULL? -ENODEV: 0; |
| 388 | } |
| 389 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 390 | static int __init pmu_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
| 392 | if (vias == NULL) |
| 393 | return -ENODEV; |
| 394 | return 0; |
| 395 | } |
| 396 | #endif /* CONFIG_ADB */ |
| 397 | |
| 398 | /* |
| 399 | * We can't wait until pmu_init gets called, that happens too late. |
| 400 | * It happens after IDE and SCSI initialization, which can take a few |
| 401 | * seconds, and by that time the PMU could have given up on us and |
| 402 | * turned us off. |
| 403 | * Thus this is called with arch_initcall rather than device_initcall. |
| 404 | */ |
| 405 | static int __init via_pmu_start(void) |
| 406 | { |
| 407 | if (vias == NULL) |
| 408 | return -ENODEV; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | batt_req.complete = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 412 | #ifndef CONFIG_PPC_MERGE |
Benjamin Herrenschmidt | e4ee69c | 2005-06-27 14:36:32 -0700 | [diff] [blame] | 413 | if (pmu_kind == PMU_KEYLARGO_BASED) |
| 414 | openpic_set_irq_priority(vias->intrs[0].line, |
| 415 | OPENPIC_PRIORITY_DEFAULT + 1); |
| 416 | #endif |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU", |
| 419 | (void *)0)) { |
| 420 | printk(KERN_ERR "VIA-PMU: can't get irq %d\n", |
| 421 | vias->intrs[0].line); |
| 422 | return -EAGAIN; |
| 423 | } |
| 424 | |
Benjamin Herrenschmidt | 51d3082 | 2005-11-23 17:57:25 +1100 | [diff] [blame] | 425 | if (pmu_kind == PMU_KEYLARGO_BASED) { |
| 426 | gpio_node = of_find_node_by_name(NULL, "extint-gpio1"); |
| 427 | if (gpio_node == NULL) |
| 428 | gpio_node = of_find_node_by_name(NULL, |
| 429 | "pmu-interrupt"); |
| 430 | if (gpio_node && gpio_node->n_intrs > 0) |
| 431 | gpio_irq = gpio_node->intrs[0].line; |
| 432 | |
| 433 | if (gpio_irq != -1) { |
| 434 | if (request_irq(gpio_irq, gpio1_interrupt, 0, |
| 435 | "GPIO1 ADB", (void *)0)) |
| 436 | printk(KERN_ERR "pmu: can't get irq %d" |
| 437 | " (GPIO1)\n", gpio_irq); |
| 438 | else |
| 439 | gpio_irq_enabled = 1; |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* Enable interrupts */ |
| 444 | out_8(&via[IER], IER_SET | SR_INT | CB1_INT); |
| 445 | |
| 446 | pmu_fully_inited = 1; |
| 447 | |
| 448 | /* Make sure PMU settle down before continuing. This is _very_ important |
| 449 | * since the IDE probe may shut interrupts down for quite a bit of time. If |
| 450 | * a PMU communication is pending while this happens, the PMU may timeout |
| 451 | * Not that on Core99 machines, the PMU keeps sending us environement |
| 452 | * messages, we should find a way to either fix IDE or make it call |
| 453 | * pmu_suspend() before masking interrupts. This can also happens while |
| 454 | * scolling with some fbdevs. |
| 455 | */ |
| 456 | do { |
| 457 | pmu_poll(); |
| 458 | } while (pmu_state != idle); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | arch_initcall(via_pmu_start); |
| 464 | |
| 465 | /* |
| 466 | * This has to be done after pci_init, which is a subsys_initcall. |
| 467 | */ |
| 468 | static int __init via_pmu_dev_init(void) |
| 469 | { |
| 470 | if (vias == NULL) |
| 471 | return -ENODEV; |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #ifdef CONFIG_PMAC_BACKLIGHT |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 474 | /* Initialize backlight */ |
| 475 | pmu_backlight_init(vias); |
| 476 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 478 | #ifdef CONFIG_PPC32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (machine_is_compatible("AAPL,3400/2400") || |
| 480 | machine_is_compatible("AAPL,3500")) { |
| 481 | int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO, |
| 482 | NULL, PMAC_MB_INFO_MODEL, 0); |
| 483 | pmu_battery_count = 1; |
| 484 | if (mb == PMAC_TYPE_COMET) |
| 485 | pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET; |
| 486 | else |
| 487 | pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER; |
| 488 | } else if (machine_is_compatible("AAPL,PowerBook1998") || |
| 489 | machine_is_compatible("PowerBook1,1")) { |
| 490 | pmu_battery_count = 2; |
| 491 | pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART; |
| 492 | pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART; |
| 493 | } else { |
| 494 | struct device_node* prim = find_devices("power-mgt"); |
| 495 | u32 *prim_info = NULL; |
| 496 | if (prim) |
| 497 | prim_info = (u32 *)get_property(prim, "prim-info", NULL); |
| 498 | if (prim_info) { |
| 499 | /* Other stuffs here yet unknown */ |
| 500 | pmu_battery_count = (prim_info[6] >> 16) & 0xff; |
| 501 | pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART; |
| 502 | if (pmu_battery_count > 1) |
| 503 | pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART; |
| 504 | } |
| 505 | } |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 506 | #endif /* CONFIG_PPC32 */ |
| 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | /* Create /proc/pmu */ |
| 509 | proc_pmu_root = proc_mkdir("pmu", NULL); |
| 510 | if (proc_pmu_root) { |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 511 | long i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | for (i=0; i<pmu_battery_count; i++) { |
| 514 | char title[16]; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 515 | sprintf(title, "battery_%ld", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root, |
| 517 | proc_get_batt, (void *)i); |
| 518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root, |
| 521 | proc_get_info, NULL); |
| 522 | proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root, |
| 523 | proc_get_irqstats, NULL); |
| 524 | proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root); |
| 525 | if (proc_pmu_options) { |
| 526 | proc_pmu_options->nlink = 1; |
| 527 | proc_pmu_options->read_proc = proc_read_options; |
| 528 | proc_pmu_options->write_proc = proc_write_options; |
| 529 | } |
| 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | device_initcall(via_pmu_dev_init); |
| 535 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 536 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | init_pmu(void) |
| 538 | { |
| 539 | int timeout; |
| 540 | struct adb_request req; |
| 541 | |
| 542 | out_8(&via[B], via[B] | TREQ); /* negate TREQ */ |
| 543 | out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */ |
| 544 | |
| 545 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask); |
| 546 | timeout = 100000; |
| 547 | while (!req.complete) { |
| 548 | if (--timeout < 0) { |
| 549 | printk(KERN_ERR "init_pmu: no response from PMU\n"); |
| 550 | return 0; |
| 551 | } |
| 552 | udelay(10); |
| 553 | pmu_poll(); |
| 554 | } |
| 555 | |
| 556 | /* ack all pending interrupts */ |
| 557 | timeout = 100000; |
| 558 | interrupt_data[0][0] = 1; |
| 559 | while (interrupt_data[0][0] || pmu_state != idle) { |
| 560 | if (--timeout < 0) { |
| 561 | printk(KERN_ERR "init_pmu: timed out acking intrs\n"); |
| 562 | return 0; |
| 563 | } |
| 564 | if (pmu_state == idle) |
| 565 | adb_int_pending = 1; |
| 566 | via_pmu_interrupt(0, NULL, NULL); |
| 567 | udelay(10); |
| 568 | } |
| 569 | |
| 570 | /* Tell PMU we are ready. */ |
| 571 | if (pmu_kind == PMU_KEYLARGO_BASED) { |
| 572 | pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2); |
| 573 | while (!req.complete) |
| 574 | pmu_poll(); |
| 575 | } |
| 576 | |
| 577 | /* Read PMU version */ |
| 578 | pmu_request(&req, NULL, 1, PMU_GET_VERSION); |
| 579 | pmu_wait_complete(&req); |
| 580 | if (req.reply_len > 0) |
| 581 | pmu_version = req.reply[0]; |
| 582 | |
| 583 | /* Read server mode setting */ |
| 584 | if (pmu_kind == PMU_KEYLARGO_BASED) { |
| 585 | pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, |
| 586 | PMU_PWR_GET_POWERUP_EVENTS); |
| 587 | pmu_wait_complete(&req); |
| 588 | if (req.reply_len == 2) { |
| 589 | if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT) |
| 590 | option_server_mode = 1; |
| 591 | printk(KERN_INFO "via-pmu: Server Mode is %s\n", |
| 592 | option_server_mode ? "enabled" : "disabled"); |
| 593 | } |
| 594 | } |
| 595 | return 1; |
| 596 | } |
| 597 | |
| 598 | int |
| 599 | pmu_get_model(void) |
| 600 | { |
| 601 | return pmu_kind; |
| 602 | } |
| 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | static void pmu_set_server_mode(int server_mode) |
| 605 | { |
| 606 | struct adb_request req; |
| 607 | |
| 608 | if (pmu_kind != PMU_KEYLARGO_BASED) |
| 609 | return; |
| 610 | |
| 611 | option_server_mode = server_mode; |
| 612 | pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS); |
| 613 | pmu_wait_complete(&req); |
| 614 | if (req.reply_len < 2) |
| 615 | return; |
| 616 | if (server_mode) |
| 617 | pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, |
| 618 | PMU_PWR_SET_POWERUP_EVENTS, |
| 619 | req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); |
| 620 | else |
| 621 | pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, |
| 622 | PMU_PWR_CLR_POWERUP_EVENTS, |
| 623 | req.reply[0], PMU_PWR_WAKEUP_AC_INSERT); |
| 624 | pmu_wait_complete(&req); |
| 625 | } |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | /* This new version of the code for 2400/3400/3500 powerbooks |
| 628 | * is inspired from the implementation in gkrellm-pmu |
| 629 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 630 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | done_battery_state_ohare(struct adb_request* req) |
| 632 | { |
| 633 | /* format: |
| 634 | * [0] : flags |
| 635 | * 0x01 : AC indicator |
| 636 | * 0x02 : charging |
| 637 | * 0x04 : battery exist |
| 638 | * 0x08 : |
| 639 | * 0x10 : |
| 640 | * 0x20 : full charged |
| 641 | * 0x40 : pcharge reset |
| 642 | * 0x80 : battery exist |
| 643 | * |
| 644 | * [1][2] : battery voltage |
| 645 | * [3] : CPU temperature |
| 646 | * [4] : battery temperature |
| 647 | * [5] : current |
| 648 | * [6][7] : pcharge |
| 649 | * --tkoba |
| 650 | */ |
| 651 | unsigned int bat_flags = PMU_BATT_TYPE_HOOPER; |
| 652 | long pcharge, charge, vb, vmax, lmax; |
| 653 | long vmax_charging, vmax_charged; |
| 654 | long amperage, voltage, time, max; |
| 655 | int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO, |
| 656 | NULL, PMAC_MB_INFO_MODEL, 0); |
| 657 | |
| 658 | if (req->reply[0] & 0x01) |
| 659 | pmu_power_flags |= PMU_PWR_AC_PRESENT; |
| 660 | else |
| 661 | pmu_power_flags &= ~PMU_PWR_AC_PRESENT; |
| 662 | |
| 663 | if (mb == PMAC_TYPE_COMET) { |
| 664 | vmax_charged = 189; |
| 665 | vmax_charging = 213; |
| 666 | lmax = 6500; |
| 667 | } else { |
| 668 | vmax_charged = 330; |
| 669 | vmax_charging = 330; |
| 670 | lmax = 6500; |
| 671 | } |
| 672 | vmax = vmax_charged; |
| 673 | |
| 674 | /* If battery installed */ |
| 675 | if (req->reply[0] & 0x04) { |
| 676 | bat_flags |= PMU_BATT_PRESENT; |
| 677 | if (req->reply[0] & 0x02) |
| 678 | bat_flags |= PMU_BATT_CHARGING; |
| 679 | vb = (req->reply[1] << 8) | req->reply[2]; |
| 680 | voltage = (vb * 265 + 72665) / 10; |
| 681 | amperage = req->reply[5]; |
| 682 | if ((req->reply[0] & 0x01) == 0) { |
| 683 | if (amperage > 200) |
| 684 | vb += ((amperage - 200) * 15)/100; |
| 685 | } else if (req->reply[0] & 0x02) { |
| 686 | vb = (vb * 97) / 100; |
| 687 | vmax = vmax_charging; |
| 688 | } |
| 689 | charge = (100 * vb) / vmax; |
| 690 | if (req->reply[0] & 0x40) { |
| 691 | pcharge = (req->reply[6] << 8) + req->reply[7]; |
| 692 | if (pcharge > lmax) |
| 693 | pcharge = lmax; |
| 694 | pcharge *= 100; |
| 695 | pcharge = 100 - pcharge / lmax; |
| 696 | if (pcharge < charge) |
| 697 | charge = pcharge; |
| 698 | } |
| 699 | if (amperage > 0) |
| 700 | time = (charge * 16440) / amperage; |
| 701 | else |
| 702 | time = 0; |
| 703 | max = 100; |
| 704 | amperage = -amperage; |
| 705 | } else |
| 706 | charge = max = amperage = voltage = time = 0; |
| 707 | |
| 708 | pmu_batteries[pmu_cur_battery].flags = bat_flags; |
| 709 | pmu_batteries[pmu_cur_battery].charge = charge; |
| 710 | pmu_batteries[pmu_cur_battery].max_charge = max; |
| 711 | pmu_batteries[pmu_cur_battery].amperage = amperage; |
| 712 | pmu_batteries[pmu_cur_battery].voltage = voltage; |
| 713 | pmu_batteries[pmu_cur_battery].time_remaining = time; |
| 714 | |
| 715 | clear_bit(0, &async_req_locks); |
| 716 | } |
| 717 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 718 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | done_battery_state_smart(struct adb_request* req) |
| 720 | { |
| 721 | /* format: |
| 722 | * [0] : format of this structure (known: 3,4,5) |
| 723 | * [1] : flags |
| 724 | * |
| 725 | * format 3 & 4: |
| 726 | * |
| 727 | * [2] : charge |
| 728 | * [3] : max charge |
| 729 | * [4] : current |
| 730 | * [5] : voltage |
| 731 | * |
| 732 | * format 5: |
| 733 | * |
| 734 | * [2][3] : charge |
| 735 | * [4][5] : max charge |
| 736 | * [6][7] : current |
| 737 | * [8][9] : voltage |
| 738 | */ |
| 739 | |
| 740 | unsigned int bat_flags = PMU_BATT_TYPE_SMART; |
| 741 | int amperage; |
| 742 | unsigned int capa, max, voltage; |
| 743 | |
| 744 | if (req->reply[1] & 0x01) |
| 745 | pmu_power_flags |= PMU_PWR_AC_PRESENT; |
| 746 | else |
| 747 | pmu_power_flags &= ~PMU_PWR_AC_PRESENT; |
| 748 | |
| 749 | |
| 750 | capa = max = amperage = voltage = 0; |
| 751 | |
| 752 | if (req->reply[1] & 0x04) { |
| 753 | bat_flags |= PMU_BATT_PRESENT; |
| 754 | switch(req->reply[0]) { |
| 755 | case 3: |
| 756 | case 4: capa = req->reply[2]; |
| 757 | max = req->reply[3]; |
| 758 | amperage = *((signed char *)&req->reply[4]); |
| 759 | voltage = req->reply[5]; |
| 760 | break; |
| 761 | case 5: capa = (req->reply[2] << 8) | req->reply[3]; |
| 762 | max = (req->reply[4] << 8) | req->reply[5]; |
| 763 | amperage = *((signed short *)&req->reply[6]); |
| 764 | voltage = (req->reply[8] << 8) | req->reply[9]; |
| 765 | break; |
| 766 | default: |
| 767 | printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n", |
| 768 | req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]); |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if ((req->reply[1] & 0x01) && (amperage > 0)) |
| 774 | bat_flags |= PMU_BATT_CHARGING; |
| 775 | |
| 776 | pmu_batteries[pmu_cur_battery].flags = bat_flags; |
| 777 | pmu_batteries[pmu_cur_battery].charge = capa; |
| 778 | pmu_batteries[pmu_cur_battery].max_charge = max; |
| 779 | pmu_batteries[pmu_cur_battery].amperage = amperage; |
| 780 | pmu_batteries[pmu_cur_battery].voltage = voltage; |
| 781 | if (amperage) { |
| 782 | if ((req->reply[1] & 0x01) && (amperage > 0)) |
| 783 | pmu_batteries[pmu_cur_battery].time_remaining |
| 784 | = ((max-capa) * 3600) / amperage; |
| 785 | else |
| 786 | pmu_batteries[pmu_cur_battery].time_remaining |
| 787 | = (capa * 3600) / (-amperage); |
| 788 | } else |
| 789 | pmu_batteries[pmu_cur_battery].time_remaining = 0; |
| 790 | |
| 791 | pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count; |
| 792 | |
| 793 | clear_bit(0, &async_req_locks); |
| 794 | } |
| 795 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 796 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | query_battery_state(void) |
| 798 | { |
| 799 | if (test_and_set_bit(0, &async_req_locks)) |
| 800 | return; |
| 801 | if (pmu_kind == PMU_OHARE_BASED) |
| 802 | pmu_request(&batt_req, done_battery_state_ohare, |
| 803 | 1, PMU_BATTERY_STATE); |
| 804 | else |
| 805 | pmu_request(&batt_req, done_battery_state_smart, |
| 806 | 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1); |
| 807 | } |
| 808 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 809 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | proc_get_info(char *page, char **start, off_t off, |
| 811 | int count, int *eof, void *data) |
| 812 | { |
| 813 | char* p = page; |
| 814 | |
| 815 | p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION); |
| 816 | p += sprintf(p, "PMU firmware version : %02x\n", pmu_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | p += sprintf(p, "AC Power : %d\n", |
Benjamin Herrenschmidt | 63e1fd4 | 2006-03-13 21:20:42 -0800 | [diff] [blame] | 818 | ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | p += sprintf(p, "Battery count : %d\n", pmu_battery_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | return p - page; |
| 822 | } |
| 823 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 824 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | proc_get_irqstats(char *page, char **start, off_t off, |
| 826 | int count, int *eof, void *data) |
| 827 | { |
| 828 | int i; |
| 829 | char* p = page; |
| 830 | static const char *irq_names[] = { |
| 831 | "Total CB1 triggered events", |
| 832 | "Total GPIO1 triggered events", |
| 833 | "PC-Card eject button", |
| 834 | "Sound/Brightness button", |
| 835 | "ADB message", |
| 836 | "Battery state change", |
| 837 | "Environment interrupt", |
| 838 | "Tick timer", |
| 839 | "Ghost interrupt (zero len)", |
| 840 | "Empty interrupt (empty mask)", |
| 841 | "Max irqs in a row" |
| 842 | }; |
| 843 | |
| 844 | for (i=0; i<11; i++) { |
| 845 | p += sprintf(p, " %2u: %10u (%s)\n", |
| 846 | i, pmu_irq_stats[i], irq_names[i]); |
| 847 | } |
| 848 | return p - page; |
| 849 | } |
| 850 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 851 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | proc_get_batt(char *page, char **start, off_t off, |
| 853 | int count, int *eof, void *data) |
| 854 | { |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 855 | long batnum = (long)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | char *p = page; |
| 857 | |
| 858 | p += sprintf(p, "\n"); |
| 859 | p += sprintf(p, "flags : %08x\n", |
| 860 | pmu_batteries[batnum].flags); |
| 861 | p += sprintf(p, "charge : %d\n", |
| 862 | pmu_batteries[batnum].charge); |
| 863 | p += sprintf(p, "max_charge : %d\n", |
| 864 | pmu_batteries[batnum].max_charge); |
| 865 | p += sprintf(p, "current : %d\n", |
| 866 | pmu_batteries[batnum].amperage); |
| 867 | p += sprintf(p, "voltage : %d\n", |
| 868 | pmu_batteries[batnum].voltage); |
| 869 | p += sprintf(p, "time rem. : %d\n", |
| 870 | pmu_batteries[batnum].time_remaining); |
| 871 | |
| 872 | return p - page; |
| 873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 875 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | proc_read_options(char *page, char **start, off_t off, |
| 877 | int count, int *eof, void *data) |
| 878 | { |
| 879 | char *p = page; |
| 880 | |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 881 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | if (pmu_kind == PMU_KEYLARGO_BASED && |
| 883 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0) |
| 884 | p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup); |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 885 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (pmu_kind == PMU_KEYLARGO_BASED) |
| 887 | p += sprintf(p, "server_mode=%d\n", option_server_mode); |
| 888 | |
| 889 | return p - page; |
| 890 | } |
| 891 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 892 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | proc_write_options(struct file *file, const char __user *buffer, |
| 894 | unsigned long count, void *data) |
| 895 | { |
| 896 | char tmp[33]; |
| 897 | char *label, *val; |
| 898 | unsigned long fcount = count; |
| 899 | |
| 900 | if (!count) |
| 901 | return -EINVAL; |
| 902 | if (count > 32) |
| 903 | count = 32; |
| 904 | if (copy_from_user(tmp, buffer, count)) |
| 905 | return -EFAULT; |
| 906 | tmp[count] = 0; |
| 907 | |
| 908 | label = tmp; |
| 909 | while(*label == ' ') |
| 910 | label++; |
| 911 | val = label; |
| 912 | while(*val && (*val != '=')) { |
| 913 | if (*val == ' ') |
| 914 | *val = 0; |
| 915 | val++; |
| 916 | } |
| 917 | if ((*val) == 0) |
| 918 | return -EINVAL; |
| 919 | *(val++) = 0; |
| 920 | while(*val == ' ') |
| 921 | val++; |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 922 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | if (pmu_kind == PMU_KEYLARGO_BASED && |
| 924 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0) |
| 925 | if (!strcmp(label, "lid_wakeup")) |
| 926 | option_lid_wakeup = ((*val) == '1'); |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) { |
| 929 | int new_value; |
| 930 | new_value = ((*val) == '1'); |
| 931 | if (new_value != option_server_mode) |
| 932 | pmu_set_server_mode(new_value); |
| 933 | } |
| 934 | return fcount; |
| 935 | } |
| 936 | |
| 937 | #ifdef CONFIG_ADB |
| 938 | /* Send an ADB command */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 939 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | pmu_send_request(struct adb_request *req, int sync) |
| 941 | { |
| 942 | int i, ret; |
| 943 | |
| 944 | if ((vias == NULL) || (!pmu_fully_inited)) { |
| 945 | req->complete = 1; |
| 946 | return -ENXIO; |
| 947 | } |
| 948 | |
| 949 | ret = -EINVAL; |
| 950 | |
| 951 | switch (req->data[0]) { |
| 952 | case PMU_PACKET: |
| 953 | for (i = 0; i < req->nbytes - 1; ++i) |
| 954 | req->data[i] = req->data[i+1]; |
| 955 | --req->nbytes; |
| 956 | if (pmu_data_len[req->data[0]][1] != 0) { |
| 957 | req->reply[0] = ADB_RET_OK; |
| 958 | req->reply_len = 1; |
| 959 | } else |
| 960 | req->reply_len = 0; |
| 961 | ret = pmu_queue_request(req); |
| 962 | break; |
| 963 | case CUDA_PACKET: |
| 964 | switch (req->data[1]) { |
| 965 | case CUDA_GET_TIME: |
| 966 | if (req->nbytes != 2) |
| 967 | break; |
| 968 | req->data[0] = PMU_READ_RTC; |
| 969 | req->nbytes = 1; |
| 970 | req->reply_len = 3; |
| 971 | req->reply[0] = CUDA_PACKET; |
| 972 | req->reply[1] = 0; |
| 973 | req->reply[2] = CUDA_GET_TIME; |
| 974 | ret = pmu_queue_request(req); |
| 975 | break; |
| 976 | case CUDA_SET_TIME: |
| 977 | if (req->nbytes != 6) |
| 978 | break; |
| 979 | req->data[0] = PMU_SET_RTC; |
| 980 | req->nbytes = 5; |
| 981 | for (i = 1; i <= 4; ++i) |
| 982 | req->data[i] = req->data[i+1]; |
| 983 | req->reply_len = 3; |
| 984 | req->reply[0] = CUDA_PACKET; |
| 985 | req->reply[1] = 0; |
| 986 | req->reply[2] = CUDA_SET_TIME; |
| 987 | ret = pmu_queue_request(req); |
| 988 | break; |
| 989 | } |
| 990 | break; |
| 991 | case ADB_PACKET: |
| 992 | if (!pmu_has_adb) |
| 993 | return -ENXIO; |
| 994 | for (i = req->nbytes - 1; i > 1; --i) |
| 995 | req->data[i+2] = req->data[i]; |
| 996 | req->data[3] = req->nbytes - 2; |
| 997 | req->data[2] = pmu_adb_flags; |
| 998 | /*req->data[1] = req->data[1];*/ |
| 999 | req->data[0] = PMU_ADB_CMD; |
| 1000 | req->nbytes += 2; |
| 1001 | req->reply_expected = 1; |
| 1002 | req->reply_len = 0; |
| 1003 | ret = pmu_queue_request(req); |
| 1004 | break; |
| 1005 | } |
| 1006 | if (ret) { |
| 1007 | req->complete = 1; |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | if (sync) |
| 1012 | while (!req->complete) |
| 1013 | pmu_poll(); |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /* Enable/disable autopolling */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1019 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | pmu_adb_autopoll(int devs) |
| 1021 | { |
| 1022 | struct adb_request req; |
| 1023 | |
| 1024 | if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb) |
| 1025 | return -ENXIO; |
| 1026 | |
| 1027 | if (devs) { |
| 1028 | adb_dev_map = devs; |
| 1029 | pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86, |
| 1030 | adb_dev_map >> 8, adb_dev_map); |
| 1031 | pmu_adb_flags = 2; |
| 1032 | } else { |
| 1033 | pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF); |
| 1034 | pmu_adb_flags = 0; |
| 1035 | } |
| 1036 | while (!req.complete) |
| 1037 | pmu_poll(); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | /* Reset the ADB bus */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1042 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | pmu_adb_reset_bus(void) |
| 1044 | { |
| 1045 | struct adb_request req; |
| 1046 | int save_autopoll = adb_dev_map; |
| 1047 | |
| 1048 | if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb) |
| 1049 | return -ENXIO; |
| 1050 | |
| 1051 | /* anyone got a better idea?? */ |
| 1052 | pmu_adb_autopoll(0); |
| 1053 | |
| 1054 | req.nbytes = 5; |
| 1055 | req.done = NULL; |
| 1056 | req.data[0] = PMU_ADB_CMD; |
| 1057 | req.data[1] = 0; |
| 1058 | req.data[2] = ADB_BUSRESET; |
| 1059 | req.data[3] = 0; |
| 1060 | req.data[4] = 0; |
| 1061 | req.reply_len = 0; |
| 1062 | req.reply_expected = 1; |
| 1063 | if (pmu_queue_request(&req) != 0) { |
| 1064 | printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n"); |
| 1065 | return -EIO; |
| 1066 | } |
| 1067 | pmu_wait_complete(&req); |
| 1068 | |
| 1069 | if (save_autopoll != 0) |
| 1070 | pmu_adb_autopoll(save_autopoll); |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | #endif /* CONFIG_ADB */ |
| 1075 | |
| 1076 | /* Construct and send a pmu request */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1077 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | pmu_request(struct adb_request *req, void (*done)(struct adb_request *), |
| 1079 | int nbytes, ...) |
| 1080 | { |
| 1081 | va_list list; |
| 1082 | int i; |
| 1083 | |
| 1084 | if (vias == NULL) |
| 1085 | return -ENXIO; |
| 1086 | |
| 1087 | if (nbytes < 0 || nbytes > 32) { |
| 1088 | printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes); |
| 1089 | req->complete = 1; |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | req->nbytes = nbytes; |
| 1093 | req->done = done; |
| 1094 | va_start(list, nbytes); |
| 1095 | for (i = 0; i < nbytes; ++i) |
| 1096 | req->data[i] = va_arg(list, int); |
| 1097 | va_end(list); |
| 1098 | req->reply_len = 0; |
| 1099 | req->reply_expected = 0; |
| 1100 | return pmu_queue_request(req); |
| 1101 | } |
| 1102 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1103 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | pmu_queue_request(struct adb_request *req) |
| 1105 | { |
| 1106 | unsigned long flags; |
| 1107 | int nsend; |
| 1108 | |
| 1109 | if (via == NULL) { |
| 1110 | req->complete = 1; |
| 1111 | return -ENXIO; |
| 1112 | } |
| 1113 | if (req->nbytes <= 0) { |
| 1114 | req->complete = 1; |
| 1115 | return 0; |
| 1116 | } |
| 1117 | nsend = pmu_data_len[req->data[0]][0]; |
| 1118 | if (nsend >= 0 && req->nbytes != nsend + 1) { |
| 1119 | req->complete = 1; |
| 1120 | return -EINVAL; |
| 1121 | } |
| 1122 | |
| 1123 | req->next = NULL; |
| 1124 | req->sent = 0; |
| 1125 | req->complete = 0; |
| 1126 | |
| 1127 | spin_lock_irqsave(&pmu_lock, flags); |
| 1128 | if (current_req != 0) { |
| 1129 | last_req->next = req; |
| 1130 | last_req = req; |
| 1131 | } else { |
| 1132 | current_req = req; |
| 1133 | last_req = req; |
| 1134 | if (pmu_state == idle) |
| 1135 | pmu_start(); |
| 1136 | } |
| 1137 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | static inline void |
| 1143 | wait_for_ack(void) |
| 1144 | { |
| 1145 | /* Sightly increased the delay, I had one occurrence of the message |
| 1146 | * reported |
| 1147 | */ |
| 1148 | int timeout = 4000; |
| 1149 | while ((in_8(&via[B]) & TACK) == 0) { |
| 1150 | if (--timeout < 0) { |
| 1151 | printk(KERN_ERR "PMU not responding (!ack)\n"); |
| 1152 | return; |
| 1153 | } |
| 1154 | udelay(10); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* New PMU seems to be very sensitive to those timings, so we make sure |
| 1159 | * PCI is flushed immediately */ |
| 1160 | static inline void |
| 1161 | send_byte(int x) |
| 1162 | { |
| 1163 | volatile unsigned char __iomem *v = via; |
| 1164 | |
| 1165 | out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT); |
| 1166 | out_8(&v[SR], x); |
| 1167 | out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */ |
| 1168 | (void)in_8(&v[B]); |
| 1169 | } |
| 1170 | |
| 1171 | static inline void |
| 1172 | recv_byte(void) |
| 1173 | { |
| 1174 | volatile unsigned char __iomem *v = via; |
| 1175 | |
| 1176 | out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT); |
| 1177 | in_8(&v[SR]); /* resets SR */ |
| 1178 | out_8(&v[B], in_8(&v[B]) & ~TREQ); |
| 1179 | (void)in_8(&v[B]); |
| 1180 | } |
| 1181 | |
| 1182 | static inline void |
| 1183 | pmu_done(struct adb_request *req) |
| 1184 | { |
| 1185 | void (*done)(struct adb_request *) = req->done; |
| 1186 | mb(); |
| 1187 | req->complete = 1; |
| 1188 | /* Here, we assume that if the request has a done member, the |
| 1189 | * struct request will survive to setting req->complete to 1 |
| 1190 | */ |
| 1191 | if (done) |
| 1192 | (*done)(req); |
| 1193 | } |
| 1194 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1195 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | pmu_start(void) |
| 1197 | { |
| 1198 | struct adb_request *req; |
| 1199 | |
| 1200 | /* assert pmu_state == idle */ |
| 1201 | /* get the packet to send */ |
| 1202 | req = current_req; |
| 1203 | if (req == 0 || pmu_state != idle |
| 1204 | || (/*req->reply_expected && */req_awaiting_reply)) |
| 1205 | return; |
| 1206 | |
| 1207 | pmu_state = sending; |
| 1208 | data_index = 1; |
| 1209 | data_len = pmu_data_len[req->data[0]][0]; |
| 1210 | |
| 1211 | /* Sounds safer to make sure ACK is high before writing. This helped |
| 1212 | * kill a problem with ADB and some iBooks |
| 1213 | */ |
| 1214 | wait_for_ack(); |
| 1215 | /* set the shift register to shift out and send a byte */ |
| 1216 | send_byte(req->data[0]); |
| 1217 | } |
| 1218 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1219 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | pmu_poll(void) |
| 1221 | { |
| 1222 | if (!via) |
| 1223 | return; |
| 1224 | if (disable_poll) |
| 1225 | return; |
| 1226 | via_pmu_interrupt(0, NULL, NULL); |
| 1227 | } |
| 1228 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1229 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | pmu_poll_adb(void) |
| 1231 | { |
| 1232 | if (!via) |
| 1233 | return; |
| 1234 | if (disable_poll) |
| 1235 | return; |
| 1236 | /* Kicks ADB read when PMU is suspended */ |
| 1237 | adb_int_pending = 1; |
| 1238 | do { |
| 1239 | via_pmu_interrupt(0, NULL, NULL); |
| 1240 | } while (pmu_suspended && (adb_int_pending || pmu_state != idle |
| 1241 | || req_awaiting_reply)); |
| 1242 | } |
| 1243 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1244 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | pmu_wait_complete(struct adb_request *req) |
| 1246 | { |
| 1247 | if (!via) |
| 1248 | return; |
| 1249 | while((pmu_state != idle && pmu_state != locked) || !req->complete) |
| 1250 | via_pmu_interrupt(0, NULL, NULL); |
| 1251 | } |
| 1252 | |
| 1253 | /* This function loops until the PMU is idle and prevents it from |
| 1254 | * anwsering to ADB interrupts. pmu_request can still be called. |
| 1255 | * This is done to avoid spurrious shutdowns when we know we'll have |
| 1256 | * interrupts switched off for a long time |
| 1257 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1258 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | pmu_suspend(void) |
| 1260 | { |
| 1261 | unsigned long flags; |
| 1262 | #ifdef SUSPEND_USES_PMU |
| 1263 | struct adb_request *req; |
| 1264 | #endif |
| 1265 | if (!via) |
| 1266 | return; |
| 1267 | |
| 1268 | spin_lock_irqsave(&pmu_lock, flags); |
| 1269 | pmu_suspended++; |
| 1270 | if (pmu_suspended > 1) { |
| 1271 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1272 | return; |
| 1273 | } |
| 1274 | |
| 1275 | do { |
| 1276 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1277 | if (req_awaiting_reply) |
| 1278 | adb_int_pending = 1; |
| 1279 | via_pmu_interrupt(0, NULL, NULL); |
| 1280 | spin_lock_irqsave(&pmu_lock, flags); |
| 1281 | if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) { |
| 1282 | #ifdef SUSPEND_USES_PMU |
| 1283 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0); |
| 1284 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1285 | while(!req.complete) |
| 1286 | pmu_poll(); |
| 1287 | #else /* SUSPEND_USES_PMU */ |
| 1288 | if (gpio_irq >= 0) |
| 1289 | disable_irq_nosync(gpio_irq); |
| 1290 | out_8(&via[IER], CB1_INT | IER_CLR); |
| 1291 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1292 | #endif /* SUSPEND_USES_PMU */ |
| 1293 | break; |
| 1294 | } |
| 1295 | } while (1); |
| 1296 | } |
| 1297 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1298 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | pmu_resume(void) |
| 1300 | { |
| 1301 | unsigned long flags; |
| 1302 | |
| 1303 | if (!via || (pmu_suspended < 1)) |
| 1304 | return; |
| 1305 | |
| 1306 | spin_lock_irqsave(&pmu_lock, flags); |
| 1307 | pmu_suspended--; |
| 1308 | if (pmu_suspended > 0) { |
| 1309 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1310 | return; |
| 1311 | } |
| 1312 | adb_int_pending = 1; |
| 1313 | #ifdef SUSPEND_USES_PMU |
| 1314 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask); |
| 1315 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1316 | while(!req.complete) |
| 1317 | pmu_poll(); |
| 1318 | #else /* SUSPEND_USES_PMU */ |
| 1319 | if (gpio_irq >= 0) |
| 1320 | enable_irq(gpio_irq); |
| 1321 | out_8(&via[IER], CB1_INT | IER_SET); |
| 1322 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1323 | pmu_poll(); |
| 1324 | #endif /* SUSPEND_USES_PMU */ |
| 1325 | } |
| 1326 | |
| 1327 | /* Interrupt data could be the result data from an ADB cmd */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1328 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs) |
| 1330 | { |
| 1331 | unsigned char ints, pirq; |
| 1332 | int i = 0; |
| 1333 | |
| 1334 | asleep = 0; |
| 1335 | if (drop_interrupts || len < 1) { |
| 1336 | adb_int_pending = 0; |
| 1337 | pmu_irq_stats[8]++; |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | /* Get PMU interrupt mask */ |
| 1342 | ints = data[0]; |
| 1343 | |
| 1344 | /* Record zero interrupts for stats */ |
| 1345 | if (ints == 0) |
| 1346 | pmu_irq_stats[9]++; |
| 1347 | |
| 1348 | /* Hack to deal with ADB autopoll flag */ |
| 1349 | if (ints & PMU_INT_ADB) |
| 1350 | ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL); |
| 1351 | |
| 1352 | next: |
| 1353 | |
| 1354 | if (ints == 0) { |
| 1355 | if (i > pmu_irq_stats[10]) |
| 1356 | pmu_irq_stats[10] = i; |
| 1357 | return; |
| 1358 | } |
| 1359 | |
| 1360 | for (pirq = 0; pirq < 8; pirq++) |
| 1361 | if (ints & (1 << pirq)) |
| 1362 | break; |
| 1363 | pmu_irq_stats[pirq]++; |
| 1364 | i++; |
| 1365 | ints &= ~(1 << pirq); |
| 1366 | |
| 1367 | /* Note: for some reason, we get an interrupt with len=1, |
| 1368 | * data[0]==0 after each normal ADB interrupt, at least |
| 1369 | * on the Pismo. Still investigating... --BenH |
| 1370 | */ |
| 1371 | if ((1 << pirq) & PMU_INT_ADB) { |
| 1372 | if ((data[0] & PMU_INT_ADB_AUTO) == 0) { |
| 1373 | struct adb_request *req = req_awaiting_reply; |
| 1374 | if (req == 0) { |
| 1375 | printk(KERN_ERR "PMU: extra ADB reply\n"); |
| 1376 | return; |
| 1377 | } |
| 1378 | req_awaiting_reply = NULL; |
| 1379 | if (len <= 2) |
| 1380 | req->reply_len = 0; |
| 1381 | else { |
| 1382 | memcpy(req->reply, data + 1, len - 1); |
| 1383 | req->reply_len = len - 1; |
| 1384 | } |
| 1385 | pmu_done(req); |
| 1386 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | if (len == 4 && data[1] == 0x2c) { |
| 1388 | extern int xmon_wants_key, xmon_adb_keycode; |
| 1389 | if (xmon_wants_key) { |
| 1390 | xmon_adb_keycode = data[2]; |
| 1391 | return; |
| 1392 | } |
| 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | #ifdef CONFIG_ADB |
| 1395 | /* |
| 1396 | * XXX On the [23]400 the PMU gives us an up |
| 1397 | * event for keycodes 0x74 or 0x75 when the PC |
| 1398 | * card eject buttons are released, so we |
| 1399 | * ignore those events. |
| 1400 | */ |
| 1401 | if (!(pmu_kind == PMU_OHARE_BASED && len == 4 |
| 1402 | && data[1] == 0x2c && data[3] == 0xff |
| 1403 | && (data[2] & ~1) == 0xf4)) |
| 1404 | adb_input(data+1, len-1, regs, 1); |
| 1405 | #endif /* CONFIG_ADB */ |
| 1406 | } |
| 1407 | } |
| 1408 | /* Sound/brightness button pressed */ |
| 1409 | else if ((1 << pirq) & PMU_INT_SNDBRT) { |
| 1410 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 1411 | if (len == 3) |
| 1412 | #ifdef CONFIG_INPUT_ADBHID |
| 1413 | if (!disable_kernel_backlight) |
| 1414 | #endif /* CONFIG_INPUT_ADBHID */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 1415 | pmac_backlight_set_legacy_brightness(data[1] >> 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | #endif /* CONFIG_PMAC_BACKLIGHT */ |
| 1417 | } |
| 1418 | /* Tick interrupt */ |
| 1419 | else if ((1 << pirq) & PMU_INT_TICK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | /* Environement or tick interrupt, query batteries */ |
| 1421 | if (pmu_battery_count) { |
| 1422 | if ((--query_batt_timer) == 0) { |
| 1423 | query_battery_state(); |
| 1424 | query_batt_timer = BATTERY_POLLING_COUNT; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | else if ((1 << pirq) & PMU_INT_ENVIRONMENT) { |
| 1429 | if (pmu_battery_count) |
| 1430 | query_battery_state(); |
| 1431 | pmu_pass_intr(data, len); |
Johannes Berg | 9e8e30a | 2006-06-26 01:49:55 -0400 | [diff] [blame] | 1432 | /* len == 6 is probably a bad check. But how do I |
| 1433 | * know what PMU versions send what events here? */ |
| 1434 | if (len == 6) { |
| 1435 | via_pmu_event(PMU_EVT_POWER, !!(data[1]&8)); |
| 1436 | via_pmu_event(PMU_EVT_LID, data[1]&1); |
| 1437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } else { |
| 1439 | pmu_pass_intr(data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | goto next; |
| 1442 | } |
| 1443 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1444 | static struct adb_request* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | pmu_sr_intr(struct pt_regs *regs) |
| 1446 | { |
| 1447 | struct adb_request *req; |
| 1448 | int bite = 0; |
| 1449 | |
| 1450 | if (via[B] & TREQ) { |
| 1451 | printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]); |
| 1452 | out_8(&via[IFR], SR_INT); |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | /* The ack may not yet be low when we get the interrupt */ |
| 1456 | while ((in_8(&via[B]) & TACK) != 0) |
| 1457 | ; |
| 1458 | |
| 1459 | /* if reading grab the byte, and reset the interrupt */ |
| 1460 | if (pmu_state == reading || pmu_state == reading_intr) |
| 1461 | bite = in_8(&via[SR]); |
| 1462 | |
| 1463 | /* reset TREQ and wait for TACK to go high */ |
| 1464 | out_8(&via[B], in_8(&via[B]) | TREQ); |
| 1465 | wait_for_ack(); |
| 1466 | |
| 1467 | switch (pmu_state) { |
| 1468 | case sending: |
| 1469 | req = current_req; |
| 1470 | if (data_len < 0) { |
| 1471 | data_len = req->nbytes - 1; |
| 1472 | send_byte(data_len); |
| 1473 | break; |
| 1474 | } |
| 1475 | if (data_index <= data_len) { |
| 1476 | send_byte(req->data[data_index++]); |
| 1477 | break; |
| 1478 | } |
| 1479 | req->sent = 1; |
| 1480 | data_len = pmu_data_len[req->data[0]][1]; |
| 1481 | if (data_len == 0) { |
| 1482 | pmu_state = idle; |
| 1483 | current_req = req->next; |
| 1484 | if (req->reply_expected) |
| 1485 | req_awaiting_reply = req; |
| 1486 | else |
| 1487 | return req; |
| 1488 | } else { |
| 1489 | pmu_state = reading; |
| 1490 | data_index = 0; |
| 1491 | reply_ptr = req->reply + req->reply_len; |
| 1492 | recv_byte(); |
| 1493 | } |
| 1494 | break; |
| 1495 | |
| 1496 | case intack: |
| 1497 | data_index = 0; |
| 1498 | data_len = -1; |
| 1499 | pmu_state = reading_intr; |
| 1500 | reply_ptr = interrupt_data[int_data_last]; |
| 1501 | recv_byte(); |
| 1502 | if (gpio_irq >= 0 && !gpio_irq_enabled) { |
| 1503 | enable_irq(gpio_irq); |
| 1504 | gpio_irq_enabled = 1; |
| 1505 | } |
| 1506 | break; |
| 1507 | |
| 1508 | case reading: |
| 1509 | case reading_intr: |
| 1510 | if (data_len == -1) { |
| 1511 | data_len = bite; |
| 1512 | if (bite > 32) |
| 1513 | printk(KERN_ERR "PMU: bad reply len %d\n", bite); |
| 1514 | } else if (data_index < 32) { |
| 1515 | reply_ptr[data_index++] = bite; |
| 1516 | } |
| 1517 | if (data_index < data_len) { |
| 1518 | recv_byte(); |
| 1519 | break; |
| 1520 | } |
| 1521 | |
| 1522 | if (pmu_state == reading_intr) { |
| 1523 | pmu_state = idle; |
| 1524 | int_data_state[int_data_last] = int_data_ready; |
| 1525 | interrupt_data_len[int_data_last] = data_len; |
| 1526 | } else { |
| 1527 | req = current_req; |
| 1528 | /* |
| 1529 | * For PMU sleep and freq change requests, we lock the |
| 1530 | * PMU until it's explicitely unlocked. This avoids any |
| 1531 | * spurrious event polling getting in |
| 1532 | */ |
| 1533 | current_req = req->next; |
| 1534 | req->reply_len += data_index; |
| 1535 | if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED) |
| 1536 | pmu_state = locked; |
| 1537 | else |
| 1538 | pmu_state = idle; |
| 1539 | return req; |
| 1540 | } |
| 1541 | break; |
| 1542 | |
| 1543 | default: |
| 1544 | printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n", |
| 1545 | pmu_state); |
| 1546 | } |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1550 | static irqreturn_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs) |
| 1552 | { |
| 1553 | unsigned long flags; |
| 1554 | int intr; |
| 1555 | int nloop = 0; |
| 1556 | int int_data = -1; |
| 1557 | struct adb_request *req = NULL; |
| 1558 | int handled = 0; |
| 1559 | |
| 1560 | /* This is a bit brutal, we can probably do better */ |
| 1561 | spin_lock_irqsave(&pmu_lock, flags); |
| 1562 | ++disable_poll; |
| 1563 | |
| 1564 | for (;;) { |
| 1565 | intr = in_8(&via[IFR]) & (SR_INT | CB1_INT); |
| 1566 | if (intr == 0) |
| 1567 | break; |
| 1568 | handled = 1; |
| 1569 | if (++nloop > 1000) { |
| 1570 | printk(KERN_DEBUG "PMU: stuck in intr loop, " |
| 1571 | "intr=%x, ier=%x pmu_state=%d\n", |
| 1572 | intr, in_8(&via[IER]), pmu_state); |
| 1573 | break; |
| 1574 | } |
| 1575 | out_8(&via[IFR], intr); |
| 1576 | if (intr & CB1_INT) { |
| 1577 | adb_int_pending = 1; |
| 1578 | pmu_irq_stats[0]++; |
| 1579 | } |
| 1580 | if (intr & SR_INT) { |
| 1581 | req = pmu_sr_intr(regs); |
| 1582 | if (req) |
| 1583 | break; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | recheck: |
| 1588 | if (pmu_state == idle) { |
| 1589 | if (adb_int_pending) { |
| 1590 | if (int_data_state[0] == int_data_empty) |
| 1591 | int_data_last = 0; |
| 1592 | else if (int_data_state[1] == int_data_empty) |
| 1593 | int_data_last = 1; |
| 1594 | else |
| 1595 | goto no_free_slot; |
| 1596 | pmu_state = intack; |
| 1597 | int_data_state[int_data_last] = int_data_fill; |
| 1598 | /* Sounds safer to make sure ACK is high before writing. |
| 1599 | * This helped kill a problem with ADB and some iBooks |
| 1600 | */ |
| 1601 | wait_for_ack(); |
| 1602 | send_byte(PMU_INT_ACK); |
| 1603 | adb_int_pending = 0; |
| 1604 | } else if (current_req) |
| 1605 | pmu_start(); |
| 1606 | } |
| 1607 | no_free_slot: |
| 1608 | /* Mark the oldest buffer for flushing */ |
| 1609 | if (int_data_state[!int_data_last] == int_data_ready) { |
| 1610 | int_data_state[!int_data_last] = int_data_flush; |
| 1611 | int_data = !int_data_last; |
| 1612 | } else if (int_data_state[int_data_last] == int_data_ready) { |
| 1613 | int_data_state[int_data_last] = int_data_flush; |
| 1614 | int_data = int_data_last; |
| 1615 | } |
| 1616 | --disable_poll; |
| 1617 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1618 | |
| 1619 | /* Deal with completed PMU requests outside of the lock */ |
| 1620 | if (req) { |
| 1621 | pmu_done(req); |
| 1622 | req = NULL; |
| 1623 | } |
| 1624 | |
| 1625 | /* Deal with interrupt datas outside of the lock */ |
| 1626 | if (int_data >= 0) { |
| 1627 | pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs); |
| 1628 | spin_lock_irqsave(&pmu_lock, flags); |
| 1629 | ++disable_poll; |
| 1630 | int_data_state[int_data] = int_data_empty; |
| 1631 | int_data = -1; |
| 1632 | goto recheck; |
| 1633 | } |
| 1634 | |
| 1635 | return IRQ_RETVAL(handled); |
| 1636 | } |
| 1637 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1638 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | pmu_unlock(void) |
| 1640 | { |
| 1641 | unsigned long flags; |
| 1642 | |
| 1643 | spin_lock_irqsave(&pmu_lock, flags); |
| 1644 | if (pmu_state == locked) |
| 1645 | pmu_state = idle; |
| 1646 | adb_int_pending = 1; |
| 1647 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1648 | } |
| 1649 | |
| 1650 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1651 | static irqreturn_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | gpio1_interrupt(int irq, void *arg, struct pt_regs *regs) |
| 1653 | { |
| 1654 | unsigned long flags; |
| 1655 | |
| 1656 | if ((in_8(gpio_reg + 0x9) & 0x02) == 0) { |
| 1657 | spin_lock_irqsave(&pmu_lock, flags); |
| 1658 | if (gpio_irq_enabled > 0) { |
| 1659 | disable_irq_nosync(gpio_irq); |
| 1660 | gpio_irq_enabled = 0; |
| 1661 | } |
| 1662 | pmu_irq_stats[1]++; |
| 1663 | adb_int_pending = 1; |
| 1664 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 1665 | via_pmu_interrupt(0, NULL, NULL); |
| 1666 | return IRQ_HANDLED; |
| 1667 | } |
| 1668 | return IRQ_NONE; |
| 1669 | } |
| 1670 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1671 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | pmu_enable_irled(int on) |
| 1673 | { |
| 1674 | struct adb_request req; |
| 1675 | |
| 1676 | if (vias == NULL) |
| 1677 | return ; |
| 1678 | if (pmu_kind == PMU_KEYLARGO_BASED) |
| 1679 | return ; |
| 1680 | |
| 1681 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED | |
| 1682 | (on ? PMU_POW_ON : PMU_POW_OFF)); |
| 1683 | pmu_wait_complete(&req); |
| 1684 | } |
| 1685 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1686 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | pmu_restart(void) |
| 1688 | { |
| 1689 | struct adb_request req; |
| 1690 | |
| 1691 | if (via == NULL) |
| 1692 | return; |
| 1693 | |
| 1694 | local_irq_disable(); |
| 1695 | |
| 1696 | drop_interrupts = 1; |
| 1697 | |
| 1698 | if (pmu_kind != PMU_KEYLARGO_BASED) { |
| 1699 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB | |
| 1700 | PMU_INT_TICK ); |
| 1701 | while(!req.complete) |
| 1702 | pmu_poll(); |
| 1703 | } |
| 1704 | |
| 1705 | pmu_request(&req, NULL, 1, PMU_RESET); |
| 1706 | pmu_wait_complete(&req); |
| 1707 | for (;;) |
| 1708 | ; |
| 1709 | } |
| 1710 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1711 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | pmu_shutdown(void) |
| 1713 | { |
| 1714 | struct adb_request req; |
| 1715 | |
| 1716 | if (via == NULL) |
| 1717 | return; |
| 1718 | |
| 1719 | local_irq_disable(); |
| 1720 | |
| 1721 | drop_interrupts = 1; |
| 1722 | |
| 1723 | if (pmu_kind != PMU_KEYLARGO_BASED) { |
| 1724 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB | |
| 1725 | PMU_INT_TICK ); |
| 1726 | pmu_wait_complete(&req); |
| 1727 | } else { |
| 1728 | /* Disable server mode on shutdown or we'll just |
| 1729 | * wake up again |
| 1730 | */ |
| 1731 | pmu_set_server_mode(0); |
| 1732 | } |
| 1733 | |
| 1734 | pmu_request(&req, NULL, 5, PMU_SHUTDOWN, |
| 1735 | 'M', 'A', 'T', 'T'); |
| 1736 | pmu_wait_complete(&req); |
| 1737 | for (;;) |
| 1738 | ; |
| 1739 | } |
| 1740 | |
| 1741 | int |
| 1742 | pmu_present(void) |
| 1743 | { |
| 1744 | return via != 0; |
| 1745 | } |
| 1746 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 1747 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
| 1749 | static LIST_HEAD(sleep_notifiers); |
| 1750 | |
| 1751 | int |
| 1752 | pmu_register_sleep_notifier(struct pmu_sleep_notifier *n) |
| 1753 | { |
| 1754 | struct list_head *list; |
| 1755 | struct pmu_sleep_notifier *notifier; |
| 1756 | |
| 1757 | for (list = sleep_notifiers.next; list != &sleep_notifiers; |
| 1758 | list = list->next) { |
| 1759 | notifier = list_entry(list, struct pmu_sleep_notifier, list); |
| 1760 | if (n->priority > notifier->priority) |
| 1761 | break; |
| 1762 | } |
| 1763 | __list_add(&n->list, list->prev, list); |
| 1764 | return 0; |
| 1765 | } |
Paul Mackerras | 3fb62b5 | 2005-11-08 12:14:50 +1100 | [diff] [blame] | 1766 | EXPORT_SYMBOL(pmu_register_sleep_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
| 1768 | int |
| 1769 | pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n) |
| 1770 | { |
| 1771 | if (n->list.next == 0) |
| 1772 | return -ENOENT; |
| 1773 | list_del(&n->list); |
| 1774 | n->list.next = NULL; |
| 1775 | return 0; |
| 1776 | } |
Paul Mackerras | 3fb62b5 | 2005-11-08 12:14:50 +1100 | [diff] [blame] | 1777 | EXPORT_SYMBOL(pmu_unregister_sleep_notifier); |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 1778 | #endif /* CONFIG_PM */ |
| 1779 | |
| 1780 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
| 1782 | /* Sleep is broadcast last-to-first */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1783 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | broadcast_sleep(int when, int fallback) |
| 1785 | { |
| 1786 | int ret = PBOOK_SLEEP_OK; |
| 1787 | struct list_head *list; |
| 1788 | struct pmu_sleep_notifier *notifier; |
| 1789 | |
| 1790 | for (list = sleep_notifiers.prev; list != &sleep_notifiers; |
| 1791 | list = list->prev) { |
| 1792 | notifier = list_entry(list, struct pmu_sleep_notifier, list); |
| 1793 | ret = notifier->notifier_call(notifier, when); |
| 1794 | if (ret != PBOOK_SLEEP_OK) { |
| 1795 | printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n", |
| 1796 | when, notifier, notifier->notifier_call); |
| 1797 | for (; list != &sleep_notifiers; list = list->next) { |
| 1798 | notifier = list_entry(list, struct pmu_sleep_notifier, list); |
| 1799 | notifier->notifier_call(notifier, fallback); |
| 1800 | } |
| 1801 | return ret; |
| 1802 | } |
| 1803 | } |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
| 1807 | /* Wake is broadcast first-to-last */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1808 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | broadcast_wake(void) |
| 1810 | { |
| 1811 | int ret = PBOOK_SLEEP_OK; |
| 1812 | struct list_head *list; |
| 1813 | struct pmu_sleep_notifier *notifier; |
| 1814 | |
| 1815 | for (list = sleep_notifiers.next; list != &sleep_notifiers; |
| 1816 | list = list->next) { |
| 1817 | notifier = list_entry(list, struct pmu_sleep_notifier, list); |
| 1818 | notifier->notifier_call(notifier, PBOOK_WAKE); |
| 1819 | } |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * This struct is used to store config register values for |
| 1825 | * PCI devices which may get powered off when we sleep. |
| 1826 | */ |
| 1827 | static struct pci_save { |
| 1828 | #ifndef HACKED_PCI_SAVE |
| 1829 | u16 command; |
| 1830 | u16 cache_lat; |
| 1831 | u16 intr; |
| 1832 | u32 rom_address; |
| 1833 | #else |
| 1834 | u32 config[16]; |
| 1835 | #endif |
| 1836 | } *pbook_pci_saves; |
| 1837 | static int pbook_npci_saves; |
| 1838 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1839 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | pbook_alloc_pci_save(void) |
| 1841 | { |
| 1842 | int npci; |
| 1843 | struct pci_dev *pd = NULL; |
| 1844 | |
| 1845 | npci = 0; |
| 1846 | while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) { |
| 1847 | ++npci; |
| 1848 | } |
| 1849 | if (npci == 0) |
| 1850 | return; |
| 1851 | pbook_pci_saves = (struct pci_save *) |
| 1852 | kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL); |
| 1853 | pbook_npci_saves = npci; |
| 1854 | } |
| 1855 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1856 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | pbook_free_pci_save(void) |
| 1858 | { |
| 1859 | if (pbook_pci_saves == NULL) |
| 1860 | return; |
| 1861 | kfree(pbook_pci_saves); |
| 1862 | pbook_pci_saves = NULL; |
| 1863 | pbook_npci_saves = 0; |
| 1864 | } |
| 1865 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1866 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | pbook_pci_save(void) |
| 1868 | { |
| 1869 | struct pci_save *ps = pbook_pci_saves; |
| 1870 | struct pci_dev *pd = NULL; |
| 1871 | int npci = pbook_npci_saves; |
| 1872 | |
| 1873 | if (ps == NULL) |
| 1874 | return; |
| 1875 | |
| 1876 | while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) { |
| 1877 | if (npci-- == 0) |
| 1878 | return; |
| 1879 | #ifndef HACKED_PCI_SAVE |
| 1880 | pci_read_config_word(pd, PCI_COMMAND, &ps->command); |
| 1881 | pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat); |
| 1882 | pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr); |
| 1883 | pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address); |
| 1884 | #else |
| 1885 | int i; |
| 1886 | for (i=1;i<16;i++) |
| 1887 | pci_read_config_dword(pd, i<<4, &ps->config[i]); |
| 1888 | #endif |
| 1889 | ++ps; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* For this to work, we must take care of a few things: If gmac was enabled |
| 1894 | * during boot, it will be in the pci dev list. If it's disabled at this point |
| 1895 | * (and it will probably be), then you can't access it's config space. |
| 1896 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1897 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | pbook_pci_restore(void) |
| 1899 | { |
| 1900 | u16 cmd; |
| 1901 | struct pci_save *ps = pbook_pci_saves - 1; |
| 1902 | struct pci_dev *pd = NULL; |
| 1903 | int npci = pbook_npci_saves; |
| 1904 | int j; |
| 1905 | |
| 1906 | while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) { |
| 1907 | #ifdef HACKED_PCI_SAVE |
| 1908 | int i; |
| 1909 | if (npci-- == 0) |
| 1910 | return; |
| 1911 | ps++; |
| 1912 | for (i=2;i<16;i++) |
| 1913 | pci_write_config_dword(pd, i<<4, ps->config[i]); |
| 1914 | pci_write_config_dword(pd, 4, ps->config[1]); |
| 1915 | #else |
| 1916 | if (npci-- == 0) |
| 1917 | return; |
| 1918 | ps++; |
| 1919 | if (ps->command == 0) |
| 1920 | continue; |
| 1921 | pci_read_config_word(pd, PCI_COMMAND, &cmd); |
| 1922 | if ((ps->command & ~cmd) == 0) |
| 1923 | continue; |
| 1924 | switch (pd->hdr_type) { |
| 1925 | case PCI_HEADER_TYPE_NORMAL: |
| 1926 | for (j = 0; j < 6; ++j) |
| 1927 | pci_write_config_dword(pd, |
| 1928 | PCI_BASE_ADDRESS_0 + j*4, |
| 1929 | pd->resource[j].start); |
| 1930 | pci_write_config_dword(pd, PCI_ROM_ADDRESS, |
| 1931 | ps->rom_address); |
| 1932 | pci_write_config_word(pd, PCI_CACHE_LINE_SIZE, |
| 1933 | ps->cache_lat); |
| 1934 | pci_write_config_word(pd, PCI_INTERRUPT_LINE, |
| 1935 | ps->intr); |
| 1936 | pci_write_config_word(pd, PCI_COMMAND, ps->command); |
| 1937 | break; |
| 1938 | } |
| 1939 | #endif |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | #ifdef DEBUG_SLEEP |
| 1944 | /* N.B. This doesn't work on the 3400 */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1945 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | pmu_blink(int n) |
| 1947 | { |
| 1948 | struct adb_request req; |
| 1949 | |
| 1950 | memset(&req, 0, sizeof(req)); |
| 1951 | |
| 1952 | for (; n > 0; --n) { |
| 1953 | req.nbytes = 4; |
| 1954 | req.done = NULL; |
| 1955 | req.data[0] = 0xee; |
| 1956 | req.data[1] = 4; |
| 1957 | req.data[2] = 0; |
| 1958 | req.data[3] = 1; |
| 1959 | req.reply[0] = ADB_RET_OK; |
| 1960 | req.reply_len = 1; |
| 1961 | req.reply_expected = 0; |
| 1962 | pmu_polled_request(&req); |
| 1963 | mdelay(50); |
| 1964 | req.nbytes = 4; |
| 1965 | req.done = NULL; |
| 1966 | req.data[0] = 0xee; |
| 1967 | req.data[1] = 4; |
| 1968 | req.data[2] = 0; |
| 1969 | req.data[3] = 0; |
| 1970 | req.reply[0] = ADB_RET_OK; |
| 1971 | req.reply_len = 1; |
| 1972 | req.reply_expected = 0; |
| 1973 | pmu_polled_request(&req); |
| 1974 | mdelay(50); |
| 1975 | } |
| 1976 | mdelay(50); |
| 1977 | } |
| 1978 | #endif |
| 1979 | |
| 1980 | /* |
| 1981 | * Put the powerbook to sleep. |
| 1982 | */ |
| 1983 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1984 | static u32 save_via[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1986 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | save_via_state(void) |
| 1988 | { |
| 1989 | save_via[0] = in_8(&via[ANH]); |
| 1990 | save_via[1] = in_8(&via[DIRA]); |
| 1991 | save_via[2] = in_8(&via[B]); |
| 1992 | save_via[3] = in_8(&via[DIRB]); |
| 1993 | save_via[4] = in_8(&via[PCR]); |
| 1994 | save_via[5] = in_8(&via[ACR]); |
| 1995 | save_via[6] = in_8(&via[T1CL]); |
| 1996 | save_via[7] = in_8(&via[T1CH]); |
| 1997 | } |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 1998 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | restore_via_state(void) |
| 2000 | { |
| 2001 | out_8(&via[ANH], save_via[0]); |
| 2002 | out_8(&via[DIRA], save_via[1]); |
| 2003 | out_8(&via[B], save_via[2]); |
| 2004 | out_8(&via[DIRB], save_via[3]); |
| 2005 | out_8(&via[PCR], save_via[4]); |
| 2006 | out_8(&via[ACR], save_via[5]); |
| 2007 | out_8(&via[T1CL], save_via[6]); |
| 2008 | out_8(&via[T1CH], save_via[7]); |
| 2009 | out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */ |
| 2010 | out_8(&via[IFR], 0x7f); /* clear IFR */ |
| 2011 | out_8(&via[IER], IER_SET | SR_INT | CB1_INT); |
| 2012 | } |
| 2013 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2014 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | pmac_suspend_devices(void) |
| 2016 | { |
| 2017 | int ret; |
| 2018 | |
| 2019 | pm_prepare_console(); |
| 2020 | |
| 2021 | /* Notify old-style device drivers & userland */ |
| 2022 | ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT); |
| 2023 | if (ret != PBOOK_SLEEP_OK) { |
| 2024 | printk(KERN_ERR "Sleep rejected by drivers\n"); |
| 2025 | return -EBUSY; |
| 2026 | } |
| 2027 | |
| 2028 | /* Sync the disks. */ |
| 2029 | /* XXX It would be nice to have some way to ensure that |
| 2030 | * nobody is dirtying any new buffers while we wait. That |
| 2031 | * could be achieved using the refrigerator for processes |
| 2032 | * that swsusp uses |
| 2033 | */ |
| 2034 | sys_sync(); |
| 2035 | |
| 2036 | /* Sleep can fail now. May not be very robust but useful for debugging */ |
| 2037 | ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE); |
| 2038 | if (ret != PBOOK_SLEEP_OK) { |
| 2039 | printk(KERN_ERR "Driver sleep failed\n"); |
| 2040 | return -EBUSY; |
| 2041 | } |
| 2042 | |
| 2043 | /* Send suspend call to devices, hold the device core's dpm_sem */ |
| 2044 | ret = device_suspend(PMSG_SUSPEND); |
| 2045 | if (ret) { |
| 2046 | broadcast_wake(); |
| 2047 | printk(KERN_ERR "Driver sleep failed\n"); |
| 2048 | return -EBUSY; |
| 2049 | } |
| 2050 | |
Benjamin Herrenschmidt | 5b9ca52 | 2006-01-07 11:41:02 +1100 | [diff] [blame] | 2051 | /* Call platform functions marked "on sleep" */ |
| 2052 | pmac_pfunc_i2c_suspend(); |
| 2053 | pmac_pfunc_base_suspend(); |
| 2054 | |
Benjamin Herrenschmidt | e521dca | 2005-05-02 16:12:00 +1000 | [diff] [blame] | 2055 | /* Stop preemption */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | preempt_disable(); |
| 2057 | |
| 2058 | /* Make sure the decrementer won't interrupt us */ |
| 2059 | asm volatile("mtdec %0" : : "r" (0x7fffffff)); |
| 2060 | /* Make sure any pending DEC interrupt occurring while we did |
| 2061 | * the above didn't re-enable the DEC */ |
| 2062 | mb(); |
| 2063 | asm volatile("mtdec %0" : : "r" (0x7fffffff)); |
| 2064 | |
| 2065 | /* We can now disable MSR_EE. This code of course works properly only |
| 2066 | * on UP machines... For SMP, if we ever implement sleep, we'll have to |
| 2067 | * stop the "other" CPUs way before we do all that stuff. |
| 2068 | */ |
| 2069 | local_irq_disable(); |
| 2070 | |
| 2071 | /* Broadcast power down irq |
| 2072 | * This isn't that useful in most cases (only directly wired devices can |
| 2073 | * use this but still... This will take care of sysdev's as well, so |
| 2074 | * we exit from here with local irqs disabled and PIC off. |
| 2075 | */ |
Pavel Machek | bf2049f | 2005-04-16 15:25:38 -0700 | [diff] [blame] | 2076 | ret = device_power_down(PMSG_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | if (ret) { |
| 2078 | wakeup_decrementer(); |
| 2079 | local_irq_enable(); |
| 2080 | preempt_enable(); |
| 2081 | device_resume(); |
| 2082 | broadcast_wake(); |
| 2083 | printk(KERN_ERR "Driver powerdown failed\n"); |
| 2084 | return -EBUSY; |
| 2085 | } |
| 2086 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2087 | /* Wait for completion of async requests */ |
| 2088 | while (!batt_req.complete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | pmu_poll(); |
| 2090 | |
| 2091 | /* Giveup the lazy FPU & vec so we don't have to back them |
| 2092 | * up from the low level code |
| 2093 | */ |
| 2094 | enable_kernel_fp(); |
| 2095 | |
| 2096 | #ifdef CONFIG_ALTIVEC |
| 2097 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 2098 | enable_kernel_altivec(); |
| 2099 | #endif /* CONFIG_ALTIVEC */ |
| 2100 | |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2104 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | pmac_wakeup_devices(void) |
| 2106 | { |
| 2107 | mdelay(100); |
| 2108 | |
| 2109 | /* Power back up system devices (including the PIC) */ |
| 2110 | device_power_up(); |
| 2111 | |
| 2112 | /* Force a poll of ADB interrupts */ |
| 2113 | adb_int_pending = 1; |
| 2114 | via_pmu_interrupt(0, NULL, NULL); |
| 2115 | |
| 2116 | /* Restart jiffies & scheduling */ |
| 2117 | wakeup_decrementer(); |
| 2118 | |
| 2119 | /* Re-enable local CPU interrupts */ |
| 2120 | local_irq_enable(); |
Benjamin Herrenschmidt | b16eeb4 | 2005-05-27 12:53:02 -0700 | [diff] [blame] | 2121 | mdelay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | preempt_enable(); |
| 2123 | |
Benjamin Herrenschmidt | 5b9ca52 | 2006-01-07 11:41:02 +1100 | [diff] [blame] | 2124 | /* Call platform functions marked "on wake" */ |
| 2125 | pmac_pfunc_base_resume(); |
| 2126 | pmac_pfunc_i2c_resume(); |
| 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /* Resume devices */ |
| 2129 | device_resume(); |
| 2130 | |
| 2131 | /* Notify old style drivers */ |
| 2132 | broadcast_wake(); |
| 2133 | |
| 2134 | pm_restore_console(); |
| 2135 | |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | #define GRACKLE_PM (1<<7) |
| 2140 | #define GRACKLE_DOZE (1<<5) |
| 2141 | #define GRACKLE_NAP (1<<4) |
| 2142 | #define GRACKLE_SLEEP (1<<3) |
| 2143 | |
Olaf Hering | 3bea631 | 2006-03-21 23:00:05 -0800 | [diff] [blame] | 2144 | static int powerbook_sleep_grackle(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | { |
| 2146 | unsigned long save_l2cr; |
| 2147 | unsigned short pmcr1; |
| 2148 | struct adb_request req; |
| 2149 | int ret; |
| 2150 | struct pci_dev *grackle; |
| 2151 | |
| 2152 | grackle = pci_find_slot(0, 0); |
| 2153 | if (!grackle) |
| 2154 | return -ENODEV; |
| 2155 | |
| 2156 | ret = pmac_suspend_devices(); |
| 2157 | if (ret) { |
| 2158 | printk(KERN_ERR "Sleep rejected by devices\n"); |
| 2159 | return ret; |
| 2160 | } |
| 2161 | |
| 2162 | /* Turn off various things. Darwin does some retry tests here... */ |
| 2163 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE); |
| 2164 | pmu_wait_complete(&req); |
| 2165 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 2166 | PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY); |
| 2167 | pmu_wait_complete(&req); |
| 2168 | |
| 2169 | /* For 750, save backside cache setting and disable it */ |
| 2170 | save_l2cr = _get_L2CR(); /* (returns -1 if not available) */ |
| 2171 | |
| 2172 | if (!__fake_sleep) { |
| 2173 | /* Ask the PMU to put us to sleep */ |
| 2174 | pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T'); |
| 2175 | pmu_wait_complete(&req); |
| 2176 | } |
| 2177 | |
| 2178 | /* The VIA is supposed not to be restored correctly*/ |
| 2179 | save_via_state(); |
| 2180 | /* We shut down some HW */ |
| 2181 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1); |
| 2182 | |
| 2183 | pci_read_config_word(grackle, 0x70, &pmcr1); |
| 2184 | /* Apparently, MacOS uses NAP mode for Grackle ??? */ |
| 2185 | pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP); |
| 2186 | pmcr1 |= GRACKLE_PM|GRACKLE_NAP; |
| 2187 | pci_write_config_word(grackle, 0x70, pmcr1); |
| 2188 | |
| 2189 | /* Call low-level ASM sleep handler */ |
| 2190 | if (__fake_sleep) |
| 2191 | mdelay(5000); |
| 2192 | else |
| 2193 | low_sleep_handler(); |
| 2194 | |
| 2195 | /* We're awake again, stop grackle PM */ |
| 2196 | pci_read_config_word(grackle, 0x70, &pmcr1); |
| 2197 | pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP); |
| 2198 | pci_write_config_word(grackle, 0x70, pmcr1); |
| 2199 | |
| 2200 | /* Make sure the PMU is idle */ |
| 2201 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0); |
| 2202 | restore_via_state(); |
| 2203 | |
| 2204 | /* Restore L2 cache */ |
| 2205 | if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0) |
| 2206 | _set_L2CR(save_l2cr); |
| 2207 | |
| 2208 | /* Restore userland MMU context */ |
Paul Mackerras | 6218a76 | 2006-06-11 14:15:17 +1000 | [diff] [blame] | 2209 | set_context(current->active_mm->context.id, current->active_mm->pgd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
| 2211 | /* Power things up */ |
| 2212 | pmu_unlock(); |
| 2213 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask); |
| 2214 | pmu_wait_complete(&req); |
| 2215 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, |
| 2216 | PMU_POW0_ON|PMU_POW0_HARD_DRIVE); |
| 2217 | pmu_wait_complete(&req); |
| 2218 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 2219 | PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY); |
| 2220 | pmu_wait_complete(&req); |
| 2221 | |
| 2222 | pmac_wakeup_devices(); |
| 2223 | |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2227 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | powerbook_sleep_Core99(void) |
| 2229 | { |
| 2230 | unsigned long save_l2cr; |
| 2231 | unsigned long save_l3cr; |
| 2232 | struct adb_request req; |
| 2233 | int ret; |
| 2234 | |
| 2235 | if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) { |
| 2236 | printk(KERN_ERR "Sleep mode not supported on this machine\n"); |
| 2237 | return -ENOSYS; |
| 2238 | } |
| 2239 | |
| 2240 | if (num_online_cpus() > 1 || cpu_is_offline(0)) |
| 2241 | return -EAGAIN; |
| 2242 | |
| 2243 | ret = pmac_suspend_devices(); |
| 2244 | if (ret) { |
| 2245 | printk(KERN_ERR "Sleep rejected by devices\n"); |
| 2246 | return ret; |
| 2247 | } |
| 2248 | |
Benjamin Herrenschmidt | b16eeb4 | 2005-05-27 12:53:02 -0700 | [diff] [blame] | 2249 | /* Stop environment and ADB interrupts */ |
| 2250 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0); |
| 2251 | pmu_wait_complete(&req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
| 2253 | /* Tell PMU what events will wake us up */ |
| 2254 | pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS, |
| 2255 | 0xff, 0xff); |
| 2256 | pmu_wait_complete(&req); |
| 2257 | pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS, |
| 2258 | 0, PMU_PWR_WAKEUP_KEY | |
| 2259 | (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0)); |
| 2260 | pmu_wait_complete(&req); |
| 2261 | |
| 2262 | /* Save the state of the L2 and L3 caches */ |
| 2263 | save_l3cr = _get_L3CR(); /* (returns -1 if not available) */ |
| 2264 | save_l2cr = _get_L2CR(); /* (returns -1 if not available) */ |
| 2265 | |
| 2266 | if (!__fake_sleep) { |
| 2267 | /* Ask the PMU to put us to sleep */ |
| 2268 | pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T'); |
| 2269 | pmu_wait_complete(&req); |
| 2270 | } |
| 2271 | |
| 2272 | /* The VIA is supposed not to be restored correctly*/ |
| 2273 | save_via_state(); |
| 2274 | |
| 2275 | /* Shut down various ASICs. There's a chance that we can no longer |
| 2276 | * talk to the PMU after this, so I moved it to _after_ sending the |
| 2277 | * sleep command to it. Still need to be checked. |
| 2278 | */ |
| 2279 | pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1); |
| 2280 | |
| 2281 | /* Call low-level ASM sleep handler */ |
| 2282 | if (__fake_sleep) |
| 2283 | mdelay(5000); |
| 2284 | else |
| 2285 | low_sleep_handler(); |
| 2286 | |
| 2287 | /* Restore Apple core ASICs state */ |
| 2288 | pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0); |
| 2289 | |
| 2290 | /* Restore VIA */ |
| 2291 | restore_via_state(); |
| 2292 | |
Benjamin Herrenschmidt | 0086b5e | 2005-06-10 14:19:02 +1000 | [diff] [blame] | 2293 | /* tweak LPJ before cpufreq is there */ |
| 2294 | loops_per_jiffy *= 2; |
| 2295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | /* Restore video */ |
| 2297 | pmac_call_early_video_resume(); |
| 2298 | |
| 2299 | /* Restore L2 cache */ |
| 2300 | if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0) |
| 2301 | _set_L2CR(save_l2cr); |
| 2302 | /* Restore L3 cache */ |
| 2303 | if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0) |
| 2304 | _set_L3CR(save_l3cr); |
| 2305 | |
| 2306 | /* Restore userland MMU context */ |
Paul Mackerras | 6218a76 | 2006-06-11 14:15:17 +1000 | [diff] [blame] | 2307 | set_context(current->active_mm->context.id, current->active_mm->pgd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | |
| 2309 | /* Tell PMU we are ready */ |
| 2310 | pmu_unlock(); |
| 2311 | pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2); |
| 2312 | pmu_wait_complete(&req); |
| 2313 | pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask); |
| 2314 | pmu_wait_complete(&req); |
| 2315 | |
Benjamin Herrenschmidt | 0086b5e | 2005-06-10 14:19:02 +1000 | [diff] [blame] | 2316 | /* Restore LPJ, cpufreq will adjust the cpu frequency */ |
| 2317 | loops_per_jiffy /= 2; |
| 2318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | pmac_wakeup_devices(); |
| 2320 | |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | #define PB3400_MEM_CTRL 0xf8000000 |
| 2325 | #define PB3400_MEM_CTRL_SLEEP 0x70 |
| 2326 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2327 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | powerbook_sleep_3400(void) |
| 2329 | { |
| 2330 | int ret, i, x; |
| 2331 | unsigned int hid0; |
| 2332 | unsigned long p; |
| 2333 | struct adb_request sleep_req; |
| 2334 | void __iomem *mem_ctrl; |
| 2335 | unsigned int __iomem *mem_ctrl_sleep; |
| 2336 | |
| 2337 | /* first map in the memory controller registers */ |
| 2338 | mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100); |
| 2339 | if (mem_ctrl == NULL) { |
| 2340 | printk("powerbook_sleep_3400: ioremap failed\n"); |
| 2341 | return -ENOMEM; |
| 2342 | } |
| 2343 | mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP; |
| 2344 | |
| 2345 | /* Allocate room for PCI save */ |
| 2346 | pbook_alloc_pci_save(); |
| 2347 | |
| 2348 | ret = pmac_suspend_devices(); |
| 2349 | if (ret) { |
| 2350 | pbook_free_pci_save(); |
| 2351 | printk(KERN_ERR "Sleep rejected by devices\n"); |
| 2352 | return ret; |
| 2353 | } |
| 2354 | |
| 2355 | /* Save the state of PCI config space for some slots */ |
| 2356 | pbook_pci_save(); |
| 2357 | |
| 2358 | /* Set the memory controller to keep the memory refreshed |
| 2359 | while we're asleep */ |
| 2360 | for (i = 0x403f; i >= 0x4000; --i) { |
| 2361 | out_be32(mem_ctrl_sleep, i); |
| 2362 | do { |
| 2363 | x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff; |
| 2364 | } while (x == 0); |
| 2365 | if (x >= 0x100) |
| 2366 | break; |
| 2367 | } |
| 2368 | |
| 2369 | /* Ask the PMU to put us to sleep */ |
| 2370 | pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T'); |
| 2371 | while (!sleep_req.complete) |
| 2372 | mb(); |
| 2373 | |
| 2374 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1); |
| 2375 | |
| 2376 | /* displacement-flush the L2 cache - necessary? */ |
| 2377 | for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000) |
| 2378 | i = *(volatile int *)p; |
| 2379 | asleep = 1; |
| 2380 | |
| 2381 | /* Put the CPU into sleep mode */ |
Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 2382 | hid0 = mfspr(SPRN_HID0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP; |
Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 2384 | mtspr(SPRN_HID0, hid0); |
| 2385 | mtmsr(mfmsr() | MSR_POW | MSR_EE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | udelay(10); |
| 2387 | |
| 2388 | /* OK, we're awake again, start restoring things */ |
| 2389 | out_be32(mem_ctrl_sleep, 0x3f); |
| 2390 | pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0); |
| 2391 | pbook_pci_restore(); |
| 2392 | pmu_unlock(); |
| 2393 | |
| 2394 | /* wait for the PMU interrupt sequence to complete */ |
| 2395 | while (asleep) |
| 2396 | mb(); |
| 2397 | |
| 2398 | pmac_wakeup_devices(); |
| 2399 | pbook_free_pci_save(); |
| 2400 | iounmap(mem_ctrl); |
| 2401 | |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2405 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | /* |
| 2408 | * Support for /dev/pmu device |
| 2409 | */ |
| 2410 | #define RB_SIZE 0x10 |
| 2411 | struct pmu_private { |
| 2412 | struct list_head list; |
| 2413 | int rb_get; |
| 2414 | int rb_put; |
| 2415 | struct rb_entry { |
| 2416 | unsigned short len; |
| 2417 | unsigned char data[16]; |
| 2418 | } rb_buf[RB_SIZE]; |
| 2419 | wait_queue_head_t wait; |
| 2420 | spinlock_t lock; |
| 2421 | #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) |
| 2422 | int backlight_locker; |
| 2423 | #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */ |
| 2424 | }; |
| 2425 | |
| 2426 | static LIST_HEAD(all_pmu_pvt); |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2427 | static DEFINE_SPINLOCK(all_pvt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2429 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | pmu_pass_intr(unsigned char *data, int len) |
| 2431 | { |
| 2432 | struct pmu_private *pp; |
| 2433 | struct list_head *list; |
| 2434 | int i; |
| 2435 | unsigned long flags; |
| 2436 | |
| 2437 | if (len > sizeof(pp->rb_buf[0].data)) |
| 2438 | len = sizeof(pp->rb_buf[0].data); |
| 2439 | spin_lock_irqsave(&all_pvt_lock, flags); |
| 2440 | for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) { |
| 2441 | pp = list_entry(list, struct pmu_private, list); |
| 2442 | spin_lock(&pp->lock); |
| 2443 | i = pp->rb_put + 1; |
| 2444 | if (i >= RB_SIZE) |
| 2445 | i = 0; |
| 2446 | if (i != pp->rb_get) { |
| 2447 | struct rb_entry *rp = &pp->rb_buf[pp->rb_put]; |
| 2448 | rp->len = len; |
| 2449 | memcpy(rp->data, data, len); |
| 2450 | pp->rb_put = i; |
| 2451 | wake_up_interruptible(&pp->wait); |
| 2452 | } |
| 2453 | spin_unlock(&pp->lock); |
| 2454 | } |
| 2455 | spin_unlock_irqrestore(&all_pvt_lock, flags); |
| 2456 | } |
| 2457 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2458 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | pmu_open(struct inode *inode, struct file *file) |
| 2460 | { |
| 2461 | struct pmu_private *pp; |
| 2462 | unsigned long flags; |
| 2463 | |
| 2464 | pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL); |
| 2465 | if (pp == 0) |
| 2466 | return -ENOMEM; |
| 2467 | pp->rb_get = pp->rb_put = 0; |
| 2468 | spin_lock_init(&pp->lock); |
| 2469 | init_waitqueue_head(&pp->wait); |
| 2470 | spin_lock_irqsave(&all_pvt_lock, flags); |
| 2471 | #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) |
| 2472 | pp->backlight_locker = 0; |
| 2473 | #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */ |
| 2474 | list_add(&pp->list, &all_pmu_pvt); |
| 2475 | spin_unlock_irqrestore(&all_pvt_lock, flags); |
| 2476 | file->private_data = pp; |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2480 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | pmu_read(struct file *file, char __user *buf, |
| 2482 | size_t count, loff_t *ppos) |
| 2483 | { |
| 2484 | struct pmu_private *pp = file->private_data; |
| 2485 | DECLARE_WAITQUEUE(wait, current); |
| 2486 | unsigned long flags; |
| 2487 | int ret = 0; |
| 2488 | |
| 2489 | if (count < 1 || pp == 0) |
| 2490 | return -EINVAL; |
| 2491 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 2492 | return -EFAULT; |
| 2493 | |
| 2494 | spin_lock_irqsave(&pp->lock, flags); |
| 2495 | add_wait_queue(&pp->wait, &wait); |
| 2496 | current->state = TASK_INTERRUPTIBLE; |
| 2497 | |
| 2498 | for (;;) { |
| 2499 | ret = -EAGAIN; |
| 2500 | if (pp->rb_get != pp->rb_put) { |
| 2501 | int i = pp->rb_get; |
| 2502 | struct rb_entry *rp = &pp->rb_buf[i]; |
| 2503 | ret = rp->len; |
| 2504 | spin_unlock_irqrestore(&pp->lock, flags); |
| 2505 | if (ret > count) |
| 2506 | ret = count; |
| 2507 | if (ret > 0 && copy_to_user(buf, rp->data, ret)) |
| 2508 | ret = -EFAULT; |
| 2509 | if (++i >= RB_SIZE) |
| 2510 | i = 0; |
| 2511 | spin_lock_irqsave(&pp->lock, flags); |
| 2512 | pp->rb_get = i; |
| 2513 | } |
| 2514 | if (ret >= 0) |
| 2515 | break; |
| 2516 | if (file->f_flags & O_NONBLOCK) |
| 2517 | break; |
| 2518 | ret = -ERESTARTSYS; |
| 2519 | if (signal_pending(current)) |
| 2520 | break; |
| 2521 | spin_unlock_irqrestore(&pp->lock, flags); |
| 2522 | schedule(); |
| 2523 | spin_lock_irqsave(&pp->lock, flags); |
| 2524 | } |
| 2525 | current->state = TASK_RUNNING; |
| 2526 | remove_wait_queue(&pp->wait, &wait); |
| 2527 | spin_unlock_irqrestore(&pp->lock, flags); |
| 2528 | |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2532 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | pmu_write(struct file *file, const char __user *buf, |
| 2534 | size_t count, loff_t *ppos) |
| 2535 | { |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2539 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | pmu_fpoll(struct file *filp, poll_table *wait) |
| 2541 | { |
| 2542 | struct pmu_private *pp = filp->private_data; |
| 2543 | unsigned int mask = 0; |
| 2544 | unsigned long flags; |
| 2545 | |
| 2546 | if (pp == 0) |
| 2547 | return 0; |
| 2548 | poll_wait(filp, &pp->wait, wait); |
| 2549 | spin_lock_irqsave(&pp->lock, flags); |
| 2550 | if (pp->rb_get != pp->rb_put) |
| 2551 | mask |= POLLIN; |
| 2552 | spin_unlock_irqrestore(&pp->lock, flags); |
| 2553 | return mask; |
| 2554 | } |
| 2555 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2556 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | pmu_release(struct inode *inode, struct file *file) |
| 2558 | { |
| 2559 | struct pmu_private *pp = file->private_data; |
| 2560 | unsigned long flags; |
| 2561 | |
| 2562 | lock_kernel(); |
| 2563 | if (pp != 0) { |
| 2564 | file->private_data = NULL; |
| 2565 | spin_lock_irqsave(&all_pvt_lock, flags); |
| 2566 | list_del(&pp->list); |
| 2567 | spin_unlock_irqrestore(&all_pvt_lock, flags); |
| 2568 | #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) |
| 2569 | if (pp->backlight_locker) { |
| 2570 | spin_lock_irqsave(&pmu_lock, flags); |
| 2571 | disable_kernel_backlight--; |
| 2572 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 2573 | } |
| 2574 | #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */ |
| 2575 | kfree(pp); |
| 2576 | } |
| 2577 | unlock_kernel(); |
| 2578 | return 0; |
| 2579 | } |
| 2580 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2581 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | pmu_ioctl(struct inode * inode, struct file *filp, |
| 2583 | u_int cmd, u_long arg) |
| 2584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | __u32 __user *argp = (__u32 __user *)arg; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2586 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | |
| 2588 | switch (cmd) { |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2589 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | case PMU_IOC_SLEEP: |
| 2591 | if (!capable(CAP_SYS_ADMIN)) |
| 2592 | return -EACCES; |
| 2593 | if (sleep_in_progress) |
| 2594 | return -EBUSY; |
| 2595 | sleep_in_progress = 1; |
| 2596 | switch (pmu_kind) { |
| 2597 | case PMU_OHARE_BASED: |
| 2598 | error = powerbook_sleep_3400(); |
| 2599 | break; |
| 2600 | case PMU_HEATHROW_BASED: |
| 2601 | case PMU_PADDINGTON_BASED: |
| 2602 | error = powerbook_sleep_grackle(); |
| 2603 | break; |
| 2604 | case PMU_KEYLARGO_BASED: |
| 2605 | error = powerbook_sleep_Core99(); |
| 2606 | break; |
| 2607 | default: |
| 2608 | error = -ENOSYS; |
| 2609 | } |
| 2610 | sleep_in_progress = 0; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2611 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | case PMU_IOC_CAN_SLEEP: |
| 2613 | if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) |
| 2614 | return put_user(0, argp); |
| 2615 | else |
| 2616 | return put_user(1, argp); |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2617 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2619 | #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY |
| 2620 | /* Compatibility ioctl's for backlight */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | case PMU_IOC_GET_BACKLIGHT: |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2622 | { |
| 2623 | int brightness; |
| 2624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | if (sleep_in_progress) |
| 2626 | return -EBUSY; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2627 | |
| 2628 | brightness = pmac_backlight_get_legacy_brightness(); |
| 2629 | if (brightness < 0) |
| 2630 | return brightness; |
| 2631 | else |
| 2632 | return put_user(brightness, argp); |
| 2633 | |
| 2634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | case PMU_IOC_SET_BACKLIGHT: |
| 2636 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2637 | int brightness; |
| 2638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | if (sleep_in_progress) |
| 2640 | return -EBUSY; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2641 | |
| 2642 | error = get_user(brightness, argp); |
| 2643 | if (error) |
| 2644 | return error; |
| 2645 | |
| 2646 | return pmac_backlight_set_legacy_brightness(brightness); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
| 2648 | #ifdef CONFIG_INPUT_ADBHID |
| 2649 | case PMU_IOC_GRAB_BACKLIGHT: { |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2650 | struct pmu_private *pp = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | unsigned long flags; |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | if (pp->backlight_locker) |
| 2654 | return 0; |
| 2655 | pp->backlight_locker = 1; |
| 2656 | spin_lock_irqsave(&pmu_lock, flags); |
| 2657 | disable_kernel_backlight++; |
| 2658 | spin_unlock_irqrestore(&pmu_lock, flags); |
| 2659 | return 0; |
| 2660 | } |
| 2661 | #endif /* CONFIG_INPUT_ADBHID */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 2662 | #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | case PMU_IOC_GET_MODEL: |
| 2664 | return put_user(pmu_kind, argp); |
| 2665 | case PMU_IOC_HAS_ADB: |
| 2666 | return put_user(pmu_has_adb, argp); |
| 2667 | } |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2668 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2671 | static struct file_operations pmu_device_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | .read = pmu_read, |
| 2673 | .write = pmu_write, |
| 2674 | .poll = pmu_fpoll, |
| 2675 | .ioctl = pmu_ioctl, |
| 2676 | .open = pmu_open, |
| 2677 | .release = pmu_release, |
| 2678 | }; |
| 2679 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2680 | static struct miscdevice pmu_device = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | PMU_MINOR, "pmu", &pmu_device_fops |
| 2682 | }; |
| 2683 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2684 | static int pmu_device_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | { |
| 2686 | if (!via) |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2687 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | if (misc_register(&pmu_device) < 0) |
| 2689 | printk(KERN_ERR "via-pmu: cannot register misc device.\n"); |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2690 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | } |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 2692 | device_initcall(pmu_device_init); |
| 2693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | |
| 2695 | #ifdef DEBUG_SLEEP |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2696 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | polled_handshake(volatile unsigned char __iomem *via) |
| 2698 | { |
| 2699 | via[B] &= ~TREQ; eieio(); |
| 2700 | while ((via[B] & TACK) != 0) |
| 2701 | ; |
| 2702 | via[B] |= TREQ; eieio(); |
| 2703 | while ((via[B] & TACK) == 0) |
| 2704 | ; |
| 2705 | } |
| 2706 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2707 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | polled_send_byte(volatile unsigned char __iomem *via, int x) |
| 2709 | { |
| 2710 | via[ACR] |= SR_OUT | SR_EXT; eieio(); |
| 2711 | via[SR] = x; eieio(); |
| 2712 | polled_handshake(via); |
| 2713 | } |
| 2714 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2715 | static inline int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | polled_recv_byte(volatile unsigned char __iomem *via) |
| 2717 | { |
| 2718 | int x; |
| 2719 | |
| 2720 | via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio(); |
| 2721 | x = via[SR]; eieio(); |
| 2722 | polled_handshake(via); |
| 2723 | x = via[SR]; eieio(); |
| 2724 | return x; |
| 2725 | } |
| 2726 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 2727 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | pmu_polled_request(struct adb_request *req) |
| 2729 | { |
| 2730 | unsigned long flags; |
| 2731 | int i, l, c; |
| 2732 | volatile unsigned char __iomem *v = via; |
| 2733 | |
| 2734 | req->complete = 1; |
| 2735 | c = req->data[0]; |
| 2736 | l = pmu_data_len[c][0]; |
| 2737 | if (l >= 0 && req->nbytes != l + 1) |
| 2738 | return -EINVAL; |
| 2739 | |
| 2740 | local_irq_save(flags); |
| 2741 | while (pmu_state != idle) |
| 2742 | pmu_poll(); |
| 2743 | |
| 2744 | while ((via[B] & TACK) == 0) |
| 2745 | ; |
| 2746 | polled_send_byte(v, c); |
| 2747 | if (l < 0) { |
| 2748 | l = req->nbytes - 1; |
| 2749 | polled_send_byte(v, l); |
| 2750 | } |
| 2751 | for (i = 1; i <= l; ++i) |
| 2752 | polled_send_byte(v, req->data[i]); |
| 2753 | |
| 2754 | l = pmu_data_len[c][1]; |
| 2755 | if (l < 0) |
| 2756 | l = polled_recv_byte(v); |
| 2757 | for (i = 0; i < l; ++i) |
| 2758 | req->reply[i + req->reply_len] = polled_recv_byte(v); |
| 2759 | |
| 2760 | if (req->done) |
| 2761 | (*req->done)(req); |
| 2762 | |
| 2763 | local_irq_restore(flags); |
| 2764 | return 0; |
| 2765 | } |
| 2766 | #endif /* DEBUG_SLEEP */ |
| 2767 | |
| 2768 | |
| 2769 | /* FIXME: This is a temporary set of callbacks to enable us |
| 2770 | * to do suspend-to-disk. |
| 2771 | */ |
| 2772 | |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2773 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | |
| 2775 | static int pmu_sys_suspended = 0; |
| 2776 | |
Pavel Machek | 3bfffd9 | 2005-04-16 15:25:37 -0700 | [diff] [blame] | 2777 | static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | { |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 2779 | if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | return 0; |
| 2781 | |
| 2782 | /* Suspend PMU event interrupts */ |
| 2783 | pmu_suspend(); |
| 2784 | |
| 2785 | pmu_sys_suspended = 1; |
| 2786 | return 0; |
| 2787 | } |
| 2788 | |
| 2789 | static int pmu_sys_resume(struct sys_device *sysdev) |
| 2790 | { |
| 2791 | struct adb_request req; |
| 2792 | |
| 2793 | if (!pmu_sys_suspended) |
| 2794 | return 0; |
| 2795 | |
| 2796 | /* Tell PMU we are ready */ |
| 2797 | pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2); |
| 2798 | pmu_wait_complete(&req); |
| 2799 | |
| 2800 | /* Resume PMU event interrupts */ |
| 2801 | pmu_resume(); |
| 2802 | |
| 2803 | pmu_sys_suspended = 0; |
| 2804 | |
| 2805 | return 0; |
| 2806 | } |
| 2807 | |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2808 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
| 2810 | static struct sysdev_class pmu_sysclass = { |
| 2811 | set_kset_name("pmu"), |
| 2812 | }; |
| 2813 | |
| 2814 | static struct sys_device device_pmu = { |
| 2815 | .id = 0, |
| 2816 | .cls = &pmu_sysclass, |
| 2817 | }; |
| 2818 | |
| 2819 | static struct sysdev_driver driver_pmu = { |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2820 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | .suspend = &pmu_sys_suspend, |
| 2822 | .resume = &pmu_sys_resume, |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2823 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | }; |
| 2825 | |
| 2826 | static int __init init_pmu_sysfs(void) |
| 2827 | { |
| 2828 | int rc; |
| 2829 | |
| 2830 | rc = sysdev_class_register(&pmu_sysclass); |
| 2831 | if (rc) { |
| 2832 | printk(KERN_ERR "Failed registering PMU sys class\n"); |
| 2833 | return -ENODEV; |
| 2834 | } |
| 2835 | rc = sysdev_register(&device_pmu); |
| 2836 | if (rc) { |
| 2837 | printk(KERN_ERR "Failed registering PMU sys device\n"); |
| 2838 | return -ENODEV; |
| 2839 | } |
| 2840 | rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu); |
| 2841 | if (rc) { |
| 2842 | printk(KERN_ERR "Failed registering PMU sys driver\n"); |
| 2843 | return -ENODEV; |
| 2844 | } |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | subsys_initcall(init_pmu_sysfs); |
| 2849 | |
| 2850 | EXPORT_SYMBOL(pmu_request); |
Benjamin Herrenschmidt | 730745a | 2006-01-07 11:30:44 +1100 | [diff] [blame] | 2851 | EXPORT_SYMBOL(pmu_queue_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | EXPORT_SYMBOL(pmu_poll); |
| 2853 | EXPORT_SYMBOL(pmu_poll_adb); |
| 2854 | EXPORT_SYMBOL(pmu_wait_complete); |
| 2855 | EXPORT_SYMBOL(pmu_suspend); |
| 2856 | EXPORT_SYMBOL(pmu_resume); |
| 2857 | EXPORT_SYMBOL(pmu_unlock); |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2858 | #if defined(CONFIG_PM) && defined(CONFIG_PPC32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | EXPORT_SYMBOL(pmu_enable_irled); |
| 2860 | EXPORT_SYMBOL(pmu_battery_count); |
| 2861 | EXPORT_SYMBOL(pmu_batteries); |
| 2862 | EXPORT_SYMBOL(pmu_power_flags); |
Paul Mackerras | a000503 | 2005-11-02 15:08:17 +1100 | [diff] [blame] | 2863 | #endif /* CONFIG_PM && CONFIG_PPC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | |