via-pmu.c 68 KB

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