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