pata_macio.c 40 KB

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