sata_fsl.c 43 KB

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