pmac.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. /*
  2. * Support for IDE interfaces on PowerMacs.
  3. *
  4. * These IDE interfaces are memory-mapped and have a DBDMA channel
  5. * for doing DMA.
  6. *
  7. * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt
  8. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Some code taken from drivers/ide/ide-dma.c:
  16. *
  17. * Copyright (c) 1995-1998 Mark Lord
  18. *
  19. * TODO: - Use pre-calculated (kauai) timing tables all the time and
  20. * get rid of the "rounded" tables used previously, so we have the
  21. * same table format for all controllers and can then just have one
  22. * big table
  23. *
  24. */
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/ide.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/pci.h>
  33. #include <linux/adb.h>
  34. #include <linux/pmu.h>
  35. #include <linux/scatterlist.h>
  36. #include <asm/prom.h>
  37. #include <asm/io.h>
  38. #include <asm/dbdma.h>
  39. #include <asm/ide.h>
  40. #include <asm/pci-bridge.h>
  41. #include <asm/machdep.h>
  42. #include <asm/pmac_feature.h>
  43. #include <asm/sections.h>
  44. #include <asm/irq.h>
  45. #ifndef CONFIG_PPC64
  46. #include <asm/mediabay.h>
  47. #endif
  48. #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. { 0 , 0 },
  287. };
  288. static struct kauai_timing kauai_mdma_timings[] =
  289. {
  290. { 1260 , 0x00fff000 },
  291. { 480 , 0x00618000 },
  292. { 360 , 0x00492000 },
  293. { 270 , 0x0038e000 },
  294. { 240 , 0x0030c000 },
  295. { 210 , 0x002cb000 },
  296. { 180 , 0x00249000 },
  297. { 150 , 0x00209000 },
  298. { 120 , 0x00148000 },
  299. { 0 , 0 },
  300. };
  301. static struct kauai_timing kauai_udma_timings[] =
  302. {
  303. { 120 , 0x000070c0 },
  304. { 90 , 0x00005d80 },
  305. { 60 , 0x00004a60 },
  306. { 45 , 0x00003a50 },
  307. { 30 , 0x00002a30 },
  308. { 20 , 0x00002921 },
  309. { 0 , 0 },
  310. };
  311. static struct kauai_timing shasta_pio_timings[] =
  312. {
  313. { 930 , 0x08000fff },
  314. { 600 , 0x0A000c97 },
  315. { 383 , 0x07000712 },
  316. { 360 , 0x040003cd },
  317. { 330 , 0x040003cd },
  318. { 300 , 0x040003cd },
  319. { 270 , 0x040003cd },
  320. { 240 , 0x040003cd },
  321. { 239 , 0x040003cd },
  322. { 180 , 0x0400028b },
  323. { 120 , 0x0400010a },
  324. { 0 , 0 },
  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. BUG();
  358. return 0;
  359. }
  360. /* allow up to 256 DBDMA commands per xfer */
  361. #define MAX_DCMDS 256
  362. /*
  363. * Wait 1s for disk to answer on IDE bus after a hard reset
  364. * of the device (via GPIO/FCR).
  365. *
  366. * Some devices seem to "pollute" the bus even after dropping
  367. * the BSY bit (typically some combo drives slave on the UDMA
  368. * bus) after a hard reset. Since we hard reset all drives on
  369. * KeyLargo ATA66, we have to keep that delay around. I may end
  370. * up not hard resetting anymore on these and keep the delay only
  371. * for older interfaces instead (we have to reset when coming
  372. * from MacOS...) --BenH.
  373. */
  374. #define IDE_WAKEUP_DELAY (1*HZ)
  375. static void pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif);
  376. static int pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq);
  377. static void pmac_ide_selectproc(ide_drive_t *drive);
  378. static void pmac_ide_kauai_selectproc(ide_drive_t *drive);
  379. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  380. /*
  381. * N.B. this can't be an initfunc, because the media-bay task can
  382. * call ide_[un]register at any time.
  383. */
  384. void
  385. pmac_ide_init_hwif_ports(hw_regs_t *hw,
  386. unsigned long data_port, unsigned long ctrl_port,
  387. int *irq)
  388. {
  389. int i, ix;
  390. if (data_port == 0)
  391. return;
  392. for (ix = 0; ix < MAX_HWIFS; ++ix)
  393. if (data_port == pmac_ide[ix].regbase)
  394. break;
  395. if (ix >= MAX_HWIFS)
  396. return; /* not an IDE PMAC interface */
  397. for (i = 0; i < 8; ++i)
  398. hw->io_ports[i] = data_port + i * 0x10;
  399. hw->io_ports[8] = data_port + 0x160;
  400. if (irq != NULL)
  401. *irq = pmac_ide[ix].irq;
  402. hw->dev = &pmac_ide[ix].mdev->ofdev.dev;
  403. }
  404. #define PMAC_IDE_REG(x) ((void __iomem *)(IDE_DATA_REG+(x)))
  405. /*
  406. * Apply the timings of the proper unit (master/slave) to the shared
  407. * timing register when selecting that unit. This version is for
  408. * ASICs with a single timing register
  409. */
  410. static void
  411. pmac_ide_selectproc(ide_drive_t *drive)
  412. {
  413. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  414. if (pmif == NULL)
  415. return;
  416. if (drive->select.b.unit & 0x01)
  417. writel(pmif->timings[1], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  418. else
  419. writel(pmif->timings[0], PMAC_IDE_REG(IDE_TIMING_CONFIG));
  420. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  421. }
  422. /*
  423. * Apply the timings of the proper unit (master/slave) to the shared
  424. * timing register when selecting that unit. This version is for
  425. * ASICs with a dual timing register (Kauai)
  426. */
  427. static void
  428. pmac_ide_kauai_selectproc(ide_drive_t *drive)
  429. {
  430. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  431. if (pmif == NULL)
  432. return;
  433. if (drive->select.b.unit & 0x01) {
  434. writel(pmif->timings[1], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  435. writel(pmif->timings[3], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  436. } else {
  437. writel(pmif->timings[0], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  438. writel(pmif->timings[2], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
  439. }
  440. (void)readl(PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
  441. }
  442. /*
  443. * Force an update of controller timing values for a given drive
  444. */
  445. static void
  446. pmac_ide_do_update_timings(ide_drive_t *drive)
  447. {
  448. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  449. if (pmif == NULL)
  450. return;
  451. if (pmif->kind == controller_sh_ata6 ||
  452. pmif->kind == controller_un_ata6 ||
  453. pmif->kind == controller_k2_ata6)
  454. pmac_ide_kauai_selectproc(drive);
  455. else
  456. pmac_ide_selectproc(drive);
  457. }
  458. static void
  459. pmac_outbsync(ide_drive_t *drive, u8 value, unsigned long port)
  460. {
  461. u32 tmp;
  462. writeb(value, (void __iomem *) port);
  463. tmp = readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  464. }
  465. /*
  466. * Old tuning functions (called on hdparm -p), sets up drive PIO timings
  467. */
  468. static void
  469. pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  470. {
  471. u32 *timings, t;
  472. unsigned accessTicks, recTicks;
  473. unsigned accessTime, recTime;
  474. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  475. unsigned int cycle_time;
  476. if (pmif == NULL)
  477. return;
  478. /* which drive is it ? */
  479. timings = &pmif->timings[drive->select.b.unit & 0x01];
  480. t = *timings;
  481. cycle_time = ide_pio_cycle_time(drive, pio);
  482. switch (pmif->kind) {
  483. case controller_sh_ata6: {
  484. /* 133Mhz cell */
  485. u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
  486. t = (t & ~TR_133_PIOREG_PIO_MASK) | tr;
  487. break;
  488. }
  489. case controller_un_ata6:
  490. case controller_k2_ata6: {
  491. /* 100Mhz cell */
  492. u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
  493. t = (t & ~TR_100_PIOREG_PIO_MASK) | tr;
  494. break;
  495. }
  496. case controller_kl_ata4:
  497. /* 66Mhz cell */
  498. recTime = cycle_time - ide_pio_timings[pio].active_time
  499. - ide_pio_timings[pio].setup_time;
  500. recTime = max(recTime, 150U);
  501. accessTime = ide_pio_timings[pio].active_time;
  502. accessTime = max(accessTime, 150U);
  503. accessTicks = SYSCLK_TICKS_66(accessTime);
  504. accessTicks = min(accessTicks, 0x1fU);
  505. recTicks = SYSCLK_TICKS_66(recTime);
  506. recTicks = min(recTicks, 0x1fU);
  507. t = (t & ~TR_66_PIO_MASK) |
  508. (accessTicks << TR_66_PIO_ACCESS_SHIFT) |
  509. (recTicks << TR_66_PIO_RECOVERY_SHIFT);
  510. break;
  511. default: {
  512. /* 33Mhz cell */
  513. int ebit = 0;
  514. recTime = cycle_time - ide_pio_timings[pio].active_time
  515. - ide_pio_timings[pio].setup_time;
  516. recTime = max(recTime, 150U);
  517. accessTime = ide_pio_timings[pio].active_time;
  518. accessTime = max(accessTime, 150U);
  519. accessTicks = SYSCLK_TICKS(accessTime);
  520. accessTicks = min(accessTicks, 0x1fU);
  521. accessTicks = max(accessTicks, 4U);
  522. recTicks = SYSCLK_TICKS(recTime);
  523. recTicks = min(recTicks, 0x1fU);
  524. recTicks = max(recTicks, 5U) - 4;
  525. if (recTicks > 9) {
  526. recTicks--; /* guess, but it's only for PIO0, so... */
  527. ebit = 1;
  528. }
  529. t = (t & ~TR_33_PIO_MASK) |
  530. (accessTicks << TR_33_PIO_ACCESS_SHIFT) |
  531. (recTicks << TR_33_PIO_RECOVERY_SHIFT);
  532. if (ebit)
  533. t |= TR_33_PIO_E;
  534. break;
  535. }
  536. }
  537. #ifdef IDE_PMAC_DEBUG
  538. printk(KERN_ERR "%s: Set PIO timing for mode %d, reg: 0x%08x\n",
  539. drive->name, pio, *timings);
  540. #endif
  541. *timings = t;
  542. pmac_ide_do_update_timings(drive);
  543. }
  544. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  545. /*
  546. * Calculate KeyLargo ATA/66 UDMA timings
  547. */
  548. static int
  549. set_timings_udma_ata4(u32 *timings, u8 speed)
  550. {
  551. unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
  552. if (speed > XFER_UDMA_4)
  553. return 1;
  554. rdyToPauseTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].rdy2pause);
  555. wrDataSetupTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].wrDataSetup);
  556. addrTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].addrSetup);
  557. *timings = ((*timings) & ~(TR_66_UDMA_MASK | TR_66_MDMA_MASK)) |
  558. (wrDataSetupTicks << TR_66_UDMA_WRDATASETUP_SHIFT) |
  559. (rdyToPauseTicks << TR_66_UDMA_RDY2PAUS_SHIFT) |
  560. (addrTicks <<TR_66_UDMA_ADDRSETUP_SHIFT) |
  561. TR_66_UDMA_EN;
  562. #ifdef IDE_PMAC_DEBUG
  563. printk(KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
  564. speed & 0xf, *timings);
  565. #endif
  566. return 0;
  567. }
  568. /*
  569. * Calculate Kauai ATA/100 UDMA timings
  570. */
  571. static int
  572. set_timings_udma_ata6(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  573. {
  574. struct ide_timing *t = ide_timing_find_mode(speed);
  575. u32 tr;
  576. if (speed > XFER_UDMA_5 || t == NULL)
  577. return 1;
  578. tr = kauai_lookup_timing(kauai_udma_timings, (int)t->udma);
  579. *ultra_timings = ((*ultra_timings) & ~TR_100_UDMAREG_UDMA_MASK) | tr;
  580. *ultra_timings = (*ultra_timings) | TR_100_UDMAREG_UDMA_EN;
  581. return 0;
  582. }
  583. /*
  584. * Calculate Shasta ATA/133 UDMA timings
  585. */
  586. static int
  587. set_timings_udma_shasta(u32 *pio_timings, u32 *ultra_timings, u8 speed)
  588. {
  589. struct ide_timing *t = ide_timing_find_mode(speed);
  590. u32 tr;
  591. if (speed > XFER_UDMA_6 || t == NULL)
  592. return 1;
  593. tr = kauai_lookup_timing(shasta_udma133_timings, (int)t->udma);
  594. *ultra_timings = ((*ultra_timings) & ~TR_133_UDMAREG_UDMA_MASK) | tr;
  595. *ultra_timings = (*ultra_timings) | TR_133_UDMAREG_UDMA_EN;
  596. return 0;
  597. }
  598. /*
  599. * Calculate MDMA timings for all cells
  600. */
  601. static void
  602. set_timings_mdma(ide_drive_t *drive, int intf_type, u32 *timings, u32 *timings2,
  603. u8 speed)
  604. {
  605. int cycleTime, accessTime = 0, recTime = 0;
  606. unsigned accessTicks, recTicks;
  607. struct hd_driveid *id = drive->id;
  608. struct mdma_timings_t* tm = NULL;
  609. int i;
  610. /* Get default cycle time for mode */
  611. switch(speed & 0xf) {
  612. case 0: cycleTime = 480; break;
  613. case 1: cycleTime = 150; break;
  614. case 2: cycleTime = 120; break;
  615. default:
  616. BUG();
  617. break;
  618. }
  619. /* Check if drive provides explicit DMA cycle time */
  620. if ((id->field_valid & 2) && id->eide_dma_time)
  621. cycleTime = max_t(int, id->eide_dma_time, cycleTime);
  622. /* OHare limits according to some old Apple sources */
  623. if ((intf_type == controller_ohare) && (cycleTime < 150))
  624. cycleTime = 150;
  625. /* Get the proper timing array for this controller */
  626. switch(intf_type) {
  627. case controller_sh_ata6:
  628. case controller_un_ata6:
  629. case controller_k2_ata6:
  630. break;
  631. case controller_kl_ata4:
  632. tm = mdma_timings_66;
  633. break;
  634. case controller_kl_ata3:
  635. tm = mdma_timings_33k;
  636. break;
  637. default:
  638. tm = mdma_timings_33;
  639. break;
  640. }
  641. if (tm != NULL) {
  642. /* Lookup matching access & recovery times */
  643. i = -1;
  644. for (;;) {
  645. if (tm[i+1].cycleTime < cycleTime)
  646. break;
  647. i++;
  648. }
  649. cycleTime = tm[i].cycleTime;
  650. accessTime = tm[i].accessTime;
  651. recTime = tm[i].recoveryTime;
  652. #ifdef IDE_PMAC_DEBUG
  653. printk(KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
  654. drive->name, cycleTime, accessTime, recTime);
  655. #endif
  656. }
  657. switch(intf_type) {
  658. case controller_sh_ata6: {
  659. /* 133Mhz cell */
  660. u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
  661. *timings = ((*timings) & ~TR_133_PIOREG_MDMA_MASK) | tr;
  662. *timings2 = (*timings2) & ~TR_133_UDMAREG_UDMA_EN;
  663. }
  664. case controller_un_ata6:
  665. case controller_k2_ata6: {
  666. /* 100Mhz cell */
  667. u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
  668. *timings = ((*timings) & ~TR_100_PIOREG_MDMA_MASK) | tr;
  669. *timings2 = (*timings2) & ~TR_100_UDMAREG_UDMA_EN;
  670. }
  671. break;
  672. case controller_kl_ata4:
  673. /* 66Mhz cell */
  674. accessTicks = SYSCLK_TICKS_66(accessTime);
  675. accessTicks = min(accessTicks, 0x1fU);
  676. accessTicks = max(accessTicks, 0x1U);
  677. recTicks = SYSCLK_TICKS_66(recTime);
  678. recTicks = min(recTicks, 0x1fU);
  679. recTicks = max(recTicks, 0x3U);
  680. /* Clear out mdma bits and disable udma */
  681. *timings = ((*timings) & ~(TR_66_MDMA_MASK | TR_66_UDMA_MASK)) |
  682. (accessTicks << TR_66_MDMA_ACCESS_SHIFT) |
  683. (recTicks << TR_66_MDMA_RECOVERY_SHIFT);
  684. break;
  685. case controller_kl_ata3:
  686. /* 33Mhz cell on KeyLargo */
  687. accessTicks = SYSCLK_TICKS(accessTime);
  688. accessTicks = max(accessTicks, 1U);
  689. accessTicks = min(accessTicks, 0x1fU);
  690. accessTime = accessTicks * IDE_SYSCLK_NS;
  691. recTicks = SYSCLK_TICKS(recTime);
  692. recTicks = max(recTicks, 1U);
  693. recTicks = min(recTicks, 0x1fU);
  694. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  695. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  696. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  697. break;
  698. default: {
  699. /* 33Mhz cell on others */
  700. int halfTick = 0;
  701. int origAccessTime = accessTime;
  702. int origRecTime = recTime;
  703. accessTicks = SYSCLK_TICKS(accessTime);
  704. accessTicks = max(accessTicks, 1U);
  705. accessTicks = min(accessTicks, 0x1fU);
  706. accessTime = accessTicks * IDE_SYSCLK_NS;
  707. recTicks = SYSCLK_TICKS(recTime);
  708. recTicks = max(recTicks, 2U) - 1;
  709. recTicks = min(recTicks, 0x1fU);
  710. recTime = (recTicks + 1) * IDE_SYSCLK_NS;
  711. if ((accessTicks > 1) &&
  712. ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
  713. ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
  714. halfTick = 1;
  715. accessTicks--;
  716. }
  717. *timings = ((*timings) & ~TR_33_MDMA_MASK) |
  718. (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
  719. (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
  720. if (halfTick)
  721. *timings |= TR_33_MDMA_HALFTICK;
  722. }
  723. }
  724. #ifdef IDE_PMAC_DEBUG
  725. printk(KERN_ERR "%s: Set MDMA timing for mode %d, reg: 0x%08x\n",
  726. drive->name, speed & 0xf, *timings);
  727. #endif
  728. }
  729. #endif /* #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC */
  730. static void pmac_ide_set_dma_mode(ide_drive_t *drive, const u8 speed)
  731. {
  732. int unit = (drive->select.b.unit & 0x01);
  733. int ret = 0;
  734. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  735. u32 *timings, *timings2, tl[2];
  736. timings = &pmif->timings[unit];
  737. timings2 = &pmif->timings[unit+2];
  738. /* Copy timings to local image */
  739. tl[0] = *timings;
  740. tl[1] = *timings2;
  741. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  742. if (speed >= XFER_UDMA_0) {
  743. if (pmif->kind == controller_kl_ata4)
  744. ret = set_timings_udma_ata4(&tl[0], speed);
  745. else if (pmif->kind == controller_un_ata6
  746. || pmif->kind == controller_k2_ata6)
  747. ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
  748. else if (pmif->kind == controller_sh_ata6)
  749. ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
  750. else
  751. ret = -1;
  752. } else
  753. set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
  754. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  755. if (ret)
  756. return;
  757. /* Apply timings to controller */
  758. *timings = tl[0];
  759. *timings2 = tl[1];
  760. pmac_ide_do_update_timings(drive);
  761. }
  762. /*
  763. * Blast some well known "safe" values to the timing registers at init or
  764. * wakeup from sleep time, before we do real calculation
  765. */
  766. static void
  767. sanitize_timings(pmac_ide_hwif_t *pmif)
  768. {
  769. unsigned int value, value2 = 0;
  770. switch(pmif->kind) {
  771. case controller_sh_ata6:
  772. value = 0x0a820c97;
  773. value2 = 0x00033031;
  774. break;
  775. case controller_un_ata6:
  776. case controller_k2_ata6:
  777. value = 0x08618a92;
  778. value2 = 0x00002921;
  779. break;
  780. case controller_kl_ata4:
  781. value = 0x0008438c;
  782. break;
  783. case controller_kl_ata3:
  784. value = 0x00084526;
  785. break;
  786. case controller_heathrow:
  787. case controller_ohare:
  788. default:
  789. value = 0x00074526;
  790. break;
  791. }
  792. pmif->timings[0] = pmif->timings[1] = value;
  793. pmif->timings[2] = pmif->timings[3] = value2;
  794. }
  795. unsigned long
  796. pmac_ide_get_base(int index)
  797. {
  798. return pmac_ide[index].regbase;
  799. }
  800. int
  801. pmac_ide_check_base(unsigned long base)
  802. {
  803. int ix;
  804. for (ix = 0; ix < MAX_HWIFS; ++ix)
  805. if (base == pmac_ide[ix].regbase)
  806. return ix;
  807. return -1;
  808. }
  809. int
  810. pmac_ide_get_irq(unsigned long base)
  811. {
  812. int ix;
  813. for (ix = 0; ix < MAX_HWIFS; ++ix)
  814. if (base == pmac_ide[ix].regbase)
  815. return pmac_ide[ix].irq;
  816. return 0;
  817. }
  818. static int ide_majors[] = { 3, 22, 33, 34, 56, 57 };
  819. dev_t __init
  820. pmac_find_ide_boot(char *bootdevice, int n)
  821. {
  822. int i;
  823. /*
  824. * Look through the list of IDE interfaces for this one.
  825. */
  826. for (i = 0; i < pmac_ide_count; ++i) {
  827. char *name;
  828. if (!pmac_ide[i].node || !pmac_ide[i].node->full_name)
  829. continue;
  830. name = pmac_ide[i].node->full_name;
  831. if (memcmp(name, bootdevice, n) == 0 && name[n] == 0) {
  832. /* XXX should cope with the 2nd drive as well... */
  833. return MKDEV(ide_majors[i], 0);
  834. }
  835. }
  836. return 0;
  837. }
  838. /* Suspend call back, should be called after the child devices
  839. * have actually been suspended
  840. */
  841. static int
  842. pmac_ide_do_suspend(ide_hwif_t *hwif)
  843. {
  844. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  845. /* We clear the timings */
  846. pmif->timings[0] = 0;
  847. pmif->timings[1] = 0;
  848. disable_irq(pmif->irq);
  849. /* The media bay will handle itself just fine */
  850. if (pmif->mediabay)
  851. return 0;
  852. /* Kauai has bus control FCRs directly here */
  853. if (pmif->kauai_fcr) {
  854. u32 fcr = readl(pmif->kauai_fcr);
  855. fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
  856. writel(fcr, pmif->kauai_fcr);
  857. }
  858. /* Disable the bus on older machines and the cell on kauai */
  859. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id,
  860. 0);
  861. return 0;
  862. }
  863. /* Resume call back, should be called before the child devices
  864. * are resumed
  865. */
  866. static int
  867. pmac_ide_do_resume(ide_hwif_t *hwif)
  868. {
  869. pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  870. /* Hard reset & re-enable controller (do we really need to reset ? -BenH) */
  871. if (!pmif->mediabay) {
  872. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 1);
  873. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id, 1);
  874. msleep(10);
  875. ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 0);
  876. /* Kauai has it different */
  877. if (pmif->kauai_fcr) {
  878. u32 fcr = readl(pmif->kauai_fcr);
  879. fcr |= KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE;
  880. writel(fcr, pmif->kauai_fcr);
  881. }
  882. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  883. }
  884. /* Sanitize drive timings */
  885. sanitize_timings(pmif);
  886. enable_irq(pmif->irq);
  887. return 0;
  888. }
  889. /*
  890. * Setup, register & probe an IDE channel driven by this driver, this is
  891. * called by one of the 2 probe functions (macio or PCI). Note that a channel
  892. * that ends up beeing free of any device is not kept around by this driver
  893. * (it is kept in 2.4). This introduce an interface numbering change on some
  894. * rare machines unfortunately, but it's better this way.
  895. */
  896. static int __devinit
  897. pmac_ide_setup_device(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif, hw_regs_t *hw)
  898. {
  899. struct device_node *np = pmif->node;
  900. const int *bidp;
  901. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  902. pmif->cable_80 = 0;
  903. pmif->broken_dma = pmif->broken_dma_warn = 0;
  904. if (of_device_is_compatible(np, "shasta-ata"))
  905. pmif->kind = controller_sh_ata6;
  906. else if (of_device_is_compatible(np, "kauai-ata"))
  907. pmif->kind = controller_un_ata6;
  908. else if (of_device_is_compatible(np, "K2-UATA"))
  909. pmif->kind = controller_k2_ata6;
  910. else if (of_device_is_compatible(np, "keylargo-ata")) {
  911. if (strcmp(np->name, "ata-4") == 0)
  912. pmif->kind = controller_kl_ata4;
  913. else
  914. pmif->kind = controller_kl_ata3;
  915. } else if (of_device_is_compatible(np, "heathrow-ata"))
  916. pmif->kind = controller_heathrow;
  917. else {
  918. pmif->kind = controller_ohare;
  919. pmif->broken_dma = 1;
  920. }
  921. bidp = of_get_property(np, "AAPL,bus-id", NULL);
  922. pmif->aapl_bus_id = bidp ? *bidp : 0;
  923. /* Get cable type from device-tree */
  924. if (pmif->kind == controller_kl_ata4 || pmif->kind == controller_un_ata6
  925. || pmif->kind == controller_k2_ata6
  926. || pmif->kind == controller_sh_ata6) {
  927. const char* cable = of_get_property(np, "cable-type", NULL);
  928. if (cable && !strncmp(cable, "80-", 3))
  929. pmif->cable_80 = 1;
  930. }
  931. /* G5's seem to have incorrect cable type in device-tree. Let's assume
  932. * they have a 80 conductor cable, this seem to be always the case unless
  933. * the user mucked around
  934. */
  935. if (of_device_is_compatible(np, "K2-UATA") ||
  936. of_device_is_compatible(np, "shasta-ata"))
  937. pmif->cable_80 = 1;
  938. /* On Kauai-type controllers, we make sure the FCR is correct */
  939. if (pmif->kauai_fcr)
  940. writel(KAUAI_FCR_UATA_MAGIC |
  941. KAUAI_FCR_UATA_RESET_N |
  942. KAUAI_FCR_UATA_ENABLE, pmif->kauai_fcr);
  943. pmif->mediabay = 0;
  944. /* Make sure we have sane timings */
  945. sanitize_timings(pmif);
  946. #ifndef CONFIG_PPC64
  947. /* XXX FIXME: Media bay stuff need re-organizing */
  948. if (np->parent && np->parent->name
  949. && strcasecmp(np->parent->name, "media-bay") == 0) {
  950. #ifdef CONFIG_PMAC_MEDIABAY
  951. media_bay_set_ide_infos(np->parent, pmif->regbase, pmif->irq, hwif->index);
  952. #endif /* CONFIG_PMAC_MEDIABAY */
  953. pmif->mediabay = 1;
  954. if (!bidp)
  955. pmif->aapl_bus_id = 1;
  956. } else if (pmif->kind == controller_ohare) {
  957. /* The code below is having trouble on some ohare machines
  958. * (timing related ?). Until I can put my hand on one of these
  959. * units, I keep the old way
  960. */
  961. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
  962. } else
  963. #endif
  964. {
  965. /* This is necessary to enable IDE when net-booting */
  966. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 1);
  967. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, pmif->aapl_bus_id, 1);
  968. msleep(10);
  969. ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 0);
  970. msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
  971. }
  972. /* Setup MMIO ops */
  973. default_hwif_mmiops(hwif);
  974. hwif->OUTBSYNC = pmac_outbsync;
  975. /* Tell common code _not_ to mess with resources */
  976. hwif->mmio = 1;
  977. hwif->hwif_data = pmif;
  978. hw->chipset = ide_pmac;
  979. ide_init_port_hw(hwif, hw);
  980. hwif->noprobe = pmif->mediabay;
  981. hwif->hold = pmif->mediabay;
  982. hwif->cbl = pmif->cable_80 ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  983. hwif->drives[0].unmask = 1;
  984. hwif->drives[1].unmask = 1;
  985. hwif->drives[0].autotune = IDE_TUNE_AUTO;
  986. hwif->drives[1].autotune = IDE_TUNE_AUTO;
  987. hwif->host_flags = IDE_HFLAG_SET_PIO_MODE_KEEP_DMA |
  988. IDE_HFLAG_PIO_NO_DOWNGRADE |
  989. IDE_HFLAG_POST_SET_MODE;
  990. hwif->pio_mask = ATA_PIO4;
  991. hwif->set_pio_mode = pmac_ide_set_pio_mode;
  992. if (pmif->kind == controller_un_ata6
  993. || pmif->kind == controller_k2_ata6
  994. || pmif->kind == controller_sh_ata6)
  995. hwif->selectproc = pmac_ide_kauai_selectproc;
  996. else
  997. hwif->selectproc = pmac_ide_selectproc;
  998. hwif->set_dma_mode = pmac_ide_set_dma_mode;
  999. printk(KERN_INFO "ide%d: Found Apple %s controller, bus ID %d%s, irq %d\n",
  1000. hwif->index, model_name[pmif->kind], pmif->aapl_bus_id,
  1001. pmif->mediabay ? " (mediabay)" : "", hwif->irq);
  1002. #ifdef CONFIG_PMAC_MEDIABAY
  1003. if (pmif->mediabay && check_media_bay_by_base(pmif->regbase, MB_CD) == 0)
  1004. hwif->noprobe = 0;
  1005. #endif /* CONFIG_PMAC_MEDIABAY */
  1006. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1007. /* has a DBDMA controller channel */
  1008. if (pmif->dma_regs)
  1009. pmac_ide_setup_dma(pmif, hwif);
  1010. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1011. idx[0] = hwif->index;
  1012. ide_device_add(idx);
  1013. return 0;
  1014. }
  1015. /*
  1016. * Attach to a macio probed interface
  1017. */
  1018. static int __devinit
  1019. pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
  1020. {
  1021. void __iomem *base;
  1022. unsigned long regbase;
  1023. int irq;
  1024. ide_hwif_t *hwif;
  1025. pmac_ide_hwif_t *pmif;
  1026. int i, rc;
  1027. hw_regs_t hw;
  1028. i = 0;
  1029. while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
  1030. || pmac_ide[i].node != NULL))
  1031. ++i;
  1032. if (i >= MAX_HWIFS) {
  1033. printk(KERN_ERR "ide-pmac: MacIO interface attach with no slot\n");
  1034. printk(KERN_ERR " %s\n", mdev->ofdev.node->full_name);
  1035. return -ENODEV;
  1036. }
  1037. pmif = &pmac_ide[i];
  1038. hwif = &ide_hwifs[i];
  1039. if (macio_resource_count(mdev) == 0) {
  1040. printk(KERN_WARNING "ide%d: no address for %s\n",
  1041. i, mdev->ofdev.node->full_name);
  1042. return -ENXIO;
  1043. }
  1044. /* Request memory resource for IO ports */
  1045. if (macio_request_resource(mdev, 0, "ide-pmac (ports)")) {
  1046. printk(KERN_ERR "ide%d: can't request mmio resource !\n", i);
  1047. return -EBUSY;
  1048. }
  1049. /* XXX This is bogus. Should be fixed in the registry by checking
  1050. * the kind of host interrupt controller, a bit like gatwick
  1051. * fixes in irq.c. That works well enough for the single case
  1052. * where that happens though...
  1053. */
  1054. if (macio_irq_count(mdev) == 0) {
  1055. printk(KERN_WARNING "ide%d: no intrs for device %s, using 13\n",
  1056. i, mdev->ofdev.node->full_name);
  1057. irq = irq_create_mapping(NULL, 13);
  1058. } else
  1059. irq = macio_irq(mdev, 0);
  1060. base = ioremap(macio_resource_start(mdev, 0), 0x400);
  1061. regbase = (unsigned long) base;
  1062. hwif->dev = &mdev->bus->pdev->dev;
  1063. pmif->mdev = mdev;
  1064. pmif->node = mdev->ofdev.node;
  1065. pmif->regbase = regbase;
  1066. pmif->irq = irq;
  1067. pmif->kauai_fcr = NULL;
  1068. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1069. if (macio_resource_count(mdev) >= 2) {
  1070. if (macio_request_resource(mdev, 1, "ide-pmac (dma)"))
  1071. printk(KERN_WARNING "ide%d: can't request DMA resource !\n", i);
  1072. else
  1073. pmif->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x1000);
  1074. } else
  1075. pmif->dma_regs = NULL;
  1076. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1077. dev_set_drvdata(&mdev->ofdev.dev, hwif);
  1078. memset(&hw, 0, sizeof(hw));
  1079. pmac_ide_init_hwif_ports(&hw, pmif->regbase, 0, NULL);
  1080. hw.irq = irq;
  1081. hw.dev = &mdev->ofdev.dev;
  1082. rc = pmac_ide_setup_device(pmif, hwif, &hw);
  1083. if (rc != 0) {
  1084. /* The inteface is released to the common IDE layer */
  1085. dev_set_drvdata(&mdev->ofdev.dev, NULL);
  1086. iounmap(base);
  1087. if (pmif->dma_regs) {
  1088. iounmap(pmif->dma_regs);
  1089. macio_release_resource(mdev, 1);
  1090. }
  1091. memset(pmif, 0, sizeof(*pmif));
  1092. macio_release_resource(mdev, 0);
  1093. }
  1094. return rc;
  1095. }
  1096. static int
  1097. pmac_ide_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1098. {
  1099. ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1100. int rc = 0;
  1101. if (mesg.event != mdev->ofdev.dev.power.power_state.event
  1102. && mesg.event == PM_EVENT_SUSPEND) {
  1103. rc = pmac_ide_do_suspend(hwif);
  1104. if (rc == 0)
  1105. mdev->ofdev.dev.power.power_state = mesg;
  1106. }
  1107. return rc;
  1108. }
  1109. static int
  1110. pmac_ide_macio_resume(struct macio_dev *mdev)
  1111. {
  1112. ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
  1113. int rc = 0;
  1114. if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
  1115. rc = pmac_ide_do_resume(hwif);
  1116. if (rc == 0)
  1117. mdev->ofdev.dev.power.power_state = PMSG_ON;
  1118. }
  1119. return rc;
  1120. }
  1121. /*
  1122. * Attach to a PCI probed interface
  1123. */
  1124. static int __devinit
  1125. pmac_ide_pci_attach(struct pci_dev *pdev, const struct pci_device_id *id)
  1126. {
  1127. ide_hwif_t *hwif;
  1128. struct device_node *np;
  1129. pmac_ide_hwif_t *pmif;
  1130. void __iomem *base;
  1131. unsigned long rbase, rlen;
  1132. int i, rc;
  1133. hw_regs_t hw;
  1134. np = pci_device_to_OF_node(pdev);
  1135. if (np == NULL) {
  1136. printk(KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
  1137. return -ENODEV;
  1138. }
  1139. i = 0;
  1140. while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
  1141. || pmac_ide[i].node != NULL))
  1142. ++i;
  1143. if (i >= MAX_HWIFS) {
  1144. printk(KERN_ERR "ide-pmac: PCI interface attach with no slot\n");
  1145. printk(KERN_ERR " %s\n", np->full_name);
  1146. return -ENODEV;
  1147. }
  1148. pmif = &pmac_ide[i];
  1149. hwif = &ide_hwifs[i];
  1150. if (pci_enable_device(pdev)) {
  1151. printk(KERN_WARNING "ide%i: Can't enable PCI device for %s\n",
  1152. i, np->full_name);
  1153. return -ENXIO;
  1154. }
  1155. pci_set_master(pdev);
  1156. if (pci_request_regions(pdev, "Kauai ATA")) {
  1157. printk(KERN_ERR "ide%d: Cannot obtain PCI resources for %s\n",
  1158. i, np->full_name);
  1159. return -ENXIO;
  1160. }
  1161. hwif->dev = &pdev->dev;
  1162. pmif->mdev = NULL;
  1163. pmif->node = np;
  1164. rbase = pci_resource_start(pdev, 0);
  1165. rlen = pci_resource_len(pdev, 0);
  1166. base = ioremap(rbase, rlen);
  1167. pmif->regbase = (unsigned long) base + 0x2000;
  1168. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1169. pmif->dma_regs = base + 0x1000;
  1170. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1171. pmif->kauai_fcr = base;
  1172. pmif->irq = pdev->irq;
  1173. pci_set_drvdata(pdev, hwif);
  1174. memset(&hw, 0, sizeof(hw));
  1175. pmac_ide_init_hwif_ports(&hw, pmif->regbase, 0, NULL);
  1176. hw.irq = pdev->irq;
  1177. hw.dev = &pdev->dev;
  1178. rc = pmac_ide_setup_device(pmif, hwif, &hw);
  1179. if (rc != 0) {
  1180. /* The inteface is released to the common IDE layer */
  1181. pci_set_drvdata(pdev, NULL);
  1182. iounmap(base);
  1183. memset(pmif, 0, sizeof(*pmif));
  1184. pci_release_regions(pdev);
  1185. }
  1186. return rc;
  1187. }
  1188. static int
  1189. pmac_ide_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1190. {
  1191. ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
  1192. int rc = 0;
  1193. if (mesg.event != pdev->dev.power.power_state.event
  1194. && mesg.event == PM_EVENT_SUSPEND) {
  1195. rc = pmac_ide_do_suspend(hwif);
  1196. if (rc == 0)
  1197. pdev->dev.power.power_state = mesg;
  1198. }
  1199. return rc;
  1200. }
  1201. static int
  1202. pmac_ide_pci_resume(struct pci_dev *pdev)
  1203. {
  1204. ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
  1205. int rc = 0;
  1206. if (pdev->dev.power.power_state.event != PM_EVENT_ON) {
  1207. rc = pmac_ide_do_resume(hwif);
  1208. if (rc == 0)
  1209. pdev->dev.power.power_state = PMSG_ON;
  1210. }
  1211. return rc;
  1212. }
  1213. static struct of_device_id pmac_ide_macio_match[] =
  1214. {
  1215. {
  1216. .name = "IDE",
  1217. },
  1218. {
  1219. .name = "ATA",
  1220. },
  1221. {
  1222. .type = "ide",
  1223. },
  1224. {
  1225. .type = "ata",
  1226. },
  1227. {},
  1228. };
  1229. static struct macio_driver pmac_ide_macio_driver =
  1230. {
  1231. .name = "ide-pmac",
  1232. .match_table = pmac_ide_macio_match,
  1233. .probe = pmac_ide_macio_attach,
  1234. .suspend = pmac_ide_macio_suspend,
  1235. .resume = pmac_ide_macio_resume,
  1236. };
  1237. static const struct pci_device_id pmac_ide_pci_match[] = {
  1238. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
  1239. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
  1240. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
  1241. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
  1242. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
  1243. {},
  1244. };
  1245. static struct pci_driver pmac_ide_pci_driver = {
  1246. .name = "ide-pmac",
  1247. .id_table = pmac_ide_pci_match,
  1248. .probe = pmac_ide_pci_attach,
  1249. .suspend = pmac_ide_pci_suspend,
  1250. .resume = pmac_ide_pci_resume,
  1251. };
  1252. MODULE_DEVICE_TABLE(pci, pmac_ide_pci_match);
  1253. int __init pmac_ide_probe(void)
  1254. {
  1255. int error;
  1256. if (!machine_is(powermac))
  1257. return -ENODEV;
  1258. #ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
  1259. error = pci_register_driver(&pmac_ide_pci_driver);
  1260. if (error)
  1261. goto out;
  1262. error = macio_register_driver(&pmac_ide_macio_driver);
  1263. if (error) {
  1264. pci_unregister_driver(&pmac_ide_pci_driver);
  1265. goto out;
  1266. }
  1267. #else
  1268. error = macio_register_driver(&pmac_ide_macio_driver);
  1269. if (error)
  1270. goto out;
  1271. error = pci_register_driver(&pmac_ide_pci_driver);
  1272. if (error) {
  1273. macio_unregister_driver(&pmac_ide_macio_driver);
  1274. goto out;
  1275. }
  1276. #endif
  1277. out:
  1278. return error;
  1279. }
  1280. #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
  1281. /*
  1282. * pmac_ide_build_dmatable builds the DBDMA command list
  1283. * for a transfer and sets the DBDMA channel to point to it.
  1284. */
  1285. static int
  1286. pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq)
  1287. {
  1288. struct dbdma_cmd *table;
  1289. int i, count = 0;
  1290. ide_hwif_t *hwif = HWIF(drive);
  1291. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1292. volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
  1293. struct scatterlist *sg;
  1294. int wr = (rq_data_dir(rq) == WRITE);
  1295. /* DMA table is already aligned */
  1296. table = (struct dbdma_cmd *) pmif->dma_table_cpu;
  1297. /* Make sure DMA controller is stopped (necessary ?) */
  1298. writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma->control);
  1299. while (readl(&dma->status) & RUN)
  1300. udelay(1);
  1301. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  1302. if (!i)
  1303. return 0;
  1304. /* Build DBDMA commands list */
  1305. sg = hwif->sg_table;
  1306. while (i && sg_dma_len(sg)) {
  1307. u32 cur_addr;
  1308. u32 cur_len;
  1309. cur_addr = sg_dma_address(sg);
  1310. cur_len = sg_dma_len(sg);
  1311. if (pmif->broken_dma && cur_addr & (L1_CACHE_BYTES - 1)) {
  1312. if (pmif->broken_dma_warn == 0) {
  1313. printk(KERN_WARNING "%s: DMA on non aligned address, "
  1314. "switching to PIO on Ohare chipset\n", drive->name);
  1315. pmif->broken_dma_warn = 1;
  1316. }
  1317. goto use_pio_instead;
  1318. }
  1319. while (cur_len) {
  1320. unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
  1321. if (count++ >= MAX_DCMDS) {
  1322. printk(KERN_WARNING "%s: DMA table too small\n",
  1323. drive->name);
  1324. goto use_pio_instead;
  1325. }
  1326. st_le16(&table->command, wr? OUTPUT_MORE: INPUT_MORE);
  1327. st_le16(&table->req_count, tc);
  1328. st_le32(&table->phy_addr, cur_addr);
  1329. table->cmd_dep = 0;
  1330. table->xfer_status = 0;
  1331. table->res_count = 0;
  1332. cur_addr += tc;
  1333. cur_len -= tc;
  1334. ++table;
  1335. }
  1336. sg = sg_next(sg);
  1337. i--;
  1338. }
  1339. /* convert the last command to an input/output last command */
  1340. if (count) {
  1341. st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
  1342. /* add the stop command to the end of the list */
  1343. memset(table, 0, sizeof(struct dbdma_cmd));
  1344. st_le16(&table->command, DBDMA_STOP);
  1345. mb();
  1346. writel(hwif->dmatable_dma, &dma->cmdptr);
  1347. return 1;
  1348. }
  1349. printk(KERN_DEBUG "%s: empty DMA table?\n", drive->name);
  1350. use_pio_instead:
  1351. ide_destroy_dmatable(drive);
  1352. return 0; /* revert to PIO for this request */
  1353. }
  1354. /* Teardown mappings after DMA has completed. */
  1355. static void
  1356. pmac_ide_destroy_dmatable (ide_drive_t *drive)
  1357. {
  1358. ide_hwif_t *hwif = drive->hwif;
  1359. if (hwif->sg_nents) {
  1360. ide_destroy_dmatable(drive);
  1361. hwif->sg_nents = 0;
  1362. }
  1363. }
  1364. /*
  1365. * Prepare a DMA transfer. We build the DMA table, adjust the timings for
  1366. * a read on KeyLargo ATA/66 and mark us as waiting for DMA completion
  1367. */
  1368. static int
  1369. pmac_ide_dma_setup(ide_drive_t *drive)
  1370. {
  1371. ide_hwif_t *hwif = HWIF(drive);
  1372. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
  1373. struct request *rq = HWGROUP(drive)->rq;
  1374. u8 unit = (drive->select.b.unit & 0x01);
  1375. u8 ata4;
  1376. if (pmif == NULL)
  1377. return 1;
  1378. ata4 = (pmif->kind == controller_kl_ata4);
  1379. if (!pmac_ide_build_dmatable(drive, rq)) {
  1380. ide_map_sg(drive, rq);
  1381. return 1;
  1382. }
  1383. /* Apple adds 60ns to wrDataSetup on reads */
  1384. if (ata4 && (pmif->timings[unit] & TR_66_UDMA_EN)) {
  1385. writel(pmif->timings[unit] + (!rq_data_dir(rq) ? 0x00800000UL : 0),
  1386. PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1387. (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
  1388. }
  1389. drive->waiting_for_dma = 1;
  1390. return 0;
  1391. }
  1392. static void
  1393. pmac_ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  1394. {
  1395. /* issue cmd to drive */
  1396. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, NULL);
  1397. }
  1398. /*
  1399. * Kick the DMA controller into life after the DMA command has been issued
  1400. * to the drive.
  1401. */
  1402. static void
  1403. pmac_ide_dma_start(ide_drive_t *drive)
  1404. {
  1405. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1406. volatile struct dbdma_regs __iomem *dma;
  1407. dma = pmif->dma_regs;
  1408. writel((RUN << 16) | RUN, &dma->control);
  1409. /* Make sure it gets to the controller right now */
  1410. (void)readl(&dma->control);
  1411. }
  1412. /*
  1413. * After a DMA transfer, make sure the controller is stopped
  1414. */
  1415. static int
  1416. pmac_ide_dma_end (ide_drive_t *drive)
  1417. {
  1418. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1419. volatile struct dbdma_regs __iomem *dma;
  1420. u32 dstat;
  1421. if (pmif == NULL)
  1422. return 0;
  1423. dma = pmif->dma_regs;
  1424. drive->waiting_for_dma = 0;
  1425. dstat = readl(&dma->status);
  1426. writel(((RUN|WAKE|DEAD) << 16), &dma->control);
  1427. pmac_ide_destroy_dmatable(drive);
  1428. /* verify good dma status. we don't check for ACTIVE beeing 0. We should...
  1429. * in theory, but with ATAPI decices doing buffer underruns, that would
  1430. * cause us to disable DMA, which isn't what we want
  1431. */
  1432. return (dstat & (RUN|DEAD)) != RUN;
  1433. }
  1434. /*
  1435. * Check out that the interrupt we got was for us. We can't always know this
  1436. * for sure with those Apple interfaces (well, we could on the recent ones but
  1437. * that's not implemented yet), on the other hand, we don't have shared interrupts
  1438. * so it's not really a problem
  1439. */
  1440. static int
  1441. pmac_ide_dma_test_irq (ide_drive_t *drive)
  1442. {
  1443. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1444. volatile struct dbdma_regs __iomem *dma;
  1445. unsigned long status, timeout;
  1446. if (pmif == NULL)
  1447. return 0;
  1448. dma = pmif->dma_regs;
  1449. /* We have to things to deal with here:
  1450. *
  1451. * - The dbdma won't stop if the command was started
  1452. * but completed with an error without transferring all
  1453. * datas. This happens when bad blocks are met during
  1454. * a multi-block transfer.
  1455. *
  1456. * - The dbdma fifo hasn't yet finished flushing to
  1457. * to system memory when the disk interrupt occurs.
  1458. *
  1459. */
  1460. /* If ACTIVE is cleared, the STOP command have passed and
  1461. * transfer is complete.
  1462. */
  1463. status = readl(&dma->status);
  1464. if (!(status & ACTIVE))
  1465. return 1;
  1466. if (!drive->waiting_for_dma)
  1467. printk(KERN_WARNING "ide%d, ide_dma_test_irq \
  1468. called while not waiting\n", HWIF(drive)->index);
  1469. /* If dbdma didn't execute the STOP command yet, the
  1470. * active bit is still set. We consider that we aren't
  1471. * sharing interrupts (which is hopefully the case with
  1472. * those controllers) and so we just try to flush the
  1473. * channel for pending data in the fifo
  1474. */
  1475. udelay(1);
  1476. writel((FLUSH << 16) | FLUSH, &dma->control);
  1477. timeout = 0;
  1478. for (;;) {
  1479. udelay(1);
  1480. status = readl(&dma->status);
  1481. if ((status & FLUSH) == 0)
  1482. break;
  1483. if (++timeout > 100) {
  1484. printk(KERN_WARNING "ide%d, ide_dma_test_irq \
  1485. timeout flushing channel\n", HWIF(drive)->index);
  1486. break;
  1487. }
  1488. }
  1489. return 1;
  1490. }
  1491. static void pmac_ide_dma_host_set(ide_drive_t *drive, int on)
  1492. {
  1493. }
  1494. static void
  1495. pmac_ide_dma_lost_irq (ide_drive_t *drive)
  1496. {
  1497. pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
  1498. volatile struct dbdma_regs __iomem *dma;
  1499. unsigned long status;
  1500. if (pmif == NULL)
  1501. return;
  1502. dma = pmif->dma_regs;
  1503. status = readl(&dma->status);
  1504. printk(KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
  1505. }
  1506. /*
  1507. * Allocate the data structures needed for using DMA with an interface
  1508. * and fill the proper list of functions pointers
  1509. */
  1510. static void __devinit
  1511. pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif)
  1512. {
  1513. struct pci_dev *dev = to_pci_dev(hwif->dev);
  1514. /* We won't need pci_dev if we switch to generic consistent
  1515. * DMA routines ...
  1516. */
  1517. if (dev == NULL)
  1518. return;
  1519. /*
  1520. * Allocate space for the DBDMA commands.
  1521. * The +2 is +1 for the stop command and +1 to allow for
  1522. * aligning the start address to a multiple of 16 bytes.
  1523. */
  1524. pmif->dma_table_cpu = (struct dbdma_cmd*)pci_alloc_consistent(
  1525. dev,
  1526. (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
  1527. &hwif->dmatable_dma);
  1528. if (pmif->dma_table_cpu == NULL) {
  1529. printk(KERN_ERR "%s: unable to allocate DMA command list\n",
  1530. hwif->name);
  1531. return;
  1532. }
  1533. hwif->sg_max_nents = MAX_DCMDS;
  1534. hwif->dma_host_set = &pmac_ide_dma_host_set;
  1535. hwif->dma_setup = &pmac_ide_dma_setup;
  1536. hwif->dma_exec_cmd = &pmac_ide_dma_exec_cmd;
  1537. hwif->dma_start = &pmac_ide_dma_start;
  1538. hwif->ide_dma_end = &pmac_ide_dma_end;
  1539. hwif->ide_dma_test_irq = &pmac_ide_dma_test_irq;
  1540. hwif->dma_timeout = &ide_dma_timeout;
  1541. hwif->dma_lost_irq = &pmac_ide_dma_lost_irq;
  1542. switch(pmif->kind) {
  1543. case controller_sh_ata6:
  1544. hwif->ultra_mask = pmif->cable_80 ? 0x7f : 0x07;
  1545. hwif->mwdma_mask = 0x07;
  1546. hwif->swdma_mask = 0x00;
  1547. break;
  1548. case controller_un_ata6:
  1549. case controller_k2_ata6:
  1550. hwif->ultra_mask = pmif->cable_80 ? 0x3f : 0x07;
  1551. hwif->mwdma_mask = 0x07;
  1552. hwif->swdma_mask = 0x00;
  1553. break;
  1554. case controller_kl_ata4:
  1555. hwif->ultra_mask = pmif->cable_80 ? 0x1f : 0x07;
  1556. hwif->mwdma_mask = 0x07;
  1557. hwif->swdma_mask = 0x00;
  1558. break;
  1559. default:
  1560. hwif->ultra_mask = 0x00;
  1561. hwif->mwdma_mask = 0x07;
  1562. hwif->swdma_mask = 0x00;
  1563. break;
  1564. }
  1565. }
  1566. #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
  1567. module_init(pmac_ide_probe);