sata_fsl.c 42 KB

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