pata_scc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Support for IDE interfaces on Celleb platform
  3. *
  4. * (C) Copyright 2006 TOSHIBA CORPORATION
  5. *
  6. * This code is based on drivers/ata/ata_piix.c:
  7. * Copyright 2003-2005 Red Hat Inc
  8. * Copyright 2003-2005 Jeff Garzik
  9. * Copyright (C) 1998-1999 Andrzej Krzysztofowicz, Author and Maintainer
  10. * Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
  11. * Copyright (C) 2003 Red Hat Inc
  12. *
  13. * and drivers/ata/ahci.c:
  14. * Copyright 2004-2005 Red Hat, Inc.
  15. *
  16. * and drivers/ata/libata-core.c:
  17. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  18. * Copyright 2003-2004 Jeff Garzik
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License along
  31. * with this program; if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h>
  39. #include <linux/device.h>
  40. #include <scsi/scsi_host.h>
  41. #include <linux/libata.h>
  42. #define DRV_NAME "pata_scc"
  43. #define DRV_VERSION "0.3"
  44. #define PCI_DEVICE_ID_TOSHIBA_SCC_ATA 0x01b4
  45. /* PCI BARs */
  46. #define SCC_CTRL_BAR 0
  47. #define SCC_BMID_BAR 1
  48. /* offset of CTRL registers */
  49. #define SCC_CTL_PIOSHT 0x000
  50. #define SCC_CTL_PIOCT 0x004
  51. #define SCC_CTL_MDMACT 0x008
  52. #define SCC_CTL_MCRCST 0x00C
  53. #define SCC_CTL_SDMACT 0x010
  54. #define SCC_CTL_SCRCST 0x014
  55. #define SCC_CTL_UDENVT 0x018
  56. #define SCC_CTL_TDVHSEL 0x020
  57. #define SCC_CTL_MODEREG 0x024
  58. #define SCC_CTL_ECMODE 0xF00
  59. #define SCC_CTL_MAEA0 0xF50
  60. #define SCC_CTL_MAEC0 0xF54
  61. #define SCC_CTL_CCKCTRL 0xFF0
  62. /* offset of BMID registers */
  63. #define SCC_DMA_CMD 0x000
  64. #define SCC_DMA_STATUS 0x004
  65. #define SCC_DMA_TABLE_OFS 0x008
  66. #define SCC_DMA_INTMASK 0x010
  67. #define SCC_DMA_INTST 0x014
  68. #define SCC_DMA_PTERADD 0x018
  69. #define SCC_REG_CMD_ADDR 0x020
  70. #define SCC_REG_DATA 0x000
  71. #define SCC_REG_ERR 0x004
  72. #define SCC_REG_FEATURE 0x004
  73. #define SCC_REG_NSECT 0x008
  74. #define SCC_REG_LBAL 0x00C
  75. #define SCC_REG_LBAM 0x010
  76. #define SCC_REG_LBAH 0x014
  77. #define SCC_REG_DEVICE 0x018
  78. #define SCC_REG_STATUS 0x01C
  79. #define SCC_REG_CMD 0x01C
  80. #define SCC_REG_ALTSTATUS 0x020
  81. /* register value */
  82. #define TDVHSEL_MASTER 0x00000001
  83. #define TDVHSEL_SLAVE 0x00000004
  84. #define MODE_JCUSFEN 0x00000080
  85. #define ECMODE_VALUE 0x01
  86. #define CCKCTRL_ATARESET 0x00040000
  87. #define CCKCTRL_BUFCNT 0x00020000
  88. #define CCKCTRL_CRST 0x00010000
  89. #define CCKCTRL_OCLKEN 0x00000100
  90. #define CCKCTRL_ATACLKOEN 0x00000002
  91. #define CCKCTRL_LCLKEN 0x00000001
  92. #define QCHCD_IOS_SS 0x00000001
  93. #define QCHSD_STPDIAG 0x00020000
  94. #define INTMASK_MSK 0xD1000012
  95. #define INTSTS_SERROR 0x80000000
  96. #define INTSTS_PRERR 0x40000000
  97. #define INTSTS_RERR 0x10000000
  98. #define INTSTS_ICERR 0x01000000
  99. #define INTSTS_BMSINT 0x00000010
  100. #define INTSTS_BMHE 0x00000008
  101. #define INTSTS_IOIRQS 0x00000004
  102. #define INTSTS_INTRQ 0x00000002
  103. #define INTSTS_ACTEINT 0x00000001
  104. /* PIO transfer mode table */
  105. /* JCHST */
  106. static const unsigned long JCHSTtbl[2][7] = {
  107. {0x0E, 0x05, 0x02, 0x03, 0x02, 0x00, 0x00}, /* 100MHz */
  108. {0x13, 0x07, 0x04, 0x04, 0x03, 0x00, 0x00} /* 133MHz */
  109. };
  110. /* JCHHT */
  111. static const unsigned long JCHHTtbl[2][7] = {
  112. {0x0E, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00}, /* 100MHz */
  113. {0x13, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00} /* 133MHz */
  114. };
  115. /* JCHCT */
  116. static const unsigned long JCHCTtbl[2][7] = {
  117. {0x1D, 0x1D, 0x1C, 0x0B, 0x06, 0x00, 0x00}, /* 100MHz */
  118. {0x27, 0x26, 0x26, 0x0E, 0x09, 0x00, 0x00} /* 133MHz */
  119. };
  120. /* DMA transfer mode table */
  121. /* JCHDCTM/JCHDCTS */
  122. static const unsigned long JCHDCTxtbl[2][7] = {
  123. {0x0A, 0x06, 0x04, 0x03, 0x01, 0x00, 0x00}, /* 100MHz */
  124. {0x0E, 0x09, 0x06, 0x04, 0x02, 0x01, 0x00} /* 133MHz */
  125. };
  126. /* JCSTWTM/JCSTWTS */
  127. static const unsigned long JCSTWTxtbl[2][7] = {
  128. {0x06, 0x04, 0x03, 0x02, 0x02, 0x02, 0x00}, /* 100MHz */
  129. {0x09, 0x06, 0x04, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  130. };
  131. /* JCTSS */
  132. static const unsigned long JCTSStbl[2][7] = {
  133. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00}, /* 100MHz */
  134. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05} /* 133MHz */
  135. };
  136. /* JCENVT */
  137. static const unsigned long JCENVTtbl[2][7] = {
  138. {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00}, /* 100MHz */
  139. {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  140. };
  141. /* JCACTSELS/JCACTSELM */
  142. static const unsigned long JCACTSELtbl[2][7] = {
  143. {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00}, /* 100MHz */
  144. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} /* 133MHz */
  145. };
  146. static const struct pci_device_id scc_pci_tbl[] = {
  147. { PCI_VDEVICE(TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SCC_ATA), 0},
  148. { } /* terminate list */
  149. };
  150. /**
  151. * scc_set_piomode - Initialize host controller PATA PIO timings
  152. * @ap: Port whose timings we are configuring
  153. * @adev: um
  154. *
  155. * Set PIO mode for device.
  156. *
  157. * LOCKING:
  158. * None (inherited from caller).
  159. */
  160. static void scc_set_piomode (struct ata_port *ap, struct ata_device *adev)
  161. {
  162. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  163. void __iomem *ctrl_base = ap->host->iomap[SCC_CTRL_BAR];
  164. void __iomem *cckctrl_port = ctrl_base + SCC_CTL_CCKCTRL;
  165. void __iomem *piosht_port = ctrl_base + SCC_CTL_PIOSHT;
  166. void __iomem *pioct_port = ctrl_base + SCC_CTL_PIOCT;
  167. unsigned long reg;
  168. int offset;
  169. reg = in_be32(cckctrl_port);
  170. if (reg & CCKCTRL_ATACLKOEN)
  171. offset = 1; /* 133MHz */
  172. else
  173. offset = 0; /* 100MHz */
  174. reg = JCHSTtbl[offset][pio] << 16 | JCHHTtbl[offset][pio];
  175. out_be32(piosht_port, reg);
  176. reg = JCHCTtbl[offset][pio];
  177. out_be32(pioct_port, reg);
  178. }
  179. /**
  180. * scc_set_dmamode - Initialize host controller PATA DMA timings
  181. * @ap: Port whose timings we are configuring
  182. * @adev: um
  183. *
  184. * Set UDMA mode for device.
  185. *
  186. * LOCKING:
  187. * None (inherited from caller).
  188. */
  189. static void scc_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  190. {
  191. unsigned int udma = adev->dma_mode;
  192. unsigned int is_slave = (adev->devno != 0);
  193. u8 speed = udma;
  194. void __iomem *ctrl_base = ap->host->iomap[SCC_CTRL_BAR];
  195. void __iomem *cckctrl_port = ctrl_base + SCC_CTL_CCKCTRL;
  196. void __iomem *mdmact_port = ctrl_base + SCC_CTL_MDMACT;
  197. void __iomem *mcrcst_port = ctrl_base + SCC_CTL_MCRCST;
  198. void __iomem *sdmact_port = ctrl_base + SCC_CTL_SDMACT;
  199. void __iomem *scrcst_port = ctrl_base + SCC_CTL_SCRCST;
  200. void __iomem *udenvt_port = ctrl_base + SCC_CTL_UDENVT;
  201. void __iomem *tdvhsel_port = ctrl_base + SCC_CTL_TDVHSEL;
  202. int offset, idx;
  203. if (in_be32(cckctrl_port) & CCKCTRL_ATACLKOEN)
  204. offset = 1; /* 133MHz */
  205. else
  206. offset = 0; /* 100MHz */
  207. if (speed >= XFER_UDMA_0)
  208. idx = speed - XFER_UDMA_0;
  209. else
  210. return;
  211. if (is_slave) {
  212. out_be32(sdmact_port, JCHDCTxtbl[offset][idx]);
  213. out_be32(scrcst_port, JCSTWTxtbl[offset][idx]);
  214. out_be32(tdvhsel_port,
  215. (in_be32(tdvhsel_port) & ~TDVHSEL_SLAVE) | (JCACTSELtbl[offset][idx] << 2));
  216. } else {
  217. out_be32(mdmact_port, JCHDCTxtbl[offset][idx]);
  218. out_be32(mcrcst_port, JCSTWTxtbl[offset][idx]);
  219. out_be32(tdvhsel_port,
  220. (in_be32(tdvhsel_port) & ~TDVHSEL_MASTER) | JCACTSELtbl[offset][idx]);
  221. }
  222. out_be32(udenvt_port,
  223. JCTSStbl[offset][idx] << 16 | JCENVTtbl[offset][idx]);
  224. }
  225. unsigned long scc_mode_filter(struct ata_device *adev, unsigned long mask)
  226. {
  227. /* errata A308 workaround: limit ATAPI UDMA mode to UDMA4 */
  228. if (adev->class == ATA_DEV_ATAPI &&
  229. (mask & (0xE0 << ATA_SHIFT_UDMA))) {
  230. printk(KERN_INFO "%s: limit ATAPI UDMA to UDMA4\n", DRV_NAME);
  231. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  232. }
  233. return mask;
  234. }
  235. /**
  236. * scc_tf_load - send taskfile registers to host controller
  237. * @ap: Port to which output is sent
  238. * @tf: ATA taskfile register set
  239. *
  240. * Note: Original code is ata_sff_tf_load().
  241. */
  242. static void scc_tf_load (struct ata_port *ap, const struct ata_taskfile *tf)
  243. {
  244. struct ata_ioports *ioaddr = &ap->ioaddr;
  245. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  246. if (tf->ctl != ap->last_ctl) {
  247. out_be32(ioaddr->ctl_addr, tf->ctl);
  248. ap->last_ctl = tf->ctl;
  249. ata_wait_idle(ap);
  250. }
  251. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  252. out_be32(ioaddr->feature_addr, tf->hob_feature);
  253. out_be32(ioaddr->nsect_addr, tf->hob_nsect);
  254. out_be32(ioaddr->lbal_addr, tf->hob_lbal);
  255. out_be32(ioaddr->lbam_addr, tf->hob_lbam);
  256. out_be32(ioaddr->lbah_addr, tf->hob_lbah);
  257. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  258. tf->hob_feature,
  259. tf->hob_nsect,
  260. tf->hob_lbal,
  261. tf->hob_lbam,
  262. tf->hob_lbah);
  263. }
  264. if (is_addr) {
  265. out_be32(ioaddr->feature_addr, tf->feature);
  266. out_be32(ioaddr->nsect_addr, tf->nsect);
  267. out_be32(ioaddr->lbal_addr, tf->lbal);
  268. out_be32(ioaddr->lbam_addr, tf->lbam);
  269. out_be32(ioaddr->lbah_addr, tf->lbah);
  270. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  271. tf->feature,
  272. tf->nsect,
  273. tf->lbal,
  274. tf->lbam,
  275. tf->lbah);
  276. }
  277. if (tf->flags & ATA_TFLAG_DEVICE) {
  278. out_be32(ioaddr->device_addr, tf->device);
  279. VPRINTK("device 0x%X\n", tf->device);
  280. }
  281. ata_wait_idle(ap);
  282. }
  283. /**
  284. * scc_check_status - Read device status reg & clear interrupt
  285. * @ap: port where the device is
  286. *
  287. * Note: Original code is ata_check_status().
  288. */
  289. static u8 scc_check_status (struct ata_port *ap)
  290. {
  291. return in_be32(ap->ioaddr.status_addr);
  292. }
  293. /**
  294. * scc_tf_read - input device's ATA taskfile shadow registers
  295. * @ap: Port from which input is read
  296. * @tf: ATA taskfile register set for storing input
  297. *
  298. * Note: Original code is ata_sff_tf_read().
  299. */
  300. static void scc_tf_read (struct ata_port *ap, struct ata_taskfile *tf)
  301. {
  302. struct ata_ioports *ioaddr = &ap->ioaddr;
  303. tf->command = scc_check_status(ap);
  304. tf->feature = in_be32(ioaddr->error_addr);
  305. tf->nsect = in_be32(ioaddr->nsect_addr);
  306. tf->lbal = in_be32(ioaddr->lbal_addr);
  307. tf->lbam = in_be32(ioaddr->lbam_addr);
  308. tf->lbah = in_be32(ioaddr->lbah_addr);
  309. tf->device = in_be32(ioaddr->device_addr);
  310. if (tf->flags & ATA_TFLAG_LBA48) {
  311. out_be32(ioaddr->ctl_addr, tf->ctl | ATA_HOB);
  312. tf->hob_feature = in_be32(ioaddr->error_addr);
  313. tf->hob_nsect = in_be32(ioaddr->nsect_addr);
  314. tf->hob_lbal = in_be32(ioaddr->lbal_addr);
  315. tf->hob_lbam = in_be32(ioaddr->lbam_addr);
  316. tf->hob_lbah = in_be32(ioaddr->lbah_addr);
  317. out_be32(ioaddr->ctl_addr, tf->ctl);
  318. ap->last_ctl = tf->ctl;
  319. }
  320. }
  321. /**
  322. * scc_exec_command - issue ATA command to host controller
  323. * @ap: port to which command is being issued
  324. * @tf: ATA taskfile register set
  325. *
  326. * Note: Original code is ata_sff_exec_command().
  327. */
  328. static void scc_exec_command (struct ata_port *ap,
  329. const struct ata_taskfile *tf)
  330. {
  331. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  332. out_be32(ap->ioaddr.command_addr, tf->command);
  333. ata_sff_pause(ap);
  334. }
  335. /**
  336. * scc_check_altstatus - Read device alternate status reg
  337. * @ap: port where the device is
  338. */
  339. static u8 scc_check_altstatus (struct ata_port *ap)
  340. {
  341. return in_be32(ap->ioaddr.altstatus_addr);
  342. }
  343. /**
  344. * scc_dev_select - Select device 0/1 on ATA bus
  345. * @ap: ATA channel to manipulate
  346. * @device: ATA device (numbered from zero) to select
  347. *
  348. * Note: Original code is ata_sff_dev_select().
  349. */
  350. static void scc_dev_select (struct ata_port *ap, unsigned int device)
  351. {
  352. u8 tmp;
  353. if (device == 0)
  354. tmp = ATA_DEVICE_OBS;
  355. else
  356. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  357. out_be32(ap->ioaddr.device_addr, tmp);
  358. ata_sff_pause(ap);
  359. }
  360. /**
  361. * scc_set_devctl - Write device control reg
  362. * @ap: port where the device is
  363. * @ctl: value to write
  364. */
  365. static void scc_set_devctl(struct ata_port *ap, u8 ctl)
  366. {
  367. out_be32(ap->ioaddr.ctl_addr, ctl);
  368. }
  369. /**
  370. * scc_bmdma_setup - Set up PCI IDE BMDMA transaction
  371. * @qc: Info associated with this ATA transaction.
  372. *
  373. * Note: Original code is ata_bmdma_setup().
  374. */
  375. static void scc_bmdma_setup (struct ata_queued_cmd *qc)
  376. {
  377. struct ata_port *ap = qc->ap;
  378. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  379. u8 dmactl;
  380. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  381. /* load PRD table addr */
  382. out_be32(mmio + SCC_DMA_TABLE_OFS, ap->bmdma_prd_dma);
  383. /* specify data direction, triple-check start bit is clear */
  384. dmactl = in_be32(mmio + SCC_DMA_CMD);
  385. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  386. if (!rw)
  387. dmactl |= ATA_DMA_WR;
  388. out_be32(mmio + SCC_DMA_CMD, dmactl);
  389. /* issue r/w command */
  390. ap->ops->sff_exec_command(ap, &qc->tf);
  391. }
  392. /**
  393. * scc_bmdma_start - Start a PCI IDE BMDMA transaction
  394. * @qc: Info associated with this ATA transaction.
  395. *
  396. * Note: Original code is ata_bmdma_start().
  397. */
  398. static void scc_bmdma_start (struct ata_queued_cmd *qc)
  399. {
  400. struct ata_port *ap = qc->ap;
  401. u8 dmactl;
  402. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  403. /* start host DMA transaction */
  404. dmactl = in_be32(mmio + SCC_DMA_CMD);
  405. out_be32(mmio + SCC_DMA_CMD, dmactl | ATA_DMA_START);
  406. }
  407. /**
  408. * scc_devchk - PATA device presence detection
  409. * @ap: ATA channel to examine
  410. * @device: Device to examine (starting at zero)
  411. *
  412. * Note: Original code is ata_devchk().
  413. */
  414. static unsigned int scc_devchk (struct ata_port *ap,
  415. unsigned int device)
  416. {
  417. struct ata_ioports *ioaddr = &ap->ioaddr;
  418. u8 nsect, lbal;
  419. ap->ops->sff_dev_select(ap, device);
  420. out_be32(ioaddr->nsect_addr, 0x55);
  421. out_be32(ioaddr->lbal_addr, 0xaa);
  422. out_be32(ioaddr->nsect_addr, 0xaa);
  423. out_be32(ioaddr->lbal_addr, 0x55);
  424. out_be32(ioaddr->nsect_addr, 0x55);
  425. out_be32(ioaddr->lbal_addr, 0xaa);
  426. nsect = in_be32(ioaddr->nsect_addr);
  427. lbal = in_be32(ioaddr->lbal_addr);
  428. if ((nsect == 0x55) && (lbal == 0xaa))
  429. return 1; /* we found a device */
  430. return 0; /* nothing found */
  431. }
  432. /**
  433. * scc_wait_after_reset - wait for devices to become ready after reset
  434. *
  435. * Note: Original code is ata_sff_wait_after_reset
  436. */
  437. static int scc_wait_after_reset(struct ata_link *link, unsigned int devmask,
  438. unsigned long deadline)
  439. {
  440. struct ata_port *ap = link->ap;
  441. struct ata_ioports *ioaddr = &ap->ioaddr;
  442. unsigned int dev0 = devmask & (1 << 0);
  443. unsigned int dev1 = devmask & (1 << 1);
  444. int rc, ret = 0;
  445. /* Spec mandates ">= 2ms" before checking status. We wait
  446. * 150ms, because that was the magic delay used for ATAPI
  447. * devices in Hale Landis's ATADRVR, for the period of time
  448. * between when the ATA command register is written, and then
  449. * status is checked. Because waiting for "a while" before
  450. * checking status is fine, post SRST, we perform this magic
  451. * delay here as well.
  452. *
  453. * Old drivers/ide uses the 2mS rule and then waits for ready.
  454. */
  455. ata_msleep(ap, 150);
  456. /* always check readiness of the master device */
  457. rc = ata_sff_wait_ready(link, deadline);
  458. /* -ENODEV means the odd clown forgot the D7 pulldown resistor
  459. * and TF status is 0xff, bail out on it too.
  460. */
  461. if (rc)
  462. return rc;
  463. /* if device 1 was found in ata_devchk, wait for register
  464. * access briefly, then wait for BSY to clear.
  465. */
  466. if (dev1) {
  467. int i;
  468. ap->ops->sff_dev_select(ap, 1);
  469. /* Wait for register access. Some ATAPI devices fail
  470. * to set nsect/lbal after reset, so don't waste too
  471. * much time on it. We're gonna wait for !BSY anyway.
  472. */
  473. for (i = 0; i < 2; i++) {
  474. u8 nsect, lbal;
  475. nsect = in_be32(ioaddr->nsect_addr);
  476. lbal = in_be32(ioaddr->lbal_addr);
  477. if ((nsect == 1) && (lbal == 1))
  478. break;
  479. ata_msleep(ap, 50); /* give drive a breather */
  480. }
  481. rc = ata_sff_wait_ready(link, deadline);
  482. if (rc) {
  483. if (rc != -ENODEV)
  484. return rc;
  485. ret = rc;
  486. }
  487. }
  488. /* is all this really necessary? */
  489. ap->ops->sff_dev_select(ap, 0);
  490. if (dev1)
  491. ap->ops->sff_dev_select(ap, 1);
  492. if (dev0)
  493. ap->ops->sff_dev_select(ap, 0);
  494. return ret;
  495. }
  496. /**
  497. * scc_bus_softreset - PATA device software reset
  498. *
  499. * Note: Original code is ata_bus_softreset().
  500. */
  501. static unsigned int scc_bus_softreset(struct ata_port *ap, unsigned int devmask,
  502. unsigned long deadline)
  503. {
  504. struct ata_ioports *ioaddr = &ap->ioaddr;
  505. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  506. /* software reset. causes dev0 to be selected */
  507. out_be32(ioaddr->ctl_addr, ap->ctl);
  508. udelay(20);
  509. out_be32(ioaddr->ctl_addr, ap->ctl | ATA_SRST);
  510. udelay(20);
  511. out_be32(ioaddr->ctl_addr, ap->ctl);
  512. scc_wait_after_reset(&ap->link, devmask, deadline);
  513. return 0;
  514. }
  515. /**
  516. * scc_softreset - reset host port via ATA SRST
  517. * @ap: port to reset
  518. * @classes: resulting classes of attached devices
  519. * @deadline: deadline jiffies for the operation
  520. *
  521. * Note: Original code is ata_sff_softreset().
  522. */
  523. static int scc_softreset(struct ata_link *link, unsigned int *classes,
  524. unsigned long deadline)
  525. {
  526. struct ata_port *ap = link->ap;
  527. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  528. unsigned int devmask = 0, err_mask;
  529. u8 err;
  530. DPRINTK("ENTER\n");
  531. /* determine if device 0/1 are present */
  532. if (scc_devchk(ap, 0))
  533. devmask |= (1 << 0);
  534. if (slave_possible && scc_devchk(ap, 1))
  535. devmask |= (1 << 1);
  536. /* select device 0 again */
  537. ap->ops->sff_dev_select(ap, 0);
  538. /* issue bus reset */
  539. DPRINTK("about to softreset, devmask=%x\n", devmask);
  540. err_mask = scc_bus_softreset(ap, devmask, deadline);
  541. if (err_mask) {
  542. ata_port_err(ap, "SRST failed (err_mask=0x%x)\n", err_mask);
  543. return -EIO;
  544. }
  545. /* determine by signature whether we have ATA or ATAPI devices */
  546. classes[0] = ata_sff_dev_classify(&ap->link.device[0],
  547. devmask & (1 << 0), &err);
  548. if (slave_possible && err != 0x81)
  549. classes[1] = ata_sff_dev_classify(&ap->link.device[1],
  550. devmask & (1 << 1), &err);
  551. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  552. return 0;
  553. }
  554. /**
  555. * scc_bmdma_stop - Stop PCI IDE BMDMA transfer
  556. * @qc: Command we are ending DMA for
  557. */
  558. static void scc_bmdma_stop (struct ata_queued_cmd *qc)
  559. {
  560. struct ata_port *ap = qc->ap;
  561. void __iomem *ctrl_base = ap->host->iomap[SCC_CTRL_BAR];
  562. void __iomem *bmid_base = ap->host->iomap[SCC_BMID_BAR];
  563. u32 reg;
  564. while (1) {
  565. reg = in_be32(bmid_base + SCC_DMA_INTST);
  566. if (reg & INTSTS_SERROR) {
  567. printk(KERN_WARNING "%s: SERROR\n", DRV_NAME);
  568. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_SERROR|INTSTS_BMSINT);
  569. out_be32(bmid_base + SCC_DMA_CMD,
  570. in_be32(bmid_base + SCC_DMA_CMD) & ~ATA_DMA_START);
  571. continue;
  572. }
  573. if (reg & INTSTS_PRERR) {
  574. u32 maea0, maec0;
  575. maea0 = in_be32(ctrl_base + SCC_CTL_MAEA0);
  576. maec0 = in_be32(ctrl_base + SCC_CTL_MAEC0);
  577. printk(KERN_WARNING "%s: PRERR [addr:%x cmd:%x]\n", DRV_NAME, maea0, maec0);
  578. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_PRERR|INTSTS_BMSINT);
  579. out_be32(bmid_base + SCC_DMA_CMD,
  580. in_be32(bmid_base + SCC_DMA_CMD) & ~ATA_DMA_START);
  581. continue;
  582. }
  583. if (reg & INTSTS_RERR) {
  584. printk(KERN_WARNING "%s: Response Error\n", DRV_NAME);
  585. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_RERR|INTSTS_BMSINT);
  586. out_be32(bmid_base + SCC_DMA_CMD,
  587. in_be32(bmid_base + SCC_DMA_CMD) & ~ATA_DMA_START);
  588. continue;
  589. }
  590. if (reg & INTSTS_ICERR) {
  591. out_be32(bmid_base + SCC_DMA_CMD,
  592. in_be32(bmid_base + SCC_DMA_CMD) & ~ATA_DMA_START);
  593. printk(KERN_WARNING "%s: Illegal Configuration\n", DRV_NAME);
  594. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_ICERR|INTSTS_BMSINT);
  595. continue;
  596. }
  597. if (reg & INTSTS_BMSINT) {
  598. unsigned int classes;
  599. unsigned long deadline = ata_deadline(jiffies, ATA_TMOUT_BOOT);
  600. printk(KERN_WARNING "%s: Internal Bus Error\n", DRV_NAME);
  601. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_BMSINT);
  602. /* TBD: SW reset */
  603. scc_softreset(&ap->link, &classes, deadline);
  604. continue;
  605. }
  606. if (reg & INTSTS_BMHE) {
  607. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_BMHE);
  608. continue;
  609. }
  610. if (reg & INTSTS_ACTEINT) {
  611. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_ACTEINT);
  612. continue;
  613. }
  614. if (reg & INTSTS_IOIRQS) {
  615. out_be32(bmid_base + SCC_DMA_INTST, INTSTS_IOIRQS);
  616. continue;
  617. }
  618. break;
  619. }
  620. /* clear start/stop bit */
  621. out_be32(bmid_base + SCC_DMA_CMD,
  622. in_be32(bmid_base + SCC_DMA_CMD) & ~ATA_DMA_START);
  623. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  624. ata_sff_dma_pause(ap); /* dummy read */
  625. }
  626. /**
  627. * scc_bmdma_status - Read PCI IDE BMDMA status
  628. * @ap: Port associated with this ATA transaction.
  629. */
  630. static u8 scc_bmdma_status (struct ata_port *ap)
  631. {
  632. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  633. u8 host_stat = in_be32(mmio + SCC_DMA_STATUS);
  634. u32 int_status = in_be32(mmio + SCC_DMA_INTST);
  635. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  636. static int retry = 0;
  637. /* return if IOS_SS is cleared */
  638. if (!(in_be32(mmio + SCC_DMA_CMD) & ATA_DMA_START))
  639. return host_stat;
  640. /* errata A252,A308 workaround: Step4 */
  641. if ((scc_check_altstatus(ap) & ATA_ERR)
  642. && (int_status & INTSTS_INTRQ))
  643. return (host_stat | ATA_DMA_INTR);
  644. /* errata A308 workaround Step5 */
  645. if (int_status & INTSTS_IOIRQS) {
  646. host_stat |= ATA_DMA_INTR;
  647. /* We don't check ATAPI DMA because it is limited to UDMA4 */
  648. if ((qc->tf.protocol == ATA_PROT_DMA &&
  649. qc->dev->xfer_mode > XFER_UDMA_4)) {
  650. if (!(int_status & INTSTS_ACTEINT)) {
  651. printk(KERN_WARNING "ata%u: operation failed (transfer data loss)\n",
  652. ap->print_id);
  653. host_stat |= ATA_DMA_ERR;
  654. if (retry++)
  655. ap->udma_mask &= ~(1 << qc->dev->xfer_mode);
  656. } else
  657. retry = 0;
  658. }
  659. }
  660. return host_stat;
  661. }
  662. /**
  663. * scc_data_xfer - Transfer data by PIO
  664. * @dev: device for this I/O
  665. * @buf: data buffer
  666. * @buflen: buffer length
  667. * @rw: read/write
  668. *
  669. * Note: Original code is ata_sff_data_xfer().
  670. */
  671. static unsigned int scc_data_xfer (struct ata_device *dev, unsigned char *buf,
  672. unsigned int buflen, int rw)
  673. {
  674. struct ata_port *ap = dev->link->ap;
  675. unsigned int words = buflen >> 1;
  676. unsigned int i;
  677. __le16 *buf16 = (__le16 *) buf;
  678. void __iomem *mmio = ap->ioaddr.data_addr;
  679. /* Transfer multiple of 2 bytes */
  680. if (rw == READ)
  681. for (i = 0; i < words; i++)
  682. buf16[i] = cpu_to_le16(in_be32(mmio));
  683. else
  684. for (i = 0; i < words; i++)
  685. out_be32(mmio, le16_to_cpu(buf16[i]));
  686. /* Transfer trailing 1 byte, if any. */
  687. if (unlikely(buflen & 0x01)) {
  688. __le16 align_buf[1] = { 0 };
  689. unsigned char *trailing_buf = buf + buflen - 1;
  690. if (rw == READ) {
  691. align_buf[0] = cpu_to_le16(in_be32(mmio));
  692. memcpy(trailing_buf, align_buf, 1);
  693. } else {
  694. memcpy(align_buf, trailing_buf, 1);
  695. out_be32(mmio, le16_to_cpu(align_buf[0]));
  696. }
  697. words++;
  698. }
  699. return words << 1;
  700. }
  701. /**
  702. * scc_postreset - standard postreset callback
  703. * @ap: the target ata_port
  704. * @classes: classes of attached devices
  705. *
  706. * Note: Original code is ata_sff_postreset().
  707. */
  708. static void scc_postreset(struct ata_link *link, unsigned int *classes)
  709. {
  710. struct ata_port *ap = link->ap;
  711. DPRINTK("ENTER\n");
  712. /* is double-select really necessary? */
  713. if (classes[0] != ATA_DEV_NONE)
  714. ap->ops->sff_dev_select(ap, 1);
  715. if (classes[1] != ATA_DEV_NONE)
  716. ap->ops->sff_dev_select(ap, 0);
  717. /* bail out if no device is present */
  718. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  719. DPRINTK("EXIT, no device\n");
  720. return;
  721. }
  722. /* set up device control */
  723. out_be32(ap->ioaddr.ctl_addr, ap->ctl);
  724. DPRINTK("EXIT\n");
  725. }
  726. /**
  727. * scc_irq_clear - Clear PCI IDE BMDMA interrupt.
  728. * @ap: Port associated with this ATA transaction.
  729. *
  730. * Note: Original code is ata_bmdma_irq_clear().
  731. */
  732. static void scc_irq_clear (struct ata_port *ap)
  733. {
  734. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  735. if (!mmio)
  736. return;
  737. out_be32(mmio + SCC_DMA_STATUS, in_be32(mmio + SCC_DMA_STATUS));
  738. }
  739. /**
  740. * scc_port_start - Set port up for dma.
  741. * @ap: Port to initialize
  742. *
  743. * Allocate space for PRD table using ata_bmdma_port_start().
  744. * Set PRD table address for PTERADD. (PRD Transfer End Read)
  745. */
  746. static int scc_port_start (struct ata_port *ap)
  747. {
  748. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  749. int rc;
  750. rc = ata_bmdma_port_start(ap);
  751. if (rc)
  752. return rc;
  753. out_be32(mmio + SCC_DMA_PTERADD, ap->bmdma_prd_dma);
  754. return 0;
  755. }
  756. /**
  757. * scc_port_stop - Undo scc_port_start()
  758. * @ap: Port to shut down
  759. *
  760. * Reset PTERADD.
  761. */
  762. static void scc_port_stop (struct ata_port *ap)
  763. {
  764. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  765. out_be32(mmio + SCC_DMA_PTERADD, 0);
  766. }
  767. static struct scsi_host_template scc_sht = {
  768. ATA_BMDMA_SHT(DRV_NAME),
  769. };
  770. static struct ata_port_operations scc_pata_ops = {
  771. .inherits = &ata_bmdma_port_ops,
  772. .set_piomode = scc_set_piomode,
  773. .set_dmamode = scc_set_dmamode,
  774. .mode_filter = scc_mode_filter,
  775. .sff_tf_load = scc_tf_load,
  776. .sff_tf_read = scc_tf_read,
  777. .sff_exec_command = scc_exec_command,
  778. .sff_check_status = scc_check_status,
  779. .sff_check_altstatus = scc_check_altstatus,
  780. .sff_dev_select = scc_dev_select,
  781. .sff_set_devctl = scc_set_devctl,
  782. .bmdma_setup = scc_bmdma_setup,
  783. .bmdma_start = scc_bmdma_start,
  784. .bmdma_stop = scc_bmdma_stop,
  785. .bmdma_status = scc_bmdma_status,
  786. .sff_data_xfer = scc_data_xfer,
  787. .cable_detect = ata_cable_80wire,
  788. .softreset = scc_softreset,
  789. .postreset = scc_postreset,
  790. .sff_irq_clear = scc_irq_clear,
  791. .port_start = scc_port_start,
  792. .port_stop = scc_port_stop,
  793. };
  794. static struct ata_port_info scc_port_info[] = {
  795. {
  796. .flags = ATA_FLAG_SLAVE_POSS,
  797. .pio_mask = ATA_PIO4,
  798. /* No MWDMA */
  799. .udma_mask = ATA_UDMA6,
  800. .port_ops = &scc_pata_ops,
  801. },
  802. };
  803. /**
  804. * scc_reset_controller - initialize SCC PATA controller.
  805. */
  806. static int scc_reset_controller(struct ata_host *host)
  807. {
  808. void __iomem *ctrl_base = host->iomap[SCC_CTRL_BAR];
  809. void __iomem *bmid_base = host->iomap[SCC_BMID_BAR];
  810. void __iomem *cckctrl_port = ctrl_base + SCC_CTL_CCKCTRL;
  811. void __iomem *mode_port = ctrl_base + SCC_CTL_MODEREG;
  812. void __iomem *ecmode_port = ctrl_base + SCC_CTL_ECMODE;
  813. void __iomem *intmask_port = bmid_base + SCC_DMA_INTMASK;
  814. void __iomem *dmastatus_port = bmid_base + SCC_DMA_STATUS;
  815. u32 reg = 0;
  816. out_be32(cckctrl_port, reg);
  817. reg |= CCKCTRL_ATACLKOEN;
  818. out_be32(cckctrl_port, reg);
  819. reg |= CCKCTRL_LCLKEN | CCKCTRL_OCLKEN;
  820. out_be32(cckctrl_port, reg);
  821. reg |= CCKCTRL_CRST;
  822. out_be32(cckctrl_port, reg);
  823. for (;;) {
  824. reg = in_be32(cckctrl_port);
  825. if (reg & CCKCTRL_CRST)
  826. break;
  827. udelay(5000);
  828. }
  829. reg |= CCKCTRL_ATARESET;
  830. out_be32(cckctrl_port, reg);
  831. out_be32(ecmode_port, ECMODE_VALUE);
  832. out_be32(mode_port, MODE_JCUSFEN);
  833. out_be32(intmask_port, INTMASK_MSK);
  834. if (in_be32(dmastatus_port) & QCHSD_STPDIAG) {
  835. printk(KERN_WARNING "%s: failed to detect 80c cable. (PDIAG# is high)\n", DRV_NAME);
  836. return -EIO;
  837. }
  838. return 0;
  839. }
  840. /**
  841. * scc_setup_ports - initialize ioaddr with SCC PATA port offsets.
  842. * @ioaddr: IO address structure to be initialized
  843. * @base: base address of BMID region
  844. */
  845. static void scc_setup_ports (struct ata_ioports *ioaddr, void __iomem *base)
  846. {
  847. ioaddr->cmd_addr = base + SCC_REG_CMD_ADDR;
  848. ioaddr->altstatus_addr = ioaddr->cmd_addr + SCC_REG_ALTSTATUS;
  849. ioaddr->ctl_addr = ioaddr->cmd_addr + SCC_REG_ALTSTATUS;
  850. ioaddr->bmdma_addr = base;
  851. ioaddr->data_addr = ioaddr->cmd_addr + SCC_REG_DATA;
  852. ioaddr->error_addr = ioaddr->cmd_addr + SCC_REG_ERR;
  853. ioaddr->feature_addr = ioaddr->cmd_addr + SCC_REG_FEATURE;
  854. ioaddr->nsect_addr = ioaddr->cmd_addr + SCC_REG_NSECT;
  855. ioaddr->lbal_addr = ioaddr->cmd_addr + SCC_REG_LBAL;
  856. ioaddr->lbam_addr = ioaddr->cmd_addr + SCC_REG_LBAM;
  857. ioaddr->lbah_addr = ioaddr->cmd_addr + SCC_REG_LBAH;
  858. ioaddr->device_addr = ioaddr->cmd_addr + SCC_REG_DEVICE;
  859. ioaddr->status_addr = ioaddr->cmd_addr + SCC_REG_STATUS;
  860. ioaddr->command_addr = ioaddr->cmd_addr + SCC_REG_CMD;
  861. }
  862. static int scc_host_init(struct ata_host *host)
  863. {
  864. struct pci_dev *pdev = to_pci_dev(host->dev);
  865. int rc;
  866. rc = scc_reset_controller(host);
  867. if (rc)
  868. return rc;
  869. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  870. if (rc)
  871. return rc;
  872. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  873. if (rc)
  874. return rc;
  875. scc_setup_ports(&host->ports[0]->ioaddr, host->iomap[SCC_BMID_BAR]);
  876. pci_set_master(pdev);
  877. return 0;
  878. }
  879. /**
  880. * scc_init_one - Register SCC PATA device with kernel services
  881. * @pdev: PCI device to register
  882. * @ent: Entry in scc_pci_tbl matching with @pdev
  883. *
  884. * LOCKING:
  885. * Inherited from PCI layer (may sleep).
  886. *
  887. * RETURNS:
  888. * Zero on success, or -ERRNO value.
  889. */
  890. static int scc_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  891. {
  892. unsigned int board_idx = (unsigned int) ent->driver_data;
  893. const struct ata_port_info *ppi[] = { &scc_port_info[board_idx], NULL };
  894. struct ata_host *host;
  895. int rc;
  896. ata_print_version_once(&pdev->dev, DRV_VERSION);
  897. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 1);
  898. if (!host)
  899. return -ENOMEM;
  900. rc = pcim_enable_device(pdev);
  901. if (rc)
  902. return rc;
  903. rc = pcim_iomap_regions(pdev, (1 << SCC_CTRL_BAR) | (1 << SCC_BMID_BAR), DRV_NAME);
  904. if (rc == -EBUSY)
  905. pcim_pin_device(pdev);
  906. if (rc)
  907. return rc;
  908. host->iomap = pcim_iomap_table(pdev);
  909. ata_port_pbar_desc(host->ports[0], SCC_CTRL_BAR, -1, "ctrl");
  910. ata_port_pbar_desc(host->ports[0], SCC_BMID_BAR, -1, "bmid");
  911. rc = scc_host_init(host);
  912. if (rc)
  913. return rc;
  914. return ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  915. IRQF_SHARED, &scc_sht);
  916. }
  917. static struct pci_driver scc_pci_driver = {
  918. .name = DRV_NAME,
  919. .id_table = scc_pci_tbl,
  920. .probe = scc_init_one,
  921. .remove = ata_pci_remove_one,
  922. #ifdef CONFIG_PM_SLEEP
  923. .suspend = ata_pci_device_suspend,
  924. .resume = ata_pci_device_resume,
  925. #endif
  926. };
  927. module_pci_driver(scc_pci_driver);
  928. MODULE_AUTHOR("Toshiba corp");
  929. MODULE_DESCRIPTION("SCSI low-level driver for Toshiba SCC PATA controller");
  930. MODULE_LICENSE("GPL");
  931. MODULE_DEVICE_TABLE(pci, scc_pci_tbl);
  932. MODULE_VERSION(DRV_VERSION);