pmac.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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. return 0;
  359. }
  360. /* allow up to 256 DBDMA commands per xfer */
  361. #define MAX_DCMDS 256
  362. /*
  363. * Wait 1s for disk to answer on IDE bus after a hard reset
  364. * of the device (via GPIO/FCR).
  365. *
  366. * Some devices seem to "pollute" the bus even after dropping
  367. * the BSY bit (typically some combo drives slave on the UDMA
  368. * bus) after a hard reset. Since we hard reset all drives on
  369. * KeyLargo ATA66, we have to keep that delay around. I may end
  370. * up not hard resetting anymore on these and keep the delay only
  371. * for older interfaces instead (we have to reset when coming
  372. * from MacOS...) --BenH.
  373. */
  374. #define IDE_WAKEUP_DELAY (1*HZ)
  375. static void pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif);
  376. static int pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq);
  377. static void pmac_ide_selectproc(ide_drive_t *drive);
  378. static void pmac_ide_kauai_selectproc(ide_drive_t *drive);
  379. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  380. /*
  381. * N.B. this can't be an initfunc, because the media-bay task can
  382. * call ide_[un]register at any time.
  383. */
  384. void
  385. pmac_ide_init_hwif_ports(hw_regs_t *hw,
  386. unsigned long data_port, unsigned long ctrl_port,
  387. int *irq)
  388. {
  389. int i, ix;
  390. if (data_port == 0)
  391. return;
  392. for (ix = 0; ix < MAX_HWIFS; ++ix)
  393. if (data_port == pmac_ide[ix].regbase)
  394. break;
  395. if (ix >= MAX_HWIFS) {
  396. /* Probably a PCI interface... */
  397. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; ++i)
  398. hw->io_ports[i] = data_port + i - IDE_DATA_OFFSET;
  399. hw->io_ports[IDE_CONTROL_OFFSET] = ctrl_port;
  400. return;
  401. }
  402. for (i = 0; i < 8; ++i)
  403. hw->io_ports[i] = data_port + i * 0x10;
  404. hw->io_ports[8] = data_port + 0x160;
  405. if (irq != NULL)
  406. *irq = pmac_ide[ix].irq;
  407. hw->dev = &pmac_ide[ix].mdev->ofdev.dev;
  408. }
  409. #define PMAC_IDE_REG(x) ((void __iomem *)(IDE_DATA_REG+(x)))
  410. /*
  411. * Apply the timings of the proper unit (master/slave) to the shared
  412. * timing register when selecting that unit. This version is for
  413. * ASICs with a single timing register
  414. */
  415. static void
  416. pmac_ide_selectproc(ide_drive_t *drive)
  417. {
  418. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  419. if (pmif == NULL)
  420. return;
  421. if (drive->select.b.unit & 0x01)
  422. writel(pmif->timings[1], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  423. else
  424. writel(pmif->timings[0], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  425. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  426. }
  427. /*
  428. * Apply the timings of the proper unit (master/slave) to the shared
  429. * timing register when selecting that unit. This version is for
  430. * ASICs with a dual timing register (Kauai)
  431. */
  432. static void
  433. pmac_ide_kauai_selectproc(ide_drive_t *drive)
  434. {
  435. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  436. if (pmif == NULL)
  437. return;
  438. if (drive->select.b.unit & 0x01) {
  439. writel(pmif->timings[1], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  440. writel(pmif->timings[3], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  441. } else {
  442. writel(pmif->timings[0], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  443. writel(pmif->timings[2], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  444. }
  445. (void)readl(PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  446. }
  447. /*
  448. * Force an update of controller timing values for a given drive
  449. */
  450. static void
  451. pmac_ide_do_update_timings(ide_drive_t *drive)
  452. {
  453. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  454. if (pmif == NULL)
  455. return;
  456. if (pmif->kind == controller_sh_ata6 ||
  457. pmif->kind == controller_un_ata6 ||
  458. pmif->kind == controller_k2_ata6)
  459. pmac_ide_kauai_selectproc(drive);
  460. else
  461. pmac_ide_selectproc(drive);
  462. }
  463. static void
  464. pmac_outbsync(ide_drive_t *drive, u8 value, unsigned long port)
  465. {
  466. u32 tmp;
  467. writeb(value, (void __iomem *) port);
  468. tmp = readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  469. }
  470. /*
  471. * Send the SET_FEATURE IDE command to the drive and update drive->id with
  472. * the new state. We currently don't use the generic routine as it used to
  473. * cause various trouble, especially with older mediabays.
  474. * This code is sometimes triggering a spurrious interrupt though, I need
  475. * to sort that out sooner or later and see if I can finally get the
  476. * common version to work properly in all cases
  477. */
  478. static int
  479. pmac_ide_do_setfeature(ide_drive_t *drive, u8 command)
  480. {
  481. ide_hwif_t *hwif = HWIF(drive);
  482. int result = 1;
  483. disable_irq_nosync(hwif->irq);
  484. udelay(1);
  485. SELECT_DRIVE(drive);
  486. SELECT_MASK(drive, 0);
  487. udelay(1);
  488. /* Get rid of pending error state */
  489. (void) hwif->INB(IDE_STATUS_REG);
  490. /* Timeout bumped for some powerbooks */
  491. if (wait_for_ready(drive, 2000)) {
  492. /* Timeout bumped for some powerbooks */
  493. printk(KERN_ERR "%s: pmac_ide_do_setfeature disk not ready "
  494. "before SET_FEATURE!\n", drive->name);
  495. goto out;
  496. }
  497. udelay(10);
  498. hwif->OUTB(drive->ctl | 2, IDE_CONTROL_REG);
  499. hwif->OUTB(command, IDE_NSECTOR_REG);
  500. hwif->OUTB(SETFEATURES_XFER, IDE_FEATURE_REG);
  501. hwif->OUTBSYNC(drive, WIN_SETFEATURES, IDE_COMMAND_REG);
  502. udelay(1);
  503. /* Timeout bumped for some powerbooks */
  504. result = wait_for_ready(drive, 2000);
  505. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  506. if (result)
  507. printk(KERN_ERR "%s: pmac_ide_do_setfeature disk not ready "
  508. "after SET_FEATURE !\n", drive->name);
  509. out:
  510. SELECT_MASK(drive, 0);
  511. if (result == 0) {
  512. drive->id->dma_ultra &= ~0xFF00;
  513. drive->id->dma_mword &= ~0x0F00;
  514. drive->id->dma_1word &= ~0x0F00;
  515. switch(command) {
  516. case XFER_UDMA_7:
  517. drive->id->dma_ultra |= 0x8080; break;
  518. case XFER_UDMA_6:
  519. drive->id->dma_ultra |= 0x4040; break;
  520. case XFER_UDMA_5:
  521. drive->id->dma_ultra |= 0x2020; break;
  522. case XFER_UDMA_4:
  523. drive->id->dma_ultra |= 0x1010; break;
  524. case XFER_UDMA_3:
  525. drive->id->dma_ultra |= 0x0808; break;
  526. case XFER_UDMA_2:
  527. drive->id->dma_ultra |= 0x0404; break;
  528. case XFER_UDMA_1:
  529. drive->id->dma_ultra |= 0x0202; break;
  530. case XFER_UDMA_0:
  531. drive->id->dma_ultra |= 0x0101; break;
  532. case XFER_MW_DMA_2:
  533. drive->id->dma_mword |= 0x0404; break;
  534. case XFER_MW_DMA_1:
  535. drive->id->dma_mword |= 0x0202; break;
  536. case XFER_MW_DMA_0:
  537. drive->id->dma_mword |= 0x0101; break;
  538. case XFER_SW_DMA_2:
  539. drive->id->dma_1word |= 0x0404; break;
  540. case XFER_SW_DMA_1:
  541. drive->id->dma_1word |= 0x0202; break;
  542. case XFER_SW_DMA_0:
  543. drive->id->dma_1word |= 0x0101; break;
  544. default: break;
  545. }
  546. if (!drive->init_speed)
  547. drive->init_speed = command;
  548. drive->current_speed = command;
  549. }
  550. enable_irq(hwif->irq);
  551. return result;
  552. }
  553. /*
  554. * Old tuning functions (called on hdparm -p), sets up drive PIO timings
  555. */
  556. static void
  557. pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  558. {
  559. u32 *timings;
  560. unsigned accessTicks, recTicks;
  561. unsigned accessTime, recTime;
  562. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  563. unsigned int cycle_time;
  564. if (pmif == NULL)
  565. return;
  566. /* which drive is it ? */
  567. timings = &pmif->timings[drive->select.b.unit & 0x01];
  568. cycle_time = ide_pio_cycle_time(drive, pio);
  569. switch (pmif->kind) {
  570. case controller_sh_ata6: {
  571. /* 133Mhz cell */
  572. u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
  573. if (tr == 0)
  574. return;
  575. *timings = ((*timings) & ~TR_133_PIOREG_PIO_MASK) | tr;
  576. break;
  577. }
  578. case controller_un_ata6:
  579. case controller_k2_ata6: {
  580. /* 100Mhz cell */
  581. u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
  582. if (tr == 0)
  583. return;
  584. *timings = ((*timings) & ~TR_100_PIOREG_PIO_MASK) | tr;
  585. break;
  586. }
  587. case controller_kl_ata4:
  588. /* 66Mhz cell */
  589. recTime = cycle_time - ide_pio_timings[pio].active_time
  590. - ide_pio_timings[pio].setup_time;
  591. recTime = max(recTime, 150U);
  592. accessTime = ide_pio_timings[pio].active_time;
  593. accessTime = max(accessTime, 150U);
  594. accessTicks = SYSCLK_TICKS_66(accessTime);
  595. accessTicks = min(accessTicks, 0x1fU);
  596. recTicks = SYSCLK_TICKS_66(recTime);
  597. recTicks = min(recTicks, 0x1fU);
  598. *timings = ((*timings) & ~TR_66_PIO_MASK) |
  599. (accessTicks << TR_66_PIO_ACCESS_SHIFT) |
  600. (recTicks << TR_66_PIO_RECOVERY_SHIFT);
  601. break;
  602. default: {
  603. /* 33Mhz cell */
  604. int ebit = 0;
  605. recTime = cycle_time - ide_pio_timings[pio].active_time
  606. - ide_pio_timings[pio].setup_time;
  607. recTime = max(recTime, 150U);
  608. accessTime = ide_pio_timings[pio].active_time;
  609. accessTime = max(accessTime, 150U);
  610. accessTicks = SYSCLK_TICKS(accessTime);
  611. accessTicks = min(accessTicks, 0x1fU);
  612. accessTicks = max(accessTicks, 4U);
  613. recTicks = SYSCLK_TICKS(recTime);
  614. recTicks = min(recTicks, 0x1fU);
  615. recTicks = max(recTicks, 5U) - 4;
  616. if (recTicks > 9) {
  617. recTicks--; /* guess, but it's only for PIO0, so... */
  618. ebit = 1;
  619. }
  620. *timings = ((*timings) & ~TR_33_PIO_MASK) |
  621. (accessTicks << TR_33_PIO_ACCESS_SHIFT) |
  622. (recTicks << TR_33_PIO_RECOVERY_SHIFT);
  623. if (ebit)
  624. *timings |= TR_33_PIO_E;
  625. break;
  626. }
  627. }
  628. #ifdef IDE_PMAC_DEBUG
  629. printk(KERN_ERR "%s: Set PIO timing for mode %d, reg: 0x%08x\n",
  630. drive->name, pio, *timings);
  631. #endif
  632. if (pmac_ide_do_setfeature(drive, XFER_PIO_0 + pio))
  633. return;
  634. pmac_ide_do_update_timings(drive);
  635. }
  636. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  637. /*
  638. * Calculate KeyLargo ATA/66 UDMA timings
  639. */
  640. static int
  641. set_timings_udma_ata4(u32 *timings, u8 speed)
  642. {
  643. unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
  644. if (speed > XFER_UDMA_4)
  645. return 1;
  646. rdyToPauseTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].rdy2pause);
  647. wrDataSetupTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].wrDataSetup);
  648. addrTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].addrSetup);
  649. *timings = ((*timings) & ~(TR_66_UDMA_MASK | TR_66_MDMA_MASK)) |
  650. (wrDataSetupTicks << TR_66_UDMA_WRDATASETUP_SHIFT) |
  651. (rdyToPauseTicks << TR_66_UDMA_RDY2PAUS_SHIFT) |
  652. (addrTicks <<TR_66_UDMA_ADDRSETUP_SHIFT) |
  653. TR_66_UDMA_EN;
  654. #ifdef IDE_PMAC_DEBUG
  655. printk(KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
  656. speed & 0xf, *timings);
  657. #endif
  658. return 0;
  659. }
  660. /*
  661. * Calculate Kauai ATA/100 UDMA timings
  662. */
  663. static int
  664. set_timings_udma_ata6(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  665. {
  666. struct ide_timing *t = ide_timing_find_mode(speed);
  667. u32 tr;
  668. if (speed > XFER_UDMA_5 || t == NULL)
  669. return 1;
  670. tr = kauai_lookup_timing(kauai_udma_timings, (int)t->udma);
  671. if (tr == 0)
  672. return 1;
  673. *ultra_timings = ((*ultra_timings) & ~TR_100_UDMAREG_UDMA_MASK) | tr;
  674. *ultra_timings = (*ultra_timings) | TR_100_UDMAREG_UDMA_EN;
  675. return 0;
  676. }
  677. /*
  678. * Calculate Shasta ATA/133 UDMA timings
  679. */
  680. static int
  681. set_timings_udma_shasta(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  682. {
  683. struct ide_timing *t = ide_timing_find_mode(speed);
  684. u32 tr;
  685. if (speed > XFER_UDMA_6 || t == NULL)
  686. return 1;
  687. tr = kauai_lookup_timing(shasta_udma133_timings, (int)t->udma);
  688. if (tr == 0)
  689. return 1;
  690. *ultra_timings = ((*ultra_timings) & ~TR_133_UDMAREG_UDMA_MASK) | tr;
  691. *ultra_timings = (*ultra_timings) | TR_133_UDMAREG_UDMA_EN;
  692. return 0;
  693. }
  694. /*
  695. * Calculate MDMA timings for all cells
  696. */
  697. static int
  698. set_timings_mdma(ide_drive_t *drive, int intf_type, u32 *timings, u32 *timings2,
  699. u8 speed, int drive_cycle_time)
  700. {
  701. int cycleTime, accessTime = 0, recTime = 0;
  702. unsigned accessTicks, recTicks;
  703. struct mdma_timings_t* tm = NULL;
  704. int i;
  705. /* Get default cycle time for mode */
  706. switch(speed & 0xf) {
  707. case 0: cycleTime = 480; break;
  708. case 1: cycleTime = 150; break;
  709. case 2: cycleTime = 120; break;
  710. default:
  711. return 1;
  712. }
  713. /* Adjust for drive */
  714. if (drive_cycle_time && drive_cycle_time > cycleTime)
  715. cycleTime = drive_cycle_time;
  716. /* OHare limits according to some old Apple sources */
  717. if ((intf_type == controller_ohare) && (cycleTime < 150))
  718. cycleTime = 150;
  719. /* Get the proper timing array for this controller */
  720. switch(intf_type) {
  721. case controller_sh_ata6:
  722. case controller_un_ata6:
  723. case controller_k2_ata6:
  724. break;
  725. case controller_kl_ata4:
  726. tm = mdma_timings_66;
  727. break;
  728. case controller_kl_ata3:
  729. tm = mdma_timings_33k;
  730. break;
  731. default:
  732. tm = mdma_timings_33;
  733. break;
  734. }
  735. if (tm != NULL) {
  736. /* Lookup matching access & recovery times */
  737. i = -1;
  738. for (;;) {
  739. if (tm[i+1].cycleTime < cycleTime)
  740. break;
  741. i++;
  742. }
  743. if (i < 0)
  744. return 1;
  745. cycleTime = tm[i].cycleTime;
  746. accessTime = tm[i].accessTime;
  747. recTime = tm[i].recoveryTime;
  748. #ifdef IDE_PMAC_DEBUG
  749. printk(KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
  750. drive->name, cycleTime, accessTime, recTime);
  751. #endif
  752. }
  753. switch(intf_type) {
  754. case controller_sh_ata6: {
  755. /* 133Mhz cell */
  756. u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
  757. if (tr == 0)
  758. return 1;
  759. *timings = ((*timings) & ~TR_133_PIOREG_MDMA_MASK) | tr;
  760. *timings2 = (*timings2) & ~TR_133_UDMAREG_UDMA_EN;
  761. }
  762. case controller_un_ata6:
  763. case controller_k2_ata6: {
  764. /* 100Mhz cell */
  765. u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
  766. if (tr == 0)
  767. return 1;
  768. *timings = ((*timings) & ~TR_100_PIOREG_MDMA_MASK) | tr;
  769. *timings2 = (*timings2) & ~TR_100_UDMAREG_UDMA_EN;
  770. }
  771. break;
  772. case controller_kl_ata4:
  773. /* 66Mhz cell */
  774. accessTicks = SYSCLK_TICKS_66(accessTime);
  775. accessTicks = min(accessTicks, 0x1fU);
  776. accessTicks = max(accessTicks, 0x1U);
  777. recTicks = SYSCLK_TICKS_66(recTime);
  778. recTicks = min(recTicks, 0x1fU);
  779. recTicks = max(recTicks, 0x3U);
  780. /* Clear out mdma bits and disable udma */
  781. *timings = ((*timings) & ~(TR_66_MDMA_MASK | TR_66_UDMA_MASK)) |
  782. (accessTicks << TR_66_MDMA_ACCESS_SHIFT) |
  783. (recTicks << TR_66_MDMA_RECOVERY_SHIFT);
  784. break;
  785. case controller_kl_ata3:
  786. /* 33Mhz cell on KeyLargo */
  787. accessTicks = SYSCLK_TICKS(accessTime);
  788. accessTicks = max(accessTicks, 1U);
  789. accessTicks = min(accessTicks, 0x1fU);
  790. accessTime = accessTicks * IDE_SYSCLK_NS;
  791. recTicks = SYSCLK_TICKS(recTime);
  792. recTicks = max(recTicks, 1U);
  793. recTicks = min(recTicks, 0x1fU);
  794. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  795. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  796. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  797. break;
  798. default: {
  799. /* 33Mhz cell on others */
  800. int halfTick = 0;
  801. int origAccessTime = accessTime;
  802. int origRecTime = recTime;
  803. accessTicks = SYSCLK_TICKS(accessTime);
  804. accessTicks = max(accessTicks, 1U);
  805. accessTicks = min(accessTicks, 0x1fU);
  806. accessTime = accessTicks * IDE_SYSCLK_NS;
  807. recTicks = SYSCLK_TICKS(recTime);
  808. recTicks = max(recTicks, 2U) - 1;
  809. recTicks = min(recTicks, 0x1fU);
  810. recTime = (recTicks + 1) * IDE_SYSCLK_NS;
  811. if ((accessTicks > 1) &&
  812. ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
  813. ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
  814. halfTick = 1;
  815. accessTicks--;
  816. }
  817. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  818. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  819. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  820. if (halfTick)
  821. *timings |= TR_33_MDMA_HALFTICK;
  822. }
  823. }
  824. #ifdef IDE_PMAC_DEBUG
  825. printk(KERN_ERR "%s: Set MDMA timing for mode %d, reg: 0x%08x\n",
  826. drive->name, speed & 0xf, *timings);
  827. #endif
  828. return 0;
  829. }
  830. #endif /* #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC */
  831. /*
  832. * Speedproc. This function is called by the core to set any of the standard
  833. * timing (PIO, MDMA or UDMA) to both the drive and the controller.
  834. * You may notice we don't use this function on normal "dma check" operation,
  835. * our dedicated function is more precise as it uses the drive provided
  836. * cycle time value. We should probably fix this one to deal with that too...
  837. */
  838. static int pmac_ide_tune_chipset(ide_drive_t *drive, const u8 speed)
  839. {
  840. int unit = (drive->select.b.unit & 0x01);
  841. int ret = 0;
  842. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  843. u32 *timings, *timings2;
  844. if (pmif == NULL)
  845. return 1;
  846. timings = &pmif->timings[unit];
  847. timings2 = &pmif->timings[unit+2];
  848. switch(speed) {
  849. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  850. case XFER_UDMA_6:
  851. case XFER_UDMA_5:
  852. case XFER_UDMA_4:
  853. case XFER_UDMA_3:
  854. case XFER_UDMA_2:
  855. case XFER_UDMA_1:
  856. case XFER_UDMA_0:
  857. if (pmif->kind == controller_kl_ata4)
  858. ret = set_timings_udma_ata4(timings, speed);
  859. else if (pmif->kind == controller_un_ata6
  860. || pmif->kind == controller_k2_ata6)
  861. ret = set_timings_udma_ata6(timings, timings2, speed);
  862. else if (pmif->kind == controller_sh_ata6)
  863. ret = set_timings_udma_shasta(timings, timings2, speed);
  864. else
  865. ret = 1;
  866. break;
  867. case XFER_MW_DMA_2:
  868. case XFER_MW_DMA_1:
  869. case XFER_MW_DMA_0:
  870. ret = set_timings_mdma(drive, pmif->kind, timings, timings2, speed, 0);
  871. break;
  872. case XFER_SW_DMA_2:
  873. case XFER_SW_DMA_1:
  874. case XFER_SW_DMA_0:
  875. return 1;
  876. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  877. case XFER_PIO_4:
  878. case XFER_PIO_3:
  879. case XFER_PIO_2:
  880. case XFER_PIO_1:
  881. case XFER_PIO_0:
  882. pmac_ide_set_pio_mode(drive, speed & 0x07);
  883. return 0;
  884. default:
  885. ret = 1;
  886. }
  887. if (ret)
  888. return ret;
  889. ret = pmac_ide_do_setfeature(drive, speed);
  890. if (ret)
  891. return ret;
  892. pmac_ide_do_update_timings(drive);
  893. return 0;
  894. }
  895. /*
  896. * Blast some well known "safe" values to the timing registers at init or
  897. * wakeup from sleep time, before we do real calculation
  898. */
  899. static void
  900. sanitize_timings(pmac_ide_hwif_t *pmif)
  901. {
  902. unsigned int value, value2 = 0;
  903. switch(pmif->kind) {
  904. case controller_sh_ata6:
  905. value = 0x0a820c97;
  906. value2 = 0x00033031;
  907. break;
  908. case controller_un_ata6:
  909. case controller_k2_ata6:
  910. value = 0x08618a92;
  911. value2 = 0x00002921;
  912. break;
  913. case controller_kl_ata4:
  914. value = 0x0008438c;
  915. break;
  916. case controller_kl_ata3:
  917. value = 0x00084526;
  918. break;
  919. case controller_heathrow:
  920. case controller_ohare:
  921. default:
  922. value = 0x00074526;
  923. break;
  924. }
  925. pmif->timings[0] = pmif->timings[1] = value;
  926. pmif->timings[2] = pmif->timings[3] = value2;
  927. }
  928. unsigned long
  929. pmac_ide_get_base(int index)
  930. {
  931. return pmac_ide[index].regbase;
  932. }
  933. int
  934. pmac_ide_check_base(unsigned long base)
  935. {
  936. int ix;
  937. for (ix = 0; ix < MAX_HWIFS; ++ix)
  938. if (base == pmac_ide[ix].regbase)
  939. return ix;
  940. return -1;
  941. }
  942. int
  943. pmac_ide_get_irq(unsigned long base)
  944. {
  945. int ix;
  946. for (ix = 0; ix < MAX_HWIFS; ++ix)
  947. if (base == pmac_ide[ix].regbase)
  948. return pmac_ide[ix].irq;
  949. return 0;
  950. }
  951. static int ide_majors[] = { 3, 22, 33, 34, 56, 57 };
  952. dev_t __init
  953. pmac_find_ide_boot(char *bootdevice, int n)
  954. {
  955. int i;
  956. /*
  957. * Look through the list of IDE interfaces for this one.
  958. */
  959. for (i = 0; i < pmac_ide_count; ++i) {
  960. char *name;
  961. if (!pmac_ide[i].node || !pmac_ide[i].node->full_name)
  962. continue;
  963. name = pmac_ide[i].node->full_name;
  964. if (memcmp(name, bootdevice, n) == 0 && name[n] == 0) {
  965. /* XXX should cope with the 2nd drive as well... */
  966. return MKDEV(ide_majors[i], 0);
  967. }
  968. }
  969. return 0;
  970. }
  971. /* Suspend call back, should be called after the child devices
  972. * have actually been suspended
  973. */
  974. static int
  975. pmac_ide_do_suspend(ide_hwif_t *hwif)
  976. {
  977. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  978. /* We clear the timings */
  979. pmif->timings[0] = 0;
  980. pmif->timings[1] = 0;
  981. disable_irq(pmif->irq);
  982. /* The media bay will handle itself just fine */
  983. if (pmif->mediabay)
  984. return 0;
  985. /* Kauai has bus control FCRs directly here */
  986. if (pmif->kauai_fcr) {
  987. u32 fcr = readl(pmif->kauai_fcr);
  988. fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
  989. writel(fcr, pmif->kauai_fcr);
  990. }
  991. /* Disable the bus on older machines and the cell on kauai */
  992. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id,
  993. 0);
  994. return 0;
  995. }
  996. /* Resume call back, should be called before the child devices
  997. * are resumed
  998. */
  999. static int
  1000. pmac_ide_do_resume(ide_hwif_t *hwif)
  1001. {
  1002. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1003. /* Hard reset & re-enable controller (do we really need to reset ? -BenH) */
  1004. if (!pmif->mediabay) {
  1005. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 1);
  1006. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id, 1);
  1007. msleep(10);
  1008. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 0);
  1009. /* Kauai has it different */
  1010. if (pmif->kauai_fcr) {
  1011. u32 fcr = readl(pmif->kauai_fcr);
  1012. fcr |= KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE;
  1013. writel(fcr, pmif->kauai_fcr);
  1014. }
  1015. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  1016. }
  1017. /* Sanitize drive timings */
  1018. sanitize_timings(pmif);
  1019. enable_irq(pmif->irq);
  1020. return 0;
  1021. }
  1022. /*
  1023. * Setup, register & probe an IDE channel driven by this driver, this is
  1024. * called by one of the 2 probe functions (macio or PCI). Note that a channel
  1025. * that ends up beeing free of any device is not kept around by this driver
  1026. * (it is kept in 2.4). This introduce an interface numbering change on some
  1027. * rare machines unfortunately, but it's better this way.
  1028. */
  1029. static int
  1030. pmac_ide_setup_device(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif)
  1031. {
  1032. struct device_node *np = pmif->node;
  1033. const int *bidp;
  1034. pmif->cable_80 = 0;
  1035. pmif->broken_dma = pmif->broken_dma_warn = 0;
  1036. if (of_device_is_compatible(np, "shasta-ata"))
  1037. pmif->kind = controller_sh_ata6;
  1038. else if (of_device_is_compatible(np, "kauai-ata"))
  1039. pmif->kind = controller_un_ata6;
  1040. else if (of_device_is_compatible(np, "K2-UATA"))
  1041. pmif->kind = controller_k2_ata6;
  1042. else if (of_device_is_compatible(np, "keylargo-ata")) {
  1043. if (strcmp(np->name, "ata-4") == 0)
  1044. pmif->kind = controller_kl_ata4;
  1045. else
  1046. pmif->kind = controller_kl_ata3;
  1047. } else if (of_device_is_compatible(np, "heathrow-ata"))
  1048. pmif->kind = controller_heathrow;
  1049. else {
  1050. pmif->kind = controller_ohare;
  1051. pmif->broken_dma = 1;
  1052. }
  1053. bidp = of_get_property(np, "AAPL,bus-id", NULL);
  1054. pmif->aapl_bus_id = bidp ? *bidp : 0;
  1055. /* Get cable type from device-tree */
  1056. if (pmif->kind == controller_kl_ata4 || pmif->kind == controller_un_ata6
  1057. || pmif->kind == controller_k2_ata6
  1058. || pmif->kind == controller_sh_ata6) {
  1059. const char* cable = of_get_property(np, "cable-type", NULL);
  1060. if (cable && !strncmp(cable, "80-", 3))
  1061. pmif->cable_80 = 1;
  1062. }
  1063. /* G5's seem to have incorrect cable type in device-tree. Let's assume
  1064. * they have a 80 conductor cable, this seem to be always the case unless
  1065. * the user mucked around
  1066. */
  1067. if (of_device_is_compatible(np, "K2-UATA") ||
  1068. of_device_is_compatible(np, "shasta-ata"))
  1069. pmif->cable_80 = 1;
  1070. /* On Kauai-type controllers, we make sure the FCR is correct */
  1071. if (pmif->kauai_fcr)
  1072. writel(KAUAI_FCR_UATA_MAGIC |
  1073. KAUAI_FCR_UATA_RESET_N |
  1074. KAUAI_FCR_UATA_ENABLE, pmif->kauai_fcr);
  1075. pmif->mediabay = 0;
  1076. /* Make sure we have sane timings */
  1077. sanitize_timings(pmif);
  1078. #ifndef CONFIG_PPC64
  1079. /* XXX FIXME: Media bay stuff need re-organizing */
  1080. if (np->parent && np->parent->name
  1081. && strcasecmp(np->parent->name, "media-bay") == 0) {
  1082. #ifdef CONFIG_PMAC_MEDIABAY
  1083. media_bay_set_ide_infos(np->parent, pmif->regbase, pmif->irq, hwif->index);
  1084. #endif /* CONFIG_PMAC_MEDIABAY */
  1085. pmif->mediabay = 1;
  1086. if (!bidp)
  1087. pmif->aapl_bus_id = 1;
  1088. } else if (pmif->kind == controller_ohare) {
  1089. /* The code below is having trouble on some ohare machines
  1090. * (timing related ?). Until I can put my hand on one of these
  1091. * units, I keep the old way
  1092. */
  1093. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
  1094. } else
  1095. #endif
  1096. {
  1097. /* This is necessary to enable IDE when net-booting */
  1098. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 1);
  1099. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, pmif->aapl_bus_id, 1);
  1100. msleep(10);
  1101. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 0);
  1102. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  1103. }
  1104. /* Setup MMIO ops */
  1105. default_hwif_mmiops(hwif);
  1106. hwif->OUTBSYNC = pmac_outbsync;
  1107. /* Tell common code _not_ to mess with resources */
  1108. hwif->mmio = 1;
  1109. hwif->hwif_data = pmif;
  1110. pmac_ide_init_hwif_ports(&hwif->hw, pmif->regbase, 0, &hwif->irq);
  1111. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
  1112. hwif->chipset = ide_pmac;
  1113. hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET] || pmif->mediabay;
  1114. hwif->hold = pmif->mediabay;
  1115. hwif->cbl = pmif->cable_80 ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  1116. hwif->drives[0].unmask = 1;
  1117. hwif->drives[1].unmask = 1;
  1118. hwif->pio_mask = ATA_PIO4;
  1119. hwif->set_pio_mode = pmac_ide_set_pio_mode;
  1120. if (pmif->kind == controller_un_ata6
  1121. || pmif->kind == controller_k2_ata6
  1122. || pmif->kind == controller_sh_ata6)
  1123. hwif->selectproc = pmac_ide_kauai_selectproc;
  1124. else
  1125. hwif->selectproc = pmac_ide_selectproc;
  1126. hwif->speedproc = pmac_ide_tune_chipset;
  1127. printk(KERN_INFO "ide%d: Found Apple %s controller, bus ID %d%s, irq %d\n",
  1128. hwif->index, model_name[pmif->kind], pmif->aapl_bus_id,
  1129. pmif->mediabay ? " (mediabay)" : "", hwif->irq);
  1130. #ifdef CONFIG_PMAC_MEDIABAY
  1131. if (pmif->mediabay && check_media_bay_by_base(pmif->regbase, MB_CD) == 0)
  1132. hwif->noprobe = 0;
  1133. #endif /* CONFIG_PMAC_MEDIABAY */
  1134. hwif->sg_max_nents = MAX_DCMDS;
  1135. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1136. /* has a DBDMA controller channel */
  1137. if (pmif->dma_regs)
  1138. pmac_ide_setup_dma(pmif, hwif);
  1139. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1140. /* We probe the hwif now */
  1141. probe_hwif_init(hwif);
  1142. ide_proc_register_port(hwif);
  1143. return 0;
  1144. }
  1145. /*
  1146. * Attach to a macio probed interface
  1147. */
  1148. static int __devinit
  1149. pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
  1150. {
  1151. void __iomem *base;
  1152. unsigned long regbase;
  1153. int irq;
  1154. ide_hwif_t *hwif;
  1155. pmac_ide_hwif_t *pmif;
  1156. int i, rc;
  1157. i = 0;
  1158. while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
  1159. || pmac_ide[i].node != NULL))
  1160. ++i;
  1161. if (i >= MAX_HWIFS) {
  1162. printk(KERN_ERR "ide-pmac: MacIO interface attach with no slot\n");
  1163. printk(KERN_ERR " %s\n", mdev->ofdev.node->full_name);
  1164. return -ENODEV;
  1165. }
  1166. pmif = &pmac_ide[i];
  1167. hwif = &ide_hwifs[i];
  1168. if (macio_resource_count(mdev) == 0) {
  1169. printk(KERN_WARNING "ide%d: no address for %s\n",
  1170. i, mdev->ofdev.node->full_name);
  1171. return -ENXIO;
  1172. }
  1173. /* Request memory resource for IO ports */
  1174. if (macio_request_resource(mdev, 0, "ide-pmac (ports)")) {
  1175. printk(KERN_ERR "ide%d: can't request mmio resource !\n", i);
  1176. return -EBUSY;
  1177. }
  1178. /* XXX This is bogus. Should be fixed in the registry by checking
  1179. * the kind of host interrupt controller, a bit like gatwick
  1180. * fixes in irq.c. That works well enough for the single case
  1181. * where that happens though...
  1182. */
  1183. if (macio_irq_count(mdev) == 0) {
  1184. printk(KERN_WARNING "ide%d: no intrs for device %s, using 13\n",
  1185. i, mdev->ofdev.node->full_name);
  1186. irq = irq_create_mapping(NULL, 13);
  1187. } else
  1188. irq = macio_irq(mdev, 0);
  1189. base = ioremap(macio_resource_start(mdev, 0), 0x400);
  1190. regbase = (unsigned long) base;
  1191. hwif->pci_dev = mdev->bus->pdev;
  1192. hwif->gendev.parent = &mdev->ofdev.dev;
  1193. pmif->mdev = mdev;
  1194. pmif->node = mdev->ofdev.node;
  1195. pmif->regbase = regbase;
  1196. pmif->irq = irq;
  1197. pmif->kauai_fcr = NULL;
  1198. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1199. if (macio_resource_count(mdev) >= 2) {
  1200. if (macio_request_resource(mdev, 1, "ide-pmac (dma)"))
  1201. printk(KERN_WARNING "ide%d: can't request DMA resource !\n", i);
  1202. else
  1203. pmif->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1204. } else
  1205. pmif->dma_regs = NULL;
  1206. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1207. dev_set_drvdata(&mdev->ofdev.dev, hwif);
  1208. rc = pmac_ide_setup_device(pmif, hwif);
  1209. if (rc != 0) {
  1210. /* The inteface is released to the common IDE layer */
  1211. dev_set_drvdata(&mdev->ofdev.dev, NULL);
  1212. iounmap(base);
  1213. if (pmif->dma_regs)
  1214. iounmap(pmif->dma_regs);
  1215. memset(pmif, 0, sizeof(*pmif));
  1216. macio_release_resource(mdev, 0);
  1217. if (pmif->dma_regs)
  1218. macio_release_resource(mdev, 1);
  1219. }
  1220. return rc;
  1221. }
  1222. static int
  1223. pmac_ide_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1224. {
  1225. ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1226. int rc = 0;
  1227. if (mesg.event != mdev->ofdev.dev.power.power_state.event
  1228. && mesg.event == PM_EVENT_SUSPEND) {
  1229. rc = pmac_ide_do_suspend(hwif);
  1230. if (rc == 0)
  1231. mdev->ofdev.dev.power.power_state = mesg;
  1232. }
  1233. return rc;
  1234. }
  1235. static int
  1236. pmac_ide_macio_resume(struct macio_dev *mdev)
  1237. {
  1238. ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1239. int rc = 0;
  1240. if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
  1241. rc = pmac_ide_do_resume(hwif);
  1242. if (rc == 0)
  1243. mdev->ofdev.dev.power.power_state = PMSG_ON;
  1244. }
  1245. return rc;
  1246. }
  1247. /*
  1248. * Attach to a PCI probed interface
  1249. */
  1250. static int __devinit
  1251. pmac_ide_pci_attach(struct pci_dev *pdev, const struct pci_device_id *id)
  1252. {
  1253. ide_hwif_t *hwif;
  1254. struct device_node *np;
  1255. pmac_ide_hwif_t *pmif;
  1256. void __iomem *base;
  1257. unsigned long rbase, rlen;
  1258. int i, rc;
  1259. np = pci_device_to_OF_node(pdev);
  1260. if (np == NULL) {
  1261. printk(KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
  1262. return -ENODEV;
  1263. }
  1264. i = 0;
  1265. while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
  1266. || pmac_ide[i].node != NULL))
  1267. ++i;
  1268. if (i >= MAX_HWIFS) {
  1269. printk(KERN_ERR "ide-pmac: PCI interface attach with no slot\n");
  1270. printk(KERN_ERR " %s\n", np->full_name);
  1271. return -ENODEV;
  1272. }
  1273. pmif = &pmac_ide[i];
  1274. hwif = &ide_hwifs[i];
  1275. if (pci_enable_device(pdev)) {
  1276. printk(KERN_WARNING "ide%i: Can't enable PCI device for %s\n",
  1277. i, np->full_name);
  1278. return -ENXIO;
  1279. }
  1280. pci_set_master(pdev);
  1281. if (pci_request_regions(pdev, "Kauai ATA")) {
  1282. printk(KERN_ERR "ide%d: Cannot obtain PCI resources for %s\n",
  1283. i, np->full_name);
  1284. return -ENXIO;
  1285. }
  1286. hwif->pci_dev = pdev;
  1287. hwif->gendev.parent = &pdev->dev;
  1288. pmif->mdev = NULL;
  1289. pmif->node = np;
  1290. rbase = pci_resource_start(pdev, 0);
  1291. rlen = pci_resource_len(pdev, 0);
  1292. base = ioremap(rbase, rlen);
  1293. pmif->regbase = (unsigned long) base + 0x2000;
  1294. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1295. pmif->dma_regs = base + 0x1000;
  1296. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1297. pmif->kauai_fcr = base;
  1298. pmif->irq = pdev->irq;
  1299. pci_set_drvdata(pdev, hwif);
  1300. rc = pmac_ide_setup_device(pmif, hwif);
  1301. if (rc != 0) {
  1302. /* The inteface is released to the common IDE layer */
  1303. pci_set_drvdata(pdev, NULL);
  1304. iounmap(base);
  1305. memset(pmif, 0, sizeof(*pmif));
  1306. pci_release_regions(pdev);
  1307. }
  1308. return rc;
  1309. }
  1310. static int
  1311. pmac_ide_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1312. {
  1313. ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
  1314. int rc = 0;
  1315. if (mesg.event != pdev->dev.power.power_state.event
  1316. && mesg.event == PM_EVENT_SUSPEND) {
  1317. rc = pmac_ide_do_suspend(hwif);
  1318. if (rc == 0)
  1319. pdev->dev.power.power_state = mesg;
  1320. }
  1321. return rc;
  1322. }
  1323. static int
  1324. pmac_ide_pci_resume(struct pci_dev *pdev)
  1325. {
  1326. ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
  1327. int rc = 0;
  1328. if (pdev->dev.power.power_state.event != PM_EVENT_ON) {
  1329. rc = pmac_ide_do_resume(hwif);
  1330. if (rc == 0)
  1331. pdev->dev.power.power_state = PMSG_ON;
  1332. }
  1333. return rc;
  1334. }
  1335. static struct of_device_id pmac_ide_macio_match[] =
  1336. {
  1337. {
  1338. .name = "IDE",
  1339. },
  1340. {
  1341. .name = "ATA",
  1342. },
  1343. {
  1344. .type = "ide",
  1345. },
  1346. {
  1347. .type = "ata",
  1348. },
  1349. {},
  1350. };
  1351. static struct macio_driver pmac_ide_macio_driver =
  1352. {
  1353. .name = "ide-pmac",
  1354. .match_table = pmac_ide_macio_match,
  1355. .probe = pmac_ide_macio_attach,
  1356. .suspend = pmac_ide_macio_suspend,
  1357. .resume = pmac_ide_macio_resume,
  1358. };
  1359. static struct pci_device_id pmac_ide_pci_match[] = {
  1360. { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA,
  1361. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1362. { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100,
  1363. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1364. { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100,
  1365. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1366. { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_SH_ATA,
  1367. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1368. { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA,
  1369. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1370. {},
  1371. };
  1372. static struct pci_driver pmac_ide_pci_driver = {
  1373. .name = "ide-pmac",
  1374. .id_table = pmac_ide_pci_match,
  1375. .probe = pmac_ide_pci_attach,
  1376. .suspend = pmac_ide_pci_suspend,
  1377. .resume = pmac_ide_pci_resume,
  1378. };
  1379. MODULE_DEVICE_TABLE(pci, pmac_ide_pci_match);
  1380. int __init pmac_ide_probe(void)
  1381. {
  1382. int error;
  1383. if (!machine_is(powermac))
  1384. return -ENODEV;
  1385. #ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
  1386. error = pci_register_driver(&pmac_ide_pci_driver);
  1387. if (error)
  1388. goto out;
  1389. error = macio_register_driver(&pmac_ide_macio_driver);
  1390. if (error) {
  1391. pci_unregister_driver(&pmac_ide_pci_driver);
  1392. goto out;
  1393. }
  1394. #else
  1395. error = macio_register_driver(&pmac_ide_macio_driver);
  1396. if (error)
  1397. goto out;
  1398. error = pci_register_driver(&pmac_ide_pci_driver);
  1399. if (error) {
  1400. macio_unregister_driver(&pmac_ide_macio_driver);
  1401. goto out;
  1402. }
  1403. #endif
  1404. out:
  1405. return error;
  1406. }
  1407. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1408. /*
  1409. * pmac_ide_build_dmatable builds the DBDMA command list
  1410. * for a transfer and sets the DBDMA channel to point to it.
  1411. */
  1412. static int
  1413. pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq)
  1414. {
  1415. struct dbdma_cmd *table;
  1416. int i, count = 0;
  1417. ide_hwif_t *hwif = HWIF(drive);
  1418. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1419. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1420. struct scatterlist *sg;
  1421. int wr = (rq_data_dir(rq) == WRITE);
  1422. /* DMA table is already aligned */
  1423. table = (struct dbdma_cmd *) pmif->dma_table_cpu;
  1424. /* Make sure DMA controller is stopped (necessary ?) */
  1425. writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma->control);
  1426. while (readl(&dma->status) & RUN)
  1427. udelay(1);
  1428. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  1429. if (!i)
  1430. return 0;
  1431. /* Build DBDMA commands list */
  1432. sg = hwif->sg_table;
  1433. while (i && sg_dma_len(sg)) {
  1434. u32 cur_addr;
  1435. u32 cur_len;
  1436. cur_addr = sg_dma_address(sg);
  1437. cur_len = sg_dma_len(sg);
  1438. if (pmif->broken_dma && cur_addr & (L1_CACHE_BYTES - 1)) {
  1439. if (pmif->broken_dma_warn == 0) {
  1440. printk(KERN_WARNING "%s: DMA on non aligned address,"
  1441. "switching to PIO on Ohare chipset\n", drive->name);
  1442. pmif->broken_dma_warn = 1;
  1443. }
  1444. goto use_pio_instead;
  1445. }
  1446. while (cur_len) {
  1447. unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
  1448. if (count++ >= MAX_DCMDS) {
  1449. printk(KERN_WARNING "%s: DMA table too small\n",
  1450. drive->name);
  1451. goto use_pio_instead;
  1452. }
  1453. st_le16(&table->command, wr? OUTPUT_MORE: INPUT_MORE);
  1454. st_le16(&table->req_count, tc);
  1455. st_le32(&table->phy_addr, cur_addr);
  1456. table->cmd_dep = 0;
  1457. table->xfer_status = 0;
  1458. table->res_count = 0;
  1459. cur_addr += tc;
  1460. cur_len -= tc;
  1461. ++table;
  1462. }
  1463. sg++;
  1464. i--;
  1465. }
  1466. /* convert the last command to an input/output last command */
  1467. if (count) {
  1468. st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
  1469. /* add the stop command to the end of the list */
  1470. memset(table, 0, sizeof(struct dbdma_cmd));
  1471. st_le16(&table->command, DBDMA_STOP);
  1472. mb();
  1473. writel(hwif->dmatable_dma, &dma->cmdptr);
  1474. return 1;
  1475. }
  1476. printk(KERN_DEBUG "%s: empty DMA table?\n", drive->name);
  1477. use_pio_instead:
  1478. pci_unmap_sg(hwif->pci_dev,
  1479. hwif->sg_table,
  1480. hwif->sg_nents,
  1481. hwif->sg_dma_direction);
  1482. return 0; /* revert to PIO for this request */
  1483. }
  1484. /* Teardown mappings after DMA has completed. */
  1485. static void
  1486. pmac_ide_destroy_dmatable (ide_drive_t *drive)
  1487. {
  1488. ide_hwif_t *hwif = drive->hwif;
  1489. struct pci_dev *dev = HWIF(drive)->pci_dev;
  1490. struct scatterlist *sg = hwif->sg_table;
  1491. int nents = hwif->sg_nents;
  1492. if (nents) {
  1493. pci_unmap_sg(dev, sg, nents, hwif->sg_dma_direction);
  1494. hwif->sg_nents = 0;
  1495. }
  1496. }
  1497. /*
  1498. * Pick up best MDMA timing for the drive and apply it
  1499. */
  1500. static int
  1501. pmac_ide_mdma_enable(ide_drive_t *drive, u16 mode)
  1502. {
  1503. ide_hwif_t *hwif = HWIF(drive);
  1504. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1505. int drive_cycle_time;
  1506. struct hd_driveid *id = drive->id;
  1507. u32 *timings, *timings2;
  1508. u32 timing_local[2];
  1509. int ret;
  1510. /* which drive is it ? */
  1511. timings = &pmif->timings[drive->select.b.unit & 0x01];
  1512. timings2 = &pmif->timings[(drive->select.b.unit & 0x01) + 2];
  1513. /* Check if drive provide explicit cycle time */
  1514. if ((id->field_valid & 2) && (id->eide_dma_time))
  1515. drive_cycle_time = id->eide_dma_time;
  1516. else
  1517. drive_cycle_time = 0;
  1518. /* Copy timings to local image */
  1519. timing_local[0] = *timings;
  1520. timing_local[1] = *timings2;
  1521. /* Calculate controller timings */
  1522. ret = set_timings_mdma( drive, pmif->kind,
  1523. &timing_local[0],
  1524. &timing_local[1],
  1525. mode,
  1526. drive_cycle_time);
  1527. if (ret)
  1528. return 0;
  1529. /* Set feature on drive */
  1530. printk(KERN_INFO "%s: Enabling MultiWord DMA %d\n", drive->name, mode & 0xf);
  1531. ret = pmac_ide_do_setfeature(drive, mode);
  1532. if (ret) {
  1533. printk(KERN_WARNING "%s: Failed !\n", drive->name);
  1534. return 0;
  1535. }
  1536. /* Apply timings to controller */
  1537. *timings = timing_local[0];
  1538. *timings2 = timing_local[1];
  1539. return 1;
  1540. }
  1541. /*
  1542. * Pick up best UDMA timing for the drive and apply it
  1543. */
  1544. static int
  1545. pmac_ide_udma_enable(ide_drive_t *drive, u16 mode)
  1546. {
  1547. ide_hwif_t *hwif = HWIF(drive);
  1548. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1549. u32 *timings, *timings2;
  1550. u32 timing_local[2];
  1551. int ret;
  1552. /* which drive is it ? */
  1553. timings = &pmif->timings[drive->select.b.unit & 0x01];
  1554. timings2 = &pmif->timings[(drive->select.b.unit & 0x01) + 2];
  1555. /* Copy timings to local image */
  1556. timing_local[0] = *timings;
  1557. timing_local[1] = *timings2;
  1558. /* Calculate timings for interface */
  1559. if (pmif->kind == controller_un_ata6
  1560. || pmif->kind == controller_k2_ata6)
  1561. ret = set_timings_udma_ata6( &timing_local[0],
  1562. &timing_local[1],
  1563. mode);
  1564. else if (pmif->kind == controller_sh_ata6)
  1565. ret = set_timings_udma_shasta( &timing_local[0],
  1566. &timing_local[1],
  1567. mode);
  1568. else
  1569. ret = set_timings_udma_ata4(&timing_local[0], mode);
  1570. if (ret)
  1571. return 0;
  1572. /* Set feature on drive */
  1573. printk(KERN_INFO "%s: Enabling Ultra DMA %d\n", drive->name, mode & 0x0f);
  1574. ret = pmac_ide_do_setfeature(drive, mode);
  1575. if (ret) {
  1576. printk(KERN_WARNING "%s: Failed !\n", drive->name);
  1577. return 0;
  1578. }
  1579. /* Apply timings to controller */
  1580. *timings = timing_local[0];
  1581. *timings2 = timing_local[1];
  1582. return 1;
  1583. }
  1584. /*
  1585. * Check what is the best DMA timing setting for the drive and
  1586. * call appropriate functions to apply it.
  1587. */
  1588. static int
  1589. pmac_ide_dma_check(ide_drive_t *drive)
  1590. {
  1591. struct hd_driveid *id = drive->id;
  1592. ide_hwif_t *hwif = HWIF(drive);
  1593. int enable = 1;
  1594. drive->using_dma = 0;
  1595. if (drive->media == ide_floppy)
  1596. enable = 0;
  1597. if (((id->capability & 1) == 0) && !__ide_dma_good_drive(drive))
  1598. enable = 0;
  1599. if (__ide_dma_bad_drive(drive))
  1600. enable = 0;
  1601. if (enable) {
  1602. u8 mode = ide_max_dma_mode(drive);
  1603. if (mode >= XFER_UDMA_0)
  1604. drive->using_dma = pmac_ide_udma_enable(drive, mode);
  1605. else if (mode >= XFER_MW_DMA_0)
  1606. drive->using_dma = pmac_ide_mdma_enable(drive, mode);
  1607. hwif->OUTB(0, IDE_CONTROL_REG);
  1608. /* Apply settings to controller */
  1609. pmac_ide_do_update_timings(drive);
  1610. }
  1611. return 0;
  1612. }
  1613. /*
  1614. * Prepare a DMA transfer. We build the DMA table, adjust the timings for
  1615. * a read on KeyLargo ATA/66 and mark us as waiting for DMA completion
  1616. */
  1617. static int
  1618. pmac_ide_dma_setup(ide_drive_t *drive)
  1619. {
  1620. ide_hwif_t *hwif = HWIF(drive);
  1621. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1622. struct request *rq = HWGROUP(drive)->rq;
  1623. u8 unit = (drive->select.b.unit & 0x01);
  1624. u8 ata4;
  1625. if (pmif == NULL)
  1626. return 1;
  1627. ata4 = (pmif->kind == controller_kl_ata4);
  1628. if (!pmac_ide_build_dmatable(drive, rq)) {
  1629. ide_map_sg(drive, rq);
  1630. return 1;
  1631. }
  1632. /* Apple adds 60ns to wrDataSetup on reads */
  1633. if (ata4 && (pmif->timings[unit] & TR_66_UDMA_EN)) {
  1634. writel(pmif->timings[unit] + (!rq_data_dir(rq) ? 0x00800000UL : 0),
  1635. PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1636. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1637. }
  1638. drive->waiting_for_dma = 1;
  1639. return 0;
  1640. }
  1641. static void
  1642. pmac_ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  1643. {
  1644. /* issue cmd to drive */
  1645. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, NULL);
  1646. }
  1647. /*
  1648. * Kick the DMA controller into life after the DMA command has been issued
  1649. * to the drive.
  1650. */
  1651. static void
  1652. pmac_ide_dma_start(ide_drive_t *drive)
  1653. {
  1654. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1655. volatile struct dbdma_regs __iomem *dma;
  1656. dma = pmif->dma_regs;
  1657. writel((RUN << 16) | RUN, &dma->control);
  1658. /* Make sure it gets to the controller right now */
  1659. (void)readl(&dma->control);
  1660. }
  1661. /*
  1662. * After a DMA transfer, make sure the controller is stopped
  1663. */
  1664. static int
  1665. pmac_ide_dma_end (ide_drive_t *drive)
  1666. {
  1667. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1668. volatile struct dbdma_regs __iomem *dma;
  1669. u32 dstat;
  1670. if (pmif == NULL)
  1671. return 0;
  1672. dma = pmif->dma_regs;
  1673. drive->waiting_for_dma = 0;
  1674. dstat = readl(&dma->status);
  1675. writel(((RUN|WAKE|DEAD) << 16), &dma->control);
  1676. pmac_ide_destroy_dmatable(drive);
  1677. /* verify good dma status. we don't check for ACTIVE beeing 0. We should...
  1678. * in theory, but with ATAPI decices doing buffer underruns, that would
  1679. * cause us to disable DMA, which isn't what we want
  1680. */
  1681. return (dstat & (RUN|DEAD)) != RUN;
  1682. }
  1683. /*
  1684. * Check out that the interrupt we got was for us. We can't always know this
  1685. * for sure with those Apple interfaces (well, we could on the recent ones but
  1686. * that's not implemented yet), on the other hand, we don't have shared interrupts
  1687. * so it's not really a problem
  1688. */
  1689. static int
  1690. pmac_ide_dma_test_irq (ide_drive_t *drive)
  1691. {
  1692. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1693. volatile struct dbdma_regs __iomem *dma;
  1694. unsigned long status, timeout;
  1695. if (pmif == NULL)
  1696. return 0;
  1697. dma = pmif->dma_regs;
  1698. /* We have to things to deal with here:
  1699. *
  1700. * - The dbdma won't stop if the command was started
  1701. * but completed with an error without transferring all
  1702. * datas. This happens when bad blocks are met during
  1703. * a multi-block transfer.
  1704. *
  1705. * - The dbdma fifo hasn't yet finished flushing to
  1706. * to system memory when the disk interrupt occurs.
  1707. *
  1708. */
  1709. /* If ACTIVE is cleared, the STOP command have passed and
  1710. * transfer is complete.
  1711. */
  1712. status = readl(&dma->status);
  1713. if (!(status & ACTIVE))
  1714. return 1;
  1715. if (!drive->waiting_for_dma)
  1716. printk(KERN_WARNING "ide%d, ide_dma_test_irq \
  1717. called while not waiting\n", HWIF(drive)->index);
  1718. /* If dbdma didn't execute the STOP command yet, the
  1719. * active bit is still set. We consider that we aren't
  1720. * sharing interrupts (which is hopefully the case with
  1721. * those controllers) and so we just try to flush the
  1722. * channel for pending data in the fifo
  1723. */
  1724. udelay(1);
  1725. writel((FLUSH << 16) | FLUSH, &dma->control);
  1726. timeout = 0;
  1727. for (;;) {
  1728. udelay(1);
  1729. status = readl(&dma->status);
  1730. if ((status & FLUSH) == 0)
  1731. break;
  1732. if (++timeout > 100) {
  1733. printk(KERN_WARNING "ide%d, ide_dma_test_irq \
  1734. timeout flushing channel\n", HWIF(drive)->index);
  1735. break;
  1736. }
  1737. }
  1738. return 1;
  1739. }
  1740. static void pmac_ide_dma_host_off(ide_drive_t *drive)
  1741. {
  1742. }
  1743. static void pmac_ide_dma_host_on(ide_drive_t *drive)
  1744. {
  1745. }
  1746. static void
  1747. pmac_ide_dma_lost_irq (ide_drive_t *drive)
  1748. {
  1749. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1750. volatile struct dbdma_regs __iomem *dma;
  1751. unsigned long status;
  1752. if (pmif == NULL)
  1753. return;
  1754. dma = pmif->dma_regs;
  1755. status = readl(&dma->status);
  1756. printk(KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
  1757. }
  1758. /*
  1759. * Allocate the data structures needed for using DMA with an interface
  1760. * and fill the proper list of functions pointers
  1761. */
  1762. static void __init
  1763. pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif)
  1764. {
  1765. /* We won't need pci_dev if we switch to generic consistent
  1766. * DMA routines ...
  1767. */
  1768. if (hwif->pci_dev == NULL)
  1769. return;
  1770. /*
  1771. * Allocate space for the DBDMA commands.
  1772. * The +2 is +1 for the stop command and +1 to allow for
  1773. * aligning the start address to a multiple of 16 bytes.
  1774. */
  1775. pmif->dma_table_cpu = (struct dbdma_cmd*)pci_alloc_consistent(
  1776. hwif->pci_dev,
  1777. (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
  1778. &hwif->dmatable_dma);
  1779. if (pmif->dma_table_cpu == NULL) {
  1780. printk(KERN_ERR "%s: unable to allocate DMA command list\n",
  1781. hwif->name);
  1782. return;
  1783. }
  1784. hwif->dma_off_quietly = &ide_dma_off_quietly;
  1785. hwif->ide_dma_on = &__ide_dma_on;
  1786. hwif->ide_dma_check = &pmac_ide_dma_check;
  1787. hwif->dma_setup = &pmac_ide_dma_setup;
  1788. hwif->dma_exec_cmd = &pmac_ide_dma_exec_cmd;
  1789. hwif->dma_start = &pmac_ide_dma_start;
  1790. hwif->ide_dma_end = &pmac_ide_dma_end;
  1791. hwif->ide_dma_test_irq = &pmac_ide_dma_test_irq;
  1792. hwif->dma_host_off = &pmac_ide_dma_host_off;
  1793. hwif->dma_host_on = &pmac_ide_dma_host_on;
  1794. hwif->dma_timeout = &ide_dma_timeout;
  1795. hwif->dma_lost_irq = &pmac_ide_dma_lost_irq;
  1796. hwif->atapi_dma = 1;
  1797. switch(pmif->kind) {
  1798. case controller_sh_ata6:
  1799. hwif->ultra_mask = pmif->cable_80 ? 0x7f : 0x07;
  1800. hwif->mwdma_mask = 0x07;
  1801. hwif->swdma_mask = 0x00;
  1802. break;
  1803. case controller_un_ata6:
  1804. case controller_k2_ata6:
  1805. hwif->ultra_mask = pmif->cable_80 ? 0x3f : 0x07;
  1806. hwif->mwdma_mask = 0x07;
  1807. hwif->swdma_mask = 0x00;
  1808. break;
  1809. case controller_kl_ata4:
  1810. hwif->ultra_mask = pmif->cable_80 ? 0x1f : 0x07;
  1811. hwif->mwdma_mask = 0x07;
  1812. hwif->swdma_mask = 0x00;
  1813. break;
  1814. default:
  1815. hwif->ultra_mask = 0x00;
  1816. hwif->mwdma_mask = 0x07;
  1817. hwif->swdma_mask = 0x00;
  1818. break;
  1819. }
  1820. }
  1821. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */