via-pmu.c 68 KB

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