1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Platform IDE driver
4  *
5  * Copyright (C) 2007 MontaVista Software
6  *
7  * Maintainer: Kumar Gala <galak@kernel.crashing.org>
8  */
9 
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/ide.h>
14 #include <linux/ioport.h>
15 #include <linux/module.h>
16 #include <linux/ata_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 
plat_ide_setup_ports(struct ide_hw * hw,void __iomem * base,void __iomem * ctrl,struct pata_platform_info * pdata,int irq)21 static void plat_ide_setup_ports(struct ide_hw *hw, void __iomem *base,
22 				 void __iomem *ctrl,
23 				 struct pata_platform_info *pdata, int irq)
24 {
25 	unsigned long port = (unsigned long)base;
26 	int i;
27 
28 	hw->io_ports.data_addr = port;
29 
30 	port += (1 << pdata->ioport_shift);
31 	for (i = 1; i <= 7;
32 	     i++, port += (1 << pdata->ioport_shift))
33 		hw->io_ports_array[i] = port;
34 
35 	hw->io_ports.ctl_addr = (unsigned long)ctrl;
36 
37 	hw->irq = irq;
38 }
39 
40 static const struct ide_port_info platform_ide_port_info = {
41 	.host_flags		= IDE_HFLAG_NO_DMA,
42 	.chipset		= ide_generic,
43 };
44 
plat_ide_probe(struct platform_device * pdev)45 static int plat_ide_probe(struct platform_device *pdev)
46 {
47 	struct resource *res_base, *res_alt, *res_irq;
48 	void __iomem *base, *alt_base;
49 	struct pata_platform_info *pdata;
50 	struct ide_host *host;
51 	int ret = 0, mmio = 0;
52 	struct ide_hw hw, *hws[] = { &hw };
53 	struct ide_port_info d = platform_ide_port_info;
54 
55 	pdata = dev_get_platdata(&pdev->dev);
56 
57 	/* get a pointer to the register memory */
58 	res_base = platform_get_resource(pdev, IORESOURCE_IO, 0);
59 	res_alt = platform_get_resource(pdev, IORESOURCE_IO, 1);
60 
61 	if (!res_base || !res_alt) {
62 		res_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
63 		res_alt = platform_get_resource(pdev, IORESOURCE_MEM, 1);
64 		if (!res_base || !res_alt) {
65 			ret = -ENOMEM;
66 			goto out;
67 		}
68 		mmio = 1;
69 	}
70 
71 	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
72 	if (!res_irq) {
73 		ret = -EINVAL;
74 		goto out;
75 	}
76 
77 	if (mmio) {
78 		base = devm_ioremap(&pdev->dev,
79 			res_base->start, resource_size(res_base));
80 		alt_base = devm_ioremap(&pdev->dev,
81 			res_alt->start, resource_size(res_alt));
82 	} else {
83 		base = devm_ioport_map(&pdev->dev,
84 			res_base->start, resource_size(res_base));
85 		alt_base = devm_ioport_map(&pdev->dev,
86 			res_alt->start, resource_size(res_alt));
87 	}
88 
89 	memset(&hw, 0, sizeof(hw));
90 	plat_ide_setup_ports(&hw, base, alt_base, pdata, res_irq->start);
91 	hw.dev = &pdev->dev;
92 
93 	d.irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
94 	if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
95 		d.irq_flags |= IRQF_SHARED;
96 
97 	if (mmio)
98 		d.host_flags |= IDE_HFLAG_MMIO;
99 
100 	ret = ide_host_add(&d, hws, 1, &host);
101 	if (ret)
102 		goto out;
103 
104 	platform_set_drvdata(pdev, host);
105 
106 	return 0;
107 
108 out:
109 	return ret;
110 }
111 
plat_ide_remove(struct platform_device * pdev)112 static int plat_ide_remove(struct platform_device *pdev)
113 {
114 	struct ide_host *host = dev_get_drvdata(&pdev->dev);
115 
116 	ide_host_remove(host);
117 
118 	return 0;
119 }
120 
121 static struct platform_driver platform_ide_driver = {
122 	.driver = {
123 		.name = "pata_platform",
124 	},
125 	.probe = plat_ide_probe,
126 	.remove = plat_ide_remove,
127 };
128 
129 module_platform_driver(platform_ide_driver);
130 
131 MODULE_DESCRIPTION("Platform IDE driver");
132 MODULE_LICENSE("GPL");
133 MODULE_ALIAS("platform:pata_platform");
134