via-pmu.c 75 KB

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