via-pmu.c 67 KB

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