1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/*
* Driver for the media bay on the PowerBook 3400 and 2400.
*
* Copyright (C) 1998 Paul Mackerras.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#define __KERNEL_SYSCALLS__
#include <linux/config.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/hdreg.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
#include <asm/prom.h>
#include <asm/pgtable.h>
#include <asm/io.h>
#include <asm/feature.h>
#include <asm/mediabay.h>
#include <asm/init.h>
#include <linux/adb.h>
#include <linux/pmu.h>
#ifdef CONFIG_PMAC_PBOOK
static int mb_notify_sleep(struct pmu_sleep_notifier *self, int when);
static struct pmu_sleep_notifier mb_sleep_notifier = {
mb_notify_sleep,
SLEEP_LEVEL_MEDIABAY,
};
#endif
#undef MB_USE_INTERRUPTS
#undef MB_DEBUG
#define MB_IGNORE_SIGNALS
#ifdef MB_DEBUG
#define MBDBG(fmt, arg...) printk(KERN_INFO fmt , ## arg)
#else
#define MBDBG(fmt, arg...) do { } while (0)
#endif
struct media_bay_hw {
unsigned char b0;
unsigned char contents;
unsigned char b2;
unsigned char b3;
};
struct media_bay_info {
volatile struct media_bay_hw* addr;
int content_id;
int state;
int last_value;
int value_count;
int timer;
struct device_node* dev_node;
#ifdef CONFIG_BLK_DEV_IDE
unsigned long cd_base;
int cd_index;
int cd_irq;
int cd_retry;
#endif
};
#define MAX_BAYS 2
static volatile struct media_bay_info media_bays[MAX_BAYS];
int media_bay_count = 0;
#define MB_CONTENTS(i) ((in_8(&media_bays[i].addr->contents) >> 4) & 7)
#ifdef CONFIG_BLK_DEV_IDE
/* check the busy bit in the media-bay ide interface
(assumes the media-bay contains an ide device) */
//#define MB_IDE_READY(i) ((inb(media_bays[i].cd_base + 0x70) & 0xc0) == 0x40)
#define MB_IDE_READY(i) ((inb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
#endif
/* Note: All delays are not in milliseconds and converted to HZ relative
* values by the macro below
*/
#define MS_TO_HZ(ms) ((ms * HZ) / 1000)
/*
* Consider the media-bay ID value stable if it is the same for
* this number of milliseconds
*/
#define MB_STABLE_DELAY 40
/* Wait after powering up the media bay this delay in ms
* timeout bumped for some powerbooks
*/
#define MB_POWER_DELAY 200
/*
* Hold the media-bay reset signal true for this many ticks
* after a device is inserted before releasing it.
*/
#define MB_RESET_DELAY 40
/*
* Wait this long after the reset signal is released and before doing
* further operations. After this delay, the IDE reset signal is released
* too for an IDE device
*/
#define MB_SETUP_DELAY 100
/*
* Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
* (or until the device is ready) before waiting for busy bit to disappear
*/
#define MB_IDE_WAIT 1000
/*
* Timeout waiting for busy bit of an IDE device to go down
*/
#define MB_IDE_TIMEOUT 5000
/*
* Max retries of the full power up/down sequence for an IDE device
*/
#define MAX_CD_RETRIES 3
/*
* States of a media bay
*/
enum {
mb_empty = 0, /* Idle */
mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
mb_up, /* Media bay full */
mb_powering_down /* Powering down (avoid too fast down/up) */
};
static void poll_media_bay(int which);
static void set_media_bay(int which, int id);
static void set_mb_power(int which, int onoff);
static void media_bay_step(int i);
static int media_bay_task(void *);
#ifdef MB_USE_INTERRUPTS
static void media_bay_intr(int irq, void *devid, struct pt_regs *regs);
#endif
/*
* It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
* register is always set when there is something in the media bay.
* This causes problems for the interrupt code if we attach an interrupt
* handler to the media-bay interrupt, because it tends to go into
* an infinite loop calling the media bay interrupt handler.
* Therefore we do it all by polling the media bay once each tick.
*/
void __pmac
media_bay_init(void)
{
struct device_node *np;
int n,i;
for (i=0; i<MAX_BAYS; i++) {
memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
media_bays[i].content_id = -1;
#ifdef CONFIG_BLK_DEV_IDE
media_bays[i].cd_index = -1;
#endif
}
np = find_devices("media-bay");
n = 0;
while(np && (n<MAX_BAYS)) {
if (np->n_addrs == 0)
continue;
media_bays[n].addr = (volatile struct media_bay_hw *)
ioremap(np->addrs[0].address, sizeof(struct media_bay_hw));
#ifdef MB_USE_INTERRUPTS
if (np->n_intrs == 0) {
printk(KERN_ERR "media bay %d has no irq\n",n);
continue;
}
if (request_irq(np->intrs[0].line, media_bay_intr, 0, "Media bay", (void *)n)) {
printk(KERN_ERR "Couldn't get IRQ %d for media bay %d\n",
np->intrs[0].line, n);
continue;
}
#endif
media_bay_count++;
media_bays[n].dev_node = np;
/* Force an immediate detect */
set_mb_power(n,0);
mdelay(MB_POWER_DELAY);
out_8(&media_bays[n].addr->contents, 0x70);
mdelay(MB_STABLE_DELAY);
media_bays[n].content_id = MB_NO;
media_bays[n].last_value = MB_CONTENTS(n);
media_bays[n].value_count = MS_TO_HZ(MB_STABLE_DELAY);
media_bays[n].state = mb_empty;
do {
mdelay(1000/HZ);
media_bay_step(n);
} while((media_bays[n].state != mb_empty) &&
(media_bays[n].state != mb_up));
n++;
np=np->next;
}
if (media_bay_count)
{
printk(KERN_INFO "Registered %d media-bay(s)\n", media_bay_count);
#ifdef CONFIG_PMAC_PBOOK
pmu_register_sleep_notifier(&mb_sleep_notifier);
#endif /* CONFIG_PMAC_PBOOK */
kernel_thread(media_bay_task, NULL, 0);
}
}
#ifdef MB_USE_INTERRUPTS
static void __pmac
media_bay_intr(int irq, void *devid, struct pt_regs *regs)
{
}
#endif
static void __pmac
set_mb_power(int which, int onoff)
{
volatile struct media_bay_info* mb = &media_bays[which];
if (onoff) {
feature_set(mb->dev_node, FEATURE_Mediabay_power);
udelay(10);
feature_set(mb->dev_node, FEATURE_Mediabay_reset);
udelay(10);
mb->state = mb_powering_up;
MBDBG("mediabay%d: powering up\n", which);
} else {
feature_clear(mb->dev_node, FEATURE_Mediabay_floppy_enable);
feature_clear(mb->dev_node, FEATURE_Mediabay_IDE_enable);
feature_clear(mb->dev_node, FEATURE_Mediabay_PCI_enable);
feature_clear(mb->dev_node, FEATURE_SWIM3_enable);
feature_clear(mb->dev_node, FEATURE_Mediabay_power);
mb->state = mb_powering_down;
MBDBG("mediabay%d: powering down\n", which);
}
mb->timer = MS_TO_HZ(MB_POWER_DELAY);
}
static void __pmac
set_media_bay(int which, int id)
{
volatile struct media_bay_info* bay;
bay = &media_bays[which];
switch (id) {
case MB_CD:
feature_set(bay->dev_node, FEATURE_Mediabay_IDE_enable);
udelay(10);
feature_set(bay->dev_node, FEATURE_Mediabay_IDE_reset);
printk(KERN_INFO "media bay %d contains a CD-ROM drive\n", which);
break;
case MB_FD:
case MB_FD1:
feature_set(bay->dev_node, FEATURE_Mediabay_floppy_enable);
feature_set(bay->dev_node, FEATURE_SWIM3_enable);
printk(KERN_INFO "media bay %d contains a floppy disk drive\n", which);
break;
case MB_NO:
break;
default:
printk(KERN_INFO "media bay %d contains an unknown device (%d)\n",
which, id);
break;
}
}
int __pmac
check_media_bay(struct device_node *which_bay, int what)
{
#ifdef CONFIG_BLK_DEV_IDE
int i;
for (i=0; i<media_bay_count; i++)
if (which_bay == media_bays[i].dev_node)
{
if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
return 0;
media_bays[i].cd_index = -1;
return -EINVAL;
}
#endif /* CONFIG_BLK_DEV_IDE */
return -ENODEV;
}
int __pmac
check_media_bay_by_base(unsigned long base, int what)
{
#ifdef CONFIG_BLK_DEV_IDE
int i;
for (i=0; i<media_bay_count; i++)
if (base == media_bays[i].cd_base)
{
if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
return 0;
media_bays[i].cd_index = -1;
return -EINVAL;
}
#endif
return -ENODEV;
}
int __pmac
media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
int irq, int index)
{
#ifdef CONFIG_BLK_DEV_IDE
int i;
for (i=0; i<media_bay_count; i++)
if (which_bay == media_bays[i].dev_node)
{
int timeout = 5000;
media_bays[i].cd_base = base;
media_bays[i].cd_irq = irq;
if ((MB_CD != media_bays[i].content_id) || media_bays[i].state != mb_up)
return 0;
printk(KERN_DEBUG "Registered ide %d for media bay %d\n", index, i);
do {
if (MB_IDE_READY(i)) {
media_bays[i].cd_index = index;
return 0;
}
mdelay(1);
} while(--timeout);
printk(KERN_DEBUG "Timeout waiting IDE in bay %d\n", i);
return -ENODEV;
}
#endif
return -ENODEV;
}
static void __pmac
media_bay_step(int i)
{
volatile struct media_bay_info* bay = &media_bays[i];
/* We don't poll when powering down */
if (bay->state != mb_powering_down)
poll_media_bay(i);
/* If timer expired or polling IDE busy, run state machine */
if ((bay->state != mb_ide_waiting) && (bay->timer != 0) && ((--bay->timer) != 0))
return;
switch(bay->state) {
case mb_powering_up:
set_media_bay(i, bay->last_value);
bay->timer = MS_TO_HZ(MB_RESET_DELAY);
bay->state = mb_enabling_bay;
MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
break;
case mb_enabling_bay:
feature_clear(bay->dev_node, FEATURE_Mediabay_reset);
bay->timer = MS_TO_HZ(MB_SETUP_DELAY);
bay->state = mb_resetting;
MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
break;
case mb_resetting:
if (bay->content_id != MB_CD) {
MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
bay->state = mb_up;
break;
}
#ifdef CONFIG_BLK_DEV_IDE
MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
feature_clear(bay->dev_node, FEATURE_Mediabay_IDE_reset);
bay->timer = MS_TO_HZ(MB_IDE_WAIT);
bay->state = mb_ide_resetting;
#else
printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
set_mb_power(i, 0);
#endif // #ifdef CONFIG_BLK_DEV_IDE
break;
#ifdef CONFIG_BLK_DEV_IDE
case mb_ide_resetting:
bay->timer = MS_TO_HZ(MB_IDE_TIMEOUT);
bay->state = mb_ide_waiting;
MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
break;
case mb_ide_waiting:
if (bay->cd_base == 0) {
bay->timer = 0;
bay->state = mb_up;
MBDBG("mediabay%d: up before IDE init\n", i);
break;
} else if (MB_IDE_READY(i)) {
bay->timer = 0;
bay->state = mb_up;
if (bay->cd_index < 0)
bay->cd_index = ide_register(bay->cd_base, 0, bay->cd_irq);
if (bay->cd_index == -1) {
/* We eventually do a retry */
bay->cd_retry++;
printk("IDE register error\n");
set_mb_power(i, 0);
} else {
printk(KERN_DEBUG "media-bay %d is ide %d\n", i, bay->cd_index);
MBDBG("mediabay %d IDE ready\n", i);
}
break;
}
if (bay->timer == 0) {
printk("\nIDE Timeout in bay %d !\n", i);
MBDBG("mediabay%d: nIDE Timeout !\n", i);
set_mb_power(i, 0);
}
break;
#endif // #ifdef CONFIG_BLK_DEV_IDE
case mb_powering_down:
bay->state = mb_empty;
#ifdef CONFIG_BLK_DEV_IDE
if (bay->cd_index >= 0) {
printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
bay->cd_index);
ide_unregister(bay->cd_index);
bay->cd_index = -1;
}
if (bay->cd_retry) {
if (bay->cd_retry > MAX_CD_RETRIES) {
/* Should add an error sound (sort of beep in dmasound) */
printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
} else {
/* Force a new power down/up sequence */
bay->content_id = MB_NO;
}
}
#endif
MBDBG("mediabay%d: end of power down\n", i);
break;
}
}
/*
* This procedure runs as a kernel thread to poll the media bay
* once each tick and register and unregister the IDE interface
* with the IDE driver. It needs to be a thread because
* ide_register can't be called from interrupt context.
*/
int __pmac
media_bay_task(void *x)
{
int i = 0;
strcpy(current->comm, "media-bay");
#ifdef MB_IGNORE_SIGNALS
sigfillset(¤t->blocked);
#endif
for (;;) {
media_bay_step(i);
if (++i >= media_bay_count) {
i = 0;
current->state = TASK_INTERRUPTIBLE;
schedule_timeout(1);
if (signal_pending(current))
return 0;
}
}
}
void __pmac
poll_media_bay(int which)
{
volatile struct media_bay_info* bay = &media_bays[which];
int id = MB_CONTENTS(which);
if (id == bay->last_value) {
if (id != bay->content_id
&& ++bay->value_count >= MS_TO_HZ(MB_STABLE_DELAY)) {
/* If the device type changes without going thru "MB_NO", we force
a pass by "MB_NO" to make sure things are properly reset */
if ((id != MB_NO) && (bay->content_id != MB_NO)) {
id = MB_NO;
MBDBG("mediabay%d: forcing MB_NO\n", which);
}
MBDBG("mediabay%d: switching to %d\n", which, id);
set_mb_power(which, id != MB_NO);
bay->content_id = id;
if (id == MB_NO) {
#ifdef CONFIG_BLK_DEV_IDE
bay->cd_retry = 0;
#endif
printk(KERN_INFO "media bay %d is empty\n", which);
}
}
} else {
bay->last_value = id;
bay->value_count = 0;
}
}
#ifdef CONFIG_PMAC_PBOOK
/*
* notify clients before sleep and reset bus afterwards
*/
int __pmac
mb_notify_sleep(struct pmu_sleep_notifier *self, int when)
{
volatile struct media_bay_info* bay;
int i;
switch (when) {
case PBOOK_SLEEP_REQUEST:
case PBOOK_SLEEP_REJECT:
break;
case PBOOK_SLEEP_NOW:
for (i=0; i<media_bay_count; i++) {
bay = &media_bays[i];
set_mb_power(i, 0);
mdelay(10);
out_8(&bay->addr->contents, 0x70);
}
break;
case PBOOK_WAKE:
for (i=0; i<media_bay_count; i++) {
bay = &media_bays[i];
/* We re-enable the bay using it's previous content
only if it did not change. Note those bozo timings, they seem
to help the 3400 get it right
*/
mdelay(MB_STABLE_DELAY);
out_8(&bay->addr->contents, 0x70);
mdelay(MB_STABLE_DELAY);
if (MB_CONTENTS(i) != bay->content_id)
continue;
set_mb_power(i, 1);
mdelay(MB_POWER_DELAY);
media_bays[i].last_value = bay->content_id;
media_bays[i].value_count = MS_TO_HZ(MB_STABLE_DELAY);
media_bays[i].timer = 0;
media_bays[i].cd_retry = 0;
do {
mdelay(1000/HZ);
media_bay_step(i);
} while((media_bays[i].state != mb_empty) &&
(media_bays[i].state != mb_up));
}
break;
}
return PBOOK_SLEEP_OK;
}
#endif /* CONFIG_PMAC_PBOOK */
|