pmac.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /*
  2. * Support for IDE interfaces on PowerMacs.
  3. *
  4. * These IDE interfaces are memory-mapped and have a DBDMA channel
  5. * for doing DMA.
  6. *
  7. * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt
  8. * Copyright (C) 2007-2008 Bartlomiej Zolnierkiewicz
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Some code taken from drivers/ide/ide-dma.c:
  16. *
  17. * Copyright (c) 1995-1998 Mark Lord
  18. *
  19. * TODO: - Use pre-calculated (kauai) timing tables all the time and
  20. * get rid of the "rounded" tables used previously, so we have the
  21. * same table format for all controllers and can then just have one
  22. * big table
  23. *
  24. */
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/ide.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/pci.h>
  33. #include <linux/adb.h>
  34. #include <linux/pmu.h>
  35. #include <linux/scatterlist.h>
  36. #include <asm/prom.h>
  37. #include <asm/io.h>
  38. #include <asm/dbdma.h>
  39. #include <asm/ide.h>
  40. #include <asm/pci-bridge.h>
  41. #include <asm/machdep.h>
  42. #include <asm/pmac_feature.h>
  43. #include <asm/sections.h>
  44. #include <asm/irq.h>
  45. #ifndef CONFIG_PPC64
  46. #include <asm/mediabay.h>
  47. #endif
  48. #define DRV_NAME "ide-pmac"
  49. #undef IDE_PMAC_DEBUG
  50. #define DMA_WAIT_TIMEOUT 50
  51. typedef struct pmac_ide_hwif {
  52. unsigned long regbase;
  53. int irq;
  54. int kind;
  55. int aapl_bus_id;
  56. unsigned mediabay : 1;
  57. unsigned broken_dma : 1;
  58. unsigned broken_dma_warn : 1;
  59. struct device_node* node;
  60. struct macio_dev *mdev;
  61. u32 timings[4];
  62. volatile u32 __iomem * *kauai_fcr;
  63. /* Those fields are duplicating what is in hwif. We currently
  64. * can't use the hwif ones because of some assumptions that are
  65. * beeing done by the generic code about the kind of dma controller
  66. * and format of the dma table. This will have to be fixed though.
  67. */
  68. volatile struct dbdma_regs __iomem * dma_regs;
  69. struct dbdma_cmd* dma_table_cpu;
  70. } pmac_ide_hwif_t;
  71. enum {
  72. controller_ohare, /* OHare based */
  73. controller_heathrow, /* Heathrow/Paddington */
  74. controller_kl_ata3, /* KeyLargo ATA-3 */
  75. controller_kl_ata4, /* KeyLargo ATA-4 */
  76. controller_un_ata6, /* UniNorth2 ATA-6 */
  77. controller_k2_ata6, /* K2 ATA-6 */
  78. controller_sh_ata6, /* Shasta ATA-6 */
  79. };
  80. static const char* model_name[] = {
  81. "OHare ATA", /* OHare based */
  82. "Heathrow ATA", /* Heathrow/Paddington */
  83. "KeyLargo ATA-3", /* KeyLargo ATA-3 (MDMA only) */
  84. "KeyLargo ATA-4", /* KeyLargo ATA-4 (UDMA/66) */
  85. "UniNorth ATA-6", /* UniNorth2 ATA-6 (UDMA/100) */
  86. "K2 ATA-6", /* K2 ATA-6 (UDMA/100) */
  87. "Shasta ATA-6", /* Shasta ATA-6 (UDMA/133) */
  88. };
  89. /*
  90. * Extra registers, both 32-bit little-endian
  91. */
  92. #define IDE_TIMING_CONFIG 0x200
  93. #define IDE_INTERRUPT 0x300
  94. /* Kauai (U2) ATA has different register setup */
  95. #define IDE_KAUAI_PIO_CONFIG 0x200
  96. #define IDE_KAUAI_ULTRA_CONFIG 0x210
  97. #define IDE_KAUAI_POLL_CONFIG 0x220
  98. /*
  99. * Timing configuration register definitions
  100. */
  101. /* Number of IDE_SYSCLK_NS ticks, argument is in nanoseconds */
  102. #define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
  103. #define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
  104. #define IDE_SYSCLK_NS 30 /* 33Mhz cell */
  105. #define IDE_SYSCLK_66_NS 15 /* 66Mhz cell */
  106. /* 133Mhz cell, found in shasta.
  107. * See comments about 100 Mhz Uninorth 2...
  108. * Note that PIO_MASK and MDMA_MASK seem to overlap
  109. */
  110. #define TR_133_PIOREG_PIO_MASK 0xff000fff
  111. #define TR_133_PIOREG_MDMA_MASK 0x00fff800
  112. #define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
  113. #define TR_133_UDMAREG_UDMA_EN 0x00000001
  114. /* 100Mhz cell, found in Uninorth 2. I don't have much infos about
  115. * this one yet, it appears as a pci device (106b/0033) on uninorth
  116. * internal PCI bus and it's clock is controlled like gem or fw. It
  117. * appears to be an evolution of keylargo ATA4 with a timing register
  118. * extended to 2 32bits registers and a similar DBDMA channel. Other
  119. * registers seem to exist but I can't tell much about them.
  120. *
  121. * So far, I'm using pre-calculated tables for this extracted from
  122. * the values used by the MacOS X driver.
  123. *
  124. * The "PIO" register controls PIO and MDMA timings, the "ULTRA"
  125. * register controls the UDMA timings. At least, it seems bit 0
  126. * of this one enables UDMA vs. MDMA, and bits 4..7 are the
  127. * cycle time in units of 10ns. Bits 8..15 are used by I don't
  128. * know their meaning yet
  129. */
  130. #define TR_100_PIOREG_PIO_MASK 0xff000fff
  131. #define TR_100_PIOREG_MDMA_MASK 0x00fff000
  132. #define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
  133. #define TR_100_UDMAREG_UDMA_EN 0x00000001
  134. /* 66Mhz cell, found in KeyLargo. Can do ultra mode 0 to 2 on
  135. * 40 connector cable and to 4 on 80 connector one.
  136. * Clock unit is 15ns (66Mhz)
  137. *
  138. * 3 Values can be programmed:
  139. * - Write data setup, which appears to match the cycle time. They
  140. * also call it DIOW setup.
  141. * - Ready to pause time (from spec)
  142. * - Address setup. That one is weird. I don't see where exactly
  143. * it fits in UDMA cycles, I got it's name from an obscure piece
  144. * of commented out code in Darwin. They leave it to 0, we do as
  145. * well, despite a comment that would lead to think it has a
  146. * min value of 45ns.
  147. * Apple also add 60ns to the write data setup (or cycle time ?) on
  148. * reads.
  149. */
  150. #define TR_66_UDMA_MASK 0xfff00000
  151. #define TR_66_UDMA_EN 0x00100000 /* Enable Ultra mode for DMA */
  152. #define TR_66_UDMA_ADDRSETUP_MASK 0xe0000000 /* Address setup */
  153. #define TR_66_UDMA_ADDRSETUP_SHIFT 29
  154. #define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000 /* Ready 2 pause time */
  155. #define TR_66_UDMA_RDY2PAUS_SHIFT 25
  156. #define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000 /* Write data setup time */
  157. #define TR_66_UDMA_WRDATASETUP_SHIFT 21
  158. #define TR_66_MDMA_MASK 0x000ffc00
  159. #define TR_66_MDMA_RECOVERY_MASK 0x000f8000
  160. #define TR_66_MDMA_RECOVERY_SHIFT 15
  161. #define TR_66_MDMA_ACCESS_MASK 0x00007c00
  162. #define TR_66_MDMA_ACCESS_SHIFT 10
  163. #define TR_66_PIO_MASK 0x000003ff
  164. #define TR_66_PIO_RECOVERY_MASK 0x000003e0
  165. #define TR_66_PIO_RECOVERY_SHIFT 5
  166. #define TR_66_PIO_ACCESS_MASK 0x0000001f
  167. #define TR_66_PIO_ACCESS_SHIFT 0
  168. /* 33Mhz cell, found in OHare, Heathrow (& Paddington) and KeyLargo
  169. * Can do pio & mdma modes, clock unit is 30ns (33Mhz)
  170. *
  171. * The access time and recovery time can be programmed. Some older
  172. * Darwin code base limit OHare to 150ns cycle time. I decided to do
  173. * the same here fore safety against broken old hardware ;)
  174. * The HalfTick bit, when set, adds half a clock (15ns) to the access
  175. * time and removes one from recovery. It's not supported on KeyLargo
  176. * implementation afaik. The E bit appears to be set for PIO mode 0 and
  177. * is used to reach long timings used in this mode.
  178. */
  179. #define TR_33_MDMA_MASK 0x003ff800
  180. #define TR_33_MDMA_RECOVERY_MASK 0x001f0000
  181. #define TR_33_MDMA_RECOVERY_SHIFT 16
  182. #define TR_33_MDMA_ACCESS_MASK 0x0000f800
  183. #define TR_33_MDMA_ACCESS_SHIFT 11
  184. #define TR_33_MDMA_HALFTICK 0x00200000
  185. #define TR_33_PIO_MASK 0x000007ff
  186. #define TR_33_PIO_E 0x00000400
  187. #define TR_33_PIO_RECOVERY_MASK 0x000003e0
  188. #define TR_33_PIO_RECOVERY_SHIFT 5
  189. #define TR_33_PIO_ACCESS_MASK 0x0000001f
  190. #define TR_33_PIO_ACCESS_SHIFT 0
  191. /*
  192. * Interrupt register definitions
  193. */
  194. #define IDE_INTR_DMA 0x80000000
  195. #define IDE_INTR_DEVICE 0x40000000
  196. /*
  197. * FCR Register on Kauai. Not sure what bit 0x4 is ...
  198. */
  199. #define KAUAI_FCR_UATA_MAGIC 0x00000004
  200. #define KAUAI_FCR_UATA_RESET_N 0x00000002
  201. #define KAUAI_FCR_UATA_ENABLE 0x00000001
  202. /* Rounded Multiword DMA timings
  203. *
  204. * I gave up finding a generic formula for all controller
  205. * types and instead, built tables based on timing values
  206. * used by Apple in Darwin's implementation.
  207. */
  208. struct mdma_timings_t {
  209. int accessTime;
  210. int recoveryTime;
  211. int cycleTime;
  212. };
  213. struct mdma_timings_t mdma_timings_33[] =
  214. {
  215. { 240, 240, 480 },
  216. { 180, 180, 360 },
  217. { 135, 135, 270 },
  218. { 120, 120, 240 },
  219. { 105, 105, 210 },
  220. { 90, 90, 180 },
  221. { 75, 75, 150 },
  222. { 75, 45, 120 },
  223. { 0, 0, 0 }
  224. };
  225. struct mdma_timings_t mdma_timings_33k[] =
  226. {
  227. { 240, 240, 480 },
  228. { 180, 180, 360 },
  229. { 150, 150, 300 },
  230. { 120, 120, 240 },
  231. { 90, 120, 210 },
  232. { 90, 90, 180 },
  233. { 90, 60, 150 },
  234. { 90, 30, 120 },
  235. { 0, 0, 0 }
  236. };
  237. struct mdma_timings_t mdma_timings_66[] =
  238. {
  239. { 240, 240, 480 },
  240. { 180, 180, 360 },
  241. { 135, 135, 270 },
  242. { 120, 120, 240 },
  243. { 105, 105, 210 },
  244. { 90, 90, 180 },
  245. { 90, 75, 165 },
  246. { 75, 45, 120 },
  247. { 0, 0, 0 }
  248. };
  249. /* KeyLargo ATA-4 Ultra DMA timings (rounded) */
  250. struct {
  251. int addrSetup; /* ??? */
  252. int rdy2pause;
  253. int wrDataSetup;
  254. } kl66_udma_timings[] =
  255. {
  256. { 0, 180, 120 }, /* Mode 0 */
  257. { 0, 150, 90 }, /* 1 */
  258. { 0, 120, 60 }, /* 2 */
  259. { 0, 90, 45 }, /* 3 */
  260. { 0, 90, 30 } /* 4 */
  261. };
  262. /* UniNorth 2 ATA/100 timings */
  263. struct kauai_timing {
  264. int cycle_time;
  265. u32 timing_reg;
  266. };
  267. static struct kauai_timing kauai_pio_timings[] =
  268. {
  269. { 930 , 0x08000fff },
  270. { 600 , 0x08000a92 },
  271. { 383 , 0x0800060f },
  272. { 360 , 0x08000492 },
  273. { 330 , 0x0800048f },
  274. { 300 , 0x080003cf },
  275. { 270 , 0x080003cc },
  276. { 240 , 0x0800038b },
  277. { 239 , 0x0800030c },
  278. { 180 , 0x05000249 },
  279. { 120 , 0x04000148 },
  280. { 0 , 0 },
  281. };
  282. static struct kauai_timing kauai_mdma_timings[] =
  283. {
  284. { 1260 , 0x00fff000 },
  285. { 480 , 0x00618000 },
  286. { 360 , 0x00492000 },
  287. { 270 , 0x0038e000 },
  288. { 240 , 0x0030c000 },
  289. { 210 , 0x002cb000 },
  290. { 180 , 0x00249000 },
  291. { 150 , 0x00209000 },
  292. { 120 , 0x00148000 },
  293. { 0 , 0 },
  294. };
  295. static struct kauai_timing kauai_udma_timings[] =
  296. {
  297. { 120 , 0x000070c0 },
  298. { 90 , 0x00005d80 },
  299. { 60 , 0x00004a60 },
  300. { 45 , 0x00003a50 },
  301. { 30 , 0x00002a30 },
  302. { 20 , 0x00002921 },
  303. { 0 , 0 },
  304. };
  305. static struct kauai_timing shasta_pio_timings[] =
  306. {
  307. { 930 , 0x08000fff },
  308. { 600 , 0x0A000c97 },
  309. { 383 , 0x07000712 },
  310. { 360 , 0x040003cd },
  311. { 330 , 0x040003cd },
  312. { 300 , 0x040003cd },
  313. { 270 , 0x040003cd },
  314. { 240 , 0x040003cd },
  315. { 239 , 0x040003cd },
  316. { 180 , 0x0400028b },
  317. { 120 , 0x0400010a },
  318. { 0 , 0 },
  319. };
  320. static struct kauai_timing shasta_mdma_timings[] =
  321. {
  322. { 1260 , 0x00fff000 },
  323. { 480 , 0x00820800 },
  324. { 360 , 0x00820800 },
  325. { 270 , 0x00820800 },
  326. { 240 , 0x00820800 },
  327. { 210 , 0x00820800 },
  328. { 180 , 0x00820800 },
  329. { 150 , 0x0028b000 },
  330. { 120 , 0x001ca000 },
  331. { 0 , 0 },
  332. };
  333. static struct kauai_timing shasta_udma133_timings[] =
  334. {
  335. { 120 , 0x00035901, },
  336. { 90 , 0x000348b1, },
  337. { 60 , 0x00033881, },
  338. { 45 , 0x00033861, },
  339. { 30 , 0x00033841, },
  340. { 20 , 0x00033031, },
  341. { 15 , 0x00033021, },
  342. { 0 , 0 },
  343. };
  344. static inline u32
  345. kauai_lookup_timing(struct kauai_timing* table, int cycle_time)
  346. {
  347. int i;
  348. for (i=0; table[i].cycle_time; i++)
  349. if (cycle_time > table[i+1].cycle_time)
  350. return table[i].timing_reg;
  351. BUG();
  352. return 0;
  353. }
  354. /* allow up to 256 DBDMA commands per xfer */
  355. #define MAX_DCMDS 256
  356. /*
  357. * Wait 1s for disk to answer on IDE bus after a hard reset
  358. * of the device (via GPIO/FCR).
  359. *
  360. * Some devices seem to "pollute" the bus even after dropping
  361. * the BSY bit (typically some combo drives slave on the UDMA
  362. * bus) after a hard reset. Since we hard reset all drives on
  363. * KeyLargo ATA66, we have to keep that delay around. I may end
  364. * up not hard resetting anymore on these and keep the delay only
  365. * for older interfaces instead (we have to reset when coming
  366. * from MacOS...) --BenH.
  367. */
  368. #define IDE_WAKEUP_DELAY (1*HZ)
  369. static int pmac_ide_init_dma(ide_hwif_t *, const struct ide_port_info *);
  370. static int pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq);
  371. static void pmac_ide_selectproc(ide_drive_t *drive);
  372. static void pmac_ide_kauai_selectproc(ide_drive_t *drive);
  373. #define PMAC_IDE_REG(x) \
  374. ((void __iomem *)((drive)->hwif->io_ports.data_addr + (x)))
  375. /*
  376. * Apply the timings of the proper unit (master/slave) to the shared
  377. * timing register when selecting that unit. This version is for
  378. * ASICs with a single timing register
  379. */
  380. static void
  381. pmac_ide_selectproc(ide_drive_t *drive)
  382. {
  383. ide_hwif_t *hwif = drive->hwif;
  384. pmac_ide_hwif_t *pmif =
  385. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  386. if (drive->dn & 1)
  387. writel(pmif->timings[1], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  388. else
  389. writel(pmif->timings[0], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  390. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  391. }
  392. /*
  393. * Apply the timings of the proper unit (master/slave) to the shared
  394. * timing register when selecting that unit. This version is for
  395. * ASICs with a dual timing register (Kauai)
  396. */
  397. static void
  398. pmac_ide_kauai_selectproc(ide_drive_t *drive)
  399. {
  400. ide_hwif_t *hwif = drive->hwif;
  401. pmac_ide_hwif_t *pmif =
  402. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  403. if (drive->dn & 1) {
  404. writel(pmif->timings[1], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  405. writel(pmif->timings[3], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  406. } else {
  407. writel(pmif->timings[0], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  408. writel(pmif->timings[2], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  409. }
  410. (void)readl(PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  411. }
  412. /*
  413. * Force an update of controller timing values for a given drive
  414. */
  415. static void
  416. pmac_ide_do_update_timings(ide_drive_t *drive)
  417. {
  418. ide_hwif_t *hwif = drive->hwif;
  419. pmac_ide_hwif_t *pmif =
  420. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  421. if (pmif->kind == controller_sh_ata6 ||
  422. pmif->kind == controller_un_ata6 ||
  423. pmif->kind == controller_k2_ata6)
  424. pmac_ide_kauai_selectproc(drive);
  425. else
  426. pmac_ide_selectproc(drive);
  427. }
  428. static void pmac_exec_command(ide_hwif_t *hwif, u8 cmd)
  429. {
  430. writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
  431. (void)readl((void __iomem *)(hwif->io_ports.data_addr
  432. + IDE_TIMING_CONFIG));
  433. }
  434. static void pmac_set_irq(ide_hwif_t *hwif, int on)
  435. {
  436. u8 ctl = ATA_DEVCTL_OBS;
  437. if (on == 4) { /* hack for SRST */
  438. ctl |= 4;
  439. on &= ~4;
  440. }
  441. ctl |= on ? 0 : 2;
  442. writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
  443. (void)readl((void __iomem *)(hwif->io_ports.data_addr
  444. + IDE_TIMING_CONFIG));
  445. }
  446. /*
  447. * Old tuning functions (called on hdparm -p), sets up drive PIO timings
  448. */
  449. static void
  450. pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  451. {
  452. ide_hwif_t *hwif = drive->hwif;
  453. pmac_ide_hwif_t *pmif =
  454. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  455. struct ide_timing *tim = ide_timing_find_mode(XFER_PIO_0 + pio);
  456. u32 *timings, t;
  457. unsigned accessTicks, recTicks;
  458. unsigned accessTime, recTime;
  459. unsigned int cycle_time;
  460. /* which drive is it ? */
  461. timings = &pmif->timings[drive->dn & 1];
  462. t = *timings;
  463. cycle_time = ide_pio_cycle_time(drive, pio);
  464. switch (pmif->kind) {
  465. case controller_sh_ata6: {
  466. /* 133Mhz cell */
  467. u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
  468. t = (t & ~TR_133_PIOREG_PIO_MASK) | tr;
  469. break;
  470. }
  471. case controller_un_ata6:
  472. case controller_k2_ata6: {
  473. /* 100Mhz cell */
  474. u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
  475. t = (t & ~TR_100_PIOREG_PIO_MASK) | tr;
  476. break;
  477. }
  478. case controller_kl_ata4:
  479. /* 66Mhz cell */
  480. recTime = cycle_time - tim->active - tim->setup;
  481. recTime = max(recTime, 150U);
  482. accessTime = tim->active;
  483. accessTime = max(accessTime, 150U);
  484. accessTicks = SYSCLK_TICKS_66(accessTime);
  485. accessTicks = min(accessTicks, 0x1fU);
  486. recTicks = SYSCLK_TICKS_66(recTime);
  487. recTicks = min(recTicks, 0x1fU);
  488. t = (t & ~TR_66_PIO_MASK) |
  489. (accessTicks << TR_66_PIO_ACCESS_SHIFT) |
  490. (recTicks << TR_66_PIO_RECOVERY_SHIFT);
  491. break;
  492. default: {
  493. /* 33Mhz cell */
  494. int ebit = 0;
  495. recTime = cycle_time - tim->active - tim->setup;
  496. recTime = max(recTime, 150U);
  497. accessTime = tim->active;
  498. accessTime = max(accessTime, 150U);
  499. accessTicks = SYSCLK_TICKS(accessTime);
  500. accessTicks = min(accessTicks, 0x1fU);
  501. accessTicks = max(accessTicks, 4U);
  502. recTicks = SYSCLK_TICKS(recTime);
  503. recTicks = min(recTicks, 0x1fU);
  504. recTicks = max(recTicks, 5U) - 4;
  505. if (recTicks > 9) {
  506. recTicks--; /* guess, but it's only for PIO0, so... */
  507. ebit = 1;
  508. }
  509. t = (t & ~TR_33_PIO_MASK) |
  510. (accessTicks << TR_33_PIO_ACCESS_SHIFT) |
  511. (recTicks << TR_33_PIO_RECOVERY_SHIFT);
  512. if (ebit)
  513. t |= TR_33_PIO_E;
  514. break;
  515. }
  516. }
  517. #ifdef IDE_PMAC_DEBUG
  518. printk(KERN_ERR "%s: Set PIO timing for mode %d, reg: 0x%08x\n",
  519. drive->name, pio, *timings);
  520. #endif
  521. *timings = t;
  522. pmac_ide_do_update_timings(drive);
  523. }
  524. /*
  525. * Calculate KeyLargo ATA/66 UDMA timings
  526. */
  527. static int
  528. set_timings_udma_ata4(u32 *timings, u8 speed)
  529. {
  530. unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
  531. if (speed > XFER_UDMA_4)
  532. return 1;
  533. rdyToPauseTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].rdy2pause);
  534. wrDataSetupTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].wrDataSetup);
  535. addrTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].addrSetup);
  536. *timings = ((*timings) & ~(TR_66_UDMA_MASK | TR_66_MDMA_MASK)) |
  537. (wrDataSetupTicks << TR_66_UDMA_WRDATASETUP_SHIFT) |
  538. (rdyToPauseTicks << TR_66_UDMA_RDY2PAUS_SHIFT) |
  539. (addrTicks <<TR_66_UDMA_ADDRSETUP_SHIFT) |
  540. TR_66_UDMA_EN;
  541. #ifdef IDE_PMAC_DEBUG
  542. printk(KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
  543. speed & 0xf, *timings);
  544. #endif
  545. return 0;
  546. }
  547. /*
  548. * Calculate Kauai ATA/100 UDMA timings
  549. */
  550. static int
  551. set_timings_udma_ata6(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  552. {
  553. struct ide_timing *t = ide_timing_find_mode(speed);
  554. u32 tr;
  555. if (speed > XFER_UDMA_5 || t == NULL)
  556. return 1;
  557. tr = kauai_lookup_timing(kauai_udma_timings, (int)t->udma);
  558. *ultra_timings = ((*ultra_timings) & ~TR_100_UDMAREG_UDMA_MASK) | tr;
  559. *ultra_timings = (*ultra_timings) | TR_100_UDMAREG_UDMA_EN;
  560. return 0;
  561. }
  562. /*
  563. * Calculate Shasta ATA/133 UDMA timings
  564. */
  565. static int
  566. set_timings_udma_shasta(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  567. {
  568. struct ide_timing *t = ide_timing_find_mode(speed);
  569. u32 tr;
  570. if (speed > XFER_UDMA_6 || t == NULL)
  571. return 1;
  572. tr = kauai_lookup_timing(shasta_udma133_timings, (int)t->udma);
  573. *ultra_timings = ((*ultra_timings) & ~TR_133_UDMAREG_UDMA_MASK) | tr;
  574. *ultra_timings = (*ultra_timings) | TR_133_UDMAREG_UDMA_EN;
  575. return 0;
  576. }
  577. /*
  578. * Calculate MDMA timings for all cells
  579. */
  580. static void
  581. set_timings_mdma(ide_drive_t *drive, int intf_type, u32 *timings, u32 *timings2,
  582. u8 speed)
  583. {
  584. u16 *id = drive->id;
  585. int cycleTime, accessTime = 0, recTime = 0;
  586. unsigned accessTicks, recTicks;
  587. struct mdma_timings_t* tm = NULL;
  588. int i;
  589. /* Get default cycle time for mode */
  590. switch(speed & 0xf) {
  591. case 0: cycleTime = 480; break;
  592. case 1: cycleTime = 150; break;
  593. case 2: cycleTime = 120; break;
  594. default:
  595. BUG();
  596. break;
  597. }
  598. /* Check if drive provides explicit DMA cycle time */
  599. if ((id[ATA_ID_FIELD_VALID] & 2) && id[ATA_ID_EIDE_DMA_TIME])
  600. cycleTime = max_t(int, id[ATA_ID_EIDE_DMA_TIME], cycleTime);
  601. /* OHare limits according to some old Apple sources */
  602. if ((intf_type == controller_ohare) && (cycleTime < 150))
  603. cycleTime = 150;
  604. /* Get the proper timing array for this controller */
  605. switch(intf_type) {
  606. case controller_sh_ata6:
  607. case controller_un_ata6:
  608. case controller_k2_ata6:
  609. break;
  610. case controller_kl_ata4:
  611. tm = mdma_timings_66;
  612. break;
  613. case controller_kl_ata3:
  614. tm = mdma_timings_33k;
  615. break;
  616. default:
  617. tm = mdma_timings_33;
  618. break;
  619. }
  620. if (tm != NULL) {
  621. /* Lookup matching access & recovery times */
  622. i = -1;
  623. for (;;) {
  624. if (tm[i+1].cycleTime < cycleTime)
  625. break;
  626. i++;
  627. }
  628. cycleTime = tm[i].cycleTime;
  629. accessTime = tm[i].accessTime;
  630. recTime = tm[i].recoveryTime;
  631. #ifdef IDE_PMAC_DEBUG
  632. printk(KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
  633. drive->name, cycleTime, accessTime, recTime);
  634. #endif
  635. }
  636. switch(intf_type) {
  637. case controller_sh_ata6: {
  638. /* 133Mhz cell */
  639. u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
  640. *timings = ((*timings) & ~TR_133_PIOREG_MDMA_MASK) | tr;
  641. *timings2 = (*timings2) & ~TR_133_UDMAREG_UDMA_EN;
  642. }
  643. case controller_un_ata6:
  644. case controller_k2_ata6: {
  645. /* 100Mhz cell */
  646. u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
  647. *timings = ((*timings) & ~TR_100_PIOREG_MDMA_MASK) | tr;
  648. *timings2 = (*timings2) & ~TR_100_UDMAREG_UDMA_EN;
  649. }
  650. break;
  651. case controller_kl_ata4:
  652. /* 66Mhz cell */
  653. accessTicks = SYSCLK_TICKS_66(accessTime);
  654. accessTicks = min(accessTicks, 0x1fU);
  655. accessTicks = max(accessTicks, 0x1U);
  656. recTicks = SYSCLK_TICKS_66(recTime);
  657. recTicks = min(recTicks, 0x1fU);
  658. recTicks = max(recTicks, 0x3U);
  659. /* Clear out mdma bits and disable udma */
  660. *timings = ((*timings) & ~(TR_66_MDMA_MASK | TR_66_UDMA_MASK)) |
  661. (accessTicks << TR_66_MDMA_ACCESS_SHIFT) |
  662. (recTicks << TR_66_MDMA_RECOVERY_SHIFT);
  663. break;
  664. case controller_kl_ata3:
  665. /* 33Mhz cell on KeyLargo */
  666. accessTicks = SYSCLK_TICKS(accessTime);
  667. accessTicks = max(accessTicks, 1U);
  668. accessTicks = min(accessTicks, 0x1fU);
  669. accessTime = accessTicks * IDE_SYSCLK_NS;
  670. recTicks = SYSCLK_TICKS(recTime);
  671. recTicks = max(recTicks, 1U);
  672. recTicks = min(recTicks, 0x1fU);
  673. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  674. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  675. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  676. break;
  677. default: {
  678. /* 33Mhz cell on others */
  679. int halfTick = 0;
  680. int origAccessTime = accessTime;
  681. int origRecTime = recTime;
  682. accessTicks = SYSCLK_TICKS(accessTime);
  683. accessTicks = max(accessTicks, 1U);
  684. accessTicks = min(accessTicks, 0x1fU);
  685. accessTime = accessTicks * IDE_SYSCLK_NS;
  686. recTicks = SYSCLK_TICKS(recTime);
  687. recTicks = max(recTicks, 2U) - 1;
  688. recTicks = min(recTicks, 0x1fU);
  689. recTime = (recTicks + 1) * IDE_SYSCLK_NS;
  690. if ((accessTicks > 1) &&
  691. ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
  692. ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
  693. halfTick = 1;
  694. accessTicks--;
  695. }
  696. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  697. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  698. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  699. if (halfTick)
  700. *timings |= TR_33_MDMA_HALFTICK;
  701. }
  702. }
  703. #ifdef IDE_PMAC_DEBUG
  704. printk(KERN_ERR "%s: Set MDMA timing for mode %d, reg: 0x%08x\n",
  705. drive->name, speed & 0xf, *timings);
  706. #endif
  707. }
  708. static void pmac_ide_set_dma_mode(ide_drive_t *drive, const u8 speed)
  709. {
  710. ide_hwif_t *hwif = drive->hwif;
  711. pmac_ide_hwif_t *pmif =
  712. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  713. int ret = 0;
  714. u32 *timings, *timings2, tl[2];
  715. u8 unit = drive->dn & 1;
  716. timings = &pmif->timings[unit];
  717. timings2 = &pmif->timings[unit+2];
  718. /* Copy timings to local image */
  719. tl[0] = *timings;
  720. tl[1] = *timings2;
  721. if (speed >= XFER_UDMA_0) {
  722. if (pmif->kind == controller_kl_ata4)
  723. ret = set_timings_udma_ata4(&tl[0], speed);
  724. else if (pmif->kind == controller_un_ata6
  725. || pmif->kind == controller_k2_ata6)
  726. ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
  727. else if (pmif->kind == controller_sh_ata6)
  728. ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
  729. else
  730. ret = -1;
  731. } else
  732. set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
  733. if (ret)
  734. return;
  735. /* Apply timings to controller */
  736. *timings = tl[0];
  737. *timings2 = tl[1];
  738. pmac_ide_do_update_timings(drive);
  739. }
  740. /*
  741. * Blast some well known "safe" values to the timing registers at init or
  742. * wakeup from sleep time, before we do real calculation
  743. */
  744. static void
  745. sanitize_timings(pmac_ide_hwif_t *pmif)
  746. {
  747. unsigned int value, value2 = 0;
  748. switch(pmif->kind) {
  749. case controller_sh_ata6:
  750. value = 0x0a820c97;
  751. value2 = 0x00033031;
  752. break;
  753. case controller_un_ata6:
  754. case controller_k2_ata6:
  755. value = 0x08618a92;
  756. value2 = 0x00002921;
  757. break;
  758. case controller_kl_ata4:
  759. value = 0x0008438c;
  760. break;
  761. case controller_kl_ata3:
  762. value = 0x00084526;
  763. break;
  764. case controller_heathrow:
  765. case controller_ohare:
  766. default:
  767. value = 0x00074526;
  768. break;
  769. }
  770. pmif->timings[0] = pmif->timings[1] = value;
  771. pmif->timings[2] = pmif->timings[3] = value2;
  772. }
  773. /* Suspend call back, should be called after the child devices
  774. * have actually been suspended
  775. */
  776. static int pmac_ide_do_suspend(pmac_ide_hwif_t *pmif)
  777. {
  778. /* We clear the timings */
  779. pmif->timings[0] = 0;
  780. pmif->timings[1] = 0;
  781. disable_irq(pmif->irq);
  782. /* The media bay will handle itself just fine */
  783. if (pmif->mediabay)
  784. return 0;
  785. /* Kauai has bus control FCRs directly here */
  786. if (pmif->kauai_fcr) {
  787. u32 fcr = readl(pmif->kauai_fcr);
  788. fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
  789. writel(fcr, pmif->kauai_fcr);
  790. }
  791. /* Disable the bus on older machines and the cell on kauai */
  792. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id,
  793. 0);
  794. return 0;
  795. }
  796. /* Resume call back, should be called before the child devices
  797. * are resumed
  798. */
  799. static int pmac_ide_do_resume(pmac_ide_hwif_t *pmif)
  800. {
  801. /* Hard reset & re-enable controller (do we really need to reset ? -BenH) */
  802. if (!pmif->mediabay) {
  803. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 1);
  804. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id, 1);
  805. msleep(10);
  806. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 0);
  807. /* Kauai has it different */
  808. if (pmif->kauai_fcr) {
  809. u32 fcr = readl(pmif->kauai_fcr);
  810. fcr |= KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE;
  811. writel(fcr, pmif->kauai_fcr);
  812. }
  813. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  814. }
  815. /* Sanitize drive timings */
  816. sanitize_timings(pmif);
  817. enable_irq(pmif->irq);
  818. return 0;
  819. }
  820. static u8 pmac_ide_cable_detect(ide_hwif_t *hwif)
  821. {
  822. pmac_ide_hwif_t *pmif =
  823. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  824. struct device_node *np = pmif->node;
  825. const char *cable = of_get_property(np, "cable-type", NULL);
  826. /* Get cable type from device-tree. */
  827. if (cable && !strncmp(cable, "80-", 3))
  828. return ATA_CBL_PATA80;
  829. /*
  830. * G5's seem to have incorrect cable type in device-tree.
  831. * Let's assume they have a 80 conductor cable, this seem
  832. * to be always the case unless the user mucked around.
  833. */
  834. if (of_device_is_compatible(np, "K2-UATA") ||
  835. of_device_is_compatible(np, "shasta-ata"))
  836. return ATA_CBL_PATA80;
  837. return ATA_CBL_PATA40;
  838. }
  839. static void pmac_ide_init_dev(ide_drive_t *drive)
  840. {
  841. ide_hwif_t *hwif = drive->hwif;
  842. pmac_ide_hwif_t *pmif =
  843. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  844. if (pmif->mediabay) {
  845. #ifdef CONFIG_PMAC_MEDIABAY
  846. if (check_media_bay_by_base(pmif->regbase, MB_CD) == 0) {
  847. drive->dev_flags &= ~IDE_DFLAG_NOPROBE;
  848. return;
  849. }
  850. #endif
  851. drive->dev_flags |= IDE_DFLAG_NOPROBE;
  852. }
  853. }
  854. static const struct ide_tp_ops pmac_tp_ops = {
  855. .exec_command = pmac_exec_command,
  856. .read_status = ide_read_status,
  857. .read_altstatus = ide_read_altstatus,
  858. .set_irq = pmac_set_irq,
  859. .tf_load = ide_tf_load,
  860. .tf_read = ide_tf_read,
  861. .input_data = ide_input_data,
  862. .output_data = ide_output_data,
  863. };
  864. static const struct ide_port_ops pmac_ide_ata6_port_ops = {
  865. .init_dev = pmac_ide_init_dev,
  866. .set_pio_mode = pmac_ide_set_pio_mode,
  867. .set_dma_mode = pmac_ide_set_dma_mode,
  868. .selectproc = pmac_ide_kauai_selectproc,
  869. .cable_detect = pmac_ide_cable_detect,
  870. };
  871. static const struct ide_port_ops pmac_ide_ata4_port_ops = {
  872. .init_dev = pmac_ide_init_dev,
  873. .set_pio_mode = pmac_ide_set_pio_mode,
  874. .set_dma_mode = pmac_ide_set_dma_mode,
  875. .selectproc = pmac_ide_selectproc,
  876. .cable_detect = pmac_ide_cable_detect,
  877. };
  878. static const struct ide_port_ops pmac_ide_port_ops = {
  879. .init_dev = pmac_ide_init_dev,
  880. .set_pio_mode = pmac_ide_set_pio_mode,
  881. .set_dma_mode = pmac_ide_set_dma_mode,
  882. .selectproc = pmac_ide_selectproc,
  883. };
  884. static const struct ide_dma_ops pmac_dma_ops;
  885. static const struct ide_port_info pmac_port_info = {
  886. .name = DRV_NAME,
  887. .init_dma = pmac_ide_init_dma,
  888. .chipset = ide_pmac,
  889. .tp_ops = &pmac_tp_ops,
  890. .port_ops = &pmac_ide_port_ops,
  891. .dma_ops = &pmac_dma_ops,
  892. .host_flags = IDE_HFLAG_SET_PIO_MODE_KEEP_DMA |
  893. IDE_HFLAG_POST_SET_MODE |
  894. IDE_HFLAG_MMIO |
  895. IDE_HFLAG_UNMASK_IRQS,
  896. .pio_mask = ATA_PIO4,
  897. .mwdma_mask = ATA_MWDMA2,
  898. };
  899. /*
  900. * Setup, register & probe an IDE channel driven by this driver, this is
  901. * called by one of the 2 probe functions (macio or PCI).
  902. */
  903. static int __devinit pmac_ide_setup_device(pmac_ide_hwif_t *pmif, hw_regs_t *hw)
  904. {
  905. struct device_node *np = pmif->node;
  906. const int *bidp;
  907. struct ide_host *host;
  908. ide_hwif_t *hwif;
  909. hw_regs_t *hws[] = { hw, NULL, NULL, NULL };
  910. struct ide_port_info d = pmac_port_info;
  911. int rc;
  912. pmif->broken_dma = pmif->broken_dma_warn = 0;
  913. if (of_device_is_compatible(np, "shasta-ata")) {
  914. pmif->kind = controller_sh_ata6;
  915. d.port_ops = &pmac_ide_ata6_port_ops;
  916. d.udma_mask = ATA_UDMA6;
  917. } else if (of_device_is_compatible(np, "kauai-ata")) {
  918. pmif->kind = controller_un_ata6;
  919. d.port_ops = &pmac_ide_ata6_port_ops;
  920. d.udma_mask = ATA_UDMA5;
  921. } else if (of_device_is_compatible(np, "K2-UATA")) {
  922. pmif->kind = controller_k2_ata6;
  923. d.port_ops = &pmac_ide_ata6_port_ops;
  924. d.udma_mask = ATA_UDMA5;
  925. } else if (of_device_is_compatible(np, "keylargo-ata")) {
  926. if (strcmp(np->name, "ata-4") == 0) {
  927. pmif->kind = controller_kl_ata4;
  928. d.port_ops = &pmac_ide_ata4_port_ops;
  929. d.udma_mask = ATA_UDMA4;
  930. } else
  931. pmif->kind = controller_kl_ata3;
  932. } else if (of_device_is_compatible(np, "heathrow-ata")) {
  933. pmif->kind = controller_heathrow;
  934. } else {
  935. pmif->kind = controller_ohare;
  936. pmif->broken_dma = 1;
  937. }
  938. bidp = of_get_property(np, "AAPL,bus-id", NULL);
  939. pmif->aapl_bus_id = bidp ? *bidp : 0;
  940. /* On Kauai-type controllers, we make sure the FCR is correct */
  941. if (pmif->kauai_fcr)
  942. writel(KAUAI_FCR_UATA_MAGIC |
  943. KAUAI_FCR_UATA_RESET_N |
  944. KAUAI_FCR_UATA_ENABLE, pmif->kauai_fcr);
  945. pmif->mediabay = 0;
  946. /* Make sure we have sane timings */
  947. sanitize_timings(pmif);
  948. host = ide_host_alloc(&d, hws);
  949. if (host == NULL)
  950. return -ENOMEM;
  951. hwif = host->ports[0];
  952. #ifndef CONFIG_PPC64
  953. /* XXX FIXME: Media bay stuff need re-organizing */
  954. if (np->parent && np->parent->name
  955. && strcasecmp(np->parent->name, "media-bay") == 0) {
  956. #ifdef CONFIG_PMAC_MEDIABAY
  957. media_bay_set_ide_infos(np->parent, pmif->regbase, pmif->irq,
  958. hwif);
  959. #endif /* CONFIG_PMAC_MEDIABAY */
  960. pmif->mediabay = 1;
  961. if (!bidp)
  962. pmif->aapl_bus_id = 1;
  963. } else if (pmif->kind == controller_ohare) {
  964. /* The code below is having trouble on some ohare machines
  965. * (timing related ?). Until I can put my hand on one of these
  966. * units, I keep the old way
  967. */
  968. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
  969. } else
  970. #endif
  971. {
  972. /* This is necessary to enable IDE when net-booting */
  973. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 1);
  974. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, pmif->aapl_bus_id, 1);
  975. msleep(10);
  976. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 0);
  977. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  978. }
  979. printk(KERN_INFO DRV_NAME ": Found Apple %s controller (%s), "
  980. "bus ID %d%s, irq %d\n", model_name[pmif->kind],
  981. pmif->mdev ? "macio" : "PCI", pmif->aapl_bus_id,
  982. pmif->mediabay ? " (mediabay)" : "", hw->irq);
  983. rc = ide_host_register(host, &d, hws);
  984. if (rc) {
  985. ide_host_free(host);
  986. return rc;
  987. }
  988. return 0;
  989. }
  990. static void __devinit pmac_ide_init_ports(hw_regs_t *hw, unsigned long base)
  991. {
  992. int i;
  993. for (i = 0; i < 8; ++i)
  994. hw->io_ports_array[i] = base + i * 0x10;
  995. hw->io_ports.ctl_addr = base + 0x160;
  996. }
  997. /*
  998. * Attach to a macio probed interface
  999. */
  1000. static int __devinit
  1001. pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
  1002. {
  1003. void __iomem *base;
  1004. unsigned long regbase;
  1005. pmac_ide_hwif_t *pmif;
  1006. int irq, rc;
  1007. hw_regs_t hw;
  1008. pmif = kzalloc(sizeof(*pmif), GFP_KERNEL);
  1009. if (pmif == NULL)
  1010. return -ENOMEM;
  1011. if (macio_resource_count(mdev) == 0) {
  1012. printk(KERN_WARNING "ide-pmac: no address for %s\n",
  1013. mdev->ofdev.node->full_name);
  1014. rc = -ENXIO;
  1015. goto out_free_pmif;
  1016. }
  1017. /* Request memory resource for IO ports */
  1018. if (macio_request_resource(mdev, 0, "ide-pmac (ports)")) {
  1019. printk(KERN_ERR "ide-pmac: can't request MMIO resource for "
  1020. "%s!\n", mdev->ofdev.node->full_name);
  1021. rc = -EBUSY;
  1022. goto out_free_pmif;
  1023. }
  1024. /* XXX This is bogus. Should be fixed in the registry by checking
  1025. * the kind of host interrupt controller, a bit like gatwick
  1026. * fixes in irq.c. That works well enough for the single case
  1027. * where that happens though...
  1028. */
  1029. if (macio_irq_count(mdev) == 0) {
  1030. printk(KERN_WARNING "ide-pmac: no intrs for device %s, using "
  1031. "13\n", mdev->ofdev.node->full_name);
  1032. irq = irq_create_mapping(NULL, 13);
  1033. } else
  1034. irq = macio_irq(mdev, 0);
  1035. base = ioremap(macio_resource_start(mdev, 0), 0x400);
  1036. regbase = (unsigned long) base;
  1037. pmif->mdev = mdev;
  1038. pmif->node = mdev->ofdev.node;
  1039. pmif->regbase = regbase;
  1040. pmif->irq = irq;
  1041. pmif->kauai_fcr = NULL;
  1042. if (macio_resource_count(mdev) >= 2) {
  1043. if (macio_request_resource(mdev, 1, "ide-pmac (dma)"))
  1044. printk(KERN_WARNING "ide-pmac: can't request DMA "
  1045. "resource for %s!\n",
  1046. mdev->ofdev.node->full_name);
  1047. else
  1048. pmif->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1049. } else
  1050. pmif->dma_regs = NULL;
  1051. dev_set_drvdata(&mdev->ofdev.dev, pmif);
  1052. memset(&hw, 0, sizeof(hw));
  1053. pmac_ide_init_ports(&hw, pmif->regbase);
  1054. hw.irq = irq;
  1055. hw.dev = &mdev->bus->pdev->dev;
  1056. hw.parent = &mdev->ofdev.dev;
  1057. rc = pmac_ide_setup_device(pmif, &hw);
  1058. if (rc != 0) {
  1059. /* The inteface is released to the common IDE layer */
  1060. dev_set_drvdata(&mdev->ofdev.dev, NULL);
  1061. iounmap(base);
  1062. if (pmif->dma_regs) {
  1063. iounmap(pmif->dma_regs);
  1064. macio_release_resource(mdev, 1);
  1065. }
  1066. macio_release_resource(mdev, 0);
  1067. kfree(pmif);
  1068. }
  1069. return rc;
  1070. out_free_pmif:
  1071. kfree(pmif);
  1072. return rc;
  1073. }
  1074. static int
  1075. pmac_ide_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1076. {
  1077. pmac_ide_hwif_t *pmif =
  1078. (pmac_ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1079. int rc = 0;
  1080. if (mesg.event != mdev->ofdev.dev.power.power_state.event
  1081. && (mesg.event & PM_EVENT_SLEEP)) {
  1082. rc = pmac_ide_do_suspend(pmif);
  1083. if (rc == 0)
  1084. mdev->ofdev.dev.power.power_state = mesg;
  1085. }
  1086. return rc;
  1087. }
  1088. static int
  1089. pmac_ide_macio_resume(struct macio_dev *mdev)
  1090. {
  1091. pmac_ide_hwif_t *pmif =
  1092. (pmac_ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1093. int rc = 0;
  1094. if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
  1095. rc = pmac_ide_do_resume(pmif);
  1096. if (rc == 0)
  1097. mdev->ofdev.dev.power.power_state = PMSG_ON;
  1098. }
  1099. return rc;
  1100. }
  1101. /*
  1102. * Attach to a PCI probed interface
  1103. */
  1104. static int __devinit
  1105. pmac_ide_pci_attach(struct pci_dev *pdev, const struct pci_device_id *id)
  1106. {
  1107. struct device_node *np;
  1108. pmac_ide_hwif_t *pmif;
  1109. void __iomem *base;
  1110. unsigned long rbase, rlen;
  1111. int rc;
  1112. hw_regs_t hw;
  1113. np = pci_device_to_OF_node(pdev);
  1114. if (np == NULL) {
  1115. printk(KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
  1116. return -ENODEV;
  1117. }
  1118. pmif = kzalloc(sizeof(*pmif), GFP_KERNEL);
  1119. if (pmif == NULL)
  1120. return -ENOMEM;
  1121. if (pci_enable_device(pdev)) {
  1122. printk(KERN_WARNING "ide-pmac: Can't enable PCI device for "
  1123. "%s\n", np->full_name);
  1124. rc = -ENXIO;
  1125. goto out_free_pmif;
  1126. }
  1127. pci_set_master(pdev);
  1128. if (pci_request_regions(pdev, "Kauai ATA")) {
  1129. printk(KERN_ERR "ide-pmac: Cannot obtain PCI resources for "
  1130. "%s\n", np->full_name);
  1131. rc = -ENXIO;
  1132. goto out_free_pmif;
  1133. }
  1134. pmif->mdev = NULL;
  1135. pmif->node = np;
  1136. rbase = pci_resource_start(pdev, 0);
  1137. rlen = pci_resource_len(pdev, 0);
  1138. base = ioremap(rbase, rlen);
  1139. pmif->regbase = (unsigned long) base + 0x2000;
  1140. pmif->dma_regs = base + 0x1000;
  1141. pmif->kauai_fcr = base;
  1142. pmif->irq = pdev->irq;
  1143. pci_set_drvdata(pdev, pmif);
  1144. memset(&hw, 0, sizeof(hw));
  1145. pmac_ide_init_ports(&hw, pmif->regbase);
  1146. hw.irq = pdev->irq;
  1147. hw.dev = &pdev->dev;
  1148. rc = pmac_ide_setup_device(pmif, &hw);
  1149. if (rc != 0) {
  1150. /* The inteface is released to the common IDE layer */
  1151. pci_set_drvdata(pdev, NULL);
  1152. iounmap(base);
  1153. pci_release_regions(pdev);
  1154. kfree(pmif);
  1155. }
  1156. return rc;
  1157. out_free_pmif:
  1158. kfree(pmif);
  1159. return rc;
  1160. }
  1161. static int
  1162. pmac_ide_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1163. {
  1164. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)pci_get_drvdata(pdev);
  1165. int rc = 0;
  1166. if (mesg.event != pdev->dev.power.power_state.event
  1167. && (mesg.event & PM_EVENT_SLEEP)) {
  1168. rc = pmac_ide_do_suspend(pmif);
  1169. if (rc == 0)
  1170. pdev->dev.power.power_state = mesg;
  1171. }
  1172. return rc;
  1173. }
  1174. static int
  1175. pmac_ide_pci_resume(struct pci_dev *pdev)
  1176. {
  1177. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)pci_get_drvdata(pdev);
  1178. int rc = 0;
  1179. if (pdev->dev.power.power_state.event != PM_EVENT_ON) {
  1180. rc = pmac_ide_do_resume(pmif);
  1181. if (rc == 0)
  1182. pdev->dev.power.power_state = PMSG_ON;
  1183. }
  1184. return rc;
  1185. }
  1186. static struct of_device_id pmac_ide_macio_match[] =
  1187. {
  1188. {
  1189. .name = "IDE",
  1190. },
  1191. {
  1192. .name = "ATA",
  1193. },
  1194. {
  1195. .type = "ide",
  1196. },
  1197. {
  1198. .type = "ata",
  1199. },
  1200. {},
  1201. };
  1202. static struct macio_driver pmac_ide_macio_driver =
  1203. {
  1204. .name = "ide-pmac",
  1205. .match_table = pmac_ide_macio_match,
  1206. .probe = pmac_ide_macio_attach,
  1207. .suspend = pmac_ide_macio_suspend,
  1208. .resume = pmac_ide_macio_resume,
  1209. };
  1210. static const struct pci_device_id pmac_ide_pci_match[] = {
  1211. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
  1212. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
  1213. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
  1214. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
  1215. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
  1216. {},
  1217. };
  1218. static struct pci_driver pmac_ide_pci_driver = {
  1219. .name = "ide-pmac",
  1220. .id_table = pmac_ide_pci_match,
  1221. .probe = pmac_ide_pci_attach,
  1222. .suspend = pmac_ide_pci_suspend,
  1223. .resume = pmac_ide_pci_resume,
  1224. };
  1225. MODULE_DEVICE_TABLE(pci, pmac_ide_pci_match);
  1226. int __init pmac_ide_probe(void)
  1227. {
  1228. int error;
  1229. if (!machine_is(powermac))
  1230. return -ENODEV;
  1231. #ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
  1232. error = pci_register_driver(&pmac_ide_pci_driver);
  1233. if (error)
  1234. goto out;
  1235. error = macio_register_driver(&pmac_ide_macio_driver);
  1236. if (error) {
  1237. pci_unregister_driver(&pmac_ide_pci_driver);
  1238. goto out;
  1239. }
  1240. #else
  1241. error = macio_register_driver(&pmac_ide_macio_driver);
  1242. if (error)
  1243. goto out;
  1244. error = pci_register_driver(&pmac_ide_pci_driver);
  1245. if (error) {
  1246. macio_unregister_driver(&pmac_ide_macio_driver);
  1247. goto out;
  1248. }
  1249. #endif
  1250. out:
  1251. return error;
  1252. }
  1253. /*
  1254. * pmac_ide_build_dmatable builds the DBDMA command list
  1255. * for a transfer and sets the DBDMA channel to point to it.
  1256. */
  1257. static int
  1258. pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq)
  1259. {
  1260. ide_hwif_t *hwif = drive->hwif;
  1261. pmac_ide_hwif_t *pmif =
  1262. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1263. struct dbdma_cmd *table;
  1264. int i, count = 0;
  1265. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1266. struct scatterlist *sg;
  1267. int wr = (rq_data_dir(rq) == WRITE);
  1268. /* DMA table is already aligned */
  1269. table = (struct dbdma_cmd *) pmif->dma_table_cpu;
  1270. /* Make sure DMA controller is stopped (necessary ?) */
  1271. writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma->control);
  1272. while (readl(&dma->status) & RUN)
  1273. udelay(1);
  1274. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  1275. if (!i)
  1276. return 0;
  1277. /* Build DBDMA commands list */
  1278. sg = hwif->sg_table;
  1279. while (i && sg_dma_len(sg)) {
  1280. u32 cur_addr;
  1281. u32 cur_len;
  1282. cur_addr = sg_dma_address(sg);
  1283. cur_len = sg_dma_len(sg);
  1284. if (pmif->broken_dma && cur_addr & (L1_CACHE_BYTES - 1)) {
  1285. if (pmif->broken_dma_warn == 0) {
  1286. printk(KERN_WARNING "%s: DMA on non aligned address, "
  1287. "switching to PIO on Ohare chipset\n", drive->name);
  1288. pmif->broken_dma_warn = 1;
  1289. }
  1290. goto use_pio_instead;
  1291. }
  1292. while (cur_len) {
  1293. unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
  1294. if (count++ >= MAX_DCMDS) {
  1295. printk(KERN_WARNING "%s: DMA table too small\n",
  1296. drive->name);
  1297. goto use_pio_instead;
  1298. }
  1299. st_le16(&table->command, wr? OUTPUT_MORE: INPUT_MORE);
  1300. st_le16(&table->req_count, tc);
  1301. st_le32(&table->phy_addr, cur_addr);
  1302. table->cmd_dep = 0;
  1303. table->xfer_status = 0;
  1304. table->res_count = 0;
  1305. cur_addr += tc;
  1306. cur_len -= tc;
  1307. ++table;
  1308. }
  1309. sg = sg_next(sg);
  1310. i--;
  1311. }
  1312. /* convert the last command to an input/output last command */
  1313. if (count) {
  1314. st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
  1315. /* add the stop command to the end of the list */
  1316. memset(table, 0, sizeof(struct dbdma_cmd));
  1317. st_le16(&table->command, DBDMA_STOP);
  1318. mb();
  1319. writel(hwif->dmatable_dma, &dma->cmdptr);
  1320. return 1;
  1321. }
  1322. printk(KERN_DEBUG "%s: empty DMA table?\n", drive->name);
  1323. use_pio_instead:
  1324. ide_destroy_dmatable(drive);
  1325. return 0; /* revert to PIO for this request */
  1326. }
  1327. /*
  1328. * Prepare a DMA transfer. We build the DMA table, adjust the timings for
  1329. * a read on KeyLargo ATA/66 and mark us as waiting for DMA completion
  1330. */
  1331. static int
  1332. pmac_ide_dma_setup(ide_drive_t *drive)
  1333. {
  1334. ide_hwif_t *hwif = drive->hwif;
  1335. pmac_ide_hwif_t *pmif =
  1336. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1337. struct request *rq = hwif->rq;
  1338. u8 unit = drive->dn & 1, ata4 = (pmif->kind == controller_kl_ata4);
  1339. if (!pmac_ide_build_dmatable(drive, rq)) {
  1340. ide_map_sg(drive, rq);
  1341. return 1;
  1342. }
  1343. /* Apple adds 60ns to wrDataSetup on reads */
  1344. if (ata4 && (pmif->timings[unit] & TR_66_UDMA_EN)) {
  1345. writel(pmif->timings[unit] + (!rq_data_dir(rq) ? 0x00800000UL : 0),
  1346. PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1347. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1348. }
  1349. drive->waiting_for_dma = 1;
  1350. return 0;
  1351. }
  1352. static void
  1353. pmac_ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  1354. {
  1355. /* issue cmd to drive */
  1356. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, NULL);
  1357. }
  1358. /*
  1359. * Kick the DMA controller into life after the DMA command has been issued
  1360. * to the drive.
  1361. */
  1362. static void
  1363. pmac_ide_dma_start(ide_drive_t *drive)
  1364. {
  1365. ide_hwif_t *hwif = drive->hwif;
  1366. pmac_ide_hwif_t *pmif =
  1367. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1368. volatile struct dbdma_regs __iomem *dma;
  1369. dma = pmif->dma_regs;
  1370. writel((RUN << 16) | RUN, &dma->control);
  1371. /* Make sure it gets to the controller right now */
  1372. (void)readl(&dma->control);
  1373. }
  1374. /*
  1375. * After a DMA transfer, make sure the controller is stopped
  1376. */
  1377. static int
  1378. pmac_ide_dma_end (ide_drive_t *drive)
  1379. {
  1380. ide_hwif_t *hwif = drive->hwif;
  1381. pmac_ide_hwif_t *pmif =
  1382. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1383. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1384. u32 dstat;
  1385. drive->waiting_for_dma = 0;
  1386. dstat = readl(&dma->status);
  1387. writel(((RUN|WAKE|DEAD) << 16), &dma->control);
  1388. ide_destroy_dmatable(drive);
  1389. /* verify good dma status. we don't check for ACTIVE beeing 0. We should...
  1390. * in theory, but with ATAPI decices doing buffer underruns, that would
  1391. * cause us to disable DMA, which isn't what we want
  1392. */
  1393. return (dstat & (RUN|DEAD)) != RUN;
  1394. }
  1395. /*
  1396. * Check out that the interrupt we got was for us. We can't always know this
  1397. * for sure with those Apple interfaces (well, we could on the recent ones but
  1398. * that's not implemented yet), on the other hand, we don't have shared interrupts
  1399. * so it's not really a problem
  1400. */
  1401. static int
  1402. pmac_ide_dma_test_irq (ide_drive_t *drive)
  1403. {
  1404. ide_hwif_t *hwif = drive->hwif;
  1405. pmac_ide_hwif_t *pmif =
  1406. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1407. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1408. unsigned long status, timeout;
  1409. /* We have to things to deal with here:
  1410. *
  1411. * - The dbdma won't stop if the command was started
  1412. * but completed with an error without transferring all
  1413. * datas. This happens when bad blocks are met during
  1414. * a multi-block transfer.
  1415. *
  1416. * - The dbdma fifo hasn't yet finished flushing to
  1417. * to system memory when the disk interrupt occurs.
  1418. *
  1419. */
  1420. /* If ACTIVE is cleared, the STOP command have passed and
  1421. * transfer is complete.
  1422. */
  1423. status = readl(&dma->status);
  1424. if (!(status & ACTIVE))
  1425. return 1;
  1426. /* If dbdma didn't execute the STOP command yet, the
  1427. * active bit is still set. We consider that we aren't
  1428. * sharing interrupts (which is hopefully the case with
  1429. * those controllers) and so we just try to flush the
  1430. * channel for pending data in the fifo
  1431. */
  1432. udelay(1);
  1433. writel((FLUSH << 16) | FLUSH, &dma->control);
  1434. timeout = 0;
  1435. for (;;) {
  1436. udelay(1);
  1437. status = readl(&dma->status);
  1438. if ((status & FLUSH) == 0)
  1439. break;
  1440. if (++timeout > 100) {
  1441. printk(KERN_WARNING "ide%d, ide_dma_test_irq \
  1442. timeout flushing channel\n", hwif->index);
  1443. break;
  1444. }
  1445. }
  1446. return 1;
  1447. }
  1448. static void pmac_ide_dma_host_set(ide_drive_t *drive, int on)
  1449. {
  1450. }
  1451. static void
  1452. pmac_ide_dma_lost_irq (ide_drive_t *drive)
  1453. {
  1454. ide_hwif_t *hwif = drive->hwif;
  1455. pmac_ide_hwif_t *pmif =
  1456. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1457. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1458. unsigned long status = readl(&dma->status);
  1459. printk(KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
  1460. }
  1461. static const struct ide_dma_ops pmac_dma_ops = {
  1462. .dma_host_set = pmac_ide_dma_host_set,
  1463. .dma_setup = pmac_ide_dma_setup,
  1464. .dma_exec_cmd = pmac_ide_dma_exec_cmd,
  1465. .dma_start = pmac_ide_dma_start,
  1466. .dma_end = pmac_ide_dma_end,
  1467. .dma_test_irq = pmac_ide_dma_test_irq,
  1468. .dma_timeout = ide_dma_timeout,
  1469. .dma_lost_irq = pmac_ide_dma_lost_irq,
  1470. };
  1471. /*
  1472. * Allocate the data structures needed for using DMA with an interface
  1473. * and fill the proper list of functions pointers
  1474. */
  1475. static int __devinit pmac_ide_init_dma(ide_hwif_t *hwif,
  1476. const struct ide_port_info *d)
  1477. {
  1478. pmac_ide_hwif_t *pmif =
  1479. (pmac_ide_hwif_t *)dev_get_drvdata(hwif->gendev.parent);
  1480. struct pci_dev *dev = to_pci_dev(hwif->dev);
  1481. /* We won't need pci_dev if we switch to generic consistent
  1482. * DMA routines ...
  1483. */
  1484. if (dev == NULL || pmif->dma_regs == 0)
  1485. return -ENODEV;
  1486. /*
  1487. * Allocate space for the DBDMA commands.
  1488. * The +2 is +1 for the stop command and +1 to allow for
  1489. * aligning the start address to a multiple of 16 bytes.
  1490. */
  1491. pmif->dma_table_cpu = (struct dbdma_cmd*)pci_alloc_consistent(
  1492. dev,
  1493. (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
  1494. &hwif->dmatable_dma);
  1495. if (pmif->dma_table_cpu == NULL) {
  1496. printk(KERN_ERR "%s: unable to allocate DMA command list\n",
  1497. hwif->name);
  1498. return -ENOMEM;
  1499. }
  1500. hwif->sg_max_nents = MAX_DCMDS;
  1501. return 0;
  1502. }
  1503. module_init(pmac_ide_probe);
  1504. MODULE_LICENSE("GPL");