libata-sff.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * libata-bmdma.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2006 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/libata.h>
  37. #include "libata.h"
  38. /**
  39. * ata_irq_on - Enable interrupts on a port.
  40. * @ap: Port on which interrupts are enabled.
  41. *
  42. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  43. * wait for idle, clear any pending interrupts.
  44. *
  45. * LOCKING:
  46. * Inherited from caller.
  47. */
  48. u8 ata_irq_on(struct ata_port *ap)
  49. {
  50. struct ata_ioports *ioaddr = &ap->ioaddr;
  51. u8 tmp;
  52. ap->ctl &= ~ATA_NIEN;
  53. ap->last_ctl = ap->ctl;
  54. if (ap->flags & ATA_FLAG_MMIO)
  55. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  56. else
  57. outb(ap->ctl, ioaddr->ctl_addr);
  58. tmp = ata_wait_idle(ap);
  59. ap->ops->irq_clear(ap);
  60. return tmp;
  61. }
  62. /**
  63. * ata_tf_load_pio - send taskfile registers to host controller
  64. * @ap: Port to which output is sent
  65. * @tf: ATA taskfile register set
  66. *
  67. * Outputs ATA taskfile to standard ATA host controller.
  68. *
  69. * LOCKING:
  70. * Inherited from caller.
  71. */
  72. static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  73. {
  74. struct ata_ioports *ioaddr = &ap->ioaddr;
  75. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  76. if (tf->ctl != ap->last_ctl) {
  77. outb(tf->ctl, ioaddr->ctl_addr);
  78. ap->last_ctl = tf->ctl;
  79. ata_wait_idle(ap);
  80. }
  81. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  82. outb(tf->hob_feature, ioaddr->feature_addr);
  83. outb(tf->hob_nsect, ioaddr->nsect_addr);
  84. outb(tf->hob_lbal, ioaddr->lbal_addr);
  85. outb(tf->hob_lbam, ioaddr->lbam_addr);
  86. outb(tf->hob_lbah, ioaddr->lbah_addr);
  87. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  88. tf->hob_feature,
  89. tf->hob_nsect,
  90. tf->hob_lbal,
  91. tf->hob_lbam,
  92. tf->hob_lbah);
  93. }
  94. if (is_addr) {
  95. outb(tf->feature, ioaddr->feature_addr);
  96. outb(tf->nsect, ioaddr->nsect_addr);
  97. outb(tf->lbal, ioaddr->lbal_addr);
  98. outb(tf->lbam, ioaddr->lbam_addr);
  99. outb(tf->lbah, ioaddr->lbah_addr);
  100. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  101. tf->feature,
  102. tf->nsect,
  103. tf->lbal,
  104. tf->lbam,
  105. tf->lbah);
  106. }
  107. if (tf->flags & ATA_TFLAG_DEVICE) {
  108. outb(tf->device, ioaddr->device_addr);
  109. VPRINTK("device 0x%X\n", tf->device);
  110. }
  111. ata_wait_idle(ap);
  112. }
  113. /**
  114. * ata_tf_load_mmio - send taskfile registers to host controller
  115. * @ap: Port to which output is sent
  116. * @tf: ATA taskfile register set
  117. *
  118. * Outputs ATA taskfile to standard ATA host controller using MMIO.
  119. *
  120. * LOCKING:
  121. * Inherited from caller.
  122. */
  123. static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  124. {
  125. struct ata_ioports *ioaddr = &ap->ioaddr;
  126. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  127. if (tf->ctl != ap->last_ctl) {
  128. writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  129. ap->last_ctl = tf->ctl;
  130. ata_wait_idle(ap);
  131. }
  132. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  133. writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
  134. writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
  135. writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
  136. writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
  137. writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
  138. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  139. tf->hob_feature,
  140. tf->hob_nsect,
  141. tf->hob_lbal,
  142. tf->hob_lbam,
  143. tf->hob_lbah);
  144. }
  145. if (is_addr) {
  146. writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
  147. writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
  148. writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
  149. writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
  150. writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
  151. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  152. tf->feature,
  153. tf->nsect,
  154. tf->lbal,
  155. tf->lbam,
  156. tf->lbah);
  157. }
  158. if (tf->flags & ATA_TFLAG_DEVICE) {
  159. writeb(tf->device, (void __iomem *) ioaddr->device_addr);
  160. VPRINTK("device 0x%X\n", tf->device);
  161. }
  162. ata_wait_idle(ap);
  163. }
  164. /**
  165. * ata_tf_load - send taskfile registers to host controller
  166. * @ap: Port to which output is sent
  167. * @tf: ATA taskfile register set
  168. *
  169. * Outputs ATA taskfile to standard ATA host controller using MMIO
  170. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  171. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  172. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  173. * hob_lbal, hob_lbam, and hob_lbah.
  174. *
  175. * This function waits for idle (!BUSY and !DRQ) after writing
  176. * registers. If the control register has a new value, this
  177. * function also waits for idle after writing control and before
  178. * writing the remaining registers.
  179. *
  180. * May be used as the tf_load() entry in ata_port_operations.
  181. *
  182. * LOCKING:
  183. * Inherited from caller.
  184. */
  185. void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  186. {
  187. if (ap->flags & ATA_FLAG_MMIO)
  188. ata_tf_load_mmio(ap, tf);
  189. else
  190. ata_tf_load_pio(ap, tf);
  191. }
  192. /**
  193. * ata_exec_command_pio - issue ATA command to host controller
  194. * @ap: port to which command is being issued
  195. * @tf: ATA taskfile register set
  196. *
  197. * Issues PIO write to ATA command register, with proper
  198. * synchronization with interrupt handler / other threads.
  199. *
  200. * LOCKING:
  201. * spin_lock_irqsave(host lock)
  202. */
  203. static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  204. {
  205. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  206. outb(tf->command, ap->ioaddr.command_addr);
  207. ata_pause(ap);
  208. }
  209. /**
  210. * ata_exec_command_mmio - issue ATA command to host controller
  211. * @ap: port to which command is being issued
  212. * @tf: ATA taskfile register set
  213. *
  214. * Issues MMIO write to ATA command register, with proper
  215. * synchronization with interrupt handler / other threads.
  216. *
  217. * FIXME: missing write posting for 400nS delay enforcement
  218. *
  219. * LOCKING:
  220. * spin_lock_irqsave(host lock)
  221. */
  222. static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  223. {
  224. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  225. writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
  226. ata_pause(ap);
  227. }
  228. /**
  229. * ata_exec_command - issue ATA command to host controller
  230. * @ap: port to which command is being issued
  231. * @tf: ATA taskfile register set
  232. *
  233. * Issues PIO/MMIO write to ATA command register, with proper
  234. * synchronization with interrupt handler / other threads.
  235. *
  236. * LOCKING:
  237. * spin_lock_irqsave(host lock)
  238. */
  239. void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  240. {
  241. if (ap->flags & ATA_FLAG_MMIO)
  242. ata_exec_command_mmio(ap, tf);
  243. else
  244. ata_exec_command_pio(ap, tf);
  245. }
  246. /**
  247. * ata_tf_read_pio - input device's ATA taskfile shadow registers
  248. * @ap: Port from which input is read
  249. * @tf: ATA taskfile register set for storing input
  250. *
  251. * Reads ATA taskfile registers for currently-selected device
  252. * into @tf.
  253. *
  254. * LOCKING:
  255. * Inherited from caller.
  256. */
  257. static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
  258. {
  259. struct ata_ioports *ioaddr = &ap->ioaddr;
  260. tf->command = ata_check_status(ap);
  261. tf->feature = inb(ioaddr->error_addr);
  262. tf->nsect = inb(ioaddr->nsect_addr);
  263. tf->lbal = inb(ioaddr->lbal_addr);
  264. tf->lbam = inb(ioaddr->lbam_addr);
  265. tf->lbah = inb(ioaddr->lbah_addr);
  266. tf->device = inb(ioaddr->device_addr);
  267. if (tf->flags & ATA_TFLAG_LBA48) {
  268. outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  269. tf->hob_feature = inb(ioaddr->error_addr);
  270. tf->hob_nsect = inb(ioaddr->nsect_addr);
  271. tf->hob_lbal = inb(ioaddr->lbal_addr);
  272. tf->hob_lbam = inb(ioaddr->lbam_addr);
  273. tf->hob_lbah = inb(ioaddr->lbah_addr);
  274. }
  275. }
  276. /**
  277. * ata_tf_read_mmio - input device's ATA taskfile shadow registers
  278. * @ap: Port from which input is read
  279. * @tf: ATA taskfile register set for storing input
  280. *
  281. * Reads ATA taskfile registers for currently-selected device
  282. * into @tf via MMIO.
  283. *
  284. * LOCKING:
  285. * Inherited from caller.
  286. */
  287. static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
  288. {
  289. struct ata_ioports *ioaddr = &ap->ioaddr;
  290. tf->command = ata_check_status(ap);
  291. tf->feature = readb((void __iomem *)ioaddr->error_addr);
  292. tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
  293. tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
  294. tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
  295. tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
  296. tf->device = readb((void __iomem *)ioaddr->device_addr);
  297. if (tf->flags & ATA_TFLAG_LBA48) {
  298. writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
  299. tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
  300. tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
  301. tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
  302. tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
  303. tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
  304. }
  305. }
  306. /**
  307. * ata_tf_read - input device's ATA taskfile shadow registers
  308. * @ap: Port from which input is read
  309. * @tf: ATA taskfile register set for storing input
  310. *
  311. * Reads ATA taskfile registers for currently-selected device
  312. * into @tf.
  313. *
  314. * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
  315. * is set, also reads the hob registers.
  316. *
  317. * May be used as the tf_read() entry in ata_port_operations.
  318. *
  319. * LOCKING:
  320. * Inherited from caller.
  321. */
  322. void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  323. {
  324. if (ap->flags & ATA_FLAG_MMIO)
  325. ata_tf_read_mmio(ap, tf);
  326. else
  327. ata_tf_read_pio(ap, tf);
  328. }
  329. /**
  330. * ata_check_status_pio - Read device status reg & clear interrupt
  331. * @ap: port where the device is
  332. *
  333. * Reads ATA taskfile status register for currently-selected device
  334. * and return its value. This also clears pending interrupts
  335. * from this device
  336. *
  337. * LOCKING:
  338. * Inherited from caller.
  339. */
  340. static u8 ata_check_status_pio(struct ata_port *ap)
  341. {
  342. return inb(ap->ioaddr.status_addr);
  343. }
  344. /**
  345. * ata_check_status_mmio - Read device status reg & clear interrupt
  346. * @ap: port where the device is
  347. *
  348. * Reads ATA taskfile status register for currently-selected device
  349. * via MMIO and return its value. This also clears pending interrupts
  350. * from this device
  351. *
  352. * LOCKING:
  353. * Inherited from caller.
  354. */
  355. static u8 ata_check_status_mmio(struct ata_port *ap)
  356. {
  357. return readb((void __iomem *) ap->ioaddr.status_addr);
  358. }
  359. /**
  360. * ata_check_status - Read device status reg & clear interrupt
  361. * @ap: port where the device is
  362. *
  363. * Reads ATA taskfile status register for currently-selected device
  364. * and return its value. This also clears pending interrupts
  365. * from this device
  366. *
  367. * May be used as the check_status() entry in ata_port_operations.
  368. *
  369. * LOCKING:
  370. * Inherited from caller.
  371. */
  372. u8 ata_check_status(struct ata_port *ap)
  373. {
  374. if (ap->flags & ATA_FLAG_MMIO)
  375. return ata_check_status_mmio(ap);
  376. return ata_check_status_pio(ap);
  377. }
  378. /**
  379. * ata_altstatus - Read device alternate status reg
  380. * @ap: port where the device is
  381. *
  382. * Reads ATA taskfile alternate status register for
  383. * currently-selected device and return its value.
  384. *
  385. * Note: may NOT be used as the check_altstatus() entry in
  386. * ata_port_operations.
  387. *
  388. * LOCKING:
  389. * Inherited from caller.
  390. */
  391. u8 ata_altstatus(struct ata_port *ap)
  392. {
  393. if (ap->ops->check_altstatus)
  394. return ap->ops->check_altstatus(ap);
  395. if (ap->flags & ATA_FLAG_MMIO)
  396. return readb((void __iomem *)ap->ioaddr.altstatus_addr);
  397. return inb(ap->ioaddr.altstatus_addr);
  398. }
  399. /**
  400. * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
  401. * @qc: Info associated with this ATA transaction.
  402. *
  403. * LOCKING:
  404. * spin_lock_irqsave(host lock)
  405. */
  406. static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
  407. {
  408. struct ata_port *ap = qc->ap;
  409. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  410. u8 dmactl;
  411. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  412. /* load PRD table addr. */
  413. mb(); /* make sure PRD table writes are visible to controller */
  414. writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
  415. /* specify data direction, triple-check start bit is clear */
  416. dmactl = readb(mmio + ATA_DMA_CMD);
  417. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  418. if (!rw)
  419. dmactl |= ATA_DMA_WR;
  420. writeb(dmactl, mmio + ATA_DMA_CMD);
  421. /* issue r/w command */
  422. ap->ops->exec_command(ap, &qc->tf);
  423. }
  424. /**
  425. * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
  426. * @qc: Info associated with this ATA transaction.
  427. *
  428. * LOCKING:
  429. * spin_lock_irqsave(host lock)
  430. */
  431. static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
  432. {
  433. struct ata_port *ap = qc->ap;
  434. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  435. u8 dmactl;
  436. /* start host DMA transaction */
  437. dmactl = readb(mmio + ATA_DMA_CMD);
  438. writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
  439. /* Strictly, one may wish to issue a readb() here, to
  440. * flush the mmio write. However, control also passes
  441. * to the hardware at this point, and it will interrupt
  442. * us when we are to resume control. So, in effect,
  443. * we don't care when the mmio write flushes.
  444. * Further, a read of the DMA status register _immediately_
  445. * following the write may not be what certain flaky hardware
  446. * is expected, so I think it is best to not add a readb()
  447. * without first all the MMIO ATA cards/mobos.
  448. * Or maybe I'm just being paranoid.
  449. */
  450. }
  451. /**
  452. * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
  453. * @qc: Info associated with this ATA transaction.
  454. *
  455. * LOCKING:
  456. * spin_lock_irqsave(host lock)
  457. */
  458. static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
  459. {
  460. struct ata_port *ap = qc->ap;
  461. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  462. u8 dmactl;
  463. /* load PRD table addr. */
  464. outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  465. /* specify data direction, triple-check start bit is clear */
  466. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  467. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  468. if (!rw)
  469. dmactl |= ATA_DMA_WR;
  470. outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  471. /* issue r/w command */
  472. ap->ops->exec_command(ap, &qc->tf);
  473. }
  474. /**
  475. * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
  476. * @qc: Info associated with this ATA transaction.
  477. *
  478. * LOCKING:
  479. * spin_lock_irqsave(host lock)
  480. */
  481. static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
  482. {
  483. struct ata_port *ap = qc->ap;
  484. u8 dmactl;
  485. /* start host DMA transaction */
  486. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  487. outb(dmactl | ATA_DMA_START,
  488. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  489. }
  490. /**
  491. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  492. * @qc: Info associated with this ATA transaction.
  493. *
  494. * Writes the ATA_DMA_START flag to the DMA command register.
  495. *
  496. * May be used as the bmdma_start() entry in ata_port_operations.
  497. *
  498. * LOCKING:
  499. * spin_lock_irqsave(host lock)
  500. */
  501. void ata_bmdma_start(struct ata_queued_cmd *qc)
  502. {
  503. if (qc->ap->flags & ATA_FLAG_MMIO)
  504. ata_bmdma_start_mmio(qc);
  505. else
  506. ata_bmdma_start_pio(qc);
  507. }
  508. /**
  509. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  510. * @qc: Info associated with this ATA transaction.
  511. *
  512. * Writes address of PRD table to device's PRD Table Address
  513. * register, sets the DMA control register, and calls
  514. * ops->exec_command() to start the transfer.
  515. *
  516. * May be used as the bmdma_setup() entry in ata_port_operations.
  517. *
  518. * LOCKING:
  519. * spin_lock_irqsave(host lock)
  520. */
  521. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  522. {
  523. if (qc->ap->flags & ATA_FLAG_MMIO)
  524. ata_bmdma_setup_mmio(qc);
  525. else
  526. ata_bmdma_setup_pio(qc);
  527. }
  528. /**
  529. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  530. * @ap: Port associated with this ATA transaction.
  531. *
  532. * Clear interrupt and error flags in DMA status register.
  533. *
  534. * May be used as the irq_clear() entry in ata_port_operations.
  535. *
  536. * LOCKING:
  537. * spin_lock_irqsave(host lock)
  538. */
  539. void ata_bmdma_irq_clear(struct ata_port *ap)
  540. {
  541. if (!ap->ioaddr.bmdma_addr)
  542. return;
  543. if (ap->flags & ATA_FLAG_MMIO) {
  544. void __iomem *mmio =
  545. ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
  546. writeb(readb(mmio), mmio);
  547. } else {
  548. unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
  549. outb(inb(addr), addr);
  550. }
  551. }
  552. /**
  553. * ata_bmdma_status - Read PCI IDE BMDMA status
  554. * @ap: Port associated with this ATA transaction.
  555. *
  556. * Read and return BMDMA status register.
  557. *
  558. * May be used as the bmdma_status() entry in ata_port_operations.
  559. *
  560. * LOCKING:
  561. * spin_lock_irqsave(host lock)
  562. */
  563. u8 ata_bmdma_status(struct ata_port *ap)
  564. {
  565. u8 host_stat;
  566. if (ap->flags & ATA_FLAG_MMIO) {
  567. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  568. host_stat = readb(mmio + ATA_DMA_STATUS);
  569. } else
  570. host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  571. return host_stat;
  572. }
  573. /**
  574. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  575. * @qc: Command we are ending DMA for
  576. *
  577. * Clears the ATA_DMA_START flag in the dma control register
  578. *
  579. * May be used as the bmdma_stop() entry in ata_port_operations.
  580. *
  581. * LOCKING:
  582. * spin_lock_irqsave(host lock)
  583. */
  584. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  585. {
  586. struct ata_port *ap = qc->ap;
  587. if (ap->flags & ATA_FLAG_MMIO) {
  588. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  589. /* clear start/stop bit */
  590. writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  591. mmio + ATA_DMA_CMD);
  592. } else {
  593. /* clear start/stop bit */
  594. outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
  595. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  596. }
  597. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  598. ata_altstatus(ap); /* dummy read */
  599. }
  600. /**
  601. * ata_bmdma_freeze - Freeze BMDMA controller port
  602. * @ap: port to freeze
  603. *
  604. * Freeze BMDMA controller port.
  605. *
  606. * LOCKING:
  607. * Inherited from caller.
  608. */
  609. void ata_bmdma_freeze(struct ata_port *ap)
  610. {
  611. struct ata_ioports *ioaddr = &ap->ioaddr;
  612. ap->ctl |= ATA_NIEN;
  613. ap->last_ctl = ap->ctl;
  614. if (ap->flags & ATA_FLAG_MMIO)
  615. writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
  616. else
  617. outb(ap->ctl, ioaddr->ctl_addr);
  618. }
  619. /**
  620. * ata_bmdma_thaw - Thaw BMDMA controller port
  621. * @ap: port to thaw
  622. *
  623. * Thaw BMDMA controller port.
  624. *
  625. * LOCKING:
  626. * Inherited from caller.
  627. */
  628. void ata_bmdma_thaw(struct ata_port *ap)
  629. {
  630. /* clear & re-enable interrupts */
  631. ata_chk_status(ap);
  632. ap->ops->irq_clear(ap);
  633. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  634. ata_irq_on(ap);
  635. }
  636. /**
  637. * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
  638. * @ap: port to handle error for
  639. * @prereset: prereset method (can be NULL)
  640. * @softreset: softreset method (can be NULL)
  641. * @hardreset: hardreset method (can be NULL)
  642. * @postreset: postreset method (can be NULL)
  643. *
  644. * Handle error for ATA BMDMA controller. It can handle both
  645. * PATA and SATA controllers. Many controllers should be able to
  646. * use this EH as-is or with some added handling before and
  647. * after.
  648. *
  649. * This function is intended to be used for constructing
  650. * ->error_handler callback by low level drivers.
  651. *
  652. * LOCKING:
  653. * Kernel thread context (may sleep)
  654. */
  655. void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  656. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  657. ata_postreset_fn_t postreset)
  658. {
  659. struct ata_eh_context *ehc = &ap->eh_context;
  660. struct ata_queued_cmd *qc;
  661. unsigned long flags;
  662. int thaw = 0;
  663. qc = __ata_qc_from_tag(ap, ap->active_tag);
  664. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  665. qc = NULL;
  666. /* reset PIO HSM and stop DMA engine */
  667. spin_lock_irqsave(ap->lock, flags);
  668. ap->hsm_task_state = HSM_ST_IDLE;
  669. if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
  670. qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
  671. u8 host_stat;
  672. host_stat = ap->ops->bmdma_status(ap);
  673. ata_ehi_push_desc(&ehc->i, "BMDMA stat 0x%x", host_stat);
  674. /* BMDMA controllers indicate host bus error by
  675. * setting DMA_ERR bit and timing out. As it wasn't
  676. * really a timeout event, adjust error mask and
  677. * cancel frozen state.
  678. */
  679. if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
  680. qc->err_mask = AC_ERR_HOST_BUS;
  681. thaw = 1;
  682. }
  683. ap->ops->bmdma_stop(qc);
  684. }
  685. ata_altstatus(ap);
  686. ata_chk_status(ap);
  687. ap->ops->irq_clear(ap);
  688. spin_unlock_irqrestore(ap->lock, flags);
  689. if (thaw)
  690. ata_eh_thaw_port(ap);
  691. /* PIO and DMA engines have been stopped, perform recovery */
  692. ata_do_eh(ap, prereset, softreset, hardreset, postreset);
  693. }
  694. /**
  695. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  696. * @ap: port to handle error for
  697. *
  698. * Stock error handler for BMDMA controller.
  699. *
  700. * LOCKING:
  701. * Kernel thread context (may sleep)
  702. */
  703. void ata_bmdma_error_handler(struct ata_port *ap)
  704. {
  705. ata_reset_fn_t hardreset;
  706. hardreset = NULL;
  707. if (sata_scr_valid(ap))
  708. hardreset = sata_std_hardreset;
  709. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  710. ata_std_postreset);
  711. }
  712. /**
  713. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
  714. * BMDMA controller
  715. * @qc: internal command to clean up
  716. *
  717. * LOCKING:
  718. * Kernel thread context (may sleep)
  719. */
  720. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  721. {
  722. ata_bmdma_stop(qc);
  723. }
  724. #ifdef CONFIG_PCI
  725. /**
  726. * ata_pci_init_native_mode - Initialize native-mode driver
  727. * @pdev: pci device to be initialized
  728. * @port: array[2] of pointers to port info structures.
  729. * @ports: bitmap of ports present
  730. *
  731. * Utility function which allocates and initializes an
  732. * ata_probe_ent structure for a standard dual-port
  733. * PIO-based IDE controller. The returned ata_probe_ent
  734. * structure can be passed to ata_device_add(). The returned
  735. * ata_probe_ent structure should then be freed with kfree().
  736. *
  737. * The caller need only pass the address of the primary port, the
  738. * secondary will be deduced automatically. If the device has non
  739. * standard secondary port mappings this function can be called twice,
  740. * once for each interface.
  741. */
  742. struct ata_probe_ent *
  743. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  744. {
  745. struct ata_probe_ent *probe_ent =
  746. ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  747. int p = 0;
  748. unsigned long bmdma;
  749. if (!probe_ent)
  750. return NULL;
  751. probe_ent->irq = pdev->irq;
  752. probe_ent->irq_flags = IRQF_SHARED;
  753. if (ports & ATA_PORT_PRIMARY) {
  754. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
  755. probe_ent->port[p].altstatus_addr =
  756. probe_ent->port[p].ctl_addr =
  757. pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
  758. bmdma = pci_resource_start(pdev, 4);
  759. if (bmdma) {
  760. if (inb(bmdma + 2) & 0x80)
  761. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  762. probe_ent->port[p].bmdma_addr = bmdma;
  763. }
  764. ata_std_ports(&probe_ent->port[p]);
  765. p++;
  766. }
  767. if (ports & ATA_PORT_SECONDARY) {
  768. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
  769. probe_ent->port[p].altstatus_addr =
  770. probe_ent->port[p].ctl_addr =
  771. pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
  772. bmdma = pci_resource_start(pdev, 4);
  773. if (bmdma) {
  774. bmdma += 8;
  775. if(inb(bmdma + 2) & 0x80)
  776. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  777. probe_ent->port[p].bmdma_addr = bmdma;
  778. }
  779. ata_std_ports(&probe_ent->port[p]);
  780. probe_ent->pinfo2 = port[1];
  781. p++;
  782. }
  783. probe_ent->n_ports = p;
  784. return probe_ent;
  785. }
  786. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  787. struct ata_port_info **port, int port_mask)
  788. {
  789. struct ata_probe_ent *probe_ent;
  790. unsigned long bmdma = pci_resource_start(pdev, 4);
  791. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  792. if (!probe_ent)
  793. return NULL;
  794. probe_ent->n_ports = 2;
  795. if (port_mask & ATA_PORT_PRIMARY) {
  796. probe_ent->irq = ATA_PRIMARY_IRQ;
  797. probe_ent->port[0].cmd_addr = ATA_PRIMARY_CMD;
  798. probe_ent->port[0].altstatus_addr =
  799. probe_ent->port[0].ctl_addr = ATA_PRIMARY_CTL;
  800. if (bmdma) {
  801. probe_ent->port[0].bmdma_addr = bmdma;
  802. if (inb(bmdma + 2) & 0x80)
  803. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  804. }
  805. ata_std_ports(&probe_ent->port[0]);
  806. } else
  807. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  808. if (port_mask & ATA_PORT_SECONDARY) {
  809. if (probe_ent->irq)
  810. probe_ent->irq2 = ATA_SECONDARY_IRQ;
  811. else
  812. probe_ent->irq = ATA_SECONDARY_IRQ;
  813. probe_ent->port[1].cmd_addr = ATA_SECONDARY_CMD;
  814. probe_ent->port[1].altstatus_addr =
  815. probe_ent->port[1].ctl_addr = ATA_SECONDARY_CTL;
  816. if (bmdma) {
  817. probe_ent->port[1].bmdma_addr = bmdma + 8;
  818. if (inb(bmdma + 10) & 0x80)
  819. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  820. }
  821. ata_std_ports(&probe_ent->port[1]);
  822. /* FIXME: could be pointing to stack area; must copy */
  823. probe_ent->pinfo2 = port[1];
  824. } else
  825. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  826. return probe_ent;
  827. }
  828. /**
  829. * ata_pci_init_one - Initialize/register PCI IDE host controller
  830. * @pdev: Controller to be initialized
  831. * @port_info: Information from low-level host driver
  832. * @n_ports: Number of ports attached to host controller
  833. *
  834. * This is a helper function which can be called from a driver's
  835. * xxx_init_one() probe function if the hardware uses traditional
  836. * IDE taskfile registers.
  837. *
  838. * This function calls pci_enable_device(), reserves its register
  839. * regions, sets the dma mask, enables bus master mode, and calls
  840. * ata_device_add()
  841. *
  842. * ASSUMPTION:
  843. * Nobody makes a single channel controller that appears solely as
  844. * the secondary legacy port on PCI.
  845. *
  846. * LOCKING:
  847. * Inherited from PCI layer (may sleep).
  848. *
  849. * RETURNS:
  850. * Zero on success, negative on errno-based value on error.
  851. */
  852. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  853. unsigned int n_ports)
  854. {
  855. struct ata_probe_ent *probe_ent = NULL;
  856. struct ata_port_info *port[2];
  857. u8 mask;
  858. unsigned int legacy_mode = 0;
  859. int disable_dev_on_err = 1;
  860. int rc;
  861. DPRINTK("ENTER\n");
  862. BUG_ON(n_ports < 1 || n_ports > 2);
  863. port[0] = port_info[0];
  864. if (n_ports > 1)
  865. port[1] = port_info[1];
  866. else
  867. port[1] = port[0];
  868. /* FIXME: Really for ATA it isn't safe because the device may be
  869. multi-purpose and we want to leave it alone if it was already
  870. enabled. Secondly for shared use as Arjan says we want refcounting
  871. Checking dev->is_enabled is insufficient as this is not set at
  872. boot for the primary video which is BIOS enabled
  873. */
  874. rc = pci_enable_device(pdev);
  875. if (rc)
  876. return rc;
  877. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  878. u8 tmp8;
  879. /* TODO: What if one channel is in native mode ... */
  880. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  881. mask = (1 << 2) | (1 << 0);
  882. if ((tmp8 & mask) != mask)
  883. legacy_mode = (1 << 3);
  884. #if defined(CONFIG_NO_ATA_LEGACY)
  885. /* Some platforms with PCI limits cannot address compat
  886. port space. In that case we punt if their firmware has
  887. left a device in compatibility mode */
  888. if (legacy_mode) {
  889. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  890. return -EOPNOTSUPP;
  891. }
  892. #endif
  893. }
  894. rc = pci_request_regions(pdev, DRV_NAME);
  895. if (rc) {
  896. disable_dev_on_err = 0;
  897. goto err_out;
  898. }
  899. if (legacy_mode) {
  900. if (!request_region(ATA_PRIMARY_CMD, 8, "libata")) {
  901. struct resource *conflict, res;
  902. res.start = ATA_PRIMARY_CMD;
  903. res.end = ATA_PRIMARY_CMD + 8 - 1;
  904. conflict = ____request_resource(&ioport_resource, &res);
  905. while (conflict->child)
  906. conflict = ____request_resource(conflict, &res);
  907. if (!strcmp(conflict->name, "libata"))
  908. legacy_mode |= ATA_PORT_PRIMARY;
  909. else {
  910. disable_dev_on_err = 0;
  911. printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
  912. "ata: conflict with %s\n",
  913. ATA_PRIMARY_CMD,
  914. conflict->name);
  915. }
  916. } else
  917. legacy_mode |= ATA_PORT_PRIMARY;
  918. if (!request_region(ATA_SECONDARY_CMD, 8, "libata")) {
  919. struct resource *conflict, res;
  920. res.start = ATA_SECONDARY_CMD;
  921. res.end = ATA_SECONDARY_CMD + 8 - 1;
  922. conflict = ____request_resource(&ioport_resource, &res);
  923. while (conflict->child)
  924. conflict = ____request_resource(conflict, &res);
  925. if (!strcmp(conflict->name, "libata"))
  926. legacy_mode |= ATA_PORT_SECONDARY;
  927. else {
  928. disable_dev_on_err = 0;
  929. printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
  930. "ata: conflict with %s\n",
  931. ATA_SECONDARY_CMD,
  932. conflict->name);
  933. }
  934. } else
  935. legacy_mode |= ATA_PORT_SECONDARY;
  936. }
  937. /* we have legacy mode, but all ports are unavailable */
  938. if (legacy_mode == (1 << 3)) {
  939. rc = -EBUSY;
  940. goto err_out_regions;
  941. }
  942. /* TODO: If we get no DMA mask we should fall back to PIO */
  943. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  944. if (rc)
  945. goto err_out_regions;
  946. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  947. if (rc)
  948. goto err_out_regions;
  949. if (legacy_mode) {
  950. probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
  951. } else {
  952. if (n_ports == 2)
  953. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  954. else
  955. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  956. }
  957. if (!probe_ent) {
  958. rc = -ENOMEM;
  959. goto err_out_regions;
  960. }
  961. pci_set_master(pdev);
  962. if (!ata_device_add(probe_ent)) {
  963. rc = -ENODEV;
  964. goto err_out_ent;
  965. }
  966. kfree(probe_ent);
  967. return 0;
  968. err_out_ent:
  969. kfree(probe_ent);
  970. err_out_regions:
  971. if (legacy_mode & ATA_PORT_PRIMARY)
  972. release_region(ATA_PRIMARY_CMD, 8);
  973. if (legacy_mode & ATA_PORT_SECONDARY)
  974. release_region(ATA_SECONDARY_CMD, 8);
  975. pci_release_regions(pdev);
  976. err_out:
  977. if (disable_dev_on_err)
  978. pci_disable_device(pdev);
  979. return rc;
  980. }
  981. /**
  982. * ata_pci_clear_simplex - attempt to kick device out of simplex
  983. * @pdev: PCI device
  984. *
  985. * Some PCI ATA devices report simplex mode but in fact can be told to
  986. * enter non simplex mode. This implements the neccessary logic to
  987. * perform the task on such devices. Calling it on other devices will
  988. * have -undefined- behaviour.
  989. */
  990. int ata_pci_clear_simplex(struct pci_dev *pdev)
  991. {
  992. unsigned long bmdma = pci_resource_start(pdev, 4);
  993. u8 simplex;
  994. if (bmdma == 0)
  995. return -ENOENT;
  996. simplex = inb(bmdma + 0x02);
  997. outb(simplex & 0x60, bmdma + 0x02);
  998. simplex = inb(bmdma + 0x02);
  999. if (simplex & 0x80)
  1000. return -EOPNOTSUPP;
  1001. return 0;
  1002. }
  1003. unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
  1004. {
  1005. /* Filter out DMA modes if the device has been configured by
  1006. the BIOS as PIO only */
  1007. if (ap->ioaddr.bmdma_addr == 0)
  1008. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1009. return xfer_mask;
  1010. }
  1011. #endif /* CONFIG_PCI */