xref: /linux/drivers/net/wireless/ath/ath9k/ahb.c (revision 9ac58615d93c8a28b1c649a90a5e2ede4dfd368a)
1203c4805SLuis R. Rodriguez /*
2203c4805SLuis R. Rodriguez  * Copyright (c) 2008-2009 Atheros Communications Inc.
3203c4805SLuis R. Rodriguez  * Copyright (c) 2009 Gabor Juhos <juhosg@openwrt.org>
4203c4805SLuis R. Rodriguez  * Copyright (c) 2009 Imre Kaloz <kaloz@openwrt.org>
5203c4805SLuis R. Rodriguez  *
6203c4805SLuis R. Rodriguez  * Permission to use, copy, modify, and/or distribute this software for any
7203c4805SLuis R. Rodriguez  * purpose with or without fee is hereby granted, provided that the above
8203c4805SLuis R. Rodriguez  * copyright notice and this permission notice appear in all copies.
9203c4805SLuis R. Rodriguez  *
10203c4805SLuis R. Rodriguez  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11203c4805SLuis R. Rodriguez  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12203c4805SLuis R. Rodriguez  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13203c4805SLuis R. Rodriguez  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14203c4805SLuis R. Rodriguez  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15203c4805SLuis R. Rodriguez  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16203c4805SLuis R. Rodriguez  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17203c4805SLuis R. Rodriguez  */
18203c4805SLuis R. Rodriguez 
19203c4805SLuis R. Rodriguez #include <linux/nl80211.h>
20203c4805SLuis R. Rodriguez #include <linux/platform_device.h>
21203c4805SLuis R. Rodriguez #include <linux/ath9k_platform.h>
22203c4805SLuis R. Rodriguez #include "ath9k.h"
23203c4805SLuis R. Rodriguez 
24203c4805SLuis R. Rodriguez /* return bus cachesize in 4B word units */
255bb12791SLuis R. Rodriguez static void ath_ahb_read_cachesize(struct ath_common *common, int *csz)
26203c4805SLuis R. Rodriguez {
27203c4805SLuis R. Rodriguez 	*csz = L1_CACHE_BYTES >> 2;
28203c4805SLuis R. Rodriguez }
29203c4805SLuis R. Rodriguez 
305bb12791SLuis R. Rodriguez static bool ath_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
31203c4805SLuis R. Rodriguez {
32e307fcf0SMarek Lindner 	struct ath_softc *sc = (struct ath_softc *)common->priv;
33203c4805SLuis R. Rodriguez 	struct platform_device *pdev = to_platform_device(sc->dev);
34203c4805SLuis R. Rodriguez 	struct ath9k_platform_data *pdata;
35203c4805SLuis R. Rodriguez 
36203c4805SLuis R. Rodriguez 	pdata = (struct ath9k_platform_data *) pdev->dev.platform_data;
37203c4805SLuis R. Rodriguez 	if (off >= (ARRAY_SIZE(pdata->eeprom_data))) {
383800276aSJoe Perches 		ath_err(common,
393800276aSJoe Perches 			"%s: flash read failed, offset %08x is out of range\n",
40203c4805SLuis R. Rodriguez 			__func__, off);
41203c4805SLuis R. Rodriguez 		return false;
42203c4805SLuis R. Rodriguez 	}
43203c4805SLuis R. Rodriguez 
44203c4805SLuis R. Rodriguez 	*data = pdata->eeprom_data[off];
45203c4805SLuis R. Rodriguez 	return true;
46203c4805SLuis R. Rodriguez }
47203c4805SLuis R. Rodriguez 
48203c4805SLuis R. Rodriguez static struct ath_bus_ops ath_ahb_bus_ops  = {
49497ad9adSSujith 	.ath_bus_type = ATH_AHB,
50203c4805SLuis R. Rodriguez 	.read_cachesize = ath_ahb_read_cachesize,
51203c4805SLuis R. Rodriguez 	.eeprom_read = ath_ahb_eeprom_read,
52203c4805SLuis R. Rodriguez };
53203c4805SLuis R. Rodriguez 
54203c4805SLuis R. Rodriguez static int ath_ahb_probe(struct platform_device *pdev)
55203c4805SLuis R. Rodriguez {
56203c4805SLuis R. Rodriguez 	void __iomem *mem;
57203c4805SLuis R. Rodriguez 	struct ath_softc *sc;
58203c4805SLuis R. Rodriguez 	struct ieee80211_hw *hw;
59203c4805SLuis R. Rodriguez 	struct resource *res;
60203c4805SLuis R. Rodriguez 	int irq;
61203c4805SLuis R. Rodriguez 	int ret = 0;
62203c4805SLuis R. Rodriguez 	struct ath_hw *ah;
63f934c4d9SLuis R. Rodriguez 	char hw_name[64];
64203c4805SLuis R. Rodriguez 
65203c4805SLuis R. Rodriguez 	if (!pdev->dev.platform_data) {
66203c4805SLuis R. Rodriguez 		dev_err(&pdev->dev, "no platform data specified\n");
67203c4805SLuis R. Rodriguez 		ret = -EINVAL;
68203c4805SLuis R. Rodriguez 		goto err_out;
69203c4805SLuis R. Rodriguez 	}
70203c4805SLuis R. Rodriguez 
71203c4805SLuis R. Rodriguez 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72203c4805SLuis R. Rodriguez 	if (res == NULL) {
73203c4805SLuis R. Rodriguez 		dev_err(&pdev->dev, "no memory resource found\n");
74203c4805SLuis R. Rodriguez 		ret = -ENXIO;
75203c4805SLuis R. Rodriguez 		goto err_out;
76203c4805SLuis R. Rodriguez 	}
77203c4805SLuis R. Rodriguez 
78203c4805SLuis R. Rodriguez 	mem = ioremap_nocache(res->start, res->end - res->start + 1);
79203c4805SLuis R. Rodriguez 	if (mem == NULL) {
80203c4805SLuis R. Rodriguez 		dev_err(&pdev->dev, "ioremap failed\n");
81203c4805SLuis R. Rodriguez 		ret = -ENOMEM;
82203c4805SLuis R. Rodriguez 		goto err_out;
83203c4805SLuis R. Rodriguez 	}
84203c4805SLuis R. Rodriguez 
85203c4805SLuis R. Rodriguez 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
86203c4805SLuis R. Rodriguez 	if (res == NULL) {
87203c4805SLuis R. Rodriguez 		dev_err(&pdev->dev, "no IRQ resource found\n");
88203c4805SLuis R. Rodriguez 		ret = -ENXIO;
89203c4805SLuis R. Rodriguez 		goto err_iounmap;
90203c4805SLuis R. Rodriguez 	}
91203c4805SLuis R. Rodriguez 
92203c4805SLuis R. Rodriguez 	irq = res->start;
93203c4805SLuis R. Rodriguez 
94*9ac58615SFelix Fietkau 	hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
95203c4805SLuis R. Rodriguez 	if (hw == NULL) {
96203c4805SLuis R. Rodriguez 		dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
97203c4805SLuis R. Rodriguez 		ret = -ENOMEM;
98203c4805SLuis R. Rodriguez 		goto err_iounmap;
99203c4805SLuis R. Rodriguez 	}
100203c4805SLuis R. Rodriguez 
101203c4805SLuis R. Rodriguez 	SET_IEEE80211_DEV(hw, &pdev->dev);
102203c4805SLuis R. Rodriguez 	platform_set_drvdata(pdev, hw);
103203c4805SLuis R. Rodriguez 
104*9ac58615SFelix Fietkau 	sc = hw->priv;
105203c4805SLuis R. Rodriguez 	sc->hw = hw;
106203c4805SLuis R. Rodriguez 	sc->dev = &pdev->dev;
107203c4805SLuis R. Rodriguez 	sc->mem = mem;
108203c4805SLuis R. Rodriguez 	sc->irq = irq;
109203c4805SLuis R. Rodriguez 
1105e4ea1f0SSujith 	/* Will be cleared in ath9k_start() */
1115e4ea1f0SSujith 	sc->sc_flags |= SC_OP_INVALID;
1125e4ea1f0SSujith 
113203c4805SLuis R. Rodriguez 	ret = request_irq(irq, ath_isr, IRQF_SHARED, "ath9k", sc);
114203c4805SLuis R. Rodriguez 	if (ret) {
115580171f7SLuis R. Rodriguez 		dev_err(&pdev->dev, "request_irq failed\n");
116285f2ddaSSujith 		goto err_free_hw;
117285f2ddaSSujith 	}
118285f2ddaSSujith 
119285f2ddaSSujith 	ret = ath9k_init_device(AR5416_AR9100_DEVID, sc, 0x0, &ath_ahb_bus_ops);
120285f2ddaSSujith 	if (ret) {
121285f2ddaSSujith 		dev_err(&pdev->dev, "failed to initialize device\n");
122285f2ddaSSujith 		goto err_irq;
123203c4805SLuis R. Rodriguez 	}
124203c4805SLuis R. Rodriguez 
125203c4805SLuis R. Rodriguez 	ah = sc->sc_ah;
126f934c4d9SLuis R. Rodriguez 	ath9k_hw_name(ah, hw_name, sizeof(hw_name));
127c96c31e4SJoe Perches 	wiphy_info(hw->wiphy, "%s mem=0x%lx, irq=%d\n",
128c96c31e4SJoe Perches 		   hw_name, (unsigned long)mem, irq);
129203c4805SLuis R. Rodriguez 
130203c4805SLuis R. Rodriguez 	return 0;
131203c4805SLuis R. Rodriguez 
132285f2ddaSSujith  err_irq:
133285f2ddaSSujith 	free_irq(irq, sc);
134203c4805SLuis R. Rodriguez  err_free_hw:
135203c4805SLuis R. Rodriguez 	ieee80211_free_hw(hw);
136203c4805SLuis R. Rodriguez 	platform_set_drvdata(pdev, NULL);
137203c4805SLuis R. Rodriguez  err_iounmap:
138203c4805SLuis R. Rodriguez 	iounmap(mem);
139203c4805SLuis R. Rodriguez  err_out:
140203c4805SLuis R. Rodriguez 	return ret;
141203c4805SLuis R. Rodriguez }
142203c4805SLuis R. Rodriguez 
143203c4805SLuis R. Rodriguez static int ath_ahb_remove(struct platform_device *pdev)
144203c4805SLuis R. Rodriguez {
145203c4805SLuis R. Rodriguez 	struct ieee80211_hw *hw = platform_get_drvdata(pdev);
146203c4805SLuis R. Rodriguez 
147203c4805SLuis R. Rodriguez 	if (hw) {
148*9ac58615SFelix Fietkau 		struct ath_softc *sc = hw->priv;
149ab5132a2SPavel Roskin 		void __iomem *mem = sc->mem;
150203c4805SLuis R. Rodriguez 
151285f2ddaSSujith 		ath9k_deinit_device(sc);
152285f2ddaSSujith 		free_irq(sc->irq, sc);
153285f2ddaSSujith 		ieee80211_free_hw(sc->hw);
154ab5132a2SPavel Roskin 		iounmap(mem);
155203c4805SLuis R. Rodriguez 		platform_set_drvdata(pdev, NULL);
156203c4805SLuis R. Rodriguez 	}
157203c4805SLuis R. Rodriguez 
158203c4805SLuis R. Rodriguez 	return 0;
159203c4805SLuis R. Rodriguez }
160203c4805SLuis R. Rodriguez 
161203c4805SLuis R. Rodriguez static struct platform_driver ath_ahb_driver = {
162203c4805SLuis R. Rodriguez 	.probe      = ath_ahb_probe,
163203c4805SLuis R. Rodriguez 	.remove     = ath_ahb_remove,
164203c4805SLuis R. Rodriguez 	.driver		= {
165203c4805SLuis R. Rodriguez 		.name	= "ath9k",
166203c4805SLuis R. Rodriguez 		.owner	= THIS_MODULE,
167203c4805SLuis R. Rodriguez 	},
168203c4805SLuis R. Rodriguez };
169203c4805SLuis R. Rodriguez 
170203c4805SLuis R. Rodriguez int ath_ahb_init(void)
171203c4805SLuis R. Rodriguez {
172203c4805SLuis R. Rodriguez 	return platform_driver_register(&ath_ahb_driver);
173203c4805SLuis R. Rodriguez }
174203c4805SLuis R. Rodriguez 
175203c4805SLuis R. Rodriguez void ath_ahb_exit(void)
176203c4805SLuis R. Rodriguez {
177203c4805SLuis R. Rodriguez 	platform_driver_unregister(&ath_ahb_driver);
178203c4805SLuis R. Rodriguez }
179