mediabay.c 21 KB

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