via-pmu.c 70 KB

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