1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Atari Falcon PATA controller driver
5  *
6  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7  *		http://www.samsung.com
8  *
9  * Based on falconide.c:
10  *
11  *     Created 12 Jul 1997 by Geert Uytterhoeven
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/blkdev.h>
18 #include <linux/delay.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <linux/ata.h>
22 #include <linux/libata.h>
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 
27 #include <asm/setup.h>
28 #include <asm/atarihw.h>
29 #include <asm/atariints.h>
30 #include <asm/atari_stdma.h>
31 #include <asm/ide.h>
32 
33 #define DRV_NAME "pata_falcon"
34 #define DRV_VERSION "0.1.0"
35 
36 #define ATA_HD_CONTROL	0x39
37 
38 static struct scsi_host_template pata_falcon_sht = {
39 	ATA_PIO_SHT(DRV_NAME),
40 };
41 
pata_falcon_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)42 static unsigned int pata_falcon_data_xfer(struct ata_queued_cmd *qc,
43 					  unsigned char *buf,
44 					  unsigned int buflen, int rw)
45 {
46 	struct ata_device *dev = qc->dev;
47 	struct ata_port *ap = dev->link->ap;
48 	void __iomem *data_addr = ap->ioaddr.data_addr;
49 	unsigned int words = buflen >> 1;
50 	struct scsi_cmnd *cmd = qc->scsicmd;
51 	bool swap = 1;
52 
53 	if (dev->class == ATA_DEV_ATA && cmd && cmd->request &&
54 	    !blk_rq_is_passthrough(cmd->request))
55 		swap = 0;
56 
57 	/* Transfer multiple of 2 bytes */
58 	if (rw == READ) {
59 		if (swap)
60 			raw_insw_swapw((u16 *)data_addr, (u16 *)buf, words);
61 		else
62 			raw_insw((u16 *)data_addr, (u16 *)buf, words);
63 	} else {
64 		if (swap)
65 			raw_outsw_swapw((u16 *)data_addr, (u16 *)buf, words);
66 		else
67 			raw_outsw((u16 *)data_addr, (u16 *)buf, words);
68 	}
69 
70 	/* Transfer trailing byte, if any. */
71 	if (unlikely(buflen & 0x01)) {
72 		unsigned char pad[2] = { };
73 
74 		/* Point buf to the tail of buffer */
75 		buf += buflen - 1;
76 
77 		if (rw == READ) {
78 			if (swap)
79 				raw_insw_swapw((u16 *)data_addr, (u16 *)pad, 1);
80 			else
81 				raw_insw((u16 *)data_addr, (u16 *)pad, 1);
82 			*buf = pad[0];
83 		} else {
84 			pad[0] = *buf;
85 			if (swap)
86 				raw_outsw_swapw((u16 *)data_addr, (u16 *)pad, 1);
87 			else
88 				raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
89 		}
90 		words++;
91 	}
92 
93 	return words << 1;
94 }
95 
96 /*
97  * Provide our own set_mode() as we don't want to change anything that has
98  * already been configured..
99  */
pata_falcon_set_mode(struct ata_link * link,struct ata_device ** unused)100 static int pata_falcon_set_mode(struct ata_link *link,
101 				struct ata_device **unused)
102 {
103 	struct ata_device *dev;
104 
105 	ata_for_each_dev(dev, link, ENABLED) {
106 		/* We don't really care */
107 		dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
108 		dev->xfer_shift = ATA_SHIFT_PIO;
109 		dev->flags |= ATA_DFLAG_PIO;
110 		ata_dev_info(dev, "configured for PIO\n");
111 	}
112 	return 0;
113 }
114 
115 static struct ata_port_operations pata_falcon_ops = {
116 	.inherits	= &ata_sff_port_ops,
117 	.sff_data_xfer	= pata_falcon_data_xfer,
118 	.cable_detect	= ata_cable_unknown,
119 	.set_mode	= pata_falcon_set_mode,
120 };
121 
pata_falcon_init_one(struct platform_device * pdev)122 static int __init pata_falcon_init_one(struct platform_device *pdev)
123 {
124 	struct resource *res;
125 	struct ata_host *host;
126 	struct ata_port *ap;
127 	void __iomem *base;
128 
129 	dev_info(&pdev->dev, "Atari Falcon PATA controller\n");
130 
131 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 	if (!res)
133 		return -ENODEV;
134 
135 	if (!devm_request_mem_region(&pdev->dev, res->start,
136 				     resource_size(res), DRV_NAME)) {
137 		dev_err(&pdev->dev, "resources busy\n");
138 		return -EBUSY;
139 	}
140 
141 	/* allocate host */
142 	host = ata_host_alloc(&pdev->dev, 1);
143 	if (!host)
144 		return -ENOMEM;
145 	ap = host->ports[0];
146 
147 	ap->ops = &pata_falcon_ops;
148 	ap->pio_mask = ATA_PIO4;
149 	ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
150 	ap->flags |= ATA_FLAG_PIO_POLLING;
151 
152 	base = (void __iomem *)res->start;
153 	ap->ioaddr.data_addr		= base;
154 	ap->ioaddr.error_addr		= base + 1 + 1 * 4;
155 	ap->ioaddr.feature_addr		= base + 1 + 1 * 4;
156 	ap->ioaddr.nsect_addr		= base + 1 + 2 * 4;
157 	ap->ioaddr.lbal_addr		= base + 1 + 3 * 4;
158 	ap->ioaddr.lbam_addr		= base + 1 + 4 * 4;
159 	ap->ioaddr.lbah_addr		= base + 1 + 5 * 4;
160 	ap->ioaddr.device_addr		= base + 1 + 6 * 4;
161 	ap->ioaddr.status_addr		= base + 1 + 7 * 4;
162 	ap->ioaddr.command_addr		= base + 1 + 7 * 4;
163 
164 	ap->ioaddr.altstatus_addr	= base + ATA_HD_CONTROL;
165 	ap->ioaddr.ctl_addr		= base + ATA_HD_CONTROL;
166 
167 	ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", (unsigned long)base,
168 		      (unsigned long)base + ATA_HD_CONTROL);
169 
170 	/* activate */
171 	return ata_host_activate(host, 0, NULL, 0, &pata_falcon_sht);
172 }
173 
pata_falcon_remove_one(struct platform_device * pdev)174 static int __exit pata_falcon_remove_one(struct platform_device *pdev)
175 {
176 	struct ata_host *host = platform_get_drvdata(pdev);
177 
178 	ata_host_detach(host);
179 
180 	return 0;
181 }
182 
183 static struct platform_driver pata_falcon_driver = {
184 	.remove = __exit_p(pata_falcon_remove_one),
185 	.driver   = {
186 		.name	= "atari-falcon-ide",
187 	},
188 };
189 
190 module_platform_driver_probe(pata_falcon_driver, pata_falcon_init_one);
191 
192 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
193 MODULE_DESCRIPTION("low-level driver for Atari Falcon PATA");
194 MODULE_LICENSE("GPL v2");
195 MODULE_ALIAS("platform:atari-falcon-ide");
196 MODULE_VERSION(DRV_VERSION);
197