libata-sff.c 30 KB

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