exynos4210_mct.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 || !distance) {
  484. distance = MCT_GT_COUNTER_STEP;
  485. }
  486. exynos4210_gfrc_set_count(&s->g_timer, distance);
  487. exynos4210_gfrc_start(&s->g_timer);
  488. }
  489. /*
  490. * Get counter of FRC local timer.
  491. */
  492. static uint64_t exynos4210_lfrc_get_count(Exynos4210MCTLT *s)
  493. {
  494. return ptimer_get_count(s->ptimer_frc);
  495. }
  496. /*
  497. * Set counter of FRC local timer.
  498. */
  499. static void exynos4210_lfrc_update_count(Exynos4210MCTLT *s)
  500. {
  501. if (!s->reg.cnt[L_REG_CNT_FRCCNTB]) {
  502. ptimer_set_count(s->ptimer_frc, MCT_LT_COUNTER_STEP);
  503. } else {
  504. ptimer_set_count(s->ptimer_frc, s->reg.cnt[L_REG_CNT_FRCCNTB]);
  505. }
  506. }
  507. /*
  508. * Start local FRC timer
  509. */
  510. static void exynos4210_lfrc_start(Exynos4210MCTLT *s)
  511. {
  512. ptimer_run(s->ptimer_frc, 1);
  513. }
  514. /*
  515. * Stop local FRC timer
  516. */
  517. static void exynos4210_lfrc_stop(Exynos4210MCTLT *s)
  518. {
  519. ptimer_stop(s->ptimer_frc);
  520. }
  521. /*
  522. * Local timer free running counter tick handler
  523. */
  524. static void exynos4210_lfrc_event(void *opaque)
  525. {
  526. Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
  527. /* local frc expired */
  528. DPRINTF("\n");
  529. s->reg.int_cstat |= L_INT_CSTAT_FRCCNT;
  530. /* update frc counter */
  531. exynos4210_lfrc_update_count(s);
  532. /* raise irq */
  533. if (s->reg.int_enb & L_INT_INTENB_FRCEIE) {
  534. qemu_irq_raise(s->irq);
  535. }
  536. /* we reached here, this means that timer is enabled */
  537. exynos4210_lfrc_start(s);
  538. }
  539. static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s);
  540. static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s);
  541. static void exynos4210_ltick_recalc_count(struct tick_timer *s);
  542. /*
  543. * Action on enabling local tick int timer
  544. */
  545. static void exynos4210_ltick_int_start(struct tick_timer *s)
  546. {
  547. if (!s->int_run) {
  548. s->int_run = 1;
  549. }
  550. }
  551. /*
  552. * Action on disabling local tick int timer
  553. */
  554. static void exynos4210_ltick_int_stop(struct tick_timer *s)
  555. {
  556. if (s->int_run) {
  557. s->last_icnto = exynos4210_ltick_int_get_cnto(s);
  558. s->int_run = 0;
  559. }
  560. }
  561. /*
  562. * Get count for INT timer
  563. */
  564. static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s)
  565. {
  566. uint32_t icnto;
  567. uint64_t remain;
  568. uint64_t count;
  569. uint64_t counted;
  570. uint64_t cur_progress;
  571. count = ptimer_get_count(s->ptimer_tick);
  572. if (count) {
  573. /* timer is still counting, called not from event */
  574. counted = s->count - ptimer_get_count(s->ptimer_tick);
  575. cur_progress = s->progress + counted;
  576. } else {
  577. /* timer expired earlier */
  578. cur_progress = s->progress;
  579. }
  580. remain = s->distance - cur_progress;
  581. if (!s->int_run) {
  582. /* INT is stopped. */
  583. icnto = s->last_icnto;
  584. } else {
  585. /* Both are counting */
  586. icnto = remain / s->tcntb;
  587. }
  588. return icnto;
  589. }
  590. /*
  591. * Start local tick cnt timer.
  592. */
  593. static void exynos4210_ltick_cnt_start(struct tick_timer *s)
  594. {
  595. if (!s->cnt_run) {
  596. exynos4210_ltick_recalc_count(s);
  597. ptimer_set_count(s->ptimer_tick, s->count);
  598. ptimer_run(s->ptimer_tick, 1);
  599. s->cnt_run = 1;
  600. }
  601. }
  602. /*
  603. * Stop local tick cnt timer.
  604. */
  605. static void exynos4210_ltick_cnt_stop(struct tick_timer *s)
  606. {
  607. if (s->cnt_run) {
  608. s->last_tcnto = exynos4210_ltick_cnt_get_cnto(s);
  609. if (s->int_run) {
  610. exynos4210_ltick_int_stop(s);
  611. }
  612. ptimer_stop(s->ptimer_tick);
  613. s->cnt_run = 0;
  614. }
  615. }
  616. /*
  617. * Get counter for CNT timer
  618. */
  619. static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s)
  620. {
  621. uint32_t tcnto;
  622. uint32_t icnto;
  623. uint64_t remain;
  624. uint64_t counted;
  625. uint64_t count;
  626. uint64_t cur_progress;
  627. count = ptimer_get_count(s->ptimer_tick);
  628. if (count) {
  629. /* timer is still counting, called not from event */
  630. counted = s->count - ptimer_get_count(s->ptimer_tick);
  631. cur_progress = s->progress + counted;
  632. } else {
  633. /* timer expired earlier */
  634. cur_progress = s->progress;
  635. }
  636. remain = s->distance - cur_progress;
  637. if (!s->cnt_run) {
  638. /* Both are stopped. */
  639. tcnto = s->last_tcnto;
  640. } else if (!s->int_run) {
  641. /* INT counter is stopped, progress is by CNT timer */
  642. tcnto = remain % s->tcntb;
  643. } else {
  644. /* Both are counting */
  645. icnto = remain / s->tcntb;
  646. if (icnto) {
  647. tcnto = remain % (icnto * s->tcntb);
  648. } else {
  649. tcnto = remain % s->tcntb;
  650. }
  651. }
  652. return tcnto;
  653. }
  654. /*
  655. * Set new values of counters for CNT and INT timers
  656. */
  657. static void exynos4210_ltick_set_cntb(struct tick_timer *s, uint32_t new_cnt,
  658. uint32_t new_int)
  659. {
  660. uint32_t cnt_stopped = 0;
  661. uint32_t int_stopped = 0;
  662. if (s->cnt_run) {
  663. exynos4210_ltick_cnt_stop(s);
  664. cnt_stopped = 1;
  665. }
  666. if (s->int_run) {
  667. exynos4210_ltick_int_stop(s);
  668. int_stopped = 1;
  669. }
  670. s->tcntb = new_cnt + 1;
  671. s->icntb = new_int + 1;
  672. if (cnt_stopped) {
  673. exynos4210_ltick_cnt_start(s);
  674. }
  675. if (int_stopped) {
  676. exynos4210_ltick_int_start(s);
  677. }
  678. }
  679. /*
  680. * Calculate new counter value for tick timer
  681. */
  682. static void exynos4210_ltick_recalc_count(struct tick_timer *s)
  683. {
  684. uint64_t to_count;
  685. if ((s->cnt_run && s->last_tcnto) || (s->int_run && s->last_icnto)) {
  686. /*
  687. * one or both timers run and not counted to the end;
  688. * distance is not passed, recalculate with last_tcnto * last_icnto
  689. */
  690. if (s->last_tcnto) {
  691. to_count = s->last_tcnto * s->last_icnto;
  692. } else {
  693. to_count = s->last_icnto;
  694. }
  695. } else {
  696. /* distance is passed, recalculate with tcnto * icnto */
  697. if (s->icntb) {
  698. s->distance = s->tcntb * s->icntb;
  699. } else {
  700. s->distance = s->tcntb;
  701. }
  702. to_count = s->distance;
  703. s->progress = 0;
  704. }
  705. if (to_count > MCT_LT_COUNTER_STEP) {
  706. /* count by step */
  707. s->count = MCT_LT_COUNTER_STEP;
  708. } else {
  709. s->count = to_count;
  710. }
  711. }
  712. /*
  713. * Initialize tick_timer
  714. */
  715. static void exynos4210_ltick_timer_init(struct tick_timer *s)
  716. {
  717. exynos4210_ltick_int_stop(s);
  718. exynos4210_ltick_cnt_stop(s);
  719. s->count = 0;
  720. s->distance = 0;
  721. s->progress = 0;
  722. s->icntb = 0;
  723. s->tcntb = 0;
  724. }
  725. /*
  726. * tick_timer event.
  727. * Raises when abstract tick_timer expires.
  728. */
  729. static void exynos4210_ltick_timer_event(struct tick_timer *s)
  730. {
  731. s->progress += s->count;
  732. }
  733. /*
  734. * Local timer tick counter handler.
  735. * Don't use reloaded timers. If timer counter = zero
  736. * then handler called but after handler finished no
  737. * timer reload occurs.
  738. */
  739. static void exynos4210_ltick_event(void *opaque)
  740. {
  741. Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
  742. uint32_t tcnto;
  743. uint32_t icnto;
  744. #ifdef DEBUG_MCT
  745. static uint64_t time1[2] = {0};
  746. static uint64_t time2[2] = {0};
  747. #endif
  748. /* Call tick_timer event handler, it will update its tcntb and icntb. */
  749. exynos4210_ltick_timer_event(&s->tick_timer);
  750. /* get tick_timer cnt */
  751. tcnto = exynos4210_ltick_cnt_get_cnto(&s->tick_timer);
  752. /* get tick_timer int */
  753. icnto = exynos4210_ltick_int_get_cnto(&s->tick_timer);
  754. /* raise IRQ if needed */
  755. if (!icnto && s->reg.tcon & L_TCON_INT_START) {
  756. /* INT counter enabled and expired */
  757. s->reg.int_cstat |= L_INT_CSTAT_INTCNT;
  758. /* raise interrupt if enabled */
  759. if (s->reg.int_enb & L_INT_INTENB_ICNTEIE) {
  760. #ifdef DEBUG_MCT
  761. time2[s->id] = qemu_get_clock_ns(vm_clock);
  762. DPRINTF("local timer[%d] IRQ: %llx\n", s->id,
  763. time2[s->id] - time1[s->id]);
  764. time1[s->id] = time2[s->id];
  765. #endif
  766. qemu_irq_raise(s->irq);
  767. }
  768. /* reload ICNTB */
  769. if (s->reg.tcon & L_TCON_INTERVAL_MODE) {
  770. exynos4210_ltick_set_cntb(&s->tick_timer,
  771. s->reg.cnt[L_REG_CNT_TCNTB],
  772. s->reg.cnt[L_REG_CNT_ICNTB]);
  773. }
  774. } else {
  775. /* reload TCNTB */
  776. if (!tcnto) {
  777. exynos4210_ltick_set_cntb(&s->tick_timer,
  778. s->reg.cnt[L_REG_CNT_TCNTB],
  779. icnto);
  780. }
  781. }
  782. /* start tick_timer cnt */
  783. exynos4210_ltick_cnt_start(&s->tick_timer);
  784. /* start tick_timer int */
  785. exynos4210_ltick_int_start(&s->tick_timer);
  786. }
  787. /* update timer frequency */
  788. static void exynos4210_mct_update_freq(Exynos4210MCTState *s)
  789. {
  790. uint32_t freq = s->freq;
  791. s->freq = 24000000 /
  792. ((MCT_CFG_GET_PRESCALER(s->reg_mct_cfg)+1) *
  793. MCT_CFG_GET_DIVIDER(s->reg_mct_cfg));
  794. if (freq != s->freq) {
  795. DPRINTF("freq=%dHz\n", s->freq);
  796. /* global timer */
  797. ptimer_set_freq(s->g_timer.ptimer_frc, s->freq);
  798. /* local timer */
  799. ptimer_set_freq(s->l_timer[0].tick_timer.ptimer_tick, s->freq);
  800. ptimer_set_freq(s->l_timer[0].ptimer_frc, s->freq);
  801. ptimer_set_freq(s->l_timer[1].tick_timer.ptimer_tick, s->freq);
  802. ptimer_set_freq(s->l_timer[1].ptimer_frc, s->freq);
  803. }
  804. }
  805. /* set defaul_timer values for all fields */
  806. static void exynos4210_mct_reset(DeviceState *d)
  807. {
  808. Exynos4210MCTState *s = (Exynos4210MCTState *)d;
  809. uint32_t i;
  810. s->reg_mct_cfg = 0;
  811. /* global timer */
  812. memset(&s->g_timer.reg, 0, sizeof(s->g_timer.reg));
  813. exynos4210_gfrc_stop(&s->g_timer);
  814. /* local timer */
  815. memset(s->l_timer[0].reg.cnt, 0, sizeof(s->l_timer[0].reg.cnt));
  816. memset(s->l_timer[1].reg.cnt, 0, sizeof(s->l_timer[1].reg.cnt));
  817. for (i = 0; i < 2; i++) {
  818. s->l_timer[i].reg.int_cstat = 0;
  819. s->l_timer[i].reg.int_enb = 0;
  820. s->l_timer[i].reg.tcon = 0;
  821. s->l_timer[i].reg.wstat = 0;
  822. s->l_timer[i].tick_timer.count = 0;
  823. s->l_timer[i].tick_timer.distance = 0;
  824. s->l_timer[i].tick_timer.progress = 0;
  825. ptimer_stop(s->l_timer[i].ptimer_frc);
  826. exynos4210_ltick_timer_init(&s->l_timer[i].tick_timer);
  827. }
  828. exynos4210_mct_update_freq(s);
  829. }
  830. /* Multi Core Timer read */
  831. static uint64_t exynos4210_mct_read(void *opaque, hwaddr offset,
  832. unsigned size)
  833. {
  834. Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
  835. int index;
  836. int shift;
  837. uint64_t count;
  838. uint32_t value;
  839. int lt_i;
  840. switch (offset) {
  841. case MCT_CFG:
  842. value = s->reg_mct_cfg;
  843. break;
  844. case G_CNT_L: case G_CNT_U:
  845. shift = 8 * (offset & 0x4);
  846. count = exynos4210_gfrc_get_count(&s->g_timer);
  847. value = UINT32_MAX & (count >> shift);
  848. DPRINTF("read FRC=0x%llx\n", count);
  849. break;
  850. case G_CNT_WSTAT:
  851. value = s->g_timer.reg.cnt_wstat;
  852. break;
  853. case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
  854. case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
  855. index = GET_G_COMP_IDX(offset);
  856. shift = 8 * (offset & 0x4);
  857. value = UINT32_MAX & (s->g_timer.reg.comp[index] >> shift);
  858. break;
  859. case G_TCON:
  860. value = s->g_timer.reg.tcon;
  861. break;
  862. case G_INT_CSTAT:
  863. value = s->g_timer.reg.int_cstat;
  864. break;
  865. case G_INT_ENB:
  866. value = s->g_timer.reg.int_enb;
  867. break;
  868. break;
  869. case G_WSTAT:
  870. value = s->g_timer.reg.wstat;
  871. break;
  872. case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
  873. case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
  874. value = s->g_timer.reg.comp_add_incr[GET_G_COMP_ADD_INCR_IDX(offset)];
  875. break;
  876. /* Local timers */
  877. case L0_TCNTB: case L0_ICNTB: case L0_FRCNTB:
  878. case L1_TCNTB: case L1_ICNTB: case L1_FRCNTB:
  879. lt_i = GET_L_TIMER_IDX(offset);
  880. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  881. value = s->l_timer[lt_i].reg.cnt[index];
  882. break;
  883. case L0_TCNTO: case L1_TCNTO:
  884. lt_i = GET_L_TIMER_IDX(offset);
  885. value = exynos4210_ltick_cnt_get_cnto(&s->l_timer[lt_i].tick_timer);
  886. DPRINTF("local timer[%d] read TCNTO %x\n", lt_i, value);
  887. break;
  888. case L0_ICNTO: case L1_ICNTO:
  889. lt_i = GET_L_TIMER_IDX(offset);
  890. value = exynos4210_ltick_int_get_cnto(&s->l_timer[lt_i].tick_timer);
  891. DPRINTF("local timer[%d] read ICNTO %x\n", lt_i, value);
  892. break;
  893. case L0_FRCNTO: case L1_FRCNTO:
  894. lt_i = GET_L_TIMER_IDX(offset);
  895. value = exynos4210_lfrc_get_count(&s->l_timer[lt_i]);
  896. break;
  897. case L0_TCON: case L1_TCON:
  898. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  899. value = s->l_timer[lt_i].reg.tcon;
  900. break;
  901. case L0_INT_CSTAT: case L1_INT_CSTAT:
  902. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  903. value = s->l_timer[lt_i].reg.int_cstat;
  904. break;
  905. case L0_INT_ENB: case L1_INT_ENB:
  906. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  907. value = s->l_timer[lt_i].reg.int_enb;
  908. break;
  909. case L0_WSTAT: case L1_WSTAT:
  910. lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
  911. value = s->l_timer[lt_i].reg.wstat;
  912. break;
  913. default:
  914. hw_error("exynos4210.mct: bad read offset "
  915. TARGET_FMT_plx "\n", offset);
  916. break;
  917. }
  918. return value;
  919. }
  920. /* MCT write */
  921. static void exynos4210_mct_write(void *opaque, hwaddr offset,
  922. uint64_t value, unsigned size)
  923. {
  924. Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
  925. int index; /* index in buffer which represents register set */
  926. int shift;
  927. int lt_i;
  928. uint64_t new_frc;
  929. uint32_t i;
  930. uint32_t old_val;
  931. #ifdef DEBUG_MCT
  932. static uint32_t icntb_max[2] = {0};
  933. static uint32_t icntb_min[2] = {UINT32_MAX, UINT32_MAX};
  934. static uint32_t tcntb_max[2] = {0};
  935. static uint32_t tcntb_min[2] = {UINT32_MAX, UINT32_MAX};
  936. #endif
  937. new_frc = s->g_timer.reg.cnt;
  938. switch (offset) {
  939. case MCT_CFG:
  940. s->reg_mct_cfg = value;
  941. exynos4210_mct_update_freq(s);
  942. break;
  943. case G_CNT_L:
  944. case G_CNT_U:
  945. if (offset == G_CNT_L) {
  946. DPRINTF("global timer write to reg.cntl %llx\n", value);
  947. new_frc = (s->g_timer.reg.cnt & (uint64_t)UINT32_MAX << 32) + value;
  948. s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_L;
  949. }
  950. if (offset == G_CNT_U) {
  951. DPRINTF("global timer write to reg.cntu %llx\n", value);
  952. new_frc = (s->g_timer.reg.cnt & UINT32_MAX) +
  953. ((uint64_t)value << 32);
  954. s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_U;
  955. }
  956. s->g_timer.reg.cnt = new_frc;
  957. exynos4210_gfrc_restart(s);
  958. break;
  959. case G_CNT_WSTAT:
  960. s->g_timer.reg.cnt_wstat &= ~(value);
  961. break;
  962. case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
  963. case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
  964. index = GET_G_COMP_IDX(offset);
  965. shift = 8 * (offset & 0x4);
  966. s->g_timer.reg.comp[index] =
  967. (s->g_timer.reg.comp[index] &
  968. (((uint64_t)UINT32_MAX << 32) >> shift)) +
  969. (value << shift);
  970. DPRINTF("comparator %d write 0x%llx val << %d\n", index, value, shift);
  971. if (offset&0x4) {
  972. s->g_timer.reg.wstat |= G_WSTAT_COMP_U(index);
  973. } else {
  974. s->g_timer.reg.wstat |= G_WSTAT_COMP_L(index);
  975. }
  976. exynos4210_gfrc_restart(s);
  977. break;
  978. case G_TCON:
  979. old_val = s->g_timer.reg.tcon;
  980. s->g_timer.reg.tcon = value;
  981. s->g_timer.reg.wstat |= G_WSTAT_TCON_WRITE;
  982. DPRINTF("global timer write to reg.g_tcon %llx\n", value);
  983. /* Start FRC if transition from disabled to enabled */
  984. if ((value & G_TCON_TIMER_ENABLE) > (old_val &
  985. G_TCON_TIMER_ENABLE)) {
  986. exynos4210_gfrc_start(&s->g_timer);
  987. }
  988. if ((value & G_TCON_TIMER_ENABLE) < (old_val &
  989. G_TCON_TIMER_ENABLE)) {
  990. exynos4210_gfrc_stop(&s->g_timer);
  991. }
  992. /* Start CMP if transition from disabled to enabled */
  993. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  994. if ((value & G_TCON_COMP_ENABLE(i)) != (old_val &
  995. G_TCON_COMP_ENABLE(i))) {
  996. exynos4210_gfrc_restart(s);
  997. }
  998. }
  999. break;
  1000. case G_INT_CSTAT:
  1001. s->g_timer.reg.int_cstat &= ~(value);
  1002. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1003. if (value & G_INT_CSTAT_COMP(i)) {
  1004. exynos4210_gcomp_lower_irq(&s->g_timer, i);
  1005. }
  1006. }
  1007. break;
  1008. case G_INT_ENB:
  1009. /* Raise IRQ if transition from disabled to enabled and CSTAT pending */
  1010. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1011. if ((value & G_INT_ENABLE(i)) > (s->g_timer.reg.tcon &
  1012. G_INT_ENABLE(i))) {
  1013. if (s->g_timer.reg.int_cstat & G_INT_CSTAT_COMP(i)) {
  1014. exynos4210_gcomp_raise_irq(&s->g_timer, i);
  1015. }
  1016. }
  1017. if ((value & G_INT_ENABLE(i)) < (s->g_timer.reg.tcon &
  1018. G_INT_ENABLE(i))) {
  1019. exynos4210_gcomp_lower_irq(&s->g_timer, i);
  1020. }
  1021. }
  1022. DPRINTF("global timer INT enable %llx\n", value);
  1023. s->g_timer.reg.int_enb = value;
  1024. break;
  1025. case G_WSTAT:
  1026. s->g_timer.reg.wstat &= ~(value);
  1027. break;
  1028. case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
  1029. case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
  1030. index = GET_G_COMP_ADD_INCR_IDX(offset);
  1031. s->g_timer.reg.comp_add_incr[index] = value;
  1032. s->g_timer.reg.wstat |= G_WSTAT_COMP_ADDINCR(index);
  1033. break;
  1034. /* Local timers */
  1035. case L0_TCON: case L1_TCON:
  1036. lt_i = GET_L_TIMER_IDX(offset);
  1037. old_val = s->l_timer[lt_i].reg.tcon;
  1038. s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCON_WRITE;
  1039. s->l_timer[lt_i].reg.tcon = value;
  1040. /* Stop local CNT */
  1041. if ((value & L_TCON_TICK_START) <
  1042. (old_val & L_TCON_TICK_START)) {
  1043. DPRINTF("local timer[%d] stop cnt\n", lt_i);
  1044. exynos4210_ltick_cnt_stop(&s->l_timer[lt_i].tick_timer);
  1045. }
  1046. /* Stop local INT */
  1047. if ((value & L_TCON_INT_START) <
  1048. (old_val & L_TCON_INT_START)) {
  1049. DPRINTF("local timer[%d] stop int\n", lt_i);
  1050. exynos4210_ltick_int_stop(&s->l_timer[lt_i].tick_timer);
  1051. }
  1052. /* Start local CNT */
  1053. if ((value & L_TCON_TICK_START) >
  1054. (old_val & L_TCON_TICK_START)) {
  1055. DPRINTF("local timer[%d] start cnt\n", lt_i);
  1056. exynos4210_ltick_cnt_start(&s->l_timer[lt_i].tick_timer);
  1057. }
  1058. /* Start local INT */
  1059. if ((value & L_TCON_INT_START) >
  1060. (old_val & L_TCON_INT_START)) {
  1061. DPRINTF("local timer[%d] start int\n", lt_i);
  1062. exynos4210_ltick_int_start(&s->l_timer[lt_i].tick_timer);
  1063. }
  1064. /* Start or Stop local FRC if TCON changed */
  1065. if ((value & L_TCON_FRC_START) >
  1066. (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
  1067. DPRINTF("local timer[%d] start frc\n", lt_i);
  1068. exynos4210_lfrc_start(&s->l_timer[lt_i]);
  1069. }
  1070. if ((value & L_TCON_FRC_START) <
  1071. (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
  1072. DPRINTF("local timer[%d] stop frc\n", lt_i);
  1073. exynos4210_lfrc_stop(&s->l_timer[lt_i]);
  1074. }
  1075. break;
  1076. case L0_TCNTB: case L1_TCNTB:
  1077. lt_i = GET_L_TIMER_IDX(offset);
  1078. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1079. /*
  1080. * TCNTB is updated to internal register only after CNT expired.
  1081. * Due to this we should reload timer to nearest moment when CNT is
  1082. * expired and then in event handler update tcntb to new TCNTB value.
  1083. */
  1084. exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer, value,
  1085. s->l_timer[lt_i].tick_timer.icntb);
  1086. s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCNTB_WRITE;
  1087. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] = value;
  1088. #ifdef DEBUG_MCT
  1089. if (tcntb_min[lt_i] > value) {
  1090. tcntb_min[lt_i] = value;
  1091. }
  1092. if (tcntb_max[lt_i] < value) {
  1093. tcntb_max[lt_i] = value;
  1094. }
  1095. DPRINTF("local timer[%d] TCNTB write %llx; max=%x, min=%x\n",
  1096. lt_i, value, tcntb_max[lt_i], tcntb_min[lt_i]);
  1097. #endif
  1098. break;
  1099. case L0_ICNTB: case L1_ICNTB:
  1100. lt_i = GET_L_TIMER_IDX(offset);
  1101. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1102. s->l_timer[lt_i].reg.wstat |= L_WSTAT_ICNTB_WRITE;
  1103. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] = value &
  1104. ~L_ICNTB_MANUAL_UPDATE;
  1105. /*
  1106. * We need to avoid too small values for TCNTB*ICNTB. If not, IRQ event
  1107. * could raise too fast disallowing QEMU to execute target code.
  1108. */
  1109. if (s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] *
  1110. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] < MCT_LT_CNT_LOW_LIMIT) {
  1111. if (!s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB]) {
  1112. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
  1113. MCT_LT_CNT_LOW_LIMIT;
  1114. } else {
  1115. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
  1116. MCT_LT_CNT_LOW_LIMIT /
  1117. s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB];
  1118. }
  1119. }
  1120. if (value & L_ICNTB_MANUAL_UPDATE) {
  1121. exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer,
  1122. s->l_timer[lt_i].tick_timer.tcntb,
  1123. s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB]);
  1124. }
  1125. #ifdef DEBUG_MCT
  1126. if (icntb_min[lt_i] > value) {
  1127. icntb_min[lt_i] = value;
  1128. }
  1129. if (icntb_max[lt_i] < value) {
  1130. icntb_max[lt_i] = value;
  1131. }
  1132. DPRINTF("local timer[%d] ICNTB write %llx; max=%x, min=%x\n\n",
  1133. lt_i, value, icntb_max[lt_i], icntb_min[lt_i]);
  1134. #endif
  1135. break;
  1136. case L0_FRCNTB: case L1_FRCNTB:
  1137. lt_i = GET_L_TIMER_IDX(offset);
  1138. index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
  1139. DPRINTF("local timer[%d] FRCNTB write %llx\n", lt_i, value);
  1140. s->l_timer[lt_i].reg.wstat |= L_WSTAT_FRCCNTB_WRITE;
  1141. s->l_timer[lt_i].reg.cnt[L_REG_CNT_FRCCNTB] = value;
  1142. break;
  1143. case L0_TCNTO: case L1_TCNTO:
  1144. case L0_ICNTO: case L1_ICNTO:
  1145. case L0_FRCNTO: case L1_FRCNTO:
  1146. fprintf(stderr, "\n[exynos4210.mct: write to RO register "
  1147. TARGET_FMT_plx "]\n\n", offset);
  1148. break;
  1149. case L0_INT_CSTAT: case L1_INT_CSTAT:
  1150. lt_i = GET_L_TIMER_IDX(offset);
  1151. DPRINTF("local timer[%d] CSTAT write %llx\n", lt_i, value);
  1152. s->l_timer[lt_i].reg.int_cstat &= ~value;
  1153. if (!s->l_timer[lt_i].reg.int_cstat) {
  1154. qemu_irq_lower(s->l_timer[lt_i].irq);
  1155. }
  1156. break;
  1157. case L0_INT_ENB: case L1_INT_ENB:
  1158. lt_i = GET_L_TIMER_IDX(offset);
  1159. old_val = s->l_timer[lt_i].reg.int_enb;
  1160. /* Raise Local timer IRQ if cstat is pending */
  1161. if ((value & L_INT_INTENB_ICNTEIE) > (old_val & L_INT_INTENB_ICNTEIE)) {
  1162. if (s->l_timer[lt_i].reg.int_cstat & L_INT_CSTAT_INTCNT) {
  1163. qemu_irq_raise(s->l_timer[lt_i].irq);
  1164. }
  1165. }
  1166. s->l_timer[lt_i].reg.int_enb = value;
  1167. break;
  1168. case L0_WSTAT: case L1_WSTAT:
  1169. lt_i = GET_L_TIMER_IDX(offset);
  1170. s->l_timer[lt_i].reg.wstat &= ~value;
  1171. break;
  1172. default:
  1173. hw_error("exynos4210.mct: bad write offset "
  1174. TARGET_FMT_plx "\n", offset);
  1175. break;
  1176. }
  1177. }
  1178. static const MemoryRegionOps exynos4210_mct_ops = {
  1179. .read = exynos4210_mct_read,
  1180. .write = exynos4210_mct_write,
  1181. .endianness = DEVICE_NATIVE_ENDIAN,
  1182. };
  1183. /* MCT init */
  1184. static int exynos4210_mct_init(SysBusDevice *dev)
  1185. {
  1186. int i;
  1187. Exynos4210MCTState *s = FROM_SYSBUS(Exynos4210MCTState, dev);
  1188. QEMUBH *bh[2];
  1189. /* Global timer */
  1190. bh[0] = qemu_bh_new(exynos4210_gfrc_event, s);
  1191. s->g_timer.ptimer_frc = ptimer_init(bh[0]);
  1192. memset(&s->g_timer.reg, 0, sizeof(struct gregs));
  1193. /* Local timers */
  1194. for (i = 0; i < 2; i++) {
  1195. bh[0] = qemu_bh_new(exynos4210_ltick_event, &s->l_timer[i]);
  1196. bh[1] = qemu_bh_new(exynos4210_lfrc_event, &s->l_timer[i]);
  1197. s->l_timer[i].tick_timer.ptimer_tick = ptimer_init(bh[0]);
  1198. s->l_timer[i].ptimer_frc = ptimer_init(bh[1]);
  1199. s->l_timer[i].id = i;
  1200. }
  1201. /* IRQs */
  1202. for (i = 0; i < MCT_GT_CMP_NUM; i++) {
  1203. sysbus_init_irq(dev, &s->g_timer.irq[i]);
  1204. }
  1205. for (i = 0; i < 2; i++) {
  1206. sysbus_init_irq(dev, &s->l_timer[i].irq);
  1207. }
  1208. memory_region_init_io(&s->iomem, &exynos4210_mct_ops, s, "exynos4210-mct",
  1209. MCT_SFR_SIZE);
  1210. sysbus_init_mmio(dev, &s->iomem);
  1211. return 0;
  1212. }
  1213. static void exynos4210_mct_class_init(ObjectClass *klass, void *data)
  1214. {
  1215. DeviceClass *dc = DEVICE_CLASS(klass);
  1216. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  1217. k->init = exynos4210_mct_init;
  1218. dc->reset = exynos4210_mct_reset;
  1219. dc->vmsd = &vmstate_exynos4210_mct_state;
  1220. }
  1221. static const TypeInfo exynos4210_mct_info = {
  1222. .name = "exynos4210.mct",
  1223. .parent = TYPE_SYS_BUS_DEVICE,
  1224. .instance_size = sizeof(Exynos4210MCTState),
  1225. .class_init = exynos4210_mct_class_init,
  1226. };
  1227. static void exynos4210_mct_register_types(void)
  1228. {
  1229. type_register_static(&exynos4210_mct_info);
  1230. }
  1231. type_init(exynos4210_mct_register_types)