exynos4210_mct.c 42 KB

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