blob: 5582dca9f7f5f5f21f04881a83c457c0a273a197 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* airport.c
2 *
3 * A driver for "Hermes" chipset based Apple Airport wireless
4 * card.
5 *
6 * Copyright notice & release notes in file orinoco.c
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +03007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Note specific to airport stub:
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +03009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 0.05 : first version of the new split driver
11 * 0.06 : fix possible hang on powerup, add sleep support
12 */
13
14#define DRIVER_NAME "airport"
15#define PFX DRIVER_NAME ": "
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
Pavel Roskinef846bf2005-09-23 04:18:07 -040020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/pmac_feature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "orinoco.h"
24
25#define AIRPORT_IO_LEN (0x1000) /* one page */
26
27struct airport {
28 struct macio_dev *mdev;
29 void __iomem *vaddr;
30 int irq_requested;
31 int ndev_registered;
32};
33
34static int
Pavel Machek05adc3b2005-04-16 15:25:25 -070035airport_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
38 struct orinoco_private *priv = netdev_priv(dev);
39 unsigned long flags;
40 int err;
41
42 printk(KERN_DEBUG "%s: Airport entering sleep mode\n", dev->name);
43
44 err = orinoco_lock(priv, &flags);
45 if (err) {
46 printk(KERN_ERR "%s: hw_unavailable on PBOOK_SLEEP_NOW\n",
47 dev->name);
48 return 0;
49 }
50
51 err = __orinoco_down(dev);
52 if (err)
53 printk(KERN_WARNING "%s: PBOOK_SLEEP_NOW: Error %d downing interface\n",
54 dev->name, err);
55
56 netif_device_detach(dev);
57
58 priv->hw_unavailable++;
59
60 orinoco_unlock(priv, &flags);
61
62 disable_irq(dev->irq);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030063 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
64 macio_get_of_node(mdev), 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 return 0;
67}
68
69static int
70airport_resume(struct macio_dev *mdev)
71{
72 struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
73 struct orinoco_private *priv = netdev_priv(dev);
74 unsigned long flags;
75 int err;
76
77 printk(KERN_DEBUG "%s: Airport waking up\n", dev->name);
78
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030079 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
80 macio_get_of_node(mdev), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 msleep(200);
82
83 enable_irq(dev->irq);
84
85 err = orinoco_reinit_firmware(dev);
86 if (err) {
87 printk(KERN_ERR "%s: Error %d re-initializing firmware on PBOOK_WAKE\n",
88 dev->name, err);
89 return 0;
90 }
91
92 spin_lock_irqsave(&priv->lock, flags);
93
94 netif_device_attach(dev);
95
96 priv->hw_unavailable--;
97
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030098 if (priv->open && (!priv->hw_unavailable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 err = __orinoco_up(dev);
100 if (err)
101 printk(KERN_ERR "%s: Error %d restarting card on PBOOK_WAKE\n",
102 dev->name, err);
103 }
104
105
106 spin_unlock_irqrestore(&priv->lock, flags);
107
108 return 0;
109}
110
111static int
112airport_detach(struct macio_dev *mdev)
113{
114 struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
115 struct orinoco_private *priv = netdev_priv(dev);
116 struct airport *card = priv->card;
117
118 if (card->ndev_registered)
119 unregister_netdev(dev);
120 card->ndev_registered = 0;
121
122 if (card->irq_requested)
123 free_irq(dev->irq, dev);
124 card->irq_requested = 0;
125
126 if (card->vaddr)
127 iounmap(card->vaddr);
128 card->vaddr = NULL;
129
130 macio_release_resource(mdev, 0);
131
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300132 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
133 macio_get_of_node(mdev), 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ssleep(1);
135
136 macio_set_drvdata(mdev, NULL);
137 free_orinocodev(dev);
138
139 return 0;
140}
141
142static int airport_hard_reset(struct orinoco_private *priv)
143{
144 /* It would be nice to power cycle the Airport for a real hard
145 * reset, but for some reason although it appears to
146 * re-initialize properly, it falls in a screaming heap
147 * shortly afterwards. */
148#if 0
149 struct net_device *dev = priv->ndev;
150 struct airport *card = priv->card;
151
152 /* Vitally important. If we don't do this it seems we get an
153 * interrupt somewhere during the power cycle, since
154 * hw_unavailable is already set it doesn't get ACKed, we get
Michael Opdenacker59c51592007-05-09 08:57:56 +0200155 * into an interrupt loop and the PMU decides to turn us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * off. */
157 disable_irq(dev->irq);
158
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300159 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
160 macio_get_of_node(card->mdev), 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ssleep(1);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300162 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
163 macio_get_of_node(card->mdev), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ssleep(1);
165
166 enable_irq(dev->irq);
167 ssleep(1);
168#endif
169
170 return 0;
171}
172
173static int
Jeff Mahoney5e655772005-07-06 15:44:41 -0400174airport_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct orinoco_private *priv;
177 struct net_device *dev;
178 struct airport *card;
179 unsigned long phys_addr;
180 hermes_t *hw;
181
182 if (macio_resource_count(mdev) < 1 || macio_irq_count(mdev) < 1) {
183 printk(KERN_ERR PFX "Wrong interrupt/addresses in OF tree\n");
184 return -ENODEV;
185 }
186
187 /* Allocate space for private device-specific data */
David Kilroy3994d502008-08-21 23:27:54 +0100188 dev = alloc_orinocodev(sizeof(*card), &mdev->ofdev.dev,
189 airport_hard_reset, NULL);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300190 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 printk(KERN_ERR PFX "Cannot allocate network device\n");
192 return -ENODEV;
193 }
194 priv = netdev_priv(dev);
195 card = priv->card;
196
197 hw = &priv->hw;
198 card->mdev = mdev;
199
200 if (macio_request_resource(mdev, 0, "airport")) {
201 printk(KERN_ERR PFX "can't request IO resource !\n");
202 free_orinocodev(dev);
203 return -EBUSY;
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
207
208 macio_set_drvdata(mdev, dev);
209
210 /* Setup interrupts & base address */
211 dev->irq = macio_irq(mdev, 0);
212 phys_addr = macio_resource_start(mdev, 0); /* Physical address */
213 printk(KERN_DEBUG PFX "Physical address %lx\n", phys_addr);
214 dev->base_addr = phys_addr;
215 card->vaddr = ioremap(phys_addr, AIRPORT_IO_LEN);
216 if (!card->vaddr) {
217 printk(KERN_ERR PFX "ioremap() failed\n");
218 goto failed;
219 }
220
221 hermes_struct_init(hw, card->vaddr, HERMES_16BIT_REGSPACING);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Power up card */
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300224 pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE,
225 macio_get_of_node(mdev), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ssleep(1);
227
228 /* Reset it before we get the interrupt */
229 hermes_init(hw);
230
231 if (request_irq(dev->irq, orinoco_interrupt, 0, dev->name, dev)) {
232 printk(KERN_ERR PFX "Couldn't get IRQ %d\n", dev->irq);
233 goto failed;
234 }
235 card->irq_requested = 1;
236
237 /* Tell the stack we exist */
238 if (register_netdev(dev) != 0) {
239 printk(KERN_ERR PFX "register_netdev() failed\n");
240 goto failed;
241 }
242 printk(KERN_DEBUG PFX "Card registered for interface %s\n", dev->name);
243 card->ndev_registered = 1;
244 return 0;
245 failed:
246 airport_detach(mdev);
247 return -ENODEV;
248} /* airport_attach */
249
250
251static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
252 " (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
253MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
254MODULE_DESCRIPTION("Driver for the Apple Airport wireless card.");
255MODULE_LICENSE("Dual MPL/GPL");
256
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300257static struct of_device_id airport_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 {
260 .name = "radio",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 },
262 {},
263};
264
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300265MODULE_DEVICE_TABLE(of, airport_match);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400266
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300267static struct macio_driver airport_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .name = DRIVER_NAME,
269 .match_table = airport_match,
270 .probe = airport_attach,
271 .remove = airport_detach,
272 .suspend = airport_suspend,
273 .resume = airport_resume,
274};
275
276static int __init
277init_airport(void)
278{
279 printk(KERN_DEBUG "%s\n", version);
280
281 return macio_register_driver(&airport_driver);
282}
283
284static void __exit
285exit_airport(void)
286{
Pavel Roskind070d852008-12-09 12:32:15 -0500287 macio_unregister_driver(&airport_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290module_init(init_airport);
291module_exit(exit_airport);