pmac.c 45 KB

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