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> |
Jonathan Bither | 32b92f4 | 2012-04-12 06:13:33 -0400 | [diff] [blame^] | 22 | #include <linux/export.h> |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 23 | #include <ar231x_platform.h> |
| 24 | #include "ath5k.h" |
| 25 | #include "debug.h" |
| 26 | #include "base.h" |
| 27 | #include "reg.h" |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 38 | struct ath5k_hw *ah = common->priv; |
| 39 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 59 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 60 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
| 61 | ah->ah_mac_srev = bcfg->devid; |
| 62 | return 0; |
| 63 | } |
| 64 | |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 65 | static int ath5k_ahb_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac) |
| 66 | { |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 67 | struct platform_device *pdev = to_platform_device(ah->dev); |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 68 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
| 69 | u8 *cfg_mac; |
| 70 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 71 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 72 | cfg_mac = bcfg->config->wlan0_mac; |
| 73 | else |
| 74 | cfg_mac = bcfg->config->wlan1_mac; |
| 75 | |
| 76 | memcpy(mac, cfg_mac, ETH_ALEN); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 80 | static const struct ath_bus_ops ath_ahb_bus_ops = { |
| 81 | .ath_bus_type = ATH_AHB, |
| 82 | .read_cachesize = ath5k_ahb_read_cachesize, |
| 83 | .eeprom_read = ath5k_ahb_eeprom_read, |
Felix Fietkau | fa9bfd6 | 2011-04-13 21:56:44 +0200 | [diff] [blame] | 84 | .eeprom_read_mac = ath5k_ahb_eeprom_read_mac, |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /*Initialization*/ |
| 88 | static int ath_ahb_probe(struct platform_device *pdev) |
| 89 | { |
| 90 | struct ar231x_board_config *bcfg = pdev->dev.platform_data; |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 91 | struct ath5k_hw *ah; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 92 | struct ieee80211_hw *hw; |
| 93 | struct resource *res; |
| 94 | void __iomem *mem; |
| 95 | int irq; |
| 96 | int ret = 0; |
| 97 | u32 reg; |
| 98 | |
| 99 | if (!pdev->dev.platform_data) { |
| 100 | dev_err(&pdev->dev, "no platform data specified\n"); |
| 101 | ret = -EINVAL; |
| 102 | goto err_out; |
| 103 | } |
| 104 | |
| 105 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 106 | if (res == NULL) { |
| 107 | dev_err(&pdev->dev, "no memory resource found\n"); |
| 108 | ret = -ENXIO; |
| 109 | goto err_out; |
| 110 | } |
| 111 | |
Shan Wei | 9ac4793 | 2011-03-07 15:18:11 +0800 | [diff] [blame] | 112 | mem = ioremap_nocache(res->start, resource_size(res)); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 113 | if (mem == NULL) { |
| 114 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 115 | ret = -ENOMEM; |
| 116 | goto err_out; |
| 117 | } |
| 118 | |
| 119 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 120 | if (res == NULL) { |
| 121 | dev_err(&pdev->dev, "no IRQ resource found\n"); |
| 122 | ret = -ENXIO; |
| 123 | goto err_out; |
| 124 | } |
| 125 | |
| 126 | irq = res->start; |
| 127 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 128 | hw = ieee80211_alloc_hw(sizeof(struct ath5k_hw), &ath5k_hw_ops); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 129 | if (hw == NULL) { |
| 130 | dev_err(&pdev->dev, "no memory for ieee80211_hw\n"); |
| 131 | ret = -ENOMEM; |
| 132 | goto err_out; |
| 133 | } |
| 134 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 135 | ah = hw->priv; |
| 136 | ah->hw = hw; |
| 137 | ah->dev = &pdev->dev; |
| 138 | ah->iobase = mem; |
| 139 | ah->irq = irq; |
| 140 | ah->devid = bcfg->devid; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 141 | |
| 142 | if (bcfg->devid >= AR5K_SREV_AR2315_R6) { |
| 143 | /* Enable WMAC AHB arbitration */ |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 144 | reg = ioread32((void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 145 | reg |= AR5K_AR2315_AHB_ARB_CTL_WLAN; |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 146 | iowrite32(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 147 | |
| 148 | /* Enable global WMAC swapping */ |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 149 | reg = ioread32((void __iomem *) AR5K_AR2315_BYTESWAP); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 150 | reg |= AR5K_AR2315_BYTESWAP_WMAC; |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 151 | iowrite32(reg, (void __iomem *) AR5K_AR2315_BYTESWAP); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 152 | } else { |
| 153 | /* Enable WMAC DMA access (assuming 5312 or 231x*/ |
| 154 | /* TODO: check other platforms */ |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 155 | reg = ioread32((void __iomem *) AR5K_AR5312_ENABLE); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 156 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 157 | reg |= AR5K_AR5312_ENABLE_WLAN0; |
| 158 | else |
| 159 | reg |= AR5K_AR5312_ENABLE_WLAN1; |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 160 | iowrite32(reg, (void __iomem *) AR5K_AR5312_ENABLE); |
Felix Fietkau | 3a9ddde | 2011-04-13 21:56:46 +0200 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * On a dual-band AR5312, the multiband radio is only |
| 164 | * used as pass-through. Disable 2 GHz support in the |
| 165 | * driver for it |
| 166 | */ |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 167 | if (to_platform_device(ah->dev)->id == 0 && |
Pavel Roskin | e4bbf2f | 2011-07-07 18:14:13 -0400 | [diff] [blame] | 168 | (bcfg->config->flags & (BD_WLAN0 | BD_WLAN1)) == |
| 169 | (BD_WLAN1 | BD_WLAN0)) |
Nick Kossifidis | 86f62d9 | 2011-11-25 20:40:28 +0200 | [diff] [blame] | 170 | ah->ah_capabilities.cap_needs_2GHz_ovr = true; |
| 171 | else |
| 172 | ah->ah_capabilities.cap_needs_2GHz_ovr = false; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Pavel Roskin | bb1f3ad | 2011-07-26 22:27:05 -0400 | [diff] [blame] | 175 | ret = ath5k_init_ah(ah, &ath_ahb_bus_ops); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 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); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 197 | struct ath5k_hw *ah; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 198 | u32 reg; |
| 199 | |
| 200 | if (!hw) |
| 201 | return 0; |
| 202 | |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 203 | ah = hw->priv; |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 204 | |
| 205 | if (bcfg->devid >= AR5K_SREV_AR2315_R6) { |
| 206 | /* Disable WMAC AHB arbitration */ |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 207 | reg = ioread32((void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 208 | reg &= ~AR5K_AR2315_AHB_ARB_CTL_WLAN; |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 209 | iowrite32(reg, (void __iomem *) AR5K_AR2315_AHB_ARB_CTL); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 210 | } else { |
| 211 | /*Stop DMA access */ |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 212 | reg = ioread32((void __iomem *) AR5K_AR5312_ENABLE); |
Pavel Roskin | e0d687b | 2011-07-14 20:21:55 -0400 | [diff] [blame] | 213 | if (to_platform_device(ah->dev)->id == 0) |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 214 | reg &= ~AR5K_AR5312_ENABLE_WLAN0; |
| 215 | else |
| 216 | reg &= ~AR5K_AR5312_ENABLE_WLAN1; |
Jonathan Bither | cede8b6 | 2012-02-13 21:47:45 -0500 | [diff] [blame] | 217 | iowrite32(reg, (void __iomem *) AR5K_AR5312_ENABLE); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Pavel Roskin | bb1f3ad | 2011-07-26 22:27:05 -0400 | [diff] [blame] | 220 | ath5k_deinit_ah(ah); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 221 | platform_set_drvdata(pdev, NULL); |
Pavel Roskin | 536e518 | 2011-06-17 17:59:02 -0400 | [diff] [blame] | 222 | ieee80211_free_hw(hw); |
Felix Fietkau | a0b907e | 2010-12-02 10:27:16 +0100 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static struct platform_driver ath_ahb_driver = { |
| 228 | .probe = ath_ahb_probe, |
| 229 | .remove = ath_ahb_remove, |
| 230 | .driver = { |
| 231 | .name = "ar231x-wmac", |
| 232 | .owner = THIS_MODULE, |
| 233 | }, |
| 234 | }; |
| 235 | |
| 236 | static int __init |
| 237 | ath5k_ahb_init(void) |
| 238 | { |
| 239 | return platform_driver_register(&ath_ahb_driver); |
| 240 | } |
| 241 | |
| 242 | static void __exit |
| 243 | ath5k_ahb_exit(void) |
| 244 | { |
| 245 | platform_driver_unregister(&ath_ahb_driver); |
| 246 | } |
| 247 | |
| 248 | module_init(ath5k_ahb_init); |
| 249 | module_exit(ath5k_ahb_exit); |