via-pmu.c 61 KB

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