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