via-pmu.c 70 KB

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