xref: /linux/drivers/net/wireless/ath/ath5k/ahb.c (revision f9035cd498486d5a82ad8ae9bcfdb91b3e57ec9d)
1 /*
2  * Copyright (c) 2008-2009 Atheros Communications Inc.
3  * Copyright (c) 2009 Gabor Juhos <juhosg@openwrt.org>
4  * Copyright (c) 2009 Imre Kaloz <kaloz@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <linux/nl80211.h>
20 #include <linux/platform_device.h>
21 #include <linux/etherdevice.h>
22 #include <ar231x_platform.h>
23 #include "ath5k.h"
24 #include "debug.h"
25 #include "base.h"
26 #include "reg.h"
27 #include "debug.h"
28 
29 /* return bus cachesize in 4B word units */
30 static void ath5k_ahb_read_cachesize(struct ath_common *common, int *csz)
31 {
32 	*csz = L1_CACHE_BYTES >> 2;
33 }
34 
35 static bool
36 ath5k_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
37 {
38 	struct ath5k_hw *ah = common->priv;
39 	struct platform_device *pdev = to_platform_device(ah->dev);
40 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
41 	u16 *eeprom, *eeprom_end;
42 
43 
44 
45 	bcfg = pdev->dev.platform_data;
46 	eeprom = (u16 *) bcfg->radio;
47 	eeprom_end = ((void *) bcfg->config) + BOARD_CONFIG_BUFSZ;
48 
49 	eeprom += off;
50 	if (eeprom > eeprom_end)
51 		return false;
52 
53 	*data = *eeprom;
54 	return true;
55 }
56 
57 int ath5k_hw_read_srev(struct ath5k_hw *ah)
58 {
59 	struct platform_device *pdev = to_platform_device(ah->dev);
60 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
61 	ah->ah_mac_srev = bcfg->devid;
62 	return 0;
63 }
64 
65 static int ath5k_ahb_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
66 {
67 	struct platform_device *pdev = to_platform_device(ah->dev);
68 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
69 	u8 *cfg_mac;
70 
71 	if (to_platform_device(ah->dev)->id == 0)
72 		cfg_mac = bcfg->config->wlan0_mac;
73 	else
74 		cfg_mac = bcfg->config->wlan1_mac;
75 
76 	memcpy(mac, cfg_mac, ETH_ALEN);
77 	return 0;
78 }
79 
80 static const struct ath_bus_ops ath_ahb_bus_ops = {
81 	.ath_bus_type = ATH_AHB,
82 	.read_cachesize = ath5k_ahb_read_cachesize,
83 	.eeprom_read = ath5k_ahb_eeprom_read,
84 	.eeprom_read_mac = ath5k_ahb_eeprom_read_mac,
85 };
86 
87 /*Initialization*/
88 static int ath_ahb_probe(struct platform_device *pdev)
89 {
90 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
91 	struct ath5k_hw *ah;
92 	struct ieee80211_hw *hw;
93 	struct resource *res;
94 	void __iomem *mem;
95 	int irq;
96 	int ret = 0;
97 	u32 reg;
98 
99 	if (!pdev->dev.platform_data) {
100 		dev_err(&pdev->dev, "no platform data specified\n");
101 		ret = -EINVAL;
102 		goto err_out;
103 	}
104 
105 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106 	if (res == NULL) {
107 		dev_err(&pdev->dev, "no memory resource found\n");
108 		ret = -ENXIO;
109 		goto err_out;
110 	}
111 
112 	mem = ioremap_nocache(res->start, resource_size(res));
113 	if (mem == NULL) {
114 		dev_err(&pdev->dev, "ioremap failed\n");
115 		ret = -ENOMEM;
116 		goto err_out;
117 	}
118 
119 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
120 	if (res == NULL) {
121 		dev_err(&pdev->dev, "no IRQ resource found\n");
122 		ret = -ENXIO;
123 		goto err_out;
124 	}
125 
126 	irq = res->start;
127 
128 	hw = ieee80211_alloc_hw(sizeof(struct ath5k_hw), &ath5k_hw_ops);
129 	if (hw == NULL) {
130 		dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
131 		ret = -ENOMEM;
132 		goto err_out;
133 	}
134 
135 	ah = hw->priv;
136 	ah->hw = hw;
137 	ah->dev = &pdev->dev;
138 	ah->iobase = mem;
139 	ah->irq = irq;
140 	ah->devid = bcfg->devid;
141 
142 	if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
143 		/* Enable WMAC AHB arbitration */
144 		reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
145 		reg |= AR5K_AR2315_AHB_ARB_CTL_WLAN;
146 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
147 
148 		/* Enable global WMAC swapping */
149 		reg = __raw_readl((void __iomem *) AR5K_AR2315_BYTESWAP);
150 		reg |= AR5K_AR2315_BYTESWAP_WMAC;
151 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_BYTESWAP);
152 	} else {
153 		/* Enable WMAC DMA access (assuming 5312 or 231x*/
154 		/* TODO: check other platforms */
155 		reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
156 		if (to_platform_device(ah->dev)->id == 0)
157 			reg |= AR5K_AR5312_ENABLE_WLAN0;
158 		else
159 			reg |= AR5K_AR5312_ENABLE_WLAN1;
160 		__raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
161 
162 		/*
163 		 * On a dual-band AR5312, the multiband radio is only
164 		 * used as pass-through. Disable 2 GHz support in the
165 		 * driver for it
166 		 */
167 		if (to_platform_device(ah->dev)->id == 0 &&
168 		    (bcfg->config->flags & (BD_WLAN0 | BD_WLAN1)) ==
169 		     (BD_WLAN1 | BD_WLAN0))
170 			__set_bit(ATH_STAT_2G_DISABLED, ah->status);
171 	}
172 
173 	ret = ath5k_init_softc(ah, &ath_ahb_bus_ops);
174 	if (ret != 0) {
175 		dev_err(&pdev->dev, "failed to attach device, err=%d\n", ret);
176 		ret = -ENODEV;
177 		goto err_free_hw;
178 	}
179 
180 	platform_set_drvdata(pdev, hw);
181 
182 	return 0;
183 
184  err_free_hw:
185 	ieee80211_free_hw(hw);
186 	platform_set_drvdata(pdev, NULL);
187  err_out:
188 	return ret;
189 }
190 
191 static int ath_ahb_remove(struct platform_device *pdev)
192 {
193 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
194 	struct ieee80211_hw *hw = platform_get_drvdata(pdev);
195 	struct ath5k_hw *ah;
196 	u32 reg;
197 
198 	if (!hw)
199 		return 0;
200 
201 	ah = hw->priv;
202 
203 	if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
204 		/* Disable WMAC AHB arbitration */
205 		reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
206 		reg &= ~AR5K_AR2315_AHB_ARB_CTL_WLAN;
207 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
208 	} else {
209 		/*Stop DMA access */
210 		reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
211 		if (to_platform_device(ah->dev)->id == 0)
212 			reg &= ~AR5K_AR5312_ENABLE_WLAN0;
213 		else
214 			reg &= ~AR5K_AR5312_ENABLE_WLAN1;
215 		__raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
216 	}
217 
218 	ath5k_deinit_softc(ah);
219 	platform_set_drvdata(pdev, NULL);
220 	ieee80211_free_hw(hw);
221 
222 	return 0;
223 }
224 
225 static struct platform_driver ath_ahb_driver = {
226 	.probe      = ath_ahb_probe,
227 	.remove     = ath_ahb_remove,
228 	.driver		= {
229 		.name	= "ar231x-wmac",
230 		.owner	= THIS_MODULE,
231 	},
232 };
233 
234 static int __init
235 ath5k_ahb_init(void)
236 {
237 	return platform_driver_register(&ath_ahb_driver);
238 }
239 
240 static void __exit
241 ath5k_ahb_exit(void)
242 {
243 	platform_driver_unregister(&ath_ahb_driver);
244 }
245 
246 module_init(ath5k_ahb_init);
247 module_exit(ath5k_ahb_exit);
248