exynos4210_mct.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. * Samsung exynos4210 Multi Core timer
  3. *
  4. * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * Evgeny Voevodin <e.voevodin@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /*
  23. * Global Timer:
  24. *
  25. * Consists of two timers. First represents Free Running Counter and second
  26. * is used to measure interval from FRC to nearest comparator.
  27. *
  28. * 0 UINT64_MAX
  29. * | timer0 |
  30. * | <-------------------------------------------------------------- |
  31. * | --------------------------------------------frc---------------> |
  32. * |______________________________________________|__________________|
  33. * CMP0 CMP1 CMP2 | CMP3
  34. * __| |_
  35. * | timer1 |
  36. * | -------------> |
  37. * frc CMPx
  38. *
  39. * Problem: when implementing global timer as is, overflow arises.
  40. * next_time = cur_time + period * count;
  41. * period and count are 64 bits width.
  42. * Lets arm timer for MCT_GT_COUNTER_STEP count and update internal G_CNT
  43. * register during each event.
  44. *
  45. * Problem: both timers need to be implemented using MCT_XT_COUNTER_STEP because
  46. * local timer contains two counters: TCNT and ICNT. TCNT == 0 -> ICNT--.
  47. * IRQ is generated when ICNT riches zero. Implementation where TCNT == 0
  48. * generates IRQs suffers from too frequently events. Better to have one
  49. * uint64_t counter equal to TCNT*ICNT and arm ptimer.c for a minimum(TCNT*ICNT,
  50. * MCT_GT_COUNTER_STEP); (yes, if target tunes ICNT * TCNT to be too low values,
  51. * there is no way to avoid frequently events).
  52. */
  53. #include "sysbus.h"
  54. #include "qemu-timer.h"
  55. #include "qemu-common.h"
  56. #include "ptimer.h"
  57. #include "exynos4210.h"
  58. //#define DEBUG_MCT
  59. #ifdef DEBUG_MCT
  60. #define DPRINTF(fmt, ...) \
  61. do { fprintf(stdout, "MCT: [%24s:%5d] " fmt, __func__, __LINE__, \
  62. ## __VA_ARGS__); } while (0)
  63. #else
  64. #define DPRINTF(fmt, ...) do {} while (0)
  65. #endif
  66. #define MCT_CFG 0x000
  67. #define G_CNT_L 0x100
  68. #define G_CNT_U 0x104
  69. #define G_CNT_WSTAT 0x110
  70. #define G_COMP0_L 0x200
  71. #define G_COMP0_U 0x204
  72. #define G_COMP0_ADD_INCR 0x208
  73. #define G_COMP1_L 0x210
  74. #define G_COMP1_U 0x214
  75. #define G_COMP1_ADD_INCR 0x218
  76. #define G_COMP2_L 0x220
  77. #define G_COMP2_U 0x224
  78. #define G_COMP2_ADD_INCR 0x228
  79. #define G_COMP3_L 0x230
  80. #define G_COMP3_U 0x234
  81. #define G_COMP3_ADD_INCR 0x238
  82. #define G_TCON 0x240
  83. #define G_INT_CSTAT 0x244
  84. #define G_INT_ENB 0x248
  85. #define G_WSTAT 0x24C
  86. #define L0_TCNTB 0x300
  87. #define L0_TCNTO 0x304
  88. #define L0_ICNTB 0x308
  89. #define L0_ICNTO 0x30C
  90. #define L0_FRCNTB 0x310
  91. #define L0_FRCNTO 0x314
  92. #define L0_TCON 0x320
  93. #define L0_INT_CSTAT 0x330
  94. #define L0_INT_ENB 0x334
  95. #define L0_WSTAT 0x340
  96. #define L1_TCNTB 0x400
  97. #define L1_TCNTO 0x404
  98. #define L1_ICNTB 0x408
  99. #define L1_ICNTO 0x40C
  100. #define L1_FRCNTB 0x410
  101. #define L1_FRCNTO 0x414
  102. #define L1_TCON 0x420
  103. #define L1_INT_CSTAT 0x430
  104. #define L1_INT_ENB 0x434
  105. #define L1_WSTAT 0x440
  106. #define MCT_CFG_GET_PRESCALER(x) ((x) & 0xFF)
  107. #define MCT_CFG_GET_DIVIDER(x) (1 << ((x) >> 8 & 7))
  108. #define GET_G_COMP_IDX(offset) (((offset) - G_COMP0_L) / 0x10)
  109. #define GET_G_COMP_ADD_INCR_IDX(offset) (((offset) - G_COMP0_ADD_INCR) / 0x10)
  110. #define G_COMP_L(x) (G_COMP0_L + (x) * 0x10)
  111. #define G_COMP_U(x) (G_COMP0_U + (x) * 0x10)
  112. #define G_COMP_ADD_INCR(x) (G_COMP0_ADD_INCR + (x) * 0x10)
  113. /* MCT bits */
  114. #define G_TCON_COMP_ENABLE(x) (1 << 2 * (x))
  115. #define G_TCON_AUTO_ICREMENT(x) (1 << (2 * (x) + 1))
  116. #define G_TCON_TIMER_ENABLE (1 << 8)
  117. #define G_INT_ENABLE(x) (1 << (x))
  118. #define G_INT_CSTAT_COMP(x) (1 << (x))
  119. #define G_CNT_WSTAT_L 1
  120. #define G_CNT_WSTAT_U 2
  121. #define G_WSTAT_COMP_L(x) (1 << 4 * (x))
  122. #define G_WSTAT_COMP_U(x) (1 << ((4 * (x)) + 1))
  123. #define G_WSTAT_COMP_ADDINCR(x) (1 << ((4 * (x)) + 2))
  124. #define G_WSTAT_TCON_WRITE (1 << 16)
  125. #define GET_L_TIMER_IDX(offset) ((((offset) & 0xF00) - L0_TCNTB) / 0x100)
  126. #define GET_L_TIMER_CNT_REG_IDX(offset, lt_i) \
  127. (((offset) - (L0_TCNTB + 0x100 * (lt_i))) >> 2)
  128. #define L_ICNTB_MANUAL_UPDATE (1 << 31)
  129. #define L_TCON_TICK_START (1)
  130. #define L_TCON_INT_START (1 << 1)
  131. #define L_TCON_INTERVAL_MODE (1 << 2)
  132. #define L_TCON_FRC_START (1 << 3)
  133. #define L_INT_CSTAT_INTCNT (1 << 0)
  134. #define L_INT_CSTAT_FRCCNT (1 << 1)
  135. #define L_INT_INTENB_ICNTEIE (1 << 0)
  136. #define L_INT_INTENB_FRCEIE (1 << 1)
  137. #define L_WSTAT_TCNTB_WRITE (1 << 0)
  138. #define L_WSTAT_ICNTB_WRITE (1 << 1)
  139. #define L_WSTAT_FRCCNTB_WRITE (1 << 2)
  140. #define L_WSTAT_TCON_WRITE (1 << 3)
  141. enum LocalTimerRegCntIndexes {
  142. L_REG_CNT_TCNTB,
  143. L_REG_CNT_TCNTO,
  144. L_REG_CNT_ICNTB,
  145. L_REG_CNT_ICNTO,
  146. L_REG_CNT_FRCCNTB,
  147. L_REG_CNT_FRCCNTO,
  148. L_REG_CNT_AMOUNT
  149. };
  150. #define MCT_NIRQ 6
  151. #define MCT_SFR_SIZE 0x444
  152. #define MCT_GT_CMP_NUM 4
  153. #define MCT_GT_MAX_VAL UINT64_MAX
  154. #define MCT_GT_COUNTER_STEP 0x100000000ULL
  155. #define MCT_LT_COUNTER_STEP 0x100000000ULL
  156. #define MCT_LT_CNT_LOW_LIMIT 0x100
  157. /* global timer */
  158. typedef struct {
  159. qemu_irq irq[MCT_GT_CMP_NUM];
  160. struct gregs {
  161. uint64_t cnt;
  162. uint32_t cnt_wstat;
  163. uint32_t tcon;
  164. uint32_t int_cstat;
  165. uint32_t int_enb;
  166. uint32_t wstat;
  167. uint64_t comp[MCT_GT_CMP_NUM];
  168. uint32_t comp_add_incr[MCT_GT_CMP_NUM];
  169. } reg;
  170. uint64_t count; /* Value FRC was armed with */
  171. int32_t curr_comp; /* Current comparator FRC is running to */
  172. ptimer_state *ptimer_frc; /* FRC timer */
  173. } Exynos4210MCTGT;
  174. /* local timer */
  175. typedef struct {
  176. int id; /* timer id */
  177. qemu_irq irq; /* local timer irq */
  178. struct tick_timer {
  179. uint32_t cnt_run; /* cnt timer is running */
  180. uint32_t int_run; /* int timer is running */
  181. uint32_t last_icnto;
  182. uint32_t last_tcnto;
  183. uint32_t tcntb; /* initial value for TCNTB */
  184. uint32_t icntb; /* initial value for ICNTB */
  185. /* for step mode */
  186. uint64_t distance; /* distance to count to the next event */
  187. uint64_t progress; /* progress when counting by steps */
  188. uint64_t count; /* count to arm timer with */
  189. ptimer_state *ptimer_tick; /* timer for tick counter */
  190. } tick_timer;
  191. /* use ptimer.c to represent count down timer */
  192. ptimer_state *ptimer_frc; /* timer for free running counter */
  193. /* registers */
  194. struct lregs {
  195. uint32_t cnt[L_REG_CNT_AMOUNT];
  196. uint32_t tcon;
  197. uint32_t int_cstat;
  198. uint32_t int_enb;
  199. uint32_t wstat;
  200. } reg;
  201. } Exynos4210MCTLT;
  202. typedef struct Exynos4210MCTState {
  203. SysBusDevice busdev;
  204. MemoryRegion iomem;
  205. /* Registers */
  206. uint32_t reg_mct_cfg;
  207. Exynos4210MCTLT l_timer[2];
  208. Exynos4210MCTGT g_timer;
  209. uint32_t freq; /* all timers tick frequency, TCLK */
  210. } Exynos4210MCTState;
  211. /*** VMState ***/
  212. static const VMStateDescription vmstate_tick_timer = {
  213. .name = "exynos4210.mct.tick_timer",
  214. .version_id = 1,
  215. .minimum_version_id = 1,
  216. .minimum_version_id_old = 1,
  217. .fields = (VMStateField[]) {
  218. VMSTATE_UINT32(cnt_run, struct tick_timer),
  219. VMSTATE_UINT32(int_run, struct tick_timer),
  220. VMSTATE_UINT32(last_icnto, struct tick_timer),
  221. VMSTATE_UINT32(last_tcnto, struct tick_timer),
  222. VMSTATE_UINT32(tcntb, struct tick_timer),
  223. VMSTATE_UINT32(icntb, struct tick_timer),
  224. VMSTATE_UINT64(distance, struct tick_timer),
  225. VMSTATE_UINT64(progress, struct tick_timer),
  226. VMSTATE_UINT64(count, struct tick_timer),
  227. VMSTATE_PTIMER(ptimer_tick, struct tick_timer),
  228. VMSTATE_END_OF_LIST()
  229. }
  230. };
  231. static const VMStateDescription vmstate_lregs = {
  232. .name = "exynos4210.mct.lregs",
  233. .version_id = 1,
  234. .minimum_version_id = 1,
  235. .minimum_version_id_old = 1,
  236. .fields = (VMStateField[]) {
  237. VMSTATE_UINT32_ARRAY(cnt, struct lregs, L_REG_CNT_AMOUNT),
  238. VMSTATE_UINT32(tcon, struct lregs),
  239. VMSTATE_UINT32(int_cstat, struct lregs),
  240. VMSTATE_UINT32(int_enb, struct lregs),
  241. VMSTATE_UINT32(wstat, struct lregs),
  242. VMSTATE_END_OF_LIST()
  243. }
  244. };
  245. static const VMStateDescription vmstate_exynos4210_mct_lt = {
  246. .name = "exynos4210.mct.lt",
  247. .version_id = 1,
  248. .minimum_version_id = 1,
  249. .minimum_version_id_old = 1,
  250. .fields = (VMStateField[]) {
  251. VMSTATE_INT32(id, Exynos4210MCTLT),
  252. VMSTATE_STRUCT(tick_timer, Exynos4210MCTLT, 0,
  253. vmstate_tick_timer,
  254. struct tick_timer),
  255. VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTLT),
  256. VMSTATE_STRUCT(reg, Exynos4210MCTLT, 0,
  257. vmstate_lregs,
  258. struct lregs),
  259. VMSTATE_END_OF_LIST()
  260. }
  261. };
  262. static const VMStateDescription vmstate_gregs = {
  263. .name = "exynos4210.mct.lregs",
  264. .version_id = 1,
  265. .minimum_version_id = 1,
  266. .minimum_version_id_old = 1,
  267. .fields = (VMStateField[]) {
  268. VMSTATE_UINT64(cnt, struct gregs),
  269. VMSTATE_UINT32(cnt_wstat, struct gregs),
  270. VMSTATE_UINT32(tcon, struct gregs),
  271. VMSTATE_UINT32(int_cstat, struct gregs),
  272. VMSTATE_UINT32(int_enb, struct gregs),
  273. VMSTATE_UINT32(wstat, struct gregs),
  274. VMSTATE_UINT64_ARRAY(comp, struct gregs, MCT_GT_CMP_NUM),
  275. VMSTATE_UINT32_ARRAY(comp_add_incr, struct gregs,
  276. MCT_GT_CMP_NUM),
  277. VMSTATE_END_OF_LIST()
  278. }
  279. };
  280. static const VMStateDescription vmstate_exynos4210_mct_gt = {
  281. .name = "exynos4210.mct.lt",
  282. .version_id = 1,
  283. .minimum_version_id = 1,
  284. .minimum_version_id_old = 1,
  285. .fields = (VMStateField[]) {
  286. VMSTATE_STRUCT(reg, Exynos4210MCTGT, 0, vmstate_gregs,
  287. struct gregs),
  288. VMSTATE_UINT64(count, Exynos4210MCTGT),
  289. VMSTATE_INT32(curr_comp, Exynos4210MCTGT),
  290. VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTGT),
  291. VMSTATE_END_OF_LIST()
  292. }
  293. };
  294. static const VMStateDescription vmstate_exynos4210_mct_state = {
  295. .name = "exynos4210.mct",
  296. .version_id = 1,
  297. .minimum_version_id = 1,
  298. .minimum_version_id_old = 1,
  299. .fields = (VMStateField[]) {
  300. VMSTATE_UINT32(reg_mct_cfg, Exynos4210MCTState),
  301. VMSTATE_STRUCT_ARRAY(l_timer, Exynos4210MCTState, 2, 0,
  302. vmstate_exynos4210_mct_lt, Exynos4210MCTLT),
  303. VMSTATE_STRUCT(g_timer, Exynos4210MCTState, 0,
  304. vmstate_exynos4210_mct_gt, Exynos4210MCTGT),
  305. VMSTATE_UINT32(freq, Exynos4210MCTState),
  306. VMSTATE_END_OF_LIST()
  307. }
  308. };
  309. static void exynos4210_mct_update_freq(Exynos4210MCTState *s);
  310. /*
  311. * Set counter of FRC global timer.
  312. */
  313. static void exynos4210_gfrc_set_count(Exynos4210MCTGT *s, uint64_t count)
  314. {
  315. s->count = count;
  316. DPRINTF("global timer frc set count 0x%llx\n", count);
  317. ptimer_set_count(s->ptimer_frc, count);
  318. }
  319. /*
  320. * Get counter of FRC global timer.
  321. */
  322. static uint64_t exynos4210_gfrc_get_count(Exynos4210MCTGT *s)
  323. {
  324. uint64_t count = 0;
  325. count = ptimer_get_count(s->ptimer_frc);
  326. count = s->count - count;
  327. return s->reg.cnt + count;
  328. }
  329. /*
  330. * Stop global FRC timer
  331. */
  332. static void exynos4210_gfrc_stop(Exynos4210MCTGT *s)
  333. {
  334. DPRINTF("global timer frc stop\n");
  335. ptimer_stop(s->ptimer_frc);
  336. }
  337. /*
  338. * Start global FRC timer
  339. */
  340. static void exynos4210_gfrc_start(Exynos4210MCTGT *s)
  341. {
  342. DPRINTF("global timer frc start\n");
  343. ptimer_run(s->ptimer_frc, 1);
  344. }
  345. /*
  346. * Find next nearest Comparator. If current Comparator value equals to other
  347. * Comparator value, skip them both
  348. */
  349. static int32_t exynos4210_gcomp_find(Exynos4210MCTState *s)
  350. {
  351. int res;
  352. int i;
  353. int enabled;
  354. uint64_t min;
  355. int min_comp_i;
  356. uint64_t gfrc;
  357. uint64_t distance;
  358. uint64_t distance_min;
  359. int comp_i;
  360. /* get gfrc count */
  361. gfrc = exynos4210_gfrc_get_count(&s->g_timer);
  362. min = UINT64_MAX;
  363. distance_min = UINT64_MAX;
  364. comp_i = MCT_GT_CMP_NUM;
  365. min_comp_i = MCT_GT_CMP_NUM;
  366. enabled = 0;
  367. /* lookup for nearest comparator */
  368. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  369. if (s->g_timer.reg.tcon & G_TCON_COMP_ENABLE(i)) {
  370. enabled = 1;
  371. if (s->g_timer.reg.comp[i] > gfrc) {
  372. /* Comparator is upper then FRC */
  373. distance = s->g_timer.reg.comp[i] - gfrc;
  374. if (distance <= distance_min) {
  375. distance_min = distance;
  376. comp_i = i;
  377. }
  378. } else {
  379. /* Comparator is below FRC, find the smallest */
  380. if (s->g_timer.reg.comp[i] <= min) {
  381. min = s->g_timer.reg.comp[i];
  382. min_comp_i = i;
  383. }
  384. }
  385. }
  386. }
  387. if (!enabled) {
  388. /* All Comparators disabled */
  389. res = -1;
  390. } else if (comp_i < MCT_GT_CMP_NUM) {
  391. /* Found upper Comparator */
  392. res = comp_i;
  393. } else {
  394. /* All Comparators are below or equal to FRC */
  395. res = min_comp_i;
  396. }
  397. DPRINTF("found comparator %d: comp 0x%llx distance 0x%llx, gfrc 0x%llx\n",
  398. res,
  399. s->g_timer.reg.comp[res],
  400. distance_min,
  401. gfrc);
  402. return res;
  403. }
  404. /*
  405. * Get distance to nearest Comparator
  406. */
  407. static uint64_t exynos4210_gcomp_get_distance(Exynos4210MCTState *s, int32_t id)
  408. {
  409. if (id == -1) {
  410. /* no enabled Comparators, choose max distance */
  411. return MCT_GT_COUNTER_STEP;
  412. }
  413. if (s->g_timer.reg.comp[id] - s->g_timer.reg.cnt < MCT_GT_COUNTER_STEP) {
  414. return s->g_timer.reg.comp[id] - s->g_timer.reg.cnt;
  415. } else {
  416. return MCT_GT_COUNTER_STEP;
  417. }
  418. }
  419. /*
  420. * Restart global FRC timer
  421. */
  422. static void exynos4210_gfrc_restart(Exynos4210MCTState *s)
  423. {
  424. uint64_t distance;
  425. exynos4210_gfrc_stop(&s->g_timer);
  426. s->g_timer.curr_comp = exynos4210_gcomp_find(s);
  427. distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
  428. if (distance > MCT_GT_COUNTER_STEP || !distance) {
  429. distance = MCT_GT_COUNTER_STEP;
  430. }
  431. exynos4210_gfrc_set_count(&s->g_timer, distance);
  432. exynos4210_gfrc_start(&s->g_timer);
  433. }
  434. /*
  435. * Raise global timer CMP IRQ
  436. */
  437. static void exynos4210_gcomp_raise_irq(void *opaque, uint32_t id)
  438. {
  439. Exynos4210MCTGT *s = opaque;
  440. /* If CSTAT is pending and IRQ is enabled */
  441. if ((s->reg.int_cstat & G_INT_CSTAT_COMP(id)) &&
  442. (s->reg.int_enb & G_INT_ENABLE(id))) {
  443. DPRINTF("gcmp timer[%d] IRQ\n", id);
  444. qemu_irq_raise(s->irq[id]);
  445. }
  446. }
  447. /*
  448. * Lower global timer CMP IRQ
  449. */
  450. static void exynos4210_gcomp_lower_irq(void *opaque, uint32_t id)
  451. {
  452. Exynos4210MCTGT *s = opaque;
  453. qemu_irq_lower(s->irq[id]);
  454. }
  455. /*
  456. * Global timer FRC event handler.
  457. * Each event occurs when internal counter reaches counter + MCT_GT_COUNTER_STEP
  458. * Every time we arm global FRC timer to count for MCT_GT_COUNTER_STEP value
  459. */
  460. static void exynos4210_gfrc_event(void *opaque)
  461. {
  462. Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
  463. int i;
  464. uint64_t distance;
  465. DPRINTF("\n");
  466. s->g_timer.reg.cnt += s->g_timer.count;
  467. /* Process all comparators */
  468. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  469. if (s->g_timer.reg.cnt == s->g_timer.reg.comp[i]) {
  470. /* reached nearest comparator */
  471. s->g_timer.reg.int_cstat |= G_INT_CSTAT_COMP(i);
  472. /* Auto increment */
  473. if (s->g_timer.reg.tcon & G_TCON_AUTO_ICREMENT(i)) {
  474. s->g_timer.reg.comp[i] += s->g_timer.reg.comp_add_incr[i];
  475. }
  476. /* IRQ */
  477. exynos4210_gcomp_raise_irq(&s->g_timer, i);
  478. }
  479. }
  480. /* Reload FRC to reach nearest comparator */
  481. s->g_timer.curr_comp = exynos4210_gcomp_find(s);
  482. distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
  483. if (distance > MCT_GT_COUNTER_STEP) {
  484. distance = MCT_GT_COUNTER_STEP;
  485. }
  486. exynos4210_gfrc_set_count(&s->g_timer, distance);
  487. exynos4210_gfrc_start(&s->g_timer);
  488. return;
  489. }
  490. /*
  491. * Get counter of FRC local timer.
  492. */
  493. static uint64_t exynos4210_lfrc_get_count(Exynos4210MCTLT *s)
  494. {
  495. return ptimer_get_count(s->ptimer_frc);
  496. }
  497. /*
  498. * Set counter of FRC local timer.
  499. */
  500. static void exynos4210_lfrc_update_count(Exynos4210MCTLT *s)
  501. {
  502. if (!s->reg.cnt[L_REG_CNT_FRCCNTB]) {
  503. ptimer_set_count(s->ptimer_frc, MCT_LT_COUNTER_STEP);
  504. } else {
  505. ptimer_set_count(s->ptimer_frc, s->reg.cnt[L_REG_CNT_FRCCNTB]);
  506. }
  507. }
  508. /*
  509. * Start local FRC timer
  510. */
  511. static void exynos4210_lfrc_start(Exynos4210MCTLT *s)
  512. {
  513. ptimer_run(s->ptimer_frc, 1);
  514. }
  515. /*
  516. * Stop local FRC timer
  517. */
  518. static void exynos4210_lfrc_stop(Exynos4210MCTLT *s)
  519. {
  520. ptimer_stop(s->ptimer_frc);
  521. }
  522. /*
  523. * Local timer free running counter tick handler
  524. */
  525. static void exynos4210_lfrc_event(void *opaque)
  526. {
  527. Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
  528. /* local frc expired */
  529. DPRINTF("\n");
  530. s->reg.int_cstat |= L_INT_CSTAT_FRCCNT;
  531. /* update frc counter */
  532. exynos4210_lfrc_update_count(s);
  533. /* raise irq */
  534. if (s->reg.int_enb & L_INT_INTENB_FRCEIE) {
  535. qemu_irq_raise(s->irq);
  536. }
  537. /* we reached here, this means that timer is enabled */
  538. exynos4210_lfrc_start(s);
  539. }
  540. static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s);
  541. static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s);
  542. static void exynos4210_ltick_recalc_count(struct tick_timer *s);
  543. /*
  544. * Action on enabling local tick int timer
  545. */
  546. static void exynos4210_ltick_int_start(struct tick_timer *s)
  547. {
  548. if (!s->int_run) {
  549. s->int_run = 1;
  550. }
  551. }
  552. /*
  553. * Action on disabling local tick int timer
  554. */
  555. static void exynos4210_ltick_int_stop(struct tick_timer *s)
  556. {
  557. if (s->int_run) {
  558. s->last_icnto = exynos4210_ltick_int_get_cnto(s);
  559. s->int_run = 0;
  560. }
  561. }
  562. /*
  563. * Get count for INT timer
  564. */
  565. static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s)
  566. {
  567. uint32_t icnto;
  568. uint64_t remain;
  569. uint64_t count;
  570. uint64_t counted;
  571. uint64_t cur_progress;
  572. count = ptimer_get_count(s->ptimer_tick);
  573. if (count) {
  574. /* timer is still counting, called not from event */
  575. counted = s->count - ptimer_get_count(s->ptimer_tick);
  576. cur_progress = s->progress + counted;
  577. } else {
  578. /* timer expired earlier */
  579. cur_progress = s->progress;
  580. }
  581. remain = s->distance - cur_progress;
  582. if (!s->int_run) {
  583. /* INT is stopped. */
  584. icnto = s->last_icnto;
  585. } else {
  586. /* Both are counting */
  587. icnto = remain / s->tcntb;
  588. }
  589. return icnto;
  590. }
  591. /*
  592. * Start local tick cnt timer.
  593. */
  594. static void exynos4210_ltick_cnt_start(struct tick_timer *s)
  595. {
  596. if (!s->cnt_run) {
  597. exynos4210_ltick_recalc_count(s);
  598. ptimer_set_count(s->ptimer_tick, s->count);
  599. ptimer_run(s->ptimer_tick, 1);
  600. s->cnt_run = 1;
  601. }
  602. }
  603. /*
  604. * Stop local tick cnt timer.
  605. */
  606. static void exynos4210_ltick_cnt_stop(struct tick_timer *s)
  607. {
  608. if (s->cnt_run) {
  609. s->last_tcnto = exynos4210_ltick_cnt_get_cnto(s);
  610. if (s->int_run) {
  611. exynos4210_ltick_int_stop(s);
  612. }
  613. ptimer_stop(s->ptimer_tick);
  614. s->cnt_run = 0;
  615. }
  616. }
  617. /*
  618. * Get counter for CNT timer
  619. */
  620. static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s)
  621. {
  622. uint32_t tcnto;
  623. uint32_t icnto;
  624. uint64_t remain;
  625. uint64_t counted;
  626. uint64_t count;
  627. uint64_t cur_progress;
  628. count = ptimer_get_count(s->ptimer_tick);
  629. if (count) {
  630. /* timer is still counting, called not from event */
  631. counted = s->count - ptimer_get_count(s->ptimer_tick);
  632. cur_progress = s->progress + counted;
  633. } else {
  634. /* timer expired earlier */
  635. cur_progress = s->progress;
  636. }
  637. remain = s->distance - cur_progress;
  638. if (!s->cnt_run) {
  639. /* Both are stopped. */
  640. tcnto = s->last_tcnto;
  641. } else if (!s->int_run) {
  642. /* INT counter is stopped, progress is by CNT timer */
  643. tcnto = remain % s->tcntb;
  644. } else {
  645. /* Both are counting */
  646. icnto = remain / s->tcntb;
  647. if (icnto) {
  648. tcnto = remain % (icnto * s->tcntb);
  649. } else {
  650. tcnto = remain % s->tcntb;
  651. }
  652. }
  653. return tcnto;
  654. }
  655. /*
  656. * Set new values of counters for CNT and INT timers
  657. */
  658. static void exynos4210_ltick_set_cntb(struct tick_timer *s, uint32_t new_cnt,
  659. uint32_t new_int)
  660. {
  661. uint32_t cnt_stopped = 0;
  662. uint32_t int_stopped = 0;
  663. if (s->cnt_run) {
  664. exynos4210_ltick_cnt_stop(s);
  665. cnt_stopped = 1;
  666. }
  667. if (s->int_run) {
  668. exynos4210_ltick_int_stop(s);
  669. int_stopped = 1;
  670. }
  671. s->tcntb = new_cnt + 1;
  672. s->icntb = new_int + 1;
  673. if (cnt_stopped) {
  674. exynos4210_ltick_cnt_start(s);
  675. }
  676. if (int_stopped) {
  677. exynos4210_ltick_int_start(s);
  678. }
  679. }
  680. /*
  681. * Calculate new counter value for tick timer
  682. */
  683. static void exynos4210_ltick_recalc_count(struct tick_timer *s)
  684. {
  685. uint64_t to_count;
  686. if ((s->cnt_run && s->last_tcnto) || (s->int_run && s->last_icnto)) {
  687. /*
  688. * one or both timers run and not counted to the end;
  689. * distance is not passed, recalculate with last_tcnto * last_icnto
  690. */
  691. if (s->last_tcnto) {
  692. to_count = s->last_tcnto * s->last_icnto;
  693. } else {
  694. to_count = s->last_icnto;
  695. }
  696. } else {
  697. /* distance is passed, recalculate with tcnto * icnto */
  698. if (s->icntb) {
  699. s->distance = s->tcntb * s->icntb;
  700. } else {
  701. s->distance = s->tcntb;
  702. }
  703. to_count = s->distance;
  704. s->progress = 0;
  705. }
  706. if (to_count > MCT_LT_COUNTER_STEP) {
  707. /* count by step */
  708. s->count = MCT_LT_COUNTER_STEP;
  709. } else {
  710. s->count = to_count;
  711. }
  712. }
  713. /*
  714. * Initialize tick_timer
  715. */
  716. static void exynos4210_ltick_timer_init(struct tick_timer *s)
  717. {
  718. exynos4210_ltick_int_stop(s);
  719. exynos4210_ltick_cnt_stop(s);
  720. s->count = 0;
  721. s->distance = 0;
  722. s->progress = 0;
  723. s->icntb = 0;
  724. s->tcntb = 0;
  725. }
  726. /*
  727. * tick_timer event.
  728. * Raises when abstract tick_timer expires.
  729. */
  730. static void exynos4210_ltick_timer_event(struct tick_timer *s)
  731. {
  732. s->progress += s->count;
  733. }
  734. /*
  735. * Local timer tick counter handler.
  736. * Don't use reloaded timers. If timer counter = zero
  737. * then handler called but after handler finished no
  738. * timer reload occurs.
  739. */
  740. static void exynos4210_ltick_event(void *opaque)
  741. {
  742. Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
  743. uint32_t tcnto;
  744. uint32_t icnto;
  745. #ifdef DEBUG_MCT
  746. static uint64_t time1[2] = {0};
  747. static uint64_t time2[2] = {0};
  748. #endif
  749. /* Call tick_timer event handler, it will update its tcntb and icntb. */
  750. exynos4210_ltick_timer_event(&s->tick_timer);
  751. /* get tick_timer cnt */
  752. tcnto = exynos4210_ltick_cnt_get_cnto(&s->tick_timer);
  753. /* get tick_timer int */
  754. icnto = exynos4210_ltick_int_get_cnto(&s->tick_timer);
  755. /* raise IRQ if needed */
  756. if (!icnto && s->reg.tcon & L_TCON_INT_START) {
  757. /* INT counter enabled and expired */
  758. s->reg.int_cstat |= L_INT_CSTAT_INTCNT;
  759. /* raise interrupt if enabled */
  760. if (s->reg.int_enb & L_INT_INTENB_ICNTEIE) {
  761. #ifdef DEBUG_MCT
  762. time2[s->id] = qemu_get_clock_ns(vm_clock);
  763. DPRINTF("local timer[%d] IRQ: %llx\n", s->id,
  764. time2[s->id] - time1[s->id]);
  765. time1[s->id] = time2[s->id];
  766. #endif
  767. qemu_irq_raise(s->irq);
  768. }
  769. /* reload ICNTB */
  770. if (s->reg.tcon & L_TCON_INTERVAL_MODE) {
  771. exynos4210_ltick_set_cntb(&s->tick_timer,
  772. s->reg.cnt[L_REG_CNT_TCNTB],
  773. s->reg.cnt[L_REG_CNT_ICNTB]);
  774. }
  775. } else {
  776. /* reload TCNTB */
  777. if (!tcnto) {
  778. exynos4210_ltick_set_cntb(&s->tick_timer,
  779. s->reg.cnt[L_REG_CNT_TCNTB],
  780. icnto);
  781. }
  782. }
  783. /* start tick_timer cnt */
  784. exynos4210_ltick_cnt_start(&s->tick_timer);
  785. /* start tick_timer int */
  786. exynos4210_ltick_int_start(&s->tick_timer);
  787. }
  788. /* update timer frequency */
  789. static void exynos4210_mct_update_freq(Exynos4210MCTState *s)
  790. {
  791. uint32_t freq = s->freq;
  792. s->freq = 24000000 /
  793. ((MCT_CFG_GET_PRESCALER(s->reg_mct_cfg)+1) *
  794. MCT_CFG_GET_DIVIDER(s->reg_mct_cfg));
  795. if (freq != s->freq) {
  796. DPRINTF("freq=%dHz\n", s->freq);
  797. /* global timer */
  798. ptimer_set_freq(s->g_timer.ptimer_frc, s->freq);
  799. /* local timer */
  800. ptimer_set_freq(s->l_timer[0].tick_timer.ptimer_tick, s->freq);
  801. ptimer_set_freq(s->l_timer[0].ptimer_frc, s->freq);
  802. ptimer_set_freq(s->l_timer[1].tick_timer.ptimer_tick, s->freq);
  803. ptimer_set_freq(s->l_timer[1].ptimer_frc, s->freq);
  804. }
  805. }
  806. /* set defaul_timer values for all fields */
  807. static void exynos4210_mct_reset(DeviceState *d)
  808. {
  809. Exynos4210MCTState *s = (Exynos4210MCTState *)d;
  810. uint32_t i;
  811. s->reg_mct_cfg = 0;
  812. /* global timer */
  813. memset(&s->g_timer.reg, 0, sizeof(s->g_timer.reg));
  814. exynos4210_gfrc_stop(&s->g_timer);
  815. /* local timer */
  816. memset(s->l_timer[0].reg.cnt, 0, sizeof(s->l_timer[0].reg.cnt));
  817. memset(s->l_timer[1].reg.cnt, 0, sizeof(s->l_timer[1].reg.cnt));
  818. for (i = 0; i < 2; i++) {
  819. s->l_timer[i].reg.int_cstat = 0;
  820. s->l_timer[i].reg.int_enb = 0;
  821. s->l_timer[i].reg.tcon = 0;
  822. s->l_timer[i].reg.wstat = 0;
  823. s->l_timer[i].tick_timer.count = 0;
  824. s->l_timer[i].tick_timer.distance = 0;
  825. s->l_timer[i].tick_timer.progress = 0;
  826. ptimer_stop(s->l_timer[i].ptimer_frc);
  827. exynos4210_ltick_timer_init(&s->l_timer[i].tick_timer);
  828. }
  829. exynos4210_mct_update_freq(s);
  830. }
  831. /* Multi Core Timer read */
  832. static uint64_t exynos4210_mct_read(void *opaque, target_phys_addr_t offset,
  833. unsigned size)
  834. {
  835. Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
  836. int index;
  837. int shift;
  838. uint64_t count;
  839. uint32_t value;
  840. int lt_i;
  841. switch (offset) {
  842. case MCT_CFG:
  843. value = s->reg_mct_cfg;
  844. break;
  845. case G_CNT_L: case G_CNT_U:
  846. shift = 8 * (offset & 0x4);
  847. count = exynos4210_gfrc_get_count(&s->g_timer);
  848. value = UINT32_MAX & (count >> shift);
  849. DPRINTF("read FRC=0x%llx\n", count);
  850. break;
  851. case G_CNT_WSTAT:
  852. value = s->g_timer.reg.cnt_wstat;
  853. break;
  854. case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
  855. case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
  856. index = GET_G_COMP_IDX(offset);
  857. shift = 8 * (offset & 0x4);
  858. value = UINT32_MAX & (s->g_timer.reg.comp[index] >> shift);
  859. break;
  860. case G_TCON:
  861. value = s->g_timer.reg.tcon;
  862. break;
  863. case G_INT_CSTAT:
  864. value = s->g_timer.reg.int_cstat;
  865. break;
  866. case G_INT_ENB:
  867. value = s->g_timer.reg.int_enb;
  868. break;
  869. break;
  870. case G_WSTAT:
  871. value = s->g_timer.reg.wstat;
  872. break;
  873. case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
  874. case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
  875. value = s->g_timer.reg.comp_add_incr[GET_G_COMP_ADD_INCR_IDX(offset)];
  876. break;
  877. /* Local timers */
  878. case L0_TCNTB: case L0_ICNTB: case L0_FRCNTB:
  879. case L1_TCNTB: case L1_ICNTB: case L1_FRCNTB:
  880. lt_i = GET_L_TIMER_IDX(offset);
  881. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  882. value = s->l_timer[lt_i].reg.cnt[index];
  883. break;
  884. case L0_TCNTO: case L1_TCNTO:
  885. lt_i = GET_L_TIMER_IDX(offset);
  886. value = exynos4210_ltick_cnt_get_cnto(&s->l_timer[lt_i].tick_timer);
  887. DPRINTF("local timer[%d] read TCNTO %x\n", lt_i, value);
  888. break;
  889. case L0_ICNTO: case L1_ICNTO:
  890. lt_i = GET_L_TIMER_IDX(offset);
  891. value = exynos4210_ltick_int_get_cnto(&s->l_timer[lt_i].tick_timer);
  892. DPRINTF("local timer[%d] read ICNTO %x\n", lt_i, value);
  893. break;
  894. case L0_FRCNTO: case L1_FRCNTO:
  895. lt_i = GET_L_TIMER_IDX(offset);
  896. value = exynos4210_lfrc_get_count(&s->l_timer[lt_i]);
  897. break;
  898. case L0_TCON: case L1_TCON:
  899. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  900. value = s->l_timer[lt_i].reg.tcon;
  901. break;
  902. case L0_INT_CSTAT: case L1_INT_CSTAT:
  903. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  904. value = s->l_timer[lt_i].reg.int_cstat;
  905. break;
  906. case L0_INT_ENB: case L1_INT_ENB:
  907. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  908. value = s->l_timer[lt_i].reg.int_enb;
  909. break;
  910. case L0_WSTAT: case L1_WSTAT:
  911. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  912. value = s->l_timer[lt_i].reg.wstat;
  913. break;
  914. default:
  915. hw_error("exynos4210.mct: bad read offset "
  916. TARGET_FMT_plx "\n", offset);
  917. break;
  918. }
  919. return value;
  920. }
  921. /* MCT write */
  922. static void exynos4210_mct_write(void *opaque, target_phys_addr_t offset,
  923. uint64_t value, unsigned size)
  924. {
  925. Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
  926. int index; /* index in buffer which represents register set */
  927. int shift;
  928. int lt_i;
  929. uint64_t new_frc;
  930. uint32_t i;
  931. uint32_t old_val;
  932. #ifdef DEBUG_MCT
  933. static uint32_t icntb_max[2] = {0};
  934. static uint32_t icntb_min[2] = {UINT32_MAX, UINT32_MAX};
  935. static uint32_t tcntb_max[2] = {0};
  936. static uint32_t tcntb_min[2] = {UINT32_MAX, UINT32_MAX};
  937. #endif
  938. new_frc = s->g_timer.reg.cnt;
  939. switch (offset) {
  940. case MCT_CFG:
  941. s->reg_mct_cfg = value;
  942. exynos4210_mct_update_freq(s);
  943. break;
  944. case G_CNT_L:
  945. case G_CNT_U:
  946. if (offset == G_CNT_L) {
  947. DPRINTF("global timer write to reg.cntl %llx\n", value);
  948. new_frc = (s->g_timer.reg.cnt & (uint64_t)UINT32_MAX << 32) + value;
  949. s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_L;
  950. }
  951. if (offset == G_CNT_U) {
  952. DPRINTF("global timer write to reg.cntu %llx\n", value);
  953. new_frc = (s->g_timer.reg.cnt & UINT32_MAX) +
  954. ((uint64_t)value << 32);
  955. s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_U;
  956. }
  957. s->g_timer.reg.cnt = new_frc;
  958. exynos4210_gfrc_restart(s);
  959. break;
  960. case G_CNT_WSTAT:
  961. s->g_timer.reg.cnt_wstat &= ~(value);
  962. break;
  963. case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
  964. case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
  965. index = GET_G_COMP_IDX(offset);
  966. shift = 8 * (offset & 0x4);
  967. s->g_timer.reg.comp[index] =
  968. (s->g_timer.reg.comp[index] &
  969. (((uint64_t)UINT32_MAX << 32) >> shift)) +
  970. (value << shift);
  971. DPRINTF("comparator %d write 0x%llx val << %d\n", index, value, shift);
  972. if (offset&0x4) {
  973. s->g_timer.reg.wstat |= G_WSTAT_COMP_U(index);
  974. } else {
  975. s->g_timer.reg.wstat |= G_WSTAT_COMP_L(index);
  976. }
  977. exynos4210_gfrc_restart(s);
  978. break;
  979. case G_TCON:
  980. old_val = s->g_timer.reg.tcon;
  981. s->g_timer.reg.tcon = value;
  982. s->g_timer.reg.wstat |= G_WSTAT_TCON_WRITE;
  983. DPRINTF("global timer write to reg.g_tcon %llx\n", value);
  984. /* Start FRC if transition from disabled to enabled */
  985. if ((value & G_TCON_TIMER_ENABLE) > (old_val &
  986. G_TCON_TIMER_ENABLE)) {
  987. exynos4210_gfrc_start(&s->g_timer);
  988. }
  989. if ((value & G_TCON_TIMER_ENABLE) < (old_val &
  990. G_TCON_TIMER_ENABLE)) {
  991. exynos4210_gfrc_stop(&s->g_timer);
  992. }
  993. /* Start CMP if transition from disabled to enabled */
  994. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  995. if ((value & G_TCON_COMP_ENABLE(i)) != (old_val &
  996. G_TCON_COMP_ENABLE(i))) {
  997. exynos4210_gfrc_restart(s);
  998. }
  999. }
  1000. break;
  1001. case G_INT_CSTAT:
  1002. s->g_timer.reg.int_cstat &= ~(value);
  1003. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1004. if (value & G_INT_CSTAT_COMP(i)) {
  1005. exynos4210_gcomp_lower_irq(&s->g_timer, i);
  1006. }
  1007. }
  1008. break;
  1009. case G_INT_ENB:
  1010. /* Raise IRQ if transition from disabled to enabled and CSTAT pending */
  1011. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1012. if ((value & G_INT_ENABLE(i)) > (s->g_timer.reg.tcon &
  1013. G_INT_ENABLE(i))) {
  1014. if (s->g_timer.reg.int_cstat & G_INT_CSTAT_COMP(i)) {
  1015. exynos4210_gcomp_raise_irq(&s->g_timer, i);
  1016. }
  1017. }
  1018. if ((value & G_INT_ENABLE(i)) < (s->g_timer.reg.tcon &
  1019. G_INT_ENABLE(i))) {
  1020. exynos4210_gcomp_lower_irq(&s->g_timer, i);
  1021. }
  1022. }
  1023. DPRINTF("global timer INT enable %llx\n", value);
  1024. s->g_timer.reg.int_enb = value;
  1025. break;
  1026. case G_WSTAT:
  1027. s->g_timer.reg.wstat &= ~(value);
  1028. break;
  1029. case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
  1030. case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
  1031. index = GET_G_COMP_ADD_INCR_IDX(offset);
  1032. s->g_timer.reg.comp_add_incr[index] = value;
  1033. s->g_timer.reg.wstat |= G_WSTAT_COMP_ADDINCR(index);
  1034. break;
  1035. /* Local timers */
  1036. case L0_TCON: case L1_TCON:
  1037. lt_i = GET_L_TIMER_IDX(offset);
  1038. old_val = s->l_timer[lt_i].reg.tcon;
  1039. s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCON_WRITE;
  1040. s->l_timer[lt_i].reg.tcon = value;
  1041. /* Stop local CNT */
  1042. if ((value & L_TCON_TICK_START) <
  1043. (old_val & L_TCON_TICK_START)) {
  1044. DPRINTF("local timer[%d] stop cnt\n", lt_i);
  1045. exynos4210_ltick_cnt_stop(&s->l_timer[lt_i].tick_timer);
  1046. }
  1047. /* Stop local INT */
  1048. if ((value & L_TCON_INT_START) <
  1049. (old_val & L_TCON_INT_START)) {
  1050. DPRINTF("local timer[%d] stop int\n", lt_i);
  1051. exynos4210_ltick_int_stop(&s->l_timer[lt_i].tick_timer);
  1052. }
  1053. /* Start local CNT */
  1054. if ((value & L_TCON_TICK_START) >
  1055. (old_val & L_TCON_TICK_START)) {
  1056. DPRINTF("local timer[%d] start cnt\n", lt_i);
  1057. exynos4210_ltick_cnt_start(&s->l_timer[lt_i].tick_timer);
  1058. }
  1059. /* Start local INT */
  1060. if ((value & L_TCON_INT_START) >
  1061. (old_val & L_TCON_INT_START)) {
  1062. DPRINTF("local timer[%d] start int\n", lt_i);
  1063. exynos4210_ltick_int_start(&s->l_timer[lt_i].tick_timer);
  1064. }
  1065. /* Start or Stop local FRC if TCON changed */
  1066. if ((value & L_TCON_FRC_START) >
  1067. (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
  1068. DPRINTF("local timer[%d] start frc\n", lt_i);
  1069. exynos4210_lfrc_start(&s->l_timer[lt_i]);
  1070. }
  1071. if ((value & L_TCON_FRC_START) <
  1072. (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
  1073. DPRINTF("local timer[%d] stop frc\n", lt_i);
  1074. exynos4210_lfrc_stop(&s->l_timer[lt_i]);
  1075. }
  1076. break;
  1077. case L0_TCNTB: case L1_TCNTB:
  1078. lt_i = GET_L_TIMER_IDX(offset);
  1079. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1080. /*
  1081. * TCNTB is updated to internal register only after CNT expired.
  1082. * Due to this we should reload timer to nearest moment when CNT is
  1083. * expired and then in event handler update tcntb to new TCNTB value.
  1084. */
  1085. exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer, value,
  1086. s->l_timer[lt_i].tick_timer.icntb);
  1087. s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCNTB_WRITE;
  1088. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] = value;
  1089. #ifdef DEBUG_MCT
  1090. if (tcntb_min[lt_i] > value) {
  1091. tcntb_min[lt_i] = value;
  1092. }
  1093. if (tcntb_max[lt_i] < value) {
  1094. tcntb_max[lt_i] = value;
  1095. }
  1096. DPRINTF("local timer[%d] TCNTB write %llx; max=%x, min=%x\n",
  1097. lt_i, value, tcntb_max[lt_i], tcntb_min[lt_i]);
  1098. #endif
  1099. break;
  1100. case L0_ICNTB: case L1_ICNTB:
  1101. lt_i = GET_L_TIMER_IDX(offset);
  1102. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1103. s->l_timer[lt_i].reg.wstat |= L_WSTAT_ICNTB_WRITE;
  1104. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] = value &
  1105. ~L_ICNTB_MANUAL_UPDATE;
  1106. /*
  1107. * We need to avoid too small values for TCNTB*ICNTB. If not, IRQ event
  1108. * could raise too fast disallowing QEMU to execute target code.
  1109. */
  1110. if (s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] *
  1111. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] < MCT_LT_CNT_LOW_LIMIT) {
  1112. if (!s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB]) {
  1113. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
  1114. MCT_LT_CNT_LOW_LIMIT;
  1115. } else {
  1116. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
  1117. MCT_LT_CNT_LOW_LIMIT /
  1118. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB];
  1119. }
  1120. }
  1121. if (value & L_ICNTB_MANUAL_UPDATE) {
  1122. exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer,
  1123. s->l_timer[lt_i].tick_timer.tcntb,
  1124. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB]);
  1125. }
  1126. #ifdef DEBUG_MCT
  1127. if (icntb_min[lt_i] > value) {
  1128. icntb_min[lt_i] = value;
  1129. }
  1130. if (icntb_max[lt_i] < value) {
  1131. icntb_max[lt_i] = value;
  1132. }
  1133. DPRINTF("local timer[%d] ICNTB write %llx; max=%x, min=%x\n\n",
  1134. lt_i, value, icntb_max[lt_i], icntb_min[lt_i]);
  1135. #endif
  1136. break;
  1137. case L0_FRCNTB: case L1_FRCNTB:
  1138. lt_i = GET_L_TIMER_IDX(offset);
  1139. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1140. DPRINTF("local timer[%d] FRCNTB write %llx\n", lt_i, value);
  1141. s->l_timer[lt_i].reg.wstat |= L_WSTAT_FRCCNTB_WRITE;
  1142. s->l_timer[lt_i].reg.cnt[L_REG_CNT_FRCCNTB] = value;
  1143. break;
  1144. case L0_TCNTO: case L1_TCNTO:
  1145. case L0_ICNTO: case L1_ICNTO:
  1146. case L0_FRCNTO: case L1_FRCNTO:
  1147. fprintf(stderr, "\n[exynos4210.mct: write to RO register "
  1148. TARGET_FMT_plx "]\n\n", offset);
  1149. break;
  1150. case L0_INT_CSTAT: case L1_INT_CSTAT:
  1151. lt_i = GET_L_TIMER_IDX(offset);
  1152. DPRINTF("local timer[%d] CSTAT write %llx\n", lt_i, value);
  1153. s->l_timer[lt_i].reg.int_cstat &= ~value;
  1154. if (!s->l_timer[lt_i].reg.int_cstat) {
  1155. qemu_irq_lower(s->l_timer[lt_i].irq);
  1156. }
  1157. break;
  1158. case L0_INT_ENB: case L1_INT_ENB:
  1159. lt_i = GET_L_TIMER_IDX(offset);
  1160. old_val = s->l_timer[lt_i].reg.int_enb;
  1161. /* Raise Local timer IRQ if cstat is pending */
  1162. if ((value & L_INT_INTENB_ICNTEIE) > (old_val & L_INT_INTENB_ICNTEIE)) {
  1163. if (s->l_timer[lt_i].reg.int_cstat & L_INT_CSTAT_INTCNT) {
  1164. qemu_irq_raise(s->l_timer[lt_i].irq);
  1165. }
  1166. }
  1167. s->l_timer[lt_i].reg.int_enb = value;
  1168. break;
  1169. case L0_WSTAT: case L1_WSTAT:
  1170. lt_i = GET_L_TIMER_IDX(offset);
  1171. s->l_timer[lt_i].reg.wstat &= ~value;
  1172. break;
  1173. default:
  1174. hw_error("exynos4210.mct: bad write offset "
  1175. TARGET_FMT_plx "\n", offset);
  1176. break;
  1177. }
  1178. }
  1179. static const MemoryRegionOps exynos4210_mct_ops = {
  1180. .read = exynos4210_mct_read,
  1181. .write = exynos4210_mct_write,
  1182. .endianness = DEVICE_NATIVE_ENDIAN,
  1183. };
  1184. /* MCT init */
  1185. static int exynos4210_mct_init(SysBusDevice *dev)
  1186. {
  1187. int i;
  1188. Exynos4210MCTState *s = FROM_SYSBUS(Exynos4210MCTState, dev);
  1189. QEMUBH *bh[2];
  1190. /* Global timer */
  1191. bh[0] = qemu_bh_new(exynos4210_gfrc_event, s);
  1192. s->g_timer.ptimer_frc = ptimer_init(bh[0]);
  1193. memset(&s->g_timer.reg, 0, sizeof(struct gregs));
  1194. /* Local timers */
  1195. for (i = 0; i < 2; i++) {
  1196. bh[0] = qemu_bh_new(exynos4210_ltick_event, &s->l_timer[i]);
  1197. bh[1] = qemu_bh_new(exynos4210_lfrc_event, &s->l_timer[i]);
  1198. s->l_timer[i].tick_timer.ptimer_tick = ptimer_init(bh[0]);
  1199. s->l_timer[i].ptimer_frc = ptimer_init(bh[1]);
  1200. s->l_timer[i].id = i;
  1201. }
  1202. /* IRQs */
  1203. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1204. sysbus_init_irq(dev, &s->g_timer.irq[i]);
  1205. }
  1206. for (i = 0; i < 2; i++) {
  1207. sysbus_init_irq(dev, &s->l_timer[i].irq);
  1208. }
  1209. memory_region_init_io(&s->iomem, &exynos4210_mct_ops, s, "exynos4210-mct",
  1210. MCT_SFR_SIZE);
  1211. sysbus_init_mmio(dev, &s->iomem);
  1212. return 0;
  1213. }
  1214. static void exynos4210_mct_class_init(ObjectClass *klass, void *data)
  1215. {
  1216. DeviceClass *dc = DEVICE_CLASS(klass);
  1217. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  1218. k->init = exynos4210_mct_init;
  1219. dc->reset = exynos4210_mct_reset;
  1220. dc->vmsd = &vmstate_exynos4210_mct_state;
  1221. }
  1222. static TypeInfo exynos4210_mct_info = {
  1223. .name = "exynos4210.mct",
  1224. .parent = TYPE_SYS_BUS_DEVICE,
  1225. .instance_size = sizeof(Exynos4210MCTState),
  1226. .class_init = exynos4210_mct_class_init,
  1227. };
  1228. static void exynos4210_mct_register_types(void)
  1229. {
  1230. type_register_static(&exynos4210_mct_info);
  1231. }
  1232. type_init(exynos4210_mct_register_types)