summaryrefslogtreecommitdiff
path: root/target/linux/ppc40x/patches-2.6.30/101-pata-magicbox-cf-driver.patch
blob: 84794d96408f8f7b47c00ed829e534f5ebc81b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
63
64
65
66
67
68
69
70
71
72
73
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
128
129
130
131
132
133
134
135
136
137
138
139
140
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
174
175
176
177
178
179
180
181
182
183
184
185
186
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
215
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--- a/drivers/ata/Kconfig
+++ b/drivers/ata/Kconfig
@@ -698,6 +698,16 @@ config PATA_IXP4XX_CF
 
 	  If unsure, say N.
 
+config PATA_MAGICBOX_CF
+	tristate "Magicbox/OpenRB Compact Flash support"
+	depends on MAGICBOXV2 || OPENRB_LIGHT
+	help
+	  This option enables support for a Compact Flash conected on
+	  the ppc405ep expansion bus. This driver had been written for
+	  the Magicbox v2 and OpenRB boards.
+
+	  If unsure, say N.
+
 config PATA_OCTEON_CF
 	tristate "OCTEON Boot Bus Compact Flash support"
 	depends on CPU_CAVIUM_OCTEON
--- a/drivers/ata/Makefile
+++ b/drivers/ata/Makefile
@@ -48,6 +48,7 @@ obj-$(CONFIG_PATA_OPTI)		+= pata_opti.o
 obj-$(CONFIG_PATA_OPTIDMA)	+= pata_optidma.o
 obj-$(CONFIG_PATA_MPC52xx)	+= pata_mpc52xx.o
 obj-$(CONFIG_PATA_MARVELL)	+= pata_marvell.o
+obj-$(CONFIG_PATA_MAGICBOX_CF)	+= pata_magicbox_cf.o
 obj-$(CONFIG_PATA_MPIIX)	+= pata_mpiix.o
 obj-$(CONFIG_PATA_OLDPIIX)	+= pata_oldpiix.o
 obj-$(CONFIG_PATA_PCMCIA)	+= pata_pcmcia.o
--- /dev/null
+++ b/drivers/ata/pata_magicbox_cf.c
@@ -0,0 +1,404 @@
+/*
+ *  PATA/CompactFlash driver for the MagicBox v2/OpenRB boards.
+ *
+ *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
+ *
+ *  Based on the IDE driver by Wojtek Kaniewski <wojtekka@toxygen.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License version 2 as published
+ *  by the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/ioport.h>
+#include <linux/libata.h>
+#include <linux/irq.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <scsi/scsi_host.h>
+
+#define DRV_DESC	"PATA/CompactFlash driver for Magicbox/OpenRB boards"
+#define DRV_NAME	"pata_magicbox_cf"
+#define DRV_VERSION	"0.1.0"
+
+#define MAGICBOX_CF_REG_CMD		(2 * ATA_REG_CMD)
+#define MAGICBOX_CF_REG_DATA		(2 * ATA_REG_DATA)
+#define MAGICBOX_CF_REG_ERR		(2 * ATA_REG_ERR)
+#define MAGICBOX_CF_REG_FEATURE 	(2 * ATA_REG_FEATURE)
+#define MAGICBOX_CF_REG_NSECT		(2 * ATA_REG_NSECT)
+#define MAGICBOX_CF_REG_LBAL		(2 * ATA_REG_LBAL)
+#define MAGICBOX_CF_REG_LBAM		(2 * ATA_REG_LBAM)
+#define MAGICBOX_CF_REG_LBAH		(2 * ATA_REG_LBAH)
+#define MAGICBOX_CF_REG_DEVICE		(2 * ATA_REG_DEVICE)
+#define MAGICBOX_CF_REG_STATUS		(2 * ATA_REG_STATUS)
+#define MAGICBOX_CF_REG_ALTSTATUS	(2 * 6)
+#define MAGICBOX_CF_REG_CTL		(2 * 6)
+
+#define MAGICBOX_CF_MAXPORTS	1
+
+struct magicbox_cf_info {
+	void __iomem	*base;
+	void __iomem	*ctrl;
+};
+
+static inline u8 magicbox_cf_inb(void __iomem *port)
+{
+	return (u8) (readw(port) >> 8) & 0xff;
+}
+
+static inline void magicbox_cf_outb(void __iomem *port, u8 value)
+{
+	writew(value << 8, port);
+}
+
+static int magicbox_cf_set_mode(struct ata_link *link,
+				struct ata_device **error)
+{
+	struct ata_device *dev;
+
+	ata_for_each_dev(dev, link, ENABLED) {
+		ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
+		dev->pio_mode = XFER_PIO_0;
+		dev->xfer_mode = XFER_PIO_0;
+		dev->xfer_shift = ATA_SHIFT_PIO;
+		dev->flags |= ATA_DFLAG_PIO;
+	}
+
+	return 0;
+}
+
+static void magicbox_cf_exec_command(struct ata_port *ap,
+				     const struct ata_taskfile *tf)
+{
+	DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
+
+	magicbox_cf_outb(ap->ioaddr.command_addr, tf->command);
+	ata_sff_pause(ap);
+}
+
+static u8 magicbox_cf_check_status(struct ata_port *ap)
+{
+	u8 status;
+
+	status = magicbox_cf_inb(ap->ioaddr.status_addr);
+
+	DPRINTK("ata%u: status 0x%X, from %p\n", ap->print_id, status,
+		ap->ioaddr.status_addr);
+
+	return status;
+}
+
+static u8 magicbox_cf_check_altstatus(struct ata_port *ap)
+{
+	u8 altstatus;
+
+	altstatus = magicbox_cf_inb(ap->ioaddr.altstatus_addr);
+
+	DPRINTK("ata%u: altstatus 0x%X, from %p\n", ap->print_id,
+		altstatus, ap->ioaddr.altstatus_addr);
+
+	return altstatus;
+}
+
+static void magicbox_cf_dev_select(struct ata_port *ap, unsigned int device)
+{
+	/* Nothing to do. We are supporting one device only. */
+}
+
+static void magicbox_cf_tf_load(struct ata_port *ap,
+			        const struct ata_taskfile *tf)
+{
+	struct ata_ioports *ioaddr = &ap->ioaddr;
+	unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
+
+	if (tf->ctl != ap->last_ctl) {
+		magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
+		ap->last_ctl = tf->ctl;
+		ata_wait_idle(ap);
+	}
+
+	if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
+		magicbox_cf_outb(ioaddr->feature_addr, tf->hob_feature);
+		magicbox_cf_outb(ioaddr->nsect_addr, tf->hob_nsect);
+		magicbox_cf_outb(ioaddr->lbal_addr, tf->hob_lbal);
+		magicbox_cf_outb(ioaddr->lbam_addr, tf->hob_lbam);
+		magicbox_cf_outb(ioaddr->lbah_addr, tf->hob_lbah);
+		VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
+			tf->hob_feature,
+			tf->hob_nsect,
+			tf->hob_lbal,
+			tf->hob_lbam,
+			tf->hob_lbah);
+	}
+
+	if (is_addr) {
+		magicbox_cf_outb(ioaddr->feature_addr, tf->feature);
+		magicbox_cf_outb(ioaddr->nsect_addr, tf->nsect);
+		magicbox_cf_outb(ioaddr->lbal_addr, tf->lbal);
+		magicbox_cf_outb(ioaddr->lbam_addr, tf->lbam);
+		magicbox_cf_outb(ioaddr->lbah_addr, tf->lbah);
+		VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
+			tf->feature,
+			tf->nsect,
+			tf->lbal,
+			tf->lbam,
+			tf->lbah);
+	}
+
+	if (tf->flags & ATA_TFLAG_DEVICE) {
+		magicbox_cf_outb(ioaddr->device_addr, tf->device);
+		VPRINTK("device 0x%X\n", tf->device);
+	}
+
+	ata_wait_idle(ap);
+}
+
+static void magicbox_cf_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
+{
+	struct ata_ioports *ioaddr = &ap->ioaddr;
+
+	tf->command = magicbox_cf_inb(ap->ioaddr.status_addr);
+	tf->feature = magicbox_cf_inb(ioaddr->error_addr);
+	tf->nsect = magicbox_cf_inb(ioaddr->nsect_addr);
+	tf->lbal = magicbox_cf_inb(ioaddr->lbal_addr);
+	tf->lbam = magicbox_cf_inb(ioaddr->lbam_addr);
+	tf->lbah = magicbox_cf_inb(ioaddr->lbah_addr);
+	tf->device = magicbox_cf_inb(ioaddr->device_addr);
+	VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
+		tf->feature,
+		tf->nsect,
+		tf->lbal,
+		tf->lbam,
+		tf->lbah);
+
+	if (tf->flags & ATA_TFLAG_LBA48) {
+		magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl | ATA_HOB);
+		tf->hob_feature = magicbox_cf_inb(ioaddr->error_addr);
+		tf->hob_nsect = magicbox_cf_inb(ioaddr->nsect_addr);
+		tf->hob_lbal = magicbox_cf_inb(ioaddr->lbal_addr);
+		tf->hob_lbam = magicbox_cf_inb(ioaddr->lbam_addr);
+		tf->hob_lbah = magicbox_cf_inb(ioaddr->lbah_addr);
+		magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
+		ap->last_ctl = tf->ctl;
+		VPRINTK("hob: feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
+			tf->feature,
+			tf->nsect,
+			tf->lbal,
+			tf->lbam,
+			tf->lbah);
+	}
+}
+
+static unsigned int magicbox_cf_data_xfer(struct ata_device *dev,
+					  unsigned char *buf,
+					  unsigned int buflen, int rw)
+{
+	struct ata_port *ap = dev->link->ap;
+	unsigned int words = buflen >> 1;
+	unsigned int i;
+	u16 *buf16 = (u16 *) buf;
+	void __iomem *mmio = ap->ioaddr.data_addr;
+
+	/* Transfer multiple of 2 bytes */
+	if (rw == READ)
+		for (i = 0; i < words; i++)
+			buf16[i] = readw(mmio);
+	else
+		for (i = 0; i < words; i++)
+			writew(buf16[i], mmio);
+
+	/* Transfer trailing 1 byte, if any. */
+	if (unlikely(buflen & 0x01)) {
+		u16 align_buf[1] = { 0 };
+		unsigned char *trailing_buf = buf + buflen - 1;
+
+		if (rw == READ) {
+			align_buf[0] = readw(mmio);
+			memcpy(trailing_buf, align_buf, 1);
+		} else {
+			memcpy(align_buf, trailing_buf, 1);
+			writew(align_buf[0], mmio);
+		}
+		words++;
+	}
+
+	return words << 1;
+}
+
+static u8 magicbox_cf_irq_on(struct ata_port *ap)
+{
+	/* Nothing to do. */
+	return 0;
+}
+
+static void magicbox_cf_irq_clear(struct ata_port *ap)
+{
+	/* Nothing to do. */
+}
+
+static struct ata_port_operations magicbox_cf_port_ops = {
+	.inherits		= &ata_sff_port_ops,
+
+	.cable_detect		= ata_cable_40wire,
+	.set_mode		= magicbox_cf_set_mode,
+
+	.sff_exec_command	= magicbox_cf_exec_command,
+	.sff_check_status	= magicbox_cf_check_status,
+	.sff_check_altstatus	= magicbox_cf_check_altstatus,
+	.sff_dev_select		= magicbox_cf_dev_select,
+	.sff_tf_load		= magicbox_cf_tf_load,
+	.sff_tf_read		= magicbox_cf_tf_read,
+	.sff_data_xfer		= magicbox_cf_data_xfer,
+
+	.sff_irq_on		= magicbox_cf_irq_on,
+	.sff_irq_clear		= magicbox_cf_irq_clear,
+
+	.port_start		= ATA_OP_NULL,
+};
+
+static struct scsi_host_template magicbox_cf_sht = {
+	ATA_PIO_SHT(DRV_NAME),
+};
+
+static inline void magicbox_cf_setup_port(struct ata_host *host)
+{
+	struct magicbox_cf_info *info = host->private_data;
+	struct ata_port *ap;
+
+	ap = host->ports[0];
+
+	ap->ops	= &magicbox_cf_port_ops;
+	ap->pio_mask = ATA_PIO4;
+	ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
+
+	ap->ioaddr.cmd_addr = info->base + MAGICBOX_CF_REG_CMD;
+	ap->ioaddr.data_addr = info->base + MAGICBOX_CF_REG_DATA;
+	ap->ioaddr.error_addr = info->base + MAGICBOX_CF_REG_ERR;
+	ap->ioaddr.feature_addr = info->base + MAGICBOX_CF_REG_FEATURE;
+	ap->ioaddr.nsect_addr = info->base + MAGICBOX_CF_REG_NSECT;
+	ap->ioaddr.lbal_addr = info->base + MAGICBOX_CF_REG_LBAL;
+	ap->ioaddr.lbam_addr = info->base + MAGICBOX_CF_REG_LBAM;
+	ap->ioaddr.lbah_addr = info->base + MAGICBOX_CF_REG_LBAH;
+	ap->ioaddr.device_addr = info->base + MAGICBOX_CF_REG_DEVICE;
+	ap->ioaddr.status_addr = info->base + MAGICBOX_CF_REG_STATUS;
+	ap->ioaddr.command_addr = info->base + MAGICBOX_CF_REG_CMD;
+
+	ap->ioaddr.altstatus_addr = info->ctrl + MAGICBOX_CF_REG_ALTSTATUS;
+	ap->ioaddr.ctl_addr = info->ctrl + MAGICBOX_CF_REG_CTL;
+
+	ata_port_desc(ap, "cmd 0x%p ctl 0x%p", ap->ioaddr.cmd_addr,
+		      ap->ioaddr.ctl_addr);
+}
+
+static int __devinit magicbox_cf_of_probe(struct of_device *op,
+					   const struct of_device_id *match)
+{
+	struct magicbox_cf_info *info;
+	struct ata_host *host;
+	int irq;
+	int ret = 0;
+
+	info = kzalloc(sizeof(struct magicbox_cf_info), GFP_KERNEL);
+	if (info == NULL) {
+		ret = -ENOMEM;
+		goto err_exit;
+	}
+
+	irq = irq_of_parse_and_map(op->node, 0);
+	if (irq < 0) {
+		dev_err(&op->dev, "invalid irq\n");
+		ret = -EINVAL;
+		goto err_free_info;
+	}
+
+	info->base = of_iomap(op->node, 0);
+	if (info->base == NULL) {
+		ret = -ENOMEM;
+		goto err_free_info;
+	}
+
+	info->ctrl = of_iomap(op->node, 1);
+	if (info->ctrl == NULL) {
+		ret = -ENOMEM;
+		goto err_unmap_base;
+	}
+
+	host = ata_host_alloc(&op->dev, MAGICBOX_CF_MAXPORTS);
+	if (host == NULL) {
+		ret = -ENOMEM;
+		goto err_unmap_ctrl;
+	}
+
+	host->private_data = info;
+	magicbox_cf_setup_port(host);
+
+	ret = ata_host_activate(host, irq, ata_sff_interrupt,
+				IRQF_TRIGGER_RISING, &magicbox_cf_sht);
+	if (ret)
+		goto err_unmap_ctrl;
+
+	dev_set_drvdata(&op->dev, host);
+	return 0;
+
+ err_unmap_ctrl:
+	iounmap(info->ctrl);
+ err_unmap_base:
+	iounmap(info->base);
+ err_free_info:
+	kfree(info);
+ err_exit:
+	return ret;
+}
+
+static __devexit int magicbox_cf_of_remove(struct of_device *op)
+{
+	struct ata_host *host = dev_get_drvdata(&op->dev);
+	struct magicbox_cf_info *info = host->private_data;
+
+	ata_host_detach(host);
+	iounmap(info->ctrl);
+	iounmap(info->base);
+	kfree(info);
+
+	return 0;
+}
+
+static struct of_device_id magicbox_cf_of_match[] = {
+	{ .compatible = "pata-magicbox-cf", },
+	{},
+};
+
+static struct of_platform_driver magicbox_cf_of_platform_driver = {
+	.owner		= THIS_MODULE,
+	.name		= DRV_NAME,
+	.match_table	= magicbox_cf_of_match,
+	.probe		= magicbox_cf_of_probe,
+	.remove		= __devexit_p(magicbox_cf_of_remove),
+	.driver		= {
+		.name	= DRV_NAME,
+		.owner	= THIS_MODULE,
+	},
+};
+
+static int __init magicbox_cf_init(void)
+{
+	return of_register_platform_driver(&magicbox_cf_of_platform_driver);
+}
+
+static void __exit magicbox_cf_exit(void)
+{
+	of_unregister_platform_driver(&magicbox_cf_of_platform_driver);
+}
+
+module_init(magicbox_cf_init);
+module_exit(magicbox_cf_exit);
+
+MODULE_DESCRIPTION(DRV_DESC);
+MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
+MODULE_LICENSE("GPL v2");
+MODULE_VERSION(DRV_VERSION);
+MODULE_DEVICE_TABLE(of, magicbox_cf_of_match);