via-pmu.c 75 KB

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