| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 1 | /* | 
 | 2 |  * This file is part of wl1271 | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2008-2010 Nokia Corporation | 
 | 5 |  * | 
 | 6 |  * Contact: Luciano Coelho <luciano.coelho@nokia.com> | 
 | 7 |  * | 
 | 8 |  * This program is free software; you can redistribute it and/or | 
 | 9 |  * modify it under the terms of the GNU General Public License | 
 | 10 |  * version 2 as published by the Free Software Foundation. | 
 | 11 |  * | 
 | 12 |  * This program is distributed in the hope that it will be useful, but | 
 | 13 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 14 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 15 |  * General Public License for more details. | 
 | 16 |  * | 
 | 17 |  * You should have received a copy of the GNU General Public License | 
 | 18 |  * along with this program; if not, write to the Free Software | 
 | 19 |  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | 
 | 20 |  * 02110-1301 USA | 
 | 21 |  * | 
 | 22 |  */ | 
 | 23 |  | 
 | 24 | #include <linux/module.h> | 
 | 25 | #include <linux/platform_device.h> | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 26 | #include <linux/spi/spi.h> | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 27 | #include <linux/interrupt.h> | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 28 |  | 
| Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 29 | #include "wl12xx.h" | 
| Luciano Coelho | 0f4e312 | 2011-10-07 11:02:42 +0300 | [diff] [blame] | 30 | #include "debug.h" | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 31 | #include "wl12xx_80211.h" | 
| Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 32 | #include "io.h" | 
| Ido Yariv | 0da13da | 2011-03-31 10:06:58 +0200 | [diff] [blame] | 33 | #include "tx.h" | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 34 |  | 
| Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 35 | #define OCP_CMD_LOOP  32 | 
 | 36 |  | 
 | 37 | #define OCP_CMD_WRITE 0x1 | 
 | 38 | #define OCP_CMD_READ  0x2 | 
 | 39 |  | 
 | 40 | #define OCP_READY_MASK  BIT(18) | 
 | 41 | #define OCP_STATUS_MASK (BIT(16) | BIT(17)) | 
 | 42 |  | 
 | 43 | #define OCP_STATUS_NO_RESP    0x00000 | 
 | 44 | #define OCP_STATUS_OK         0x10000 | 
 | 45 | #define OCP_STATUS_REQ_FAILED 0x20000 | 
 | 46 | #define OCP_STATUS_RESP_ERROR 0x30000 | 
 | 47 |  | 
| Shahar Levi | 48a6147 | 2011-03-06 16:32:08 +0200 | [diff] [blame] | 48 | bool wl1271_set_block_size(struct wl1271 *wl) | 
 | 49 | { | 
 | 50 | 	if (wl->if_ops->set_block_size) { | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 51 | 		wl->if_ops->set_block_size(wl->dev, WL12XX_BUS_BLOCK_SIZE); | 
| Shahar Levi | 48a6147 | 2011-03-06 16:32:08 +0200 | [diff] [blame] | 52 | 		return true; | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	return false; | 
 | 56 | } | 
 | 57 |  | 
| Teemu Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame] | 58 | void wl1271_disable_interrupts(struct wl1271 *wl) | 
 | 59 | { | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 60 | 	disable_irq(wl->irq); | 
| Teemu Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame] | 61 | } | 
 | 62 |  | 
 | 63 | void wl1271_enable_interrupts(struct wl1271 *wl) | 
 | 64 | { | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 65 | 	enable_irq(wl->irq); | 
| Teemu Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame] | 66 | } | 
 | 67 |  | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 68 | /* Set the SPI partitions to access the chip addresses | 
 | 69 |  * | 
 | 70 |  * To simplify driver code, a fixed (virtual) memory map is defined for | 
 | 71 |  * register and memory addresses. Because in the chipset, in different stages | 
 | 72 |  * of operation, those addresses will move around, an address translation | 
 | 73 |  * mechanism is required. | 
 | 74 |  * | 
 | 75 |  * There are four partitions (three memory and one register partition), | 
 | 76 |  * which are mapped to two different areas of the hardware memory. | 
 | 77 |  * | 
 | 78 |  *                                Virtual address | 
 | 79 |  *                                     space | 
 | 80 |  * | 
 | 81 |  *                                    |    | | 
 | 82 |  *                                 ...+----+--> mem.start | 
 | 83 |  *          Physical address    ...   |    | | 
 | 84 |  *               space       ...      |    | [PART_0] | 
 | 85 |  *                        ...         |    | | 
 | 86 |  *  00000000  <--+----+...         ...+----+--> mem.start + mem.size | 
 | 87 |  *               |    |         ...   |    | | 
 | 88 |  *               |MEM |      ...      |    | | 
 | 89 |  *               |    |   ...         |    | | 
 | 90 |  *  mem.size  <--+----+...            |    | {unused area) | 
 | 91 |  *               |    |   ...         |    | | 
 | 92 |  *               |REG |      ...      |    | | 
 | 93 |  *  mem.size     |    |         ...   |    | | 
 | 94 |  *      +     <--+----+...         ...+----+--> reg.start | 
 | 95 |  *  reg.size     |    |   ...         |    | | 
 | 96 |  *               |MEM2|      ...      |    | [PART_1] | 
 | 97 |  *               |    |         ...   |    | | 
 | 98 |  *                                 ...+----+--> reg.start + reg.size | 
 | 99 |  *                                    |    | | 
 | 100 |  * | 
 | 101 |  */ | 
 | 102 | int wl1271_set_partition(struct wl1271 *wl, | 
 | 103 | 			 struct wl1271_partition_set *p) | 
 | 104 | { | 
 | 105 | 	/* copy partition info */ | 
 | 106 | 	memcpy(&wl->part, p, sizeof(*p)); | 
 | 107 |  | 
 | 108 | 	wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X", | 
 | 109 | 		     p->mem.start, p->mem.size); | 
 | 110 | 	wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X", | 
 | 111 | 		     p->reg.start, p->reg.size); | 
 | 112 | 	wl1271_debug(DEBUG_SPI, "mem2_start %08X mem2_size %08X", | 
 | 113 | 		     p->mem2.start, p->mem2.size); | 
 | 114 | 	wl1271_debug(DEBUG_SPI, "mem3_start %08X mem3_size %08X", | 
 | 115 | 		     p->mem3.start, p->mem3.size); | 
 | 116 |  | 
 | 117 | 	/* write partition info to the chipset */ | 
 | 118 | 	wl1271_raw_write32(wl, HW_PART0_START_ADDR, p->mem.start); | 
 | 119 | 	wl1271_raw_write32(wl, HW_PART0_SIZE_ADDR, p->mem.size); | 
 | 120 | 	wl1271_raw_write32(wl, HW_PART1_START_ADDR, p->reg.start); | 
 | 121 | 	wl1271_raw_write32(wl, HW_PART1_SIZE_ADDR, p->reg.size); | 
 | 122 | 	wl1271_raw_write32(wl, HW_PART2_START_ADDR, p->mem2.start); | 
 | 123 | 	wl1271_raw_write32(wl, HW_PART2_SIZE_ADDR, p->mem2.size); | 
 | 124 | 	wl1271_raw_write32(wl, HW_PART3_START_ADDR, p->mem3.start); | 
 | 125 |  | 
 | 126 | 	return 0; | 
 | 127 | } | 
| Roger Quadros | 870c367 | 2010-11-29 16:24:57 +0200 | [diff] [blame] | 128 | EXPORT_SYMBOL_GPL(wl1271_set_partition); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 129 |  | 
| Teemu Paasikivi | 9b28072 | 2010-02-18 13:25:56 +0200 | [diff] [blame] | 130 | void wl1271_io_reset(struct wl1271 *wl) | 
 | 131 | { | 
| Felipe Balbi | 77d7d7a | 2011-05-14 00:26:21 +0300 | [diff] [blame] | 132 | 	if (wl->if_ops->reset) | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 133 | 		wl->if_ops->reset(wl->dev); | 
| Teemu Paasikivi | 9b28072 | 2010-02-18 13:25:56 +0200 | [diff] [blame] | 134 | } | 
 | 135 |  | 
 | 136 | void wl1271_io_init(struct wl1271 *wl) | 
 | 137 | { | 
| Felipe Balbi | 77d7d7a | 2011-05-14 00:26:21 +0300 | [diff] [blame] | 138 | 	if (wl->if_ops->init) | 
| Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame] | 139 | 		wl->if_ops->init(wl->dev); | 
| Teemu Paasikivi | 9b28072 | 2010-02-18 13:25:56 +0200 | [diff] [blame] | 140 | } | 
 | 141 |  | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 142 | void wl1271_top_reg_write(struct wl1271 *wl, int addr, u16 val) | 
 | 143 | { | 
 | 144 | 	/* write address >> 1 + 0x30000 to OCP_POR_CTR */ | 
 | 145 | 	addr = (addr >> 1) + 0x30000; | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 146 | 	wl1271_write32(wl, OCP_POR_CTR, addr); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 147 |  | 
 | 148 | 	/* write value to OCP_POR_WDATA */ | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 149 | 	wl1271_write32(wl, OCP_DATA_WRITE, val); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 150 |  | 
 | 151 | 	/* write 1 to OCP_CMD */ | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 152 | 	wl1271_write32(wl, OCP_CMD, OCP_CMD_WRITE); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 153 | } | 
 | 154 |  | 
 | 155 | u16 wl1271_top_reg_read(struct wl1271 *wl, int addr) | 
 | 156 | { | 
 | 157 | 	u32 val; | 
 | 158 | 	int timeout = OCP_CMD_LOOP; | 
 | 159 |  | 
 | 160 | 	/* write address >> 1 + 0x30000 to OCP_POR_CTR */ | 
 | 161 | 	addr = (addr >> 1) + 0x30000; | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 162 | 	wl1271_write32(wl, OCP_POR_CTR, addr); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 163 |  | 
 | 164 | 	/* write 2 to OCP_CMD */ | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 165 | 	wl1271_write32(wl, OCP_CMD, OCP_CMD_READ); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 166 |  | 
 | 167 | 	/* poll for data ready */ | 
 | 168 | 	do { | 
| Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 169 | 		val = wl1271_read32(wl, OCP_DATA_READ); | 
| Teemu Paasikivi | 521a5b2 | 2010-02-18 13:25:54 +0200 | [diff] [blame] | 170 | 	} while (!(val & OCP_READY_MASK) && --timeout); | 
 | 171 |  | 
 | 172 | 	if (!timeout) { | 
 | 173 | 		wl1271_warning("Top register access timed out."); | 
 | 174 | 		return 0xffff; | 
 | 175 | 	} | 
 | 176 |  | 
 | 177 | 	/* check data status and return if OK */ | 
 | 178 | 	if ((val & OCP_STATUS_MASK) == OCP_STATUS_OK) | 
 | 179 | 		return val & 0xffff; | 
 | 180 | 	else { | 
 | 181 | 		wl1271_warning("Top register access returned error."); | 
 | 182 | 		return 0xffff; | 
 | 183 | 	} | 
 | 184 | } | 
 | 185 |  |