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