exynos4210_mct.c 42 KB

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