This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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                 psgl->ptail->next = psgl->ptail_dma + sizeof(ppc_sgl_t);
124                 psgl->ptail++;
125                 psgl->ptail_dma += sizeof(ppc_sgl_t);
126         }
127
128         psgl->ptail->control = psgl->control;
129         psgl->ptail->src_addr = src_addr;
130         psgl->ptail->dst_addr = dst_addr;
131         psgl->ptail->control_count = (count >> p_dma_ch->shift) |
132             psgl->sgl_control;
133         psgl->ptail->next = (uint32_t) NULL;
134
135         return DMA_STATUS_GOOD;
136 }
137
138 /*
139  * Enable (start) the DMA described by the sgl handle.
140  */
141 void
142 ppc4xx_enable_dma_sgl(sgl_handle_t handle)
143 {
144         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
145         ppc_dma_ch_t *p_dma_ch;
146         uint32_t sg_command;
147
148         if (!handle) {
149                 printk("ppc4xx_enable_dma_sgl: null handle\n");
150                 return;
151         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
152                 printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
153                        psgl->dmanr);
154                 return;
155         } else if (!psgl->phead) {
156                 printk("ppc4xx_enable_dma_sgl: sg list empty\n");
157                 return;
158         }
159
160         p_dma_ch = &dma_channels[psgl->dmanr];
161         psgl->ptail->control_count &= ~SG_LINK; /* make this the last dscrptr */
162         sg_command = mfdcr(DCRN_ASGC);
163
164         ppc4xx_set_sg_addr(psgl->dmanr, psgl->phead_dma);
165
166         sg_command |= SSG_ENABLE(psgl->dmanr);
167
168         mtdcr(DCRN_ASGC, sg_command);   /* start transfer */
169 }
170
171 /*
172  * Halt an active scatter/gather DMA operation.
173  */
174 void
175 ppc4xx_disable_dma_sgl(sgl_handle_t handle)
176 {
177         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
178         uint32_t sg_command;
179
180         if (!handle) {
181                 printk("ppc4xx_enable_dma_sgl: null handle\n");
182                 return;
183         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
184                 printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
185                        psgl->dmanr);
186                 return;
187         }
188
189         sg_command = mfdcr(DCRN_ASGC);
190         sg_command &= ~SSG_ENABLE(psgl->dmanr);
191         mtdcr(DCRN_ASGC, sg_command);   /* stop transfer */
192 }
193
194 /*
195  *  Returns number of bytes left to be transferred from the entire sgl list.
196  *  *src_addr and *dst_addr get set to the source/destination address of
197  *  the sgl descriptor where the DMA stopped.
198  *
199  *  An sgl transfer must NOT be active when this function is called.
200  */
201 int
202 ppc4xx_get_dma_sgl_residue(sgl_handle_t handle, phys_addr_t * src_addr,
203                            phys_addr_t * dst_addr)
204 {
205         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
206         ppc_dma_ch_t *p_dma_ch;
207         ppc_sgl_t *pnext, *sgl_addr;
208         uint32_t count_left;
209
210         if (!handle) {
211                 printk("ppc4xx_get_dma_sgl_residue: null handle\n");
212                 return DMA_STATUS_BAD_HANDLE;
213         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
214                 printk("ppc4xx_get_dma_sgl_residue: bad channel in handle %d\n",
215                        psgl->dmanr);
216                 return DMA_STATUS_BAD_CHANNEL;
217         }
218
219         sgl_addr = (ppc_sgl_t *) __va(mfdcr(DCRN_ASG0 + (psgl->dmanr * 0x8)));
220         count_left = mfdcr(DCRN_DMACT0 + (psgl->dmanr * 0x8));
221
222         if (!sgl_addr) {
223                 printk("ppc4xx_get_dma_sgl_residue: sgl addr register is null\n");
224                 goto error;
225         }
226
227         pnext = psgl->phead;
228         while (pnext &&
229                ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE) &&
230                 (pnext != sgl_addr))
231             ) {
232                 pnext++;
233         }
234
235         if (pnext == sgl_addr) {        /* found the sgl descriptor */
236
237                 *src_addr = pnext->src_addr;
238                 *dst_addr = pnext->dst_addr;
239
240                 /*
241                  * Now search the remaining descriptors and add their count.
242                  * We already have the remaining count from this descriptor in
243                  * count_left.
244                  */
245                 pnext++;
246
247                 while ((pnext != psgl->ptail) &&
248                        ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE))
249                     ) {
250                         count_left += pnext->control_count & SG_COUNT_MASK;
251                 }
252
253                 if (pnext != psgl->ptail) {     /* should never happen */
254                         printk
255                             ("ppc4xx_get_dma_sgl_residue error (1) psgl->ptail 0x%x handle 0x%x\n",
256                              (unsigned int) psgl->ptail, (unsigned int) handle);
257                         goto error;
258                 }
259
260                 /* success */
261                 p_dma_ch = &dma_channels[psgl->dmanr];
262                 return (count_left << p_dma_ch->shift); /* count in bytes */
263
264         } else {
265                 /* this shouldn't happen */
266                 printk
267                     ("get_dma_sgl_residue, unable to match current address 0x%x, handle 0x%x\n",
268                      (unsigned int) sgl_addr, (unsigned int) handle);
269
270         }
271
272       error:
273         *src_addr = (phys_addr_t) NULL;
274         *dst_addr = (phys_addr_t) NULL;
275         return 0;
276 }
277
278 /*
279  * Returns the address(es) of the buffer(s) contained in the head element of
280  * the scatter/gather list.  The element is removed from the scatter/gather
281  * list and the next element becomes the head.
282  *
283  * This function should only be called when the DMA is not active.
284  */
285 int
286 ppc4xx_delete_dma_sgl_element(sgl_handle_t handle, phys_addr_t * src_dma_addr,
287                               phys_addr_t * dst_dma_addr)
288 {
289         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
290
291         if (!handle) {
292                 printk("ppc4xx_delete_sgl_element: null handle\n");
293                 return DMA_STATUS_BAD_HANDLE;
294         } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
295                 printk("ppc4xx_delete_sgl_element: bad channel in handle %d\n",
296                        psgl->dmanr);
297                 return DMA_STATUS_BAD_CHANNEL;
298         }
299
300         if (!psgl->phead) {
301                 printk("ppc4xx_delete_sgl_element: sgl list empty\n");
302                 *src_dma_addr = (phys_addr_t) NULL;
303                 *dst_dma_addr = (phys_addr_t) NULL;
304                 return DMA_STATUS_SGL_LIST_EMPTY;
305         }
306
307         *src_dma_addr = (phys_addr_t) psgl->phead->src_addr;
308         *dst_dma_addr = (phys_addr_t) psgl->phead->dst_addr;
309
310         if (psgl->phead == psgl->ptail) {
311                 /* last descriptor on the list */
312                 psgl->phead = NULL;
313                 psgl->ptail = NULL;
314         } else {
315                 psgl->phead++;
316                 psgl->phead_dma += sizeof(ppc_sgl_t);
317         }
318
319         return DMA_STATUS_GOOD;
320 }
321
322
323 /*
324  *   Create a scatter/gather list handle.  This is simply a structure which
325  *   describes a scatter/gather list.
326  *
327  *   A handle is returned in "handle" which the driver should save in order to
328  *   be able to access this list later.  A chunk of memory will be allocated
329  *   to be used by the API for internal management purposes, including managing
330  *   the sg list and allocating memory for the sgl descriptors.  One page should
331  *   be more than enough for that purpose.  Perhaps it's a bit wasteful to use
332  *   a whole page for a single sg list, but most likely there will be only one
333  *   sg list per channel.
334  *
335  *   Interrupt notes:
336  *   Each sgl descriptor has a copy of the DMA control word which the DMA engine
337  *   loads in the control register.  The control word has a "global" interrupt
338  *   enable bit for that channel. Interrupts are further qualified by a few bits
339  *   in the sgl descriptor count register.  In order to setup an sgl, we have to
340  *   know ahead of time whether or not interrupts will be enabled at the completion
341  *   of the transfers.  Thus, enable_dma_interrupt()/disable_dma_interrupt() MUST
342  *   be called before calling alloc_dma_handle().  If the interrupt mode will never
343  *   change after powerup, then enable_dma_interrupt()/disable_dma_interrupt()
344  *   do not have to be called -- interrupts will be enabled or disabled based
345  *   on how the channel was configured after powerup by the hw_init_dma_channel()
346  *   function.  Each sgl descriptor will be setup to interrupt if an error occurs;
347  *   however, only the last descriptor will be setup to interrupt. Thus, an
348  *   interrupt will occur (if interrupts are enabled) only after the complete
349  *   sgl transfer is done.
350  */
351 int
352 ppc4xx_alloc_dma_handle(sgl_handle_t * phandle, unsigned int mode, unsigned int dmanr)
353 {
354         sgl_list_info_t *psgl;
355         dma_addr_t dma_addr;
356         ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
357         uint32_t sg_command;
358         void *ret;
359
360         if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
361                 printk("ppc4xx_alloc_dma_handle: invalid channel 0x%x\n", dmanr);
362                 return DMA_STATUS_BAD_CHANNEL;
363         }
364
365         if (!phandle) {
366                 printk("ppc4xx_alloc_dma_handle: null handle pointer\n");
367                 return DMA_STATUS_NULL_POINTER;
368         }
369
370         /* Get a page of memory, which is zeroed out by consistent_alloc() */
371         ret = dma_alloc_coherent(NULL, DMA_PPC4xx_SIZE, &dma_addr, GFP_KERNEL);
372         if (ret != NULL) {
373                 memset(ret, 0, DMA_PPC4xx_SIZE);
374                 psgl = (sgl_list_info_t *) ret;
375         }
376
377         if (psgl == NULL) {
378                 *phandle = (sgl_handle_t) NULL;
379                 return DMA_STATUS_OUT_OF_MEMORY;
380         }
381
382         psgl->dma_addr = dma_addr;
383         psgl->dmanr = dmanr;
384
385         /*
386          * Modify and save the control word. These words will be
387          * written to each sgl descriptor.  The DMA engine then
388          * loads this control word into the control register
389          * every time it reads a new descriptor.
390          */
391         psgl->control = p_dma_ch->control;
392         /* Clear all mode bits */
393         psgl->control &= ~(DMA_TM_MASK | DMA_TD);
394         /* Save control word and mode */
395         psgl->control |= (mode | DMA_CE_ENABLE);
396
397         /* In MM mode, we must set ETD/TCE */
398         if (mode == DMA_MODE_MM)
399                 psgl->control |= DMA_ETD_OUTPUT | DMA_TCE_ENABLE;
400
401         if (p_dma_ch->int_enable) {
402                 /* Enable channel interrupt */
403                 psgl->control |= DMA_CIE_ENABLE;
404         } else {
405                 psgl->control &= ~DMA_CIE_ENABLE;
406         }
407
408         sg_command = mfdcr(DCRN_ASGC);
409         sg_command |= SSG_MASK_ENABLE(dmanr);
410
411         /* Enable SGL control access */
412         mtdcr(DCRN_ASGC, sg_command);
413         psgl->sgl_control = SG_ERI_ENABLE | SG_LINK;
414
415         if (p_dma_ch->int_enable) {
416                 if (p_dma_ch->tce_enable)
417                         psgl->sgl_control |= SG_TCI_ENABLE;
418                 else
419                         psgl->sgl_control |= SG_ETI_ENABLE;
420         }
421
422         *phandle = (sgl_handle_t) psgl;
423         return DMA_STATUS_GOOD;
424 }
425
426 /*
427  * Destroy a scatter/gather list handle that was created by alloc_dma_handle().
428  * The list must be empty (contain no elements).
429  */
430 void
431 ppc4xx_free_dma_handle(sgl_handle_t handle)
432 {
433         sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
434
435         if (!handle) {
436                 printk("ppc4xx_free_dma_handle: got NULL\n");
437                 return;
438         } else if (psgl->phead) {
439                 printk("ppc4xx_free_dma_handle: list not empty\n");
440                 return;
441         } else if (!psgl->dma_addr) {   /* should never happen */
442                 printk("ppc4xx_free_dma_handle: no dma address\n");
443                 return;
444         }
445
446         dma_free_coherent(NULL, DMA_PPC4xx_SIZE, (void *) psgl, 0);
447 }
448
449 EXPORT_SYMBOL(ppc4xx_alloc_dma_handle);
450 EXPORT_SYMBOL(ppc4xx_free_dma_handle);
451 EXPORT_SYMBOL(ppc4xx_add_dma_sgl);
452 EXPORT_SYMBOL(ppc4xx_delete_dma_sgl_element);
453 EXPORT_SYMBOL(ppc4xx_enable_dma_sgl);
454 EXPORT_SYMBOL(ppc4xx_disable_dma_sgl);
455 EXPORT_SYMBOL(ppc4xx_get_dma_sgl_residue);