mediabay.c 22 KB

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