via-pmu.c 67 KB

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