ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-ppc / ppc405_dma.h
1 /*
2  * Author: Pete Popov <ppopov@mvista.com>
3  *
4  * 2000 (c) MontaVista, Software, Inc.  This file is licensed under
5  * the terms of the GNU General Public License version 2.  This program
6  * is licensed "as is" without any warranty of any kind, whether express
7  * or implied.
8  *
9  * Data structures specific to the IBM PowerPC 405 on-chip DMA controller
10  * and API.
11  */
12
13 #ifdef __KERNEL__
14 #ifndef __ASMPPC_405_DMA_H
15 #define __ASMPPC_405_DMA_H
16
17 #include <linux/types.h>
18
19 /* #define DEBUG_405DMA */
20
21 #define TRUE  1
22 #define FALSE 0
23
24 #define SGL_LIST_SIZE 4096
25 /* #define PCI_ALLOC_IS_NONCONSISTENT */
26
27 #define MAX_405GP_DMA_CHANNELS  4
28
29 /* The maximum address that we can perform a DMA transfer to on this platform */
30 /* Doesn't really apply... */
31 #define MAX_DMA_ADDRESS         0xFFFFFFFF
32
33 extern unsigned long ISA_DMA_THRESHOLD;
34
35 #define dma_outb        outb
36 #define dma_inb         inb
37
38
39 /*
40  * Function return status codes
41  * These values are used to indicate whether or not the function
42  * call was successful, or a bad/invalid parameter was passed.
43  */
44 #define DMA_STATUS_GOOD                 0
45 #define DMA_STATUS_BAD_CHANNEL          1
46 #define DMA_STATUS_BAD_HANDLE           2
47 #define DMA_STATUS_BAD_MODE             3
48 #define DMA_STATUS_NULL_POINTER         4
49 #define DMA_STATUS_OUT_OF_MEMORY        5
50 #define DMA_STATUS_SGL_LIST_EMPTY       6
51 #define DMA_STATUS_GENERAL_ERROR        7
52
53
54 /*
55  * These indicate status as returned from the DMA Status Register.
56  */
57 #define DMA_STATUS_NO_ERROR     0
58 #define DMA_STATUS_CS           1       /* Count Status        */
59 #define DMA_STATUS_TS           2       /* Transfer Status     */
60 #define DMA_STATUS_DMA_ERROR    3       /* DMA Error Occurred  */
61 #define DMA_STATUS_DMA_BUSY     4       /* The channel is busy */
62
63
64 /*
65  * Transfer Modes
66  * These modes are defined in a way that makes it possible to
67  * simply "or" in the value in the control register.
68  */
69 #define DMA_MODE_READ           DMA_TD                /* Peripheral to Memory */
70 #define DMA_MODE_WRITE          0                     /* Memory to Peripheral */
71 #define DMA_MODE_MM             (SET_DMA_TM(TM_S_MM)) /* memory to memory */
72
73                                 /* Device-paced memory to memory, */
74                                 /* device is at source address    */
75 #define DMA_MODE_MM_DEVATSRC    (DMA_TD | SET_DMA_TM(TM_D_MM))
76
77                                 /* Device-paced memory to memory,      */
78                                 /* device is at destination address    */
79 #define DMA_MODE_MM_DEVATDST    (SET_DMA_TM(TM_D_MM))
80
81
82 /*
83  * DMA Polarity Configuration Register
84  */
85 #define DMAReq0_ActiveLow (1<<31)
86 #define DMAAck0_ActiveLow (1<<30)
87 #define EOT0_ActiveLow    (1<<29)           /* End of Transfer      */
88
89 #define DMAReq1_ActiveLow (1<<28)
90 #define DMAAck1_ActiveLow (1<<27)
91 #define EOT1_ActiveLow    (1<<26)
92
93 #define DMAReq2_ActiveLow (1<<25)
94 #define DMAAck2_ActiveLow (1<<24)
95 #define EOT2_ActiveLow    (1<<23)
96
97 #define DMAReq3_ActiveLow (1<<22)
98 #define DMAAck3_ActiveLow (1<<21)
99 #define EOT3_ActiveLow    (1<<20)
100
101 /*
102  * DMA Sleep Mode Register
103  */
104 #define SLEEP_MODE_ENABLE (1<<21)
105
106
107 /*
108  * DMA Status Register
109  */
110 #define DMA_CS0           (1<<31) /* Terminal Count has been reached */
111 #define DMA_CS1           (1<<30)
112 #define DMA_CS2           (1<<29)
113 #define DMA_CS3           (1<<28)
114
115 #define DMA_TS0           (1<<27) /* End of Transfer has been requested */
116 #define DMA_TS1           (1<<26)
117 #define DMA_TS2           (1<<25)
118 #define DMA_TS3           (1<<24)
119
120 #define DMA_CH0_ERR       (1<<23) /* DMA Chanel 0 Error */
121 #define DMA_CH1_ERR       (1<<22)
122 #define DMA_CH2_ERR       (1<<21)
123 #define DMA_CH3_ERR       (1<<20)
124
125 #define DMA_IN_DMA_REQ0   (1<<19) /* Internal DMA Request is pending */
126 #define DMA_IN_DMA_REQ1   (1<<18)
127 #define DMA_IN_DMA_REQ2   (1<<17)
128 #define DMA_IN_DMA_REQ3   (1<<16)
129
130 #define DMA_EXT_DMA_REQ0  (1<<15) /* External DMA Request is pending */
131 #define DMA_EXT_DMA_REQ1  (1<<14)
132 #define DMA_EXT_DMA_REQ2  (1<<13)
133 #define DMA_EXT_DMA_REQ3  (1<<12)
134
135 #define DMA_CH0_BUSY      (1<<11) /* DMA Channel 0 Busy */
136 #define DMA_CH1_BUSY      (1<<10)
137 #define DMA_CH2_BUSY       (1<<9)
138 #define DMA_CH3_BUSY       (1<<8)
139
140 #define DMA_SG0            (1<<7) /* DMA Channel 0 Scatter/Gather in progress */
141 #define DMA_SG1            (1<<6)
142 #define DMA_SG2            (1<<5)
143 #define DMA_SG3            (1<<4)
144
145
146
147 /*
148  * DMA Channel Control Registers
149  */
150 #define DMA_CH_ENABLE         (1<<31)     /* DMA Channel Enable */
151 #define SET_DMA_CH_ENABLE(x)  (((x)&0x1)<<31)
152 #define GET_DMA_CH_ENABLE(x)  (((x)&DMA_CH_ENABLE)>>31)
153
154 #define DMA_CIE_ENABLE        (1<<30)     /* DMA Channel Interrupt Enable */
155 #define SET_DMA_CIE_ENABLE(x) (((x)&0x1)<<30)
156 #define GET_DMA_CIE_ENABLE(x) (((x)&DMA_CIE_ENABLE)>>30)
157
158 #define DMA_TD                (1<<29)
159 #define SET_DMA_TD(x)         (((x)&0x1)<<29)
160 #define GET_DMA_TD(x)         (((x)&DMA_TD)>>29)
161
162 #define DMA_PL                (1<<28)     /* Peripheral Location */
163 #define SET_DMA_PL(x)         (((x)&0x1)<<28)
164 #define GET_DMA_PL(x)         (((x)&DMA_PL)>>28)
165
166 #define EXTERNAL_PERIPHERAL    0
167 #define INTERNAL_PERIPHERAL    1
168
169
170 #define SET_DMA_PW(x)     (((x)&0x3)<<26) /* Peripheral Width */
171 #define DMA_PW_MASK       SET_DMA_PW(3)
172 #define   PW_8                 0
173 #define   PW_16                1
174 #define   PW_32                2
175 #define   PW_64                3
176 #define GET_DMA_PW(x)     (((x)&DMA_PW_MASK)>>26)
177
178 #define DMA_DAI           (1<<25)         /* Destination Address Increment */
179 #define SET_DMA_DAI(x)    (((x)&0x1)<<25)
180
181 #define DMA_SAI           (1<<24)         /* Source Address Increment */
182 #define SET_DMA_SAI(x)    (((x)&0x1)<<24)
183
184 #define DMA_BEN           (1<<23)         /* Buffer Enable */
185 #define SET_DMA_BEN(x)    (((x)&0x1)<<23)
186
187 #define SET_DMA_TM(x)     (((x)&0x3)<<21) /* Transfer Mode */
188 #define DMA_TM_MASK       SET_DMA_TM(3)
189 #define   TM_PERIPHERAL        0          /* Peripheral */
190 #define   TM_RESERVED          1          /* Reserved */
191 #define   TM_S_MM              2          /* Memory to Memory */
192 #define   TM_D_MM              3          /* Device Paced Memory to Memory */
193 #define GET_DMA_TM(x)     (((x)&DMA_TM_MASK)>>21)
194
195 #define SET_DMA_PSC(x)    (((x)&0x3)<<19) /* Peripheral Setup Cycles */
196 #define DMA_PSC_MASK      SET_DMA_PSC(3)
197 #define GET_DMA_PSC(x)    (((x)&DMA_PSC_MASK)>>19)
198
199 #define SET_DMA_PWC(x)    (((x)&0x3F)<<13) /* Peripheral Wait Cycles */
200 #define DMA_PWC_MASK      SET_DMA_PWC(0x3F)
201 #define GET_DMA_PWC(x)    (((x)&DMA_PWC_MASK)>>13)
202
203 #define SET_DMA_PHC(x)    (((x)&0x7)<<10) /* Peripheral Hold Cycles */
204 #define DMA_PHC_MASK      SET_DMA_PHC(0x7)
205 #define GET_DMA_PHC(x)    (((x)&DMA_PHC_MASK)>>10)
206
207 #define DMA_ETD_OUTPUT     (1<<9)         /* EOT pin is a TC output */
208 #define SET_DMA_ETD(x)     (((x)&0x1)<<9)
209
210 #define DMA_TCE_ENABLE     (1<<8)
211 #define SET_DMA_TCE(x)     (((x)&0x1)<<8)
212
213 #define SET_DMA_PRIORITY(x)   (((x)&0x3)<<6)   /* DMA Channel Priority */
214 #define DMA_PRIORITY_MASK SET_DMA_PRIORITY(3)
215 #define   PRIORITY_LOW         0
216 #define   PRIORITY_MID_LOW     1
217 #define   PRIORITY_MID_HIGH    2
218 #define   PRIORITY_HIGH        3
219 #define GET_DMA_PRIORITY(x) (((x)&DMA_PRIORITY_MASK)>>6)
220
221 #define SET_DMA_PREFETCH(x)   (((x)&0x3)<<4)  /* Memory Read Prefetch */
222 #define DMA_PREFETCH_MASK      SET_DMA_PREFETCH(3)
223 #define   PREFETCH_1           0              /* Prefetch 1 Double Word */
224 #define   PREFETCH_2           1
225 #define   PREFETCH_4           2
226 #define GET_DMA_PREFETCH(x) (((x)&DMA_PREFETCH_MASK)>>4)
227
228 #define DMA_PCE            (1<<3)         /* Parity Check Enable */
229 #define SET_DMA_PCE(x)     (((x)&0x1)<<3)
230 #define GET_DMA_PCE(x)     (((x)&DMA_PCE)>>3)
231
232 #define DMA_DEC            (1<<2)         /* Address Decrement */
233 #define SET_DMA_DEC(x)     (((x)&0x1)<<2)
234 #define GET_DMA_DEC(x)     (((x)&DMA_DEC)>>2)
235
236 /*
237  * DMA SG Command Register
238  */
239 #define SSG0_ENABLE        (1<<31)        /* Start Scatter Gather */
240 #define SSG1_ENABLE        (1<<30)
241 #define SSG2_ENABLE        (1<<29)
242 #define SSG3_ENABLE        (1<<28)
243 #define SSG0_MASK_ENABLE   (1<<15)        /* Enable writing to SSG0 bit */
244 #define SSG1_MASK_ENABLE   (1<<14)
245 #define SSG2_MASK_ENABLE   (1<<13)
246 #define SSG3_MASK_ENABLE   (1<<12)
247
248
249 /*
250  * DMA Scatter/Gather Descriptor Bit fields
251  */
252 #define SG_LINK            (1<<31)        /* Link */
253 #define SG_TCI_ENABLE      (1<<29)        /* Enable Terminal Count Interrupt */
254 #define SG_ETI_ENABLE      (1<<28)        /* Enable End of Transfer Interrupt */
255 #define SG_ERI_ENABLE      (1<<27)        /* Enable Error Interrupt */
256 #define SG_COUNT_MASK       0xFFFF        /* Count Field */
257
258
259
260
261 typedef uint32_t sgl_handle_t;
262
263 typedef struct {
264
265         /*
266          * Valid polarity settings:
267          *   DMAReq0_ActiveLow
268          *   DMAAck0_ActiveLow
269          *   EOT0_ActiveLow
270          *
271          *   DMAReq1_ActiveLow
272          *   DMAAck1_ActiveLow
273          *   EOT1_ActiveLow
274          *
275          *   DMAReq2_ActiveLow
276          *   DMAAck2_ActiveLow
277          *   EOT2_ActiveLow
278          *
279          *   DMAReq3_ActiveLow
280          *   DMAAck3_ActiveLow
281          *   EOT3_ActiveLow
282          */
283         unsigned int polarity;
284
285         char buffer_enable;      /* Boolean: buffer enable            */
286         char tce_enable;         /* Boolean: terminal count enable    */
287         char etd_output;         /* Boolean: eot pin is a tc output   */
288         char pce;                /* Boolean: parity check enable      */
289
290         /*
291          * Peripheral location:
292          * INTERNAL_PERIPHERAL (UART0 on the 405GP)
293          * EXTERNAL_PERIPHERAL
294          */
295         char pl;                 /* internal/external peripheral      */
296
297         /*
298          * Valid pwidth settings:
299          *   PW_8
300          *   PW_16
301          *   PW_32
302          *   PW_64
303          */
304         unsigned int pwidth;
305
306         char dai;                /* Boolean: dst address increment   */
307         char sai;                /* Boolean: src address increment   */
308
309         /*
310          * Valid psc settings: 0-3
311          */
312         unsigned int psc;        /* Peripheral Setup Cycles         */
313
314         /*
315          * Valid pwc settings:
316          * 0-63
317          */
318         unsigned int pwc;        /* Peripheral Wait Cycles          */
319
320         /*
321          * Valid phc settings:
322          * 0-7
323          */
324         unsigned int phc;        /* Peripheral Hold Cycles          */
325
326         /*
327          * Valid cp (channel priority) settings:
328          *   PRIORITY_LOW
329          *   PRIORITY_MID_LOW
330          *   PRIORITY_MID_HIGH
331          *   PRIORITY_HIGH
332          */
333         unsigned int cp;         /* channel priority                */
334
335         /*
336          * Valid pf (memory read prefetch) settings:
337          *
338          *   PREFETCH_1
339          *   PREFETCH_2
340          *   PREFETCH_4
341          */
342         unsigned int pf;         /* memory read prefetch            */
343
344         /*
345          * Boolean: channel interrupt enable
346          * NOTE: for sgl transfers, only the last descriptor will be setup to
347          * interrupt.
348          */
349         char int_enable;
350
351         char shift;              /* easy access to byte_count shift, based on */
352                                  /* the width of the channel                  */
353
354         uint32_t control;        /* channel control word                      */
355
356
357         /* These variabled are used ONLY in single dma transfers              */
358         unsigned int mode;       /* transfer mode                     */
359         dma_addr_t addr;
360
361 } ppc_dma_ch_t;
362
363
364 typedef struct {
365         uint32_t control;
366         uint32_t src_addr;
367         uint32_t dst_addr;
368         uint32_t control_count;
369         uint32_t next;
370 } ppc_sgl_t;
371
372
373
374 typedef struct {
375         unsigned int dmanr;
376         uint32_t control;     /* channel ctrl word; loaded from each descrptr */
377         uint32_t sgl_control; /* LK, TCI, ETI, and ERI bits in sgl descriptor */
378         dma_addr_t dma_addr;  /* dma (physical) address of this list          */
379         ppc_sgl_t *phead;
380         ppc_sgl_t *ptail;
381
382 } sgl_list_info_t;
383
384
385 typedef struct {
386         unsigned int *src_addr;
387         unsigned int *dst_addr;
388         dma_addr_t dma_src_addr;
389         dma_addr_t dma_dst_addr;
390 } pci_alloc_desc_t;
391
392
393 extern ppc_dma_ch_t dma_channels[];
394
395 /*
396  *
397  * DMA API inline functions
398  * These functions are implemented here as inline functions for
399  * performance reasons.
400  *
401  */
402
403 static __inline__ int get_405gp_dma_status(void)
404 {
405         return (mfdcr(DCRN_DMASR));
406 }
407
408
409 static __inline__ int enable_405gp_dma(unsigned int dmanr)
410 {
411         unsigned int control;
412         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
413
414 #ifdef DEBUG_405DMA
415         if (dmanr >= MAX_405GP_DMA_CHANNELS) {
416                 printk("enable_dma: bad channel: %d\n", dmanr);
417                 return DMA_STATUS_BAD_CHANNEL;
418         }
419 #endif
420
421
422         switch (dmanr) {
423         case 0:
424                 if (p_dma_ch->mode == DMA_MODE_READ) {
425                         /* peripheral to memory */
426                         mtdcr(DCRN_DMASA0, NULL);
427                         mtdcr(DCRN_DMADA0, p_dma_ch->addr);
428                         }
429                 else if (p_dma_ch->mode == DMA_MODE_WRITE) {
430                         /* memory to peripheral */
431                         mtdcr(DCRN_DMASA0, p_dma_ch->addr);
432                         mtdcr(DCRN_DMADA0, NULL);
433                 }
434                 /* for other xfer modes, the addresses are already set */
435                 control = mfdcr(DCRN_DMACR0);
436                 control &= ~(DMA_TM_MASK | DMA_TD);   /* clear all mode bits */
437                 control |= (p_dma_ch->mode | DMA_CH_ENABLE);
438                 mtdcr(DCRN_DMACR0, control);
439                 break;
440         case 1:
441                 if (p_dma_ch->mode == DMA_MODE_READ) {
442                         mtdcr(DCRN_DMASA1, NULL);
443                         mtdcr(DCRN_DMADA1, p_dma_ch->addr);
444                 } else if (p_dma_ch->mode == DMA_MODE_WRITE) {
445                         mtdcr(DCRN_DMASA1, p_dma_ch->addr);
446                         mtdcr(DCRN_DMADA1, NULL);
447                 }
448                 control = mfdcr(DCRN_DMACR1);
449                 control &= ~(DMA_TM_MASK | DMA_TD);
450                 control |= (p_dma_ch->mode | DMA_CH_ENABLE);
451                 mtdcr(DCRN_DMACR1, control);
452                 break;
453         case 2:
454                 if (p_dma_ch->mode == DMA_MODE_READ) {
455                         mtdcr(DCRN_DMASA2, NULL);
456                         mtdcr(DCRN_DMADA2, p_dma_ch->addr);
457                 } else if (p_dma_ch->mode == DMA_MODE_WRITE) {
458                         mtdcr(DCRN_DMASA2, p_dma_ch->addr);
459                         mtdcr(DCRN_DMADA2, NULL);
460                 }
461                 control = mfdcr(DCRN_DMACR2);
462                 control &= ~(DMA_TM_MASK | DMA_TD);
463                 control |= (p_dma_ch->mode | DMA_CH_ENABLE);
464                 mtdcr(DCRN_DMACR2, control);
465                 break;
466         case 3:
467                 if (p_dma_ch->mode == DMA_MODE_READ) {
468                         mtdcr(DCRN_DMASA3, NULL);
469                         mtdcr(DCRN_DMADA3, p_dma_ch->addr);
470                 } else if (p_dma_ch->mode == DMA_MODE_WRITE) {
471                         mtdcr(DCRN_DMASA3, p_dma_ch->addr);
472                         mtdcr(DCRN_DMADA3, NULL);
473                 }
474                 control = mfdcr(DCRN_DMACR3);
475                 control &= ~(DMA_TM_MASK | DMA_TD);
476                 control |= (p_dma_ch->mode | DMA_CH_ENABLE);
477                 mtdcr(DCRN_DMACR3, control);
478                 break;
479         default:
480                 return DMA_STATUS_BAD_CHANNEL;
481         }
482         return DMA_STATUS_GOOD;
483 }
484
485
486
487 static __inline__ void disable_405gp_dma(unsigned int dmanr)
488 {
489         unsigned int control;
490
491         switch (dmanr) {
492         case 0:
493                 control = mfdcr(DCRN_DMACR0);
494                 control &= ~DMA_CH_ENABLE;
495                 mtdcr(DCRN_DMACR0, control);
496                 break;
497         case 1:
498                 control = mfdcr(DCRN_DMACR1);
499                 control &= ~DMA_CH_ENABLE;
500                 mtdcr(DCRN_DMACR1, control);
501                 break;
502         case 2:
503                 control = mfdcr(DCRN_DMACR2);
504                 control &= ~DMA_CH_ENABLE;
505                 mtdcr(DCRN_DMACR2, control);
506                 break;
507         case 3:
508                 control = mfdcr(DCRN_DMACR3);
509                 control &= ~DMA_CH_ENABLE;
510                 mtdcr(DCRN_DMACR3, control);
511                 break;
512         default:
513 #ifdef DEBUG_405DMA
514                 printk("disable_dma: bad channel: %d\n", dmanr);
515 #endif
516         }
517 }
518
519
520
521 /*
522  * Sets the dma mode for single DMA transfers only.
523  * For scatter/gather transfers, the mode is passed to the
524  * alloc_dma_handle() function as one of the parameters.
525  *
526  * The mode is simply saved and used later.  This allows
527  * the driver to call set_dma_mode() and set_dma_addr() in
528  * any order.
529  *
530  * Valid mode values are:
531  *
532  * DMA_MODE_READ          peripheral to memory
533  * DMA_MODE_WRITE         memory to peripheral
534  * DMA_MODE_MM            memory to memory
535  * DMA_MODE_MM_DEVATSRC   device-paced memory to memory, device at src
536  * DMA_MODE_MM_DEVATDST   device-paced memory to memory, device at dst
537  */
538 static __inline__ int set_405gp_dma_mode(unsigned int dmanr, unsigned int mode)
539 {
540         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
541
542 #ifdef DEBUG_405DMA
543         switch (mode) {
544         case DMA_MODE_READ:
545         case DMA_MODE_WRITE:
546         case DMA_MODE_MM:
547         case DMA_MODE_MM_DEVATSRC:
548         case DMA_MODE_MM_DEVATDST:
549                 break;
550         default:
551                 printk("set_dma_mode: bad mode 0x%x\n", mode);
552                 return DMA_STATUS_BAD_MODE;
553         }
554         if (dmanr >= MAX_405GP_DMA_CHANNELS) {
555                 printk("set_dma_mode: bad channel 0x%x\n", dmanr);
556                 return DMA_STATUS_BAD_CHANNEL;
557         }
558 #endif
559
560         p_dma_ch->mode = mode;
561         return DMA_STATUS_GOOD;
562 }
563
564
565
566 /*
567  * Sets the DMA Count register. Note that 'count' is in bytes.
568  * However, the DMA Count register counts the number of "transfers",
569  * where each transfer is equal to the bus width.  Thus, count
570  * MUST be a multiple of the bus width.
571  */
572 static __inline__ void
573 set_405gp_dma_count(unsigned int dmanr, unsigned int count)
574 {
575         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
576
577 #ifdef DEBUG_405DMA
578         {
579         int error = 0;
580         switch(p_dma_ch->pwidth) {
581         case PW_8:
582                 break;
583         case PW_16:
584                 if (count & 0x1)
585                 error = 1;
586                 break;
587         case PW_32:
588                 if (count & 0x3)
589                 error = 1;
590                 break;
591         case PW_64:
592                 if (count & 0x7)
593                 error = 1;
594                 break;
595         default:
596                 printk("set_dma_count: invalid bus width: 0x%x\n",
597                         p_dma_ch->pwidth);
598                 return;
599         }
600         if (error)
601                 printk("Warning: set_dma_count count 0x%x bus width %d\n",
602                         count, p_dma_ch->pwidth);
603         }
604 #endif
605
606         count = count >> p_dma_ch->shift;
607         switch (dmanr) {
608         case 0:
609                 mtdcr(DCRN_DMACT0, count);
610                 break;
611         case 1:
612                 mtdcr(DCRN_DMACT1, count);
613                 break;
614         case 2:
615                 mtdcr(DCRN_DMACT2, count);
616                 break;
617         case 3:
618                 mtdcr(DCRN_DMACT3, count);
619                 break;
620         default:
621 #ifdef DEBUG_405DMA
622                 printk("set_dma_count: bad channel: %d\n", dmanr);
623 #endif
624         }
625 }
626
627
628
629 /*
630  *   Returns the number of bytes left to be transfered.
631  *   After a DMA transfer, this should return zero.
632  *   Reading this while a DMA transfer is still in progress will return
633  *   unpredictable results.
634  */
635 static __inline__ int get_405gp_dma_residue(unsigned int dmanr)
636 {
637         unsigned int count;
638         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
639
640         switch (dmanr) {
641         case 0:
642                 count = mfdcr(DCRN_DMACT0);
643                 break;
644         case 1:
645                 count = mfdcr(DCRN_DMACT1);
646                 break;
647         case 2:
648                 count = mfdcr(DCRN_DMACT2);
649                 break;
650         case 3:
651                 count = mfdcr(DCRN_DMACT3);
652                 break;
653         default:
654 #ifdef DEBUG_405DMA
655                 printk("get_dma_residue: bad channel: %d\n", dmanr);
656 #endif
657             return 0;
658         }
659
660         return (count << p_dma_ch->shift);
661 }
662
663
664
665 /*
666  * Sets the DMA address for a memory to peripheral or peripheral
667  * to memory transfer.  The address is just saved in the channel
668  * structure for now and used later in enable_dma().
669  */
670 static __inline__ void set_405gp_dma_addr(unsigned int dmanr, dma_addr_t addr)
671 {
672         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
673 #ifdef DEBUG_405DMA
674         {
675         int error = 0;
676         switch(p_dma_ch->pwidth) {
677         case PW_8:
678                 break;
679         case PW_16:
680                 if ((unsigned)addr & 0x1)
681                 error = 1;
682                 break;
683         case PW_32:
684                 if ((unsigned)addr & 0x3)
685                 error = 1;
686                 break;
687         case PW_64:
688                 if ((unsigned)addr & 0x7)
689                 error = 1;
690                 break;
691         default:
692                 printk("set_dma_addr: invalid bus width: 0x%x\n",
693                         p_dma_ch->pwidth);
694                 return;
695         }
696         if (error)
697                 printk("Warning: set_dma_addr addr 0x%x bus width %d\n",
698                         addr, p_dma_ch->pwidth);
699         }
700 #endif
701
702         /* save dma address and program it later after we know the xfer mode */
703         p_dma_ch->addr = addr;
704 }
705
706
707
708
709 /*
710  * Sets both DMA addresses for a memory to memory transfer.
711  * For memory to peripheral or peripheral to memory transfers
712  * the function set_dma_addr() should be used instead.
713  */
714 static __inline__ void
715 set_405gp_dma_addr2(unsigned int dmanr, dma_addr_t src_dma_addr,
716         dma_addr_t dst_dma_addr)
717 {
718 #ifdef DEBUG_405DMA
719         {
720         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
721         int error = 0;
722         switch(p_dma_ch->pwidth) {
723         case PW_8:
724                 break;
725         case PW_16:
726                 if (((unsigned)src_dma_addr & 0x1) ||
727                     ((unsigned)dst_dma_addr & 0x1)
728                    )
729                         error = 1;
730                 break;
731         case PW_32:
732                 if (((unsigned)src_dma_addr & 0x3) ||
733                     ((unsigned)dst_dma_addr & 0x3)
734                    )
735                         error = 1;
736                 break;
737         case PW_64:
738                 if (((unsigned)src_dma_addr & 0x7) ||
739                     ((unsigned)dst_dma_addr & 0x7)
740                    )
741                         error = 1;
742                 break;
743         default:
744                 printk("set_dma_addr2: invalid bus width: 0x%x\n",
745                         p_dma_ch->pwidth);
746                 return;
747         }
748         if (error)
749                 printk("Warning: set_dma_addr2 src 0x%x dst 0x%x bus width %d\n",
750                         src_dma_addr, dst_dma_addr, p_dma_ch->pwidth);
751         }
752 #endif
753
754         switch (dmanr) {
755         case 0:
756                 mtdcr(DCRN_DMASA0, src_dma_addr);
757                 mtdcr(DCRN_DMADA0, dst_dma_addr);
758                 break;
759         case 1:
760                 mtdcr(DCRN_DMASA1, src_dma_addr);
761                 mtdcr(DCRN_DMADA1, dst_dma_addr);
762                 break;
763         case 2:
764                 mtdcr(DCRN_DMASA2, src_dma_addr);
765                 mtdcr(DCRN_DMADA2, dst_dma_addr);
766                 break;
767         case 3:
768                 mtdcr(DCRN_DMASA3, src_dma_addr);
769                 mtdcr(DCRN_DMADA3, dst_dma_addr);
770                 break;
771         default:
772 #ifdef DEBUG_405DMA
773                 printk("set_dma_addr2: bad channel: %d\n", dmanr);
774 #endif
775         }
776 }
777
778
779
780 /*
781  * Enables the channel interrupt.
782  *
783  * If performing a scatter/gatter transfer, this function
784  * MUST be called before calling alloc_dma_handle() and building
785  * the sgl list.  Otherwise, interrupts will not be enabled, if
786  * they were previously disabled.
787  */
788 static __inline__ int
789 enable_405gp_dma_interrupt(unsigned int dmanr)
790 {
791         unsigned int control;
792         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
793
794         p_dma_ch->int_enable = TRUE;
795         switch (dmanr) {
796         case 0:
797                 control = mfdcr(DCRN_DMACR0);
798                 control|= DMA_CIE_ENABLE;        /* Channel Interrupt Enable */
799                 mtdcr(DCRN_DMACR0, control);
800                 break;
801         case 1:
802                 control = mfdcr(DCRN_DMACR1);
803                 control|= DMA_CIE_ENABLE;
804                 mtdcr(DCRN_DMACR1, control);
805                 break;
806         case 2:
807                 control = mfdcr(DCRN_DMACR2);
808                 control|= DMA_CIE_ENABLE;
809                 mtdcr(DCRN_DMACR2, control);
810                 break;
811         case 3:
812                 control = mfdcr(DCRN_DMACR3);
813                 control|= DMA_CIE_ENABLE;
814                 mtdcr(DCRN_DMACR3, control);
815                 break;
816         default:
817 #ifdef DEBUG_405DMA
818                 printk("enable_dma_interrupt: bad channel: %d\n", dmanr);
819 #endif
820                 return DMA_STATUS_BAD_CHANNEL;
821         }
822         return DMA_STATUS_GOOD;
823 }
824
825
826
827 /*
828  * Disables the channel interrupt.
829  *
830  * If performing a scatter/gatter transfer, this function
831  * MUST be called before calling alloc_dma_handle() and building
832  * the sgl list.  Otherwise, interrupts will not be disabled, if
833  * they were previously enabled.
834  */
835 static __inline__ int
836 disable_405gp_dma_interrupt(unsigned int dmanr)
837 {
838         unsigned int control;
839         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
840
841         p_dma_ch->int_enable = TRUE;
842         switch (dmanr) {
843         case 0:
844                 control = mfdcr(DCRN_DMACR0);
845                 control &= ~DMA_CIE_ENABLE;       /* Channel Interrupt Enable */
846                 mtdcr(DCRN_DMACR0, control);
847                 break;
848         case 1:
849                 control = mfdcr(DCRN_DMACR1);
850                 control &= ~DMA_CIE_ENABLE;
851                 mtdcr(DCRN_DMACR1, control);
852                 break;
853         case 2:
854                 control = mfdcr(DCRN_DMACR2);
855                 control &= ~DMA_CIE_ENABLE;
856                 mtdcr(DCRN_DMACR2, control);
857                 break;
858         case 3:
859                 control = mfdcr(DCRN_DMACR3);
860                 control &= ~DMA_CIE_ENABLE;
861                 mtdcr(DCRN_DMACR3, control);
862                 break;
863         default:
864 #ifdef DEBUG_405DMA
865                 printk("enable_dma_interrupt: bad channel: %d\n", dmanr);
866 #endif
867                 return DMA_STATUS_BAD_CHANNEL;
868         }
869         return DMA_STATUS_GOOD;
870 }
871
872
873 #ifdef DCRNCAP_DMA_SG
874
875 /*
876  *   Add a new sgl descriptor to the end of a scatter/gather list
877  *   which was created by alloc_dma_handle().
878  *
879  *   For a memory to memory transfer, both dma addresses must be
880  *   valid. For a peripheral to memory transfer, one of the addresses
881  *   must be set to NULL, depending on the direction of the transfer:
882  *   memory to peripheral: set dst_addr to NULL,
883  *   peripheral to memory: set src_addr to NULL.
884  */
885 static __inline__ int
886 add_405gp_dma_sgl(sgl_handle_t handle, dma_addr_t src_addr, dma_addr_t dst_addr,
887         unsigned int count)
888 {
889         sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
890         ppc_dma_ch_t *p_dma_ch;
891
892         if (!handle) {
893 #ifdef DEBUG_405DMA
894                 printk("add_dma_sgl: null handle\n");
895 #endif
896                 return DMA_STATUS_BAD_HANDLE;
897         }
898
899 #ifdef DEBUG_405DMA
900         if (psgl->dmanr >= MAX_405GP_DMA_CHANNELS) {
901                 printk("add_dma_sgl error: psgl->dmanr == %d\n", psgl->dmanr);
902                 return DMA_STATUS_BAD_CHANNEL;
903         }
904 #endif
905
906         p_dma_ch = &dma_channels[psgl->dmanr];
907
908 #ifdef DEBUG_405DMA
909         {
910         int error = 0;
911         unsigned int aligned = (unsigned)src_addr | (unsigned)dst_addr | count;
912         switch(p_dma_ch->pwidth) {
913         case PW_8:
914                 break;
915         case PW_16:
916                 if (aligned & 0x1)
917                 error = 1;
918                 break;
919         case PW_32:
920                 if (aligned & 0x3)
921                         error = 1;
922                 break;
923         case PW_64:
924                 if (aligned & 0x7)
925                         error = 1;
926                 break;
927         default:
928                 printk("add_dma_sgl: invalid bus width: 0x%x\n",
929                         p_dma_ch->pwidth);
930                 return DMA_STATUS_GENERAL_ERROR;
931         }
932         if (error)
933                 printk("Alignment warning: add_dma_sgl src 0x%x dst 0x%x count 0x%x bus width var %d\n",
934                         src_addr, dst_addr, count, p_dma_ch->pwidth);
935
936         }
937 #endif
938
939         if ((unsigned)(psgl->ptail + 1) >= ((unsigned)psgl + SGL_LIST_SIZE)) {
940 #ifdef DEBUG_405DMA
941                 printk("sgl handle out of memory \n");
942 #endif
943                 return DMA_STATUS_OUT_OF_MEMORY;
944         }
945
946
947         if (!psgl->ptail) {
948                 psgl->phead = (ppc_sgl_t *)
949                               ((unsigned)psgl + sizeof(sgl_list_info_t));
950                 psgl->ptail = psgl->phead;
951         } else {
952                 psgl->ptail->next = virt_to_bus(psgl->ptail + 1);
953                 psgl->ptail++;
954         }
955
956         psgl->ptail->control       = psgl->control;
957         psgl->ptail->src_addr      = src_addr;
958         psgl->ptail->dst_addr      = dst_addr;
959         psgl->ptail->control_count = (count >> p_dma_ch->shift) |
960                                      psgl->sgl_control;
961         psgl->ptail->next          = (uint32_t)NULL;
962
963         return DMA_STATUS_GOOD;
964 }
965
966
967
968 /*
969  * Enable (start) the DMA described by the sgl handle.
970  */
971 static __inline__ void enable_405gp_dma_sgl(sgl_handle_t handle)
972 {
973         sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
974         ppc_dma_ch_t *p_dma_ch;
975         uint32_t sg_command;
976
977 #ifdef DEBUG_405DMA
978         if (!handle) {
979                 printk("enable_dma_sgl: null handle\n");
980                 return;
981         } else if (psgl->dmanr > (MAX_405GP_DMA_CHANNELS - 1)) {
982                 printk("enable_dma_sgl: bad channel in handle %d\n",
983                         psgl->dmanr);
984                 return;
985         } else if (!psgl->phead) {
986                 printk("enable_dma_sgl: sg list empty\n");
987                 return;
988         }
989 #endif
990
991         p_dma_ch = &dma_channels[psgl->dmanr];
992         psgl->ptail->control_count &= ~SG_LINK; /* make this the last dscrptr */
993         sg_command = mfdcr(DCRN_ASGC);
994
995         switch(psgl->dmanr) {
996         case 0:
997                 mtdcr(DCRN_ASG0, virt_to_bus(psgl->phead));
998                 sg_command |= SSG0_ENABLE;
999                 break;
1000         case 1:
1001                 mtdcr(DCRN_ASG1, virt_to_bus(psgl->phead));
1002                 sg_command |= SSG1_ENABLE;
1003                 break;
1004         case 2:
1005                 mtdcr(DCRN_ASG2, virt_to_bus(psgl->phead));
1006                 sg_command |= SSG2_ENABLE;
1007                 break;
1008         case 3:
1009                 mtdcr(DCRN_ASG3, virt_to_bus(psgl->phead));
1010                 sg_command |= SSG3_ENABLE;
1011                 break;
1012         default:
1013 #ifdef DEBUG_405DMA
1014                 printk("enable_dma_sgl: bad channel: %d\n", psgl->dmanr);
1015 #endif
1016         }
1017
1018 #if 0 /* debug */
1019         printk("\n\nenable_dma_sgl at dma_addr 0x%x\n",
1020                 virt_to_bus(psgl->phead));
1021         {
1022         ppc_sgl_t *pnext, *sgl_addr;
1023
1024         pnext = psgl->phead;
1025         while (pnext) {
1026                 printk("dma descriptor at 0x%x, dma addr 0x%x\n",
1027                         (unsigned)pnext, (unsigned)virt_to_bus(pnext));
1028                 printk("control 0x%x src 0x%x dst 0x%x c_count 0x%x, next 0x%x\n",
1029                         (unsigned)pnext->control, (unsigned)pnext->src_addr,
1030                         (unsigned)pnext->dst_addr,
1031                         (unsigned)pnext->control_count, (unsigned)pnext->next);
1032
1033                 (unsigned)pnext = bus_to_virt(pnext->next);
1034         }
1035         printk("sg_command 0x%x\n", sg_command);
1036         }
1037 #endif
1038
1039 #ifdef PCI_ALLOC_IS_NONCONSISTENT
1040         /*
1041         * This is temporary only, until pci_alloc_consistent() really does
1042         * return "consistent" memory.
1043         */
1044         flush_dcache_range((unsigned)handle, (unsigned)handle + SGL_LIST_SIZE);
1045 #endif
1046
1047         mtdcr(DCRN_ASGC, sg_command);             /* start transfer */
1048 }
1049
1050
1051
1052 /*
1053  * Halt an active scatter/gather DMA operation.
1054  */
1055 static __inline__ void disable_405gp_dma_sgl(sgl_handle_t handle)
1056 {
1057         sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
1058         uint32_t sg_command;
1059
1060 #ifdef DEBUG_405DMA
1061         if (!handle) {
1062                 printk("enable_dma_sgl: null handle\n");
1063                 return;
1064         } else if (psgl->dmanr > (MAX_405GP_DMA_CHANNELS - 1)) {
1065                 printk("enable_dma_sgl: bad channel in handle %d\n",
1066                         psgl->dmanr);
1067                 return;
1068         }
1069 #endif
1070         sg_command = mfdcr(DCRN_ASGC);
1071         switch(psgl->dmanr) {
1072         case 0:
1073                 sg_command &= ~SSG0_ENABLE;
1074                 break;
1075         case 1:
1076                 sg_command &= ~SSG1_ENABLE;
1077                 break;
1078         case 2:
1079                 sg_command &= ~SSG2_ENABLE;
1080                 break;
1081         case 3:
1082                 sg_command &= ~SSG3_ENABLE;
1083                 break;
1084         default:
1085 #ifdef DEBUG_405DMA
1086                 printk("enable_dma_sgl: bad channel: %d\n", psgl->dmanr);
1087 #endif
1088         }
1089
1090         mtdcr(DCRN_ASGC, sg_command);             /* stop transfer */
1091 }
1092
1093
1094
1095 /*
1096  *  Returns number of bytes left to be transferred from the entire sgl list.
1097  *  *src_addr and *dst_addr get set to the source/destination address of
1098  *  the sgl descriptor where the DMA stopped.
1099  *
1100  *  An sgl transfer must NOT be active when this function is called.
1101  */
1102 static __inline__ int
1103 get_405gp_dma_sgl_residue(sgl_handle_t handle, dma_addr_t *src_addr,
1104         dma_addr_t *dst_addr)
1105 {
1106         sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
1107         ppc_dma_ch_t *p_dma_ch;
1108         ppc_sgl_t *pnext, *sgl_addr;
1109         uint32_t count_left;
1110
1111 #ifdef DEBUG_405DMA
1112         if (!handle) {
1113                 printk("get_dma_sgl_residue: null handle\n");
1114                 return DMA_STATUS_BAD_HANDLE;
1115         } else if (psgl->dmanr > (MAX_405GP_DMA_CHANNELS - 1)) {
1116                 printk("get_dma_sgl_residue: bad channel in handle %d\n",
1117                         psgl->dmanr);
1118                 return DMA_STATUS_BAD_CHANNEL;
1119         }
1120 #endif
1121
1122         switch(psgl->dmanr) {
1123         case 0:
1124                 sgl_addr = (ppc_sgl_t *)bus_to_virt(mfdcr(DCRN_ASG0));
1125                 count_left = mfdcr(DCRN_DMACT0);
1126                 break;
1127         case 1:
1128                 sgl_addr = (ppc_sgl_t *)bus_to_virt(mfdcr(DCRN_ASG1));
1129                 count_left = mfdcr(DCRN_DMACT1);
1130                 break;
1131         case 2:
1132                 sgl_addr = (ppc_sgl_t *)bus_to_virt(mfdcr(DCRN_ASG2));
1133                 count_left = mfdcr(DCRN_DMACT2);
1134                 break;
1135         case 3:
1136                 sgl_addr = (ppc_sgl_t *)bus_to_virt(mfdcr(DCRN_ASG3));
1137                 count_left = mfdcr(DCRN_DMACT3);
1138                 break;
1139         default:
1140 #ifdef DEBUG_405DMA
1141                 printk("get_dma_sgl_residue: bad channel: %d\n", psgl->dmanr);
1142 #endif
1143                 goto error;
1144         }
1145
1146         if (!sgl_addr) {
1147 #ifdef DEBUG_405DMA
1148                 printk("get_dma_sgl_residue: sgl addr register is null\n");
1149 #endif
1150                 goto error;
1151         }
1152
1153         pnext = psgl->phead;
1154         while (pnext &&
1155                 ((unsigned)pnext < ((unsigned)psgl + SGL_LIST_SIZE) &&
1156                 (pnext != sgl_addr))
1157               ) {
1158                 pnext = pnext++;
1159         }
1160
1161         if (pnext == sgl_addr) {           /* found the sgl descriptor */
1162
1163                 *src_addr = pnext->src_addr;
1164                 *dst_addr = pnext->dst_addr;
1165
1166                 /*
1167                  * Now search the remaining descriptors and add their count.
1168                  * We already have the remaining count from this descriptor in
1169                  * count_left.
1170                  */
1171                 pnext++;
1172
1173                 while ((pnext != psgl->ptail) &&
1174                         ((unsigned)pnext < ((unsigned)psgl + SGL_LIST_SIZE))
1175                       ) {
1176                         count_left += pnext->control_count & SG_COUNT_MASK;
1177                 }
1178
1179                 if (pnext != psgl->ptail) { /* should never happen */
1180 #ifdef DEBUG_405DMA
1181                         printk("get_dma_sgl_residue error (1) psgl->ptail 0x%x handle 0x%x\n",
1182                                 (unsigned int)psgl->ptail,
1183                                 (unsigned int)handle);
1184 #endif
1185                         goto error;
1186                 }
1187
1188                 /* success */
1189                 p_dma_ch = &dma_channels[psgl->dmanr];
1190                 return (count_left << p_dma_ch->shift);  /* count in bytes */
1191
1192         } else {
1193         /* this shouldn't happen */
1194 #ifdef DEBUG_405DMA
1195                 printk("get_dma_sgl_residue, unable to match current address 0x%x, handle 0x%x\n",
1196                         (unsigned int)sgl_addr, (unsigned int)handle);
1197
1198 #endif
1199         }
1200
1201
1202 error:
1203         *src_addr = (dma_addr_t)NULL;
1204         *dst_addr = (dma_addr_t)NULL;
1205         return 0;
1206 }
1207
1208
1209
1210
1211 /*
1212  * Returns the address(es) of the buffer(s) contained in the head element of
1213  * the scatter/gather list.  The element is removed from the scatter/gather
1214  * list and the next element becomes the head.
1215  *
1216  * This function should only be called when the DMA is not active.
1217  */
1218 static __inline__ int
1219 delete_405gp_dma_sgl_element(sgl_handle_t handle, dma_addr_t *src_dma_addr,
1220         dma_addr_t *dst_dma_addr)
1221 {
1222         sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
1223
1224 #ifdef DEBUG_405DMA
1225         if (!handle) {
1226                 printk("delete_sgl_element: null handle\n");
1227                 return DMA_STATUS_BAD_HANDLE;
1228         } else if (psgl->dmanr > (MAX_405GP_DMA_CHANNELS - 1)) {
1229                 printk("delete_sgl_element: bad channel in handle %d\n",
1230                         psgl->dmanr);
1231                 return DMA_STATUS_BAD_CHANNEL;
1232         }
1233 #endif
1234
1235         if (!psgl->phead) {
1236 #ifdef DEBUG_405DMA
1237                 printk("delete_sgl_element: sgl list empty\n");
1238 #endif
1239                 *src_dma_addr = (dma_addr_t)NULL;
1240                 *dst_dma_addr = (dma_addr_t)NULL;
1241                 return DMA_STATUS_SGL_LIST_EMPTY;
1242         }
1243
1244         *src_dma_addr = (dma_addr_t)psgl->phead->src_addr;
1245         *dst_dma_addr = (dma_addr_t)psgl->phead->dst_addr;
1246
1247         if (psgl->phead == psgl->ptail) {
1248                 /* last descriptor on the list */
1249                 psgl->phead = NULL;
1250                 psgl->ptail = NULL;
1251         } else {
1252                 psgl->phead++;
1253         }
1254
1255         return DMA_STATUS_GOOD;
1256 }
1257
1258 #endif /* DCRNCAP_DMA_SG */
1259
1260 /*
1261  * The rest of the DMA API, in ppc405_dma.c
1262  */
1263 extern int hw_init_dma_channel(unsigned int,  ppc_dma_ch_t *);
1264 extern int get_channel_config(unsigned int, ppc_dma_ch_t *);
1265 extern int set_channel_priority(unsigned int, unsigned int);
1266 extern unsigned int get_peripheral_width(unsigned int);
1267 extern int alloc_dma_handle(sgl_handle_t *, unsigned int, unsigned int);
1268 extern void free_dma_handle(sgl_handle_t);
1269
1270 #endif
1271 #endif /* __KERNEL__ */