sata_fsl.c 41 KB

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