sata_fsl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * drivers/ata/sata_fsl.c
  3. *
  4. * Freescale 3.0Gbps SATA device driver
  5. *
  6. * Author: Ashish Kalra <ashish.kalra@freescale.com>
  7. * Li Yang <leoli@freescale.com>
  8. *
  9. * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <scsi/scsi_host.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <linux/libata.h>
  24. #include <asm/io.h>
  25. #include <linux/of_platform.h>
  26. /* Controller information */
  27. enum {
  28. SATA_FSL_QUEUE_DEPTH = 16,
  29. SATA_FSL_MAX_PRD = 63,
  30. SATA_FSL_MAX_PRD_USABLE = SATA_FSL_MAX_PRD - 1,
  31. SATA_FSL_MAX_PRD_DIRECT = 16, /* Direct PRDT entries */
  32. SATA_FSL_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_PIO_DMA |
  33. ATA_FLAG_PMP | ATA_FLAG_NCQ | ATA_FLAG_AN),
  34. SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
  35. SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
  36. SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
  37. /*
  38. * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
  39. * chained indirect PRDEs upto a max count of 63.
  40. * We are allocating an array of 63 PRDEs contiguously, but PRDE#15 will
  41. * be setup as an indirect descriptor, pointing to it's next
  42. * (contiguous) PRDE. Though chained indirect PRDE arrays are
  43. * supported,it will be more efficient to use a direct PRDT and
  44. * a single chain/link to indirect PRDE array/PRDT.
  45. */
  46. SATA_FSL_CMD_DESC_CFIS_SZ = 32,
  47. SATA_FSL_CMD_DESC_SFIS_SZ = 32,
  48. SATA_FSL_CMD_DESC_ACMD_SZ = 16,
  49. SATA_FSL_CMD_DESC_RSRVD = 16,
  50. SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
  51. SATA_FSL_CMD_DESC_SFIS_SZ +
  52. SATA_FSL_CMD_DESC_ACMD_SZ +
  53. SATA_FSL_CMD_DESC_RSRVD +
  54. SATA_FSL_MAX_PRD * 16),
  55. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
  56. (SATA_FSL_CMD_DESC_CFIS_SZ +
  57. SATA_FSL_CMD_DESC_SFIS_SZ +
  58. SATA_FSL_CMD_DESC_ACMD_SZ +
  59. SATA_FSL_CMD_DESC_RSRVD),
  60. SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
  61. SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
  62. SATA_FSL_CMD_DESC_AR_SZ),
  63. /*
  64. * MPC8315 has two SATA controllers, SATA1 & SATA2
  65. * (one port per controller)
  66. * MPC837x has 2/4 controllers, one port per controller
  67. */
  68. SATA_FSL_MAX_PORTS = 1,
  69. SATA_FSL_IRQ_FLAG = IRQF_SHARED,
  70. };
  71. /*
  72. * Host Controller command register set - per port
  73. */
  74. enum {
  75. CQ = 0,
  76. CA = 8,
  77. CC = 0x10,
  78. CE = 0x18,
  79. DE = 0x20,
  80. CHBA = 0x24,
  81. HSTATUS = 0x28,
  82. HCONTROL = 0x2C,
  83. CQPMP = 0x30,
  84. SIGNATURE = 0x34,
  85. ICC = 0x38,
  86. /*
  87. * Host Status Register (HStatus) bitdefs
  88. */
  89. ONLINE = (1 << 31),
  90. GOING_OFFLINE = (1 << 30),
  91. BIST_ERR = (1 << 29),
  92. FATAL_ERR_HC_MASTER_ERR = (1 << 18),
  93. FATAL_ERR_PARITY_ERR_TX = (1 << 17),
  94. FATAL_ERR_PARITY_ERR_RX = (1 << 16),
  95. FATAL_ERR_DATA_UNDERRUN = (1 << 13),
  96. FATAL_ERR_DATA_OVERRUN = (1 << 12),
  97. FATAL_ERR_CRC_ERR_TX = (1 << 11),
  98. FATAL_ERR_CRC_ERR_RX = (1 << 10),
  99. FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
  100. FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
  101. FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
  102. FATAL_ERR_PARITY_ERR_TX |
  103. FATAL_ERR_PARITY_ERR_RX |
  104. FATAL_ERR_DATA_UNDERRUN |
  105. FATAL_ERR_DATA_OVERRUN |
  106. FATAL_ERR_CRC_ERR_TX |
  107. FATAL_ERR_CRC_ERR_RX |
  108. FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
  109. INT_ON_FATAL_ERR = (1 << 5),
  110. INT_ON_PHYRDY_CHG = (1 << 4),
  111. INT_ON_SIGNATURE_UPDATE = (1 << 3),
  112. INT_ON_SNOTIFY_UPDATE = (1 << 2),
  113. INT_ON_SINGL_DEVICE_ERR = (1 << 1),
  114. INT_ON_CMD_COMPLETE = 1,
  115. INT_ON_ERROR = INT_ON_FATAL_ERR | INT_ON_SNOTIFY_UPDATE |
  116. INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
  117. /*
  118. * Host Control Register (HControl) bitdefs
  119. */
  120. HCONTROL_ONLINE_PHY_RST = (1 << 31),
  121. HCONTROL_FORCE_OFFLINE = (1 << 30),
  122. HCONTROL_PARITY_PROT_MOD = (1 << 14),
  123. HCONTROL_DPATH_PARITY = (1 << 12),
  124. HCONTROL_SNOOP_ENABLE = (1 << 10),
  125. HCONTROL_PMP_ATTACHED = (1 << 9),
  126. HCONTROL_COPYOUT_STATFIS = (1 << 8),
  127. IE_ON_FATAL_ERR = (1 << 5),
  128. IE_ON_PHYRDY_CHG = (1 << 4),
  129. IE_ON_SIGNATURE_UPDATE = (1 << 3),
  130. IE_ON_SNOTIFY_UPDATE = (1 << 2),
  131. IE_ON_SINGL_DEVICE_ERR = (1 << 1),
  132. IE_ON_CMD_COMPLETE = 1,
  133. DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
  134. IE_ON_SIGNATURE_UPDATE | IE_ON_SNOTIFY_UPDATE |
  135. IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
  136. EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
  137. DATA_SNOOP_ENABLE = (1 << 22),
  138. };
  139. /*
  140. * SATA Superset Registers
  141. */
  142. enum {
  143. SSTATUS = 0,
  144. SERROR = 4,
  145. SCONTROL = 8,
  146. SNOTIFY = 0xC,
  147. };
  148. /*
  149. * Control Status Register Set
  150. */
  151. enum {
  152. TRANSCFG = 0,
  153. TRANSSTATUS = 4,
  154. LINKCFG = 8,
  155. LINKCFG1 = 0xC,
  156. LINKCFG2 = 0x10,
  157. LINKSTATUS = 0x14,
  158. LINKSTATUS1 = 0x18,
  159. PHYCTRLCFG = 0x1C,
  160. COMMANDSTAT = 0x20,
  161. };
  162. /* TRANSCFG (transport-layer) configuration control */
  163. enum {
  164. TRANSCFG_RX_WATER_MARK = (1 << 4),
  165. };
  166. /* PHY (link-layer) configuration control */
  167. enum {
  168. PHY_BIST_ENABLE = 0x01,
  169. };
  170. /*
  171. * Command Header Table entry, i.e, command slot
  172. * 4 Dwords per command slot, command header size == 64 Dwords.
  173. */
  174. struct cmdhdr_tbl_entry {
  175. u32 cda;
  176. u32 prde_fis_len;
  177. u32 ttl;
  178. u32 desc_info;
  179. };
  180. /*
  181. * Description information bitdefs
  182. */
  183. enum {
  184. CMD_DESC_RES = (1 << 11),
  185. VENDOR_SPECIFIC_BIST = (1 << 10),
  186. CMD_DESC_SNOOP_ENABLE = (1 << 9),
  187. FPDMA_QUEUED_CMD = (1 << 8),
  188. SRST_CMD = (1 << 7),
  189. BIST = (1 << 6),
  190. ATAPI_CMD = (1 << 5),
  191. };
  192. /*
  193. * Command Descriptor
  194. */
  195. struct command_desc {
  196. u8 cfis[8 * 4];
  197. u8 sfis[8 * 4];
  198. u8 acmd[4 * 4];
  199. u8 fill[4 * 4];
  200. u32 prdt[SATA_FSL_MAX_PRD_DIRECT * 4];
  201. u32 prdt_indirect[(SATA_FSL_MAX_PRD - SATA_FSL_MAX_PRD_DIRECT) * 4];
  202. };
  203. /*
  204. * Physical region table descriptor(PRD)
  205. */
  206. struct prde {
  207. u32 dba;
  208. u8 fill[2 * 4];
  209. u32 ddc_and_ext;
  210. };
  211. /*
  212. * ata_port private data
  213. * This is our per-port instance data.
  214. */
  215. struct sata_fsl_port_priv {
  216. struct cmdhdr_tbl_entry *cmdslot;
  217. dma_addr_t cmdslot_paddr;
  218. struct command_desc *cmdentry;
  219. dma_addr_t cmdentry_paddr;
  220. };
  221. /*
  222. * ata_port->host_set private data
  223. */
  224. struct sata_fsl_host_priv {
  225. void __iomem *hcr_base;
  226. void __iomem *ssr_base;
  227. void __iomem *csr_base;
  228. int irq;
  229. };
  230. static inline unsigned int sata_fsl_tag(unsigned int tag,
  231. void __iomem *hcr_base)
  232. {
  233. /* We let libATA core do actual (queue) tag allocation */
  234. /* all non NCQ/queued commands should have tag#0 */
  235. if (ata_tag_internal(tag)) {
  236. DPRINTK("mapping internal cmds to tag#0\n");
  237. return 0;
  238. }
  239. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  240. DPRINTK("tag %d invalid : out of range\n", tag);
  241. return 0;
  242. }
  243. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  244. DPRINTK("tag %d invalid : in use!!\n", tag);
  245. return 0;
  246. }
  247. return tag;
  248. }
  249. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  250. unsigned int tag, u32 desc_info,
  251. u32 data_xfer_len, u8 num_prde,
  252. u8 fis_len)
  253. {
  254. dma_addr_t cmd_descriptor_address;
  255. cmd_descriptor_address = pp->cmdentry_paddr +
  256. tag * SATA_FSL_CMD_DESC_SIZE;
  257. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  258. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  259. pp->cmdslot[tag].prde_fis_len =
  260. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  261. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  262. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  263. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  264. pp->cmdslot[tag].cda,
  265. pp->cmdslot[tag].prde_fis_len,
  266. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  267. }
  268. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  269. u32 *ttl, dma_addr_t cmd_desc_paddr)
  270. {
  271. struct scatterlist *sg;
  272. unsigned int num_prde = 0;
  273. u32 ttl_dwords = 0;
  274. /*
  275. * NOTE : direct & indirect prdt's are contiguously allocated
  276. */
  277. struct prde *prd = (struct prde *)&((struct command_desc *)
  278. cmd_desc)->prdt;
  279. struct prde *prd_ptr_to_indirect_ext = NULL;
  280. unsigned indirect_ext_segment_sz = 0;
  281. dma_addr_t indirect_ext_segment_paddr;
  282. unsigned int si;
  283. VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
  284. indirect_ext_segment_paddr = cmd_desc_paddr +
  285. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  286. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  287. dma_addr_t sg_addr = sg_dma_address(sg);
  288. u32 sg_len = sg_dma_len(sg);
  289. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%llx, sg_len = %d\n",
  290. (unsigned long long)sg_addr, sg_len);
  291. /* warn if each s/g element is not dword aligned */
  292. if (sg_addr & 0x03)
  293. ata_port_printk(qc->ap, KERN_ERR,
  294. "s/g addr unaligned : 0x%llx\n",
  295. (unsigned long long)sg_addr);
  296. if (sg_len & 0x03)
  297. ata_port_printk(qc->ap, KERN_ERR,
  298. "s/g len unaligned : 0x%x\n", sg_len);
  299. if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
  300. sg_next(sg) != NULL) {
  301. VPRINTK("setting indirect prde\n");
  302. prd_ptr_to_indirect_ext = prd;
  303. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  304. indirect_ext_segment_sz = 0;
  305. ++prd;
  306. ++num_prde;
  307. }
  308. ttl_dwords += sg_len;
  309. prd->dba = cpu_to_le32(sg_addr);
  310. prd->ddc_and_ext =
  311. cpu_to_le32(DATA_SNOOP_ENABLE | (sg_len & ~0x03));
  312. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  313. ttl_dwords, prd->dba, prd->ddc_and_ext);
  314. ++num_prde;
  315. ++prd;
  316. if (prd_ptr_to_indirect_ext)
  317. indirect_ext_segment_sz += sg_len;
  318. }
  319. if (prd_ptr_to_indirect_ext) {
  320. /* set indirect extension flag along with indirect ext. size */
  321. prd_ptr_to_indirect_ext->ddc_and_ext =
  322. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  323. DATA_SNOOP_ENABLE |
  324. (indirect_ext_segment_sz & ~0x03)));
  325. }
  326. *ttl = ttl_dwords;
  327. return num_prde;
  328. }
  329. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  330. {
  331. struct ata_port *ap = qc->ap;
  332. struct sata_fsl_port_priv *pp = ap->private_data;
  333. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  334. void __iomem *hcr_base = host_priv->hcr_base;
  335. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  336. struct command_desc *cd;
  337. u32 desc_info = CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE;
  338. u32 num_prde = 0;
  339. u32 ttl_dwords = 0;
  340. dma_addr_t cd_paddr;
  341. cd = (struct command_desc *)pp->cmdentry + tag;
  342. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  343. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *) &cd->cfis);
  344. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  345. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  346. if (qc->tf.protocol == ATA_PROT_NCQ) {
  347. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  348. cd->cfis[3], cd->cfis[11]);
  349. }
  350. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  351. if (ata_is_atapi(qc->tf.protocol)) {
  352. desc_info |= ATAPI_CMD;
  353. memset((void *)&cd->acmd, 0, 32);
  354. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  355. }
  356. if (qc->flags & ATA_QCFLAG_DMAMAP)
  357. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  358. &ttl_dwords, cd_paddr);
  359. if (qc->tf.protocol == ATA_PROT_NCQ)
  360. desc_info |= FPDMA_QUEUED_CMD;
  361. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  362. num_prde, 5);
  363. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  364. desc_info, ttl_dwords, num_prde);
  365. }
  366. static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
  367. {
  368. struct ata_port *ap = qc->ap;
  369. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  370. void __iomem *hcr_base = host_priv->hcr_base;
  371. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  372. VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
  373. ioread32(CQ + hcr_base),
  374. ioread32(CA + hcr_base),
  375. ioread32(CE + hcr_base), ioread32(CC + hcr_base));
  376. iowrite32(qc->dev->link->pmp, CQPMP + hcr_base);
  377. /* Simply queue command to the controller/device */
  378. iowrite32(1 << tag, CQ + hcr_base);
  379. VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
  380. tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
  381. VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
  382. ioread32(CE + hcr_base),
  383. ioread32(DE + hcr_base),
  384. ioread32(CC + hcr_base),
  385. ioread32(COMMANDSTAT + host_priv->csr_base));
  386. return 0;
  387. }
  388. static bool sata_fsl_qc_fill_rtf(struct ata_queued_cmd *qc)
  389. {
  390. struct sata_fsl_port_priv *pp = qc->ap->private_data;
  391. struct sata_fsl_host_priv *host_priv = qc->ap->host->private_data;
  392. void __iomem *hcr_base = host_priv->hcr_base;
  393. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  394. struct command_desc *cd;
  395. cd = pp->cmdentry + tag;
  396. ata_tf_from_fis(cd->sfis, &qc->result_tf);
  397. return true;
  398. }
  399. static int sata_fsl_scr_write(struct ata_link *link,
  400. unsigned int sc_reg_in, u32 val)
  401. {
  402. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  403. void __iomem *ssr_base = host_priv->ssr_base;
  404. unsigned int sc_reg;
  405. switch (sc_reg_in) {
  406. case SCR_STATUS:
  407. case SCR_ERROR:
  408. case SCR_CONTROL:
  409. case SCR_ACTIVE:
  410. sc_reg = sc_reg_in;
  411. break;
  412. default:
  413. return -EINVAL;
  414. }
  415. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  416. iowrite32(val, ssr_base + (sc_reg * 4));
  417. return 0;
  418. }
  419. static int sata_fsl_scr_read(struct ata_link *link,
  420. unsigned int sc_reg_in, u32 *val)
  421. {
  422. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  423. void __iomem *ssr_base = host_priv->ssr_base;
  424. unsigned int sc_reg;
  425. switch (sc_reg_in) {
  426. case SCR_STATUS:
  427. case SCR_ERROR:
  428. case SCR_CONTROL:
  429. case SCR_ACTIVE:
  430. sc_reg = sc_reg_in;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  436. *val = ioread32(ssr_base + (sc_reg * 4));
  437. return 0;
  438. }
  439. static void sata_fsl_freeze(struct ata_port *ap)
  440. {
  441. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  442. void __iomem *hcr_base = host_priv->hcr_base;
  443. u32 temp;
  444. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  445. ioread32(CQ + hcr_base),
  446. ioread32(CA + hcr_base),
  447. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  448. VPRINTK("CmdStat = 0x%x\n",
  449. ioread32(host_priv->csr_base + COMMANDSTAT));
  450. /* disable interrupts on the controller/port */
  451. temp = ioread32(hcr_base + HCONTROL);
  452. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  453. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  454. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  455. }
  456. static void sata_fsl_thaw(struct ata_port *ap)
  457. {
  458. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  459. void __iomem *hcr_base = host_priv->hcr_base;
  460. u32 temp;
  461. /* ack. any pending IRQs for this controller/port */
  462. temp = ioread32(hcr_base + HSTATUS);
  463. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  464. if (temp & 0x3F)
  465. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  466. /* enable interrupts on the controller/port */
  467. temp = ioread32(hcr_base + HCONTROL);
  468. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  469. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  470. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  471. }
  472. static void sata_fsl_pmp_attach(struct ata_port *ap)
  473. {
  474. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  475. void __iomem *hcr_base = host_priv->hcr_base;
  476. u32 temp;
  477. temp = ioread32(hcr_base + HCONTROL);
  478. iowrite32((temp | HCONTROL_PMP_ATTACHED), hcr_base + HCONTROL);
  479. }
  480. static void sata_fsl_pmp_detach(struct ata_port *ap)
  481. {
  482. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  483. void __iomem *hcr_base = host_priv->hcr_base;
  484. u32 temp;
  485. temp = ioread32(hcr_base + HCONTROL);
  486. temp &= ~HCONTROL_PMP_ATTACHED;
  487. iowrite32(temp, hcr_base + HCONTROL);
  488. /* enable interrupts on the controller/port */
  489. temp = ioread32(hcr_base + HCONTROL);
  490. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  491. }
  492. static int sata_fsl_port_start(struct ata_port *ap)
  493. {
  494. struct device *dev = ap->host->dev;
  495. struct sata_fsl_port_priv *pp;
  496. void *mem;
  497. dma_addr_t mem_dma;
  498. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  499. void __iomem *hcr_base = host_priv->hcr_base;
  500. u32 temp;
  501. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  502. if (!pp)
  503. return -ENOMEM;
  504. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  505. GFP_KERNEL);
  506. if (!mem) {
  507. kfree(pp);
  508. return -ENOMEM;
  509. }
  510. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  511. pp->cmdslot = mem;
  512. pp->cmdslot_paddr = mem_dma;
  513. mem += SATA_FSL_CMD_SLOT_SIZE;
  514. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  515. pp->cmdentry = mem;
  516. pp->cmdentry_paddr = mem_dma;
  517. ap->private_data = pp;
  518. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  519. pp->cmdslot_paddr, pp->cmdentry_paddr);
  520. /* Now, update the CHBA register in host controller cmd register set */
  521. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  522. /*
  523. * Now, we can bring the controller on-line & also initiate
  524. * the COMINIT sequence, we simply return here and the boot-probing
  525. * & device discovery process is re-initiated by libATA using a
  526. * Softreset EH (dummy) session. Hence, boot probing and device
  527. * discovey will be part of sata_fsl_softreset() callback.
  528. */
  529. temp = ioread32(hcr_base + HCONTROL);
  530. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  531. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  532. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  533. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  534. #ifdef CONFIG_MPC8315_DS
  535. /*
  536. * Workaround for 8315DS board 3gbps link-up issue,
  537. * currently limit SATA port to GEN1 speed
  538. */
  539. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  540. temp &= ~(0xF << 4);
  541. temp |= (0x1 << 4);
  542. sata_fsl_scr_write(&ap->link, SCR_CONTROL, temp);
  543. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  544. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  545. temp);
  546. #endif
  547. return 0;
  548. }
  549. static void sata_fsl_port_stop(struct ata_port *ap)
  550. {
  551. struct device *dev = ap->host->dev;
  552. struct sata_fsl_port_priv *pp = ap->private_data;
  553. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  554. void __iomem *hcr_base = host_priv->hcr_base;
  555. u32 temp;
  556. /*
  557. * Force host controller to go off-line, aborting current operations
  558. */
  559. temp = ioread32(hcr_base + HCONTROL);
  560. temp &= ~HCONTROL_ONLINE_PHY_RST;
  561. temp |= HCONTROL_FORCE_OFFLINE;
  562. iowrite32(temp, hcr_base + HCONTROL);
  563. /* Poll for controller to go offline - should happen immediately */
  564. ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  565. ap->private_data = NULL;
  566. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  567. pp->cmdslot, pp->cmdslot_paddr);
  568. kfree(pp);
  569. }
  570. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  571. {
  572. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  573. void __iomem *hcr_base = host_priv->hcr_base;
  574. struct ata_taskfile tf;
  575. u32 temp;
  576. temp = ioread32(hcr_base + SIGNATURE);
  577. VPRINTK("raw sig = 0x%x\n", temp);
  578. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  579. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  580. tf.lbah = (temp >> 24) & 0xff;
  581. tf.lbam = (temp >> 16) & 0xff;
  582. tf.lbal = (temp >> 8) & 0xff;
  583. tf.nsect = temp & 0xff;
  584. return ata_dev_classify(&tf);
  585. }
  586. static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
  587. unsigned long deadline)
  588. {
  589. struct ata_port *ap = link->ap;
  590. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  591. void __iomem *hcr_base = host_priv->hcr_base;
  592. u32 temp;
  593. int i = 0;
  594. unsigned long start_jiffies;
  595. DPRINTK("in xx_hardreset\n");
  596. try_offline_again:
  597. /*
  598. * Force host controller to go off-line, aborting current operations
  599. */
  600. temp = ioread32(hcr_base + HCONTROL);
  601. temp &= ~HCONTROL_ONLINE_PHY_RST;
  602. iowrite32(temp, hcr_base + HCONTROL);
  603. /* Poll for controller to go offline */
  604. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE,
  605. 1, 500);
  606. if (temp & ONLINE) {
  607. ata_port_printk(ap, KERN_ERR,
  608. "Hardreset failed, not off-lined %d\n", i);
  609. /*
  610. * Try to offline controller atleast twice
  611. */
  612. i++;
  613. if (i == 2)
  614. goto err;
  615. else
  616. goto try_offline_again;
  617. }
  618. DPRINTK("hardreset, controller off-lined\n");
  619. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  620. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  621. /*
  622. * PHY reset should remain asserted for atleast 1ms
  623. */
  624. ata_msleep(ap, 1);
  625. /*
  626. * Now, bring the host controller online again, this can take time
  627. * as PHY reset and communication establishment, 1st D2H FIS and
  628. * device signature update is done, on safe side assume 500ms
  629. * NOTE : Host online status may be indicated immediately!!
  630. */
  631. temp = ioread32(hcr_base + HCONTROL);
  632. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  633. temp |= HCONTROL_PMP_ATTACHED;
  634. iowrite32(temp, hcr_base + HCONTROL);
  635. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  636. if (!(temp & ONLINE)) {
  637. ata_port_printk(ap, KERN_ERR,
  638. "Hardreset failed, not on-lined\n");
  639. goto err;
  640. }
  641. DPRINTK("hardreset, controller off-lined & on-lined\n");
  642. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  643. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  644. /*
  645. * First, wait for the PHYRDY change to occur before waiting for
  646. * the signature, and also verify if SStatus indicates device
  647. * presence
  648. */
  649. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  650. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  651. ata_port_printk(ap, KERN_WARNING,
  652. "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  653. ioread32(hcr_base + HSTATUS));
  654. *class = ATA_DEV_NONE;
  655. return 0;
  656. }
  657. /*
  658. * Wait for the first D2H from device,i.e,signature update notification
  659. */
  660. start_jiffies = jiffies;
  661. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0x10,
  662. 500, jiffies_to_msecs(deadline - start_jiffies));
  663. if ((temp & 0xFF) != 0x18) {
  664. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  665. *class = ATA_DEV_NONE;
  666. goto do_followup_srst;
  667. } else {
  668. ata_port_printk(ap, KERN_INFO,
  669. "Signature Update detected @ %d msecs\n",
  670. jiffies_to_msecs(jiffies - start_jiffies));
  671. *class = sata_fsl_dev_classify(ap);
  672. return 0;
  673. }
  674. do_followup_srst:
  675. /*
  676. * request libATA to perform follow-up softreset
  677. */
  678. return -EAGAIN;
  679. err:
  680. return -EIO;
  681. }
  682. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  683. unsigned long deadline)
  684. {
  685. struct ata_port *ap = link->ap;
  686. struct sata_fsl_port_priv *pp = ap->private_data;
  687. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  688. void __iomem *hcr_base = host_priv->hcr_base;
  689. int pmp = sata_srst_pmp(link);
  690. u32 temp;
  691. struct ata_taskfile tf;
  692. u8 *cfis;
  693. u32 Serror;
  694. DPRINTK("in xx_softreset\n");
  695. if (ata_link_offline(link)) {
  696. DPRINTK("PHY reports no device\n");
  697. *class = ATA_DEV_NONE;
  698. return 0;
  699. }
  700. /*
  701. * Send a device reset (SRST) explicitly on command slot #0
  702. * Check : will the command queue (reg) be cleared during offlining ??
  703. * Also we will be online only if Phy commn. has been established
  704. * and device presence has been detected, therefore if we have
  705. * reached here, we can send a command to the target device
  706. */
  707. DPRINTK("Sending SRST/device reset\n");
  708. ata_tf_init(link->device, &tf);
  709. cfis = (u8 *) &pp->cmdentry->cfis;
  710. /* device reset/SRST is a control register update FIS, uses tag0 */
  711. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  712. SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  713. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  714. ata_tf_to_fis(&tf, pmp, 0, cfis);
  715. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  716. cfis[0], cfis[1], cfis[2], cfis[3]);
  717. /*
  718. * Queue SRST command to the controller/device, ensure that no
  719. * other commands are active on the controller/device
  720. */
  721. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  722. ioread32(CQ + hcr_base),
  723. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  724. iowrite32(0xFFFF, CC + hcr_base);
  725. if (pmp != SATA_PMP_CTRL_PORT)
  726. iowrite32(pmp, CQPMP + hcr_base);
  727. iowrite32(1, CQ + hcr_base);
  728. temp = ata_wait_register(ap, CQ + hcr_base, 0x1, 0x1, 1, 5000);
  729. if (temp & 0x1) {
  730. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  731. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  732. ioread32(CQ + hcr_base),
  733. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  734. sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
  735. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  736. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  737. DPRINTK("Serror = 0x%x\n", Serror);
  738. goto err;
  739. }
  740. ata_msleep(ap, 1);
  741. /*
  742. * SATA device enters reset state after receving a Control register
  743. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  744. * FIS with SRST bit cleared, then the device does internal diags &
  745. * initialization, followed by indicating it's initialization status
  746. * using ATA signature D2H register FIS to the host controller.
  747. */
  748. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
  749. 0, 0, 5);
  750. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  751. ata_tf_to_fis(&tf, pmp, 0, cfis);
  752. if (pmp != SATA_PMP_CTRL_PORT)
  753. iowrite32(pmp, CQPMP + hcr_base);
  754. iowrite32(1, CQ + hcr_base);
  755. ata_msleep(ap, 150); /* ?? */
  756. /*
  757. * The above command would have signalled an interrupt on command
  758. * complete, which needs special handling, by clearing the Nth
  759. * command bit of the CCreg
  760. */
  761. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  762. DPRINTK("SATA FSL : Now checking device signature\n");
  763. *class = ATA_DEV_NONE;
  764. /* Verify if SStatus indicates device presence */
  765. if (ata_link_online(link)) {
  766. /*
  767. * if we are here, device presence has been detected,
  768. * 1st D2H FIS would have been received, but sfis in
  769. * command desc. is not updated, but signature register
  770. * would have been updated
  771. */
  772. *class = sata_fsl_dev_classify(ap);
  773. DPRINTK("class = %d\n", *class);
  774. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  775. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  776. }
  777. return 0;
  778. err:
  779. return -EIO;
  780. }
  781. static void sata_fsl_error_handler(struct ata_port *ap)
  782. {
  783. DPRINTK("in xx_error_handler\n");
  784. sata_pmp_error_handler(ap);
  785. }
  786. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  787. {
  788. if (qc->flags & ATA_QCFLAG_FAILED)
  789. qc->err_mask |= AC_ERR_OTHER;
  790. if (qc->err_mask) {
  791. /* make DMA engine forget about the failed command */
  792. }
  793. }
  794. static void sata_fsl_error_intr(struct ata_port *ap)
  795. {
  796. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  797. void __iomem *hcr_base = host_priv->hcr_base;
  798. u32 hstatus, dereg=0, cereg = 0, SError = 0;
  799. unsigned int err_mask = 0, action = 0;
  800. int freeze = 0, abort=0;
  801. struct ata_link *link = NULL;
  802. struct ata_queued_cmd *qc = NULL;
  803. struct ata_eh_info *ehi;
  804. hstatus = ioread32(hcr_base + HSTATUS);
  805. cereg = ioread32(hcr_base + CE);
  806. /* first, analyze and record host port events */
  807. link = &ap->link;
  808. ehi = &link->eh_info;
  809. ata_ehi_clear_desc(ehi);
  810. /*
  811. * Handle & Clear SError
  812. */
  813. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  814. if (unlikely(SError & 0xFFFF0000))
  815. sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
  816. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  817. hstatus, cereg, ioread32(hcr_base + DE), SError);
  818. /* handle fatal errors */
  819. if (hstatus & FATAL_ERROR_DECODE) {
  820. ehi->err_mask |= AC_ERR_ATA_BUS;
  821. ehi->action |= ATA_EH_SOFTRESET;
  822. freeze = 1;
  823. }
  824. /* Handle SDB FIS receive & notify update */
  825. if (hstatus & INT_ON_SNOTIFY_UPDATE)
  826. sata_async_notification(ap);
  827. /* Handle PHYRDY change notification */
  828. if (hstatus & INT_ON_PHYRDY_CHG) {
  829. DPRINTK("SATA FSL: PHYRDY change indication\n");
  830. /* Setup a soft-reset EH action */
  831. ata_ehi_hotplugged(ehi);
  832. ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
  833. freeze = 1;
  834. }
  835. /* handle single device errors */
  836. if (cereg) {
  837. /*
  838. * clear the command error, also clears queue to the device
  839. * in error, and we can (re)issue commands to this device.
  840. * When a device is in error all commands queued into the
  841. * host controller and at the device are considered aborted
  842. * and the queue for that device is stopped. Now, after
  843. * clearing the device error, we can issue commands to the
  844. * device to interrogate it to find the source of the error.
  845. */
  846. abort = 1;
  847. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  848. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  849. /* find out the offending link and qc */
  850. if (ap->nr_pmp_links) {
  851. unsigned int dev_num;
  852. dereg = ioread32(hcr_base + DE);
  853. iowrite32(dereg, hcr_base + DE);
  854. iowrite32(cereg, hcr_base + CE);
  855. dev_num = ffs(dereg) - 1;
  856. if (dev_num < ap->nr_pmp_links && dereg != 0) {
  857. link = &ap->pmp_link[dev_num];
  858. ehi = &link->eh_info;
  859. qc = ata_qc_from_tag(ap, link->active_tag);
  860. /*
  861. * We should consider this as non fatal error,
  862. * and TF must be updated as done below.
  863. */
  864. err_mask |= AC_ERR_DEV;
  865. } else {
  866. err_mask |= AC_ERR_HSM;
  867. action |= ATA_EH_HARDRESET;
  868. freeze = 1;
  869. }
  870. } else {
  871. dereg = ioread32(hcr_base + DE);
  872. iowrite32(dereg, hcr_base + DE);
  873. iowrite32(cereg, hcr_base + CE);
  874. qc = ata_qc_from_tag(ap, link->active_tag);
  875. /*
  876. * We should consider this as non fatal error,
  877. * and TF must be updated as done below.
  878. */
  879. err_mask |= AC_ERR_DEV;
  880. }
  881. }
  882. /* record error info */
  883. if (qc)
  884. qc->err_mask |= err_mask;
  885. else
  886. ehi->err_mask |= err_mask;
  887. ehi->action |= action;
  888. /* freeze or abort */
  889. if (freeze)
  890. ata_port_freeze(ap);
  891. else if (abort) {
  892. if (qc)
  893. ata_link_abort(qc->dev->link);
  894. else
  895. ata_port_abort(ap);
  896. }
  897. }
  898. static void sata_fsl_host_intr(struct ata_port *ap)
  899. {
  900. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  901. void __iomem *hcr_base = host_priv->hcr_base;
  902. u32 hstatus, done_mask = 0;
  903. struct ata_queued_cmd *qc;
  904. u32 SError;
  905. hstatus = ioread32(hcr_base + HSTATUS);
  906. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  907. if (unlikely(SError & 0xFFFF0000)) {
  908. DPRINTK("serror @host_intr : 0x%x\n", SError);
  909. sata_fsl_error_intr(ap);
  910. }
  911. if (unlikely(hstatus & INT_ON_ERROR)) {
  912. DPRINTK("error interrupt!!\n");
  913. sata_fsl_error_intr(ap);
  914. return;
  915. }
  916. /* Read command completed register */
  917. done_mask = ioread32(hcr_base + CC);
  918. VPRINTK("Status of all queues :\n");
  919. VPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%x\n",
  920. done_mask,
  921. ioread32(hcr_base + CA),
  922. ioread32(hcr_base + CE),
  923. ioread32(hcr_base + CQ),
  924. ap->qc_active);
  925. if (done_mask & ap->qc_active) {
  926. int i;
  927. /* clear CC bit, this will also complete the interrupt */
  928. iowrite32(done_mask, hcr_base + CC);
  929. DPRINTK("Status of all queues :\n");
  930. DPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  931. done_mask, ioread32(hcr_base + CA),
  932. ioread32(hcr_base + CE));
  933. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  934. if (done_mask & (1 << i))
  935. DPRINTK
  936. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  937. i, ioread32(hcr_base + CC),
  938. ioread32(hcr_base + CA));
  939. }
  940. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  941. return;
  942. } else if ((ap->qc_active & (1 << ATA_TAG_INTERNAL))) {
  943. iowrite32(1, hcr_base + CC);
  944. qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  945. DPRINTK("completing non-ncq cmd, CC=0x%x\n",
  946. ioread32(hcr_base + CC));
  947. if (qc) {
  948. ata_qc_complete(qc);
  949. }
  950. } else {
  951. /* Spurious Interrupt!! */
  952. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  953. ioread32(hcr_base + CC));
  954. iowrite32(done_mask, hcr_base + CC);
  955. return;
  956. }
  957. }
  958. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  959. {
  960. struct ata_host *host = dev_instance;
  961. struct sata_fsl_host_priv *host_priv = host->private_data;
  962. void __iomem *hcr_base = host_priv->hcr_base;
  963. u32 interrupt_enables;
  964. unsigned handled = 0;
  965. struct ata_port *ap;
  966. /* ack. any pending IRQs for this controller/port */
  967. interrupt_enables = ioread32(hcr_base + HSTATUS);
  968. interrupt_enables &= 0x3F;
  969. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  970. if (!interrupt_enables)
  971. return IRQ_NONE;
  972. spin_lock(&host->lock);
  973. /* Assuming one port per host controller */
  974. ap = host->ports[0];
  975. if (ap) {
  976. sata_fsl_host_intr(ap);
  977. } else {
  978. dev_printk(KERN_WARNING, host->dev,
  979. "interrupt on disabled port 0\n");
  980. }
  981. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  982. handled = 1;
  983. spin_unlock(&host->lock);
  984. return IRQ_RETVAL(handled);
  985. }
  986. /*
  987. * Multiple ports are represented by multiple SATA controllers with
  988. * one port per controller
  989. */
  990. static int sata_fsl_init_controller(struct ata_host *host)
  991. {
  992. struct sata_fsl_host_priv *host_priv = host->private_data;
  993. void __iomem *hcr_base = host_priv->hcr_base;
  994. u32 temp;
  995. /*
  996. * NOTE : We cannot bring the controller online before setting
  997. * the CHBA, hence main controller initialization is done as
  998. * part of the port_start() callback
  999. */
  1000. /* ack. any pending IRQs for this controller/port */
  1001. temp = ioread32(hcr_base + HSTATUS);
  1002. if (temp & 0x3F)
  1003. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  1004. /* Keep interrupts disabled on the controller */
  1005. temp = ioread32(hcr_base + HCONTROL);
  1006. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  1007. /* Disable interrupt coalescing control(icc), for the moment */
  1008. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  1009. iowrite32(0x01000000, hcr_base + ICC);
  1010. /* clear error registers, SError is cleared by libATA */
  1011. iowrite32(0x00000FFFF, hcr_base + CE);
  1012. iowrite32(0x00000FFFF, hcr_base + DE);
  1013. /*
  1014. * host controller will be brought on-line, during xx_port_start()
  1015. * callback, that should also initiate the OOB, COMINIT sequence
  1016. */
  1017. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  1018. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  1019. return 0;
  1020. }
  1021. /*
  1022. * scsi mid-layer and libata interface structures
  1023. */
  1024. static struct scsi_host_template sata_fsl_sht = {
  1025. ATA_NCQ_SHT("sata_fsl"),
  1026. .can_queue = SATA_FSL_QUEUE_DEPTH,
  1027. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  1028. .dma_boundary = ATA_DMA_BOUNDARY,
  1029. };
  1030. static struct ata_port_operations sata_fsl_ops = {
  1031. .inherits = &sata_pmp_port_ops,
  1032. .qc_defer = ata_std_qc_defer,
  1033. .qc_prep = sata_fsl_qc_prep,
  1034. .qc_issue = sata_fsl_qc_issue,
  1035. .qc_fill_rtf = sata_fsl_qc_fill_rtf,
  1036. .scr_read = sata_fsl_scr_read,
  1037. .scr_write = sata_fsl_scr_write,
  1038. .freeze = sata_fsl_freeze,
  1039. .thaw = sata_fsl_thaw,
  1040. .softreset = sata_fsl_softreset,
  1041. .hardreset = sata_fsl_hardreset,
  1042. .pmp_softreset = sata_fsl_softreset,
  1043. .error_handler = sata_fsl_error_handler,
  1044. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1045. .port_start = sata_fsl_port_start,
  1046. .port_stop = sata_fsl_port_stop,
  1047. .pmp_attach = sata_fsl_pmp_attach,
  1048. .pmp_detach = sata_fsl_pmp_detach,
  1049. };
  1050. static const struct ata_port_info sata_fsl_port_info[] = {
  1051. {
  1052. .flags = SATA_FSL_HOST_FLAGS,
  1053. .pio_mask = ATA_PIO4,
  1054. .udma_mask = ATA_UDMA6,
  1055. .port_ops = &sata_fsl_ops,
  1056. },
  1057. };
  1058. static int sata_fsl_probe(struct platform_device *ofdev)
  1059. {
  1060. int retval = -ENXIO;
  1061. void __iomem *hcr_base = NULL;
  1062. void __iomem *ssr_base = NULL;
  1063. void __iomem *csr_base = NULL;
  1064. struct sata_fsl_host_priv *host_priv = NULL;
  1065. int irq;
  1066. struct ata_host *host;
  1067. u32 temp;
  1068. struct ata_port_info pi = sata_fsl_port_info[0];
  1069. const struct ata_port_info *ppi[] = { &pi, NULL };
  1070. dev_printk(KERN_INFO, &ofdev->dev,
  1071. "Sata FSL Platform/CSB Driver init\n");
  1072. hcr_base = of_iomap(ofdev->dev.of_node, 0);
  1073. if (!hcr_base)
  1074. goto error_exit_with_cleanup;
  1075. ssr_base = hcr_base + 0x100;
  1076. csr_base = hcr_base + 0x140;
  1077. if (!of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc8315-sata")) {
  1078. temp = ioread32(csr_base + TRANSCFG);
  1079. temp = temp & 0xffffffe0;
  1080. iowrite32(temp | TRANSCFG_RX_WATER_MARK, csr_base + TRANSCFG);
  1081. }
  1082. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1083. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1084. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1085. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1086. if (!host_priv)
  1087. goto error_exit_with_cleanup;
  1088. host_priv->hcr_base = hcr_base;
  1089. host_priv->ssr_base = ssr_base;
  1090. host_priv->csr_base = csr_base;
  1091. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1092. if (irq < 0) {
  1093. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1094. goto error_exit_with_cleanup;
  1095. }
  1096. host_priv->irq = irq;
  1097. /* allocate host structure */
  1098. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1099. /* host->iomap is not used currently */
  1100. host->private_data = host_priv;
  1101. /* initialize host controller */
  1102. sata_fsl_init_controller(host);
  1103. /*
  1104. * Now, register with libATA core, this will also initiate the
  1105. * device discovery process, invoking our port_start() handler &
  1106. * error_handler() to execute a dummy Softreset EH session
  1107. */
  1108. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1109. &sata_fsl_sht);
  1110. dev_set_drvdata(&ofdev->dev, host);
  1111. return 0;
  1112. error_exit_with_cleanup:
  1113. if (hcr_base)
  1114. iounmap(hcr_base);
  1115. if (host_priv)
  1116. kfree(host_priv);
  1117. return retval;
  1118. }
  1119. static int sata_fsl_remove(struct platform_device *ofdev)
  1120. {
  1121. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1122. struct sata_fsl_host_priv *host_priv = host->private_data;
  1123. ata_host_detach(host);
  1124. dev_set_drvdata(&ofdev->dev, NULL);
  1125. irq_dispose_mapping(host_priv->irq);
  1126. iounmap(host_priv->hcr_base);
  1127. kfree(host_priv);
  1128. return 0;
  1129. }
  1130. #ifdef CONFIG_PM
  1131. static int sata_fsl_suspend(struct platform_device *op, pm_message_t state)
  1132. {
  1133. struct ata_host *host = dev_get_drvdata(&op->dev);
  1134. return ata_host_suspend(host, state);
  1135. }
  1136. static int sata_fsl_resume(struct platform_device *op)
  1137. {
  1138. struct ata_host *host = dev_get_drvdata(&op->dev);
  1139. struct sata_fsl_host_priv *host_priv = host->private_data;
  1140. int ret;
  1141. void __iomem *hcr_base = host_priv->hcr_base;
  1142. struct ata_port *ap = host->ports[0];
  1143. struct sata_fsl_port_priv *pp = ap->private_data;
  1144. ret = sata_fsl_init_controller(host);
  1145. if (ret) {
  1146. dev_printk(KERN_ERR, &op->dev,
  1147. "Error initialize hardware\n");
  1148. return ret;
  1149. }
  1150. /* Recovery the CHBA register in host controller cmd register set */
  1151. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  1152. ata_host_resume(host);
  1153. return 0;
  1154. }
  1155. #endif
  1156. static struct of_device_id fsl_sata_match[] = {
  1157. {
  1158. .compatible = "fsl,pq-sata",
  1159. },
  1160. {},
  1161. };
  1162. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1163. static struct platform_driver fsl_sata_driver = {
  1164. .driver = {
  1165. .name = "fsl-sata",
  1166. .owner = THIS_MODULE,
  1167. .of_match_table = fsl_sata_match,
  1168. },
  1169. .probe = sata_fsl_probe,
  1170. .remove = sata_fsl_remove,
  1171. #ifdef CONFIG_PM
  1172. .suspend = sata_fsl_suspend,
  1173. .resume = sata_fsl_resume,
  1174. #endif
  1175. };
  1176. static int __init sata_fsl_init(void)
  1177. {
  1178. platform_driver_register(&fsl_sata_driver);
  1179. return 0;
  1180. }
  1181. static void __exit sata_fsl_exit(void)
  1182. {
  1183. platform_driver_unregister(&fsl_sata_driver);
  1184. }
  1185. MODULE_LICENSE("GPL");
  1186. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1187. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1188. MODULE_VERSION("1.10");
  1189. module_init(sata_fsl_init);
  1190. module_exit(sata_fsl_exit);