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