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