treewide: remove duplicate includes
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath5k / ahb.c
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
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 int ath5k_ahb_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
66 {
67         struct ath5k_softc *sc = ah->ah_sc;
68         struct platform_device *pdev = to_platform_device(sc->dev);
69         struct ar231x_board_config *bcfg = pdev->dev.platform_data;
70         u8 *cfg_mac;
71
72         if (to_platform_device(sc->dev)->id == 0)
73                 cfg_mac = bcfg->config->wlan0_mac;
74         else
75                 cfg_mac = bcfg->config->wlan1_mac;
76
77         memcpy(mac, cfg_mac, ETH_ALEN);
78         return 0;
79 }
80
81 static const struct ath_bus_ops ath_ahb_bus_ops = {
82         .ath_bus_type = ATH_AHB,
83         .read_cachesize = ath5k_ahb_read_cachesize,
84         .eeprom_read = ath5k_ahb_eeprom_read,
85         .eeprom_read_mac = ath5k_ahb_eeprom_read_mac,
86 };
87
88 /*Initialization*/
89 static int ath_ahb_probe(struct platform_device *pdev)
90 {
91         struct ar231x_board_config *bcfg = pdev->dev.platform_data;
92         struct ath5k_softc *sc;
93         struct ieee80211_hw *hw;
94         struct resource *res;
95         void __iomem *mem;
96         int irq;
97         int ret = 0;
98         u32 reg;
99
100         if (!pdev->dev.platform_data) {
101                 dev_err(&pdev->dev, "no platform data specified\n");
102                 ret = -EINVAL;
103                 goto err_out;
104         }
105
106         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
107         if (res == NULL) {
108                 dev_err(&pdev->dev, "no memory resource found\n");
109                 ret = -ENXIO;
110                 goto err_out;
111         }
112
113         mem = ioremap_nocache(res->start, resource_size(res));
114         if (mem == NULL) {
115                 dev_err(&pdev->dev, "ioremap failed\n");
116                 ret = -ENOMEM;
117                 goto err_out;
118         }
119
120         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
121         if (res == NULL) {
122                 dev_err(&pdev->dev, "no IRQ resource found\n");
123                 ret = -ENXIO;
124                 goto err_out;
125         }
126
127         irq = res->start;
128
129         hw = ieee80211_alloc_hw(sizeof(struct ath5k_softc), &ath5k_hw_ops);
130         if (hw == NULL) {
131                 dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
132                 ret = -ENOMEM;
133                 goto err_out;
134         }
135
136         sc = hw->priv;
137         sc->hw = hw;
138         sc->dev = &pdev->dev;
139         sc->iobase = mem;
140         sc->irq = irq;
141         sc->devid = bcfg->devid;
142
143         if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
144                 /* Enable WMAC AHB arbitration */
145                 reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
146                 reg |= AR5K_AR2315_AHB_ARB_CTL_WLAN;
147                 __raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
148
149                 /* Enable global WMAC swapping */
150                 reg = __raw_readl((void __iomem *) AR5K_AR2315_BYTESWAP);
151                 reg |= AR5K_AR2315_BYTESWAP_WMAC;
152                 __raw_writel(reg, (void __iomem *) AR5K_AR2315_BYTESWAP);
153         } else {
154                 /* Enable WMAC DMA access (assuming 5312 or 231x*/
155                 /* TODO: check other platforms */
156                 reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
157                 if (to_platform_device(sc->dev)->id == 0)
158                         reg |= AR5K_AR5312_ENABLE_WLAN0;
159                 else
160                         reg |= AR5K_AR5312_ENABLE_WLAN1;
161                 __raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
162
163                 /*
164                  * On a dual-band AR5312, the multiband radio is only
165                  * used as pass-through. Disable 2 GHz support in the
166                  * driver for it
167                  */
168                 if (to_platform_device(sc->dev)->id == 0 &&
169                     (bcfg->config->flags & (BD_WLAN0|BD_WLAN1)) ==
170                      (BD_WLAN1|BD_WLAN0))
171                         __set_bit(ATH_STAT_2G_DISABLED, sc->status);
172         }
173
174         ret = ath5k_init_softc(sc, &ath_ahb_bus_ops);
175         if (ret != 0) {
176                 dev_err(&pdev->dev, "failed to attach device, err=%d\n", ret);
177                 ret = -ENODEV;
178                 goto err_free_hw;
179         }
180
181         platform_set_drvdata(pdev, hw);
182
183         return 0;
184
185  err_free_hw:
186         ieee80211_free_hw(hw);
187         platform_set_drvdata(pdev, NULL);
188  err_out:
189         return ret;
190 }
191
192 static int ath_ahb_remove(struct platform_device *pdev)
193 {
194         struct ar231x_board_config *bcfg = pdev->dev.platform_data;
195         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
196         struct ath5k_softc *sc;
197         u32 reg;
198
199         if (!hw)
200                 return 0;
201
202         sc = hw->priv;
203
204         if (bcfg->devid >= AR5K_SREV_AR2315_R6) {
205                 /* Disable WMAC AHB arbitration */
206                 reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
207                 reg &= ~AR5K_AR2315_AHB_ARB_CTL_WLAN;
208                 __raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL);
209         } else {
210                 /*Stop DMA access */
211                 reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE);
212                 if (to_platform_device(sc->dev)->id == 0)
213                         reg &= ~AR5K_AR5312_ENABLE_WLAN0;
214                 else
215                         reg &= ~AR5K_AR5312_ENABLE_WLAN1;
216                 __raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE);
217         }
218
219         ath5k_deinit_softc(sc);
220         platform_set_drvdata(pdev, NULL);
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);