Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / ppc / syslib / ppc4xx_sgdma.c
1 /*
2  * arch/ppc/kernel/ppc4xx_sgdma.c
3  *
4  * IBM PPC4xx DMA engine scatter/gather library
5  *
6  * Copyright 2002-2003 MontaVista Software Inc.
7  *
8  * Cleaned up and converted to new DCR access
9  * Matt Porter <mporter@kernel.crashing.org>
10  *
11  * Original code by Armin Kuster <akuster@mvista.com>
12  * and Pete Popov <ppopov@mvista.com>
13  *
14  * This program is free software; you can redistribute  it and/or modify it
15  * under  the terms of  the GNU General  Public License as published by the
16  * Free Software Foundation;  either version 2 of the  License, or (at your
17  * option) any later version.
18  *
19  * You should have received a copy of the  GNU General Public License along
20  * with this program; if not, write  to the Free Software Foundation, Inc.,
21  * 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30
31 #include <asm/system.h>
32 #include <asm/io.h>
33 #include <asm/ppc4xx_dma.h>
34
35 void
36 ppc4xx_set_sg_addr(int dmanr, phys_addr_t sg_addr)
37 {
38         if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
39                 printk("ppc4xx_set_sg_addr: bad channel: %d\n", dmanr);
40                 return;
41         }
42
43 #ifdef PPC4xx_DMA_64BIT
44         mtdcr(DCRN_ASGH0 + (dmanr * 0x8), (u32)(sg_addr >> 32));
45 #endif
46         mtdcr(DCRN_ASG0 + (dmanr * 0x8), (u32)sg_addr);
47 }
48
49 /*
50  *   Add a new sgl descriptor to the end of a scatter/gather list
51  *   which was created by alloc_dma_handle().
52  *
53  *   For a memory to memory transfer, both dma addresses must be
54  *   valid. For a peripheral to memory transfer, one of the addresses
55  *   must be set to NULL, depending on the direction of the transfer:
56  *   memory to peripheral: set dst_addr to NULL,
57  *   peripheral to memory: set src_addr to NULL.
58  */
59 int
60 ppc4xx_add_dma_sgl(sgl_handle_t handle, phys_addr_t src_addr, phys_addr_t dst_addr,
61                    unsigned int count)
62 {
63         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
64         ppc_dma_ch_t *p_dma_ch;
65
66         if (!handle) {
67                 printk("ppc4xx_add_dma_sgl: null handle\n");
68                 return DMA_STATUS_BAD_HANDLE;
69         }
70
71         if (psgl->dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
72                 printk("ppc4xx_add_dma_sgl: bad channel: %d\n", psgl->dmanr);
73                 return DMA_STATUS_BAD_CHANNEL;
74         }
75
76         p_dma_ch = &dma_channels[psgl->dmanr];
77
78 #ifdef DEBUG_4xxDMA
79         {
80                 int error = 0;
81                 unsigned int aligned =
82                     (unsigned) src_addr | (unsigned) dst_addr | count;
83                 switch (p_dma_ch->pwidth) {
84                 case PW_8:
85                         break;
86                 case PW_16:
87                         if (aligned & 0x1)
88                                 error = 1;
89                         break;
90                 case PW_32:
91                         if (aligned & 0x3)
92                                 error = 1;
93                         break;
94                 case PW_64:
95                         if (aligned & 0x7)
96                                 error = 1;
97                         break;
98                 default:
99                         printk("ppc4xx_add_dma_sgl: invalid bus width: 0x%x\n",
100                                p_dma_ch->pwidth);
101                         return DMA_STATUS_GENERAL_ERROR;
102                 }
103                 if (error)
104                         printk
105                             ("Alignment warning: ppc4xx_add_dma_sgl src 0x%x dst 0x%x count 0x%x bus width var %d\n",
106                              src_addr, dst_addr, count, p_dma_ch->pwidth);
107
108         }
109 #endif
110
111         if ((unsigned) (psgl->ptail + 1) >= ((unsigned) psgl + SGL_LIST_SIZE)) {
112                 printk("sgl handle out of memory \n");
113                 return DMA_STATUS_OUT_OF_MEMORY;
114         }
115
116         if (!psgl->ptail) {
117                 psgl->phead = (ppc_sgl_t *)
118                     ((unsigned) psgl + sizeof (sgl_list_info_t));
119                 psgl->phead_dma = psgl->dma_addr + sizeof(sgl_list_info_t);
120                 psgl->ptail = psgl->phead;
121                 psgl->ptail_dma = psgl->phead_dma;
122         } else {
123                 if(p_dma_ch->int_on_final_sg) {
124                         /* mask out all dma interrupts, except error, on tail
125                         before adding new tail. */
126                         psgl->ptail->control_count &=
127                                 ~(SG_TCI_ENABLE | SG_ETI_ENABLE);
128                 }
129                 psgl->ptail->next = psgl->ptail_dma + sizeof(ppc_sgl_t);
130                 psgl->ptail++;
131                 psgl->ptail_dma += sizeof(ppc_sgl_t);
132         }
133
134         psgl->ptail->control = psgl->control;
135         psgl->ptail->src_addr = src_addr;
136         psgl->ptail->dst_addr = dst_addr;
137         psgl->ptail->control_count = (count >> p_dma_ch->shift) |
138             psgl->sgl_control;
139         psgl->ptail->next = (uint32_t) NULL;
140
141         return DMA_STATUS_GOOD;
142 }
143
144 /*
145  * Enable (start) the DMA described by the sgl handle.
146  */
147 void
148 ppc4xx_enable_dma_sgl(sgl_handle_t handle)
149 {
150         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
151         ppc_dma_ch_t *p_dma_ch;
152         uint32_t sg_command;
153
154         if (!handle) {
155                 printk("ppc4xx_enable_dma_sgl: null handle\n");
156                 return;
157         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
158                 printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
159                        psgl->dmanr);
160                 return;
161         } else if (!psgl->phead) {
162                 printk("ppc4xx_enable_dma_sgl: sg list empty\n");
163                 return;
164         }
165
166         p_dma_ch = &dma_channels[psgl->dmanr];
167         psgl->ptail->control_count &= ~SG_LINK; /* make this the last dscrptr */
168         sg_command = mfdcr(DCRN_ASGC);
169
170         ppc4xx_set_sg_addr(psgl->dmanr, psgl->phead_dma);
171
172         sg_command |= SSG_ENABLE(psgl->dmanr);
173
174         mtdcr(DCRN_ASGC, sg_command);   /* start transfer */
175 }
176
177 /*
178  * Halt an active scatter/gather DMA operation.
179  */
180 void
181 ppc4xx_disable_dma_sgl(sgl_handle_t handle)
182 {
183         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
184         uint32_t sg_command;
185
186         if (!handle) {
187                 printk("ppc4xx_enable_dma_sgl: null handle\n");
188                 return;
189         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
190                 printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
191                        psgl->dmanr);
192                 return;
193         }
194
195         sg_command = mfdcr(DCRN_ASGC);
196         sg_command &= ~SSG_ENABLE(psgl->dmanr);
197         mtdcr(DCRN_ASGC, sg_command);   /* stop transfer */
198 }
199
200 /*
201  *  Returns number of bytes left to be transferred from the entire sgl list.
202  *  *src_addr and *dst_addr get set to the source/destination address of
203  *  the sgl descriptor where the DMA stopped.
204  *
205  *  An sgl transfer must NOT be active when this function is called.
206  */
207 int
208 ppc4xx_get_dma_sgl_residue(sgl_handle_t handle, phys_addr_t * src_addr,
209                            phys_addr_t * dst_addr)
210 {
211         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
212         ppc_dma_ch_t *p_dma_ch;
213         ppc_sgl_t *pnext, *sgl_addr;
214         uint32_t count_left;
215
216         if (!handle) {
217                 printk("ppc4xx_get_dma_sgl_residue: null handle\n");
218                 return DMA_STATUS_BAD_HANDLE;
219         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
220                 printk("ppc4xx_get_dma_sgl_residue: bad channel in handle %d\n",
221                        psgl->dmanr);
222                 return DMA_STATUS_BAD_CHANNEL;
223         }
224
225         sgl_addr = (ppc_sgl_t *) __va(mfdcr(DCRN_ASG0 + (psgl->dmanr * 0x8)));
226         count_left = mfdcr(DCRN_DMACT0 + (psgl->dmanr * 0x8)) & SG_COUNT_MASK;
227
228         if (!sgl_addr) {
229                 printk("ppc4xx_get_dma_sgl_residue: sgl addr register is null\n");
230                 goto error;
231         }
232
233         pnext = psgl->phead;
234         while (pnext &&
235                ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE) &&
236                 (pnext != sgl_addr))
237             ) {
238                 pnext++;
239         }
240
241         if (pnext == sgl_addr) {        /* found the sgl descriptor */
242
243                 *src_addr = pnext->src_addr;
244                 *dst_addr = pnext->dst_addr;
245
246                 /*
247                  * Now search the remaining descriptors and add their count.
248                  * We already have the remaining count from this descriptor in
249                  * count_left.
250                  */
251                 pnext++;
252
253                 while ((pnext != psgl->ptail) &&
254                        ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE))
255                     ) {
256                         count_left += pnext->control_count & SG_COUNT_MASK;
257                 }
258
259                 if (pnext != psgl->ptail) {     /* should never happen */
260                         printk
261                             ("ppc4xx_get_dma_sgl_residue error (1) psgl->ptail 0x%x handle 0x%x\n",
262                              (unsigned int) psgl->ptail, (unsigned int) handle);
263                         goto error;
264                 }
265
266                 /* success */
267                 p_dma_ch = &dma_channels[psgl->dmanr];
268                 return (count_left << p_dma_ch->shift); /* count in bytes */
269
270         } else {
271                 /* this shouldn't happen */
272                 printk
273                     ("get_dma_sgl_residue, unable to match current address 0x%x, handle 0x%x\n",
274                      (unsigned int) sgl_addr, (unsigned int) handle);
275
276         }
277
278       error:
279         *src_addr = (phys_addr_t) NULL;
280         *dst_addr = (phys_addr_t) NULL;
281         return 0;
282 }
283
284 /*
285  * Returns the address(es) of the buffer(s) contained in the head element of
286  * the scatter/gather list.  The element is removed from the scatter/gather
287  * list and the next element becomes the head.
288  *
289  * This function should only be called when the DMA is not active.
290  */
291 int
292 ppc4xx_delete_dma_sgl_element(sgl_handle_t handle, phys_addr_t * src_dma_addr,
293                               phys_addr_t * dst_dma_addr)
294 {
295         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
296
297         if (!handle) {
298                 printk("ppc4xx_delete_sgl_element: null handle\n");
299                 return DMA_STATUS_BAD_HANDLE;
300         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
301                 printk("ppc4xx_delete_sgl_element: bad channel in handle %d\n",
302                        psgl->dmanr);
303                 return DMA_STATUS_BAD_CHANNEL;
304         }
305
306         if (!psgl->phead) {
307                 printk("ppc4xx_delete_sgl_element: sgl list empty\n");
308                 *src_dma_addr = (phys_addr_t) NULL;
309                 *dst_dma_addr = (phys_addr_t) NULL;
310                 return DMA_STATUS_SGL_LIST_EMPTY;
311         }
312
313         *src_dma_addr = (phys_addr_t) psgl->phead->src_addr;
314         *dst_dma_addr = (phys_addr_t) psgl->phead->dst_addr;
315
316         if (psgl->phead == psgl->ptail) {
317                 /* last descriptor on the list */
318                 psgl->phead = NULL;
319                 psgl->ptail = NULL;
320         } else {
321                 psgl->phead++;
322                 psgl->phead_dma += sizeof(ppc_sgl_t);
323         }
324
325         return DMA_STATUS_GOOD;
326 }
327
328
329 /*
330  *   Create a scatter/gather list handle.  This is simply a structure which
331  *   describes a scatter/gather list.
332  *
333  *   A handle is returned in "handle" which the driver should save in order to
334  *   be able to access this list later.  A chunk of memory will be allocated
335  *   to be used by the API for internal management purposes, including managing
336  *   the sg list and allocating memory for the sgl descriptors.  One page should
337  *   be more than enough for that purpose.  Perhaps it's a bit wasteful to use
338  *   a whole page for a single sg list, but most likely there will be only one
339  *   sg list per channel.
340  *
341  *   Interrupt notes:
342  *   Each sgl descriptor has a copy of the DMA control word which the DMA engine
343  *   loads in the control register.  The control word has a "global" interrupt
344  *   enable bit for that channel. Interrupts are further qualified by a few bits
345  *   in the sgl descriptor count register.  In order to setup an sgl, we have to
346  *   know ahead of time whether or not interrupts will be enabled at the completion
347  *   of the transfers.  Thus, enable_dma_interrupt()/disable_dma_interrupt() MUST
348  *   be called before calling alloc_dma_handle().  If the interrupt mode will never
349  *   change after powerup, then enable_dma_interrupt()/disable_dma_interrupt()
350  *   do not have to be called -- interrupts will be enabled or disabled based
351  *   on how the channel was configured after powerup by the hw_init_dma_channel()
352  *   function.  Each sgl descriptor will be setup to interrupt if an error occurs;
353  *   however, only the last descriptor will be setup to interrupt. Thus, an
354  *   interrupt will occur (if interrupts are enabled) only after the complete
355  *   sgl transfer is done.
356  */
357 int
358 ppc4xx_alloc_dma_handle(sgl_handle_t * phandle, unsigned int mode, unsigned int dmanr)
359 {
360         sgl_list_info_t *psgl=NULL;
361         dma_addr_t dma_addr;
362         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
363         uint32_t sg_command;
364         uint32_t ctc_settings;
365         void *ret;
366
367         if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
368                 printk("ppc4xx_alloc_dma_handle: invalid channel 0x%x\n", dmanr);
369                 return DMA_STATUS_BAD_CHANNEL;
370         }
371
372         if (!phandle) {
373                 printk("ppc4xx_alloc_dma_handle: null handle pointer\n");
374                 return DMA_STATUS_NULL_POINTER;
375         }
376
377         /* Get a page of memory, which is zeroed out by consistent_alloc() */
378         ret = dma_alloc_coherent(NULL, DMA_PPC4xx_SIZE, &dma_addr, GFP_KERNEL);
379         if (ret != NULL) {
380                 memset(ret, 0, DMA_PPC4xx_SIZE);
381                 psgl = (sgl_list_info_t *) ret;
382         }
383
384         if (psgl == NULL) {
385                 *phandle = (sgl_handle_t) NULL;
386                 return DMA_STATUS_OUT_OF_MEMORY;
387         }
388
389         psgl->dma_addr = dma_addr;
390         psgl->dmanr = dmanr;
391
392         /*
393          * Modify and save the control word. These words will be
394          * written to each sgl descriptor.  The DMA engine then
395          * loads this control word into the control register
396          * every time it reads a new descriptor.
397          */
398         psgl->control = p_dma_ch->control;
399         /* Clear all mode bits */
400         psgl->control &= ~(DMA_TM_MASK | DMA_TD);
401         /* Save control word and mode */
402         psgl->control |= (mode | DMA_CE_ENABLE);
403
404         /* In MM mode, we must set ETD/TCE */
405         if (mode == DMA_MODE_MM)
406                 psgl->control |= DMA_ETD_OUTPUT | DMA_TCE_ENABLE;
407
408         if (p_dma_ch->int_enable) {
409                 /* Enable channel interrupt */
410                 psgl->control |= DMA_CIE_ENABLE;
411         } else {
412                 psgl->control &= ~DMA_CIE_ENABLE;
413         }
414
415         sg_command = mfdcr(DCRN_ASGC);
416         sg_command |= SSG_MASK_ENABLE(dmanr);
417
418         /* Enable SGL control access */
419         mtdcr(DCRN_ASGC, sg_command);
420         psgl->sgl_control = SG_ERI_ENABLE | SG_LINK;
421
422         /* keep control count register settings */
423         ctc_settings = mfdcr(DCRN_DMACT0 + (dmanr * 0x8))
424                 & (DMA_CTC_BSIZ_MSK | DMA_CTC_BTEN); /*burst mode settings*/
425         psgl->sgl_control |= ctc_settings;
426
427         if (p_dma_ch->int_enable) {
428                 if (p_dma_ch->tce_enable)
429                         psgl->sgl_control |= SG_TCI_ENABLE;
430                 else
431                         psgl->sgl_control |= SG_ETI_ENABLE;
432         }
433
434         *phandle = (sgl_handle_t) psgl;
435         return DMA_STATUS_GOOD;
436 }
437
438 /*
439  * Destroy a scatter/gather list handle that was created by alloc_dma_handle().
440  * The list must be empty (contain no elements).
441  */
442 void
443 ppc4xx_free_dma_handle(sgl_handle_t handle)
444 {
445         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
446
447         if (!handle) {
448                 printk("ppc4xx_free_dma_handle: got NULL\n");
449                 return;
450         } else if (psgl->phead) {
451                 printk("ppc4xx_free_dma_handle: list not empty\n");
452                 return;
453         } else if (!psgl->dma_addr) {   /* should never happen */
454                 printk("ppc4xx_free_dma_handle: no dma address\n");
455                 return;
456         }
457
458         dma_free_coherent(NULL, DMA_PPC4xx_SIZE, (void *) psgl, 0);
459 }
460
461 EXPORT_SYMBOL(ppc4xx_alloc_dma_handle);
462 EXPORT_SYMBOL(ppc4xx_free_dma_handle);
463 EXPORT_SYMBOL(ppc4xx_add_dma_sgl);
464 EXPORT_SYMBOL(ppc4xx_delete_dma_sgl_element);
465 EXPORT_SYMBOL(ppc4xx_enable_dma_sgl);
466 EXPORT_SYMBOL(ppc4xx_disable_dma_sgl);
467 EXPORT_SYMBOL(ppc4xx_get_dma_sgl_residue);