2
0

exynos4210_mct.c 45 KB

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