Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / ide / cris / ide-cris.c
1 /* $Id: cris-ide-driver.patch,v 1.1 2005/06/29 21:39:07 akpm Exp $
2  *
3  * Etrax specific IDE functions, like init and PIO-mode setting etc.
4  * Almost the entire ide.c is used for the rest of the Etrax ATA driver.
5  * Copyright (c) 2000-2005 Axis Communications AB
6  *
7  * Authors:    Bjorn Wesen        (initial version)
8  *             Mikael Starvik     (crisv32 port)
9  */
10
11 /* Regarding DMA:
12  *
13  * There are two forms of DMA - "DMA handshaking" between the interface and the drive,
14  * and DMA between the memory and the interface. We can ALWAYS use the latter, since it's
15  * something built-in in the Etrax. However only some drives support the DMA-mode handshaking
16  * on the ATA-bus. The normal PC driver and Triton interface disables memory-if DMA when the
17  * device can't do DMA handshaking for some stupid reason. We don't need to do that.
18  */
19
20 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
21
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/mm.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/blkdev.h>
29 #include <linux/hdreg.h>
30 #include <linux/ide.h>
31 #include <linux/init.h>
32
33 #include <asm/io.h>
34 #include <asm/dma.h>
35
36 /* number of DMA descriptors */
37 #define MAX_DMA_DESCRS 64
38
39 /* number of times to retry busy-flags when reading/writing IDE-registers
40  * this can't be too high because a hung harddisk might cause the watchdog
41  * to trigger (sometimes INB and OUTB are called with irq's disabled)
42  */
43
44 #define IDE_REGISTER_TIMEOUT 300
45
46 #define LOWDB(x)
47 #define D(x)
48
49 enum /* Transfer types */
50 {
51         TYPE_PIO,
52         TYPE_DMA,
53         TYPE_UDMA
54 };
55
56 /* CRISv32 specifics */
57 #ifdef CONFIG_ETRAX_ARCH_V32
58 #include <asm/arch/hwregs/ata_defs.h>
59 #include <asm/arch/hwregs/dma_defs.h>
60 #include <asm/arch/hwregs/dma.h>
61 #include <asm/arch/pinmux.h>
62
63 #define ATA_UDMA2_CYC    2
64 #define ATA_UDMA2_DVS    3
65 #define ATA_UDMA1_CYC    2
66 #define ATA_UDMA1_DVS    4
67 #define ATA_UDMA0_CYC    4
68 #define ATA_UDMA0_DVS    6
69 #define ATA_DMA2_STROBE  7
70 #define ATA_DMA2_HOLD    1
71 #define ATA_DMA1_STROBE  8
72 #define ATA_DMA1_HOLD    3
73 #define ATA_DMA0_STROBE 25
74 #define ATA_DMA0_HOLD   19
75 #define ATA_PIO4_SETUP   3
76 #define ATA_PIO4_STROBE  7
77 #define ATA_PIO4_HOLD    1
78 #define ATA_PIO3_SETUP   3
79 #define ATA_PIO3_STROBE  9
80 #define ATA_PIO3_HOLD    3
81 #define ATA_PIO2_SETUP   3
82 #define ATA_PIO2_STROBE 13
83 #define ATA_PIO2_HOLD    5
84 #define ATA_PIO1_SETUP   5
85 #define ATA_PIO1_STROBE 23
86 #define ATA_PIO1_HOLD    9
87 #define ATA_PIO0_SETUP   9
88 #define ATA_PIO0_STROBE 39
89 #define ATA_PIO0_HOLD    9
90
91 int
92 cris_ide_ack_intr(ide_hwif_t* hwif)
93 {
94         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2,
95                                  int, hwif->io_ports[0]);
96         REG_WR_INT(ata, regi_ata, rw_ack_intr, 1 << ctrl2.sel);
97         return 1;
98 }
99
100 static inline int
101 cris_ide_busy(void)
102 {
103         reg_ata_rs_stat_data stat_data;
104         stat_data = REG_RD(ata, regi_ata, rs_stat_data);
105         return stat_data.busy;
106 }
107
108 static inline int
109 cris_ide_ready(void)
110 {
111         return !cris_ide_busy();
112 }
113
114 static inline int
115 cris_ide_data_available(unsigned short* data)
116 {
117         reg_ata_rs_stat_data stat_data;
118         stat_data = REG_RD(ata, regi_ata, rs_stat_data);
119         *data = stat_data.data;
120         return stat_data.dav;
121 }
122
123 static void
124 cris_ide_write_command(unsigned long command)
125 {
126         REG_WR_INT(ata, regi_ata, rw_ctrl2, command); /* write data to the drive's register */
127 }
128
129 static void
130 cris_ide_set_speed(int type, int setup, int strobe, int hold)
131 {
132         reg_ata_rw_ctrl0 ctrl0 = REG_RD(ata, regi_ata, rw_ctrl0);
133         reg_ata_rw_ctrl1 ctrl1 = REG_RD(ata, regi_ata, rw_ctrl1);
134
135         if (type == TYPE_PIO) {
136                 ctrl0.pio_setup = setup;
137                 ctrl0.pio_strb = strobe;
138                 ctrl0.pio_hold = hold;
139         } else if (type == TYPE_DMA) {
140                 ctrl0.dma_strb = strobe;
141                 ctrl0.dma_hold = hold;
142         } else if (type == TYPE_UDMA) {
143                 ctrl1.udma_tcyc = setup;
144                 ctrl1.udma_tdvs = strobe;
145         }
146         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
147         REG_WR(ata, regi_ata, rw_ctrl1, ctrl1);
148 }
149
150 static unsigned long
151 cris_ide_base_address(int bus)
152 {
153         reg_ata_rw_ctrl2 ctrl2 = {0};
154         ctrl2.sel = bus;
155         return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
156 }
157
158 static unsigned long
159 cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
160 {
161         reg_ata_rw_ctrl2 ctrl2 = {0};
162         ctrl2.addr = addr;
163         ctrl2.cs1 = cs1;
164         ctrl2.cs0 = cs0;
165         return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
166 }
167
168 static __init void
169 cris_ide_reset(unsigned val)
170 {
171         reg_ata_rw_ctrl0 ctrl0 = {0};
172         ctrl0.rst = val ? regk_ata_active : regk_ata_inactive;
173         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
174 }
175
176 static __init void
177 cris_ide_init(void)
178 {
179         reg_ata_rw_ctrl0 ctrl0 = {0};
180         reg_ata_rw_intr_mask intr_mask = {0};
181
182         ctrl0.en = regk_ata_yes;
183         REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
184
185         intr_mask.bus0 = regk_ata_yes;
186         intr_mask.bus1 = regk_ata_yes;
187         intr_mask.bus2 = regk_ata_yes;
188         intr_mask.bus3 = regk_ata_yes;
189
190         REG_WR(ata, regi_ata, rw_intr_mask, intr_mask);
191
192         crisv32_request_dma(2, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
193         crisv32_request_dma(3, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
194
195         crisv32_pinmux_alloc_fixed(pinmux_ata);
196         crisv32_pinmux_alloc_fixed(pinmux_ata0);
197         crisv32_pinmux_alloc_fixed(pinmux_ata1);
198         crisv32_pinmux_alloc_fixed(pinmux_ata2);
199         crisv32_pinmux_alloc_fixed(pinmux_ata3);
200
201         DMA_RESET(regi_dma2);
202         DMA_ENABLE(regi_dma2);
203         DMA_RESET(regi_dma3);
204         DMA_ENABLE(regi_dma3);
205
206         DMA_WR_CMD (regi_dma2, regk_dma_set_w_size2);
207         DMA_WR_CMD (regi_dma3, regk_dma_set_w_size2);
208 }
209
210 static dma_descr_context mycontext __attribute__ ((__aligned__(32)));
211
212 #define cris_dma_descr_type dma_descr_data
213 #define cris_pio_read regk_ata_rd
214 #define cris_ultra_mask 0x7
215 #define MAX_DESCR_SIZE 0xffffffffUL
216
217 static unsigned long
218 cris_ide_get_reg(unsigned long reg)
219 {
220         return (reg & 0x0e000000) >> 25;
221 }
222
223 static void
224 cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
225 {
226         d->buf = (char*)virt_to_phys(buf);
227         d->after = d->buf + len;
228         d->eol = last;
229 }
230
231 static void
232 cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir,int type,int len)
233 {
234         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
235         reg_ata_rw_trf_cnt trf_cnt = {0};
236
237         mycontext.saved_data = (dma_descr_data*)virt_to_phys(d);
238         mycontext.saved_data_buf = d->buf;
239         /* start the dma channel */
240         DMA_START_CONTEXT(dir ? regi_dma3 : regi_dma2, virt_to_phys(&mycontext));
241
242         /* initiate a multi word dma read using PIO handshaking */
243         trf_cnt.cnt = len >> 1;
244         /* Due to a "feature" the transfer count has to be one extra word for UDMA. */
245         if (type == TYPE_UDMA)
246                 trf_cnt.cnt++;
247         REG_WR(ata, regi_ata, rw_trf_cnt, trf_cnt);
248
249         ctrl2.rw = dir ? regk_ata_rd : regk_ata_wr;
250         ctrl2.trf_mode = regk_ata_dma;
251         ctrl2.hsh = type == TYPE_PIO ? regk_ata_pio :
252                     type == TYPE_DMA ? regk_ata_dma : regk_ata_udma;
253         ctrl2.multi = regk_ata_yes;
254         ctrl2.dma_size = regk_ata_word;
255         REG_WR(ata, regi_ata, rw_ctrl2, ctrl2);
256 }
257
258 static void
259 cris_ide_wait_dma(int dir)
260 {
261         reg_dma_rw_stat status;
262         do
263         {
264                 status = REG_RD(dma, dir ? regi_dma3 : regi_dma2, rw_stat);
265         } while(status.list_state != regk_dma_data_at_eol);
266 }
267
268 static int cris_dma_test_irq(ide_drive_t *drive)
269 {
270         int intr = REG_RD_INT(ata, regi_ata, r_intr);
271         reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
272         return intr & (1 << ctrl2.sel) ? 1 : 0;
273 }
274
275 static void cris_ide_initialize_dma(int dir)
276 {
277 }
278
279 #else
280 /* CRISv10 specifics */
281 #include <asm/arch/svinto.h>
282 #include <asm/arch/io_interface_mux.h>
283
284 /* PIO timing (in R_ATA_CONFIG)
285  *
286  *                        _____________________________
287  * ADDRESS :     ________/
288  *
289  *                            _______________
290  * DIOR    :     ____________/               \__________
291  *
292  *                               _______________
293  * DATA    :     XXXXXXXXXXXXXXXX_______________XXXXXXXX
294  *
295  *
296  * DIOR is unbuffered while address and data is buffered.
297  * This creates two problems:
298  * 1. The DIOR pulse is to early (because it is unbuffered)
299  * 2. The rise time of DIOR is long
300  *
301  * There are at least three different plausible solutions
302  * 1. Use a pad capable of larger currents in Etrax
303  * 2. Use an external buffer
304  * 3. Make the strobe pulse longer
305  *
306  * Some of the strobe timings below are modified to compensate
307  * for this. This implies a slight performance decrease.
308  *
309  * THIS SHOULD NEVER BE CHANGED!
310  *
311  * TODO: Is this true for the latest LX boards still ?
312  */
313
314 #define ATA_UDMA2_CYC    0 /* No UDMA supported, just to make it compile. */
315 #define ATA_UDMA2_DVS    0
316 #define ATA_UDMA1_CYC    0
317 #define ATA_UDMA1_DVS    0
318 #define ATA_UDMA0_CYC    0
319 #define ATA_UDMA0_DVS    0
320 #define ATA_DMA2_STROBE  4
321 #define ATA_DMA2_HOLD    0
322 #define ATA_DMA1_STROBE  4
323 #define ATA_DMA1_HOLD    1
324 #define ATA_DMA0_STROBE 12
325 #define ATA_DMA0_HOLD    9
326 #define ATA_PIO4_SETUP   1
327 #define ATA_PIO4_STROBE  5
328 #define ATA_PIO4_HOLD    0
329 #define ATA_PIO3_SETUP   1
330 #define ATA_PIO3_STROBE  5
331 #define ATA_PIO3_HOLD    1
332 #define ATA_PIO2_SETUP   1
333 #define ATA_PIO2_STROBE  6
334 #define ATA_PIO2_HOLD    2
335 #define ATA_PIO1_SETUP   2
336 #define ATA_PIO1_STROBE 11
337 #define ATA_PIO1_HOLD    4
338 #define ATA_PIO0_SETUP   4
339 #define ATA_PIO0_STROBE 19
340 #define ATA_PIO0_HOLD    4
341
342 int
343 cris_ide_ack_intr(ide_hwif_t* hwif)
344 {
345         return 1;
346 }
347
348 static inline int
349 cris_ide_busy(void)
350 {
351         return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy) ;
352 }
353
354 static inline int
355 cris_ide_ready(void)
356 {
357         return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy) ;
358 }
359
360 static inline int
361 cris_ide_data_available(unsigned short* data)
362 {
363         unsigned long status = *R_ATA_STATUS_DATA;
364         *data = (unsigned short)status;
365         return status & IO_MASK(R_ATA_STATUS_DATA, dav);
366 }
367
368 static void
369 cris_ide_write_command(unsigned long command)
370 {
371         *R_ATA_CTRL_DATA = command;
372 }
373
374 static void
375 cris_ide_set_speed(int type, int setup, int strobe, int hold)
376 {
377         static int pio_setup = ATA_PIO4_SETUP;
378         static int pio_strobe = ATA_PIO4_STROBE;
379         static int pio_hold = ATA_PIO4_HOLD;
380         static int dma_strobe = ATA_DMA2_STROBE;
381         static int dma_hold = ATA_DMA2_HOLD;
382
383         if (type == TYPE_PIO) {
384                 pio_setup = setup;
385                 pio_strobe = strobe;
386                 pio_hold = hold;
387         } else if (type == TYPE_DMA) {
388                 dma_strobe = strobe;
389           dma_hold = hold;
390         }
391         *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
392           IO_FIELD( R_ATA_CONFIG, dma_strobe, dma_strobe ) |
393                 IO_FIELD( R_ATA_CONFIG, dma_hold,   dma_hold ) |
394                 IO_FIELD( R_ATA_CONFIG, pio_setup,  pio_setup ) |
395                 IO_FIELD( R_ATA_CONFIG, pio_strobe, pio_strobe ) |
396                 IO_FIELD( R_ATA_CONFIG, pio_hold,   pio_hold ) );
397 }
398
399 static unsigned long
400 cris_ide_base_address(int bus)
401 {
402         return IO_FIELD(R_ATA_CTRL_DATA, sel, bus);
403 }
404
405 static unsigned long
406 cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
407 {
408         return IO_FIELD(R_ATA_CTRL_DATA, addr, addr) |
409                IO_FIELD(R_ATA_CTRL_DATA, cs0, cs0) |
410                IO_FIELD(R_ATA_CTRL_DATA, cs1, cs1);
411 }
412
413 static __init void
414 cris_ide_reset(unsigned val)
415 {
416 #ifdef CONFIG_ETRAX_IDE_G27_RESET
417         REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, val);
418 #endif
419 #ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET
420         REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, val);
421 #endif
422 #ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET
423         REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, val);
424 #endif
425 #ifdef CONFIG_ETRAX_IDE_PB7_RESET
426         port_pb_dir_shadow = port_pb_dir_shadow |
427                 IO_STATE(R_PORT_PB_DIR, dir7, output);
428         *R_PORT_PB_DIR = port_pb_dir_shadow;
429         REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, val);
430 #endif
431 }
432
433 static __init void
434 cris_ide_init(void)
435 {
436         volatile unsigned int dummy;
437
438         *R_ATA_CTRL_DATA = 0;
439         *R_ATA_TRANSFER_CNT = 0;
440         *R_ATA_CONFIG = 0;
441
442         if (cris_request_io_interface(if_ata, "ETRAX100LX IDE")) {
443                 printk(KERN_CRIT "ide: Failed to get IO interface\n");
444                 return;
445         } else if (cris_request_dma(ATA_TX_DMA_NBR,
446                                           "ETRAX100LX IDE TX",
447                                           DMA_VERBOSE_ON_ERROR,
448                                           dma_ata)) {
449                 cris_free_io_interface(if_ata);
450                 printk(KERN_CRIT "ide: Failed to get Tx DMA channel\n");
451                 return;
452         } else if (cris_request_dma(ATA_RX_DMA_NBR,
453                                           "ETRAX100LX IDE RX",
454                                           DMA_VERBOSE_ON_ERROR,
455                                           dma_ata)) {
456                 cris_free_dma(ATA_TX_DMA_NBR, "ETRAX100LX IDE Tx");
457                 cris_free_io_interface(if_ata);
458                 printk(KERN_CRIT "ide: Failed to get Rx DMA channel\n");
459                 return;
460         }
461
462         /* make a dummy read to set the ata controller in a proper state */
463         dummy = *R_ATA_STATUS_DATA;
464
465         *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ));
466         *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw,   read) |
467                              IO_FIELD( R_ATA_CTRL_DATA, addr, 1   ) );
468
469         while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
470
471         *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
472                              IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
473                              IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
474                              IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
475
476         /* reset the dma channels we will use */
477
478         RESET_DMA(ATA_TX_DMA_NBR);
479         RESET_DMA(ATA_RX_DMA_NBR);
480         WAIT_DMA(ATA_TX_DMA_NBR);
481         WAIT_DMA(ATA_RX_DMA_NBR);
482 }
483
484 #define cris_dma_descr_type etrax_dma_descr
485 #define cris_pio_read IO_STATE(R_ATA_CTRL_DATA, rw, read)
486 #define cris_ultra_mask 0x0
487 #define MAX_DESCR_SIZE 0x10000UL
488
489 static unsigned long
490 cris_ide_get_reg(unsigned long reg)
491 {
492         return (reg & 0x0e000000) >> 25;
493 }
494
495 static void
496 cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
497 {
498         d->buf = virt_to_phys(buf);
499         d->sw_len = len == MAX_DESCR_SIZE ? 0 : len;
500         if (last)
501                 d->ctrl |= d_eol;
502 }
503
504 static void cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir, int type, int len)
505 {
506         unsigned long cmd;
507
508         if (dir) {
509                 /* need to do this before RX DMA due to a chip bug
510                  * it is enough to just flush the part of the cache that
511                  * corresponds to the buffers we start, but since HD transfers
512                  * usually are more than 8 kB, it is easier to optimize for the
513                  * normal case and just flush the entire cache. its the only
514                  * way to be sure! (OB movie quote)
515                  */
516                 flush_etrax_cache();
517                 *R_DMA_CH3_FIRST = virt_to_phys(d);
518                 *R_DMA_CH3_CMD   = IO_STATE(R_DMA_CH3_CMD, cmd, start);
519
520         } else {
521                 *R_DMA_CH2_FIRST = virt_to_phys(d);
522                 *R_DMA_CH2_CMD   = IO_STATE(R_DMA_CH2_CMD, cmd, start);
523         }
524
525         /* initiate a multi word dma read using DMA handshaking */
526
527         *R_ATA_TRANSFER_CNT =
528                 IO_FIELD(R_ATA_TRANSFER_CNT, count, len >> 1);
529
530         cmd = dir ? IO_STATE(R_ATA_CTRL_DATA, rw, read) : IO_STATE(R_ATA_CTRL_DATA, rw, write);
531         cmd |= type == TYPE_PIO ? IO_STATE(R_ATA_CTRL_DATA, handsh, pio) :
532                                   IO_STATE(R_ATA_CTRL_DATA, handsh, dma);
533         *R_ATA_CTRL_DATA =
534                 cmd |
535                 IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
536                 IO_STATE(R_ATA_CTRL_DATA, src_dst,  dma)  |
537                 IO_STATE(R_ATA_CTRL_DATA, multi,    on)   |
538                 IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
539 }
540
541 static void
542 cris_ide_wait_dma(int dir)
543 {
544         if (dir)
545                 WAIT_DMA(ATA_RX_DMA_NBR);
546         else
547                 WAIT_DMA(ATA_TX_DMA_NBR);
548 }
549
550 static int cris_dma_test_irq(ide_drive_t *drive)
551 {
552         int intr = *R_IRQ_MASK0_RD;
553         int bus = IO_EXTRACT(R_ATA_CTRL_DATA, sel, IDE_DATA_REG);
554         return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;
555 }
556
557
558 static void cris_ide_initialize_dma(int dir)
559 {
560         if (dir)
561         {
562                 RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
563                 WAIT_DMA(ATA_RX_DMA_NBR);
564         }
565         else
566         {
567                 RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
568                 WAIT_DMA(ATA_TX_DMA_NBR);
569         }
570 }
571
572 #endif
573
574 void
575 cris_ide_outw(unsigned short data, unsigned long reg) {
576         int timeleft;
577
578         LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
579
580         /* note the lack of handling any timeouts. we stop waiting, but we don't
581          * really notify anybody.
582          */
583
584         timeleft = IDE_REGISTER_TIMEOUT;
585         /* wait for busy flag */
586         do {
587                 timeleft--;
588         } while(timeleft && cris_ide_busy());
589
590         /*
591          * Fall through at a timeout, so the ongoing command will be
592          * aborted by the write below, which is expected to be a dummy
593          * command to the command register.  This happens when a faulty
594          * drive times out on a command.  See comment on timeout in
595          * INB.
596          */
597         if(!timeleft)
598                 printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
599
600         cris_ide_write_command(reg|data); /* write data to the drive's register */
601
602         timeleft = IDE_REGISTER_TIMEOUT;
603         /* wait for transmitter ready */
604         do {
605                 timeleft--;
606         } while(timeleft && !cris_ide_ready());
607 }
608
609 void
610 cris_ide_outb(unsigned char data, unsigned long reg)
611 {
612         cris_ide_outw(data, reg);
613 }
614
615 void
616 cris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
617 {
618         cris_ide_outw(addr, port);
619 }
620
621 unsigned short
622 cris_ide_inw(unsigned long reg) {
623         int timeleft;
624         unsigned short val;
625
626         timeleft = IDE_REGISTER_TIMEOUT;
627         /* wait for busy flag */
628         do {
629                 timeleft--;
630         } while(timeleft && cris_ide_busy());
631
632         if(!timeleft) {
633                 /*
634                  * If we're asked to read the status register, like for
635                  * example when a command does not complete for an
636                  * extended time, but the ATA interface is stuck in a
637                  * busy state at the *ETRAX* ATA interface level (as has
638                  * happened repeatedly with at least one bad disk), then
639                  * the best thing to do is to pretend that we read
640                  * "busy" in the status register, so the IDE driver will
641                  * time-out, abort the ongoing command and perform a
642                  * reset sequence.  Note that the subsequent OUT_BYTE
643                  * call will also timeout on busy, but as long as the
644                  * write is still performed, everything will be fine.
645                  */
646                 if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET)
647                         return BUSY_STAT;
648                 else
649                         /* For other rare cases we assume 0 is good enough.  */
650                         return 0;
651         }
652
653         cris_ide_write_command(reg | cris_pio_read);
654
655         timeleft = IDE_REGISTER_TIMEOUT;
656         /* wait for available */
657         do {
658                 timeleft--;
659         } while(timeleft && !cris_ide_data_available(&val));
660
661         if(!timeleft)
662                 return 0;
663
664         LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg));
665
666         return val;
667 }
668
669 unsigned char
670 cris_ide_inb(unsigned long reg)
671 {
672         return (unsigned char)cris_ide_inw(reg);
673 }
674
675 static int cris_dma_check (ide_drive_t *drive);
676 static int cris_dma_end (ide_drive_t *drive);
677 static int cris_dma_setup (ide_drive_t *drive);
678 static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);
679 static int cris_dma_test_irq(ide_drive_t *drive);
680 static void cris_dma_start(ide_drive_t *drive);
681 static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);
682 static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);
683 static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
684 static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
685 static int cris_dma_off (ide_drive_t *drive);
686 static int cris_dma_on (ide_drive_t *drive);
687
688 static void tune_cris_ide(ide_drive_t *drive, u8 pio)
689 {
690         int setup, strobe, hold;
691
692         switch(pio)
693         {
694                 case 0:
695                         setup = ATA_PIO0_SETUP;
696                         strobe = ATA_PIO0_STROBE;
697                         hold = ATA_PIO0_HOLD;
698                         break;
699                 case 1:
700                         setup = ATA_PIO1_SETUP;
701                         strobe = ATA_PIO1_STROBE;
702                         hold = ATA_PIO1_HOLD;
703                         break;
704                 case 2:
705                         setup = ATA_PIO2_SETUP;
706                         strobe = ATA_PIO2_STROBE;
707                         hold = ATA_PIO2_HOLD;
708                         break;
709                 case 3:
710                         setup = ATA_PIO3_SETUP;
711                         strobe = ATA_PIO3_STROBE;
712                         hold = ATA_PIO3_HOLD;
713                         break;
714                 case 4:
715                         setup = ATA_PIO4_SETUP;
716                         strobe = ATA_PIO4_STROBE;
717                         hold = ATA_PIO4_HOLD;
718                         break;
719                 default:
720                         return;
721         }
722
723         cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);
724 }
725
726 static int speed_cris_ide(ide_drive_t *drive, u8 speed)
727 {
728         int cyc = 0, dvs = 0, strobe = 0, hold = 0;
729
730         if (speed >= XFER_PIO_0 && speed <= XFER_PIO_4) {
731                 tune_cris_ide(drive, speed - XFER_PIO_0);
732                 return 0;
733         }
734
735         switch(speed)
736         {
737                 case XFER_UDMA_0:
738                         cyc = ATA_UDMA0_CYC;
739                         dvs = ATA_UDMA0_DVS;
740                         break;
741                 case XFER_UDMA_1:
742                         cyc = ATA_UDMA1_CYC;
743                         dvs = ATA_UDMA1_DVS;
744                         break;
745                 case XFER_UDMA_2:
746                         cyc = ATA_UDMA2_CYC;
747                         dvs = ATA_UDMA2_DVS;
748                         break;
749                 case XFER_MW_DMA_0:
750                         strobe = ATA_DMA0_STROBE;
751                         hold = ATA_DMA0_HOLD;
752                         break;
753                 case XFER_MW_DMA_1:
754                         strobe = ATA_DMA1_STROBE;
755                         hold = ATA_DMA1_HOLD;
756                         break;
757                 case XFER_MW_DMA_2:
758                         strobe = ATA_DMA2_STROBE;
759                         hold = ATA_DMA2_HOLD;
760                         break;
761                 default:
762                         return 0;
763         }
764
765         if (speed >= XFER_UDMA_0)
766                 cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0);
767         else
768                 cris_ide_set_speed(TYPE_DMA, 0, strobe, hold);
769
770         return 0;
771 }
772
773 void __init
774 init_e100_ide (void)
775 {
776         hw_regs_t hw;
777         int ide_offsets[IDE_NR_PORTS];
778         int h;
779         int i;
780
781         printk("ide: ETRAX FS built-in ATA DMA controller\n");
782
783         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
784                 ide_offsets[i] = cris_ide_reg_addr(i, 0, 1);
785
786         /* the IDE control register is at ATA address 6, with CS1 active instead of CS0 */
787         ide_offsets[IDE_CONTROL_OFFSET] = cris_ide_reg_addr(6, 1, 0);
788
789         /* first fill in some stuff in the ide_hwifs fields */
790
791         for(h = 0; h < MAX_HWIFS; h++) {
792                 ide_hwif_t *hwif = &ide_hwifs[h];
793                 ide_setup_ports(&hw, cris_ide_base_address(h),
794                                 ide_offsets,
795                                 0, 0, cris_ide_ack_intr,
796                                 ide_default_irq(0));
797                 ide_register_hw(&hw, &hwif);
798                 hwif->mmio = 2;
799                 hwif->chipset = ide_etrax100;
800                 hwif->tuneproc = &tune_cris_ide;
801                 hwif->speedproc = &speed_cris_ide;
802                 hwif->ata_input_data = &cris_ide_input_data;
803                 hwif->ata_output_data = &cris_ide_output_data;
804                 hwif->atapi_input_bytes = &cris_atapi_input_bytes;
805                 hwif->atapi_output_bytes = &cris_atapi_output_bytes;
806                 hwif->ide_dma_check = &cris_dma_check;
807                 hwif->ide_dma_end = &cris_dma_end;
808                 hwif->dma_setup = &cris_dma_setup;
809                 hwif->dma_exec_cmd = &cris_dma_exec_cmd;
810                 hwif->ide_dma_test_irq = &cris_dma_test_irq;
811                 hwif->dma_start = &cris_dma_start;
812                 hwif->OUTB = &cris_ide_outb;
813                 hwif->OUTW = &cris_ide_outw;
814                 hwif->OUTBSYNC = &cris_ide_outbsync;
815                 hwif->INB = &cris_ide_inb;
816                 hwif->INW = &cris_ide_inw;
817                 hwif->ide_dma_host_off = &cris_dma_off;
818                 hwif->ide_dma_host_on = &cris_dma_on;
819                 hwif->ide_dma_off_quietly = &cris_dma_off;
820                 hwif->udma_four = 0;
821                 hwif->ultra_mask = cris_ultra_mask;
822                 hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */
823                 hwif->swdma_mask = 0x07; /* Singleword DMA 0-2 */
824         }
825
826         /* Reset pulse */
827         cris_ide_reset(0);
828         udelay(25);
829         cris_ide_reset(1);
830
831         cris_ide_init();
832
833         cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD);
834         cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD);
835         cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);
836 }
837
838 static int cris_dma_off (ide_drive_t *drive)
839 {
840         return 0;
841 }
842
843 static int cris_dma_on (ide_drive_t *drive)
844 {
845         return 0;
846 }
847
848
849 static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));
850
851 /*
852  * The following routines are mainly used by the ATAPI drivers.
853  *
854  * These routines will round up any request for an odd number of bytes,
855  * so if an odd bytecount is specified, be sure that there's at least one
856  * extra byte allocated for the buffer.
857  */
858 static void
859 cris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
860 {
861         D(printk("atapi_input_bytes, buffer 0x%x, count %d\n",
862                  buffer, bytecount));
863
864         if(bytecount & 1) {
865                 printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
866                 bytecount++; /* to round off */
867         }
868
869         /* setup DMA and start transfer */
870
871         cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
872         cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount);
873
874         /* wait for completion */
875         LED_DISK_READ(1);
876         cris_ide_wait_dma(1);
877         LED_DISK_READ(0);
878 }
879
880 static void
881 cris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
882 {
883         D(printk("atapi_output_bytes, buffer 0x%x, count %d\n",
884                  buffer, bytecount));
885
886         if(bytecount & 1) {
887                 printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
888                 bytecount++;
889         }
890
891         cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
892         cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount);
893
894         /* wait for completion */
895
896         LED_DISK_WRITE(1);
897         LED_DISK_READ(1);
898         cris_ide_wait_dma(0);
899         LED_DISK_WRITE(0);
900 }
901
902 /*
903  * This is used for most PIO data transfers *from* the IDE interface
904  */
905 static void
906 cris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
907 {
908         cris_atapi_input_bytes(drive, buffer, wcount << 2);
909 }
910
911 /*
912  * This is used for most PIO data transfers *to* the IDE interface
913  */
914 static void
915 cris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
916 {
917         cris_atapi_output_bytes(drive, buffer, wcount << 2);
918 }
919
920 /* we only have one DMA channel on the chip for ATA, so we can keep these statically */
921 static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));
922 static unsigned int ata_tot_size;
923
924 /*
925  * cris_ide_build_dmatable() prepares a dma request.
926  * Returns 0 if all went okay, returns 1 otherwise.
927  */
928 static int cris_ide_build_dmatable (ide_drive_t *drive)
929 {
930         ide_hwif_t *hwif = drive->hwif;
931         struct scatterlist* sg;
932         struct request *rq  = drive->hwif->hwgroup->rq;
933         unsigned long size, addr;
934         unsigned int count = 0;
935         int i = 0;
936
937         sg = hwif->sg_table;
938
939         ata_tot_size = 0;
940
941         ide_map_sg(drive, rq);
942         i = hwif->sg_nents;
943
944         while(i) {
945                 /*
946                  * Determine addr and size of next buffer area.  We assume that
947                  * individual virtual buffers are always composed linearly in
948                  * physical memory.  For example, we assume that any 8kB buffer
949                  * is always composed of two adjacent physical 4kB pages rather
950                  * than two possibly non-adjacent physical 4kB pages.
951                  */
952                 /* group sequential buffers into one large buffer */
953                 addr = page_to_phys(sg->page) + sg->offset;
954                 size = sg_dma_len(sg);
955                 while (sg++, --i) {
956                         if ((addr + size) != page_to_phys(sg->page) + sg->offset)
957                                 break;
958                         size += sg_dma_len(sg);
959                 }
960
961                 /* did we run out of descriptors? */
962
963                 if(count >= MAX_DMA_DESCRS) {
964                         printk("%s: too few DMA descriptors\n", drive->name);
965                         return 1;
966                 }
967
968                 /* however, this case is more difficult - rw_trf_cnt cannot be more
969                    than 65536 words per transfer, so in that case we need to either
970                    1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with
971                       the descriptors, or
972                    2) simply do the request here, and get dma_intr to only ide_end_request on
973                       those blocks that were actually set-up for transfer.
974                 */
975
976                 if(ata_tot_size + size > 131072) {
977                         printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
978                         return 1;
979                 }
980
981                 /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. Since we
982                    don't handle size > 131072 only one split is necessary */
983
984                 if(size > MAX_DESCR_SIZE) {
985                         cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0);
986                         count++;
987                         ata_tot_size += MAX_DESCR_SIZE;
988                         size -= MAX_DESCR_SIZE;
989                         addr += MAX_DESCR_SIZE;
990                 }
991
992                 cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1);
993                 count++;
994                 ata_tot_size += size;
995         }
996
997         if (count) {
998                 /* return and say all is ok */
999                 return 0;
1000         }
1001
1002         printk("%s: empty DMA table?\n", drive->name);
1003         return 1;       /* let the PIO routines handle this weirdness */
1004 }
1005
1006 static int cris_config_drive_for_dma (ide_drive_t *drive)
1007 {
1008         u8 speed = ide_dma_speed(drive, 1);
1009
1010         if (!speed)
1011                 return 0;
1012
1013         speed_cris_ide(drive, speed);
1014         ide_config_drive_speed(drive, speed);
1015
1016         return ide_dma_enable(drive);
1017 }
1018
1019 /*
1020  * cris_dma_intr() is the handler for disk read/write DMA interrupts
1021  */
1022 static ide_startstop_t cris_dma_intr (ide_drive_t *drive)
1023 {
1024         LED_DISK_READ(0);
1025         LED_DISK_WRITE(0);
1026
1027         return ide_dma_intr(drive);
1028 }
1029
1030 /*
1031  * Functions below initiates/aborts DMA read/write operations on a drive.
1032  *
1033  * The caller is assumed to have selected the drive and programmed the drive's
1034  * sector address using CHS or LBA.  All that remains is to prepare for DMA
1035  * and then issue the actual read/write DMA/PIO command to the drive.
1036  *
1037  * For ATAPI devices, we just prepare for DMA and return. The caller should
1038  * then issue the packet command to the drive and call us again with
1039  * cris_dma_start afterwards.
1040  *
1041  * Returns 0 if all went well.
1042  * Returns 1 if DMA read/write could not be started, in which case
1043  * the caller should revert to PIO for the current request.
1044  */
1045
1046 static int cris_dma_check(ide_drive_t *drive)
1047 {
1048         ide_hwif_t *hwif = drive->hwif;
1049         struct hd_driveid* id = drive->id;
1050
1051         if (id && (id->capability & 1)) {
1052                 if (ide_use_dma(drive)) {
1053                         if (cris_config_drive_for_dma(drive))
1054                                 return hwif->ide_dma_on(drive);
1055                 }
1056         }
1057
1058         return hwif->ide_dma_off_quietly(drive);
1059 }
1060
1061 static int cris_dma_end(ide_drive_t *drive)
1062 {
1063         drive->waiting_for_dma = 0;
1064         return 0;
1065 }
1066
1067 static int cris_dma_setup(ide_drive_t *drive)
1068 {
1069         struct request *rq = drive->hwif->hwgroup->rq;
1070
1071         cris_ide_initialize_dma(!rq_data_dir(rq));
1072         if (cris_ide_build_dmatable (drive)) {
1073                 ide_map_sg(drive, rq);
1074                 return 1;
1075         }
1076
1077         drive->waiting_for_dma = 1;
1078         return 0;
1079 }
1080
1081 static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command)
1082 {
1083         /* set the irq handler which will finish the request when DMA is done */
1084         ide_set_handler(drive, &cris_dma_intr, WAIT_CMD, NULL);
1085
1086         /* issue cmd to drive */
1087         cris_ide_outb(command, IDE_COMMAND_REG);
1088 }
1089
1090 static void cris_dma_start(ide_drive_t *drive)
1091 {
1092         struct request *rq = drive->hwif->hwgroup->rq;
1093         int writing = rq_data_dir(rq);
1094         int type = TYPE_DMA;
1095
1096         if (drive->current_speed >= XFER_UDMA_0)
1097                 type = TYPE_UDMA;
1098
1099         cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size);
1100
1101         if (writing) {
1102                 LED_DISK_WRITE(1);
1103         } else {
1104                 LED_DISK_READ(1);
1105         }
1106 }