via-pmu.c 60 KB

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