blob: 936788272a5f671e435ca220f8ecf5d4f9e9093b [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>
Paul Mackerras33f6e792007-12-13 14:12:58 +110023#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010081#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 void __iomem *cd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int cd_irq;
84 int cd_retry;
85#endif
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010086#if defined(CONFIG_BLK_DEV_IDE_PMAC) || defined(CONFIG_MAC_FLOPPY)
87 int cd_index;
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
91#define MAX_BAYS 2
92
93static struct media_bay_info media_bays[MAX_BAYS];
94int media_bay_count = 0;
95
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010096#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* check the busy bit in the media-bay ide interface
98 (assumes the media-bay contains an ide device) */
99#define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
100#endif
101
102/*
103 * Wait that number of ms between each step in normal polling mode
104 */
105#define MB_POLL_DELAY 25
106
107/*
108 * Consider the media-bay ID value stable if it is the same for
109 * this number of milliseconds
110 */
111#define MB_STABLE_DELAY 100
112
113/* Wait after powering up the media bay this delay in ms
114 * timeout bumped for some powerbooks
115 */
116#define MB_POWER_DELAY 200
117
118/*
119 * Hold the media-bay reset signal true for this many ticks
120 * after a device is inserted before releasing it.
121 */
122#define MB_RESET_DELAY 50
123
124/*
125 * Wait this long after the reset signal is released and before doing
126 * further operations. After this delay, the IDE reset signal is released
127 * too for an IDE device
128 */
129#define MB_SETUP_DELAY 100
130
131/*
132 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
133 * (or until the device is ready) before waiting for busy bit to disappear
134 */
135#define MB_IDE_WAIT 1000
136
137/*
138 * Timeout waiting for busy bit of an IDE device to go down
139 */
140#define MB_IDE_TIMEOUT 5000
141
142/*
143 * Max retries of the full power up/down sequence for an IDE device
144 */
145#define MAX_CD_RETRIES 3
146
147/*
148 * States of a media bay
149 */
150enum {
151 mb_empty = 0, /* Idle */
152 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
153 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
154 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
155 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
156 mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
157 mb_up, /* Media bay full */
158 mb_powering_down /* Powering down (avoid too fast down/up) */
159};
160
161#define MB_POWER_SOUND 0x08
162#define MB_POWER_FLOPPY 0x04
163#define MB_POWER_ATA 0x02
164#define MB_POWER_PCI 0x01
165#define MB_POWER_OFF 0x00
166
167/*
168 * Functions for polling content of media bay
169 */
170
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500171static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172ohare_mb_content(struct media_bay_info *bay)
173{
174 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
175}
176
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500177static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178heathrow_mb_content(struct media_bay_info *bay)
179{
180 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
181}
182
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500183static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184keylargo_mb_content(struct media_bay_info *bay)
185{
186 int new_gpio;
187
188 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
189 if (new_gpio) {
190 bay->cached_gpio = new_gpio;
191 return MB_NO;
192 } else if (bay->cached_gpio != new_gpio) {
193 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
194 (void)MB_IN32(bay, KEYLARGO_MBCR);
195 udelay(5);
196 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
197 (void)MB_IN32(bay, KEYLARGO_MBCR);
198 udelay(5);
199 bay->cached_gpio = new_gpio;
200 }
201 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
202}
203
204/*
205 * Functions for powering up/down the bay, puts the bay device
206 * into reset state as well
207 */
208
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210ohare_mb_power(struct media_bay_info* bay, int on_off)
211{
212 if (on_off) {
213 /* Power up device, assert it's reset line */
214 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
215 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
216 } else {
217 /* Disable all devices */
218 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
219 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
220 /* Cut power from bay, release reset line */
221 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
222 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
223 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
224 }
225 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
226}
227
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500228static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229heathrow_mb_power(struct media_bay_info* bay, int on_off)
230{
231 if (on_off) {
232 /* Power up device, assert it's reset line */
233 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
234 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
235 } else {
236 /* Disable all devices */
237 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
238 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
239 /* Cut power from bay, release reset line */
240 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
241 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
242 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
243 }
244 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
245}
246
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500247static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248keylargo_mb_power(struct media_bay_info* bay, int on_off)
249{
250 if (on_off) {
251 /* Power up device, assert it's reset line */
252 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
253 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
254 } else {
255 /* Disable all devices */
256 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
257 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
258 /* Cut power from bay, release reset line */
259 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
260 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
261 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
262 }
263 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
264}
265
266/*
267 * Functions for configuring the media bay for a given type of device,
268 * enable the related busses
269 */
270
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500271static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
273{
274 switch(device_id) {
275 case MB_FD:
276 case MB_FD1:
277 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
278 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
279 return 0;
280 case MB_CD:
281 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
282 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
283 return 0;
284 case MB_PCI:
285 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
286 return 0;
287 }
288 return -ENODEV;
289}
290
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500291static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
293{
294 switch(device_id) {
295 case MB_FD:
296 case MB_FD1:
297 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
298 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
299 return 0;
300 case MB_CD:
301 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
302 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
303 return 0;
304 case MB_PCI:
305 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
306 return 0;
307 }
308 return -ENODEV;
309}
310
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500311static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
313{
314 switch(device_id) {
315 case MB_CD:
316 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
317 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
318 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
319 return 0;
320 case MB_PCI:
321 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
322 return 0;
323 case MB_SOUND:
324 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
325 return 0;
326 }
327 return -ENODEV;
328}
329
330/*
331 * Functions for tweaking resets
332 */
333
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500334static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335ohare_mb_un_reset(struct media_bay_info* bay)
336{
337 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
338}
339
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500340static void keylargo_mb_init(struct media_bay_info *bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
343}
344
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500345static void heathrow_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
348}
349
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500350static void keylargo_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
353}
354
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500355static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
358}
359
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500360static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
363}
364
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500365static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
368}
369
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500370static inline void set_mb_power(struct media_bay_info* bay, int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 /* Power up up and assert the bay reset line */
373 if (onoff) {
374 bay->ops->power(bay, 1);
375 bay->state = mb_powering_up;
376 MBDBG("mediabay%d: powering up\n", bay->index);
377 } else {
378 /* Make sure everything is powered down & disabled */
379 bay->ops->power(bay, 0);
380 bay->state = mb_powering_down;
381 MBDBG("mediabay%d: powering down\n", bay->index);
382 }
383 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
384}
385
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500386static void poll_media_bay(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 int id = bay->ops->content(bay);
389
390 if (id == bay->last_value) {
391 if (id != bay->content_id) {
392 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
393 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
394 /* If the device type changes without going thru
395 * "MB_NO", we force a pass by "MB_NO" to make sure
396 * things are properly reset
397 */
398 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
399 id = MB_NO;
400 MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
401 }
402 MBDBG("mediabay%d: switching to %d\n", bay->index, id);
403 set_mb_power(bay, id != MB_NO);
404 bay->content_id = id;
405 if (id == MB_NO) {
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100406#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 bay->cd_retry = 0;
408#endif
409 printk(KERN_INFO "media bay %d is empty\n", bay->index);
410 }
411 }
412 }
413 } else {
414 bay->last_value = id;
415 bay->value_count = 0;
416 }
417}
418
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100419#ifdef CONFIG_MAC_FLOPPY
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500420int check_media_bay(struct device_node *which_bay, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int i;
423
424 for (i=0; i<media_bay_count; i++)
425 if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
426 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
427 return 0;
428 media_bays[i].cd_index = -1;
429 return -EINVAL;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ENODEV;
432}
433EXPORT_SYMBOL(check_media_bay);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100434#endif /* CONFIG_MAC_FLOPPY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100436#ifdef CONFIG_BLK_DEV_IDE_PMAC
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500437int check_media_bay_by_base(unsigned long base, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int i;
440
441 for (i=0; i<media_bay_count; i++)
442 if (media_bays[i].mdev && base == (unsigned long) media_bays[i].cd_base) {
443 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
444 return 0;
445 media_bays[i].cd_index = -1;
446 return -EINVAL;
447 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -ENODEV;
450}
451
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500452int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100453 int irq, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 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 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -ENODEV;
488}
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100489#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 case mb_resetting:
525 if (bay->content_id != MB_CD) {
526 MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
527 bay->state = mb_up;
528 break;
529 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100530#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
532 bay->ops->un_reset_ide(bay);
533 bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
534 bay->state = mb_ide_resetting;
535#else
536 printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
537 set_mb_power(bay, 0);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100538#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100540#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 case mb_ide_resetting:
542 bay->timer = msecs_to_jiffies(MB_IDE_TIMEOUT);
543 bay->state = mb_ide_waiting;
544 MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 case mb_ide_waiting:
547 if (bay->cd_base == NULL) {
548 bay->timer = 0;
549 bay->state = mb_up;
550 MBDBG("mediabay%d: up before IDE init\n", i);
551 break;
552 } else if (MB_IDE_READY(i)) {
553 bay->timer = 0;
554 bay->state = mb_up;
555 if (bay->cd_index < 0) {
556 hw_regs_t hw;
557
558 printk("mediabay %d, registering IDE...\n", i);
559 pmu_suspend();
560 ide_init_hwif_ports(&hw, (unsigned long) bay->cd_base, (unsigned long) 0, NULL);
561 hw.irq = bay->cd_irq;
562 hw.chipset = ide_pmac;
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +0100563 bay->cd_index =
564 ide_register_hw(&hw, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 pmu_resume();
566 }
567 if (bay->cd_index == -1) {
568 /* We eventually do a retry */
569 bay->cd_retry++;
570 printk("IDE register error\n");
571 set_mb_power(bay, 0);
572 } else {
573 printk(KERN_DEBUG "media-bay %d is ide%d\n", i, bay->cd_index);
574 MBDBG("mediabay %d IDE ready\n", i);
575 }
576 break;
577 } else if (bay->timer > 0)
578 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
579 if (bay->timer <= 0) {
580 printk("\nIDE Timeout in bay %d !, IDE state is: 0x%02x\n",
581 i, readb(bay->cd_base + 0x70));
582 MBDBG("mediabay%d: nIDE Timeout !\n", i);
583 set_mb_power(bay, 0);
584 bay->timer = 0;
585 }
586 break;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100587#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 case mb_powering_down:
589 bay->state = mb_empty;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100590#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (bay->cd_index >= 0) {
592 printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
593 bay->cd_index);
Bartlomiej Zolnierkiewiczf82c2b12008-02-02 19:56:39 +0100594 ide_unregister(bay->cd_index, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 bay->cd_index = -1;
596 }
597 if (bay->cd_retry) {
598 if (bay->cd_retry > MAX_CD_RETRIES) {
599 /* Should add an error sound (sort of beep in dmasound) */
600 printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
601 } else {
602 /* Force a new power down/up sequence */
603 bay->content_id = MB_NO;
604 }
605 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100606#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 MBDBG("mediabay%d: end of power down\n", i);
608 break;
609 }
610}
611
612/*
613 * This procedure runs as a kernel thread to poll the media bay
614 * once each tick and register and unregister the IDE interface
615 * with the IDE driver. It needs to be a thread because
616 * ide_register can't be called from interrupt context.
617 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500618static int media_bay_task(void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 int i;
621
Paul Mackerras33f6e792007-12-13 14:12:58 +1100622 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 for (i = 0; i < media_bay_count; ++i) {
624 down(&media_bays[i].lock);
625 if (!media_bays[i].sleeping)
626 media_bay_step(i);
627 up(&media_bays[i].lock);
628 }
629
630 msleep_interruptible(MB_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Paul Mackerras33f6e792007-12-13 14:12:58 +1100632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Jeff Mahoney5e655772005-07-06 15:44:41 -0400635static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct media_bay_info* bay;
638 u32 __iomem *regbase;
639 struct device_node *ofnode;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100640 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int i;
642
643 ofnode = mdev->ofdev.node;
644
645 if (macio_resource_count(mdev) < 1)
646 return -ENODEV;
647 if (macio_request_resources(mdev, "media-bay"))
648 return -EBUSY;
649 /* Media bay registers are located at the beginning of the
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100650 * mac-io chip, for now, we trick and align down the first
651 * resource passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100653 base = macio_resource_start(mdev, 0) & 0xffff0000u;
654 regbase = (u32 __iomem *)ioremap(base, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (regbase == NULL) {
656 macio_release_resources(mdev);
657 return -ENOMEM;
658 }
659
660 i = media_bay_count++;
661 bay = &media_bays[i];
662 bay->mdev = mdev;
663 bay->base = regbase;
664 bay->index = i;
665 bay->ops = match->data;
666 bay->sleeping = 0;
667 init_MUTEX(&bay->lock);
668
669 /* Init HW probing */
670 if (bay->ops->init)
671 bay->ops->init(bay);
672
673 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
674
675 /* Force an immediate detect */
676 set_mb_power(bay, 0);
677 msleep(MB_POWER_DELAY);
678 bay->content_id = MB_NO;
679 bay->last_value = bay->ops->content(bay);
680 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
681 bay->state = mb_empty;
682 do {
683 msleep(MB_POLL_DELAY);
684 media_bay_step(i);
685 } while((bay->state != mb_empty) &&
686 (bay->state != mb_up));
687
688 /* Mark us ready by filling our mdev data */
689 macio_set_drvdata(mdev, bay);
690
691 /* Startup kernel thread */
692 if (i == 0)
Paul Mackerras33f6e792007-12-13 14:12:58 +1100693 kthread_run(media_bay_task, NULL, "media-bay");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return 0;
696
697}
698
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500699static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct media_bay_info *bay = macio_get_drvdata(mdev);
702
Pavel Machekca078ba2005-09-03 15:56:57 -0700703 if (state.event != mdev->ofdev.dev.power.power_state.event && state.event == PM_EVENT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 down(&bay->lock);
705 bay->sleeping = 1;
706 set_mb_power(bay, 0);
707 up(&bay->lock);
708 msleep(MB_POLL_DELAY);
709 mdev->ofdev.dev.power.power_state = state;
710 }
711 return 0;
712}
713
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500714static int media_bay_resume(struct macio_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct media_bay_info *bay = macio_get_drvdata(mdev);
717
Pavel Machekca078ba2005-09-03 15:56:57 -0700718 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
Pavel Machek829ca9a2005-09-03 15:56:56 -0700719 mdev->ofdev.dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* We re-enable the bay using it's previous content
722 only if it did not change. Note those bozo timings,
723 they seem to help the 3400 get it right.
724 */
725 /* Force MB power to 0 */
726 down(&bay->lock);
727 set_mb_power(bay, 0);
728 msleep(MB_POWER_DELAY);
729 if (bay->ops->content(bay) != bay->content_id) {
730 printk("mediabay%d: content changed during sleep...\n", bay->index);
731 up(&bay->lock);
732 return 0;
733 }
734 set_mb_power(bay, 1);
735 bay->last_value = bay->content_id;
736 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
737 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100738#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 bay->cd_retry = 0;
740#endif
741 do {
742 msleep(MB_POLL_DELAY);
743 media_bay_step(bay->index);
744 } while((bay->state != mb_empty) &&
745 (bay->state != mb_up));
746 bay->sleeping = 0;
747 up(&bay->lock);
748 }
749 return 0;
750}
751
752
753/* Definitions of "ops" structures.
754 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500755static struct mb_ops ohare_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .name = "Ohare",
757 .content = ohare_mb_content,
758 .power = ohare_mb_power,
759 .setup_bus = ohare_mb_setup_bus,
760 .un_reset = ohare_mb_un_reset,
761 .un_reset_ide = ohare_mb_un_reset_ide,
762};
763
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500764static struct mb_ops heathrow_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .name = "Heathrow",
766 .content = heathrow_mb_content,
767 .power = heathrow_mb_power,
768 .setup_bus = heathrow_mb_setup_bus,
769 .un_reset = heathrow_mb_un_reset,
770 .un_reset_ide = heathrow_mb_un_reset_ide,
771};
772
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500773static struct mb_ops keylargo_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .name = "KeyLargo",
775 .init = keylargo_mb_init,
776 .content = keylargo_mb_content,
777 .power = keylargo_mb_power,
778 .setup_bus = keylargo_mb_setup_bus,
779 .un_reset = keylargo_mb_un_reset,
780 .un_reset_ide = keylargo_mb_un_reset_ide,
781};
782
783/*
784 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
785 * register is always set when there is something in the media bay.
786 * This causes problems for the interrupt code if we attach an interrupt
787 * handler to the media-bay interrupt, because it tends to go into
788 * an infinite loop calling the media bay interrupt handler.
789 * Therefore we do it all by polling the media bay once each tick.
790 */
791
Jeff Mahoney5e655772005-07-06 15:44:41 -0400792static struct of_device_id media_bay_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 {
795 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .compatible = "keylargo-media-bay",
797 .data = &keylargo_mb_ops,
798 },
799 {
800 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .compatible = "heathrow-media-bay",
802 .data = &heathrow_mb_ops,
803 },
804 {
805 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .compatible = "ohare-media-bay",
807 .data = &ohare_mb_ops,
808 },
809 {},
810};
811
812static struct macio_driver media_bay_driver =
813{
814 .name = "media-bay",
815 .match_table = media_bay_match,
816 .probe = media_bay_attach,
817 .suspend = media_bay_suspend,
818 .resume = media_bay_resume
819};
820
821static int __init media_bay_init(void)
822{
823 int i;
824
825 for (i=0; i<MAX_BAYS; i++) {
826 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
827 media_bays[i].content_id = -1;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100828#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 media_bays[i].cd_index = -1;
830#endif
831 }
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100832 if (!machine_is(powermac))
833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 macio_register_driver(&media_bay_driver);
836
837 return 0;
838}
839
840device_initcall(media_bay_init);