summaryrefslogtreecommitdiff
path: root/board/isee/igep0046/igep0046.c
blob: d3877d8a6192755a16b07cf15c987a174ebe848c (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
/*
 * Copyright (C) 2016 ISEE 2007 SL - http://www.isee.biz
 *
 * Source file for IGEP0046 board
 *
 * Author: Jose Miguel Sanchez Sanabria <jsanabria@iseebcn.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <asm/arch/clock.h>
#include <asm/arch/imx-regs.h>
#include <asm/arch/iomux.h>
#include <asm/arch/mx6-pins.h>
#include <asm/arch/mx6-ddr.h>
#include <linux/errno.h>
#include <asm/gpio.h>
#include <asm/imx-common/mxc_i2c.h>
#include <asm/imx-common/iomux-v3.h>
#include <asm/imx-common/boot_mode.h>
#include <mmc.h>
#include <fsl_esdhc.h>
#include <malloc.h>
#include <miiphy.h>
#include <netdev.h>
#include <asm/arch/crm_regs.h>
#include <asm/arch/sys_proto.h>
#include <asm/arch-mx6/sys_proto.h>
#include <i2c.h>
#include <asm/io.h>
#include <power/pmic.h>
#include <power/pfuze100_pmic.h>
#include "pfuze.h"
#include "../common/igep_common.h"
#include "../common/igep_eeprom.h"
#include "../common/igep_test.h"
#include <usb.h>
#include <mmc.h>
#include <net.h>

DECLARE_GLOBAL_DATA_PTR;

/* MACRO MUX defines */
#define UART_PAD_CTRL  (PAD_CTL_PUS_100K_UP |	\
	PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm |	\
	PAD_CTL_SRE_FAST  | PAD_CTL_HYS)

#define GPIO_VERSION_PAD_CTRL  (PAD_CTL_PUS_100K_UP |	\
	PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm |	\
	PAD_CTL_SRE_FAST)

#define GPIO_LED_PAD_CTRL  (PAD_CTL_PUS_22K_UP |	\
	PAD_CTL_SPEED_LOW | PAD_CTL_DSE_40ohm |	\
	PAD_CTL_SRE_FAST )

#define USDHC_PAD_CTRL (PAD_CTL_PUS_47K_UP |	\
	PAD_CTL_SPEED_LOW | PAD_CTL_DSE_48ohm |	\
	PAD_CTL_SRE_FAST  | PAD_CTL_HYS |	\
	PAD_CTL_PUE | PAD_CTL_PKE)

#define USDHC_CLK_PAD_CTRL (PAD_CTL_PUS_100K_DOWN |	\
	PAD_CTL_SPEED_LOW | PAD_CTL_DSE_48ohm |	\
	PAD_CTL_SRE_FAST  | PAD_CTL_HYS)

#define ENET_PAD_CTRL  (PAD_CTL_PUS_100K_UP |	\
	PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | PAD_CTL_HYS)

#ifdef CONFIG_SYS_I2C
#define I2C_PAD_CTRL  ( PAD_CTL_ODE | PAD_CTL_SPEED_MED |	\
	PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST | PAD_CTL_HYS)
#define I2C_PMIC	1
#define I2C_PAD MUX_PAD_CTRL(I2C_PAD_CTRL)
#endif

#ifdef CONFIG_USB_EHCI_MX6
#define OTG_ID_PAD_CTRL (PAD_CTL_PKE | PAD_CTL_PUE |		\
	PAD_CTL_PUS_47K_UP  | PAD_CTL_SPEED_LOW |		\
	PAD_CTL_DSE_80ohm   | PAD_CTL_SRE_FAST  | PAD_CTL_HYS)
#endif

#define PCB_CFG_0	IMX_GPIO_NR(3, 22)
#define PCB_CFG_1	IMX_GPIO_NR(3, 23)
#define PCB_CFG_2	IMX_GPIO_NR(3, 29)
#define PCB_CFG_3	IMX_GPIO_NR(6, 31)
#define PCB_REV_A	1
#define PCB_REV_B	2
#define PCB_REV_C	3
#define PCB_REV_D10	4

/* Dual Lite case */
#define GPIO_LED_RED1		IMX_GPIO_NR(4, 18)
#define GPIO_LED_GREEN1		IMX_GPIO_NR(4, 19)
#define GPIO_LED_RED2		IMX_GPIO_NR(4, 20)
#define GPIO_LED_GREEN2		IMX_GPIO_NR(4, 17)

/* Define Revision */
#define TEST_REVISION 4

int dram_init(void)
{
	gd->ram_size = imx_ddr_size();
	return 0;
}

void dram_init_banksize(void)
{
	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
	gd->bd->bi_dram[0].size = imx_ddr_size();
}

/* UART MUX */
static iomux_v3_cfg_t const uart1_pads[] =
{
	MX6_PAD_CSI0_DAT10__UART1_TX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
	MX6_PAD_CSI0_DAT11__UART1_RX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
};

static iomux_v3_cfg_t const uart2_pads[] =
{
	MX6_PAD_EIM_D26__UART2_TX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
	MX6_PAD_EIM_D27__UART2_RX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
};

/* SDIO MUX */
iomux_v3_cfg_t const usdhc1_pads[] =
{
	MX6_PAD_SD1_CLK__SD1_CLK		| MUX_PAD_CTRL(USDHC_CLK_PAD_CTRL),
	MX6_PAD_SD1_CMD__SD1_CMD		| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD1_DAT0__SD1_DATA0	| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD1_DAT1__SD1_DATA1	| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD1_DAT2__SD1_DATA2	| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD1_DAT3__SD1_DATA3	| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_GPIO_2__GPIO1_IO02  | MUX_PAD_CTRL(NO_PAD_CTRL),
};
/* eMMC MUX */
iomux_v3_cfg_t const usdhc3_pads[] =
{
	MX6_PAD_SD3_CLK__SD3_CLK   	| MUX_PAD_CTRL(USDHC_CLK_PAD_CTRL),
	MX6_PAD_SD3_CMD__SD3_CMD   	| MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT0__SD3_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT1__SD3_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT2__SD3_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT3__SD3_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT4__SD3_DATA4 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT5__SD3_DATA5 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT6__SD3_DATA6 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
	MX6_PAD_SD3_DAT7__SD3_DATA7 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
};
/* Ethernet MUX */
iomux_v3_cfg_t const enet_pads[] =
{
	MX6_PAD_ENET_MDIO__ENET_MDIO				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_ENET_MDC__ENET_MDC				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TXC__RGMII_TXC				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TD0__RGMII_TD0				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TD1__RGMII_TD1				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TD2__RGMII_TD2				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TD3__RGMII_TD3				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_TX_CTL__RGMII_TX_CTL			| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_ENET_REF_CLK__ENET_TX_CLK			| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RXC__RGMII_RXC				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RD0__RGMII_RD0				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RD1__RGMII_RD1				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RD2__RGMII_RD2				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RD3__RGMII_RD3				| MUX_PAD_CTRL(ENET_PAD_CTRL),
	MX6_PAD_RGMII_RX_CTL__RGMII_RX_CTL			| MUX_PAD_CTRL(ENET_PAD_CTRL),
	/* Marvell Alaska PHY Reset */
	MX6_PAD_ENET_CRS_DV__GPIO1_IO25				| MUX_PAD_CTRL(NO_PAD_CTRL),
};
/* GPIO MISC MUX */
static iomux_v3_cfg_t const init_pads[] =
{
	/* PCB GPIO to Detect Module Version */
	MX6_PAD_EIM_D22__GPIO3_IO22  | MUX_PAD_CTRL(GPIO_VERSION_PAD_CTRL),
	MX6_PAD_EIM_D23__GPIO3_IO23  | MUX_PAD_CTRL(GPIO_VERSION_PAD_CTRL),
	MX6_PAD_EIM_D29__GPIO3_IO29  | MUX_PAD_CTRL(GPIO_VERSION_PAD_CTRL),
	MX6_PAD_EIM_BCLK__GPIO6_IO31 | MUX_PAD_CTRL(GPIO_VERSION_PAD_CTRL),
	/* PCB GPIO LEDs */
	MX6_PAD_DI0_PIN2__GPIO4_IO18  | MUX_PAD_CTRL(GPIO_LED_PAD_CTRL),
	MX6_PAD_DI0_PIN3__GPIO4_IO19  | MUX_PAD_CTRL(GPIO_LED_PAD_CTRL),
	MX6_PAD_DI0_PIN4__GPIO4_IO20  | MUX_PAD_CTRL(GPIO_LED_PAD_CTRL),
	MX6_PAD_DI0_PIN15__GPIO4_IO17 | MUX_PAD_CTRL(GPIO_LED_PAD_CTRL),
	#ifdef CONFIG_BASE0040
	/* TLV320AIC3106 Audio codec Reset*/
	MX6_PAD_KEY_COL2__GPIO4_IO10 | MUX_PAD_CTRL(NO_PAD_CTRL),
	/* USB2514 HUB Reset */
	MX6_PAD_CSI0_DAT4__GPIO5_IO22 | MUX_PAD_CTRL(NO_PAD_CTRL),
	/* USB Power Lines */
	MX6_PAD_CSI0_DAT5__GPIO5_IO23 | MUX_PAD_CTRL(NO_PAD_CTRL),
	MX6_PAD_CSI0_DAT6__GPIO5_IO24 | MUX_PAD_CTRL(NO_PAD_CTRL),
	MX6_PAD_CSI0_DAT7__GPIO5_IO25 | MUX_PAD_CTRL(NO_PAD_CTRL),
	#endif
};
#define STATION 0x02
const uchar igep_mac0 [6] = { 0x02, 0xBA, 0xD0, 0xBB, 0xD1, STATION };
uchar enetaddr[6];	
static int igep_eeprom_valid = 0;
static struct igep_mf_setup igep0046_eeprom_config;
static int net_fail = 0;

/* I2C MUX */
#ifdef CONFIG_SYS_I2C
static struct i2c_pads_info i2c_pad_info1 =
{
	.scl =
	{
		.i2c_mode = MX6_PAD_KEY_COL3__I2C2_SCL | I2C_PAD,
		.gpio_mode = MX6_PAD_KEY_COL3__GPIO4_IO12 | I2C_PAD,
		.gp = IMX_GPIO_NR(4, 12)
	},
	.sda =
	{
		.i2c_mode = MX6_PAD_KEY_ROW3__I2C2_SDA | I2C_PAD,
		.gpio_mode = MX6_PAD_KEY_ROW3__GPIO4_IO13 | I2C_PAD,
		.gp = IMX_GPIO_NR(4, 13)
	}
};

static struct i2c_pads_info i2c_pad_info2 =
{
	.scl =
	{
		.i2c_mode = MX6_PAD_EIM_D17__I2C3_SCL | I2C_PAD,
		.gpio_mode = MX6_PAD_EIM_D17__GPIO3_IO17 | I2C_PAD,
		.gp = IMX_GPIO_NR(3, 17)
	},
	.sda =
	{
		.i2c_mode = MX6_PAD_EIM_D18__I2C3_SDA | I2C_PAD,
		.gpio_mode = MX6_PAD_EIM_D18__GPIO3_IO18 | I2C_PAD,
		.gp = IMX_GPIO_NR(3, 18)
	}
};
#endif

static void setup_iomux_uart(void)
{
	imx_iomux_v3_setup_multiple_pads(uart2_pads, ARRAY_SIZE(uart2_pads));
}

#ifdef CONFIG_USB_EHCI_MX6
#define USB_OTHERREGS_OFFSET	0x800
#define UCTRL_PWR_POL		(1 << 9)

static iomux_v3_cfg_t const usb_otg_pads[] =
{
	MX6_PAD_ENET_RX_ER__USB_OTG_ID | MUX_PAD_CTRL(OTG_ID_PAD_CTRL),
};

#ifdef CONFIG_BASE0040
static void reset_audio(void)
{
	/* Audio Reset */
	gpio_direction_output(IMX_GPIO_NR(4, 10), 0);
	mdelay(5);
}

static void reset_usb_hub(void)
{
	/* Activate USB_PWRx */
	gpio_direction_output(IMX_GPIO_NR(5, 23), 1);
	gpio_direction_output(IMX_GPIO_NR(5, 24), 1);
	gpio_direction_output(IMX_GPIO_NR(5, 25), 1);
}
#endif

static void setup_usb(void)
{
	imx_iomux_v3_setup_multiple_pads(usb_otg_pads,
					 ARRAY_SIZE(usb_otg_pads));
}

int board_ehci_hcd_init(int port)
{
	u32 *usbnc_usb_ctrl;

	if (port > 1)
		return -EINVAL;

	usbnc_usb_ctrl = (u32 *)(USB_BASE_ADDR + USB_OTHERREGS_OFFSET +
				 port * 4);

	setbits_le32(usbnc_usb_ctrl, UCTRL_PWR_POL);

	return 0;
}
#endif

#ifdef CONFIG_FSL_ESDHC
struct fsl_esdhc_cfg usdhc_cfg[CONFIG_SYS_FSL_USDHC_NUM] =
{
	{USDHC1_BASE_ADDR},
	{USDHC3_BASE_ADDR},
};

#define USDHC1_CD_GPIO		IMX_GPIO_NR(1, 1)

int board_mmc_getcd(struct mmc *mmc)
{
	struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
	int ret = 0;

	switch (cfg->esdhc_base)
	{
	case USDHC1_BASE_ADDR:
		ret = !gpio_get_value(USDHC1_CD_GPIO);
		break;
	case USDHC3_BASE_ADDR:
		ret = 1; /* eMMC/uSDHC3 is always present */
		break;
	}

	return ret;
}

int board_mmc_init(bd_t *bis)
{
	int ret;
	int i;
	/*
	 * According to the board_mmc_init() the following map is done:
	 * (U-boot device node)    (Physical Port)
	 * mmc0                    SD1 (external SDIO)
	 * mmc1                    SD3 (internal eMMC)
	 */
	for (i = 0; i < CONFIG_SYS_FSL_USDHC_NUM; i++)
	{
		switch (i)
		{
		case 0:
			imx_iomux_v3_setup_multiple_pads(
				usdhc1_pads, ARRAY_SIZE(usdhc1_pads));
			gpio_direction_input(USDHC1_CD_GPIO);
			usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK);
			break;
		case 1:
			imx_iomux_v3_setup_multiple_pads(
				usdhc3_pads, ARRAY_SIZE(usdhc3_pads));
			usdhc_cfg[1].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
			break;
		default:
			printf("Warning: you configured more USDHC controllers"
			       "(%d) then supported by the board (%d)\n",
			       i + 1, CONFIG_SYS_FSL_USDHC_NUM);
			return -EINVAL;
		}

		ret = fsl_esdhc_initialize(bis, &usdhc_cfg[i]);
		if (ret)
			return ret;
	}

	return 0;
}
#endif

static void setup_iomux_enet(void)
{
	imx_iomux_v3_setup_multiple_pads(enet_pads, ARRAY_SIZE(enet_pads));
	gpio_request(IMX_GPIO_NR(1, 25), "ENET PHY Reset");
	gpio_direction_output(IMX_GPIO_NR(1, 25) , 0);
	mdelay(15);
	gpio_set_value(IMX_GPIO_NR(1, 25), 1);
	mdelay(10);
}

int board_phy_config(struct phy_device *phydev)
{
	unsigned short val;
	
	/* Marvel 88E1510 */
	if (phydev->phy_id == 0x1410dd1) {
		/*
		 * Page 3, Register 16: LED[2:0] Function Control Register
		 * LED[0] (SPD:Amber) R16_3.3:0 to 0111: on-GbE link
		 * LED[1] (LNK:Green) R16_3.7:4 to 0001: on-link, blink-activity
		 */
		phy_write(phydev, MDIO_DEVAD_NONE, 22, 3);
		val = phy_read(phydev, MDIO_DEVAD_NONE, 16);
		val &= 0xff00;
		val |= 0x0017;
		phy_write(phydev, MDIO_DEVAD_NONE, 16, val);
		phy_write(phydev, MDIO_DEVAD_NONE, 22, 0);
	}
	if (phydev->drv->config)
		phydev->drv->config(phydev);
	return 0;
}


static int get_mac_address (void)
{
	// MAC are 17 ASCII characters so we need to work it out a bit
	int i = 0;
	uchar ans[6];

	if(igep_eeprom_valid){
		for (i = 0; i < 6; i++){
		ans[i] = parse_char(igep0046_eeprom_config.bmac0[(i*3)]) * 0x10 + parse_char(igep0046_eeprom_config.bmac0[((i*3)+1)]);
		}
		memcpy(enetaddr, ans, 6);
		//memcpy(enetaddr, igep_mac0, 6);	
	}else{
		memcpy(enetaddr, igep_mac0, 6);
	}
	return eth_setenv_enetaddr("ethaddr", enetaddr);
}

int board_eth_init(bd_t *bis)
{
	get_mac_address();
	setup_iomux_enet();
#ifdef CONFIG_FEC_MXC
	if(cpu_eth_init(bis)){
		net_fail = 1;
	}
#endif
	return 0;
}

int checkboard(void)
{
	return 0;
}

int board_early_init_f(void)
{
	setup_iomux_uart();

	imx_iomux_v3_setup_multiple_pads(init_pads, ARRAY_SIZE(init_pads));
	return 0;
}

/* ------------- HERE IT WILL GO THE TEST FUNCTIONS THAT ITS EASIER TO BE HERE ------------- */

void error_loop(int casee){

	int su = 1;
	/* Turn off LEDs */
	gpio_direction_output(GPIO_LED_RED1, 0);
	gpio_direction_output(GPIO_LED_GREEN1, 0);
	gpio_direction_output(GPIO_LED_RED2, 0);
	gpio_direction_output(GPIO_LED_GREEN2, 0);

	switch (casee){
		case 0:
			/* I2C BUS ERROR */
			/* Turn on LED combination to notify I2C FAILED */
			/* We will infinite loop here 1 every 1 sec, combination is red-green switching every sec */
			while(1){
				if (su){
					gpio_direction_output(GPIO_LED_RED1, 1);
					gpio_direction_output(GPIO_LED_GREEN1, 0);
					gpio_direction_output(GPIO_LED_RED2, 0);
					gpio_direction_output(GPIO_LED_GREEN2, 1);
					su=0;
				}else{
					gpio_direction_output(GPIO_LED_RED1, 0);
					gpio_direction_output(GPIO_LED_GREEN1, 1);
					gpio_direction_output(GPIO_LED_RED2, 1);
					gpio_direction_output(GPIO_LED_GREEN2, 0);
					su=1;
				}
				mdelay(500);
			}
			break;
		case 1:
			/* EEPROM ERROR */
			while(1){
				if (su){
					gpio_direction_output(GPIO_LED_RED1, 1);
					gpio_direction_output(GPIO_LED_GREEN1, 1);
					gpio_direction_output(GPIO_LED_RED2, 0);
					gpio_direction_output(GPIO_LED_GREEN2, 0);
					su=0;
				}else{
					gpio_direction_output(GPIO_LED_RED1, 0);
					gpio_direction_output(GPIO_LED_GREEN1, 0);
					gpio_direction_output(GPIO_LED_RED2, 1);
					gpio_direction_output(GPIO_LED_GREEN2, 1);
					su=1;
				}
				mdelay(500);
			}
			break;
		case 2:
			/* SDRAM ANY CHIP ERROR */
			while(1){
				if (su){
					gpio_direction_output(GPIO_LED_RED1, 1);
					gpio_direction_output(GPIO_LED_GREEN1, 1);
					gpio_direction_output(GPIO_LED_RED2, 0);
					gpio_direction_output(GPIO_LED_GREEN2, 0);
					su=0;
				}else{
					gpio_direction_output(GPIO_LED_RED1, 0);
					gpio_direction_output(GPIO_LED_GREEN1, 0);
					gpio_direction_output(GPIO_LED_RED2, 0);
					gpio_direction_output(GPIO_LED_GREEN2, 0);
					su=1;
				}
				mdelay(500);
			}
			break;
	}
}

int test_i2c(void){

	puts("Testing I2C...\n");
	/* First we will test I2C */
	if (setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info1)){
		puts("I2C: Bus 2 Error\n");
		return 1;
	}
	if(setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info2)){
		puts("I2C: Bus 3 Error\n");
		return 1;
	}
	mdelay(2);
	return 0;
}

uint8_t test_SDRAM(void)
{
	//dcache_disable();
	int i = 0;
	uint8_t result = 0;
	uint64_t* file1 = 0;
	uint64_t* file2 = 0;
	uint64_t* file3 = 0;
	uint64_t* file4 = 0;
	int crc_value1 = 0;
	int crc_value2 = 0;
	int crc_value3 = 0;
	int crc_value4 = 0;
	int bytes = 1*1024*1024;

	file1 = (uint64_t*) 0x20000000;
	file2 = (uint64_t*) 0x40000000;
	file3 = (uint64_t*) 0x60000000;
	file4 = (uint64_t*) 0x80000000;

/*
	for(i=0; i<bytes;i++){
		file1[i] = i%16;
		file2[i] = i%16;
		file3[i] = i%16;
		file4[i] = i%16;
	}
*/
	for(i=0; i<bytes;i++){
		switch (i%6) {
			case 0:
				file1[i] = 0x0000AAAAULL;
				file2[i] = (0x33330000ULL << 16);
				file3[i] = (0x0000AAAAULL << 32);
				file4[i] = (0x33330000ULL << 48);
				break;
			case 1:
				file1[i] = 0x00005555ULL;
				file2[i] = (0x99990000ULL << 16);
				file3[i] = (0x00005555ULL << 32);
				file4[i] = (0x99990000ULL << 48);
				break;
			case 2:
				file1[i] = 0x0000CCCCULL;
				file2[i] = (0x66660000ULL << 16);
				file3[i] = (0x0000CCCCULL << 32);
				file4[i] = (0x66660000ULL << 48);
				break;
			case 3:
				file1[i] = 0x0000AAAAULL;
				file2[i] = (0x33330000ULL << 16);
				file3[i] = (0x0000AAAAULL << 32);
				file4[i] = (0x00003333ULL << 48);
				break;
			case 4:
				file1[i] = 0x00005555ULL;
				file2[i] = (0x99990000ULL << 16);
				file3[i] = (0x00005555ULL << 32);
				file4[i] = (0x00009999ULL << 48);
				break;
			case 5:
				file1[i] = 0x0000CCCCULL;
				file2[i] = (0x66660000ULL << 16);
				file3[i] = (0x0000CCCCULL << 32);
				file4[i] = (0x66660000ULL << 48);
				break;
			default:
				puts("Test RAM error \n");
		}		
	}

	/* crc file 1 - CHIP 1 */
	crc_value1 = crc32(0, (const unsigned char*) file1, bytes);
	/* crc file 2 - CHIP 2 */
	crc_value2 = crc32(0, (const unsigned char*) file2, bytes);
	/* crc file 3 - CHIP 3 */
	crc_value3 = crc32(0, (const unsigned char*) file3, bytes);
	/* crc file 4 - CHIP 4 */
	crc_value4 = crc32(0, (const unsigned char*) file4, bytes);

  	printf("CHIP 1 U500 crc32: 0x%x \n", crc_value1);
  	printf("CHIP 2 U501 crc32: 0x%x \n", crc_value2);
 	printf("CHIP 3 U502 crc32: 0x%x \n", crc_value3);
  	printf("CHIP 4 U503 crc32: 0x%x \n", crc_value4);

  	//dcache_enable();

  	if(crc_value1 != KNOWN_CRC_CUSTOM_1MB_1){
  		/* Chip 1 Fail */
  		result = result + 0x01;
  	}
  	if(crc_value2 != KNOWN_CRC_CUSTOM_1MB_2){
  		/* Chip 2 Fail */
  		result = result + 0x02;
  	}
  	if(crc_value3 != KNOWN_CRC_CUSTOM_1MB_3){
  		/* Chip 3 Fail */
  		result = result + 0x04;
  	}
  	if(crc_value4 != KNOWN_CRC_CUSTOM_1MB_4){
  		/* Chip 4 Fail */
  		result = result + 0x08;
  	}

  	/* RESULT */
  	return result;
}

int test_MMC(int who)
{
	struct mmc *mmc;

	if(who){
	/* eMMC case */
	mmc = find_mmc_device(1);
	}else{
	/* SD case */
	mmc = find_mmc_device(0);
	}

	if (!mmc){
		return 1;
	}

	if (mmc_init(mmc)){
		return 1;
	}

	if (IS_SD(mmc)){
		return 0;
	}else{
		return 0;
	}
	return 0;
}

int test_ETH(void)
{
	net_ping_ip = string_to_ip("192.168.2.171");
	net_ip = string_to_ip("192.168.2.101");
	net_netmask = string_to_ip("255.255.255.0");
	net_gateway = string_to_ip("192.168.2.1");

	/* Just to avoid warning */
	if (net_ping_ip.s_addr == 0){
		return CMD_RET_USAGE;
	}

	/* Check if network is already down */
	if(net_fail){
		return 1;
	}
	/*
	else{
		if (net_loop(5) < 0) {
			printf("ping failed; host 192.168.2.171 is not alive\n");
			return 1;
		}
	}
	*/
	return 0;
}

int board_init(void)
{
	/* Configure LEDS - UBOOT = 2 YELLOW */
	gpio_direction_output(GPIO_LED_RED1, 1);
	gpio_direction_output(GPIO_LED_GREEN1, 1);
	gpio_direction_output(GPIO_LED_RED2, 1);
	gpio_direction_output(GPIO_LED_GREEN2, 1);

#ifdef CONFIG_BASE0040
	reset_audio();
#endif

#ifdef CONFIG_SYS_I2C
	setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info1);
	setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info2);
	mdelay(1);
#endif

#ifdef CONFIG_BASE0040
	reset_usb_hub();
#endif

#ifdef CONFIG_USB_EHCI_MX6
	setup_usb();
#endif
	return 0;	
}

static inline unsigned int pcb_version(void)
{
	unsigned int pcb_version_bit0, pcb_version_bit1,
		pcb_version_bit2, pcb_version_bit3;

	pcb_version_bit0 = gpio_get_value(PCB_CFG_0);
	pcb_version_bit1 = gpio_get_value(PCB_CFG_1);
	pcb_version_bit2 = gpio_get_value(PCB_CFG_2);
	pcb_version_bit3 = gpio_get_value(PCB_CFG_3);

	if (pcb_version_bit0 && pcb_version_bit1 \
		&& pcb_version_bit2 && pcb_version_bit3)
		/* RA revision: 0b1111*/
		return PCB_REV_A;
	else if (!(pcb_version_bit0 || pcb_version_bit1 \
		|| pcb_version_bit2 || pcb_version_bit3))
		/* RB revision: 0b0000*/
		return PCB_REV_B;
	else if (!(!(pcb_version_bit0) || pcb_version_bit1 \
		|| pcb_version_bit2 || pcb_version_bit3))
		/* RC revision: 0b0001*/
		return PCB_REV_C;
	else if (!(pcb_version_bit0 || pcb_version_bit1 \
		|| pcb_version_bit2 || !(pcb_version_bit3)))
		/* RD10 revision: 0b1000*/
		return PCB_REV_D10;
	else
		return 0;
}

int board_late_init(void)
{
	uint64_t cpuid = 0;
	checkboard();
	switch (pcb_version()) {
		case PCB_REV_A:
			puts("Board: MX6-IGEP0046 Rev A\n");
	#ifdef CONFIG_MX6Q
			setenv("fdt_file", "imx6q-igep-base0040ra1.dtb");
	#elif CONFIG_MX6DL
			setenv("fdt_file", "imx6dl-igep-base0040ra1.dtb");
	#endif
			break;
		case PCB_REV_B:
			puts("Board: MX6-IGEP0046 Rev B\n");
	#ifdef CONFIG_MX6Q
			setenv("fdt_file", "imx6q-igep-base0040rb2.dtb");
	#elif CONFIG_MX6DL
			setenv("fdt_file", "imx6dl-igep-base0040rb2.dtb");
	#endif
			break;
		case PCB_REV_C:
			puts("Board: MX6-IGEP0046 Rev C/D\n");
	#ifdef CONFIG_MX6Q
			setenv("fdt_file", "imx6q-igep-base0040rc2.dtb");
	#elif CONFIG_MX6DL
			setenv("fdt_file", "imx6dl-igep-base0040rc2.dtb");
	#endif
			break;
		case PCB_REV_D10:
			puts("Board: MX6-IGEP0046 Rev D10\n");
	#ifdef CONFIG_MX6Q
			setenv("fdt_file", "imx6q-igep-base0040rd102.dtb");
	#elif CONFIG_MX6DL
			setenv("fdt_file", "imx6dl-igep-base0040rd102.dtb");
	#endif
			break;
		default:
			puts("Board: ERROR unknown PCB revision\n");
			setenv("fdt_file", "");
			break;
	}

	/* Get CPU ID */
	cpuid = ((uint64_t)readl(0x21bc420) << 32) | (readl(0x21bc410));
	printf("CPU ID: 0x%llx \n",cpuid);
	return 0;
}

#ifdef CONFIG_LAST_STAGE_INIT
int last_stage_init(void){
	
	/* This is the function that will be called last before running bootcmd u-boot */
	uint16_t test = 0;
	//uint8_t ramtest = 0;
	uint64_t cpuid = 0;
	
	/* We need to read SPL eeprom struct to acquire whose test have already being done */
	puts("---------------------------- |!| ISEE UBOOT TEST START |!| ----------------------------\n");
	/* Get SPL last test */
	test = load_test(SPL_MAGIC_ID_COUNTER);
	if (test == 0x4000){
		puts("SPL GET TEST FAILED.\n");

	}else if (test == 0xfff0){
		puts("I2C OK.\n");
		puts("EEPROM OK.\n");
		puts("POWER OK.\n");
		puts("CPU OK.\n");
		puts("SD OK.\n");
		puts("REV OK.\n");
		puts("IRAM OK.\n");
		puts("SDRAM OK.\n");
		puts("CPUID OK.\n");
	}else{
		puts("SOME SPL TEST FAILED, PERFORM MANUAL CHECK.\n");
	}

	/* Test RAM Now this will be done in SPL !!!
	ramtest = test_SDRAM();

	if((ramtest & 0x01) == 0x01){
		puts("SDRAM CHIP 1 FAIL.\n");
	}

	if((ramtest & 0x02) == 0x02){
		puts("SDRAM CHIP 2 FAIL.\n");
	}

	if((ramtest & 0x04) == 0x04){
		puts("SDRAM CHIP 3 FAIL.\n");
	}
	
	if((ramtest & 0x08) == 0x08){
		puts("SDRAM CHIP 4 FAIL.\n");
	}

	if(!ramtest){
		puts("RAM OK.\n");
		test = test + 0xF0;
	}else{
		puts("RAM FAIL.\n");
	}
	*/

	/* Test eMMC */
	if(test_MMC(1)){
		puts("eMMC FAIL.\n");
	}else{
		puts("eMMC OK.\n");
		test = test + 0x08;
	}

	/* Test Ethernet */
	if(test_ETH()){
		puts("Ethernet FAIL.\n");
	}else{
		puts("Ethernet OK.\n");
		test = test + 0x04;
	}

	/*
	test_SATA();
	test_USB();
	*/
	
	/* We will reserve 2 bits for future test development like SATA, USB */
	/* So for now add test the last 3 bits to 1 to forma a 0xffff if everything is ok */
	test = test + 0x03;

	/* Get CPU ID */
	cpuid = ((uint64_t)readl(0x21bc420) << 32) | (readl(0x21bc410));
	puts("CPUID OK.\n");

	printf("U-BOOT Test Result: 0x%x\n",test);

	/* Save Results to EEPROM */
	if (save_test(test,UB_MAGIC_ID,cpuid)){
		puts("ISEE U-BOOT TEST FAIL.\n");
	}else{
		puts("ISEE U-BOOT TEST OK.\n");
	}

	/* Let test process continue, bootcmd take us to kernel load via tftp and jump to it */
	return 0;
}
#endif

#ifdef CONFIG_LDO_BYPASS_CHECK
/* TODO, use external pmic, for now always ldo_enable */
void ldo_mode_set(int ldo_bypass)
{
	return;
}
#endif

#ifdef CONFIG_POWER
int power_init_board(void)
{
#ifdef CONFIG_SYS_I2C 
	struct pmic *p;
	unsigned int reg, ret;

	p = pfuze_common_init(I2C_PMIC);
	if (!p)
		return -ENODEV;

	ret = pfuze_mode_init(p, APS_PFM);
	if (ret < 0)
		return ret;

	/* Increase VGEN5 from 2.8 to 3V */
	pmic_reg_read(p, PFUZE100_VGEN5VOL, &reg);
	reg &= ~LDO_VOL_MASK;
	reg |= LDOB_3_00V;
	pmic_reg_write(p, PFUZE100_VGEN5VOL, reg);

	/* Decrease VGEN6 from 3.3 to 2.5V */
	pmic_reg_read(p, PFUZE100_VGEN6VOL, &reg);
	reg &= ~LDO_VOL_MASK;
	reg |= LDOB_2_50V;
	pmic_reg_write(p, PFUZE100_VGEN6VOL, reg);
#endif
	return 0;
}
#endif


#ifdef CONFIG_RESET_PHY_R
void mv_phy_88e1510_init(char *name)
{
	u16 val;
	u16 devadr;

	if (miiphy_set_current_dev(name))
		return;

	/* command to read PHY dev address */
	if (miiphy_read(name, 0xEE, 0xEE, (u16 *) &devadr)) {
		printf("Err..%s could not read PHY dev address\n",
			__FUNCTION__);
		return;
	}

	/*
	* Page 2 Register 24: RGMII Output Impedance Calibration Override
	* VDDO Level R24_2.13: 1; 2.5V used
	*/
	miiphy_write(name, 0, 22, 2);
	miiphy_read(name, 0, 24, &val);
	val |= 0x2000;
	miiphy_write(name, 0, 24, val);

	/*
	* Page 0 Register 9: 1000BASE-T Control Register
	* 1000BASE-T Full-Duplex R0_9.8: 0 Do not advertise
	* 1000BASE-T Half-Duplex R0_9.7: 0 Do not advertise
	*/
	miiphy_write(name, 0, 22, 0);
	miiphy_read(name, 0, 9, &val);
	val = 0x0000;
	miiphy_write(name, 0, 9, val);

	/*
	* Page 0 Register 0: Copper Control Register
	* Copper Reset R0_0.15: 1; PHY Software reset
	* Auto-Negotiation Enable R0_0.12: 1; Enable Auto-Negotiation Process
	*/
	miiphy_write(name, 0, 22, 0);
	miiphy_read(name, 0, 0, &val);
	val |= 0x9000;
	miiphy_write(name, 0, 0, val);
	miiphy_write(name, 0, 22, 0);

	printf("88E1510 Initialized on %s\n", name);
}

void reset_phy(void)
{
	/* configure and initialize */
	mv_phy_88e1510_init("FEC");
}
#endif /* CONFIG_RESET_PHY_R */

/* Add SPL Support if we want to build u-boot with SPL for i.MX6 */

#ifdef CONFIG_SPL_BUILD
#include <spl.h>
#include <libfdt.h>
#include <power/pmic.h>
#include <power/pfuze100_pmic.h>
#include "pfuze.h"

#define IMX6DQ_DRIVE_STRENGTH		0x30
#define IMX6SDL_DRIVE_STRENGTH		0x28

/* configure MX6Q/DUAL mmdc DDR io registers */

#ifdef CONFIG_MX6Q
static struct mx6dq_iomux_ddr_regs mx6dq_ddr_ioregs = {
	.dram_sdclk_0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdclk_1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_cas = IMX6DQ_DRIVE_STRENGTH,
	.dram_ras = IMX6DQ_DRIVE_STRENGTH,
	.dram_reset = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdcke0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdcke1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdba2 = 0x00000000,
	.dram_sdodt0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdodt1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs2 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs3 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs4 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs5 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs6 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs7 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm2 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm3 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm4 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm5 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm6 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm7 = IMX6DQ_DRIVE_STRENGTH,
};

/* configure MX6Q/DUAL mmdc GRP io registers */
static struct mx6dq_iomux_grp_regs mx6dq_grp_ioregs = {
	.grp_ddr_type = 0x000c0000,
	.grp_ddrmode_ctl = 0x00020000,
	.grp_ddrpke = 0x00000000,
	.grp_addds = IMX6DQ_DRIVE_STRENGTH,
	.grp_ctlds = IMX6DQ_DRIVE_STRENGTH,
	.grp_ddrmode = 0x00020000,
	.grp_b0ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b1ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b2ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b3ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b4ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b5ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b6ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b7ds = IMX6DQ_DRIVE_STRENGTH,
};

/* DDR 64bit 2GB */
static struct mx6_ddr_sysinfo mem_q = {
	.dsize		= 2,
	.cs1_mirror	= 0,
	/* config for full 4GB range so that get_mem_size() works */
	.cs_density	= 32,
	.ncs		= 1,
	.bi_on		= 1,
	.rtt_nom	= 1,
	.rtt_wr		= 0,
	.ralat		= 5,
	.walat		= 0,
	.mif3_mode	= 3,
	.rst_to_cke	= 0x23,
	.sde_to_rst	= 0x10,
};

static struct mx6_mmdc_calibration mx6q_2g_mmdc_calib = {
	.p0_mpwldectrl0 = 0x00530056,
	.p0_mpwldectrl1 = 0x00440053,
	.p1_mpwldectrl0 = 0x002B002B,
	.p1_mpwldectrl1 = 0x0028003F,
	.p0_mpdgctrl0 = 0x021C021C,
	.p0_mpdgctrl1 = 0x02140214,
	.p1_mpdgctrl0 = 0x0178017C,
	.p1_mpdgctrl1 = 0x016C0170,
	.p0_mprddlctl = 0x46484C48,
	.p1_mprddlctl = 0x42424842,
	.p0_mpwrdlctl = 0x3434302E,
	.p1_mpwrdlctl = 0x3A303830,
};
#endif

static struct mx6sdl_iomux_ddr_regs mx6sdl_ddr_ioregs = {
	.dram_sdclk_0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdclk_1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_cas = IMX6DQ_DRIVE_STRENGTH,
	.dram_ras = IMX6DQ_DRIVE_STRENGTH,
	.dram_reset = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdcke0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdcke1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdba2 = 0x00000000,
	.dram_sdodt0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdodt1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs2 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs3 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs4 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs5 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs6 = IMX6DQ_DRIVE_STRENGTH,
	.dram_sdqs7 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm0 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm1 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm2 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm3 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm4 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm5 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm6 = IMX6DQ_DRIVE_STRENGTH,
	.dram_dqm7 = IMX6DQ_DRIVE_STRENGTH,
};

/* configure MX6SOLO/DUALLITE mmdc GRP io registers */
struct mx6sdl_iomux_grp_regs mx6sdl_grp_ioregs = {
	.grp_ddr_type = 0x000c0000,
	.grp_ddrmode_ctl = 0x00020000,
	.grp_ddrpke = 0x00000000,
	.grp_addds = IMX6DQ_DRIVE_STRENGTH,
	.grp_ctlds = IMX6DQ_DRIVE_STRENGTH,
	.grp_ddrmode = 0x00020000,
	.grp_b0ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b1ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b2ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b3ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b4ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b5ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b6ds = IMX6DQ_DRIVE_STRENGTH,
	.grp_b7ds = IMX6DQ_DRIVE_STRENGTH,
};

static struct mx6_ddr_sysinfo mem_dl = {
	.dsize		= 2, /* MMDCx_MDCTL */
	.cs1_mirror	= 0, /* MMDCx_MDMISC */
	/* config for full 4GB range so that get_mem_size() works */
	.cs_density	= 32, /* MMDCx_MDCTL */
	.ncs		= 1, /* MMDCx_MDCTL */
	.bi_on		= 1, /* MMDCx_MDMISC */
	.rtt_nom	= 1, /* MMDCx_MPODTCTRL */
	.rtt_wr		= 0, /* - */
	.ralat		= 5, /* MMDCx_MDMISC */
	.walat		= 0, /* MMDCx_MDMISC */
	.mif3_mode	= 3, /* MMDCx_MDMISC */
	.rst_to_cke	= 0x23, /* MMDCx_MDOR */
	.sde_to_rst	= 0x10, /* MMDCx_MDOR */
};

static struct mx6_ddr_sysinfo mem_s = {
	.dsize		= 2,
	.cs1_mirror	= 0,
	/* config for full 4GB range so that get_mem_size() works */
	.cs_density	= 32,
	.ncs		= 1,
	.bi_on		= 1,
	.rtt_nom	= 1,
	.rtt_wr		= 0,
	.ralat		= 5,
	.walat		= 0,
	.mif3_mode	= 3,
	.rst_to_cke	= 0x23,
	.sde_to_rst	= 0x10,
};

/* K484G1646D-8MK0 (4Gb density) */
static struct mx6_ddr3_cfg k484g1646d_8mk0 = {
	.mem_speed = 1600,
	.density = 4,
	.width = 16,
	.banks = 8,
	.rowaddr = 15,
	.coladdr = 10,
	.pagesz = 2,
	.trcd = 1375,
	.trcmin = 4875,
	.trasmin = 3500,
};

static struct mx6_mmdc_calibration mx6dl_2g_mmdc_calib = {
	.p0_mpwldectrl0 = 0x00530056,
	.p0_mpwldectrl1 = 0x00440053,
	.p1_mpwldectrl0 = 0x002B002B,
	.p1_mpwldectrl1 = 0x0028003F,
	.p0_mpdgctrl0 = 0x021C021C,
	.p0_mpdgctrl1 = 0x02140214,
	.p1_mpdgctrl0 = 0x0178017C,
	.p1_mpdgctrl1 = 0x016C0170,
	.p0_mprddlctl = 0x46484C48,
	.p1_mprddlctl = 0x42424842,
	.p0_mpwrdlctl = 0x3434302E,
	.p1_mpwrdlctl = 0x3A303830,
};

static void spl_dram_init(void)
{

/* TO DO: IMPLEMENT CASE FOR EACH MEMORY DEVICE AND MEMORY DENSITY CONFIGURATION (1,2,4 GB) */

	if (is_cpu_type(MXC_CPU_MX6SOLO)) {
		mx6sdl_dram_iocfg(64, &mx6sdl_ddr_ioregs, &mx6sdl_grp_ioregs);
		mx6_dram_cfg(&mem_s, &mx6dl_2g_mmdc_calib, &k484g1646d_8mk0);
	} else if (is_cpu_type(MXC_CPU_MX6DL)) {
		mx6sdl_dram_iocfg(64, &mx6sdl_ddr_ioregs, &mx6sdl_grp_ioregs);
		mx6_dram_cfg(&mem_dl, &mx6dl_2g_mmdc_calib, &k484g1646d_8mk0);
	}
#ifdef CONFIG_MX6Q
	else if (is_cpu_type(MXC_CPU_MX6Q)) {
		mx6dq_dram_iocfg(64, &mx6dq_ddr_ioregs, &mx6dq_grp_ioregs);
		mx6_dram_cfg(&mem_q, &mx6q_2g_mmdc_calib, &k484g1646d_8mk0);
	}
#endif
	udelay(100);
}

static void ccgr_init(void)
{
	struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;

	writel(0x00C03F3F, &ccm->CCGR0);
	writel(0x0030FC03, &ccm->CCGR1);
	writel(0x0FFFC000, &ccm->CCGR2);
	writel(0x3FF00000, &ccm->CCGR3);
	writel(0x00FFF300, &ccm->CCGR4);
	writel(0x0F0000C3, &ccm->CCGR5);
	writel(0x000003FF, &ccm->CCGR6);
}

static void gpr_init(void)
{
	struct iomuxc *iomux = (struct iomuxc *)IOMUXC_BASE_ADDR;

	/* enable AXI cache for VDOA/VPU/IPU */
	writel(0xF00000CF, &iomux->gpr[4]);
	if (is_mx6dqp()) {
		/* set IPU AXI-id1 Qos=0x1 AXI-id0/2/3 Qos=0x7 */
		writel(0x007F007F, &iomux->gpr[6]);
		writel(0x007F007F, &iomux->gpr[7]);
	} else {
		/* set IPU AXI-id0 Qos=0xf(bypass) AXI-id1 Qos=0x7 */
		writel(0x007F007F, &iomux->gpr[6]);
		writel(0x007F007F, &iomux->gpr[7]);
	}
}
/*
 * called from C runtime startup code (arch/arm/lib/crt0.S:_main)
 * - we have a stack and a place to store GD, both in SRAM
 * - no variable global data is available
 */
void board_init_f(ulong dummy)
{

	/* setup AIPS and disable watchdog */
	arch_cpu_init();

	/* setup clock gating */
	ccgr_init();

	/* setup general purpose register */
	gpr_init();

	/* iomux and setup of UART and leds */
	board_early_init_f();

	/* configure LEDS - SPL = 1 YELLOW */
	gpio_direction_output(GPIO_LED_RED1, 1);
	gpio_direction_output(GPIO_LED_GREEN1, 1);
	gpio_direction_output(GPIO_LED_RED2, 0);
	gpio_direction_output(GPIO_LED_GREEN2, 0);

	/* setup GP timer */
	timer_init();

	/* Silent Console */
	gd->flags |= GD_FLG_SILENT;
	/* UART clocks enabled and gd valid - init serial console */
	preloader_console_init();

	/* DDR initialization */
	spl_dram_init();

	/* Get RAM size */
	dram_init();

	/* Initialize SDRAM banks - fdt_fixup*/
	dram_init_banksize();

	/* Clear the BSS. */
	memset(__bss_start, 0, __bss_end - __bss_start);
	
	/* load/boot image from boot device */
	board_init_r(NULL, 0);
}

/* called from board_init_r after gd setup if CONFIG_SPL_BOARD_INIT defined */
/* its our chance to print info about boot device */
void spl_board_init(void)
{	
	u32 boot_device = 0;
	uint64_t cpuid = 0;
	u16 test = 0;
	uint8_t ramtest = 0;

	/* Minimal init sequence for pmic setup of igep imx6 boards */
	#ifdef CONFIG_BASE0040
	reset_audio();
	#endif

	/* First we will test I2C */
	puts("---------------------------- |!| ISEE SPL TEST START |!| ----------------------------\n");

	/* Test I2C */	
	#ifdef CONFIG_SYS_I2C
	if(test_i2c()){
		puts("I2C FAIL.\n");
		error_loop(0);
	}else{
		puts("I2C OK.\n");
	}
	#endif

	/* Test EEPROM */
	if(test_eeprom()){
		puts("EEPROM FAIL.\n");
		error_loop(1);
	}else{
		puts("EEPROM OK.\n");
	}
	mdelay(2);

	/* If we are here we put test variable to 11000000 00000000 */ 
	test = 0xC000;

	/* Next we would want to know the counter just for control purpose */
	//test_counter_check(SPL_TEST_COUNTER_OFF);

	/* Test Power, PMIC */
	if(power_init_board()){
		puts("POWER FAIL.\n");
	}else{
		puts("POWER OK.\n");
		/* Test = 11100000 00000000 */
		test = test + 0x2000;
	}

	/* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 */
	boot_device = spl_boot_device();
	switch (boot_device) {
	case BOOT_DEVICE_MMC1:
		puts("Booting from MMC\n");
		break;
	case BOOT_DEVICE_NAND:
		puts("Booting from NAND\n");
		break;
	case BOOT_DEVICE_SATA:
		puts("Booting from SATA\n");
		break;
	default:
		puts("Unknown boot device\n");
	}

	/* If we are here (imx6) case CPU and SD should be working */
	puts("CPU OK.\n");
	puts("SD OK.\n");
	/* Test = 11111000 00000000 */
	test = test + 0x1800;
	
	/* Test Revision PINs */
	if (!(pcb_version() == TEST_REVISION)) {
		puts("REV FAIL.\n");
	}else{
		puts("REV OK.\n");
		/* Test = 11111100 00000000 */
		test = test + 0x400;
	}

	/* Internal RAM is working */
	puts("Internal RAM OK.\n");
	/* Test = 11111110 00000000 */
	test = test + 0x200;

	/* Test SDRAM */
	ramtest = test_SDRAM();

	if((ramtest & 0x01) == 0x01){
		puts("SDRAM CHIP 1 FAIL.\n");
	}

	if((ramtest & 0x02) == 0x02){
		puts("SDRAM CHIP 2 FAIL.\n");
	}

	if((ramtest & 0x04) == 0x04){
		puts("SDRAM CHIP 3 FAIL.\n");
	}
	
	if((ramtest & 0x08) == 0x08){
		puts("SDRAM CHIP 4 FAIL.\n");
	}

	if(!ramtest){
		puts("SDRAM OK.\n");
		test = test + 0xF0;
	}else{
		puts("SDRAM FAIL.\n");
		/* Do not jump to UBOOT until SDRAM hardware is good */
		error_loop(2);
	}

	/* Get CPU ID */
	cpuid = ((uint64_t)readl(0x21bc420) << 32) | (readl(0x21bc410));
	puts("CPUID OK.\n");
	/* Test = 11111111 00000000 */
	test = test + 0x100;

	/* Print Test Result */
	printf("SPL Test Result: 0x%x\n",test);

	/* Save Results to EEPROM */
	if (save_test(test,SPL_MAGIC_ID,cpuid)){
		puts("ISEE SPL TEST FAIL.\n");
	}else{
		puts("ISEE SPL TEST OK.\n");
	}
	/* Jump to U-BOOT */
}

#ifdef CONFIG_SPL_OS_BOOT
/* return 1 if we wish to boot to uboot vs 0 if we wish to falcon mode */
int spl_start_uboot(void)
{
	return 1;
}

void spl_board_prepare_for_linux(void)
{
	/* Fill here igep i.MX6 specific code to set up board just before
	Jumping to Linux in SPL Falcon Mode */

#ifdef CONFIG_FALCON_RAMDISK

	#define CONFIG_RAMDISK_START	0x5000
	#define CONFIG_RAMDISK_SIZE		22 * 1024 * 1024
	#define CONFIG_RAMDISK_ARG_DDR	0x14000000
	#define CONFIG_RAMDISK_MMC_DEV 	0

	u32 initrd_size_sectors;
	ulong start;
	ulong size;
	int dev;
	struct mmc *mmc;

	/* We need to re-fill mmc structure for blk_dread to work with mmc */
	dev = CONFIG_RAMDISK_MMC_DEV;
	mmc = find_mmc_device(dev);
	if (!mmc) {
		puts("error MMC init\n");
	}

	mmc_init(mmc);

	/* We expect initrd.img in offset RAMDISK_ARG_DDR of device x */
	start = CONFIG_RAMDISK_START;
	/* We exect initrd.img to be size of RAMDISK_SIZE */
	size = CONFIG_RAMDISK_SIZE;

	/* convert size to sectors - round up */
	initrd_size_sectors = (size / 512);

	/* Load initrd.img from RAW MMC starting at start with size size into 0x14000000 ADDR */
	blk_dread(mmc_get_blk_desc(mmc), start, initrd_size_sectors,
			  (void *)CONFIG_RAMDISK_ARG_DDR);

#endif
}

#endif
#endif