pata_macio.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * Libata based driver for Apple "macio" family of PATA controllers
  3. *
  4. * Copyright 2008/2009 Benjamin Herrenschmidt, IBM Corp
  5. * <benh@kernel.crashing.org>
  6. *
  7. * Some bits and pieces from drivers/ide/ppc/pmac.c
  8. *
  9. */
  10. #undef DEBUG
  11. #undef DEBUG_DMA
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/ata.h>
  17. #include <linux/libata.h>
  18. #include <linux/adb.h>
  19. #include <linux/pmu.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/of.h>
  22. #include <linux/gfp.h>
  23. #include <linux/pci.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_device.h>
  27. #include <asm/macio.h>
  28. #include <asm/io.h>
  29. #include <asm/dbdma.h>
  30. #include <asm/machdep.h>
  31. #include <asm/pmac_feature.h>
  32. #include <asm/mediabay.h>
  33. #ifdef DEBUG_DMA
  34. #define dev_dbgdma(dev, format, arg...) \
  35. dev_printk(KERN_DEBUG , dev , format , ## arg)
  36. #else
  37. #define dev_dbgdma(dev, format, arg...) \
  38. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  39. #endif
  40. #define DRV_NAME "pata_macio"
  41. #define DRV_VERSION "0.9"
  42. /* Models of macio ATA controller */
  43. enum {
  44. controller_ohare, /* OHare based */
  45. controller_heathrow, /* Heathrow/Paddington */
  46. controller_kl_ata3, /* KeyLargo ATA-3 */
  47. controller_kl_ata4, /* KeyLargo ATA-4 */
  48. controller_un_ata6, /* UniNorth2 ATA-6 */
  49. controller_k2_ata6, /* K2 ATA-6 */
  50. controller_sh_ata6, /* Shasta ATA-6 */
  51. };
  52. static const char* macio_ata_names[] = {
  53. "OHare ATA", /* OHare based */
  54. "Heathrow ATA", /* Heathrow/Paddington */
  55. "KeyLargo ATA-3", /* KeyLargo ATA-3 (MDMA only) */
  56. "KeyLargo ATA-4", /* KeyLargo ATA-4 (UDMA/66) */
  57. "UniNorth ATA-6", /* UniNorth2 ATA-6 (UDMA/100) */
  58. "K2 ATA-6", /* K2 ATA-6 (UDMA/100) */
  59. "Shasta ATA-6", /* Shasta ATA-6 (UDMA/133) */
  60. };
  61. /*
  62. * Extra registers, both 32-bit little-endian
  63. */
  64. #define IDE_TIMING_CONFIG 0x200
  65. #define IDE_INTERRUPT 0x300
  66. /* Kauai (U2) ATA has different register setup */
  67. #define IDE_KAUAI_PIO_CONFIG 0x200
  68. #define IDE_KAUAI_ULTRA_CONFIG 0x210
  69. #define IDE_KAUAI_POLL_CONFIG 0x220
  70. /*
  71. * Timing configuration register definitions
  72. */
  73. /* Number of IDE_SYSCLK_NS ticks, argument is in nanoseconds */
  74. #define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
  75. #define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
  76. #define IDE_SYSCLK_NS 30 /* 33Mhz cell */
  77. #define IDE_SYSCLK_66_NS 15 /* 66Mhz cell */
  78. /* 133Mhz cell, found in shasta.
  79. * See comments about 100 Mhz Uninorth 2...
  80. * Note that PIO_MASK and MDMA_MASK seem to overlap, that's just
  81. * weird and I don't now why .. at this stage
  82. */
  83. #define TR_133_PIOREG_PIO_MASK 0xff000fff
  84. #define TR_133_PIOREG_MDMA_MASK 0x00fff800
  85. #define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
  86. #define TR_133_UDMAREG_UDMA_EN 0x00000001
  87. /* 100Mhz cell, found in Uninorth 2 and K2. It appears as a pci device
  88. * (106b/0033) on uninorth or K2 internal PCI bus and it's clock is
  89. * controlled like gem or fw. It appears to be an evolution of keylargo
  90. * ATA4 with a timing register extended to 2x32bits registers (one
  91. * for PIO & MWDMA and one for UDMA, and a similar DBDMA channel.
  92. * It has it's own local feature control register as well.
  93. *
  94. * After scratching my mind over the timing values, at least for PIO
  95. * and MDMA, I think I've figured the format of the timing register,
  96. * though I use pre-calculated tables for UDMA as usual...
  97. */
  98. #define TR_100_PIO_ADDRSETUP_MASK 0xff000000 /* Size of field unknown */
  99. #define TR_100_PIO_ADDRSETUP_SHIFT 24
  100. #define TR_100_MDMA_MASK 0x00fff000
  101. #define TR_100_MDMA_RECOVERY_MASK 0x00fc0000
  102. #define TR_100_MDMA_RECOVERY_SHIFT 18
  103. #define TR_100_MDMA_ACCESS_MASK 0x0003f000
  104. #define TR_100_MDMA_ACCESS_SHIFT 12
  105. #define TR_100_PIO_MASK 0xff000fff
  106. #define TR_100_PIO_RECOVERY_MASK 0x00000fc0
  107. #define TR_100_PIO_RECOVERY_SHIFT 6
  108. #define TR_100_PIO_ACCESS_MASK 0x0000003f
  109. #define TR_100_PIO_ACCESS_SHIFT 0
  110. #define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
  111. #define TR_100_UDMAREG_UDMA_EN 0x00000001
  112. /* 66Mhz cell, found in KeyLargo. Can do ultra mode 0 to 2 on
  113. * 40 connector cable and to 4 on 80 connector one.
  114. * Clock unit is 15ns (66Mhz)
  115. *
  116. * 3 Values can be programmed:
  117. * - Write data setup, which appears to match the cycle time. They
  118. * also call it DIOW setup.
  119. * - Ready to pause time (from spec)
  120. * - Address setup. That one is weird. I don't see where exactly
  121. * it fits in UDMA cycles, I got it's name from an obscure piece
  122. * of commented out code in Darwin. They leave it to 0, we do as
  123. * well, despite a comment that would lead to think it has a
  124. * min value of 45ns.
  125. * Apple also add 60ns to the write data setup (or cycle time ?) on
  126. * reads.
  127. */
  128. #define TR_66_UDMA_MASK 0xfff00000
  129. #define TR_66_UDMA_EN 0x00100000 /* Enable Ultra mode for DMA */
  130. #define TR_66_PIO_ADDRSETUP_MASK 0xe0000000 /* Address setup */
  131. #define TR_66_PIO_ADDRSETUP_SHIFT 29
  132. #define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000 /* Ready 2 pause time */
  133. #define TR_66_UDMA_RDY2PAUS_SHIFT 25
  134. #define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000 /* Write data setup time */
  135. #define TR_66_UDMA_WRDATASETUP_SHIFT 21
  136. #define TR_66_MDMA_MASK 0x000ffc00
  137. #define TR_66_MDMA_RECOVERY_MASK 0x000f8000
  138. #define TR_66_MDMA_RECOVERY_SHIFT 15
  139. #define TR_66_MDMA_ACCESS_MASK 0x00007c00
  140. #define TR_66_MDMA_ACCESS_SHIFT 10
  141. #define TR_66_PIO_MASK 0xe00003ff
  142. #define TR_66_PIO_RECOVERY_MASK 0x000003e0
  143. #define TR_66_PIO_RECOVERY_SHIFT 5
  144. #define TR_66_PIO_ACCESS_MASK 0x0000001f
  145. #define TR_66_PIO_ACCESS_SHIFT 0
  146. /* 33Mhz cell, found in OHare, Heathrow (& Paddington) and KeyLargo
  147. * Can do pio & mdma modes, clock unit is 30ns (33Mhz)
  148. *
  149. * The access time and recovery time can be programmed. Some older
  150. * Darwin code base limit OHare to 150ns cycle time. I decided to do
  151. * the same here fore safety against broken old hardware ;)
  152. * The HalfTick bit, when set, adds half a clock (15ns) to the access
  153. * time and removes one from recovery. It's not supported on KeyLargo
  154. * implementation afaik. The E bit appears to be set for PIO mode 0 and
  155. * is used to reach long timings used in this mode.
  156. */
  157. #define TR_33_MDMA_MASK 0x003ff800
  158. #define TR_33_MDMA_RECOVERY_MASK 0x001f0000
  159. #define TR_33_MDMA_RECOVERY_SHIFT 16
  160. #define TR_33_MDMA_ACCESS_MASK 0x0000f800
  161. #define TR_33_MDMA_ACCESS_SHIFT 11
  162. #define TR_33_MDMA_HALFTICK 0x00200000
  163. #define TR_33_PIO_MASK 0x000007ff
  164. #define TR_33_PIO_E 0x00000400
  165. #define TR_33_PIO_RECOVERY_MASK 0x000003e0
  166. #define TR_33_PIO_RECOVERY_SHIFT 5
  167. #define TR_33_PIO_ACCESS_MASK 0x0000001f
  168. #define TR_33_PIO_ACCESS_SHIFT 0
  169. /*
  170. * Interrupt register definitions. Only present on newer cells
  171. * (Keylargo and later afaik) so we don't use it.
  172. */
  173. #define IDE_INTR_DMA 0x80000000
  174. #define IDE_INTR_DEVICE 0x40000000
  175. /*
  176. * FCR Register on Kauai. Not sure what bit 0x4 is ...
  177. */
  178. #define KAUAI_FCR_UATA_MAGIC 0x00000004
  179. #define KAUAI_FCR_UATA_RESET_N 0x00000002
  180. #define KAUAI_FCR_UATA_ENABLE 0x00000001
  181. /* Allow up to 256 DBDMA commands per xfer */
  182. #define MAX_DCMDS 256
  183. /* Don't let a DMA segment go all the way to 64K */
  184. #define MAX_DBDMA_SEG 0xff00
  185. /*
  186. * Wait 1s for disk to answer on IDE bus after a hard reset
  187. * of the device (via GPIO/FCR).
  188. *
  189. * Some devices seem to "pollute" the bus even after dropping
  190. * the BSY bit (typically some combo drives slave on the UDMA
  191. * bus) after a hard reset. Since we hard reset all drives on
  192. * KeyLargo ATA66, we have to keep that delay around. I may end
  193. * up not hard resetting anymore on these and keep the delay only
  194. * for older interfaces instead (we have to reset when coming
  195. * from MacOS...) --BenH.
  196. */
  197. #define IDE_WAKEUP_DELAY_MS 1000
  198. struct pata_macio_timing;
  199. struct pata_macio_priv {
  200. int kind;
  201. int aapl_bus_id;
  202. int mediabay : 1;
  203. struct device_node *node;
  204. struct macio_dev *mdev;
  205. struct pci_dev *pdev;
  206. struct device *dev;
  207. int irq;
  208. u32 treg[2][2];
  209. void __iomem *tfregs;
  210. void __iomem *kauai_fcr;
  211. struct dbdma_cmd * dma_table_cpu;
  212. dma_addr_t dma_table_dma;
  213. struct ata_host *host;
  214. const struct pata_macio_timing *timings;
  215. };
  216. /* Previous variants of this driver used to calculate timings
  217. * for various variants of the chip and use tables for others.
  218. *
  219. * Not only was this confusing, but in addition, it isn't clear
  220. * whether our calculation code was correct. It didn't entirely
  221. * match the darwin code and whatever documentation I could find
  222. * on these cells
  223. *
  224. * I decided to entirely rely on a table instead for this version
  225. * of the driver. Also, because I don't really care about derated
  226. * modes and really old HW other than making it work, I'm not going
  227. * to calculate / snoop timing values for something else than the
  228. * standard modes.
  229. */
  230. struct pata_macio_timing {
  231. int mode;
  232. u32 reg1; /* Bits to set in first timing reg */
  233. u32 reg2; /* Bits to set in second timing reg */
  234. };
  235. static const struct pata_macio_timing pata_macio_ohare_timings[] = {
  236. { XFER_PIO_0, 0x00000526, 0, },
  237. { XFER_PIO_1, 0x00000085, 0, },
  238. { XFER_PIO_2, 0x00000025, 0, },
  239. { XFER_PIO_3, 0x00000025, 0, },
  240. { XFER_PIO_4, 0x00000025, 0, },
  241. { XFER_MW_DMA_0, 0x00074000, 0, },
  242. { XFER_MW_DMA_1, 0x00221000, 0, },
  243. { XFER_MW_DMA_2, 0x00211000, 0, },
  244. { -1, 0, 0 }
  245. };
  246. static const struct pata_macio_timing pata_macio_heathrow_timings[] = {
  247. { XFER_PIO_0, 0x00000526, 0, },
  248. { XFER_PIO_1, 0x00000085, 0, },
  249. { XFER_PIO_2, 0x00000025, 0, },
  250. { XFER_PIO_3, 0x00000025, 0, },
  251. { XFER_PIO_4, 0x00000025, 0, },
  252. { XFER_MW_DMA_0, 0x00074000, 0, },
  253. { XFER_MW_DMA_1, 0x00221000, 0, },
  254. { XFER_MW_DMA_2, 0x00211000, 0, },
  255. { -1, 0, 0 }
  256. };
  257. static const struct pata_macio_timing pata_macio_kl33_timings[] = {
  258. { XFER_PIO_0, 0x00000526, 0, },
  259. { XFER_PIO_1, 0x00000085, 0, },
  260. { XFER_PIO_2, 0x00000025, 0, },
  261. { XFER_PIO_3, 0x00000025, 0, },
  262. { XFER_PIO_4, 0x00000025, 0, },
  263. { XFER_MW_DMA_0, 0x00084000, 0, },
  264. { XFER_MW_DMA_1, 0x00021800, 0, },
  265. { XFER_MW_DMA_2, 0x00011800, 0, },
  266. { -1, 0, 0 }
  267. };
  268. static const struct pata_macio_timing pata_macio_kl66_timings[] = {
  269. { XFER_PIO_0, 0x0000038c, 0, },
  270. { XFER_PIO_1, 0x0000020a, 0, },
  271. { XFER_PIO_2, 0x00000127, 0, },
  272. { XFER_PIO_3, 0x000000c6, 0, },
  273. { XFER_PIO_4, 0x00000065, 0, },
  274. { XFER_MW_DMA_0, 0x00084000, 0, },
  275. { XFER_MW_DMA_1, 0x00029800, 0, },
  276. { XFER_MW_DMA_2, 0x00019400, 0, },
  277. { XFER_UDMA_0, 0x19100000, 0, },
  278. { XFER_UDMA_1, 0x14d00000, 0, },
  279. { XFER_UDMA_2, 0x10900000, 0, },
  280. { XFER_UDMA_3, 0x0c700000, 0, },
  281. { XFER_UDMA_4, 0x0c500000, 0, },
  282. { -1, 0, 0 }
  283. };
  284. static const struct pata_macio_timing pata_macio_kauai_timings[] = {
  285. { XFER_PIO_0, 0x08000a92, 0, },
  286. { XFER_PIO_1, 0x0800060f, 0, },
  287. { XFER_PIO_2, 0x0800038b, 0, },
  288. { XFER_PIO_3, 0x05000249, 0, },
  289. { XFER_PIO_4, 0x04000148, 0, },
  290. { XFER_MW_DMA_0, 0x00618000, 0, },
  291. { XFER_MW_DMA_1, 0x00209000, 0, },
  292. { XFER_MW_DMA_2, 0x00148000, 0, },
  293. { XFER_UDMA_0, 0, 0x000070c1, },
  294. { XFER_UDMA_1, 0, 0x00005d81, },
  295. { XFER_UDMA_2, 0, 0x00004a61, },
  296. { XFER_UDMA_3, 0, 0x00003a51, },
  297. { XFER_UDMA_4, 0, 0x00002a31, },
  298. { XFER_UDMA_5, 0, 0x00002921, },
  299. { -1, 0, 0 }
  300. };
  301. static const struct pata_macio_timing pata_macio_shasta_timings[] = {
  302. { XFER_PIO_0, 0x0a000c97, 0, },
  303. { XFER_PIO_1, 0x07000712, 0, },
  304. { XFER_PIO_2, 0x040003cd, 0, },
  305. { XFER_PIO_3, 0x0500028b, 0, },
  306. { XFER_PIO_4, 0x0400010a, 0, },
  307. { XFER_MW_DMA_0, 0x00820800, 0, },
  308. { XFER_MW_DMA_1, 0x0028b000, 0, },
  309. { XFER_MW_DMA_2, 0x001ca000, 0, },
  310. { XFER_UDMA_0, 0, 0x00035901, },
  311. { XFER_UDMA_1, 0, 0x000348b1, },
  312. { XFER_UDMA_2, 0, 0x00033881, },
  313. { XFER_UDMA_3, 0, 0x00033861, },
  314. { XFER_UDMA_4, 0, 0x00033841, },
  315. { XFER_UDMA_5, 0, 0x00033031, },
  316. { XFER_UDMA_6, 0, 0x00033021, },
  317. { -1, 0, 0 }
  318. };
  319. static const struct pata_macio_timing *pata_macio_find_timing(
  320. struct pata_macio_priv *priv,
  321. int mode)
  322. {
  323. int i;
  324. for (i = 0; priv->timings[i].mode > 0; i++) {
  325. if (priv->timings[i].mode == mode)
  326. return &priv->timings[i];
  327. }
  328. return NULL;
  329. }
  330. static void pata_macio_apply_timings(struct ata_port *ap, unsigned int device)
  331. {
  332. struct pata_macio_priv *priv = ap->private_data;
  333. void __iomem *rbase = ap->ioaddr.cmd_addr;
  334. if (priv->kind == controller_sh_ata6 ||
  335. priv->kind == controller_un_ata6 ||
  336. priv->kind == controller_k2_ata6) {
  337. writel(priv->treg[device][0], rbase + IDE_KAUAI_PIO_CONFIG);
  338. writel(priv->treg[device][1], rbase + IDE_KAUAI_ULTRA_CONFIG);
  339. } else
  340. writel(priv->treg[device][0], rbase + IDE_TIMING_CONFIG);
  341. }
  342. static void pata_macio_dev_select(struct ata_port *ap, unsigned int device)
  343. {
  344. ata_sff_dev_select(ap, device);
  345. /* Apply timings */
  346. pata_macio_apply_timings(ap, device);
  347. }
  348. static void pata_macio_set_timings(struct ata_port *ap,
  349. struct ata_device *adev)
  350. {
  351. struct pata_macio_priv *priv = ap->private_data;
  352. const struct pata_macio_timing *t;
  353. dev_dbg(priv->dev, "Set timings: DEV=%d,PIO=0x%x (%s),DMA=0x%x (%s)\n",
  354. adev->devno,
  355. adev->pio_mode,
  356. ata_mode_string(ata_xfer_mode2mask(adev->pio_mode)),
  357. adev->dma_mode,
  358. ata_mode_string(ata_xfer_mode2mask(adev->dma_mode)));
  359. /* First clear timings */
  360. priv->treg[adev->devno][0] = priv->treg[adev->devno][1] = 0;
  361. /* Now get the PIO timings */
  362. t = pata_macio_find_timing(priv, adev->pio_mode);
  363. if (t == NULL) {
  364. dev_warn(priv->dev, "Invalid PIO timing requested: 0x%x\n",
  365. adev->pio_mode);
  366. t = pata_macio_find_timing(priv, XFER_PIO_0);
  367. }
  368. BUG_ON(t == NULL);
  369. /* PIO timings only ever use the first treg */
  370. priv->treg[adev->devno][0] |= t->reg1;
  371. /* Now get DMA timings */
  372. t = pata_macio_find_timing(priv, adev->dma_mode);
  373. if (t == NULL || (t->reg1 == 0 && t->reg2 == 0)) {
  374. dev_dbg(priv->dev, "DMA timing not set yet, using MW_DMA_0\n");
  375. t = pata_macio_find_timing(priv, XFER_MW_DMA_0);
  376. }
  377. BUG_ON(t == NULL);
  378. /* DMA timings can use both tregs */
  379. priv->treg[adev->devno][0] |= t->reg1;
  380. priv->treg[adev->devno][1] |= t->reg2;
  381. dev_dbg(priv->dev, " -> %08x %08x\n",
  382. priv->treg[adev->devno][0],
  383. priv->treg[adev->devno][1]);
  384. /* Apply to hardware */
  385. pata_macio_apply_timings(ap, adev->devno);
  386. }
  387. /*
  388. * Blast some well known "safe" values to the timing registers at init or
  389. * wakeup from sleep time, before we do real calculation
  390. */
  391. static void pata_macio_default_timings(struct pata_macio_priv *priv)
  392. {
  393. unsigned int value, value2 = 0;
  394. switch(priv->kind) {
  395. case controller_sh_ata6:
  396. value = 0x0a820c97;
  397. value2 = 0x00033031;
  398. break;
  399. case controller_un_ata6:
  400. case controller_k2_ata6:
  401. value = 0x08618a92;
  402. value2 = 0x00002921;
  403. break;
  404. case controller_kl_ata4:
  405. value = 0x0008438c;
  406. break;
  407. case controller_kl_ata3:
  408. value = 0x00084526;
  409. break;
  410. case controller_heathrow:
  411. case controller_ohare:
  412. default:
  413. value = 0x00074526;
  414. break;
  415. }
  416. priv->treg[0][0] = priv->treg[1][0] = value;
  417. priv->treg[0][1] = priv->treg[1][1] = value2;
  418. }
  419. static int pata_macio_cable_detect(struct ata_port *ap)
  420. {
  421. struct pata_macio_priv *priv = ap->private_data;
  422. /* Get cable type from device-tree */
  423. if (priv->kind == controller_kl_ata4 ||
  424. priv->kind == controller_un_ata6 ||
  425. priv->kind == controller_k2_ata6 ||
  426. priv->kind == controller_sh_ata6) {
  427. const char* cable = of_get_property(priv->node, "cable-type",
  428. NULL);
  429. struct device_node *root = of_find_node_by_path("/");
  430. const char *model = of_get_property(root, "model", NULL);
  431. if (cable && !strncmp(cable, "80-", 3)) {
  432. /* Some drives fail to detect 80c cable in PowerBook
  433. * These machine use proprietary short IDE cable
  434. * anyway
  435. */
  436. if (!strncmp(model, "PowerBook", 9))
  437. return ATA_CBL_PATA40_SHORT;
  438. else
  439. return ATA_CBL_PATA80;
  440. }
  441. }
  442. /* G5's seem to have incorrect cable type in device-tree.
  443. * Let's assume they always have a 80 conductor cable, this seem to
  444. * be always the case unless the user mucked around
  445. */
  446. if (of_device_is_compatible(priv->node, "K2-UATA") ||
  447. of_device_is_compatible(priv->node, "shasta-ata"))
  448. return ATA_CBL_PATA80;
  449. /* Anything else is 40 connectors */
  450. return ATA_CBL_PATA40;
  451. }
  452. static enum ata_completion_errors pata_macio_qc_prep(struct ata_queued_cmd *qc)
  453. {
  454. unsigned int write = (qc->tf.flags & ATA_TFLAG_WRITE);
  455. struct ata_port *ap = qc->ap;
  456. struct pata_macio_priv *priv = ap->private_data;
  457. struct scatterlist *sg;
  458. struct dbdma_cmd *table;
  459. unsigned int si, pi;
  460. dev_dbgdma(priv->dev, "%s: qc %p flags %lx, write %d dev %d\n",
  461. __func__, qc, qc->flags, write, qc->dev->devno);
  462. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  463. return AC_ERR_OK;
  464. table = (struct dbdma_cmd *) priv->dma_table_cpu;
  465. pi = 0;
  466. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  467. u32 addr, sg_len, len;
  468. /* determine if physical DMA addr spans 64K boundary.
  469. * Note h/w doesn't support 64-bit, so we unconditionally
  470. * truncate dma_addr_t to u32.
  471. */
  472. addr = (u32) sg_dma_address(sg);
  473. sg_len = sg_dma_len(sg);
  474. while (sg_len) {
  475. /* table overflow should never happen */
  476. BUG_ON (pi++ >= MAX_DCMDS);
  477. len = (sg_len < MAX_DBDMA_SEG) ? sg_len : MAX_DBDMA_SEG;
  478. table->command = cpu_to_le16(write ? OUTPUT_MORE: INPUT_MORE);
  479. table->req_count = cpu_to_le16(len);
  480. table->phy_addr = cpu_to_le32(addr);
  481. table->cmd_dep = 0;
  482. table->xfer_status = 0;
  483. table->res_count = 0;
  484. addr += len;
  485. sg_len -= len;
  486. ++table;
  487. }
  488. }
  489. /* Should never happen according to Tejun */
  490. BUG_ON(!pi);
  491. /* Convert the last command to an input/output */
  492. table--;
  493. table->command = cpu_to_le16(write ? OUTPUT_LAST: INPUT_LAST);
  494. table++;
  495. /* Add the stop command to the end of the list */
  496. memset(table, 0, sizeof(struct dbdma_cmd));
  497. table->command = cpu_to_le16(DBDMA_STOP);
  498. dev_dbgdma(priv->dev, "%s: %d DMA list entries\n", __func__, pi);
  499. return AC_ERR_OK;
  500. }
  501. static void pata_macio_freeze(struct ata_port *ap)
  502. {
  503. struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
  504. if (dma_regs) {
  505. unsigned int timeout = 1000000;
  506. /* Make sure DMA controller is stopped */
  507. writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma_regs->control);
  508. while (--timeout && (readl(&dma_regs->status) & RUN))
  509. udelay(1);
  510. }
  511. ata_sff_freeze(ap);
  512. }
  513. static void pata_macio_bmdma_setup(struct ata_queued_cmd *qc)
  514. {
  515. struct ata_port *ap = qc->ap;
  516. struct pata_macio_priv *priv = ap->private_data;
  517. struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
  518. int dev = qc->dev->devno;
  519. dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
  520. /* Make sure DMA commands updates are visible */
  521. writel(priv->dma_table_dma, &dma_regs->cmdptr);
  522. /* On KeyLargo 66Mhz cell, we need to add 60ns to wrDataSetup on
  523. * UDMA reads
  524. */
  525. if (priv->kind == controller_kl_ata4 &&
  526. (priv->treg[dev][0] & TR_66_UDMA_EN)) {
  527. void __iomem *rbase = ap->ioaddr.cmd_addr;
  528. u32 reg = priv->treg[dev][0];
  529. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  530. reg += 0x00800000;
  531. writel(reg, rbase + IDE_TIMING_CONFIG);
  532. }
  533. /* issue r/w command */
  534. ap->ops->sff_exec_command(ap, &qc->tf);
  535. }
  536. static void pata_macio_bmdma_start(struct ata_queued_cmd *qc)
  537. {
  538. struct ata_port *ap = qc->ap;
  539. struct pata_macio_priv *priv = ap->private_data;
  540. struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
  541. dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
  542. writel((RUN << 16) | RUN, &dma_regs->control);
  543. /* Make sure it gets to the controller right now */
  544. (void)readl(&dma_regs->control);
  545. }
  546. static void pata_macio_bmdma_stop(struct ata_queued_cmd *qc)
  547. {
  548. struct ata_port *ap = qc->ap;
  549. struct pata_macio_priv *priv = ap->private_data;
  550. struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
  551. unsigned int timeout = 1000000;
  552. dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
  553. /* Stop the DMA engine and wait for it to full halt */
  554. writel (((RUN|WAKE|DEAD) << 16), &dma_regs->control);
  555. while (--timeout && (readl(&dma_regs->status) & RUN))
  556. udelay(1);
  557. }
  558. static u8 pata_macio_bmdma_status(struct ata_port *ap)
  559. {
  560. struct pata_macio_priv *priv = ap->private_data;
  561. struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
  562. u32 dstat, rstat = ATA_DMA_INTR;
  563. unsigned long timeout = 0;
  564. dstat = readl(&dma_regs->status);
  565. dev_dbgdma(priv->dev, "%s: dstat=%x\n", __func__, dstat);
  566. /* We have two things to deal with here:
  567. *
  568. * - The dbdma won't stop if the command was started
  569. * but completed with an error without transferring all
  570. * datas. This happens when bad blocks are met during
  571. * a multi-block transfer.
  572. *
  573. * - The dbdma fifo hasn't yet finished flushing to
  574. * to system memory when the disk interrupt occurs.
  575. *
  576. */
  577. /* First check for errors */
  578. if ((dstat & (RUN|DEAD)) != RUN)
  579. rstat |= ATA_DMA_ERR;
  580. /* If ACTIVE is cleared, the STOP command has been hit and
  581. * the transfer is complete. If not, we have to flush the
  582. * channel.
  583. */
  584. if ((dstat & ACTIVE) == 0)
  585. return rstat;
  586. dev_dbgdma(priv->dev, "%s: DMA still active, flushing...\n", __func__);
  587. /* If dbdma didn't execute the STOP command yet, the
  588. * active bit is still set. We consider that we aren't
  589. * sharing interrupts (which is hopefully the case with
  590. * those controllers) and so we just try to flush the
  591. * channel for pending data in the fifo
  592. */
  593. udelay(1);
  594. writel((FLUSH << 16) | FLUSH, &dma_regs->control);
  595. for (;;) {
  596. udelay(1);
  597. dstat = readl(&dma_regs->status);
  598. if ((dstat & FLUSH) == 0)
  599. break;
  600. if (++timeout > 1000) {
  601. dev_warn(priv->dev, "timeout flushing DMA\n");
  602. rstat |= ATA_DMA_ERR;
  603. break;
  604. }
  605. }
  606. return rstat;
  607. }
  608. /* port_start is when we allocate the DMA command list */
  609. static int pata_macio_port_start(struct ata_port *ap)
  610. {
  611. struct pata_macio_priv *priv = ap->private_data;
  612. if (ap->ioaddr.bmdma_addr == NULL)
  613. return 0;
  614. /* Allocate space for the DBDMA commands.
  615. *
  616. * The +2 is +1 for the stop command and +1 to allow for
  617. * aligning the start address to a multiple of 16 bytes.
  618. */
  619. priv->dma_table_cpu =
  620. dmam_alloc_coherent(priv->dev,
  621. (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
  622. &priv->dma_table_dma, GFP_KERNEL);
  623. if (priv->dma_table_cpu == NULL) {
  624. dev_err(priv->dev, "Unable to allocate DMA command list\n");
  625. ap->ioaddr.bmdma_addr = NULL;
  626. ap->mwdma_mask = 0;
  627. ap->udma_mask = 0;
  628. }
  629. return 0;
  630. }
  631. static void pata_macio_irq_clear(struct ata_port *ap)
  632. {
  633. struct pata_macio_priv *priv = ap->private_data;
  634. /* Nothing to do here */
  635. dev_dbgdma(priv->dev, "%s\n", __func__);
  636. }
  637. static void pata_macio_reset_hw(struct pata_macio_priv *priv, int resume)
  638. {
  639. dev_dbg(priv->dev, "Enabling & resetting... \n");
  640. if (priv->mediabay)
  641. return;
  642. if (priv->kind == controller_ohare && !resume) {
  643. /* The code below is having trouble on some ohare machines
  644. * (timing related ?). Until I can put my hand on one of these
  645. * units, I keep the old way
  646. */
  647. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, priv->node, 0, 1);
  648. } else {
  649. int rc;
  650. /* Reset and enable controller */
  651. rc = ppc_md.feature_call(PMAC_FTR_IDE_RESET,
  652. priv->node, priv->aapl_bus_id, 1);
  653. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE,
  654. priv->node, priv->aapl_bus_id, 1);
  655. msleep(10);
  656. /* Only bother waiting if there's a reset control */
  657. if (rc == 0) {
  658. ppc_md.feature_call(PMAC_FTR_IDE_RESET,
  659. priv->node, priv->aapl_bus_id, 0);
  660. msleep(IDE_WAKEUP_DELAY_MS);
  661. }
  662. }
  663. /* If resuming a PCI device, restore the config space here */
  664. if (priv->pdev && resume) {
  665. int rc;
  666. pci_restore_state(priv->pdev);
  667. rc = pcim_enable_device(priv->pdev);
  668. if (rc)
  669. dev_err(&priv->pdev->dev,
  670. "Failed to enable device after resume (%d)\n",
  671. rc);
  672. else
  673. pci_set_master(priv->pdev);
  674. }
  675. /* On Kauai, initialize the FCR. We don't perform a reset, doesn't really
  676. * seem necessary and speeds up the boot process
  677. */
  678. if (priv->kauai_fcr)
  679. writel(KAUAI_FCR_UATA_MAGIC |
  680. KAUAI_FCR_UATA_RESET_N |
  681. KAUAI_FCR_UATA_ENABLE, priv->kauai_fcr);
  682. }
  683. /* Hook the standard slave config to fixup some HW related alignment
  684. * restrictions
  685. */
  686. static int pata_macio_slave_config(struct scsi_device *sdev)
  687. {
  688. struct ata_port *ap = ata_shost_to_port(sdev->host);
  689. struct pata_macio_priv *priv = ap->private_data;
  690. struct ata_device *dev;
  691. u16 cmd;
  692. int rc;
  693. /* First call original */
  694. rc = ata_scsi_slave_config(sdev);
  695. if (rc)
  696. return rc;
  697. /* This is lifted from sata_nv */
  698. dev = &ap->link.device[sdev->id];
  699. /* OHare has issues with non cache aligned DMA on some chipsets */
  700. if (priv->kind == controller_ohare) {
  701. blk_queue_update_dma_alignment(sdev->request_queue, 31);
  702. blk_queue_update_dma_pad(sdev->request_queue, 31);
  703. /* Tell the world about it */
  704. ata_dev_info(dev, "OHare alignment limits applied\n");
  705. return 0;
  706. }
  707. /* We only have issues with ATAPI */
  708. if (dev->class != ATA_DEV_ATAPI)
  709. return 0;
  710. /* Shasta and K2 seem to have "issues" with reads ... */
  711. if (priv->kind == controller_sh_ata6 || priv->kind == controller_k2_ata6) {
  712. /* Allright these are bad, apply restrictions */
  713. blk_queue_update_dma_alignment(sdev->request_queue, 15);
  714. blk_queue_update_dma_pad(sdev->request_queue, 15);
  715. /* We enable MWI and hack cache line size directly here, this
  716. * is specific to this chipset and not normal values, we happen
  717. * to somewhat know what we are doing here (which is basically
  718. * to do the same Apple does and pray they did not get it wrong :-)
  719. */
  720. BUG_ON(!priv->pdev);
  721. pci_write_config_byte(priv->pdev, PCI_CACHE_LINE_SIZE, 0x08);
  722. pci_read_config_word(priv->pdev, PCI_COMMAND, &cmd);
  723. pci_write_config_word(priv->pdev, PCI_COMMAND,
  724. cmd | PCI_COMMAND_INVALIDATE);
  725. /* Tell the world about it */
  726. ata_dev_info(dev, "K2/Shasta alignment limits applied\n");
  727. }
  728. return 0;
  729. }
  730. #ifdef CONFIG_PM_SLEEP
  731. static int pata_macio_do_suspend(struct pata_macio_priv *priv, pm_message_t mesg)
  732. {
  733. int rc;
  734. /* First, core libata suspend to do most of the work */
  735. rc = ata_host_suspend(priv->host, mesg);
  736. if (rc)
  737. return rc;
  738. /* Restore to default timings */
  739. pata_macio_default_timings(priv);
  740. /* Mask interrupt. Not strictly necessary but old driver did
  741. * it and I'd rather not change that here */
  742. disable_irq(priv->irq);
  743. /* The media bay will handle itself just fine */
  744. if (priv->mediabay)
  745. return 0;
  746. /* Kauai has bus control FCRs directly here */
  747. if (priv->kauai_fcr) {
  748. u32 fcr = readl(priv->kauai_fcr);
  749. fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
  750. writel(fcr, priv->kauai_fcr);
  751. }
  752. /* For PCI, save state and disable DMA. No need to call
  753. * pci_set_power_state(), the HW doesn't do D states that
  754. * way, the platform code will take care of suspending the
  755. * ASIC properly
  756. */
  757. if (priv->pdev) {
  758. pci_save_state(priv->pdev);
  759. pci_disable_device(priv->pdev);
  760. }
  761. /* Disable the bus on older machines and the cell on kauai */
  762. ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, priv->node,
  763. priv->aapl_bus_id, 0);
  764. return 0;
  765. }
  766. static int pata_macio_do_resume(struct pata_macio_priv *priv)
  767. {
  768. /* Reset and re-enable the HW */
  769. pata_macio_reset_hw(priv, 1);
  770. /* Sanitize drive timings */
  771. pata_macio_apply_timings(priv->host->ports[0], 0);
  772. /* We want our IRQ back ! */
  773. enable_irq(priv->irq);
  774. /* Let the libata core take it from there */
  775. ata_host_resume(priv->host);
  776. return 0;
  777. }
  778. #endif /* CONFIG_PM_SLEEP */
  779. static struct scsi_host_template pata_macio_sht = {
  780. ATA_BASE_SHT(DRV_NAME),
  781. .sg_tablesize = MAX_DCMDS,
  782. /* We may not need that strict one */
  783. .dma_boundary = ATA_DMA_BOUNDARY,
  784. .slave_configure = pata_macio_slave_config,
  785. };
  786. static struct ata_port_operations pata_macio_ops = {
  787. .inherits = &ata_bmdma_port_ops,
  788. .freeze = pata_macio_freeze,
  789. .set_piomode = pata_macio_set_timings,
  790. .set_dmamode = pata_macio_set_timings,
  791. .cable_detect = pata_macio_cable_detect,
  792. .sff_dev_select = pata_macio_dev_select,
  793. .qc_prep = pata_macio_qc_prep,
  794. .bmdma_setup = pata_macio_bmdma_setup,
  795. .bmdma_start = pata_macio_bmdma_start,
  796. .bmdma_stop = pata_macio_bmdma_stop,
  797. .bmdma_status = pata_macio_bmdma_status,
  798. .port_start = pata_macio_port_start,
  799. .sff_irq_clear = pata_macio_irq_clear,
  800. };
  801. static void pata_macio_invariants(struct pata_macio_priv *priv)
  802. {
  803. const int *bidp;
  804. /* Identify the type of controller */
  805. if (of_device_is_compatible(priv->node, "shasta-ata")) {
  806. priv->kind = controller_sh_ata6;
  807. priv->timings = pata_macio_shasta_timings;
  808. } else if (of_device_is_compatible(priv->node, "kauai-ata")) {
  809. priv->kind = controller_un_ata6;
  810. priv->timings = pata_macio_kauai_timings;
  811. } else if (of_device_is_compatible(priv->node, "K2-UATA")) {
  812. priv->kind = controller_k2_ata6;
  813. priv->timings = pata_macio_kauai_timings;
  814. } else if (of_device_is_compatible(priv->node, "keylargo-ata")) {
  815. if (strcmp(priv->node->name, "ata-4") == 0) {
  816. priv->kind = controller_kl_ata4;
  817. priv->timings = pata_macio_kl66_timings;
  818. } else {
  819. priv->kind = controller_kl_ata3;
  820. priv->timings = pata_macio_kl33_timings;
  821. }
  822. } else if (of_device_is_compatible(priv->node, "heathrow-ata")) {
  823. priv->kind = controller_heathrow;
  824. priv->timings = pata_macio_heathrow_timings;
  825. } else {
  826. priv->kind = controller_ohare;
  827. priv->timings = pata_macio_ohare_timings;
  828. }
  829. /* XXX FIXME --- setup priv->mediabay here */
  830. /* Get Apple bus ID (for clock and ASIC control) */
  831. bidp = of_get_property(priv->node, "AAPL,bus-id", NULL);
  832. priv->aapl_bus_id = bidp ? *bidp : 0;
  833. /* Fixup missing Apple bus ID in case of media-bay */
  834. if (priv->mediabay && bidp == 0)
  835. priv->aapl_bus_id = 1;
  836. }
  837. static void pata_macio_setup_ios(struct ata_ioports *ioaddr,
  838. void __iomem * base, void __iomem * dma)
  839. {
  840. /* cmd_addr is the base of regs for that port */
  841. ioaddr->cmd_addr = base;
  842. /* taskfile registers */
  843. ioaddr->data_addr = base + (ATA_REG_DATA << 4);
  844. ioaddr->error_addr = base + (ATA_REG_ERR << 4);
  845. ioaddr->feature_addr = base + (ATA_REG_FEATURE << 4);
  846. ioaddr->nsect_addr = base + (ATA_REG_NSECT << 4);
  847. ioaddr->lbal_addr = base + (ATA_REG_LBAL << 4);
  848. ioaddr->lbam_addr = base + (ATA_REG_LBAM << 4);
  849. ioaddr->lbah_addr = base + (ATA_REG_LBAH << 4);
  850. ioaddr->device_addr = base + (ATA_REG_DEVICE << 4);
  851. ioaddr->status_addr = base + (ATA_REG_STATUS << 4);
  852. ioaddr->command_addr = base + (ATA_REG_CMD << 4);
  853. ioaddr->altstatus_addr = base + 0x160;
  854. ioaddr->ctl_addr = base + 0x160;
  855. ioaddr->bmdma_addr = dma;
  856. }
  857. static void pmac_macio_calc_timing_masks(struct pata_macio_priv *priv,
  858. struct ata_port_info *pinfo)
  859. {
  860. int i = 0;
  861. pinfo->pio_mask = 0;
  862. pinfo->mwdma_mask = 0;
  863. pinfo->udma_mask = 0;
  864. while (priv->timings[i].mode > 0) {
  865. unsigned int mask = 1U << (priv->timings[i].mode & 0x0f);
  866. switch(priv->timings[i].mode & 0xf0) {
  867. case 0x00: /* PIO */
  868. pinfo->pio_mask |= (mask >> 8);
  869. break;
  870. case 0x20: /* MWDMA */
  871. pinfo->mwdma_mask |= mask;
  872. break;
  873. case 0x40: /* UDMA */
  874. pinfo->udma_mask |= mask;
  875. break;
  876. }
  877. i++;
  878. }
  879. dev_dbg(priv->dev, "Supported masks: PIO=%lx, MWDMA=%lx, UDMA=%lx\n",
  880. pinfo->pio_mask, pinfo->mwdma_mask, pinfo->udma_mask);
  881. }
  882. static int pata_macio_common_init(struct pata_macio_priv *priv,
  883. resource_size_t tfregs,
  884. resource_size_t dmaregs,
  885. resource_size_t fcregs,
  886. unsigned long irq)
  887. {
  888. struct ata_port_info pinfo;
  889. const struct ata_port_info *ppi[] = { &pinfo, NULL };
  890. void __iomem *dma_regs = NULL;
  891. /* Fill up privates with various invariants collected from the
  892. * device-tree
  893. */
  894. pata_macio_invariants(priv);
  895. /* Make sure we have sane initial timings in the cache */
  896. pata_macio_default_timings(priv);
  897. /* Not sure what the real max is but we know it's less than 64K, let's
  898. * use 64K minus 256
  899. */
  900. dma_set_max_seg_size(priv->dev, MAX_DBDMA_SEG);
  901. /* Allocate libata host for 1 port */
  902. memset(&pinfo, 0, sizeof(struct ata_port_info));
  903. pmac_macio_calc_timing_masks(priv, &pinfo);
  904. pinfo.flags = ATA_FLAG_SLAVE_POSS;
  905. pinfo.port_ops = &pata_macio_ops;
  906. pinfo.private_data = priv;
  907. priv->host = ata_host_alloc_pinfo(priv->dev, ppi, 1);
  908. if (priv->host == NULL) {
  909. dev_err(priv->dev, "Failed to allocate ATA port structure\n");
  910. return -ENOMEM;
  911. }
  912. /* Setup the private data in host too */
  913. priv->host->private_data = priv;
  914. /* Map base registers */
  915. priv->tfregs = devm_ioremap(priv->dev, tfregs, 0x100);
  916. if (priv->tfregs == NULL) {
  917. dev_err(priv->dev, "Failed to map ATA ports\n");
  918. return -ENOMEM;
  919. }
  920. priv->host->iomap = &priv->tfregs;
  921. /* Map DMA regs */
  922. if (dmaregs != 0) {
  923. dma_regs = devm_ioremap(priv->dev, dmaregs,
  924. sizeof(struct dbdma_regs));
  925. if (dma_regs == NULL)
  926. dev_warn(priv->dev, "Failed to map ATA DMA registers\n");
  927. }
  928. /* If chip has local feature control, map those regs too */
  929. if (fcregs != 0) {
  930. priv->kauai_fcr = devm_ioremap(priv->dev, fcregs, 4);
  931. if (priv->kauai_fcr == NULL) {
  932. dev_err(priv->dev, "Failed to map ATA FCR register\n");
  933. return -ENOMEM;
  934. }
  935. }
  936. /* Setup port data structure */
  937. pata_macio_setup_ios(&priv->host->ports[0]->ioaddr,
  938. priv->tfregs, dma_regs);
  939. priv->host->ports[0]->private_data = priv;
  940. /* hard-reset the controller */
  941. pata_macio_reset_hw(priv, 0);
  942. pata_macio_apply_timings(priv->host->ports[0], 0);
  943. /* Enable bus master if necessary */
  944. if (priv->pdev && dma_regs)
  945. pci_set_master(priv->pdev);
  946. dev_info(priv->dev, "Activating pata-macio chipset %s, Apple bus ID %d\n",
  947. macio_ata_names[priv->kind], priv->aapl_bus_id);
  948. /* Start it up */
  949. priv->irq = irq;
  950. return ata_host_activate(priv->host, irq, ata_bmdma_interrupt, 0,
  951. &pata_macio_sht);
  952. }
  953. static int pata_macio_attach(struct macio_dev *mdev,
  954. const struct of_device_id *match)
  955. {
  956. struct pata_macio_priv *priv;
  957. resource_size_t tfregs, dmaregs = 0;
  958. unsigned long irq;
  959. int rc;
  960. /* Check for broken device-trees */
  961. if (macio_resource_count(mdev) == 0) {
  962. dev_err(&mdev->ofdev.dev,
  963. "No addresses for controller\n");
  964. return -ENXIO;
  965. }
  966. /* Enable managed resources */
  967. macio_enable_devres(mdev);
  968. /* Allocate and init private data structure */
  969. priv = devm_kzalloc(&mdev->ofdev.dev,
  970. sizeof(struct pata_macio_priv), GFP_KERNEL);
  971. if (priv == NULL) {
  972. dev_err(&mdev->ofdev.dev,
  973. "Failed to allocate private memory\n");
  974. return -ENOMEM;
  975. }
  976. priv->node = of_node_get(mdev->ofdev.dev.of_node);
  977. priv->mdev = mdev;
  978. priv->dev = &mdev->ofdev.dev;
  979. /* Request memory resource for taskfile registers */
  980. if (macio_request_resource(mdev, 0, "pata-macio")) {
  981. dev_err(&mdev->ofdev.dev,
  982. "Cannot obtain taskfile resource\n");
  983. return -EBUSY;
  984. }
  985. tfregs = macio_resource_start(mdev, 0);
  986. /* Request resources for DMA registers if any */
  987. if (macio_resource_count(mdev) >= 2) {
  988. if (macio_request_resource(mdev, 1, "pata-macio-dma"))
  989. dev_err(&mdev->ofdev.dev,
  990. "Cannot obtain DMA resource\n");
  991. else
  992. dmaregs = macio_resource_start(mdev, 1);
  993. }
  994. /*
  995. * Fixup missing IRQ for some old implementations with broken
  996. * device-trees.
  997. *
  998. * This is a bit bogus, it should be fixed in the device-tree itself,
  999. * via the existing macio fixups, based on the type of interrupt
  1000. * controller in the machine. However, I have no test HW for this case,
  1001. * and this trick works well enough on those old machines...
  1002. */
  1003. if (macio_irq_count(mdev) == 0) {
  1004. dev_warn(&mdev->ofdev.dev,
  1005. "No interrupts for controller, using 13\n");
  1006. irq = irq_create_mapping(NULL, 13);
  1007. } else
  1008. irq = macio_irq(mdev, 0);
  1009. /* Prevvent media bay callbacks until fully registered */
  1010. lock_media_bay(priv->mdev->media_bay);
  1011. /* Get register addresses and call common initialization */
  1012. rc = pata_macio_common_init(priv,
  1013. tfregs, /* Taskfile regs */
  1014. dmaregs, /* DBDMA regs */
  1015. 0, /* Feature control */
  1016. irq);
  1017. unlock_media_bay(priv->mdev->media_bay);
  1018. return rc;
  1019. }
  1020. static int pata_macio_detach(struct macio_dev *mdev)
  1021. {
  1022. struct ata_host *host = macio_get_drvdata(mdev);
  1023. struct pata_macio_priv *priv = host->private_data;
  1024. lock_media_bay(priv->mdev->media_bay);
  1025. /* Make sure the mediabay callback doesn't try to access
  1026. * dead stuff
  1027. */
  1028. priv->host->private_data = NULL;
  1029. ata_host_detach(host);
  1030. unlock_media_bay(priv->mdev->media_bay);
  1031. return 0;
  1032. }
  1033. #ifdef CONFIG_PM_SLEEP
  1034. static int pata_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
  1035. {
  1036. struct ata_host *host = macio_get_drvdata(mdev);
  1037. return pata_macio_do_suspend(host->private_data, mesg);
  1038. }
  1039. static int pata_macio_resume(struct macio_dev *mdev)
  1040. {
  1041. struct ata_host *host = macio_get_drvdata(mdev);
  1042. return pata_macio_do_resume(host->private_data);
  1043. }
  1044. #endif /* CONFIG_PM_SLEEP */
  1045. #ifdef CONFIG_PMAC_MEDIABAY
  1046. static void pata_macio_mb_event(struct macio_dev* mdev, int mb_state)
  1047. {
  1048. struct ata_host *host = macio_get_drvdata(mdev);
  1049. struct ata_port *ap;
  1050. struct ata_eh_info *ehi;
  1051. struct ata_device *dev;
  1052. unsigned long flags;
  1053. if (!host || !host->private_data)
  1054. return;
  1055. ap = host->ports[0];
  1056. spin_lock_irqsave(ap->lock, flags);
  1057. ehi = &ap->link.eh_info;
  1058. if (mb_state == MB_CD) {
  1059. ata_ehi_push_desc(ehi, "mediabay plug");
  1060. ata_ehi_hotplugged(ehi);
  1061. ata_port_freeze(ap);
  1062. } else {
  1063. ata_ehi_push_desc(ehi, "mediabay unplug");
  1064. ata_for_each_dev(dev, &ap->link, ALL)
  1065. dev->flags |= ATA_DFLAG_DETACH;
  1066. ata_port_abort(ap);
  1067. }
  1068. spin_unlock_irqrestore(ap->lock, flags);
  1069. }
  1070. #endif /* CONFIG_PMAC_MEDIABAY */
  1071. static int pata_macio_pci_attach(struct pci_dev *pdev,
  1072. const struct pci_device_id *id)
  1073. {
  1074. struct pata_macio_priv *priv;
  1075. struct device_node *np;
  1076. resource_size_t rbase;
  1077. /* We cannot use a MacIO controller without its OF device node */
  1078. np = pci_device_to_OF_node(pdev);
  1079. if (np == NULL) {
  1080. dev_err(&pdev->dev,
  1081. "Cannot find OF device node for controller\n");
  1082. return -ENODEV;
  1083. }
  1084. /* Check that it can be enabled */
  1085. if (pcim_enable_device(pdev)) {
  1086. dev_err(&pdev->dev,
  1087. "Cannot enable controller PCI device\n");
  1088. return -ENXIO;
  1089. }
  1090. /* Allocate and init private data structure */
  1091. priv = devm_kzalloc(&pdev->dev,
  1092. sizeof(struct pata_macio_priv), GFP_KERNEL);
  1093. if (priv == NULL) {
  1094. dev_err(&pdev->dev,
  1095. "Failed to allocate private memory\n");
  1096. return -ENOMEM;
  1097. }
  1098. priv->node = of_node_get(np);
  1099. priv->pdev = pdev;
  1100. priv->dev = &pdev->dev;
  1101. /* Get MMIO regions */
  1102. if (pci_request_regions(pdev, "pata-macio")) {
  1103. dev_err(&pdev->dev,
  1104. "Cannot obtain PCI resources\n");
  1105. return -EBUSY;
  1106. }
  1107. /* Get register addresses and call common initialization */
  1108. rbase = pci_resource_start(pdev, 0);
  1109. if (pata_macio_common_init(priv,
  1110. rbase + 0x2000, /* Taskfile regs */
  1111. rbase + 0x1000, /* DBDMA regs */
  1112. rbase, /* Feature control */
  1113. pdev->irq))
  1114. return -ENXIO;
  1115. return 0;
  1116. }
  1117. static void pata_macio_pci_detach(struct pci_dev *pdev)
  1118. {
  1119. struct ata_host *host = pci_get_drvdata(pdev);
  1120. ata_host_detach(host);
  1121. }
  1122. #ifdef CONFIG_PM_SLEEP
  1123. static int pata_macio_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1124. {
  1125. struct ata_host *host = pci_get_drvdata(pdev);
  1126. return pata_macio_do_suspend(host->private_data, mesg);
  1127. }
  1128. static int pata_macio_pci_resume(struct pci_dev *pdev)
  1129. {
  1130. struct ata_host *host = pci_get_drvdata(pdev);
  1131. return pata_macio_do_resume(host->private_data);
  1132. }
  1133. #endif /* CONFIG_PM_SLEEP */
  1134. static const struct of_device_id pata_macio_match[] =
  1135. {
  1136. {
  1137. .name = "IDE",
  1138. },
  1139. {
  1140. .name = "ATA",
  1141. },
  1142. {
  1143. .type = "ide",
  1144. },
  1145. {
  1146. .type = "ata",
  1147. },
  1148. {},
  1149. };
  1150. MODULE_DEVICE_TABLE(of, pata_macio_match);
  1151. static struct macio_driver pata_macio_driver =
  1152. {
  1153. .driver = {
  1154. .name = "pata-macio",
  1155. .owner = THIS_MODULE,
  1156. .of_match_table = pata_macio_match,
  1157. },
  1158. .probe = pata_macio_attach,
  1159. .remove = pata_macio_detach,
  1160. #ifdef CONFIG_PM_SLEEP
  1161. .suspend = pata_macio_suspend,
  1162. .resume = pata_macio_resume,
  1163. #endif
  1164. #ifdef CONFIG_PMAC_MEDIABAY
  1165. .mediabay_event = pata_macio_mb_event,
  1166. #endif
  1167. };
  1168. static const struct pci_device_id pata_macio_pci_match[] = {
  1169. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
  1170. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
  1171. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
  1172. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
  1173. { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
  1174. {},
  1175. };
  1176. static struct pci_driver pata_macio_pci_driver = {
  1177. .name = "pata-pci-macio",
  1178. .id_table = pata_macio_pci_match,
  1179. .probe = pata_macio_pci_attach,
  1180. .remove = pata_macio_pci_detach,
  1181. #ifdef CONFIG_PM_SLEEP
  1182. .suspend = pata_macio_pci_suspend,
  1183. .resume = pata_macio_pci_resume,
  1184. #endif
  1185. .driver = {
  1186. .owner = THIS_MODULE,
  1187. },
  1188. };
  1189. MODULE_DEVICE_TABLE(pci, pata_macio_pci_match);
  1190. static int __init pata_macio_init(void)
  1191. {
  1192. int rc;
  1193. if (!machine_is(powermac))
  1194. return -ENODEV;
  1195. rc = pci_register_driver(&pata_macio_pci_driver);
  1196. if (rc)
  1197. return rc;
  1198. rc = macio_register_driver(&pata_macio_driver);
  1199. if (rc) {
  1200. pci_unregister_driver(&pata_macio_pci_driver);
  1201. return rc;
  1202. }
  1203. return 0;
  1204. }
  1205. static void __exit pata_macio_exit(void)
  1206. {
  1207. macio_unregister_driver(&pata_macio_driver);
  1208. pci_unregister_driver(&pata_macio_pci_driver);
  1209. }
  1210. module_init(pata_macio_init);
  1211. module_exit(pata_macio_exit);
  1212. MODULE_AUTHOR("Benjamin Herrenschmidt");
  1213. MODULE_DESCRIPTION("Apple MacIO PATA driver");
  1214. MODULE_LICENSE("GPL");
  1215. MODULE_VERSION(DRV_VERSION);