1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
5 * (C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
7 * Per Atp867 data sheet rev 1.2, Acard.
8 * Based in part on early ide code from
9 * 2003-2004 by Eric Uhrhane, Google, Inc.
12 * 1. RAID features [comparison, XOR, striping, mirroring, etc.]
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/blkdev.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/gfp.h>
22 #include <scsi/scsi_host.h>
23 #include <linux/libata.h>
25 #define DRV_NAME "pata_atp867x"
26 #define DRV_VERSION "0.7.5"
30 * Note that all runtime hot priv ports are cached in ap private_data
34 ATP867X_IO_CHANNEL_OFFSET
= 0x10,
37 * IO Register Bitfields
40 ATP867X_IO_PIOSPD_ACTIVE_SHIFT
= 4,
41 ATP867X_IO_PIOSPD_RECOVER_SHIFT
= 0,
43 ATP867X_IO_DMAMODE_MSTR_SHIFT
= 0,
44 ATP867X_IO_DMAMODE_MSTR_MASK
= 0x07,
45 ATP867X_IO_DMAMODE_SLAVE_SHIFT
= 4,
46 ATP867X_IO_DMAMODE_SLAVE_MASK
= 0x70,
48 ATP867X_IO_DMAMODE_UDMA_6
= 0x07,
49 ATP867X_IO_DMAMODE_UDMA_5
= 0x06,
50 ATP867X_IO_DMAMODE_UDMA_4
= 0x05,
51 ATP867X_IO_DMAMODE_UDMA_3
= 0x04,
52 ATP867X_IO_DMAMODE_UDMA_2
= 0x03,
53 ATP867X_IO_DMAMODE_UDMA_1
= 0x02,
54 ATP867X_IO_DMAMODE_UDMA_0
= 0x01,
55 ATP867X_IO_DMAMODE_DISABLE
= 0x00,
57 ATP867X_IO_SYS_INFO_66MHZ
= 0x04,
58 ATP867X_IO_SYS_INFO_SLOW_UDMA5
= 0x02,
59 ATP867X_IO_SYS_MASK_RESERVED
= (~0xf1),
61 ATP867X_IO_PORTSPD_VAL
= 0x1143,
62 ATP867X_PREREAD_VAL
= 0x0200,
64 ATP867X_NUM_PORTS
= 4,
65 ATP867X_BAR_IOBASE
= 0,
66 ATP867X_BAR_ROMBASE
= 6,
69 #define ATP867X_IOBASE(ap) ((ap)->host->iomap[0])
70 #define ATP867X_SYS_INFO(ap) (0x3F + ATP867X_IOBASE(ap))
72 #define ATP867X_IO_PORTBASE(ap, port) (0x00 + ATP867X_IOBASE(ap) + \
73 (port) * ATP867X_IO_CHANNEL_OFFSET)
74 #define ATP867X_IO_DMABASE(ap, port) (0x40 + \
75 ATP867X_IO_PORTBASE((ap), (port)))
77 #define ATP867X_IO_STATUS(ap, port) (0x07 + \
78 ATP867X_IO_PORTBASE((ap), (port)))
79 #define ATP867X_IO_ALTSTATUS(ap, port) (0x0E + \
80 ATP867X_IO_PORTBASE((ap), (port)))
85 #define ATP867X_IO_MSTRPIOSPD(ap, port) (0x08 + \
86 ATP867X_IO_DMABASE((ap), (port)))
87 #define ATP867X_IO_SLAVPIOSPD(ap, port) (0x09 + \
88 ATP867X_IO_DMABASE((ap), (port)))
89 #define ATP867X_IO_8BPIOSPD(ap, port) (0x0A + \
90 ATP867X_IO_DMABASE((ap), (port)))
91 #define ATP867X_IO_DMAMODE(ap, port) (0x0B + \
92 ATP867X_IO_DMABASE((ap), (port)))
94 #define ATP867X_IO_PORTSPD(ap, port) (0x4A + \
95 ATP867X_IO_PORTBASE((ap), (port)))
96 #define ATP867X_IO_PREREAD(ap, port) (0x4C + \
97 ATP867X_IO_PORTBASE((ap), (port)))
100 void __iomem
*dma_mode
;
101 void __iomem
*mstr_piospd
;
102 void __iomem
*slave_piospd
;
103 void __iomem
*eightb_piospd
;
107 static void atp867x_set_dmamode(struct ata_port
*ap
, struct ata_device
*adev
)
109 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
110 struct atp867x_priv
*dp
= ap
->private_data
;
111 u8 speed
= adev
->dma_mode
;
113 u8 mode
= speed
- XFER_UDMA_0
+ 1;
116 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
118 * rev-A: UDMA_1~4 (5, 6 no change)
119 * rev-B: all UDMA modes
120 * UDMA_0 stays not to disable UDMA
122 if (dp
->pci66mhz
&& mode
> ATP867X_IO_DMAMODE_UDMA_0
&&
123 (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
||
124 mode
< ATP867X_IO_DMAMODE_UDMA_5
))
127 b
= ioread8(dp
->dma_mode
);
128 if (adev
->devno
& 1) {
129 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
) |
130 (mode
<< ATP867X_IO_DMAMODE_SLAVE_SHIFT
);
132 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
) |
133 (mode
<< ATP867X_IO_DMAMODE_MSTR_SHIFT
);
135 iowrite8(b
, dp
->dma_mode
);
138 static int atp867x_get_active_clocks_shifted(struct ata_port
*ap
,
141 struct atp867x_priv
*dp
= ap
->private_data
;
142 unsigned char clocks
= clk
;
145 * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
158 ata_port_warn(ap
, "ATP867X: active %dclk is invalid. "
159 "Using 12clk.\n", clk
);
162 clocks
= 7; /* 12 clk */
165 case 8: /* default 8 clk */
167 goto active_clock_shift_done
;
170 active_clock_shift_done
:
171 return clocks
<< ATP867X_IO_PIOSPD_ACTIVE_SHIFT
;
174 static int atp867x_get_recover_clocks_shifted(struct ata_port
*ap
,
177 unsigned char clocks
= clk
;
187 --clocks
; /* by the spec */
192 ata_port_warn(ap
, "ATP867X: recover %dclk is invalid. "
193 "Using default 12clk.\n", clk
);
195 case 12: /* default 12 clk */
200 return clocks
<< ATP867X_IO_PIOSPD_RECOVER_SHIFT
;
203 static void atp867x_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
205 struct ata_device
*peer
= ata_dev_pair(adev
);
206 struct atp867x_priv
*dp
= ap
->private_data
;
207 u8 speed
= adev
->pio_mode
;
208 struct ata_timing t
, p
;
212 T
= 1000000000 / 33333;
215 ata_timing_compute(adev
, speed
, &t
, T
, UT
);
216 if (peer
&& peer
->pio_mode
) {
217 ata_timing_compute(peer
, peer
->pio_mode
, &p
, T
, UT
);
218 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_8BIT
);
221 b
= ioread8(dp
->dma_mode
);
223 b
= (b
& ~ATP867X_IO_DMAMODE_SLAVE_MASK
);
225 b
= (b
& ~ATP867X_IO_DMAMODE_MSTR_MASK
);
226 iowrite8(b
, dp
->dma_mode
);
228 b
= atp867x_get_active_clocks_shifted(ap
, t
.active
) |
229 atp867x_get_recover_clocks_shifted(ap
, t
.recover
);
232 iowrite8(b
, dp
->slave_piospd
);
234 iowrite8(b
, dp
->mstr_piospd
);
236 b
= atp867x_get_active_clocks_shifted(ap
, t
.act8b
) |
237 atp867x_get_recover_clocks_shifted(ap
, t
.rec8b
);
239 iowrite8(b
, dp
->eightb_piospd
);
242 static int atp867x_cable_override(struct pci_dev
*pdev
)
244 if (pdev
->subsystem_vendor
== PCI_VENDOR_ID_ARTOP
&&
245 (pdev
->subsystem_device
== PCI_DEVICE_ID_ARTOP_ATP867A
||
246 pdev
->subsystem_device
== PCI_DEVICE_ID_ARTOP_ATP867B
)) {
252 static int atp867x_cable_detect(struct ata_port
*ap
)
254 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
256 if (atp867x_cable_override(pdev
))
257 return ATA_CBL_PATA40_SHORT
;
259 return ATA_CBL_PATA_UNK
;
262 static const struct scsi_host_template atp867x_sht
= {
263 ATA_BMDMA_SHT(DRV_NAME
),
266 static struct ata_port_operations atp867x_ops
= {
267 .inherits
= &ata_bmdma_port_ops
,
268 .cable_detect
= atp867x_cable_detect
,
269 .set_piomode
= atp867x_set_piomode
,
270 .set_dmamode
= atp867x_set_dmamode
,
274 static void atp867x_check_res(struct pci_dev
*pdev
)
277 unsigned long start
, len
;
279 /* Check the PCI resources for this channel are enabled */
280 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
281 start
= pci_resource_start(pdev
, i
);
282 len
= pci_resource_len(pdev
, i
);
283 dev_dbg(&pdev
->dev
, "ATP867X: resource start:len=%lx:%lx\n",
288 static void atp867x_check_ports(struct ata_port
*ap
, int port
)
290 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
291 struct atp867x_priv
*dp
= ap
->private_data
;
293 ata_port_dbg(ap
, "ATP867X: port[%d] addresses\n"
294 " cmd_addr =0x%lx, 0x%lx\n"
295 " ctl_addr =0x%lx, 0x%lx\n"
296 " bmdma_addr =0x%lx, 0x%lx\n"
297 " data_addr =0x%lx\n"
298 " error_addr =0x%lx\n"
299 " feature_addr =0x%lx\n"
300 " nsect_addr =0x%lx\n"
301 " lbal_addr =0x%lx\n"
302 " lbam_addr =0x%lx\n"
303 " lbah_addr =0x%lx\n"
304 " device_addr =0x%lx\n"
305 " status_addr =0x%lx\n"
306 " command_addr =0x%lx\n"
307 " dp->dma_mode =0x%lx\n"
308 " dp->mstr_piospd =0x%lx\n"
309 " dp->slave_piospd =0x%lx\n"
310 " dp->eightb_piospd =0x%lx\n"
311 " dp->pci66mhz =0x%lx\n",
313 (unsigned long)ioaddr
->cmd_addr
,
314 (unsigned long)ATP867X_IO_PORTBASE(ap
, port
),
315 (unsigned long)ioaddr
->ctl_addr
,
316 (unsigned long)ATP867X_IO_ALTSTATUS(ap
, port
),
317 (unsigned long)ioaddr
->bmdma_addr
,
318 (unsigned long)ATP867X_IO_DMABASE(ap
, port
),
319 (unsigned long)ioaddr
->data_addr
,
320 (unsigned long)ioaddr
->error_addr
,
321 (unsigned long)ioaddr
->feature_addr
,
322 (unsigned long)ioaddr
->nsect_addr
,
323 (unsigned long)ioaddr
->lbal_addr
,
324 (unsigned long)ioaddr
->lbam_addr
,
325 (unsigned long)ioaddr
->lbah_addr
,
326 (unsigned long)ioaddr
->device_addr
,
327 (unsigned long)ioaddr
->status_addr
,
328 (unsigned long)ioaddr
->command_addr
,
329 (unsigned long)dp
->dma_mode
,
330 (unsigned long)dp
->mstr_piospd
,
331 (unsigned long)dp
->slave_piospd
,
332 (unsigned long)dp
->eightb_piospd
,
333 (unsigned long)dp
->pci66mhz
);
336 static int atp867x_set_priv(struct ata_port
*ap
)
338 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
339 struct atp867x_priv
*dp
;
340 int port
= ap
->port_no
;
342 dp
= ap
->private_data
=
343 devm_kzalloc(&pdev
->dev
, sizeof(*dp
), GFP_KERNEL
);
347 dp
->dma_mode
= ATP867X_IO_DMAMODE(ap
, port
);
348 dp
->mstr_piospd
= ATP867X_IO_MSTRPIOSPD(ap
, port
);
349 dp
->slave_piospd
= ATP867X_IO_SLAVPIOSPD(ap
, port
);
350 dp
->eightb_piospd
= ATP867X_IO_8BPIOSPD(ap
, port
);
353 ioread8(ATP867X_SYS_INFO(ap
)) & ATP867X_IO_SYS_INFO_66MHZ
;
358 static void atp867x_fixup(struct ata_host
*host
)
360 struct pci_dev
*pdev
= to_pci_dev(host
->dev
);
361 struct ata_port
*ap
= host
->ports
[0];
366 * Broken BIOS might not set latency high enough
368 pci_read_config_byte(pdev
, PCI_LATENCY_TIMER
, &v
);
371 pci_write_config_byte(pdev
, PCI_LATENCY_TIMER
, v
);
372 dev_dbg(&pdev
->dev
, "ATP867X: set latency timer to %d\n", v
);
376 * init 8bit io ports speed(0aaarrrr) to 43h and
377 * init udma modes of master/slave to 0/0(11h)
379 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
380 iowrite16(ATP867X_IO_PORTSPD_VAL
, ATP867X_IO_PORTSPD(ap
, i
));
383 * init PreREAD counts
385 for (i
= 0; i
< ATP867X_NUM_PORTS
; i
++)
386 iowrite16(ATP867X_PREREAD_VAL
, ATP867X_IO_PREREAD(ap
, i
));
388 v
= ioread8(ATP867X_IOBASE(ap
) + 0x28);
389 v
&= 0xcf; /* Enable INTA#: bit4=0 means enable */
390 v
|= 0xc0; /* Enable PCI burst, MRM & not immediate interrupts */
391 iowrite8(v
, ATP867X_IOBASE(ap
) + 0x28);
394 * Turn off the over clocked udma5 mode, only for Rev-B
396 v
= ioread8(ATP867X_SYS_INFO(ap
));
397 v
&= ATP867X_IO_SYS_MASK_RESERVED
;
398 if (pdev
->device
== PCI_DEVICE_ID_ARTOP_ATP867B
)
399 v
|= ATP867X_IO_SYS_INFO_SLOW_UDMA5
;
400 iowrite8(v
, ATP867X_SYS_INFO(ap
));
403 static int atp867x_ata_pci_sff_init_host(struct ata_host
*host
)
405 struct device
*gdev
= host
->dev
;
406 struct pci_dev
*pdev
= to_pci_dev(gdev
);
407 unsigned int mask
= 0;
413 rc
= pcim_iomap_regions(pdev
, 1 << ATP867X_BAR_IOBASE
, DRV_NAME
);
415 pcim_pin_device(pdev
);
418 host
->iomap
= pcim_iomap_table(pdev
);
420 atp867x_check_res(pdev
);
422 for (i
= 0; i
< PCI_STD_NUM_BARS
; i
++)
423 dev_dbg(gdev
, "ATP867X: iomap[%d]=0x%p\n", i
,
427 * request, iomap BARs and init port addresses accordingly
429 for (i
= 0; i
< host
->n_ports
; i
++) {
430 struct ata_port
*ap
= host
->ports
[i
];
431 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
433 ioaddr
->cmd_addr
= ATP867X_IO_PORTBASE(ap
, i
);
434 ioaddr
->ctl_addr
= ioaddr
->altstatus_addr
435 = ATP867X_IO_ALTSTATUS(ap
, i
);
436 ioaddr
->bmdma_addr
= ATP867X_IO_DMABASE(ap
, i
);
438 ata_sff_std_ports(ioaddr
);
439 rc
= atp867x_set_priv(ap
);
443 atp867x_check_ports(ap
, i
);
445 ata_port_desc(ap
, "cmd 0x%lx ctl 0x%lx",
446 (unsigned long)ioaddr
->cmd_addr
,
447 (unsigned long)ioaddr
->ctl_addr
);
448 ata_port_desc(ap
, "bmdma 0x%lx",
449 (unsigned long)ioaddr
->bmdma_addr
);
455 dev_err(gdev
, "no available native port\n");
461 return dma_set_mask_and_coherent(&pdev
->dev
, ATA_DMA_MASK
);
464 static int atp867x_init_one(struct pci_dev
*pdev
,
465 const struct pci_device_id
*id
)
467 static const struct ata_port_info info_867x
= {
468 .flags
= ATA_FLAG_SLAVE_POSS
,
469 .pio_mask
= ATA_PIO4
,
470 .udma_mask
= ATA_UDMA6
,
471 .port_ops
= &atp867x_ops
,
474 struct ata_host
*host
;
475 const struct ata_port_info
*ppi
[] = { &info_867x
, NULL
};
478 ata_print_version_once(&pdev
->dev
, DRV_VERSION
);
480 rc
= pcim_enable_device(pdev
);
484 dev_info(&pdev
->dev
, "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
487 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, ATP867X_NUM_PORTS
);
489 dev_err(&pdev
->dev
, "failed to allocate ATA host\n");
494 rc
= atp867x_ata_pci_sff_init_host(host
);
496 dev_err(&pdev
->dev
, "failed to init host\n");
500 pci_set_master(pdev
);
502 rc
= ata_host_activate(host
, pdev
->irq
, ata_bmdma_interrupt
,
503 IRQF_SHARED
, &atp867x_sht
);
505 dev_err(&pdev
->dev
, "failed to activate host\n");
511 #ifdef CONFIG_PM_SLEEP
512 static int atp867x_reinit_one(struct pci_dev
*pdev
)
514 struct ata_host
*host
= pci_get_drvdata(pdev
);
517 rc
= ata_pci_device_do_resume(pdev
);
523 ata_host_resume(host
);
528 static struct pci_device_id atp867x_pci_tbl
[] = {
529 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867A
), 0 },
530 { PCI_VDEVICE(ARTOP
, PCI_DEVICE_ID_ARTOP_ATP867B
), 0 },
534 static struct pci_driver atp867x_driver
= {
536 .id_table
= atp867x_pci_tbl
,
537 .probe
= atp867x_init_one
,
538 .remove
= ata_pci_remove_one
,
539 #ifdef CONFIG_PM_SLEEP
540 .suspend
= ata_pci_device_suspend
,
541 .resume
= atp867x_reinit_one
,
545 module_pci_driver(atp867x_driver
);
547 MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
548 MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
549 MODULE_LICENSE("GPL");
550 MODULE_DEVICE_TABLE(pci
, atp867x_pci_tbl
);
551 MODULE_VERSION(DRV_VERSION
);