blob: 9f47d0022453a0809990c8ada1a2f5ac1ccfa239 [file] [log] [blame]
Tejun Heo1fd7a692007-01-03 17:32:45 +09001/*
2 * sata_inic162x.c - Driver for Initio 162x SATA controllers
3 *
4 * Copyright 2006 SUSE Linux Products GmbH
5 * Copyright 2006 Tejun Heo <teheo@novell.com>
6 *
7 * This file is released under GPL v2.
8 *
9 * This controller is eccentric and easily locks up if something isn't
10 * right. Documentation is available at initio's website but it only
11 * documents registers (not programming model).
12 *
13 * - ATA disks work.
14 * - Hotplug works.
15 * - ATAPI read works but burning doesn't. This thing is really
16 * peculiar about ATAPI and I couldn't figure out how ATAPI PIO and
17 * ATAPI DMA WRITE should be programmed. If you've got a clue, be
18 * my guest.
19 * - Both STR and STD work.
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <scsi/scsi_host.h>
26#include <linux/libata.h>
27#include <linux/blkdev.h>
28#include <scsi/scsi_device.h>
29
30#define DRV_NAME "sata_inic162x"
Jeff Garzik2a3103c2007-08-31 04:54:06 -040031#define DRV_VERSION "0.3"
Tejun Heo1fd7a692007-01-03 17:32:45 +090032
33enum {
34 MMIO_BAR = 5,
35
36 NR_PORTS = 2,
37
38 HOST_CTL = 0x7c,
39 HOST_STAT = 0x7e,
40 HOST_IRQ_STAT = 0xbc,
41 HOST_IRQ_MASK = 0xbe,
42
43 PORT_SIZE = 0x40,
44
45 /* registers for ATA TF operation */
46 PORT_TF = 0x00,
47 PORT_ALT_STAT = 0x08,
48 PORT_IRQ_STAT = 0x09,
49 PORT_IRQ_MASK = 0x0a,
50 PORT_PRD_CTL = 0x0b,
51 PORT_PRD_ADDR = 0x0c,
52 PORT_PRD_XFERLEN = 0x10,
53
54 /* IDMA register */
55 PORT_IDMA_CTL = 0x14,
56
57 PORT_SCR = 0x20,
58
59 /* HOST_CTL bits */
60 HCTL_IRQOFF = (1 << 8), /* global IRQ off */
61 HCTL_PWRDWN = (1 << 13), /* power down PHYs */
62 HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */
63 HCTL_RPGSEL = (1 << 15), /* register page select */
64
65 HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
66 HCTL_RPGSEL,
67
68 /* HOST_IRQ_(STAT|MASK) bits */
69 HIRQ_PORT0 = (1 << 0),
70 HIRQ_PORT1 = (1 << 1),
71 HIRQ_SOFT = (1 << 14),
72 HIRQ_GLOBAL = (1 << 15), /* STAT only */
73
74 /* PORT_IRQ_(STAT|MASK) bits */
75 PIRQ_OFFLINE = (1 << 0), /* device unplugged */
76 PIRQ_ONLINE = (1 << 1), /* device plugged */
77 PIRQ_COMPLETE = (1 << 2), /* completion interrupt */
78 PIRQ_FATAL = (1 << 3), /* fatal error */
79 PIRQ_ATA = (1 << 4), /* ATA interrupt */
80 PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */
81 PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */
82
83 PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
84
85 PIRQ_MASK_DMA_READ = PIRQ_REPLY | PIRQ_ATA,
86 PIRQ_MASK_OTHER = PIRQ_REPLY | PIRQ_COMPLETE,
87 PIRQ_MASK_FREEZE = 0xff,
88
89 /* PORT_PRD_CTL bits */
90 PRD_CTL_START = (1 << 0),
91 PRD_CTL_WR = (1 << 3),
92 PRD_CTL_DMAEN = (1 << 7), /* DMA enable */
93
94 /* PORT_IDMA_CTL bits */
95 IDMA_CTL_RST_ATA = (1 << 2), /* hardreset ATA bus */
96 IDMA_CTL_RST_IDMA = (1 << 5), /* reset IDMA machinary */
97 IDMA_CTL_GO = (1 << 7), /* IDMA mode go */
98 IDMA_CTL_ATA_NIEN = (1 << 8), /* ATA IRQ disable */
99};
100
101struct inic_host_priv {
102 u16 cached_hctl;
103};
104
105struct inic_port_priv {
106 u8 dfl_prdctl;
107 u8 cached_prdctl;
108 u8 cached_pirq_mask;
109};
110
Tejun Heo1fd7a692007-01-03 17:32:45 +0900111static struct scsi_host_template inic_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900112 ATA_BMDMA_SHT(DRV_NAME),
Tejun Heo1fd7a692007-01-03 17:32:45 +0900113};
114
115static const int scr_map[] = {
116 [SCR_STATUS] = 0,
117 [SCR_ERROR] = 1,
118 [SCR_CONTROL] = 2,
119};
120
Jeff Garzik5796d1c2007-10-26 00:03:37 -0400121static void __iomem *inic_port_base(struct ata_port *ap)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900122{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900123 return ap->host->iomap[MMIO_BAR] + ap->port_no * PORT_SIZE;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900124}
125
126static void __inic_set_pirq_mask(struct ata_port *ap, u8 mask)
127{
128 void __iomem *port_base = inic_port_base(ap);
129 struct inic_port_priv *pp = ap->private_data;
130
131 writeb(mask, port_base + PORT_IRQ_MASK);
132 pp->cached_pirq_mask = mask;
133}
134
135static void inic_set_pirq_mask(struct ata_port *ap, u8 mask)
136{
137 struct inic_port_priv *pp = ap->private_data;
138
139 if (pp->cached_pirq_mask != mask)
140 __inic_set_pirq_mask(ap, mask);
141}
142
143static void inic_reset_port(void __iomem *port_base)
144{
145 void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
146 u16 ctl;
147
148 ctl = readw(idma_ctl);
149 ctl &= ~(IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN | IDMA_CTL_GO);
150
151 /* mask IRQ and assert reset */
152 writew(ctl | IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN, idma_ctl);
153 readw(idma_ctl); /* flush */
154
155 /* give it some time */
156 msleep(1);
157
158 /* release reset */
159 writew(ctl | IDMA_CTL_ATA_NIEN, idma_ctl);
160
161 /* clear irq */
162 writeb(0xff, port_base + PORT_IRQ_STAT);
163
164 /* reenable ATA IRQ, turn off IDMA mode */
165 writew(ctl, idma_ctl);
166}
167
Tejun Heoda3dbb12007-07-16 14:29:40 +0900168static int inic_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900169{
Jeff Garzik59f99882007-05-28 07:07:20 -0400170 void __iomem *scr_addr = ap->ioaddr.scr_addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900171 void __iomem *addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900172
173 if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
Tejun Heoda3dbb12007-07-16 14:29:40 +0900174 return -EINVAL;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900175
176 addr = scr_addr + scr_map[sc_reg] * 4;
Tejun Heoda3dbb12007-07-16 14:29:40 +0900177 *val = readl(scr_addr + scr_map[sc_reg] * 4);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900178
179 /* this controller has stuck DIAG.N, ignore it */
180 if (sc_reg == SCR_ERROR)
Tejun Heoda3dbb12007-07-16 14:29:40 +0900181 *val &= ~SERR_PHYRDY_CHG;
182 return 0;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900183}
184
Tejun Heoda3dbb12007-07-16 14:29:40 +0900185static int inic_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900186{
Jeff Garzik59f99882007-05-28 07:07:20 -0400187 void __iomem *scr_addr = ap->ioaddr.scr_addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900188 void __iomem *addr;
189
190 if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
Tejun Heoda3dbb12007-07-16 14:29:40 +0900191 return -EINVAL;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900192
193 addr = scr_addr + scr_map[sc_reg] * 4;
194 writel(val, scr_addr + scr_map[sc_reg] * 4);
Tejun Heoda3dbb12007-07-16 14:29:40 +0900195 return 0;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900196}
197
198/*
199 * In TF mode, inic162x is very similar to SFF device. TF registers
200 * function the same. DMA engine behaves similary using the same PRD
201 * format as BMDMA but different command register, interrupt and event
202 * notification methods are used. The following inic_bmdma_*()
203 * functions do the impedance matching.
204 */
205static void inic_bmdma_setup(struct ata_queued_cmd *qc)
206{
207 struct ata_port *ap = qc->ap;
208 struct inic_port_priv *pp = ap->private_data;
209 void __iomem *port_base = inic_port_base(ap);
210 int rw = qc->tf.flags & ATA_TFLAG_WRITE;
211
212 /* make sure device sees PRD table writes */
213 wmb();
214
215 /* load transfer length */
216 writel(qc->nbytes, port_base + PORT_PRD_XFERLEN);
217
218 /* turn on DMA and specify data direction */
219 pp->cached_prdctl = pp->dfl_prdctl | PRD_CTL_DMAEN;
220 if (!rw)
221 pp->cached_prdctl |= PRD_CTL_WR;
222 writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
223
224 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +0900225 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900226}
227
228static void inic_bmdma_start(struct ata_queued_cmd *qc)
229{
230 struct ata_port *ap = qc->ap;
231 struct inic_port_priv *pp = ap->private_data;
232 void __iomem *port_base = inic_port_base(ap);
233
234 /* start host DMA transaction */
235 pp->cached_prdctl |= PRD_CTL_START;
236 writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
237}
238
239static void inic_bmdma_stop(struct ata_queued_cmd *qc)
240{
241 struct ata_port *ap = qc->ap;
242 struct inic_port_priv *pp = ap->private_data;
243 void __iomem *port_base = inic_port_base(ap);
244
245 /* stop DMA engine */
246 writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
247}
248
249static u8 inic_bmdma_status(struct ata_port *ap)
250{
251 /* event is already verified by the interrupt handler */
252 return ATA_DMA_INTR;
253}
254
Tejun Heo1fd7a692007-01-03 17:32:45 +0900255static void inic_host_intr(struct ata_port *ap)
256{
257 void __iomem *port_base = inic_port_base(ap);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900258 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900259 u8 irq_stat;
260
261 /* fetch and clear irq */
262 irq_stat = readb(port_base + PORT_IRQ_STAT);
263 writeb(irq_stat, port_base + PORT_IRQ_STAT);
264
265 if (likely(!(irq_stat & PIRQ_ERR))) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900266 struct ata_queued_cmd *qc =
267 ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900268
269 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900270 ap->ops->sff_check_status(ap); /* clear ATA interrupt */
Tejun Heo1fd7a692007-01-03 17:32:45 +0900271 return;
272 }
273
Tejun Heo9363c382008-04-07 22:47:16 +0900274 if (likely(ata_sff_host_intr(ap, qc)))
Tejun Heo1fd7a692007-01-03 17:32:45 +0900275 return;
276
Tejun Heo5682ed32008-04-07 22:47:16 +0900277 ap->ops->sff_check_status(ap); /* clear ATA interrupt */
Tejun Heo1fd7a692007-01-03 17:32:45 +0900278 ata_port_printk(ap, KERN_WARNING, "unhandled "
279 "interrupt, irq_stat=%x\n", irq_stat);
280 return;
281 }
282
283 /* error */
284 ata_ehi_push_desc(ehi, "irq_stat=0x%x", irq_stat);
285
286 if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
287 ata_ehi_hotplugged(ehi);
288 ata_port_freeze(ap);
289 } else
290 ata_port_abort(ap);
291}
292
293static irqreturn_t inic_interrupt(int irq, void *dev_instance)
294{
295 struct ata_host *host = dev_instance;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900296 void __iomem *mmio_base = host->iomap[MMIO_BAR];
Tejun Heo1fd7a692007-01-03 17:32:45 +0900297 u16 host_irq_stat;
298 int i, handled = 0;;
299
300 host_irq_stat = readw(mmio_base + HOST_IRQ_STAT);
301
302 if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
303 goto out;
304
305 spin_lock(&host->lock);
306
307 for (i = 0; i < NR_PORTS; i++) {
308 struct ata_port *ap = host->ports[i];
309
310 if (!(host_irq_stat & (HIRQ_PORT0 << i)))
311 continue;
312
313 if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
314 inic_host_intr(ap);
315 handled++;
316 } else {
317 if (ata_ratelimit())
318 dev_printk(KERN_ERR, host->dev, "interrupt "
319 "from disabled port %d (0x%x)\n",
320 i, host_irq_stat);
321 }
322 }
323
324 spin_unlock(&host->lock);
325
326 out:
327 return IRQ_RETVAL(handled);
328}
329
330static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
331{
332 struct ata_port *ap = qc->ap;
333
334 /* ATA IRQ doesn't wait for DMA transfer completion and vice
335 * versa. Mask IRQ selectively to detect command completion.
336 * Without it, ATA DMA read command can cause data corruption.
337 *
338 * Something similar might be needed for ATAPI writes. I
339 * tried a lot of combinations but couldn't find the solution.
340 */
341 if (qc->tf.protocol == ATA_PROT_DMA &&
342 !(qc->tf.flags & ATA_TFLAG_WRITE))
343 inic_set_pirq_mask(ap, PIRQ_MASK_DMA_READ);
344 else
345 inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
346
347 /* Issuing a command to yet uninitialized port locks up the
348 * controller. Most of the time, this happens for the first
349 * command after reset which are ATA and ATAPI IDENTIFYs.
350 * Fast fail if stat is 0x7f or 0xff for those commands.
351 */
352 if (unlikely(qc->tf.command == ATA_CMD_ID_ATA ||
353 qc->tf.command == ATA_CMD_ID_ATAPI)) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900354 u8 stat = ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900355 if (stat == 0x7f || stat == 0xff)
356 return AC_ERR_HSM;
357 }
358
Tejun Heo9363c382008-04-07 22:47:16 +0900359 return ata_sff_qc_issue(qc);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900360}
361
362static void inic_freeze(struct ata_port *ap)
363{
364 void __iomem *port_base = inic_port_base(ap);
365
366 __inic_set_pirq_mask(ap, PIRQ_MASK_FREEZE);
367
Tejun Heo5682ed32008-04-07 22:47:16 +0900368 ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900369 writeb(0xff, port_base + PORT_IRQ_STAT);
370
371 readb(port_base + PORT_IRQ_STAT); /* flush */
372}
373
374static void inic_thaw(struct ata_port *ap)
375{
376 void __iomem *port_base = inic_port_base(ap);
377
Tejun Heo5682ed32008-04-07 22:47:16 +0900378 ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900379 writeb(0xff, port_base + PORT_IRQ_STAT);
380
381 __inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
382
383 readb(port_base + PORT_IRQ_STAT); /* flush */
384}
385
386/*
387 * SRST and SControl hardreset don't give valid signature on this
388 * controller. Only controller specific hardreset mechanism works.
389 */
Tejun Heocc0680a2007-08-06 18:36:23 +0900390static int inic_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +0900391 unsigned long deadline)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900392{
Tejun Heocc0680a2007-08-06 18:36:23 +0900393 struct ata_port *ap = link->ap;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900394 void __iomem *port_base = inic_port_base(ap);
395 void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
Tejun Heocc0680a2007-08-06 18:36:23 +0900396 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900397 u16 val;
398 int rc;
399
400 /* hammer it into sane state */
401 inic_reset_port(port_base);
402
Tejun Heo1fd7a692007-01-03 17:32:45 +0900403 val = readw(idma_ctl);
404 writew(val | IDMA_CTL_RST_ATA, idma_ctl);
405 readw(idma_ctl); /* flush */
406 msleep(1);
407 writew(val & ~IDMA_CTL_RST_ATA, idma_ctl);
408
Tejun Heocc0680a2007-08-06 18:36:23 +0900409 rc = sata_link_resume(link, timing, deadline);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900410 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +0900411 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heofe334602007-02-02 15:29:52 +0900412 "link after reset (errno=%d)\n", rc);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900413 return rc;
414 }
415
Tejun Heo1fd7a692007-01-03 17:32:45 +0900416 *class = ATA_DEV_NONE;
Tejun Heocc0680a2007-08-06 18:36:23 +0900417 if (ata_link_online(link)) {
Tejun Heo1fd7a692007-01-03 17:32:45 +0900418 struct ata_taskfile tf;
419
Tejun Heo705e76b2008-04-07 22:47:19 +0900420 /* wait for link to become ready */
421 rc = ata_sff_wait_after_reset(link, 1, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +0900422 /* link occupied, -ENODEV too is an error */
423 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +0900424 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heod4b2bab2007-02-02 16:50:52 +0900425 "after hardreset (errno=%d)\n", rc);
426 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900427 }
428
Tejun Heo9363c382008-04-07 22:47:16 +0900429 ata_sff_tf_read(ap, &tf);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900430 *class = ata_dev_classify(&tf);
431 if (*class == ATA_DEV_UNKNOWN)
432 *class = ATA_DEV_NONE;
433 }
434
435 return 0;
436}
437
438static void inic_error_handler(struct ata_port *ap)
439{
440 void __iomem *port_base = inic_port_base(ap);
441 struct inic_port_priv *pp = ap->private_data;
442 unsigned long flags;
443
444 /* reset PIO HSM and stop DMA engine */
445 inic_reset_port(port_base);
446
447 spin_lock_irqsave(ap->lock, flags);
448 ap->hsm_task_state = HSM_ST_IDLE;
449 writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
450 spin_unlock_irqrestore(ap->lock, flags);
451
452 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900453 ata_std_error_handler(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900454}
455
456static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
457{
458 /* make DMA engine forget about the failed command */
Tejun Heoa51d6442007-03-20 15:24:11 +0900459 if (qc->flags & ATA_QCFLAG_FAILED)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900460 inic_reset_port(inic_port_base(qc->ap));
461}
462
Alancd0d3bb2007-03-02 00:56:15 +0000463static void inic_dev_config(struct ata_device *dev)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900464{
465 /* inic can only handle upto LBA28 max sectors */
466 if (dev->max_sectors > ATA_MAX_SECTORS)
467 dev->max_sectors = ATA_MAX_SECTORS;
Tejun Heo90c93782007-06-29 11:33:08 +0900468
469 if (dev->n_sectors >= 1 << 28) {
470 ata_dev_printk(dev, KERN_ERR,
471 "ERROR: This driver doesn't support LBA48 yet and may cause\n"
472 " data corruption on such devices. Disabling.\n");
473 ata_dev_disable(dev);
474 }
Tejun Heo1fd7a692007-01-03 17:32:45 +0900475}
476
477static void init_port(struct ata_port *ap)
478{
479 void __iomem *port_base = inic_port_base(ap);
480
481 /* Setup PRD address */
482 writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
483}
484
485static int inic_port_resume(struct ata_port *ap)
486{
487 init_port(ap);
488 return 0;
489}
490
491static int inic_port_start(struct ata_port *ap)
492{
493 void __iomem *port_base = inic_port_base(ap);
494 struct inic_port_priv *pp;
495 u8 tmp;
496 int rc;
497
498 /* alloc and initialize private data */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900499 pp = devm_kzalloc(ap->host->dev, sizeof(*pp), GFP_KERNEL);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900500 if (!pp)
501 return -ENOMEM;
502 ap->private_data = pp;
503
504 /* default PRD_CTL value, DMAEN, WR and START off */
505 tmp = readb(port_base + PORT_PRD_CTL);
506 tmp &= ~(PRD_CTL_DMAEN | PRD_CTL_WR | PRD_CTL_START);
507 pp->dfl_prdctl = tmp;
508
509 /* Alloc resources */
510 rc = ata_port_start(ap);
511 if (rc) {
512 kfree(pp);
513 return rc;
514 }
515
516 init_port(ap);
517
518 return 0;
519}
520
Tejun Heo1fd7a692007-01-03 17:32:45 +0900521static struct ata_port_operations inic_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900522 .inherits = &ata_sff_port_ops,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900523
524 .bmdma_setup = inic_bmdma_setup,
525 .bmdma_start = inic_bmdma_start,
526 .bmdma_stop = inic_bmdma_stop,
527 .bmdma_status = inic_bmdma_status,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900528 .qc_issue = inic_qc_issue,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900529
530 .freeze = inic_freeze,
531 .thaw = inic_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900532 .softreset = ATA_OP_NULL, /* softreset is broken */
533 .hardreset = inic_hardreset,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900534 .error_handler = inic_error_handler,
535 .post_internal_cmd = inic_post_internal_cmd,
536 .dev_config = inic_dev_config,
537
Tejun Heo029cfd62008-03-25 12:22:49 +0900538 .scr_read = inic_scr_read,
539 .scr_write = inic_scr_write,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900540
Tejun Heo029cfd62008-03-25 12:22:49 +0900541 .port_resume = inic_port_resume,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900542 .port_start = inic_port_start,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900543};
544
545static struct ata_port_info inic_port_info = {
Tejun Heo0dc36882007-12-18 16:34:43 -0500546 /* For some reason, ATAPI_PROT_PIO is broken on this
Tejun Heo1fd7a692007-01-03 17:32:45 +0900547 * controller, and no, PIO_POLLING does't fix it. It somehow
548 * manages to report the wrong ireason and ignoring ireason
549 * results in machine lock up. Tell libata to always prefer
550 * DMA.
551 */
552 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
553 .pio_mask = 0x1f, /* pio0-4 */
554 .mwdma_mask = 0x07, /* mwdma0-2 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400555 .udma_mask = ATA_UDMA6,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900556 .port_ops = &inic_port_ops
557};
558
559static int init_controller(void __iomem *mmio_base, u16 hctl)
560{
561 int i;
562 u16 val;
563
564 hctl &= ~HCTL_KNOWN_BITS;
565
566 /* Soft reset whole controller. Spec says reset duration is 3
567 * PCI clocks, be generous and give it 10ms.
568 */
569 writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
570 readw(mmio_base + HOST_CTL); /* flush */
571
572 for (i = 0; i < 10; i++) {
573 msleep(1);
574 val = readw(mmio_base + HOST_CTL);
575 if (!(val & HCTL_SOFTRST))
576 break;
577 }
578
579 if (val & HCTL_SOFTRST)
580 return -EIO;
581
582 /* mask all interrupts and reset ports */
583 for (i = 0; i < NR_PORTS; i++) {
584 void __iomem *port_base = mmio_base + i * PORT_SIZE;
585
586 writeb(0xff, port_base + PORT_IRQ_MASK);
587 inic_reset_port(port_base);
588 }
589
590 /* port IRQ is masked now, unmask global IRQ */
591 writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
592 val = readw(mmio_base + HOST_IRQ_MASK);
593 val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
594 writew(val, mmio_base + HOST_IRQ_MASK);
595
596 return 0;
597}
598
Tejun Heo438ac6d2007-03-02 17:31:26 +0900599#ifdef CONFIG_PM
Tejun Heo1fd7a692007-01-03 17:32:45 +0900600static int inic_pci_device_resume(struct pci_dev *pdev)
601{
602 struct ata_host *host = dev_get_drvdata(&pdev->dev);
603 struct inic_host_priv *hpriv = host->private_data;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900604 void __iomem *mmio_base = host->iomap[MMIO_BAR];
Tejun Heo1fd7a692007-01-03 17:32:45 +0900605 int rc;
606
Dmitriy Monakhov5aea4082007-03-06 02:37:54 -0800607 rc = ata_pci_device_do_resume(pdev);
608 if (rc)
609 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900610
611 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
Tejun Heo1fd7a692007-01-03 17:32:45 +0900612 rc = init_controller(mmio_base, hpriv->cached_hctl);
613 if (rc)
614 return rc;
615 }
616
617 ata_host_resume(host);
618
619 return 0;
620}
Tejun Heo438ac6d2007-03-02 17:31:26 +0900621#endif
Tejun Heo1fd7a692007-01-03 17:32:45 +0900622
623static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
624{
625 static int printed_version;
Tejun Heo4447d352007-04-17 23:44:08 +0900626 const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
627 struct ata_host *host;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900628 struct inic_host_priv *hpriv;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900629 void __iomem * const *iomap;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900630 int i, rc;
631
632 if (!printed_version++)
633 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
634
Tejun Heo4447d352007-04-17 23:44:08 +0900635 /* alloc host */
636 host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
637 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
638 if (!host || !hpriv)
639 return -ENOMEM;
640
641 host->private_data = hpriv;
642
643 /* acquire resources and fill host */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900644 rc = pcim_enable_device(pdev);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900645 if (rc)
646 return rc;
647
Tejun Heo0d5ff562007-02-01 15:06:36 +0900648 rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
649 if (rc)
650 return rc;
Tejun Heo4447d352007-04-17 23:44:08 +0900651 host->iomap = iomap = pcim_iomap_table(pdev);
652
653 for (i = 0; i < NR_PORTS; i++) {
Tejun Heocbcdd872007-08-18 13:14:55 +0900654 struct ata_port *ap = host->ports[i];
655 struct ata_ioports *port = &ap->ioaddr;
656 unsigned int offset = i * PORT_SIZE;
Tejun Heo4447d352007-04-17 23:44:08 +0900657
658 port->cmd_addr = iomap[2 * i];
659 port->altstatus_addr =
660 port->ctl_addr = (void __iomem *)
661 ((unsigned long)iomap[2 * i + 1] | ATA_PCI_CTL_OFS);
Tejun Heocbcdd872007-08-18 13:14:55 +0900662 port->scr_addr = iomap[MMIO_BAR] + offset + PORT_SCR;
Tejun Heo4447d352007-04-17 23:44:08 +0900663
Tejun Heo9363c382008-04-07 22:47:16 +0900664 ata_sff_std_ports(port);
Tejun Heocbcdd872007-08-18 13:14:55 +0900665
666 ata_port_pbar_desc(ap, MMIO_BAR, -1, "mmio");
667 ata_port_pbar_desc(ap, MMIO_BAR, offset, "port");
668 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
669 (unsigned long long)pci_resource_start(pdev, 2 * i),
670 (unsigned long long)pci_resource_start(pdev, (2 * i + 1)) |
671 ATA_PCI_CTL_OFS);
Tejun Heo4447d352007-04-17 23:44:08 +0900672 }
673
674 hpriv->cached_hctl = readw(iomap[MMIO_BAR] + HOST_CTL);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900675
676 /* Set dma_mask. This devices doesn't support 64bit addressing. */
677 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
678 if (rc) {
679 dev_printk(KERN_ERR, &pdev->dev,
680 "32-bit DMA enable failed\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900681 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900682 }
683
684 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
685 if (rc) {
686 dev_printk(KERN_ERR, &pdev->dev,
687 "32-bit consistent DMA enable failed\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900688 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900689 }
690
FUJITA Tomonorib7d86292008-02-04 22:28:05 -0800691 /*
692 * This controller is braindamaged. dma_boundary is 0xffff
693 * like others but it will lock up the whole machine HARD if
694 * 65536 byte PRD entry is fed. Reduce maximum segment size.
695 */
696 rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
697 if (rc) {
698 dev_printk(KERN_ERR, &pdev->dev,
699 "failed to set the maximum segment size.\n");
700 return rc;
701 }
702
Tejun Heo0d5ff562007-02-01 15:06:36 +0900703 rc = init_controller(iomap[MMIO_BAR], hpriv->cached_hctl);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900704 if (rc) {
705 dev_printk(KERN_ERR, &pdev->dev,
706 "failed to initialize controller\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900707 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900708 }
709
710 pci_set_master(pdev);
Tejun Heo4447d352007-04-17 23:44:08 +0900711 return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
712 &inic_sht);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900713}
714
715static const struct pci_device_id inic_pci_tbl[] = {
716 { PCI_VDEVICE(INIT, 0x1622), },
717 { },
718};
719
720static struct pci_driver inic_pci_driver = {
721 .name = DRV_NAME,
722 .id_table = inic_pci_tbl,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900723#ifdef CONFIG_PM
Tejun Heo1fd7a692007-01-03 17:32:45 +0900724 .suspend = ata_pci_device_suspend,
725 .resume = inic_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900726#endif
Tejun Heo1fd7a692007-01-03 17:32:45 +0900727 .probe = inic_init_one,
728 .remove = ata_pci_remove_one,
729};
730
731static int __init inic_init(void)
732{
733 return pci_register_driver(&inic_pci_driver);
734}
735
736static void __exit inic_exit(void)
737{
738 pci_unregister_driver(&inic_pci_driver);
739}
740
741MODULE_AUTHOR("Tejun Heo");
742MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
743MODULE_LICENSE("GPL v2");
744MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
745MODULE_VERSION(DRV_VERSION);
746
747module_init(inic_init);
748module_exit(inic_exit);