via-pmu.c 60 KB

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