Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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> |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame^] | 21 | #include <linux/etherdevice.h> |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 35 | static bool |
| 36 | ath5k_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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) |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 51 | return false; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 52 | |
| 53 | *data = *eeprom; |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 54 | return true; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame^] | 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 | |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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, |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame^] | 86 | .eeprom_read_mac = ath5k_ahb_eeprom_read_mac, |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | |
Shan Wei | 9ac4793 | 2011-03-07 15:18:11 +0800 | [diff] [blame] | 114 | mem = ioremap_nocache(res->start, resource_size(res)); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | ret = ath5k_init_softc(sc, &ath_ahb_bus_ops); |
| 166 | if (ret != 0) { |
| 167 | dev_err(&pdev->dev, "failed to attach device, err=%d\n", ret); |
| 168 | ret = -ENODEV; |
| 169 | goto err_free_hw; |
| 170 | } |
| 171 | |
| 172 | platform_set_drvdata(pdev, hw); |
| 173 | |
| 174 | return 0; |
| 175 | |
| 176 | err_free_hw: |
| 177 | ieee80211_free_hw(hw); |
| 178 | platform_set_drvdata(pdev, NULL); |
| 179 | err_out: |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int ath_ahb_remove(struct platform_device *pdev) |
| 184 | { |
| 185 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
| 186 | struct ieee80211_hw *hw = platform_get_drvdata(pdev); |
| 187 | struct ath5k_softc *sc; |
| 188 | u32 reg; |
| 189 | |
| 190 | if (!hw) |
| 191 | return 0; |
| 192 | |
| 193 | sc = hw->priv; |
| 194 | |
| 195 | if (bcfg->devid >= AR5K_SREV_AR2315_R6) { |
| 196 | /* Disable WMAC AHB arbitration */ |
| 197 | reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
| 198 | reg &= ~AR5K_AR2315_AHB_ARB_CTL_WLAN; |
| 199 | __raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
| 200 | } else { |
| 201 | /*Stop DMA access */ |
| 202 | reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE); |
| 203 | if (to_platform_device(sc->dev)->id == 0) |
| 204 | reg &= ~AR5K_AR5312_ENABLE_WLAN0; |
| 205 | else |
| 206 | reg &= ~AR5K_AR5312_ENABLE_WLAN1; |
| 207 | __raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE); |
| 208 | } |
| 209 | |
| 210 | ath5k_deinit_softc(sc); |
| 211 | platform_set_drvdata(pdev, NULL); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static struct platform_driver ath_ahb_driver = { |
| 217 | .probe = ath_ahb_probe, |
| 218 | .remove = ath_ahb_remove, |
| 219 | .driver = { |
| 220 | .name = "ar231x-wmac", |
| 221 | .owner = THIS_MODULE, |
| 222 | }, |
| 223 | }; |
| 224 | |
| 225 | static int __init |
| 226 | ath5k_ahb_init(void) |
| 227 | { |
| 228 | return platform_driver_register(&ath_ahb_driver); |
| 229 | } |
| 230 | |
| 231 | static void __exit |
| 232 | ath5k_ahb_exit(void) |
| 233 | { |
| 234 | platform_driver_unregister(&ath_ahb_driver); |
| 235 | } |
| 236 | |
| 237 | module_init(ath5k_ahb_init); |
| 238 | module_exit(ath5k_ahb_exit); |