via-pmu.c 67 KB

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