via-pmu.c 68 KB

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