2 * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
4 * (C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
6 * Per Atp867 data sheet rev 1.2, Acard.
7 * Based in part on early ide code from
8 * 2003-2004 by Eric Uhrhane, Google, Inc.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * 1. RAID features [comparison, XOR, striping, mirroring, etc.]
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/blkdev.h>
33 #include <linux/delay.h>
34 #include <linux/device.h>
35 #include <linux/gfp.h>
36 #include <scsi/scsi_host.h>
37 #include <linux/libata.h>
39 #define DRV_NAME "pata_atp867x"
40 #define DRV_VERSION "0.7.5"
44 * Note that all runtime hot priv ports are cached in ap private_data
48 ATP867X_IO_CHANNEL_OFFSET
= 0x10,
51 * IO Register Bitfields
54 ATP867X_IO_PIOSPD_ACTIVE_SHIFT
= 4,
55 ATP867X_IO_PIOSPD_RECOVER_SHIFT
= 0,
57 ATP867X_IO_DMAMODE_MSTR_SHIFT
= 0,
58 ATP867X_IO_DMAMODE_MSTR_MASK
= 0x07,
59 ATP867X_IO_DMAMODE_SLAVE_SHIFT
= 4,
60 ATP867X_IO_DMAMODE_SLAVE_MASK
= 0x70,
62 ATP867X_IO_DMAMODE_UDMA_6
= 0x07,
63 ATP867X_IO_DMAMODE_UDMA_5
= 0x06,
64 ATP867X_IO_DMAMODE_UDMA_4
= 0x05,
65 ATP867X_IO_DMAMODE_UDMA_3
= 0x04,
66 ATP867X_IO_DMAMODE_UDMA_2
= 0x03,
67 ATP867X_IO_DMAMODE_UDMA_1
= 0x02,
68 ATP867X_IO_DMAMODE_UDMA_0
= 0x01,
69 ATP867X_IO_DMAMODE_DISABLE
= 0x00,
71 ATP867X_IO_SYS_INFO_66MHZ
= 0x04,
72 ATP867X_IO_SYS_INFO_SLOW_UDMA5
= 0x02,
73 ATP867X_IO_SYS_MASK_RESERVED
= (~0xf1),
75 ATP867X_IO_PORTSPD_VAL
= 0x1143,
76 ATP867X_PREREAD_VAL
= 0x0200,
78 ATP867X_NUM_PORTS
= 4,
79 ATP867X_BAR_IOBASE
= 0,
80 ATP867X_BAR_ROMBASE
= 6,
83 #define ATP867X_IOBASE(ap) ((ap)->host->iomap[0])
84 #define ATP867X_SYS_INFO(ap) (0x3F + ATP867X_IOBASE(ap))
86 #define ATP867X_IO_PORTBASE(ap, port) (0x00 + ATP867X_IOBASE(ap) + \
87 (port) * ATP867X_IO_CHANNEL_OFFSET)
88 #define ATP867X_IO_DMABASE(ap, port) (0x40 + \
89 ATP867X_IO_PORTBASE((ap), (port)))
91 #define ATP867X_IO_STATUS(ap, port) (0x07 + \
92 ATP867X_IO_PORTBASE((ap), (port)))
93 #define ATP867X_IO_ALTSTATUS(ap, port) (0x0E + \
94 ATP867X_IO_PORTBASE((ap), (port)))
99 #define ATP867X_IO_MSTRPIOSPD(ap, port) (0x08 + \
100 ATP867X_IO_DMABASE((ap), (port)))
101 #define ATP867X_IO_SLAVPIOSPD(ap, port) (0x09 + \
102 ATP867X_IO_DMABASE((ap), (port)))
103 #define ATP867X_IO_8BPIOSPD(ap, port) (0x0A + \
104 ATP867X_IO_DMABASE((ap), (port)))
105 #define ATP867X_IO_DMAMODE(ap, port) (0x0B + \
106 ATP867X_IO_DMABASE((ap), (port)))
108 #define ATP867X_IO_PORTSPD(ap, port) (0x4A + \
109 ATP867X_IO_PORTBASE((ap), (port)))
110 #define ATP867X_IO_PREREAD(ap, port) (0x4C + \
111 ATP867X_IO_PORTBASE((ap), (port)))
113 struct atp867x_priv
{
114 void __iomem
*dma_mode
;
115 void __iomem
*mstr_piospd
;
116 void __iomem
*slave_piospd
;
117 void __iomem
*eightb_piospd
;
121 static void atp867x_set_dmamode(struct ata_port
*ap
, struct ata_device
*adev
)
123 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
124 struct atp867x_priv
*dp
= ap
->private_data
;
125 u8 speed
= adev
->dma_mode
;
127 u8 mode
= speed
- XFER_UDMA_0
+ 1;
130 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
132 * rev-A: UDMA_1~4 (5, 6 no change)
133 * rev-B: all UDMA modes
134 * UDMA_0 stays not to disable UDMA
136 if (dp
->pci66mhz
&& mode
> ATP867X_IO_DMAMODE_UDMA_0
&&
137 (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
||
138 mode
< ATP867X_IO_DMAMODE_UDMA_5
))
141 b
= ioread8(dp
->dma_mode
);
142 if (adev
->devno
& 1) {
143 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
) |
144 (mode
<< ATP867X_IO_DMAMODE_SLAVE_SHIFT
);
146 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
) |
147 (mode
<< ATP867X_IO_DMAMODE_MSTR_SHIFT
);
149 iowrite8(b
, dp
->dma_mode
);
152 static int atp867x_get_active_clocks_shifted(struct ata_port
*ap
,
155 struct atp867x_priv
*dp
= ap
->private_data
;
156 unsigned char clocks
= clk
;
159 * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
172 printk(KERN_WARNING
"ATP867X: active %dclk is invalid. "
173 "Using 12clk.\n", clk
);
176 clocks
= 7; /* 12 clk */
179 case 8: /* default 8 clk */
181 goto active_clock_shift_done
;
184 active_clock_shift_done
:
185 return clocks
<< ATP867X_IO_PIOSPD_ACTIVE_SHIFT
;
188 static int atp867x_get_recover_clocks_shifted(unsigned int clk
)
190 unsigned char clocks
= clk
;
200 --clocks
; /* by the spec */
205 printk(KERN_WARNING
"ATP867X: recover %dclk is invalid. "
206 "Using default 12clk.\n", clk
);
208 case 12: /* default 12 clk */
213 return clocks
<< ATP867X_IO_PIOSPD_RECOVER_SHIFT
;
216 static void atp867x_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
218 struct ata_device
*peer
= ata_dev_pair(adev
);
219 struct atp867x_priv
*dp
= ap
->private_data
;
220 u8 speed
= adev
->pio_mode
;
221 struct ata_timing t
, p
;
225 T
= 1000000000 / 33333;
228 ata_timing_compute(adev
, speed
, &t
, T
, UT
);
229 if (peer
&& peer
->pio_mode
) {
230 ata_timing_compute(peer
, peer
->pio_mode
, &p
, T
, UT
);
231 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_8BIT
);
234 b
= ioread8(dp
->dma_mode
);
236 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
);
238 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
);
239 iowrite8(b
, dp
->dma_mode
);
241 b
= atp867x_get_active_clocks_shifted(ap
, t
.active
) |
242 atp867x_get_recover_clocks_shifted(t
.recover
);
245 iowrite8(b
, dp
->slave_piospd
);
247 iowrite8(b
, dp
->mstr_piospd
);
249 b
= atp867x_get_active_clocks_shifted(ap
, t
.act8b
) |
250 atp867x_get_recover_clocks_shifted(t
.rec8b
);
252 iowrite8(b
, dp
->eightb_piospd
);
255 static int atp867x_cable_override(struct pci_dev
*pdev
)
257 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_ARTOP
&&
258 (pdev
->subsystem_device
== PCI_DEVICE_ID_ARTOP_ATP867A
||
259 pdev
->subsystem_device
== PCI_DEVICE_ID_ARTOP_ATP867B
)) {
265 static int atp867x_cable_detect(struct ata_port
*ap
)
267 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
269 if (atp867x_cable_override(pdev
))
270 return ATA_CBL_PATA40_SHORT
;
272 return ATA_CBL_PATA_UNK
;
275 static struct scsi_host_template atp867x_sht
= {
276 ATA_BMDMA_SHT(DRV_NAME
),
279 static struct ata_port_operations atp867x_ops
= {
280 .inherits
= &ata_bmdma_port_ops
,
281 .cable_detect
= atp867x_cable_detect
,
282 .set_piomode
= atp867x_set_piomode
,
283 .set_dmamode
= atp867x_set_dmamode
,
288 static void atp867x_check_res(struct pci_dev
*pdev
)
291 unsigned long start
, len
;
293 /* Check the PCI resources for this channel are enabled */
294 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
295 start
= pci_resource_start(pdev
, i
);
296 len
= pci_resource_len(pdev
, i
);
297 printk(KERN_DEBUG
"ATP867X: resource start:len=%lx:%lx\n",
302 static void atp867x_check_ports(struct ata_port
*ap
, int port
)
304 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
305 struct atp867x_priv
*dp
= ap
->private_data
;
307 printk(KERN_DEBUG
"ATP867X: port[%d] addresses\n"
308 " cmd_addr =0x%llx, 0x%llx\n"
309 " ctl_addr =0x%llx, 0x%llx\n"
310 " bmdma_addr =0x%llx, 0x%llx\n"
311 " data_addr =0x%llx\n"
312 " error_addr =0x%llx\n"
313 " feature_addr =0x%llx\n"
314 " nsect_addr =0x%llx\n"
315 " lbal_addr =0x%llx\n"
316 " lbam_addr =0x%llx\n"
317 " lbah_addr =0x%llx\n"
318 " device_addr =0x%llx\n"
319 " status_addr =0x%llx\n"
320 " command_addr =0x%llx\n"
321 " dp->dma_mode =0x%llx\n"
322 " dp->mstr_piospd =0x%llx\n"
323 " dp->slave_piospd =0x%llx\n"
324 " dp->eightb_piospd =0x%llx\n"
325 " dp->pci66mhz =0x%lx\n",
327 (unsigned long long)ioaddr
->cmd_addr
,
328 (unsigned long long)ATP867X_IO_PORTBASE(ap
, port
),
329 (unsigned long long)ioaddr
->ctl_addr
,
330 (unsigned long long)ATP867X_IO_ALTSTATUS(ap
, port
),
331 (unsigned long long)ioaddr
->bmdma_addr
,
332 (unsigned long long)ATP867X_IO_DMABASE(ap
, port
),
333 (unsigned long long)ioaddr
->data_addr
,
334 (unsigned long long)ioaddr
->error_addr
,
335 (unsigned long long)ioaddr
->feature_addr
,
336 (unsigned long long)ioaddr
->nsect_addr
,
337 (unsigned long long)ioaddr
->lbal_addr
,
338 (unsigned long long)ioaddr
->lbam_addr
,
339 (unsigned long long)ioaddr
->lbah_addr
,
340 (unsigned long long)ioaddr
->device_addr
,
341 (unsigned long long)ioaddr
->status_addr
,
342 (unsigned long long)ioaddr
->command_addr
,
343 (unsigned long long)dp
->dma_mode
,
344 (unsigned long long)dp
->mstr_piospd
,
345 (unsigned long long)dp
->slave_piospd
,
346 (unsigned long long)dp
->eightb_piospd
,
347 (unsigned long)dp
->pci66mhz
);
351 static int atp867x_set_priv(struct ata_port
*ap
)
353 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
354 struct atp867x_priv
*dp
;
355 int port
= ap
->port_no
;
357 dp
= ap
->private_data
=
358 devm_kzalloc(&pdev
->dev
, sizeof(*dp
), GFP_KERNEL
);
362 dp
->dma_mode
= ATP867X_IO_DMAMODE(ap
, port
);
363 dp
->mstr_piospd
= ATP867X_IO_MSTRPIOSPD(ap
, port
);
364 dp
->slave_piospd
= ATP867X_IO_SLAVPIOSPD(ap
, port
);
365 dp
->eightb_piospd
= ATP867X_IO_8BPIOSPD(ap
, port
);
368 ioread8(ATP867X_SYS_INFO(ap
)) & ATP867X_IO_SYS_INFO_66MHZ
;
373 static void atp867x_fixup(struct ata_host
*host
)
375 struct pci_dev
*pdev
= to_pci_dev(host
->dev
);
376 struct ata_port
*ap
= host
->ports
[0];
381 * Broken BIOS might not set latency high enough
383 pci_read_config_byte(pdev
, PCI_LATENCY_TIMER
, &v
);
386 pci_write_config_byte(pdev
, PCI_LATENCY_TIMER
, v
);
387 printk(KERN_DEBUG
"ATP867X: set latency timer of device %s"
388 " to %d\n", pci_name(pdev
), v
);
392 * init 8bit io ports speed(0aaarrrr) to 43h and
393 * init udma modes of master/slave to 0/0(11h)
395 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
396 iowrite16(ATP867X_IO_PORTSPD_VAL
, ATP867X_IO_PORTSPD(ap
, i
));
399 * init PreREAD counts
401 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
402 iowrite16(ATP867X_PREREAD_VAL
, ATP867X_IO_PREREAD(ap
, i
));
404 v
= ioread8(ATP867X_IOBASE(ap
) + 0x28);
405 v
&= 0xcf; /* Enable INTA#: bit4=0 means enable */
406 v
|= 0xc0; /* Enable PCI burst, MRM & not immediate interrupts */
407 iowrite8(v
, ATP867X_IOBASE(ap
) + 0x28);
410 * Turn off the over clocked udma5 mode, only for Rev-B
412 v
= ioread8(ATP867X_SYS_INFO(ap
));
413 v
&= ATP867X_IO_SYS_MASK_RESERVED
;
414 if (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
)
415 v
|= ATP867X_IO_SYS_INFO_SLOW_UDMA5
;
416 iowrite8(v
, ATP867X_SYS_INFO(ap
));
419 static int atp867x_ata_pci_sff_init_host(struct ata_host
*host
)
421 struct device
*gdev
= host
->dev
;
422 struct pci_dev
*pdev
= to_pci_dev(gdev
);
423 unsigned int mask
= 0;
429 rc
= pcim_iomap_regions(pdev
, 1 << ATP867X_BAR_IOBASE
, DRV_NAME
);
431 pcim_pin_device(pdev
);
434 host
->iomap
= pcim_iomap_table(pdev
);
437 atp867x_check_res(pdev
);
439 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
440 printk(KERN_DEBUG
"ATP867X: iomap[%d]=0x%llx\n", i
,
441 (unsigned long long)(host
->iomap
[i
]));
445 * request, iomap BARs and init port addresses accordingly
447 for (i
= 0; i
< host
->n_ports
; i
++) {
448 struct ata_port
*ap
= host
->ports
[i
];
449 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
451 ioaddr
->cmd_addr
= ATP867X_IO_PORTBASE(ap
, i
);
452 ioaddr
->ctl_addr
= ioaddr
->altstatus_addr
453 = ATP867X_IO_ALTSTATUS(ap
, i
);
454 ioaddr
->bmdma_addr
= ATP867X_IO_DMABASE(ap
, i
);
456 ata_sff_std_ports(ioaddr
);
457 rc
= atp867x_set_priv(ap
);
462 atp867x_check_ports(ap
, i
);
464 ata_port_desc(ap
, "cmd 0x%lx ctl 0x%lx",
465 (unsigned long)ioaddr
->cmd_addr
,
466 (unsigned long)ioaddr
->ctl_addr
);
467 ata_port_desc(ap
, "bmdma 0x%lx",
468 (unsigned long)ioaddr
->bmdma_addr
);
474 dev_err(gdev
, "no available native port\n");
480 rc
= dma_set_mask(&pdev
->dev
, ATA_DMA_MASK
);
484 rc
= dma_set_coherent_mask(&pdev
->dev
, ATA_DMA_MASK
);
488 static int atp867x_init_one(struct pci_dev
*pdev
,
489 const struct pci_device_id
*id
)
491 static const struct ata_port_info info_867x
= {
492 .flags
= ATA_FLAG_SLAVE_POSS
,
493 .pio_mask
= ATA_PIO4
,
494 .udma_mask
= ATA_UDMA6
,
495 .port_ops
= &atp867x_ops
,
498 struct ata_host
*host
;
499 const struct ata_port_info
*ppi
[] = { &info_867x
, NULL
};
502 ata_print_version_once(&pdev
->dev
, DRV_VERSION
);
504 rc
= pcim_enable_device(pdev
);
508 printk(KERN_INFO
"ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
511 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, ATP867X_NUM_PORTS
);
513 dev_err(&pdev
->dev
, "failed to allocate ATA host\n");
518 rc
= atp867x_ata_pci_sff_init_host(host
);
520 dev_err(&pdev
->dev
, "failed to init host\n");
524 pci_set_master(pdev
);
526 rc
= ata_host_activate(host
, pdev
->irq
, ata_bmdma_interrupt
,
527 IRQF_SHARED
, &atp867x_sht
);
529 dev_err(&pdev
->dev
, "failed to activate host\n");
535 #ifdef CONFIG_PM_SLEEP
536 static int atp867x_reinit_one(struct pci_dev
*pdev
)
538 struct ata_host
*host
= pci_get_drvdata(pdev
);
541 rc
= ata_pci_device_do_resume(pdev
);
547 ata_host_resume(host
);
552 static struct pci_device_id atp867x_pci_tbl
[] = {
553 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867A
), 0 },
554 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867B
), 0 },
558 static struct pci_driver atp867x_driver
= {
560 .id_table
= atp867x_pci_tbl
,
561 .probe
= atp867x_init_one
,
562 .remove
= ata_pci_remove_one
,
563 #ifdef CONFIG_PM_SLEEP
564 .suspend
= ata_pci_device_suspend
,
565 .resume
= atp867x_reinit_one
,
569 module_pci_driver(atp867x_driver
);
571 MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
572 MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
573 MODULE_LICENSE("GPL");
574 MODULE_DEVICE_TABLE(pci
, atp867x_pci_tbl
);
575 MODULE_VERSION(DRV_VERSION
);