blob: 9118587a0db8a15824966ccd9040d1794acc3c85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1996-2004 Russell King.
3 *
4 * Please note that this platform does not support 32-bit IDE IO.
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/string.h>
8#include <linux/module.h>
9#include <linux/ioport.h>
10#include <linux/slab.h>
11#include <linux/blkdev.h>
12#include <linux/errno.h>
13#include <linux/hdreg.h>
14#include <linux/ide.h>
15#include <linux/dma-mapping.h>
16#include <linux/device.h>
17#include <linux/init.h>
18#include <linux/scatterlist.h>
Al Viroba5b55d2007-07-15 21:01:32 +010019#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/dma.h>
22#include <asm/ecard.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ICS_IDENT_OFFSET 0x2280
25
26#define ICS_ARCIN_V5_INTRSTAT 0x0000
27#define ICS_ARCIN_V5_INTROFFSET 0x0004
28#define ICS_ARCIN_V5_IDEOFFSET 0x2800
29#define ICS_ARCIN_V5_IDEALTOFFSET 0x2b80
30#define ICS_ARCIN_V5_IDESTEPPING 6
31
32#define ICS_ARCIN_V6_IDEOFFSET_1 0x2000
33#define ICS_ARCIN_V6_INTROFFSET_1 0x2200
34#define ICS_ARCIN_V6_INTRSTAT_1 0x2290
35#define ICS_ARCIN_V6_IDEALTOFFSET_1 0x2380
36#define ICS_ARCIN_V6_IDEOFFSET_2 0x3000
37#define ICS_ARCIN_V6_INTROFFSET_2 0x3200
38#define ICS_ARCIN_V6_INTRSTAT_2 0x3290
39#define ICS_ARCIN_V6_IDEALTOFFSET_2 0x3380
40#define ICS_ARCIN_V6_IDESTEPPING 6
41
42struct cardinfo {
43 unsigned int dataoffset;
44 unsigned int ctrloffset;
45 unsigned int stepping;
46};
47
48static struct cardinfo icside_cardinfo_v5 = {
49 .dataoffset = ICS_ARCIN_V5_IDEOFFSET,
50 .ctrloffset = ICS_ARCIN_V5_IDEALTOFFSET,
51 .stepping = ICS_ARCIN_V5_IDESTEPPING,
52};
53
54static struct cardinfo icside_cardinfo_v6_1 = {
55 .dataoffset = ICS_ARCIN_V6_IDEOFFSET_1,
56 .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_1,
57 .stepping = ICS_ARCIN_V6_IDESTEPPING,
58};
59
60static struct cardinfo icside_cardinfo_v6_2 = {
61 .dataoffset = ICS_ARCIN_V6_IDEOFFSET_2,
62 .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_2,
63 .stepping = ICS_ARCIN_V6_IDESTEPPING,
64};
65
66struct icside_state {
67 unsigned int channel;
68 unsigned int enabled;
69 void __iomem *irq_port;
70 void __iomem *ioc_base;
71 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 ide_hwif_t *hwif[2];
73};
74
75#define ICS_TYPE_A3IN 0
76#define ICS_TYPE_A3USER 1
77#define ICS_TYPE_V6 3
78#define ICS_TYPE_V5 15
79#define ICS_TYPE_NOTYPE ((unsigned int)-1)
80
81/* ---------------- Version 5 PCB Support Functions --------------------- */
82/* Prototype: icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
83 * Purpose : enable interrupts from card
84 */
85static void icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
86{
87 struct icside_state *state = ec->irq_data;
88
89 writeb(0, state->irq_port + ICS_ARCIN_V5_INTROFFSET);
90}
91
92/* Prototype: icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
93 * Purpose : disable interrupts from card
94 */
95static void icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
96{
97 struct icside_state *state = ec->irq_data;
98
99 readb(state->irq_port + ICS_ARCIN_V5_INTROFFSET);
100}
101
102static const expansioncard_ops_t icside_ops_arcin_v5 = {
103 .irqenable = icside_irqenable_arcin_v5,
104 .irqdisable = icside_irqdisable_arcin_v5,
105};
106
107
108/* ---------------- Version 6 PCB Support Functions --------------------- */
109/* Prototype: icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
110 * Purpose : enable interrupts from card
111 */
112static void icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
113{
114 struct icside_state *state = ec->irq_data;
115 void __iomem *base = state->irq_port;
116
117 state->enabled = 1;
118
119 switch (state->channel) {
120 case 0:
121 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
122 readb(base + ICS_ARCIN_V6_INTROFFSET_2);
123 break;
124 case 1:
125 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
126 readb(base + ICS_ARCIN_V6_INTROFFSET_1);
127 break;
128 }
129}
130
131/* Prototype: icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
132 * Purpose : disable interrupts from card
133 */
134static void icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
135{
136 struct icside_state *state = ec->irq_data;
137
138 state->enabled = 0;
139
140 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
141 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
142}
143
144/* Prototype: icside_irqprobe(struct expansion_card *ec)
145 * Purpose : detect an active interrupt from card
146 */
147static int icside_irqpending_arcin_v6(struct expansion_card *ec)
148{
149 struct icside_state *state = ec->irq_data;
150
151 return readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
152 readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
153}
154
155static const expansioncard_ops_t icside_ops_arcin_v6 = {
156 .irqenable = icside_irqenable_arcin_v6,
157 .irqdisable = icside_irqdisable_arcin_v6,
158 .irqpending = icside_irqpending_arcin_v6,
159};
160
161/*
162 * Handle routing of interrupts. This is called before
163 * we write the command to the drive.
164 */
165static void icside_maskproc(ide_drive_t *drive, int mask)
166{
167 ide_hwif_t *hwif = HWIF(drive);
168 struct icside_state *state = hwif->hwif_data;
169 unsigned long flags;
170
171 local_irq_save(flags);
172
173 state->channel = hwif->channel;
174
175 if (state->enabled && !mask) {
176 switch (hwif->channel) {
177 case 0:
178 writeb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
179 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
180 break;
181 case 1:
182 writeb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
183 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
184 break;
185 }
186 } else {
187 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
188 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
189 }
190
191 local_irq_restore(flags);
192}
193
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200194static const struct ide_port_ops icside_v6_no_dma_port_ops = {
195 .maskproc = icside_maskproc,
196};
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#ifdef CONFIG_BLK_DEV_IDEDMA_ICS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * SG-DMA support.
201 *
202 * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
203 * There is only one DMA controller per card, which means that only
204 * one drive can be accessed at one time. NOTE! We do not enforce that
205 * here, but we rely on the main IDE driver spotting that both
206 * interfaces use the same IRQ, which should guarantee this.
207 */
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Configure the IOMD to give the appropriate timings for the transfer
211 * mode being requested. We take the advice of the ATA standards, and
212 * calculate the cycle time based on the transfer mode, and the EIDE
213 * MW DMA specs that the drive provides in the IDENTIFY command.
214 *
215 * We have the following IOMD DMA modes to choose from:
216 *
217 * Type Active Recovery Cycle
218 * A 250 (250) 312 (550) 562 (800)
219 * B 187 250 437
220 * C 125 (125) 125 (375) 250 (500)
221 * D 62 125 187
222 *
223 * (figures in brackets are actual measured timings)
224 *
225 * However, we also need to take care of the read/write active and
226 * recovery timings:
227 *
228 * Read Write
229 * Mode Active -- Recovery -- Cycle IOMD type
230 * MW0 215 50 215 480 A
231 * MW1 80 50 50 150 C
232 * MW2 70 25 25 120 C
233 */
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200234static void icside_set_dma_mode(ide_drive_t *drive, const u8 xfer_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Bartlomiej Zolnierkiewiczf44ae582007-10-11 23:54:00 +0200236 int cycle_time, use_dma_info = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (xfer_mode) {
239 case XFER_MW_DMA_2:
240 cycle_time = 250;
241 use_dma_info = 1;
242 break;
243
244 case XFER_MW_DMA_1:
245 cycle_time = 250;
246 use_dma_info = 1;
247 break;
248
249 case XFER_MW_DMA_0:
250 cycle_time = 480;
251 break;
252
253 case XFER_SW_DMA_2:
254 case XFER_SW_DMA_1:
255 case XFER_SW_DMA_0:
256 cycle_time = 480;
257 break;
258 }
259
260 /*
261 * If we're going to be doing MW_DMA_1 or MW_DMA_2, we should
262 * take care to note the values in the ID...
263 */
264 if (use_dma_info && drive->id->eide_dma_time > cycle_time)
265 cycle_time = drive->id->eide_dma_time;
266
267 drive->drive_data = cycle_time;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk("%s: %s selected (peak %dMB/s)\n", drive->name,
270 ide_xfer_verbose(xfer_mode), 2000 / drive->drive_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200273static const struct ide_port_ops icside_v6_port_ops = {
274 .set_dma_mode = icside_set_dma_mode,
275 .maskproc = icside_maskproc,
276};
277
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100278static void icside_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static int icside_dma_end(ide_drive_t *drive)
283{
284 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100285 struct expansion_card *ec = ECARD_DEV(hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 drive->waiting_for_dma = 0;
288
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100289 disable_dma(ec->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Teardown mappings after DMA has completed. */
Bartlomiej Zolnierkiewicz062f9f02008-02-01 23:09:32 +0100292 ide_destroy_dmatable(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100294 return get_dma_residue(ec->dma) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static void icside_dma_start(ide_drive_t *drive)
298{
299 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100300 struct expansion_card *ec = ECARD_DEV(hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* We can not enable DMA on both channels simultaneously. */
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100303 BUG_ON(dma_channel_active(ec->dma));
304 enable_dma(ec->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static int icside_dma_setup(ide_drive_t *drive)
308{
309 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100310 struct expansion_card *ec = ECARD_DEV(hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct request *rq = hwif->hwgroup->rq;
312 unsigned int dma_mode;
313
314 if (rq_data_dir(rq))
315 dma_mode = DMA_MODE_WRITE;
316 else
317 dma_mode = DMA_MODE_READ;
318
319 /*
320 * We can not enable DMA on both channels.
321 */
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100322 BUG_ON(dma_channel_active(ec->dma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Bartlomiej Zolnierkiewicz062f9f02008-02-01 23:09:32 +0100324 hwif->sg_nents = ide_build_sglist(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /*
327 * Ensure that we have the right interrupt routed.
328 */
329 icside_maskproc(drive, 0);
330
331 /*
332 * Route the DMA signals to the correct interface.
333 */
334 writeb(hwif->select_data, hwif->config_data);
335
336 /*
337 * Select the correct timing for this drive.
338 */
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100339 set_dma_speed(ec->dma, drive->drive_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * Tell the DMA engine about the SG table and
343 * data direction.
344 */
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100345 set_dma_sg(ec->dma, hwif->sg_table, hwif->sg_nents);
346 set_dma_mode(ec->dma, dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 drive->waiting_for_dma = 1;
349
350 return 0;
351}
352
353static void icside_dma_exec_cmd(ide_drive_t *drive, u8 cmd)
354{
355 /* issue cmd to drive */
356 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD, NULL);
357}
358
359static int icside_dma_test_irq(ide_drive_t *drive)
360{
361 ide_hwif_t *hwif = HWIF(drive);
362 struct icside_state *state = hwif->hwif_data;
363
364 return readb(state->irq_port +
365 (hwif->channel ?
366 ICS_ARCIN_V6_INTRSTAT_2 :
367 ICS_ARCIN_V6_INTRSTAT_1)) & 1;
368}
369
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200370static void icside_dma_timeout(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 printk(KERN_ERR "%s: DMA timeout occurred: ", drive->name);
373
374 if (icside_dma_test_irq(drive))
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100377 ide_dump_status(drive, "DMA timeout", ide_read_status(drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200379 icside_dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Sergei Shtylyov841d2a92007-07-09 23:17:54 +0200382static void icside_dma_lost_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 printk(KERN_ERR "%s: IRQ lost\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200387static int icside_dma_init(ide_hwif_t *hwif, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 hwif->dmatable_cpu = NULL;
390 hwif->dmatable_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100392 hwif->dma_host_set = icside_dma_host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 hwif->dma_setup = icside_dma_setup;
394 hwif->dma_exec_cmd = icside_dma_exec_cmd;
395 hwif->dma_start = icside_dma_start;
396 hwif->ide_dma_end = icside_dma_end;
397 hwif->ide_dma_test_irq = icside_dma_test_irq;
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200398 hwif->dma_timeout = icside_dma_timeout;
Sergei Shtylyov841d2a92007-07-09 23:17:54 +0200399 hwif->dma_lost_irq = icside_dma_lost_irq;
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200400
401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif
404
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200405static int icside_dma_off_init(ide_hwif_t *hwif, const struct ide_port_info *d)
406{
407 return -EOPNOTSUPP;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static ide_hwif_t *
411icside_setup(void __iomem *base, struct cardinfo *info, struct expansion_card *ec)
412{
413 unsigned long port = (unsigned long)base + info->dataoffset;
414 ide_hwif_t *hwif;
415
Bartlomiej Zolnierkiewicz59bff5b2008-04-26 17:36:32 +0200416 hwif = ide_find_port();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (hwif) {
418 int i;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Ensure we're using MMIO
422 */
423 default_hwif_mmiops(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 hwif->io_ports[i] = port;
427 port += 1 << info->stepping;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 hwif->io_ports[IDE_CONTROL_OFFSET] = (unsigned long)base + info->ctrloffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 hwif->irq = ec->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 hwif->chipset = ide_acorn;
432 hwif->gendev.parent = &ec->dev;
Bartlomiej Zolnierkiewiczf8341c12008-02-01 23:09:32 +0100433 hwif->dev = &ec->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 return hwif;
437}
438
439static int __init
440icside_register_v5(struct icside_state *state, struct expansion_card *ec)
441{
442 ide_hwif_t *hwif;
443 void __iomem *base;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200444 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Russell King10bdaaa2007-05-10 18:40:51 +0100446 base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!base)
448 return -ENOMEM;
449
450 state->irq_port = base;
451
452 ec->irqaddr = base + ICS_ARCIN_V5_INTRSTAT;
453 ec->irqmask = 1;
Russell Kingc7b87f32007-05-10 16:46:13 +0100454
455 ecard_setirq(ec, &icside_ops_arcin_v5, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /*
458 * Be on the safe side - disable interrupts
459 */
460 icside_irqdisable_arcin_v5(ec, 0);
461
462 hwif = icside_setup(base, &icside_cardinfo_v5, ec);
Russell King10bdaaa2007-05-10 18:40:51 +0100463 if (!hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 state->hwif[0] = hwif;
467
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200468 idx[0] = hwif->index;
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200469
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100470 ide_device_add(idx, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return 0;
473}
474
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100475static const struct ide_port_info icside_v6_port_info __initdata = {
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200476 .init_dma = icside_dma_off_init,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200477 .port_ops = &icside_v6_no_dma_port_ops,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100478 .host_flags = IDE_HFLAG_SERIALIZE |
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100479 IDE_HFLAG_NO_AUTOTUNE,
480 .mwdma_mask = ATA_MWDMA2,
481 .swdma_mask = ATA_SWDMA2,
482};
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static int __init
485icside_register_v6(struct icside_state *state, struct expansion_card *ec)
486{
487 ide_hwif_t *hwif, *mate;
488 void __iomem *ioc_base, *easi_base;
489 unsigned int sel = 0;
490 int ret;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200491 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100492 struct ide_port_info d = icside_v6_port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Russell King10bdaaa2007-05-10 18:40:51 +0100494 ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!ioc_base) {
496 ret = -ENOMEM;
497 goto out;
498 }
499
500 easi_base = ioc_base;
501
502 if (ecard_resource_flags(ec, ECARD_RES_EASI)) {
Russell King10bdaaa2007-05-10 18:40:51 +0100503 easi_base = ecardm_iomap(ec, ECARD_RES_EASI, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!easi_base) {
505 ret = -ENOMEM;
Russell King10bdaaa2007-05-10 18:40:51 +0100506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 /*
510 * Enable access to the EASI region.
511 */
512 sel = 1 << 5;
513 }
514
515 writeb(sel, ioc_base);
516
Russell Kingc7b87f32007-05-10 16:46:13 +0100517 ecard_setirq(ec, &icside_ops_arcin_v6, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 state->irq_port = easi_base;
520 state->ioc_base = ioc_base;
521
522 /*
523 * Be on the safe side - disable interrupts
524 */
525 icside_irqdisable_arcin_v6(ec, 0);
526
527 /*
528 * Find and register the interfaces.
529 */
530 hwif = icside_setup(easi_base, &icside_cardinfo_v6_1, ec);
531 mate = icside_setup(easi_base, &icside_cardinfo_v6_2, ec);
532
533 if (!hwif || !mate) {
534 ret = -ENODEV;
Russell King10bdaaa2007-05-10 18:40:51 +0100535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 state->hwif[0] = hwif;
539 state->hwif[1] = mate;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 hwif->hwif_data = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 hwif->config_data = (unsigned long)ioc_base;
543 hwif->select_data = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 mate->maskproc = icside_maskproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 mate->hwif_data = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 mate->config_data = (unsigned long)ioc_base;
548 mate->select_data = sel | 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (ec->dma != NO_DMA && !request_dma(ec->dma, hwif->name)) {
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200551 d.init_dma = icside_dma_init;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200552 d.port_ops = &icside_v6_dma_port_ops;
Bartlomiej Zolnierkiewicz91432f42008-04-26 22:25:23 +0200553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200555 idx[0] = hwif->index;
556 idx[1] = mate->index;
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200557
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100558 ide_device_add(idx, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return 0;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 out:
563 return ret;
564}
565
566static int __devinit
567icside_probe(struct expansion_card *ec, const struct ecard_id *id)
568{
569 struct icside_state *state;
570 void __iomem *idmem;
571 int ret;
572
573 ret = ecard_request_resources(ec);
574 if (ret)
575 goto out;
576
Mariusz Kozlowskicc60d8b2007-08-01 23:46:44 +0200577 state = kzalloc(sizeof(struct icside_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!state) {
579 ret = -ENOMEM;
580 goto release;
581 }
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 state->type = ICS_TYPE_NOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Russell King10bdaaa2007-05-10 18:40:51 +0100585 idmem = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (idmem) {
587 unsigned int type;
588
589 type = readb(idmem + ICS_IDENT_OFFSET) & 1;
590 type |= (readb(idmem + ICS_IDENT_OFFSET + 4) & 1) << 1;
591 type |= (readb(idmem + ICS_IDENT_OFFSET + 8) & 1) << 2;
592 type |= (readb(idmem + ICS_IDENT_OFFSET + 12) & 1) << 3;
Russell King10bdaaa2007-05-10 18:40:51 +0100593 ecardm_iounmap(ec, idmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 state->type = type;
596 }
597
598 switch (state->type) {
599 case ICS_TYPE_A3IN:
600 dev_warn(&ec->dev, "A3IN unsupported\n");
601 ret = -ENODEV;
602 break;
603
604 case ICS_TYPE_A3USER:
605 dev_warn(&ec->dev, "A3USER unsupported\n");
606 ret = -ENODEV;
607 break;
608
609 case ICS_TYPE_V5:
610 ret = icside_register_v5(state, ec);
611 break;
612
613 case ICS_TYPE_V6:
614 ret = icside_register_v6(state, ec);
615 break;
616
617 default:
618 dev_warn(&ec->dev, "unknown interface type\n");
619 ret = -ENODEV;
620 break;
621 }
622
623 if (ret == 0) {
624 ecard_set_drvdata(ec, state);
625 goto out;
626 }
627
628 kfree(state);
629 release:
630 ecard_release_resources(ec);
631 out:
632 return ret;
633}
634
635static void __devexit icside_remove(struct expansion_card *ec)
636{
637 struct icside_state *state = ecard_get_drvdata(ec);
638
639 switch (state->type) {
640 case ICS_TYPE_V5:
641 /* FIXME: tell IDE to stop using the interface */
642
643 /* Disable interrupts */
644 icside_irqdisable_arcin_v5(ec, 0);
645 break;
646
647 case ICS_TYPE_V6:
648 /* FIXME: tell IDE to stop using the interface */
649 if (ec->dma != NO_DMA)
650 free_dma(ec->dma);
651
652 /* Disable interrupts */
653 icside_irqdisable_arcin_v6(ec, 0);
654
655 /* Reset the ROM pointer/EASI selection */
656 writeb(0, state->ioc_base);
657 break;
658 }
659
660 ecard_set_drvdata(ec, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kfree(state);
663 ecard_release_resources(ec);
664}
665
666static void icside_shutdown(struct expansion_card *ec)
667{
668 struct icside_state *state = ecard_get_drvdata(ec);
669 unsigned long flags;
670
671 /*
672 * Disable interrupts from this card. We need to do
673 * this before disabling EASI since we may be accessing
674 * this register via that region.
675 */
676 local_irq_save(flags);
677 ec->ops->irqdisable(ec, 0);
678 local_irq_restore(flags);
679
680 /*
681 * Reset the ROM pointer so that we can read the ROM
682 * after a soft reboot. This also disables access to
683 * the IDE taskfile via the EASI region.
684 */
685 if (state->ioc_base)
686 writeb(0, state->ioc_base);
687}
688
689static const struct ecard_id icside_ids[] = {
690 { MANU_ICS, PROD_ICS_IDE },
691 { MANU_ICS2, PROD_ICS2_IDE },
692 { 0xffff, 0xffff }
693};
694
695static struct ecard_driver icside_driver = {
696 .probe = icside_probe,
697 .remove = __devexit_p(icside_remove),
698 .shutdown = icside_shutdown,
699 .id_table = icside_ids,
700 .drv = {
701 .name = "icside",
702 },
703};
704
705static int __init icside_init(void)
706{
707 return ecard_register_driver(&icside_driver);
708}
709
710MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
711MODULE_LICENSE("GPL");
712MODULE_DESCRIPTION("ICS IDE driver");
713
714module_init(icside_init);