via-pmu.c 62 KB

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