summaryrefslogtreecommitdiff
path: root/arch/arm/mach-u300/clock.c
blob: 7a94729caff9a87e2576a1b5822b6bb452a70ad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
/*
 *
 * arch/arm/mach-u300/clock.c
 *
 *
 * Copyright (C) 2007-2009 ST-Ericsson AB
 * License terms: GNU General Public License (GPL) version 2
 * Define clocks in the app platform.
 * Author: Linus Walleij <linus.walleij@stericsson.com>
 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
 *
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/io.h>
#include <linux/seq_file.h>

#include <asm/clkdev.h>
#include <mach/hardware.h>
#include <mach/syscon.h>

#include "clock.h"

/*
 * TODO:
 * - move all handling of the CCR register into this file and create
 *   a spinlock for the CCR register
 * - switch to the clkdevice lookup mechanism that maps clocks to
 *   device ID:s instead when it becomes available in kernel 2.6.29.
 * - implement rate get/set for all clocks that need it.
 */

/*
 * Syscon clock I/O registers lock so clock requests don't collide
 * NOTE: this is a local lock only used to lock access to clock and
 * reset registers in syscon.
 */
static DEFINE_SPINLOCK(syscon_clkreg_lock);
static DEFINE_SPINLOCK(syscon_resetreg_lock);

/*
 * The clocking hierarchy currently looks like this.
 * NOTE: the idea is NOT to show how the clocks are routed on the chip!
 * The ideas is to show dependencies, so a clock higher up in the
 * hierarchy has to be on in order for another clock to be on. Now,
 * both CPU and DMA can actually be on top of the hierarchy, and that
 * is not modeled currently. Instead we have the backbone AMBA bus on
 * top. This bus cannot be programmed in any way but conceptually it
 * needs to be active for the bridges and devices to transport data.
 *
 * Please be aware that a few clocks are hw controlled, which mean that
 * the hw itself can turn on/off or change the rate of the clock when
 * needed!
 *
 *  AMBA bus
 *  |
 *  +- CPU
 *  +- NANDIF NAND Flash interface
 *  +- SEMI Shared Memory interface
 *  +- ISP Image Signal Processor (U335 only)
 *  +- CDS (U335 only)
 *  +- DMA Direct Memory Access Controller
 *  +- AAIF APP/ACC Inteface (Mobile Scalable Link, MSL)
 *  +- APEX
 *  +- VIDEO_ENC AVE2/3 Video Encoder
 *  +- XGAM Graphics Accelerator Controller
 *  +- AHB
 *  |
 *  +- ahb:0 AHB Bridge
 *  |  |
 *  |  +- ahb:1 INTCON Interrupt controller
 *  |  +- ahb:3 MSPRO  Memory Stick Pro controller
 *  |  +- ahb:4 EMIF   External Memory interface
 *  |
 *  +- fast:0 FAST bridge
 *  |  |
 *  |  +- fast:1 MMCSD MMC/SD card reader controller
 *  |  +- fast:2 I2S0  PCM I2S channel 0 controller
 *  |  +- fast:3 I2S1  PCM I2S channel 1 controller
 *  |  +- fast:4 I2C0  I2C channel 0 controller
 *  |  +- fast:5 I2C1  I2C channel 1 controller
 *  |  +- fast:6 SPI   SPI controller
 *  |  +- fast:7 UART1 Secondary UART (U335 only)
 *  |
 *  +- slow:0 SLOW bridge
 *     |
 *     +- slow:1 SYSCON (not possible to control)
 *     +- slow:2 WDOG Watchdog
 *     +- slow:3 UART0 primary UART
 *     +- slow:4 TIMER_APP Application timer - used in Linux
 *     +- slow:5 KEYPAD controller
 *     +- slow:6 GPIO controller
 *     +- slow:7 RTC controller
 *     +- slow:8 BT Bus Tracer (not used currently)
 *     +- slow:9 EH Event Handler (not used currently)
 *     +- slow:a TIMER_ACC Access style timer (not used currently)
 *     +- slow:b PPM (U335 only, what is that?)
 */

/*
 * Reset control functions. We remember if a block has been
 * taken out of reset and don't remove the reset assertion again
 * and vice versa. Currently we only remove resets so the
 * enablement function is defined out.
 */
static void syscon_block_reset_enable(struct clk *clk)
{
	u16 val;
	unsigned long iflags;

	/* Not all blocks support resetting */
	if (!clk->res_reg || !clk->res_mask)
		return;
	spin_lock_irqsave(&syscon_resetreg_lock, iflags);
	val = readw(clk->res_reg);
	val |= clk->res_mask;
	writew(val, clk->res_reg);
	spin_unlock_irqrestore(&syscon_resetreg_lock, iflags);
	clk->reset = true;
}

static void syscon_block_reset_disable(struct clk *clk)
{
	u16 val;
	unsigned long iflags;

	/* Not all blocks support resetting */
	if (!clk->res_reg || !clk->res_mask)
		return;
	spin_lock_irqsave(&syscon_resetreg_lock, iflags);
	val = readw(clk->res_reg);
	val &= ~clk->res_mask;
	writew(val, clk->res_reg);
	spin_unlock_irqrestore(&syscon_resetreg_lock, iflags);
	clk->reset = false;
}

int __clk_get(struct clk *clk)
{
	u16 val;

	/* The MMC and MSPRO clocks need some special set-up */
	if (!strcmp(clk->name, "MCLK")) {
		/* Set default MMC clock divisor to 18.9 MHz */
		writew(0x0054U, U300_SYSCON_VBASE + U300_SYSCON_MMF0R);
		val = readw(U300_SYSCON_VBASE + U300_SYSCON_MMCR);
		/* Disable the MMC feedback clock */
		val &= ~U300_SYSCON_MMCR_MMC_FB_CLK_SEL_ENABLE;
		/* Disable MSPRO frequency */
		val &= ~U300_SYSCON_MMCR_MSPRO_FREQSEL_ENABLE;
		writew(val, U300_SYSCON_VBASE + U300_SYSCON_MMCR);
	}
	if (!strcmp(clk->name, "MSPRO")) {
		val = readw(U300_SYSCON_VBASE + U300_SYSCON_MMCR);
		/* Disable the MMC feedback clock */
		val &= ~U300_SYSCON_MMCR_MMC_FB_CLK_SEL_ENABLE;
		/* Enable MSPRO frequency */
		val |= U300_SYSCON_MMCR_MSPRO_FREQSEL_ENABLE;
		writew(val, U300_SYSCON_VBASE + U300_SYSCON_MMCR);
	}
	return 1;
}
EXPORT_SYMBOL(__clk_get);

void __clk_put(struct clk *clk)
{
}
EXPORT_SYMBOL(__clk_put);

static void syscon_clk_disable(struct clk *clk)
{
	unsigned long iflags;

	/* Don't touch the hardware controlled clocks */
	if (clk->hw_ctrld)
		return;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	writew(clk->clk_val, U300_SYSCON_VBASE + U300_SYSCON_SBCDR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

static void syscon_clk_enable(struct clk *clk)
{
	unsigned long iflags;

	/* Don't touch the hardware controlled clocks */
	if (clk->hw_ctrld)
		return;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	writew(clk->clk_val, U300_SYSCON_VBASE + U300_SYSCON_SBCER);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

static u16 syscon_clk_get_rate(void)
{
	u16 val;
	unsigned long iflags;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val &= U300_SYSCON_CCR_CLKING_PERFORMANCE_MASK;
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
	return val;
}

#ifdef CONFIG_MACH_U300_USE_I2S_AS_MASTER
static void enable_i2s0_vcxo(void)
{
	u16 val;
	unsigned long iflags;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	/* Set I2S0 to use the VCXO 26 MHz clock */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val |= U300_SYSCON_CCR_TURN_VCXO_ON;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val |= U300_SYSCON_CCR_I2S0_USE_VCXO;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	val |= U300_SYSCON_CEFR_I2S0_CLK_EN;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

static void enable_i2s1_vcxo(void)
{
	u16 val;
	unsigned long iflags;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	/* Set I2S1 to use the VCXO 26 MHz clock */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val |= U300_SYSCON_CCR_TURN_VCXO_ON;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val |= U300_SYSCON_CCR_I2S1_USE_VCXO;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	val |= U300_SYSCON_CEFR_I2S1_CLK_EN;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

static void disable_i2s0_vcxo(void)
{
	u16 val;
	unsigned long iflags;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	/* Disable I2S0 use of the VCXO 26 MHz clock */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val &= ~U300_SYSCON_CCR_I2S0_USE_VCXO;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	/* Deactivate VCXO if noone else is using VCXO */
	if (!(val & U300_SYSCON_CCR_I2S1_USE_VCXO))
		val &= ~U300_SYSCON_CCR_TURN_VCXO_ON;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	val &= ~U300_SYSCON_CEFR_I2S0_CLK_EN;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

static void disable_i2s1_vcxo(void)
{
	u16 val;
	unsigned long iflags;

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	/* Disable I2S1 use of the VCXO 26 MHz clock */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val &= ~U300_SYSCON_CCR_I2S1_USE_VCXO;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	/* Deactivate VCXO if noone else is using VCXO */
	if (!(val & U300_SYSCON_CCR_I2S0_USE_VCXO))
		val &= ~U300_SYSCON_CCR_TURN_VCXO_ON;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	val &= ~U300_SYSCON_CEFR_I2S0_CLK_EN;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CEFR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}
#endif /* CONFIG_MACH_U300_USE_I2S_AS_MASTER */


static void syscon_clk_rate_set_mclk(unsigned long rate)
{
	u16 val;
	u32 reg;
	unsigned long iflags;

	switch (rate) {
	case 18900000:
		val = 0x0054;
		break;
	case 20800000:
		val = 0x0044;
		break;
	case 23100000:
		val = 0x0043;
		break;
	case 26000000:
		val = 0x0033;
		break;
	case 29700000:
		val = 0x0032;
		break;
	case 34700000:
		val = 0x0022;
		break;
	case 41600000:
		val = 0x0021;
		break;
	case 52000000:
		val = 0x0011;
		break;
	case 104000000:
		val = 0x0000;
		break;
	default:
		printk(KERN_ERR "Trying to set MCLK to unknown speed! %ld\n",
		       rate);
		return;
	}

	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	reg = readw(U300_SYSCON_VBASE + U300_SYSCON_MMF0R) &
		~U300_SYSCON_MMF0R_MASK;
	writew(reg | val, U300_SYSCON_VBASE + U300_SYSCON_MMF0R);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}

void syscon_clk_rate_set_cpuclk(unsigned long rate)
{
	u16 val;
	unsigned long iflags;

	switch (rate) {
	case 13000000:
		val = U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER;
		break;
	case 52000000:
		val = U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE;
		break;
	case 104000000:
		val = U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH;
		break;
	case 208000000:
		val = U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST;
		break;
	default:
		return;
	}
	spin_lock_irqsave(&syscon_clkreg_lock, iflags);
	val |= readw(U300_SYSCON_VBASE + U300_SYSCON_CCR) &
		~U300_SYSCON_CCR_CLKING_PERFORMANCE_MASK ;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	spin_unlock_irqrestore(&syscon_clkreg_lock, iflags);
}
EXPORT_SYMBOL(syscon_clk_rate_set_cpuclk);

void clk_disable(struct clk *clk)
{
	unsigned long iflags;

	spin_lock_irqsave(&clk->lock, iflags);
	if (clk->usecount > 0 && !(--clk->usecount)) {
		/* some blocks lack clocking registers and cannot be disabled */
		if (clk->disable)
			clk->disable(clk);
		if (likely((u32)clk->parent))
			clk_disable(clk->parent);
	}
#ifdef CONFIG_MACH_U300_USE_I2S_AS_MASTER
	if (unlikely(!strcmp(clk->name, "I2S0")))
		disable_i2s0_vcxo();
	if (unlikely(!strcmp(clk->name, "I2S1")))
		disable_i2s1_vcxo();
#endif
	spin_unlock_irqrestore(&clk->lock, iflags);
}
EXPORT_SYMBOL(clk_disable);

int clk_enable(struct clk *clk)
{
	int ret = 0;
	unsigned long iflags;

	spin_lock_irqsave(&clk->lock, iflags);
	if (clk->usecount++ == 0) {
		if (likely((u32)clk->parent))
			ret = clk_enable(clk->parent);

		if (unlikely(ret != 0))
			clk->usecount--;
		else {
			/* remove reset line (we never enable reset again) */
			syscon_block_reset_disable(clk);
			/* clocks without enable function are always on */
			if (clk->enable)
				clk->enable(clk);
#ifdef CONFIG_MACH_U300_USE_I2S_AS_MASTER
			if (unlikely(!strcmp(clk->name, "I2S0")))
				enable_i2s0_vcxo();
			if (unlikely(!strcmp(clk->name, "I2S1")))
				enable_i2s1_vcxo();
#endif
		}
	}
	spin_unlock_irqrestore(&clk->lock, iflags);
	return ret;

}
EXPORT_SYMBOL(clk_enable);

/* Returns the clock rate in Hz */
static unsigned long clk_get_rate_cpuclk(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
		return 13000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
		return 52000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
		return 104000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
		return 208000000;
	default:
		break;
	}
	return clk->rate;
}

static unsigned long clk_get_rate_ahb_clk(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
		return 6500000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
		return 26000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
		return 52000000;
	default:
		break;
	}
	return clk->rate;

}

static unsigned long clk_get_rate_emif_clk(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
		return 13000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
		return 52000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
		return 104000000;
	default:
		break;
	}
	return clk->rate;

}

static unsigned long clk_get_rate_xgamclk(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
		return 6500000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
		return 26000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
		return 52000000;
	default:
		break;
	}

	return clk->rate;
}

static unsigned long clk_get_rate_mclk(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
		/*
		 * Here, the 208 MHz PLL gets shut down and the always
		 * on 13 MHz PLL used for RTC etc kicks into use
		 * instead.
		 */
		return 13000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
	{
		/*
		 * This clock is under program control. The register is
		 * divided in two nybbles, bit 7-4 gives cycles-1 to count
		 * high, bit 3-0 gives cycles-1 to count low. Distribute
		 * these with no more than 1 cycle difference between
		 * low and high and add low and high to get the actual
		 * divisor. The base PLL is 208 MHz. Writing 0x00 will
		 * divide by 1 and 1 so the highest frequency possible
		 * is 104 MHz.
		 *
		 * e.g. 0x54 =>
		 * f = 208 / ((5+1) + (4+1)) = 208 / 11 = 18.9 MHz
		 */
		u16 val = readw(U300_SYSCON_VBASE + U300_SYSCON_MMF0R) &
			U300_SYSCON_MMF0R_MASK;
		switch (val) {
		case 0x0054:
			return 18900000;
		case 0x0044:
			return 20800000;
		case 0x0043:
			return 23100000;
		case 0x0033:
			return 26000000;
		case 0x0032:
			return 29700000;
		case 0x0022:
			return 34700000;
		case 0x0021:
			return 41600000;
		case 0x0011:
			return 52000000;
		case 0x0000:
			return 104000000;
		default:
			break;
		}
	}
	default:
		break;
	}

	return clk->rate;
}

static unsigned long clk_get_rate_i2s_i2c_spi(struct clk *clk)
{
	u16 val;

	val = syscon_clk_get_rate();

	switch (val) {
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW_POWER:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_LOW:
		return 13000000;
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_INTERMEDIATE:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_HIGH:
	case U300_SYSCON_CCR_CLKING_PERFORMANCE_BEST:
		return 26000000;
	default:
		break;
	}

	return clk->rate;
}

unsigned long clk_get_rate(struct clk *clk)
{
	if (clk->get_rate)
		return clk->get_rate(clk);
	else
		return clk->rate;
}
EXPORT_SYMBOL(clk_get_rate);

static unsigned long clk_round_rate_mclk(struct clk *clk, unsigned long rate)
{
	if (rate <= 18900000)
		return 18900000;
	if (rate <= 20800000)
		return 20800000;
	if (rate <= 23100000)
		return 23100000;
	if (rate <= 26000000)
		return 26000000;
	if (rate <= 29700000)
		return 29700000;
	if (rate <= 34700000)
		return 34700000;
	if (rate <= 41600000)
		return 41600000;
	if (rate <= 52000000)
		return 52000000;
	return -EINVAL;
}

static unsigned long clk_round_rate_cpuclk(struct clk *clk, unsigned long rate)
{
	if (rate <= 13000000)
		return 13000000;
	if (rate <= 52000000)
		return 52000000;
	if (rate <= 104000000)
		return 104000000;
	if (rate <= 208000000)
		return 208000000;
	return -EINVAL;
}

/*
 * This adjusts a requested rate to the closest exact rate
 * a certain clock can provide. For a fixed clock it's
 * mostly clk->rate.
 */
long clk_round_rate(struct clk *clk, unsigned long rate)
{
	/* TODO: get apropriate switches for EMIFCLK, AHBCLK and MCLK */
	/* Else default to fixed value */

	if (clk->round_rate) {
		return (long) clk->round_rate(clk, rate);
	} else {
		printk(KERN_ERR "clock: Failed to round rate of %s\n",
		       clk->name);
	}
	return (long) clk->rate;
}
EXPORT_SYMBOL(clk_round_rate);

static int clk_set_rate_mclk(struct clk *clk, unsigned long rate)
{
	syscon_clk_rate_set_mclk(clk_round_rate(clk, rate));
	return 0;
}

static int clk_set_rate_cpuclk(struct clk *clk, unsigned long rate)
{
	syscon_clk_rate_set_cpuclk(clk_round_rate(clk, rate));
	return 0;
}

int clk_set_rate(struct clk *clk, unsigned long rate)
{
	/* TODO: set for EMIFCLK and AHBCLK */
	/* Else assume the clock is fixed and fail */
	if (clk->set_rate) {
		return clk->set_rate(clk, rate);
	} else {
		printk(KERN_ERR "clock: Failed to set %s to %ld hz\n",
		       clk->name, rate);
		return -EINVAL;
	}
}
EXPORT_SYMBOL(clk_set_rate);

/*
 * Clock definitions. The clock parents are set to respective
 * bridge and the clock framework makes sure that the clocks have
 * parents activated and are brought out of reset when in use.
 *
 * Clocks that have hw_ctrld = true are hw controlled, and the hw
 * can by itself turn these clocks on and off.
 * So in other words, we don't really have to care about them.
 */

static struct clk amba_clk = {
	.name	    = "AMBA",
	.rate	    = 52000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = false,
	.lock       = __SPIN_LOCK_UNLOCKED(amba_clk.lock),
};

/*
 * These blocks are connected directly to the AMBA bus
 * with no bridge.
 */

static struct clk cpu_clk = {
	.name	    = "CPU",
	.parent	    = &amba_clk,
	.rate	    = 208000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_CPU_RESET_EN,
	.set_rate   = clk_set_rate_cpuclk,
	.get_rate   = clk_get_rate_cpuclk,
	.round_rate = clk_round_rate_cpuclk,
	.lock       = __SPIN_LOCK_UNLOCKED(cpu_clk.lock),
};

static struct clk nandif_clk = {
	.name       = "NANDIF",
	.parent	    = &amba_clk,
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_NANDIF_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_NANDIF_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(nandif_clk.lock),
};

static struct clk semi_clk = {
	.name       = "SEMI",
	.parent	    = &amba_clk,
	.rate       = 0, /* FIXME */
	/* It is not possible to reset SEMI */
	.hw_ctrld   = false,
	.reset	    = false,
	.clk_val    = U300_SYSCON_SBCER_SEMI_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(semi_clk.lock),
};

#ifdef CONFIG_MACH_U300_BS335
static struct clk isp_clk = {
	.name	    = "ISP",
	.parent	    = &amba_clk,
	.rate	    = 0, /* FIXME */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_ISP_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_ISP_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(isp_clk.lock),
};

static struct clk cds_clk = {
	.name	    = "CDS",
	.parent	    = &amba_clk,
	.rate	    = 0, /* FIXME */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_CDS_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_CDS_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(cds_clk.lock),
};
#endif

static struct clk dma_clk = {
	.name       = "DMA",
	.parent	    = &amba_clk,
	.rate       = 52000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_DMAC_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_DMAC_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(dma_clk.lock),
};

static struct clk aaif_clk = {
	.name       = "AAIF",
	.parent	    = &amba_clk,
	.rate       = 52000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_AAIF_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_AAIF_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(aaif_clk.lock),
};

static struct clk apex_clk = {
	.name       = "APEX",
	.parent	    = &amba_clk,
	.rate       = 0, /* FIXME */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_APEX_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_APEX_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(apex_clk.lock),
};

static struct clk video_enc_clk = {
	.name       = "VIDEO_ENC",
	.parent	    = &amba_clk,
	.rate       = 208000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = false,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	/* This has XGAM in the name but refers to the video encoder */
	.res_mask   = U300_SYSCON_RRR_XGAM_VC_SYNC_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_VIDEO_ENC_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(video_enc_clk.lock),
};

static struct clk xgam_clk = {
	.name       = "XGAMCLK",
	.parent	    = &amba_clk,
	.rate       = 52000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_XGAM_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_XGAM_CLK_EN,
	.get_rate   = clk_get_rate_xgamclk,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(xgam_clk.lock),
};

/* This clock is used to activate the video encoder */
static struct clk ahb_clk = {
	.name	    = "AHB",
	.parent	    = &amba_clk,
	.rate	    = 52000000, /* this varies! */
	.hw_ctrld   = false, /* This one is set to false due to HW bug */
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_AHB_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_AHB_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_ahb_clk,
	.lock       = __SPIN_LOCK_UNLOCKED(ahb_clk.lock),
};


/*
 * Clocks on the AHB bridge
 */

static struct clk ahb_subsys_clk = {
	.name	    = "AHB_SUBSYS",
	.parent	    = &amba_clk,
	.rate	    = 52000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = false,
	.clk_val    = U300_SYSCON_SBCER_AHB_SUBSYS_BRIDGE_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_ahb_clk,
	.lock       = __SPIN_LOCK_UNLOCKED(ahb_subsys_clk.lock),
};

static struct clk intcon_clk = {
	.name	    = "INTCON",
	.parent	    = &ahb_subsys_clk,
	.rate	    = 52000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_INTCON_RESET_EN,
	/* INTCON can be reset but not clock-gated */
	.lock       = __SPIN_LOCK_UNLOCKED(intcon_clk.lock),

};

static struct clk mspro_clk = {
	.name       = "MSPRO",
	.parent	    = &ahb_subsys_clk,
	.rate       = 0, /* FIXME */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_MSPRO_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_MSPRO_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(mspro_clk.lock),
};

static struct clk emif_clk = {
	.name	    = "EMIF",
	.parent	    = &ahb_subsys_clk,
	.rate	    = 104000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RRR,
	.res_mask   = U300_SYSCON_RRR_EMIF_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_EMIF_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_emif_clk,
	.lock       = __SPIN_LOCK_UNLOCKED(emif_clk.lock),
};


/*
 * Clocks on the FAST bridge
 */
static struct clk fast_clk = {
	.name	    = "FAST_BRIDGE",
	.parent	    = &amba_clk,
	.rate	    = 13000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_FAST_BRIDGE_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_FAST_BRIDGE_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(fast_clk.lock),
};

/*
 * The MMCI apb_pclk is hardwired to the same terminal as the
 * external MCI clock. Thus this will be referenced twice.
 */
static struct clk mmcsd_clk = {
	.name       = "MCLK",
	.parent	    = &fast_clk,
	.rate       = 18900000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_MMC_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_MMC_CLK_EN,
	.get_rate   = clk_get_rate_mclk,
	.set_rate   = clk_set_rate_mclk,
	.round_rate = clk_round_rate_mclk,
	.disable    = syscon_clk_disable,
	.enable     = syscon_clk_enable,
	.lock       = __SPIN_LOCK_UNLOCKED(mmcsd_clk.lock),
};

static struct clk i2s0_clk = {
	.name       = "i2s0",
	.parent	    = &fast_clk,
	.rate       = 26000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_PCM_I2S0_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_I2S0_CORE_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_i2s_i2c_spi,
	.lock       = __SPIN_LOCK_UNLOCKED(i2s0_clk.lock),
};

static struct clk i2s1_clk = {
	.name       = "i2s1",
	.parent	    = &fast_clk,
	.rate       = 26000000, /* this varies! */
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_PCM_I2S1_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_I2S1_CORE_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_i2s_i2c_spi,
	.lock       = __SPIN_LOCK_UNLOCKED(i2s1_clk.lock),
};

static struct clk i2c0_clk = {
	.name       = "I2C0",
	.parent	    = &fast_clk,
	.rate       = 26000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_I2C0_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_I2C0_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_i2s_i2c_spi,
	.lock       = __SPIN_LOCK_UNLOCKED(i2c0_clk.lock),
};

static struct clk i2c1_clk = {
	.name       = "I2C1",
	.parent	    = &fast_clk,
	.rate       = 26000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_I2C1_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_I2C1_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_i2s_i2c_spi,
	.lock       = __SPIN_LOCK_UNLOCKED(i2c1_clk.lock),
};

/*
 * The SPI apb_pclk is hardwired to the same terminal as the
 * external SPI clock. Thus this will be referenced twice.
 */
static struct clk spi_clk = {
	.name       = "SPI",
	.parent	    = &fast_clk,
	.rate       = 26000000, /* this varies! */
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_SPI_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_SPI_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.get_rate   = clk_get_rate_i2s_i2c_spi,
	.lock       = __SPIN_LOCK_UNLOCKED(spi_clk.lock),
};

#ifdef CONFIG_MACH_U300_BS335
static struct clk uart1_pclk = {
	.name	    = "UART1_PCLK",
	.parent	    = &fast_clk,
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RFR,
	.res_mask   = U300_SYSCON_RFR_UART1_RESET_ENABLE,
	.clk_val    = U300_SYSCON_SBCER_UART1_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(uart1_pclk.lock),
};

/* This one is hardwired to PLL13 */
static struct clk uart1_clk = {
	.name	    = "UART1_CLK",
	.rate	    = 13000000,
	.hw_ctrld   = true,
	.lock       = __SPIN_LOCK_UNLOCKED(uart1_clk.lock),
};
#endif


/*
 * Clocks on the SLOW bridge
 */
static struct clk slow_clk = {
	.name	    = "SLOW_BRIDGE",
	.parent	    = &amba_clk,
	.rate	    = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_SLOW_BRIDGE_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_SLOW_BRIDGE_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(slow_clk.lock),
};

/* TODO: implement SYSCON clock? */

static struct clk wdog_clk = {
	.name	    = "WDOG",
	.parent	    = &slow_clk,
	.hw_ctrld   = false,
	.rate	    = 32768,
	.reset	    = false,
	/* This is always on, cannot be enabled/disabled or reset */
	.lock       = __SPIN_LOCK_UNLOCKED(wdog_clk.lock),
};

static struct clk uart0_pclk = {
	.name	    = "UART0_PCLK",
	.parent	    = &slow_clk,
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_UART_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_UART_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(uart0_pclk.lock),
};

/* This one is hardwired to PLL13 */
static struct clk uart0_clk = {
	.name	    = "UART0_CLK",
	.parent	    = &slow_clk,
	.rate	    = 13000000,
	.hw_ctrld   = true,
	.lock       = __SPIN_LOCK_UNLOCKED(uart0_clk.lock),
};

static struct clk keypad_clk = {
	.name       = "KEYPAD",
	.parent	    = &slow_clk,
	.rate       = 32768,
	.hw_ctrld   = false,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_KEYPAD_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_KEYPAD_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(keypad_clk.lock),
};

static struct clk gpio_clk = {
	.name       = "GPIO",
	.parent	    = &slow_clk,
	.rate       = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_GPIO_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_GPIO_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(gpio_clk.lock),
};

static struct clk rtc_clk = {
	.name	    = "RTC",
	.parent	    = &slow_clk,
	.rate	    = 32768,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_RTC_RESET_EN,
	/* This clock is always on, cannot be enabled/disabled */
	.lock       = __SPIN_LOCK_UNLOCKED(rtc_clk.lock),
};

static struct clk bustr_clk = {
	.name       = "BUSTR",
	.parent	    = &slow_clk,
	.rate       = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_BTR_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_BTR_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(bustr_clk.lock),
};

static struct clk evhist_clk = {
	.name       = "EVHIST",
	.parent	    = &slow_clk,
	.rate       = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_EH_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_EH_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(evhist_clk.lock),
};

static struct clk timer_clk = {
	.name       = "TIMER",
	.parent	    = &slow_clk,
	.rate       = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_ACC_TMR_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_ACC_TMR_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(timer_clk.lock),
};

static struct clk app_timer_clk = {
	.name       = "TIMER_APP",
	.parent	    = &slow_clk,
	.rate       = 13000000,
	.hw_ctrld   = true,
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_APP_TMR_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_APP_TMR_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(app_timer_clk.lock),
};

#ifdef CONFIG_MACH_U300_BS335
static struct clk ppm_clk = {
	.name	    = "PPM",
	.parent	    = &slow_clk,
	.rate	    = 0, /* FIXME */
	.hw_ctrld   = true, /* TODO: Look up if it is hw ctrld or not */
	.reset	    = true,
	.res_reg    = U300_SYSCON_VBASE + U300_SYSCON_RSR,
	.res_mask   = U300_SYSCON_RSR_PPM_RESET_EN,
	.clk_val    = U300_SYSCON_SBCER_PPM_CLK_EN,
	.enable     = syscon_clk_enable,
	.disable    = syscon_clk_disable,
	.lock       = __SPIN_LOCK_UNLOCKED(ppm_clk.lock),
};
#endif

#define DEF_LOOKUP(devid, clkref)		\
	{					\
	.dev_id = devid,			\
	.clk = clkref,				\
	}

#define DEF_LOOKUP_CON(devid, conid, clkref)	\
	{					\
	.dev_id = devid,			\
	.con_id = conid,			\
	.clk = clkref,				\
	}

/*
 * Here we only define clocks that are meaningful to
 * look up through clockdevice.
 */
static struct clk_lookup lookups[] = {
	/* Connected directly to the AMBA bus */
	DEF_LOOKUP("amba",      &amba_clk),
	DEF_LOOKUP("cpu",       &cpu_clk),
	DEF_LOOKUP("fsmc",      &nandif_clk),
	DEF_LOOKUP("semi",      &semi_clk),
#ifdef CONFIG_MACH_U300_BS335
	DEF_LOOKUP("isp",       &isp_clk),
	DEF_LOOKUP("cds",       &cds_clk),
#endif
	DEF_LOOKUP("dma",       &dma_clk),
	DEF_LOOKUP("msl",       &aaif_clk),
	DEF_LOOKUP("apex",      &apex_clk),
	DEF_LOOKUP("video_enc", &video_enc_clk),
	DEF_LOOKUP("xgam",      &xgam_clk),
	DEF_LOOKUP("ahb",       &ahb_clk),
	/* AHB bridge clocks */
	DEF_LOOKUP("ahb_subsys", &ahb_subsys_clk),
	DEF_LOOKUP("intcon",    &intcon_clk),
	DEF_LOOKUP_CON("intcon", "apb_pclk", &intcon_clk),
	DEF_LOOKUP("mspro",     &mspro_clk),
	DEF_LOOKUP("pl172",     &emif_clk),
	DEF_LOOKUP_CON("pl172", "apb_pclk", &emif_clk),
	/* FAST bridge clocks */
	DEF_LOOKUP("fast",      &fast_clk),
	DEF_LOOKUP("mmci",      &mmcsd_clk),
	DEF_LOOKUP_CON("mmci", "apb_pclk", &mmcsd_clk),
	/*
	 * The .0 and .1 identifiers on these comes from the platform device
	 * .id field and are assigned when the platform devices are registered.
	 */
	DEF_LOOKUP("i2s.0",     &i2s0_clk),
	DEF_LOOKUP("i2s.1",     &i2s1_clk),
	DEF_LOOKUP("stu300.0",  &i2c0_clk),
	DEF_LOOKUP("stu300.1",  &i2c1_clk),
	DEF_LOOKUP("pl022",     &spi_clk),
	DEF_LOOKUP_CON("pl022", "apb_pclk", &spi_clk),
#ifdef CONFIG_MACH_U300_BS335
	DEF_LOOKUP("uart1",     &uart1_clk),
	DEF_LOOKUP_CON("uart1", "apb_pclk", &uart1_pclk),
#endif
	/* SLOW bridge clocks */
	DEF_LOOKUP("slow",      &slow_clk),
	DEF_LOOKUP("coh901327_wdog",      &wdog_clk),
	DEF_LOOKUP("uart0",     &uart0_clk),
	DEF_LOOKUP_CON("uart0", "apb_pclk", &uart0_pclk),
	DEF_LOOKUP("apptimer",  &app_timer_clk),
	DEF_LOOKUP("coh901461-keypad",    &keypad_clk),
	DEF_LOOKUP("u300-gpio", &gpio_clk),
	DEF_LOOKUP("rtc-coh901331",      &rtc_clk),
	DEF_LOOKUP("bustr",     &bustr_clk),
	DEF_LOOKUP("evhist",    &evhist_clk),
	DEF_LOOKUP("timer",     &timer_clk),
#ifdef CONFIG_MACH_U300_BS335
	DEF_LOOKUP("ppm",       &ppm_clk),
#endif
};

static void __init clk_register(void)
{
	/* Register the lookups */
	clkdev_add_table(lookups, ARRAY_SIZE(lookups));
}

#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_U300_DEBUG))
/*
 * The following makes it possible to view the status (especially
 * reference count and reset status) for the clocks in the platform
 * by looking into the special file <debugfs>/u300_clocks
 */

/* A list of all clocks in the platform */
static struct clk *clks[] = {
	/* Top node clock for the AMBA bus */
	&amba_clk,
	/* Connected directly to the AMBA bus */
	&cpu_clk,
	&nandif_clk,
	&semi_clk,
#ifdef CONFIG_MACH_U300_BS335
	&isp_clk,
	&cds_clk,
#endif
	&dma_clk,
	&aaif_clk,
	&apex_clk,
	&video_enc_clk,
	&xgam_clk,
	&ahb_clk,

	/* AHB bridge clocks */
	&ahb_subsys_clk,
	&intcon_clk,
	&mspro_clk,
	&emif_clk,
	/* FAST bridge clocks */
	&fast_clk,
	&mmcsd_clk,
	&i2s0_clk,
	&i2s1_clk,
	&i2c0_clk,
	&i2c1_clk,
	&spi_clk,
#ifdef CONFIG_MACH_U300_BS335
	&uart1_clk,
	&uart1_pclk,
#endif
	/* SLOW bridge clocks */
	&slow_clk,
	&wdog_clk,
	&uart0_clk,
	&uart0_pclk,
	&app_timer_clk,
	&keypad_clk,
	&gpio_clk,
	&rtc_clk,
	&bustr_clk,
	&evhist_clk,
	&timer_clk,
#ifdef CONFIG_MACH_U300_BS335
	&ppm_clk,
#endif
};

static int u300_clocks_show(struct seq_file *s, void *data)
{
	struct clk *clk;
	int i;

	seq_printf(s, "CLOCK           DEVICE          RESET STATE\t" \
		   "ACTIVE\tUSERS\tHW CTRL FREQ\n");
	seq_printf(s, "---------------------------------------------" \
		   "-----------------------------------------\n");
	for (i = 0; i < ARRAY_SIZE(clks); i++) {
		clk = clks[i];
		if (clk != ERR_PTR(-ENOENT)) {
			/* Format clock and device name nicely */
			char cdp[33];
			int chars;

			chars = snprintf(&cdp[0], 17, "%s", clk->name);
			while (chars < 16) {
				cdp[chars] = ' ';
				chars++;
			}
			chars = snprintf(&cdp[16], 17, "%s", clk->dev ?
					 dev_name(clk->dev) : "N/A");
			while (chars < 16) {
				cdp[chars+16] = ' ';
				chars++;
			}
			cdp[32] = '\0';
			if (clk->get_rate || clk->rate != 0)
				seq_printf(s,
					   "%s%s\t%s\t%d\t%s\t%lu Hz\n",
					   &cdp[0],
					   clk->reset ?
					   "ASSERTED" : "RELEASED",
					   clk->usecount ? "ON" : "OFF",
					   clk->usecount,
					   clk->hw_ctrld  ? "YES" : "NO ",
					   clk_get_rate(clk));
			else
				seq_printf(s,
					   "%s%s\t%s\t%d\t%s\t" \
					   "(unknown rate)\n",
					   &cdp[0],
					   clk->reset ?
					   "ASSERTED" : "RELEASED",
					   clk->usecount ? "ON" : "OFF",
					   clk->usecount,
					   clk->hw_ctrld  ? "YES" : "NO ");
		}
	}
	return 0;
}

static int u300_clocks_open(struct inode *inode, struct file *file)
{
	return single_open(file, u300_clocks_show, NULL);
}

static const struct file_operations u300_clocks_operations = {
	.open		= u300_clocks_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int __init init_clk_read_debugfs(void)
{
	/* Expose a simple debugfs interface to view all clocks */
	(void) debugfs_create_file("u300_clocks", S_IFREG | S_IRUGO,
				   NULL, NULL,
				   &u300_clocks_operations);
	return 0;
}
/*
 * This needs to come in after the core_initcall() for the
 * overall clocks, because debugfs is not available until
 * the subsystems come up.
 */
module_init(init_clk_read_debugfs);
#endif

int __init u300_clock_init(void)
{
	u16 val;

	/*
	 * FIXME: shall all this powermanagement stuff really live here???
	 */

	/* Set system to run at PLL208, max performance, a known state. */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_CCR);
	val &= ~U300_SYSCON_CCR_CLKING_PERFORMANCE_MASK;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_CCR);
	/* Wait for the PLL208 to lock if not locked in yet */
	while (!(readw(U300_SYSCON_VBASE + U300_SYSCON_CSR) &
		 U300_SYSCON_CSR_PLL208_LOCK_IND));

	/* Power management enable */
	val = readw(U300_SYSCON_VBASE + U300_SYSCON_PMCR);
	val |= U300_SYSCON_PMCR_PWR_MGNT_ENABLE;
	writew(val, U300_SYSCON_VBASE + U300_SYSCON_PMCR);

	clk_register();

	/*
	 * Some of these may be on when we boot the system so make sure they
	 * are turned OFF.
	 */
	syscon_block_reset_enable(&timer_clk);
	timer_clk.disable(&timer_clk);

	/*
	 * These shall be turned on by default when we boot the system
	 * so make sure they are ON. (Adding CPU here is a bit too much.)
	 * These clocks will be claimed by drivers later.
	 */
	syscon_block_reset_disable(&semi_clk);
	syscon_block_reset_disable(&emif_clk);
	clk_enable(&semi_clk);
	clk_enable(&emif_clk);

	return 0;
}