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