pmac.c 51 KB

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