via-pmu.c 63 KB

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