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