meego-device-adaptation import version
[sgx.git] / pvr / sysutils.c
1 /**********************************************************************
2  *
3  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful but, except
10  * as otherwise stated in writing, without any warranty; without even the
11  * implied warranty of merchantability or fitness for a particular purpose.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23  * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
24  *
25  ******************************************************************************/
26
27 #include <linux/version.h>
28 #include <linux/clk.h>
29 #include <linux/err.h>
30 #include <linux/hardirq.h>
31 #include <plat/omap-pm.h>
32 #include <linux/bug.h>
33 #include <plat/clock.h>
34 #include <plat/cpu.h>
35 #include "sgxdefs.h"
36 #include "services_headers.h"
37 #include "sysinfo.h"
38 #include "sgxapi_km.h"
39 #include "sysconfig.h"
40 #include "sgxinfokm.h"
41 #include "syslocal.h"
42 #include "env_data.h"
43 #include "ocpdefs.h"
44
45 #define HZ_TO_MHZ(m) ((m) / 1000000)
46
47 static inline unsigned long scale_by_rate(unsigned long val,
48                                           unsigned long rate1,
49                                           unsigned long rate2)
50 {
51         if (rate1 >= rate2)
52                 return val * (rate1 / rate2);
53
54         return val / (rate2 / rate1);
55 }
56
57 static inline unsigned long scale_prop_to_SGX_clock(unsigned long val,
58                                                     unsigned long rate)
59 {
60         return scale_by_rate(val, rate, sgx_get_max_freq());
61 }
62
63 void SysGetSGXTimingInformation(struct SGX_TIMING_INFORMATION *psTimingInfo)
64 {
65         unsigned long rate;
66
67 #if defined(NO_HARDWARE)
68         rate = SYS_SGX_MAX_FREQ_NO_HW;
69 #else
70         PVR_ASSERT(atomic_read(&gpsSysSpecificData->sSGXClocksEnabled) != 0);
71
72         rate = clk_get_rate(gpsSysSpecificData->psSGX_FCK);
73         PVR_ASSERT(rate != 0);
74 #endif
75         psTimingInfo->ui32CoreClockSpeed = rate;
76         psTimingInfo->ui32HWRecoveryFreq =
77             scale_prop_to_SGX_clock(SYS_SGX_HWRECOVERY_TIMEOUT_FREQ, rate);
78         psTimingInfo->ui32uKernelFreq =
79             scale_prop_to_SGX_clock(SYS_SGX_PDS_TIMER_FREQ, rate);
80         psTimingInfo->ui32ActivePowManLatencyms =
81             SYS_SGX_ACTIVE_POWER_LATENCY_MS;
82 }
83
84 static int vdd2_post_func(struct notifier_block *n, unsigned long event,
85                           void *ptr)
86 {
87         PVR_UNREFERENCED_PARAMETER(n);
88         PVR_UNREFERENCED_PARAMETER(event);
89         PVR_UNREFERENCED_PARAMETER(ptr);
90
91         if (atomic_read(&gpsSysSpecificData->sSGXClocksEnabled) != 0 &&
92             gpsSysSpecificData->bSGXInitComplete) {
93 #if defined(DEBUG)
94                 unsigned long rate;
95
96                 rate = clk_get_rate(gpsSysSpecificData->psSGX_FCK);
97
98                 PVR_ASSERT(rate != 0);
99
100                 PVR_TRACE("%s: SGX clock rate: %dMHz", __func__,
101                            HZ_TO_MHZ(rate));
102 #endif
103                 PVRSRVDevicePostClockSpeedChange(gpsSysSpecificData->
104                                                  psSGXDevNode->sDevId.
105                                                  ui32DeviceIndex, IMG_TRUE,
106                                                  NULL);
107         }
108         return 0;
109 }
110
111 static int vdd2_pre_func(struct notifier_block *n, unsigned long event,
112                          void *ptr)
113 {
114         PVR_UNREFERENCED_PARAMETER(n);
115         PVR_UNREFERENCED_PARAMETER(event);
116         PVR_UNREFERENCED_PARAMETER(ptr);
117
118         if (atomic_read(&gpsSysSpecificData->sSGXClocksEnabled) != 0 &&
119             gpsSysSpecificData->bSGXInitComplete) {
120                 BUG_ON(gpsSysData->eCurrentPowerState > PVRSRV_POWER_STATE_D1);
121                 PVRSRVDevicePreClockSpeedChange(gpsSysSpecificData->
122                                                 psSGXDevNode->sDevId.
123                                                 ui32DeviceIndex, IMG_TRUE,
124                                                 NULL);
125         }
126
127         return 0;
128 }
129
130 static int vdd2_pre_post_func(struct notifier_block *n, unsigned long event,
131                               void *ptr)
132 {
133         struct clk_notifier_data *cnd;
134
135         PVR_UNREFERENCED_PARAMETER(n);
136
137         cnd = (struct clk_notifier_data *)ptr;
138
139         PVR_TRACE("vdd2_pre_post_func: %s clock rate = %lu",
140                   (CLK_PRE_RATE_CHANGE == event) ? "old" :
141                   (CLK_POST_RATE_CHANGE == event) ? "new" :
142                   "???",
143                   cnd->rate);
144
145         if (CLK_PRE_RATE_CHANGE == event) {
146                 PVRSRVDvfsLock();
147                 PVR_TRACE("vdd2_pre_post_func: CLK_PRE_RATE_CHANGE event");
148                 vdd2_pre_func(n, event, ptr);
149         } else if (CLK_POST_RATE_CHANGE == event) {
150                 PVR_TRACE("vdd2_pre_post_func: CLK_POST_RATE_CHANGE event");
151                 vdd2_post_func(n, event, ptr);
152                 PVRSRVDvfsUnlock();
153         } else if (CLK_ABORT_RATE_CHANGE == event) {
154                 PVR_TRACE("vdd2_pre_post_func: CLK_ABORT_RATE_CHANGE event");
155                 PVRSRVDvfsUnlock();
156         } else {
157                 printk(KERN_ERR "vdd2_pre_post_func: unexpected event (%lu)\n",
158                         event);
159                 PVR_DPF(PVR_DBG_ERROR,
160                          "vdd2_pre_post_func: unexpected event (%lu)", event);
161         }
162         PVR_TRACE("vdd2_pre_post_func end.");
163         return 0;
164 }
165
166 static struct notifier_block vdd2_pre_post = {
167         vdd2_pre_post_func,
168         NULL
169 };
170
171 static void RegisterConstraintNotifications(struct SYS_SPECIFIC_DATA
172                                             *psSysSpecData)
173 {
174         PVR_TRACE("Registering constraint notifications");
175
176         clk_notifier_register(psSysSpecData->psSGX_FCK, &vdd2_pre_post);
177         PVR_TRACE("VDD2 constraint notifications registered");
178 }
179
180 static void UnRegisterConstraintNotifications(struct SYS_SPECIFIC_DATA
181                                               *psSysSpecData)
182 {
183         PVR_TRACE("Unregistering constraint notifications");
184
185         clk_notifier_unregister(psSysSpecData->psSGX_FCK, &vdd2_pre_post);
186 }
187
188 static struct device sgx_dev;
189 static int sgx_clock_enabled;
190
191 /* return value: current sgx load
192  * 0 - not busy
193  * 100 - busy
194  */
195 static unsigned int sgx_current_load(void)
196 {
197         enum PVRSRV_ERROR eError;
198         struct SYS_DATA *psSysData;
199         struct SYS_SPECIFIC_DATA *psSysSpecData;
200         struct PVRSRV_DEVICE_NODE *psDeviceNode;
201         static unsigned int kicks_prev;
202         static long time_prev;
203
204         eError = SysAcquireData(&psSysData);
205         if (eError != PVRSRV_OK)
206                 return 0;
207         psSysSpecData =
208             (struct SYS_SPECIFIC_DATA *)psSysData->pvSysSpecificData;
209         if (!psSysSpecData ||
210             atomic_read(&psSysSpecData->sSGXClocksEnabled) == 0)
211                 return 0;
212         psDeviceNode = psSysData->psDeviceNodeList;
213         while (psDeviceNode) {
214                 if ((psDeviceNode->sDevId.eDeviceType ==
215                             PVRSRV_DEVICE_TYPE_SGX) &&
216                     psDeviceNode->pvDevice) {
217                         struct PVRSRV_SGXDEV_INFO *psDevInfo =
218                             (struct PVRSRV_SGXDEV_INFO *)psDeviceNode->pvDevice;
219                         unsigned int kicks = psDevInfo->ui32KickTACounter;
220                         unsigned int load;
221                         long time_elapsed;
222
223                         time_elapsed = jiffies - time_prev;
224                         if (likely(time_elapsed))
225                                 load =
226                                     1000 * (kicks - kicks_prev) / time_elapsed;
227                         else
228                                 load = 0;
229                         kicks_prev = kicks;
230                         time_prev += time_elapsed;
231                         /*
232                          * if the period between calls to this function was
233                          * too long, then load stats are invalid
234                          */
235                         if (time_elapsed > 5 * HZ)
236                                 return 0;
237                         /*pr_err("SGX load %u\n", load); */
238
239                         /*
240                          * 'load' shows how many times sgx was kicked
241                          * per 1000 jiffies
242                          * 150 is arbitrarily chosen threshold.
243                          * If the number of kicks is below threshold
244                          * then sgx is doing
245                          * some small jobs and we can keep the clock freq low.
246                          */
247                         if (load < 150)
248                                 return 0;
249                         else
250                                 return 100;
251                 }
252                 psDeviceNode = psDeviceNode->psNext;
253         }
254         return 0;
255 }
256
257 static void sgx_lock_perf(struct work_struct *work)
258 {
259         int vdd1, vdd2;
260         static int bHigh;
261         int high;
262         unsigned int load;
263         struct delayed_work *d_work =
264             container_of(work, struct delayed_work, work);
265         struct ENV_DATA *psEnvData =
266             container_of(d_work, struct ENV_DATA, sPerfWork);
267
268         load = sgx_current_load();
269         if (load) {
270                 vdd1 = 500000000;
271                 vdd2 = 400000;
272                 high = 1;
273         } else {
274                 vdd1 = 0;
275                 vdd2 = 0;
276                 high = 0;
277         }
278         if (high != bHigh) {
279                 omap_pm_set_min_bus_tput(&sgx_dev, OCP_INITIATOR_AGENT, vdd2);
280                 bHigh = high;
281         }
282
283         if (sgx_clock_enabled || load)
284                 queue_delayed_work(psEnvData->psPerfWorkqueue,
285                                    &psEnvData->sPerfWork, HZ / 5);
286 }
287
288 static void sgx_need_perf(struct SYS_DATA *psSysData, int ena)
289 {
290         struct ENV_DATA *psEnvData =
291             (struct ENV_DATA *)psSysData->pvEnvSpecificData;
292
293         sgx_clock_enabled = ena;
294         cancel_delayed_work(&psEnvData->sPerfWork);
295         queue_delayed_work(psEnvData->psPerfWorkqueue, &psEnvData->sPerfWork,
296                            0);
297 }
298
299 enum PVRSRV_ERROR OSInitPerf(void *pvSysData)
300 {
301         struct SYS_DATA *psSysData = (struct SYS_DATA *)pvSysData;
302         struct ENV_DATA *psEnvData =
303             (struct ENV_DATA *)psSysData->pvEnvSpecificData;
304
305         if (psEnvData->psPerfWorkqueue) {
306                 PVR_DPF(PVR_DBG_ERROR, "OSInitPerf: already inited");
307                 return PVRSRV_ERROR_GENERIC;
308         }
309
310         PVR_TRACE("Initing DVFS %x", pvSysData);
311
312         psEnvData->psPerfWorkqueue = create_singlethread_workqueue("sgx_perf");
313         INIT_DELAYED_WORK(&psEnvData->sPerfWork, sgx_lock_perf);
314
315         return PVRSRV_OK;
316 }
317
318 enum PVRSRV_ERROR OSCleanupPerf(void *pvSysData)
319 {
320         struct SYS_DATA *psSysData = (struct SYS_DATA *)pvSysData;
321         struct ENV_DATA *psEnvData =
322             (struct ENV_DATA *)psSysData->pvEnvSpecificData;
323
324         if (!psEnvData->psPerfWorkqueue) {
325                 PVR_DPF(PVR_DBG_ERROR, "OSCleanupPerf: not inited");
326                 return PVRSRV_ERROR_GENERIC;
327         }
328
329         PVR_TRACE("Cleaning up DVFS");
330
331         sgx_clock_enabled = 0;
332         flush_workqueue(psEnvData->psPerfWorkqueue);
333         destroy_workqueue(psEnvData->psPerfWorkqueue);
334
335         return PVRSRV_OK;
336 }
337
338 static inline void setup_int_bypass(void)
339 {
340         if (cpu_is_omap3630())
341                 sgx_ocp_write_reg(EUR_CR_OCP_DEBUG_CONFIG,
342                               EUR_CR_OCP_DEBUG_CONFIG_THALIA_INT_BYPASS_MASK);
343 }
344
345 #ifndef NO_HARDWARE
346
347 static enum PVRSRV_ERROR sgx_force_enable_clocks(struct SYS_DATA *psSysData)
348 {
349         struct SYS_SPECIFIC_DATA *psSysSpecData =
350             (struct SYS_SPECIFIC_DATA *)psSysData->pvSysSpecificData;
351         int res;
352
353         res = clk_enable(psSysSpecData->psSGX_FCK);
354         if (res < 0) {
355                 PVR_DPF(PVR_DBG_ERROR, "%s: "
356                                 "Couldn't enable SGX functional clock (%d)",
357                          __func__, res);
358                 return PVRSRV_ERROR_GENERIC;
359         }
360
361         res = clk_enable(psSysSpecData->psSGX_ICK);
362         if (res < 0) {
363                 PVR_DPF(PVR_DBG_ERROR, "%s: "
364                                 "Couldn't enable SGX interface clock (%d)",
365                          __func__, res);
366
367                 clk_disable(psSysSpecData->psSGX_FCK);
368                 return PVRSRV_ERROR_GENERIC;
369         }
370
371         setup_int_bypass();
372
373         return PVRSRV_OK;
374 }
375
376 static void sgx_force_disable_clocks(struct SYS_DATA *psSysData)
377 {
378         struct SYS_SPECIFIC_DATA *psSysSpecData =
379             (struct SYS_SPECIFIC_DATA *)psSysData->pvSysSpecificData;
380
381         if (psSysSpecData->psSGX_ICK)
382                 clk_disable(psSysSpecData->psSGX_ICK);
383
384         if (psSysSpecData->psSGX_FCK)
385                 clk_disable(psSysSpecData->psSGX_FCK);
386 }
387
388 #else           /* NO_HARDWARE */
389
390 static enum PVRSRV_ERROR sgx_force_enable_clocks(struct SYS_DATA *psSYsData)
391 {
392         return PVRSRV_OK;
393 }
394
395 static void sgx_force_disable_clocks(struct SYS_DATA *psSYsData)
396 {
397 }
398
399 #endif          /* NO_HARDWARE */
400
401 static bool force_clocks_on(void)
402 {
403 #ifdef CONFIG_PVR_FORCE_CLOCKS_ON
404         return true;
405 #else
406         return false;
407 #endif
408 }
409
410 enum PVRSRV_ERROR EnableSGXClocks(struct SYS_DATA *psSysData)
411 {
412         struct SYS_SPECIFIC_DATA *psSysSpecData =
413             (struct SYS_SPECIFIC_DATA *)psSysData->pvSysSpecificData;
414         enum PVRSRV_ERROR res = PVRSRV_OK;
415
416         if (atomic_xchg(&psSysSpecData->sSGXClocksEnabled, 1))
417                 return PVRSRV_OK;
418
419         /*
420          * In case of force clocks on we have already enabled the clocks
421          * at init time.
422          */
423         if (!force_clocks_on())
424                 res = sgx_force_enable_clocks(psSysData);
425
426         if (res == PVRSRV_OK) {
427                 BUG_ON(!atomic_read(&psSysSpecData->sSGXClocksEnabled));
428                 sgx_need_perf(psSysData, 1);
429         } else {
430                 atomic_set(&psSysSpecData->sSGXClocksEnabled, 0);
431         }
432
433         return res;
434 }
435
436 void DisableSGXClocks(struct SYS_DATA *psSysData)
437 {
438         struct SYS_SPECIFIC_DATA *psSysSpecData =
439             (struct SYS_SPECIFIC_DATA *)psSysData->pvSysSpecificData;
440
441         if (!atomic_xchg(&psSysSpecData->sSGXClocksEnabled, 0))
442                 return;
443
444         if (!force_clocks_on())
445                 sgx_force_disable_clocks(psSysData);
446
447         BUG_ON(atomic_read(&psSysSpecData->sSGXClocksEnabled));
448
449         sgx_need_perf(psSysData, 0);
450 }
451
452 static enum PVRSRV_ERROR InitSgxClocks(struct SYS_DATA *psSysData)
453 {
454         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
455         struct clk *psCLK;
456         struct clk *core_ck = NULL;
457
458         psCLK = clk_get(NULL, "sgx_fck");
459         if (IS_ERR(psCLK))
460                 goto err0;
461         psSysSpecData->psSGX_FCK = psCLK;
462
463         psCLK = clk_get(NULL, "sgx_ick");
464         if (IS_ERR(psCLK))
465                 goto err1;
466         psSysSpecData->psSGX_ICK = psCLK;
467
468         core_ck = clk_get(NULL, "core_ck");
469         if (IS_ERR(core_ck))
470                 goto err2;
471         if (clk_set_parent(psSysSpecData->psSGX_FCK, core_ck) < 0) {
472                 clk_put(core_ck);
473                 goto err2;
474         }
475         clk_put(core_ck);
476
477         RegisterConstraintNotifications(psSysSpecData);
478         return PVRSRV_OK;
479
480 err2:
481         clk_put(psSysSpecData->psSGX_ICK);
482 err1:
483         clk_put(psSysSpecData->psSGX_FCK);
484 err0:
485         PVR_DPF(PVR_DBG_ERROR,
486                  "%s: couldn't init clocks fck %p ick %p core %p", __func__,
487                  psSysSpecData->psSGX_FCK, psSysSpecData->psSGX_ICK, core_ck);
488         psSysSpecData->psSGX_FCK = NULL;
489         psSysSpecData->psSGX_ICK = NULL;
490
491         return PVRSRV_ERROR_GENERIC;
492 }
493
494 static void CleanupSgxClocks(struct SYS_DATA *psSysData)
495 {
496         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
497         UnRegisterConstraintNotifications(psSysSpecData);
498
499         if (psSysSpecData->psSGX_ICK) {
500                 clk_put(psSysSpecData->psSGX_ICK);
501                 psSysSpecData->psSGX_ICK = NULL;
502         }
503
504         if (psSysSpecData->psSGX_FCK) {
505                 clk_put(psSysSpecData->psSGX_FCK);
506                 psSysSpecData->psSGX_FCK = NULL;
507         }
508 }
509
510 #if defined(DEBUG) || defined(TIMING)
511 static inline u32 gpt_read_reg(struct SYS_DATA *psSysData, u32 reg)
512 {
513         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
514
515         return __raw_readl(psSysSpecData->gpt_base + reg);
516 }
517
518 static inline void gpt_write_reg(struct SYS_DATA *psSysData, u32 reg, u32 val)
519 {
520         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
521
522         __raw_writel(val, psSysSpecData->gpt_base + reg);
523 }
524
525 static enum PVRSRV_ERROR InitDebugClocks(struct SYS_DATA *psSysData)
526 {
527         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
528         struct clk *psCLK;
529         struct clk *sys_ck = NULL;
530         u32 rate;
531
532         psCLK = clk_get(NULL, "mpu_ck");
533         if (IS_ERR(psCLK))
534                 goto err0;
535         psSysSpecData->psMPU_CK = psCLK;
536
537         psCLK = clk_get(NULL, "gpt11_fck");
538         if (IS_ERR(psCLK))
539                 goto err1;
540         psSysSpecData->psGPT11_FCK = psCLK;
541
542         psCLK = clk_get(NULL, "gpt11_ick");
543         if (IS_ERR(psCLK))
544                 goto err2;
545         psSysSpecData->psGPT11_ICK = psCLK;
546
547         sys_ck = clk_get(NULL, "sys_ck");
548         if (IS_ERR(sys_ck))
549                 goto err3;
550         if (clk_get_parent(psSysSpecData->psGPT11_FCK) != sys_ck)
551                 if (clk_set_parent(psSysSpecData->psGPT11_FCK, sys_ck) < 0) {
552                         clk_put(sys_ck);
553                         goto err3;
554                 }
555         clk_put(sys_ck);
556
557         PVR_TRACE("GPTIMER11 clock is %dMHz",
558                    HZ_TO_MHZ(clk_get_rate(psSysSpecData->psGPT11_FCK)));
559
560         psSysSpecData->gpt_base = ioremap(SYS_OMAP3430_GP11TIMER_PHYS_BASE,
561                                           SYS_OMAP3430_GPTIMER_SIZE);
562         if (!psSysSpecData->gpt_base)
563                 goto err3;
564
565         clk_enable(psSysSpecData->psGPT11_ICK);
566         clk_enable(psSysSpecData->psGPT11_FCK);
567
568         rate = gpt_read_reg(psSysData, SYS_OMAP3430_GPTIMER_TSICR);
569         if (!(rate & 4)) {
570                 PVR_TRACE("Setting GPTIMER11 mode to posted "
571                           "(currently is non-posted)");
572                 gpt_write_reg(psSysData, SYS_OMAP3430_GPTIMER_TSICR, rate | 4);
573         }
574
575         clk_disable(psSysSpecData->psGPT11_FCK);
576         clk_disable(psSysSpecData->psGPT11_ICK);
577
578         return PVRSRV_OK;
579
580 err3:
581         clk_put(psSysSpecData->psGPT11_ICK);
582 err2:
583         clk_put(psSysSpecData->psGPT11_FCK);
584 err1:
585         clk_put(psSysSpecData->psMPU_CK);
586 err0:
587         PVR_DPF(PVR_DBG_ERROR,
588                  "%s: couldn't init clocks: mpu %p sys %p fck %p ick %p",
589                  __func__, psSysSpecData->psMPU_CK, sys_ck,
590                  psSysSpecData->psGPT11_FCK, psSysSpecData->psGPT11_ICK);
591
592         psSysSpecData->psMPU_CK = NULL;
593         psSysSpecData->psGPT11_FCK = NULL;
594         psSysSpecData->psGPT11_ICK = NULL;
595
596         return PVRSRV_ERROR_GENERIC;
597 }
598
599 static void CleanupDebugClocks(struct SYS_DATA *psSysData)
600 {
601         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
602
603         if (psSysSpecData->psMPU_CK) {
604                 clk_put(psSysSpecData->psMPU_CK);
605                 psSysSpecData->psMPU_CK = NULL;
606         }
607         if (psSysSpecData->psGPT11_FCK) {
608                 clk_put(psSysSpecData->psGPT11_FCK);
609                 psSysSpecData->psGPT11_FCK = NULL;
610         }
611         if (psSysSpecData->psGPT11_ICK) {
612                 clk_put(psSysSpecData->psGPT11_ICK);
613                 psSysSpecData->psGPT11_ICK = NULL;
614         }
615 }
616
617 static enum PVRSRV_ERROR EnableDebugClocks(struct SYS_DATA *psSysData)
618 {
619         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
620
621         if (clk_enable(psSysSpecData->psGPT11_FCK) < 0)
622                 goto err0;
623
624         if (clk_enable(psSysSpecData->psGPT11_ICK) < 0)
625                 goto err1;
626
627         gpt_write_reg(psSysData, SYS_OMAP3430_GPTIMER_ENABLE, 3);
628
629         return PVRSRV_OK;
630
631 err1:
632         clk_disable(psSysSpecData->psGPT11_FCK);
633 err0:
634         PVR_DPF(PVR_DBG_ERROR, "%s: can't enable clocks", __func__);
635
636         return PVRSRV_ERROR_GENERIC;
637 }
638
639 static inline void DisableDebugClocks(struct SYS_DATA *psSysData)
640 {
641         struct SYS_SPECIFIC_DATA *psSysSpecData = psSysData->pvSysSpecificData;
642
643         gpt_write_reg(psSysData, SYS_OMAP3430_GPTIMER_ENABLE, 0);
644
645         clk_disable(psSysSpecData->psGPT11_ICK);
646         clk_disable(psSysSpecData->psGPT11_FCK);
647 }
648
649 #else
650
651 inline enum PVRSRV_ERROR InitDebugClocks(struct SYS_DATA *psSysData)
652 {
653         return PVRSRV_OK;
654 }
655
656 static inline void CleanupDebugClocks(struct SYS_DATA *psSysData)
657 {
658 }
659
660 static inline enum PVRSRV_ERROR EnableDebugClocks(struct SYS_DATA *psSysData)
661 {
662         return PVRSRV_OK;
663 }
664
665 static inline void DisableDebugClocks(struct SYS_DATA *psSysData)
666 {
667 }
668 #endif
669
670 enum PVRSRV_ERROR InitSystemClocks(struct SYS_DATA *psSysData)
671 {
672         if (InitSgxClocks(psSysData) != PVRSRV_OK)
673                 goto err0;
674
675         if (InitDebugClocks(psSysData) != PVRSRV_OK)
676                 goto err1;
677
678         return PVRSRV_OK;
679
680 err1:
681         CleanupSgxClocks(psSysData);
682 err0:
683         return PVRSRV_ERROR_GENERIC;
684 }
685
686 void CleanupSystemClocks(struct SYS_DATA *psSysData)
687 {
688         CleanupDebugClocks(psSysData);
689         CleanupSgxClocks(psSysData);
690 }
691
692 enum PVRSRV_ERROR EnableSystemClocks(struct SYS_DATA *psSysData)
693 {
694         PVR_TRACE("EnableSystemClocks: Enabling System Clocks");
695
696         /*
697          * We force clocks on by increasing their refcount here during
698          * module init time and decreasing it at cleanup time.
699          */
700         if (force_clocks_on())
701                 sgx_force_enable_clocks(gpsSysData);
702         if (EnableDebugClocks(psSysData) != PVRSRV_OK)
703                 goto err1;
704
705         return PVRSRV_OK;
706
707 err1:
708         return PVRSRV_ERROR_GENERIC;
709 }
710
711 void DisableSystemClocks(struct SYS_DATA *psSysData)
712 {
713         PVR_TRACE("DisableSystemClocks: Disabling System Clocks");
714
715         DisableDebugClocks(psSysData);
716         /* Decrease the clocks' refcount that was increased at init time. */
717         if (force_clocks_on())
718                 sgx_force_disable_clocks(gpsSysData);
719 }