pata_macio.c 40 KB

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