blob: 1b10455e1ae01c704d4fcd21db84b6ed28a99e78 [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
Tejun Heob0dd9b82008-04-30 16:35:09 +090038 HOST_ACTRL = 0x08,
Tejun Heo1fd7a692007-01-03 17:32:45 +090039 HOST_CTL = 0x7c,
40 HOST_STAT = 0x7e,
41 HOST_IRQ_STAT = 0xbc,
42 HOST_IRQ_MASK = 0xbe,
43
44 PORT_SIZE = 0x40,
45
46 /* registers for ATA TF operation */
Tejun Heob0dd9b82008-04-30 16:35:09 +090047 PORT_TF_DATA = 0x00,
48 PORT_TF_FEATURE = 0x01,
49 PORT_TF_NSECT = 0x02,
50 PORT_TF_LBAL = 0x03,
51 PORT_TF_LBAM = 0x04,
52 PORT_TF_LBAH = 0x05,
53 PORT_TF_DEVICE = 0x06,
54 PORT_TF_COMMAND = 0x07,
55 PORT_TF_ALT_STAT = 0x08,
Tejun Heo1fd7a692007-01-03 17:32:45 +090056 PORT_IRQ_STAT = 0x09,
57 PORT_IRQ_MASK = 0x0a,
58 PORT_PRD_CTL = 0x0b,
59 PORT_PRD_ADDR = 0x0c,
60 PORT_PRD_XFERLEN = 0x10,
Tejun Heob0dd9b82008-04-30 16:35:09 +090061 PORT_CPB_CPBLAR = 0x18,
62 PORT_CPB_PTQFIFO = 0x1c,
Tejun Heo1fd7a692007-01-03 17:32:45 +090063
64 /* IDMA register */
65 PORT_IDMA_CTL = 0x14,
Tejun Heob0dd9b82008-04-30 16:35:09 +090066 PORT_IDMA_STAT = 0x16,
67
68 PORT_RPQ_FIFO = 0x1e,
69 PORT_RPQ_CNT = 0x1f,
Tejun Heo1fd7a692007-01-03 17:32:45 +090070
71 PORT_SCR = 0x20,
72
73 /* HOST_CTL bits */
74 HCTL_IRQOFF = (1 << 8), /* global IRQ off */
Tejun Heob0dd9b82008-04-30 16:35:09 +090075 HCTL_FTHD0 = (1 << 10), /* fifo threshold 0 */
76 HCTL_FTHD1 = (1 << 11), /* fifo threshold 1*/
77 HCTL_PWRDWN = (1 << 12), /* power down PHYs */
Tejun Heo1fd7a692007-01-03 17:32:45 +090078 HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */
79 HCTL_RPGSEL = (1 << 15), /* register page select */
80
81 HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
82 HCTL_RPGSEL,
83
84 /* HOST_IRQ_(STAT|MASK) bits */
85 HIRQ_PORT0 = (1 << 0),
86 HIRQ_PORT1 = (1 << 1),
87 HIRQ_SOFT = (1 << 14),
88 HIRQ_GLOBAL = (1 << 15), /* STAT only */
89
90 /* PORT_IRQ_(STAT|MASK) bits */
91 PIRQ_OFFLINE = (1 << 0), /* device unplugged */
92 PIRQ_ONLINE = (1 << 1), /* device plugged */
93 PIRQ_COMPLETE = (1 << 2), /* completion interrupt */
94 PIRQ_FATAL = (1 << 3), /* fatal error */
95 PIRQ_ATA = (1 << 4), /* ATA interrupt */
96 PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */
97 PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */
98
99 PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
100
101 PIRQ_MASK_DMA_READ = PIRQ_REPLY | PIRQ_ATA,
102 PIRQ_MASK_OTHER = PIRQ_REPLY | PIRQ_COMPLETE,
103 PIRQ_MASK_FREEZE = 0xff,
104
105 /* PORT_PRD_CTL bits */
106 PRD_CTL_START = (1 << 0),
107 PRD_CTL_WR = (1 << 3),
108 PRD_CTL_DMAEN = (1 << 7), /* DMA enable */
109
110 /* PORT_IDMA_CTL bits */
111 IDMA_CTL_RST_ATA = (1 << 2), /* hardreset ATA bus */
112 IDMA_CTL_RST_IDMA = (1 << 5), /* reset IDMA machinary */
113 IDMA_CTL_GO = (1 << 7), /* IDMA mode go */
114 IDMA_CTL_ATA_NIEN = (1 << 8), /* ATA IRQ disable */
Tejun Heob0dd9b82008-04-30 16:35:09 +0900115
116 /* PORT_IDMA_STAT bits */
117 IDMA_STAT_PERR = (1 << 0), /* PCI ERROR MODE */
118 IDMA_STAT_CPBERR = (1 << 1), /* ADMA CPB error */
119 IDMA_STAT_LGCY = (1 << 3), /* ADMA legacy */
120 IDMA_STAT_UIRQ = (1 << 4), /* ADMA unsolicited irq */
121 IDMA_STAT_STPD = (1 << 5), /* ADMA stopped */
122 IDMA_STAT_PSD = (1 << 6), /* ADMA pause */
123 IDMA_STAT_DONE = (1 << 7), /* ADMA done */
124
125 IDMA_STAT_ERR = IDMA_STAT_PERR | IDMA_STAT_CPBERR,
126
127 /* CPB Control Flags*/
128 CPB_CTL_VALID = (1 << 0), /* CPB valid */
129 CPB_CTL_QUEUED = (1 << 1), /* queued command */
130 CPB_CTL_DATA = (1 << 2), /* data, rsvd in datasheet */
131 CPB_CTL_IEN = (1 << 3), /* PCI interrupt enable */
132 CPB_CTL_DEVDIR = (1 << 4), /* device direction control */
133
134 /* CPB Response Flags */
135 CPB_RESP_DONE = (1 << 0), /* ATA command complete */
136 CPB_RESP_REL = (1 << 1), /* ATA release */
137 CPB_RESP_IGNORED = (1 << 2), /* CPB ignored */
138 CPB_RESP_ATA_ERR = (1 << 3), /* ATA command error */
139 CPB_RESP_SPURIOUS = (1 << 4), /* ATA spurious interrupt error */
140 CPB_RESP_UNDERFLOW = (1 << 5), /* APRD deficiency length error */
141 CPB_RESP_OVERFLOW = (1 << 6), /* APRD exccess length error */
142 CPB_RESP_CPB_ERR = (1 << 7), /* CPB error flag */
143
144 /* PRD Control Flags */
145 PRD_DRAIN = (1 << 1), /* ignore data excess */
146 PRD_CDB = (1 << 2), /* atapi packet command pointer */
147 PRD_DIRECT_INTR = (1 << 3), /* direct interrupt */
148 PRD_DMA = (1 << 4), /* data transfer method */
149 PRD_WRITE = (1 << 5), /* data dir, rsvd in datasheet */
150 PRD_IOM = (1 << 6), /* io/memory transfer */
151 PRD_END = (1 << 7), /* APRD chain end */
Tejun Heo1fd7a692007-01-03 17:32:45 +0900152};
153
154struct inic_host_priv {
Tejun Heo36f674d2008-04-30 16:35:08 +0900155 u16 cached_hctl;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900156};
157
158struct inic_port_priv {
Tejun Heo36f674d2008-04-30 16:35:08 +0900159 u8 dfl_prdctl;
160 u8 cached_prdctl;
161 u8 cached_pirq_mask;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900162};
163
Tejun Heo1fd7a692007-01-03 17:32:45 +0900164static struct scsi_host_template inic_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900165 ATA_BMDMA_SHT(DRV_NAME),
Tejun Heo1fd7a692007-01-03 17:32:45 +0900166};
167
168static const int scr_map[] = {
169 [SCR_STATUS] = 0,
170 [SCR_ERROR] = 1,
171 [SCR_CONTROL] = 2,
172};
173
Jeff Garzik5796d1c2007-10-26 00:03:37 -0400174static void __iomem *inic_port_base(struct ata_port *ap)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900175{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900176 return ap->host->iomap[MMIO_BAR] + ap->port_no * PORT_SIZE;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900177}
178
179static void __inic_set_pirq_mask(struct ata_port *ap, u8 mask)
180{
181 void __iomem *port_base = inic_port_base(ap);
182 struct inic_port_priv *pp = ap->private_data;
183
184 writeb(mask, port_base + PORT_IRQ_MASK);
185 pp->cached_pirq_mask = mask;
186}
187
188static void inic_set_pirq_mask(struct ata_port *ap, u8 mask)
189{
190 struct inic_port_priv *pp = ap->private_data;
191
192 if (pp->cached_pirq_mask != mask)
193 __inic_set_pirq_mask(ap, mask);
194}
195
196static void inic_reset_port(void __iomem *port_base)
197{
198 void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
199 u16 ctl;
200
201 ctl = readw(idma_ctl);
202 ctl &= ~(IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN | IDMA_CTL_GO);
203
204 /* mask IRQ and assert reset */
205 writew(ctl | IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN, idma_ctl);
206 readw(idma_ctl); /* flush */
207
208 /* give it some time */
209 msleep(1);
210
211 /* release reset */
212 writew(ctl | IDMA_CTL_ATA_NIEN, idma_ctl);
213
214 /* clear irq */
215 writeb(0xff, port_base + PORT_IRQ_STAT);
216
217 /* reenable ATA IRQ, turn off IDMA mode */
218 writew(ctl, idma_ctl);
219}
220
Tejun Heoda3dbb12007-07-16 14:29:40 +0900221static int inic_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900222{
Jeff Garzik59f99882007-05-28 07:07:20 -0400223 void __iomem *scr_addr = ap->ioaddr.scr_addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900224 void __iomem *addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900225
226 if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
Tejun Heoda3dbb12007-07-16 14:29:40 +0900227 return -EINVAL;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900228
229 addr = scr_addr + scr_map[sc_reg] * 4;
Tejun Heoda3dbb12007-07-16 14:29:40 +0900230 *val = readl(scr_addr + scr_map[sc_reg] * 4);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900231
232 /* this controller has stuck DIAG.N, ignore it */
233 if (sc_reg == SCR_ERROR)
Tejun Heoda3dbb12007-07-16 14:29:40 +0900234 *val &= ~SERR_PHYRDY_CHG;
235 return 0;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900236}
237
Tejun Heoda3dbb12007-07-16 14:29:40 +0900238static int inic_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900239{
Jeff Garzik59f99882007-05-28 07:07:20 -0400240 void __iomem *scr_addr = ap->ioaddr.scr_addr;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900241
242 if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
Tejun Heoda3dbb12007-07-16 14:29:40 +0900243 return -EINVAL;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900244
Tejun Heo1fd7a692007-01-03 17:32:45 +0900245 writel(val, scr_addr + scr_map[sc_reg] * 4);
Tejun Heoda3dbb12007-07-16 14:29:40 +0900246 return 0;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900247}
248
249/*
250 * In TF mode, inic162x is very similar to SFF device. TF registers
251 * function the same. DMA engine behaves similary using the same PRD
252 * format as BMDMA but different command register, interrupt and event
253 * notification methods are used. The following inic_bmdma_*()
254 * functions do the impedance matching.
255 */
256static void inic_bmdma_setup(struct ata_queued_cmd *qc)
257{
258 struct ata_port *ap = qc->ap;
259 struct inic_port_priv *pp = ap->private_data;
260 void __iomem *port_base = inic_port_base(ap);
261 int rw = qc->tf.flags & ATA_TFLAG_WRITE;
262
263 /* make sure device sees PRD table writes */
264 wmb();
265
266 /* load transfer length */
267 writel(qc->nbytes, port_base + PORT_PRD_XFERLEN);
268
269 /* turn on DMA and specify data direction */
270 pp->cached_prdctl = pp->dfl_prdctl | PRD_CTL_DMAEN;
271 if (!rw)
272 pp->cached_prdctl |= PRD_CTL_WR;
273 writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
274
275 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +0900276 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900277}
278
279static void inic_bmdma_start(struct ata_queued_cmd *qc)
280{
281 struct ata_port *ap = qc->ap;
282 struct inic_port_priv *pp = ap->private_data;
283 void __iomem *port_base = inic_port_base(ap);
284
285 /* start host DMA transaction */
286 pp->cached_prdctl |= PRD_CTL_START;
287 writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
288}
289
290static void inic_bmdma_stop(struct ata_queued_cmd *qc)
291{
292 struct ata_port *ap = qc->ap;
293 struct inic_port_priv *pp = ap->private_data;
294 void __iomem *port_base = inic_port_base(ap);
295
296 /* stop DMA engine */
297 writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
298}
299
300static u8 inic_bmdma_status(struct ata_port *ap)
301{
302 /* event is already verified by the interrupt handler */
303 return ATA_DMA_INTR;
304}
305
Tejun Heo1fd7a692007-01-03 17:32:45 +0900306static void inic_host_intr(struct ata_port *ap)
307{
308 void __iomem *port_base = inic_port_base(ap);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900309 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900310 u8 irq_stat;
311
312 /* fetch and clear irq */
313 irq_stat = readb(port_base + PORT_IRQ_STAT);
314 writeb(irq_stat, port_base + PORT_IRQ_STAT);
315
316 if (likely(!(irq_stat & PIRQ_ERR))) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900317 struct ata_queued_cmd *qc =
318 ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900319
320 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900321 ap->ops->sff_check_status(ap); /* clear ATA interrupt */
Tejun Heo1fd7a692007-01-03 17:32:45 +0900322 return;
323 }
324
Tejun Heo9363c382008-04-07 22:47:16 +0900325 if (likely(ata_sff_host_intr(ap, qc)))
Tejun Heo1fd7a692007-01-03 17:32:45 +0900326 return;
327
Tejun Heo5682ed32008-04-07 22:47:16 +0900328 ap->ops->sff_check_status(ap); /* clear ATA interrupt */
Tejun Heo1fd7a692007-01-03 17:32:45 +0900329 ata_port_printk(ap, KERN_WARNING, "unhandled "
330 "interrupt, irq_stat=%x\n", irq_stat);
331 return;
332 }
333
334 /* error */
335 ata_ehi_push_desc(ehi, "irq_stat=0x%x", irq_stat);
336
337 if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
338 ata_ehi_hotplugged(ehi);
339 ata_port_freeze(ap);
340 } else
341 ata_port_abort(ap);
342}
343
344static irqreturn_t inic_interrupt(int irq, void *dev_instance)
345{
346 struct ata_host *host = dev_instance;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900347 void __iomem *mmio_base = host->iomap[MMIO_BAR];
Tejun Heo1fd7a692007-01-03 17:32:45 +0900348 u16 host_irq_stat;
349 int i, handled = 0;;
350
351 host_irq_stat = readw(mmio_base + HOST_IRQ_STAT);
352
353 if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
354 goto out;
355
356 spin_lock(&host->lock);
357
358 for (i = 0; i < NR_PORTS; i++) {
359 struct ata_port *ap = host->ports[i];
360
361 if (!(host_irq_stat & (HIRQ_PORT0 << i)))
362 continue;
363
364 if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
365 inic_host_intr(ap);
366 handled++;
367 } else {
368 if (ata_ratelimit())
369 dev_printk(KERN_ERR, host->dev, "interrupt "
370 "from disabled port %d (0x%x)\n",
371 i, host_irq_stat);
372 }
373 }
374
375 spin_unlock(&host->lock);
376
377 out:
378 return IRQ_RETVAL(handled);
379}
380
381static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
382{
383 struct ata_port *ap = qc->ap;
384
385 /* ATA IRQ doesn't wait for DMA transfer completion and vice
386 * versa. Mask IRQ selectively to detect command completion.
387 * Without it, ATA DMA read command can cause data corruption.
388 *
389 * Something similar might be needed for ATAPI writes. I
390 * tried a lot of combinations but couldn't find the solution.
391 */
392 if (qc->tf.protocol == ATA_PROT_DMA &&
393 !(qc->tf.flags & ATA_TFLAG_WRITE))
394 inic_set_pirq_mask(ap, PIRQ_MASK_DMA_READ);
395 else
396 inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
397
398 /* Issuing a command to yet uninitialized port locks up the
399 * controller. Most of the time, this happens for the first
400 * command after reset which are ATA and ATAPI IDENTIFYs.
401 * Fast fail if stat is 0x7f or 0xff for those commands.
402 */
403 if (unlikely(qc->tf.command == ATA_CMD_ID_ATA ||
404 qc->tf.command == ATA_CMD_ID_ATAPI)) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900405 u8 stat = ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900406 if (stat == 0x7f || stat == 0xff)
407 return AC_ERR_HSM;
408 }
409
Tejun Heo9363c382008-04-07 22:47:16 +0900410 return ata_sff_qc_issue(qc);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900411}
412
413static void inic_freeze(struct ata_port *ap)
414{
415 void __iomem *port_base = inic_port_base(ap);
416
417 __inic_set_pirq_mask(ap, PIRQ_MASK_FREEZE);
418
Tejun Heo5682ed32008-04-07 22:47:16 +0900419 ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900420 writeb(0xff, port_base + PORT_IRQ_STAT);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900421}
422
423static void inic_thaw(struct ata_port *ap)
424{
425 void __iomem *port_base = inic_port_base(ap);
426
Tejun Heo5682ed32008-04-07 22:47:16 +0900427 ap->ops->sff_check_status(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900428 writeb(0xff, port_base + PORT_IRQ_STAT);
429
430 __inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900431}
432
433/*
434 * SRST and SControl hardreset don't give valid signature on this
435 * controller. Only controller specific hardreset mechanism works.
436 */
Tejun Heocc0680a2007-08-06 18:36:23 +0900437static int inic_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +0900438 unsigned long deadline)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900439{
Tejun Heocc0680a2007-08-06 18:36:23 +0900440 struct ata_port *ap = link->ap;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900441 void __iomem *port_base = inic_port_base(ap);
442 void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
Tejun Heocc0680a2007-08-06 18:36:23 +0900443 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900444 u16 val;
445 int rc;
446
447 /* hammer it into sane state */
448 inic_reset_port(port_base);
449
Tejun Heo1fd7a692007-01-03 17:32:45 +0900450 val = readw(idma_ctl);
451 writew(val | IDMA_CTL_RST_ATA, idma_ctl);
452 readw(idma_ctl); /* flush */
453 msleep(1);
454 writew(val & ~IDMA_CTL_RST_ATA, idma_ctl);
455
Tejun Heocc0680a2007-08-06 18:36:23 +0900456 rc = sata_link_resume(link, timing, deadline);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900457 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +0900458 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heofe334602007-02-02 15:29:52 +0900459 "link after reset (errno=%d)\n", rc);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900460 return rc;
461 }
462
Tejun Heo1fd7a692007-01-03 17:32:45 +0900463 *class = ATA_DEV_NONE;
Tejun Heocc0680a2007-08-06 18:36:23 +0900464 if (ata_link_online(link)) {
Tejun Heo1fd7a692007-01-03 17:32:45 +0900465 struct ata_taskfile tf;
466
Tejun Heo705e76b2008-04-07 22:47:19 +0900467 /* wait for link to become ready */
468 rc = ata_sff_wait_after_reset(link, 1, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +0900469 /* link occupied, -ENODEV too is an error */
470 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +0900471 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heod4b2bab2007-02-02 16:50:52 +0900472 "after hardreset (errno=%d)\n", rc);
473 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900474 }
475
Tejun Heo9363c382008-04-07 22:47:16 +0900476 ata_sff_tf_read(ap, &tf);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900477 *class = ata_dev_classify(&tf);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900478 }
479
480 return 0;
481}
482
483static void inic_error_handler(struct ata_port *ap)
484{
485 void __iomem *port_base = inic_port_base(ap);
486 struct inic_port_priv *pp = ap->private_data;
487 unsigned long flags;
488
489 /* reset PIO HSM and stop DMA engine */
490 inic_reset_port(port_base);
491
492 spin_lock_irqsave(ap->lock, flags);
493 ap->hsm_task_state = HSM_ST_IDLE;
494 writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
495 spin_unlock_irqrestore(ap->lock, flags);
496
497 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900498 ata_std_error_handler(ap);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900499}
500
501static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
502{
503 /* make DMA engine forget about the failed command */
Tejun Heoa51d6442007-03-20 15:24:11 +0900504 if (qc->flags & ATA_QCFLAG_FAILED)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900505 inic_reset_port(inic_port_base(qc->ap));
506}
507
Alancd0d3bb2007-03-02 00:56:15 +0000508static void inic_dev_config(struct ata_device *dev)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900509{
510 /* inic can only handle upto LBA28 max sectors */
511 if (dev->max_sectors > ATA_MAX_SECTORS)
512 dev->max_sectors = ATA_MAX_SECTORS;
Tejun Heo90c93782007-06-29 11:33:08 +0900513
514 if (dev->n_sectors >= 1 << 28) {
515 ata_dev_printk(dev, KERN_ERR,
516 "ERROR: This driver doesn't support LBA48 yet and may cause\n"
517 " data corruption on such devices. Disabling.\n");
518 ata_dev_disable(dev);
519 }
Tejun Heo1fd7a692007-01-03 17:32:45 +0900520}
521
522static void init_port(struct ata_port *ap)
523{
524 void __iomem *port_base = inic_port_base(ap);
525
526 /* Setup PRD address */
527 writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
528}
529
530static int inic_port_resume(struct ata_port *ap)
531{
532 init_port(ap);
533 return 0;
534}
535
536static int inic_port_start(struct ata_port *ap)
537{
538 void __iomem *port_base = inic_port_base(ap);
539 struct inic_port_priv *pp;
540 u8 tmp;
541 int rc;
542
543 /* alloc and initialize private data */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900544 pp = devm_kzalloc(ap->host->dev, sizeof(*pp), GFP_KERNEL);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900545 if (!pp)
546 return -ENOMEM;
547 ap->private_data = pp;
548
549 /* default PRD_CTL value, DMAEN, WR and START off */
550 tmp = readb(port_base + PORT_PRD_CTL);
551 tmp &= ~(PRD_CTL_DMAEN | PRD_CTL_WR | PRD_CTL_START);
552 pp->dfl_prdctl = tmp;
553
554 /* Alloc resources */
555 rc = ata_port_start(ap);
Tejun Heo36f674d2008-04-30 16:35:08 +0900556 if (rc)
Tejun Heo1fd7a692007-01-03 17:32:45 +0900557 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900558
559 init_port(ap);
560
561 return 0;
562}
563
Tejun Heo1fd7a692007-01-03 17:32:45 +0900564static struct ata_port_operations inic_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900565 .inherits = &ata_sff_port_ops,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900566
567 .bmdma_setup = inic_bmdma_setup,
568 .bmdma_start = inic_bmdma_start,
569 .bmdma_stop = inic_bmdma_stop,
570 .bmdma_status = inic_bmdma_status,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900571 .qc_issue = inic_qc_issue,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900572
573 .freeze = inic_freeze,
574 .thaw = inic_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900575 .softreset = ATA_OP_NULL, /* softreset is broken */
576 .hardreset = inic_hardreset,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900577 .error_handler = inic_error_handler,
578 .post_internal_cmd = inic_post_internal_cmd,
579 .dev_config = inic_dev_config,
580
Tejun Heo029cfd62008-03-25 12:22:49 +0900581 .scr_read = inic_scr_read,
582 .scr_write = inic_scr_write,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900583
Tejun Heo029cfd62008-03-25 12:22:49 +0900584 .port_resume = inic_port_resume,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900585 .port_start = inic_port_start,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900586};
587
588static struct ata_port_info inic_port_info = {
Tejun Heo0dc36882007-12-18 16:34:43 -0500589 /* For some reason, ATAPI_PROT_PIO is broken on this
Tejun Heo1fd7a692007-01-03 17:32:45 +0900590 * controller, and no, PIO_POLLING does't fix it. It somehow
591 * manages to report the wrong ireason and ignoring ireason
592 * results in machine lock up. Tell libata to always prefer
593 * DMA.
594 */
595 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
596 .pio_mask = 0x1f, /* pio0-4 */
597 .mwdma_mask = 0x07, /* mwdma0-2 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400598 .udma_mask = ATA_UDMA6,
Tejun Heo1fd7a692007-01-03 17:32:45 +0900599 .port_ops = &inic_port_ops
600};
601
602static int init_controller(void __iomem *mmio_base, u16 hctl)
603{
604 int i;
605 u16 val;
606
607 hctl &= ~HCTL_KNOWN_BITS;
608
609 /* Soft reset whole controller. Spec says reset duration is 3
610 * PCI clocks, be generous and give it 10ms.
611 */
612 writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
613 readw(mmio_base + HOST_CTL); /* flush */
614
615 for (i = 0; i < 10; i++) {
616 msleep(1);
617 val = readw(mmio_base + HOST_CTL);
618 if (!(val & HCTL_SOFTRST))
619 break;
620 }
621
622 if (val & HCTL_SOFTRST)
623 return -EIO;
624
625 /* mask all interrupts and reset ports */
626 for (i = 0; i < NR_PORTS; i++) {
627 void __iomem *port_base = mmio_base + i * PORT_SIZE;
628
629 writeb(0xff, port_base + PORT_IRQ_MASK);
630 inic_reset_port(port_base);
631 }
632
633 /* port IRQ is masked now, unmask global IRQ */
634 writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
635 val = readw(mmio_base + HOST_IRQ_MASK);
636 val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
637 writew(val, mmio_base + HOST_IRQ_MASK);
638
639 return 0;
640}
641
Tejun Heo438ac6d2007-03-02 17:31:26 +0900642#ifdef CONFIG_PM
Tejun Heo1fd7a692007-01-03 17:32:45 +0900643static int inic_pci_device_resume(struct pci_dev *pdev)
644{
645 struct ata_host *host = dev_get_drvdata(&pdev->dev);
646 struct inic_host_priv *hpriv = host->private_data;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900647 void __iomem *mmio_base = host->iomap[MMIO_BAR];
Tejun Heo1fd7a692007-01-03 17:32:45 +0900648 int rc;
649
Dmitriy Monakhov5aea4082007-03-06 02:37:54 -0800650 rc = ata_pci_device_do_resume(pdev);
651 if (rc)
652 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900653
654 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
Tejun Heo1fd7a692007-01-03 17:32:45 +0900655 rc = init_controller(mmio_base, hpriv->cached_hctl);
656 if (rc)
657 return rc;
658 }
659
660 ata_host_resume(host);
661
662 return 0;
663}
Tejun Heo438ac6d2007-03-02 17:31:26 +0900664#endif
Tejun Heo1fd7a692007-01-03 17:32:45 +0900665
666static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
667{
668 static int printed_version;
Tejun Heo4447d352007-04-17 23:44:08 +0900669 const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
670 struct ata_host *host;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900671 struct inic_host_priv *hpriv;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900672 void __iomem * const *iomap;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900673 int i, rc;
674
675 if (!printed_version++)
676 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
677
Tejun Heo4447d352007-04-17 23:44:08 +0900678 /* alloc host */
679 host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
680 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
681 if (!host || !hpriv)
682 return -ENOMEM;
683
684 host->private_data = hpriv;
685
686 /* acquire resources and fill host */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900687 rc = pcim_enable_device(pdev);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900688 if (rc)
689 return rc;
690
Tejun Heo0d5ff562007-02-01 15:06:36 +0900691 rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
692 if (rc)
693 return rc;
Tejun Heo4447d352007-04-17 23:44:08 +0900694 host->iomap = iomap = pcim_iomap_table(pdev);
695
696 for (i = 0; i < NR_PORTS; i++) {
Tejun Heocbcdd872007-08-18 13:14:55 +0900697 struct ata_port *ap = host->ports[i];
698 struct ata_ioports *port = &ap->ioaddr;
699 unsigned int offset = i * PORT_SIZE;
Tejun Heo4447d352007-04-17 23:44:08 +0900700
701 port->cmd_addr = iomap[2 * i];
702 port->altstatus_addr =
703 port->ctl_addr = (void __iomem *)
704 ((unsigned long)iomap[2 * i + 1] | ATA_PCI_CTL_OFS);
Tejun Heocbcdd872007-08-18 13:14:55 +0900705 port->scr_addr = iomap[MMIO_BAR] + offset + PORT_SCR;
Tejun Heo4447d352007-04-17 23:44:08 +0900706
Tejun Heo9363c382008-04-07 22:47:16 +0900707 ata_sff_std_ports(port);
Tejun Heocbcdd872007-08-18 13:14:55 +0900708
709 ata_port_pbar_desc(ap, MMIO_BAR, -1, "mmio");
710 ata_port_pbar_desc(ap, MMIO_BAR, offset, "port");
711 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
712 (unsigned long long)pci_resource_start(pdev, 2 * i),
713 (unsigned long long)pci_resource_start(pdev, (2 * i + 1)) |
714 ATA_PCI_CTL_OFS);
Tejun Heo4447d352007-04-17 23:44:08 +0900715 }
716
717 hpriv->cached_hctl = readw(iomap[MMIO_BAR] + HOST_CTL);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900718
719 /* Set dma_mask. This devices doesn't support 64bit addressing. */
720 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
721 if (rc) {
722 dev_printk(KERN_ERR, &pdev->dev,
723 "32-bit DMA enable failed\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900724 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900725 }
726
727 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
728 if (rc) {
729 dev_printk(KERN_ERR, &pdev->dev,
730 "32-bit consistent DMA enable failed\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900731 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900732 }
733
FUJITA Tomonorib7d86292008-02-04 22:28:05 -0800734 /*
735 * This controller is braindamaged. dma_boundary is 0xffff
736 * like others but it will lock up the whole machine HARD if
737 * 65536 byte PRD entry is fed. Reduce maximum segment size.
738 */
739 rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
740 if (rc) {
741 dev_printk(KERN_ERR, &pdev->dev,
742 "failed to set the maximum segment size.\n");
743 return rc;
744 }
745
Tejun Heo0d5ff562007-02-01 15:06:36 +0900746 rc = init_controller(iomap[MMIO_BAR], hpriv->cached_hctl);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900747 if (rc) {
748 dev_printk(KERN_ERR, &pdev->dev,
749 "failed to initialize controller\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900750 return rc;
Tejun Heo1fd7a692007-01-03 17:32:45 +0900751 }
752
753 pci_set_master(pdev);
Tejun Heo4447d352007-04-17 23:44:08 +0900754 return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
755 &inic_sht);
Tejun Heo1fd7a692007-01-03 17:32:45 +0900756}
757
758static const struct pci_device_id inic_pci_tbl[] = {
759 { PCI_VDEVICE(INIT, 0x1622), },
760 { },
761};
762
763static struct pci_driver inic_pci_driver = {
764 .name = DRV_NAME,
765 .id_table = inic_pci_tbl,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900766#ifdef CONFIG_PM
Tejun Heo1fd7a692007-01-03 17:32:45 +0900767 .suspend = ata_pci_device_suspend,
768 .resume = inic_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900769#endif
Tejun Heo1fd7a692007-01-03 17:32:45 +0900770 .probe = inic_init_one,
771 .remove = ata_pci_remove_one,
772};
773
774static int __init inic_init(void)
775{
776 return pci_register_driver(&inic_pci_driver);
777}
778
779static void __exit inic_exit(void)
780{
781 pci_unregister_driver(&inic_pci_driver);
782}
783
784MODULE_AUTHOR("Tejun Heo");
785MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
786MODULE_LICENSE("GPL v2");
787MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
788MODULE_VERSION(DRV_VERSION);
789
790module_init(inic_init);
791module_exit(inic_exit);