sata_sil24.c 24 KB

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