exynos4210_mct.c 45 KB

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