ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / syslib / ppc4xx_dma.c
1 /*
2  * Author: Pete Popov <ppopov@mvista.com> or source@mvista.com
3  *
4  * arch/ppc/kernel/ppc405_dma.c
5  *
6  * 2000 (c) MontaVista, Software, Inc.  This file is licensed under
7  * the terms of the GNU General Public License version 2.  This program
8  * is licensed "as is" without any warranty of any kind, whether express
9  * or implied.
10  *
11  * IBM 405 DMA Controller Functions
12  */
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <asm/system.h>
17 #include <asm/io.h>
18 #include <linux/mm.h>
19 #include <linux/miscdevice.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22
23 #include <asm/ppc405_dma.h>
24
25
26 /*
27  * Function prototypes
28  */
29
30 int hw_init_dma_channel(unsigned int,  ppc_dma_ch_t *);
31 int init_dma_channel(unsigned int);
32 int get_channel_config(unsigned int, ppc_dma_ch_t *);
33 int set_channel_priority(unsigned int, unsigned int);
34 unsigned int get_peripheral_width(unsigned int);
35 int alloc_dma_handle(sgl_handle_t *, unsigned int, unsigned int);
36 void free_dma_handle(sgl_handle_t);
37
38
39 ppc_dma_ch_t dma_channels[MAX_405GP_DMA_CHANNELS];
40
41 /*
42  * Configures a DMA channel, including the peripheral bus width, if a
43  * peripheral is attached to the channel, the polarity of the DMAReq and
44  * DMAAck signals, etc.  This information should really be setup by the boot
45  * code, since most likely the configuration won't change dynamically.
46  * If the kernel has to call this function, it's recommended that it's
47  * called from platform specific init code.  The driver should not need to
48  * call this function.
49  */
50 int hw_init_dma_channel(unsigned int dmanr,  ppc_dma_ch_t *p_init)
51 {
52     unsigned int polarity;
53     uint32_t control = 0;
54     ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
55
56 #ifdef DEBUG_405DMA
57     if (!p_init) {
58         printk("hw_init_dma_channel: NULL p_init\n");
59         return DMA_STATUS_NULL_POINTER;
60     }
61     if (dmanr >= MAX_405GP_DMA_CHANNELS) {
62         printk("hw_init_dma_channel: bad channel %d\n", dmanr);
63         return DMA_STATUS_BAD_CHANNEL;
64     }
65 #endif
66
67 #if DCRN_POL > 0
68     polarity = mfdcr(DCRN_POL);
69 #else
70     polarity = 0;
71 #endif
72
73     /* Setup the control register based on the values passed to
74      * us in p_init.  Then, over-write the control register with this
75      * new value.
76      */
77
78     control |= (
79                 SET_DMA_CIE_ENABLE(p_init->int_enable) | /* interrupt enable         */
80                 SET_DMA_BEN(p_init->buffer_enable)     | /* buffer enable            */
81                 SET_DMA_ETD(p_init->etd_output)        | /* end of transfer pin      */
82                 SET_DMA_TCE(p_init->tce_enable)        | /* terminal count enable    */
83                 SET_DMA_PL(p_init->pl)                 | /* peripheral location      */
84                 SET_DMA_DAI(p_init->dai)               | /* dest addr increment      */
85                 SET_DMA_SAI(p_init->sai)               | /* src addr increment       */
86                 SET_DMA_PRIORITY(p_init->cp)           |  /* channel priority        */
87                 SET_DMA_PW(p_init->pwidth)             |  /* peripheral/bus width    */
88                 SET_DMA_PSC(p_init->psc)               |  /* peripheral setup cycles */
89                 SET_DMA_PWC(p_init->pwc)               |  /* peripheral wait cycles  */
90                 SET_DMA_PHC(p_init->phc)               |  /* peripheral hold cycles  */
91                 SET_DMA_PREFETCH(p_init->pf)              /* read prefetch           */
92                 );
93
94     switch (dmanr) {
95         case 0:
96             /* clear all polarity signals and then "or" in new signal levels */
97             polarity &= ~(DMAReq0_ActiveLow | DMAAck0_ActiveLow | EOT0_ActiveLow);
98             polarity |= p_dma_ch->polarity;
99 #if DCRN_POL > 0
100             mtdcr(DCRN_POL, polarity);
101 #endif
102             mtdcr(DCRN_DMACR0, control);
103             break;
104         case 1:
105             polarity &= ~(DMAReq1_ActiveLow | DMAAck1_ActiveLow | EOT1_ActiveLow);
106             polarity |= p_dma_ch->polarity;
107 #if DCRN_POL > 0
108             mtdcr(DCRN_POL, polarity);
109 #endif
110             mtdcr(DCRN_DMACR1, control);
111             break;
112         case 2:
113             polarity &= ~(DMAReq2_ActiveLow | DMAAck2_ActiveLow | EOT2_ActiveLow);
114             polarity |= p_dma_ch->polarity;
115 #if DCRN_POL > 0
116             mtdcr(DCRN_POL, polarity);
117 #endif
118             mtdcr(DCRN_DMACR2, control);
119             break;
120         case 3:
121             polarity &= ~(DMAReq3_ActiveLow | DMAAck3_ActiveLow | EOT3_ActiveLow);
122             polarity |= p_dma_ch->polarity;
123 #if DCRN_POL > 0
124             mtdcr(DCRN_POL, polarity);
125 #endif
126             mtdcr(DCRN_DMACR3, control);
127             break;
128         default:
129             return DMA_STATUS_BAD_CHANNEL;
130     }
131
132     /* save these values in our dma channel structure */
133     memcpy(p_dma_ch, p_init, sizeof(ppc_dma_ch_t));
134
135     /*
136      * The peripheral width values written in the control register are:
137      *   PW_8                 0
138      *   PW_16                1
139      *   PW_32                2
140      *   PW_64                3
141      *
142      *   Since the DMA count register takes the number of "transfers",
143      *   we need to divide the count sent to us in certain
144      *   functions by the appropriate number.  It so happens that our
145      *   right shift value is equal to the peripheral width value.
146      */
147     p_dma_ch->shift = p_init->pwidth;
148
149     /*
150      * Save the control word for easy access.
151      */
152     p_dma_ch->control = control;
153
154     mtdcr(DCRN_DMASR, 0xffffffff); /* clear status register */
155     return DMA_STATUS_GOOD;
156 }
157
158
159
160
161 /*
162  * This function returns the channel configuration.
163  */
164 int get_channel_config(unsigned int dmanr, ppc_dma_ch_t *p_dma_ch)
165 {
166     unsigned int polarity;
167     unsigned int control;
168
169 #if DCRN_POL > 0
170     polarity = mfdcr(DCRN_POL);
171 #else
172     polarity = 0;
173 #endif
174
175     switch (dmanr) {
176         case 0:
177             p_dma_ch->polarity =
178                 polarity & (DMAReq0_ActiveLow | DMAAck0_ActiveLow | EOT0_ActiveLow);
179             control = mfdcr(DCRN_DMACR0);
180             break;
181         case 1:
182             p_dma_ch->polarity =
183                 polarity & (DMAReq1_ActiveLow | DMAAck1_ActiveLow | EOT1_ActiveLow);
184             control = mfdcr(DCRN_DMACR1);
185             break;
186         case 2:
187             p_dma_ch->polarity =
188                 polarity & (DMAReq2_ActiveLow | DMAAck2_ActiveLow | EOT2_ActiveLow);
189             control = mfdcr(DCRN_DMACR2);
190             break;
191         case 3:
192             p_dma_ch->polarity =
193                 polarity & (DMAReq3_ActiveLow | DMAAck3_ActiveLow | EOT3_ActiveLow);
194             control = mfdcr(DCRN_DMACR3);
195             break;
196         default:
197             return DMA_STATUS_BAD_CHANNEL;
198     }
199
200     p_dma_ch->cp = GET_DMA_PRIORITY(control);
201     p_dma_ch->pwidth = GET_DMA_PW(control);
202     p_dma_ch->psc = GET_DMA_PSC(control);
203     p_dma_ch->pwc = GET_DMA_PWC(control);
204     p_dma_ch->phc = GET_DMA_PHC(control);
205     p_dma_ch->pf = GET_DMA_PREFETCH(control);
206     p_dma_ch->int_enable = GET_DMA_CIE_ENABLE(control);
207     p_dma_ch->shift = GET_DMA_PW(control);
208
209     return DMA_STATUS_GOOD;
210 }
211
212 /*
213  * Sets the priority for the DMA channel dmanr.
214  * Since this is setup by the hardware init function, this function
215  * can be used to dynamically change the priority of a channel.
216  *
217  * Acceptable priorities:
218  *
219  * PRIORITY_LOW
220  * PRIORITY_MID_LOW
221  * PRIORITY_MID_HIGH
222  * PRIORITY_HIGH
223  *
224  */
225 int set_channel_priority(unsigned int dmanr, unsigned int priority)
226 {
227     unsigned int control;
228
229 #ifdef DEBUG_405DMA
230     if ( (priority != PRIORITY_LOW) &&
231             (priority != PRIORITY_MID_LOW) &&
232             (priority != PRIORITY_MID_HIGH) &&
233             (priority != PRIORITY_HIGH)) {
234         printk("set_channel_priority: bad priority: 0x%x\n", priority);
235     }
236 #endif
237
238     switch (dmanr) {
239         case 0:
240             control = mfdcr(DCRN_DMACR0);
241             control|= SET_DMA_PRIORITY(priority);
242             mtdcr(DCRN_DMACR0, control);
243             break;
244         case 1:
245             control = mfdcr(DCRN_DMACR1);
246             control|= SET_DMA_PRIORITY(priority);
247             mtdcr(DCRN_DMACR1, control);
248             break;
249         case 2:
250             control = mfdcr(DCRN_DMACR2);
251             control|= SET_DMA_PRIORITY(priority);
252             mtdcr(DCRN_DMACR2, control);
253             break;
254         case 3:
255             control = mfdcr(DCRN_DMACR3);
256             control|= SET_DMA_PRIORITY(priority);
257             mtdcr(DCRN_DMACR3, control);
258             break;
259         default:
260 #ifdef DEBUG_405DMA
261             printk("set_channel_priority: bad channel: %d\n", dmanr);
262 #endif
263             return DMA_STATUS_BAD_CHANNEL;
264     }
265     return DMA_STATUS_GOOD;
266 }
267
268
269
270 /*
271  * Returns the width of the peripheral attached to this channel. This assumes
272  * that someone who knows the hardware configuration, boot code or some other
273  * init code, already set the width.
274  *
275  * The return value is one of:
276  *   PW_8
277  *   PW_16
278  *   PW_32
279  *   PW_64
280  *
281  *   The function returns 0 on error.
282  */
283 unsigned int get_peripheral_width(unsigned int dmanr)
284 {
285     unsigned int control;
286
287     switch (dmanr) {
288         case 0:
289             control = mfdcr(DCRN_DMACR0);
290             break;
291         case 1:
292             control = mfdcr(DCRN_DMACR1);
293             break;
294         case 2:
295             control = mfdcr(DCRN_DMACR2);
296             break;
297         case 3:
298             control = mfdcr(DCRN_DMACR3);
299             break;
300         default:
301 #ifdef DEBUG_405DMA
302             printk("get_peripheral_width: bad channel: %d\n", dmanr);
303 #endif
304             return 0;
305     }
306     return(GET_DMA_PW(control));
307 }
308
309
310
311
312 /*
313  *   Create a scatter/gather list handle.  This is simply a structure which
314  *   describes a scatter/gather list.
315  *
316  *   A handle is returned in "handle" which the driver should save in order to
317  *   be able to access this list later.  A chunk of memory will be allocated
318  *   to be used by the API for internal management purposes, including managing
319  *   the sg list and allocating memory for the sgl descriptors.  One page should
320  *   be more than enough for that purpose.  Perhaps it's a bit wasteful to use
321  *   a whole page for a single sg list, but most likely there will be only one
322  *   sg list per channel.
323  *
324  *   Interrupt notes:
325  *   Each sgl descriptor has a copy of the DMA control word which the DMA engine
326  *   loads in the control register.  The control word has a "global" interrupt
327  *   enable bit for that channel. Interrupts are further qualified by a few bits
328  *   in the sgl descriptor count register.  In order to setup an sgl, we have to
329  *   know ahead of time whether or not interrupts will be enabled at the completion
330  *   of the transfers.  Thus, enable_dma_interrupt()/disable_dma_interrupt() MUST
331  *   be called before calling alloc_dma_handle().  If the interrupt mode will never
332  *   change after powerup, then enable_dma_interrupt()/disable_dma_interrupt()
333  *   do not have to be called -- interrupts will be enabled or disabled based
334  *   on how the channel was configured after powerup by the hw_init_dma_channel()
335  *   function.  Each sgl descriptor will be setup to interrupt if an error occurs;
336  *   however, only the last descriptor will be setup to interrupt. Thus, an
337  *   interrupt will occur (if interrupts are enabled) only after the complete
338  *   sgl transfer is done.
339  */
340 int alloc_dma_handle(sgl_handle_t *phandle, unsigned int mode, unsigned int dmanr)
341 {
342     sgl_list_info_t *psgl;
343     dma_addr_t dma_addr;
344     ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
345     uint32_t sg_command;
346     void *ret;
347
348 #ifdef DEBUG_405DMA
349     if (!phandle) {
350             printk("alloc_dma_handle: null handle pointer\n");
351             return DMA_STATUS_NULL_POINTER;
352     }
353     switch (mode) {
354         case DMA_MODE_READ:
355         case DMA_MODE_WRITE:
356         case DMA_MODE_MM:
357         case DMA_MODE_MM_DEVATSRC:
358         case DMA_MODE_MM_DEVATDST:
359             break;
360         default:
361             printk("alloc_dma_handle: bad mode 0x%x\n", mode);
362             return DMA_STATUS_BAD_MODE;
363     }
364     if (dmanr >= MAX_405GP_DMA_CHANNELS) {
365         printk("alloc_dma_handle: invalid channel 0x%x\n", dmanr);
366         return DMA_STATUS_BAD_CHANNEL;
367     }
368 #endif
369
370     /* Get a page of memory, which is zeroed out by pci_alloc_consistent() */
371
372 /* wrong not a pci device - armin */
373     /* psgl = (sgl_list_info_t *) pci_alloc_consistent(NULL, SGL_LIST_SIZE, &dma_addr);
374 */
375
376         ret = consistent_alloc(GFP_ATOMIC |GFP_DMA, SGL_LIST_SIZE, &dma_addr);
377         if (ret != NULL) {
378                 memset(ret, 0,SGL_LIST_SIZE );
379                 psgl = (sgl_list_info_t *) ret;
380         }
381
382
383     if (psgl == NULL) {
384         *phandle = (sgl_handle_t)NULL;
385         return DMA_STATUS_OUT_OF_MEMORY;
386     }
387
388     psgl->dma_addr = dma_addr;
389     psgl->dmanr = dmanr;
390
391     /*
392      * Modify and save the control word. These word will get written to each sgl
393      * descriptor.  The DMA engine then loads this control word into the control
394      * register every time it reads a new descriptor.
395      */
396     psgl->control = p_dma_ch->control;
397     psgl->control &= ~(DMA_TM_MASK | DMA_TD);  /* clear all "mode" bits first               */
398     psgl->control |= (mode | DMA_CH_ENABLE);   /* save the control word along with the mode */
399
400     if (p_dma_ch->int_enable) {
401         psgl->control |= DMA_CIE_ENABLE;       /* channel interrupt enabled                 */
402     }
403     else {
404         psgl->control &= ~DMA_CIE_ENABLE;
405     }
406
407 #if DCRN_ASGC > 0
408     sg_command = mfdcr(DCRN_ASGC);
409     switch (dmanr) {
410         case 0:
411             sg_command |= SSG0_MASK_ENABLE;
412             break;
413         case 1:
414             sg_command |= SSG1_MASK_ENABLE;
415             break;
416         case 2:
417             sg_command |= SSG2_MASK_ENABLE;
418             break;
419         case 3:
420             sg_command |= SSG3_MASK_ENABLE;
421             break;
422         default:
423 #ifdef DEBUG_405DMA
424             printk("alloc_dma_handle: bad channel: %d\n", dmanr);
425 #endif
426             free_dma_handle((sgl_handle_t)psgl);
427             *phandle = (sgl_handle_t)NULL;
428             return DMA_STATUS_BAD_CHANNEL;
429     }
430
431     mtdcr(DCRN_ASGC, sg_command);  /* enable writing to this channel's sgl control bits */
432 #else
433    (void)sg_command;
434 #endif
435     psgl->sgl_control = SG_ERI_ENABLE | SG_LINK;   /* sgl descriptor control bits */
436
437     if (p_dma_ch->int_enable) {
438         if (p_dma_ch->tce_enable)
439             psgl->sgl_control |= SG_TCI_ENABLE;
440         else
441             psgl->sgl_control |= SG_ETI_ENABLE;
442     }
443
444     *phandle = (sgl_handle_t)psgl;
445     return DMA_STATUS_GOOD;
446 }
447
448
449
450 /*
451  * Destroy a scatter/gather list handle that was created by alloc_dma_handle().
452  * The list must be empty (contain no elements).
453  */
454 void free_dma_handle(sgl_handle_t handle)
455 {
456     sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
457
458     if (!handle) {
459 #ifdef DEBUG_405DMA
460         printk("free_dma_handle: got NULL\n");
461 #endif
462         return;
463     }
464     else if (psgl->phead) {
465 #ifdef DEBUG_405DMA
466         printk("free_dma_handle: list not empty\n");
467 #endif
468         return;
469     }
470     else if (!psgl->dma_addr) { /* should never happen */
471 #ifdef DEBUG_405DMA
472         printk("free_dma_handle: no dma address\n");
473 #endif
474         return;
475     }
476
477   /* wrong not a PCI device -armin */
478   /*  pci_free_consistent(NULL, SGL_LIST_SIZE, (void *)psgl, psgl->dma_addr); */
479         //      free_pages((unsigned long)psgl, get_order(SGL_LIST_SIZE));
480         consistent_free((void *)psgl);
481
482
483 }
484
485
486 EXPORT_SYMBOL(hw_init_dma_channel);
487 EXPORT_SYMBOL(get_channel_config);
488 EXPORT_SYMBOL(set_channel_priority);
489 EXPORT_SYMBOL(get_peripheral_width);
490 EXPORT_SYMBOL(alloc_dma_handle);
491 EXPORT_SYMBOL(free_dma_handle);
492 EXPORT_SYMBOL(dma_channels);