via-pmu.c 68 KB

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