pmac.c 54 KB

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