xref: /linux/drivers/ata/pata_rb532_cf.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  A low-level PATA driver to handle a Compact Flash connected on the
4  *  Mikrotik's RouterBoard 532 board.
5  *
6  *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
7  *  Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
8  *
9  *  This file was based on: drivers/ata/pata_ixp4xx_cf.c
10  *	Copyright (C) 2006-07 Tower Technologies
11  *	Author: Alessandro Zummo <a.zummo@towertech.it>
12  *
13  *  Also was based on the driver for Linux 2.4.xx published by Mikrotik for
14  *  their RouterBoard 1xx and 5xx series devices. The original Mikrotik code
15  *  seems not to have a license.
16  */
17 
18 #include <linux/gfp.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 
23 #include <linux/io.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio/consumer.h>
27 
28 #include <linux/libata.h>
29 #include <scsi/scsi_host.h>
30 
31 #include <asm/mach-rc32434/rb.h>
32 
33 #define DRV_NAME	"pata-rb532-cf"
34 #define DRV_VERSION	"0.1.0"
35 #define DRV_DESC	"PATA driver for RouterBOARD 532 Compact Flash"
36 
37 #define RB500_CF_MAXPORTS	1
38 #define RB500_CF_IO_DELAY	400
39 
40 #define RB500_CF_REG_BASE	0x0800
41 #define RB500_CF_REG_ERR	0x080D
42 #define RB500_CF_REG_CTRL	0x080E
43 /* 32bit buffered data register offset */
44 #define RB500_CF_REG_DBUF32	0x0C00
45 
46 struct rb532_cf_info {
47 	void __iomem	*iobase;
48 	struct gpio_desc *gpio_line;
49 	unsigned int	irq;
50 };
51 
52 /* ------------------------------------------------------------------------ */
53 
54 static irqreturn_t rb532_pata_irq_handler(int irq, void *dev_instance)
55 {
56 	struct ata_host *ah = dev_instance;
57 	struct rb532_cf_info *info = ah->private_data;
58 
59 	if (gpiod_get_value(info->gpio_line)) {
60 		irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
61 		ata_sff_interrupt(info->irq, dev_instance);
62 	} else {
63 		irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
64 	}
65 
66 	return IRQ_HANDLED;
67 }
68 
69 static struct ata_port_operations rb532_pata_port_ops = {
70 	.inherits		= &ata_sff_port_ops,
71 	.sff_data_xfer		= ata_sff_data_xfer32,
72 };
73 
74 /* ------------------------------------------------------------------------ */
75 
76 static const struct scsi_host_template rb532_pata_sht = {
77 	ATA_PIO_SHT(DRV_NAME),
78 };
79 
80 /* ------------------------------------------------------------------------ */
81 
82 static void rb532_pata_setup_ports(struct ata_host *ah)
83 {
84 	struct rb532_cf_info *info = ah->private_data;
85 	struct ata_port *ap;
86 
87 	ap = ah->ports[0];
88 
89 	ap->ops		= &rb532_pata_port_ops;
90 	ap->pio_mask	= ATA_PIO4;
91 
92 	ap->ioaddr.cmd_addr	= info->iobase + RB500_CF_REG_BASE;
93 	ap->ioaddr.ctl_addr	= info->iobase + RB500_CF_REG_CTRL;
94 	ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL;
95 
96 	ata_sff_std_ports(&ap->ioaddr);
97 
98 	ap->ioaddr.data_addr	= info->iobase + RB500_CF_REG_DBUF32;
99 	ap->ioaddr.error_addr	= info->iobase + RB500_CF_REG_ERR;
100 }
101 
102 static int rb532_pata_driver_probe(struct platform_device *pdev)
103 {
104 	int irq;
105 	struct gpio_desc *gpiod;
106 	struct ata_host *ah;
107 	struct rb532_cf_info *info;
108 	void __iomem *iobase;
109 	int ret;
110 
111 	iobase = devm_platform_ioremap_resource(pdev, 0);
112 	if (IS_ERR(iobase))
113 		return PTR_ERR(iobase);
114 
115 	irq = platform_get_irq(pdev, 0);
116 	if (irq < 0)
117 		return irq;
118 	if (!irq)
119 		return -EINVAL;
120 
121 	gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
122 	if (IS_ERR(gpiod)) {
123 		dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
124 		return PTR_ERR(gpiod);
125 	}
126 	gpiod_set_consumer_name(gpiod, DRV_NAME);
127 
128 	/* allocate host */
129 	ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS);
130 	if (!ah)
131 		return -ENOMEM;
132 
133 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
134 	if (!info)
135 		return -ENOMEM;
136 
137 	ah->private_data = info;
138 	info->gpio_line = gpiod;
139 	info->irq = irq;
140 	info->iobase = iobase;
141 
142 	rb532_pata_setup_ports(ah);
143 
144 	ret = ata_host_activate(ah, irq, rb532_pata_irq_handler,
145 				IRQF_TRIGGER_LOW, &rb532_pata_sht);
146 	if (ret)
147 		return ret;
148 
149 	return 0;
150 }
151 
152 static void rb532_pata_driver_remove(struct platform_device *pdev)
153 {
154 	struct ata_host *ah = platform_get_drvdata(pdev);
155 
156 	ata_host_detach(ah);
157 }
158 
159 static struct platform_driver rb532_pata_platform_driver = {
160 	.probe		= rb532_pata_driver_probe,
161 	.remove		= rb532_pata_driver_remove,
162 	.driver	 = {
163 		.name   = DRV_NAME,
164 	},
165 };
166 
167 #define DRV_INFO DRV_DESC " version " DRV_VERSION
168 
169 module_platform_driver(rb532_pata_platform_driver);
170 
171 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
172 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
173 MODULE_DESCRIPTION(DRV_DESC);
174 MODULE_VERSION(DRV_VERSION);
175 MODULE_LICENSE("GPL");
176 MODULE_ALIAS("platform:" DRV_NAME);
177