sata_sil24.c 28 KB

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