#ifndef _ASM_POWERPC_DMA_H1#define _ASM_POWERPC_DMA_H2#ifdef __KERNEL__34/*5* Defines for using and allocating dma channels.6* Written by Hennus Bergman, 1992.7* High DMA channel support & info by Hannu Savolainen8* and John Boyd, Nov. 1992.9* Changes for ppc sound by Christoph Nadig10*/1112/*13* Note: Adapted for PowerPC by Gary Thomas14* Modified by Cort Dougan <[email protected]>15*16* None of this really applies for Power Macintoshes. There is17* basically just enough here to get kernel/dma.c to compile.18*19* There may be some comments or restrictions made here which are20* not valid for the PReP platform. Take what you read21* with a grain of salt.22*/2324#include <asm/io.h>25#include <linux/spinlock.h>26#include <asm/system.h>2728#ifndef MAX_DMA_CHANNELS29#define MAX_DMA_CHANNELS 830#endif3132/* The maximum address that we can perform a DMA transfer to on this platform */33/* Doesn't really apply... */34#define MAX_DMA_ADDRESS (~0UL)3536#if !defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI)3738#ifdef HAVE_REALLY_SLOW_DMA_CONTROLLER39#define dma_outb outb_p40#else41#define dma_outb outb42#endif4344#define dma_inb inb4546/*47* NOTES about DMA transfers:48*49* controller 1: channels 0-3, byte operations, ports 00-1F50* controller 2: channels 4-7, word operations, ports C0-DF51*52* - ALL registers are 8 bits only, regardless of transfer size53* - channel 4 is not used - cascades 1 into 2.54* - channels 0-3 are byte - addresses/counts are for physical bytes55* - channels 5-7 are word - addresses/counts are for physical words56* - transfers must not cross physical 64K (0-3) or 128K (5-7) boundaries57* - transfer count loaded to registers is 1 less than actual count58* - controller 2 offsets are all even (2x offsets for controller 1)59* - page registers for 5-7 don't use data bit 0, represent 128K pages60* - page registers for 0-3 use bit 0, represent 64K pages61*62* On PReP, DMA transfers are limited to the lower 16MB of _physical_ memory.63* On CHRP, the W83C553F (and VLSI Tollgate?) support full 32 bit addressing.64* Note that addresses loaded into registers must be _physical_ addresses,65* not logical addresses (which may differ if paging is active).66*67* Address mapping for channels 0-3:68*69* A23 ... A16 A15 ... A8 A7 ... A0 (Physical addresses)70* | ... | | ... | | ... |71* | ... | | ... | | ... |72* | ... | | ... | | ... |73* P7 ... P0 A7 ... A0 A7 ... A074* | Page | Addr MSB | Addr LSB | (DMA registers)75*76* Address mapping for channels 5-7:77*78* A23 ... A17 A16 A15 ... A9 A8 A7 ... A1 A0 (Physical addresses)79* | ... | \ \ ... \ \ \ ... \ \80* | ... | \ \ ... \ \ \ ... \ (not used)81* | ... | \ \ ... \ \ \ ... \82* P7 ... P1 (0) A7 A6 ... A0 A7 A6 ... A083* | Page | Addr MSB | Addr LSB | (DMA registers)84*85* Again, channels 5-7 transfer _physical_ words (16 bits), so addresses86* and counts _must_ be word-aligned (the lowest address bit is _ignored_ at87* the hardware level, so odd-byte transfers aren't possible).88*89* Transfer count (_not # bytes_) is limited to 64K, represented as actual90* count - 1 : 64K => 0xFFFF, 1 => 0x0000. Thus, count is always 1 or more,91* and up to 128K bytes may be transferred on channels 5-7 in one operation.92*93*/9495/* 8237 DMA controllers */96#define IO_DMA1_BASE 0x00 /* 8 bit slave DMA, channels 0..3 */97#define IO_DMA2_BASE 0xC0 /* 16 bit master DMA, ch 4(=slave input)..7 */9899/* DMA controller registers */100#define DMA1_CMD_REG 0x08 /* command register (w) */101#define DMA1_STAT_REG 0x08 /* status register (r) */102#define DMA1_REQ_REG 0x09 /* request register (w) */103#define DMA1_MASK_REG 0x0A /* single-channel mask (w) */104#define DMA1_MODE_REG 0x0B /* mode register (w) */105#define DMA1_CLEAR_FF_REG 0x0C /* clear pointer flip-flop (w) */106#define DMA1_TEMP_REG 0x0D /* Temporary Register (r) */107#define DMA1_RESET_REG 0x0D /* Master Clear (w) */108#define DMA1_CLR_MASK_REG 0x0E /* Clear Mask */109#define DMA1_MASK_ALL_REG 0x0F /* all-channels mask (w) */110111#define DMA2_CMD_REG 0xD0 /* command register (w) */112#define DMA2_STAT_REG 0xD0 /* status register (r) */113#define DMA2_REQ_REG 0xD2 /* request register (w) */114#define DMA2_MASK_REG 0xD4 /* single-channel mask (w) */115#define DMA2_MODE_REG 0xD6 /* mode register (w) */116#define DMA2_CLEAR_FF_REG 0xD8 /* clear pointer flip-flop (w) */117#define DMA2_TEMP_REG 0xDA /* Temporary Register (r) */118#define DMA2_RESET_REG 0xDA /* Master Clear (w) */119#define DMA2_CLR_MASK_REG 0xDC /* Clear Mask */120#define DMA2_MASK_ALL_REG 0xDE /* all-channels mask (w) */121122#define DMA_ADDR_0 0x00 /* DMA address registers */123#define DMA_ADDR_1 0x02124#define DMA_ADDR_2 0x04125#define DMA_ADDR_3 0x06126#define DMA_ADDR_4 0xC0127#define DMA_ADDR_5 0xC4128#define DMA_ADDR_6 0xC8129#define DMA_ADDR_7 0xCC130131#define DMA_CNT_0 0x01 /* DMA count registers */132#define DMA_CNT_1 0x03133#define DMA_CNT_2 0x05134#define DMA_CNT_3 0x07135#define DMA_CNT_4 0xC2136#define DMA_CNT_5 0xC6137#define DMA_CNT_6 0xCA138#define DMA_CNT_7 0xCE139140#define DMA_LO_PAGE_0 0x87 /* DMA page registers */141#define DMA_LO_PAGE_1 0x83142#define DMA_LO_PAGE_2 0x81143#define DMA_LO_PAGE_3 0x82144#define DMA_LO_PAGE_5 0x8B145#define DMA_LO_PAGE_6 0x89146#define DMA_LO_PAGE_7 0x8A147148#define DMA_HI_PAGE_0 0x487 /* DMA page registers */149#define DMA_HI_PAGE_1 0x483150#define DMA_HI_PAGE_2 0x481151#define DMA_HI_PAGE_3 0x482152#define DMA_HI_PAGE_5 0x48B153#define DMA_HI_PAGE_6 0x489154#define DMA_HI_PAGE_7 0x48A155156#define DMA1_EXT_REG 0x40B157#define DMA2_EXT_REG 0x4D6158159#ifndef __powerpc64__160/* in arch/ppc/kernel/setup.c -- Cort */161extern unsigned int DMA_MODE_WRITE;162extern unsigned int DMA_MODE_READ;163extern unsigned long ISA_DMA_THRESHOLD;164#else165#define DMA_MODE_READ 0x44 /* I/O to memory, no autoinit, increment, single mode */166#define DMA_MODE_WRITE 0x48 /* memory to I/O, no autoinit, increment, single mode */167#endif168169#define DMA_MODE_CASCADE 0xC0 /* pass thru DREQ->HRQ, DACK<-HLDA only */170171#define DMA_AUTOINIT 0x10172173extern spinlock_t dma_spin_lock;174175static __inline__ unsigned long claim_dma_lock(void)176{177unsigned long flags;178spin_lock_irqsave(&dma_spin_lock, flags);179return flags;180}181182static __inline__ void release_dma_lock(unsigned long flags)183{184spin_unlock_irqrestore(&dma_spin_lock, flags);185}186187/* enable/disable a specific DMA channel */188static __inline__ void enable_dma(unsigned int dmanr)189{190unsigned char ucDmaCmd = 0x00;191192if (dmanr != 4) {193dma_outb(0, DMA2_MASK_REG); /* This may not be enabled */194dma_outb(ucDmaCmd, DMA2_CMD_REG); /* Enable group */195}196if (dmanr <= 3) {197dma_outb(dmanr, DMA1_MASK_REG);198dma_outb(ucDmaCmd, DMA1_CMD_REG); /* Enable group */199} else {200dma_outb(dmanr & 3, DMA2_MASK_REG);201}202}203204static __inline__ void disable_dma(unsigned int dmanr)205{206if (dmanr <= 3)207dma_outb(dmanr | 4, DMA1_MASK_REG);208else209dma_outb((dmanr & 3) | 4, DMA2_MASK_REG);210}211212/* Clear the 'DMA Pointer Flip Flop'.213* Write 0 for LSB/MSB, 1 for MSB/LSB access.214* Use this once to initialize the FF to a known state.215* After that, keep track of it. :-)216* --- In order to do that, the DMA routines below should ---217* --- only be used while interrupts are disabled! ---218*/219static __inline__ void clear_dma_ff(unsigned int dmanr)220{221if (dmanr <= 3)222dma_outb(0, DMA1_CLEAR_FF_REG);223else224dma_outb(0, DMA2_CLEAR_FF_REG);225}226227/* set mode (above) for a specific DMA channel */228static __inline__ void set_dma_mode(unsigned int dmanr, char mode)229{230if (dmanr <= 3)231dma_outb(mode | dmanr, DMA1_MODE_REG);232else233dma_outb(mode | (dmanr & 3), DMA2_MODE_REG);234}235236/* Set only the page register bits of the transfer address.237* This is used for successive transfers when we know the contents of238* the lower 16 bits of the DMA current address register, but a 64k boundary239* may have been crossed.240*/241static __inline__ void set_dma_page(unsigned int dmanr, int pagenr)242{243switch (dmanr) {244case 0:245dma_outb(pagenr, DMA_LO_PAGE_0);246dma_outb(pagenr >> 8, DMA_HI_PAGE_0);247break;248case 1:249dma_outb(pagenr, DMA_LO_PAGE_1);250dma_outb(pagenr >> 8, DMA_HI_PAGE_1);251break;252case 2:253dma_outb(pagenr, DMA_LO_PAGE_2);254dma_outb(pagenr >> 8, DMA_HI_PAGE_2);255break;256case 3:257dma_outb(pagenr, DMA_LO_PAGE_3);258dma_outb(pagenr >> 8, DMA_HI_PAGE_3);259break;260case 5:261dma_outb(pagenr & 0xfe, DMA_LO_PAGE_5);262dma_outb(pagenr >> 8, DMA_HI_PAGE_5);263break;264case 6:265dma_outb(pagenr & 0xfe, DMA_LO_PAGE_6);266dma_outb(pagenr >> 8, DMA_HI_PAGE_6);267break;268case 7:269dma_outb(pagenr & 0xfe, DMA_LO_PAGE_7);270dma_outb(pagenr >> 8, DMA_HI_PAGE_7);271break;272}273}274275/* Set transfer address & page bits for specific DMA channel.276* Assumes dma flipflop is clear.277*/278static __inline__ void set_dma_addr(unsigned int dmanr, unsigned int phys)279{280if (dmanr <= 3) {281dma_outb(phys & 0xff,282((dmanr & 3) << 1) + IO_DMA1_BASE);283dma_outb((phys >> 8) & 0xff,284((dmanr & 3) << 1) + IO_DMA1_BASE);285} else {286dma_outb((phys >> 1) & 0xff,287((dmanr & 3) << 2) + IO_DMA2_BASE);288dma_outb((phys >> 9) & 0xff,289((dmanr & 3) << 2) + IO_DMA2_BASE);290}291set_dma_page(dmanr, phys >> 16);292}293294295/* Set transfer size (max 64k for DMA1..3, 128k for DMA5..7) for296* a specific DMA channel.297* You must ensure the parameters are valid.298* NOTE: from a manual: "the number of transfers is one more299* than the initial word count"! This is taken into account.300* Assumes dma flip-flop is clear.301* NOTE 2: "count" represents _bytes_ and must be even for channels 5-7.302*/303static __inline__ void set_dma_count(unsigned int dmanr, unsigned int count)304{305count--;306if (dmanr <= 3) {307dma_outb(count & 0xff,308((dmanr & 3) << 1) + 1 + IO_DMA1_BASE);309dma_outb((count >> 8) & 0xff,310((dmanr & 3) << 1) + 1 + IO_DMA1_BASE);311} else {312dma_outb((count >> 1) & 0xff,313((dmanr & 3) << 2) + 2 + IO_DMA2_BASE);314dma_outb((count >> 9) & 0xff,315((dmanr & 3) << 2) + 2 + IO_DMA2_BASE);316}317}318319320/* Get DMA residue count. After a DMA transfer, this321* should return zero. Reading this while a DMA transfer is322* still in progress will return unpredictable results.323* If called before the channel has been used, it may return 1.324* Otherwise, it returns the number of _bytes_ left to transfer.325*326* Assumes DMA flip-flop is clear.327*/328static __inline__ int get_dma_residue(unsigned int dmanr)329{330unsigned int io_port = (dmanr <= 3)331? ((dmanr & 3) << 1) + 1 + IO_DMA1_BASE332: ((dmanr & 3) << 2) + 2 + IO_DMA2_BASE;333334/* using short to get 16-bit wrap around */335unsigned short count;336337count = 1 + dma_inb(io_port);338count += dma_inb(io_port) << 8;339340return (dmanr <= 3) ? count : (count << 1);341}342343/* These are in kernel/dma.c: */344345/* reserve a DMA channel */346extern int request_dma(unsigned int dmanr, const char *device_id);347/* release it again */348extern void free_dma(unsigned int dmanr);349350#ifdef CONFIG_PCI351extern int isa_dma_bridge_buggy;352#else353#define isa_dma_bridge_buggy (0)354#endif355356#endif /* !defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI) */357358#endif /* __KERNEL__ */359#endif /* _ASM_POWERPC_DMA_H */360361362