blob: cf02188a90a6bf5b226b4371da9c684c7c896d52 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/init.h>
21#include <linux/ide.h>
Paul Mackerras33f6e792007-12-13 14:12:58 +110022#include <linux/kthread.h>
Daniel Walker9a247292008-06-10 09:26:09 +100023#include <linux/mutex.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;
Daniel Walker9a247292008-06-10 09:26:09 +100080 struct mutex lock;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010081#ifdef CONFIG_BLK_DEV_IDE_PMAC
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +020082 ide_hwif_t *cd_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 void __iomem *cd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int cd_irq;
85 int cd_retry;
86#endif
Adrian Bunk305c7362008-06-11 03:47:45 +100087#if defined(CONFIG_BLK_DEV_IDE_PMAC)
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010088 int cd_index;
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
92#define MAX_BAYS 2
93
94static struct media_bay_info media_bays[MAX_BAYS];
95int media_bay_count = 0;
96
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +010097#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* check the busy bit in the media-bay ide interface
99 (assumes the media-bay contains an ide device) */
100#define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
101#endif
102
103/*
104 * Wait that number of ms between each step in normal polling mode
105 */
106#define MB_POLL_DELAY 25
107
108/*
109 * Consider the media-bay ID value stable if it is the same for
110 * this number of milliseconds
111 */
112#define MB_STABLE_DELAY 100
113
114/* Wait after powering up the media bay this delay in ms
115 * timeout bumped for some powerbooks
116 */
117#define MB_POWER_DELAY 200
118
119/*
120 * Hold the media-bay reset signal true for this many ticks
121 * after a device is inserted before releasing it.
122 */
123#define MB_RESET_DELAY 50
124
125/*
126 * Wait this long after the reset signal is released and before doing
127 * further operations. After this delay, the IDE reset signal is released
128 * too for an IDE device
129 */
130#define MB_SETUP_DELAY 100
131
132/*
133 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
134 * (or until the device is ready) before waiting for busy bit to disappear
135 */
136#define MB_IDE_WAIT 1000
137
138/*
139 * Timeout waiting for busy bit of an IDE device to go down
140 */
141#define MB_IDE_TIMEOUT 5000
142
143/*
144 * Max retries of the full power up/down sequence for an IDE device
145 */
146#define MAX_CD_RETRIES 3
147
148/*
149 * States of a media bay
150 */
151enum {
152 mb_empty = 0, /* Idle */
153 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
154 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
155 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
156 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
157 mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
158 mb_up, /* Media bay full */
159 mb_powering_down /* Powering down (avoid too fast down/up) */
160};
161
162#define MB_POWER_SOUND 0x08
163#define MB_POWER_FLOPPY 0x04
164#define MB_POWER_ATA 0x02
165#define MB_POWER_PCI 0x01
166#define MB_POWER_OFF 0x00
167
168/*
169 * Functions for polling content of media bay
170 */
171
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500172static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173ohare_mb_content(struct media_bay_info *bay)
174{
175 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
176}
177
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500178static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179heathrow_mb_content(struct media_bay_info *bay)
180{
181 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
182}
183
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500184static u8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185keylargo_mb_content(struct media_bay_info *bay)
186{
187 int new_gpio;
188
189 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
190 if (new_gpio) {
191 bay->cached_gpio = new_gpio;
192 return MB_NO;
193 } else if (bay->cached_gpio != new_gpio) {
194 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
195 (void)MB_IN32(bay, KEYLARGO_MBCR);
196 udelay(5);
197 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
198 (void)MB_IN32(bay, KEYLARGO_MBCR);
199 udelay(5);
200 bay->cached_gpio = new_gpio;
201 }
202 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
203}
204
205/*
206 * Functions for powering up/down the bay, puts the bay device
207 * into reset state as well
208 */
209
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500210static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211ohare_mb_power(struct media_bay_info* bay, int on_off)
212{
213 if (on_off) {
214 /* Power up device, assert it's reset line */
215 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
216 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
217 } else {
218 /* Disable all devices */
219 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
220 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
221 /* Cut power from bay, release reset line */
222 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
223 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
224 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
225 }
226 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
227}
228
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500229static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230heathrow_mb_power(struct media_bay_info* bay, int on_off)
231{
232 if (on_off) {
233 /* Power up device, assert it's reset line */
234 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
235 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
236 } else {
237 /* Disable all devices */
238 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
239 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
240 /* Cut power from bay, release reset line */
241 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
242 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
243 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
244 }
245 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
246}
247
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500248static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249keylargo_mb_power(struct media_bay_info* bay, int on_off)
250{
251 if (on_off) {
252 /* Power up device, assert it's reset line */
253 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
254 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
255 } else {
256 /* Disable all devices */
257 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
258 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
259 /* Cut power from bay, release reset line */
260 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
261 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
262 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
263 }
264 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
265}
266
267/*
268 * Functions for configuring the media bay for a given type of device,
269 * enable the related busses
270 */
271
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500272static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
274{
275 switch(device_id) {
276 case MB_FD:
277 case MB_FD1:
278 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
279 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
280 return 0;
281 case MB_CD:
282 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
283 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
284 return 0;
285 case MB_PCI:
286 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
287 return 0;
288 }
289 return -ENODEV;
290}
291
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500292static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
294{
295 switch(device_id) {
296 case MB_FD:
297 case MB_FD1:
298 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
299 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
300 return 0;
301 case MB_CD:
302 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
303 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
304 return 0;
305 case MB_PCI:
306 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
307 return 0;
308 }
309 return -ENODEV;
310}
311
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500312static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
314{
315 switch(device_id) {
316 case MB_CD:
317 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
318 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
319 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
320 return 0;
321 case MB_PCI:
322 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
323 return 0;
324 case MB_SOUND:
325 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
326 return 0;
327 }
328 return -ENODEV;
329}
330
331/*
332 * Functions for tweaking resets
333 */
334
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500335static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336ohare_mb_un_reset(struct media_bay_info* bay)
337{
338 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
339}
340
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500341static void keylargo_mb_init(struct media_bay_info *bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
344}
345
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500346static void heathrow_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
349}
350
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500351static void keylargo_mb_un_reset(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
354}
355
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500356static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
359}
360
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500361static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
364}
365
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500366static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
369}
370
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500371static inline void set_mb_power(struct media_bay_info* bay, int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 /* Power up up and assert the bay reset line */
374 if (onoff) {
375 bay->ops->power(bay, 1);
376 bay->state = mb_powering_up;
377 MBDBG("mediabay%d: powering up\n", bay->index);
378 } else {
379 /* Make sure everything is powered down & disabled */
380 bay->ops->power(bay, 0);
381 bay->state = mb_powering_down;
382 MBDBG("mediabay%d: powering down\n", bay->index);
383 }
384 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
385}
386
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500387static void poll_media_bay(struct media_bay_info* bay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 int id = bay->ops->content(bay);
390
391 if (id == bay->last_value) {
392 if (id != bay->content_id) {
393 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
394 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
395 /* If the device type changes without going thru
396 * "MB_NO", we force a pass by "MB_NO" to make sure
397 * things are properly reset
398 */
399 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
400 id = MB_NO;
401 MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
402 }
403 MBDBG("mediabay%d: switching to %d\n", bay->index, id);
404 set_mb_power(bay, id != MB_NO);
405 bay->content_id = id;
406 if (id == MB_NO) {
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100407#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 bay->cd_retry = 0;
409#endif
410 printk(KERN_INFO "media bay %d is empty\n", bay->index);
411 }
412 }
413 }
414 } else {
415 bay->last_value = id;
416 bay->value_count = 0;
417 }
418}
419
Adrian Bunk305c7362008-06-11 03:47:45 +1000420#ifdef CONFIG_BLK_DEV_IDE_PMAC
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500421int check_media_bay(struct device_node *which_bay, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int i;
424
425 for (i=0; i<media_bay_count; i++)
426 if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
427 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
428 return 0;
429 media_bays[i].cd_index = -1;
430 return -EINVAL;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -ENODEV;
433}
434EXPORT_SYMBOL(check_media_bay);
435
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500436int check_media_bay_by_base(unsigned long base, int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 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 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -ENODEV;
449}
Bartlomiej Zolnierkiewicz6329db82009-04-21 09:04:37 +0000450EXPORT_SYMBOL_GPL(check_media_bay_by_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500452int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200453 int irq, ide_hwif_t *hwif)
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) {
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200461 int timeout = 5000, index = hwif->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Daniel Walker9a247292008-06-10 09:26:09 +1000463 mutex_lock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200465 bay->cd_port = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bay->cd_base = (void __iomem *) base;
467 bay->cd_irq = irq;
468
469 if ((MB_CD != bay->content_id) || bay->state != mb_up) {
Daniel Walker9a247292008-06-10 09:26:09 +1000470 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 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;
Daniel Walker9a247292008-06-10 09:26:09 +1000477 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479 }
480 mdelay(1);
481 } while(--timeout);
482 printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
Daniel Walker9a247292008-06-10 09:26:09 +1000483 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -ENODEV;
485 }
486 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENODEV;
489}
Bartlomiej Zolnierkiewicz6329db82009-04-21 09:04:37 +0000490EXPORT_SYMBOL_GPL(media_bay_set_ide_infos);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100491#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500493static void media_bay_step(int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct media_bay_info* bay = &media_bays[i];
496
497 /* We don't poll when powering down */
498 if (bay->state != mb_powering_down)
499 poll_media_bay(bay);
500
501 /* If timer expired or polling IDE busy, run state machine */
502 if ((bay->state != mb_ide_waiting) && (bay->timer != 0)) {
503 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
504 if (bay->timer > 0)
505 return;
506 bay->timer = 0;
507 }
508
509 switch(bay->state) {
510 case mb_powering_up:
511 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
512 MBDBG("mediabay%d: device not supported (kind:%d)\n", i, bay->content_id);
513 set_mb_power(bay, 0);
514 break;
515 }
516 bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
517 bay->state = mb_enabling_bay;
518 MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
519 break;
520 case mb_enabling_bay:
521 bay->ops->un_reset(bay);
522 bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
523 bay->state = mb_resetting;
524 MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 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 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100532#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 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);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100540#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100542#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case mb_ide_waiting:
549 if (bay->cd_base == NULL) {
550 bay->timer = 0;
551 bay->state = mb_up;
552 MBDBG("mediabay%d: up before IDE init\n", i);
553 break;
554 } else if (MB_IDE_READY(i)) {
555 bay->timer = 0;
556 bay->state = mb_up;
557 if (bay->cd_index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 printk("mediabay %d, registering IDE...\n", i);
559 pmu_suspend();
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200560 ide_port_scan(bay->cd_port);
Bartlomiej Zolnierkiewiczc1a8e392008-06-15 21:00:23 +0200561 if (bay->cd_port->present)
562 bay->cd_index = bay->cd_port->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 pmu_resume();
564 }
565 if (bay->cd_index == -1) {
566 /* We eventually do a retry */
567 bay->cd_retry++;
568 printk("IDE register error\n");
569 set_mb_power(bay, 0);
570 } else {
571 printk(KERN_DEBUG "media-bay %d is ide%d\n", i, bay->cd_index);
572 MBDBG("mediabay %d IDE ready\n", i);
573 }
574 break;
575 } else if (bay->timer > 0)
576 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
577 if (bay->timer <= 0) {
578 printk("\nIDE Timeout in bay %d !, IDE state is: 0x%02x\n",
579 i, readb(bay->cd_base + 0x70));
580 MBDBG("mediabay%d: nIDE Timeout !\n", i);
581 set_mb_power(bay, 0);
582 bay->timer = 0;
583 }
584 break;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100585#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 case mb_powering_down:
587 bay->state = mb_empty;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100588#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (bay->cd_index >= 0) {
590 printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
591 bay->cd_index);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200592 ide_port_unregister_devices(bay->cd_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 bay->cd_index = -1;
594 }
595 if (bay->cd_retry) {
596 if (bay->cd_retry > MAX_CD_RETRIES) {
597 /* Should add an error sound (sort of beep in dmasound) */
598 printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
599 } else {
600 /* Force a new power down/up sequence */
601 bay->content_id = MB_NO;
602 }
603 }
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100604#endif /* CONFIG_BLK_DEV_IDE_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 MBDBG("mediabay%d: end of power down\n", i);
606 break;
607 }
608}
609
610/*
611 * This procedure runs as a kernel thread to poll the media bay
612 * once each tick and register and unregister the IDE interface
613 * with the IDE driver. It needs to be a thread because
614 * ide_register can't be called from interrupt context.
615 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500616static int media_bay_task(void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int i;
619
Paul Mackerras33f6e792007-12-13 14:12:58 +1100620 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 for (i = 0; i < media_bay_count; ++i) {
Daniel Walker9a247292008-06-10 09:26:09 +1000622 mutex_lock(&media_bays[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!media_bays[i].sleeping)
624 media_bay_step(i);
Daniel Walker9a247292008-06-10 09:26:09 +1000625 mutex_unlock(&media_bays[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 msleep_interruptible(MB_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Paul Mackerras33f6e792007-12-13 14:12:58 +1100630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Jeff Mahoney5e655772005-07-06 15:44:41 -0400633static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct media_bay_info* bay;
636 u32 __iomem *regbase;
637 struct device_node *ofnode;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100638 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int i;
640
641 ofnode = mdev->ofdev.node;
642
643 if (macio_resource_count(mdev) < 1)
644 return -ENODEV;
645 if (macio_request_resources(mdev, "media-bay"))
646 return -EBUSY;
647 /* Media bay registers are located at the beginning of the
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100648 * mac-io chip, for now, we trick and align down the first
649 * resource passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100651 base = macio_resource_start(mdev, 0) & 0xffff0000u;
652 regbase = (u32 __iomem *)ioremap(base, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (regbase == NULL) {
654 macio_release_resources(mdev);
655 return -ENOMEM;
656 }
657
658 i = media_bay_count++;
659 bay = &media_bays[i];
660 bay->mdev = mdev;
661 bay->base = regbase;
662 bay->index = i;
663 bay->ops = match->data;
664 bay->sleeping = 0;
Daniel Walker9a247292008-06-10 09:26:09 +1000665 mutex_init(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Init HW probing */
668 if (bay->ops->init)
669 bay->ops->init(bay);
670
671 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
672
673 /* Force an immediate detect */
674 set_mb_power(bay, 0);
675 msleep(MB_POWER_DELAY);
676 bay->content_id = MB_NO;
677 bay->last_value = bay->ops->content(bay);
678 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
679 bay->state = mb_empty;
680 do {
681 msleep(MB_POLL_DELAY);
682 media_bay_step(i);
683 } while((bay->state != mb_empty) &&
684 (bay->state != mb_up));
685
686 /* Mark us ready by filling our mdev data */
687 macio_set_drvdata(mdev, bay);
688
689 /* Startup kernel thread */
690 if (i == 0)
Paul Mackerras33f6e792007-12-13 14:12:58 +1100691 kthread_run(media_bay_task, NULL, "media-bay");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return 0;
694
695}
696
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500697static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct media_bay_info *bay = macio_get_drvdata(mdev);
700
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100701 if (state.event != mdev->ofdev.dev.power.power_state.event
702 && (state.event & PM_EVENT_SLEEP)) {
Daniel Walker9a247292008-06-10 09:26:09 +1000703 mutex_lock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 bay->sleeping = 1;
705 set_mb_power(bay, 0);
Daniel Walker9a247292008-06-10 09:26:09 +1000706 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 msleep(MB_POLL_DELAY);
708 mdev->ofdev.dev.power.power_state = state;
709 }
710 return 0;
711}
712
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500713static int media_bay_resume(struct macio_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct media_bay_info *bay = macio_get_drvdata(mdev);
716
Pavel Machekca078ba2005-09-03 15:56:57 -0700717 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
Pavel Machek829ca9a2005-09-03 15:56:56 -0700718 mdev->ofdev.dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* We re-enable the bay using it's previous content
721 only if it did not change. Note those bozo timings,
722 they seem to help the 3400 get it right.
723 */
724 /* Force MB power to 0 */
Daniel Walker9a247292008-06-10 09:26:09 +1000725 mutex_lock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 set_mb_power(bay, 0);
727 msleep(MB_POWER_DELAY);
728 if (bay->ops->content(bay) != bay->content_id) {
729 printk("mediabay%d: content changed during sleep...\n", bay->index);
Daniel Walker9a247292008-06-10 09:26:09 +1000730 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return 0;
732 }
733 set_mb_power(bay, 1);
734 bay->last_value = bay->content_id;
735 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
736 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100737#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 bay->cd_retry = 0;
739#endif
740 do {
741 msleep(MB_POLL_DELAY);
742 media_bay_step(bay->index);
743 } while((bay->state != mb_empty) &&
744 (bay->state != mb_up));
745 bay->sleeping = 0;
Daniel Walker9a247292008-06-10 09:26:09 +1000746 mutex_unlock(&bay->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 return 0;
749}
750
751
752/* Definitions of "ops" structures.
753 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500754static struct mb_ops ohare_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .name = "Ohare",
756 .content = ohare_mb_content,
757 .power = ohare_mb_power,
758 .setup_bus = ohare_mb_setup_bus,
759 .un_reset = ohare_mb_un_reset,
760 .un_reset_ide = ohare_mb_un_reset_ide,
761};
762
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500763static struct mb_ops heathrow_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 .name = "Heathrow",
765 .content = heathrow_mb_content,
766 .power = heathrow_mb_power,
767 .setup_bus = heathrow_mb_setup_bus,
768 .un_reset = heathrow_mb_un_reset,
769 .un_reset_ide = heathrow_mb_un_reset_ide,
770};
771
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500772static struct mb_ops keylargo_mb_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 .name = "KeyLargo",
774 .init = keylargo_mb_init,
775 .content = keylargo_mb_content,
776 .power = keylargo_mb_power,
777 .setup_bus = keylargo_mb_setup_bus,
778 .un_reset = keylargo_mb_un_reset,
779 .un_reset_ide = keylargo_mb_un_reset_ide,
780};
781
782/*
783 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
784 * register is always set when there is something in the media bay.
785 * This causes problems for the interrupt code if we attach an interrupt
786 * handler to the media-bay interrupt, because it tends to go into
787 * an infinite loop calling the media bay interrupt handler.
788 * Therefore we do it all by polling the media bay once each tick.
789 */
790
Jeff Mahoney5e655772005-07-06 15:44:41 -0400791static struct of_device_id media_bay_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 {
794 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .compatible = "keylargo-media-bay",
796 .data = &keylargo_mb_ops,
797 },
798 {
799 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .compatible = "heathrow-media-bay",
801 .data = &heathrow_mb_ops,
802 },
803 {
804 .name = "media-bay",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .compatible = "ohare-media-bay",
806 .data = &ohare_mb_ops,
807 },
808 {},
809};
810
811static struct macio_driver media_bay_driver =
812{
813 .name = "media-bay",
814 .match_table = media_bay_match,
815 .probe = media_bay_attach,
816 .suspend = media_bay_suspend,
817 .resume = media_bay_resume
818};
819
820static int __init media_bay_init(void)
821{
822 int i;
823
824 for (i=0; i<MAX_BAYS; i++) {
825 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
826 media_bays[i].content_id = -1;
Bartlomiej Zolnierkiewicz34394e42008-02-06 02:57:50 +0100827#ifdef CONFIG_BLK_DEV_IDE_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 media_bays[i].cd_index = -1;
829#endif
830 }
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100831 if (!machine_is(powermac))
832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 macio_register_driver(&media_bay_driver);
835
836 return 0;
837}
838
839device_initcall(media_bay_init);