via-pmu.c 68 KB

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