pata_macio.c 40 KB

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