2 +++ b/drivers/ata/cns3xxx_ahci.c
5 + * ahci.c - AHCI SATA support
7 + * Maintained by: Jeff Garzik <jgarzik@pobox.com>
8 + * Please ALWAYS copy linux-ide@vger.kernel.org
11 + * Copyright 2004-2005 Red Hat, Inc.
14 + * This program is free software; you can redistribute it and/or modify
15 + * it under the terms of the GNU General Public License as published by
16 + * the Free Software Foundation; either version 2, or (at your option)
17 + * any later version.
19 + * This program is distributed in the hope that it will be useful,
20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 + * GNU General Public License for more details.
24 + * You should have received a copy of the GNU General Public License
25 + * along with this program; see the file COPYING. If not, write to
26 + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 + * libata documentation is available via 'make {ps|pdf}docs',
30 + * as Documentation/DocBook/libata.*
32 + * AHCI hardware documentation:
33 + * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
34 + * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
38 + * Cavium CNS3XXX notice
39 + * This driver is copy from ahci, and this driver only modify memory access function.
40 + * Let the driver support non-PCI device
42 +#include <linux/kernel.h>
43 +#include <linux/module.h>
44 +#include <linux/pci.h>
45 +#include <linux/init.h>
46 +#include <linux/blkdev.h>
47 +#include <linux/delay.h>
48 +#include <linux/interrupt.h>
49 +#include <linux/dma-mapping.h>
50 +#include <linux/device.h>
51 +#include <linux/dmi.h>
52 +#include <scsi/scsi_host.h>
53 +#include <scsi/scsi_cmnd.h>
54 +#include <linux/libata.h>
55 +#include <linux/platform_device.h>
57 +#include <mach/misc.h>
59 +#define DRV_NAME "cns3xxx_ahci"
60 +#define DRV_VERSION "3.0"
62 +#define MISC_REG_VALUE(offset) (*((volatile unsigned int *)(CNS3XXX_MISC_BASE_VIRT+offset)))
63 +#define CNS3XXX_MISC_REGISTER MISC_REG_VALUE(0x514)
64 +#define AHCI_REG_VALUE(offset) (*((volatile unsigned int *)(CNS3XXX_SATA2_BASE_VIRT+offset)))
65 +#define CNS3XXX_AHCI_HOSTCTL_REG AHCI_REG_VALUE(0x04)
67 +/* Enclosure Management Control */
68 +#define EM_CTRL_MSG_TYPE 0x000f0000
70 +/* Enclosure Management LED Message Type */
71 +#define EM_MSG_LED_HBA_PORT 0x0000000f
72 +#define EM_MSG_LED_PMP_SLOT 0x0000ff00
73 +#define EM_MSG_LED_VALUE 0xffff0000
74 +#define EM_MSG_LED_VALUE_ACTIVITY 0x00070000
75 +#define EM_MSG_LED_VALUE_OFF 0xfff80000
76 +#define EM_MSG_LED_VALUE_ON 0x00010000
78 +/* PHY Misc Define */
79 +#define MISC_SATA_POWER_MODE MISC_MEM_MAP_VALUE(0x310)
80 +#define MISC_SATA_CORE_ID MISC_MEM_MAP_VALUE(0x600)
81 +#define MISC_SATA_PORT0_PHY_CFG MISC_MEM_MAP_VALUE(0x604)
82 +#define MISC_SATA_PORT1_PHY_CFG MISC_MEM_MAP_VALUE(0x608)
83 +#define MISC_SATA_PORT0_PHY_TST MISC_MEM_MAP_VALUE(0x60C)
84 +#define MISC_SATA_PORT1_PHY_TST MISC_MEM_MAP_VALUE(0x610)
87 +static int ahci_skip_host_reset;
88 +static int ahci_ignore_sss;
90 +module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
91 +MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
93 +module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
94 +MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
96 +static int ahci_enable_alpm(struct ata_port *ap,
97 + enum link_pm policy);
98 +static void ahci_disable_alpm(struct ata_port *ap);
99 +static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
100 +static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
102 +static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
107 + AHCI_MAX_PORTS = 32,
108 + AHCI_MAX_SG = 168, /* hardware max is 64K */
109 + AHCI_DMA_BOUNDARY = 0xffffffff,
110 + AHCI_MAX_CMDS = 32,
112 + AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
113 + AHCI_RX_FIS_SZ = 256,
114 + AHCI_CMD_TBL_CDB = 0x40,
115 + AHCI_CMD_TBL_HDR_SZ = 0x80,
116 + AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
117 + AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
118 + AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
120 + AHCI_IRQ_ON_SG = (1 << 31),
121 + AHCI_CMD_ATAPI = (1 << 5),
122 + AHCI_CMD_WRITE = (1 << 6),
123 + AHCI_CMD_PREFETCH = (1 << 7),
124 + AHCI_CMD_RESET = (1 << 8),
125 + AHCI_CMD_CLR_BUSY = (1 << 10),
127 + RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
128 + RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
129 + RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
132 + board_ahci_vt8251 = 1,
133 + board_ahci_ign_iferr = 2,
134 + board_ahci_sb600 = 3,
136 + board_ahci_sb700 = 5, /* for SB700 and SB800 */
137 + board_ahci_mcp65 = 6,
138 + board_ahci_nopmp = 7,
139 + board_ahci_yesncq = 8,
141 + /* global controller registers */
142 + HOST_CAP = 0x00, /* host capabilities */
143 + HOST_CTL = 0x04, /* global host control */
144 + HOST_IRQ_STAT = 0x08, /* interrupt status */
145 + HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
146 + HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
147 + HOST_EM_LOC = 0x1c, /* Enclosure Management location */
148 + HOST_EM_CTL = 0x20, /* Enclosure Management Control */
150 + /* HOST_CTL bits */
151 + HOST_RESET = (1 << 0), /* reset controller; self-clear */
152 + HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
153 + HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
155 + /* HOST_CAP bits */
156 + HOST_CAP_EMS = (1 << 6), /* Enclosure Management support */
157 + HOST_CAP_SSC = (1 << 14), /* Slumber capable */
158 + HOST_CAP_PMP = (1 << 17), /* Port Multiplier support */
159 + HOST_CAP_CLO = (1 << 24), /* Command List Override support */
160 + HOST_CAP_ALPM = (1 << 26), /* Aggressive Link PM support */
161 + HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
162 + HOST_CAP_SNTF = (1 << 29), /* SNotification register */
163 + HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
164 + HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
166 + /* registers for each SATA port */
167 + PORT_LST_ADDR = 0x00, /* command list DMA addr */
168 + PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
169 + PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
170 + PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
171 + PORT_IRQ_STAT = 0x10, /* interrupt status */
172 + PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
173 + PORT_CMD = 0x18, /* port command */
174 + PORT_TFDATA = 0x20, /* taskfile data */
175 + PORT_SIG = 0x24, /* device TF signature */
176 + PORT_CMD_ISSUE = 0x38, /* command issue */
177 + PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
178 + PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
179 + PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
180 + PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
181 + PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
183 + /* PORT_IRQ_{STAT,MASK} bits */
184 + PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
185 + PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
186 + PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
187 + PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
188 + PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
189 + PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
190 + PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
191 + PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
193 + PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
194 + PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
195 + PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
196 + PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
197 + PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
198 + PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
199 + PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
200 + PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
201 + PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
203 + PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
209 + PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
211 + PORT_IRQ_HBUS_DATA_ERR,
212 + DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
213 + PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
214 + PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
216 + /* PORT_CMD bits */
217 + PORT_CMD_ASP = (1 << 27), /* Aggressive Slumber/Partial */
218 + PORT_CMD_ALPE = (1 << 26), /* Aggressive Link PM enable */
219 + PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
220 + PORT_CMD_PMP = (1 << 17), /* PMP attached */
221 + PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
222 + PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
223 + PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
224 + PORT_CMD_CLO = (1 << 3), /* Command list override */
225 + PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
226 + PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
227 + PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
229 + PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
230 + PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
231 + PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
232 + PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
234 + /* hpriv->flags bits */
235 + AHCI_HFLAG_NO_NCQ = (1 << 0),
236 + AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
237 + AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
238 + AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
239 + AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
240 + AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
241 + AHCI_HFLAG_NO_PMP = (1 << 6), /* no PMP */
242 + AHCI_HFLAG_NO_HOTPLUG = (1 << 7), /* ignore PxSERR.DIAG.N */
243 + AHCI_HFLAG_SECT255 = (1 << 8), /* max 255 sectors */
244 + AHCI_HFLAG_YES_NCQ = (1 << 9), /* force NCQ cap on */
245 + AHCI_HFLAG_NO_SUSPEND = (1 << 10), /* don't suspend */
246 + AHCI_HFLAG_SRST_TOUT_IS_OFFLINE = (1 << 11), /* treat SRST timeout as
249 + /* ap->flags bits */
251 + AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
252 + ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
253 + ATA_FLAG_ACPI_SATA | ATA_FLAG_AN |
256 + ICH_MAP = 0x90, /* ICH MAP register */
263 + EM_CTL_RST = (1 << 9), /* Reset */
264 + EM_CTL_TM = (1 << 8), /* Transmit Message */
265 + EM_CTL_ALHD = (1 << 26), /* Activity LED */
267 + /* CNS3XXX define */
268 + HOST_TIMER1MS = 0xe0, /* Timer 1ms register */
271 +struct ahci_cmd_hdr {
275 + __le32 tbl_addr_hi;
276 + __le32 reserved[4];
286 +struct ahci_em_priv {
287 + enum sw_activity blink_policy;
288 + struct timer_list timer;
289 + unsigned long saved_activity;
290 + unsigned long activity;
291 + unsigned long led_state;
294 +struct ahci_host_priv {
295 + unsigned int flags; /* AHCI_HFLAG_* */
296 + u32 cap; /* cap to use */
297 + u32 port_map; /* port map to use */
298 + u32 saved_cap; /* saved initial cap */
299 + u32 saved_port_map; /* saved initial port_map */
300 + u32 em_loc; /* enclosure management location */
303 +struct ahci_port_priv {
304 + struct ata_link *active_link;
305 + struct ahci_cmd_hdr *cmd_slot;
306 + dma_addr_t cmd_slot_dma;
308 + dma_addr_t cmd_tbl_dma;
310 + dma_addr_t rx_fis_dma;
311 + /* for NCQ spurious interrupt analysis */
312 + unsigned int ncq_saw_d2h:1;
313 + unsigned int ncq_saw_dmas:1;
314 + unsigned int ncq_saw_sdb:1;
315 + u32 intr_mask; /* interrupts to enable */
316 + /* enclosure management info per PM slot */
317 + struct ahci_em_priv em_priv[EM_MAX_SLOTS];
320 +static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
321 +static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
323 +static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
325 +static int ahci_probe(struct platform_device *pdev);
326 +static int ahci_remove(struct platform_device *pdev);
328 +static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
329 +static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
330 +static int ahci_port_start(struct ata_port *ap);
331 +static void ahci_port_stop(struct ata_port *ap);
332 +static void ahci_qc_prep(struct ata_queued_cmd *qc);
333 +static void ahci_freeze(struct ata_port *ap);
334 +static void ahci_thaw(struct ata_port *ap);
335 +static void ahci_pmp_attach(struct ata_port *ap);
336 +static void ahci_pmp_detach(struct ata_port *ap);
337 +static int ahci_softreset(struct ata_link *link, unsigned int *class,
338 + unsigned long deadline);
339 +static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
340 + unsigned long deadline);
341 +static int ahci_hardreset(struct ata_link *link, unsigned int *class,
342 + unsigned long deadline);
343 +static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
344 + unsigned long deadline);
346 +static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
347 + unsigned long deadline);
349 +static void ahci_postreset(struct ata_link *link, unsigned int *class);
350 +static void ahci_error_handler(struct ata_port *ap);
351 +static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
352 +static int ahci_port_resume(struct ata_port *ap);
353 +static void ahci_dev_config(struct ata_device *dev);
354 +static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
357 +static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
358 +static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
359 +static int ahci_pci_device_resume(struct pci_dev *pdev);
361 +static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
362 +static ssize_t ahci_activity_store(struct ata_device *dev,
363 + enum sw_activity val);
364 +static void ahci_init_sw_activity(struct ata_link *link);
366 +static struct device_attribute *ahci_shost_attrs[] = {
367 + &dev_attr_link_power_management_policy,
368 + &dev_attr_em_message_type,
369 + &dev_attr_em_message,
373 +static struct device_attribute *ahci_sdev_attrs[] = {
374 + &dev_attr_sw_activity,
375 + &dev_attr_unload_heads,
379 +static struct scsi_host_template ahci_sht = {
380 + ATA_NCQ_SHT(DRV_NAME),
381 + .can_queue = AHCI_MAX_CMDS - 1,
382 + .sg_tablesize = AHCI_MAX_SG,
383 + .dma_boundary = AHCI_DMA_BOUNDARY,
384 + .shost_attrs = ahci_shost_attrs,
385 + .sdev_attrs = ahci_sdev_attrs,
388 +static struct ata_port_operations ahci_ops = {
389 + .inherits = &sata_pmp_port_ops,
391 + .qc_defer = sata_pmp_qc_defer_cmd_switch,
392 + .qc_prep = ahci_qc_prep,
393 + .qc_issue = ahci_qc_issue,
394 + .qc_fill_rtf = ahci_qc_fill_rtf,
396 + .freeze = ahci_freeze,
398 + .softreset = ahci_softreset,
399 + .hardreset = ahci_hardreset,
400 + .postreset = ahci_postreset,
401 + .pmp_softreset = ahci_softreset,
402 + .error_handler = ahci_error_handler,
403 + .post_internal_cmd = ahci_post_internal_cmd,
404 + .dev_config = ahci_dev_config,
406 + .scr_read = ahci_scr_read,
407 + .scr_write = ahci_scr_write,
408 + .pmp_attach = ahci_pmp_attach,
409 + .pmp_detach = ahci_pmp_detach,
411 + .enable_pm = ahci_enable_alpm,
412 + .disable_pm = ahci_disable_alpm,
413 + .em_show = ahci_led_show,
414 + .em_store = ahci_led_store,
415 + .sw_activity_show = ahci_activity_show,
416 + .sw_activity_store = ahci_activity_store,
418 + .port_suspend = ahci_port_suspend,
419 + .port_resume = ahci_port_resume,
421 + .port_start = ahci_port_start,
422 + .port_stop = ahci_port_stop,
425 +static struct ata_port_operations ahci_vt8251_ops = {
426 + .inherits = &ahci_ops,
427 + .hardreset = ahci_vt8251_hardreset,
431 +static struct ata_port_operations ahci_p5wdh_ops = {
432 + .inherits = &ahci_ops,
433 + .hardreset = ahci_p5wdh_hardreset,
437 +static struct ata_port_operations ahci_sb600_ops = {
438 + .inherits = &ahci_ops,
439 + .softreset = ahci_sb600_softreset,
440 + .pmp_softreset = ahci_sb600_softreset,
443 +#define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
445 +static const struct ata_port_info ahci_port_info[] = {
448 + .flags = AHCI_FLAG_COMMON,
449 + .pio_mask = ATA_PIO4,
450 + .udma_mask = ATA_UDMA6,
451 + .port_ops = &ahci_ops,
453 + [board_ahci_vt8251] =
455 + AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
456 + .flags = AHCI_FLAG_COMMON,
457 + .pio_mask = ATA_PIO4,
458 + .udma_mask = ATA_UDMA6,
459 + .port_ops = &ahci_vt8251_ops,
461 + [board_ahci_ign_iferr] =
463 + AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
464 + .flags = AHCI_FLAG_COMMON,
465 + .pio_mask = ATA_PIO4,
466 + .udma_mask = ATA_UDMA6,
467 + .port_ops = &ahci_ops,
469 + [board_ahci_sb600] =
471 + AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL |
472 + AHCI_HFLAG_NO_MSI | AHCI_HFLAG_SECT255),
473 + .flags = AHCI_FLAG_COMMON,
474 + .pio_mask = ATA_PIO4,
475 + .udma_mask = ATA_UDMA6,
476 + .port_ops = &ahci_sb600_ops,
480 + AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
481 + AHCI_HFLAG_MV_PATA | AHCI_HFLAG_NO_PMP),
482 + .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
483 + ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
484 + .pio_mask = ATA_PIO4,
485 + .udma_mask = ATA_UDMA6,
486 + .port_ops = &ahci_ops,
488 + [board_ahci_sb700] = /* for SB700 and SB800 */
490 + AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL),
491 + .flags = AHCI_FLAG_COMMON,
492 + .pio_mask = ATA_PIO4,
493 + .udma_mask = ATA_UDMA6,
494 + .port_ops = &ahci_sb600_ops,
496 + [board_ahci_mcp65] =
498 + AHCI_HFLAGS (AHCI_HFLAG_YES_NCQ),
499 + .flags = AHCI_FLAG_COMMON,
500 + .pio_mask = ATA_PIO4,
501 + .udma_mask = ATA_UDMA6,
502 + .port_ops = &ahci_ops,
504 + [board_ahci_nopmp] =
506 + AHCI_HFLAGS (AHCI_HFLAG_NO_PMP),
507 + .flags = AHCI_FLAG_COMMON,
508 + .pio_mask = ATA_PIO4,
509 + .udma_mask = ATA_UDMA6,
510 + .port_ops = &ahci_ops,
512 + /* board_ahci_yesncq */
514 + AHCI_HFLAGS (AHCI_HFLAG_YES_NCQ),
515 + .flags = AHCI_FLAG_COMMON,
516 + .pio_mask = ATA_PIO4,
517 + .udma_mask = ATA_UDMA6,
518 + .port_ops = &ahci_ops,
522 +static const struct pci_device_id ahci_pci_tbl[] = {
524 + { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
525 + { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
526 + { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
527 + { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
528 + { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
529 + { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
530 + { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
531 + { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
532 + { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
533 + { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
534 + { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
535 + { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
536 + { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
537 + { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
538 + { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
539 + { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
540 + { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
541 + { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
542 + { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
543 + { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
544 + { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
545 + { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
546 + { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
547 + { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
548 + { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
549 + { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
550 + { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
551 + { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
552 + { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
553 + { PCI_VDEVICE(INTEL, 0x3a05), board_ahci }, /* ICH10 */
554 + { PCI_VDEVICE(INTEL, 0x3a22), board_ahci }, /* ICH10 */
555 + { PCI_VDEVICE(INTEL, 0x3a25), board_ahci }, /* ICH10 */
556 + { PCI_VDEVICE(INTEL, 0x3b22), board_ahci }, /* PCH AHCI */
557 + { PCI_VDEVICE(INTEL, 0x3b23), board_ahci }, /* PCH AHCI */
558 + { PCI_VDEVICE(INTEL, 0x3b24), board_ahci }, /* PCH RAID */
559 + { PCI_VDEVICE(INTEL, 0x3b25), board_ahci }, /* PCH RAID */
560 + { PCI_VDEVICE(INTEL, 0x3b29), board_ahci }, /* PCH AHCI */
561 + { PCI_VDEVICE(INTEL, 0x3b2b), board_ahci }, /* PCH RAID */
562 + { PCI_VDEVICE(INTEL, 0x3b2c), board_ahci }, /* PCH RAID */
563 + { PCI_VDEVICE(INTEL, 0x3b2f), board_ahci }, /* PCH AHCI */
565 + /* JMicron 360/1/3/5/6, match class to avoid IDE function */
566 + { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
567 + PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
570 + { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
571 + { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb700 }, /* ATI SB700/800 */
572 + { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb700 }, /* ATI SB700/800 */
573 + { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb700 }, /* ATI SB700/800 */
574 + { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb700 }, /* ATI SB700/800 */
575 + { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb700 }, /* ATI SB700/800 */
576 + { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb700 }, /* ATI SB700/800 */
579 + { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
580 + { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
583 + { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci_mcp65 }, /* MCP65 */
584 + { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci_mcp65 }, /* MCP65 */
585 + { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci_mcp65 }, /* MCP65 */
586 + { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci_mcp65 }, /* MCP65 */
587 + { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci_mcp65 }, /* MCP65 */
588 + { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci_mcp65 }, /* MCP65 */
589 + { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci_mcp65 }, /* MCP65 */
590 + { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci_mcp65 }, /* MCP65 */
591 + { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci_yesncq }, /* MCP67 */
592 + { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci_yesncq }, /* MCP67 */
593 + { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci_yesncq }, /* MCP67 */
594 + { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci_yesncq }, /* MCP67 */
595 + { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci_yesncq }, /* MCP67 */
596 + { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci_yesncq }, /* MCP67 */
597 + { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci_yesncq }, /* MCP67 */
598 + { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci_yesncq }, /* MCP67 */
599 + { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci_yesncq }, /* MCP67 */
600 + { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci_yesncq }, /* MCP67 */
601 + { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci_yesncq }, /* MCP67 */
602 + { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci_yesncq }, /* MCP67 */
603 + { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci_yesncq }, /* MCP73 */
604 + { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci_yesncq }, /* MCP73 */
605 + { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci_yesncq }, /* MCP73 */
606 + { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci_yesncq }, /* MCP73 */
607 + { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci_yesncq }, /* MCP73 */
608 + { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci_yesncq }, /* MCP73 */
609 + { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci_yesncq }, /* MCP73 */
610 + { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci_yesncq }, /* MCP73 */
611 + { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci_yesncq }, /* MCP73 */
612 + { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci_yesncq }, /* MCP73 */
613 + { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci_yesncq }, /* MCP73 */
614 + { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci_yesncq }, /* MCP73 */
615 + { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
616 + { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
617 + { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
618 + { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
619 + { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
620 + { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
621 + { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
622 + { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
623 + { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
624 + { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
625 + { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
626 + { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
627 + { PCI_VDEVICE(NVIDIA, 0x0ab4), board_ahci }, /* MCP79 */
628 + { PCI_VDEVICE(NVIDIA, 0x0ab5), board_ahci }, /* MCP79 */
629 + { PCI_VDEVICE(NVIDIA, 0x0ab6), board_ahci }, /* MCP79 */
630 + { PCI_VDEVICE(NVIDIA, 0x0ab7), board_ahci }, /* MCP79 */
631 + { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci }, /* MCP79 */
632 + { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci }, /* MCP79 */
633 + { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci }, /* MCP79 */
634 + { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci }, /* MCP79 */
635 + { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci }, /* MCP79 */
636 + { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci }, /* MCP79 */
637 + { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci }, /* MCP79 */
638 + { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci }, /* MCP79 */
639 + { PCI_VDEVICE(NVIDIA, 0x0d84), board_ahci }, /* MCP89 */
640 + { PCI_VDEVICE(NVIDIA, 0x0d85), board_ahci }, /* MCP89 */
641 + { PCI_VDEVICE(NVIDIA, 0x0d86), board_ahci }, /* MCP89 */
642 + { PCI_VDEVICE(NVIDIA, 0x0d87), board_ahci }, /* MCP89 */
643 + { PCI_VDEVICE(NVIDIA, 0x0d88), board_ahci }, /* MCP89 */
644 + { PCI_VDEVICE(NVIDIA, 0x0d89), board_ahci }, /* MCP89 */
645 + { PCI_VDEVICE(NVIDIA, 0x0d8a), board_ahci }, /* MCP89 */
646 + { PCI_VDEVICE(NVIDIA, 0x0d8b), board_ahci }, /* MCP89 */
647 + { PCI_VDEVICE(NVIDIA, 0x0d8c), board_ahci }, /* MCP89 */
648 + { PCI_VDEVICE(NVIDIA, 0x0d8d), board_ahci }, /* MCP89 */
649 + { PCI_VDEVICE(NVIDIA, 0x0d8e), board_ahci }, /* MCP89 */
650 + { PCI_VDEVICE(NVIDIA, 0x0d8f), board_ahci }, /* MCP89 */
653 + { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
654 + { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 968 */
655 + { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
658 + { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
659 + { PCI_VDEVICE(MARVELL, 0x6121), board_ahci_mv }, /* 6121 */
662 + { PCI_VDEVICE(PROMISE, 0x3f20), board_ahci }, /* PDC42819 */
664 + /* Generic, PCI class code for AHCI */
665 + { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
666 + PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
668 + { } /* terminate list */
673 +static struct pci_driver ahci_pci_driver = {
675 + .id_table = ahci_pci_tbl,
676 + .probe = ahci_init_one,
677 + .remove = ata_pci_remove_one,
679 + .suspend = ahci_pci_device_suspend,
680 + .resume = ahci_pci_device_resume,
684 +static struct platform_driver ahci_driver = {
685 + .probe = ahci_probe,
686 + .remove = __devexit_p(ahci_remove),
689 + .owner = THIS_MODULE,
694 +static int ahci_em_messages = 1;
695 +module_param(ahci_em_messages, int, 0444);
696 +/* add other LED protocol types when they become supported */
697 +MODULE_PARM_DESC(ahci_em_messages,
698 + "Set AHCI Enclosure Management Message type (0 = disabled, 1 = LED");
700 +#if defined(CONFIG_PATA_MARVELL) || defined(CONFIG_PATA_MARVELL_MODULE)
701 +static int marvell_enable;
703 +static int marvell_enable = 1;
705 +module_param(marvell_enable, int, 0644);
706 +MODULE_PARM_DESC(marvell_enable, "Marvell SATA via AHCI (1 = enabled)");
709 +static inline int ahci_nr_ports(u32 cap)
711 + return (cap & 0x1f) + 1;
714 +static inline void __iomem *__ahci_port_base(struct ata_host *host,
715 + unsigned int port_no)
718 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
720 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
723 + return mmio + 0x100 + (port_no * 0x80);
726 +static inline void __iomem *ahci_port_base(struct ata_port *ap)
728 + return __ahci_port_base(ap->host, ap->port_no);
731 +static void ahci_enable_ahci(void __iomem *mmio)
736 + /* turn on AHCI_EN */
737 + tmp = readl(mmio + HOST_CTL);
738 + if (tmp & HOST_AHCI_EN)
741 + /* Some controllers need AHCI_EN to be written multiple times.
742 + * Try a few times before giving up.
744 + for (i = 0; i < 5; i++) {
745 + tmp |= HOST_AHCI_EN;
746 + writel(tmp, mmio + HOST_CTL);
747 + tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
748 + if (tmp & HOST_AHCI_EN)
757 + * ahci_save_initial_config - Save and fixup initial config values
758 + * @pdev: target PCI device
759 + * @hpriv: host private area to store config values
761 + * Some registers containing configuration info might be setup by
762 + * BIOS and might be cleared on reset. This function saves the
763 + * initial values of those registers into @hpriv such that they
764 + * can be restored after controller reset.
766 + * If inconsistent, config values are fixed up by this function.
772 +static void ahci_save_initial_config(struct pci_dev *pdev,
773 + struct ahci_host_priv *hpriv)
775 +static void ahci_save_initial_config(struct platform_device *pdev,
776 + struct ahci_host_priv *hpriv,
781 + void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
783 + void __iomem *mmio = (void __iomem *)base;
791 + /* make sure AHCI mode is enabled before accessing CAP */
792 + ahci_enable_ahci(mmio);
794 + /* Values prefixed with saved_ are written back to host after
795 + * reset. Values without are used for driver operation.
797 + hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
798 + hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
800 + /* some chips have errata preventing 64bit use */
801 + if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
802 + dev_printk(KERN_INFO, &pdev->dev,
803 + "controller can't do 64bit DMA, forcing 32bit\n");
804 + cap &= ~HOST_CAP_64;
807 + if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
808 + dev_printk(KERN_INFO, &pdev->dev,
809 + "controller can't do NCQ, turning off CAP_NCQ\n");
810 + cap &= ~HOST_CAP_NCQ;
813 + if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
814 + dev_printk(KERN_INFO, &pdev->dev,
815 + "controller can do NCQ, turning on CAP_NCQ\n");
816 + cap |= HOST_CAP_NCQ;
819 + if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
820 + dev_printk(KERN_INFO, &pdev->dev,
821 + "controller can't do PMP, turning off CAP_PMP\n");
822 + cap &= ~HOST_CAP_PMP;
825 + if (pdev->vendor == PCI_VENDOR_ID_JMICRON && pdev->device == 0x2361 &&
827 + dev_printk(KERN_INFO, &pdev->dev,
828 + "JMB361 has only one port, port_map 0x%x -> 0x%x\n",
834 + * Temporary Marvell 6145 hack: PATA port presence
835 + * is asserted through the standard AHCI port
836 + * presence register, as bit 4 (counting from 0)
838 + if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
839 + if (pdev->device == 0x6121)
843 + dev_printk(KERN_ERR, &pdev->dev,
844 + "MV_AHCI HACK: port_map %x -> %x\n",
847 + dev_printk(KERN_ERR, &pdev->dev,
848 + "Disabling your PATA port. Use the boot option 'ahci.marvell_enable=0' to avoid this.\n");
854 + /* cross check port_map and cap.n_ports */
858 + for (i = 0; i < AHCI_MAX_PORTS; i++)
859 + if (port_map & (1 << i))
862 + /* If PI has more ports than n_ports, whine, clear
863 + * port_map and let it be generated from n_ports.
865 + if (map_ports > ahci_nr_ports(cap)) {
866 + dev_printk(KERN_WARNING, &pdev->dev,
867 + "implemented port map (0x%x) contains more "
868 + "ports than nr_ports (%u), using nr_ports\n",
869 + port_map, ahci_nr_ports(cap));
874 + /* fabricate port_map from cap.nr_ports */
876 + port_map = (1 << ahci_nr_ports(cap)) - 1;
877 + dev_printk(KERN_WARNING, &pdev->dev,
878 + "forcing PORTS_IMPL to 0x%x\n", port_map);
880 + /* write the fixed up value to the PI register */
881 + hpriv->saved_port_map = port_map;
884 + /* record values to use during operation */
886 + hpriv->port_map = port_map;
890 + * ahci_restore_initial_config - Restore initial config
891 + * @host: target ATA host
893 + * Restore initial config stored by ahci_save_initial_config().
898 +static void ahci_restore_initial_config(struct ata_host *host)
900 + struct ahci_host_priv *hpriv = host->private_data;
902 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
904 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
907 + writel(hpriv->saved_cap, mmio + HOST_CAP);
908 + writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
909 + (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
912 +static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
914 + static const int offset[] = {
915 + [SCR_STATUS] = PORT_SCR_STAT,
916 + [SCR_CONTROL] = PORT_SCR_CTL,
917 + [SCR_ERROR] = PORT_SCR_ERR,
918 + [SCR_ACTIVE] = PORT_SCR_ACT,
919 + [SCR_NOTIFICATION] = PORT_SCR_NTF,
921 + struct ahci_host_priv *hpriv = ap->host->private_data;
923 + if (sc_reg < ARRAY_SIZE(offset) &&
924 + (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
925 + return offset[sc_reg];
929 +static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
931 + void __iomem *port_mmio = ahci_port_base(link->ap);
932 + int offset = ahci_scr_offset(link->ap, sc_reg);
935 + *val = readl(port_mmio + offset);
941 +static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
943 + void __iomem *port_mmio = ahci_port_base(link->ap);
944 + int offset = ahci_scr_offset(link->ap, sc_reg);
947 + writel(val, port_mmio + offset);
953 +static void ahci_start_engine(struct ata_port *ap)
955 + void __iomem *port_mmio = ahci_port_base(ap);
959 + tmp = readl(port_mmio + PORT_CMD);
960 + tmp |= PORT_CMD_START;
961 + writel(tmp, port_mmio + PORT_CMD);
962 + readl(port_mmio + PORT_CMD); /* flush */
965 +static int ahci_stop_engine(struct ata_port *ap)
967 + void __iomem *port_mmio = ahci_port_base(ap);
970 + tmp = readl(port_mmio + PORT_CMD);
972 + /* check if the HBA is idle */
973 + if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
976 + /* setting HBA to idle */
977 + tmp &= ~PORT_CMD_START;
978 + writel(tmp, port_mmio + PORT_CMD);
980 + /* wait for engine to stop. This could be as long as 500 msec */
981 + tmp = ata_wait_register(port_mmio + PORT_CMD,
982 + PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
983 + if (tmp & PORT_CMD_LIST_ON)
989 +static void ahci_start_fis_rx(struct ata_port *ap)
991 + void __iomem *port_mmio = ahci_port_base(ap);
992 + struct ahci_host_priv *hpriv = ap->host->private_data;
993 + struct ahci_port_priv *pp = ap->private_data;
996 + /* set FIS registers */
997 + if (hpriv->cap & HOST_CAP_64)
998 + writel((pp->cmd_slot_dma >> 16) >> 16,
999 + port_mmio + PORT_LST_ADDR_HI);
1000 + writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
1002 + if (hpriv->cap & HOST_CAP_64)
1003 + writel((pp->rx_fis_dma >> 16) >> 16,
1004 + port_mmio + PORT_FIS_ADDR_HI);
1005 + writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
1007 + /* enable FIS reception */
1008 + tmp = readl(port_mmio + PORT_CMD);
1009 + tmp |= PORT_CMD_FIS_RX;
1010 + writel(tmp, port_mmio + PORT_CMD);
1013 + readl(port_mmio + PORT_CMD);
1016 +static int ahci_stop_fis_rx(struct ata_port *ap)
1018 + void __iomem *port_mmio = ahci_port_base(ap);
1021 + /* disable FIS reception */
1022 + tmp = readl(port_mmio + PORT_CMD);
1023 + tmp &= ~PORT_CMD_FIS_RX;
1024 + writel(tmp, port_mmio + PORT_CMD);
1026 + /* wait for completion, spec says 500ms, give it 1000 */
1027 + tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
1028 + PORT_CMD_FIS_ON, 10, 1000);
1029 + if (tmp & PORT_CMD_FIS_ON)
1035 +static void ahci_power_up(struct ata_port *ap)
1037 + struct ahci_host_priv *hpriv = ap->host->private_data;
1038 + void __iomem *port_mmio = ahci_port_base(ap);
1041 + cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
1043 + /* spin up device */
1044 + if (hpriv->cap & HOST_CAP_SSS) {
1045 + cmd |= PORT_CMD_SPIN_UP;
1046 + writel(cmd, port_mmio + PORT_CMD);
1049 + /* wake up link */
1050 + writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
1053 +static void ahci_disable_alpm(struct ata_port *ap)
1055 + struct ahci_host_priv *hpriv = ap->host->private_data;
1056 + void __iomem *port_mmio = ahci_port_base(ap);
1058 + struct ahci_port_priv *pp = ap->private_data;
1060 + /* IPM bits should be disabled by libata-core */
1061 + /* get the existing command bits */
1062 + cmd = readl(port_mmio + PORT_CMD);
1064 + /* disable ALPM and ASP */
1065 + cmd &= ~PORT_CMD_ASP;
1066 + cmd &= ~PORT_CMD_ALPE;
1068 + /* force the interface back to active */
1069 + cmd |= PORT_CMD_ICC_ACTIVE;
1071 + /* write out new cmd value */
1072 + writel(cmd, port_mmio + PORT_CMD);
1073 + cmd = readl(port_mmio + PORT_CMD);
1075 + /* wait 10ms to be sure we've come out of any low power state */
1078 + /* clear out any PhyRdy stuff from interrupt status */
1079 + writel(PORT_IRQ_PHYRDY, port_mmio + PORT_IRQ_STAT);
1081 + /* go ahead and clean out PhyRdy Change from Serror too */
1082 + ahci_scr_write(&ap->link, SCR_ERROR, ((1 << 16) | (1 << 18)));
1085 + * Clear flag to indicate that we should ignore all PhyRdy
1088 + hpriv->flags &= ~AHCI_HFLAG_NO_HOTPLUG;
1091 + * Enable interrupts on Phy Ready.
1093 + pp->intr_mask |= PORT_IRQ_PHYRDY;
1094 + writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1097 + * don't change the link pm policy - we can be called
1098 + * just to turn of link pm temporarily
1102 +static int ahci_enable_alpm(struct ata_port *ap,
1103 + enum link_pm policy)
1105 + struct ahci_host_priv *hpriv = ap->host->private_data;
1106 + void __iomem *port_mmio = ahci_port_base(ap);
1108 + struct ahci_port_priv *pp = ap->private_data;
1111 + /* Make sure the host is capable of link power management */
1112 + if (!(hpriv->cap & HOST_CAP_ALPM))
1116 + case MAX_PERFORMANCE:
1117 + case NOT_AVAILABLE:
1119 + * if we came here with NOT_AVAILABLE,
1120 + * it just means this is the first time we
1121 + * have tried to enable - default to max performance,
1122 + * and let the user go to lower power modes on request.
1124 + ahci_disable_alpm(ap);
1127 + /* configure HBA to enter SLUMBER */
1128 + asp = PORT_CMD_ASP;
1130 + case MEDIUM_POWER:
1131 + /* configure HBA to enter PARTIAL */
1139 + * Disable interrupts on Phy Ready. This keeps us from
1140 + * getting woken up due to spurious phy ready interrupts
1141 + * TBD - Hot plug should be done via polling now, is
1142 + * that even supported?
1144 + pp->intr_mask &= ~PORT_IRQ_PHYRDY;
1145 + writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1148 + * Set a flag to indicate that we should ignore all PhyRdy
1149 + * state changes since these can happen now whenever we
1150 + * change link state
1152 + hpriv->flags |= AHCI_HFLAG_NO_HOTPLUG;
1154 + /* get the existing command bits */
1155 + cmd = readl(port_mmio + PORT_CMD);
1158 + * Set ASP based on Policy
1163 + * Setting this bit will instruct the HBA to aggressively
1164 + * enter a lower power link state when it's appropriate and
1165 + * based on the value set above for ASP
1167 + cmd |= PORT_CMD_ALPE;
1169 + /* write out new cmd value */
1170 + writel(cmd, port_mmio + PORT_CMD);
1171 + cmd = readl(port_mmio + PORT_CMD);
1173 + /* IPM bits should be set by libata-core */
1178 +static void ahci_power_down(struct ata_port *ap)
1180 + struct ahci_host_priv *hpriv = ap->host->private_data;
1181 + void __iomem *port_mmio = ahci_port_base(ap);
1182 + u32 cmd, scontrol;
1184 + if (!(hpriv->cap & HOST_CAP_SSS))
1187 + /* put device into listen mode, first set PxSCTL.DET to 0 */
1188 + scontrol = readl(port_mmio + PORT_SCR_CTL);
1190 + writel(scontrol, port_mmio + PORT_SCR_CTL);
1192 + /* then set PxCMD.SUD to 0 */
1193 + cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
1194 + cmd &= ~PORT_CMD_SPIN_UP;
1195 + writel(cmd, port_mmio + PORT_CMD);
1199 +static void ahci_start_port(struct ata_port *ap)
1201 + struct ahci_port_priv *pp = ap->private_data;
1202 + struct ata_link *link;
1203 + struct ahci_em_priv *emp;
1207 + /* enable FIS reception */
1208 + ahci_start_fis_rx(ap);
1211 + ahci_start_engine(ap);
1213 + /* turn on LEDs */
1214 + if (ap->flags & ATA_FLAG_EM) {
1215 + ata_for_each_link(link, ap, EDGE) {
1216 + emp = &pp->em_priv[link->pmp];
1218 + /* EM Transmit bit maybe busy during init */
1219 + for (i = 0; i < EM_MAX_RETRY; i++) {
1220 + rc = ahci_transmit_led_message(ap,
1231 + if (ap->flags & ATA_FLAG_SW_ACTIVITY)
1232 + ata_for_each_link(link, ap, EDGE)
1233 + ahci_init_sw_activity(link);
1237 +static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
1242 + rc = ahci_stop_engine(ap);
1244 + *emsg = "failed to stop engine";
1248 + /* disable FIS reception */
1249 + rc = ahci_stop_fis_rx(ap);
1251 + *emsg = "failed stop FIS RX";
1258 +static int ahci_reset_controller(struct ata_host *host)
1261 + struct pci_dev *pdev = to_pci_dev(host->dev);
1262 + struct ahci_host_priv *hpriv = host->private_data;
1263 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1265 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
1269 + /* we must be in AHCI mode, before using anything
1270 + * AHCI-specific, such as HOST_RESET.
1272 + ahci_enable_ahci(mmio);
1274 + /* global controller reset */
1275 + if (!ahci_skip_host_reset) {
1276 + tmp = readl(mmio + HOST_CTL);
1277 + if ((tmp & HOST_RESET) == 0) {
1278 + writel(tmp | HOST_RESET, mmio + HOST_CTL);
1279 + readl(mmio + HOST_CTL); /* flush */
1283 + * to perform host reset, OS should set HOST_RESET
1284 + * and poll until this bit is read to be "0".
1285 + * reset must complete within 1 second, or
1286 + * the hardware should be considered fried.
1288 + tmp = ata_wait_register(mmio + HOST_CTL, HOST_RESET,
1289 + HOST_RESET, 10, 1000);
1291 + if (tmp & HOST_RESET) {
1292 + dev_printk(KERN_ERR, host->dev,
1293 + "controller reset failed (0x%x)\n", tmp);
1297 + /* turn on AHCI mode */
1298 + ahci_enable_ahci(mmio);
1300 + /* Some registers might be cleared on reset. Restore
1303 + ahci_restore_initial_config(host);
1305 + dev_printk(KERN_INFO, host->dev,
1306 + "skipping global host reset\n");
1309 + if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1312 + /* configure PCS */
1313 + pci_read_config_word(pdev, 0x92, &tmp16);
1314 + if ((tmp16 & hpriv->port_map) != hpriv->port_map) {
1315 + tmp16 |= hpriv->port_map;
1316 + pci_write_config_word(pdev, 0x92, tmp16);
1324 +static void ahci_sw_activity(struct ata_link *link)
1326 + struct ata_port *ap = link->ap;
1327 + struct ahci_port_priv *pp = ap->private_data;
1328 + struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1330 + if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
1334 + if (!timer_pending(&emp->timer))
1335 + mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
1338 +static void ahci_sw_activity_blink(unsigned long arg)
1340 + struct ata_link *link = (struct ata_link *)arg;
1341 + struct ata_port *ap = link->ap;
1342 + struct ahci_port_priv *pp = ap->private_data;
1343 + struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1344 + unsigned long led_message = emp->led_state;
1345 + u32 activity_led_state;
1346 + unsigned long flags;
1348 + led_message &= EM_MSG_LED_VALUE;
1349 + led_message |= ap->port_no | (link->pmp << 8);
1351 + /* check to see if we've had activity. If so,
1352 + * toggle state of LED and reset timer. If not,
1353 + * turn LED to desired idle state.
1355 + spin_lock_irqsave(ap->lock, flags);
1356 + if (emp->saved_activity != emp->activity) {
1357 + emp->saved_activity = emp->activity;
1358 + /* get the current LED state */
1359 + activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
1361 + if (activity_led_state)
1362 + activity_led_state = 0;
1364 + activity_led_state = 1;
1366 + /* clear old state */
1367 + led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1369 + /* toggle state */
1370 + led_message |= (activity_led_state << 16);
1371 + mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1373 + /* switch to idle */
1374 + led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1375 + if (emp->blink_policy == BLINK_OFF)
1376 + led_message |= (1 << 16);
1378 + spin_unlock_irqrestore(ap->lock, flags);
1379 + ahci_transmit_led_message(ap, led_message, 4);
1382 +static void ahci_init_sw_activity(struct ata_link *link)
1384 + struct ata_port *ap = link->ap;
1385 + struct ahci_port_priv *pp = ap->private_data;
1386 + struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1388 + /* init activity stats, setup timer */
1389 + emp->saved_activity = emp->activity = 0;
1390 + setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
1392 + /* check our blink policy and set flag for link if it's enabled */
1393 + if (emp->blink_policy)
1394 + link->flags |= ATA_LFLAG_SW_ACTIVITY;
1397 +static int ahci_reset_em(struct ata_host *host)
1400 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1402 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
1406 + em_ctl = readl(mmio + HOST_EM_CTL);
1407 + if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1410 + writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1414 +static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1417 + struct ahci_host_priv *hpriv = ap->host->private_data;
1418 + struct ahci_port_priv *pp = ap->private_data;
1420 + void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
1422 + void __iomem *mmio = (void __iomem *)ap->host->iomap;//[AHCI_BAR];
1425 + u32 message[] = {0, 0};
1426 + unsigned long flags;
1428 + struct ahci_em_priv *emp;
1430 + /* get the slot number from the message */
1431 + pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1432 + if (pmp < EM_MAX_SLOTS)
1433 + emp = &pp->em_priv[pmp];
1437 + spin_lock_irqsave(ap->lock, flags);
1440 + * if we are still busy transmitting a previous message,
1443 + em_ctl = readl(mmio + HOST_EM_CTL);
1444 + if (em_ctl & EM_CTL_TM) {
1445 + spin_unlock_irqrestore(ap->lock, flags);
1450 + * create message header - this is all zero except for
1451 + * the message size, which is 4 bytes.
1453 + message[0] |= (4 << 8);
1455 + /* ignore 0:4 of byte zero, fill in port info yourself */
1456 + message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
1458 + /* write message to EM_LOC */
1459 + writel(message[0], mmio + hpriv->em_loc);
1460 + writel(message[1], mmio + hpriv->em_loc+4);
1462 + /* save off new led state for port/slot */
1463 + emp->led_state = state;
1466 + * tell hardware to transmit the message
1468 + writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1470 + spin_unlock_irqrestore(ap->lock, flags);
1474 +static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1476 + struct ahci_port_priv *pp = ap->private_data;
1477 + struct ata_link *link;
1478 + struct ahci_em_priv *emp;
1481 + ata_for_each_link(link, ap, EDGE) {
1482 + emp = &pp->em_priv[link->pmp];
1483 + rc += sprintf(buf, "%lx\n", emp->led_state);
1488 +static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1493 + struct ahci_port_priv *pp = ap->private_data;
1494 + struct ahci_em_priv *emp;
1496 + state = simple_strtoul(buf, NULL, 0);
1498 + /* get the slot number from the message */
1499 + pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1500 + if (pmp < EM_MAX_SLOTS)
1501 + emp = &pp->em_priv[pmp];
1505 + /* mask off the activity bits if we are in sw_activity
1506 + * mode, user should turn off sw_activity before setting
1507 + * activity led through em_message
1509 + if (emp->blink_policy)
1510 + state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1512 + return ahci_transmit_led_message(ap, state, size);
1515 +static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1517 + struct ata_link *link = dev->link;
1518 + struct ata_port *ap = link->ap;
1519 + struct ahci_port_priv *pp = ap->private_data;
1520 + struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1521 + u32 port_led_state = emp->led_state;
1523 + /* save the desired Activity LED behavior */
1526 + link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1528 + /* set the LED to OFF */
1529 + port_led_state &= EM_MSG_LED_VALUE_OFF;
1530 + port_led_state |= (ap->port_no | (link->pmp << 8));
1531 + ahci_transmit_led_message(ap, port_led_state, 4);
1533 + link->flags |= ATA_LFLAG_SW_ACTIVITY;
1534 + if (val == BLINK_OFF) {
1535 + /* set LED to ON for idle */
1536 + port_led_state &= EM_MSG_LED_VALUE_OFF;
1537 + port_led_state |= (ap->port_no | (link->pmp << 8));
1538 + port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
1539 + ahci_transmit_led_message(ap, port_led_state, 4);
1542 + emp->blink_policy = val;
1546 +static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1548 + struct ata_link *link = dev->link;
1549 + struct ata_port *ap = link->ap;
1550 + struct ahci_port_priv *pp = ap->private_data;
1551 + struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1553 + /* display the saved value of activity behavior for this
1556 + return sprintf(buf, "%d\n", emp->blink_policy);
1560 +static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
1561 + int port_no, void __iomem *mmio,
1562 + void __iomem *port_mmio)
1564 +static void ahci_port_init(struct platform_device *pdev, struct ata_port *ap,
1565 + int port_no, void __iomem *mmio,
1566 + void __iomem *port_mmio)
1569 + const char *emsg = NULL;
1573 + /* make sure port is not active */
1574 + rc = ahci_deinit_port(ap, &emsg);
1576 + dev_printk(KERN_WARNING, &pdev->dev,
1577 + "%s (%d)\n", emsg, rc);
1579 + /* clear SError */
1580 + tmp = readl(port_mmio + PORT_SCR_ERR);
1581 + VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1582 + writel(tmp, port_mmio + PORT_SCR_ERR);
1584 + /* clear port IRQ */
1585 + tmp = readl(port_mmio + PORT_IRQ_STAT);
1586 + VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1588 + writel(tmp, port_mmio + PORT_IRQ_STAT);
1590 + writel(1 << port_no, mmio + HOST_IRQ_STAT);
1593 +static void ahci_init_controller(struct ata_host *host)
1595 + struct ahci_host_priv *hpriv = host->private_data;
1597 + struct pci_dev *pdev = to_pci_dev(host->dev);
1598 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1600 + struct platform_device *pdev = to_platform_device(host->dev);
1601 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
1604 + void __iomem *port_mmio;
1608 + if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
1610 + if (pdev->device == 0x6121)
1617 + port_mmio = __ahci_port_base(host, mv);
1619 + writel(0, port_mmio + PORT_IRQ_MASK);
1621 + /* clear port IRQ */
1622 + tmp = readl(port_mmio + PORT_IRQ_STAT);
1623 + VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1625 + writel(tmp, port_mmio + PORT_IRQ_STAT);
1628 + /* set Timer 1ms, hclk = 200Mhz */
1629 + /* FIXME: Add auto detect function */
1630 + printk("CPU clock : %d \n", cns3xxx_cpu_clock());
1631 + tmp = readl(mmio + HOST_TIMER1MS);
1632 + printk("*** Timer 1ms: %d(0x%x) ***\n",tmp,tmp);
1633 + writel(cns3xxx_cpu_clock()*500, mmio + HOST_TIMER1MS);
1634 + tmp = readl(mmio + HOST_TIMER1MS);
1635 + printk("*** Set to: %d(0x%x) ***\n",tmp, tmp);
1639 + for (i = 0; i < host->n_ports; i++) {
1640 + struct ata_port *ap = host->ports[i];
1642 + port_mmio = ahci_port_base(ap);
1643 + if (ata_port_is_dummy(ap))
1646 + ahci_port_init(pdev, ap, i, mmio, port_mmio);
1649 + tmp = readl(mmio + HOST_CTL);
1650 + VPRINTK("HOST_CTL 0x%x\n", tmp);
1651 + writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1652 + tmp = readl(mmio + HOST_CTL);
1653 + VPRINTK("HOST_CTL 0x%x\n", tmp);
1656 +static void ahci_dev_config(struct ata_device *dev)
1658 + struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1660 + if (hpriv->flags & AHCI_HFLAG_SECT255) {
1661 + dev->max_sectors = 255;
1662 + ata_dev_printk(dev, KERN_INFO,
1663 + "SB600 AHCI: limiting to 255 sectors per cmd\n");
1667 +static unsigned int ahci_dev_classify(struct ata_port *ap)
1669 + void __iomem *port_mmio = ahci_port_base(ap);
1670 + struct ata_taskfile tf;
1673 + tmp = readl(port_mmio + PORT_SIG);
1674 + tf.lbah = (tmp >> 24) & 0xff;
1675 + tf.lbam = (tmp >> 16) & 0xff;
1676 + tf.lbal = (tmp >> 8) & 0xff;
1677 + tf.nsect = (tmp) & 0xff;
1679 + return ata_dev_classify(&tf);
1682 +static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1685 + dma_addr_t cmd_tbl_dma;
1687 + cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1690 + pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1692 + pp->cmd_slot[tag].opts = opts;
1694 + pp->cmd_slot[tag].status = 0;
1696 + pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1697 + pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1699 + pp->cmd_slot[tag].tbl_addr = cmd_tbl_dma & 0xffffffff;
1700 + pp->cmd_slot[tag].tbl_addr_hi = (cmd_tbl_dma >> 16) >> 16;
1704 +static int ahci_kick_engine(struct ata_port *ap, int force_restart)
1706 + void __iomem *port_mmio = ahci_port_base(ap);
1707 + struct ahci_host_priv *hpriv = ap->host->private_data;
1708 + u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1712 + /* do we need to kick the port? */
1713 + busy = status & (ATA_BUSY | ATA_DRQ);
1714 + if (!busy && !force_restart)
1718 + rc = ahci_stop_engine(ap);
1722 + /* need to do CLO? */
1728 + if (!(hpriv->cap & HOST_CAP_CLO)) {
1734 + tmp = readl(port_mmio + PORT_CMD);
1735 + tmp |= PORT_CMD_CLO;
1736 + writel(tmp, port_mmio + PORT_CMD);
1739 + tmp = ata_wait_register(port_mmio + PORT_CMD,
1740 + PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1741 + if (tmp & PORT_CMD_CLO)
1744 + /* restart engine */
1746 + ahci_start_engine(ap);
1750 +static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1751 + struct ata_taskfile *tf, int is_cmd, u16 flags,
1752 + unsigned long timeout_msec)
1754 + const u32 cmd_fis_len = 5; /* five dwords */
1755 + struct ahci_port_priv *pp = ap->private_data;
1756 + void __iomem *port_mmio = ahci_port_base(ap);
1757 + u8 *fis = pp->cmd_tbl;
1760 + /* prep the command */
1761 + ata_tf_to_fis(tf, pmp, is_cmd, fis);
1762 + ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1764 + /* issue & wait */
1765 + writel(1, port_mmio + PORT_CMD_ISSUE);
1767 + if (timeout_msec) {
1768 + tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
1771 + ahci_kick_engine(ap, 1);
1775 + readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1780 +static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1781 + int pmp, unsigned long deadline,
1782 + int (*check_ready)(struct ata_link *link))
1784 + struct ata_port *ap = link->ap;
1785 + struct ahci_host_priv *hpriv = ap->host->private_data;
1786 + const char *reason = NULL;
1787 + unsigned long now, msecs;
1788 + struct ata_taskfile tf;
1791 + DPRINTK("ENTER\n");
1793 + /* prepare for SRST (AHCI-1.1 10.4.1) */
1794 + rc = ahci_kick_engine(ap, 1);
1795 + if (rc && rc != -EOPNOTSUPP)
1796 + ata_link_printk(link, KERN_WARNING,
1797 + "failed to reset engine (errno=%d)\n", rc);
1799 + ata_tf_init(link->device, &tf);
1801 + /* issue the first D2H Register FIS */
1804 + if (time_after(now, deadline))
1805 + msecs = jiffies_to_msecs(deadline - now);
1807 + tf.ctl |= ATA_SRST;
1808 + if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1809 + AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1811 + reason = "1st FIS failed";
1815 + /* spec says at least 5us, but be generous and sleep for 1ms */
1818 + /* issue the second D2H Register FIS */
1819 + tf.ctl &= ~ATA_SRST;
1820 + ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1822 + /* wait for link to become ready */
1823 + rc = ata_wait_after_reset(link, deadline, check_ready);
1824 + if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1826 + * Workaround for cases where link online status can't
1827 + * be trusted. Treat device readiness timeout as link
1830 + ata_link_printk(link, KERN_INFO,
1831 + "device not ready, treating as offline\n");
1832 + *class = ATA_DEV_NONE;
1834 + /* link occupied, -ENODEV too is an error */
1835 + reason = "device not ready";
1838 + *class = ahci_dev_classify(ap);
1840 + DPRINTK("EXIT, class=%u\n", *class);
1844 + ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
1848 +static int ahci_check_ready(struct ata_link *link)
1850 + void __iomem *port_mmio = ahci_port_base(link->ap);
1851 + u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1853 + return ata_check_ready(status);
1856 +static int ahci_softreset(struct ata_link *link, unsigned int *class,
1857 + unsigned long deadline)
1859 + int pmp = sata_srst_pmp(link);
1861 + DPRINTK("ENTER\n");
1863 + return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1866 +static int ahci_sb600_check_ready(struct ata_link *link)
1868 + void __iomem *port_mmio = ahci_port_base(link->ap);
1869 + u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1870 + u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1873 + * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1874 + * which can save timeout delay.
1876 + if (irq_status & PORT_IRQ_BAD_PMP)
1879 + return ata_check_ready(status);
1882 +static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
1883 + unsigned long deadline)
1885 + struct ata_port *ap = link->ap;
1886 + void __iomem *port_mmio = ahci_port_base(ap);
1887 + int pmp = sata_srst_pmp(link);
1891 + DPRINTK("ENTER\n");
1893 + rc = ahci_do_softreset(link, class, pmp, deadline,
1894 + ahci_sb600_check_ready);
1897 + * Soft reset fails on some ATI chips with IPMS set when PMP
1898 + * is enabled but SATA HDD/ODD is connected to SATA port,
1899 + * do soft reset again to port 0.
1902 + irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1903 + if (irq_sts & PORT_IRQ_BAD_PMP) {
1904 + ata_link_printk(link, KERN_WARNING,
1905 + "applying SB600 PMP SRST workaround "
1906 + "and retrying\n");
1907 + rc = ahci_do_softreset(link, class, 0, deadline,
1908 + ahci_check_ready);
1915 +static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1916 + unsigned long deadline)
1918 + const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1919 + struct ata_port *ap = link->ap;
1920 + struct ahci_port_priv *pp = ap->private_data;
1921 + u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1922 + struct ata_taskfile tf;
1926 + DPRINTK("ENTER\n");
1928 + ahci_stop_engine(ap);
1930 + /* clear D2H reception area to properly wait for D2H FIS */
1931 + ata_tf_init(link->device, &tf);
1932 + tf.command = 0x80;
1933 + ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1935 + rc = sata_link_hardreset(link, timing, deadline, &online,
1936 + ahci_check_ready);
1938 + ahci_start_engine(ap);
1941 + *class = ahci_dev_classify(ap);
1943 + DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1947 +static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
1948 + unsigned long deadline)
1950 + struct ata_port *ap = link->ap;
1954 + DPRINTK("ENTER\n");
1956 + ahci_stop_engine(ap);
1958 + rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
1959 + deadline, &online, NULL);
1961 + ahci_start_engine(ap);
1963 + DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1965 + /* vt8251 doesn't clear BSY on signature FIS reception,
1966 + * request follow-up softreset.
1968 + return online ? -EAGAIN : rc;
1972 +static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
1973 + unsigned long deadline)
1975 + struct ata_port *ap = link->ap;
1976 + struct ahci_port_priv *pp = ap->private_data;
1977 + u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1978 + struct ata_taskfile tf;
1982 + ahci_stop_engine(ap);
1984 + /* clear D2H reception area to properly wait for D2H FIS */
1985 + ata_tf_init(link->device, &tf);
1986 + tf.command = 0x80;
1987 + ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1989 + rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
1990 + deadline, &online, NULL);
1992 + ahci_start_engine(ap);
1994 + /* The pseudo configuration device on SIMG4726 attached to
1995 + * ASUS P5W-DH Deluxe doesn't send signature FIS after
1996 + * hardreset if no device is attached to the first downstream
1997 + * port && the pseudo device locks up on SRST w/ PMP==0. To
1998 + * work around this, wait for !BSY only briefly. If BSY isn't
1999 + * cleared, perform CLO and proceed to IDENTIFY (achieved by
2000 + * ATA_LFLAG_NO_SRST and ATA_LFLAG_ASSUME_ATA).
2002 + * Wait for two seconds. Devices attached to downstream port
2003 + * which can't process the following IDENTIFY after this will
2004 + * have to be reset again. For most cases, this should
2005 + * suffice while making probing snappish enough.
2008 + rc = ata_wait_after_reset(link, jiffies + 2 * HZ,
2009 + ahci_check_ready);
2011 + ahci_kick_engine(ap, 0);
2017 +static void ahci_postreset(struct ata_link *link, unsigned int *class)
2019 + struct ata_port *ap = link->ap;
2020 + void __iomem *port_mmio = ahci_port_base(ap);
2023 + ata_std_postreset(link, class);
2025 + /* Make sure port's ATAPI bit is set appropriately */
2026 + new_tmp = tmp = readl(port_mmio + PORT_CMD);
2027 + if (*class == ATA_DEV_ATAPI)
2028 + new_tmp |= PORT_CMD_ATAPI;
2030 + new_tmp &= ~PORT_CMD_ATAPI;
2031 + if (new_tmp != tmp) {
2032 + writel(new_tmp, port_mmio + PORT_CMD);
2033 + readl(port_mmio + PORT_CMD); /* flush */
2037 +static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
2039 + struct scatterlist *sg;
2040 + struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
2043 + VPRINTK("ENTER\n");
2046 + * Next, the S/G list.
2048 + for_each_sg(qc->sg, sg, qc->n_elem, si) {
2049 + dma_addr_t addr = sg_dma_address(sg);
2050 + u32 sg_len = sg_dma_len(sg);
2053 + ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
2054 + ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
2055 + ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
2057 + ahci_sg[si].addr = addr & 0xffffffff;
2058 + ahci_sg[si].addr_hi = (addr >> 16) >> 16;
2059 + ahci_sg[si].flags_size = sg_len - 1;
2066 +static void ahci_qc_prep(struct ata_queued_cmd *qc)
2068 + struct ata_port *ap = qc->ap;
2069 + struct ahci_port_priv *pp = ap->private_data;
2070 + int is_atapi = ata_is_atapi(qc->tf.protocol);
2073 + const u32 cmd_fis_len = 5; /* five dwords */
2074 + unsigned int n_elem;
2077 + * Fill in command table information. First, the header,
2078 + * a SATA Register - Host to Device command FIS.
2080 + cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
2082 + ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
2084 + memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
2085 + memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
2089 + if (qc->flags & ATA_QCFLAG_DMAMAP)
2090 + n_elem = ahci_fill_sg(qc, cmd_tbl);
2093 + * Fill in command slot information.
2095 + opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
2096 + if (qc->tf.flags & ATA_TFLAG_WRITE)
2097 + opts |= AHCI_CMD_WRITE;
2099 + opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
2101 + ahci_fill_cmd_slot(pp, qc->tag, opts);
2104 +static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
2106 + struct ahci_host_priv *hpriv = ap->host->private_data;
2107 + struct ahci_port_priv *pp = ap->private_data;
2108 + struct ata_eh_info *host_ehi = &ap->link.eh_info;
2109 + struct ata_link *link = NULL;
2110 + struct ata_queued_cmd *active_qc;
2111 + struct ata_eh_info *active_ehi;
2114 + /* determine active link */
2115 + ata_for_each_link(link, ap, EDGE)
2116 + if (ata_link_active(link))
2121 + active_qc = ata_qc_from_tag(ap, link->active_tag);
2122 + active_ehi = &link->eh_info;
2124 + /* record irq stat */
2125 + ata_ehi_clear_desc(host_ehi);
2126 + ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
2128 + /* AHCI needs SError cleared; otherwise, it might lock up */
2129 + ahci_scr_read(&ap->link, SCR_ERROR, &serror);
2130 + ahci_scr_write(&ap->link, SCR_ERROR, serror);
2131 + host_ehi->serror |= serror;
2133 + /* some controllers set IRQ_IF_ERR on device errors, ignore it */
2134 + if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
2135 + irq_stat &= ~PORT_IRQ_IF_ERR;
2137 + if (irq_stat & PORT_IRQ_TF_ERR) {
2138 + /* If qc is active, charge it; otherwise, the active
2139 + * link. There's no active qc on NCQ errors. It will
2140 + * be determined by EH by reading log page 10h.
2143 + active_qc->err_mask |= AC_ERR_DEV;
2145 + active_ehi->err_mask |= AC_ERR_DEV;
2147 + if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
2148 + host_ehi->serror &= ~SERR_INTERNAL;
2151 + if (irq_stat & PORT_IRQ_UNK_FIS) {
2152 + u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
2154 + active_ehi->err_mask |= AC_ERR_HSM;
2155 + active_ehi->action |= ATA_EH_RESET;
2156 + ata_ehi_push_desc(active_ehi,
2157 + "unknown FIS %08x %08x %08x %08x" ,
2158 + unk[0], unk[1], unk[2], unk[3]);
2161 + if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
2162 + active_ehi->err_mask |= AC_ERR_HSM;
2163 + active_ehi->action |= ATA_EH_RESET;
2164 + ata_ehi_push_desc(active_ehi, "incorrect PMP");
2167 + if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
2168 + host_ehi->err_mask |= AC_ERR_HOST_BUS;
2169 + host_ehi->action |= ATA_EH_RESET;
2170 + ata_ehi_push_desc(host_ehi, "host bus error");
2173 + if (irq_stat & PORT_IRQ_IF_ERR) {
2174 + host_ehi->err_mask |= AC_ERR_ATA_BUS;
2175 + host_ehi->action |= ATA_EH_RESET;
2176 + ata_ehi_push_desc(host_ehi, "interface fatal error");
2179 + if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
2180 + ata_ehi_hotplugged(host_ehi);
2181 + ata_ehi_push_desc(host_ehi, "%s",
2182 + irq_stat & PORT_IRQ_CONNECT ?
2183 + "connection status changed" : "PHY RDY changed");
2186 + /* okay, let's hand over to EH */
2188 + if (irq_stat & PORT_IRQ_FREEZE)
2189 + ata_port_freeze(ap);
2191 + ata_port_abort(ap);
2194 +static void ahci_port_intr(struct ata_port *ap)
2196 + void __iomem *port_mmio = ahci_port_base(ap);
2197 + struct ata_eh_info *ehi = &ap->link.eh_info;
2198 + struct ahci_port_priv *pp = ap->private_data;
2199 + struct ahci_host_priv *hpriv = ap->host->private_data;
2200 + int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
2201 + u32 status, qc_active;
2204 + status = readl(port_mmio + PORT_IRQ_STAT);
2205 + writel(status, port_mmio + PORT_IRQ_STAT);
2207 + /* ignore BAD_PMP while resetting */
2208 + if (unlikely(resetting))
2209 + status &= ~PORT_IRQ_BAD_PMP;
2211 + /* If we are getting PhyRdy, this is
2212 + * just a power state change, we should
2213 + * clear out this, plus the PhyRdy/Comm
2214 + * Wake bits from Serror
2216 + if ((hpriv->flags & AHCI_HFLAG_NO_HOTPLUG) &&
2217 + (status & PORT_IRQ_PHYRDY)) {
2218 + status &= ~PORT_IRQ_PHYRDY;
2219 + ahci_scr_write(&ap->link, SCR_ERROR, ((1 << 16) | (1 << 18)));
2222 + if (unlikely(status & PORT_IRQ_ERROR)) {
2223 + ahci_error_intr(ap, status);
2227 + if (status & PORT_IRQ_SDB_FIS) {
2228 + /* If SNotification is available, leave notification
2229 + * handling to sata_async_notification(). If not,
2230 + * emulate it by snooping SDB FIS RX area.
2232 + * Snooping FIS RX area is probably cheaper than
2233 + * poking SNotification but some constrollers which
2234 + * implement SNotification, ICH9 for example, don't
2235 + * store AN SDB FIS into receive area.
2237 + if (hpriv->cap & HOST_CAP_SNTF)
2238 + sata_async_notification(ap);
2240 + /* If the 'N' bit in word 0 of the FIS is set,
2241 + * we just received asynchronous notification.
2242 + * Tell libata about it.
2244 + const __le32 *f = pp->rx_fis + RX_FIS_SDB;
2246 + u32 f0 = le32_to_cpu(f[0]);
2251 + if (f0 & (1 << 15))
2252 + sata_async_notification(ap);
2256 + /* pp->active_link is valid iff any command is in flight */
2257 + if (ap->qc_active && pp->active_link->sactive)
2258 + qc_active = readl(port_mmio + PORT_SCR_ACT);
2260 + qc_active = readl(port_mmio + PORT_CMD_ISSUE);
2262 + rc = ata_qc_complete_multiple(ap, qc_active);
2264 + /* while resetting, invalid completions are expected */
2265 + if (unlikely(rc < 0 && !resetting)) {
2266 + ehi->err_mask |= AC_ERR_HSM;
2267 + ehi->action |= ATA_EH_RESET;
2268 + ata_port_freeze(ap);
2272 +static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
2274 + struct ata_host *host = dev_instance;
2275 + struct ahci_host_priv *hpriv;
2276 + unsigned int i, handled = 0;
2277 + void __iomem *mmio;
2278 + u32 irq_stat, irq_masked;
2280 + VPRINTK("ENTER\n");
2282 + hpriv = host->private_data;
2284 + mmio = host->iomap[AHCI_PCI_BAR];
2286 + mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
2289 + /* sigh. 0xffffffff is a valid return from h/w */
2290 + irq_stat = readl(mmio + HOST_IRQ_STAT);
2294 + irq_masked = irq_stat & hpriv->port_map;
2296 + spin_lock(&host->lock);
2298 + for (i = 0; i < host->n_ports; i++) {
2299 + struct ata_port *ap;
2301 + if (!(irq_masked & (1 << i)))
2304 + ap = host->ports[i];
2306 + ahci_port_intr(ap);
2307 + VPRINTK("port %u\n", i);
2309 + VPRINTK("port %u (no irq)\n", i);
2310 + if (ata_ratelimit())
2311 + dev_printk(KERN_WARNING, host->dev,
2312 + "interrupt on disabled port %u\n", i);
2318 + /* HOST_IRQ_STAT behaves as level triggered latch meaning that
2319 + * it should be cleared after all the port events are cleared;
2320 + * otherwise, it will raise a spurious interrupt after each
2321 + * valid one. Please read section 10.6.2 of ahci 1.1 for more
2324 + * Also, use the unmasked value to clear interrupt as spurious
2325 + * pending event on a dummy port might cause screaming IRQ.
2327 + writel(irq_stat, mmio + HOST_IRQ_STAT);
2329 + spin_unlock(&host->lock);
2331 + VPRINTK("EXIT\n");
2333 + return IRQ_RETVAL(handled);
2336 +static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
2338 + struct ata_port *ap = qc->ap;
2339 + void __iomem *port_mmio = ahci_port_base(ap);
2340 + struct ahci_port_priv *pp = ap->private_data;
2342 + /* Keep track of the currently active link. It will be used
2343 + * in completion path to determine whether NCQ phase is in
2346 + pp->active_link = qc->dev->link;
2348 + if (qc->tf.protocol == ATA_PROT_NCQ)
2349 + writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
2350 + writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
2352 + ahci_sw_activity(qc->dev->link);
2357 +static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2359 + struct ahci_port_priv *pp = qc->ap->private_data;
2360 + u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
2362 + ata_tf_from_fis(d2h_fis, &qc->result_tf);
2366 +static void ahci_freeze(struct ata_port *ap)
2368 + void __iomem *port_mmio = ahci_port_base(ap);
2370 + /* turn IRQ off */
2371 + writel(0, port_mmio + PORT_IRQ_MASK);
2374 +static void ahci_thaw(struct ata_port *ap)
2377 + void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
2379 + void __iomem *mmio = (void __iomem *)ap->host->iomap;//[AHCI_BAR];
2381 + void __iomem *port_mmio = ahci_port_base(ap);
2383 + struct ahci_port_priv *pp = ap->private_data;
2386 + tmp = readl(port_mmio + PORT_IRQ_STAT);
2387 + writel(tmp, port_mmio + PORT_IRQ_STAT);
2388 + writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2390 + /* turn IRQ back on */
2391 + writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2394 +static void ahci_error_handler(struct ata_port *ap)
2396 + if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2397 + /* restart engine */
2398 + ahci_stop_engine(ap);
2399 + ahci_start_engine(ap);
2402 + sata_pmp_error_handler(ap);
2405 +static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2407 + struct ata_port *ap = qc->ap;
2409 + /* make DMA engine forget about the failed command */
2410 + if (qc->flags & ATA_QCFLAG_FAILED)
2411 + ahci_kick_engine(ap, 1);
2414 +static void ahci_pmp_attach(struct ata_port *ap)
2416 + void __iomem *port_mmio = ahci_port_base(ap);
2417 + struct ahci_port_priv *pp = ap->private_data;
2420 + cmd = readl(port_mmio + PORT_CMD);
2421 + cmd |= PORT_CMD_PMP;
2422 + writel(cmd, port_mmio + PORT_CMD);
2424 + pp->intr_mask |= PORT_IRQ_BAD_PMP;
2425 + writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2428 +static void ahci_pmp_detach(struct ata_port *ap)
2430 + void __iomem *port_mmio = ahci_port_base(ap);
2431 + struct ahci_port_priv *pp = ap->private_data;
2434 + cmd = readl(port_mmio + PORT_CMD);
2435 + cmd &= ~PORT_CMD_PMP;
2436 + writel(cmd, port_mmio + PORT_CMD);
2438 + pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2439 + writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2442 +static int ahci_port_resume(struct ata_port *ap)
2444 + ahci_power_up(ap);
2445 + ahci_start_port(ap);
2447 + if (sata_pmp_attached(ap))
2448 + ahci_pmp_attach(ap);
2450 + ahci_pmp_detach(ap);
2456 +static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2458 + const char *emsg = NULL;
2461 + rc = ahci_deinit_port(ap, &emsg);
2463 + ahci_power_down(ap);
2465 + ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
2466 + ahci_start_port(ap);
2472 +static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
2474 + struct ata_host *host = dev_get_drvdata(&pdev->dev);
2475 + struct ahci_host_priv *hpriv = host->private_data;
2476 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
2479 + if (mesg.event & PM_EVENT_SUSPEND &&
2480 + hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
2481 + dev_printk(KERN_ERR, &pdev->dev,
2482 + "BIOS update required for suspend/resume\n");
2486 + if (mesg.event & PM_EVENT_SLEEP) {
2487 + /* AHCI spec rev1.1 section 8.3.3:
2488 + * Software must disable interrupts prior to requesting a
2489 + * transition of the HBA to D3 state.
2491 + ctl = readl(mmio + HOST_CTL);
2492 + ctl &= ~HOST_IRQ_EN;
2493 + writel(ctl, mmio + HOST_CTL);
2494 + readl(mmio + HOST_CTL); /* flush */
2497 + return ata_pci_device_suspend(pdev, mesg);
2500 +static int ahci_pci_device_resume(struct pci_dev *pdev)
2502 + struct ata_host *host = dev_get_drvdata(&pdev->dev);
2505 + rc = ata_pci_device_do_resume(pdev);
2509 + if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
2510 + rc = ahci_reset_controller(host);
2514 + ahci_init_controller(host);
2517 + ata_host_resume(host);
2523 +static int ahci_port_start(struct ata_port *ap)
2525 + struct device *dev = ap->host->dev;
2526 + struct ahci_port_priv *pp;
2528 + dma_addr_t mem_dma;
2530 + pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2534 + mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
2538 + memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
2541 + * First item in chunk of DMA memory: 32-slot command table,
2542 + * 32 bytes each in size
2544 + pp->cmd_slot = mem;
2545 + pp->cmd_slot_dma = mem_dma;
2547 + mem += AHCI_CMD_SLOT_SZ;
2548 + mem_dma += AHCI_CMD_SLOT_SZ;
2551 + * Second item: Received-FIS area
2554 + pp->rx_fis_dma = mem_dma;
2556 + mem += AHCI_RX_FIS_SZ;
2557 + mem_dma += AHCI_RX_FIS_SZ;
2560 + * Third item: data area for storing a single command
2561 + * and its scatter-gather table
2563 + pp->cmd_tbl = mem;
2564 + pp->cmd_tbl_dma = mem_dma;
2567 + * Save off initial list of interrupts to be enabled.
2568 + * This could be changed later
2570 + pp->intr_mask = DEF_PORT_IRQ;
2572 + ap->private_data = pp;
2574 + /* engage engines, captain */
2575 + return ahci_port_resume(ap);
2578 +static void ahci_port_stop(struct ata_port *ap)
2580 + const char *emsg = NULL;
2583 + /* de-initialize port */
2584 + rc = ahci_deinit_port(ap, &emsg);
2586 + ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
2590 +static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
2595 + !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
2596 + rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2598 + rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2600 + dev_printk(KERN_ERR, &pdev->dev,
2601 + "64-bit DMA enable failed\n");
2606 + rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2608 + dev_printk(KERN_ERR, &pdev->dev,
2609 + "32-bit DMA enable failed\n");
2612 + rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2614 + dev_printk(KERN_ERR, &pdev->dev,
2615 + "32-bit consistent DMA enable failed\n");
2623 +static void ahci_print_info(struct ata_host *host)
2625 + struct ahci_host_priv *hpriv = host->private_data;
2627 + struct pci_dev *pdev = to_pci_dev(host->dev);
2628 + void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
2630 + struct platform_device *pdev = to_platform_device(host->dev);
2631 + void __iomem *mmio = (void __iomem *)host->iomap;//[AHCI_BAR];
2633 + u32 vers, cap, impl, speed;
2634 + const char *speed_s;
2638 + const char *scc_s;
2640 + vers = readl(mmio + HOST_VERSION);
2642 + impl = hpriv->port_map;
2644 + speed = (cap >> 20) & 0xf;
2647 + else if (speed == 2)
2649 + else if (speed == 3)
2655 + pci_read_config_word(pdev, 0x0a, &cc);
2656 + if (cc == PCI_CLASS_STORAGE_IDE)
2658 + else if (cc == PCI_CLASS_STORAGE_SATA)
2660 + else if (cc == PCI_CLASS_STORAGE_RAID)
2663 + scc_s = "unknown";
2668 + dev_printk(KERN_INFO, &pdev->dev,
2669 + "AHCI %02x%02x.%02x%02x "
2670 + "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2673 + (vers >> 24) & 0xff,
2674 + (vers >> 16) & 0xff,
2675 + (vers >> 8) & 0xff,
2678 + ((cap >> 8) & 0x1f) + 1,
2684 + dev_printk(KERN_INFO, &pdev->dev,
2691 + cap & (1 << 31) ? "64bit " : "",
2692 + cap & (1 << 30) ? "ncq " : "",
2693 + cap & (1 << 29) ? "sntf " : "",
2694 + cap & (1 << 28) ? "ilck " : "",
2695 + cap & (1 << 27) ? "stag " : "",
2696 + cap & (1 << 26) ? "pm " : "",
2697 + cap & (1 << 25) ? "led " : "",
2699 + cap & (1 << 24) ? "clo " : "",
2700 + cap & (1 << 19) ? "nz " : "",
2701 + cap & (1 << 18) ? "only " : "",
2702 + cap & (1 << 17) ? "pmp " : "",
2703 + cap & (1 << 15) ? "pio " : "",
2704 + cap & (1 << 14) ? "slum " : "",
2705 + cap & (1 << 13) ? "part " : "",
2706 + cap & (1 << 6) ? "ems ": ""
2711 +/* On ASUS P5W DH Deluxe, the second port of PCI device 00:1f.2 is
2712 + * hardwired to on-board SIMG 4726. The chipset is ICH8 and doesn't
2713 + * support PMP and the 4726 either directly exports the device
2714 + * attached to the first downstream port or acts as a hardware storage
2715 + * controller and emulate a single ATA device (can be RAID 0/1 or some
2716 + * other configuration).
2718 + * When there's no device attached to the first downstream port of the
2719 + * 4726, "Config Disk" appears, which is a pseudo ATA device to
2720 + * configure the 4726. However, ATA emulation of the device is very
2721 + * lame. It doesn't send signature D2H Reg FIS after the initial
2722 + * hardreset, pukes on SRST w/ PMP==0 and has bunch of other issues.
2724 + * The following function works around the problem by always using
2725 + * hardreset on the port and not depending on receiving signature FIS
2726 + * afterward. If signature FIS isn't received soon, ATA class is
2727 + * assumed without follow-up softreset.
2729 +static void ahci_p5wdh_workaround(struct ata_host *host)
2731 + static struct dmi_system_id sysids[] = {
2733 + .ident = "P5W DH Deluxe",
2735 + DMI_MATCH(DMI_SYS_VENDOR,
2736 + "ASUSTEK COMPUTER INC"),
2737 + DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
2742 + struct pci_dev *pdev = to_pci_dev(host->dev);
2744 + if (pdev->bus->number == 0 && pdev->devfn == PCI_DEVFN(0x1f, 2) &&
2745 + dmi_check_system(sysids)) {
2746 + struct ata_port *ap = host->ports[1];
2748 + dev_printk(KERN_INFO, &pdev->dev, "enabling ASUS P5W DH "
2749 + "Deluxe on-board SIMG4726 workaround\n");
2751 + ap->ops = &ahci_p5wdh_ops;
2752 + ap->link.flags |= ATA_LFLAG_NO_SRST | ATA_LFLAG_ASSUME_ATA;
2757 + * SB600 ahci controller on ASUS M2A-VM can't do 64bit DMA with older
2758 + * BIOS. The oldest version known to be broken is 0901 and working is
2759 + * 1501 which was released on 2007-10-26. Force 32bit DMA on anything
2760 + * older than 1501. Please read bko#9412 for more info.
2762 +static bool ahci_asus_m2a_vm_32bit_only(struct pci_dev *pdev)
2764 + static const struct dmi_system_id sysids[] = {
2766 + .ident = "ASUS M2A-VM",
2768 + DMI_MATCH(DMI_BOARD_VENDOR,
2769 + "ASUSTeK Computer INC."),
2770 + DMI_MATCH(DMI_BOARD_NAME, "M2A-VM"),
2775 + const char *cutoff_mmdd = "10/26";
2779 + if (pdev->bus->number != 0 || pdev->devfn != PCI_DEVFN(0x12, 0) ||
2780 + !dmi_check_system(sysids))
2784 + * Argh.... both version and date are free form strings.
2785 + * Let's hope they're using the same date format across
2786 + * different versions.
2788 + date = dmi_get_system_info(DMI_BIOS_DATE);
2789 + year = dmi_get_year(DMI_BIOS_DATE);
2790 + if (date && strlen(date) >= 10 && date[2] == '/' && date[5] == '/' &&
2792 + (year == 2007 && strncmp(date, cutoff_mmdd, 5) >= 0)))
2795 + dev_printk(KERN_WARNING, &pdev->dev, "ASUS M2A-VM: BIOS too old, "
2796 + "forcing 32bit DMA, update BIOS\n");
2801 +static bool ahci_broken_system_poweroff(struct pci_dev *pdev)
2803 + static const struct dmi_system_id broken_systems[] = {
2805 + .ident = "HP Compaq nx6310",
2807 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2808 + DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6310"),
2810 + /* PCI slot number of the controller */
2811 + .driver_data = (void *)0x1FUL,
2814 + .ident = "HP Compaq 6720s",
2816 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2817 + DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 6720s"),
2819 + /* PCI slot number of the controller */
2820 + .driver_data = (void *)0x1FUL,
2823 + { } /* terminate list */
2825 + const struct dmi_system_id *dmi = dmi_first_match(broken_systems);
2828 + unsigned long slot = (unsigned long)dmi->driver_data;
2829 + /* apply the quirk only to on-board controllers */
2830 + return slot == PCI_SLOT(pdev->devfn);
2836 +static bool ahci_broken_suspend(struct pci_dev *pdev)
2838 + static const struct dmi_system_id sysids[] = {
2840 + * On HP dv[4-6] and HDX18 with earlier BIOSen, link
2841 + * to the harddisk doesn't become online after
2842 + * resuming from STR. Warn and fail suspend.
2847 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2848 + DMI_MATCH(DMI_PRODUCT_NAME,
2849 + "HP Pavilion dv4 Notebook PC"),
2851 + .driver_data = "F.30", /* cutoff BIOS version */
2856 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2857 + DMI_MATCH(DMI_PRODUCT_NAME,
2858 + "HP Pavilion dv5 Notebook PC"),
2860 + .driver_data = "F.16", /* cutoff BIOS version */
2865 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2866 + DMI_MATCH(DMI_PRODUCT_NAME,
2867 + "HP Pavilion dv6 Notebook PC"),
2869 + .driver_data = "F.21", /* cutoff BIOS version */
2874 + DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2875 + DMI_MATCH(DMI_PRODUCT_NAME,
2876 + "HP HDX18 Notebook PC"),
2878 + .driver_data = "F.23", /* cutoff BIOS version */
2880 + { } /* terminate list */
2882 + const struct dmi_system_id *dmi = dmi_first_match(sysids);
2885 + if (!dmi || pdev->bus->number || pdev->devfn != PCI_DEVFN(0x1f, 2))
2888 + ver = dmi_get_system_info(DMI_BIOS_VERSION);
2890 + return !ver || strcmp(ver, dmi->driver_data) < 0;
2893 +static bool ahci_broken_online(struct pci_dev *pdev)
2895 +#define ENCODE_BUSDEVFN(bus, slot, func) \
2896 + (void *)(unsigned long)(((bus) << 8) | PCI_DEVFN((slot), (func)))
2897 + static const struct dmi_system_id sysids[] = {
2899 + * There are several gigabyte boards which use
2900 + * SIMG5723s configured as hardware RAID. Certain
2901 + * 5723 firmware revisions shipped there keep the link
2902 + * online but fail to answer properly to SRST or
2903 + * IDENTIFY when no device is attached downstream
2904 + * causing libata to retry quite a few times leading
2905 + * to excessive detection delay.
2907 + * As these firmwares respond to the second reset try
2908 + * with invalid device signature, considering unknown
2909 + * sig as offline works around the problem acceptably.
2912 + .ident = "EP45-DQ6",
2914 + DMI_MATCH(DMI_BOARD_VENDOR,
2915 + "Gigabyte Technology Co., Ltd."),
2916 + DMI_MATCH(DMI_BOARD_NAME, "EP45-DQ6"),
2918 + .driver_data = ENCODE_BUSDEVFN(0x0a, 0x00, 0),
2921 + .ident = "EP45-DS5",
2923 + DMI_MATCH(DMI_BOARD_VENDOR,
2924 + "Gigabyte Technology Co., Ltd."),
2925 + DMI_MATCH(DMI_BOARD_NAME, "EP45-DS5"),
2927 + .driver_data = ENCODE_BUSDEVFN(0x03, 0x00, 0),
2929 + { } /* terminate list */
2931 +#undef ENCODE_BUSDEVFN
2932 + const struct dmi_system_id *dmi = dmi_first_match(sysids);
2938 + val = (unsigned long)dmi->driver_data;
2940 + return pdev->bus->number == (val >> 8) && pdev->devfn == (val & 0xff);
2944 +static int ahci_remove(struct platform_device *pdev)
2946 + struct device *dev = &pdev->dev;
2947 + struct ata_host *host = dev_get_drvdata(dev);
2949 + ata_host_detach(host);
2954 +static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2956 +static int __init ahci_probe(struct platform_device *pdev)
2959 + static int printed_version;
2961 + unsigned int board_id = ent->driver_data;
2962 + struct ata_port_info pi = ahci_port_info[board_id];
2964 + struct ata_port_info pi = ahci_port_info[board_ahci];
2966 + const struct ata_port_info *ppi[] = { &pi, NULL };
2967 + struct device *dev = &pdev->dev;
2968 + struct ahci_host_priv *hpriv;
2969 + struct ata_host *host;
2970 + int n_ports, i, rc;
2971 + struct resource *res;
2974 + VPRINTK("ENTER\n");
2976 + WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
2978 + if (!printed_version++)
2979 + dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
2982 + /* The AHCI driver can only drive the SATA ports, the PATA driver
2983 + can drive them all so if both drivers are selected make sure
2984 + AHCI stays out of the way */
2985 + if (pdev->vendor == PCI_VENDOR_ID_MARVELL && !marvell_enable)
2988 + /* acquire resources */
2989 + rc = pcim_enable_device(pdev);
2993 + /* AHCI controllers often implement SFF compatible interface.
2994 + * Grab all PCI BARs just in case.
2996 + rc = pcim_iomap_regions_request_all(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
2998 + pcim_pin_device(pdev);
3002 + if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
3003 + (pdev->device == 0x2652 || pdev->device == 0x2653)) {
3006 + /* ICH6s share the same PCI ID for both piix and ahci
3007 + * modes. Enabling ahci mode while MAP indicates
3008 + * combined mode is a bad idea. Yield to ata_piix.
3010 + pci_read_config_byte(pdev, ICH_MAP, &map);
3012 + dev_printk(KERN_INFO, &pdev->dev, "controller is in "
3013 + "combined mode, can't enable AHCI mode\n");
3019 + hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
3022 + hpriv->flags |= (unsigned long)pi.private_data;
3025 + /* MCP65 revision A1 and A2 can't do MSI */
3026 + if (board_id == board_ahci_mcp65 &&
3027 + (pdev->revision == 0xa1 || pdev->revision == 0xa2))
3028 + hpriv->flags |= AHCI_HFLAG_NO_MSI;
3030 + /* SB800 does NOT need the workaround to ignore SERR_INTERNAL */
3031 + if (board_id == board_ahci_sb700 && pdev->revision >= 0x40)
3032 + hpriv->flags &= ~AHCI_HFLAG_IGN_SERR_INTERNAL;
3034 + /* apply ASUS M2A_VM quirk */
3035 + if (ahci_asus_m2a_vm_32bit_only(pdev))
3036 + hpriv->flags |= AHCI_HFLAG_32BIT_ONLY;
3038 + if (!(hpriv->flags & AHCI_HFLAG_NO_MSI))
3039 + pci_enable_msi(pdev);
3042 + /* Cavium CNS3XXX Initial */
3043 + /* Get SATA register base address */
3044 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3046 + dev_err(&pdev->dev, "no reg addr\n");
3050 + /* ioremap SATA registers */
3051 + base = devm_ioremap(&pdev->dev, res->start, res->end - res->start + 1);
3054 + dev_err(&pdev->dev, "ioremap failed for 0x%x\n", res->start);
3059 + /* reset PHY test chip */
3060 + printk("*** Reset PHY ***\n");
3061 + CNS3XXX_MISC_REGISTER |= 0xF;
3064 + printk("%s %d, base:0x%x\n",__FUNCTION__,__LINE__,(u32)base);
3066 + /* set PI first */
3067 + printk("*** Manually set PI ***\n");
3068 + writel(0x1, (void __iomem *)base + HOST_PORTS_IMPL);
3069 + printk("*** Now PI is: 0x%x ***\n",readl((void __iomem *)base + HOST_PORTS_IMPL));
3075 + /* save initial config */
3077 + ahci_save_initial_config(pdev, hpriv);
3079 + ahci_save_initial_config(pdev, hpriv, base);
3082 + /* prepare host */
3083 + if (hpriv->cap & HOST_CAP_NCQ)
3084 + pi.flags |= ATA_FLAG_NCQ;
3086 + if (hpriv->cap & HOST_CAP_PMP)
3087 + pi.flags |= ATA_FLAG_PMP;
3089 + if (ahci_em_messages && (hpriv->cap & HOST_CAP_EMS)) {
3092 + void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
3094 + void __iomem *mmio = (void __iomem *)base;
3096 + u32 em_loc = readl(mmio + HOST_EM_LOC);
3097 + u32 em_ctl = readl(mmio + HOST_EM_CTL);
3099 + messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
3101 + /* we only support LED message type right now */
3102 + if ((messages & 0x01) && (ahci_em_messages == 1)) {
3103 + /* store em_loc */
3104 + hpriv->em_loc = ((em_loc >> 16) * 4);
3105 + pi.flags |= ATA_FLAG_EM;
3106 + if (!(em_ctl & EM_CTL_ALHD))
3107 + pi.flags |= ATA_FLAG_SW_ACTIVITY;
3112 + if (ahci_broken_system_poweroff(pdev)) {
3113 + pi.flags |= ATA_FLAG_NO_POWEROFF_SPINDOWN;
3114 + dev_info(&pdev->dev,
3115 + "quirky BIOS, skipping spindown on poweroff\n");
3118 + if (ahci_broken_suspend(pdev)) {
3119 + hpriv->flags |= AHCI_HFLAG_NO_SUSPEND;
3120 + dev_printk(KERN_WARNING, &pdev->dev,
3121 + "BIOS update required for suspend/resume\n");
3124 + if (ahci_broken_online(pdev)) {
3125 + hpriv->flags |= AHCI_HFLAG_SRST_TOUT_IS_OFFLINE;
3126 + dev_info(&pdev->dev,
3127 + "online status unreliable, applying workaround\n");
3131 + /* CAP.NP sometimes indicate the index of the last enabled
3132 + * port, at other times, that of the last possible port, so
3133 + * determining the maximum port number requires looking at
3134 + * both CAP.NP and port_map.
3136 + n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
3138 + host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3142 + host->iomap = pcim_iomap_table(pdev);
3144 + host->iomap = (void __iomem *)base;
3146 + host->private_data = hpriv;
3148 + if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
3149 + host->flags |= ATA_HOST_PARALLEL_SCAN;
3151 + printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
3153 + if (pi.flags & ATA_FLAG_EM)
3154 + ahci_reset_em(host);
3156 + for (i = 0; i < host->n_ports; i++) {
3157 + struct ata_port *ap = host->ports[i];
3160 + ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
3161 + ata_port_pbar_desc(ap, AHCI_PCI_BAR,
3162 + 0x100 + ap->port_no * 0x80, "port");
3164 + ata_port_desc(ap, "%s %s%llu@0x%llx", "ahci bar", "m",
3165 + (long long)(res->end - res->start) + 1, (long long)res->start);
3166 + ata_port_desc(ap, "%s 0x%llx", "port",
3167 + (long long)res->start + 0x100 + ap->port_no * 0x80);
3170 + /* set initial link pm policy */
3171 + ap->pm_policy = NOT_AVAILABLE;
3173 + /* set enclosure management message type */
3174 + if (ap->flags & ATA_FLAG_EM)
3175 + ap->em_message_type = ahci_em_messages;
3178 + /* disabled/not-implemented port */
3179 + if (!(hpriv->port_map & (1 << i)))
3180 + ap->ops = &ata_dummy_port_ops;
3184 + /* apply workaround for ASUS P5W DH Deluxe mainboard */
3185 + ahci_p5wdh_workaround(host);
3187 + /* initialize adapter */
3188 + rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
3193 + rc = ahci_reset_controller(host);
3197 + ahci_init_controller(host);
3198 + ahci_print_info(host);
3201 + pci_set_master(pdev);
3207 + return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
3210 + /* Get SATA port interrupt number */
3211 + res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3213 + dev_err(&pdev->dev, "no SATA irq\n");
3217 + return ata_host_activate(host, res->start, ahci_interrupt, IRQF_SHARED,
3225 +#if defined(CONFIG_CNS3XXX_SILICON) || defined(CONFIG_SILICON)
3226 +static void ahci_phy_init(void){
3231 + u32tmp = MISC_SATA_POWER_MODE;
3232 + u32tmp |= 0x1<< 16; // Disable SATA PHY 0 from SLUMBER Mode
3233 + u32tmp |= 0x1<< 17; // Disable SATA PHY 1 from SLUMBER Mode
3234 + MISC_SATA_POWER_MODE = u32tmp;
3236 + /* Enable SATA PHY */
3237 + cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY0);
3238 + cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY1);
3240 + /* Enable SATA Clock */
3241 + cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_SATA);
3243 + /* De-Asscer SATA Reset */
3244 + u32tmp = PM_SOFT_RST_REG;
3245 + u32tmp |= 0x1 << PM_SOFT_RST_REG_OFFST_SATA;
3246 + PM_SOFT_RST_REG = u32tmp;
3252 +static int __init ahci_init(void)
3255 + return pci_register_driver(&ahci_pci_driver);
3257 + printk("CNS3XXX AHCI SATA low-level driver\n");
3258 +#if defined(CONFIG_CNS3XXX_SILICON) || defined(CONFIG_SILICON)
3261 + return platform_driver_register(&ahci_driver);
3265 +static void __exit ahci_exit(void)
3268 + pci_unregister_driver(&ahci_pci_driver);
3270 + platform_driver_unregister(&ahci_driver);
3275 +MODULE_AUTHOR("Jeff Garzik");
3276 +MODULE_DESCRIPTION("AHCI SATA low-level driver");
3277 +MODULE_LICENSE("GPL");
3279 +MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
3281 +MODULE_VERSION(DRV_VERSION);
3283 +module_init(ahci_init);
3284 +module_exit(ahci_exit);
3285 --- a/drivers/ata/Kconfig
3286 +++ b/drivers/ata/Kconfig
3287 @@ -47,6 +47,14 @@ config SATA_PMP
3288 This option adds support for SATA Port Multipliers
3289 (the SATA version of an ethernet hub, or SAS expander).
3291 +config SATA_CNS3XXX_AHCI
3292 + tristate "Cavium CNS3XXX AHCI SATA support"
3294 + This option enables support for AHCI Serial ATA support for Cavium CNS3XXX.
3300 tristate "AHCI SATA support"
3302 --- a/drivers/ata/libata-scsi.c
3303 +++ b/drivers/ata/libata-scsi.c
3304 @@ -3096,12 +3096,22 @@ int ata_scsi_queuecmd(struct scsi_cmnd *
3305 struct scsi_device *scsidev = cmd->device;
3306 struct Scsi_Host *shost = scsidev->host;
3311 + local_save_flags(flags);
3313 ap = ata_shost_to_port(shost);
3315 spin_unlock(shost->host_lock);
3317 spin_lock(ap->lock);
3320 + while(!spin_trylock(ap->lock)){
3321 + if(!irqs_disabled()) continue;
3322 + local_irq_enable();
3323 + local_irq_restore(flags);
3326 ata_scsi_dump_cdb(ap, cmd);
3328 dev = ata_scsi_find_dev(ap, scsidev);
3329 --- a/drivers/ata/libata-sff.c
3330 +++ b/drivers/ata/libata-sff.c
3331 @@ -893,6 +893,9 @@ static void ata_pio_sector(struct ata_qu
3336 + flush_dcache_page(page);
3338 qc->curbytes += qc->sect_size;
3339 qc->cursg_ofs += qc->sect_size;
3341 --- a/drivers/ata/Makefile
3342 +++ b/drivers/ata/Makefile
3345 obj-$(CONFIG_ATA) += libata.o
3347 +obj-$(CONFIG_SATA_CNS3XXX_AHCI) += cns3xxx_ahci.o
3348 obj-$(CONFIG_SATA_AHCI) += ahci.o
3349 obj-$(CONFIG_SATA_SVW) += sata_svw.o
3350 obj-$(CONFIG_ATA_PIIX) += ata_piix.o