blob: e3b8750e8e9dfb8fb67357c6d52dcded5499bf08 [file] [log] [blame]
Loc Ho81d01bf2014-03-14 17:53:20 -06001/*
2 * AppliedMicro X-Gene SoC SATA Host Controller Driver
3 *
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
5 * Author: Loc Ho <lho@apm.com>
6 * Tuan Phan <tphan@apm.com>
7 * Suman Tripathi <stripathi@apm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * NOTE: PM support is not currently available.
23 *
24 */
25#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/ahci_platform.h>
28#include <linux/of_address.h>
29#include <linux/of_irq.h>
30#include <linux/phy/phy.h>
31#include "ahci.h"
32
Akinobu Mita018d5ef2015-01-29 08:30:29 +090033#define DRV_NAME "xgene-ahci"
34
Loc Ho81d01bf2014-03-14 17:53:20 -060035/* Max # of disk per a controller */
36#define MAX_AHCI_CHN_PERCTR 2
37
38/* MUX CSR */
39#define SATA_ENET_CONFIG_REG 0x00000000
40#define CFG_SATA_ENET_SELECT_MASK 0x00000001
41
42/* SATA core host controller CSR */
43#define SLVRDERRATTRIBUTES 0x00000000
44#define SLVWRERRATTRIBUTES 0x00000004
45#define MSTRDERRATTRIBUTES 0x00000008
46#define MSTWRERRATTRIBUTES 0x0000000c
47#define BUSCTLREG 0x00000014
48#define IOFMSTRWAUX 0x00000018
49#define INTSTATUSMASK 0x0000002c
50#define ERRINTSTATUS 0x00000030
51#define ERRINTSTATUSMASK 0x00000034
52
53/* SATA host AHCI CSR */
54#define PORTCFG 0x000000a4
55#define PORTADDR_SET(dst, src) \
56 (((dst) & ~0x0000003f) | (((u32)(src)) & 0x0000003f))
57#define PORTPHY1CFG 0x000000a8
58#define PORTPHY1CFG_FRCPHYRDY_SET(dst, src) \
59 (((dst) & ~0x00100000) | (((u32)(src) << 0x14) & 0x00100000))
60#define PORTPHY2CFG 0x000000ac
61#define PORTPHY3CFG 0x000000b0
62#define PORTPHY4CFG 0x000000b4
63#define PORTPHY5CFG 0x000000b8
64#define SCTL0 0x0000012C
65#define PORTPHY5CFG_RTCHG_SET(dst, src) \
66 (((dst) & ~0xfff00000) | (((u32)(src) << 0x14) & 0xfff00000))
67#define PORTAXICFG_EN_CONTEXT_SET(dst, src) \
68 (((dst) & ~0x01000000) | (((u32)(src) << 0x18) & 0x01000000))
69#define PORTAXICFG 0x000000bc
70#define PORTAXICFG_OUTTRANS_SET(dst, src) \
71 (((dst) & ~0x00f00000) | (((u32)(src) << 0x14) & 0x00f00000))
Suman Tripathiaeae4dc2014-07-29 12:24:49 +053072#define PORTRANSCFG 0x000000c8
73#define PORTRANSCFG_RXWM_SET(dst, src) \
74 (((dst) & ~0x0000007f) | (((u32)(src)) & 0x0000007f))
Loc Ho81d01bf2014-03-14 17:53:20 -060075
76/* SATA host controller AXI CSR */
77#define INT_SLV_TMOMASK 0x00000010
78
79/* SATA diagnostic CSR */
80#define CFG_MEM_RAM_SHUTDOWN 0x00000070
81#define BLOCK_MEM_RDY 0x00000074
82
Suman Tripathi0babe612014-08-28 14:51:22 +053083/* Max retry for link down */
84#define MAX_LINK_DOWN_RETRY 3
85
Loc Ho81d01bf2014-03-14 17:53:20 -060086struct xgene_ahci_context {
87 struct ahci_host_priv *hpriv;
88 struct device *dev;
Suman Tripathi2a0bdff2014-07-07 22:33:05 +053089 u8 last_cmd[MAX_AHCI_CHN_PERCTR]; /* tracking the last command issued*/
Suman Tripathia3a84bc2015-01-06 15:32:16 +053090 u32 class[MAX_AHCI_CHN_PERCTR]; /* tracking the class of device */
Loc Ho81d01bf2014-03-14 17:53:20 -060091 void __iomem *csr_core; /* Core CSR address of IP */
92 void __iomem *csr_diag; /* Diag CSR address of IP */
93 void __iomem *csr_axi; /* AXI CSR address of IP */
94 void __iomem *csr_mux; /* MUX CSR address of IP */
95};
96
97static int xgene_ahci_init_memram(struct xgene_ahci_context *ctx)
98{
99 dev_dbg(ctx->dev, "Release memory from shutdown\n");
100 writel(0x0, ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN);
101 readl(ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN); /* Force a barrier */
102 msleep(1); /* reset may take up to 1ms */
103 if (readl(ctx->csr_diag + BLOCK_MEM_RDY) != 0xFFFFFFFF) {
104 dev_err(ctx->dev, "failed to release memory from shutdown\n");
105 return -ENODEV;
106 }
107 return 0;
108}
109
110/**
Suman Tripathi15400352015-01-06 15:32:15 +0530111 * xgene_ahci_poll_reg_val- Poll a register on a specific value.
112 * @ap : ATA port of interest.
113 * @reg : Register of interest.
114 * @val : Value to be attained.
115 * @interval : waiting interval for polling.
116 * @timeout : timeout for achieving the value.
117 */
118static int xgene_ahci_poll_reg_val(struct ata_port *ap,
119 void __iomem *reg, unsigned
120 int val, unsigned long interval,
121 unsigned long timeout)
122{
123 unsigned long deadline;
124 unsigned int tmp;
125
126 tmp = ioread32(reg);
127 deadline = ata_deadline(jiffies, timeout);
128
129 while (tmp != val && time_before(jiffies, deadline)) {
130 ata_msleep(ap, interval);
131 tmp = ioread32(reg);
132 }
133
134 return tmp;
135}
136
137/**
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530138 * xgene_ahci_restart_engine - Restart the dma engine.
139 * @ap : ATA port of interest
140 *
Suman Tripathi15400352015-01-06 15:32:15 +0530141 * Waits for completion of multiple commands and restarts
142 * the DMA engine inside the controller.
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530143 */
144static int xgene_ahci_restart_engine(struct ata_port *ap)
145{
146 struct ahci_host_priv *hpriv = ap->host->private_data;
Suman Tripathi15400352015-01-06 15:32:15 +0530147 struct ahci_port_priv *pp = ap->private_data;
148 void __iomem *port_mmio = ahci_port_base(ap);
149 u32 fbs;
150
151 /*
152 * In case of PMP multiple IDENTIFY DEVICE commands can be
153 * issued inside PxCI. So need to poll PxCI for the
154 * completion of outstanding IDENTIFY DEVICE commands before
155 * we restart the DMA engine.
156 */
157 if (xgene_ahci_poll_reg_val(ap, port_mmio +
158 PORT_CMD_ISSUE, 0x0, 1, 100))
159 return -EBUSY;
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530160
161 ahci_stop_engine(ap);
162 ahci_start_fis_rx(ap);
Suman Tripathi15400352015-01-06 15:32:15 +0530163
164 /*
165 * Enable the PxFBS.FBS_EN bit as it
166 * gets cleared due to stopping the engine.
167 */
168 if (pp->fbs_supported) {
169 fbs = readl(port_mmio + PORT_FBS);
170 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
171 fbs = readl(port_mmio + PORT_FBS);
172 }
173
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530174 hpriv->start_engine(ap);
175
176 return 0;
177}
178
179/**
180 * xgene_ahci_qc_issue - Issue commands to the device
181 * @qc: Command to issue
182 *
Suman Tripathia3a84bc2015-01-06 15:32:16 +0530183 * Due to Hardware errata for IDENTIFY DEVICE command, the controller cannot
184 * clear the BSY bit after receiving the PIO setup FIS. This results in the dma
185 * state machine goes into the CMFatalErrorUpdate state and locks up. By
186 * restarting the dma engine, it removes the controller out of lock up state.
187 *
188 * Due to H/W errata, the controller is unable to save the PMP
189 * field fetched from command header before sending the H2D FIS.
190 * When the device returns the PMP port field in the D2H FIS, there is
191 * a mismatch and results in command completion failure. The
192 * workaround is to write the pmp value to PxFBS.DEV field before issuing
193 * any command to PMP.
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530194 */
195static unsigned int xgene_ahci_qc_issue(struct ata_queued_cmd *qc)
196{
197 struct ata_port *ap = qc->ap;
198 struct ahci_host_priv *hpriv = ap->host->private_data;
199 struct xgene_ahci_context *ctx = hpriv->plat_data;
200 int rc = 0;
Suman Tripathia3a84bc2015-01-06 15:32:16 +0530201 u32 port_fbs;
202 void *port_mmio = ahci_port_base(ap);
203
204 /*
205 * Write the pmp value to PxFBS.DEV
206 * for case of Port Mulitplier.
207 */
208 if (ctx->class[ap->port_no] == ATA_DEV_PMP) {
209 port_fbs = readl(port_mmio + PORT_FBS);
210 port_fbs &= ~PORT_FBS_DEV_MASK;
211 port_fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
212 writel(port_fbs, port_mmio + PORT_FBS);
213 }
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530214
Suman Tripathi11024072014-12-29 08:52:47 +0530215 if (unlikely((ctx->last_cmd[ap->port_no] == ATA_CMD_ID_ATA) ||
216 (ctx->last_cmd[ap->port_no] == ATA_CMD_PACKET)))
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530217 xgene_ahci_restart_engine(ap);
218
219 rc = ahci_qc_issue(qc);
220
221 /* Save the last command issued */
222 ctx->last_cmd[ap->port_no] = qc->tf.command;
223
224 return rc;
225}
226
Suman Tripathi0bed13b2014-08-28 14:51:21 +0530227static bool xgene_ahci_is_memram_inited(struct xgene_ahci_context *ctx)
228{
229 void __iomem *diagcsr = ctx->csr_diag;
230
231 return (readl(diagcsr + CFG_MEM_RAM_SHUTDOWN) == 0 &&
232 readl(diagcsr + BLOCK_MEM_RDY) == 0xFFFFFFFF);
233}
234
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530235/**
Loc Ho81d01bf2014-03-14 17:53:20 -0600236 * xgene_ahci_read_id - Read ID data from the specified device
237 * @dev: device
238 * @tf: proposed taskfile
239 * @id: data buffer
240 *
241 * This custom read ID function is required due to the fact that the HW
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530242 * does not support DEVSLP.
Loc Ho81d01bf2014-03-14 17:53:20 -0600243 */
244static unsigned int xgene_ahci_read_id(struct ata_device *dev,
245 struct ata_taskfile *tf, u16 *id)
246{
247 u32 err_mask;
Loc Ho81d01bf2014-03-14 17:53:20 -0600248
249 err_mask = ata_do_dev_read_id(dev, tf, id);
250 if (err_mask)
251 return err_mask;
252
253 /*
254 * Mask reserved area. Word78 spec of Link Power Management
255 * bit15-8: reserved
256 * bit7: NCQ autosence
257 * bit6: Software settings preservation supported
258 * bit5: reserved
259 * bit4: In-order sata delivery supported
260 * bit3: DIPM requests supported
261 * bit2: DMA Setup FIS Auto-Activate optimization supported
262 * bit1: DMA Setup FIX non-Zero buffer offsets supported
263 * bit0: Reserved
264 *
265 * Clear reserved bit 8 (DEVSLP bit) as we don't support DEVSLP
266 */
Suman Tripathi5c0b8e02014-12-29 08:52:46 +0530267 id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
Loc Ho81d01bf2014-03-14 17:53:20 -0600268
Loc Ho81d01bf2014-03-14 17:53:20 -0600269 return 0;
270}
271
272static void xgene_ahci_set_phy_cfg(struct xgene_ahci_context *ctx, int channel)
273{
274 void __iomem *mmio = ctx->hpriv->mmio;
275 u32 val;
276
277 dev_dbg(ctx->dev, "port configure mmio 0x%p channel %d\n",
278 mmio, channel);
279 val = readl(mmio + PORTCFG);
280 val = PORTADDR_SET(val, channel == 0 ? 2 : 3);
281 writel(val, mmio + PORTCFG);
282 readl(mmio + PORTCFG); /* Force a barrier */
283 /* Disable fix rate */
284 writel(0x0001fffe, mmio + PORTPHY1CFG);
285 readl(mmio + PORTPHY1CFG); /* Force a barrier */
Suman Tripathi0185b1b2014-07-29 12:24:51 +0530286 writel(0x28183219, mmio + PORTPHY2CFG);
Loc Ho81d01bf2014-03-14 17:53:20 -0600287 readl(mmio + PORTPHY2CFG); /* Force a barrier */
Suman Tripathi0185b1b2014-07-29 12:24:51 +0530288 writel(0x13081008, mmio + PORTPHY3CFG);
Loc Ho81d01bf2014-03-14 17:53:20 -0600289 readl(mmio + PORTPHY3CFG); /* Force a barrier */
Suman Tripathi0185b1b2014-07-29 12:24:51 +0530290 writel(0x00480815, mmio + PORTPHY4CFG);
Loc Ho81d01bf2014-03-14 17:53:20 -0600291 readl(mmio + PORTPHY4CFG); /* Force a barrier */
292 /* Set window negotiation */
293 val = readl(mmio + PORTPHY5CFG);
294 val = PORTPHY5CFG_RTCHG_SET(val, 0x300);
295 writel(val, mmio + PORTPHY5CFG);
296 readl(mmio + PORTPHY5CFG); /* Force a barrier */
297 val = readl(mmio + PORTAXICFG);
298 val = PORTAXICFG_EN_CONTEXT_SET(val, 0x1); /* Enable context mgmt */
299 val = PORTAXICFG_OUTTRANS_SET(val, 0xe); /* Set outstanding */
300 writel(val, mmio + PORTAXICFG);
301 readl(mmio + PORTAXICFG); /* Force a barrier */
Suman Tripathiaeae4dc2014-07-29 12:24:49 +0530302 /* Set the watermark threshold of the receive FIFO */
303 val = readl(mmio + PORTRANSCFG);
304 val = PORTRANSCFG_RXWM_SET(val, 0x30);
305 writel(val, mmio + PORTRANSCFG);
Loc Ho81d01bf2014-03-14 17:53:20 -0600306}
307
308/**
309 * xgene_ahci_do_hardreset - Issue the actual COMRESET
310 * @link: link to reset
311 * @deadline: deadline jiffies for the operation
312 * @online: Return value to indicate if device online
313 *
314 * Due to the limitation of the hardware PHY, a difference set of setting is
315 * required for each supported disk speed - Gen3 (6.0Gbps), Gen2 (3.0Gbps),
316 * and Gen1 (1.5Gbps). Otherwise during long IO stress test, the PHY will
317 * report disparity error and etc. In addition, during COMRESET, there can
318 * be error reported in the register PORT_SCR_ERR. For SERR_DISPARITY and
Suman Tripathi0babe612014-08-28 14:51:22 +0530319 * SERR_10B_8B_ERR, the PHY receiver line must be reseted. Also during long
320 * reboot cycle regression, sometimes the PHY reports link down even if the
321 * device is present because of speed negotiation failure. so need to retry
322 * the COMRESET to get the link up. The following algorithm is followed to
323 * proper configure the hardware PHY during COMRESET:
Loc Ho81d01bf2014-03-14 17:53:20 -0600324 *
325 * Alg Part 1:
326 * 1. Start the PHY at Gen3 speed (default setting)
327 * 2. Issue the COMRESET
328 * 3. If no link, go to Alg Part 3
329 * 4. If link up, determine if the negotiated speed matches the PHY
330 * configured speed
331 * 5. If they matched, go to Alg Part 2
332 * 6. If they do not matched and first time, configure the PHY for the linked
333 * up disk speed and repeat step 2
334 * 7. Go to Alg Part 2
335 *
336 * Alg Part 2:
337 * 1. On link up, if there are any SERR_DISPARITY and SERR_10B_8B_ERR error
338 * reported in the register PORT_SCR_ERR, then reset the PHY receiver line
Suman Tripathi0babe612014-08-28 14:51:22 +0530339 * 2. Go to Alg Part 4
Loc Ho81d01bf2014-03-14 17:53:20 -0600340 *
341 * Alg Part 3:
Suman Tripathi0babe612014-08-28 14:51:22 +0530342 * 1. Check the PORT_SCR_STAT to see whether device presence detected but PHY
343 * communication establishment failed and maximum link down attempts are
344 * less than Max attempts 3 then goto Alg Part 1.
345 * 2. Go to Alg Part 4.
346 *
347 * Alg Part 4:
Loc Ho81d01bf2014-03-14 17:53:20 -0600348 * 1. Clear any pending from register PORT_SCR_ERR.
349 *
350 * NOTE: For the initial version, we will NOT support Gen1/Gen2. In addition
351 * and until the underlying PHY supports an method to reset the receiver
352 * line, on detection of SERR_DISPARITY or SERR_10B_8B_ERR errors,
353 * an warning message will be printed.
354 */
355static int xgene_ahci_do_hardreset(struct ata_link *link,
356 unsigned long deadline, bool *online)
357{
358 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
359 struct ata_port *ap = link->ap;
360 struct ahci_host_priv *hpriv = ap->host->private_data;
361 struct xgene_ahci_context *ctx = hpriv->plat_data;
362 struct ahci_port_priv *pp = ap->private_data;
363 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
364 void __iomem *port_mmio = ahci_port_base(ap);
365 struct ata_taskfile tf;
Suman Tripathi0babe612014-08-28 14:51:22 +0530366 int link_down_retry = 0;
Loc Ho81d01bf2014-03-14 17:53:20 -0600367 int rc;
Suman Tripathi0babe612014-08-28 14:51:22 +0530368 u32 val, sstatus;
Loc Ho81d01bf2014-03-14 17:53:20 -0600369
Suman Tripathi0babe612014-08-28 14:51:22 +0530370 do {
371 /* clear D2H reception area to properly wait for D2H FIS */
372 ata_tf_init(link->device, &tf);
373 tf.command = ATA_BUSY;
374 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
375 rc = sata_link_hardreset(link, timing, deadline, online,
Loc Ho81d01bf2014-03-14 17:53:20 -0600376 ahci_check_ready);
Suman Tripathi0babe612014-08-28 14:51:22 +0530377 if (*online) {
378 val = readl(port_mmio + PORT_SCR_ERR);
379 if (val & (SERR_DISPARITY | SERR_10B_8B_ERR))
380 dev_warn(ctx->dev, "link has error\n");
381 break;
382 }
Loc Ho81d01bf2014-03-14 17:53:20 -0600383
Suman Tripathi0babe612014-08-28 14:51:22 +0530384 sata_scr_read(link, SCR_STATUS, &sstatus);
385 } while (link_down_retry++ < MAX_LINK_DOWN_RETRY &&
386 (sstatus & 0xff) == 0x1);
Loc Ho81d01bf2014-03-14 17:53:20 -0600387
388 /* clear all errors if any pending */
389 val = readl(port_mmio + PORT_SCR_ERR);
390 writel(val, port_mmio + PORT_SCR_ERR);
391
392 return rc;
393}
394
395static int xgene_ahci_hardreset(struct ata_link *link, unsigned int *class,
396 unsigned long deadline)
397{
398 struct ata_port *ap = link->ap;
399 struct ahci_host_priv *hpriv = ap->host->private_data;
400 void __iomem *port_mmio = ahci_port_base(ap);
401 bool online;
402 int rc;
403 u32 portcmd_saved;
404 u32 portclb_saved;
405 u32 portclbhi_saved;
406 u32 portrxfis_saved;
407 u32 portrxfishi_saved;
408
409 /* As hardreset resets these CSR, save it to restore later */
410 portcmd_saved = readl(port_mmio + PORT_CMD);
411 portclb_saved = readl(port_mmio + PORT_LST_ADDR);
412 portclbhi_saved = readl(port_mmio + PORT_LST_ADDR_HI);
413 portrxfis_saved = readl(port_mmio + PORT_FIS_ADDR);
414 portrxfishi_saved = readl(port_mmio + PORT_FIS_ADDR_HI);
415
416 ahci_stop_engine(ap);
417
418 rc = xgene_ahci_do_hardreset(link, deadline, &online);
419
420 /* As controller hardreset clears them, restore them */
421 writel(portcmd_saved, port_mmio + PORT_CMD);
422 writel(portclb_saved, port_mmio + PORT_LST_ADDR);
423 writel(portclbhi_saved, port_mmio + PORT_LST_ADDR_HI);
424 writel(portrxfis_saved, port_mmio + PORT_FIS_ADDR);
425 writel(portrxfishi_saved, port_mmio + PORT_FIS_ADDR_HI);
426
427 hpriv->start_engine(ap);
428
429 if (online)
430 *class = ahci_dev_classify(ap);
431
432 return rc;
433}
434
435static void xgene_ahci_host_stop(struct ata_host *host)
436{
437 struct ahci_host_priv *hpriv = host->private_data;
438
439 ahci_platform_disable_resources(hpriv);
440}
441
Suman Tripathia3a84bc2015-01-06 15:32:16 +0530442/**
443 * xgene_ahci_pmp_softreset - Issue the softreset to the drives connected
444 * to Port Multiplier.
445 * @link: link to reset
446 * @class: Return value to indicate class of device
447 * @deadline: deadline jiffies for the operation
448 *
449 * Due to H/W errata, the controller is unable to save the PMP
450 * field fetched from command header before sending the H2D FIS.
451 * When the device returns the PMP port field in the D2H FIS, there is
452 * a mismatch and results in command completion failure. The workaround
453 * is to write the pmp value to PxFBS.DEV field before issuing any command
454 * to PMP.
455 */
456static int xgene_ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
457 unsigned long deadline)
458{
459 int pmp = sata_srst_pmp(link);
460 struct ata_port *ap = link->ap;
461 u32 rc;
462 void *port_mmio = ahci_port_base(ap);
463 u32 port_fbs;
464
465 /*
466 * Set PxFBS.DEV field with pmp
467 * value.
468 */
469 port_fbs = readl(port_mmio + PORT_FBS);
470 port_fbs &= ~PORT_FBS_DEV_MASK;
471 port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
472 writel(port_fbs, port_mmio + PORT_FBS);
473
474 rc = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
475
476 return rc;
477}
478
479/**
480 * xgene_ahci_softreset - Issue the softreset to the drive.
481 * @link: link to reset
482 * @class: Return value to indicate class of device
483 * @deadline: deadline jiffies for the operation
484 *
485 * Due to H/W errata, the controller is unable to save the PMP
486 * field fetched from command header before sending the H2D FIS.
487 * When the device returns the PMP port field in the D2H FIS, there is
488 * a mismatch and results in command completion failure. The workaround
489 * is to write the pmp value to PxFBS.DEV field before issuing any command
490 * to PMP. Here is the algorithm to detect PMP :
491 *
492 * 1. Save the PxFBS value
493 * 2. Program PxFBS.DEV with pmp value send by framework. Framework sends
494 * 0xF for both PMP/NON-PMP initially
495 * 3. Issue softreset
496 * 4. If signature class is PMP goto 6
497 * 5. restore the original PxFBS and goto 3
498 * 6. return
499 */
500static int xgene_ahci_softreset(struct ata_link *link, unsigned int *class,
501 unsigned long deadline)
502{
503 int pmp = sata_srst_pmp(link);
504 struct ata_port *ap = link->ap;
505 struct ahci_host_priv *hpriv = ap->host->private_data;
506 struct xgene_ahci_context *ctx = hpriv->plat_data;
507 void *port_mmio = ahci_port_base(ap);
508 u32 port_fbs;
509 u32 port_fbs_save;
510 u32 retry = 1;
511 u32 rc;
512
513 port_fbs_save = readl(port_mmio + PORT_FBS);
514
515 /*
516 * Set PxFBS.DEV field with pmp
517 * value.
518 */
519 port_fbs = readl(port_mmio + PORT_FBS);
520 port_fbs &= ~PORT_FBS_DEV_MASK;
521 port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
522 writel(port_fbs, port_mmio + PORT_FBS);
523
524softreset_retry:
525 rc = ahci_do_softreset(link, class, pmp,
526 deadline, ahci_check_ready);
527
528 ctx->class[ap->port_no] = *class;
529 if (*class != ATA_DEV_PMP) {
530 /*
531 * Retry for normal drives without
532 * setting PxFBS.DEV field with pmp value.
533 */
534 if (retry--) {
535 writel(port_fbs_save, port_mmio + PORT_FBS);
536 goto softreset_retry;
537 }
538 }
539
540 return rc;
541}
542
Loc Ho81d01bf2014-03-14 17:53:20 -0600543static struct ata_port_operations xgene_ahci_ops = {
544 .inherits = &ahci_ops,
545 .host_stop = xgene_ahci_host_stop,
546 .hardreset = xgene_ahci_hardreset,
547 .read_id = xgene_ahci_read_id,
Suman Tripathi2a0bdff2014-07-07 22:33:05 +0530548 .qc_issue = xgene_ahci_qc_issue,
Suman Tripathia3a84bc2015-01-06 15:32:16 +0530549 .softreset = xgene_ahci_softreset,
550 .pmp_softreset = xgene_ahci_pmp_softreset
Loc Ho81d01bf2014-03-14 17:53:20 -0600551};
552
553static const struct ata_port_info xgene_ahci_port_info = {
Suman Tripathi15400352015-01-06 15:32:15 +0530554 .flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
Loc Ho81d01bf2014-03-14 17:53:20 -0600555 .pio_mask = ATA_PIO4,
556 .udma_mask = ATA_UDMA6,
557 .port_ops = &xgene_ahci_ops,
558};
559
560static int xgene_ahci_hw_init(struct ahci_host_priv *hpriv)
561{
562 struct xgene_ahci_context *ctx = hpriv->plat_data;
563 int i;
564 int rc;
565 u32 val;
566
567 /* Remove IP RAM out of shutdown */
568 rc = xgene_ahci_init_memram(ctx);
569 if (rc)
570 return rc;
571
572 for (i = 0; i < MAX_AHCI_CHN_PERCTR; i++)
573 xgene_ahci_set_phy_cfg(ctx, i);
574
575 /* AXI disable Mask */
576 writel(0xffffffff, hpriv->mmio + HOST_IRQ_STAT);
577 readl(hpriv->mmio + HOST_IRQ_STAT); /* Force a barrier */
578 writel(0, ctx->csr_core + INTSTATUSMASK);
Loc Ho6a969182014-03-18 12:14:37 -0600579 val = readl(ctx->csr_core + INTSTATUSMASK); /* Force a barrier */
Loc Ho81d01bf2014-03-14 17:53:20 -0600580 dev_dbg(ctx->dev, "top level interrupt mask 0x%X value 0x%08X\n",
581 INTSTATUSMASK, val);
582
583 writel(0x0, ctx->csr_core + ERRINTSTATUSMASK);
584 readl(ctx->csr_core + ERRINTSTATUSMASK); /* Force a barrier */
585 writel(0x0, ctx->csr_axi + INT_SLV_TMOMASK);
586 readl(ctx->csr_axi + INT_SLV_TMOMASK);
587
588 /* Enable AXI Interrupt */
589 writel(0xffffffff, ctx->csr_core + SLVRDERRATTRIBUTES);
590 writel(0xffffffff, ctx->csr_core + SLVWRERRATTRIBUTES);
591 writel(0xffffffff, ctx->csr_core + MSTRDERRATTRIBUTES);
592 writel(0xffffffff, ctx->csr_core + MSTWRERRATTRIBUTES);
593
594 /* Enable coherency */
595 val = readl(ctx->csr_core + BUSCTLREG);
596 val &= ~0x00000002; /* Enable write coherency */
597 val &= ~0x00000001; /* Enable read coherency */
598 writel(val, ctx->csr_core + BUSCTLREG);
599
600 val = readl(ctx->csr_core + IOFMSTRWAUX);
601 val |= (1 << 3); /* Enable read coherency */
602 val |= (1 << 9); /* Enable write coherency */
603 writel(val, ctx->csr_core + IOFMSTRWAUX);
604 val = readl(ctx->csr_core + IOFMSTRWAUX);
605 dev_dbg(ctx->dev, "coherency 0x%X value 0x%08X\n",
606 IOFMSTRWAUX, val);
607
608 return rc;
609}
610
611static int xgene_ahci_mux_select(struct xgene_ahci_context *ctx)
612{
613 u32 val;
614
615 /* Check for optional MUX resource */
Suman Tripathia77b6ee2014-09-22 18:31:33 +0530616 if (!ctx->csr_mux)
Loc Ho81d01bf2014-03-14 17:53:20 -0600617 return 0;
618
619 val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
620 val &= ~CFG_SATA_ENET_SELECT_MASK;
621 writel(val, ctx->csr_mux + SATA_ENET_CONFIG_REG);
622 val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
623 return val & CFG_SATA_ENET_SELECT_MASK ? -1 : 0;
624}
625
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900626static struct scsi_host_template ahci_platform_sht = {
627 AHCI_SHT(DRV_NAME),
628};
629
Loc Ho81d01bf2014-03-14 17:53:20 -0600630static int xgene_ahci_probe(struct platform_device *pdev)
631{
632 struct device *dev = &pdev->dev;
633 struct ahci_host_priv *hpriv;
634 struct xgene_ahci_context *ctx;
635 struct resource *res;
636 int rc;
637
638 hpriv = ahci_platform_get_resources(pdev);
639 if (IS_ERR(hpriv))
640 return PTR_ERR(hpriv);
641
642 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
643 if (!ctx)
644 return -ENOMEM;
645
646 hpriv->plat_data = ctx;
647 ctx->hpriv = hpriv;
648 ctx->dev = dev;
649
650 /* Retrieve the IP core resource */
651 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
652 ctx->csr_core = devm_ioremap_resource(dev, res);
653 if (IS_ERR(ctx->csr_core))
654 return PTR_ERR(ctx->csr_core);
655
656 /* Retrieve the IP diagnostic resource */
657 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
658 ctx->csr_diag = devm_ioremap_resource(dev, res);
659 if (IS_ERR(ctx->csr_diag))
660 return PTR_ERR(ctx->csr_diag);
661
662 /* Retrieve the IP AXI resource */
663 res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
664 ctx->csr_axi = devm_ioremap_resource(dev, res);
665 if (IS_ERR(ctx->csr_axi))
666 return PTR_ERR(ctx->csr_axi);
667
668 /* Retrieve the optional IP mux resource */
669 res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
Suman Tripathia77b6ee2014-09-22 18:31:33 +0530670 if (res) {
671 void __iomem *csr = devm_ioremap_resource(dev, res);
672 if (IS_ERR(csr))
673 return PTR_ERR(csr);
674
675 ctx->csr_mux = csr;
676 }
Loc Ho81d01bf2014-03-14 17:53:20 -0600677
678 dev_dbg(dev, "VAddr 0x%p Mmio VAddr 0x%p\n", ctx->csr_core,
679 hpriv->mmio);
680
681 /* Select ATA */
682 if ((rc = xgene_ahci_mux_select(ctx))) {
683 dev_err(dev, "SATA mux selection failed error %d\n", rc);
684 return -ENODEV;
685 }
686
Suman Tripathi0bed13b2014-08-28 14:51:21 +0530687 if (xgene_ahci_is_memram_inited(ctx)) {
688 dev_info(dev, "skip clock and PHY initialization\n");
689 goto skip_clk_phy;
690 }
691
Loc Ho81d01bf2014-03-14 17:53:20 -0600692 /* Due to errata, HW requires full toggle transition */
693 rc = ahci_platform_enable_clks(hpriv);
694 if (rc)
695 goto disable_resources;
696 ahci_platform_disable_clks(hpriv);
697
698 rc = ahci_platform_enable_resources(hpriv);
699 if (rc)
700 goto disable_resources;
701
702 /* Configure the host controller */
703 xgene_ahci_hw_init(hpriv);
Suman Tripathi0bed13b2014-08-28 14:51:21 +0530704skip_clk_phy:
Suman Tripathi72f79f92014-08-08 21:44:25 +0530705 hpriv->flags = AHCI_HFLAG_NO_PMP | AHCI_HFLAG_NO_NCQ;
Kefeng Wangf9f36912014-05-14 14:13:41 +0800706
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900707 rc = ahci_platform_init_host(pdev, hpriv, &xgene_ahci_port_info,
708 &ahci_platform_sht);
Loc Ho81d01bf2014-03-14 17:53:20 -0600709 if (rc)
710 goto disable_resources;
711
712 dev_dbg(dev, "X-Gene SATA host controller initialized\n");
713 return 0;
714
715disable_resources:
716 ahci_platform_disable_resources(hpriv);
717 return rc;
718}
719
720static const struct of_device_id xgene_ahci_of_match[] = {
721 {.compatible = "apm,xgene-ahci"},
722 {},
723};
724MODULE_DEVICE_TABLE(of, xgene_ahci_of_match);
725
726static struct platform_driver xgene_ahci_driver = {
727 .probe = xgene_ahci_probe,
728 .remove = ata_platform_remove_one,
729 .driver = {
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900730 .name = DRV_NAME,
Loc Ho81d01bf2014-03-14 17:53:20 -0600731 .of_match_table = xgene_ahci_of_match,
732 },
733};
734
735module_platform_driver(xgene_ahci_driver);
736
737MODULE_DESCRIPTION("APM X-Gene AHCI SATA driver");
738MODULE_AUTHOR("Loc Ho <lho@apm.com>");
739MODULE_LICENSE("GPL");
740MODULE_VERSION("0.4");