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/init.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/device.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 inline u8
atp867x_speed_to_mode(u8 speed
)
123 return speed
- XFER_UDMA_0
+ 1;
126 static void atp867x_set_dmamode(struct ata_port
*ap
, struct ata_device
*adev
)
128 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
129 struct atp867x_priv
*dp
= ap
->private_data
;
130 u8 speed
= adev
->dma_mode
;
134 mode
= atp867x_speed_to_mode(speed
);
137 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
139 * rev-A: UDMA_1~4 (5, 6 no change)
140 * rev-B: all UDMA modes
141 * UDMA_0 stays not to disable UDMA
143 if (dp
->pci66mhz
&& mode
> ATP867X_IO_DMAMODE_UDMA_0
&&
144 (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
||
145 mode
< ATP867X_IO_DMAMODE_UDMA_5
))
148 b
= ioread8(dp
->dma_mode
);
149 if (adev
->devno
& 1) {
150 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
) |
151 (mode
<< ATP867X_IO_DMAMODE_SLAVE_SHIFT
);
153 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
) |
154 (mode
<< ATP867X_IO_DMAMODE_MSTR_SHIFT
);
156 iowrite8(b
, dp
->dma_mode
);
159 static int atp867x_get_active_clocks_shifted(unsigned int clk
)
161 unsigned char clocks
= clk
;
173 printk(KERN_WARNING
"ATP867X: active %dclk is invalid. "
174 "Using default 8clk.\n", clk
);
175 clocks
= 0; /* 8 clk */
178 return clocks
<< ATP867X_IO_PIOSPD_ACTIVE_SHIFT
;
181 static int atp867x_get_recover_clocks_shifted(unsigned int clk
)
183 unsigned char clocks
= clk
;
200 printk(KERN_WARNING
"ATP867X: recover %dclk is invalid. "
201 "Using default 15clk.\n", clk
);
202 clocks
= 0; /* 12 clk */
205 return clocks
<< ATP867X_IO_PIOSPD_RECOVER_SHIFT
;
208 static void atp867x_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
210 struct ata_device
*peer
= ata_dev_pair(adev
);
211 struct atp867x_priv
*dp
= ap
->private_data
;
212 u8 speed
= adev
->pio_mode
;
213 struct ata_timing t
, p
;
217 T
= 1000000000 / 33333;
220 ata_timing_compute(adev
, speed
, &t
, T
, UT
);
221 if (peer
&& peer
->pio_mode
) {
222 ata_timing_compute(peer
, peer
->pio_mode
, &p
, T
, UT
);
223 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_8BIT
);
226 b
= ioread8(dp
->dma_mode
);
228 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
);
230 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
);
231 iowrite8(b
, dp
->dma_mode
);
233 b
= atp867x_get_active_clocks_shifted(t
.active
) |
234 atp867x_get_recover_clocks_shifted(t
.recover
);
239 iowrite8(b
, dp
->slave_piospd
);
241 iowrite8(b
, dp
->mstr_piospd
);
244 * use the same value for comand timing as for PIO timimg
246 iowrite8(b
, dp
->eightb_piospd
);
249 static int atp867x_cable_detect(struct ata_port
*ap
)
251 return ATA_CBL_PATA40_SHORT
;
254 static struct scsi_host_template atp867x_sht
= {
255 ATA_BMDMA_SHT(DRV_NAME
),
258 static struct ata_port_operations atp867x_ops
= {
259 .inherits
= &ata_bmdma_port_ops
,
260 .cable_detect
= atp867x_cable_detect
,
261 .set_piomode
= atp867x_set_piomode
,
262 .set_dmamode
= atp867x_set_dmamode
,
267 static void atp867x_check_res(struct pci_dev
*pdev
)
270 unsigned long start
, len
;
272 /* Check the PCI resources for this channel are enabled */
273 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
274 start
= pci_resource_start(pdev
, i
);
275 len
= pci_resource_len(pdev
, i
);
276 printk(KERN_DEBUG
"ATP867X: resource start:len=%lx:%lx\n",
281 static void atp867x_check_ports(struct ata_port
*ap
, int port
)
283 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
284 struct atp867x_priv
*dp
= ap
->private_data
;
286 printk(KERN_DEBUG
"ATP867X: port[%d] addresses\n"
287 " cmd_addr =0x%llx, 0x%llx\n"
288 " ctl_addr =0x%llx, 0x%llx\n"
289 " bmdma_addr =0x%llx, 0x%llx\n"
290 " data_addr =0x%llx\n"
291 " error_addr =0x%llx\n"
292 " feature_addr =0x%llx\n"
293 " nsect_addr =0x%llx\n"
294 " lbal_addr =0x%llx\n"
295 " lbam_addr =0x%llx\n"
296 " lbah_addr =0x%llx\n"
297 " device_addr =0x%llx\n"
298 " status_addr =0x%llx\n"
299 " command_addr =0x%llx\n"
300 " dp->dma_mode =0x%llx\n"
301 " dp->mstr_piospd =0x%llx\n"
302 " dp->slave_piospd =0x%llx\n"
303 " dp->eightb_piospd =0x%llx\n"
304 " dp->pci66mhz =0x%lx\n",
306 (unsigned long long)ioaddr
->cmd_addr
,
307 (unsigned long long)ATP867X_IO_PORTBASE(ap
, port
),
308 (unsigned long long)ioaddr
->ctl_addr
,
309 (unsigned long long)ATP867X_IO_ALTSTATUS(ap
, port
),
310 (unsigned long long)ioaddr
->bmdma_addr
,
311 (unsigned long long)ATP867X_IO_DMABASE(ap
, port
),
312 (unsigned long long)ioaddr
->data_addr
,
313 (unsigned long long)ioaddr
->error_addr
,
314 (unsigned long long)ioaddr
->feature_addr
,
315 (unsigned long long)ioaddr
->nsect_addr
,
316 (unsigned long long)ioaddr
->lbal_addr
,
317 (unsigned long long)ioaddr
->lbam_addr
,
318 (unsigned long long)ioaddr
->lbah_addr
,
319 (unsigned long long)ioaddr
->device_addr
,
320 (unsigned long long)ioaddr
->status_addr
,
321 (unsigned long long)ioaddr
->command_addr
,
322 (unsigned long long)dp
->dma_mode
,
323 (unsigned long long)dp
->mstr_piospd
,
324 (unsigned long long)dp
->slave_piospd
,
325 (unsigned long long)dp
->eightb_piospd
,
326 (unsigned long)dp
->pci66mhz
);
330 static int atp867x_set_priv(struct ata_port
*ap
)
332 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
333 struct atp867x_priv
*dp
;
334 int port
= ap
->port_no
;
336 dp
= ap
->private_data
=
337 devm_kzalloc(&pdev
->dev
, sizeof(*dp
), GFP_KERNEL
);
341 dp
->dma_mode
= ATP867X_IO_DMAMODE(ap
, port
);
342 dp
->mstr_piospd
= ATP867X_IO_MSTRPIOSPD(ap
, port
);
343 dp
->slave_piospd
= ATP867X_IO_SLAVPIOSPD(ap
, port
);
344 dp
->eightb_piospd
= ATP867X_IO_8BPIOSPD(ap
, port
);
347 ioread8(ATP867X_SYS_INFO(ap
)) & ATP867X_IO_SYS_INFO_66MHZ
;
352 static void atp867x_fixup(struct ata_host
*host
)
354 struct pci_dev
*pdev
= to_pci_dev(host
->dev
);
355 struct ata_port
*ap
= host
->ports
[0];
360 * Broken BIOS might not set latency high enough
362 pci_read_config_byte(pdev
, PCI_LATENCY_TIMER
, &v
);
365 pci_write_config_byte(pdev
, PCI_LATENCY_TIMER
, v
);
366 printk(KERN_DEBUG
"ATP867X: set latency timer of device %s"
367 " to %d\n", pci_name(pdev
), v
);
371 * init 8bit io ports speed(0aaarrrr) to 43h and
372 * init udma modes of master/slave to 0/0(11h)
374 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
375 iowrite16(ATP867X_IO_PORTSPD_VAL
, ATP867X_IO_PORTSPD(ap
, i
));
378 * init PreREAD counts
380 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
381 iowrite16(ATP867X_PREREAD_VAL
, ATP867X_IO_PREREAD(ap
, i
));
383 v
= ioread8(ATP867X_IOBASE(ap
) + 0x28);
384 v
&= 0xcf; /* Enable INTA#: bit4=0 means enable */
385 v
|= 0xc0; /* Enable PCI burst, MRM & not immediate interrupts */
386 iowrite8(v
, ATP867X_IOBASE(ap
) + 0x28);
389 * Turn off the over clocked udma5 mode, only for Rev-B
391 v
= ioread8(ATP867X_SYS_INFO(ap
));
392 v
&= ATP867X_IO_SYS_MASK_RESERVED
;
393 if (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
)
394 v
|= ATP867X_IO_SYS_INFO_SLOW_UDMA5
;
395 iowrite8(v
, ATP867X_SYS_INFO(ap
));
398 static int atp867x_ata_pci_sff_init_host(struct ata_host
*host
)
400 struct device
*gdev
= host
->dev
;
401 struct pci_dev
*pdev
= to_pci_dev(gdev
);
402 unsigned int mask
= 0;
408 rc
= pcim_iomap_regions(pdev
, 1 << ATP867X_BAR_IOBASE
, DRV_NAME
);
410 pcim_pin_device(pdev
);
413 host
->iomap
= pcim_iomap_table(pdev
);
416 atp867x_check_res(pdev
);
418 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
419 printk(KERN_DEBUG
"ATP867X: iomap[%d]=0x%llx\n", i
,
420 (unsigned long long)(host
->iomap
[i
]));
424 * request, iomap BARs and init port addresses accordingly
426 for (i
= 0; i
< host
->n_ports
; i
++) {
427 struct ata_port
*ap
= host
->ports
[i
];
428 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
430 ioaddr
->cmd_addr
= ATP867X_IO_PORTBASE(ap
, i
);
431 ioaddr
->ctl_addr
= ioaddr
->altstatus_addr
432 = ATP867X_IO_ALTSTATUS(ap
, i
);
433 ioaddr
->bmdma_addr
= ATP867X_IO_DMABASE(ap
, i
);
435 ata_sff_std_ports(ioaddr
);
436 rc
= atp867x_set_priv(ap
);
441 atp867x_check_ports(ap
, i
);
443 ata_port_desc(ap
, "cmd 0x%lx ctl 0x%lx",
444 (unsigned long)ioaddr
->cmd_addr
,
445 (unsigned long)ioaddr
->ctl_addr
);
446 ata_port_desc(ap
, "bmdma 0x%lx",
447 (unsigned long)ioaddr
->bmdma_addr
);
453 dev_printk(KERN_ERR
, gdev
, "no available native port\n");
459 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
463 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
467 static int atp867x_init_one(struct pci_dev
*pdev
,
468 const struct pci_device_id
*id
)
470 static int printed_version
;
471 static const struct ata_port_info info_867x
= {
472 .flags
= ATA_FLAG_SLAVE_POSS
,
473 .pio_mask
= ATA_PIO4
,
474 .mwdma_mask
= ATA_MWDMA2
,
475 .udma_mask
= ATA_UDMA6
,
476 .port_ops
= &atp867x_ops
,
479 struct ata_host
*host
;
480 const struct ata_port_info
*ppi
[] = { &info_867x
, NULL
};
483 if (!printed_version
++)
484 dev_printk(KERN_INFO
, &pdev
->dev
, "version " DRV_VERSION
"\n");
486 rc
= pcim_enable_device(pdev
);
490 printk(KERN_INFO
"ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
493 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, ATP867X_NUM_PORTS
);
495 dev_printk(KERN_ERR
, &pdev
->dev
,
496 "failed to allocate ATA host\n");
501 rc
= atp867x_ata_pci_sff_init_host(host
);
503 dev_printk(KERN_ERR
, &pdev
->dev
, "failed to init host\n");
507 pci_set_master(pdev
);
509 rc
= ata_host_activate(host
, pdev
->irq
, ata_sff_interrupt
,
510 IRQF_SHARED
, &atp867x_sht
);
512 dev_printk(KERN_ERR
, &pdev
->dev
, "failed to activate host\n");
518 static struct pci_device_id atp867x_pci_tbl
[] = {
519 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867A
), 0 },
520 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867B
), 0 },
524 static struct pci_driver atp867x_driver
= {
526 .id_table
= atp867x_pci_tbl
,
527 .probe
= atp867x_init_one
,
528 .remove
= ata_pci_remove_one
,
531 static int __init
atp867x_init(void)
533 return pci_register_driver(&atp867x_driver
);
536 static void __exit
atp867x_exit(void)
538 pci_unregister_driver(&atp867x_driver
);
541 MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
542 MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
543 MODULE_LICENSE("GPL");
544 MODULE_DEVICE_TABLE(pci
, atp867x_pci_tbl
);
545 MODULE_VERSION(DRV_VERSION
);
547 module_init(atp867x_init
);
548 module_exit(atp867x_exit
);