via-pmu.c 60 KB

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