via-pmu.c 62 KB

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