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