via-pmu.c 61 KB

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