exynos4210_mct.c 45 KB

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