via-pmu.c 75 KB

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