pmac.c 55 KB

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