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