blob: bccf872903e5c25fa809b3336f55492e4a00e732 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1997 Wu Ching Chen
3 * 2.1.x update (C) 1998 Krzysztof G. Baranowski
Alan Coxfa195af2008-10-27 15:16:36 +00004 * 2.5.x update (C) 2002 Red Hat
5 * 2.6.x update (C) 2004 Red Hat
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
8 *
9 * Wu Ching Chen : NULL pointer fixes 2000/06/02
10 * support atp876 chip
11 * enable 32 bit fifo transfer
12 * support cdrom & remove device run ultra speed
13 * fix disconnect bug 2000/12/21
14 * support atp880 chip lvd u160 2001/05/15
15 * fix prd table bug 2001/09/12 (7.1)
16 *
17 * atp885 support add by ACARD Hao Ping Lian 2005/01/05
18 */
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ioport.h>
26#include <linux/delay.h>
27#include <linux/proc_fs.h>
28#include <linux/spinlock.h>
29#include <linux/pci.h>
30#include <linux/blkdev.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080031#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34
35#include <scsi/scsi.h>
36#include <scsi/scsi_cmnd.h>
37#include <scsi/scsi_device.h>
38#include <scsi/scsi_host.h>
39
40#include "atp870u.h"
41
42static struct scsi_host_template atp870u_template;
43static void send_s870(struct atp_unit *dev,unsigned char c);
Ondrej Zary4192a402015-11-17 19:24:06 +010044static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void tscam_885(void);
46
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010047static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
48{
49 outb(val, atp->baseport + reg);
50}
51
Ondrej Zaryd804bb22015-11-17 19:24:07 +010052static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
53{
54 outw(val, atp->baseport + reg);
55}
56
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010057static inline void atp_writeb_io(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
58{
59 outb(val, atp->ioport[channel] + reg);
60}
61
62static inline void atp_writew_io(struct atp_unit *atp, u8 channel, u8 reg, u16 val)
63{
64 outw(val, atp->ioport[channel] + reg);
65}
66
67static inline void atp_writeb_pci(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
68{
69 outb(val, atp->pciport[channel] + reg);
70}
71
72static inline void atp_writel_pci(struct atp_unit *atp, u8 channel, u8 reg, u32 val)
73{
74 outl(val, atp->pciport[channel] + reg);
75}
76
77static inline u8 atp_readb_base(struct atp_unit *atp, u8 reg)
78{
79 return inb(atp->baseport + reg);
80}
81
Ondrej Zaryd804bb22015-11-17 19:24:07 +010082static inline u16 atp_readw_base(struct atp_unit *atp, u8 reg)
83{
84 return inw(atp->baseport + reg);
85}
86
87static inline u32 atp_readl_base(struct atp_unit *atp, u8 reg)
88{
89 return inl(atp->baseport + reg);
90}
91
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010092static inline u8 atp_readb_io(struct atp_unit *atp, u8 channel, u8 reg)
93{
94 return inb(atp->ioport[channel] + reg);
95}
96
97static inline u16 atp_readw_io(struct atp_unit *atp, u8 channel, u8 reg)
98{
99 return inw(atp->ioport[channel] + reg);
100}
101
102static inline u8 atp_readb_pci(struct atp_unit *atp, u8 channel, u8 reg)
103{
104 return inb(atp->pciport[channel] + reg);
105}
106
David Howells7d12e782006-10-05 14:55:46 +0100107static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned long flags;
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100110 unsigned short int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned char i, j, c, target_id, lun,cmdp;
112 unsigned char *prd;
113 struct scsi_cmnd *workreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned long adrcnt, k;
115#ifdef ED_DBGP
116 unsigned long l;
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct Scsi_Host *host = dev_id;
119 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
120
121 for (c = 0; c < 2; c++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100122 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if ((j & 0x80) != 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 dev->in_int[c] = 0;
126 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100127 if ((j & 0x80) == 0)
128 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#ifdef ED_DBGP
130 printk("atp870u_intr_handle enter\n");
131#endif
132 dev->in_int[c] = 1;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100133 cmdp = atp_readb_io(dev, c, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (dev->working[c] != 0) {
135 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100136 if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
137 atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100139 if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 for (k=0; k < 1000; k++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100142 if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100143 break;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100144 if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100145 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100148 atp_writeb_pci(dev, c, 0, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100150 i = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100152 if (dev->dev_id == ATP885_DEVID)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100153 atp_writeb_pci(dev, c, 2, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100155 target_id = atp_readb_io(dev, c, 0x15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /*
158 * Remap wide devices onto id numbers
159 */
160
161 if ((target_id & 0x40) != 0) {
162 target_id = (target_id & 0x07) | 0x08;
163 } else {
164 target_id &= 0x07;
165 }
166
167 if ((j & 0x40) != 0) {
168 if (dev->last_cmd[c] == 0xff) {
169 dev->last_cmd[c] = target_id;
170 }
171 dev->last_cmd[c] |= 0x40;
172 }
173 if (dev->dev_id == ATP885_DEVID)
174 dev->r1f[c][target_id] |= j;
175#ifdef ED_DBGP
176 printk("atp870u_intr_handle status = %x\n",i);
177#endif
178 if (i == 0x85) {
179 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
180 dev->last_cmd[c] = 0xff;
181 }
182 if (dev->dev_id == ATP885_DEVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100184 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
185 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
186 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (dev->id[c][target_id].last_len != adrcnt)
188 {
189 k = dev->id[c][target_id].last_len;
190 k -= adrcnt;
191 dev->id[c][target_id].tran_len = k;
192 dev->id[c][target_id].last_len = adrcnt;
193 }
194#ifdef ED_DBGP
Ondrej Zary3a38e532015-11-17 19:23:39 +0100195 printk("dev->id[c][target_id].last_len = %d dev->id[c][target_id].tran_len = %d\n",dev->id[c][target_id].last_len,dev->id[c][target_id].tran_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#endif
197 }
198
199 /*
200 * Flip wide
201 */
202 if (dev->wide_id[c] != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100203 atp_writeb_io(dev, c, 0x1b, 0x01);
204 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
205 atp_writeb_io(dev, c, 0x1b, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 /*
208 * Issue more commands
209 */
210 spin_lock_irqsave(dev->host->host_lock, flags);
211 if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
212 (dev->in_snd[c] == 0)) {
213#ifdef ED_DBGP
214 printk("Call sent_s870\n");
215#endif
216 send_s870(dev,c);
217 }
218 spin_unlock_irqrestore(dev->host->host_lock, flags);
219 /*
220 * Done
221 */
222 dev->in_int[c] = 0;
223#ifdef ED_DBGP
224 printk("Status 0x85 return\n");
225#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100226 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 if (i == 0x40) {
230 dev->last_cmd[c] |= 0x40;
231 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100232 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 if (i == 0x21) {
236 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
237 dev->last_cmd[c] = 0xff;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100240 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
241 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
242 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 k = dev->id[c][target_id].last_len;
244 k -= adrcnt;
245 dev->id[c][target_id].tran_len = k;
246 dev->id[c][target_id].last_len = adrcnt;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100247 atp_writeb_io(dev, c, 0x10, 0x41);
248 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100250 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 if (dev->dev_id == ATP885_DEVID) {
254 if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
255 if ((i == 0x4c) || (i == 0x8c))
256 i=0x48;
257 else
258 i=0x49;
259 }
260
261 }
262 if ((i == 0x80) || (i == 0x8f)) {
263#ifdef ED_DBGP
264 printk(KERN_DEBUG "Device reselect\n");
265#endif
266 lun = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100267 if (cmdp == 0x44 || i == 0x80)
268 lun = atp_readb_io(dev, c, 0x1d) & 0x07;
269 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
271 dev->last_cmd[c] = 0xff;
272 }
273 if (cmdp == 0x41) {
274#ifdef ED_DBGP
275 printk("cmdp = 0x41\n");
276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100278 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
279 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
280 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 k = dev->id[c][target_id].last_len;
282 k -= adrcnt;
283 dev->id[c][target_id].tran_len = k;
284 dev->id[c][target_id].last_len = adrcnt;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100285 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100287 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else {
289#ifdef ED_DBGP
290 printk("cmdp != 0x41\n");
291#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100292 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100294 atp_writeb_io(dev, c, 0x12, 0x00);
295 atp_writeb_io(dev, c, 0x13, 0x00);
296 atp_writeb_io(dev, c, 0x14, 0x00);
297 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100299 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 }
302 if (dev->last_cmd[c] != 0xff) {
303 dev->last_cmd[c] |= 0x40;
304 }
305 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100306 j = atp_readb_base(dev, 0x29) & 0xfe;
307 atp_writeb_base(dev, 0x29, j);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100308 } else
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100309 atp_writeb_io(dev, c, 0x10, 0x45);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100310
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100311 target_id = atp_readb_io(dev, c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Remap wide identifiers
314 */
315 if ((target_id & 0x10) != 0) {
316 target_id = (target_id & 0x07) | 0x08;
317 } else {
318 target_id &= 0x07;
319 }
Ondrej Zary3a38e532015-11-17 19:23:39 +0100320 if (dev->dev_id == ATP885_DEVID)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100321 atp_writeb_io(dev, c, 0x10, 0x45);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 workreq = dev->id[c][target_id].curr_req;
323#ifdef ED_DBGP
Jeff Garzik017560f2005-10-24 18:04:36 -0400324 scmd_printk(KERN_DEBUG, workreq, "CDB");
325 for (l = 0; l < workreq->cmd_len; l++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 printk(KERN_DEBUG " %x",workreq->cmnd[l]);
Jeff Garzik017560f2005-10-24 18:04:36 -0400327 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#endif
329
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100330 atp_writeb_io(dev, c, 0x0f, lun);
331 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 adrcnt = dev->id[c][target_id].tran_len;
333 k = dev->id[c][target_id].last_len;
334
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100335 atp_writeb_io(dev, c, 0x12, ((unsigned char *) &k)[2]);
336 atp_writeb_io(dev, c, 0x13, ((unsigned char *) &k)[1]);
337 atp_writeb_io(dev, c, 0x14, ((unsigned char *) &k)[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#ifdef ED_DBGP
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100339 printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, atp_readb_io(dev, c, 0x14), atp_readb_io(dev, c, 0x13), atp_readb_io(dev, c, 0x12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
341 /* Remap wide */
342 j = target_id;
343 if (target_id > 7) {
344 j = (j & 0x07) | 0x40;
345 }
346 /* Add direction */
347 j |= dev->id[c][target_id].dirct;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100348 atp_writeb_io(dev, c, 0x15, j);
349 atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* enable 32 bit fifo transfer */
352 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100353 i = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 //j=workreq->cmnd[0];
355 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
356 i |= 0x0c;
357 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100358 atp_writeb_pci(dev, c, 1, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else if ((dev->dev_id == ATP880_DEVID1) ||
360 (dev->dev_id == ATP880_DEVID2) ) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100361 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
362 atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
363 else
364 atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100366 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
367 atp_writeb_io(dev, c, 0x3a, (atp_readb_io(dev, c, 0x3a) & 0xf3) | 0x08);
368 else
369 atp_writeb_io(dev, c, 0x3a, atp_readb_io(dev, c, 0x3a) & 0xf3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 j = 0;
372 id = 1;
373 id = id << target_id;
374 /*
375 * Is this a wide device
376 */
377 if ((id & dev->wide_id[c]) != 0) {
378 j |= 0x01;
379 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100380 atp_writeb_io(dev, c, 0x1b, j);
381 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
382 atp_writeb_io(dev, c, 0x1b, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (dev->id[c][target_id].last_len == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100384 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dev->in_int[c] = 0;
386#ifdef ED_DBGP
387 printk("dev->id[c][target_id].last_len = 0\n");
388#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100389 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391#ifdef ED_DBGP
392 printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
393#endif
394 prd = dev->id[c][target_id].prd_pos;
395 while (adrcnt != 0) {
396 id = ((unsigned short int *)prd)[2];
397 if (id == 0) {
398 k = 0x10000;
399 } else {
400 k = id;
401 }
402 if (k > adrcnt) {
403 ((unsigned short int *)prd)[2] = (unsigned short int)
404 (k - adrcnt);
405 ((unsigned long *)prd)[0] += adrcnt;
406 adrcnt = 0;
407 dev->id[c][target_id].prd_pos = prd;
408 } else {
409 adrcnt -= k;
410 dev->id[c][target_id].prdaddr += 0x08;
411 prd += 0x08;
412 if (adrcnt == 0) {
413 dev->id[c][target_id].prd_pos = prd;
414 }
415 }
416 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100417 atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef ED_DBGP
419 printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
420#endif
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100421 if (dev->dev_id != ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100422 atp_writeb_pci(dev, c, 2, 0x06);
423 atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * Check transfer direction
427 */
428 if (dev->id[c][target_id].dirct != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100429 atp_writeb_io(dev, c, 0x18, 0x08);
430 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dev->in_int[c] = 0;
432#ifdef ED_DBGP
433 printk("status 0x80 return dirct != 0\n");
434#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100435 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100437 atp_writeb_io(dev, c, 0x18, 0x08);
438 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 dev->in_int[c] = 0;
440#ifdef ED_DBGP
441 printk("status 0x80 return dirct = 0\n");
442#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100443 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 /*
447 * Current scsi request on this target
448 */
449
450 workreq = dev->id[c][target_id].curr_req;
451
Ondrej Zary78614ec2015-11-17 19:23:49 +0100452 if (i == 0x42 || i == 0x16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
454 dev->last_cmd[c] = 0xff;
455 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100456 if (i == 0x16) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100457 workreq->result = atp_readb_io(dev, c, 0x0f);
Ondrej Zary78614ec2015-11-17 19:23:49 +0100458 if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
459 printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
460 workreq->result = 0x02;
461 }
462 } else
463 workreq->result = 0x02;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100466 j = atp_readb_base(dev, 0x29) | 0x01;
467 atp_writeb_base(dev, 0x29, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 /*
470 * Complete the command
471 */
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300472 scsi_dma_unmap(workreq);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 spin_lock_irqsave(dev->host->host_lock, flags);
475 (*workreq->scsi_done) (workreq);
476#ifdef ED_DBGP
477 printk("workreq->scsi_done\n");
478#endif
479 /*
480 * Clear it off the queue
481 */
482 dev->id[c][target_id].curr_req = NULL;
483 dev->working[c]--;
484 spin_unlock_irqrestore(dev->host->host_lock, flags);
485 /*
486 * Take it back wide
487 */
488 if (dev->wide_id[c] != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100489 atp_writeb_io(dev, c, 0x1b, 0x01);
490 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
491 atp_writeb_io(dev, c, 0x1b, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 /*
494 * If there is stuff to send and nothing going then send it
495 */
496 spin_lock_irqsave(dev->host->host_lock, flags);
497 if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
498 (dev->in_snd[c] == 0)) {
499#ifdef ED_DBGP
500 printk("Call sent_s870(scsi_done)\n");
501#endif
502 send_s870(dev,c);
503 }
504 spin_unlock_irqrestore(dev->host->host_lock, flags);
505 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100506 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
509 dev->last_cmd[c] = 0xff;
510 }
511 if (i == 0x4f) {
512 i = 0x89;
513 }
514 i &= 0x0f;
515 if (i == 0x09) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100516 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
517 atp_writeb_pci(dev, c, 2, 0x06);
518 atp_writeb_pci(dev, c, 2, 0x00);
519 atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (dev->dev_id == ATP885_DEVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 k = dev->id[c][target_id].last_len;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100522 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
523 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
524 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
527 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100529 atp_writeb_io(dev, c, 0x18, 0x08);
530 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100532 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 if (i == 0x08) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100535 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
536 atp_writeb_pci(dev, c, 2, 0x06);
537 atp_writeb_pci(dev, c, 2, 0x00);
538 atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (dev->dev_id == ATP885_DEVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 k = dev->id[c][target_id].last_len;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100541 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
542 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
543 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100545 atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100547 atp_writeb_io(dev, c, 0x18, 0x08);
548 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100550 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100552 if (i == 0x0a)
553 atp_writeb_io(dev, c, 0x10, 0x30);
554 else
555 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100557 atp_writeb_io(dev, c, 0x12, 0x00);
558 atp_writeb_io(dev, c, 0x13, 0x00);
559 atp_writeb_io(dev, c, 0x14, 0x00);
560 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100562 dev->in_int[c] = 0;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return IRQ_HANDLED;
565}
566/**
567 * atp870u_queuecommand - Queue SCSI command
568 * @req_p: request block
569 * @done: completion function
570 *
571 * Queue a command to the ATP queue. Called with the host lock held.
572 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500573static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 void (*done) (struct scsi_cmnd *))
575{
576 unsigned char c;
Ondrej Zary3b836462015-11-17 19:23:40 +0100577 unsigned int m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct atp_unit *dev;
579 struct Scsi_Host *host;
580
Jeff Garzik422c0d62005-10-24 18:05:09 -0400581 c = scmd_channel(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 req_p->sense_buffer[0]=0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300583 scsi_set_resid(req_p, 0);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400584 if (scmd_channel(req_p) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 req_p->result = 0x00040000;
586 done(req_p);
587#ifdef ED_DBGP
588 printk("atp870u_queuecommand : req_p->device->channel > 1\n");
589#endif
590 return 0;
591 }
592
593 host = req_p->device->host;
594 dev = (struct atp_unit *)&host->hostdata;
595
596
597
598 m = 1;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400599 m = m << scmd_id(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /*
602 * Fake a timeout for missing targets
603 */
604
605 if ((m & dev->active_id[c]) == 0) {
606 req_p->result = 0x00040000;
607 done(req_p);
608 return 0;
609 }
610
611 if (done) {
612 req_p->scsi_done = done;
613 } else {
614#ifdef ED_DBGP
615 printk( "atp870u_queuecommand: done can't be NULL\n");
616#endif
617 req_p->result = 0;
618 done(req_p);
619 return 0;
620 }
621
622 /*
623 * Count new command
624 */
625 dev->quend[c]++;
626 if (dev->quend[c] >= qcnt) {
627 dev->quend[c] = 0;
628 }
629
630 /*
631 * Check queue state
632 */
633 if (dev->quhd[c] == dev->quend[c]) {
634 if (dev->quend[c] == 0) {
635 dev->quend[c] = qcnt;
636 }
637#ifdef ED_DBGP
638 printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
639#endif
640 dev->quend[c]--;
641 req_p->result = 0x00020000;
642 done(req_p);
643 return 0;
644 }
645 dev->quereq[c][dev->quend[c]] = req_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#ifdef ED_DBGP
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100647 printk("dev->ioport[c] = %x atp_readb_io(dev, c, 0x1c) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],atp_readb_io(dev, c, 0x1c),c,dev->in_int[c],c,dev->in_snd[c]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100649 if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#ifdef ED_DBGP
651 printk("Call sent_s870(atp870u_queuecommand)\n");
652#endif
653 send_s870(dev,c);
654 }
655#ifdef ED_DBGP
656 printk("atp870u_queuecommand : exit\n");
657#endif
658 return 0;
659}
660
Jeff Garzikf2812332010-11-16 02:10:29 -0500661static DEF_SCSI_QCMD(atp870u_queuecommand)
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/**
664 * send_s870 - send a command to the controller
665 * @host: host
666 *
667 * On entry there is work queued to be done. We move some of that work to the
668 * controller itself.
669 *
670 * Caller holds the host lock.
671 */
672static void send_s870(struct atp_unit *dev,unsigned char c)
673{
Ondrej Zary468b8962015-11-17 19:23:50 +0100674 struct scsi_cmnd *workreq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned int i;//,k;
676 unsigned char j, target_id;
677 unsigned char *prd;
Ondrej Zaryc2bab402015-11-17 19:23:48 +0100678 unsigned short int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long l, bttl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned long sg_count;
681
682 if (dev->in_snd[c] != 0) {
683#ifdef ED_DBGP
684 printk("cmnd in_snd\n");
685#endif
686 return;
687 }
688#ifdef ED_DBGP
689 printk("Sent_s870 enter\n");
690#endif
691 dev->in_snd[c] = 1;
692 if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
693 dev->last_cmd[c] &= 0x0f;
694 workreq = dev->id[c][dev->last_cmd[c]].curr_req;
Ondrej Zary468b8962015-11-17 19:23:50 +0100695 if (!workreq) {
696 dev->last_cmd[c] = 0xff;
697 if (dev->quhd[c] == dev->quend[c]) {
698 dev->in_snd[c] = 0;
699 return;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 }
Ondrej Zary468b8962015-11-17 19:23:50 +0100703 if (!workreq) {
704 if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
705 dev->in_snd[c] = 0;
706 return;
707 }
708 dev->working[c]++;
709 j = dev->quhd[c];
710 dev->quhd[c]++;
711 if (dev->quhd[c] >= qcnt)
712 dev->quhd[c] = 0;
713 workreq = dev->quereq[c][dev->quhd[c]];
714 if (dev->id[c][scmd_id(workreq)].curr_req != NULL) {
715 dev->quhd[c] = j;
716 dev->working[c]--;
717 dev->in_snd[c] = 0;
718 return;
719 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400720 dev->id[c][scmd_id(workreq)].curr_req = workreq;
721 dev->last_cmd[c] = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100723 if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#ifdef ED_DBGP
Ondrej Zary468b8962015-11-17 19:23:50 +0100725 printk("Abort to Send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#endif
Ondrej Zary468b8962015-11-17 19:23:50 +0100727 dev->last_cmd[c] |= 0x40;
728 dev->in_snd[c] = 0;
729 return;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#ifdef ED_DBGP
732 printk("OK to Send\n");
Jeff Garzik422c0d62005-10-24 18:05:09 -0400733 scmd_printk(KERN_DEBUG, workreq, "CDB");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 for(i=0;i<workreq->cmd_len;i++) {
735 printk(" %x",workreq->cmnd[i]);
736 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400737 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300739 l = scsi_bufflen(workreq);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100742 j = atp_readb_base(dev, 0x29) & 0xfe;
743 atp_writeb_base(dev, 0x29, j);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400744 dev->r1f[c][scmd_id(workreq)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 if (workreq->cmnd[0] == READ_CAPACITY) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300748 if (l > 8)
749 l = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751 if (workreq->cmnd[0] == 0x00) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300752 l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 j = 0;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400756 target_id = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /*
759 * Wide ?
760 */
761 w = 1;
762 w = w << target_id;
763 if ((w & dev->wide_id[c]) != 0) {
764 j |= 0x01;
765 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100766 atp_writeb_io(dev, c, 0x1b, j);
767 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
768 atp_writeb_pci(dev, c, 0x1b, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#ifdef ED_DBGP
770 printk("send_s870 while loop 1\n");
771#endif
772 }
773 /*
774 * Write the command
775 */
776
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100777 atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
778 atp_writeb_io(dev, c, 0x01, 0x2c);
779 if (dev->dev_id == ATP885_DEVID)
780 atp_writeb_io(dev, c, 0x02, 0x7f);
781 else
782 atp_writeb_io(dev, c, 0x02, 0xcf);
783 for (i = 0; i < workreq->cmd_len; i++)
784 atp_writeb_io(dev, c, 0x03 + i, workreq->cmnd[i]);
785 atp_writeb_io(dev, c, 0x0f, workreq->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * Write the target
788 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100789 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#ifdef ED_DBGP
791 printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
792#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300793
794 sg_count = scsi_dma_map(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Write transfer size
797 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100798 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&l))[2]);
799 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&l))[1]);
800 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&l))[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 j = target_id;
802 dev->id[c][j].last_len = l;
803 dev->id[c][j].tran_len = 0;
804#ifdef ED_DBGP
805 printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
806#endif
807 /*
808 * Flip the wide bits
809 */
810 if ((j & 0x08) != 0) {
811 j = (j & 0x07) | 0x40;
812 }
813 /*
814 * Check transfer direction
815 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100816 if (workreq->sc_data_direction == DMA_TO_DEVICE)
817 atp_writeb_io(dev, c, 0x15, j | 0x20);
818 else
819 atp_writeb_io(dev, c, 0x15, j);
820 atp_writeb_io(dev, c, 0x16, atp_readb_io(dev, c, 0x16) | 0x80);
821 atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 dev->id[c][target_id].dirct = 0;
823 if (l == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100824 if (atp_readb_io(dev, c, 0x1c) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#ifdef ED_DBGP
826 printk("change SCSI_CMD_REG 0x08\n");
827#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100828 atp_writeb_io(dev, c, 0x18, 0x08);
829 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dev->last_cmd[c] |= 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 dev->in_snd[c] = 0;
832 return;
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 prd = dev->id[c][target_id].prd_table;
835 dev->id[c][target_id].prd_pos = prd;
836
837 /*
838 * Now write the request list. Either as scatter/gather or as
839 * a linear chain.
840 */
841
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300842 if (l) {
843 struct scatterlist *sgpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 i = 0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300845 scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
846 bttl = sg_dma_address(sgpnt);
847 l=sg_dma_len(sgpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#ifdef ED_DBGP
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300849 printk("1. bttl %x, l %x\n",bttl, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300851 while (l > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 (((u16 *) (prd))[i + 3]) = 0x0000;
853 (((u16 *) (prd))[i + 2]) = 0x0000;
854 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
855 l -= 0x10000;
856 bttl += 0x10000;
857 i += 0x04;
858 }
859 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
860 (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
861 (((u16 *) (prd))[i + 3]) = 0;
862 i += 0x04;
863 }
864 (((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
865#ifdef ED_DBGP
866 printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
867 printk("2. bttl %x, l %x\n",bttl, l);
868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#ifdef ED_DBGP
Ondrej Zaryc2bab402015-11-17 19:23:48 +0100871 printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#endif
James Bottomleyb5683552005-09-15 08:59:36 -0500873 dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100874 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
875 atp_writeb_pci(dev, c, 2, 0x06);
876 atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100878 j = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
880 (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
881 j |= 0x0c;
882 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100883 atp_writeb_pci(dev, c, 1, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else if ((dev->dev_id == ATP880_DEVID1) ||
885 (dev->dev_id == ATP880_DEVID2)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100886 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
887 atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
888 else
889 atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100891 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
892 atp_writeb_io(dev, c, 0x3a, (atp_readb_io(dev, c, 0x3a) & 0xf3) | 0x08);
893 else
894 atp_writeb_io(dev, c, 0x3a, atp_readb_io(dev, c, 0x3a) & 0xf3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if(workreq->sc_data_direction == DMA_TO_DEVICE) {
898 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100899 if (atp_readb_io(dev, c, 0x1c) == 0) {
900 atp_writeb_io(dev, c, 0x18, 0x08);
901 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#ifdef ED_DBGP
903 printk( "start DMA(to target)\n");
904#endif
905 } else {
906 dev->last_cmd[c] |= 0x40;
907 }
908 dev->in_snd[c] = 0;
909 return;
910 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100911 if (atp_readb_io(dev, c, 0x1c) == 0) {
912 atp_writeb_io(dev, c, 0x18, 0x08);
913 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#ifdef ED_DBGP
915 printk( "start DMA(to host)\n");
916#endif
917 } else {
918 dev->last_cmd[c] |= 0x40;
919 }
920 dev->in_snd[c] = 0;
921 return;
922
923}
924
925static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
926{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned short int i, k;
928 unsigned char j;
929
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100930 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100932 k = atp_readw_io(dev, 0, 0x1c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 j = (unsigned char) (k >> 8);
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100934 if ((k & 0x8000) != 0) /* DB7 all release? */
935 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 *val |= 0x4000; /* assert DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100938 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *val &= 0xdfff; /* assert DB5 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100940 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100942 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0) /* DB5 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100943 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 *val |= 0x8000; /* no DB4-0, assert DB7 */
946 *val &= 0xe0ff;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100947 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *val &= 0xbfff; /* release DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100949 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100951 if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0) /* DB6 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100952 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 return j;
956}
957
958static void tscam(struct Scsi_Host *host)
959{
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unsigned char i, j, k;
962 unsigned long n;
963 unsigned short int m, assignid_map, val;
964 unsigned char mbuf[33], quintet[2];
965 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
966 static unsigned char g2q_tab[8] = {
967 0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
968 };
969
970/* I can't believe we need this before we've even done anything. Remove it
971 * and see if anyone bitches.
972 for (i = 0; i < 0x10; i++) {
973 udelay(0xffff);
974 }
975 */
976
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100977 atp_writeb_io(dev, 0, 1, 0x08);
978 atp_writeb_io(dev, 0, 2, 0x7f);
979 atp_writeb_io(dev, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if ((dev->scam_on & 0x40) == 0) {
982 return;
983 }
984 m = 1;
985 m <<= dev->host_id[0];
986 j = 16;
987 if (dev->chip_ver < 4) {
988 m |= 0xff00;
989 j = 8;
990 }
991 assignid_map = m;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100992 atp_writeb_io(dev, 0, 0x02, 0x02); /* 2*2=4ms,3EH 2/32*3E=3.9ms */
993 atp_writeb_io(dev, 0, 0x03, 0);
994 atp_writeb_io(dev, 0, 0x04, 0);
995 atp_writeb_io(dev, 0, 0x05, 0);
996 atp_writeb_io(dev, 0, 0x06, 0);
997 atp_writeb_io(dev, 0, 0x07, 0);
998 atp_writeb_io(dev, 0, 0x08, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 for (i = 0; i < j; i++) {
1001 m = 1;
1002 m = m << i;
1003 if ((m & assignid_map) != 0) {
1004 continue;
1005 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001006 atp_writeb_io(dev, 0, 0x0f, 0);
1007 atp_writeb_io(dev, 0, 0x12, 0);
1008 atp_writeb_io(dev, 0, 0x13, 0);
1009 atp_writeb_io(dev, 0, 0x14, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (i > 7) {
1011 k = (i & 0x07) | 0x40;
1012 } else {
1013 k = i;
1014 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001015 atp_writeb_io(dev, 0, 0x15, k);
1016 if (dev->chip_ver == 4)
1017 atp_writeb_io(dev, 0, 0x1b, 0x01);
1018 else
1019 atp_writeb_io(dev, 0, 0x1b, 0x00);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001020 do {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001021 atp_writeb_io(dev, 0, 0x18, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001023 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001024 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001025 k = atp_readb_io(dev, 0, 0x17);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001026 if ((k == 0x85) || (k == 0x42))
1027 break;
1028 if (k != 0x16)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001029 atp_writeb_io(dev, 0, 0x10, 0x41);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001030 } while (k != 0x16);
1031 if ((k == 0x85) || (k == 0x42))
1032 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 assignid_map |= m;
1034
1035 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001036 atp_writeb_io(dev, 0, 0x02, 0x7f);
1037 atp_writeb_io(dev, 0, 0x1b, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 outb(0, 0x80);
1040
1041 val = 0x0080; /* bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001042 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 val |= 0x0040; /* sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001044 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 val |= 0x0004; /* msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001046 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 inb(0x80); /* 2 deskew delay(45ns*2=90ns) */
1048 val &= 0x007f; /* no bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001049 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 mdelay(128);
1051 val &= 0x00fb; /* after 1ms no msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001052 atp_writew_io(dev, 0, 0x1c, val);
1053 while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001054 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 outb(1, 0x80);
1056 udelay(100);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001057 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001058 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0) /* bsy ? */
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001059 break;
1060 if (n < 0x30000)
1061 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001062 if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001063 inb(0x80);
1064 val |= 0x8003; /* io,cd,db7 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001065 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001066 inb(0x80);
1067 val &= 0x00bf; /* no sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001068 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001069 outb(2, 0x80);
1070 break;
1071 }
1072 while (1) {
Martin Michlmayr0f6d93a2012-10-04 17:11:25 -07001073 /*
1074 * The funny division into multiple delays is to accomodate
1075 * arches like ARM where udelay() multiplies its argument by
1076 * a large number to initialize a loop counter. To avoid
1077 * overflow, the maximum supported udelay is 2000 microseconds.
1078 *
1079 * XXX it would be more polite to find a way to use msleep()
1080 */
1081 mdelay(2);
1082 udelay(48);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001083 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) == 0x00) { /* bsy ? */
1084 atp_writew_io(dev, 0, 0x1c, 0);
1085 atp_writeb_io(dev, 0, 0x1b, 0);
1086 atp_writeb_io(dev, 0, 0x15, 0);
1087 atp_writeb_io(dev, 0, 0x18, 0x09);
1088 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001090 atp_readb_io(dev, 0, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return;
1092 }
1093 val &= 0x00ff; /* synchronization */
1094 val |= 0x3f00;
1095 fun_scam(dev, &val);
1096 outb(3, 0x80);
1097 val &= 0x00ff; /* isolation */
1098 val |= 0x2000;
1099 fun_scam(dev, &val);
1100 outb(4, 0x80);
1101 i = 8;
1102 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001104 while (1) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001105 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001106 continue;
1107 outb(5, 0x80);
1108 val &= 0x00ff; /* get ID_STRING */
1109 val |= 0x2000;
1110 k = fun_scam(dev, &val);
1111 if ((k & 0x03) == 0)
1112 break;
1113 mbuf[j] <<= 0x01;
1114 mbuf[j] &= 0xfe;
1115 if ((k & 0x02) != 0)
1116 mbuf[j] |= 0x01;
1117 i--;
1118 if (i > 0)
1119 continue;
1120 j++;
1121 i = 8;
1122 }
1123
1124 /* isolation complete.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* mbuf[32]=0;
1126 printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
1127 i = 15;
1128 j = mbuf[0];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001129 if ((j & 0x20) != 0) { /* bit5=1:ID up to 7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 i = 7;
1131 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001132 if ((j & 0x06) != 0) { /* IDvalid? */
1133 k = mbuf[1];
1134 while (1) {
1135 m = 1;
1136 m <<= k;
1137 if ((m & assignid_map) == 0)
1138 break;
1139 if (k > 0)
1140 k--;
1141 else
1142 break;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001145 if ((m & assignid_map) != 0) { /* srch from max acceptable ID# */
1146 k = i; /* max acceptable ID# */
1147 while (1) {
1148 m = 1;
1149 m <<= k;
1150 if ((m & assignid_map) == 0)
1151 break;
1152 if (k > 0)
1153 k--;
1154 else
1155 break;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001158 /* k=binID#, */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 assignid_map |= m;
1160 if (k < 8) {
1161 quintet[0] = 0x38; /* 1st dft ID<8 */
1162 } else {
1163 quintet[0] = 0x31; /* 1st ID>=8 */
1164 }
1165 k &= 0x07;
1166 quintet[1] = g2q_tab[k];
1167
1168 val &= 0x00ff; /* AssignID 1stQuintet,AH=001xxxxx */
1169 m = quintet[0] << 8;
1170 val |= m;
1171 fun_scam(dev, &val);
1172 val &= 0x00ff; /* AssignID 2ndQuintet,AH=001xxxxx */
1173 m = quintet[1] << 8;
1174 val |= m;
1175 fun_scam(dev, &val);
1176
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180static void atp870u_free_tables(struct Scsi_Host *host)
1181{
1182 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1183 int j, k;
1184 for (j=0; j < 2; j++) {
1185 for (k = 0; k < 16; k++) {
1186 if (!atp_dev->id[j][k].prd_table)
1187 continue;
James Bottomleyb5683552005-09-15 08:59:36 -05001188 pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 atp_dev->id[j][k].prd_table = NULL;
1190 }
1191 }
1192}
1193
1194static int atp870u_init_tables(struct Scsi_Host *host)
1195{
1196 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1197 int c,k;
1198 for(c=0;c < 2;c++) {
1199 for(k=0;k<16;k++) {
James Bottomleyb5683552005-09-15 08:59:36 -05001200 atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!atp_dev->id[c][k].prd_table) {
1202 printk("atp870u_init_tables fail\n");
1203 atp870u_free_tables(host);
1204 return -ENOMEM;
1205 }
James Bottomleyb5683552005-09-15 08:59:36 -05001206 atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 atp_dev->id[c][k].devsp=0x20;
1208 atp_dev->id[c][k].devtype = 0x7f;
1209 atp_dev->id[c][k].curr_req = NULL;
1210 }
1211
1212 atp_dev->active_id[c] = 0;
1213 atp_dev->wide_id[c] = 0;
1214 atp_dev->host_id[c] = 0x07;
1215 atp_dev->quhd[c] = 0;
1216 atp_dev->quend[c] = 0;
1217 atp_dev->last_cmd[c] = 0xff;
1218 atp_dev->in_snd[c] = 0;
1219 atp_dev->in_int[c] = 0;
1220
1221 for (k = 0; k < qcnt; k++) {
1222 atp_dev->quereq[c][k] = NULL;
1223 }
1224 for (k = 0; k < 16; k++) {
1225 atp_dev->id[c][k].curr_req = NULL;
1226 atp_dev->sp[c][k] = 0x04;
1227 }
1228 }
1229 return 0;
1230}
1231
1232/* return non-zero on detection */
1233static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1234{
1235 unsigned char k, m, c;
1236 unsigned long flags;
Ondrej Zary493c5202015-11-17 19:23:44 +01001237 unsigned int base_io, error,n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 unsigned char host_id;
1239 struct Scsi_Host *shpnt = NULL;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001240 struct atp_unit *atpdev, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned char setupdata[2][16];
1242 int count = 0;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001243
1244 atpdev = kzalloc(sizeof(*atpdev), GFP_KERNEL);
1245 if (!atpdev)
1246 return -ENOMEM;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (pci_enable_device(pdev))
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001249 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Yang Hongyang284901a2009-04-06 19:01:15 -07001251 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk(KERN_INFO "atp870u: use 32bit DMA mask.\n");
1253 } else {
1254 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001255 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * It's probably easier to weed out some revisions like
1260 * this than via the PCI device table
1261 */
1262 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610) {
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03001263 atpdev->chip_ver = pdev->revision;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001264 if (atpdev->chip_ver < 2)
1265 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
1268 switch (ent->device) {
1269 case PCI_DEVICE_ID_ARTOP_AEC7612UW:
1270 case PCI_DEVICE_ID_ARTOP_AEC7612SUW:
1271 case ATP880_DEVID1:
1272 case ATP880_DEVID2:
1273 case ATP885_DEVID:
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001274 atpdev->chip_ver = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 default:
1276 break;
1277 }
1278 base_io = pci_resource_start(pdev, 0);
1279 base_io &= 0xfffffff8;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001280 atpdev->baseport = base_io;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03001283 atpdev->chip_ver = pdev->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803
1285
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001286 atpdev->ioport[0] = base_io + 0x40;
1287 atpdev->pciport[0] = base_io + 0x28;
1288
1289 host_id = atp_readb_base(atpdev, 0x39);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 host_id >>= 0x04;
1291
1292 printk(KERN_INFO " ACARD AEC-67160 PCI Ultra3 LVD Host Adapter: %d"
1293 " IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001294 atpdev->dev_id = ent->device;
1295 atpdev->host_id[0] = host_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001297 atpdev->scam_on = atp_readb_base(atpdev, 0x22);
1298 atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
1299 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 n = 0x3f09;
1302next_fblk_880:
1303 if (n >= 0x4000)
1304 goto flash_ok_880;
1305
1306 m = 0;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001307 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001309 if (atp_readb_base(atpdev, 0x30) == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 goto flash_ok_880;
1311
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001312 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1313 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1314 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1315 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1316 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001318 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1319 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1320 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1321 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1322 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001324 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1325 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1326 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1327 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1328 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001330 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1331 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1332 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1333 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 n += 0x0018;
1335 goto next_fblk_880;
1336flash_ok_880:
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001337 atp_writew_base(atpdev, 0x34, 0);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001338 atpdev->ultra_map[0] = 0;
1339 atpdev->async[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 for (k = 0; k < 16; k++) {
1341 n = 1;
1342 n = n << k;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001343 if (atpdev->sp[0][k] > 1) {
1344 atpdev->ultra_map[0] |= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 } else {
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001346 if (atpdev->sp[0][k] == 0)
1347 atpdev->async[0] |= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 }
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001350 atpdev->async[0] = ~(atpdev->async[0]);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001351 atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1354 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001355 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 p = (struct atp_unit *)&shpnt->hostdata;
1358
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001359 atpdev->host = shpnt;
1360 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001362 memcpy(p, atpdev, sizeof(*atpdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (atp870u_init_tables(shpnt) < 0) {
1364 printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
1365 goto unregister;
1366 }
1367
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001368 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1370 goto free_tables;
1371 }
1372
1373 spin_lock_irqsave(shpnt->host_lock, flags);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001374 k = atp_readb_base(p, 0x38) & 0x80;
1375 atp_writeb_base(p, 0x38, k);
1376 atp_writeb_base(p, 0x3b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001378 atp_writeb_base(p, 0x3b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001380 atp_readb_io(p, 0, 0x1b);
1381 atp_readb_io(p, 0, 0x17);
1382 atp_writeb_io(p, 0, 0, host_id | 0x08);
1383 atp_writeb_io(p, 0, 0x18, 0);
1384 while ((atp_readb_io(p, 0, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 mdelay(1);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001386 atp_readb_io(p, 0, 0x17);
1387 atp_writeb_io(p, 0, 1, 8);
1388 atp_writeb_io(p, 0, 2, 0x7f);
1389 atp_writeb_io(p, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 tscam(shpnt);
Ondrej Zary4192a402015-11-17 19:24:06 +01001392 atp_is(p, 0, true, atp_readb_base(p, 0x3f) & 0x40);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001393 atp_writeb_base(p, 0x38, 0xb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 shpnt->max_id = 16;
1395 shpnt->this_id = host_id;
1396 shpnt->unique_id = base_io;
1397 shpnt->io_port = base_io;
1398 shpnt->n_io_port = 0x60; /* Number of bytes of I/O space used */
1399 shpnt->irq = pdev->irq;
1400 } else if (ent->device == ATP885_DEVID) {
1401 printk(KERN_INFO " ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%x, IRQ:%d.\n"
1402 , base_io, pdev->irq);
1403
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001404 atpdev->pdev = pdev;
1405 atpdev->dev_id = ent->device;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001406 atpdev->ioport[0] = base_io + 0x80;
1407 atpdev->ioport[1] = base_io + 0xc0;
1408 atpdev->pciport[0] = base_io + 0x40;
1409 atpdev->pciport[1] = base_io + 0x50;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1412 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001413 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 p = (struct atp_unit *)&shpnt->hostdata;
1416
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001417 atpdev->host = shpnt;
1418 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001420 memcpy(p, atpdev, sizeof(struct atp_unit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (atp870u_init_tables(shpnt) < 0)
1422 goto unregister;
1423
1424#ifdef ED_DBGP
1425 printk("request_irq() shpnt %p hostdata %p\n", shpnt, p);
1426#endif
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001427 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
1429 goto free_tables;
1430 }
1431
1432 spin_lock_irqsave(shpnt->host_lock, flags);
1433
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001434 c = atp_readb_base(p, 0x29);
1435 atp_writeb_base(p, 0x29, c | 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 n=0x1f80;
1438next_fblk_885:
1439 if (n >= 0x2000) {
1440 goto flash_ok_885;
1441 }
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001442 atp_writew_base(p, 0x3c, n);
1443 if (atp_readl_base(p, 0x38) == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto flash_ok_885;
1445 }
1446 for (m=0; m < 2; m++) {
1447 p->global_map[m]= 0;
1448 for (k=0; k < 4; k++) {
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001449 atp_writew_base(p, 0x3c, n++);
1450 ((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(p, 0x38);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452 for (k=0; k < 4; k++) {
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001453 atp_writew_base(p, 0x3c, n++);
1454 ((unsigned long *)&p->sp[m][0])[k] = atp_readl_base(p, 0x38);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 n += 8;
1457 }
1458 goto next_fblk_885;
1459flash_ok_885:
1460#ifdef ED_DBGP
1461 printk( "Flash Read OK\n");
1462#endif
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001463 c = atp_readb_base(p, 0x29);
1464 atp_writeb_base(p, 0x29, c & 0xfb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 for (c=0;c < 2;c++) {
1466 p->ultra_map[c]=0;
1467 p->async[c] = 0;
1468 for (k=0; k < 16; k++) {
1469 n=1;
1470 n = n << k;
1471 if (p->sp[c][k] > 1) {
1472 p->ultra_map[c] |= n;
1473 } else {
1474 if (p->sp[c][k] == 0) {
1475 p->async[c] |= n;
1476 }
1477 }
1478 }
1479 p->async[c] = ~(p->async[c]);
1480
1481 if (p->global_map[c] == 0) {
1482 k=setupdata[c][1];
1483 if ((k & 0x40) != 0)
1484 p->global_map[c] |= 0x20;
1485 k &= 0x07;
1486 p->global_map[c] |= k;
1487 if ((setupdata[c][2] & 0x04) != 0)
1488 p->global_map[c] |= 0x08;
1489 p->host_id[c] = setupdata[c][0] & 0x07;
1490 }
1491 }
1492
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001493 k = atp_readb_base(p, 0x28) & 0x8f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 k |= 0x10;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001495 atp_writeb_base(p, 0x28, k);
1496 atp_writeb_pci(p, 0, 1, 0x80);
1497 atp_writeb_pci(p, 1, 1, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 mdelay(100);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001499 atp_writeb_pci(p, 0, 1, 0);
1500 atp_writeb_pci(p, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 mdelay(1000);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001502 atp_readb_io(p, 0, 0x1b);
1503 atp_readb_io(p, 0, 0x17);
1504 atp_readb_io(p, 1, 0x1b);
1505 atp_readb_io(p, 1, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 k=p->host_id[0];
1507 if (k > 7)
1508 k = (k & 0x07) | 0x40;
1509 k |= 0x08;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001510 atp_writeb_io(p, 0, 0, k);
1511 atp_writeb_io(p, 0, 0x18, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001513 while ((atp_readb_io(p, 0, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 cpu_relax();
1515
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001516 atp_readb_io(p, 0, 0x17);
1517 atp_writeb_io(p, 0, 1, 8);
1518 atp_writeb_io(p, 0, 2, 0x7f);
1519 atp_writeb_io(p, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 k=p->host_id[1];
1522 if (k > 7)
1523 k = (k & 0x07) | 0x40;
1524 k |= 0x08;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001525 atp_writeb_io(p, 1, 0, k);
1526 atp_writeb_io(p, 1, 0x18, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001528 while ((atp_readb_io(p, 1, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 cpu_relax();
1530
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001531 atp_readb_io(p, 1, 0x17);
1532 atp_writeb_io(p, 1, 1, 8);
1533 atp_writeb_io(p, 1, 2, 0x7f);
1534 atp_writeb_io(p, 1, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 tscam_885();
1537 printk(KERN_INFO " Scanning Channel A SCSI Device ...\n");
Ondrej Zary4192a402015-11-17 19:24:06 +01001538 atp_is(p, 0, true, atp_readb_io(p, 0, 0x1b) >> 7);
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001539 atp_writeb_io(p, 0, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 printk(KERN_INFO " Scanning Channel B SCSI Device ...\n");
Ondrej Zary4192a402015-11-17 19:24:06 +01001541 atp_is(p, 1, true, atp_readb_io(p, 1, 0x1b) >> 7);
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001542 atp_writeb_io(p, 1, 0x16, 0x80);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001543 k = atp_readb_base(p, 0x28) & 0xcf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 k |= 0xc0;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001545 atp_writeb_base(p, 0x28, k);
1546 k = atp_readb_base(p, 0x1f) | 0x80;
1547 atp_writeb_base(p, 0x1f, k);
1548 k = atp_readb_base(p, 0x29) | 0x01;
1549 atp_writeb_base(p, 0x29, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550#ifdef ED_DBGP
1551 //printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
1552#endif
1553 shpnt->max_id = 16;
1554 shpnt->max_lun = (p->global_map[0] & 0x07) + 1;
1555 shpnt->max_channel = 1;
1556 shpnt->this_id = p->host_id[0];
1557 shpnt->unique_id = base_io;
1558 shpnt->io_port = base_io;
1559 shpnt->n_io_port = 0xff; /* Number of bytes of I/O space used */
1560 shpnt->irq = pdev->irq;
1561
1562 } else {
1563 error = pci_read_config_byte(pdev, 0x49, &host_id);
1564
1565 printk(KERN_INFO " ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: %d "
1566 "IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
1567
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001568 atpdev->ioport[0] = base_io;
1569 atpdev->pciport[0] = base_io + 0x20;
1570 atpdev->dev_id = ent->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 host_id &= 0x07;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001572 atpdev->host_id[0] = host_id;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001573 atpdev->scam_on = atp_readb_pci(atpdev, 0, 2);
1574 atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
1575 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001577 if (atpdev->ultra_map[0] == 0) {
1578 atpdev->scam_on = 0x00;
1579 atpdev->global_map[0] = 0x20;
1580 atpdev->ultra_map[0] = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
1583 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1584 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001585 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 p = (struct atp_unit *)&shpnt->hostdata;
1588
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001589 atpdev->host = shpnt;
1590 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001592 memcpy(p, atpdev, sizeof(*atpdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (atp870u_init_tables(shpnt) < 0)
1594 goto unregister;
1595
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001596 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870i", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1598 goto free_tables;
1599 }
1600
1601 spin_lock_irqsave(shpnt->host_lock, flags);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001602 if (atpdev->chip_ver > 0x07) /* check if atp876 chip then enable terminator */
1603 atp_writeb_base(p, 0x3e, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001605 k = (atp_readb_base(p, 0x3a) & 0xf3) | 0x10;
1606 atp_writeb_base(p, 0x3a, k);
1607 atp_writeb_base(p, 0x3a, k & 0xdf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001609 atp_writeb_base(p, 0x3a, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001611 atp_writeb_io(p, 0, 0, host_id | 0x08);
1612 atp_writeb_io(p, 0, 0x18, 0);
1613 while ((atp_readb_io(p, 0, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 mdelay(1);
1615
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001616 atp_readb_io(p, 0, 0x17);
1617 atp_writeb_io(p, 0, 1, 8);
1618 atp_writeb_io(p, 0, 2, 0x7f);
1619 atp_writeb_io(p, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 tscam(shpnt);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001622 atp_writeb_base(p, 0x3a, atp_readb_base(p, 0x3a) | 0x10);
Ondrej Zary4192a402015-11-17 19:24:06 +01001623 atp_is(p, 0, p->chip_ver == 4, 0);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001624 atp_writeb_base(p, 0x3a, atp_readb_base(p, 0x3a) & 0xef);
1625 atp_writeb_base(p, 0x3b, atp_readb_base(p, 0x3b) | 0x20);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001626 if (atpdev->chip_ver == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 shpnt->max_id = 16;
1628 else
Hannes Reinecke2b89dad2006-05-23 10:29:28 +02001629 shpnt->max_id = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 shpnt->this_id = host_id;
1631 shpnt->unique_id = base_io;
1632 shpnt->io_port = base_io;
1633 shpnt->n_io_port = 0x40; /* Number of bytes of I/O space used */
1634 shpnt->irq = pdev->irq;
1635 }
1636 spin_unlock_irqrestore(shpnt->host_lock, flags);
1637 if(ent->device==ATP885_DEVID) {
1638 if(!request_region(base_io, 0xff, "atp870u")) /* Register the IO ports that we use */
1639 goto request_io_fail;
1640 } else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
1641 if(!request_region(base_io, 0x60, "atp870u")) /* Register the IO ports that we use */
1642 goto request_io_fail;
1643 } else {
1644 if(!request_region(base_io, 0x40, "atp870u")) /* Register the IO ports that we use */
1645 goto request_io_fail;
1646 }
1647 count++;
1648 if (scsi_add_host(shpnt, &pdev->dev))
1649 goto scsi_add_fail;
1650 scsi_scan_host(shpnt);
1651#ifdef ED_DBGP
1652 printk("atp870u_prob : exit\n");
1653#endif
1654 return 0;
1655
1656scsi_add_fail:
1657 printk("atp870u_prob:scsi_add_fail\n");
1658 if(ent->device==ATP885_DEVID) {
1659 release_region(base_io, 0xff);
1660 } else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
1661 release_region(base_io, 0x60);
1662 } else {
1663 release_region(base_io, 0x40);
1664 }
1665request_io_fail:
1666 printk("atp870u_prob:request_io_fail\n");
1667 free_irq(pdev->irq, shpnt);
1668free_tables:
1669 printk("atp870u_prob:free_table\n");
1670 atp870u_free_tables(shpnt);
1671unregister:
1672 printk("atp870u_prob:unregister\n");
1673 scsi_host_put(shpnt);
1674 return -1;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001675err_eio:
1676 kfree(atpdev);
1677 return -EIO;
1678err_nomem:
1679 kfree(atpdev);
1680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/* The abort command does not leave the device in a clean state where
1684 it is available to be used again. Until this gets worked out, we will
1685 leave it commented out. */
1686
1687static int atp870u_abort(struct scsi_cmnd * SCpnt)
1688{
1689 unsigned char j, k, c;
1690 struct scsi_cmnd *workrequ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct atp_unit *dev;
1692 struct Scsi_Host *host;
1693 host = SCpnt->device->host;
1694
1695 dev = (struct atp_unit *)&host->hostdata;
Jeff Garzik422c0d62005-10-24 18:05:09 -04001696 c = scmd_channel(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 printk(" atp870u: abort Channel = %x \n", c);
1698 printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
1699 printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 for (j = 0; j < 0x18; j++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001701 printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001703 printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
1704 printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
1705 printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
1706 printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 for(j=0;j<16;j++) {
1708 if (dev->id[c][j].curr_req != NULL) {
1709 workrequ = dev->id[c][j].curr_req;
1710 printk("\n que cdb= ");
1711 for (k=0; k < workrequ->cmd_len; k++) {
1712 printk(" %2x ",workrequ->cmnd[k]);
1713 }
1714 printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
1715 }
1716 }
1717 return SUCCESS;
1718}
1719
1720static const char *atp870u_info(struct Scsi_Host *notused)
1721{
1722 static char buffer[128];
1723
1724 strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
1725
1726 return buffer;
1727}
1728
Al Virod773e422013-03-31 03:26:26 -04001729static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Rasmus Villemoes3d300792014-12-03 00:10:53 +01001731 seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
1732 "Adapter Configuration:\n");
Al Virod773e422013-03-31 03:26:26 -04001733 seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
1734 seq_printf(m, " IRQ: %d\n", HBAptr->irq);
1735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738
1739static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
1740 sector_t capacity, int *ip)
1741{
1742 int heads, sectors, cylinders;
1743
1744 heads = 64;
1745 sectors = 32;
1746 cylinders = (unsigned long)capacity / (heads * sectors);
1747 if (cylinders > 1024) {
1748 heads = 255;
1749 sectors = 63;
1750 cylinders = (unsigned long)capacity / (heads * sectors);
1751 }
1752 ip[0] = heads;
1753 ip[1] = sectors;
1754 ip[2] = cylinders;
1755
1756 return 0;
1757}
1758
1759static void atp870u_remove (struct pci_dev *pdev)
1760{
1761 struct atp_unit *devext = pci_get_drvdata(pdev);
1762 struct Scsi_Host *pshost = devext->host;
1763
1764
1765 scsi_remove_host(pshost);
1766 printk(KERN_INFO "free_irq : %d\n",pshost->irq);
1767 free_irq(pshost->irq, pshost);
1768 release_region(pshost->io_port, pshost->n_io_port);
1769 printk(KERN_INFO "atp870u_free_tables : %p\n",pshost);
1770 atp870u_free_tables(pshost);
1771 printk(KERN_INFO "scsi_host_put : %p\n",pshost);
1772 scsi_host_put(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774MODULE_LICENSE("GPL");
1775
1776static struct scsi_host_template atp870u_template = {
1777 .module = THIS_MODULE,
1778 .name = "atp870u" /* name */,
1779 .proc_name = "atp870u",
Al Virod773e422013-03-31 03:26:26 -04001780 .show_info = atp870u_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 .info = atp870u_info /* info */,
1782 .queuecommand = atp870u_queuecommand /* queuecommand */,
1783 .eh_abort_handler = atp870u_abort /* abort */,
1784 .bios_param = atp870u_biosparam /* biosparm */,
1785 .can_queue = qcnt /* can_queue */,
1786 .this_id = 7 /* SCSI ID */,
1787 .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/ /*SG_NONE*/,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .use_clustering = ENABLE_CLUSTERING,
1789 .max_sectors = ATP870U_MAX_SECTORS,
1790};
1791
1792static struct pci_device_id atp870u_id_table[] = {
1793 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
1794 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
1795 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
1796 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
1797 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
1798 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
1799 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
1800 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
1801 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
1802 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
1803 { 0, },
1804};
1805
1806MODULE_DEVICE_TABLE(pci, atp870u_id_table);
1807
1808static struct pci_driver atp870u_driver = {
1809 .id_table = atp870u_id_table,
1810 .name = "atp870u",
1811 .probe = atp870u_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001812 .remove = atp870u_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813};
1814
1815static int __init atp870u_init(void)
1816{
1817#ifdef ED_DBGP
1818 printk("atp870u_init: Entry\n");
1819#endif
1820 return pci_register_driver(&atp870u_driver);
1821}
1822
1823static void __exit atp870u_exit(void)
1824{
1825#ifdef ED_DBGP
1826 printk("atp870u_exit: Entry\n");
1827#endif
1828 pci_unregister_driver(&atp870u_driver);
1829}
1830
1831static void tscam_885(void)
1832{
1833 unsigned char i;
1834
1835 for (i = 0; i < 0x2; i++) {
1836 mdelay(300);
1837 }
1838 return;
1839}
1840
1841
1842
Ondrej Zary4192a402015-11-17 19:24:06 +01001843static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001845 unsigned char i, j, k, rmb, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 unsigned short int m;
1847 static unsigned char mbuf[512];
Ondrej Zary80b52a72015-11-17 19:23:58 +01001848 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1849 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1850 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1851 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1852 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
Ondrej Zary460da9182015-11-17 19:24:03 +01001853 static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
Ondrej Zary80b52a72015-11-17 19:23:58 +01001854 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1855 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1856 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 for (i = 0; i < 16; i++) {
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001859 if (!wide_chip && (i > 7))
1860 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 m = 1;
1862 m = m << i;
1863 if ((m & dev->active_id[c]) != 0) {
1864 continue;
1865 }
1866 if (i == dev->host_id[c]) {
1867 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
1868 continue;
1869 }
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001870 atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001871 atp_writeb_io(dev, c, 1, 0x08);
1872 atp_writeb_io(dev, c, 2, 0x7f);
1873 atp_writeb_io(dev, c, 3, satn[0]);
1874 atp_writeb_io(dev, c, 4, satn[1]);
1875 atp_writeb_io(dev, c, 5, satn[2]);
1876 atp_writeb_io(dev, c, 6, satn[3]);
1877 atp_writeb_io(dev, c, 7, satn[4]);
1878 atp_writeb_io(dev, c, 8, satn[5]);
1879 atp_writeb_io(dev, c, 0x0f, 0);
1880 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001881 atp_writeb_io(dev, c, 0x12, 0);
1882 atp_writeb_io(dev, c, 0x13, satn[6]);
1883 atp_writeb_io(dev, c, 0x14, satn[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 j = i;
1885 if ((j & 0x08) != 0) {
1886 j = (j & 0x07) | 0x40;
1887 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001888 atp_writeb_io(dev, c, 0x15, j);
1889 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001891 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001893
1894 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001896
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001897 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 dev->active_id[c] |= m;
1901
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001902 atp_writeb_io(dev, c, 0x10, 0x30);
Ondrej Zary460da9182015-11-17 19:24:03 +01001903 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
1904 atp_writeb_io(dev, c, 0x14, 0x00);
1905 else /* result of is870() merge - is this a bug? */
1906 atp_writeb_io(dev, c, 0x04, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908phase_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001909 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001910
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001911 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001913
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001914 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (j != 0x16) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001916 atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto phase_cmd;
1918 }
1919sel_ok:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001920 atp_writeb_io(dev, c, 3, inqd[0]);
1921 atp_writeb_io(dev, c, 4, inqd[1]);
1922 atp_writeb_io(dev, c, 5, inqd[2]);
1923 atp_writeb_io(dev, c, 6, inqd[3]);
1924 atp_writeb_io(dev, c, 7, inqd[4]);
1925 atp_writeb_io(dev, c, 8, inqd[5]);
1926 atp_writeb_io(dev, c, 0x0f, 0);
1927 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1928 atp_writeb_io(dev, c, 0x12, 0);
1929 atp_writeb_io(dev, c, 0x13, inqd[6]);
1930 atp_writeb_io(dev, c, 0x14, inqd[7]);
1931 atp_writeb_io(dev, c, 0x18, inqd[8]);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001932
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001933 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001935
1936 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001938
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001939 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001941
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001942 if (wide_chip)
1943 atp_writeb_io(dev, c, 0x1b, 0x00);
1944
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001945 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 j = 0;
1947rd_inq_data:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001948 k = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if ((k & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001950 mbuf[j++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto rd_inq_data;
1952 }
1953 if ((k & 0x80) == 0) {
1954 goto rd_inq_data;
1955 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001956 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (j == 0x16) {
1958 goto inq_ok;
1959 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001960 atp_writeb_io(dev, c, 0x10, 0x46);
1961 atp_writeb_io(dev, c, 0x12, 0);
1962 atp_writeb_io(dev, c, 0x13, 0);
1963 atp_writeb_io(dev, c, 0x14, 0);
1964 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001965
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001966 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001968
1969 if (atp_readb_io(dev, c, 0x17) != 0x16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto sel_ok;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972inq_ok:
1973 mbuf[36] = 0;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001974 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 dev->id[c][i].devtype = mbuf[0];
1976 rmb = mbuf[1];
1977 n = mbuf[7];
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001978 if (!wide_chip)
1979 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if ((mbuf[7] & 0x60) == 0) {
1981 goto not_wide;
1982 }
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001983 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
1984 if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
1985 goto not_wide;
1986 } else { /* result of is870() merge - is this a bug? */
1987 if ((dev->global_map[c] & 0x20) == 0)
1988 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990 if (lvdmode == 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01001991 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01001993 if (dev->sp[c][i] != 0x04) // force u2
1994 {
1995 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001998 atp_writeb_io(dev, c, 0x1b, 0x01);
1999 atp_writeb_io(dev, c, 3, satn[0]);
2000 atp_writeb_io(dev, c, 4, satn[1]);
2001 atp_writeb_io(dev, c, 5, satn[2]);
2002 atp_writeb_io(dev, c, 6, satn[3]);
2003 atp_writeb_io(dev, c, 7, satn[4]);
2004 atp_writeb_io(dev, c, 8, satn[5]);
2005 atp_writeb_io(dev, c, 0x0f, 0);
2006 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2007 atp_writeb_io(dev, c, 0x12, 0);
2008 atp_writeb_io(dev, c, 0x13, satn[6]);
2009 atp_writeb_io(dev, c, 0x14, satn[7]);
2010 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002012 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002014
2015 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002017
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002018 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021try_u3:
2022 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002023 atp_writeb_io(dev, c, 0x14, 0x09);
2024 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002026 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2027 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2028 atp_writeb_io(dev, c, 0x19, u3[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 cpu_relax();
2030 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002031
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002032 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002034
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002035 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (j == 0x0f) {
2037 goto u3p_in;
2038 }
2039 if (j == 0x0a) {
2040 goto u3p_cmd;
2041 }
2042 if (j == 0x0e) {
2043 goto try_u3;
2044 }
2045 continue;
2046u3p_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002047 atp_writeb_io(dev, c, 0x18, 0x20);
2048 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2049 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2050 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 cpu_relax();
2052 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002053 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (j == 0x0f) {
2055 goto u3p_in;
2056 }
2057 if (j == 0x0a) {
2058 goto u3p_cmd;
2059 }
2060 if (j == 0x0e) {
2061 goto u3p_out;
2062 }
2063 continue;
2064u3p_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002065 atp_writeb_io(dev, c, 0x14, 0x09);
2066 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 k = 0;
2068u3p_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002069 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002071 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 goto u3p_in1;
2073 }
2074 if ((j & 0x80) == 0x00) {
2075 goto u3p_in1;
2076 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002077 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (j == 0x0f) {
2079 goto u3p_in;
2080 }
2081 if (j == 0x0a) {
2082 goto u3p_cmd;
2083 }
2084 if (j == 0x0e) {
2085 goto u3p_out;
2086 }
2087 continue;
2088u3p_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002089 atp_writeb_io(dev, c, 0x10, 0x30);
2090 atp_writeb_io(dev, c, 0x14, 0x00);
2091 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002092
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002093 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002094
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002095 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (j != 0x16) {
2097 if (j == 0x4e) {
2098 goto u3p_out;
2099 }
2100 continue;
2101 }
2102 if (mbuf[0] != 0x01) {
2103 goto chg_wide;
2104 }
2105 if (mbuf[1] != 0x06) {
2106 goto chg_wide;
2107 }
2108 if (mbuf[2] != 0x04) {
2109 goto chg_wide;
2110 }
2111 if (mbuf[3] == 0x09) {
2112 m = 1;
2113 m = m << i;
2114 dev->wide_id[c] |= m;
2115 dev->id[c][i].devsp = 0xce;
2116#ifdef ED_DBGP
2117 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2118#endif
2119 continue;
2120 }
2121chg_wide:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002122 atp_writeb_io(dev, c, 0x1b, 0x01);
2123 atp_writeb_io(dev, c, 3, satn[0]);
2124 atp_writeb_io(dev, c, 4, satn[1]);
2125 atp_writeb_io(dev, c, 5, satn[2]);
2126 atp_writeb_io(dev, c, 6, satn[3]);
2127 atp_writeb_io(dev, c, 7, satn[4]);
2128 atp_writeb_io(dev, c, 8, satn[5]);
2129 atp_writeb_io(dev, c, 0x0f, 0);
2130 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2131 atp_writeb_io(dev, c, 0x12, 0);
2132 atp_writeb_io(dev, c, 0x13, satn[6]);
2133 atp_writeb_io(dev, c, 0x14, satn[7]);
2134 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002136 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002138
2139 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002141
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002142 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145try_wide:
2146 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002147 atp_writeb_io(dev, c, 0x14, 0x05);
2148 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002150 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2151 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2152 atp_writeb_io(dev, c, 0x19, wide[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 cpu_relax();
2154 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002155
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002156 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002158
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002159 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (j == 0x0f) {
2161 goto widep_in;
2162 }
2163 if (j == 0x0a) {
2164 goto widep_cmd;
2165 }
2166 if (j == 0x0e) {
2167 goto try_wide;
2168 }
2169 continue;
2170widep_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002171 atp_writeb_io(dev, c, 0x18, 0x20);
2172 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2173 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2174 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 cpu_relax();
2176 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002177 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (j == 0x0f) {
2179 goto widep_in;
2180 }
2181 if (j == 0x0a) {
2182 goto widep_cmd;
2183 }
2184 if (j == 0x0e) {
2185 goto widep_out;
2186 }
2187 continue;
2188widep_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002189 atp_writeb_io(dev, c, 0x14, 0xff);
2190 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 k = 0;
2192widep_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002193 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002195 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto widep_in1;
2197 }
2198 if ((j & 0x80) == 0x00) {
2199 goto widep_in1;
2200 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002201 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (j == 0x0f) {
2203 goto widep_in;
2204 }
2205 if (j == 0x0a) {
2206 goto widep_cmd;
2207 }
2208 if (j == 0x0e) {
2209 goto widep_out;
2210 }
2211 continue;
2212widep_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002213 atp_writeb_io(dev, c, 0x10, 0x30);
2214 atp_writeb_io(dev, c, 0x14, 0x00);
2215 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002216
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002217 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002219
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002220 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (j != 0x16) {
2222 if (j == 0x4e) {
2223 goto widep_out;
2224 }
2225 continue;
2226 }
2227 if (mbuf[0] != 0x01) {
2228 goto not_wide;
2229 }
2230 if (mbuf[1] != 0x02) {
2231 goto not_wide;
2232 }
2233 if (mbuf[2] != 0x03) {
2234 goto not_wide;
2235 }
2236 if (mbuf[3] != 0x01) {
2237 goto not_wide;
2238 }
2239 m = 1;
2240 m = m << i;
2241 dev->wide_id[c] |= m;
2242not_wide:
Ondrej Zary80b52a72015-11-17 19:23:58 +01002243 if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 m = 1;
2245 m = m << i;
2246 if ((dev->async[c] & m) != 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002247 goto set_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249 }
2250 continue;
2251set_sync:
Ondrej Zary460da9182015-11-17 19:24:03 +01002252 if ((dev->dev_id != ATP885_DEVID && dev->dev_id != ATP880_DEVID1 && dev->dev_id != ATP880_DEVID2) || (dev->sp[c][i] == 0x02)) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002253 synu[4] = 0x0c;
2254 synuw[4] = 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 } else {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002256 if (dev->sp[c][i] >= 0x03) {
2257 synu[4] = 0x0a;
2258 synuw[4] = 0x0a;
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 j = 0;
2262 if ((m & dev->wide_id[c]) != 0) {
2263 j |= 0x01;
2264 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002265 atp_writeb_io(dev, c, 0x1b, j);
2266 atp_writeb_io(dev, c, 3, satn[0]);
2267 atp_writeb_io(dev, c, 4, satn[1]);
2268 atp_writeb_io(dev, c, 5, satn[2]);
2269 atp_writeb_io(dev, c, 6, satn[3]);
2270 atp_writeb_io(dev, c, 7, satn[4]);
2271 atp_writeb_io(dev, c, 8, satn[5]);
2272 atp_writeb_io(dev, c, 0x0f, 0);
2273 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2274 atp_writeb_io(dev, c, 0x12, 0);
2275 atp_writeb_io(dev, c, 0x13, satn[6]);
2276 atp_writeb_io(dev, c, 0x14, satn[7]);
2277 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002279 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002281
2282 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002284
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002285 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288try_sync:
2289 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002290 atp_writeb_io(dev, c, 0x14, 0x06);
2291 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002293 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2294 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if ((m & dev->wide_id[c]) != 0) {
Ondrej Zary460da9182015-11-17 19:24:03 +01002296 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2297 if ((m & dev->ultra_map[c]) != 0) {
2298 atp_writeb_io(dev, c, 0x19, synuw[j++]);
2299 } else {
2300 atp_writeb_io(dev, c, 0x19, synw[j++]);
2301 }
2302 } else
2303 atp_writeb_io(dev, c, 0x19, synw_870[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 } else {
2305 if ((m & dev->ultra_map[c]) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002306 atp_writeb_io(dev, c, 0x19, synu[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 } else {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002308 atp_writeb_io(dev, c, 0x19, synn[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002313
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002314 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002316
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002317 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (j == 0x0f) {
2319 goto phase_ins;
2320 }
2321 if (j == 0x0a) {
2322 goto phase_cmds;
2323 }
2324 if (j == 0x0e) {
2325 goto try_sync;
2326 }
2327 continue;
2328phase_outs:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002329 atp_writeb_io(dev, c, 0x18, 0x20);
2330 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
2331 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
2332 atp_writeb_io(dev, c, 0x19, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 cpu_relax();
2334 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002335 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (j == 0x85) {
2337 goto tar_dcons;
2338 }
2339 j &= 0x0f;
2340 if (j == 0x0f) {
2341 goto phase_ins;
2342 }
2343 if (j == 0x0a) {
2344 goto phase_cmds;
2345 }
2346 if (j == 0x0e) {
2347 goto phase_outs;
2348 }
2349 continue;
2350phase_ins:
Ondrej Zary460da9182015-11-17 19:24:03 +01002351 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2352 atp_writeb_io(dev, c, 0x14, 0x06);
2353 else
2354 atp_writeb_io(dev, c, 0x14, 0xff);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002355 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 k = 0;
2357phase_ins1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002358 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if ((j & 0x01) != 0x00) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002360 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto phase_ins1;
2362 }
2363 if ((j & 0x80) == 0x00) {
2364 goto phase_ins1;
2365 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002366
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002367 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002368
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002369 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (j == 0x85) {
2371 goto tar_dcons;
2372 }
2373 j &= 0x0f;
2374 if (j == 0x0f) {
2375 goto phase_ins;
2376 }
2377 if (j == 0x0a) {
2378 goto phase_cmds;
2379 }
2380 if (j == 0x0e) {
2381 goto phase_outs;
2382 }
2383 continue;
2384phase_cmds:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002385 atp_writeb_io(dev, c, 0x10, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386tar_dcons:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002387 atp_writeb_io(dev, c, 0x14, 0x00);
2388 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002389
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002390 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002392
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002393 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (j != 0x16) {
2395 continue;
2396 }
2397 if (mbuf[0] != 0x01) {
2398 continue;
2399 }
2400 if (mbuf[1] != 0x03) {
2401 continue;
2402 }
2403 if (mbuf[4] == 0x00) {
2404 continue;
2405 }
2406 if (mbuf[3] > 0x64) {
2407 continue;
2408 }
Ondrej Zary460da9182015-11-17 19:24:03 +01002409 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2410 if (mbuf[4] > 0x0e) {
2411 mbuf[4] = 0x0e;
2412 }
2413 } else {
2414 if (mbuf[4] > 0x0c) {
2415 mbuf[4] = 0x0c;
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 dev->id[c][i].devsp = mbuf[4];
Ondrej Zary460da9182015-11-17 19:24:03 +01002419 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2420 if (mbuf[3] < 0x0c) {
2421 j = 0xb0;
2422 goto set_syn_ok;
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2425 j = 0xa0;
2426 goto set_syn_ok;
2427 }
2428 if (mbuf[3] < 0x1a) {
2429 j = 0x20;
2430 goto set_syn_ok;
2431 }
2432 if (mbuf[3] < 0x33) {
2433 j = 0x40;
2434 goto set_syn_ok;
2435 }
2436 if (mbuf[3] < 0x4c) {
2437 j = 0x50;
2438 goto set_syn_ok;
2439 }
2440 j = 0x60;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002441set_syn_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002443#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2445#endif
2446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449module_init(atp870u_init);
2450module_exit(atp870u_exit);
2451