pmac.c 58 KB

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