ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / macintosh / mediabay.c
1 /*
2  * Driver for the media bay on the PowerBook 3400 and 2400.
3  *
4  * Copyright (C) 1998 Paul Mackerras.
5  *
6  * Various evolutions by Benjamin Herrenschmidt & Henry Worth
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version
11  *  2 of the License, or (at your option) any later version.
12  */
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/hdreg.h>
21 #include <linux/stddef.h>
22 #include <linux/init.h>
23 #include <linux/ide.h>
24 #include <asm/prom.h>
25 #include <asm/pgtable.h>
26 #include <asm/io.h>
27 #include <asm/machdep.h>
28 #include <asm/pmac_feature.h>
29 #include <asm/mediabay.h>
30 #include <asm/sections.h>
31 #include <asm/ohare.h>
32 #include <asm/heathrow.h>
33 #include <asm/keylargo.h>
34 #include <linux/adb.h>
35 #include <linux/pmu.h>
36
37
38 #define MB_DEBUG
39 #define MB_IGNORE_SIGNALS
40
41 #ifdef MB_DEBUG
42 #define MBDBG(fmt, arg...)      printk(KERN_INFO fmt , ## arg)
43 #else
44 #define MBDBG(fmt, arg...)      do { } while (0)
45 #endif
46
47 #define MB_FCR32(bay, r)        ((bay)->base + ((r) >> 2))
48 #define MB_FCR8(bay, r)         (((volatile u8*)((bay)->base)) + (r))
49
50 #define MB_IN32(bay,r)          (in_le32(MB_FCR32(bay,r)))
51 #define MB_OUT32(bay,r,v)       (out_le32(MB_FCR32(bay,r), (v)))
52 #define MB_BIS(bay,r,v)         (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
53 #define MB_BIC(bay,r,v)         (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
54 #define MB_IN8(bay,r)           (in_8(MB_FCR8(bay,r)))
55 #define MB_OUT8(bay,r,v)        (out_8(MB_FCR8(bay,r), (v)))
56
57 struct media_bay_info;
58
59 struct mb_ops {
60         char*   name;
61         void    (*init)(struct media_bay_info *bay);
62         u8      (*content)(struct media_bay_info *bay);
63         void    (*power)(struct media_bay_info *bay, int on_off);
64         int     (*setup_bus)(struct media_bay_info *bay, u8 device_id);
65         void    (*un_reset)(struct media_bay_info *bay);
66         void    (*un_reset_ide)(struct media_bay_info *bay);
67 };
68
69 struct media_bay_info {
70         volatile u32*                   base;
71         int                             content_id;
72         int                             state;
73         int                             last_value;
74         int                             value_count;
75         int                             timer;
76         struct macio_dev                *mdev;
77         struct mb_ops*                  ops;
78         int                             index;
79         int                             cached_gpio;
80         int                             sleeping;
81         struct semaphore                lock;
82 #ifdef CONFIG_BLK_DEV_IDE
83         unsigned long                   cd_base;
84         int                             cd_index;
85         int                             cd_irq;
86         int                             cd_retry;
87 #endif
88 };
89
90 #define MAX_BAYS        2
91
92 static struct media_bay_info media_bays[MAX_BAYS];
93 int media_bay_count = 0;
94
95 #ifdef CONFIG_BLK_DEV_IDE
96 /* check the busy bit in the media-bay ide interface
97    (assumes the media-bay contains an ide device) */
98 #define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
99 #endif
100
101 /* Note: All delays are not in milliseconds and converted to HZ relative
102  * values by the macro below
103  */
104 #define MS_TO_HZ(ms)    ((ms * HZ + 999) / 1000)
105
106 /*
107  * Wait that number of ms between each step in normal polling mode
108  */
109 #define MB_POLL_DELAY   25
110
111 /*
112  * Consider the media-bay ID value stable if it is the same for
113  * this number of milliseconds
114  */
115 #define MB_STABLE_DELAY 100
116
117 /* Wait after powering up the media bay this delay in ms
118  * timeout bumped for some powerbooks
119  */
120 #define MB_POWER_DELAY  200
121
122 /*
123  * Hold the media-bay reset signal true for this many ticks
124  * after a device is inserted before releasing it.
125  */
126 #define MB_RESET_DELAY  50
127
128 /*
129  * Wait this long after the reset signal is released and before doing
130  * further operations. After this delay, the IDE reset signal is released
131  * too for an IDE device
132  */
133 #define MB_SETUP_DELAY  100
134
135 /*
136  * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
137  * (or until the device is ready) before waiting for busy bit to disappear
138  */
139 #define MB_IDE_WAIT     1000
140
141 /*
142  * Timeout waiting for busy bit of an IDE device to go down
143  */
144 #define MB_IDE_TIMEOUT  5000
145
146 /*
147  * Max retries of the full power up/down sequence for an IDE device
148  */
149 #define MAX_CD_RETRIES  3
150
151 /*
152  * States of a media bay
153  */
154 enum {
155         mb_empty = 0,           /* Idle */
156         mb_powering_up,         /* power bit set, waiting MB_POWER_DELAY */
157         mb_enabling_bay,        /* enable bits set, waiting MB_RESET_DELAY */
158         mb_resetting,           /* reset bit unset, waiting MB_SETUP_DELAY */
159         mb_ide_resetting,       /* IDE reset bit unser, waiting MB_IDE_WAIT */
160         mb_ide_waiting,         /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
161         mb_up,                  /* Media bay full */
162         mb_powering_down        /* Powering down (avoid too fast down/up) */
163 };
164
165 #define MB_POWER_SOUND          0x08
166 #define MB_POWER_FLOPPY         0x04
167 #define MB_POWER_ATA            0x02
168 #define MB_POWER_PCI            0x01
169 #define MB_POWER_OFF            0x00
170
171 /*
172  * Functions for polling content of media bay
173  */
174  
175 static u8 __pmac
176 ohare_mb_content(struct media_bay_info *bay)
177 {
178         return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
179 }
180
181 static u8 __pmac
182 heathrow_mb_content(struct media_bay_info *bay)
183 {
184         return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
185 }
186
187 static u8 __pmac
188 keylargo_mb_content(struct media_bay_info *bay)
189 {
190         int new_gpio;
191
192         new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
193         if (new_gpio) {
194                 bay->cached_gpio = new_gpio;
195                 return MB_NO;
196         } else if (bay->cached_gpio != new_gpio) {
197                 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
198                 (void)MB_IN32(bay, KEYLARGO_MBCR);
199                 udelay(5);
200                 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
201                 (void)MB_IN32(bay, KEYLARGO_MBCR);
202                 udelay(5);
203                 bay->cached_gpio = new_gpio;
204         }
205         return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
206 }
207
208 /*
209  * Functions for powering up/down the bay, puts the bay device
210  * into reset state as well
211  */
212
213 static void __pmac
214 ohare_mb_power(struct media_bay_info* bay, int on_off)
215 {
216         if (on_off) {
217                 /* Power up device, assert it's reset line */
218                 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
219                 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
220         } else {
221                 /* Disable all devices */
222                 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
223                 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
224                 /* Cut power from bay, release reset line */
225                 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
226                 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
227                 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
228         }
229         MB_BIC(bay, OHARE_MBCR, 0x00000F00);
230 }
231
232 static void __pmac
233 heathrow_mb_power(struct media_bay_info* bay, int on_off)
234 {
235         if (on_off) {
236                 /* Power up device, assert it's reset line */
237                 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
238                 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
239         } else {
240                 /* Disable all devices */
241                 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
242                 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
243                 /* Cut power from bay, release reset line */
244                 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
245                 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
246                 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
247         }
248         MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
249 }
250
251 static void __pmac
252 keylargo_mb_power(struct media_bay_info* bay, int on_off)
253 {
254         if (on_off) {
255                 /* Power up device, assert it's reset line */
256                 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
257                 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
258         } else {
259                 /* Disable all devices */
260                 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
261                 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
262                 /* Cut power from bay, release reset line */
263                 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
264                 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
265                 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
266         }
267         MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
268 }
269
270 /*
271  * Functions for configuring the media bay for a given type of device,
272  * enable the related busses
273  */
274
275 static int __pmac
276 ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
277 {
278         switch(device_id) {
279                 case MB_FD:
280                 case MB_FD1:
281                         MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
282                         MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
283                         return 0;
284                 case MB_CD:
285                         MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
286                         MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
287                         return 0;
288                 case MB_PCI:
289                         MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
290                         return 0;
291         }
292         return -ENODEV;
293 }
294
295 static int __pmac
296 heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
297 {
298         switch(device_id) {
299                 case MB_FD:
300                 case MB_FD1:
301                         MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
302                         MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
303                         return 0;
304                 case MB_CD:
305                         MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
306                         MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
307                         return 0;
308                 case MB_PCI:
309                         MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
310                         return 0;
311         }
312         return -ENODEV;
313 }
314
315 static int __pmac
316 keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
317 {
318         switch(device_id) {
319                 case MB_CD:
320                         MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
321                         MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
322                         MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
323                         return 0;
324                 case MB_PCI:
325                         MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
326                         return 0;
327                 case MB_SOUND:
328                         MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
329                         return 0;
330         }
331         return -ENODEV;
332 }
333
334 /*
335  * Functions for tweaking resets
336  */
337
338 static void __pmac
339 ohare_mb_un_reset(struct media_bay_info* bay)
340 {
341         MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
342 }
343
344 static void __pmac keylargo_mb_init(struct media_bay_info *bay)
345 {
346         MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
347 }
348
349 static void __pmac heathrow_mb_un_reset(struct media_bay_info* bay)
350 {
351         MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
352 }
353
354 static void __pmac keylargo_mb_un_reset(struct media_bay_info* bay)
355 {
356         MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
357 }
358
359 static void __pmac ohare_mb_un_reset_ide(struct media_bay_info* bay)
360 {
361         MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
362 }
363
364 static void __pmac heathrow_mb_un_reset_ide(struct media_bay_info* bay)
365 {
366         MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
367 }
368
369 static void __pmac keylargo_mb_un_reset_ide(struct media_bay_info* bay)
370 {
371         MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
372 }
373
374 static inline void __pmac set_mb_power(struct media_bay_info* bay, int onoff)
375 {
376         /* Power up up and assert the bay reset line */
377         if (onoff) {
378                 bay->ops->power(bay, 1);
379                 bay->state = mb_powering_up;
380                 MBDBG("mediabay%d: powering up\n", bay->index);
381         } else { 
382                 /* Make sure everything is powered down & disabled */
383                 bay->ops->power(bay, 0);
384                 bay->state = mb_powering_down;
385                 MBDBG("mediabay%d: powering down\n", bay->index);
386         }
387         bay->timer = MS_TO_HZ(MB_POWER_DELAY);
388 }
389
390 static void __pmac poll_media_bay(struct media_bay_info* bay)
391 {
392         int id = bay->ops->content(bay);
393
394         if (id == bay->last_value) {
395                 if (id != bay->content_id) {
396                         bay->value_count += MS_TO_HZ(MB_POLL_DELAY);
397                         if (bay->value_count >= MS_TO_HZ(MB_STABLE_DELAY)) {
398                                 /* If the device type changes without going thru
399                                  * "MB_NO", we force a pass by "MB_NO" to make sure
400                                  * things are properly reset
401                                  */
402                                 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
403                                         id = MB_NO;
404                                         MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
405                                 }
406                                 MBDBG("mediabay%d: switching to %d\n", bay->index, id);
407                                 set_mb_power(bay, id != MB_NO);
408                                 bay->content_id = id;
409                                 if (id == MB_NO) {
410 #ifdef CONFIG_BLK_DEV_IDE
411                                         bay->cd_retry = 0;
412 #endif
413                                         printk(KERN_INFO "media bay %d is empty\n", bay->index);
414                                 }
415                         }
416                 }
417         } else {
418                 bay->last_value = id;
419                 bay->value_count = 0;
420         }
421 }
422
423 int __pmac check_media_bay(struct device_node *which_bay, int what)
424 {
425 #ifdef CONFIG_BLK_DEV_IDE
426         int     i;
427
428         for (i=0; i<media_bay_count; i++)
429                 if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
430                         if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
431                                 return 0;
432                         media_bays[i].cd_index = -1;
433                         return -EINVAL;
434                 }
435 #endif /* CONFIG_BLK_DEV_IDE */
436         return -ENODEV;
437 }
438
439 int __pmac check_media_bay_by_base(unsigned long base, int what)
440 {
441 #ifdef CONFIG_BLK_DEV_IDE
442         int     i;
443
444         for (i=0; i<media_bay_count; i++)
445                 if (media_bays[i].mdev && base == media_bays[i].cd_base) {
446                         if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
447                                 return 0;
448                         media_bays[i].cd_index = -1;
449                         return -EINVAL;
450                 } 
451 #endif
452         
453         return -ENODEV;
454 }
455
456 int __pmac media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
457         int irq, int index)
458 {
459 #ifdef CONFIG_BLK_DEV_IDE
460         int     i;
461
462         for (i=0; i<media_bay_count; i++) {
463                 struct media_bay_info* bay = &media_bays[i];
464
465                 if (bay->mdev && which_bay == bay->mdev->ofdev.node) {
466                         int timeout = 5000;
467                         
468                         down(&bay->lock);
469
470                         bay->cd_base    = base;
471                         bay->cd_irq     = irq;
472
473                         if ((MB_CD != bay->content_id) || bay->state != mb_up) {
474                                 up(&bay->lock);
475                                 return 0;
476                         }
477                         printk(KERN_DEBUG "Registered ide%d for media bay %d\n", index, i);
478                         do {
479                                 if (MB_IDE_READY(i)) {
480                                         bay->cd_index   = index;
481                                         up(&bay->lock);
482                                         return 0;
483                                 }
484                                 mdelay(1);
485                         } while(--timeout);
486                         printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
487                         up(&bay->lock);
488                         return -ENODEV;
489                 }
490         }
491 #endif /* CONFIG_BLK_DEV_IDE */
492         
493         return -ENODEV;
494 }
495
496 static void __pmac media_bay_step(int i)
497 {
498         struct media_bay_info* bay = &media_bays[i];
499
500         /* We don't poll when powering down */
501         if (bay->state != mb_powering_down)
502             poll_media_bay(bay);
503
504         /* If timer expired or polling IDE busy, run state machine */
505         if ((bay->state != mb_ide_waiting) && (bay->timer != 0)) {
506                 bay->timer -= MS_TO_HZ(MB_POLL_DELAY);
507                 if (bay->timer > 0)
508                         return;
509                 bay->timer = 0;
510         }
511
512         switch(bay->state) {
513         case mb_powering_up:
514                 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
515                         MBDBG("mediabay%d: device not supported (kind:%d)\n", i, bay->content_id);
516                         set_mb_power(bay, 0);
517                         break;
518                 }
519                 bay->timer = MS_TO_HZ(MB_RESET_DELAY);
520                 bay->state = mb_enabling_bay;
521                 MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
522                 break;
523         case mb_enabling_bay:
524                 bay->ops->un_reset(bay);
525                 bay->timer = MS_TO_HZ(MB_SETUP_DELAY);
526                 bay->state = mb_resetting;
527                 MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
528                 break;
529             
530         case mb_resetting:
531                 if (bay->content_id != MB_CD) {
532                         MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
533                         bay->state = mb_up;
534                         break;
535                 }
536 #ifdef CONFIG_BLK_DEV_IDE
537                 MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
538                 bay->ops->un_reset_ide(bay);
539                 bay->timer = MS_TO_HZ(MB_IDE_WAIT);
540                 bay->state = mb_ide_resetting;
541 #else
542                 printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
543                 set_mb_power(bay, 0);
544 #endif /* CONFIG_BLK_DEV_IDE */
545                 break;
546             
547 #ifdef CONFIG_BLK_DEV_IDE
548         case mb_ide_resetting:
549                 bay->timer = MS_TO_HZ(MB_IDE_TIMEOUT);
550                 bay->state = mb_ide_waiting;
551                 MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
552                 break;
553             
554         case mb_ide_waiting:
555                 if (bay->cd_base == 0) {
556                         bay->timer = 0;
557                         bay->state = mb_up;
558                         MBDBG("mediabay%d: up before IDE init\n", i);
559                         break;
560                 } else if (MB_IDE_READY(i)) {
561                         bay->timer = 0;
562                         bay->state = mb_up;
563                         if (bay->cd_index < 0) {
564                                 hw_regs_t hw;
565
566                                 printk("mediabay %d, registering IDE...\n", i);
567                                 pmu_suspend();
568                                 ide_init_hwif_ports(&hw, (unsigned long) bay->cd_base, (unsigned long) 0, NULL);
569                                 hw.irq = bay->cd_irq;
570                                 hw.chipset = ide_pmac;
571                                 bay->cd_index = ide_register_hw(&hw, NULL);
572                                 pmu_resume();
573                         }
574                         if (bay->cd_index == -1) {
575                                 /* We eventually do a retry */
576                                 bay->cd_retry++;
577                                 printk("IDE register error\n");
578                                 set_mb_power(bay, 0);
579                         } else {
580                                 printk(KERN_DEBUG "media-bay %d is ide%d\n", i, bay->cd_index);
581                                 MBDBG("mediabay %d IDE ready\n", i);
582                         }
583                         break;
584                 } else if (bay->timer > 0)
585                         bay->timer -= MS_TO_HZ(MB_POLL_DELAY);
586                 if (bay->timer <= 0) {
587                         printk("\nIDE Timeout in bay %d !, IDE state is: 0x%02x\n",
588                                i, readb(bay->cd_base + 0x70));
589                         MBDBG("mediabay%d: nIDE Timeout !\n", i);
590                         set_mb_power(bay, 0);
591                         bay->timer = 0;
592                 }
593                 break;
594 #endif /* CONFIG_BLK_DEV_IDE */
595
596         case mb_powering_down:
597                 bay->state = mb_empty;
598 #ifdef CONFIG_BLK_DEV_IDE
599                 if (bay->cd_index >= 0) {
600                         printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
601                                bay->cd_index);
602                         ide_unregister(bay->cd_index);
603                         bay->cd_index = -1;
604                 }
605                 if (bay->cd_retry) {
606                         if (bay->cd_retry > MAX_CD_RETRIES) {
607                                 /* Should add an error sound (sort of beep in dmasound) */
608                                 printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
609                         } else {
610                                 /* Force a new power down/up sequence */
611                                 bay->content_id = MB_NO;
612                         }
613                 }
614 #endif /* CONFIG_BLK_DEV_IDE */    
615                 MBDBG("mediabay%d: end of power down\n", i);
616                 break;
617         }
618 }
619
620 /*
621  * This procedure runs as a kernel thread to poll the media bay
622  * once each tick and register and unregister the IDE interface
623  * with the IDE driver.  It needs to be a thread because
624  * ide_register can't be called from interrupt context.
625  */
626 static int __pmac media_bay_task(void *x)
627 {
628         int     i;
629
630         strcpy(current->comm, "media-bay");
631 #ifdef MB_IGNORE_SIGNALS
632         sigfillset(&current->blocked);
633 #endif
634
635         for (;;) {
636                 for (i = 0; i < media_bay_count; ++i) {
637                         down(&media_bays[i].lock);
638                         if (!media_bays[i].sleeping)
639                                 media_bay_step(i);
640                         up(&media_bays[i].lock);
641                 }
642
643                 current->state = TASK_INTERRUPTIBLE;
644                 schedule_timeout(MS_TO_HZ(MB_POLL_DELAY));
645                 if (signal_pending(current))
646                         return 0;
647         }
648 }
649
650 static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_match *match)
651 {
652         struct media_bay_info* bay;
653         volatile u32 *regbase;
654         struct device_node *ofnode;
655         int i;
656
657         ofnode = mdev->ofdev.node;
658
659         if (macio_resource_count(mdev) < 1)
660                 return -ENODEV;
661         if (macio_request_resources(mdev, "media-bay"))
662                 return -EBUSY;
663         /* Media bay registers are located at the beginning of the
664          * mac-io chip, we get the parent address for now (hrm...)
665          */
666         regbase = (volatile u32 *)ioremap(ofnode->parent->addrs[0].address, 0x100);
667         if (regbase == NULL) {
668                 macio_release_resources(mdev);
669                 return -ENOMEM;
670         }
671         
672         i = media_bay_count++;
673         bay = &media_bays[i];
674         bay->mdev = mdev;
675         bay->base = regbase;
676         bay->index = i;
677         bay->ops = match->data;
678         bay->sleeping = 0;
679         init_MUTEX(&bay->lock);
680
681         /* Init HW probing */
682         if (bay->ops->init)
683                 bay->ops->init(bay);
684
685         printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
686
687         /* Force an immediate detect */
688         set_mb_power(bay, 0);
689         set_current_state(TASK_UNINTERRUPTIBLE);
690         schedule_timeout(MS_TO_HZ(MB_POWER_DELAY));
691         bay->content_id = MB_NO;
692         bay->last_value = bay->ops->content(bay);
693         bay->value_count = MS_TO_HZ(MB_STABLE_DELAY);
694         bay->state = mb_empty;
695         do {
696                 set_current_state(TASK_UNINTERRUPTIBLE);
697                 schedule_timeout(MS_TO_HZ(MB_POLL_DELAY));
698                 media_bay_step(i);
699         } while((bay->state != mb_empty) &&
700                 (bay->state != mb_up));
701
702         /* Mark us ready by filling our mdev data */
703         macio_set_drvdata(mdev, bay);
704
705         /* Startup kernel thread */
706         if (i == 0)
707                 kernel_thread(media_bay_task, NULL, CLONE_KERNEL);
708
709         return 0;
710
711 }
712
713 static int __pmac media_bay_suspend(struct macio_dev *mdev, u32 state)
714 {
715         struct media_bay_info   *bay = macio_get_drvdata(mdev);
716
717         if (state != mdev->ofdev.dev.power_state && state >= 2) {
718                 down(&bay->lock);
719                 bay->sleeping = 1;
720                 set_mb_power(bay, 0);
721                 up(&bay->lock);
722                 set_current_state(TASK_UNINTERRUPTIBLE);
723                 schedule_timeout(MS_TO_HZ(MB_POLL_DELAY));
724                 mdev->ofdev.dev.power_state = state;
725         }
726         return 0;
727 }
728
729 static int __pmac media_bay_resume(struct macio_dev *mdev)
730 {
731         struct media_bay_info   *bay = macio_get_drvdata(mdev);
732
733         if (mdev->ofdev.dev.power_state != 0) {
734                 mdev->ofdev.dev.power_state = 0;
735
736                 /* We re-enable the bay using it's previous content
737                    only if it did not change. Note those bozo timings,
738                    they seem to help the 3400 get it right.
739                  */
740                 /* Force MB power to 0 */
741                 down(&bay->lock);
742                 set_mb_power(bay, 0);
743                 set_current_state(TASK_UNINTERRUPTIBLE);
744                 schedule_timeout(MS_TO_HZ(MB_POWER_DELAY));
745                 if (bay->ops->content(bay) != bay->content_id) {
746                         printk("mediabay%d: content changed during sleep...\n", bay->index);
747                         up(&bay->lock);
748                         return 0;
749                 }
750                 set_mb_power(bay, 1);
751                 bay->last_value = bay->content_id;
752                 bay->value_count = MS_TO_HZ(MB_STABLE_DELAY);
753                 bay->timer = MS_TO_HZ(MB_POWER_DELAY);
754 #ifdef CONFIG_BLK_DEV_IDE
755                 bay->cd_retry = 0;
756 #endif
757                 do {
758                         set_current_state(TASK_UNINTERRUPTIBLE);
759                         schedule_timeout(MS_TO_HZ(MB_POLL_DELAY));
760                         media_bay_step(bay->index);
761                 } while((bay->state != mb_empty) &&
762                         (bay->state != mb_up));
763                 bay->sleeping = 0;
764                 up(&bay->lock);
765         }
766         return 0;
767 }
768
769
770 /* Definitions of "ops" structures.
771  */
772 static struct mb_ops ohare_mb_ops __pmacdata = {
773         .name           = "Ohare",
774         .content        = ohare_mb_content,
775         .power          = ohare_mb_power,
776         .setup_bus      = ohare_mb_setup_bus,
777         .un_reset       = ohare_mb_un_reset,
778         .un_reset_ide   = ohare_mb_un_reset_ide,
779 };
780
781 static struct mb_ops heathrow_mb_ops __pmacdata = {
782         .name           = "Heathrow",
783         .content        = heathrow_mb_content,
784         .power          = heathrow_mb_power,
785         .setup_bus      = heathrow_mb_setup_bus,
786         .un_reset       = heathrow_mb_un_reset,
787         .un_reset_ide   = heathrow_mb_un_reset_ide,
788 };
789
790 static struct mb_ops keylargo_mb_ops __pmacdata = {
791         .name           = "KeyLargo",
792         .init           = keylargo_mb_init,
793         .content        = keylargo_mb_content,
794         .power          = keylargo_mb_power,
795         .setup_bus      = keylargo_mb_setup_bus,
796         .un_reset       = keylargo_mb_un_reset,
797         .un_reset_ide   = keylargo_mb_un_reset_ide,
798 };
799
800 /*
801  * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
802  * register is always set when there is something in the media bay.
803  * This causes problems for the interrupt code if we attach an interrupt
804  * handler to the media-bay interrupt, because it tends to go into
805  * an infinite loop calling the media bay interrupt handler.
806  * Therefore we do it all by polling the media bay once each tick.
807  */
808
809 static struct of_match media_bay_match[] =
810 {
811         {
812         .name           = "media-bay",
813         .type           = OF_ANY_MATCH,
814         .compatible     = "keylargo-media-bay",
815         .data           = &keylargo_mb_ops,
816         },
817         {
818         .name           = "media-bay",
819         .type           = OF_ANY_MATCH,
820         .compatible     = "heathrow-media-bay",
821         .data           = &heathrow_mb_ops,
822         },
823         {
824         .name           = "media-bay",
825         .type           = OF_ANY_MATCH,
826         .compatible     = "ohare-media-bay",
827         .data           = &ohare_mb_ops,
828         },
829         {},
830 };
831
832 static struct macio_driver media_bay_driver =
833 {
834         .name           = "media-bay",
835         .match_table    = media_bay_match,
836         .probe          = media_bay_attach,
837         .suspend        = media_bay_suspend,
838         .resume         = media_bay_resume
839 };
840
841 static int __init media_bay_init(void)
842 {
843         int i;
844
845         for (i=0; i<MAX_BAYS; i++) {
846                 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
847                 media_bays[i].content_id        = -1;
848 #ifdef CONFIG_BLK_DEV_IDE
849                 media_bays[i].cd_index          = -1;
850 #endif
851         }
852         if (_machine != _MACH_Pmac)
853                 return -ENODEV;
854
855         macio_register_driver(&media_bay_driver);       
856
857         return 0;
858 }
859
860 device_initcall(media_bay_init);