xref: /linux/drivers/net/wireless/ath/ath5k/ahb.c (revision 7bf7e370d5919112c223a269462cd0b546903829)
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 <ar231x_platform.h>
22 #include "ath5k.h"
23 #include "debug.h"
24 #include "base.h"
25 #include "reg.h"
26 #include "debug.h"
27 
28 /* return bus cachesize in 4B word units */
29 static void ath5k_ahb_read_cachesize(struct ath_common *common, int *csz)
30 {
31 	*csz = L1_CACHE_BYTES >> 2;
32 }
33 
34 static bool
35 ath5k_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
36 {
37 	struct ath5k_softc *sc = common->priv;
38 	struct platform_device *pdev = to_platform_device(sc->dev);
39 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
40 	u16 *eeprom, *eeprom_end;
41 
42 
43 
44 	bcfg = pdev->dev.platform_data;
45 	eeprom = (u16 *) bcfg->radio;
46 	eeprom_end = ((void *) bcfg->config) + BOARD_CONFIG_BUFSZ;
47 
48 	eeprom += off;
49 	if (eeprom > eeprom_end)
50 		return false;
51 
52 	*data = *eeprom;
53 	return true;
54 }
55 
56 int ath5k_hw_read_srev(struct ath5k_hw *ah)
57 {
58 	struct ath5k_softc *sc = ah->ah_sc;
59 	struct platform_device *pdev = to_platform_device(sc->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 const struct ath_bus_ops ath_ahb_bus_ops = {
66 	.ath_bus_type = ATH_AHB,
67 	.read_cachesize = ath5k_ahb_read_cachesize,
68 	.eeprom_read = ath5k_ahb_eeprom_read,
69 };
70 
71 /*Initialization*/
72 static int ath_ahb_probe(struct platform_device *pdev)
73 {
74 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
75 	struct ath5k_softc *sc;
76 	struct ieee80211_hw *hw;
77 	struct resource *res;
78 	void __iomem *mem;
79 	int irq;
80 	int ret = 0;
81 	u32 reg;
82 
83 	if (!pdev->dev.platform_data) {
84 		dev_err(&pdev->dev, "no platform data specified\n");
85 		ret = -EINVAL;
86 		goto err_out;
87 	}
88 
89 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90 	if (res == NULL) {
91 		dev_err(&pdev->dev, "no memory resource found\n");
92 		ret = -ENXIO;
93 		goto err_out;
94 	}
95 
96 	mem = ioremap_nocache(res->start, resource_size(res));
97 	if (mem == NULL) {
98 		dev_err(&pdev->dev, "ioremap failed\n");
99 		ret = -ENOMEM;
100 		goto err_out;
101 	}
102 
103 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
104 	if (res == NULL) {
105 		dev_err(&pdev->dev, "no IRQ resource found\n");
106 		ret = -ENXIO;
107 		goto err_out;
108 	}
109 
110 	irq = res->start;
111 
112 	hw = ieee80211_alloc_hw(sizeof(struct ath5k_softc), &ath5k_hw_ops);
113 	if (hw == NULL) {
114 		dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
115 		ret = -ENOMEM;
116 		goto err_out;
117 	}
118 
119 	sc = hw->priv;
120 	sc->hw = hw;
121 	sc->dev = &pdev->dev;
122 	sc->iobase = mem;
123 	sc->irq = irq;
124 	sc->devid = bcfg->devid;
125 
126 	if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
127 		/* Enable WMAC AHB arbitration */
128 		reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
129 		reg |= AR5K_AR2315_AHB_ARB_CTL_WLAN;
130 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
131 
132 		/* Enable global WMAC swapping */
133 		reg = __raw_readl((void __iomem *) AR5K_AR2315_BYTESWAP);
134 		reg |= AR5K_AR2315_BYTESWAP_WMAC;
135 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_BYTESWAP);
136 	} else {
137 		/* Enable WMAC DMA access (assuming 5312 or 231x*/
138 		/* TODO: check other platforms */
139 		reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
140 		if (to_platform_device(sc->dev)->id == 0)
141 			reg |= AR5K_AR5312_ENABLE_WLAN0;
142 		else
143 			reg |= AR5K_AR5312_ENABLE_WLAN1;
144 		__raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
145 	}
146 
147 	ret = ath5k_init_softc(sc, &ath_ahb_bus_ops);
148 	if (ret != 0) {
149 		dev_err(&pdev->dev, "failed to attach device, err=%d\n", ret);
150 		ret = -ENODEV;
151 		goto err_free_hw;
152 	}
153 
154 	platform_set_drvdata(pdev, hw);
155 
156 	return 0;
157 
158  err_free_hw:
159 	ieee80211_free_hw(hw);
160 	platform_set_drvdata(pdev, NULL);
161  err_out:
162 	return ret;
163 }
164 
165 static int ath_ahb_remove(struct platform_device *pdev)
166 {
167 	struct ar231x_board_config *bcfg = pdev->dev.platform_data;
168 	struct ieee80211_hw *hw = platform_get_drvdata(pdev);
169 	struct ath5k_softc *sc;
170 	u32 reg;
171 
172 	if (!hw)
173 		return 0;
174 
175 	sc = hw->priv;
176 
177 	if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
178 		/* Disable WMAC AHB arbitration */
179 		reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
180 		reg &= ~AR5K_AR2315_AHB_ARB_CTL_WLAN;
181 		__raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
182 	} else {
183 		/*Stop DMA access */
184 		reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
185 		if (to_platform_device(sc->dev)->id == 0)
186 			reg &= ~AR5K_AR5312_ENABLE_WLAN0;
187 		else
188 			reg &= ~AR5K_AR5312_ENABLE_WLAN1;
189 		__raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
190 	}
191 
192 	ath5k_deinit_softc(sc);
193 	platform_set_drvdata(pdev, NULL);
194 
195 	return 0;
196 }
197 
198 static struct platform_driver ath_ahb_driver = {
199 	.probe      = ath_ahb_probe,
200 	.remove     = ath_ahb_remove,
201 	.driver		= {
202 		.name	= "ar231x-wmac",
203 		.owner	= THIS_MODULE,
204 	},
205 };
206 
207 static int __init
208 ath5k_ahb_init(void)
209 {
210 	return platform_driver_register(&ath_ahb_driver);
211 }
212 
213 static void __exit
214 ath5k_ahb_exit(void)
215 {
216 	platform_driver_unregister(&ath_ahb_driver);
217 }
218 
219 module_init(ath5k_ahb_init);
220 module_exit(ath5k_ahb_exit);
221