via-pmu.c 75 KB

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