162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * linux/include/asm/dma.h: Defines for using and allocating dma channels.
462306a36Sopenharmony_ci * Written by Hennus Bergman, 1992.
562306a36Sopenharmony_ci * High DMA channel support & info by Hannu Savolainen
662306a36Sopenharmony_ci * and John Boyd, Nov. 1992.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * NOTE: all this is true *only* for ISA/EISA expansions on Mips boards
962306a36Sopenharmony_ci * and can only be used for expansion cards. Onboard DMA controllers, such
1062306a36Sopenharmony_ci * as the R4030 on Jazz boards behave totally different!
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#ifndef _ASM_DMA_H
1462306a36Sopenharmony_ci#define _ASM_DMA_H
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <asm/io.h>			/* need byte IO */
1762306a36Sopenharmony_ci#include <linux/spinlock.h>		/* And spinlocks */
1862306a36Sopenharmony_ci#include <linux/delay.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#ifdef HAVE_REALLY_SLOW_DMA_CONTROLLER
2262306a36Sopenharmony_ci#define dma_outb	outb_p
2362306a36Sopenharmony_ci#else
2462306a36Sopenharmony_ci#define dma_outb	outb
2562306a36Sopenharmony_ci#endif
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define dma_inb		inb
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * NOTES about DMA transfers:
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci *  controller 1: channels 0-3, byte operations, ports 00-1F
3362306a36Sopenharmony_ci *  controller 2: channels 4-7, word operations, ports C0-DF
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci *  - ALL registers are 8 bits only, regardless of transfer size
3662306a36Sopenharmony_ci *  - channel 4 is not used - cascades 1 into 2.
3762306a36Sopenharmony_ci *  - channels 0-3 are byte - addresses/counts are for physical bytes
3862306a36Sopenharmony_ci *  - channels 5-7 are word - addresses/counts are for physical words
3962306a36Sopenharmony_ci *  - transfers must not cross physical 64K (0-3) or 128K (5-7) boundaries
4062306a36Sopenharmony_ci *  - transfer count loaded to registers is 1 less than actual count
4162306a36Sopenharmony_ci *  - controller 2 offsets are all even (2x offsets for controller 1)
4262306a36Sopenharmony_ci *  - page registers for 5-7 don't use data bit 0, represent 128K pages
4362306a36Sopenharmony_ci *  - page registers for 0-3 use bit 0, represent 64K pages
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * DMA transfers are limited to the lower 16MB of _physical_ memory.
4662306a36Sopenharmony_ci * Note that addresses loaded into registers must be _physical_ addresses,
4762306a36Sopenharmony_ci * not logical addresses (which may differ if paging is active).
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci *  Address mapping for channels 0-3:
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci *   A23 ... A16 A15 ... A8  A7 ... A0	  (Physical addresses)
5262306a36Sopenharmony_ci *    |	 ...  |	  |  ... |   |	... |
5362306a36Sopenharmony_ci *    |	 ...  |	  |  ... |   |	... |
5462306a36Sopenharmony_ci *    |	 ...  |	  |  ... |   |	... |
5562306a36Sopenharmony_ci *   P7	 ...  P0  A7 ... A0  A7 ... A0
5662306a36Sopenharmony_ci * |	Page	| Addr MSB | Addr LSB |	  (DMA registers)
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci *  Address mapping for channels 5-7:
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci *   A23 ... A17 A16 A15 ... A9 A8 A7 ... A1 A0	   (Physical addresses)
6162306a36Sopenharmony_ci *    |	 ...  |	  \   \	  ... \	 \  \  ... \  \
6262306a36Sopenharmony_ci *    |	 ...  |	   \   \   ... \  \  \	... \  (not used)
6362306a36Sopenharmony_ci *    |	 ...  |	    \	\   ... \  \  \	 ... \
6462306a36Sopenharmony_ci *   P7	 ...  P1 (0) A7 A6  ... A0 A7 A6 ... A0
6562306a36Sopenharmony_ci * |	  Page	    |  Addr MSB	  |  Addr LSB  |   (DMA registers)
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * Again, channels 5-7 transfer _physical_ words (16 bits), so addresses
6862306a36Sopenharmony_ci * and counts _must_ be word-aligned (the lowest address bit is _ignored_ at
6962306a36Sopenharmony_ci * the hardware level, so odd-byte transfers aren't possible).
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * Transfer count (_not # bytes_) is limited to 64K, represented as actual
7262306a36Sopenharmony_ci * count - 1 : 64K => 0xFFFF, 1 => 0x0000.  Thus, count is always 1 or more,
7362306a36Sopenharmony_ci * and up to 128K bytes may be transferred on channels 5-7 in one operation.
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#ifndef CONFIG_GENERIC_ISA_DMA_SUPPORT_BROKEN
7862306a36Sopenharmony_ci#define MAX_DMA_CHANNELS	8
7962306a36Sopenharmony_ci#endif
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * The maximum address in KSEG0 that we can perform a DMA transfer to on this
8362306a36Sopenharmony_ci * platform.  This describes only the PC style part of the DMA logic like on
8462306a36Sopenharmony_ci * Deskstations or Acer PICA but not the much more versatile DMA logic used
8562306a36Sopenharmony_ci * for the local devices on Acer PICA or Magnums.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ci#if defined(CONFIG_SGI_IP22) || defined(CONFIG_SGI_IP28)
8862306a36Sopenharmony_ci/* don't care; ISA bus master won't work, ISA slave DMA supports 32bit addr */
8962306a36Sopenharmony_ci#define MAX_DMA_ADDRESS		PAGE_OFFSET
9062306a36Sopenharmony_ci#else
9162306a36Sopenharmony_ci#define MAX_DMA_ADDRESS		(PAGE_OFFSET + 0x01000000)
9262306a36Sopenharmony_ci#endif
9362306a36Sopenharmony_ci#define MAX_DMA_PFN		PFN_DOWN(virt_to_phys((void *)MAX_DMA_ADDRESS))
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#ifndef MAX_DMA32_PFN
9662306a36Sopenharmony_ci#define MAX_DMA32_PFN		(1UL << (32 - PAGE_SHIFT))
9762306a36Sopenharmony_ci#endif
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/* 8237 DMA controllers */
10062306a36Sopenharmony_ci#define IO_DMA1_BASE	0x00	/* 8 bit slave DMA, channels 0..3 */
10162306a36Sopenharmony_ci#define IO_DMA2_BASE	0xC0	/* 16 bit master DMA, ch 4(=slave input)..7 */
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci/* DMA controller registers */
10462306a36Sopenharmony_ci#define DMA1_CMD_REG		0x08	/* command register (w) */
10562306a36Sopenharmony_ci#define DMA1_STAT_REG		0x08	/* status register (r) */
10662306a36Sopenharmony_ci#define DMA1_REQ_REG		0x09	/* request register (w) */
10762306a36Sopenharmony_ci#define DMA1_MASK_REG		0x0A	/* single-channel mask (w) */
10862306a36Sopenharmony_ci#define DMA1_MODE_REG		0x0B	/* mode register (w) */
10962306a36Sopenharmony_ci#define DMA1_CLEAR_FF_REG	0x0C	/* clear pointer flip-flop (w) */
11062306a36Sopenharmony_ci#define DMA1_TEMP_REG		0x0D	/* Temporary Register (r) */
11162306a36Sopenharmony_ci#define DMA1_RESET_REG		0x0D	/* Master Clear (w) */
11262306a36Sopenharmony_ci#define DMA1_CLR_MASK_REG	0x0E	/* Clear Mask */
11362306a36Sopenharmony_ci#define DMA1_MASK_ALL_REG	0x0F	/* all-channels mask (w) */
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define DMA2_CMD_REG		0xD0	/* command register (w) */
11662306a36Sopenharmony_ci#define DMA2_STAT_REG		0xD0	/* status register (r) */
11762306a36Sopenharmony_ci#define DMA2_REQ_REG		0xD2	/* request register (w) */
11862306a36Sopenharmony_ci#define DMA2_MASK_REG		0xD4	/* single-channel mask (w) */
11962306a36Sopenharmony_ci#define DMA2_MODE_REG		0xD6	/* mode register (w) */
12062306a36Sopenharmony_ci#define DMA2_CLEAR_FF_REG	0xD8	/* clear pointer flip-flop (w) */
12162306a36Sopenharmony_ci#define DMA2_TEMP_REG		0xDA	/* Temporary Register (r) */
12262306a36Sopenharmony_ci#define DMA2_RESET_REG		0xDA	/* Master Clear (w) */
12362306a36Sopenharmony_ci#define DMA2_CLR_MASK_REG	0xDC	/* Clear Mask */
12462306a36Sopenharmony_ci#define DMA2_MASK_ALL_REG	0xDE	/* all-channels mask (w) */
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci#define DMA_ADDR_0		0x00	/* DMA address registers */
12762306a36Sopenharmony_ci#define DMA_ADDR_1		0x02
12862306a36Sopenharmony_ci#define DMA_ADDR_2		0x04
12962306a36Sopenharmony_ci#define DMA_ADDR_3		0x06
13062306a36Sopenharmony_ci#define DMA_ADDR_4		0xC0
13162306a36Sopenharmony_ci#define DMA_ADDR_5		0xC4
13262306a36Sopenharmony_ci#define DMA_ADDR_6		0xC8
13362306a36Sopenharmony_ci#define DMA_ADDR_7		0xCC
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#define DMA_CNT_0		0x01	/* DMA count registers */
13662306a36Sopenharmony_ci#define DMA_CNT_1		0x03
13762306a36Sopenharmony_ci#define DMA_CNT_2		0x05
13862306a36Sopenharmony_ci#define DMA_CNT_3		0x07
13962306a36Sopenharmony_ci#define DMA_CNT_4		0xC2
14062306a36Sopenharmony_ci#define DMA_CNT_5		0xC6
14162306a36Sopenharmony_ci#define DMA_CNT_6		0xCA
14262306a36Sopenharmony_ci#define DMA_CNT_7		0xCE
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#define DMA_PAGE_0		0x87	/* DMA page registers */
14562306a36Sopenharmony_ci#define DMA_PAGE_1		0x83
14662306a36Sopenharmony_ci#define DMA_PAGE_2		0x81
14762306a36Sopenharmony_ci#define DMA_PAGE_3		0x82
14862306a36Sopenharmony_ci#define DMA_PAGE_5		0x8B
14962306a36Sopenharmony_ci#define DMA_PAGE_6		0x89
15062306a36Sopenharmony_ci#define DMA_PAGE_7		0x8A
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#define DMA_MODE_READ	0x44	/* I/O to memory, no autoinit, increment, single mode */
15362306a36Sopenharmony_ci#define DMA_MODE_WRITE	0x48	/* memory to I/O, no autoinit, increment, single mode */
15462306a36Sopenharmony_ci#define DMA_MODE_CASCADE 0xC0	/* pass thru DREQ->HRQ, DACK<-HLDA only */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#define DMA_AUTOINIT	0x10
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciextern spinlock_t  dma_spin_lock;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic __inline__ unsigned long claim_dma_lock(void)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	unsigned long flags;
16362306a36Sopenharmony_ci	spin_lock_irqsave(&dma_spin_lock, flags);
16462306a36Sopenharmony_ci	return flags;
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic __inline__ void release_dma_lock(unsigned long flags)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	spin_unlock_irqrestore(&dma_spin_lock, flags);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* enable/disable a specific DMA channel */
17362306a36Sopenharmony_cistatic __inline__ void enable_dma(unsigned int dmanr)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	if (dmanr<=3)
17662306a36Sopenharmony_ci		dma_outb(dmanr,	 DMA1_MASK_REG);
17762306a36Sopenharmony_ci	else
17862306a36Sopenharmony_ci		dma_outb(dmanr & 3,  DMA2_MASK_REG);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic __inline__ void disable_dma(unsigned int dmanr)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	if (dmanr<=3)
18462306a36Sopenharmony_ci		dma_outb(dmanr | 4,  DMA1_MASK_REG);
18562306a36Sopenharmony_ci	else
18662306a36Sopenharmony_ci		dma_outb((dmanr & 3) | 4,  DMA2_MASK_REG);
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/* Clear the 'DMA Pointer Flip Flop'.
19062306a36Sopenharmony_ci * Write 0 for LSB/MSB, 1 for MSB/LSB access.
19162306a36Sopenharmony_ci * Use this once to initialize the FF to a known state.
19262306a36Sopenharmony_ci * After that, keep track of it. :-)
19362306a36Sopenharmony_ci * --- In order to do that, the DMA routines below should ---
19462306a36Sopenharmony_ci * --- only be used while holding the DMA lock ! ---
19562306a36Sopenharmony_ci */
19662306a36Sopenharmony_cistatic __inline__ void clear_dma_ff(unsigned int dmanr)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	if (dmanr<=3)
19962306a36Sopenharmony_ci		dma_outb(0,  DMA1_CLEAR_FF_REG);
20062306a36Sopenharmony_ci	else
20162306a36Sopenharmony_ci		dma_outb(0,  DMA2_CLEAR_FF_REG);
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci/* set mode (above) for a specific DMA channel */
20562306a36Sopenharmony_cistatic __inline__ void set_dma_mode(unsigned int dmanr, char mode)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	if (dmanr<=3)
20862306a36Sopenharmony_ci		dma_outb(mode | dmanr,	DMA1_MODE_REG);
20962306a36Sopenharmony_ci	else
21062306a36Sopenharmony_ci		dma_outb(mode | (dmanr&3),  DMA2_MODE_REG);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* Set only the page register bits of the transfer address.
21462306a36Sopenharmony_ci * This is used for successive transfers when we know the contents of
21562306a36Sopenharmony_ci * the lower 16 bits of the DMA current address register, but a 64k boundary
21662306a36Sopenharmony_ci * may have been crossed.
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_cistatic __inline__ void set_dma_page(unsigned int dmanr, char pagenr)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	switch(dmanr) {
22162306a36Sopenharmony_ci		case 0:
22262306a36Sopenharmony_ci			dma_outb(pagenr, DMA_PAGE_0);
22362306a36Sopenharmony_ci			break;
22462306a36Sopenharmony_ci		case 1:
22562306a36Sopenharmony_ci			dma_outb(pagenr, DMA_PAGE_1);
22662306a36Sopenharmony_ci			break;
22762306a36Sopenharmony_ci		case 2:
22862306a36Sopenharmony_ci			dma_outb(pagenr, DMA_PAGE_2);
22962306a36Sopenharmony_ci			break;
23062306a36Sopenharmony_ci		case 3:
23162306a36Sopenharmony_ci			dma_outb(pagenr, DMA_PAGE_3);
23262306a36Sopenharmony_ci			break;
23362306a36Sopenharmony_ci		case 5:
23462306a36Sopenharmony_ci			dma_outb(pagenr & 0xfe, DMA_PAGE_5);
23562306a36Sopenharmony_ci			break;
23662306a36Sopenharmony_ci		case 6:
23762306a36Sopenharmony_ci			dma_outb(pagenr & 0xfe, DMA_PAGE_6);
23862306a36Sopenharmony_ci			break;
23962306a36Sopenharmony_ci		case 7:
24062306a36Sopenharmony_ci			dma_outb(pagenr & 0xfe, DMA_PAGE_7);
24162306a36Sopenharmony_ci			break;
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci}
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci/* Set transfer address & page bits for specific DMA channel.
24762306a36Sopenharmony_ci * Assumes dma flipflop is clear.
24862306a36Sopenharmony_ci */
24962306a36Sopenharmony_cistatic __inline__ void set_dma_addr(unsigned int dmanr, unsigned int a)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci	set_dma_page(dmanr, a>>16);
25262306a36Sopenharmony_ci	if (dmanr <= 3)	 {
25362306a36Sopenharmony_ci	    dma_outb( a & 0xff, ((dmanr&3)<<1) + IO_DMA1_BASE );
25462306a36Sopenharmony_ci	    dma_outb( (a>>8) & 0xff, ((dmanr&3)<<1) + IO_DMA1_BASE );
25562306a36Sopenharmony_ci	}  else	 {
25662306a36Sopenharmony_ci	    dma_outb( (a>>1) & 0xff, ((dmanr&3)<<2) + IO_DMA2_BASE );
25762306a36Sopenharmony_ci	    dma_outb( (a>>9) & 0xff, ((dmanr&3)<<2) + IO_DMA2_BASE );
25862306a36Sopenharmony_ci	}
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci/* Set transfer size (max 64k for DMA0..3, 128k for DMA5..7) for
26362306a36Sopenharmony_ci * a specific DMA channel.
26462306a36Sopenharmony_ci * You must ensure the parameters are valid.
26562306a36Sopenharmony_ci * NOTE: from a manual: "the number of transfers is one more
26662306a36Sopenharmony_ci * than the initial word count"! This is taken into account.
26762306a36Sopenharmony_ci * Assumes dma flip-flop is clear.
26862306a36Sopenharmony_ci * NOTE 2: "count" represents _bytes_ and must be even for channels 5-7.
26962306a36Sopenharmony_ci */
27062306a36Sopenharmony_cistatic __inline__ void set_dma_count(unsigned int dmanr, unsigned int count)
27162306a36Sopenharmony_ci{
27262306a36Sopenharmony_ci	count--;
27362306a36Sopenharmony_ci	if (dmanr <= 3)	 {
27462306a36Sopenharmony_ci	    dma_outb( count & 0xff, ((dmanr&3)<<1) + 1 + IO_DMA1_BASE );
27562306a36Sopenharmony_ci	    dma_outb( (count>>8) & 0xff, ((dmanr&3)<<1) + 1 + IO_DMA1_BASE );
27662306a36Sopenharmony_ci	} else {
27762306a36Sopenharmony_ci	    dma_outb( (count>>1) & 0xff, ((dmanr&3)<<2) + 2 + IO_DMA2_BASE );
27862306a36Sopenharmony_ci	    dma_outb( (count>>9) & 0xff, ((dmanr&3)<<2) + 2 + IO_DMA2_BASE );
27962306a36Sopenharmony_ci	}
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci/* Get DMA residue count. After a DMA transfer, this
28462306a36Sopenharmony_ci * should return zero. Reading this while a DMA transfer is
28562306a36Sopenharmony_ci * still in progress will return unpredictable results.
28662306a36Sopenharmony_ci * If called before the channel has been used, it may return 1.
28762306a36Sopenharmony_ci * Otherwise, it returns the number of _bytes_ left to transfer.
28862306a36Sopenharmony_ci *
28962306a36Sopenharmony_ci * Assumes DMA flip-flop is clear.
29062306a36Sopenharmony_ci */
29162306a36Sopenharmony_cistatic __inline__ int get_dma_residue(unsigned int dmanr)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	unsigned int io_port = (dmanr<=3)? ((dmanr&3)<<1) + 1 + IO_DMA1_BASE
29462306a36Sopenharmony_ci					 : ((dmanr&3)<<2) + 2 + IO_DMA2_BASE;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	/* using short to get 16-bit wrap around */
29762306a36Sopenharmony_ci	unsigned short count;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	count = 1 + dma_inb(io_port);
30062306a36Sopenharmony_ci	count += dma_inb(io_port) << 8;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	return (dmanr<=3)? count : (count<<1);
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci/* These are in kernel/dma.c: */
30762306a36Sopenharmony_ciextern int request_dma(unsigned int dmanr, const char * device_id);	/* reserve a DMA channel */
30862306a36Sopenharmony_ciextern void free_dma(unsigned int dmanr);	/* release it again */
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci#endif /* _ASM_DMA_H */
311