Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
  4 *
  5 *	(C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
  6 *
  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.
 10 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 11 * TODO:
 12 *   1. RAID features [comparison, XOR, striping, mirroring, etc.]
 13 */
 14
 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>
 24
 25#define	DRV_NAME	"pata_atp867x"
 26#define	DRV_VERSION	"0.7.5"
 27
 28/*
 29 * IO Registers
 30 * Note that all runtime hot priv ports are cached in ap private_data
 31 */
 32
 33enum {
 34	ATP867X_IO_CHANNEL_OFFSET	= 0x10,
 35
 36	/*
 37	 * IO Register Bitfields
 38	 */
 39
 40	ATP867X_IO_PIOSPD_ACTIVE_SHIFT	= 4,
 41	ATP867X_IO_PIOSPD_RECOVER_SHIFT	= 0,
 42
 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,
 47
 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,
 56
 57	ATP867X_IO_SYS_INFO_66MHZ	= 0x04,
 58	ATP867X_IO_SYS_INFO_SLOW_UDMA5	= 0x02,
 59	ATP867X_IO_SYS_MASK_RESERVED	= (~0xf1),
 60
 61	ATP867X_IO_PORTSPD_VAL		= 0x1143,
 62	ATP867X_PREREAD_VAL		= 0x0200,
 63
 64	ATP867X_NUM_PORTS		= 4,
 65	ATP867X_BAR_IOBASE		= 0,
 66	ATP867X_BAR_ROMBASE		= 6,
 67};
 68
 69#define ATP867X_IOBASE(ap)		((ap)->host->iomap[0])
 70#define ATP867X_SYS_INFO(ap)		(0x3F + ATP867X_IOBASE(ap))
 71
 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)))
 76
 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)))
 81
 82/*
 83 * hot priv ports
 84 */
 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)))
 93
 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)))
 98
 99struct atp867x_priv {
100	void __iomem *dma_mode;
101	void __iomem *mstr_piospd;
102	void __iomem *slave_piospd;
103	void __iomem *eightb_piospd;
104	int		pci66mhz;
105};
106
107static void atp867x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
108{
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;
112	u8 b;
113	u8 mode = speed - XFER_UDMA_0 + 1;
114
115	/*
116	 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
117	 * on 66MHz bus
118	 *   rev-A: UDMA_1~4 (5, 6 no change)
119	 *   rev-B: all UDMA modes
120	 *   UDMA_0 stays not to disable UDMA
121	 */
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))
125		mode--;
126
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);
131	} else {
132		b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK) |
133			(mode << ATP867X_IO_DMAMODE_MSTR_SHIFT);
134	}
135	iowrite8(b, dp->dma_mode);
136}
137
138static int atp867x_get_active_clocks_shifted(struct ata_port *ap,
139	unsigned int clk)
140{
141	struct atp867x_priv *dp = ap->private_data;
142	unsigned char clocks = clk;
143
144	/*
145	 * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
146	 * on 66MHz bus
147	 */
148	if (dp->pci66mhz)
149		clocks++;
150
151	switch (clocks) {
152	case 0:
153		clocks = 1;
154		break;
155	case 1 ... 6:
156		break;
157	default:
158		ata_port_warn(ap, "ATP867X: active %dclk is invalid. "
159			"Using 12clk.\n", clk);
160		fallthrough;
161	case 9 ... 12:
162		clocks = 7;	/* 12 clk */
163		break;
164	case 7:
165	case 8:	/* default 8 clk */
166		clocks = 0;
167		goto active_clock_shift_done;
168	}
169
170active_clock_shift_done:
171	return clocks << ATP867X_IO_PIOSPD_ACTIVE_SHIFT;
172}
173
174static int atp867x_get_recover_clocks_shifted(struct ata_port *ap,
175					      unsigned int clk)
176{
177	unsigned char clocks = clk;
178
179	switch (clocks) {
180	case 0:
181		clocks = 1;
182		break;
183	case 1 ... 11:
184		break;
185	case 13:
186	case 14:
187		--clocks;	/* by the spec */
188		break;
189	case 15:
190		break;
191	default:
192		ata_port_warn(ap, "ATP867X: recover %dclk is invalid. "
193			"Using default 12clk.\n", clk);
194		fallthrough;
195	case 12:	/* default 12 clk */
196		clocks = 0;
197		break;
198	}
199
200	return clocks << ATP867X_IO_PIOSPD_RECOVER_SHIFT;
201}
202
203static void atp867x_set_piomode(struct ata_port *ap, struct ata_device *adev)
204{
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;
209	int T, UT;
210	u8 b;
211
212	T = 1000000000 / 33333;
213	UT = T / 4;
214
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);
219	}
220
221	b = ioread8(dp->dma_mode);
222	if (adev->devno & 1)
223		b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK);
224	else
225		b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK);
226	iowrite8(b, dp->dma_mode);
227
228	b = atp867x_get_active_clocks_shifted(ap, t.active) |
229		atp867x_get_recover_clocks_shifted(ap, t.recover);
230
231	if (adev->devno & 1)
232		iowrite8(b, dp->slave_piospd);
233	else
234		iowrite8(b, dp->mstr_piospd);
235
236	b = atp867x_get_active_clocks_shifted(ap, t.act8b) |
237		atp867x_get_recover_clocks_shifted(ap, t.rec8b);
238
239	iowrite8(b, dp->eightb_piospd);
240}
241
242static int atp867x_cable_override(struct pci_dev *pdev)
243{
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)) {
247		return 1;
248	}
249	return 0;
250}
251
252static int atp867x_cable_detect(struct ata_port *ap)
253{
254	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
255
256	if (atp867x_cable_override(pdev))
257		return ATA_CBL_PATA40_SHORT;
258
259	return ATA_CBL_PATA_UNK;
260}
261
262static const struct scsi_host_template atp867x_sht = {
263	ATA_BMDMA_SHT(DRV_NAME),
264};
265
266static 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,
271};
272
273
 
274static void atp867x_check_res(struct pci_dev *pdev)
275{
276	int i;
277	unsigned long start, len;
278
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",
284			start, len);
285	}
286}
287
288static void atp867x_check_ports(struct ata_port *ap, int port)
289{
290	struct ata_ioports *ioaddr = &ap->ioaddr;
291	struct atp867x_priv *dp = ap->private_data;
292
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",
312		port,
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);
334}
 
335
336static int atp867x_set_priv(struct ata_port *ap)
337{
338	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
339	struct atp867x_priv *dp;
340	int port = ap->port_no;
341
342	dp = ap->private_data =
343		devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
344	if (dp == NULL)
345		return -ENOMEM;
346
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);
351
352	dp->pci66mhz =
353		ioread8(ATP867X_SYS_INFO(ap)) & ATP867X_IO_SYS_INFO_66MHZ;
354
355	return 0;
356}
357
358static void atp867x_fixup(struct ata_host *host)
359{
360	struct pci_dev *pdev = to_pci_dev(host->dev);
361	struct ata_port *ap = host->ports[0];
362	int i;
363	u8 v;
364
365	/*
366	 * Broken BIOS might not set latency high enough
367	 */
368	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &v);
369	if (v < 0x80) {
370		v = 0x80;
371		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, v);
372		dev_dbg(&pdev->dev, "ATP867X: set latency timer to %d\n", v);
 
373	}
374
375	/*
376	 * init 8bit io ports speed(0aaarrrr) to 43h and
377	 * init udma modes of master/slave to 0/0(11h)
378	 */
379	for (i = 0; i < ATP867X_NUM_PORTS; i++)
380		iowrite16(ATP867X_IO_PORTSPD_VAL, ATP867X_IO_PORTSPD(ap, i));
381
382	/*
383	 * init PreREAD counts
384	 */
385	for (i = 0; i < ATP867X_NUM_PORTS; i++)
386		iowrite16(ATP867X_PREREAD_VAL, ATP867X_IO_PREREAD(ap, i));
387
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);
392
393	/*
394	 * Turn off the over clocked udma5 mode, only for Rev-B
395	 */
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));
401}
402
403static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
404{
405	struct device *gdev = host->dev;
406	struct pci_dev *pdev = to_pci_dev(gdev);
407	unsigned int mask = 0;
408	int i, rc;
409
410	/*
411	 * do not map rombase
412	 */
413	rc = pcim_iomap_regions(pdev, 1 << ATP867X_BAR_IOBASE, DRV_NAME);
414	if (rc == -EBUSY)
415		pcim_pin_device(pdev);
416	if (rc)
417		return rc;
418	host->iomap = pcim_iomap_table(pdev);
419
 
420	atp867x_check_res(pdev);
421
422	for (i = 0; i < PCI_STD_NUM_BARS; i++)
423		dev_dbg(gdev, "ATP867X: iomap[%d]=0x%p\n", i,
424			host->iomap[i]);
 
425
426	/*
427	 * request, iomap BARs and init port addresses accordingly
428	 */
429	for (i = 0; i < host->n_ports; i++) {
430		struct ata_port *ap = host->ports[i];
431		struct ata_ioports *ioaddr = &ap->ioaddr;
432
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);
437
438		ata_sff_std_ports(ioaddr);
439		rc = atp867x_set_priv(ap);
440		if (rc)
441			return rc;
442
 
443		atp867x_check_ports(ap, i);
444
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);
450
451		mask |= 1 << i;
452	}
453
454	if (!mask) {
455		dev_err(gdev, "no available native port\n");
456		return -ENODEV;
457	}
458
459	atp867x_fixup(host);
460
461	return dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
 
 
 
 
 
462}
463
464static int atp867x_init_one(struct pci_dev *pdev,
465	const struct pci_device_id *id)
466{
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,
472	};
473
474	struct ata_host *host;
475	const struct ata_port_info *ppi[] = { &info_867x, NULL };
476	int rc;
477
478	ata_print_version_once(&pdev->dev, DRV_VERSION);
479
480	rc = pcim_enable_device(pdev);
481	if (rc)
482		return rc;
483
484	dev_info(&pdev->dev, "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
485		pdev->device);
486
487	host = ata_host_alloc_pinfo(&pdev->dev, ppi, ATP867X_NUM_PORTS);
488	if (!host) {
489		dev_err(&pdev->dev, "failed to allocate ATA host\n");
490		rc = -ENOMEM;
491		goto err_out;
492	}
493
494	rc = atp867x_ata_pci_sff_init_host(host);
495	if (rc) {
496		dev_err(&pdev->dev, "failed to init host\n");
497		goto err_out;
498	}
499
500	pci_set_master(pdev);
501
502	rc = ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
503				IRQF_SHARED, &atp867x_sht);
504	if (rc)
505		dev_err(&pdev->dev, "failed to activate host\n");
506
507err_out:
508	return rc;
509}
510
511#ifdef CONFIG_PM_SLEEP
512static int atp867x_reinit_one(struct pci_dev *pdev)
513{
514	struct ata_host *host = pci_get_drvdata(pdev);
515	int rc;
516
517	rc = ata_pci_device_do_resume(pdev);
518	if (rc)
519		return rc;
520
521	atp867x_fixup(host);
522
523	ata_host_resume(host);
524	return 0;
525}
526#endif
527
528static 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 },
531	{ },
532};
533
534static struct pci_driver atp867x_driver = {
535	.name 		= DRV_NAME,
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,
542#endif
543};
544
545module_pci_driver(atp867x_driver);
546
547MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
548MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
549MODULE_LICENSE("GPL");
550MODULE_DEVICE_TABLE(pci, atp867x_pci_tbl);
551MODULE_VERSION(DRV_VERSION);
v4.6
 
  1/*
  2 * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
  3 *
  4 *	(C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
  5 *
  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.
  9 *
 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.
 14 *
 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.
 19 *
 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
 23 *
 24 *
 25 * TODO:
 26 *   1. RAID features [comparison, XOR, striping, mirroring, etc.]
 27 */
 28
 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>
 38
 39#define	DRV_NAME	"pata_atp867x"
 40#define	DRV_VERSION	"0.7.5"
 41
 42/*
 43 * IO Registers
 44 * Note that all runtime hot priv ports are cached in ap private_data
 45 */
 46
 47enum {
 48	ATP867X_IO_CHANNEL_OFFSET	= 0x10,
 49
 50	/*
 51	 * IO Register Bitfields
 52	 */
 53
 54	ATP867X_IO_PIOSPD_ACTIVE_SHIFT	= 4,
 55	ATP867X_IO_PIOSPD_RECOVER_SHIFT	= 0,
 56
 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,
 61
 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,
 70
 71	ATP867X_IO_SYS_INFO_66MHZ	= 0x04,
 72	ATP867X_IO_SYS_INFO_SLOW_UDMA5	= 0x02,
 73	ATP867X_IO_SYS_MASK_RESERVED	= (~0xf1),
 74
 75	ATP867X_IO_PORTSPD_VAL		= 0x1143,
 76	ATP867X_PREREAD_VAL		= 0x0200,
 77
 78	ATP867X_NUM_PORTS		= 4,
 79	ATP867X_BAR_IOBASE		= 0,
 80	ATP867X_BAR_ROMBASE		= 6,
 81};
 82
 83#define ATP867X_IOBASE(ap)		((ap)->host->iomap[0])
 84#define ATP867X_SYS_INFO(ap)		(0x3F + ATP867X_IOBASE(ap))
 85
 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)))
 90
 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)))
 95
 96/*
 97 * hot priv ports
 98 */
 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)))
107
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)))
112
113struct atp867x_priv {
114	void __iomem *dma_mode;
115	void __iomem *mstr_piospd;
116	void __iomem *slave_piospd;
117	void __iomem *eightb_piospd;
118	int		pci66mhz;
119};
120
121static void atp867x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
122{
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;
126	u8 b;
127	u8 mode = speed - XFER_UDMA_0 + 1;
128
129	/*
130	 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
131	 * on 66MHz bus
132	 *   rev-A: UDMA_1~4 (5, 6 no change)
133	 *   rev-B: all UDMA modes
134	 *   UDMA_0 stays not to disable UDMA
135	 */
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))
139		mode--;
140
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);
145	} else {
146		b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK) |
147			(mode << ATP867X_IO_DMAMODE_MSTR_SHIFT);
148	}
149	iowrite8(b, dp->dma_mode);
150}
151
152static int atp867x_get_active_clocks_shifted(struct ata_port *ap,
153	unsigned int clk)
154{
155	struct atp867x_priv *dp = ap->private_data;
156	unsigned char clocks = clk;
157
158	/*
159	 * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
160	 * on 66MHz bus
161	 */
162	if (dp->pci66mhz)
163		clocks++;
164
165	switch (clocks) {
166	case 0:
167		clocks = 1;
168		break;
169	case 1 ... 6:
170		break;
171	default:
172		printk(KERN_WARNING "ATP867X: active %dclk is invalid. "
173			"Using 12clk.\n", clk);
 
174	case 9 ... 12:
175		clocks = 7;	/* 12 clk */
176		break;
177	case 7:
178	case 8:	/* default 8 clk */
179		clocks = 0;
180		goto active_clock_shift_done;
181	}
182
183active_clock_shift_done:
184	return clocks << ATP867X_IO_PIOSPD_ACTIVE_SHIFT;
185}
186
187static int atp867x_get_recover_clocks_shifted(unsigned int clk)
 
188{
189	unsigned char clocks = clk;
190
191	switch (clocks) {
192	case 0:
193		clocks = 1;
194		break;
195	case 1 ... 11:
196		break;
197	case 13:
198	case 14:
199		--clocks;	/* by the spec */
200		break;
201	case 15:
202		break;
203	default:
204		printk(KERN_WARNING "ATP867X: recover %dclk is invalid. "
205			"Using default 12clk.\n", clk);
 
206	case 12:	/* default 12 clk */
207		clocks = 0;
208		break;
209	}
210
211	return clocks << ATP867X_IO_PIOSPD_RECOVER_SHIFT;
212}
213
214static void atp867x_set_piomode(struct ata_port *ap, struct ata_device *adev)
215{
216	struct ata_device *peer = ata_dev_pair(adev);
217	struct atp867x_priv *dp = ap->private_data;
218	u8 speed = adev->pio_mode;
219	struct ata_timing t, p;
220	int T, UT;
221	u8 b;
222
223	T = 1000000000 / 33333;
224	UT = T / 4;
225
226	ata_timing_compute(adev, speed, &t, T, UT);
227	if (peer && peer->pio_mode) {
228		ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
229		ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
230	}
231
232	b = ioread8(dp->dma_mode);
233	if (adev->devno & 1)
234		b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK);
235	else
236		b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK);
237	iowrite8(b, dp->dma_mode);
238
239	b = atp867x_get_active_clocks_shifted(ap, t.active) |
240	    atp867x_get_recover_clocks_shifted(t.recover);
241
242	if (adev->devno & 1)
243		iowrite8(b, dp->slave_piospd);
244	else
245		iowrite8(b, dp->mstr_piospd);
246
247	b = atp867x_get_active_clocks_shifted(ap, t.act8b) |
248	    atp867x_get_recover_clocks_shifted(t.rec8b);
249
250	iowrite8(b, dp->eightb_piospd);
251}
252
253static int atp867x_cable_override(struct pci_dev *pdev)
254{
255	if (pdev->subsystem_vendor == PCI_VENDOR_ID_ARTOP &&
256		(pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867A ||
257		 pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867B)) {
258		return 1;
259	}
260	return 0;
261}
262
263static int atp867x_cable_detect(struct ata_port *ap)
264{
265	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
266
267	if (atp867x_cable_override(pdev))
268		return ATA_CBL_PATA40_SHORT;
269
270	return ATA_CBL_PATA_UNK;
271}
272
273static struct scsi_host_template atp867x_sht = {
274	ATA_BMDMA_SHT(DRV_NAME),
275};
276
277static struct ata_port_operations atp867x_ops = {
278	.inherits		= &ata_bmdma_port_ops,
279	.cable_detect		= atp867x_cable_detect,
280	.set_piomode		= atp867x_set_piomode,
281	.set_dmamode		= atp867x_set_dmamode,
282};
283
284
285#ifdef	ATP867X_DEBUG
286static void atp867x_check_res(struct pci_dev *pdev)
287{
288	int i;
289	unsigned long start, len;
290
291	/* Check the PCI resources for this channel are enabled */
292	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
293		start = pci_resource_start(pdev, i);
294		len   = pci_resource_len(pdev, i);
295		printk(KERN_DEBUG "ATP867X: resource start:len=%lx:%lx\n",
296			start, len);
297	}
298}
299
300static void atp867x_check_ports(struct ata_port *ap, int port)
301{
302	struct ata_ioports *ioaddr = &ap->ioaddr;
303	struct atp867x_priv *dp = ap->private_data;
304
305	printk(KERN_DEBUG "ATP867X: port[%d] addresses\n"
306		"  cmd_addr	=0x%llx, 0x%llx\n"
307		"  ctl_addr	=0x%llx, 0x%llx\n"
308		"  bmdma_addr	=0x%llx, 0x%llx\n"
309		"  data_addr	=0x%llx\n"
310		"  error_addr	=0x%llx\n"
311		"  feature_addr	=0x%llx\n"
312		"  nsect_addr	=0x%llx\n"
313		"  lbal_addr	=0x%llx\n"
314		"  lbam_addr	=0x%llx\n"
315		"  lbah_addr	=0x%llx\n"
316		"  device_addr	=0x%llx\n"
317		"  status_addr	=0x%llx\n"
318		"  command_addr	=0x%llx\n"
319		"  dp->dma_mode	=0x%llx\n"
320		"  dp->mstr_piospd	=0x%llx\n"
321		"  dp->slave_piospd	=0x%llx\n"
322		"  dp->eightb_piospd	=0x%llx\n"
323		"  dp->pci66mhz		=0x%lx\n",
324		port,
325		(unsigned long long)ioaddr->cmd_addr,
326		(unsigned long long)ATP867X_IO_PORTBASE(ap, port),
327		(unsigned long long)ioaddr->ctl_addr,
328		(unsigned long long)ATP867X_IO_ALTSTATUS(ap, port),
329		(unsigned long long)ioaddr->bmdma_addr,
330		(unsigned long long)ATP867X_IO_DMABASE(ap, port),
331		(unsigned long long)ioaddr->data_addr,
332		(unsigned long long)ioaddr->error_addr,
333		(unsigned long long)ioaddr->feature_addr,
334		(unsigned long long)ioaddr->nsect_addr,
335		(unsigned long long)ioaddr->lbal_addr,
336		(unsigned long long)ioaddr->lbam_addr,
337		(unsigned long long)ioaddr->lbah_addr,
338		(unsigned long long)ioaddr->device_addr,
339		(unsigned long long)ioaddr->status_addr,
340		(unsigned long long)ioaddr->command_addr,
341		(unsigned long long)dp->dma_mode,
342		(unsigned long long)dp->mstr_piospd,
343		(unsigned long long)dp->slave_piospd,
344		(unsigned long long)dp->eightb_piospd,
345		(unsigned long)dp->pci66mhz);
346}
347#endif
348
349static int atp867x_set_priv(struct ata_port *ap)
350{
351	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
352	struct atp867x_priv *dp;
353	int port = ap->port_no;
354
355	dp = ap->private_data =
356		devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
357	if (dp == NULL)
358		return -ENOMEM;
359
360	dp->dma_mode	 = ATP867X_IO_DMAMODE(ap, port);
361	dp->mstr_piospd	 = ATP867X_IO_MSTRPIOSPD(ap, port);
362	dp->slave_piospd = ATP867X_IO_SLAVPIOSPD(ap, port);
363	dp->eightb_piospd = ATP867X_IO_8BPIOSPD(ap, port);
364
365	dp->pci66mhz =
366		ioread8(ATP867X_SYS_INFO(ap)) & ATP867X_IO_SYS_INFO_66MHZ;
367
368	return 0;
369}
370
371static void atp867x_fixup(struct ata_host *host)
372{
373	struct pci_dev *pdev = to_pci_dev(host->dev);
374	struct ata_port *ap = host->ports[0];
375	int i;
376	u8 v;
377
378	/*
379	 * Broken BIOS might not set latency high enough
380	 */
381	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &v);
382	if (v < 0x80) {
383		v = 0x80;
384		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, v);
385		printk(KERN_DEBUG "ATP867X: set latency timer of device %s"
386			" to %d\n", pci_name(pdev), v);
387	}
388
389	/*
390	 * init 8bit io ports speed(0aaarrrr) to 43h and
391	 * init udma modes of master/slave to 0/0(11h)
392	 */
393	for (i = 0; i < ATP867X_NUM_PORTS; i++)
394		iowrite16(ATP867X_IO_PORTSPD_VAL, ATP867X_IO_PORTSPD(ap, i));
395
396	/*
397	 * init PreREAD counts
398	 */
399	for (i = 0; i < ATP867X_NUM_PORTS; i++)
400		iowrite16(ATP867X_PREREAD_VAL, ATP867X_IO_PREREAD(ap, i));
401
402	v = ioread8(ATP867X_IOBASE(ap) + 0x28);
403	v &= 0xcf;	/* Enable INTA#: bit4=0 means enable */
404	v |= 0xc0;	/* Enable PCI burst, MRM & not immediate interrupts */
405	iowrite8(v, ATP867X_IOBASE(ap) + 0x28);
406
407	/*
408	 * Turn off the over clocked udma5 mode, only for Rev-B
409	 */
410	v = ioread8(ATP867X_SYS_INFO(ap));
411	v &= ATP867X_IO_SYS_MASK_RESERVED;
412	if (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B)
413		v |= ATP867X_IO_SYS_INFO_SLOW_UDMA5;
414	iowrite8(v, ATP867X_SYS_INFO(ap));
415}
416
417static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
418{
419	struct device *gdev = host->dev;
420	struct pci_dev *pdev = to_pci_dev(gdev);
421	unsigned int mask = 0;
422	int i, rc;
423
424	/*
425	 * do not map rombase
426	 */
427	rc = pcim_iomap_regions(pdev, 1 << ATP867X_BAR_IOBASE, DRV_NAME);
428	if (rc == -EBUSY)
429		pcim_pin_device(pdev);
430	if (rc)
431		return rc;
432	host->iomap = pcim_iomap_table(pdev);
433
434#ifdef	ATP867X_DEBUG
435	atp867x_check_res(pdev);
436
437	for (i = 0; i < PCI_ROM_RESOURCE; i++)
438		printk(KERN_DEBUG "ATP867X: iomap[%d]=0x%llx\n", i,
439			(unsigned long long)(host->iomap[i]));
440#endif
441
442	/*
443	 * request, iomap BARs and init port addresses accordingly
444	 */
445	for (i = 0; i < host->n_ports; i++) {
446		struct ata_port *ap = host->ports[i];
447		struct ata_ioports *ioaddr = &ap->ioaddr;
448
449		ioaddr->cmd_addr = ATP867X_IO_PORTBASE(ap, i);
450		ioaddr->ctl_addr = ioaddr->altstatus_addr
451				 = ATP867X_IO_ALTSTATUS(ap, i);
452		ioaddr->bmdma_addr = ATP867X_IO_DMABASE(ap, i);
453
454		ata_sff_std_ports(ioaddr);
455		rc = atp867x_set_priv(ap);
456		if (rc)
457			return rc;
458
459#ifdef	ATP867X_DEBUG
460		atp867x_check_ports(ap, i);
461#endif
462		ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
463			(unsigned long)ioaddr->cmd_addr,
464			(unsigned long)ioaddr->ctl_addr);
465		ata_port_desc(ap, "bmdma 0x%lx",
466			(unsigned long)ioaddr->bmdma_addr);
467
468		mask |= 1 << i;
469	}
470
471	if (!mask) {
472		dev_err(gdev, "no available native port\n");
473		return -ENODEV;
474	}
475
476	atp867x_fixup(host);
477
478	rc = dma_set_mask(&pdev->dev, ATA_DMA_MASK);
479	if (rc)
480		return rc;
481
482	rc = dma_set_coherent_mask(&pdev->dev, ATA_DMA_MASK);
483	return rc;
484}
485
486static int atp867x_init_one(struct pci_dev *pdev,
487	const struct pci_device_id *id)
488{
489	static const struct ata_port_info info_867x = {
490		.flags		= ATA_FLAG_SLAVE_POSS,
491		.pio_mask	= ATA_PIO4,
492		.udma_mask 	= ATA_UDMA6,
493		.port_ops	= &atp867x_ops,
494	};
495
496	struct ata_host *host;
497	const struct ata_port_info *ppi[] = { &info_867x, NULL };
498	int rc;
499
500	ata_print_version_once(&pdev->dev, DRV_VERSION);
501
502	rc = pcim_enable_device(pdev);
503	if (rc)
504		return rc;
505
506	printk(KERN_INFO "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
507		pdev->device);
508
509	host = ata_host_alloc_pinfo(&pdev->dev, ppi, ATP867X_NUM_PORTS);
510	if (!host) {
511		dev_err(&pdev->dev, "failed to allocate ATA host\n");
512		rc = -ENOMEM;
513		goto err_out;
514	}
515
516	rc = atp867x_ata_pci_sff_init_host(host);
517	if (rc) {
518		dev_err(&pdev->dev, "failed to init host\n");
519		goto err_out;
520	}
521
522	pci_set_master(pdev);
523
524	rc = ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
525				IRQF_SHARED, &atp867x_sht);
526	if (rc)
527		dev_err(&pdev->dev, "failed to activate host\n");
528
529err_out:
530	return rc;
531}
532
533#ifdef CONFIG_PM_SLEEP
534static int atp867x_reinit_one(struct pci_dev *pdev)
535{
536	struct ata_host *host = pci_get_drvdata(pdev);
537	int rc;
538
539	rc = ata_pci_device_do_resume(pdev);
540	if (rc)
541		return rc;
542
543	atp867x_fixup(host);
544
545	ata_host_resume(host);
546	return 0;
547}
548#endif
549
550static struct pci_device_id atp867x_pci_tbl[] = {
551	{ PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867A),	0 },
552	{ PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867B),	0 },
553	{ },
554};
555
556static struct pci_driver atp867x_driver = {
557	.name 		= DRV_NAME,
558	.id_table 	= atp867x_pci_tbl,
559	.probe 		= atp867x_init_one,
560	.remove		= ata_pci_remove_one,
561#ifdef CONFIG_PM_SLEEP
562	.suspend	= ata_pci_device_suspend,
563	.resume		= atp867x_reinit_one,
564#endif
565};
566
567module_pci_driver(atp867x_driver);
568
569MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
570MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
571MODULE_LICENSE("GPL");
572MODULE_DEVICE_TABLE(pci, atp867x_pci_tbl);
573MODULE_VERSION(DRV_VERSION);