libata-sff.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. /* Under certain circumstances, some controllers raise IRQ on
  619. * ATA_NIEN manipulation. Also, many controllers fail to mask
  620. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  621. */
  622. ata_chk_status(ap);
  623. ap->ops->irq_clear(ap);
  624. }
  625. /**
  626. * ata_bmdma_thaw - Thaw BMDMA controller port
  627. * @ap: port to thaw
  628. *
  629. * Thaw BMDMA controller port.
  630. *
  631. * LOCKING:
  632. * Inherited from caller.
  633. */
  634. void ata_bmdma_thaw(struct ata_port *ap)
  635. {
  636. /* clear & re-enable interrupts */
  637. ata_chk_status(ap);
  638. ap->ops->irq_clear(ap);
  639. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  640. ata_irq_on(ap);
  641. }
  642. /**
  643. * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
  644. * @ap: port to handle error for
  645. * @prereset: prereset method (can be NULL)
  646. * @softreset: softreset method (can be NULL)
  647. * @hardreset: hardreset method (can be NULL)
  648. * @postreset: postreset method (can be NULL)
  649. *
  650. * Handle error for ATA BMDMA controller. It can handle both
  651. * PATA and SATA controllers. Many controllers should be able to
  652. * use this EH as-is or with some added handling before and
  653. * after.
  654. *
  655. * This function is intended to be used for constructing
  656. * ->error_handler callback by low level drivers.
  657. *
  658. * LOCKING:
  659. * Kernel thread context (may sleep)
  660. */
  661. void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  662. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  663. ata_postreset_fn_t postreset)
  664. {
  665. struct ata_queued_cmd *qc;
  666. unsigned long flags;
  667. int thaw = 0;
  668. qc = __ata_qc_from_tag(ap, ap->active_tag);
  669. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  670. qc = NULL;
  671. /* reset PIO HSM and stop DMA engine */
  672. spin_lock_irqsave(ap->lock, flags);
  673. ap->hsm_task_state = HSM_ST_IDLE;
  674. if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
  675. qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
  676. u8 host_stat;
  677. host_stat = ap->ops->bmdma_status(ap);
  678. /* BMDMA controllers indicate host bus error by
  679. * setting DMA_ERR bit and timing out. As it wasn't
  680. * really a timeout event, adjust error mask and
  681. * cancel frozen state.
  682. */
  683. if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
  684. qc->err_mask = AC_ERR_HOST_BUS;
  685. thaw = 1;
  686. }
  687. ap->ops->bmdma_stop(qc);
  688. }
  689. ata_altstatus(ap);
  690. ata_chk_status(ap);
  691. ap->ops->irq_clear(ap);
  692. spin_unlock_irqrestore(ap->lock, flags);
  693. if (thaw)
  694. ata_eh_thaw_port(ap);
  695. /* PIO and DMA engines have been stopped, perform recovery */
  696. ata_do_eh(ap, prereset, softreset, hardreset, postreset);
  697. }
  698. /**
  699. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  700. * @ap: port to handle error for
  701. *
  702. * Stock error handler for BMDMA controller.
  703. *
  704. * LOCKING:
  705. * Kernel thread context (may sleep)
  706. */
  707. void ata_bmdma_error_handler(struct ata_port *ap)
  708. {
  709. ata_reset_fn_t hardreset;
  710. hardreset = NULL;
  711. if (sata_scr_valid(ap))
  712. hardreset = sata_std_hardreset;
  713. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  714. ata_std_postreset);
  715. }
  716. /**
  717. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
  718. * BMDMA controller
  719. * @qc: internal command to clean up
  720. *
  721. * LOCKING:
  722. * Kernel thread context (may sleep)
  723. */
  724. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  725. {
  726. ata_bmdma_stop(qc);
  727. }
  728. #ifdef CONFIG_PCI
  729. /**
  730. * ata_pci_init_native_mode - Initialize native-mode driver
  731. * @pdev: pci device to be initialized
  732. * @port: array[2] of pointers to port info structures.
  733. * @ports: bitmap of ports present
  734. *
  735. * Utility function which allocates and initializes an
  736. * ata_probe_ent structure for a standard dual-port
  737. * PIO-based IDE controller. The returned ata_probe_ent
  738. * structure can be passed to ata_device_add(). The returned
  739. * ata_probe_ent structure should then be freed with kfree().
  740. *
  741. * The caller need only pass the address of the primary port, the
  742. * secondary will be deduced automatically. If the device has non
  743. * standard secondary port mappings this function can be called twice,
  744. * once for each interface.
  745. */
  746. struct ata_probe_ent *
  747. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  748. {
  749. struct ata_probe_ent *probe_ent =
  750. ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  751. int p = 0;
  752. unsigned long bmdma;
  753. if (!probe_ent)
  754. return NULL;
  755. probe_ent->irq = pdev->irq;
  756. probe_ent->irq_flags = IRQF_SHARED;
  757. if (ports & ATA_PORT_PRIMARY) {
  758. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
  759. probe_ent->port[p].altstatus_addr =
  760. probe_ent->port[p].ctl_addr =
  761. pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
  762. bmdma = pci_resource_start(pdev, 4);
  763. if (bmdma) {
  764. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  765. (inb(bmdma + 2) & 0x80))
  766. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  767. probe_ent->port[p].bmdma_addr = bmdma;
  768. }
  769. ata_std_ports(&probe_ent->port[p]);
  770. p++;
  771. }
  772. if (ports & ATA_PORT_SECONDARY) {
  773. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
  774. probe_ent->port[p].altstatus_addr =
  775. probe_ent->port[p].ctl_addr =
  776. pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
  777. bmdma = pci_resource_start(pdev, 4);
  778. if (bmdma) {
  779. bmdma += 8;
  780. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  781. (inb(bmdma + 2) & 0x80))
  782. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  783. probe_ent->port[p].bmdma_addr = bmdma;
  784. }
  785. ata_std_ports(&probe_ent->port[p]);
  786. probe_ent->pinfo2 = port[1];
  787. p++;
  788. }
  789. probe_ent->n_ports = p;
  790. return probe_ent;
  791. }
  792. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  793. struct ata_port_info **port, int port_mask)
  794. {
  795. struct ata_probe_ent *probe_ent;
  796. unsigned long bmdma = pci_resource_start(pdev, 4);
  797. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  798. if (!probe_ent)
  799. return NULL;
  800. probe_ent->n_ports = 2;
  801. probe_ent->irq_flags = IRQF_SHARED;
  802. if (port_mask & ATA_PORT_PRIMARY) {
  803. probe_ent->irq = ATA_PRIMARY_IRQ;
  804. probe_ent->port[0].cmd_addr = ATA_PRIMARY_CMD;
  805. probe_ent->port[0].altstatus_addr =
  806. probe_ent->port[0].ctl_addr = ATA_PRIMARY_CTL;
  807. if (bmdma) {
  808. probe_ent->port[0].bmdma_addr = bmdma;
  809. if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  810. (inb(bmdma + 2) & 0x80))
  811. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  812. }
  813. ata_std_ports(&probe_ent->port[0]);
  814. } else
  815. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  816. if (port_mask & ATA_PORT_SECONDARY) {
  817. if (probe_ent->irq)
  818. probe_ent->irq2 = ATA_SECONDARY_IRQ;
  819. else
  820. probe_ent->irq = ATA_SECONDARY_IRQ;
  821. probe_ent->port[1].cmd_addr = ATA_SECONDARY_CMD;
  822. probe_ent->port[1].altstatus_addr =
  823. probe_ent->port[1].ctl_addr = ATA_SECONDARY_CTL;
  824. if (bmdma) {
  825. probe_ent->port[1].bmdma_addr = bmdma + 8;
  826. if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  827. (inb(bmdma + 10) & 0x80))
  828. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  829. }
  830. ata_std_ports(&probe_ent->port[1]);
  831. /* FIXME: could be pointing to stack area; must copy */
  832. probe_ent->pinfo2 = port[1];
  833. } else
  834. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  835. return probe_ent;
  836. }
  837. /**
  838. * ata_pci_init_one - Initialize/register PCI IDE host controller
  839. * @pdev: Controller to be initialized
  840. * @port_info: Information from low-level host driver
  841. * @n_ports: Number of ports attached to host controller
  842. *
  843. * This is a helper function which can be called from a driver's
  844. * xxx_init_one() probe function if the hardware uses traditional
  845. * IDE taskfile registers.
  846. *
  847. * This function calls pci_enable_device(), reserves its register
  848. * regions, sets the dma mask, enables bus master mode, and calls
  849. * ata_device_add()
  850. *
  851. * ASSUMPTION:
  852. * Nobody makes a single channel controller that appears solely as
  853. * the secondary legacy port on PCI.
  854. *
  855. * LOCKING:
  856. * Inherited from PCI layer (may sleep).
  857. *
  858. * RETURNS:
  859. * Zero on success, negative on errno-based value on error.
  860. */
  861. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  862. unsigned int n_ports)
  863. {
  864. struct ata_probe_ent *probe_ent = NULL;
  865. struct ata_port_info *port[2];
  866. u8 mask;
  867. unsigned int legacy_mode = 0;
  868. int disable_dev_on_err = 1;
  869. int rc;
  870. DPRINTK("ENTER\n");
  871. BUG_ON(n_ports < 1 || n_ports > 2);
  872. port[0] = port_info[0];
  873. if (n_ports > 1)
  874. port[1] = port_info[1];
  875. else
  876. port[1] = port[0];
  877. /* FIXME: Really for ATA it isn't safe because the device may be
  878. multi-purpose and we want to leave it alone if it was already
  879. enabled. Secondly for shared use as Arjan says we want refcounting
  880. Checking dev->is_enabled is insufficient as this is not set at
  881. boot for the primary video which is BIOS enabled
  882. */
  883. rc = pci_enable_device(pdev);
  884. if (rc)
  885. return rc;
  886. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  887. u8 tmp8;
  888. /* TODO: What if one channel is in native mode ... */
  889. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  890. mask = (1 << 2) | (1 << 0);
  891. if ((tmp8 & mask) != mask)
  892. legacy_mode = (1 << 3);
  893. #if defined(CONFIG_NO_ATA_LEGACY)
  894. /* Some platforms with PCI limits cannot address compat
  895. port space. In that case we punt if their firmware has
  896. left a device in compatibility mode */
  897. if (legacy_mode) {
  898. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  899. return -EOPNOTSUPP;
  900. }
  901. #endif
  902. }
  903. if (!legacy_mode) {
  904. rc = pci_request_regions(pdev, DRV_NAME);
  905. if (rc) {
  906. disable_dev_on_err = 0;
  907. goto err_out;
  908. }
  909. } else {
  910. /* Deal with combined mode hack. This side of the logic all
  911. goes away once the combined mode hack is killed in 2.6.21 */
  912. if (!request_region(ATA_PRIMARY_CMD, 8, "libata")) {
  913. struct resource *conflict, res;
  914. res.start = ATA_PRIMARY_CMD;
  915. res.end = ATA_PRIMARY_CMD + 8 - 1;
  916. conflict = ____request_resource(&ioport_resource, &res);
  917. while (conflict->child)
  918. conflict = ____request_resource(conflict, &res);
  919. if (!strcmp(conflict->name, "libata"))
  920. legacy_mode |= ATA_PORT_PRIMARY;
  921. else {
  922. disable_dev_on_err = 0;
  923. printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
  924. "ata: conflict with %s\n",
  925. ATA_PRIMARY_CMD,
  926. conflict->name);
  927. }
  928. } else
  929. legacy_mode |= ATA_PORT_PRIMARY;
  930. if (!request_region(ATA_SECONDARY_CMD, 8, "libata")) {
  931. struct resource *conflict, res;
  932. res.start = ATA_SECONDARY_CMD;
  933. res.end = ATA_SECONDARY_CMD + 8 - 1;
  934. conflict = ____request_resource(&ioport_resource, &res);
  935. while (conflict->child)
  936. conflict = ____request_resource(conflict, &res);
  937. if (!strcmp(conflict->name, "libata"))
  938. legacy_mode |= ATA_PORT_SECONDARY;
  939. else {
  940. disable_dev_on_err = 0;
  941. printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
  942. "ata: conflict with %s\n",
  943. ATA_SECONDARY_CMD,
  944. conflict->name);
  945. }
  946. } else
  947. legacy_mode |= ATA_PORT_SECONDARY;
  948. if (legacy_mode & ATA_PORT_PRIMARY)
  949. pci_request_region(pdev, 1, DRV_NAME);
  950. if (legacy_mode & ATA_PORT_SECONDARY)
  951. pci_request_region(pdev, 3, DRV_NAME);
  952. /* If there is a DMA resource, allocate it */
  953. pci_request_region(pdev, 4, DRV_NAME);
  954. }
  955. /* we have legacy mode, but all ports are unavailable */
  956. if (legacy_mode == (1 << 3)) {
  957. rc = -EBUSY;
  958. goto err_out_regions;
  959. }
  960. /* TODO: If we get no DMA mask we should fall back to PIO */
  961. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  962. if (rc)
  963. goto err_out_regions;
  964. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  965. if (rc)
  966. goto err_out_regions;
  967. if (legacy_mode) {
  968. probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
  969. } else {
  970. if (n_ports == 2)
  971. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  972. else
  973. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  974. }
  975. if (!probe_ent) {
  976. rc = -ENOMEM;
  977. goto err_out_regions;
  978. }
  979. pci_set_master(pdev);
  980. if (!ata_device_add(probe_ent)) {
  981. rc = -ENODEV;
  982. goto err_out_ent;
  983. }
  984. kfree(probe_ent);
  985. return 0;
  986. err_out_ent:
  987. kfree(probe_ent);
  988. err_out_regions:
  989. /* All this conditional stuff is needed for the combined mode hack
  990. until 2.6.21 when it can go */
  991. if (legacy_mode) {
  992. pci_release_region(pdev, 4);
  993. if (legacy_mode & ATA_PORT_PRIMARY) {
  994. release_region(ATA_PRIMARY_CMD, 8);
  995. pci_release_region(pdev, 1);
  996. }
  997. if (legacy_mode & ATA_PORT_SECONDARY) {
  998. release_region(ATA_SECONDARY_CMD, 8);
  999. pci_release_region(pdev, 3);
  1000. }
  1001. } else
  1002. pci_release_regions(pdev);
  1003. err_out:
  1004. if (disable_dev_on_err)
  1005. pci_disable_device(pdev);
  1006. return rc;
  1007. }
  1008. /**
  1009. * ata_pci_clear_simplex - attempt to kick device out of simplex
  1010. * @pdev: PCI device
  1011. *
  1012. * Some PCI ATA devices report simplex mode but in fact can be told to
  1013. * enter non simplex mode. This implements the neccessary logic to
  1014. * perform the task on such devices. Calling it on other devices will
  1015. * have -undefined- behaviour.
  1016. */
  1017. int ata_pci_clear_simplex(struct pci_dev *pdev)
  1018. {
  1019. unsigned long bmdma = pci_resource_start(pdev, 4);
  1020. u8 simplex;
  1021. if (bmdma == 0)
  1022. return -ENOENT;
  1023. simplex = inb(bmdma + 0x02);
  1024. outb(simplex & 0x60, bmdma + 0x02);
  1025. simplex = inb(bmdma + 0x02);
  1026. if (simplex & 0x80)
  1027. return -EOPNOTSUPP;
  1028. return 0;
  1029. }
  1030. unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
  1031. {
  1032. /* Filter out DMA modes if the device has been configured by
  1033. the BIOS as PIO only */
  1034. if (ap->ioaddr.bmdma_addr == 0)
  1035. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1036. return xfer_mask;
  1037. }
  1038. #endif /* CONFIG_PCI */