mediabay.c 22 KB

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