sata_sil24.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers
  3. *
  4. * Copyright 2005 Tejun Heo
  5. *
  6. * Based on preview driver from Silicon Image.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <linux/libata.h>
  30. #include <asm/io.h>
  31. #define DRV_NAME "sata_sil24"
  32. #define DRV_VERSION "0.24"
  33. /*
  34. * Port request block (PRB) 32 bytes
  35. */
  36. struct sil24_prb {
  37. u16 ctrl;
  38. u16 prot;
  39. u32 rx_cnt;
  40. u8 fis[6 * 4];
  41. };
  42. /*
  43. * Scatter gather entry (SGE) 16 bytes
  44. */
  45. struct sil24_sge {
  46. u64 addr;
  47. u32 cnt;
  48. u32 flags;
  49. };
  50. /*
  51. * Port multiplier
  52. */
  53. struct sil24_port_multiplier {
  54. u32 diag;
  55. u32 sactive;
  56. };
  57. enum {
  58. /*
  59. * Global controller registers (128 bytes @ BAR0)
  60. */
  61. /* 32 bit regs */
  62. HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */
  63. HOST_CTRL = 0x40,
  64. HOST_IRQ_STAT = 0x44,
  65. HOST_PHY_CFG = 0x48,
  66. HOST_BIST_CTRL = 0x50,
  67. HOST_BIST_PTRN = 0x54,
  68. HOST_BIST_STAT = 0x58,
  69. HOST_MEM_BIST_STAT = 0x5c,
  70. HOST_FLASH_CMD = 0x70,
  71. /* 8 bit regs */
  72. HOST_FLASH_DATA = 0x74,
  73. HOST_TRANSITION_DETECT = 0x75,
  74. HOST_GPIO_CTRL = 0x76,
  75. HOST_I2C_ADDR = 0x78, /* 32 bit */
  76. HOST_I2C_DATA = 0x7c,
  77. HOST_I2C_XFER_CNT = 0x7e,
  78. HOST_I2C_CTRL = 0x7f,
  79. /* HOST_SLOT_STAT bits */
  80. HOST_SSTAT_ATTN = (1 << 31),
  81. /*
  82. * Port registers
  83. * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
  84. */
  85. PORT_REGS_SIZE = 0x2000,
  86. PORT_PRB = 0x0000, /* (32 bytes PRB + 16 bytes SGEs * 6) * 31 (3968 bytes) */
  87. PORT_PM = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */
  88. /* 32 bit regs */
  89. PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */
  90. PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */
  91. PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */
  92. PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */
  93. PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */
  94. PORT_ACTIVATE_UPPER_ADDR= 0x101c,
  95. PORT_EXEC_FIFO = 0x1020, /* command execution fifo */
  96. PORT_CMD_ERR = 0x1024, /* command error number */
  97. PORT_FIS_CFG = 0x1028,
  98. PORT_FIFO_THRES = 0x102c,
  99. /* 16 bit regs */
  100. PORT_DECODE_ERR_CNT = 0x1040,
  101. PORT_DECODE_ERR_THRESH = 0x1042,
  102. PORT_CRC_ERR_CNT = 0x1044,
  103. PORT_CRC_ERR_THRESH = 0x1046,
  104. PORT_HSHK_ERR_CNT = 0x1048,
  105. PORT_HSHK_ERR_THRESH = 0x104a,
  106. /* 32 bit regs */
  107. PORT_PHY_CFG = 0x1050,
  108. PORT_SLOT_STAT = 0x1800,
  109. PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
  110. PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
  111. PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
  112. PORT_SCONTROL = 0x1f00,
  113. PORT_SSTATUS = 0x1f04,
  114. PORT_SERROR = 0x1f08,
  115. PORT_SACTIVE = 0x1f0c,
  116. /* PORT_CTRL_STAT bits */
  117. PORT_CS_PORT_RST = (1 << 0), /* port reset */
  118. PORT_CS_DEV_RST = (1 << 1), /* device reset */
  119. PORT_CS_INIT = (1 << 2), /* port initialize */
  120. PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */
  121. PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */
  122. PORT_CS_RESUME = (1 << 6), /* port resume */
  123. PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */
  124. PORT_CS_PM_EN = (1 << 13), /* port multiplier enable */
  125. PORT_CS_RDY = (1 << 31), /* port ready to accept commands */
  126. /* PORT_IRQ_STAT/ENABLE_SET/CLR */
  127. /* bits[11:0] are masked */
  128. PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */
  129. PORT_IRQ_ERROR = (1 << 1), /* command execution error */
  130. PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */
  131. PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */
  132. PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */
  133. PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */
  134. PORT_IRQ_UNK_FIS = (1 << 6), /* Unknown FIS received */
  135. PORT_IRQ_SDB_FIS = (1 << 11), /* SDB FIS received */
  136. /* bits[27:16] are unmasked (raw) */
  137. PORT_IRQ_RAW_SHIFT = 16,
  138. PORT_IRQ_MASKED_MASK = 0x7ff,
  139. PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT),
  140. /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
  141. PORT_IRQ_STEER_SHIFT = 30,
  142. PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT),
  143. /* PORT_CMD_ERR constants */
  144. PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */
  145. PORT_CERR_SDB = 2, /* Error bit in SDB FIS */
  146. PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */
  147. PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */
  148. PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */
  149. PORT_CERR_DIRECTION = 6, /* Data direction mismatch */
  150. PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */
  151. PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */
  152. PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */
  153. PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */
  154. PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */
  155. PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */
  156. PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
  157. PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
  158. PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */
  159. PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */
  160. PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
  161. PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */
  162. PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */
  163. PORT_CERR_XFR_MSTABRT = 34, /* PSD ecode 10 - master abort */
  164. PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */
  165. PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */
  166. /* bits of PRB control field */
  167. PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */
  168. PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */
  169. PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */
  170. PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */
  171. PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */
  172. /* PRB protocol field */
  173. PRB_PROT_PACKET = (1 << 0),
  174. PRB_PROT_TCQ = (1 << 1),
  175. PRB_PROT_NCQ = (1 << 2),
  176. PRB_PROT_READ = (1 << 3),
  177. PRB_PROT_WRITE = (1 << 4),
  178. PRB_PROT_TRANSPARENT = (1 << 5),
  179. /*
  180. * Other constants
  181. */
  182. SGE_TRM = (1 << 31), /* Last SGE in chain */
  183. SGE_LNK = (1 << 30), /* linked list
  184. Points to SGT, not SGE */
  185. SGE_DRD = (1 << 29), /* discard data read (/dev/null)
  186. data address ignored */
  187. /* board id */
  188. BID_SIL3124 = 0,
  189. BID_SIL3132 = 1,
  190. BID_SIL3131 = 2,
  191. IRQ_STAT_4PORTS = 0xf,
  192. };
  193. struct sil24_ata_block {
  194. struct sil24_prb prb;
  195. struct sil24_sge sge[LIBATA_MAX_PRD];
  196. };
  197. struct sil24_atapi_block {
  198. struct sil24_prb prb;
  199. u8 cdb[16];
  200. struct sil24_sge sge[LIBATA_MAX_PRD - 1];
  201. };
  202. union sil24_cmd_block {
  203. struct sil24_ata_block ata;
  204. struct sil24_atapi_block atapi;
  205. };
  206. /*
  207. * ap->private_data
  208. *
  209. * The preview driver always returned 0 for status. We emulate it
  210. * here from the previous interrupt.
  211. */
  212. struct sil24_port_priv {
  213. union sil24_cmd_block *cmd_block; /* 32 cmd blocks */
  214. dma_addr_t cmd_block_dma; /* DMA base addr for them */
  215. struct ata_taskfile tf; /* Cached taskfile registers */
  216. };
  217. /* ap->host_set->private_data */
  218. struct sil24_host_priv {
  219. void __iomem *host_base; /* global controller control (128 bytes @BAR0) */
  220. void __iomem *port_base; /* port registers (4 * 8192 bytes @BAR2) */
  221. };
  222. static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev);
  223. static u8 sil24_check_status(struct ata_port *ap);
  224. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg);
  225. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
  226. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  227. static int sil24_probe_reset(struct ata_port *ap, unsigned int *classes);
  228. static void sil24_qc_prep(struct ata_queued_cmd *qc);
  229. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc);
  230. static void sil24_irq_clear(struct ata_port *ap);
  231. static void sil24_eng_timeout(struct ata_port *ap);
  232. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
  233. static int sil24_port_start(struct ata_port *ap);
  234. static void sil24_port_stop(struct ata_port *ap);
  235. static void sil24_host_stop(struct ata_host_set *host_set);
  236. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  237. static const struct pci_device_id sil24_pci_tbl[] = {
  238. { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
  239. { 0x8086, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
  240. { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 },
  241. { 0x1095, 0x3131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  242. { 0x1095, 0x3531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  243. { } /* terminate list */
  244. };
  245. static struct pci_driver sil24_pci_driver = {
  246. .name = DRV_NAME,
  247. .id_table = sil24_pci_tbl,
  248. .probe = sil24_init_one,
  249. .remove = ata_pci_remove_one, /* safe? */
  250. };
  251. static struct scsi_host_template sil24_sht = {
  252. .module = THIS_MODULE,
  253. .name = DRV_NAME,
  254. .ioctl = ata_scsi_ioctl,
  255. .queuecommand = ata_scsi_queuecmd,
  256. .can_queue = ATA_DEF_QUEUE,
  257. .this_id = ATA_SHT_THIS_ID,
  258. .sg_tablesize = LIBATA_MAX_PRD,
  259. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  260. .emulated = ATA_SHT_EMULATED,
  261. .use_clustering = ATA_SHT_USE_CLUSTERING,
  262. .proc_name = DRV_NAME,
  263. .dma_boundary = ATA_DMA_BOUNDARY,
  264. .slave_configure = ata_scsi_slave_config,
  265. .bios_param = ata_std_bios_param,
  266. };
  267. static const struct ata_port_operations sil24_ops = {
  268. .port_disable = ata_port_disable,
  269. .dev_config = sil24_dev_config,
  270. .check_status = sil24_check_status,
  271. .check_altstatus = sil24_check_status,
  272. .dev_select = ata_noop_dev_select,
  273. .tf_read = sil24_tf_read,
  274. .probe_reset = sil24_probe_reset,
  275. .qc_prep = sil24_qc_prep,
  276. .qc_issue = sil24_qc_issue,
  277. .eng_timeout = sil24_eng_timeout,
  278. .irq_handler = sil24_interrupt,
  279. .irq_clear = sil24_irq_clear,
  280. .scr_read = sil24_scr_read,
  281. .scr_write = sil24_scr_write,
  282. .port_start = sil24_port_start,
  283. .port_stop = sil24_port_stop,
  284. .host_stop = sil24_host_stop,
  285. };
  286. /*
  287. * Use bits 30-31 of host_flags to encode available port numbers.
  288. * Current maxium is 4.
  289. */
  290. #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30)
  291. #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1)
  292. static struct ata_port_info sil24_port_info[] = {
  293. /* sil_3124 */
  294. {
  295. .sht = &sil24_sht,
  296. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  297. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  298. SIL24_NPORTS2FLAG(4),
  299. .pio_mask = 0x1f, /* pio0-4 */
  300. .mwdma_mask = 0x07, /* mwdma0-2 */
  301. .udma_mask = 0x3f, /* udma0-5 */
  302. .port_ops = &sil24_ops,
  303. },
  304. /* sil_3132 */
  305. {
  306. .sht = &sil24_sht,
  307. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  308. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  309. SIL24_NPORTS2FLAG(2),
  310. .pio_mask = 0x1f, /* pio0-4 */
  311. .mwdma_mask = 0x07, /* mwdma0-2 */
  312. .udma_mask = 0x3f, /* udma0-5 */
  313. .port_ops = &sil24_ops,
  314. },
  315. /* sil_3131/sil_3531 */
  316. {
  317. .sht = &sil24_sht,
  318. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  319. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  320. SIL24_NPORTS2FLAG(1),
  321. .pio_mask = 0x1f, /* pio0-4 */
  322. .mwdma_mask = 0x07, /* mwdma0-2 */
  323. .udma_mask = 0x3f, /* udma0-5 */
  324. .port_ops = &sil24_ops,
  325. },
  326. };
  327. static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev)
  328. {
  329. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  330. if (dev->cdb_len == 16)
  331. writel(PORT_CS_CDB16, port + PORT_CTRL_STAT);
  332. else
  333. writel(PORT_CS_CDB16, port + PORT_CTRL_CLR);
  334. }
  335. static inline void sil24_update_tf(struct ata_port *ap)
  336. {
  337. struct sil24_port_priv *pp = ap->private_data;
  338. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  339. struct sil24_prb __iomem *prb = port;
  340. u8 fis[6 * 4];
  341. memcpy_fromio(fis, prb->fis, 6 * 4);
  342. ata_tf_from_fis(fis, &pp->tf);
  343. }
  344. static u8 sil24_check_status(struct ata_port *ap)
  345. {
  346. struct sil24_port_priv *pp = ap->private_data;
  347. return pp->tf.command;
  348. }
  349. static int sil24_scr_map[] = {
  350. [SCR_CONTROL] = 0,
  351. [SCR_STATUS] = 1,
  352. [SCR_ERROR] = 2,
  353. [SCR_ACTIVE] = 3,
  354. };
  355. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg)
  356. {
  357. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  358. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  359. void __iomem *addr;
  360. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  361. return readl(scr_addr + sil24_scr_map[sc_reg] * 4);
  362. }
  363. return 0xffffffffU;
  364. }
  365. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  366. {
  367. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  368. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  369. void __iomem *addr;
  370. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  371. writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
  372. }
  373. }
  374. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  375. {
  376. struct sil24_port_priv *pp = ap->private_data;
  377. *tf = pp->tf;
  378. }
  379. static int sil24_softreset(struct ata_port *ap, unsigned int *class)
  380. {
  381. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  382. struct sil24_port_priv *pp = ap->private_data;
  383. struct sil24_prb *prb = &pp->cmd_block[0].ata.prb;
  384. dma_addr_t paddr = pp->cmd_block_dma;
  385. u32 mask, irq_enable, irq_stat;
  386. const char *reason;
  387. DPRINTK("ENTER\n");
  388. if (!sata_dev_present(ap)) {
  389. DPRINTK("PHY reports no device\n");
  390. *class = ATA_DEV_NONE;
  391. goto out;
  392. }
  393. /* temporarily turn off IRQs during SRST */
  394. irq_enable = readl(port + PORT_IRQ_ENABLE_SET);
  395. writel(irq_enable, port + PORT_IRQ_ENABLE_CLR);
  396. /*
  397. * XXX: Not sure whether the following sleep is needed or not.
  398. * The original driver had it. So....
  399. */
  400. msleep(10);
  401. prb->ctrl = PRB_CTRL_SRST;
  402. prb->fis[1] = 0; /* no PM yet */
  403. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  404. mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
  405. irq_stat = ata_wait_register(port + PORT_IRQ_STAT, mask, 0x0,
  406. 100, ATA_TMOUT_BOOT / HZ * 1000);
  407. writel(irq_stat, port + PORT_IRQ_STAT); /* clear IRQs */
  408. irq_stat >>= PORT_IRQ_RAW_SHIFT;
  409. /* restore IRQs */
  410. writel(irq_enable, port + PORT_IRQ_ENABLE_SET);
  411. if (!(irq_stat & PORT_IRQ_COMPLETE)) {
  412. if (irq_stat & PORT_IRQ_ERROR)
  413. reason = "SRST command error";
  414. else
  415. reason = "timeout";
  416. goto err;
  417. }
  418. sil24_update_tf(ap);
  419. *class = ata_dev_classify(&pp->tf);
  420. if (*class == ATA_DEV_UNKNOWN)
  421. *class = ATA_DEV_NONE;
  422. out:
  423. DPRINTK("EXIT, class=%u\n", *class);
  424. return 0;
  425. err:
  426. printk(KERN_ERR "ata%u: softreset failed (%s)\n", ap->id, reason);
  427. return -EIO;
  428. }
  429. static int sil24_hardreset(struct ata_port *ap, unsigned int *class)
  430. {
  431. unsigned int dummy_class;
  432. /* sil24 doesn't report device signature after hard reset */
  433. return sata_std_hardreset(ap, &dummy_class);
  434. }
  435. static int sil24_probe_reset(struct ata_port *ap, unsigned int *classes)
  436. {
  437. return ata_drive_probe_reset(ap, ata_std_probeinit,
  438. sil24_softreset, sil24_hardreset,
  439. ata_std_postreset, classes);
  440. }
  441. static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
  442. struct sil24_sge *sge)
  443. {
  444. struct scatterlist *sg;
  445. unsigned int idx = 0;
  446. ata_for_each_sg(sg, qc) {
  447. sge->addr = cpu_to_le64(sg_dma_address(sg));
  448. sge->cnt = cpu_to_le32(sg_dma_len(sg));
  449. if (ata_sg_is_last(sg, qc))
  450. sge->flags = cpu_to_le32(SGE_TRM);
  451. else
  452. sge->flags = 0;
  453. sge++;
  454. idx++;
  455. }
  456. }
  457. static void sil24_qc_prep(struct ata_queued_cmd *qc)
  458. {
  459. struct ata_port *ap = qc->ap;
  460. struct sil24_port_priv *pp = ap->private_data;
  461. union sil24_cmd_block *cb = pp->cmd_block + qc->tag;
  462. struct sil24_prb *prb;
  463. struct sil24_sge *sge;
  464. switch (qc->tf.protocol) {
  465. case ATA_PROT_PIO:
  466. case ATA_PROT_DMA:
  467. case ATA_PROT_NODATA:
  468. prb = &cb->ata.prb;
  469. sge = cb->ata.sge;
  470. prb->ctrl = 0;
  471. break;
  472. case ATA_PROT_ATAPI:
  473. case ATA_PROT_ATAPI_DMA:
  474. case ATA_PROT_ATAPI_NODATA:
  475. prb = &cb->atapi.prb;
  476. sge = cb->atapi.sge;
  477. memset(cb->atapi.cdb, 0, 32);
  478. memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len);
  479. if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) {
  480. if (qc->tf.flags & ATA_TFLAG_WRITE)
  481. prb->ctrl = PRB_CTRL_PACKET_WRITE;
  482. else
  483. prb->ctrl = PRB_CTRL_PACKET_READ;
  484. } else
  485. prb->ctrl = 0;
  486. break;
  487. default:
  488. prb = NULL; /* shut up, gcc */
  489. sge = NULL;
  490. BUG();
  491. }
  492. ata_tf_to_fis(&qc->tf, prb->fis, 0);
  493. if (qc->flags & ATA_QCFLAG_DMAMAP)
  494. sil24_fill_sg(qc, sge);
  495. }
  496. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
  497. {
  498. struct ata_port *ap = qc->ap;
  499. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  500. struct sil24_port_priv *pp = ap->private_data;
  501. dma_addr_t paddr = pp->cmd_block_dma + qc->tag * sizeof(*pp->cmd_block);
  502. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  503. return 0;
  504. }
  505. static void sil24_irq_clear(struct ata_port *ap)
  506. {
  507. /* unused */
  508. }
  509. static int __sil24_restart_controller(void __iomem *port)
  510. {
  511. u32 tmp;
  512. int cnt;
  513. writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
  514. /* Max ~10ms */
  515. for (cnt = 0; cnt < 10000; cnt++) {
  516. tmp = readl(port + PORT_CTRL_STAT);
  517. if (tmp & PORT_CS_RDY)
  518. return 0;
  519. udelay(1);
  520. }
  521. return -1;
  522. }
  523. static void sil24_restart_controller(struct ata_port *ap)
  524. {
  525. if (__sil24_restart_controller((void __iomem *)ap->ioaddr.cmd_addr))
  526. printk(KERN_ERR DRV_NAME
  527. " ata%u: failed to restart controller\n", ap->id);
  528. }
  529. static int __sil24_reset_controller(void __iomem *port)
  530. {
  531. int cnt;
  532. u32 tmp;
  533. /* Reset controller state. Is this correct? */
  534. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  535. readl(port + PORT_CTRL_STAT); /* sync */
  536. /* Max ~100ms */
  537. for (cnt = 0; cnt < 1000; cnt++) {
  538. udelay(100);
  539. tmp = readl(port + PORT_CTRL_STAT);
  540. if (!(tmp & PORT_CS_DEV_RST))
  541. break;
  542. }
  543. if (tmp & PORT_CS_DEV_RST)
  544. return -1;
  545. if (tmp & PORT_CS_RDY)
  546. return 0;
  547. return __sil24_restart_controller(port);
  548. }
  549. static void sil24_reset_controller(struct ata_port *ap)
  550. {
  551. printk(KERN_NOTICE DRV_NAME
  552. " ata%u: resetting controller...\n", ap->id);
  553. if (__sil24_reset_controller((void __iomem *)ap->ioaddr.cmd_addr))
  554. printk(KERN_ERR DRV_NAME
  555. " ata%u: failed to reset controller\n", ap->id);
  556. }
  557. static void sil24_eng_timeout(struct ata_port *ap)
  558. {
  559. struct ata_queued_cmd *qc;
  560. qc = ata_qc_from_tag(ap, ap->active_tag);
  561. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  562. qc->err_mask |= AC_ERR_TIMEOUT;
  563. ata_eh_qc_complete(qc);
  564. sil24_reset_controller(ap);
  565. }
  566. static void sil24_error_intr(struct ata_port *ap, u32 slot_stat)
  567. {
  568. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  569. struct sil24_port_priv *pp = ap->private_data;
  570. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  571. u32 irq_stat, cmd_err, sstatus, serror;
  572. unsigned int err_mask;
  573. irq_stat = readl(port + PORT_IRQ_STAT);
  574. writel(irq_stat, port + PORT_IRQ_STAT); /* clear irq */
  575. if (!(irq_stat & PORT_IRQ_ERROR)) {
  576. /* ignore non-completion, non-error irqs for now */
  577. printk(KERN_WARNING DRV_NAME
  578. "ata%u: non-error exception irq (irq_stat %x)\n",
  579. ap->id, irq_stat);
  580. return;
  581. }
  582. cmd_err = readl(port + PORT_CMD_ERR);
  583. sstatus = readl(port + PORT_SSTATUS);
  584. serror = readl(port + PORT_SERROR);
  585. if (serror)
  586. writel(serror, port + PORT_SERROR);
  587. /*
  588. * Don't log ATAPI device errors. They're supposed to happen
  589. * and any serious errors will be logged using sense data by
  590. * the SCSI layer.
  591. */
  592. if (ap->device[0].class != ATA_DEV_ATAPI || cmd_err > PORT_CERR_SDB)
  593. printk("ata%u: error interrupt on port%d\n"
  594. " stat=0x%x irq=0x%x cmd_err=%d sstatus=0x%x serror=0x%x\n",
  595. ap->id, ap->port_no, slot_stat, irq_stat, cmd_err, sstatus, serror);
  596. if (cmd_err == PORT_CERR_DEV || cmd_err == PORT_CERR_SDB) {
  597. /*
  598. * Device is reporting error, tf registers are valid.
  599. */
  600. sil24_update_tf(ap);
  601. err_mask = ac_err_mask(pp->tf.command);
  602. sil24_restart_controller(ap);
  603. } else {
  604. /*
  605. * Other errors. libata currently doesn't have any
  606. * mechanism to report these errors. Just turn on
  607. * ATA_ERR.
  608. */
  609. err_mask = AC_ERR_OTHER;
  610. sil24_reset_controller(ap);
  611. }
  612. if (qc) {
  613. qc->err_mask |= err_mask;
  614. ata_qc_complete(qc);
  615. }
  616. }
  617. static inline void sil24_host_intr(struct ata_port *ap)
  618. {
  619. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  620. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  621. u32 slot_stat;
  622. slot_stat = readl(port + PORT_SLOT_STAT);
  623. if (!(slot_stat & HOST_SSTAT_ATTN)) {
  624. struct sil24_port_priv *pp = ap->private_data;
  625. /*
  626. * !HOST_SSAT_ATTN guarantees successful completion,
  627. * so reading back tf registers is unnecessary for
  628. * most commands. TODO: read tf registers for
  629. * commands which require these values on successful
  630. * completion (EXECUTE DEVICE DIAGNOSTIC, CHECK POWER,
  631. * DEVICE RESET and READ PORT MULTIPLIER (any more?).
  632. */
  633. sil24_update_tf(ap);
  634. if (qc) {
  635. qc->err_mask |= ac_err_mask(pp->tf.command);
  636. ata_qc_complete(qc);
  637. }
  638. } else
  639. sil24_error_intr(ap, slot_stat);
  640. }
  641. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  642. {
  643. struct ata_host_set *host_set = dev_instance;
  644. struct sil24_host_priv *hpriv = host_set->private_data;
  645. unsigned handled = 0;
  646. u32 status;
  647. int i;
  648. status = readl(hpriv->host_base + HOST_IRQ_STAT);
  649. if (status == 0xffffffff) {
  650. printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
  651. "PCI fault or device removal?\n");
  652. goto out;
  653. }
  654. if (!(status & IRQ_STAT_4PORTS))
  655. goto out;
  656. spin_lock(&host_set->lock);
  657. for (i = 0; i < host_set->n_ports; i++)
  658. if (status & (1 << i)) {
  659. struct ata_port *ap = host_set->ports[i];
  660. if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
  661. sil24_host_intr(host_set->ports[i]);
  662. handled++;
  663. } else
  664. printk(KERN_ERR DRV_NAME
  665. ": interrupt from disabled port %d\n", i);
  666. }
  667. spin_unlock(&host_set->lock);
  668. out:
  669. return IRQ_RETVAL(handled);
  670. }
  671. static inline void sil24_cblk_free(struct sil24_port_priv *pp, struct device *dev)
  672. {
  673. const size_t cb_size = sizeof(*pp->cmd_block);
  674. dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma);
  675. }
  676. static int sil24_port_start(struct ata_port *ap)
  677. {
  678. struct device *dev = ap->host_set->dev;
  679. struct sil24_port_priv *pp;
  680. union sil24_cmd_block *cb;
  681. size_t cb_size = sizeof(*cb);
  682. dma_addr_t cb_dma;
  683. int rc = -ENOMEM;
  684. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  685. if (!pp)
  686. goto err_out;
  687. pp->tf.command = ATA_DRDY;
  688. cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
  689. if (!cb)
  690. goto err_out_pp;
  691. memset(cb, 0, cb_size);
  692. rc = ata_pad_alloc(ap, dev);
  693. if (rc)
  694. goto err_out_pad;
  695. pp->cmd_block = cb;
  696. pp->cmd_block_dma = cb_dma;
  697. ap->private_data = pp;
  698. return 0;
  699. err_out_pad:
  700. sil24_cblk_free(pp, dev);
  701. err_out_pp:
  702. kfree(pp);
  703. err_out:
  704. return rc;
  705. }
  706. static void sil24_port_stop(struct ata_port *ap)
  707. {
  708. struct device *dev = ap->host_set->dev;
  709. struct sil24_port_priv *pp = ap->private_data;
  710. sil24_cblk_free(pp, dev);
  711. ata_pad_free(ap, dev);
  712. kfree(pp);
  713. }
  714. static void sil24_host_stop(struct ata_host_set *host_set)
  715. {
  716. struct sil24_host_priv *hpriv = host_set->private_data;
  717. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  718. pci_iounmap(pdev, hpriv->host_base);
  719. pci_iounmap(pdev, hpriv->port_base);
  720. kfree(hpriv);
  721. }
  722. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  723. {
  724. static int printed_version = 0;
  725. unsigned int board_id = (unsigned int)ent->driver_data;
  726. struct ata_port_info *pinfo = &sil24_port_info[board_id];
  727. struct ata_probe_ent *probe_ent = NULL;
  728. struct sil24_host_priv *hpriv = NULL;
  729. void __iomem *host_base = NULL;
  730. void __iomem *port_base = NULL;
  731. int i, rc;
  732. if (!printed_version++)
  733. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  734. rc = pci_enable_device(pdev);
  735. if (rc)
  736. return rc;
  737. rc = pci_request_regions(pdev, DRV_NAME);
  738. if (rc)
  739. goto out_disable;
  740. rc = -ENOMEM;
  741. /* map mmio registers */
  742. host_base = pci_iomap(pdev, 0, 0);
  743. if (!host_base)
  744. goto out_free;
  745. port_base = pci_iomap(pdev, 2, 0);
  746. if (!port_base)
  747. goto out_free;
  748. /* allocate & init probe_ent and hpriv */
  749. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  750. if (!probe_ent)
  751. goto out_free;
  752. hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
  753. if (!hpriv)
  754. goto out_free;
  755. probe_ent->dev = pci_dev_to_dev(pdev);
  756. INIT_LIST_HEAD(&probe_ent->node);
  757. probe_ent->sht = pinfo->sht;
  758. probe_ent->host_flags = pinfo->host_flags;
  759. probe_ent->pio_mask = pinfo->pio_mask;
  760. probe_ent->mwdma_mask = pinfo->mwdma_mask;
  761. probe_ent->udma_mask = pinfo->udma_mask;
  762. probe_ent->port_ops = pinfo->port_ops;
  763. probe_ent->n_ports = SIL24_FLAG2NPORTS(pinfo->host_flags);
  764. probe_ent->irq = pdev->irq;
  765. probe_ent->irq_flags = SA_SHIRQ;
  766. probe_ent->mmio_base = port_base;
  767. probe_ent->private_data = hpriv;
  768. hpriv->host_base = host_base;
  769. hpriv->port_base = port_base;
  770. /*
  771. * Configure the device
  772. */
  773. /*
  774. * FIXME: This device is certainly 64-bit capable. We just
  775. * don't know how to use it. After fixing 32bit activation in
  776. * this function, enable 64bit masks here.
  777. */
  778. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  779. if (rc) {
  780. dev_printk(KERN_ERR, &pdev->dev,
  781. "32-bit DMA enable failed\n");
  782. goto out_free;
  783. }
  784. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  785. if (rc) {
  786. dev_printk(KERN_ERR, &pdev->dev,
  787. "32-bit consistent DMA enable failed\n");
  788. goto out_free;
  789. }
  790. /* GPIO off */
  791. writel(0, host_base + HOST_FLASH_CMD);
  792. /* clear global reset & mask interrupts during initialization */
  793. writel(0, host_base + HOST_CTRL);
  794. for (i = 0; i < probe_ent->n_ports; i++) {
  795. void __iomem *port = port_base + i * PORT_REGS_SIZE;
  796. unsigned long portu = (unsigned long)port;
  797. u32 tmp;
  798. probe_ent->port[i].cmd_addr = portu + PORT_PRB;
  799. probe_ent->port[i].scr_addr = portu + PORT_SCONTROL;
  800. ata_std_ports(&probe_ent->port[i]);
  801. /* Initial PHY setting */
  802. writel(0x20c, port + PORT_PHY_CFG);
  803. /* Clear port RST */
  804. tmp = readl(port + PORT_CTRL_STAT);
  805. if (tmp & PORT_CS_PORT_RST) {
  806. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  807. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  808. PORT_CS_PORT_RST,
  809. PORT_CS_PORT_RST, 10, 100);
  810. if (tmp & PORT_CS_PORT_RST)
  811. dev_printk(KERN_ERR, &pdev->dev,
  812. "failed to clear port RST\n");
  813. }
  814. /* Zero error counters. */
  815. writel(0x8000, port + PORT_DECODE_ERR_THRESH);
  816. writel(0x8000, port + PORT_CRC_ERR_THRESH);
  817. writel(0x8000, port + PORT_HSHK_ERR_THRESH);
  818. writel(0x0000, port + PORT_DECODE_ERR_CNT);
  819. writel(0x0000, port + PORT_CRC_ERR_CNT);
  820. writel(0x0000, port + PORT_HSHK_ERR_CNT);
  821. /* FIXME: 32bit activation? */
  822. writel(0, port + PORT_ACTIVATE_UPPER_ADDR);
  823. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_STAT);
  824. /* Configure interrupts */
  825. writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
  826. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | PORT_IRQ_SDB_FIS,
  827. port + PORT_IRQ_ENABLE_SET);
  828. /* Clear interrupts */
  829. writel(0x0fff0fff, port + PORT_IRQ_STAT);
  830. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  831. /* Clear port multiplier enable and resume bits */
  832. writel(PORT_CS_PM_EN | PORT_CS_RESUME, port + PORT_CTRL_CLR);
  833. /* Reset itself */
  834. if (__sil24_reset_controller(port))
  835. dev_printk(KERN_ERR, &pdev->dev,
  836. "failed to reset controller\n");
  837. }
  838. /* Turn on interrupts */
  839. writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
  840. pci_set_master(pdev);
  841. /* FIXME: check ata_device_add return value */
  842. ata_device_add(probe_ent);
  843. kfree(probe_ent);
  844. return 0;
  845. out_free:
  846. if (host_base)
  847. pci_iounmap(pdev, host_base);
  848. if (port_base)
  849. pci_iounmap(pdev, port_base);
  850. kfree(probe_ent);
  851. kfree(hpriv);
  852. pci_release_regions(pdev);
  853. out_disable:
  854. pci_disable_device(pdev);
  855. return rc;
  856. }
  857. static int __init sil24_init(void)
  858. {
  859. return pci_module_init(&sil24_pci_driver);
  860. }
  861. static void __exit sil24_exit(void)
  862. {
  863. pci_unregister_driver(&sil24_pci_driver);
  864. }
  865. MODULE_AUTHOR("Tejun Heo");
  866. MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
  867. MODULE_LICENSE("GPL");
  868. MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
  869. module_init(sil24_init);
  870. module_exit(sil24_exit);