4aa97eefe0d4f536111c777e9e4be3ec2b1ad9c2
[oweals/u-boot.git] / drivers / ata / sata_mv.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) Excito Elektronik i Skåne AB, 2010.
4  * Author: Tor Krill <tor@excito.com>
5  *
6  * Copyright (C) 2015, 2019 Stefan Roese <sr@denx.de>
7  */
8
9 /*
10  * This driver supports the SATA controller of some Mavell SoC's.
11  * Here a (most likely incomplete) list of the supported SoC's:
12  * - Kirkwood
13  * - Armada 370
14  * - Armada XP
15  *
16  * This driver implementation is an alternative to the already available
17  * driver via the "ide" commands interface (drivers/block/mvsata_ide.c).
18  * But this driver only supports PIO mode and as this new driver also
19  * supports transfer via DMA, its much faster.
20  *
21  * Please note, that the newer SoC's (e.g. Armada 38x) are not supported
22  * by this driver. As they have an AHCI compatible SATA controller
23  * integrated.
24  */
25
26 /*
27  * TODO:
28  * Better error recovery
29  * No support for using PRDs (Thus max 64KB transfers)
30  * No NCQ support
31  * No port multiplier support
32  */
33
34 #include <common.h>
35 #include <ahci.h>
36 #include <blk.h>
37 #include <cpu_func.h>
38 #include <dm.h>
39 #include <log.h>
40 #include <asm/cache.h>
41 #include <dm/device-internal.h>
42 #include <dm/lists.h>
43 #include <fis.h>
44 #include <libata.h>
45 #include <malloc.h>
46 #include <sata.h>
47 #include <linux/errno.h>
48 #include <asm/io.h>
49 #include <linux/mbus.h>
50
51 #include <asm/arch/soc.h>
52 #if defined(CONFIG_ARCH_KIRKWOOD)
53 #define SATAHC_BASE             KW_SATA_BASE
54 #else
55 #define SATAHC_BASE             MVEBU_AXP_SATA_BASE
56 #endif
57
58 #define SATA0_BASE              (SATAHC_BASE + 0x2000)
59 #define SATA1_BASE              (SATAHC_BASE + 0x4000)
60
61 /* EDMA registers */
62 #define EDMA_CFG                0x000
63 #define EDMA_CFG_NCQ            (1 << 5)
64 #define EDMA_CFG_EQUE           (1 << 9)
65 #define EDMA_TIMER              0x004
66 #define EDMA_IECR               0x008
67 #define EDMA_IEMR               0x00c
68 #define EDMA_RQBA_HI            0x010
69 #define EDMA_RQIPR              0x014
70 #define EDMA_RQIPR_IPMASK       (0x1f << 5)
71 #define EDMA_RQIPR_IPSHIFT      5
72 #define EDMA_RQOPR              0x018
73 #define EDMA_RQOPR_OPMASK       (0x1f << 5)
74 #define EDMA_RQOPR_OPSHIFT      5
75 #define EDMA_RSBA_HI            0x01c
76 #define EDMA_RSIPR              0x020
77 #define EDMA_RSIPR_IPMASK       (0x1f << 3)
78 #define EDMA_RSIPR_IPSHIFT      3
79 #define EDMA_RSOPR              0x024
80 #define EDMA_RSOPR_OPMASK       (0x1f << 3)
81 #define EDMA_RSOPR_OPSHIFT      3
82 #define EDMA_CMD                0x028
83 #define EDMA_CMD_ENEDMA         (0x01 << 0)
84 #define EDMA_CMD_DISEDMA        (0x01 << 1)
85 #define EDMA_CMD_ATARST         (0x01 << 2)
86 #define EDMA_CMD_FREEZE         (0x01 << 4)
87 #define EDMA_TEST_CTL           0x02c
88 #define EDMA_STATUS             0x030
89 #define EDMA_IORTO              0x034
90 #define EDMA_CDTR               0x040
91 #define EDMA_HLTCND             0x060
92 #define EDMA_NTSR               0x094
93
94 /* Basic DMA registers */
95 #define BDMA_CMD                0x224
96 #define BDMA_STATUS             0x228
97 #define BDMA_DTLB               0x22c
98 #define BDMA_DTHB               0x230
99 #define BDMA_DRL                0x234
100 #define BDMA_DRH                0x238
101
102 /* SATA Interface registers */
103 #define SIR_ICFG                0x050
104 #define SIR_CFG_GEN2EN          (0x1 << 7)
105 #define SIR_PLL_CFG             0x054
106 #define SIR_SSTATUS             0x300
107 #define SSTATUS_DET_MASK        (0x0f << 0)
108 #define SIR_SERROR              0x304
109 #define SIR_SCONTROL            0x308
110 #define SIR_SCONTROL_DETEN      (0x01 << 0)
111 #define SIR_LTMODE              0x30c
112 #define SIR_LTMODE_NELBE        (0x01 << 7)
113 #define SIR_PHYMODE3            0x310
114 #define SIR_PHYMODE4            0x314
115 #define SIR_PHYMODE1            0x32c
116 #define SIR_PHYMODE2            0x330
117 #define SIR_BIST_CTRL           0x334
118 #define SIR_BIST_DW1            0x338
119 #define SIR_BIST_DW2            0x33c
120 #define SIR_SERR_IRQ_MASK       0x340
121 #define SIR_SATA_IFCTRL         0x344
122 #define SIR_SATA_TESTCTRL       0x348
123 #define SIR_SATA_IFSTATUS       0x34c
124 #define SIR_VEND_UNIQ           0x35c
125 #define SIR_FIS_CFG             0x360
126 #define SIR_FIS_IRQ_CAUSE       0x364
127 #define SIR_FIS_IRQ_MASK        0x368
128 #define SIR_FIS_DWORD0          0x370
129 #define SIR_FIS_DWORD1          0x374
130 #define SIR_FIS_DWORD2          0x378
131 #define SIR_FIS_DWORD3          0x37c
132 #define SIR_FIS_DWORD4          0x380
133 #define SIR_FIS_DWORD5          0x384
134 #define SIR_FIS_DWORD6          0x388
135 #define SIR_PHYM9_GEN2          0x398
136 #define SIR_PHYM9_GEN1          0x39c
137 #define SIR_PHY_CFG             0x3a0
138 #define SIR_PHYCTL              0x3a4
139 #define SIR_PHYM10              0x3a8
140 #define SIR_PHYM12              0x3b0
141
142 /* Shadow registers */
143 #define PIO_DATA                0x100
144 #define PIO_ERR_FEATURES        0x104
145 #define PIO_SECTOR_COUNT        0x108
146 #define PIO_LBA_LOW             0x10c
147 #define PIO_LBA_MID             0x110
148 #define PIO_LBA_HI              0x114
149 #define PIO_DEVICE              0x118
150 #define PIO_CMD_STATUS          0x11c
151 #define PIO_STATUS_ERR          (0x01 << 0)
152 #define PIO_STATUS_DRQ          (0x01 << 3)
153 #define PIO_STATUS_DF           (0x01 << 5)
154 #define PIO_STATUS_DRDY         (0x01 << 6)
155 #define PIO_STATUS_BSY          (0x01 << 7)
156 #define PIO_CTRL_ALTSTAT        0x120
157
158 /* SATAHC arbiter registers */
159 #define SATAHC_CFG              0x000
160 #define SATAHC_RQOP             0x004
161 #define SATAHC_RQIP             0x008
162 #define SATAHC_ICT              0x00c
163 #define SATAHC_ITT              0x010
164 #define SATAHC_ICR              0x014
165 #define SATAHC_ICR_PORT0        (0x01 << 0)
166 #define SATAHC_ICR_PORT1        (0x01 << 1)
167 #define SATAHC_MIC              0x020
168 #define SATAHC_MIM              0x024
169 #define SATAHC_LED_CFG          0x02c
170
171 #define REQUEST_QUEUE_SIZE      32
172 #define RESPONSE_QUEUE_SIZE     REQUEST_QUEUE_SIZE
173
174 struct crqb {
175         u32 dtb_low;            /* DW0 */
176         u32 dtb_high;           /* DW1 */
177         u32 control_flags;      /* DW2 */
178         u32 drb_count;          /* DW3 */
179         u32 ata_cmd_feat;       /* DW4 */
180         u32 ata_addr;           /* DW5 */
181         u32 ata_addr_exp;       /* DW6 */
182         u32 ata_sect_count;     /* DW7 */
183 };
184
185 #define CRQB_ALIGN                      0x400
186
187 #define CRQB_CNTRLFLAGS_DIR             (0x01 << 0)
188 #define CRQB_CNTRLFLAGS_DQTAGMASK       (0x1f << 1)
189 #define CRQB_CNTRLFLAGS_DQTAGSHIFT      1
190 #define CRQB_CNTRLFLAGS_PMPORTMASK      (0x0f << 12)
191 #define CRQB_CNTRLFLAGS_PMPORTSHIFT     12
192 #define CRQB_CNTRLFLAGS_PRDMODE         (0x01 << 16)
193 #define CRQB_CNTRLFLAGS_HQTAGMASK       (0x1f << 17)
194 #define CRQB_CNTRLFLAGS_HQTAGSHIFT      17
195
196 #define CRQB_CMDFEAT_CMDMASK            (0xff << 16)
197 #define CRQB_CMDFEAT_CMDSHIFT           16
198 #define CRQB_CMDFEAT_FEATMASK           (0xff << 16)
199 #define CRQB_CMDFEAT_FEATSHIFT          24
200
201 #define CRQB_ADDR_LBA_LOWMASK           (0xff << 0)
202 #define CRQB_ADDR_LBA_LOWSHIFT          0
203 #define CRQB_ADDR_LBA_MIDMASK           (0xff << 8)
204 #define CRQB_ADDR_LBA_MIDSHIFT          8
205 #define CRQB_ADDR_LBA_HIGHMASK          (0xff << 16)
206 #define CRQB_ADDR_LBA_HIGHSHIFT         16
207 #define CRQB_ADDR_DEVICE_MASK           (0xff << 24)
208 #define CRQB_ADDR_DEVICE_SHIFT          24
209
210 #define CRQB_ADDR_LBA_LOW_EXP_MASK      (0xff << 0)
211 #define CRQB_ADDR_LBA_LOW_EXP_SHIFT     0
212 #define CRQB_ADDR_LBA_MID_EXP_MASK      (0xff << 8)
213 #define CRQB_ADDR_LBA_MID_EXP_SHIFT     8
214 #define CRQB_ADDR_LBA_HIGH_EXP_MASK     (0xff << 16)
215 #define CRQB_ADDR_LBA_HIGH_EXP_SHIFT    16
216 #define CRQB_ADDR_FEATURE_EXP_MASK      (0xff << 24)
217 #define CRQB_ADDR_FEATURE_EXP_SHIFT     24
218
219 #define CRQB_SECTCOUNT_COUNT_MASK       (0xff << 0)
220 #define CRQB_SECTCOUNT_COUNT_SHIFT      0
221 #define CRQB_SECTCOUNT_COUNT_EXP_MASK   (0xff << 8)
222 #define CRQB_SECTCOUNT_COUNT_EXP_SHIFT  8
223
224 #define MVSATA_WIN_CONTROL(w)   (SATAHC_BASE + 0x30 + ((w) << 4))
225 #define MVSATA_WIN_BASE(w)      (SATAHC_BASE + 0x34 + ((w) << 4))
226
227 struct eprd {
228         u32 phyaddr_low;
229         u32 bytecount_eot;
230         u32 phyaddr_hi;
231         u32 reserved;
232 };
233
234 #define EPRD_PHYADDR_MASK       0xfffffffe
235 #define EPRD_BYTECOUNT_MASK     0x0000ffff
236 #define EPRD_EOT                (0x01 << 31)
237
238 struct crpb {
239         u32 id;
240         u32 flags;
241         u32 timestamp;
242 };
243
244 #define CRPB_ALIGN              0x100
245
246 #define READ_CMD                0
247 #define WRITE_CMD               1
248
249 /*
250  * Since we don't use PRDs yet max transfer size
251  * is 64KB
252  */
253 #define MV_ATA_MAX_SECTORS      (65535 / ATA_SECT_SIZE)
254
255 /* Keep track if hw is initialized or not */
256 static u32 hw_init;
257
258 struct mv_priv {
259         char name[12];
260         u32 link;
261         u32 regbase;
262         u32 queue_depth;
263         u16 pio;
264         u16 mwdma;
265         u16 udma;
266         int dev_nr;
267
268         void *crqb_alloc;
269         struct crqb *request;
270
271         void *crpb_alloc;
272         struct crpb *response;
273 };
274
275 static int ata_wait_register(u32 *addr, u32 mask, u32 val, u32 timeout_msec)
276 {
277         ulong start;
278
279         start = get_timer(0);
280         do {
281                 if ((in_le32(addr) & mask) == val)
282                         return 0;
283         } while (get_timer(start) < timeout_msec);
284
285         return -ETIMEDOUT;
286 }
287
288 /* Cut from sata_mv in linux kernel */
289 static int mv_stop_edma_engine(struct udevice *dev, int port)
290 {
291         struct mv_priv *priv = dev_get_platdata(dev);
292         int i;
293
294         /* Disable eDMA. The disable bit auto clears. */
295         out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_DISEDMA);
296
297         /* Wait for the chip to confirm eDMA is off. */
298         for (i = 10000; i > 0; i--) {
299                 u32 reg = in_le32(priv->regbase + EDMA_CMD);
300                 if (!(reg & EDMA_CMD_ENEDMA)) {
301                         debug("EDMA stop on port %d succesful\n", port);
302                         return 0;
303                 }
304                 udelay(10);
305         }
306         debug("EDMA stop on port %d failed\n", port);
307         return -1;
308 }
309
310 static int mv_start_edma_engine(struct udevice *dev, int port)
311 {
312         struct mv_priv *priv = dev_get_platdata(dev);
313         u32 tmp;
314
315         /* Check preconditions */
316         tmp = in_le32(priv->regbase + SIR_SSTATUS);
317         if ((tmp & SSTATUS_DET_MASK) != 0x03) {
318                 printf("Device error on port: %d\n", port);
319                 return -1;
320         }
321
322         tmp = in_le32(priv->regbase + PIO_CMD_STATUS);
323         if (tmp & (ATA_BUSY | ATA_DRQ)) {
324                 printf("Device not ready on port: %d\n", port);
325                 return -1;
326         }
327
328         /* Clear interrupt cause */
329         out_le32(priv->regbase + EDMA_IECR, 0x0);
330
331         tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
332         tmp &= ~(port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1);
333         out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
334
335         /* Configure edma operation */
336         tmp = in_le32(priv->regbase + EDMA_CFG);
337         tmp &= ~EDMA_CFG_NCQ;   /* No NCQ */
338         tmp &= ~EDMA_CFG_EQUE;  /* Dont queue operations */
339         out_le32(priv->regbase + EDMA_CFG, tmp);
340
341         out_le32(priv->regbase + SIR_FIS_IRQ_CAUSE, 0x0);
342
343         /* Configure fis, set all to no-wait for now */
344         out_le32(priv->regbase + SIR_FIS_CFG, 0x0);
345
346         /* Setup request queue */
347         out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
348         out_le32(priv->regbase + EDMA_RQIPR, priv->request);
349         out_le32(priv->regbase + EDMA_RQOPR, 0x0);
350
351         /* Setup response queue */
352         out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
353         out_le32(priv->regbase + EDMA_RSOPR, priv->response);
354         out_le32(priv->regbase + EDMA_RSIPR, 0x0);
355
356         /* Start edma */
357         out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ENEDMA);
358
359         return 0;
360 }
361
362 static int mv_reset_channel(struct udevice *dev, int port)
363 {
364         struct mv_priv *priv = dev_get_platdata(dev);
365
366         /* Make sure edma is stopped  */
367         mv_stop_edma_engine(dev, port);
368
369         out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ATARST);
370         udelay(25);             /* allow reset propagation */
371         out_le32(priv->regbase + EDMA_CMD, 0);
372         mdelay(10);
373
374         return 0;
375 }
376
377 static void mv_reset_port(struct udevice *dev, int port)
378 {
379         struct mv_priv *priv = dev_get_platdata(dev);
380
381         mv_reset_channel(dev, port);
382
383         out_le32(priv->regbase + EDMA_CMD, 0x0);
384         out_le32(priv->regbase + EDMA_CFG, 0x101f);
385         out_le32(priv->regbase + EDMA_IECR, 0x0);
386         out_le32(priv->regbase + EDMA_IEMR, 0x0);
387         out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
388         out_le32(priv->regbase + EDMA_RQIPR, 0x0);
389         out_le32(priv->regbase + EDMA_RQOPR, 0x0);
390         out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
391         out_le32(priv->regbase + EDMA_RSIPR, 0x0);
392         out_le32(priv->regbase + EDMA_RSOPR, 0x0);
393         out_le32(priv->regbase + EDMA_IORTO, 0xfa);
394 }
395
396 static void mv_reset_one_hc(void)
397 {
398         out_le32(SATAHC_BASE + SATAHC_ICT, 0x00);
399         out_le32(SATAHC_BASE + SATAHC_ITT, 0x00);
400         out_le32(SATAHC_BASE + SATAHC_ICR, 0x00);
401 }
402
403 static int probe_port(struct udevice *dev, int port)
404 {
405         struct mv_priv *priv = dev_get_platdata(dev);
406         int tries, tries2, set15 = 0;
407         u32 tmp;
408
409         debug("Probe port: %d\n", port);
410
411         for (tries = 0; tries < 2; tries++) {
412                 /* Clear SError */
413                 out_le32(priv->regbase + SIR_SERROR, 0x0);
414
415                 /* trigger com-init */
416                 tmp = in_le32(priv->regbase + SIR_SCONTROL);
417                 tmp = (tmp & 0x0f0) | 0x300 | SIR_SCONTROL_DETEN;
418                 out_le32(priv->regbase + SIR_SCONTROL, tmp);
419
420                 mdelay(1);
421
422                 tmp = in_le32(priv->regbase + SIR_SCONTROL);
423                 tries2 = 5;
424                 do {
425                         tmp = (tmp & 0x0f0) | 0x300;
426                         out_le32(priv->regbase + SIR_SCONTROL, tmp);
427                         mdelay(10);
428                         tmp = in_le32(priv->regbase + SIR_SCONTROL);
429                 } while ((tmp & 0xf0f) != 0x300 && tries2--);
430
431                 mdelay(10);
432
433                 for (tries2 = 0; tries2 < 200; tries2++) {
434                         tmp = in_le32(priv->regbase + SIR_SSTATUS);
435                         if ((tmp & SSTATUS_DET_MASK) == 0x03) {
436                                 debug("Found device on port\n");
437                                 return 0;
438                         }
439                         mdelay(1);
440                 }
441
442                 if ((tmp & SSTATUS_DET_MASK) == 0) {
443                         debug("No device attached on port %d\n", port);
444                         return -ENODEV;
445                 }
446
447                 if (!set15) {
448                         /* Try on 1.5Gb/S */
449                         debug("Try 1.5Gb link\n");
450                         set15 = 1;
451                         out_le32(priv->regbase + SIR_SCONTROL, 0x304);
452
453                         tmp = in_le32(priv->regbase + SIR_ICFG);
454                         tmp &= ~SIR_CFG_GEN2EN;
455                         out_le32(priv->regbase + SIR_ICFG, tmp);
456
457                         mv_reset_channel(dev, port);
458                 }
459         }
460
461         debug("Failed to probe port\n");
462         return -1;
463 }
464
465 /* Get request queue in pointer */
466 static int get_reqip(struct udevice *dev, int port)
467 {
468         struct mv_priv *priv = dev_get_platdata(dev);
469         u32 tmp;
470
471         tmp = in_le32(priv->regbase + EDMA_RQIPR) & EDMA_RQIPR_IPMASK;
472         tmp = tmp >> EDMA_RQIPR_IPSHIFT;
473
474         return tmp;
475 }
476
477 static void set_reqip(struct udevice *dev, int port, int reqin)
478 {
479         struct mv_priv *priv = dev_get_platdata(dev);
480         u32 tmp;
481
482         tmp = in_le32(priv->regbase + EDMA_RQIPR) & ~EDMA_RQIPR_IPMASK;
483         tmp |= ((reqin << EDMA_RQIPR_IPSHIFT) & EDMA_RQIPR_IPMASK);
484         out_le32(priv->regbase + EDMA_RQIPR, tmp);
485 }
486
487 /* Get next available slot, ignoring possible overwrite */
488 static int get_next_reqip(struct udevice *dev, int port)
489 {
490         int slot = get_reqip(dev, port);
491         slot = (slot + 1) % REQUEST_QUEUE_SIZE;
492         return slot;
493 }
494
495 /* Get response queue in pointer */
496 static int get_rspip(struct udevice *dev, int port)
497 {
498         struct mv_priv *priv = dev_get_platdata(dev);
499         u32 tmp;
500
501         tmp = in_le32(priv->regbase + EDMA_RSIPR) & EDMA_RSIPR_IPMASK;
502         tmp = tmp >> EDMA_RSIPR_IPSHIFT;
503
504         return tmp;
505 }
506
507 /* Get response queue out pointer */
508 static int get_rspop(struct udevice *dev, int port)
509 {
510         struct mv_priv *priv = dev_get_platdata(dev);
511         u32 tmp;
512
513         tmp = in_le32(priv->regbase + EDMA_RSOPR) & EDMA_RSOPR_OPMASK;
514         tmp = tmp >> EDMA_RSOPR_OPSHIFT;
515         return tmp;
516 }
517
518 /* Get next response queue pointer  */
519 static int get_next_rspop(struct udevice *dev, int port)
520 {
521         return (get_rspop(dev, port) + 1) % RESPONSE_QUEUE_SIZE;
522 }
523
524 /* Set response queue pointer */
525 static void set_rspop(struct udevice *dev, int port, int reqin)
526 {
527         struct mv_priv *priv = dev_get_platdata(dev);
528         u32 tmp;
529
530         tmp = in_le32(priv->regbase + EDMA_RSOPR) & ~EDMA_RSOPR_OPMASK;
531         tmp |= ((reqin << EDMA_RSOPR_OPSHIFT) & EDMA_RSOPR_OPMASK);
532
533         out_le32(priv->regbase + EDMA_RSOPR, tmp);
534 }
535
536 static int wait_dma_completion(struct udevice *dev, int port, int index,
537                                u32 timeout_msec)
538 {
539         u32 tmp, res;
540
541         tmp = port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1;
542         res = ata_wait_register((u32 *)(SATAHC_BASE + SATAHC_ICR), tmp,
543                                 tmp, timeout_msec);
544         if (res)
545                 printf("Failed to wait for completion on port %d\n", port);
546
547         return res;
548 }
549
550 static void process_responses(struct udevice *dev, int port)
551 {
552 #ifdef DEBUG
553         struct mv_priv *priv = dev_get_platdata(dev);
554 #endif
555         u32 tmp;
556         u32 outind = get_rspop(dev, port);
557
558         /* Ack interrupts */
559         tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
560         if (port == 0)
561                 tmp &= ~(BIT(0) | BIT(8));
562         else
563                 tmp &= ~(BIT(1) | BIT(9));
564         tmp &= ~(BIT(4));
565         out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
566
567         while (get_rspip(dev, port) != outind) {
568 #ifdef DEBUG
569                 debug("Response index %d flags %08x on port %d\n", outind,
570                       priv->response[outind].flags, port);
571 #endif
572                 outind = get_next_rspop(dev, port);
573                 set_rspop(dev, port, outind);
574         }
575 }
576
577 static int mv_ata_exec_ata_cmd(struct udevice *dev, int port,
578                                struct sata_fis_h2d *cfis,
579                                u8 *buffer, u32 len, u32 iswrite)
580 {
581         struct mv_priv *priv = dev_get_platdata(dev);
582         struct crqb *req;
583         int slot;
584         u32 start;
585
586         if (len >= 64 * 1024) {
587                 printf("We only support <64K transfers for now\n");
588                 return -1;
589         }
590
591         /* Initialize request */
592         slot = get_reqip(dev, port);
593         memset(&priv->request[slot], 0, sizeof(struct crqb));
594         req = &priv->request[slot];
595
596         req->dtb_low = (u32)buffer;
597
598         /* Dont use PRDs */
599         req->control_flags = CRQB_CNTRLFLAGS_PRDMODE;
600         req->control_flags |= iswrite ? 0 : CRQB_CNTRLFLAGS_DIR;
601         req->control_flags |=
602             ((cfis->pm_port_c << CRQB_CNTRLFLAGS_PMPORTSHIFT)
603              & CRQB_CNTRLFLAGS_PMPORTMASK);
604
605         req->drb_count = len;
606
607         req->ata_cmd_feat = (cfis->command << CRQB_CMDFEAT_CMDSHIFT) &
608                 CRQB_CMDFEAT_CMDMASK;
609         req->ata_cmd_feat |= (cfis->features << CRQB_CMDFEAT_FEATSHIFT) &
610                 CRQB_CMDFEAT_FEATMASK;
611
612         req->ata_addr = (cfis->lba_low << CRQB_ADDR_LBA_LOWSHIFT) &
613                 CRQB_ADDR_LBA_LOWMASK;
614         req->ata_addr |= (cfis->lba_mid << CRQB_ADDR_LBA_MIDSHIFT) &
615                 CRQB_ADDR_LBA_MIDMASK;
616         req->ata_addr |= (cfis->lba_high << CRQB_ADDR_LBA_HIGHSHIFT) &
617                 CRQB_ADDR_LBA_HIGHMASK;
618         req->ata_addr |= (cfis->device << CRQB_ADDR_DEVICE_SHIFT) &
619                 CRQB_ADDR_DEVICE_MASK;
620
621         req->ata_addr_exp = (cfis->lba_low_exp << CRQB_ADDR_LBA_LOW_EXP_SHIFT) &
622                 CRQB_ADDR_LBA_LOW_EXP_MASK;
623         req->ata_addr_exp |=
624                 (cfis->lba_mid_exp << CRQB_ADDR_LBA_MID_EXP_SHIFT) &
625                 CRQB_ADDR_LBA_MID_EXP_MASK;
626         req->ata_addr_exp |=
627                 (cfis->lba_high_exp << CRQB_ADDR_LBA_HIGH_EXP_SHIFT) &
628                 CRQB_ADDR_LBA_HIGH_EXP_MASK;
629         req->ata_addr_exp |=
630                 (cfis->features_exp << CRQB_ADDR_FEATURE_EXP_SHIFT) &
631                 CRQB_ADDR_FEATURE_EXP_MASK;
632
633         req->ata_sect_count =
634                 (cfis->sector_count << CRQB_SECTCOUNT_COUNT_SHIFT) &
635                 CRQB_SECTCOUNT_COUNT_MASK;
636         req->ata_sect_count |=
637                 (cfis->sector_count_exp << CRQB_SECTCOUNT_COUNT_EXP_SHIFT) &
638                 CRQB_SECTCOUNT_COUNT_EXP_MASK;
639
640         /* Flush data */
641         start = (u32)req & ~(ARCH_DMA_MINALIGN - 1);
642         flush_dcache_range(start,
643                            start + ALIGN(sizeof(*req), ARCH_DMA_MINALIGN));
644
645         /* Trigger operation */
646         slot = get_next_reqip(dev, port);
647         set_reqip(dev, port, slot);
648
649         /* Wait for completion */
650         if (wait_dma_completion(dev, port, slot, 10000)) {
651                 printf("ATA operation timed out\n");
652                 return -1;
653         }
654
655         process_responses(dev, port);
656
657         /* Invalidate data on read */
658         if (buffer && len) {
659                 start = (u32)buffer & ~(ARCH_DMA_MINALIGN - 1);
660                 invalidate_dcache_range(start,
661                                         start + ALIGN(len, ARCH_DMA_MINALIGN));
662         }
663
664         return len;
665 }
666
667 static u32 mv_sata_rw_cmd_ext(struct udevice *dev, int port, lbaint_t start,
668                               u32 blkcnt,
669                               u8 *buffer, int is_write)
670 {
671         struct sata_fis_h2d cfis;
672         u32 res;
673         u64 block;
674
675         block = (u64)start;
676
677         memset(&cfis, 0, sizeof(struct sata_fis_h2d));
678
679         cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
680         cfis.command = (is_write) ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
681
682         cfis.lba_high_exp = (block >> 40) & 0xff;
683         cfis.lba_mid_exp = (block >> 32) & 0xff;
684         cfis.lba_low_exp = (block >> 24) & 0xff;
685         cfis.lba_high = (block >> 16) & 0xff;
686         cfis.lba_mid = (block >> 8) & 0xff;
687         cfis.lba_low = block & 0xff;
688         cfis.device = ATA_LBA;
689         cfis.sector_count_exp = (blkcnt >> 8) & 0xff;
690         cfis.sector_count = blkcnt & 0xff;
691
692         res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
693                                   ATA_SECT_SIZE * blkcnt, is_write);
694
695         return res >= 0 ? blkcnt : res;
696 }
697
698 static u32 mv_sata_rw_cmd(struct udevice *dev, int port, lbaint_t start,
699                           u32 blkcnt, u8 *buffer, int is_write)
700 {
701         struct sata_fis_h2d cfis;
702         lbaint_t block;
703         u32 res;
704
705         block = start;
706
707         memset(&cfis, 0, sizeof(struct sata_fis_h2d));
708
709         cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
710         cfis.command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
711         cfis.device = ATA_LBA;
712
713         cfis.device |= (block >> 24) & 0xf;
714         cfis.lba_high = (block >> 16) & 0xff;
715         cfis.lba_mid = (block >> 8) & 0xff;
716         cfis.lba_low = block & 0xff;
717         cfis.sector_count = (u8)(blkcnt & 0xff);
718
719         res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
720                                   ATA_SECT_SIZE * blkcnt, is_write);
721
722         return res >= 0 ? blkcnt : res;
723 }
724
725 static u32 ata_low_level_rw(struct udevice *dev, int port, lbaint_t blknr,
726                             lbaint_t blkcnt, void *buffer, int is_write)
727 {
728         struct blk_desc *desc = dev_get_uclass_platdata(dev);
729         lbaint_t start, blks;
730         u8 *addr;
731         int max_blks;
732
733         debug("%s: " LBAFU " " LBAFU "\n", __func__, blknr, blkcnt);
734
735         start = blknr;
736         blks = blkcnt;
737         addr = (u8 *)buffer;
738
739         max_blks = MV_ATA_MAX_SECTORS;
740         do {
741                 if (blks > max_blks) {
742                         if (desc->lba48) {
743                                 mv_sata_rw_cmd_ext(dev, port, start, max_blks,
744                                                    addr, is_write);
745                         } else {
746                                 mv_sata_rw_cmd(dev, port, start, max_blks,
747                                                addr, is_write);
748                         }
749                         start += max_blks;
750                         blks -= max_blks;
751                         addr += ATA_SECT_SIZE * max_blks;
752                 } else {
753                         if (desc->lba48) {
754                                 mv_sata_rw_cmd_ext(dev, port, start, blks, addr,
755                                                    is_write);
756                         } else {
757                                 mv_sata_rw_cmd(dev, port, start, blks, addr,
758                                                is_write);
759                         }
760                         start += blks;
761                         blks = 0;
762                         addr += ATA_SECT_SIZE * blks;
763                 }
764         } while (blks != 0);
765
766         return blkcnt;
767 }
768
769 static int mv_ata_exec_ata_cmd_nondma(struct udevice *dev, int port,
770                                       struct sata_fis_h2d *cfis, u8 *buffer,
771                                       u32 len, u32 iswrite)
772 {
773         struct mv_priv *priv = dev_get_platdata(dev);
774         int i;
775         u16 *tp;
776
777         debug("%s\n", __func__);
778
779         out_le32(priv->regbase + PIO_SECTOR_COUNT, cfis->sector_count);
780         out_le32(priv->regbase + PIO_LBA_HI, cfis->lba_high);
781         out_le32(priv->regbase + PIO_LBA_MID, cfis->lba_mid);
782         out_le32(priv->regbase + PIO_LBA_LOW, cfis->lba_low);
783         out_le32(priv->regbase + PIO_ERR_FEATURES, cfis->features);
784         out_le32(priv->regbase + PIO_DEVICE, cfis->device);
785         out_le32(priv->regbase + PIO_CMD_STATUS, cfis->command);
786
787         if (ata_wait_register((u32 *)(priv->regbase + PIO_CMD_STATUS),
788                               ATA_BUSY, 0x0, 10000)) {
789                 debug("Failed to wait for completion\n");
790                 return -1;
791         }
792
793         if (len > 0) {
794                 tp = (u16 *)buffer;
795                 for (i = 0; i < len / 2; i++) {
796                         if (iswrite)
797                                 out_le16(priv->regbase + PIO_DATA, *tp++);
798                         else
799                                 *tp++ = in_le16(priv->regbase + PIO_DATA);
800                 }
801         }
802
803         return len;
804 }
805
806 static int mv_sata_identify(struct udevice *dev, int port, u16 *id)
807 {
808         struct sata_fis_h2d h2d;
809
810         memset(&h2d, 0, sizeof(struct sata_fis_h2d));
811
812         h2d.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
813         h2d.command = ATA_CMD_ID_ATA;
814
815         /* Give device time to get operational */
816         mdelay(10);
817
818         return mv_ata_exec_ata_cmd_nondma(dev, port, &h2d, (u8 *)id,
819                                           ATA_ID_WORDS * 2, READ_CMD);
820 }
821
822 static void mv_sata_xfer_mode(struct udevice *dev, int port, u16 *id)
823 {
824         struct mv_priv *priv = dev_get_platdata(dev);
825
826         priv->pio = id[ATA_ID_PIO_MODES];
827         priv->mwdma = id[ATA_ID_MWDMA_MODES];
828         priv->udma = id[ATA_ID_UDMA_MODES];
829         debug("pio %04x, mwdma %04x, udma %04x\n", priv->pio, priv->mwdma,
830               priv->udma);
831 }
832
833 static void mv_sata_set_features(struct udevice *dev, int port)
834 {
835         struct mv_priv *priv = dev_get_platdata(dev);
836         struct sata_fis_h2d cfis;
837         u8 udma_cap;
838
839         memset(&cfis, 0, sizeof(struct sata_fis_h2d));
840
841         cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
842         cfis.command = ATA_CMD_SET_FEATURES;
843         cfis.features = SETFEATURES_XFER;
844
845         /* First check the device capablity */
846         udma_cap = (u8) (priv->udma & 0xff);
847
848         if (udma_cap == ATA_UDMA6)
849                 cfis.sector_count = XFER_UDMA_6;
850         if (udma_cap == ATA_UDMA5)
851                 cfis.sector_count = XFER_UDMA_5;
852         if (udma_cap == ATA_UDMA4)
853                 cfis.sector_count = XFER_UDMA_4;
854         if (udma_cap == ATA_UDMA3)
855                 cfis.sector_count = XFER_UDMA_3;
856
857         mv_ata_exec_ata_cmd_nondma(dev, port, &cfis, NULL, 0, READ_CMD);
858 }
859
860 /*
861  * Initialize SATA memory windows
862  */
863 static void mvsata_ide_conf_mbus_windows(void)
864 {
865         const struct mbus_dram_target_info *dram;
866         int i;
867
868         dram = mvebu_mbus_dram_info();
869
870         /* Disable windows, Set Size/Base to 0  */
871         for (i = 0; i < 4; i++) {
872                 writel(0, MVSATA_WIN_CONTROL(i));
873                 writel(0, MVSATA_WIN_BASE(i));
874         }
875
876         for (i = 0; i < dram->num_cs; i++) {
877                 const struct mbus_dram_window *cs = dram->cs + i;
878                 writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
879                        (dram->mbus_dram_target_id << 4) | 1,
880                        MVSATA_WIN_CONTROL(i));
881                 writel(cs->base & 0xffff0000, MVSATA_WIN_BASE(i));
882         }
883 }
884
885 static int sata_mv_init_sata(struct udevice *dev, int port)
886 {
887         struct mv_priv *priv = dev_get_platdata(dev);
888
889         debug("Initialize sata dev: %d\n", port);
890
891         if (port < 0 || port >= CONFIG_SYS_SATA_MAX_DEVICE) {
892                 printf("Invalid sata device %d\n", port);
893                 return -1;
894         }
895
896         /* Allocate and align request buffer */
897         priv->crqb_alloc = malloc(sizeof(struct crqb) * REQUEST_QUEUE_SIZE +
898                                   CRQB_ALIGN);
899         if (!priv->crqb_alloc) {
900                 printf("Unable to allocate memory for request queue\n");
901                 return -ENOMEM;
902         }
903         memset(priv->crqb_alloc, 0,
904                sizeof(struct crqb) * REQUEST_QUEUE_SIZE + CRQB_ALIGN);
905         priv->request = (struct crqb *)(((u32) priv->crqb_alloc + CRQB_ALIGN) &
906                                         ~(CRQB_ALIGN - 1));
907
908         /* Allocate and align response buffer */
909         priv->crpb_alloc = malloc(sizeof(struct crpb) * REQUEST_QUEUE_SIZE +
910                                   CRPB_ALIGN);
911         if (!priv->crpb_alloc) {
912                 printf("Unable to allocate memory for response queue\n");
913                 return -ENOMEM;
914         }
915         memset(priv->crpb_alloc, 0,
916                sizeof(struct crpb) * REQUEST_QUEUE_SIZE + CRPB_ALIGN);
917         priv->response = (struct crpb *)(((u32) priv->crpb_alloc + CRPB_ALIGN) &
918                                          ~(CRPB_ALIGN - 1));
919
920         sprintf(priv->name, "SATA%d", port);
921
922         priv->regbase = port == 0 ? SATA0_BASE : SATA1_BASE;
923
924         if (!hw_init) {
925                 debug("Initialize sata hw\n");
926                 hw_init = 1;
927                 mv_reset_one_hc();
928                 mvsata_ide_conf_mbus_windows();
929         }
930
931         mv_reset_port(dev, port);
932
933         if (probe_port(dev, port)) {
934                 priv->link = 0;
935                 return -ENODEV;
936         }
937         priv->link = 1;
938
939         return 0;
940 }
941
942 static int sata_mv_scan_sata(struct udevice *dev, int port)
943 {
944         struct blk_desc *desc = dev_get_uclass_platdata(dev);
945         struct mv_priv *priv = dev_get_platdata(dev);
946         unsigned char serial[ATA_ID_SERNO_LEN + 1];
947         unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
948         unsigned char product[ATA_ID_PROD_LEN + 1];
949         u64 n_sectors;
950         u16 *id;
951
952         if (!priv->link)
953                 return -ENODEV;
954
955         id = (u16 *)malloc(ATA_ID_WORDS * 2);
956         if (!id) {
957                 printf("Failed to malloc id data\n");
958                 return -ENOMEM;
959         }
960
961         mv_sata_identify(dev, port, id);
962         ata_swap_buf_le16(id, ATA_ID_WORDS);
963 #ifdef DEBUG
964         ata_dump_id(id);
965 #endif
966
967         /* Serial number */
968         ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
969         memcpy(desc->product, serial, sizeof(serial));
970
971         /* Firmware version */
972         ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
973         memcpy(desc->revision, firmware, sizeof(firmware));
974
975         /* Product model */
976         ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
977         memcpy(desc->vendor, product, sizeof(product));
978
979         /* Total sectors */
980         n_sectors = ata_id_n_sectors(id);
981         desc->lba = n_sectors;
982
983         /* Check if support LBA48 */
984         if (ata_id_has_lba48(id)) {
985                 desc->lba48 = 1;
986                 debug("Device support LBA48\n");
987         }
988
989         /* Get the NCQ queue depth from device */
990         priv->queue_depth = ata_id_queue_depth(id);
991
992         /* Get the xfer mode from device */
993         mv_sata_xfer_mode(dev, port, id);
994
995         /* Set the xfer mode to highest speed */
996         mv_sata_set_features(dev, port);
997
998         /* Start up */
999         mv_start_edma_engine(dev, port);
1000
1001         return 0;
1002 }
1003
1004 static ulong sata_mv_read(struct udevice *blk, lbaint_t blknr,
1005                           lbaint_t blkcnt, void *buffer)
1006 {
1007         struct mv_priv *priv = dev_get_platdata(blk);
1008
1009         return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1010                                 buffer, READ_CMD);
1011 }
1012
1013 static ulong sata_mv_write(struct udevice *blk, lbaint_t blknr,
1014                            lbaint_t blkcnt, const void *buffer)
1015 {
1016         struct mv_priv *priv = dev_get_platdata(blk);
1017
1018         return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1019                                 (void *)buffer, WRITE_CMD);
1020 }
1021
1022 static const struct blk_ops sata_mv_blk_ops = {
1023         .read   = sata_mv_read,
1024         .write  = sata_mv_write,
1025 };
1026
1027 U_BOOT_DRIVER(sata_mv_driver) = {
1028         .name = "sata_mv_blk",
1029         .id = UCLASS_BLK,
1030         .ops = &sata_mv_blk_ops,
1031         .platdata_auto_alloc_size = sizeof(struct mv_priv),
1032 };
1033
1034 static int sata_mv_probe(struct udevice *dev)
1035 {
1036         const void *blob = gd->fdt_blob;
1037         int node = dev_of_offset(dev);
1038         struct mv_priv *priv;
1039         struct udevice *blk;
1040         int nr_ports;
1041         int ret;
1042         int i;
1043
1044         /* Get number of ports of this SATA controller */
1045         nr_ports = min(fdtdec_get_int(blob, node, "nr-ports", -1),
1046                        CONFIG_SYS_SATA_MAX_DEVICE);
1047
1048         for (i = 0; i < nr_ports; i++) {
1049                 ret = blk_create_devicef(dev, "sata_mv_blk", "blk",
1050                                          IF_TYPE_SATA, -1, 512, 0, &blk);
1051                 if (ret) {
1052                         debug("Can't create device\n");
1053                         return ret;
1054                 }
1055
1056                 priv = dev_get_platdata(blk);
1057                 priv->dev_nr = i;
1058
1059                 /* Init SATA port */
1060                 ret = sata_mv_init_sata(blk, i);
1061                 if (ret) {
1062                         debug("%s: Failed to init bus\n", __func__);
1063                         return ret;
1064                 }
1065
1066                 /* Scan SATA port */
1067                 ret = sata_mv_scan_sata(blk, i);
1068                 if (ret) {
1069                         debug("%s: Failed to scan bus\n", __func__);
1070                         return ret;
1071                 }
1072         }
1073
1074         return 0;
1075 }
1076
1077 static int sata_mv_scan(struct udevice *dev)
1078 {
1079         /* Nothing to do here */
1080
1081         return 0;
1082 }
1083
1084 static const struct udevice_id sata_mv_ids[] = {
1085         { .compatible = "marvell,armada-370-sata" },
1086         { .compatible = "marvell,orion-sata" },
1087         { }
1088 };
1089
1090 struct ahci_ops sata_mv_ahci_ops = {
1091         .scan = sata_mv_scan,
1092 };
1093
1094 U_BOOT_DRIVER(sata_mv_ahci) = {
1095         .name = "sata_mv_ahci",
1096         .id = UCLASS_AHCI,
1097         .of_match = sata_mv_ids,
1098         .ops = &sata_mv_ahci_ops,
1099         .probe = sata_mv_probe,
1100 };