Blame view

drivers/ata/pata_ns87415.c 11.1 KB
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
1
2
3
  /*
   *    pata_ns87415.c - NS87415 (non PARISC) PATA
   *
ab7716300   Alan Cox   ata: Switch all m...
4
   *	(C) 2005 Red Hat <alan@lxorguk.ukuu.org.uk>
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   *    This is a fairly generic MWDMA controller. It has some limitations
   *    as it requires timing reloads on PIO/DMA transitions but it is otherwise
   *    fairly well designed.
   *
   *    This driver assumes the firmware has left the chip in a valid ST506
   *    compliant state, either legacy IRQ 14/15 or native INTA shared. You
   *    may need to add platform code if your system fails to do this.
   *
   *    The same cell appears in the 87560 controller used by some PARISC
   *    systems. This has its own special mountain of errata.
   *
   *    TODO:
   *	Test PARISC SuperIO
   *	Get someone to test on SPARC
2dcb407e6   Jeff Garzik   [libata] checkpat...
20
   *	Implement lazy pio/dma switching for better performance
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
   *	8bit shared timing.
   *	See if we need to kill the FIFO for ATAPI
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/init.h>
  #include <linux/blkdev.h>
  #include <linux/delay.h>
  #include <linux/device.h>
  #include <scsi/scsi_host.h>
  #include <linux/libata.h>
  #include <linux/ata.h>
  
  #define DRV_NAME	"pata_ns87415"
  #define DRV_VERSION	"0.0.1"
  
  /**
   *	ns87415_set_mode - Initialize host controller mode timings
   *	@ap: Port whose timings we are configuring
   *	@adev: Device whose timings we are configuring
   *	@mode: Mode to set
   *
   *	Program the mode registers for this controller, channel and
   *	device. Because the chip is quite an old design we have to do this
   *	for PIO/DMA switches.
   *
   *	LOCKING:
   *	None (inherited from caller).
   */
  
  static void ns87415_set_mode(struct ata_port *ap, struct ata_device *adev, u8 mode)
  {
  	struct pci_dev *dev	= to_pci_dev(ap->host->dev);
  	int unit		= 2 * ap->port_no + adev->devno;
  	int timing		= 0x44 + 2 * unit;
  	unsigned long T		= 1000000000 / 33333;	/* PCI clocks */
  	struct ata_timing t;
  	u16 clocking;
  	u8 iordy;
  	u8 status;
2dcb407e6   Jeff Garzik   [libata] checkpat...
63

c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
64
65
  	/* Timing register format is 17 - low nybble read timing with
  	   the high nybble being 16 - x for recovery time in PCI clocks */
2dcb407e6   Jeff Garzik   [libata] checkpat...
66

c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
67
  	ata_timing_compute(adev, adev->pio_mode, &t, T, 0);
07633b5d0   Harvey Harrison   ata: remove FIT()...
68
69
  	clocking = 17 - clamp_val(t.active, 2, 17);
  	clocking |= (16 - clamp_val(t.recover, 1, 16)) << 4;
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
70
71
72
   	/* Use the same timing for read and write bytes */
  	clocking |= (clocking << 8);
  	pci_write_config_word(dev, timing, clocking);
2dcb407e6   Jeff Garzik   [libata] checkpat...
73

c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  	/* Set the IORDY enable versus DMA enable on or off properly */
  	pci_read_config_byte(dev, 0x42, &iordy);
  	iordy &= ~(1 << (4 + unit));
  	if (mode >= XFER_MW_DMA_0 || !ata_pio_need_iordy(adev))
  		iordy |= (1 << (4 + unit));
  
  	/* Paranoia: We shouldn't ever get here with busy write buffers
  	   but if so wait */
  
  	pci_read_config_byte(dev, 0x43, &status);
  	while (status & 0x03) {
  		udelay(1);
  		pci_read_config_byte(dev, 0x43, &status);
  	}
  	/* Flip the IORDY/DMA bits now we are sure the write buffers are
  	   clear */
  	pci_write_config_byte(dev, 0x42, iordy);
  
  	/* TODO: Set byte 54 command timing to the best 8bit
  	   mode shared by all four devices */
  }
  
  /**
   *	ns87415_set_piomode - Initialize host controller PATA PIO timings
   *	@ap: Port whose timings we are configuring
   *	@adev: Device to program
   *
   *	Set PIO mode for device, in host controller PCI config space.
   *
   *	LOCKING:
   *	None (inherited from caller).
   */
  
  static void ns87415_set_piomode(struct ata_port *ap, struct ata_device *adev)
  {
  	ns87415_set_mode(ap, adev, adev->pio_mode);
  }
  
  /**
   *	ns87415_bmdma_setup		-	Set up DMA
   *	@qc: Command block
   *
   *	Set up for bus masterng DMA. We have to do this ourselves
   *	rather than use the helper due to a chip erratum
   */
  
  static void ns87415_bmdma_setup(struct ata_queued_cmd *qc)
  {
  	struct ata_port *ap = qc->ap;
  	unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  	u8 dmactl;
  
  	/* load PRD table addr. */
  	mb();	/* make sure PRD table writes are visible to controller */
f60d70113   Tejun Heo   libata-sff: prd i...
128
  	iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
129
130
131
132
133
134
135
136
137
138
139
  
  	/* specify data direction, triple-check start bit is clear */
  	dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  	dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  	/* Due to an erratum we need to write these bits to the wrong
  	   place - which does save us an I/O bizarrely */
  	dmactl |= ATA_DMA_INTR | ATA_DMA_ERR;
  	if (!rw)
  		dmactl |= ATA_DMA_WR;
  	iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  	/* issue r/w command */
5682ed33a   Tejun Heo   libata: rename SF...
140
  	ap->ops->sff_exec_command(ap, &qc->tf);
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  }
  
  /**
   *	ns87415_bmdma_start		-	Begin DMA transfer
   *	@qc: Command block
   *
   *	Switch the timings for the chip and set up for a DMA transfer
   *	before the DMA burst begins.
   *
   *	FIXME: We should do lazy switching on bmdma_start versus
   *	ata_pio_data_xfer for better performance.
   */
  
  static void ns87415_bmdma_start(struct ata_queued_cmd *qc)
  {
  	ns87415_set_mode(qc->ap, qc->dev, qc->dev->dma_mode);
  	ata_bmdma_start(qc);
  }
  
  /**
   *	ns87415_bmdma_stop		-	End DMA transfer
   *	@qc: Command block
   *
   *	End DMA mode and switch the controller back into PIO mode
   */
  
  static void ns87415_bmdma_stop(struct ata_queued_cmd *qc)
  {
  	ata_bmdma_stop(qc);
  	ns87415_set_mode(qc->ap, qc->dev, qc->dev->pio_mode);
  }
  
  /**
9363c3825   Tejun Heo   libata: rename SF...
174
   *	ns87415_irq_clear		-	Clear interrupt
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
175
176
177
178
179
   *	@ap: Channel to clear
   *
   *	Erratum: Due to a chip bug regisers 02 and 0A bit 1 and 2 (the
   *	error bits) are reset by writing to register 00 or 08.
   */
9363c3825   Tejun Heo   libata: rename SF...
180
  static void ns87415_irq_clear(struct ata_port *ap)
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
181
182
183
184
185
  {
  	void __iomem *mmio = ap->ioaddr.bmdma_addr;
  
  	if (!mmio)
  		return;
2dcb407e6   Jeff Garzik   [libata] checkpat...
186
  	iowrite8((ioread8(mmio + ATA_DMA_CMD) | ATA_DMA_INTR | ATA_DMA_ERR),
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  			mmio + ATA_DMA_CMD);
  }
  
  /**
   *	ns87415_check_atapi_dma		-	ATAPI DMA filter
   *	@qc: Command block
   *
   *	Disable ATAPI DMA (for now). We may be able to do DMA if we
   *	kill the prefetching. This isn't clear.
   */
  
  static int ns87415_check_atapi_dma(struct ata_queued_cmd *qc)
  {
  	return -EOPNOTSUPP;
  }
  
  #if defined(CONFIG_SUPERIO)
  
  /* SUPERIO 87560 is a PoS chip that NatSem denies exists.
   * Unfortunately, it's built-in on all Astro-based PA-RISC workstations
   * which use the integrated NS87514 cell for CD-ROM support.
   * i.e we have to support for CD-ROM installs.
   * See drivers/parisc/superio.c for more gory details.
   *
   * Workarounds taken from drivers/ide/pci/ns87415.c
   */
  
  #include <asm/superio.h>
a9efacbad   Frank Lichtenheld   pata_ns87415: def...
215
  #define SUPERIO_IDE_MAX_RETRIES 25
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  /**
   *	ns87560_read_buggy	-	workaround buggy Super I/O chip
   *	@port: Port to read
   *
   *	Work around chipset problems in the 87560 SuperIO chip
   */
  
  static u8 ns87560_read_buggy(void __iomem *port)
  {
  	u8 tmp;
  	int retries = SUPERIO_IDE_MAX_RETRIES;
  	do {
  		tmp = ioread8(port);
  		if (tmp != 0)
  			return tmp;
  		udelay(50);
  	} while(retries-- > 0);
  	return tmp;
  }
  
  /**
   *	ns87560_check_status
   *	@ap: channel to check
   *
   *	Return the status of the channel working around the
   *	87560 flaws.
   */
  
  static u8 ns87560_check_status(struct ata_port *ap)
  {
  	return ns87560_read_buggy(ap->ioaddr.status_addr);
  }
  
  /**
   *	ns87560_tf_read - input device's ATA taskfile shadow registers
   *	@ap: Port from which input is read
   *	@tf: ATA taskfile register set for storing input
   *
   *	Reads ATA taskfile registers for currently-selected device
   *	into @tf. Work around the 87560 bugs.
   *
   *	LOCKING:
   *	Inherited from caller.
   */
  void ns87560_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  {
  	struct ata_ioports *ioaddr = &ap->ioaddr;
  
  	tf->command = ns87560_check_status(ap);
  	tf->feature = ioread8(ioaddr->error_addr);
  	tf->nsect = ioread8(ioaddr->nsect_addr);
  	tf->lbal = ioread8(ioaddr->lbal_addr);
  	tf->lbam = ioread8(ioaddr->lbam_addr);
  	tf->lbah = ioread8(ioaddr->lbah_addr);
  	tf->device = ns87560_read_buggy(ioaddr->device_addr);
  
  	if (tf->flags & ATA_TFLAG_LBA48) {
  		iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  		tf->hob_feature = ioread8(ioaddr->error_addr);
  		tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  		tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  		tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  		tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  		iowrite8(tf->ctl, ioaddr->ctl_addr);
  		ap->last_ctl = tf->ctl;
  	}
  }
  
  /**
   *	ns87560_bmdma_status
   *	@ap: channel to check
   *
   *	Return the DMA status of the channel working around the
   *	87560 flaws.
   */
  
  static u8 ns87560_bmdma_status(struct ata_port *ap)
  {
  	return ns87560_read_buggy(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  }
029cfd6b7   Tejun Heo   libata: implement...
296
  #endif		/* 87560 SuperIO Support */
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
297

029cfd6b7   Tejun Heo   libata: implement...
298
299
  static struct ata_port_operations ns87415_pata_ops = {
  	.inherits		= &ata_bmdma_port_ops,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
300

c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
301
  	.check_atapi_dma	= ns87415_check_atapi_dma,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
302
303
304
  	.bmdma_setup		= ns87415_bmdma_setup,
  	.bmdma_start		= ns87415_bmdma_start,
  	.bmdma_stop		= ns87415_bmdma_stop,
5682ed33a   Tejun Heo   libata: rename SF...
305
  	.sff_irq_clear		= ns87415_irq_clear,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
306

c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
307
  	.cable_detect		= ata_cable_40wire,
029cfd6b7   Tejun Heo   libata: implement...
308
309
  	.set_piomode		= ns87415_set_piomode,
  };
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
310

029cfd6b7   Tejun Heo   libata: implement...
311
312
313
  #if defined(CONFIG_SUPERIO)
  static struct ata_port_operations ns87560_pata_ops = {
  	.inherits		= &ns87415_pata_ops,
5682ed33a   Tejun Heo   libata: rename SF...
314
315
  	.sff_tf_read		= ns87560_tf_read,
  	.sff_check_status	= ns87560_check_status,
029cfd6b7   Tejun Heo   libata: implement...
316
  	.bmdma_status		= ns87560_bmdma_status,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
317
  };
029cfd6b7   Tejun Heo   libata: implement...
318
  #endif
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
319
320
  
  static struct scsi_host_template ns87415_sht = {
68d1d07b5   Tejun Heo   libata: implement...
321
  	ATA_BMDMA_SHT(DRV_NAME),
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
322
  };
a809c6875   Bartlomiej Zolnierkiewicz   pata_ns87415: Pow...
323
324
325
326
327
328
329
  static void ns87415_fixup(struct pci_dev *pdev)
  {
  	/* Select 512 byte sectors */
  	pci_write_config_byte(pdev, 0x55, 0xEE);
  	/* Select PIO0 8bit clocking */
  	pci_write_config_byte(pdev, 0x54, 0xB7);
  }
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
  
  /**
   *	ns87415_init_one - Register 87415 ATA PCI device with kernel services
   *	@pdev: PCI device to register
   *	@ent: Entry in ns87415_pci_tbl matching with @pdev
   *
   *	Called from kernel PCI layer.  We probe for combined mode (sigh),
   *	and then hand over control to libata, for it to do the rest.
   *
   *	LOCKING:
   *	Inherited from PCI layer (may sleep).
   *
   *	RETURNS:
   *	Zero on success, or -ERRNO value.
   */
  
  static int ns87415_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  {
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
348
  	static const struct ata_port_info info = {
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
349
  		.flags		= ATA_FLAG_SLAVE_POSS,
14bdef982   Erik Inge Bolsø   [libata] convert ...
350
351
  		.pio_mask	= ATA_PIO4,
  		.mwdma_mask	= ATA_MWDMA2,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
352
353
354
  		.port_ops	= &ns87415_pata_ops,
  	};
  	const struct ata_port_info *ppi[] = { &info, NULL };
f08048e94   Tejun Heo   libata: PCI devic...
355
  	int rc;
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
356
357
  #if defined(CONFIG_SUPERIO)
  	static const struct ata_port_info info87560 = {
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
358
  		.flags		= ATA_FLAG_SLAVE_POSS,
14bdef982   Erik Inge Bolsø   [libata] convert ...
359
360
  		.pio_mask	= ATA_PIO4,
  		.mwdma_mask	= ATA_MWDMA2,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
361
362
363
364
365
366
  		.port_ops	= &ns87560_pata_ops,
  	};
  
  	if (PCI_SLOT(pdev->devfn) == 0x0E)
  		ppi[0] = &info87560;
  #endif
06296a1e6   Joe Perches   ata: Add and use ...
367
  	ata_print_version_once(&pdev->dev, DRV_VERSION);
f08048e94   Tejun Heo   libata: PCI devic...
368
369
370
371
  
  	rc = pcim_enable_device(pdev);
  	if (rc)
  		return rc;
a809c6875   Bartlomiej Zolnierkiewicz   pata_ns87415: Pow...
372
  	ns87415_fixup(pdev);
1c5afdf7a   Tejun Heo   libata-sff: separ...
373
  	return ata_pci_bmdma_init_one(pdev, ppi, &ns87415_sht, NULL, 0);
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
374
375
376
377
378
379
380
  }
  
  static const struct pci_device_id ns87415_pci_tbl[] = {
  	{ PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_87415), },
  
  	{ }	/* terminate list */
  };
a809c6875   Bartlomiej Zolnierkiewicz   pata_ns87415: Pow...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  #ifdef CONFIG_PM
  static int ns87415_reinit_one(struct pci_dev *pdev)
  {
  	struct ata_host *host = dev_get_drvdata(&pdev->dev);
  	int rc;
  
  	rc = ata_pci_device_do_resume(pdev);
  	if (rc)
  		return rc;
  
  	ns87415_fixup(pdev);
  
  	ata_host_resume(host);
  	return 0;
  }
  #endif
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
397
398
399
400
401
402
403
  static struct pci_driver ns87415_pci_driver = {
  	.name			= DRV_NAME,
  	.id_table		= ns87415_pci_tbl,
  	.probe			= ns87415_init_one,
  	.remove			= ata_pci_remove_one,
  #ifdef CONFIG_PM
  	.suspend		= ata_pci_device_suspend,
a809c6875   Bartlomiej Zolnierkiewicz   pata_ns87415: Pow...
404
  	.resume			= ns87415_reinit_one,
c4b5b7b6c   Alan Cox   pata_ns87415: Ini...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  #endif
  };
  
  static int __init ns87415_init(void)
  {
  	return pci_register_driver(&ns87415_pci_driver);
  }
  
  static void __exit ns87415_exit(void)
  {
  	pci_unregister_driver(&ns87415_pci_driver);
  }
  
  module_init(ns87415_init);
  module_exit(ns87415_exit);
  
  MODULE_AUTHOR("Alan Cox");
  MODULE_DESCRIPTION("ATA low-level driver for NS87415 controllers");
  MODULE_LICENSE("GPL");
  MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
  MODULE_VERSION(DRV_VERSION);