xref: /linux/drivers/net/wireless/ti/wl1251/io.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20764de64SBob Copeland /*
30764de64SBob Copeland  * This file is part of wl12xx
40764de64SBob Copeland  *
50764de64SBob Copeland  * Copyright (C) 2008 Nokia Corporation
60764de64SBob Copeland  */
70764de64SBob Copeland 
80764de64SBob Copeland #include "wl1251.h"
99bc6772eSKalle Valo #include "reg.h"
109bc6772eSKalle Valo #include "io.h"
110764de64SBob Copeland 
120e71bb08SKalle Valo /* FIXME: this is static data nowadays and the table can be removed */
130e71bb08SKalle Valo static enum wl12xx_acx_int_reg wl1251_io_reg_table[ACX_REG_TABLE_LEN] = {
140e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_TRIG]     = (REGISTERS_BASE + 0x0474),
150e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_TRIG_H]   = (REGISTERS_BASE + 0x0478),
160e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_MASK]     = (REGISTERS_BASE + 0x0494),
170e71bb08SKalle Valo 	[ACX_REG_HINT_MASK_SET]      = (REGISTERS_BASE + 0x0498),
180e71bb08SKalle Valo 	[ACX_REG_HINT_MASK_CLR]      = (REGISTERS_BASE + 0x049C),
190e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_NO_CLEAR] = (REGISTERS_BASE + 0x04B0),
200e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_CLEAR]    = (REGISTERS_BASE + 0x04A4),
210e71bb08SKalle Valo 	[ACX_REG_INTERRUPT_ACK]      = (REGISTERS_BASE + 0x04A8),
220e71bb08SKalle Valo 	[ACX_REG_SLV_SOFT_RESET]     = (REGISTERS_BASE + 0x0000),
230e71bb08SKalle Valo 	[ACX_REG_EE_START]           = (REGISTERS_BASE + 0x080C),
240e71bb08SKalle Valo 	[ACX_REG_ECPU_CONTROL]       = (REGISTERS_BASE + 0x0804)
250e71bb08SKalle Valo };
260e71bb08SKalle Valo 
wl1251_translate_reg_addr(struct wl1251 * wl,int addr)270764de64SBob Copeland static int wl1251_translate_reg_addr(struct wl1251 *wl, int addr)
280764de64SBob Copeland {
290764de64SBob Copeland 	/* If the address is lower than REGISTERS_BASE, it means that this is
300764de64SBob Copeland 	 * a chip-specific register address, so look it up in the registers
310764de64SBob Copeland 	 * table */
320764de64SBob Copeland 	if (addr < REGISTERS_BASE) {
330764de64SBob Copeland 		/* Make sure we don't go over the table */
340764de64SBob Copeland 		if (addr >= ACX_REG_TABLE_LEN) {
350764de64SBob Copeland 			wl1251_error("address out of range (%d)", addr);
360764de64SBob Copeland 			return -EINVAL;
370764de64SBob Copeland 		}
380e71bb08SKalle Valo 		addr = wl1251_io_reg_table[addr];
390764de64SBob Copeland 	}
400764de64SBob Copeland 
410764de64SBob Copeland 	return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
420764de64SBob Copeland }
430764de64SBob Copeland 
wl1251_translate_mem_addr(struct wl1251 * wl,int addr)440764de64SBob Copeland static int wl1251_translate_mem_addr(struct wl1251 *wl, int addr)
450764de64SBob Copeland {
460764de64SBob Copeland 	return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
470764de64SBob Copeland }
480764de64SBob Copeland 
wl1251_mem_read(struct wl1251 * wl,int addr,void * buf,size_t len)490764de64SBob Copeland void wl1251_mem_read(struct wl1251 *wl, int addr, void *buf, size_t len)
500764de64SBob Copeland {
510764de64SBob Copeland 	int physical;
520764de64SBob Copeland 
530764de64SBob Copeland 	physical = wl1251_translate_mem_addr(wl, addr);
540764de64SBob Copeland 
5508d9f572SBob Copeland 	wl->if_ops->read(wl, physical, buf, len);
560764de64SBob Copeland }
570764de64SBob Copeland 
wl1251_mem_write(struct wl1251 * wl,int addr,void * buf,size_t len)580764de64SBob Copeland void wl1251_mem_write(struct wl1251 *wl, int addr, void *buf, size_t len)
590764de64SBob Copeland {
600764de64SBob Copeland 	int physical;
610764de64SBob Copeland 
620764de64SBob Copeland 	physical = wl1251_translate_mem_addr(wl, addr);
630764de64SBob Copeland 
6408d9f572SBob Copeland 	wl->if_ops->write(wl, physical, buf, len);
650764de64SBob Copeland }
660764de64SBob Copeland 
wl1251_mem_read32(struct wl1251 * wl,int addr)670764de64SBob Copeland u32 wl1251_mem_read32(struct wl1251 *wl, int addr)
680764de64SBob Copeland {
690764de64SBob Copeland 	return wl1251_read32(wl, wl1251_translate_mem_addr(wl, addr));
700764de64SBob Copeland }
710764de64SBob Copeland 
wl1251_mem_write32(struct wl1251 * wl,int addr,u32 val)720764de64SBob Copeland void wl1251_mem_write32(struct wl1251 *wl, int addr, u32 val)
730764de64SBob Copeland {
740764de64SBob Copeland 	wl1251_write32(wl, wl1251_translate_mem_addr(wl, addr), val);
750764de64SBob Copeland }
760764de64SBob Copeland 
wl1251_reg_read32(struct wl1251 * wl,int addr)770764de64SBob Copeland u32 wl1251_reg_read32(struct wl1251 *wl, int addr)
780764de64SBob Copeland {
790764de64SBob Copeland 	return wl1251_read32(wl, wl1251_translate_reg_addr(wl, addr));
800764de64SBob Copeland }
810764de64SBob Copeland 
wl1251_reg_write32(struct wl1251 * wl,int addr,u32 val)820764de64SBob Copeland void wl1251_reg_write32(struct wl1251 *wl, int addr, u32 val)
830764de64SBob Copeland {
840764de64SBob Copeland 	wl1251_write32(wl, wl1251_translate_reg_addr(wl, addr), val);
850764de64SBob Copeland }
860d77e141SBob Copeland 
870d77e141SBob Copeland /* Set the partitions to access the chip addresses.
880d77e141SBob Copeland  *
890d77e141SBob Copeland  * There are two VIRTUAL partitions (the memory partition and the
900d77e141SBob Copeland  * registers partition), which are mapped to two different areas of the
910d77e141SBob Copeland  * PHYSICAL (hardware) memory.  This function also makes other checks to
920d77e141SBob Copeland  * ensure that the partitions are not overlapping.  In the diagram below, the
930d77e141SBob Copeland  * memory partition comes before the register partition, but the opposite is
940d77e141SBob Copeland  * also supported.
950d77e141SBob Copeland  *
960d77e141SBob Copeland  *                               PHYSICAL address
970d77e141SBob Copeland  *                                     space
980d77e141SBob Copeland  *
990d77e141SBob Copeland  *                                    |    |
1000d77e141SBob Copeland  *                                 ...+----+--> mem_start
1010d77e141SBob Copeland  *          VIRTUAL address     ...   |    |
1020d77e141SBob Copeland  *               space       ...      |    | [PART_0]
1030d77e141SBob Copeland  *                        ...         |    |
1040d77e141SBob Copeland  * 0x00000000 <--+----+...         ...+----+--> mem_start + mem_size
1050d77e141SBob Copeland  *               |    |         ...   |    |
1060d77e141SBob Copeland  *               |MEM |      ...      |    |
1070d77e141SBob Copeland  *               |    |   ...         |    |
1080d77e141SBob Copeland  *  part_size <--+----+...            |    | {unused area)
1090d77e141SBob Copeland  *               |    |   ...         |    |
1100d77e141SBob Copeland  *               |REG |      ...      |    |
1110d77e141SBob Copeland  *  part_size    |    |         ...   |    |
1120d77e141SBob Copeland  *      +     <--+----+...         ...+----+--> reg_start
1130d77e141SBob Copeland  *  reg_size              ...         |    |
1140d77e141SBob Copeland  *                           ...      |    | [PART_1]
1150d77e141SBob Copeland  *                              ...   |    |
1160d77e141SBob Copeland  *                                 ...+----+--> reg_start + reg_size
1170d77e141SBob Copeland  *                                    |    |
1180d77e141SBob Copeland  *
1190d77e141SBob Copeland  */
wl1251_set_partition(struct wl1251 * wl,u32 mem_start,u32 mem_size,u32 reg_start,u32 reg_size)1200d77e141SBob Copeland void wl1251_set_partition(struct wl1251 *wl,
1210d77e141SBob Copeland 			  u32 mem_start, u32 mem_size,
1220d77e141SBob Copeland 			  u32 reg_start, u32 reg_size)
1230d77e141SBob Copeland {
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 	}
1310d77e141SBob Copeland 
1320d77e141SBob Copeland 	wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
1330d77e141SBob Copeland 		     mem_start, mem_size);
1340d77e141SBob Copeland 	wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
1350d77e141SBob Copeland 		     reg_start, reg_size);
1360d77e141SBob Copeland 
1370d77e141SBob Copeland 	/* Make sure that the two partitions together don't exceed the
1380d77e141SBob Copeland 	 * address range */
1390d77e141SBob Copeland 	if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
1400d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
1410d77e141SBob Copeland 			     " address range.  Truncating partition[0].");
1420d77e141SBob Copeland 		mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
1430d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
1440d77e141SBob Copeland 			     mem_start, mem_size);
1450d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
1460d77e141SBob Copeland 			     reg_start, reg_size);
1470d77e141SBob Copeland 	}
1480d77e141SBob Copeland 
1490d77e141SBob Copeland 	if ((mem_start < reg_start) &&
1500d77e141SBob Copeland 	    ((mem_start + mem_size) > reg_start)) {
1510d77e141SBob Copeland 		/* Guarantee that the memory partition doesn't overlap the
1520d77e141SBob Copeland 		 * registers partition */
1530d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "End of partition[0] is "
1540d77e141SBob Copeland 			     "overlapping partition[1].  Adjusted.");
1550d77e141SBob Copeland 		mem_size = reg_start - mem_start;
1560d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
1570d77e141SBob Copeland 			     mem_start, mem_size);
1580d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
1590d77e141SBob Copeland 			     reg_start, reg_size);
1600d77e141SBob Copeland 	} else if ((reg_start < mem_start) &&
1610d77e141SBob Copeland 		   ((reg_start + reg_size) > mem_start)) {
1620d77e141SBob Copeland 		/* Guarantee that the register partition doesn't overlap the
1630d77e141SBob Copeland 		 * memory partition */
1640d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "End of partition[1] is"
1650d77e141SBob Copeland 			     " overlapping partition[0].  Adjusted.");
1660d77e141SBob Copeland 		reg_size = mem_start - reg_start;
1670d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
1680d77e141SBob Copeland 			     mem_start, mem_size);
1690d77e141SBob Copeland 		wl1251_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
1700d77e141SBob Copeland 			     reg_start, reg_size);
1710d77e141SBob Copeland 	}
1720d77e141SBob Copeland 
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;
1770d77e141SBob Copeland 
1780d77e141SBob Copeland 	wl->physical_mem_addr = mem_start;
1790d77e141SBob Copeland 	wl->physical_reg_addr = reg_start;
1800d77e141SBob Copeland 
1810d77e141SBob Copeland 	wl->virtual_mem_addr = 0;
1820d77e141SBob Copeland 	wl->virtual_reg_addr = mem_size;
1830d77e141SBob Copeland 
1840d77e141SBob Copeland 	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);
1880d77e141SBob Copeland }
189