2 * Author: Pete Popov <ppopov@mvista.com> or source@mvista.com
4 * arch/ppc/kernel/ppc405_dma.c
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
11 * IBM 405 DMA Controller Functions
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <asm/system.h>
19 #include <linux/miscdevice.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
23 #include <asm/ppc405_dma.h>
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);
39 ppc_dma_ch_t dma_channels[MAX_405GP_DMA_CHANNELS];
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
50 int hw_init_dma_channel(unsigned int dmanr, ppc_dma_ch_t *p_init)
52 unsigned int polarity;
54 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
58 printk("hw_init_dma_channel: NULL p_init\n");
59 return DMA_STATUS_NULL_POINTER;
61 if (dmanr >= MAX_405GP_DMA_CHANNELS) {
62 printk("hw_init_dma_channel: bad channel %d\n", dmanr);
63 return DMA_STATUS_BAD_CHANNEL;
68 polarity = mfdcr(DCRN_POL);
73 /* Setup the control register based on the values passed to
74 * us in p_init. Then, over-write the control register with this
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 */
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;
100 mtdcr(DCRN_POL, polarity);
102 mtdcr(DCRN_DMACR0, control);
105 polarity &= ~(DMAReq1_ActiveLow | DMAAck1_ActiveLow | EOT1_ActiveLow);
106 polarity |= p_dma_ch->polarity;
108 mtdcr(DCRN_POL, polarity);
110 mtdcr(DCRN_DMACR1, control);
113 polarity &= ~(DMAReq2_ActiveLow | DMAAck2_ActiveLow | EOT2_ActiveLow);
114 polarity |= p_dma_ch->polarity;
116 mtdcr(DCRN_POL, polarity);
118 mtdcr(DCRN_DMACR2, control);
121 polarity &= ~(DMAReq3_ActiveLow | DMAAck3_ActiveLow | EOT3_ActiveLow);
122 polarity |= p_dma_ch->polarity;
124 mtdcr(DCRN_POL, polarity);
126 mtdcr(DCRN_DMACR3, control);
129 return DMA_STATUS_BAD_CHANNEL;
132 /* save these values in our dma channel structure */
133 memcpy(p_dma_ch, p_init, sizeof(ppc_dma_ch_t));
136 * The peripheral width values written in the control register are:
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.
147 p_dma_ch->shift = p_init->pwidth;
150 * Save the control word for easy access.
152 p_dma_ch->control = control;
154 mtdcr(DCRN_DMASR, 0xffffffff); /* clear status register */
155 return DMA_STATUS_GOOD;
162 * This function returns the channel configuration.
164 int get_channel_config(unsigned int dmanr, ppc_dma_ch_t *p_dma_ch)
166 unsigned int polarity;
167 unsigned int control;
170 polarity = mfdcr(DCRN_POL);
178 polarity & (DMAReq0_ActiveLow | DMAAck0_ActiveLow | EOT0_ActiveLow);
179 control = mfdcr(DCRN_DMACR0);
183 polarity & (DMAReq1_ActiveLow | DMAAck1_ActiveLow | EOT1_ActiveLow);
184 control = mfdcr(DCRN_DMACR1);
188 polarity & (DMAReq2_ActiveLow | DMAAck2_ActiveLow | EOT2_ActiveLow);
189 control = mfdcr(DCRN_DMACR2);
193 polarity & (DMAReq3_ActiveLow | DMAAck3_ActiveLow | EOT3_ActiveLow);
194 control = mfdcr(DCRN_DMACR3);
197 return DMA_STATUS_BAD_CHANNEL;
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);
209 return DMA_STATUS_GOOD;
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.
217 * Acceptable priorities:
225 int set_channel_priority(unsigned int dmanr, unsigned int priority)
227 unsigned int control;
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);
240 control = mfdcr(DCRN_DMACR0);
241 control|= SET_DMA_PRIORITY(priority);
242 mtdcr(DCRN_DMACR0, control);
245 control = mfdcr(DCRN_DMACR1);
246 control|= SET_DMA_PRIORITY(priority);
247 mtdcr(DCRN_DMACR1, control);
250 control = mfdcr(DCRN_DMACR2);
251 control|= SET_DMA_PRIORITY(priority);
252 mtdcr(DCRN_DMACR2, control);
255 control = mfdcr(DCRN_DMACR3);
256 control|= SET_DMA_PRIORITY(priority);
257 mtdcr(DCRN_DMACR3, control);
261 printk("set_channel_priority: bad channel: %d\n", dmanr);
263 return DMA_STATUS_BAD_CHANNEL;
265 return DMA_STATUS_GOOD;
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.
275 * The return value is one of:
281 * The function returns 0 on error.
283 unsigned int get_peripheral_width(unsigned int dmanr)
285 unsigned int control;
289 control = mfdcr(DCRN_DMACR0);
292 control = mfdcr(DCRN_DMACR1);
295 control = mfdcr(DCRN_DMACR2);
298 control = mfdcr(DCRN_DMACR3);
302 printk("get_peripheral_width: bad channel: %d\n", dmanr);
306 return(GET_DMA_PW(control));
313 * Create a scatter/gather list handle. This is simply a structure which
314 * describes a scatter/gather list.
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.
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.
340 int alloc_dma_handle(sgl_handle_t *phandle, unsigned int mode, unsigned int dmanr)
342 sgl_list_info_t *psgl;
344 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
350 printk("alloc_dma_handle: null handle pointer\n");
351 return DMA_STATUS_NULL_POINTER;
357 case DMA_MODE_MM_DEVATSRC:
358 case DMA_MODE_MM_DEVATDST:
361 printk("alloc_dma_handle: bad mode 0x%x\n", mode);
362 return DMA_STATUS_BAD_MODE;
364 if (dmanr >= MAX_405GP_DMA_CHANNELS) {
365 printk("alloc_dma_handle: invalid channel 0x%x\n", dmanr);
366 return DMA_STATUS_BAD_CHANNEL;
370 /* Get a page of memory, which is zeroed out by pci_alloc_consistent() */
372 /* wrong not a pci device - armin */
373 /* psgl = (sgl_list_info_t *) pci_alloc_consistent(NULL, SGL_LIST_SIZE, &dma_addr);
376 ret = consistent_alloc(GFP_ATOMIC |GFP_DMA, SGL_LIST_SIZE, &dma_addr);
378 memset(ret, 0,SGL_LIST_SIZE );
379 psgl = (sgl_list_info_t *) ret;
384 *phandle = (sgl_handle_t)NULL;
385 return DMA_STATUS_OUT_OF_MEMORY;
388 psgl->dma_addr = dma_addr;
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.
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 */
400 if (p_dma_ch->int_enable) {
401 psgl->control |= DMA_CIE_ENABLE; /* channel interrupt enabled */
404 psgl->control &= ~DMA_CIE_ENABLE;
408 sg_command = mfdcr(DCRN_ASGC);
411 sg_command |= SSG0_MASK_ENABLE;
414 sg_command |= SSG1_MASK_ENABLE;
417 sg_command |= SSG2_MASK_ENABLE;
420 sg_command |= SSG3_MASK_ENABLE;
424 printk("alloc_dma_handle: bad channel: %d\n", dmanr);
426 free_dma_handle((sgl_handle_t)psgl);
427 *phandle = (sgl_handle_t)NULL;
428 return DMA_STATUS_BAD_CHANNEL;
431 mtdcr(DCRN_ASGC, sg_command); /* enable writing to this channel's sgl control bits */
435 psgl->sgl_control = SG_ERI_ENABLE | SG_LINK; /* sgl descriptor control bits */
437 if (p_dma_ch->int_enable) {
438 if (p_dma_ch->tce_enable)
439 psgl->sgl_control |= SG_TCI_ENABLE;
441 psgl->sgl_control |= SG_ETI_ENABLE;
444 *phandle = (sgl_handle_t)psgl;
445 return DMA_STATUS_GOOD;
451 * Destroy a scatter/gather list handle that was created by alloc_dma_handle().
452 * The list must be empty (contain no elements).
454 void free_dma_handle(sgl_handle_t handle)
456 sgl_list_info_t *psgl = (sgl_list_info_t *)handle;
460 printk("free_dma_handle: got NULL\n");
464 else if (psgl->phead) {
466 printk("free_dma_handle: list not empty\n");
470 else if (!psgl->dma_addr) { /* should never happen */
472 printk("free_dma_handle: no dma address\n");
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);
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);