pmac.c 54 KB

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