Linux Audio

Check our new training course

Loading...
v6.8
  1/*
  2 * Freescale iMX PATA driver
  3 *
  4 * Copyright (C) 2011 Arnaud Patard <arnaud.patard@rtp-net.org>
  5 *
  6 * Based on pata_platform - Copyright (C) 2006 - 2007  Paul Mundt
  7 *
  8 * This file is subject to the terms and conditions of the GNU General Public
  9 * License.  See the file "COPYING" in the main directory of this archive
 10 * for more details.
 11 *
 12 * TODO:
 13 * - dmaengine support
 
 14 */
 15
 
 
 
 16#include <linux/ata.h>
 17#include <linux/clk.h>
 18#include <linux/libata.h>
 19#include <linux/module.h>
 20#include <linux/mod_devicetable.h>
 21#include <linux/platform_device.h>
 
 22
 23#define DRV_NAME "pata_imx"
 24
 25#define PATA_IMX_ATA_TIME_OFF		0x00
 26#define PATA_IMX_ATA_TIME_ON		0x01
 27#define PATA_IMX_ATA_TIME_1		0x02
 28#define PATA_IMX_ATA_TIME_2W		0x03
 29#define PATA_IMX_ATA_TIME_2R		0x04
 30#define PATA_IMX_ATA_TIME_AX		0x05
 31#define PATA_IMX_ATA_TIME_PIO_RDX	0x06
 32#define PATA_IMX_ATA_TIME_4		0x07
 33#define PATA_IMX_ATA_TIME_9		0x08
 34
 35#define PATA_IMX_ATA_CONTROL		0x24
 36#define PATA_IMX_ATA_CTRL_FIFO_RST_B	(1<<7)
 37#define PATA_IMX_ATA_CTRL_ATA_RST_B	(1<<6)
 38#define PATA_IMX_ATA_CTRL_IORDY_EN	(1<<0)
 39#define PATA_IMX_ATA_INT_EN		0x2C
 40#define PATA_IMX_ATA_INTR_ATA_INTRQ2	(1<<3)
 41#define PATA_IMX_DRIVE_DATA		0xA0
 42#define PATA_IMX_DRIVE_CONTROL		0xD8
 43
 44static u32 pio_t4[] = { 30,  20,  15,  10,  10 };
 45static u32 pio_t9[] = { 20,  15,  10,  10,  10 };
 46static u32 pio_tA[] = { 35,  35,  35,  35,  35 };
 47
 48struct pata_imx_priv {
 49	struct clk *clk;
 50	/* timings/interrupt/control regs */
 51	void __iomem *host_regs;
 52	u32 ata_ctl;
 53};
 54
 55static void pata_imx_set_timing(struct ata_device *adev,
 56				struct pata_imx_priv *priv)
 57{
 58	struct ata_timing timing;
 59	unsigned long clkrate;
 60	u32 T, mode;
 61
 62	clkrate = clk_get_rate(priv->clk);
 63
 64	if (adev->pio_mode < XFER_PIO_0 || adev->pio_mode > XFER_PIO_4 ||
 65	    !clkrate)
 66		return;
 67
 68	T = 1000000000 / clkrate;
 69	ata_timing_compute(adev, adev->pio_mode, &timing, T * 1000, 0);
 70
 71	mode = adev->pio_mode - XFER_PIO_0;
 72
 73	writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_OFF);
 74	writeb(3, priv->host_regs + PATA_IMX_ATA_TIME_ON);
 75	writeb(timing.setup, priv->host_regs + PATA_IMX_ATA_TIME_1);
 76	writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2W);
 77	writeb(timing.act8b, priv->host_regs + PATA_IMX_ATA_TIME_2R);
 78	writeb(1, priv->host_regs + PATA_IMX_ATA_TIME_PIO_RDX);
 79
 80	writeb(pio_t4[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_4);
 81	writeb(pio_t9[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_9);
 82	writeb(pio_tA[mode] / T + 1, priv->host_regs + PATA_IMX_ATA_TIME_AX);
 83}
 84
 85static void pata_imx_set_piomode(struct ata_port *ap, struct ata_device *adev)
 86{
 
 
 87	struct pata_imx_priv *priv = ap->host->private_data;
 88	u32 val;
 89
 90	pata_imx_set_timing(adev, priv);
 
 
 
 
 
 
 
 
 
 
 91
 92	val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
 93	if (ata_pio_need_iordy(adev))
 94		val |= PATA_IMX_ATA_CTRL_IORDY_EN;
 95	else
 96		val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
 97	__raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
 98}
 99
100static const struct scsi_host_template pata_imx_sht = {
101	ATA_PIO_SHT(DRV_NAME),
102};
103
104static struct ata_port_operations pata_imx_port_ops = {
105	.inherits		= &ata_sff_port_ops,
106	.sff_data_xfer		= ata_sff_data_xfer32,
107	.cable_detect		= ata_cable_unknown,
108	.set_piomode		= pata_imx_set_piomode,
109};
110
111static void pata_imx_setup_port(struct ata_ioports *ioaddr)
112{
113	/* Fixup the port shift for platforms that need it */
114	ioaddr->data_addr	= ioaddr->cmd_addr + (ATA_REG_DATA    << 2);
115	ioaddr->error_addr	= ioaddr->cmd_addr + (ATA_REG_ERR     << 2);
116	ioaddr->feature_addr	= ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
117	ioaddr->nsect_addr	= ioaddr->cmd_addr + (ATA_REG_NSECT   << 2);
118	ioaddr->lbal_addr	= ioaddr->cmd_addr + (ATA_REG_LBAL    << 2);
119	ioaddr->lbam_addr	= ioaddr->cmd_addr + (ATA_REG_LBAM    << 2);
120	ioaddr->lbah_addr	= ioaddr->cmd_addr + (ATA_REG_LBAH    << 2);
121	ioaddr->device_addr	= ioaddr->cmd_addr + (ATA_REG_DEVICE  << 2);
122	ioaddr->status_addr	= ioaddr->cmd_addr + (ATA_REG_STATUS  << 2);
123	ioaddr->command_addr	= ioaddr->cmd_addr + (ATA_REG_CMD     << 2);
124}
125
126static int pata_imx_probe(struct platform_device *pdev)
127{
128	struct ata_host *host;
129	struct ata_port *ap;
130	struct pata_imx_priv *priv;
131	int irq = 0;
132	struct resource *io_res;
133	int ret;
134
135	irq = platform_get_irq(pdev, 0);
136	if (irq < 0)
137		return irq;
138
139	priv = devm_kzalloc(&pdev->dev,
140				sizeof(struct pata_imx_priv), GFP_KERNEL);
141	if (!priv)
142		return -ENOMEM;
143
144	priv->clk = devm_clk_get_enabled(&pdev->dev, NULL);
145	if (IS_ERR(priv->clk)) {
146		dev_err(&pdev->dev, "Failed to get and enable clock\n");
147		return PTR_ERR(priv->clk);
148	}
149
 
 
 
 
150	host = ata_host_alloc(&pdev->dev, 1);
151	if (!host)
152		return -ENOMEM;
 
 
153
154	host->private_data = priv;
155	ap = host->ports[0];
156
157	ap->ops = &pata_imx_port_ops;
158	ap->pio_mask = ATA_PIO4;
159	ap->flags |= ATA_FLAG_SLAVE_POSS;
160
161	priv->host_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &io_res);
162	if (IS_ERR(priv->host_regs))
163		return PTR_ERR(priv->host_regs);
 
 
 
164
165	ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
166	ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
167
168	ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
169
170	pata_imx_setup_port(&ap->ioaddr);
171
172	ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
173		(unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
174		(unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
175
176	/* deassert resets */
177	__raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
178			PATA_IMX_ATA_CTRL_ATA_RST_B,
179			priv->host_regs + PATA_IMX_ATA_CONTROL);
180	/* enable interrupts */
181	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
182			priv->host_regs + PATA_IMX_ATA_INT_EN);
183
184	/* activate */
185	ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
186				&pata_imx_sht);
187
188	if (ret)
189		return ret;
190
191	return 0;
 
 
 
 
192}
193
194static void pata_imx_remove(struct platform_device *pdev)
195{
196	struct ata_host *host = platform_get_drvdata(pdev);
197	struct pata_imx_priv *priv = host->private_data;
198
199	ata_host_detach(host);
200
201	__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
 
 
 
 
202}
203
204#ifdef CONFIG_PM_SLEEP
205static int pata_imx_suspend(struct device *dev)
206{
207	struct ata_host *host = dev_get_drvdata(dev);
208	struct pata_imx_priv *priv = host->private_data;
 
209
210	ata_host_suspend(host, PMSG_SUSPEND);
211
212	__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
213	priv->ata_ctl = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
214	clk_disable_unprepare(priv->clk);
 
 
215
216	return 0;
217}
218
219static int pata_imx_resume(struct device *dev)
220{
221	struct ata_host *host = dev_get_drvdata(dev);
222	struct pata_imx_priv *priv = host->private_data;
223
224	int ret = clk_prepare_enable(priv->clk);
225	if (ret)
226		return ret;
227
228	__raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
229
230	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
231			priv->host_regs + PATA_IMX_ATA_INT_EN);
232
233	ata_host_resume(host);
234
235	return 0;
236}
237#endif
238
239static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
240
241static const struct of_device_id imx_pata_dt_ids[] = {
242	{
243		.compatible = "fsl,imx27-pata",
244	}, {
245		/* sentinel */
246	}
247};
248MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
249
250static struct platform_driver pata_imx_driver = {
251	.probe		= pata_imx_probe,
252	.remove_new	= pata_imx_remove,
253	.driver = {
254		.name		= DRV_NAME,
255		.of_match_table	= imx_pata_dt_ids,
256		.pm		= &pata_imx_pm_ops,
257	},
258};
259
260module_platform_driver(pata_imx_driver);
261
262MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
263MODULE_DESCRIPTION("low-level driver for iMX PATA");
264MODULE_LICENSE("GPL");
265MODULE_ALIAS("platform:" DRV_NAME);
v4.6
  1/*
  2 * Freescale iMX PATA driver
  3 *
  4 * Copyright (C) 2011 Arnaud Patard <arnaud.patard@rtp-net.org>
  5 *
  6 * Based on pata_platform - Copyright (C) 2006 - 2007  Paul Mundt
  7 *
  8 * This file is subject to the terms and conditions of the GNU General Public
  9 * License.  See the file "COPYING" in the main directory of this archive
 10 * for more details.
 11 *
 12 * TODO:
 13 * - dmaengine support
 14 * - check if timing stuff needed
 15 */
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/blkdev.h>
 19#include <scsi/scsi_host.h>
 20#include <linux/ata.h>
 
 21#include <linux/libata.h>
 
 
 22#include <linux/platform_device.h>
 23#include <linux/clk.h>
 24
 25#define DRV_NAME "pata_imx"
 26
 
 
 
 
 
 
 
 
 
 
 27#define PATA_IMX_ATA_CONTROL		0x24
 28#define PATA_IMX_ATA_CTRL_FIFO_RST_B	(1<<7)
 29#define PATA_IMX_ATA_CTRL_ATA_RST_B	(1<<6)
 30#define PATA_IMX_ATA_CTRL_IORDY_EN	(1<<0)
 31#define PATA_IMX_ATA_INT_EN		0x2C
 32#define PATA_IMX_ATA_INTR_ATA_INTRQ2	(1<<3)
 33#define PATA_IMX_DRIVE_DATA		0xA0
 34#define PATA_IMX_DRIVE_CONTROL		0xD8
 35
 
 
 
 
 36struct pata_imx_priv {
 37	struct clk *clk;
 38	/* timings/interrupt/control regs */
 39	void __iomem *host_regs;
 40	u32 ata_ctl;
 41};
 42
 43static int pata_imx_set_mode(struct ata_link *link, struct ata_device **unused)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44{
 45	struct ata_device *dev;
 46	struct ata_port *ap = link->ap;
 47	struct pata_imx_priv *priv = ap->host->private_data;
 48	u32 val;
 49
 50	ata_for_each_dev(dev, link, ENABLED) {
 51		dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
 52		dev->xfer_shift = ATA_SHIFT_PIO;
 53		dev->flags |= ATA_DFLAG_PIO;
 54
 55		val = __raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
 56		if (ata_pio_need_iordy(dev))
 57			val |= PATA_IMX_ATA_CTRL_IORDY_EN;
 58		else
 59			val &= ~PATA_IMX_ATA_CTRL_IORDY_EN;
 60		__raw_writel(val, priv->host_regs + PATA_IMX_ATA_CONTROL);
 61
 62		ata_dev_info(dev, "configured for PIO\n");
 63	}
 64	return 0;
 
 
 
 65}
 66
 67static struct scsi_host_template pata_imx_sht = {
 68	ATA_PIO_SHT(DRV_NAME),
 69};
 70
 71static struct ata_port_operations pata_imx_port_ops = {
 72	.inherits		= &ata_sff_port_ops,
 73	.sff_data_xfer		= ata_sff_data_xfer_noirq,
 74	.cable_detect		= ata_cable_unknown,
 75	.set_mode		= pata_imx_set_mode,
 76};
 77
 78static void pata_imx_setup_port(struct ata_ioports *ioaddr)
 79{
 80	/* Fixup the port shift for platforms that need it */
 81	ioaddr->data_addr	= ioaddr->cmd_addr + (ATA_REG_DATA    << 2);
 82	ioaddr->error_addr	= ioaddr->cmd_addr + (ATA_REG_ERR     << 2);
 83	ioaddr->feature_addr	= ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
 84	ioaddr->nsect_addr	= ioaddr->cmd_addr + (ATA_REG_NSECT   << 2);
 85	ioaddr->lbal_addr	= ioaddr->cmd_addr + (ATA_REG_LBAL    << 2);
 86	ioaddr->lbam_addr	= ioaddr->cmd_addr + (ATA_REG_LBAM    << 2);
 87	ioaddr->lbah_addr	= ioaddr->cmd_addr + (ATA_REG_LBAH    << 2);
 88	ioaddr->device_addr	= ioaddr->cmd_addr + (ATA_REG_DEVICE  << 2);
 89	ioaddr->status_addr	= ioaddr->cmd_addr + (ATA_REG_STATUS  << 2);
 90	ioaddr->command_addr	= ioaddr->cmd_addr + (ATA_REG_CMD     << 2);
 91}
 92
 93static int pata_imx_probe(struct platform_device *pdev)
 94{
 95	struct ata_host *host;
 96	struct ata_port *ap;
 97	struct pata_imx_priv *priv;
 98	int irq = 0;
 99	struct resource *io_res;
100	int ret;
101
102	irq = platform_get_irq(pdev, 0);
103	if (irq < 0)
104		return irq;
105
106	priv = devm_kzalloc(&pdev->dev,
107				sizeof(struct pata_imx_priv), GFP_KERNEL);
108	if (!priv)
109		return -ENOMEM;
110
111	priv->clk = devm_clk_get(&pdev->dev, NULL);
112	if (IS_ERR(priv->clk)) {
113		dev_err(&pdev->dev, "Failed to get clock\n");
114		return PTR_ERR(priv->clk);
115	}
116
117	ret = clk_prepare_enable(priv->clk);
118	if (ret)
119		return ret;
120
121	host = ata_host_alloc(&pdev->dev, 1);
122	if (!host) {
123		ret = -ENOMEM;
124		goto err;
125	}
126
127	host->private_data = priv;
128	ap = host->ports[0];
129
130	ap->ops = &pata_imx_port_ops;
131	ap->pio_mask = ATA_PIO0;
132	ap->flags |= ATA_FLAG_SLAVE_POSS;
133
134	io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135	priv->host_regs = devm_ioremap_resource(&pdev->dev, io_res);
136	if (IS_ERR(priv->host_regs)) {
137		ret = PTR_ERR(priv->host_regs);
138		goto err;
139	}
140
141	ap->ioaddr.cmd_addr = priv->host_regs + PATA_IMX_DRIVE_DATA;
142	ap->ioaddr.ctl_addr = priv->host_regs + PATA_IMX_DRIVE_CONTROL;
143
144	ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
145
146	pata_imx_setup_port(&ap->ioaddr);
147
148	ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
149		(unsigned long long)io_res->start + PATA_IMX_DRIVE_DATA,
150		(unsigned long long)io_res->start + PATA_IMX_DRIVE_CONTROL);
151
152	/* deassert resets */
153	__raw_writel(PATA_IMX_ATA_CTRL_FIFO_RST_B |
154			PATA_IMX_ATA_CTRL_ATA_RST_B,
155			priv->host_regs + PATA_IMX_ATA_CONTROL);
156	/* enable interrupts */
157	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
158			priv->host_regs + PATA_IMX_ATA_INT_EN);
159
160	/* activate */
161	ret = ata_host_activate(host, irq, ata_sff_interrupt, 0,
162				&pata_imx_sht);
163
164	if (ret)
165		goto err;
166
167	return 0;
168err:
169	clk_disable_unprepare(priv->clk);
170
171	return ret;
172}
173
174static int pata_imx_remove(struct platform_device *pdev)
175{
176	struct ata_host *host = platform_get_drvdata(pdev);
177	struct pata_imx_priv *priv = host->private_data;
178
179	ata_host_detach(host);
180
181	__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
182
183	clk_disable_unprepare(priv->clk);
184
185	return 0;
186}
187
188#ifdef CONFIG_PM_SLEEP
189static int pata_imx_suspend(struct device *dev)
190{
191	struct ata_host *host = dev_get_drvdata(dev);
192	struct pata_imx_priv *priv = host->private_data;
193	int ret;
194
195	ret = ata_host_suspend(host, PMSG_SUSPEND);
196	if (!ret) {
197		__raw_writel(0, priv->host_regs + PATA_IMX_ATA_INT_EN);
198		priv->ata_ctl =
199			__raw_readl(priv->host_regs + PATA_IMX_ATA_CONTROL);
200		clk_disable_unprepare(priv->clk);
201	}
202
203	return ret;
204}
205
206static int pata_imx_resume(struct device *dev)
207{
208	struct ata_host *host = dev_get_drvdata(dev);
209	struct pata_imx_priv *priv = host->private_data;
210
211	int ret = clk_prepare_enable(priv->clk);
212	if (ret)
213		return ret;
214
215	__raw_writel(priv->ata_ctl, priv->host_regs + PATA_IMX_ATA_CONTROL);
216
217	__raw_writel(PATA_IMX_ATA_INTR_ATA_INTRQ2,
218			priv->host_regs + PATA_IMX_ATA_INT_EN);
219
220	ata_host_resume(host);
221
222	return 0;
223}
224#endif
225
226static SIMPLE_DEV_PM_OPS(pata_imx_pm_ops, pata_imx_suspend, pata_imx_resume);
227
228static const struct of_device_id imx_pata_dt_ids[] = {
229	{
230		.compatible = "fsl,imx27-pata",
231	}, {
232		/* sentinel */
233	}
234};
235MODULE_DEVICE_TABLE(of, imx_pata_dt_ids);
236
237static struct platform_driver pata_imx_driver = {
238	.probe		= pata_imx_probe,
239	.remove		= pata_imx_remove,
240	.driver = {
241		.name		= DRV_NAME,
242		.of_match_table	= imx_pata_dt_ids,
243		.pm		= &pata_imx_pm_ops,
244	},
245};
246
247module_platform_driver(pata_imx_driver);
248
249MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
250MODULE_DESCRIPTION("low-level driver for iMX PATA");
251MODULE_LICENSE("GPL");
252MODULE_ALIAS("platform:" DRV_NAME);