xref: /openbmc/linux/drivers/net/wireless/ti/wl1251/io.c (revision 45474475)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
290921014SLuciano Coelho /*
390921014SLuciano Coelho  * This file is part of wl12xx
490921014SLuciano Coelho  *
590921014SLuciano Coelho  * Copyright (C) 2008 Nokia Corporation
690921014SLuciano Coelho  */
790921014SLuciano Coelho 
890921014SLuciano Coelho #include "wl1251.h"
990921014SLuciano Coelho #include "reg.h"
1090921014SLuciano Coelho #include "io.h"
1190921014SLuciano Coelho 
1290921014SLuciano Coelho /* FIXME: this is static data nowadays and the table can be removed */
1390921014SLuciano Coelho static enum wl12xx_acx_int_reg wl1251_io_reg_table[ACX_REG_TABLE_LEN] = {
1490921014SLuciano Coelho 	[ACX_REG_INTERRUPT_TRIG]     = (REGISTERS_BASE + 0x0474),
1590921014SLuciano Coelho 	[ACX_REG_INTERRUPT_TRIG_H]   = (REGISTERS_BASE + 0x0478),
1690921014SLuciano Coelho 	[ACX_REG_INTERRUPT_MASK]     = (REGISTERS_BASE + 0x0494),
1790921014SLuciano Coelho 	[ACX_REG_HINT_MASK_SET]      = (REGISTERS_BASE + 0x0498),
1890921014SLuciano Coelho 	[ACX_REG_HINT_MASK_CLR]      = (REGISTERS_BASE + 0x049C),
1990921014SLuciano Coelho 	[ACX_REG_INTERRUPT_NO_CLEAR] = (REGISTERS_BASE + 0x04B0),
2090921014SLuciano Coelho 	[ACX_REG_INTERRUPT_CLEAR]    = (REGISTERS_BASE + 0x04A4),
2190921014SLuciano Coelho 	[ACX_REG_INTERRUPT_ACK]      = (REGISTERS_BASE + 0x04A8),
2290921014SLuciano Coelho 	[ACX_REG_SLV_SOFT_RESET]     = (REGISTERS_BASE + 0x0000),
2390921014SLuciano Coelho 	[ACX_REG_EE_START]           = (REGISTERS_BASE + 0x080C),
2490921014SLuciano Coelho 	[ACX_REG_ECPU_CONTROL]       = (REGISTERS_BASE + 0x0804)
2590921014SLuciano Coelho };
2690921014SLuciano Coelho 
wl1251_translate_reg_addr(struct wl1251 * wl,int addr)2790921014SLuciano Coelho static int wl1251_translate_reg_addr(struct wl1251 *wl, int addr)
2890921014SLuciano Coelho {
2990921014SLuciano Coelho 	/* If the address is lower than REGISTERS_BASE, it means that this is
3090921014SLuciano Coelho 	 * a chip-specific register address, so look it up in the registers
3190921014SLuciano Coelho 	 * table */
3290921014SLuciano Coelho 	if (addr < REGISTERS_BASE) {
3390921014SLuciano Coelho 		/* Make sure we don't go over the table */
3490921014SLuciano Coelho 		if (addr >= ACX_REG_TABLE_LEN) {
3590921014SLuciano Coelho 			wl1251_error("address out of range (%d)", addr);
3690921014SLuciano Coelho 			return -EINVAL;
3790921014SLuciano Coelho 		}
3890921014SLuciano Coelho 		addr = wl1251_io_reg_table[addr];
3990921014SLuciano Coelho 	}
4090921014SLuciano Coelho 
4190921014SLuciano Coelho 	return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
4290921014SLuciano Coelho }
4390921014SLuciano Coelho 
wl1251_translate_mem_addr(struct wl1251 * wl,int addr)4490921014SLuciano Coelho static int wl1251_translate_mem_addr(struct wl1251 *wl, int addr)
4590921014SLuciano Coelho {
4690921014SLuciano Coelho 	return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
4790921014SLuciano Coelho }
4890921014SLuciano Coelho 
wl1251_mem_read(struct wl1251 * wl,int addr,void * buf,size_t len)4990921014SLuciano Coelho void wl1251_mem_read(struct wl1251 *wl, int addr, void *buf, size_t len)
5090921014SLuciano Coelho {
5190921014SLuciano Coelho 	int physical;
5290921014SLuciano Coelho 
5390921014SLuciano Coelho 	physical = wl1251_translate_mem_addr(wl, addr);
5490921014SLuciano Coelho 
5590921014SLuciano Coelho 	wl->if_ops->read(wl, physical, buf, len);
5690921014SLuciano Coelho }
5790921014SLuciano Coelho 
wl1251_mem_write(struct wl1251 * wl,int addr,void * buf,size_t len)5890921014SLuciano Coelho void wl1251_mem_write(struct wl1251 *wl, int addr, void *buf, size_t len)
5990921014SLuciano Coelho {
6090921014SLuciano Coelho 	int physical;
6190921014SLuciano Coelho 
6290921014SLuciano Coelho 	physical = wl1251_translate_mem_addr(wl, addr);
6390921014SLuciano Coelho 
6490921014SLuciano Coelho 	wl->if_ops->write(wl, physical, buf, len);
6590921014SLuciano Coelho }
6690921014SLuciano Coelho 
wl1251_mem_read32(struct wl1251 * wl,int addr)6790921014SLuciano Coelho u32 wl1251_mem_read32(struct wl1251 *wl, int addr)
6890921014SLuciano Coelho {
6990921014SLuciano Coelho 	return wl1251_read32(wl, wl1251_translate_mem_addr(wl, addr));
7090921014SLuciano Coelho }
7190921014SLuciano Coelho 
wl1251_mem_write32(struct wl1251 * wl,int addr,u32 val)7290921014SLuciano Coelho void wl1251_mem_write32(struct wl1251 *wl, int addr, u32 val)
7390921014SLuciano Coelho {
7490921014SLuciano Coelho 	wl1251_write32(wl, wl1251_translate_mem_addr(wl, addr), val);
7590921014SLuciano Coelho }
7690921014SLuciano Coelho 
wl1251_reg_read32(struct wl1251 * wl,int addr)7790921014SLuciano Coelho u32 wl1251_reg_read32(struct wl1251 *wl, int addr)
7890921014SLuciano Coelho {
7990921014SLuciano Coelho 	return wl1251_read32(wl, wl1251_translate_reg_addr(wl, addr));
8090921014SLuciano Coelho }
8190921014SLuciano Coelho 
wl1251_reg_write32(struct wl1251 * wl,int addr,u32 val)8290921014SLuciano Coelho void wl1251_reg_write32(struct wl1251 *wl, int addr, u32 val)
8390921014SLuciano Coelho {
8490921014SLuciano Coelho 	wl1251_write32(wl, wl1251_translate_reg_addr(wl, addr), val);
8590921014SLuciano Coelho }
8690921014SLuciano Coelho 
8790921014SLuciano Coelho /* Set the partitions to access the chip addresses.
8890921014SLuciano Coelho  *
8990921014SLuciano Coelho  * There are two VIRTUAL partitions (the memory partition and the
9090921014SLuciano Coelho  * registers partition), which are mapped to two different areas of the
9190921014SLuciano Coelho  * PHYSICAL (hardware) memory.  This function also makes other checks to
9290921014SLuciano Coelho  * ensure that the partitions are not overlapping.  In the diagram below, the
9390921014SLuciano Coelho  * memory partition comes before the register partition, but the opposite is
9490921014SLuciano Coelho  * also supported.
9590921014SLuciano Coelho  *
9690921014SLuciano Coelho  *                               PHYSICAL address
9790921014SLuciano Coelho  *                                     space
9890921014SLuciano Coelho  *
9990921014SLuciano Coelho  *                                    |    |
10090921014SLuciano Coelho  *                                 ...+----+--> mem_start
10190921014SLuciano Coelho  *          VIRTUAL address     ...   |    |
10290921014SLuciano Coelho  *               space       ...      |    | [PART_0]
10390921014SLuciano Coelho  *                        ...         |    |
10490921014SLuciano Coelho  * 0x00000000 <--+----+...         ...+----+--> mem_start + mem_size
10590921014SLuciano Coelho  *               |    |         ...   |    |
10690921014SLuciano Coelho  *               |MEM |      ...      |    |
10790921014SLuciano Coelho  *               |    |   ...         |    |
10890921014SLuciano Coelho  *  part_size <--+----+...            |    | {unused area)
10990921014SLuciano Coelho  *               |    |   ...         |    |
11090921014SLuciano Coelho  *               |REG |      ...      |    |
11190921014SLuciano Coelho  *  part_size    |    |         ...   |    |
11290921014SLuciano Coelho  *      +     <--+----+...         ...+----+--> reg_start
11390921014SLuciano Coelho  *  reg_size              ...         |    |
11490921014SLuciano Coelho  *                           ...      |    | [PART_1]
11590921014SLuciano Coelho  *                              ...   |    |
11690921014SLuciano Coelho  *                                 ...+----+--> reg_start + reg_size
11790921014SLuciano Coelho  *                                    |    |
11890921014SLuciano Coelho  *
11990921014SLuciano Coelho  */
wl1251_set_partition(struct wl1251 * wl,u32 mem_start,u32 mem_size,u32 reg_start,u32 reg_size)12090921014SLuciano Coelho void wl1251_set_partition(struct wl1251 *wl,
12190921014SLuciano Coelho 			  u32 mem_start, u32 mem_size,
12290921014SLuciano Coelho 			  u32 reg_start, u32 reg_size)
12390921014SLuciano Coelho {
124*45474475SH. Nikolaus Schaller 	struct wl1251_partition_set *partition;
125*45474475SH. Nikolaus Schaller 
126*45474475SH. Nikolaus Schaller 	partition = kmalloc(sizeof(*partition), GFP_KERNEL);
127*45474475SH. Nikolaus Schaller 	if (!partition) {
128*45474475SH. Nikolaus Schaller 		wl1251_error("can not allocate partition buffer");
129*45474475SH. Nikolaus Schaller 		return;
130*45474475SH. Nikolaus Schaller 	}
13190921014SLuciano Coelho 
13290921014SLuciano Coelho 	wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
13390921014SLuciano Coelho 		     mem_start, mem_size);
13490921014SLuciano Coelho 	wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
13590921014SLuciano Coelho 		     reg_start, reg_size);
13690921014SLuciano Coelho 
13790921014SLuciano Coelho 	/* Make sure that the two partitions together don't exceed the
13890921014SLuciano Coelho 	 * address range */
13990921014SLuciano Coelho 	if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
14090921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
14190921014SLuciano Coelho 			     " address range.  Truncating partition[0].");
14290921014SLuciano Coelho 		mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
14390921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
14490921014SLuciano Coelho 			     mem_start, mem_size);
14590921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
14690921014SLuciano Coelho 			     reg_start, reg_size);
14790921014SLuciano Coelho 	}
14890921014SLuciano Coelho 
14990921014SLuciano Coelho 	if ((mem_start < reg_start) &&
15090921014SLuciano Coelho 	    ((mem_start + mem_size) > reg_start)) {
15190921014SLuciano Coelho 		/* Guarantee that the memory partition doesn't overlap the
15290921014SLuciano Coelho 		 * registers partition */
15390921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "End of partition[0] is "
15490921014SLuciano Coelho 			     "overlapping partition[1].  Adjusted.");
15590921014SLuciano Coelho 		mem_size = reg_start - mem_start;
15690921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
15790921014SLuciano Coelho 			     mem_start, mem_size);
15890921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
15990921014SLuciano Coelho 			     reg_start, reg_size);
16090921014SLuciano Coelho 	} else if ((reg_start < mem_start) &&
16190921014SLuciano Coelho 		   ((reg_start + reg_size) > mem_start)) {
16290921014SLuciano Coelho 		/* Guarantee that the register partition doesn't overlap the
16390921014SLuciano Coelho 		 * memory partition */
16490921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "End of partition[1] is"
16590921014SLuciano Coelho 			     " overlapping partition[0].  Adjusted.");
16690921014SLuciano Coelho 		reg_size = mem_start - reg_start;
16790921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
16890921014SLuciano Coelho 			     mem_start, mem_size);
16990921014SLuciano Coelho 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
17090921014SLuciano Coelho 			     reg_start, reg_size);
17190921014SLuciano Coelho 	}
17290921014SLuciano Coelho 
173*45474475SH. Nikolaus Schaller 	partition->mem.start = mem_start;
174*45474475SH. Nikolaus Schaller 	partition->mem.size  = mem_size;
175*45474475SH. Nikolaus Schaller 	partition->reg.start = reg_start;
176*45474475SH. Nikolaus Schaller 	partition->reg.size  = reg_size;
17790921014SLuciano Coelho 
17890921014SLuciano Coelho 	wl->physical_mem_addr = mem_start;
17990921014SLuciano Coelho 	wl->physical_reg_addr = reg_start;
18090921014SLuciano Coelho 
18190921014SLuciano Coelho 	wl->virtual_mem_addr = 0;
18290921014SLuciano Coelho 	wl->virtual_reg_addr = mem_size;
18390921014SLuciano Coelho 
18490921014SLuciano Coelho 	wl->if_ops->write(wl, HW_ACCESS_PART0_SIZE_ADDR, partition,
185*45474475SH. Nikolaus Schaller 		sizeof(*partition));
186*45474475SH. Nikolaus Schaller 
187*45474475SH. Nikolaus Schaller 	kfree(partition);
18890921014SLuciano Coelho }
189