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" |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 34 | static bool |
| 35 | ath5k_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 36 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 37 | struct ath5k_hw *ah = common->priv; |
| 38 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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) |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 50 | return false; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 51 | |
| 52 | *data = *eeprom; |
Wojciech Dubowik | fda9b7a | 2011-01-11 09:00:31 +0100 | [diff] [blame] | 53 | return true; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int ath5k_hw_read_srev(struct ath5k_hw *ah) |
| 57 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 58 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 59 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
| 60 | ah->ah_mac_srev = bcfg->devid; |
| 61 | return 0; |
| 62 | } |
| 63 | |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 64 | static int ath5k_ahb_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac) |
| 65 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 66 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 67 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
| 68 | u8 *cfg_mac; |
| 69 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 70 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 71 | cfg_mac = bcfg->config->wlan0_mac; |
| 72 | else |
| 73 | cfg_mac = bcfg->config->wlan1_mac; |
| 74 | |
| 75 | memcpy(mac, cfg_mac, ETH_ALEN); |
| 76 | return 0; |
| 77 | } |
| 78 | |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 79 | static const struct ath_bus_ops ath_ahb_bus_ops = { |
| 80 | .ath_bus_type = ATH_AHB, |
| 81 | .read_cachesize = ath5k_ahb_read_cachesize, |
| 82 | .eeprom_read = ath5k_ahb_eeprom_read, |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 83 | .eeprom_read_mac = ath5k_ahb_eeprom_read_mac, |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /*Initialization*/ |
| 87 | static int ath_ahb_probe(struct platform_device *pdev) |
| 88 | { |
| 89 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 90 | struct ath5k_hw *ah; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 91 | struct ieee80211_hw *hw; |
| 92 | struct resource *res; |
| 93 | void __iomem *mem; |
| 94 | int irq; |
| 95 | int ret = 0; |
| 96 | u32 reg; |
| 97 | |
| 98 | if (!pdev->dev.platform_data) { |
| 99 | dev_err(&pdev->dev, "no platform data specified\n"); |
| 100 | ret = -EINVAL; |
| 101 | goto err_out; |
| 102 | } |
| 103 | |
| 104 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 105 | if (res == NULL) { |
| 106 | dev_err(&pdev->dev, "no memory resource found\n"); |
| 107 | ret = -ENXIO; |
| 108 | goto err_out; |
| 109 | } |
| 110 | |
Shan Wei | 9ac4793 | 2011-03-07 15:18:11 +0800 | [diff] [blame] | 111 | mem = ioremap_nocache(res->start, resource_size(res)); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 112 | if (mem == NULL) { |
| 113 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 114 | ret = -ENOMEM; |
| 115 | goto err_out; |
| 116 | } |
| 117 | |
| 118 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 119 | if (res == NULL) { |
| 120 | dev_err(&pdev->dev, "no IRQ resource found\n"); |
| 121 | ret = -ENXIO; |
| 122 | goto err_out; |
| 123 | } |
| 124 | |
| 125 | irq = res->start; |
| 126 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 127 | hw = ieee80211_alloc_hw(sizeof(struct ath5k_hw), &ath5k_hw_ops); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 128 | if (hw == NULL) { |
| 129 | dev_err(&pdev->dev, "no memory for ieee80211_hw\n"); |
| 130 | ret = -ENOMEM; |
| 131 | goto err_out; |
| 132 | } |
| 133 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 134 | ah = hw->priv; |
| 135 | ah->hw = hw; |
| 136 | ah->dev = &pdev->dev; |
| 137 | ah->iobase = mem; |
| 138 | ah->irq = irq; |
| 139 | ah->devid = bcfg->devid; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 140 | |
| 141 | if (bcfg->devid >= AR5K_SREV_AR2315_R6) { |
| 142 | /* Enable WMAC AHB arbitration */ |
| 143 | reg = __raw_readl((void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
| 144 | reg |= AR5K_AR2315_AHB_ARB_CTL_WLAN; |
| 145 | __raw_writel(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
| 146 | |
| 147 | /* Enable global WMAC swapping */ |
| 148 | reg = __raw_readl((void __iomem *) AR5K_AR2315_BYTESWAP); |
| 149 | reg |= AR5K_AR2315_BYTESWAP_WMAC; |
| 150 | __raw_writel(reg, (void __iomem *) AR5K_AR2315_BYTESWAP); |
| 151 | } else { |
| 152 | /* Enable WMAC DMA access (assuming 5312 or 231x*/ |
| 153 | /* TODO: check other platforms */ |
| 154 | reg = __raw_readl((void __iomem *) AR5K_AR5312_ENABLE); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 155 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 156 | reg |= AR5K_AR5312_ENABLE_WLAN0; |
| 157 | else |
| 158 | reg |= AR5K_AR5312_ENABLE_WLAN1; |
| 159 | __raw_writel(reg, (void __iomem *) AR5K_AR5312_ENABLE); |
Felix Fietkau | 3a9ddde | 2011-04-13 21:56:46 +0200 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * On a dual-band AR5312, the multiband radio is only |
| 163 | * used as pass-through. Disable 2 GHz support in the |
| 164 | * driver for it |
| 165 | */ |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 166 | if (to_platform_device(ah->dev)->id == 0 && |
Pavel Roskin | e4bbf2f | 2011-07-07 18:14:13 -0400 | [diff] [blame] | 167 | (bcfg->config->flags & (BD_WLAN0 | BD_WLAN1)) == |
| 168 | (BD_WLAN1 | BD_WLAN0)) |
Nick Kossifidis | 86f62d9 | 2011-11-25 20:40:28 +0200 | [diff] [blame] | 169 | ah->ah_capabilities.cap_needs_2GHz_ovr = true; |
| 170 | else |
| 171 | ah->ah_capabilities.cap_needs_2GHz_ovr = false; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Pavel Roskin | bb1f3ad | 2011-07-26 22:27:05 -0400 | [diff] [blame] | 174 | ret = ath5k_init_ah(ah, &ath_ahb_bus_ops); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 196 | struct ath5k_hw *ah; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 197 | u32 reg; |
| 198 | |
| 199 | if (!hw) |
| 200 | return 0; |
| 201 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 202 | ah = hw->priv; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 212 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | |
Pavel Roskin | bb1f3ad | 2011-07-26 22:27:05 -0400 | [diff] [blame] | 219 | ath5k_deinit_ah(ah); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 220 | platform_set_drvdata(pdev, NULL); |
Pavel Roskin | 536e518 | 2011-06-17 17:59:02 -0400 | [diff] [blame] | 221 | ieee80211_free_hw(hw); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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); |