blob: eaba4a9b231e5479a5b61be9b05f5085e6f20e5b [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/sched.h>
18#include <linux/timer.h>
19#include <linux/hdreg.h>
20#include <linux/stddef.h>
21#include <linux/init.h>
22#include <linux/ide.h>
23#include <asm/prom.h>
24#include <asm/pgtable.h>
25#include <asm/io.h>
26#include <asm/machdep.h>
27#include <asm/pmac_feature.h>
28#include <asm/mediabay.h>
29#include <asm/sections.h>
30#include <asm/ohare.h>
31#include <asm/heathrow.h>
32#include <asm/keylargo.h>
33#include <linux/adb.h>
34#include <linux/pmu.h>
35
36
37#define MB_DEBUG
38#define MB_IGNORE_SIGNALS
39
40#ifdef MB_DEBUG
41#define MBDBG(fmt, arg...) printk(KERN_INFO fmt , ## arg)
42#else
43#define MBDBG(fmt, arg...) do { } while (0)
44#endif
45
46#define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2))
47#define MB_FCR8(bay, r) (((volatile u8 __iomem *)((bay)->base)) + (r))
48
49#define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r)))
50#define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v)))
51#define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
52#define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
53#define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r)))
54#define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v)))
55
56struct media_bay_info;
57
58struct mb_ops {
59 char* name;
60 void (*init)(struct media_bay_info *bay);
61 u8 (*content)(struct media_bay_info *bay);
62 void (*power)(struct media_bay_info *bay, int on_off);
63 int (*setup_bus)(struct media_bay_info *bay, u8 device_id);
64 void (*un_reset)(struct media_bay_info *bay);
65 void (*un_reset_ide)(struct media_bay_info *bay);
66};
67
68struct media_bay_info {
69 u32 __iomem *base;
70 int content_id;
71 int state;
72 int last_value;
73 int value_count;
74 int timer;
75 struct macio_dev *mdev;
76 struct mb_ops* ops;
77 int index;
78 int cached_gpio;
79 int sleeping;
80 struct semaphore lock;
81#ifdef CONFIG_BLK_DEV_IDE
82 void __iomem *cd_base;
83 int cd_index;
84 int cd_irq;
85 int cd_retry;
86#endif
87};
88
89#define MAX_BAYS 2
90
91static struct media_bay_info media_bays[MAX_BAYS];
92int media_bay_count = 0;
93
94#ifdef CONFIG_BLK_DEV_IDE
95/* check the busy bit in the media-bay ide interface
96 (assumes the media-bay contains an ide device) */
97#define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
98#endif
99
100/*
101 * Wait that number of ms between each step in normal polling mode
102 */
103#define MB_POLL_DELAY 25
104
105/*
106 * Consider the media-bay ID value stable if it is the same for
107 * this number of milliseconds
108 */
109#define MB_STABLE_DELAY 100
110
111/* Wait after powering up the media bay this delay in ms
112 * timeout bumped for some powerbooks
113 */
114#define MB_POWER_DELAY 200
115
116/*
117 * Hold the media-bay reset signal true for this many ticks
118 * after a device is inserted before releasing it.
119 */
120#define MB_RESET_DELAY 50
121
122/*
123 * Wait this long after the reset signal is released and before doing
124 * further operations. After this delay, the IDE reset signal is released
125 * too for an IDE device
126 */
127#define MB_SETUP_DELAY 100
128
129/*
130 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
131 * (or until the device is ready) before waiting for busy bit to disappear
132 */
133#define MB_IDE_WAIT 1000
134
135/*
136 * Timeout waiting for busy bit of an IDE device to go down
137 */
138#define MB_IDE_TIMEOUT 5000
139
140/*
141 * Max retries of the full power up/down sequence for an IDE device
142 */
143#define MAX_CD_RETRIES 3
144
145/*
146 * States of a media bay
147 */
148enum {
149 mb_empty = 0, /* Idle */
150 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
151 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
152 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
153 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
154 mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
155 mb_up, /* Media bay full */
156 mb_powering_down /* Powering down (avoid too fast down/up) */
157};
158
159#define MB_POWER_SOUND 0x08
160#define MB_POWER_FLOPPY 0x04
161#define MB_POWER_ATA 0x02
162#define MB_POWER_PCI 0x01
163#define MB_POWER_OFF 0x00
164
165/*
166 * Functions for polling content of media bay
167 */
168
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500169static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170ohare_mb_content(struct media_bay_info *bay)
171{
172 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
173}
174
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500175static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176heathrow_mb_content(struct media_bay_info *bay)
177{
178 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
179}
180
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500181static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182keylargo_mb_content(struct media_bay_info *bay)
183{
184 int new_gpio;
185
186 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
187 if (new_gpio) {
188 bay->cached_gpio = new_gpio;
189 return MB_NO;
190 } else if (bay->cached_gpio != new_gpio) {
191 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
192 (void)MB_IN32(bay, KEYLARGO_MBCR);
193 udelay(5);
194 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
195 (void)MB_IN32(bay, KEYLARGO_MBCR);
196 udelay(5);
197 bay->cached_gpio = new_gpio;
198 }
199 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
200}
201
202/*
203 * Functions for powering up/down the bay, puts the bay device
204 * into reset state as well
205 */
206
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500207static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208ohare_mb_power(struct media_bay_info* bay, int on_off)
209{
210 if (on_off) {
211 /* Power up device, assert it's reset line */
212 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
213 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
214 } else {
215 /* Disable all devices */
216 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
217 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
218 /* Cut power from bay, release reset line */
219 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
220 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
221 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
222 }
223 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
224}
225
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500226static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227heathrow_mb_power(struct media_bay_info* bay, int on_off)
228{
229 if (on_off) {
230 /* Power up device, assert it's reset line */
231 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
232 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
233 } else {
234 /* Disable all devices */
235 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
236 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
237 /* Cut power from bay, release reset line */
238 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
239 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
240 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
241 }
242 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
243}
244
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500245static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246keylargo_mb_power(struct media_bay_info* bay, int on_off)
247{
248 if (on_off) {
249 /* Power up device, assert it's reset line */
250 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
251 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
252 } else {
253 /* Disable all devices */
254 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
255 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
256 /* Cut power from bay, release reset line */
257 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
258 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
259 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
260 }
261 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
262}
263
264/*
265 * Functions for configuring the media bay for a given type of device,
266 * enable the related busses
267 */
268
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500269static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
271{
272 switch(device_id) {
273 case MB_FD:
274 case MB_FD1:
275 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
276 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
277 return 0;
278 case MB_CD:
279 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
280 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
281 return 0;
282 case MB_PCI:
283 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
284 return 0;
285 }
286 return -ENODEV;
287}
288
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500289static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
291{
292 switch(device_id) {
293 case MB_FD:
294 case MB_FD1:
295 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
296 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
297 return 0;
298 case MB_CD:
299 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
300 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
301 return 0;
302 case MB_PCI:
303 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
304 return 0;
305 }
306 return -ENODEV;
307}
308
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500309static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
311{
312 switch(device_id) {
313 case MB_CD:
314 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
315 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
316 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
317 return 0;
318 case MB_PCI:
319 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
320 return 0;
321 case MB_SOUND:
322 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
323 return 0;
324 }
325 return -ENODEV;
326}
327
328/*
329 * Functions for tweaking resets
330 */
331
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500332static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333ohare_mb_un_reset(struct media_bay_info* bay)
334{
335 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
336}
337
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500338static void keylargo_mb_init(struct media_bay_info *bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
341}
342
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500343static void heathrow_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
346}
347
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500348static void keylargo_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
351}
352
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500353static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
356}
357
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500358static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
361}
362
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500363static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
366}
367
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500368static inline void set_mb_power(struct media_bay_info* bay, int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 /* Power up up and assert the bay reset line */
371 if (onoff) {
372 bay->ops->power(bay, 1);
373 bay->state = mb_powering_up;
374 MBDBG("mediabay%d: powering up\n", bay->index);
375 } else {
376 /* Make sure everything is powered down & disabled */
377 bay->ops->power(bay, 0);
378 bay->state = mb_powering_down;
379 MBDBG("mediabay%d: powering down\n", bay->index);
380 }
381 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
382}
383
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500384static void poll_media_bay(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int id = bay->ops->content(bay);
387
388 if (id == bay->last_value) {
389 if (id != bay->content_id) {
390 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
391 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
392 /* If the device type changes without going thru
393 * "MB_NO", we force a pass by "MB_NO" to make sure
394 * things are properly reset
395 */
396 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
397 id = MB_NO;
398 MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
399 }
400 MBDBG("mediabay%d: switching to %d\n", bay->index, id);
401 set_mb_power(bay, id != MB_NO);
402 bay->content_id = id;
403 if (id == MB_NO) {
404#ifdef CONFIG_BLK_DEV_IDE
405 bay->cd_retry = 0;
406#endif
407 printk(KERN_INFO "media bay %d is empty\n", bay->index);
408 }
409 }
410 }
411 } else {
412 bay->last_value = id;
413 bay->value_count = 0;
414 }
415}
416
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500417int check_media_bay(struct device_node *which_bay, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419#ifdef CONFIG_BLK_DEV_IDE
420 int i;
421
422 for (i=0; i<media_bay_count; i++)
423 if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
424 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
425 return 0;
426 media_bays[i].cd_index = -1;
427 return -EINVAL;
428 }
429#endif /* CONFIG_BLK_DEV_IDE */
430 return -ENODEV;
431}
432EXPORT_SYMBOL(check_media_bay);
433
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500434int check_media_bay_by_base(unsigned long base, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436#ifdef CONFIG_BLK_DEV_IDE
437 int i;
438
439 for (i=0; i<media_bay_count; i++)
440 if (media_bays[i].mdev && base == (unsigned long) media_bays[i].cd_base) {
441 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
442 return 0;
443 media_bays[i].cd_index = -1;
444 return -EINVAL;
445 }
446#endif
447
448 return -ENODEV;
449}
450
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500451int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int irq, int index)
453{
454#ifdef CONFIG_BLK_DEV_IDE
455 int i;
456
457 for (i=0; i<media_bay_count; i++) {
458 struct media_bay_info* bay = &media_bays[i];
459
460 if (bay->mdev && which_bay == bay->mdev->ofdev.node) {
461 int timeout = 5000;
462
463 down(&bay->lock);
464
465 bay->cd_base = (void __iomem *) base;
466 bay->cd_irq = irq;
467
468 if ((MB_CD != bay->content_id) || bay->state != mb_up) {
469 up(&bay->lock);
470 return 0;
471 }
472 printk(KERN_DEBUG "Registered ide%d for media bay %d\n", index, i);
473 do {
474 if (MB_IDE_READY(i)) {
475 bay->cd_index = index;
476 up(&bay->lock);
477 return 0;
478 }
479 mdelay(1);
480 } while(--timeout);
481 printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
482 up(&bay->lock);
483 return -ENODEV;
484 }
485 }
486#endif /* CONFIG_BLK_DEV_IDE */
487
488 return -ENODEV;
489}
490
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500491static void media_bay_step(int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct media_bay_info* bay = &media_bays[i];
494
495 /* We don't poll when powering down */
496 if (bay->state != mb_powering_down)
497 poll_media_bay(bay);
498
499 /* If timer expired or polling IDE busy, run state machine */
500 if ((bay->state != mb_ide_waiting) && (bay->timer != 0)) {
501 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
502 if (bay->timer > 0)
503 return;
504 bay->timer = 0;
505 }
506
507 switch(bay->state) {
508 case mb_powering_up:
509 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
510 MBDBG("mediabay%d: device not supported (kind:%d)\n", i, bay->content_id);
511 set_mb_power(bay, 0);
512 break;
513 }
514 bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
515 bay->state = mb_enabling_bay;
516 MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
517 break;
518 case mb_enabling_bay:
519 bay->ops->un_reset(bay);
520 bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
521 bay->state = mb_resetting;
522 MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
523 break;
524
525 case mb_resetting:
526 if (bay->content_id != MB_CD) {
527 MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
528 bay->state = mb_up;
529 break;
530 }
531#ifdef CONFIG_BLK_DEV_IDE
532 MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
533 bay->ops->un_reset_ide(bay);
534 bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
535 bay->state = mb_ide_resetting;
536#else
537 printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
538 set_mb_power(bay, 0);
539#endif /* CONFIG_BLK_DEV_IDE */
540 break;
541
542#ifdef CONFIG_BLK_DEV_IDE
543 case mb_ide_resetting:
544 bay->timer = msecs_to_jiffies(MB_IDE_TIMEOUT);
545 bay->state = mb_ide_waiting;
546 MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
547 break;
548
549 case mb_ide_waiting:
550 if (bay->cd_base == NULL) {
551 bay->timer = 0;
552 bay->state = mb_up;
553 MBDBG("mediabay%d: up before IDE init\n", i);
554 break;
555 } else if (MB_IDE_READY(i)) {
556 bay->timer = 0;
557 bay->state = mb_up;
558 if (bay->cd_index < 0) {
559 hw_regs_t hw;
560
561 printk("mediabay %d, registering IDE...\n", i);
562 pmu_suspend();
563 ide_init_hwif_ports(&hw, (unsigned long) bay->cd_base, (unsigned long) 0, NULL);
564 hw.irq = bay->cd_irq;
565 hw.chipset = ide_pmac;
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +0100566 bay->cd_index =
567 ide_register_hw(&hw, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 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 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500622static int media_bay_task(void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
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;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100650 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int i;
652
653 ofnode = mdev->ofdev.node;
654
655 if (macio_resource_count(mdev) < 1)
656 return -ENODEV;
657 if (macio_request_resources(mdev, "media-bay"))
658 return -EBUSY;
659 /* Media bay registers are located at the beginning of the
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100660 * mac-io chip, for now, we trick and align down the first
661 * resource passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100663 base = macio_resource_start(mdev, 0) & 0xffff0000u;
664 regbase = (u32 __iomem *)ioremap(base, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (regbase == NULL) {
666 macio_release_resources(mdev);
667 return -ENOMEM;
668 }
669
670 i = media_bay_count++;
671 bay = &media_bays[i];
672 bay->mdev = mdev;
673 bay->base = regbase;
674 bay->index = i;
675 bay->ops = match->data;
676 bay->sleeping = 0;
677 init_MUTEX(&bay->lock);
678
679 /* Init HW probing */
680 if (bay->ops->init)
681 bay->ops->init(bay);
682
683 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
684
685 /* Force an immediate detect */
686 set_mb_power(bay, 0);
687 msleep(MB_POWER_DELAY);
688 bay->content_id = MB_NO;
689 bay->last_value = bay->ops->content(bay);
690 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
691 bay->state = mb_empty;
692 do {
693 msleep(MB_POLL_DELAY);
694 media_bay_step(i);
695 } while((bay->state != mb_empty) &&
696 (bay->state != mb_up));
697
698 /* Mark us ready by filling our mdev data */
699 macio_set_drvdata(mdev, bay);
700
701 /* Startup kernel thread */
702 if (i == 0)
703 kernel_thread(media_bay_task, NULL, CLONE_KERNEL);
704
705 return 0;
706
707}
708
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500709static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct media_bay_info *bay = macio_get_drvdata(mdev);
712
Pavel Machekca078ba2005-09-03 15:56:57 -0700713 if (state.event != mdev->ofdev.dev.power.power_state.event && state.event == PM_EVENT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 down(&bay->lock);
715 bay->sleeping = 1;
716 set_mb_power(bay, 0);
717 up(&bay->lock);
718 msleep(MB_POLL_DELAY);
719 mdev->ofdev.dev.power.power_state = state;
720 }
721 return 0;
722}
723
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500724static int media_bay_resume(struct macio_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct media_bay_info *bay = macio_get_drvdata(mdev);
727
Pavel Machekca078ba2005-09-03 15:56:57 -0700728 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
Pavel Machek829ca9a2005-09-03 15:56:56 -0700729 mdev->ofdev.dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* We re-enable the bay using it's previous content
732 only if it did not change. Note those bozo timings,
733 they seem to help the 3400 get it right.
734 */
735 /* Force MB power to 0 */
736 down(&bay->lock);
737 set_mb_power(bay, 0);
738 msleep(MB_POWER_DELAY);
739 if (bay->ops->content(bay) != bay->content_id) {
740 printk("mediabay%d: content changed during sleep...\n", bay->index);
741 up(&bay->lock);
742 return 0;
743 }
744 set_mb_power(bay, 1);
745 bay->last_value = bay->content_id;
746 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
747 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
748#ifdef CONFIG_BLK_DEV_IDE
749 bay->cd_retry = 0;
750#endif
751 do {
752 msleep(MB_POLL_DELAY);
753 media_bay_step(bay->index);
754 } while((bay->state != mb_empty) &&
755 (bay->state != mb_up));
756 bay->sleeping = 0;
757 up(&bay->lock);
758 }
759 return 0;
760}
761
762
763/* Definitions of "ops" structures.
764 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500765static struct mb_ops ohare_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .name = "Ohare",
767 .content = ohare_mb_content,
768 .power = ohare_mb_power,
769 .setup_bus = ohare_mb_setup_bus,
770 .un_reset = ohare_mb_un_reset,
771 .un_reset_ide = ohare_mb_un_reset_ide,
772};
773
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500774static struct mb_ops heathrow_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 .name = "Heathrow",
776 .content = heathrow_mb_content,
777 .power = heathrow_mb_power,
778 .setup_bus = heathrow_mb_setup_bus,
779 .un_reset = heathrow_mb_un_reset,
780 .un_reset_ide = heathrow_mb_un_reset_ide,
781};
782
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500783static struct mb_ops keylargo_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .name = "KeyLargo",
785 .init = keylargo_mb_init,
786 .content = keylargo_mb_content,
787 .power = keylargo_mb_power,
788 .setup_bus = keylargo_mb_setup_bus,
789 .un_reset = keylargo_mb_un_reset,
790 .un_reset_ide = keylargo_mb_un_reset_ide,
791};
792
793/*
794 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
795 * register is always set when there is something in the media bay.
796 * This causes problems for the interrupt code if we attach an interrupt
797 * handler to the media-bay interrupt, because it tends to go into
798 * an infinite loop calling the media bay interrupt handler.
799 * Therefore we do it all by polling the media bay once each tick.
800 */
801
Jeff Mahoney5e655772005-07-06 15:44:41 -0400802static struct of_device_id media_bay_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 {
805 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .compatible = "keylargo-media-bay",
807 .data = &keylargo_mb_ops,
808 },
809 {
810 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .compatible = "heathrow-media-bay",
812 .data = &heathrow_mb_ops,
813 },
814 {
815 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .compatible = "ohare-media-bay",
817 .data = &ohare_mb_ops,
818 },
819 {},
820};
821
822static struct macio_driver media_bay_driver =
823{
824 .name = "media-bay",
825 .match_table = media_bay_match,
826 .probe = media_bay_attach,
827 .suspend = media_bay_suspend,
828 .resume = media_bay_resume
829};
830
831static int __init media_bay_init(void)
832{
833 int i;
834
835 for (i=0; i<MAX_BAYS; i++) {
836 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
837 media_bays[i].content_id = -1;
838#ifdef CONFIG_BLK_DEV_IDE
839 media_bays[i].cd_index = -1;
840#endif
841 }
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100842 if (!machine_is(powermac))
843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 macio_register_driver(&media_bay_driver);
846
847 return 0;
848}
849
850device_initcall(media_bay_init);