1
2
3
4
5
6
7
8
9
10
11#define DEBUG
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/platform_device.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/slab.h>
22#include <scsi/scsi_host.h>
23#include <linux/ata.h>
24#include <linux/libata.h>
25#include <linux/err.h>
26#include <linux/io.h>
27
28#include <mach/board.h>
29#include <mach/smc.h>
30
31#define DRV_NAME "pata_at32"
32#define DRV_VERSION "0.0.3"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#define CF_IDE_OFFSET 0x00c00000
49#define CF_ALT_IDE_OFFSET 0x00e00000
50#define CF_RES_SIZE 2048
51
52
53
54
55
56#undef DEBUG_BUS
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71enum {
72 PIO_MASK = ATA_PIO4,
73};
74
75
76
77
78struct at32_ide_info {
79 unsigned int irq;
80 struct resource res_ide;
81 struct resource res_alt;
82 void __iomem *ide_addr;
83 void __iomem *alt_addr;
84 unsigned int cs;
85 struct smc_config smc;
86};
87
88
89
90
91static int pata_at32_setup_timing(struct device *dev,
92 struct at32_ide_info *info,
93 const struct ata_timing *ata)
94{
95 struct smc_config *smc = &info->smc;
96 struct smc_timing timing;
97
98 int active;
99 int recover;
100
101 memset(&timing, 0, sizeof(struct smc_timing));
102
103
104 timing.read_cycle = ata->cyc8b;
105
106
107 timing.nrd_setup = ata->setup;
108 timing.nrd_pulse = ata->act8b;
109 timing.nrd_recover = ata->rec8b;
110
111
112 smc_set_timing(smc, &timing);
113
114
115 smc->nrd_setup = smc->nrd_setup + 1;
116
117 active = smc->nrd_setup + smc->nrd_pulse;
118 recover = smc->read_cycle - active;
119
120
121 if (recover < 2)
122 smc->read_cycle = active + 2;
123
124
125 smc->ncs_read_setup = 1;
126 smc->ncs_read_pulse = smc->read_cycle - 2;
127
128
129 smc->write_cycle = smc->read_cycle;
130 smc->nwe_setup = smc->nrd_setup;
131 smc->nwe_pulse = smc->nrd_pulse;
132 smc->ncs_write_setup = smc->ncs_read_setup;
133 smc->ncs_write_pulse = smc->ncs_read_pulse;
134
135
136 dev_dbg(dev, "ATA: C=%d S=%d P=%d R=%d\n",
137 ata->cyc8b, ata->setup, ata->act8b, ata->rec8b);
138
139 dev_dbg(dev, "SMC: C=%d S=%d P=%d NS=%d NP=%d\n",
140 smc->read_cycle, smc->nrd_setup, smc->nrd_pulse,
141 smc->ncs_read_setup, smc->ncs_read_pulse);
142
143
144 return smc_set_configuration(info->cs, smc);
145}
146
147
148
149
150static void pata_at32_set_piomode(struct ata_port *ap, struct ata_device *adev)
151{
152 struct ata_timing timing;
153 struct at32_ide_info *info = ap->host->private_data;
154
155 int ret;
156
157
158 ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
159 if (ret) {
160 dev_warn(ap->dev, "Failed to compute ATA timing %d\n", ret);
161 return;
162 }
163
164
165 ret = pata_at32_setup_timing(ap->dev, info, &timing);
166 if (ret) {
167 dev_warn(ap->dev, "Failed to setup ATA timing %d\n", ret);
168 return;
169 }
170}
171
172static struct scsi_host_template at32_sht = {
173 ATA_PIO_SHT(DRV_NAME),
174};
175
176static struct ata_port_operations at32_port_ops = {
177 .inherits = &ata_sff_port_ops,
178 .cable_detect = ata_cable_40wire,
179 .set_piomode = pata_at32_set_piomode,
180};
181
182static int __init pata_at32_init_one(struct device *dev,
183 struct at32_ide_info *info)
184{
185 struct ata_host *host;
186 struct ata_port *ap;
187
188 host = ata_host_alloc(dev, 1);
189 if (!host)
190 return -ENOMEM;
191
192 ap = host->ports[0];
193
194
195 ap->ops = &at32_port_ops;
196 ap->pio_mask = PIO_MASK;
197 ap->flags |= ATA_FLAG_SLAVE_POSS;
198
199
200
201
202
203
204
205
206
207
208
209
210
211 ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
212 ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
213
214 ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
215 ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
216 ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
217 ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
218 ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
219 ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
220 ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
221 ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
222 ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
223 ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
224
225
226 host->private_data = info;
227
228
229 return ata_host_activate(host, info->irq, ata_sff_interrupt,
230 IRQF_SHARED | IRQF_TRIGGER_RISING,
231 &at32_sht);
232}
233
234
235
236
237
238#ifdef DEBUG_BUS
239
240static void __init pata_at32_debug_bus(struct device *dev,
241 struct at32_ide_info *info)
242{
243 const int d1 = 0xff;
244 const int d2 = 0x00;
245
246 int i;
247
248
249 iowrite8(d1, info->alt_addr + (0x06 << 1));
250 iowrite8(d2, info->alt_addr + (0x06 << 1));
251
252 for (i = 0; i < 8; i++) {
253 iowrite8(d1, info->ide_addr + (i << 1));
254 iowrite8(d2, info->ide_addr + (i << 1));
255 }
256
257
258 iowrite16(d1, info->ide_addr);
259 iowrite16(d1 << 8, info->ide_addr);
260
261 iowrite16(d1, info->ide_addr);
262 iowrite16(d1 << 8, info->ide_addr);
263}
264
265#endif
266
267static int __init pata_at32_probe(struct platform_device *pdev)
268{
269 const struct ata_timing initial_timing =
270 {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
271
272 struct device *dev = &pdev->dev;
273 struct at32_ide_info *info;
274 struct ide_platform_data *board = dev_get_platdata(&pdev->dev);
275 struct resource *res;
276
277 int irq;
278 int ret;
279
280 if (!board)
281 return -ENXIO;
282
283 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
284 if (!res)
285 return -ENXIO;
286
287
288 irq = platform_get_irq(pdev, 0);
289 if (irq < 0)
290 return irq;
291
292
293 info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
294 if (!info)
295 return -ENOMEM;
296
297 info->irq = irq;
298 info->cs = board->cs;
299
300
301 info->res_ide.start = res->start + CF_IDE_OFFSET;
302 info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
303 info->res_ide.name = "ide";
304 info->res_ide.flags = IORESOURCE_MEM;
305
306 ret = request_resource(res, &info->res_ide);
307 if (ret)
308 goto err_req_res_ide;
309
310 info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
311 info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
312 info->res_alt.name = "alt";
313 info->res_alt.flags = IORESOURCE_MEM;
314
315 ret = request_resource(res, &info->res_alt);
316 if (ret)
317 goto err_req_res_alt;
318
319
320 info->smc.bus_width = 2;
321 info->smc.nrd_controlled = 1;
322 info->smc.nwe_controlled = 0;
323 info->smc.nwait_mode = 3;
324 info->smc.byte_write = 0;
325 info->smc.tdf_mode = 0;
326 info->smc.tdf_cycles = 0;
327
328
329 ret = pata_at32_setup_timing(dev, info, &initial_timing);
330 if (ret)
331 goto err_setup_timing;
332
333
334 ret = -ENOMEM;
335 info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
336 info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
337 if (!info->ide_addr || !info->alt_addr)
338 goto err_ioremap;
339
340#ifdef DEBUG_BUS
341 pata_at32_debug_bus(dev, info);
342#endif
343
344
345 ret = pata_at32_init_one(dev, info);
346 if (ret)
347 goto err_ata_device;
348
349 return 0;
350
351 err_ata_device:
352 err_ioremap:
353 err_setup_timing:
354 release_resource(&info->res_alt);
355 err_req_res_alt:
356 release_resource(&info->res_ide);
357 err_req_res_ide:
358 kfree(info);
359
360 return ret;
361}
362
363static int __exit pata_at32_remove(struct platform_device *pdev)
364{
365 struct ata_host *host = platform_get_drvdata(pdev);
366 struct at32_ide_info *info;
367
368 if (!host)
369 return 0;
370
371 info = host->private_data;
372 ata_host_detach(host);
373
374 if (!info)
375 return 0;
376
377 release_resource(&info->res_ide);
378 release_resource(&info->res_alt);
379
380 kfree(info);
381
382 return 0;
383}
384
385
386MODULE_ALIAS("platform:at32_ide");
387
388static struct platform_driver pata_at32_driver = {
389 .remove = __exit_p(pata_at32_remove),
390 .driver = {
391 .name = "at32_ide",
392 },
393};
394
395module_platform_driver_probe(pata_at32_driver, pata_at32_probe);
396
397MODULE_LICENSE("GPL");
398MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
399MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
400MODULE_VERSION(DRV_VERSION);
401