summaryrefslogtreecommitdiffstats
path: root/drivers/block/sata_dwc.c
blob: b2b3804be9b3989527e330dfa56377b0587c7653 (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
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
/*
 * sata_dwc.c
 *
 * Synopsys DesignWare Cores (DWC) SATA host driver
 *
 * Author: Mark Miesfeld <mmiesfeld@amcc.com>
 *
 * Ported from 2.6.19.2 to 2.6.25/26 by Stefan Roese <sr@denx.de>
 * Copyright 2008 DENX Software Engineering
 *
 * Based on versions provided by AMCC and Synopsys which are:
 *          Copyright 2006 Applied Micro Circuits Corporation
 *          COPYRIGHT (C) 2005  SYNOPSYS, INC.  ALL RIGHTS RESERVED
 *
 * This program is free software; you can redistribute
 * it and/or modify it under the terms of the GNU
 * General Public License as published by the
 * Free Software Foundation;  either version 2 of the  License,
 * or (at your option) any later version.
 *
 */
/*
 * SATA support based on the chip canyonlands.
 *
 * 04-17-2009
 *		The local version of this driver for the canyonlands board
 *		does not use interrupts but polls the chip instead.
 */

#include <common.h>
#include <command.h>
#include <pci.h>
#include <asm/processor.h>
#include <asm/errno.h>
#include <asm/io.h>
#include <malloc.h>
#include <ata.h>
#include <linux/ctype.h>

#include "sata_dwc.h"

#define DMA_NUM_CHANS			1
#define DMA_NUM_CHAN_REGS		8

#define AHB_DMA_BRST_DFLT		16

struct dmareg {
	u32 low;
	u32 high;
};

struct dma_chan_regs {
	struct dmareg sar;
	struct dmareg dar;
	struct dmareg llp;
	struct dmareg ctl;
	struct dmareg sstat;
	struct dmareg dstat;
	struct dmareg sstatar;
	struct dmareg dstatar;
	struct dmareg cfg;
	struct dmareg sgr;
	struct dmareg dsr;
};

struct dma_interrupt_regs {
	struct dmareg tfr;
	struct dmareg block;
	struct dmareg srctran;
	struct dmareg dsttran;
	struct dmareg error;
};

struct ahb_dma_regs {
	struct dma_chan_regs	chan_regs[DMA_NUM_CHAN_REGS];
	struct dma_interrupt_regs	interrupt_raw;
	struct dma_interrupt_regs	interrupt_status;
	struct dma_interrupt_regs	interrupt_mask;
	struct dma_interrupt_regs	interrupt_clear;
	struct dmareg			statusInt;
	struct dmareg			rq_srcreg;
	struct dmareg			rq_dstreg;
	struct dmareg			rq_sgl_srcreg;
	struct dmareg			rq_sgl_dstreg;
	struct dmareg			rq_lst_srcreg;
	struct dmareg			rq_lst_dstreg;
	struct dmareg			dma_cfg;
	struct dmareg			dma_chan_en;
	struct dmareg			dma_id;
	struct dmareg			dma_test;
	struct dmareg			res1;
	struct dmareg			res2;
	/* DMA Comp Params
	 * Param 6 = dma_param[0], Param 5 = dma_param[1],
	 * Param 4 = dma_param[2] ...
	 */
	struct dmareg			dma_params[6];
};

#define DMA_EN			0x00000001
#define DMA_DI			0x00000000
#define DMA_CHANNEL(ch)		(0x00000001 << (ch))
#define DMA_ENABLE_CHAN(ch)	((0x00000001 << (ch)) |	\
				((0x000000001 << (ch)) << 8))
#define DMA_DISABLE_CHAN(ch)	(0x00000000 | 	\
				((0x000000001 << (ch)) << 8))

#define SATA_DWC_MAX_PORTS	1
#define SATA_DWC_SCR_OFFSET	0x24
#define SATA_DWC_REG_OFFSET	0x64

struct sata_dwc_regs {
	u32 fptagr;
	u32 fpbor;
	u32 fptcr;
	u32 dmacr;
	u32 dbtsr;
	u32 intpr;
	u32 intmr;
	u32 errmr;
	u32 llcr;
	u32 phycr;
	u32 physr;
	u32 rxbistpd;
	u32 rxbistpd1;
	u32 rxbistpd2;
	u32 txbistpd;
	u32 txbistpd1;
	u32 txbistpd2;
	u32 bistcr;
	u32 bistfctr;
	u32 bistsr;
	u32 bistdecr;
	u32 res[15];
	u32 testr;
	u32 versionr;
	u32 idr;
	u32 unimpl[192];
	u32 dmadr[256];
};

#define SATA_DWC_TXFIFO_DEPTH		0x01FF
#define SATA_DWC_RXFIFO_DEPTH		0x01FF

#define SATA_DWC_DBTSR_MWR(size)	((size / 4) & SATA_DWC_TXFIFO_DEPTH)
#define SATA_DWC_DBTSR_MRD(size)	(((size / 4) &	\
					SATA_DWC_RXFIFO_DEPTH) << 16)
#define SATA_DWC_INTPR_DMAT		0x00000001
#define SATA_DWC_INTPR_NEWFP		0x00000002
#define SATA_DWC_INTPR_PMABRT		0x00000004
#define SATA_DWC_INTPR_ERR		0x00000008
#define SATA_DWC_INTPR_NEWBIST		0x00000010
#define SATA_DWC_INTPR_IPF		0x10000000
#define SATA_DWC_INTMR_DMATM		0x00000001
#define SATA_DWC_INTMR_NEWFPM		0x00000002
#define SATA_DWC_INTMR_PMABRTM		0x00000004
#define SATA_DWC_INTMR_ERRM		0x00000008
#define SATA_DWC_INTMR_NEWBISTM		0x00000010

#define SATA_DWC_DMACR_TMOD_TXCHEN	0x00000004
#define SATA_DWC_DMACR_TXRXCH_CLEAR	SATA_DWC_DMACR_TMOD_TXCHEN

#define SATA_DWC_QCMD_MAX	32

#define SATA_DWC_SERROR_ERR_BITS	0x0FFF0F03

#define HSDEVP_FROM_AP(ap)	(struct sata_dwc_device_port*)	\
				(ap)->private_data

struct sata_dwc_device {
	struct device		*dev;
	struct ata_probe_ent	*pe;
	struct ata_host		*host;
	u8			*reg_base;
	struct sata_dwc_regs	*sata_dwc_regs;
	int			irq_dma;
};

struct sata_dwc_device_port {
	struct sata_dwc_device	*hsdev;
	int			cmd_issued[SATA_DWC_QCMD_MAX];
	u32			dma_chan[SATA_DWC_QCMD_MAX];
	int			dma_pending[SATA_DWC_QCMD_MAX];
};

enum {
	SATA_DWC_CMD_ISSUED_NOT		= 0,
	SATA_DWC_CMD_ISSUED_PEND	= 1,
	SATA_DWC_CMD_ISSUED_EXEC	= 2,
	SATA_DWC_CMD_ISSUED_NODATA	= 3,

	SATA_DWC_DMA_PENDING_NONE	= 0,
	SATA_DWC_DMA_PENDING_TX		= 1,
	SATA_DWC_DMA_PENDING_RX		= 2,
};

#define msleep(a)	udelay(a * 1000)
#define ssleep(a)	msleep(a * 1000)

static int ata_probe_timeout = (ATA_TMOUT_INTERNAL / 100);

enum sata_dev_state {
	SATA_INIT = 0,
	SATA_READY = 1,
	SATA_NODEVICE = 2,
	SATA_ERROR = 3,
};
enum sata_dev_state dev_state = SATA_INIT;

static struct ahb_dma_regs		*sata_dma_regs = 0;
static struct ata_host			*phost;
static struct ata_port			ap;
static struct ata_port			*pap = &ap;
static struct ata_device		ata_device;
static struct sata_dwc_device_port	dwc_devp;

static void	*scr_addr_sstatus;
static u32	temp_n_block = 0;

static unsigned ata_exec_internal(struct ata_device *dev,
			struct ata_taskfile *tf, const u8 *cdb,
			int dma_dir, unsigned int buflen,
			unsigned long timeout);
static unsigned int ata_dev_set_feature(struct ata_device *dev,
			u8 enable,u8 feature);
static unsigned int ata_dev_init_params(struct ata_device *dev,
			u16 heads, u16 sectors);
static u8 ata_irq_on(struct ata_port *ap);
static struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
			unsigned int tag);
static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
			u8 status, int in_wq);
static void ata_tf_to_host(struct ata_port *ap,
			const struct ata_taskfile *tf);
static void ata_exec_command(struct ata_port *ap,
			const struct ata_taskfile *tf);
static unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc);
static u8 ata_check_altstatus(struct ata_port *ap);
static u8 ata_check_status(struct ata_port *ap);
static void ata_dev_select(struct ata_port *ap, unsigned int device,
			unsigned int wait, unsigned int can_sleep);
static void ata_qc_issue(struct ata_queued_cmd *qc);
static void ata_tf_load(struct ata_port *ap,
			const struct ata_taskfile *tf);
static int ata_dev_read_sectors(unsigned char* pdata,
			unsigned long datalen, u32 block, u32 n_block);
static int ata_dev_write_sectors(unsigned char* pdata,
			unsigned long datalen , u32 block, u32 n_block);
static void ata_std_dev_select(struct ata_port *ap, unsigned int device);
static void ata_qc_complete(struct ata_queued_cmd *qc);
static void __ata_qc_complete(struct ata_queued_cmd *qc);
static void fill_result_tf(struct ata_queued_cmd *qc);
static void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
static void ata_mmio_data_xfer(struct ata_device *dev,
			unsigned char *buf,
			unsigned int buflen,int do_write);
static void ata_pio_task(struct ata_port *arg_ap);
static void __ata_port_freeze(struct ata_port *ap);
static int ata_port_freeze(struct ata_port *ap);
static void ata_qc_free(struct ata_queued_cmd *qc);
static void ata_pio_sectors(struct ata_queued_cmd *qc);
static void ata_pio_sector(struct ata_queued_cmd *qc);
static void ata_pio_queue_task(struct ata_port *ap,
			void *data,unsigned long delay);
static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq);
static int sata_dwc_softreset(struct ata_port *ap);
static int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
		unsigned int flags, u16 *id);
static int check_sata_dev_state(void);

extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];

static const struct ata_port_info sata_dwc_port_info[] = {
	{
		.flags		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
				ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING |
				ATA_FLAG_SRST | ATA_FLAG_NCQ,
		.pio_mask	= 0x1f,
		.mwdma_mask	= 0x07,
		.udma_mask	= 0x7f,
	},
};

int init_sata(int dev)
{
	struct sata_dwc_device hsdev;
	struct ata_host host;
	struct ata_port_info pi = sata_dwc_port_info[0];
	struct ata_link *link;
	struct sata_dwc_device_port hsdevp = dwc_devp;
	u8 *base = 0;
	u8 *sata_dma_regs_addr = 0;
	u8 status;
	unsigned long base_addr = 0;
	int chan = 0;
	int rc;
	int i;

	phost = &host;

	base = (u8*)SATA_BASE_ADDR;

	hsdev.sata_dwc_regs = (void *__iomem)(base + SATA_DWC_REG_OFFSET);

	host.n_ports = SATA_DWC_MAX_PORTS;

	for (i = 0; i < SATA_DWC_MAX_PORTS; i++) {
		ap.pflags |= ATA_PFLAG_INITIALIZING;
		ap.flags = ATA_FLAG_DISABLED;
		ap.print_id = -1;
		ap.ctl = ATA_DEVCTL_OBS;
		ap.host = &host;
		ap.last_ctl = 0xFF;

		link = &ap.link;
		link->ap = &ap;
		link->pmp = 0;
		link->active_tag = ATA_TAG_POISON;
		link->hw_sata_spd_limit = 0;

		ap.port_no = i;
		host.ports[i] = &ap;
	}

	ap.pio_mask = pi.pio_mask;
	ap.mwdma_mask = pi.mwdma_mask;
	ap.udma_mask = pi.udma_mask;
	ap.flags |= pi.flags;
	ap.link.flags |= pi.link_flags;

	host.ports[0]->ioaddr.cmd_addr = base;
	host.ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
	scr_addr_sstatus = base + SATA_DWC_SCR_OFFSET;

	base_addr = (unsigned long)base;

	host.ports[0]->ioaddr.cmd_addr = (void *)base_addr + 0x00;
	host.ports[0]->ioaddr.data_addr = (void *)base_addr + 0x00;

	host.ports[0]->ioaddr.error_addr = (void *)base_addr + 0x04;
	host.ports[0]->ioaddr.feature_addr = (void *)base_addr + 0x04;

	host.ports[0]->ioaddr.nsect_addr = (void *)base_addr + 0x08;

	host.ports[0]->ioaddr.lbal_addr = (void *)base_addr + 0x0c;
	host.ports[0]->ioaddr.lbam_addr = (void *)base_addr + 0x10;
	host.ports[0]->ioaddr.lbah_addr = (void *)base_addr + 0x14;

	host.ports[0]->ioaddr.device_addr = (void *)base_addr + 0x18;
	host.ports[0]->ioaddr.command_addr = (void *)base_addr + 0x1c;
	host.ports[0]->ioaddr.status_addr = (void *)base_addr + 0x1c;

	host.ports[0]->ioaddr.altstatus_addr = (void *)base_addr + 0x20;
	host.ports[0]->ioaddr.ctl_addr = (void *)base_addr + 0x20;

	sata_dma_regs_addr = (u8*)SATA_DMA_REG_ADDR;
	sata_dma_regs = (void *__iomem)sata_dma_regs_addr;

	status = ata_check_altstatus(&ap);

	if (status == 0x7f) {
		printf("Hard Disk not found.\n");
		dev_state = SATA_NODEVICE;
		rc = FALSE;
		return rc;
	}

	printf("Waiting for device...");
	i = 0;
	while (1) {
		udelay(10000);

		status = ata_check_altstatus(&ap);

		if ((status & ATA_BUSY) == 0) {
			printf("\n");
			break;
		}

		i++;
		if (i > (ATA_RESET_TIME * 100)) {
			printf("** TimeOUT **\n");

			dev_state = SATA_NODEVICE;
			rc = FALSE;
			return rc;
		}
		if ((i >= 100) && ((i % 100) == 0))
			printf(".");
	}

	rc = sata_dwc_softreset(&ap);

	if (rc) {
		printf("sata_dwc : error. soft reset failed\n");
		return rc;
	}

	for (chan = 0; chan < DMA_NUM_CHANS; chan++) {
		out_le32(&(sata_dma_regs->interrupt_mask.error.low),
				DMA_DISABLE_CHAN(chan));

		out_le32(&(sata_dma_regs->interrupt_mask.tfr.low),
				DMA_DISABLE_CHAN(chan));
	}

	out_le32(&(sata_dma_regs->dma_cfg.low), DMA_DI);

	out_le32(&hsdev.sata_dwc_regs->intmr,
		SATA_DWC_INTMR_ERRM |
		SATA_DWC_INTMR_PMABRTM);

	/* Unmask the error bits that should trigger
	 * an error interrupt by setting the error mask register.
	 */
	out_le32(&hsdev.sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);

	hsdev.host = ap.host;
	memset(&hsdevp, 0, sizeof(hsdevp));
	hsdevp.hsdev = &hsdev;

	for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
		hsdevp.cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;

	out_le32((void __iomem *)scr_addr_sstatus + 4,
		in_le32((void __iomem *)scr_addr_sstatus + 4));

	rc = 0;
	return rc;
}

static u8 ata_check_altstatus(struct ata_port *ap)
{
	u8 val = 0;
	val = readb(ap->ioaddr.altstatus_addr);
	return val;
}

static int sata_dwc_softreset(struct ata_port *ap)
{
	u8 nsect,lbal = 0;
	u8 tmp = 0;
	u32 serror = 0;
	u8 status = 0;
	struct ata_ioports *ioaddr = &ap->ioaddr;

	serror = in_le32((void *)ap->ioaddr.scr_addr + (SCR_ERROR * 4));

	writeb(0x55, ioaddr->nsect_addr);
	writeb(0xaa, ioaddr->lbal_addr);
	writeb(0xaa, ioaddr->nsect_addr);
	writeb(0x55, ioaddr->lbal_addr);
	writeb(0x55, ioaddr->nsect_addr);
	writeb(0xaa, ioaddr->lbal_addr);

	nsect = readb(ioaddr->nsect_addr);
	lbal = readb(ioaddr->lbal_addr);

	if ((nsect == 0x55) && (lbal == 0xaa)) {
		printf("Device found\n");
	} else {
		printf("No device found\n");
		dev_state = SATA_NODEVICE;
		return FALSE;
	}

	tmp = ATA_DEVICE_OBS;
	writeb(tmp, ioaddr->device_addr);
	writeb(ap->ctl, ioaddr->ctl_addr);

	udelay(200);

	writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);

	udelay(200);
	writeb(ap->ctl, ioaddr->ctl_addr);

	msleep(150);
	status = ata_check_status(ap);

	msleep(50);
	ata_check_status(ap);

	while (1) {
		u8 status = ata_check_status(ap);

		if (!(status & ATA_BUSY))
			break;

		printf("Hard Disk status is BUSY.\n");
		msleep(50);
	}

	tmp = ATA_DEVICE_OBS;
	writeb(tmp, ioaddr->device_addr);

	nsect = readb(ioaddr->nsect_addr);
	lbal = readb(ioaddr->lbal_addr);

	return 0;
}

static u8 ata_check_status(struct ata_port *ap)
{
	u8 val = 0;
	val = readb(ap->ioaddr.status_addr);
	return val;
}

static int ata_id_has_hipm(const u16 *id)
{
	u16 val = id[76];

	if (val == 0 || val == 0xffff)
		return -1;

	return val & (1 << 9);
}

static int ata_id_has_dipm(const u16 *id)
{
	u16 val = id[78];

	if (val == 0 || val == 0xffff)
		return -1;

	return val & (1 << 3);
}

int scan_sata(int dev)
{
	int i;
	int rc;
	u8 status;
	const u16 *id;
	struct ata_device *ata_dev = &ata_device;
	unsigned long pio_mask, mwdma_mask, udma_mask;
	unsigned long xfer_mask;
	char revbuf[7];
	u16 iobuf[ATA_SECTOR_WORDS];

	memset(iobuf, 0, sizeof(iobuf));

	if (dev_state == SATA_NODEVICE)
		return 1;

	printf("Waiting for device...");
	i = 0;
	while (1) {
		udelay(10000);

		status = ata_check_altstatus(&ap);

		if ((status & ATA_BUSY) == 0) {
			printf("\n");
			break;
		}

		i++;
		if (i > (ATA_RESET_TIME * 100)) {
			printf("** TimeOUT **\n");

			dev_state = SATA_NODEVICE;
			return 1;
		}
		if ((i >= 100) && ((i % 100) == 0))
			printf(".");
	}

	udelay(1000);

	rc = ata_dev_read_id(ata_dev, &ata_dev->class,
			ATA_READID_POSTRESET,ata_dev->id);
	if (rc) {
		printf("sata_dwc : error. failed sata scan\n");
		return 1;
	}

	/* SATA drives indicate we have a bridge. We don't know which
	 * end of the link the bridge is which is a problem
	 */
	if (ata_id_is_sata(ata_dev->id))
		ap.cbl = ATA_CBL_SATA;

	id = ata_dev->id;

	ata_dev->flags &= ~ATA_DFLAG_CFG_MASK;
	ata_dev->max_sectors = 0;
	ata_dev->cdb_len = 0;
	ata_dev->n_sectors = 0;
	ata_dev->cylinders = 0;
	ata_dev->heads = 0;
	ata_dev->sectors = 0;

	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
		pio_mask <<= 3;
		pio_mask |= 0x7;
	} else {
		/* If word 64 isn't valid then Word 51 high byte holds
		 * the PIO timing number for the maximum. Turn it into
		 * a mask.
		 */
		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
		if (mode < 5) {
			pio_mask = (2 << mode) - 1;
		} else {
			pio_mask = 1;
		}
	}

	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;

	if (ata_id_is_cfa(id)) {
		int pio = id[163] & 0x7;
		int dma = (id[163] >> 3) & 7;

		if (pio)
			pio_mask |= (1 << 5);
		if (pio > 1)
			pio_mask |= (1 << 6);
		if (dma)
			mwdma_mask |= (1 << 3);
		if (dma > 1)
			mwdma_mask |= (1 << 4);
	}

	udma_mask = 0;
	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;

	xfer_mask = ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);

	if (ata_dev->class == ATA_DEV_ATA) {
		if (ata_id_is_cfa(id)) {
			if (id[162] & 1)
				printf("supports DRM functions and may "
					"not be fully accessable.\n");
			sprintf(revbuf, "%s", "CFA");
		} else {
			if (ata_id_has_tpm(id))
				printf("supports DRM functions and may "
						"not be fully accessable.\n");
		}

		ata_dev->n_sectors = ata_id_n_sectors((u16*)id);

		if (ata_dev->id[59] & 0x100)
			ata_dev->multi_count = ata_dev->id[59] & 0xff;

		if (ata_id_has_lba(id)) {
			const char *lba_desc;
			char ncq_desc[20];

			lba_desc = "LBA";
			ata_dev->flags |= ATA_DFLAG_LBA;
			if (ata_id_has_lba48(id)) {
				ata_dev->flags |= ATA_DFLAG_LBA48;
				lba_desc = "LBA48";

				if (ata_dev->n_sectors >= (1UL << 28) &&
					ata_id_has_flush_ext(id))
					ata_dev->flags |= ATA_DFLAG_FLUSH_EXT;
			}
			if (!ata_id_has_ncq(ata_dev->id))
				ncq_desc[0] = '\0';

			if (ata_dev->horkage & ATA_HORKAGE_NONCQ)
				sprintf(ncq_desc, "%s", "NCQ (not used)");

			if (ap.flags & ATA_FLAG_NCQ)
				ata_dev->flags |= ATA_DFLAG_NCQ;
		}
		ata_dev->cdb_len = 16;
	}
	ata_dev->max_sectors = ATA_MAX_SECTORS;
	if (ata_dev->flags & ATA_DFLAG_LBA48)
		ata_dev->max_sectors = ATA_MAX_SECTORS_LBA48;

	if (!(ata_dev->horkage & ATA_HORKAGE_IPM)) {
		if (ata_id_has_hipm(ata_dev->id))
			ata_dev->flags |= ATA_DFLAG_HIPM;
		if (ata_id_has_dipm(ata_dev->id))
			ata_dev->flags |= ATA_DFLAG_DIPM;
	}

	if ((ap.cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ata_dev->id))) {
		ata_dev->udma_mask &= ATA_UDMA5;
		ata_dev->max_sectors = ATA_MAX_SECTORS;
	}

	if (ata_dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
		printf("Drive reports diagnostics failure."
				"This may indicate a drive\n");
		printf("fault or invalid emulation."
				"Contact drive vendor for information.\n");
	}

	rc = check_sata_dev_state();

	ata_id_c_string(ata_dev->id,
			(unsigned char *)sata_dev_desc[dev].revision,
			 ATA_ID_FW_REV, sizeof(sata_dev_desc[dev].revision));
	ata_id_c_string(ata_dev->id,
			(unsigned char *)sata_dev_desc[dev].vendor,
			 ATA_ID_PROD, sizeof(sata_dev_desc[dev].vendor));
	ata_id_c_string(ata_dev->id,
			(unsigned char *)sata_dev_desc[dev].product,
			 ATA_ID_SERNO, sizeof(sata_dev_desc[dev].product));

	sata_dev_desc[dev].lba = (u32) ata_dev->n_sectors;

#ifdef CONFIG_LBA48
	if (ata_dev->id[83] & (1 << 10)) {
		sata_dev_desc[dev].lba48 = 1;
	} else {
		sata_dev_desc[dev].lba48 = 0;
	}
#endif

	return 0;
}

static u8 ata_busy_wait(struct ata_port *ap,
		unsigned int bits,unsigned int max)
{
	u8 status;

	do {
		udelay(10);
		status = ata_check_status(ap);
		max--;
	} while (status != 0xff && (status & bits) && (max > 0));

	return status;
}

static int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
		unsigned int flags, u16 *id)
{
	struct ata_port *ap = pap;
	unsigned int class = *p_class;
	struct ata_taskfile tf;
	unsigned int err_mask = 0;
	const char *reason;
	int may_fallback = 1, tried_spinup = 0;
	u8 status;
	int rc;

	status = ata_busy_wait(ap, ATA_BUSY, 30000);
	if (status & ATA_BUSY) {
		printf("BSY = 0 check. timeout.\n");
		rc = FALSE;
		return rc;
	}

	ata_dev_select(ap, dev->devno, 1, 1);

retry:
	memset(&tf, 0, sizeof(tf));
	ap->print_id = 1;
	ap->flags &= ~ATA_FLAG_DISABLED;
	tf.ctl = ap->ctl;
	tf.device = ATA_DEVICE_OBS;
	tf.command = ATA_CMD_ID_ATA;
	tf.protocol = ATA_PROT_PIO;

	/* Some devices choke if TF registers contain garbage.  Make
	 * sure those are properly initialized.
	 */
	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;

	/* Device presence detection is unreliable on some
	 * controllers.  Always poll IDENTIFY if available.
	 */
	tf.flags |= ATA_TFLAG_POLLING;

	temp_n_block = 1;

	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
					sizeof(id[0]) * ATA_ID_WORDS, 0);

	if (err_mask) {
		if (err_mask & AC_ERR_NODEV_HINT) {
			printf("NODEV after polling detection\n");
			return -ENOENT;
		}

		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
			/* Device or controller might have reported
			 * the wrong device class.  Give a shot at the
			 * other IDENTIFY if the current one is
			 * aborted by the device.
			 */
			if (may_fallback) {
				may_fallback = 0;

				if (class == ATA_DEV_ATA) {
					class = ATA_DEV_ATAPI;
				} else {
					class = ATA_DEV_ATA;
				}
				goto retry;
			}
			/* Control reaches here iff the device aborted
			 * both flavors of IDENTIFYs which happens
			 * sometimes with phantom devices.
			 */
			printf("both IDENTIFYs aborted, assuming NODEV\n");
			return -ENOENT;
		}
		rc = -EIO;
		reason = "I/O error";
		goto err_out;
	}

	/* Falling back doesn't make sense if ID data was read
	 * successfully at least once.
	 */
	may_fallback = 0;

	unsigned int id_cnt;

	for (id_cnt = 0; id_cnt < ATA_ID_WORDS; id_cnt++)
		id[id_cnt] = le16_to_cpu(id[id_cnt]);


	rc = -EINVAL;
	reason = "device reports invalid type";

	if (class == ATA_DEV_ATA) {
		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
			goto err_out;
	} else {
		if (ata_id_is_ata(id))
			goto err_out;
	}
	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
		tried_spinup = 1;
		/*
		 * Drive powered-up in standby mode, and requires a specific
		 * SET_FEATURES spin-up subcommand before it will accept
		 * anything other than the original IDENTIFY command.
		 */
		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
		if (err_mask && id[2] != 0x738c) {
			rc = -EIO;
			reason = "SPINUP failed";
			goto err_out;
		}
		/*
		 * If the drive initially returned incomplete IDENTIFY info,
		 * we now must reissue the IDENTIFY command.
		 */
		if (id[2] == 0x37c8)
			goto retry;
	}

	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
		/*
		 * The exact sequence expected by certain pre-ATA4 drives is:
		 * SRST RESET
		 * IDENTIFY (optional in early ATA)
		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
		 * anything else..
		 * Some drives were very specific about that exact sequence.
		 *
		 * Note that ATA4 says lba is mandatory so the second check
		 * shoud never trigger.
		 */
		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
			err_mask = ata_dev_init_params(dev, id[3], id[6]);
			if (err_mask) {
				rc = -EIO;
				reason = "INIT_DEV_PARAMS failed";
				goto err_out;
			}

			/* current CHS translation info (id[53-58]) might be
			 * changed. reread the identify device info.
			 */
			flags &= ~ATA_READID_POSTRESET;
			goto retry;
		}
	}

	*p_class = class;
	return 0;

err_out:
	return rc;
}

static u8 ata_wait_idle(struct ata_port *ap)
{
	u8 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
	return status;
}

static void ata_dev_select(struct ata_port *ap, unsigned int device,
		unsigned int wait, unsigned int can_sleep)
{
	if (wait)
		ata_wait_idle(ap);

	ata_std_dev_select(ap, device);

	if (wait)
		ata_wait_idle(ap);
}

static void ata_std_dev_select(struct ata_port *ap, unsigned int device)
{
	u8 tmp;

	if (device == 0) {
		tmp = ATA_DEVICE_OBS;
	} else {
		tmp = ATA_DEVICE_OBS | ATA_DEV1;
	}

	writeb(tmp, ap->ioaddr.device_addr);

	readb(ap->ioaddr.altstatus_addr);

	udelay(1);
}

static int waiting_for_reg_state(volatile u8 *offset,
				int timeout_msec,
				u32 sign)
{
	int i;
	u32 status;

	for (i = 0; i < timeout_msec; i++) {
		status = readl(offset);
		if ((status & sign) != 0)
			break;
		msleep(1);
	}

	return (i < timeout_msec) ? 0 : -1;
}

static void ata_qc_reinit(struct ata_queued_cmd *qc)
{
	qc->dma_dir = DMA_NONE;
	qc->flags = 0;
	qc->nbytes = qc->extrabytes = qc->curbytes = 0;
	qc->n_elem = 0;
	qc->err_mask = 0;
	qc->sect_size = ATA_SECT_SIZE;
	qc->nbytes = ATA_SECT_SIZE * temp_n_block;

	memset(&qc->tf, 0, sizeof(qc->tf));
	qc->tf.ctl = 0;
	qc->tf.device = ATA_DEVICE_OBS;

	qc->result_tf.command = ATA_DRDY;
	qc->result_tf.feature = 0;
}

struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
					unsigned int tag)
{
	if (tag < ATA_MAX_QUEUE)
		return &ap->qcmd[tag];
	return NULL;
}

static void __ata_port_freeze(struct ata_port *ap)
{
	printf("set port freeze.\n");
	ap->pflags |= ATA_PFLAG_FROZEN;
}

static int ata_port_freeze(struct ata_port *ap)
{
	__ata_port_freeze(ap);
	return 0;
}

unsigned ata_exec_internal(struct ata_device *dev,
			struct ata_taskfile *tf, const u8 *cdb,
			int dma_dir, unsigned int buflen,
			unsigned long timeout)
{
	struct ata_link *link = dev->link;
	struct ata_port *ap = pap;
	struct ata_queued_cmd *qc;
	unsigned int tag, preempted_tag;
	u32 preempted_sactive, preempted_qc_active;
	int preempted_nr_active_links;
	unsigned int err_mask;
	int rc = 0;
	u8 status;

	status = ata_busy_wait(ap, ATA_BUSY, 300000);
	if (status & ATA_BUSY) {
		printf("BSY = 0 check. timeout.\n");
		rc = FALSE;
		return rc;
	}

	if (ap->pflags & ATA_PFLAG_FROZEN)
		return AC_ERR_SYSTEM;

	tag = ATA_TAG_INTERNAL;

	if (test_and_set_bit(tag, &ap->qc_allocated)) {
		rc = FALSE;
		return rc;
	}

	qc = __ata_qc_from_tag(ap, tag);
	qc->tag = tag;
	qc->ap = ap;
	qc->dev = dev;

	ata_qc_reinit(qc);

	preempted_tag = link->active_tag;
	preempted_sactive = link->sactive;
	preempted_qc_active = ap->qc_active;
	preempted_nr_active_links = ap->nr_active_links;
	link->active_tag = ATA_TAG_POISON;
	link->sactive = 0;
	ap->qc_active = 0;
	ap->nr_active_links = 0;

	qc->tf = *tf;
	if (cdb)
		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
	qc->flags |= ATA_QCFLAG_RESULT_TF;
	qc->dma_dir = dma_dir;
	qc->private_data = 0;

	ata_qc_issue(qc);

	if (!timeout)
		timeout = ata_probe_timeout * 1000 / HZ;

	status = ata_busy_wait(ap, ATA_BUSY, 30000);
	if (status & ATA_BUSY) {
		printf("BSY = 0 check. timeout.\n");
		printf("altstatus = 0x%x.\n", status);
		qc->err_mask |= AC_ERR_OTHER;
		return qc->err_mask;
	}

	if (waiting_for_reg_state(ap->ioaddr.altstatus_addr, 1000, 0x8)) {
		u8 status = 0;
		u8 errorStatus = 0;

		status = readb(ap->ioaddr.altstatus_addr);
		if ((status & 0x01) != 0) {
			errorStatus = readb(ap->ioaddr.feature_addr);
			if (errorStatus == 0x04 &&
				qc->tf.command == ATA_CMD_PIO_READ_EXT){
				printf("Hard Disk doesn't support LBA48\n");
				dev_state = SATA_ERROR;
				qc->err_mask |= AC_ERR_OTHER;
				return qc->err_mask;
			}
		}
		qc->err_mask |= AC_ERR_OTHER;
		return qc->err_mask;
	}

	status = ata_busy_wait(ap, ATA_BUSY, 10);
	if (status & ATA_BUSY) {
		printf("BSY = 0 check. timeout.\n");
		qc->err_mask |= AC_ERR_OTHER;
		return qc->err_mask;
	}

	ata_pio_task(ap);

	if (!rc) {
		if (qc->flags & ATA_QCFLAG_ACTIVE) {
			qc->err_mask |= AC_ERR_TIMEOUT;
			ata_port_freeze(ap);
		}
	}

	if (qc->flags & ATA_QCFLAG_FAILED) {
		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
			qc->err_mask |= AC_ERR_DEV;

		if (!qc->err_mask)
			qc->err_mask |= AC_ERR_OTHER;

		if (qc->err_mask & ~AC_ERR_OTHER)
			qc->err_mask &= ~AC_ERR_OTHER;
	}

	*tf = qc->result_tf;
	err_mask = qc->err_mask;
	ata_qc_free(qc);
	link->active_tag = preempted_tag;
	link->sactive = preempted_sactive;
	ap->qc_active = preempted_qc_active;
	ap->nr_active_links = preempted_nr_active_links;

	if (ap->flags & ATA_FLAG_DISABLED) {
		err_mask |= AC_ERR_SYSTEM;
		ap->flags &= ~ATA_FLAG_DISABLED;
	}

	return err_mask;
}

static void ata_qc_issue(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	struct ata_link *link = qc->dev->link;
	u8 prot = qc->tf.protocol;

	if (ata_is_ncq(prot)) {
		if (!link->sactive)
			ap->nr_active_links++;
		link->sactive |= 1 << qc->tag;
	} else {
		ap->nr_active_links++;
		link->active_tag = qc->tag;
	}

	qc->flags |= ATA_QCFLAG_ACTIVE;
	ap->qc_active |= 1 << qc->tag;

	if (qc->dev->flags & ATA_DFLAG_SLEEPING) {
		msleep(1);
		return;
	}

	qc->err_mask |= ata_qc_issue_prot(qc);
	if (qc->err_mask)
		goto err;

	return;
err:
	ata_qc_complete(qc);
}

static unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;

	if (ap->flags & ATA_FLAG_PIO_POLLING) {
		switch (qc->tf.protocol) {
		case ATA_PROT_PIO:
		case ATA_PROT_NODATA:
		case ATAPI_PROT_PIO:
		case ATAPI_PROT_NODATA:
			qc->tf.flags |= ATA_TFLAG_POLLING;
			break;
		default:
			break;
		}
	}

	ata_dev_select(ap, qc->dev->devno, 1, 0);

	switch (qc->tf.protocol) {
	case ATA_PROT_PIO:
		if (qc->tf.flags & ATA_TFLAG_POLLING)
			qc->tf.ctl |= ATA_NIEN;

		ata_tf_to_host(ap, &qc->tf);

		ap->hsm_task_state = HSM_ST;

		if (qc->tf.flags & ATA_TFLAG_POLLING)
			ata_pio_queue_task(ap, qc, 0);

		break;

	default:
		return AC_ERR_SYSTEM;
	}

	return 0;
}

static void ata_tf_to_host(struct ata_port *ap,
			const struct ata_taskfile *tf)
{
	ata_tf_load(ap, tf);
	ata_exec_command(ap, tf);
}

static void ata_tf_load(struct ata_port *ap,
			const struct ata_taskfile *tf)
{
	struct ata_ioports *ioaddr = &ap->ioaddr;
	unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;

	if (tf->ctl != ap->last_ctl) {
		if (ioaddr->ctl_addr)
			writeb(tf->ctl, ioaddr->ctl_addr);
		ap->last_ctl = tf->ctl;
		ata_wait_idle(ap);
	}

	if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
		writeb(tf->hob_feature, ioaddr->feature_addr);
		writeb(tf->hob_nsect, ioaddr->nsect_addr);
		writeb(tf->hob_lbal, ioaddr->lbal_addr);
		writeb(tf->hob_lbam, ioaddr->lbam_addr);
		writeb(tf->hob_lbah, ioaddr->lbah_addr);
	}

	if (is_addr) {
		writeb(tf->feature, ioaddr->feature_addr);
		writeb(tf->nsect, ioaddr->nsect_addr);
		writeb(tf->lbal, ioaddr->lbal_addr);
		writeb(tf->lbam, ioaddr->lbam_addr);
		writeb(tf->lbah, ioaddr->lbah_addr);
	}

	if (tf->flags & ATA_TFLAG_DEVICE)
		writeb(tf->device, ioaddr->device_addr);

	ata_wait_idle(ap);
}

static void ata_exec_command(struct ata_port *ap,
			const struct ata_taskfile *tf)
{
	writeb(tf->command, ap->ioaddr.command_addr);

	readb(ap->ioaddr.altstatus_addr);

	udelay(1);
}

static void ata_pio_queue_task(struct ata_port *ap,
			void *data,unsigned long delay)
{
	ap->port_task_data = data;
}

static unsigned int ac_err_mask(u8 status)
{
	if (status & (ATA_BUSY | ATA_DRQ))
		return AC_ERR_HSM;
	if (status & (ATA_ERR | ATA_DF))
		return AC_ERR_DEV;
	return 0;
}

static unsigned int __ac_err_mask(u8 status)
{
	unsigned int mask = ac_err_mask(status);
	if (mask == 0)
		return AC_ERR_OTHER;
	return mask;
}

static void ata_pio_task(struct ata_port *arg_ap)
{
	struct ata_port *ap = arg_ap;
	struct ata_queued_cmd *qc = ap->port_task_data;
	u8 status;
	int poll_next;

fsm_start:
	/*
	 * This is purely heuristic.  This is a fast path.
	 * Sometimes when we enter, BSY will be cleared in
	 * a chk-status or two.  If not, the drive is probably seeking
	 * or something.  Snooze for a couple msecs, then
	 * chk-status again.  If still busy, queue delayed work.
	 */
	status = ata_busy_wait(ap, ATA_BUSY, 5);
	if (status & ATA_BUSY) {
		msleep(2);
		status = ata_busy_wait(ap, ATA_BUSY, 10);
		if (status & ATA_BUSY) {
			ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
			return;
		}
	}

	poll_next = ata_hsm_move(ap, qc, status, 1);

	/* another command or interrupt handler
	 * may be running at this point.
	 */
	if (poll_next)
		goto fsm_start;
}

static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
			u8 status, int in_wq)
{
	int poll_next;

fsm_start:
	switch (ap->hsm_task_state) {
	case HSM_ST_FIRST:
		poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);

		if ((status & ATA_DRQ) == 0) {
			if (status & (ATA_ERR | ATA_DF)) {
				qc->err_mask |= AC_ERR_DEV;
			} else {
				qc->err_mask |= AC_ERR_HSM;
			}
			ap->hsm_task_state = HSM_ST_ERR;
			goto fsm_start;
		}

		/* Device should not ask for data transfer (DRQ=1)
		 * when it finds something wrong.
		 * We ignore DRQ here and stop the HSM by
		 * changing hsm_task_state to HSM_ST_ERR and
		 * let the EH abort the command or reset the device.
		 */
		if (status & (ATA_ERR | ATA_DF)) {
			if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
				printf("DRQ=1 with device error, "
					"dev_stat 0x%X\n", status);
				qc->err_mask |= AC_ERR_HSM;
				ap->hsm_task_state = HSM_ST_ERR;
				goto fsm_start;
			}
		}

		if (qc->tf.protocol == ATA_PROT_PIO) {
			/* PIO data out protocol.
			 * send first data block.
			 */
			/* ata_pio_sectors() might change the state
			 * to HSM_ST_LAST. so, the state is changed here
			 * before ata_pio_sectors().
			 */
			ap->hsm_task_state = HSM_ST;
			ata_pio_sectors(qc);
		} else {
			printf("protocol is not ATA_PROT_PIO \n");
		}
		break;

	case HSM_ST:
		if ((status & ATA_DRQ) == 0) {
			if (status & (ATA_ERR | ATA_DF)) {
				qc->err_mask |= AC_ERR_DEV;
			} else {
				/* HSM violation. Let EH handle this.
				 * Phantom devices also trigger this
				 * condition.  Mark hint.
				 */
				qc->err_mask |= AC_ERR_HSM | AC_ERR_NODEV_HINT;
			}

			ap->hsm_task_state = HSM_ST_ERR;
			goto fsm_start;
		}
		/* For PIO reads, some devices may ask for
		 * data transfer (DRQ=1) alone with ERR=1.
		 * We respect DRQ here and transfer one
		 * block of junk data before changing the
		 * hsm_task_state to HSM_ST_ERR.
		 *
		 * For PIO writes, ERR=1 DRQ=1 doesn't make
		 * sense since the data block has been
		 * transferred to the device.
		 */
		if (status & (ATA_ERR | ATA_DF)) {
			qc->err_mask |= AC_ERR_DEV;

			if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
				ata_pio_sectors(qc);
				status = ata_wait_idle(ap);
			}

			if (status & (ATA_BUSY | ATA_DRQ))
				qc->err_mask |= AC_ERR_HSM;

			/* ata_pio_sectors() might change the
			 * state to HSM_ST_LAST. so, the state
			 * is changed after ata_pio_sectors().
			 */
			ap->hsm_task_state = HSM_ST_ERR;
			goto fsm_start;
		}

		ata_pio_sectors(qc);
		if (ap->hsm_task_state == HSM_ST_LAST &&
			(!(qc->tf.flags & ATA_TFLAG_WRITE))) {
			status = ata_wait_idle(ap);
			goto fsm_start;
		}

		poll_next = 1;
		break;

	case HSM_ST_LAST:
		if (!ata_ok(status)) {
			qc->err_mask |= __ac_err_mask(status);
			ap->hsm_task_state = HSM_ST_ERR;
			goto fsm_start;
		}

		ap->hsm_task_state = HSM_ST_IDLE;

		ata_hsm_qc_complete(qc, in_wq);

		poll_next = 0;
		break;

	case HSM_ST_ERR:
		/* make sure qc->err_mask is available to
		 * know what's wrong and recover
		 */
		ap->hsm_task_state = HSM_ST_IDLE;

		ata_hsm_qc_complete(qc, in_wq);

		poll_next = 0;
		break;
	default:
		poll_next = 0;
	}

	return poll_next;
}

static void ata_pio_sectors(struct ata_queued_cmd *qc)
{
	struct ata_port *ap;
	ap = pap;
	qc->pdata = ap->pdata;

	ata_pio_sector(qc);

	readb(qc->ap->ioaddr.altstatus_addr);
	udelay(1);
}

static void ata_pio_sector(struct ata_queued_cmd *qc)
{
	int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
	struct ata_port *ap = qc->ap;
	unsigned int offset;
	unsigned char *buf;
	char temp_data_buf[512];

	if (qc->curbytes == qc->nbytes - qc->sect_size)
		ap->hsm_task_state = HSM_ST_LAST;

	offset = qc->curbytes;

	switch (qc->tf.command) {
	case ATA_CMD_ID_ATA:
		buf = (unsigned char *)&ata_device.id[0];
		break;
	case ATA_CMD_PIO_READ_EXT:
	case ATA_CMD_PIO_READ:
	case ATA_CMD_PIO_WRITE_EXT:
	case ATA_CMD_PIO_WRITE:
		buf = qc->pdata + offset;
		break;
	default:
		buf = (unsigned char *)&temp_data_buf[0];
	}

	ata_mmio_data_xfer(qc->dev, buf, qc->sect_size, do_write);

	qc->curbytes += qc->sect_size;

}

static void ata_mmio_data_xfer(struct ata_device *dev, unsigned char *buf,
				unsigned int buflen, int do_write)
{
	struct ata_port *ap = pap;
	void __iomem *data_addr = ap->ioaddr.data_addr;
	unsigned int words = buflen >> 1;
	u16 *buf16 = (u16 *)buf;
	unsigned int i = 0;

	udelay(100);
	if (do_write) {
		for (i = 0; i < words; i++)
			writew(le16_to_cpu(buf16[i]), data_addr);
	} else {
		for (i = 0; i < words; i++)
			buf16[i] = cpu_to_le16(readw(data_addr));
	}

	if (buflen & 0x01) {
		__le16 align_buf[1] = { 0 };
		unsigned char *trailing_buf = buf + buflen - 1;

		if (do_write) {
			memcpy(align_buf, trailing_buf, 1);
			writew(le16_to_cpu(align_buf[0]), data_addr);
		} else {
			align_buf[0] = cpu_to_le16(readw(data_addr));
			memcpy(trailing_buf, align_buf, 1);
		}
	}
}

static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
{
	struct ata_port *ap = qc->ap;

	if (in_wq) {
		/* EH might have kicked in while host lock is
		 * released.
		 */
		qc = &ap->qcmd[qc->tag];
		if (qc) {
			if (!(qc->err_mask & AC_ERR_HSM)) {
				ata_irq_on(ap);
				ata_qc_complete(qc);
			} else {
				ata_port_freeze(ap);
			}
		}
	} else {
		if (!(qc->err_mask & AC_ERR_HSM)) {
			ata_qc_complete(qc);
		} else {
			ata_port_freeze(ap);
		}
	}
}

static u8 ata_irq_on(struct ata_port *ap)
{
	struct ata_ioports *ioaddr = &ap->ioaddr;
	u8 tmp;

	ap->ctl &= ~ATA_NIEN;
	ap->last_ctl = ap->ctl;

	if (ioaddr->ctl_addr)
		writeb(ap->ctl, ioaddr->ctl_addr);

	tmp = ata_wait_idle(ap);

	return tmp;
}

static unsigned int ata_tag_internal(unsigned int tag)
{
	return tag == ATA_MAX_QUEUE - 1;
}

static void ata_qc_complete(struct ata_queued_cmd *qc)
{
	struct ata_device *dev = qc->dev;
	if (qc->err_mask)
		qc->flags |= ATA_QCFLAG_FAILED;

	if (qc->flags & ATA_QCFLAG_FAILED) {
		if (!ata_tag_internal(qc->tag)) {
			fill_result_tf(qc);
			return;
		}
	}
	if (qc->flags & ATA_QCFLAG_RESULT_TF)
		fill_result_tf(qc);

	/* Some commands need post-processing after successful
	 * completion.
	 */
	switch (qc->tf.command) {
	case ATA_CMD_SET_FEATURES:
		if (qc->tf.feature != SETFEATURES_WC_ON &&
				qc->tf.feature != SETFEATURES_WC_OFF)
			break;
	case ATA_CMD_INIT_DEV_PARAMS:
	case ATA_CMD_SET_MULTI:
		break;

	case ATA_CMD_SLEEP:
		dev->flags |= ATA_DFLAG_SLEEPING;
		break;
	}

	__ata_qc_complete(qc);
}

static void fill_result_tf(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;

	qc->result_tf.flags = qc->tf.flags;
	ata_tf_read(ap, &qc->result_tf);
}

static void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
{
	struct ata_ioports *ioaddr = &ap->ioaddr;

	tf->command = ata_check_status(ap);
	tf->feature = readb(ioaddr->error_addr);
	tf->nsect = readb(ioaddr->nsect_addr);
	tf->lbal = readb(ioaddr->lbal_addr);
	tf->lbam = readb(ioaddr->lbam_addr);
	tf->lbah = readb(ioaddr->lbah_addr);
	tf->device = readb(ioaddr->device_addr);

	if (tf->flags & ATA_TFLAG_LBA48) {
		if (ioaddr->ctl_addr) {
			writeb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);

			tf->hob_feature = readb(ioaddr->error_addr);
			tf->hob_nsect = readb(ioaddr->nsect_addr);
			tf->hob_lbal = readb(ioaddr->lbal_addr);
			tf->hob_lbam = readb(ioaddr->lbam_addr);
			tf->hob_lbah = readb(ioaddr->lbah_addr);

			writeb(tf->ctl, ioaddr->ctl_addr);
			ap->last_ctl = tf->ctl;
		} else {
			printf("sata_dwc warnning register read.\n");
		}
	}
}

static void __ata_qc_complete(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	struct ata_link *link = qc->dev->link;

	link->active_tag = ATA_TAG_POISON;
	ap->nr_active_links--;

	if (qc->flags & ATA_QCFLAG_CLEAR_EXCL && ap->excl_link == link)
		ap->excl_link = NULL;

	qc->flags &= ~ATA_QCFLAG_ACTIVE;
	ap->qc_active &= ~(1 << qc->tag);
}

static void ata_qc_free(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	unsigned int tag;
	qc->flags = 0;
	tag = qc->tag;
	if (tag < ATA_MAX_QUEUE) {
		qc->tag = ATA_TAG_POISON;
		clear_bit(tag, &ap->qc_allocated);
	}
}

static int check_sata_dev_state(void)
{
	unsigned long datalen;
	unsigned char *pdata;
	int ret = 0;
	int i = 0;
	char temp_data_buf[512];

	while (1) {
		udelay(10000);

		pdata = (unsigned char*)&temp_data_buf[0];
		datalen = 512;

		ret = ata_dev_read_sectors(pdata, datalen, 0, 1);

		if (ret == TRUE)
			break;

		i++;
		if (i > (ATA_RESET_TIME * 100)) {
			printf("** TimeOUT **\n");
			dev_state = SATA_NODEVICE;
			return FALSE;
		}

		if ((i >= 100) && ((i % 100) == 0))
			printf(".");
	}

	dev_state = SATA_READY;

	return TRUE;
}

static unsigned int ata_dev_set_feature(struct ata_device *dev,
				u8 enable, u8 feature)
{
	struct ata_taskfile tf;
	struct ata_port *ap;
	ap = pap;
	unsigned int err_mask;

	memset(&tf, 0, sizeof(tf));
	tf.ctl = ap->ctl;

	tf.device = ATA_DEVICE_OBS;
	tf.command = ATA_CMD_SET_FEATURES;
	tf.feature = enable;
	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
	tf.protocol = ATA_PROT_NODATA;
	tf.nsect = feature;

	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, 0, 0);

	return err_mask;
}

static unsigned int ata_dev_init_params(struct ata_device *dev,
				u16 heads, u16 sectors)
{
	struct ata_taskfile tf;
	struct ata_port *ap;
	ap = pap;
	unsigned int err_mask;

	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
		return AC_ERR_INVALID;

	memset(&tf, 0, sizeof(tf));
	tf.ctl = ap->ctl;
	tf.device = ATA_DEVICE_OBS;
	tf.command = ATA_CMD_INIT_DEV_PARAMS;
	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
	tf.protocol = ATA_PROT_NODATA;
	tf.nsect = sectors;
	tf.device |= (heads - 1) & 0x0f;

	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, 0, 0);

	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
		err_mask = 0;

	return err_mask;
}

#if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
#define SATA_MAX_READ_BLK 0xFF
#else
#define SATA_MAX_READ_BLK 0xFFFF
#endif

ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
{
	ulong start,blks, buf_addr;
	unsigned short smallblks;
	unsigned long datalen;
	unsigned char *pdata;
	device &= 0xff;

	u32 block = 0;
	u32 n_block = 0;

	if (dev_state != SATA_READY)
		return 0;

	buf_addr = (unsigned long)buffer;
	start = blknr;
	blks = blkcnt;
	do {
		pdata = (unsigned char *)buf_addr;
		if (blks > SATA_MAX_READ_BLK) {
			datalen = sata_dev_desc[device].blksz * SATA_MAX_READ_BLK;
			smallblks = SATA_MAX_READ_BLK;

			block = (u32)start;
			n_block = (u32)smallblks;

			start += SATA_MAX_READ_BLK;
			blks -= SATA_MAX_READ_BLK;
		} else {
			datalen = sata_dev_desc[device].blksz * SATA_MAX_READ_BLK;
			datalen = sata_dev_desc[device].blksz * blks;
			smallblks = (unsigned short)blks;

			block = (u32)start;
			n_block = (u32)smallblks;

			start += blks;
			blks = 0;
		}

		if (ata_dev_read_sectors(pdata, datalen, block, n_block) != TRUE) {
			printf("sata_dwc : Hard disk read error.\n");
			blkcnt -= blks;
			break;
		}
		buf_addr += datalen;
	} while (blks != 0);

	return (blkcnt);
}

static int ata_dev_read_sectors(unsigned char *pdata, unsigned long datalen,
						u32 block, u32 n_block)
{
	struct ata_port *ap = pap;
	struct ata_device *dev = &ata_device;
	struct ata_taskfile tf;
	unsigned int class = ATA_DEV_ATA;
	unsigned int err_mask = 0;
	const char *reason;
	int may_fallback = 1;
	int rc;

	if (dev_state == SATA_ERROR)
		return FALSE;

	ata_dev_select(ap, dev->devno, 1, 1);

retry:
	memset(&tf, 0, sizeof(tf));
	tf.ctl = ap->ctl;
	ap->print_id = 1;
	ap->flags &= ~ATA_FLAG_DISABLED;

	ap->pdata = pdata;

	tf.device = ATA_DEVICE_OBS;

	temp_n_block = n_block;

#ifdef CONFIG_LBA48
	tf.command = ATA_CMD_PIO_READ_EXT;
	tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;

	tf.hob_feature = 31;
	tf.feature = 31;
	tf.hob_nsect = (n_block >> 8) & 0xff;
	tf.nsect = n_block & 0xff;

	tf.hob_lbah = 0x0;
	tf.hob_lbam = 0x0;
	tf.hob_lbal = (block >> 24) & 0xff;
	tf.lbah = (block >> 16) & 0xff;
	tf.lbam = (block >> 8) & 0xff;
	tf.lbal = block & 0xff;

	tf.device = 1 << 6;
	if (tf.flags & ATA_TFLAG_FUA)
		tf.device |= 1 << 7;
#else
	tf.command = ATA_CMD_PIO_READ;
	tf.flags |= ATA_TFLAG_LBA ;

	tf.feature = 31;
	tf.nsect = n_block & 0xff;

	tf.lbah = (block >> 16) & 0xff;
	tf.lbam = (block >> 8) & 0xff;
	tf.lbal = block & 0xff;

	tf.device = (block >> 24) & 0xf;

	tf.device |= 1 << 6;
	if (tf.flags & ATA_TFLAG_FUA)
		tf.device |= 1 << 7;

#endif

	tf.protocol = ATA_PROT_PIO;

	/* Some devices choke if TF registers contain garbage.  Make
	 * sure those are properly initialized.
	 */
	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
	tf.flags |= ATA_TFLAG_POLLING;

	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 0, 0);

	if (err_mask) {
		if (err_mask & AC_ERR_NODEV_HINT) {
			printf("READ_SECTORS NODEV after polling detection\n");
			return -ENOENT;
		}

		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
			/* Device or controller might have reported
			 * the wrong device class.  Give a shot at the
			 * other IDENTIFY if the current one is
			 * aborted by the device.
			 */
			if (may_fallback) {
				may_fallback = 0;

				if (class == ATA_DEV_ATA) {
					class = ATA_DEV_ATAPI;
				} else {
					class = ATA_DEV_ATA;
				}
				goto retry;
			}
			/* Control reaches here iff the device aborted
			 * both flavors of IDENTIFYs which happens
			 * sometimes with phantom devices.
			 */
			printf("both IDENTIFYs aborted, assuming NODEV\n");
			return -ENOENT;
		}

		rc = -EIO;
		reason = "I/O error";
		goto err_out;
	}

	/* Falling back doesn't make sense if ID data was read
	 * successfully at least once.
	 */
	may_fallback = 0;

	rc = -EINVAL;
	reason = "device reports invalid type";

	return TRUE;

err_out:
	printf("failed to READ SECTORS (%s, err_mask=0x%x)\n", reason, err_mask);
	return FALSE;
}

#if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
#define SATA_MAX_WRITE_BLK 0xFF
#else
#define SATA_MAX_WRITE_BLK 0xFFFF
#endif

ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
{
	ulong start,blks, buf_addr;
	unsigned short smallblks;
	unsigned long datalen;
	unsigned char *pdata;
	device &= 0xff;


	u32 block = 0;
	u32 n_block = 0;

	if (dev_state != SATA_READY)
		return 0;

	buf_addr = (unsigned long)buffer;
	start = blknr;
	blks = blkcnt;
	do {
		pdata = (unsigned char *)buf_addr;
		if (blks > SATA_MAX_WRITE_BLK) {
			datalen = sata_dev_desc[device].blksz * SATA_MAX_WRITE_BLK;
			smallblks = SATA_MAX_WRITE_BLK;

			block = (u32)start;
			n_block = (u32)smallblks;

			start += SATA_MAX_WRITE_BLK;
			blks -= SATA_MAX_WRITE_BLK;
		} else {
			datalen = sata_dev_desc[device].blksz * blks;
			smallblks = (unsigned short)blks;

			block = (u32)start;
			n_block = (u32)smallblks;

			start += blks;
			blks = 0;
		}

		if (ata_dev_write_sectors(pdata, datalen, block, n_block) != TRUE) {
			printf("sata_dwc : Hard disk read error.\n");
			blkcnt -= blks;
			break;
		}
		buf_addr += datalen;
	} while (blks != 0);

	return (blkcnt);
}

static int ata_dev_write_sectors(unsigned char* pdata, unsigned long datalen,
						u32 block, u32 n_block)
{
	struct ata_port *ap = pap;
	struct ata_device *dev = &ata_device;
	struct ata_taskfile tf;
	unsigned int class = ATA_DEV_ATA;
	unsigned int err_mask = 0;
	const char *reason;
	int may_fallback = 1;
	int rc;

	if (dev_state == SATA_ERROR)
		return FALSE;

	ata_dev_select(ap, dev->devno, 1, 1);

retry:
	memset(&tf, 0, sizeof(tf));
	tf.ctl = ap->ctl;
	ap->print_id = 1;
	ap->flags &= ~ATA_FLAG_DISABLED;

	ap->pdata = pdata;

	tf.device = ATA_DEVICE_OBS;

	temp_n_block = n_block;


#ifdef CONFIG_LBA48
	tf.command = ATA_CMD_PIO_WRITE_EXT;
	tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48 | ATA_TFLAG_WRITE;

	tf.hob_feature = 31;
	tf.feature = 31;
	tf.hob_nsect = (n_block >> 8) & 0xff;
	tf.nsect = n_block & 0xff;

	tf.hob_lbah = 0x0;
	tf.hob_lbam = 0x0;
	tf.hob_lbal = (block >> 24) & 0xff;
	tf.lbah = (block >> 16) & 0xff;
	tf.lbam = (block >> 8) & 0xff;
	tf.lbal = block & 0xff;

	tf.device = 1 << 6;
	if (tf.flags & ATA_TFLAG_FUA)
		tf.device |= 1 << 7;
#else
	tf.command = ATA_CMD_PIO_WRITE;
	tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_WRITE;

	tf.feature = 31;
	tf.nsect = n_block & 0xff;

	tf.lbah = (block >> 16) & 0xff;
	tf.lbam = (block >> 8) & 0xff;
	tf.lbal = block & 0xff;

	tf.device = (block >> 24) & 0xf;

	tf.device |= 1 << 6;
	if (tf.flags & ATA_TFLAG_FUA)
		tf.device |= 1 << 7;

#endif

	tf.protocol = ATA_PROT_PIO;

	/* Some devices choke if TF registers contain garbage.  Make
	 * sure those are properly initialized.
	 */
	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
	tf.flags |= ATA_TFLAG_POLLING;

	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 0, 0);

	if (err_mask) {
		if (err_mask & AC_ERR_NODEV_HINT) {
			printf("READ_SECTORS NODEV after polling detection\n");
			return -ENOENT;
		}

		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
			/* Device or controller might have reported
			 * the wrong device class.  Give a shot at the
			 * other IDENTIFY if the current one is
			 * aborted by the device.
			 */
			if (may_fallback) {
				may_fallback = 0;

				if (class == ATA_DEV_ATA) {
					class = ATA_DEV_ATAPI;
				} else {
					class = ATA_DEV_ATA;
				}
				goto retry;
			}
			/* Control reaches here iff the device aborted
			 * both flavors of IDENTIFYs which happens
			 * sometimes with phantom devices.
			 */
			printf("both IDENTIFYs aborted, assuming NODEV\n");
			return -ENOENT;
		}

		rc = -EIO;
		reason = "I/O error";
		goto err_out;
	}

	/* Falling back doesn't make sense if ID data was read
	 * successfully at least once.
	 */
	may_fallback = 0;

	rc = -EINVAL;
	reason = "device reports invalid type";

	return TRUE;

err_out:
	printf("failed to WRITE SECTORS (%s, err_mask=0x%x)\n", reason, err_mask);
	return FALSE;
}
OpenPOWER on IntegriCloud