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