via-pmu.c 63 KB

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