sata_fsl.c 38 KB

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