pmac.c 58 KB

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