via-pmu.c 68 KB

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