1/*
2 * Copyright (c) 2008-2011 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/ath9k_platform.h>
22#include <linux/module.h>
23#include "ath9k.h"
24
25static const struct platform_device_id ath9k_platform_id_table[] = {
26	{
27		.name = "ath9k",
28		.driver_data = AR5416_AR9100_DEVID,
29	},
30	{
31		.name = "ar933x_wmac",
32		.driver_data = AR9300_DEVID_AR9330,
33	},
34	{
35		.name = "ar934x_wmac",
36		.driver_data = AR9300_DEVID_AR9340,
37	},
38	{},
39};
40
41/* return bus cachesize in 4B word units */
42static void ath_ahb_read_cachesize(struct ath_common *common, int *csz)
43{
44	*csz = L1_CACHE_BYTES >> 2;
45}
46
47static bool ath_ahb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
48{
49	struct ath_softc *sc = (struct ath_softc *)common->priv;
50	struct platform_device *pdev = to_platform_device(sc->dev);
51	struct ath9k_platform_data *pdata;
52
53	pdata = (struct ath9k_platform_data *) pdev->dev.platform_data;
54	if (off >= (ARRAY_SIZE(pdata->eeprom_data))) {
55		ath_err(common,
56			"%s: flash read failed, offset %08x is out of range\n",
57			__func__, off);
58		return false;
59	}
60
61	*data = pdata->eeprom_data[off];
62	return true;
63}
64
65static struct ath_bus_ops ath_ahb_bus_ops  = {
66	.ath_bus_type = ATH_AHB,
67	.read_cachesize = ath_ahb_read_cachesize,
68	.eeprom_read = ath_ahb_eeprom_read,
69};
70
71static int ath_ahb_probe(struct platform_device *pdev)
72{
73	void __iomem *mem;
74	struct ath_softc *sc;
75	struct ieee80211_hw *hw;
76	struct resource *res;
77	const struct platform_device_id *id = platform_get_device_id(pdev);
78	int irq;
79	int ret = 0;
80	struct ath_hw *ah;
81	char hw_name[64];
82
83	if (!pdev->dev.platform_data) {
84		dev_err(&pdev->dev, "no platform data specified\n");
85		ret = -EINVAL;
86		goto err_out;
87	}
88
89	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90	if (res == NULL) {
91		dev_err(&pdev->dev, "no memory resource found\n");
92		ret = -ENXIO;
93		goto err_out;
94	}
95
96	mem = ioremap_nocache(res->start, resource_size(res));
97	if (mem == NULL) {
98		dev_err(&pdev->dev, "ioremap failed\n");
99		ret = -ENOMEM;
100		goto err_out;
101	}
102
103	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
104	if (res == NULL) {
105		dev_err(&pdev->dev, "no IRQ resource found\n");
106		ret = -ENXIO;
107		goto err_iounmap;
108	}
109
110	irq = res->start;
111
112	hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
113	if (hw == NULL) {
114		dev_err(&pdev->dev, "no memory for ieee80211_hw\n");
115		ret = -ENOMEM;
116		goto err_iounmap;
117	}
118
119	SET_IEEE80211_DEV(hw, &pdev->dev);
120	platform_set_drvdata(pdev, hw);
121
122	sc = hw->priv;
123	sc->hw = hw;
124	sc->dev = &pdev->dev;
125	sc->mem = mem;
126	sc->irq = irq;
127
128	/* Will be cleared in ath9k_start() */
129	sc->sc_flags |= SC_OP_INVALID;
130
131	ret = request_irq(irq, ath_isr, IRQF_SHARED, "ath9k", sc);
132	if (ret) {
133		dev_err(&pdev->dev, "request_irq failed\n");
134		goto err_free_hw;
135	}
136
137	ret = ath9k_init_device(id->driver_data, sc, &ath_ahb_bus_ops);
138	if (ret) {
139		dev_err(&pdev->dev, "failed to initialize device\n");
140		goto err_irq;
141	}
142
143	ah = sc->sc_ah;
144	ath9k_hw_name(ah, hw_name, sizeof(hw_name));
145	wiphy_info(hw->wiphy, "%s mem=0x%lx, irq=%d\n",
146		   hw_name, (unsigned long)mem, irq);
147
148	return 0;
149
150 err_irq:
151	free_irq(irq, sc);
152 err_free_hw:
153	ieee80211_free_hw(hw);
154	platform_set_drvdata(pdev, NULL);
155 err_iounmap:
156	iounmap(mem);
157 err_out:
158	return ret;
159}
160
161static int ath_ahb_remove(struct platform_device *pdev)
162{
163	struct ieee80211_hw *hw = platform_get_drvdata(pdev);
164
165	if (hw) {
166		struct ath_softc *sc = hw->priv;
167		void __iomem *mem = sc->mem;
168
169		ath9k_deinit_device(sc);
170		free_irq(sc->irq, sc);
171		ieee80211_free_hw(sc->hw);
172		iounmap(mem);
173		platform_set_drvdata(pdev, NULL);
174	}
175
176	return 0;
177}
178
179static struct platform_driver ath_ahb_driver = {
180	.probe      = ath_ahb_probe,
181	.remove     = ath_ahb_remove,
182	.driver		= {
183		.name	= "ath9k",
184		.owner	= THIS_MODULE,
185	},
186	.id_table    = ath9k_platform_id_table,
187};
188
189MODULE_DEVICE_TABLE(platform, ath9k_platform_id_table);
190
191int ath_ahb_init(void)
192{
193	return platform_driver_register(&ath_ahb_driver);
194}
195
196void ath_ahb_exit(void)
197{
198	platform_driver_unregister(&ath_ahb_driver);
199}
200