Blame view

drivers/ata/sata_qstor.c 17.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  /*
   *  sata_qstor.c - Pacific Digital Corporation QStor SATA
   *
   *  Maintained by:  Mark Lord <mlord@pobox.com>
   *
   *  Copyright 2005 Pacific Digital Corporation.
   *  (OSL/GPL code release authorized by Jalil Fadavi).
   *
af36d7f0d   Jeff Garzik   [libata] license ...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2, or (at your option)
   *  any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; see the file COPYING.  If not, write to
   *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
   *
   *
   *  libata documentation is available via 'make {ps|pdf}docs',
19285f3c4   Mauro Carvalho Chehab   ata: update refer...
26
   *  as Documentation/driver-api/libata.rst
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
   *
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
32
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #include <linux/pci.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  #include <linux/blkdev.h>
  #include <linux/delay.h>
  #include <linux/interrupt.h>
a9524a76f   Jeff Garzik   [libata] use dev_...
37
  #include <linux/device.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  #include <scsi/scsi_host.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
  #include <linux/libata.h>
  
  #define DRV_NAME	"sata_qstor"
2a3103ce4   Jeff Garzik   [libata] Bump dri...
42
  #define DRV_VERSION	"0.09"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
  
  enum {
0d5ff5667   Tejun Heo   libata: convert t...
45
  	QS_MMIO_BAR		= 4,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
50
51
  	QS_PORTS		= 4,
  	QS_MAX_PRD		= LIBATA_MAX_PRD,
  	QS_CPB_ORDER		= 6,
  	QS_CPB_BYTES		= (1 << QS_CPB_ORDER),
  	QS_PRD_BYTES		= QS_MAX_PRD * 16,
  	QS_PKT_BYTES		= QS_CPB_BYTES + QS_PRD_BYTES,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  	/* global register offsets */
  	QS_HCF_CNFG3		= 0x0003, /* host configuration offset */
  	QS_HID_HPHY		= 0x0004, /* host physical interface info */
  	QS_HCT_CTRL		= 0x00e4, /* global interrupt mask offset */
  	QS_HST_SFF		= 0x0100, /* host status fifo offset */
  	QS_HVS_SERD3		= 0x0393, /* PHY enable offset */
  
  	/* global control bits */
  	QS_HPHY_64BIT		= (1 << 1), /* 64-bit bus detected */
  	QS_CNFG3_GSRST		= 0x01,     /* global chip reset */
  	QS_SERD3_PHY_ENA	= 0xf0,     /* PHY detection ENAble*/
  
  	/* per-channel register offsets */
  	QS_CCF_CPBA		= 0x0710, /* chan CPB base address */
  	QS_CCF_CSEP		= 0x0718, /* chan CPB separation factor */
  	QS_CFC_HUFT		= 0x0800, /* host upstream fifo threshold */
  	QS_CFC_HDFT		= 0x0804, /* host downstream fifo threshold */
  	QS_CFC_DUFT		= 0x0808, /* dev upstream fifo threshold */
  	QS_CFC_DDFT		= 0x080c, /* dev downstream fifo threshold */
  	QS_CCT_CTR0		= 0x0900, /* chan control-0 offset */
  	QS_CCT_CTR1		= 0x0901, /* chan control-1 offset */
  	QS_CCT_CFF		= 0x0a00, /* chan command fifo offset */
  
  	/* channel control bits */
  	QS_CTR0_REG		= (1 << 1),   /* register mode (vs. pkt mode) */
  	QS_CTR0_CLER		= (1 << 2),   /* clear channel errors */
  	QS_CTR1_RDEV		= (1 << 1),   /* sata phy/comms reset */
  	QS_CTR1_RCHN		= (1 << 4),   /* reset channel logic */
  	QS_CCF_RUN_PKT		= 0x107,      /* RUN a new dma PKT */
  
  	/* pkt sub-field headers */
  	QS_HCB_HDR		= 0x01,   /* Host Control Block header */
  	QS_DCB_HDR		= 0x02,   /* Device Control Block header */
  
  	/* pkt HCB flag bits */
  	QS_HF_DIRO		= (1 << 0),   /* data DIRection Out */
  	QS_HF_DAT		= (1 << 3),   /* DATa pkt */
  	QS_HF_IEN		= (1 << 4),   /* Interrupt ENable */
  	QS_HF_VLD		= (1 << 5),   /* VaLiD pkt */
  
  	/* pkt DCB flag bits */
  	QS_DF_PORD		= (1 << 2),   /* Pio OR Dma */
  	QS_DF_ELBA		= (1 << 3),   /* Extended LBA (lba48) */
  
  	/* PCI device IDs */
  	board_2068_idx		= 0,	/* QStor 4-port SATA/RAID */
  };
0420dd121   Al Viro   [PATCH] enum safe...
99
100
101
  enum {
  	QS_DMA_BOUNDARY		= ~0UL
  };
12ee7d3ce   Mark Lord   libata sata_qstor...
102
  typedef enum { qs_state_mmio, qs_state_pkt } qs_state_t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
107
108
  
  struct qs_port_priv {
  	u8			*pkt;
  	dma_addr_t		pkt_dma;
  	qs_state_t		state;
  };
82ef04fb4   Tejun Heo   libata: make SCR ...
109
110
  static int qs_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  static int qs_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
5796d1c4c   Jeff Garzik   [libata] Address ...
111
  static int qs_ata_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
  static int qs_port_start(struct ata_port *ap);
cca3974e4   Jeff Garzik   libata: Grand ren...
113
  static void qs_host_stop(struct ata_host *host);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  static void qs_qc_prep(struct ata_queued_cmd *qc);
9a3d9eb01   Tejun Heo   [PATCH] libata: r...
115
  static unsigned int qs_qc_issue(struct ata_queued_cmd *qc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  static int qs_check_atapi_dma(struct ata_queued_cmd *qc);
6004bda1c   Mark Lord   libata sata_qstor...
117
118
  static void qs_freeze(struct ata_port *ap);
  static void qs_thaw(struct ata_port *ap);
a1efdaba2   Tejun Heo   libata: make rese...
119
  static int qs_prereset(struct ata_link *link, unsigned long deadline);
6004bda1c   Mark Lord   libata sata_qstor...
120
  static void qs_error_handler(struct ata_port *ap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121

193515d51   Jeff Garzik   [libata] eliminat...
122
  static struct scsi_host_template qs_ata_sht = {
68d1d07b5   Tejun Heo   libata: implement...
123
  	ATA_BASE_SHT(DRV_NAME),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  	.sg_tablesize		= QS_MAX_PRD,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  	.dma_boundary		= QS_DMA_BOUNDARY,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  };
029cfd6b7   Tejun Heo   libata: implement...
127
128
  static struct ata_port_operations qs_ata_ops = {
  	.inherits		= &ata_sff_port_ops,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  	.check_atapi_dma	= qs_check_atapi_dma,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
  	.qc_prep		= qs_qc_prep,
  	.qc_issue		= qs_qc_issue,
029cfd6b7   Tejun Heo   libata: implement...
132

6004bda1c   Mark Lord   libata sata_qstor...
133
134
  	.freeze			= qs_freeze,
  	.thaw			= qs_thaw,
a1efdaba2   Tejun Heo   libata: make rese...
135
136
  	.prereset		= qs_prereset,
  	.softreset		= ATA_OP_NULL,
6004bda1c   Mark Lord   libata sata_qstor...
137
  	.error_handler		= qs_error_handler,
c96f1732e   Alan Cox   [libata] Improve ...
138
  	.lost_interrupt		= ATA_OP_NULL,
029cfd6b7   Tejun Heo   libata: implement...
139

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
  	.scr_read		= qs_scr_read,
  	.scr_write		= qs_scr_write,
029cfd6b7   Tejun Heo   libata: implement...
142

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
  	.port_start		= qs_port_start,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  	.host_stop		= qs_host_stop,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  };
98ac62def   Arjan van de Ven   [PATCH] mark seve...
146
  static const struct ata_port_info qs_port_info[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	/* board_2068_idx */
  	{
9cbe056f6   Sergei Shtylyov   libata: remove AT...
149
  		.flags		= ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING,
14bdef982   Erik Inge Bolsø   [libata] convert ...
150
  		.pio_mask	= ATA_PIO4_ONLY,
bf6263a85   Jeff Garzik   [libata] Use ATA_...
151
  		.udma_mask	= ATA_UDMA6,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
  		.port_ops	= &qs_ata_ops,
  	},
  };
3b7d697df   Jeff Garzik   [libata] constify...
155
  static const struct pci_device_id qs_ata_pci_tbl[] = {
2d2744fc8   Jeff Garzik   [libata] PCI ID t...
156
  	{ PCI_VDEVICE(PDC, 0x2068), board_2068_idx },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
164
165
166
  
  	{ }	/* terminate list */
  };
  
  static struct pci_driver qs_ata_pci_driver = {
  	.name			= DRV_NAME,
  	.id_table		= qs_ata_pci_tbl,
  	.probe			= qs_ata_init_one,
  	.remove			= ata_pci_remove_one,
  };
0d5ff5667   Tejun Heo   libata: convert t...
167
168
169
170
  static void __iomem *qs_mmio_base(struct ata_host *host)
  {
  	return host->iomap[QS_MMIO_BAR];
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
  static int qs_check_atapi_dma(struct ata_queued_cmd *qc)
  {
  	return 1;	/* ATAPI DMA not supported */
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
  static inline void qs_enter_reg_mode(struct ata_port *ap)
  {
0d5ff5667   Tejun Heo   libata: convert t...
177
  	u8 __iomem *chan = qs_mmio_base(ap->host) + (ap->port_no * 0x4000);
12ee7d3ce   Mark Lord   libata sata_qstor...
178
  	struct qs_port_priv *pp = ap->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179

12ee7d3ce   Mark Lord   libata sata_qstor...
180
  	pp->state = qs_state_mmio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
  	writeb(QS_CTR0_REG, chan + QS_CCT_CTR0);
  	readb(chan + QS_CCT_CTR0);        /* flush */
  }
  
  static inline void qs_reset_channel_logic(struct ata_port *ap)
  {
0d5ff5667   Tejun Heo   libata: convert t...
187
  	u8 __iomem *chan = qs_mmio_base(ap->host) + (ap->port_no * 0x4000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
192
  
  	writeb(QS_CTR1_RCHN, chan + QS_CCT_CTR1);
  	readb(chan + QS_CCT_CTR0);        /* flush */
  	qs_enter_reg_mode(ap);
  }
6004bda1c   Mark Lord   libata sata_qstor...
193
  static void qs_freeze(struct ata_port *ap)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  {
6004bda1c   Mark Lord   libata sata_qstor...
195
196
197
198
  	u8 __iomem *mmio_base = qs_mmio_base(ap->host);
  
  	writeb(0, mmio_base + QS_HCT_CTRL); /* disable host interrupts */
  	qs_enter_reg_mode(ap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
  }
6004bda1c   Mark Lord   libata sata_qstor...
200
  static void qs_thaw(struct ata_port *ap)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
  {
6004bda1c   Mark Lord   libata sata_qstor...
202
203
204
205
206
207
208
209
210
  	u8 __iomem *mmio_base = qs_mmio_base(ap->host);
  
  	qs_enter_reg_mode(ap);
  	writeb(1, mmio_base + QS_HCT_CTRL); /* enable host interrupts */
  }
  
  static int qs_prereset(struct ata_link *link, unsigned long deadline)
  {
  	struct ata_port *ap = link->ap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
  	qs_reset_channel_logic(ap);
9363c3825   Tejun Heo   libata: rename SF...
212
  	return ata_sff_prereset(link, deadline);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  }
82ef04fb4   Tejun Heo   libata: make SCR ...
214
  static int qs_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
  {
  	if (sc_reg > SCR_CONTROL)
da3dbb17a   Tejun Heo   libata: make ->sc...
217
  		return -EINVAL;
82ef04fb4   Tejun Heo   libata: make SCR ...
218
  	*val = readl(link->ap->ioaddr.scr_addr + (sc_reg * 8));
da3dbb17a   Tejun Heo   libata: make ->sc...
219
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  }
6004bda1c   Mark Lord   libata sata_qstor...
221
222
223
  static void qs_error_handler(struct ata_port *ap)
  {
  	qs_enter_reg_mode(ap);
fe06e5f9b   Tejun Heo   libata-sff: separ...
224
  	ata_sff_error_handler(ap);
6004bda1c   Mark Lord   libata sata_qstor...
225
  }
82ef04fb4   Tejun Heo   libata: make SCR ...
226
  static int qs_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
  {
  	if (sc_reg > SCR_CONTROL)
da3dbb17a   Tejun Heo   libata: make ->sc...
229
  		return -EINVAL;
82ef04fb4   Tejun Heo   libata: make SCR ...
230
  	writel(val, link->ap->ioaddr.scr_addr + (sc_reg * 8));
da3dbb17a   Tejun Heo   libata: make ->sc...
231
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
  }
828d09de4   Jeff Garzik   [libata ahci, qst...
233
  static unsigned int qs_fill_sg(struct ata_queued_cmd *qc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  {
cedc9a478   Jeff Garzik   libata: fix ATAPI...
235
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
  	struct ata_port *ap = qc->ap;
  	struct qs_port_priv *pp = ap->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  	u8 *prd = pp->pkt + QS_CPB_BYTES;
ff2aeb1eb   Tejun Heo   libata: convert t...
239
  	unsigned int si;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240

ff2aeb1eb   Tejun Heo   libata: convert t...
241
  	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
244
245
246
247
248
249
250
251
  		u64 addr;
  		u32 len;
  
  		addr = sg_dma_address(sg);
  		*(__le64 *)prd = cpu_to_le64(addr);
  		prd += sizeof(u64);
  
  		len = sg_dma_len(sg);
  		*(__le32 *)prd = cpu_to_le32(len);
  		prd += sizeof(u64);
ff2aeb1eb   Tejun Heo   libata: convert t...
252
253
  		VPRINTK("PRD[%u] = (0x%llX, 0x%X)
  ", si,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
  					(unsigned long long)addr, len);
  	}
828d09de4   Jeff Garzik   [libata ahci, qst...
256

ff2aeb1eb   Tejun Heo   libata: convert t...
257
  	return si;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
261
262
263
264
265
  }
  
  static void qs_qc_prep(struct ata_queued_cmd *qc)
  {
  	struct qs_port_priv *pp = qc->ap->private_data;
  	u8 dflags = QS_DF_PORD, *buf = pp->pkt;
  	u8 hflags = QS_HF_DAT | QS_HF_IEN | QS_HF_VLD;
  	u64 addr;
828d09de4   Jeff Garzik   [libata ahci, qst...
266
  	unsigned int nelem;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
269
270
271
  
  	VPRINTK("ENTER
  ");
  
  	qs_enter_reg_mode(qc->ap);
f47451c45   Tejun Heo   libata-sff: ata_s...
272
  	if (qc->tf.protocol != ATA_PROT_DMA)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274

828d09de4   Jeff Garzik   [libata ahci, qst...
275
  	nelem = qs_fill_sg(qc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
277
278
279
280
281
282
283
284
  
  	if ((qc->tf.flags & ATA_TFLAG_WRITE))
  		hflags |= QS_HF_DIRO;
  	if ((qc->tf.flags & ATA_TFLAG_LBA48))
  		dflags |= QS_DF_ELBA;
  
  	/* host control block (HCB) */
  	buf[ 0] = QS_HCB_HDR;
  	buf[ 1] = hflags;
726f0785b   Tejun Heo   libata: kill qc->...
285
  	*(__le32 *)(&buf[ 4]) = cpu_to_le32(qc->nbytes);
828d09de4   Jeff Garzik   [libata ahci, qst...
286
  	*(__le32 *)(&buf[ 8]) = cpu_to_le32(nelem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
294
  	addr = ((u64)pp->pkt_dma) + QS_CPB_BYTES;
  	*(__le64 *)(&buf[16]) = cpu_to_le64(addr);
  
  	/* device control block (DCB) */
  	buf[24] = QS_DCB_HDR;
  	buf[28] = dflags;
  
  	/* frame information structure (FIS) */
9977126c4   Tejun Heo   libata: add @is_c...
295
  	ata_tf_to_fis(&qc->tf, 0, 1, &buf[32]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
297
298
299
300
  }
  
  static inline void qs_packet_start(struct ata_queued_cmd *qc)
  {
  	struct ata_port *ap = qc->ap;
0d5ff5667   Tejun Heo   libata: convert t...
301
  	u8 __iomem *chan = qs_mmio_base(ap->host) + (ap->port_no * 0x4000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
304
305
306
307
308
309
310
  
  	VPRINTK("ENTER, ap %p
  ", ap);
  
  	writeb(QS_CTR0_CLER, chan + QS_CCT_CTR0);
  	wmb();                             /* flush PRDs and pkt to memory */
  	writel(QS_CCF_RUN_PKT, chan + QS_CCT_CFF);
  	readl(chan + QS_CCT_CFF);          /* flush */
  }
9a3d9eb01   Tejun Heo   [PATCH] libata: r...
311
  static unsigned int qs_qc_issue(struct ata_queued_cmd *qc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
316
  {
  	struct qs_port_priv *pp = qc->ap->private_data;
  
  	switch (qc->tf.protocol) {
  	case ATA_PROT_DMA:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
  		pp->state = qs_state_pkt;
  		qs_packet_start(qc);
  		return 0;
0dc36888d   Tejun Heo   libata: rename AT...
320
  	case ATAPI_PROT_DMA:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
328
  		BUG();
  		break;
  
  	default:
  		break;
  	}
  
  	pp->state = qs_state_mmio;
9363c3825   Tejun Heo   libata: rename SF...
329
  	return ata_sff_qc_issue(qc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  }
6004bda1c   Mark Lord   libata sata_qstor...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  static void qs_do_or_die(struct ata_queued_cmd *qc, u8 status)
  {
  	qc->err_mask |= ac_err_mask(status);
  
  	if (!qc->err_mask) {
  		ata_qc_complete(qc);
  	} else {
  		struct ata_port    *ap  = qc->ap;
  		struct ata_eh_info *ehi = &ap->link.eh_info;
  
  		ata_ehi_clear_desc(ehi);
  		ata_ehi_push_desc(ehi, "status 0x%02X", status);
  
  		if (qc->err_mask == AC_ERR_DEV)
  			ata_port_abort(ap);
  		else
  			ata_port_freeze(ap);
  	}
  }
cca3974e4   Jeff Garzik   libata: Grand ren...
350
  static inline unsigned int qs_intr_pkt(struct ata_host *host)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
353
  {
  	unsigned int handled = 0;
  	u8 sFFE;
0d5ff5667   Tejun Heo   libata: convert t...
354
  	u8 __iomem *mmio_base = qs_mmio_base(host);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
360
361
362
363
364
365
  
  	do {
  		u32 sff0 = readl(mmio_base + QS_HST_SFF);
  		u32 sff1 = readl(mmio_base + QS_HST_SFF + 4);
  		u8 sEVLD = (sff1 >> 30) & 0x01;	/* valid flag */
  		sFFE  = sff1 >> 31;		/* empty flag */
  
  		if (sEVLD) {
  			u8 sDST = sff0 >> 16;	/* dev status */
  			u8 sHST = sff1 & 0x3f;	/* host status */
  			unsigned int port_no = (sff1 >> 8) & 0x03;
cca3974e4   Jeff Garzik   libata: Grand ren...
366
  			struct ata_port *ap = host->ports[port_no];
3e4ec3443   Tejun Heo   libata: kill ATA_...
367
368
  			struct qs_port_priv *pp = ap->private_data;
  			struct ata_queued_cmd *qc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
  
  			DPRINTK("SFF=%08x%08x: sCHAN=%u sHST=%d sDST=%02x
  ",
  					sff1, sff0, port_no, sHST, sDST);
  			handled = 1;
3e4ec3443   Tejun Heo   libata: kill ATA_...
374
375
376
377
378
379
380
381
382
383
384
385
  			if (!pp || pp->state != qs_state_pkt)
  				continue;
  			qc = ata_qc_from_tag(ap, ap->link.active_tag);
  			if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
  				switch (sHST) {
  				case 0: /* successful CPB */
  				case 3: /* device error */
  					qs_enter_reg_mode(qc->ap);
  					qs_do_or_die(qc, sDST);
  					break;
  				default:
  					break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
391
  				}
  			}
  		}
  	} while (!sFFE);
  	return handled;
  }
cca3974e4   Jeff Garzik   libata: Grand ren...
392
  static inline unsigned int qs_intr_mmio(struct ata_host *host)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
  {
  	unsigned int handled = 0, port_no;
cca3974e4   Jeff Garzik   libata: Grand ren...
395
  	for (port_no = 0; port_no < host->n_ports; ++port_no) {
3e4ec3443   Tejun Heo   libata: kill ATA_...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
  		struct ata_port *ap = host->ports[port_no];
  		struct qs_port_priv *pp = ap->private_data;
  		struct ata_queued_cmd *qc;
  
  		qc = ata_qc_from_tag(ap, ap->link.active_tag);
  		if (!qc) {
  			/*
  			 * The qstor hardware generates spurious
  			 * interrupts from time to time when switching
  			 * in and out of packet mode.  There's no
  			 * obvious way to know if we're here now due
  			 * to that, so just ack the irq and pretend we
  			 * knew it was ours.. (ugh).  This does not
  			 * affect packet mode.
  			 */
  			ata_sff_check_status(ap);
  			handled = 1;
  			continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  		}
3e4ec3443   Tejun Heo   libata: kill ATA_...
415
416
417
418
  
  		if (!pp || pp->state != qs_state_mmio)
  			continue;
  		if (!(qc->tf.flags & ATA_TFLAG_POLLING))
c3b288942   Tejun Heo   libata-sff: separ...
419
  			handled |= ata_sff_port_intr(ap, qc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
  	}
  	return handled;
  }
7d12e780e   David Howells   IRQ: Maintain reg...
423
  static irqreturn_t qs_intr(int irq, void *dev_instance)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
  {
cca3974e4   Jeff Garzik   libata: Grand ren...
425
  	struct ata_host *host = dev_instance;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
  	unsigned int handled = 0;
904c7bad9   Mark Lord   libata sata_qstor...
427
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
430
  
  	VPRINTK("ENTER
  ");
904c7bad9   Mark Lord   libata sata_qstor...
431
  	spin_lock_irqsave(&host->lock, flags);
cca3974e4   Jeff Garzik   libata: Grand ren...
432
  	handled  = qs_intr_pkt(host) | qs_intr_mmio(host);
904c7bad9   Mark Lord   libata sata_qstor...
433
  	spin_unlock_irqrestore(&host->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
436
437
438
439
  
  	VPRINTK("EXIT
  ");
  
  	return IRQ_RETVAL(handled);
  }
0d5ff5667   Tejun Heo   libata: convert t...
440
  static void qs_ata_setup_port(struct ata_ioports *port, void __iomem *base)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  {
  	port->cmd_addr		=
  	port->data_addr		= base + 0x400;
  	port->error_addr	=
  	port->feature_addr	= base + 0x408; /* hob_feature = 0x409 */
  	port->nsect_addr	= base + 0x410; /* hob_nsect   = 0x411 */
  	port->lbal_addr		= base + 0x418; /* hob_lbal    = 0x419 */
  	port->lbam_addr		= base + 0x420; /* hob_lbam    = 0x421 */
  	port->lbah_addr		= base + 0x428; /* hob_lbah    = 0x429 */
  	port->device_addr	= base + 0x430;
  	port->status_addr	=
  	port->command_addr	= base + 0x438;
  	port->altstatus_addr	=
  	port->ctl_addr		= base + 0x440;
  	port->scr_addr		= base + 0xc00;
  }
  
  static int qs_port_start(struct ata_port *ap)
  {
cca3974e4   Jeff Garzik   libata: Grand ren...
460
  	struct device *dev = ap->host->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
  	struct qs_port_priv *pp;
0d5ff5667   Tejun Heo   libata: convert t...
462
  	void __iomem *mmio_base = qs_mmio_base(ap->host);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
464
  	void __iomem *chan = mmio_base + (ap->port_no * 0x4000);
  	u64 addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465

24dc5f33e   Tejun Heo   libata: update li...
466
467
468
469
470
471
472
  	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  	if (!pp)
  		return -ENOMEM;
  	pp->pkt = dmam_alloc_coherent(dev, QS_PKT_BYTES, &pp->pkt_dma,
  				      GFP_KERNEL);
  	if (!pp->pkt)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
  	memset(pp->pkt, 0, QS_PKT_BYTES);
  	ap->private_data = pp;
12ee7d3ce   Mark Lord   libata sata_qstor...
475
  	qs_enter_reg_mode(ap);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
478
479
  	addr = (u64)pp->pkt_dma;
  	writel((u32) addr,        chan + QS_CCF_CPBA);
  	writel((u32)(addr >> 32), chan + QS_CCF_CPBA + 4);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  }
cca3974e4   Jeff Garzik   libata: Grand ren...
481
  static void qs_host_stop(struct ata_host *host)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
  {
0d5ff5667   Tejun Heo   libata: convert t...
483
  	void __iomem *mmio_base = qs_mmio_base(host);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
487
  
  	writeb(0, mmio_base + QS_HCT_CTRL); /* disable host interrupts */
  	writeb(QS_CNFG3_GSRST, mmio_base + QS_HCF_CNFG3); /* global reset */
  }
4447d3515   Tejun Heo   libata: convert t...
488
  static void qs_host_init(struct ata_host *host, unsigned int chip_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
  {
4447d3515   Tejun Heo   libata: convert t...
490
  	void __iomem *mmio_base = host->iomap[QS_MMIO_BAR];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
  	unsigned int port_no;
  
  	writeb(0, mmio_base + QS_HCT_CTRL); /* disable host interrupts */
  	writeb(QS_CNFG3_GSRST, mmio_base + QS_HCF_CNFG3); /* global reset */
  
  	/* reset each channel in turn */
4447d3515   Tejun Heo   libata: convert t...
497
  	for (port_no = 0; port_no < host->n_ports; ++port_no) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
499
500
501
502
503
  		u8 __iomem *chan = mmio_base + (port_no * 0x4000);
  		writeb(QS_CTR1_RDEV|QS_CTR1_RCHN, chan + QS_CCT_CTR1);
  		writeb(QS_CTR0_REG, chan + QS_CCT_CTR0);
  		readb(chan + QS_CCT_CTR0);        /* flush */
  	}
  	writeb(QS_SERD3_PHY_ENA, mmio_base + QS_HVS_SERD3); /* enable phy */
4447d3515   Tejun Heo   libata: convert t...
504
  	for (port_no = 0; port_no < host->n_ports; ++port_no) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  		u8 __iomem *chan = mmio_base + (port_no * 0x4000);
  		/* set FIFO depths to same settings as Windows driver */
  		writew(32, chan + QS_CFC_HUFT);
  		writew(32, chan + QS_CFC_HDFT);
  		writew(10, chan + QS_CFC_DUFT);
  		writew( 8, chan + QS_CFC_DDFT);
  		/* set CPB size in bytes, as a power of two */
  		writeb(QS_CPB_ORDER,    chan + QS_CCF_CSEP);
  	}
  	writeb(1, mmio_base + QS_HCT_CTRL); /* enable host interrupts */
  }
  
  /*
   * The QStor understands 64-bit buses, and uses 64-bit fields
   * for DMA pointers regardless of bus width.  We just have to
   * make sure our DMA masks are set appropriately for whatever
   * bridge lies between us and the QStor, and then the DMA mapping
   * code will ensure we only ever "see" appropriate buffer addresses.
   * If we're 32-bit limited somewhere, then our 64-bit fields will
   * just end up with zeros in the upper 32-bits, without any special
   * logic required outside of this routine (below).
   */
  static int qs_set_dma_masks(struct pci_dev *pdev, void __iomem *mmio_base)
  {
  	u32 bus_info = readl(mmio_base + QS_HID_HPHY);
  	int rc, have_64bit_bus = (bus_info & QS_HPHY_64BIT);
  
  	if (have_64bit_bus &&
c54c719b5   Quentin Lambert   ata: remove depre...
533
534
  	    !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  		rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
  		if (rc) {
c54c719b5   Quentin Lambert   ata: remove depre...
536
  			rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  			if (rc) {
a44fec1fc   Joe Perches   ata: Convert dev_...
538
539
540
  				dev_err(&pdev->dev,
  					"64-bit DMA enable failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
542
543
544
  				return rc;
  			}
  		}
  	} else {
c54c719b5   Quentin Lambert   ata: remove depre...
545
  		rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  		if (rc) {
a44fec1fc   Joe Perches   ata: Convert dev_...
547
548
  			dev_err(&pdev->dev, "32-bit DMA enable failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
550
  			return rc;
  		}
c54c719b5   Quentin Lambert   ata: remove depre...
551
  		rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
  		if (rc) {
a44fec1fc   Joe Perches   ata: Convert dev_...
553
  			dev_err(&pdev->dev,
a9524a76f   Jeff Garzik   [libata] use dev_...
554
555
  				"32-bit consistent DMA enable failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
560
561
562
563
564
  			return rc;
  		}
  	}
  	return 0;
  }
  
  static int qs_ata_init_one(struct pci_dev *pdev,
  				const struct pci_device_id *ent)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
565
  	unsigned int board_idx = (unsigned int) ent->driver_data;
4447d3515   Tejun Heo   libata: convert t...
566
567
  	const struct ata_port_info *ppi[] = { &qs_port_info[board_idx], NULL };
  	struct ata_host *host;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  	int rc, port_no;
06296a1e6   Joe Perches   ata: Add and use ...
569
  	ata_print_version_once(&pdev->dev, DRV_VERSION);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570

4447d3515   Tejun Heo   libata: convert t...
571
572
573
574
575
576
  	/* alloc host */
  	host = ata_host_alloc_pinfo(&pdev->dev, ppi, QS_PORTS);
  	if (!host)
  		return -ENOMEM;
  
  	/* acquire resources and fill host */
24dc5f33e   Tejun Heo   libata: update li...
577
  	rc = pcim_enable_device(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
  	if (rc)
  		return rc;
0d5ff5667   Tejun Heo   libata: convert t...
580
  	if ((pci_resource_flags(pdev, QS_MMIO_BAR) & IORESOURCE_MEM) == 0)
24dc5f33e   Tejun Heo   libata: update li...
581
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582

0d5ff5667   Tejun Heo   libata: convert t...
583
584
585
  	rc = pcim_iomap_regions(pdev, 1 << QS_MMIO_BAR, DRV_NAME);
  	if (rc)
  		return rc;
4447d3515   Tejun Heo   libata: convert t...
586
  	host->iomap = pcim_iomap_table(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587

4447d3515   Tejun Heo   libata: convert t...
588
  	rc = qs_set_dma_masks(pdev, host->iomap[QS_MMIO_BAR]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  	if (rc)
24dc5f33e   Tejun Heo   libata: update li...
590
  		return rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591

4447d3515   Tejun Heo   libata: convert t...
592
  	for (port_no = 0; port_no < host->n_ports; ++port_no) {
cbcdd8759   Tejun Heo   libata: implement...
593
594
595
596
597
598
599
600
  		struct ata_port *ap = host->ports[port_no];
  		unsigned int offset = port_no * 0x4000;
  		void __iomem *chan = host->iomap[QS_MMIO_BAR] + offset;
  
  		qs_ata_setup_port(&ap->ioaddr, chan);
  
  		ata_port_pbar_desc(ap, QS_MMIO_BAR, -1, "mmio");
  		ata_port_pbar_desc(ap, QS_MMIO_BAR, offset, "port");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
  	/* initialize adapter */
4447d3515   Tejun Heo   libata: convert t...
603
  	qs_host_init(host, board_idx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604

4447d3515   Tejun Heo   libata: convert t...
605
606
607
  	pci_set_master(pdev);
  	return ata_host_activate(host, pdev->irq, qs_intr, IRQF_SHARED,
  				 &qs_ata_sht);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
  }
2fc75da0c   Axel Lin   ata: use module_p...
609
  module_pci_driver(qs_ata_pci_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
614
615
  
  MODULE_AUTHOR("Mark Lord");
  MODULE_DESCRIPTION("Pacific Digital Corporation QStor SATA low-level driver");
  MODULE_LICENSE("GPL");
  MODULE_DEVICE_TABLE(pci, qs_ata_pci_tbl);
  MODULE_VERSION(DRV_VERSION);