sata_fsl.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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 <[email protected]>
  8. * Li Yang <[email protected]>
  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/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_cmnd.h>
  18. #include <linux/libata.h>
  19. #include <asm/io.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.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 bool 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. return true;
  488. }
  489. static int sata_fsl_scr_write(struct ata_link *link,
  490. unsigned int sc_reg_in, u32 val)
  491. {
  492. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  493. void __iomem *ssr_base = host_priv->ssr_base;
  494. unsigned int sc_reg;
  495. switch (sc_reg_in) {
  496. case SCR_STATUS:
  497. case SCR_ERROR:
  498. case SCR_CONTROL:
  499. case SCR_ACTIVE:
  500. sc_reg = sc_reg_in;
  501. break;
  502. default:
  503. return -EINVAL;
  504. }
  505. ata_link_dbg(link, "reg_in = %d\n", sc_reg);
  506. iowrite32(val, ssr_base + (sc_reg * 4));
  507. return 0;
  508. }
  509. static int sata_fsl_scr_read(struct ata_link *link,
  510. unsigned int sc_reg_in, u32 *val)
  511. {
  512. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  513. void __iomem *ssr_base = host_priv->ssr_base;
  514. unsigned int sc_reg;
  515. switch (sc_reg_in) {
  516. case SCR_STATUS:
  517. case SCR_ERROR:
  518. case SCR_CONTROL:
  519. case SCR_ACTIVE:
  520. sc_reg = sc_reg_in;
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. ata_link_dbg(link, "reg_in = %d\n", sc_reg);
  526. *val = ioread32(ssr_base + (sc_reg * 4));
  527. return 0;
  528. }
  529. static void sata_fsl_freeze(struct ata_port *ap)
  530. {
  531. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  532. void __iomem *hcr_base = host_priv->hcr_base;
  533. u32 temp;
  534. ata_port_dbg(ap, "CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  535. ioread32(CQ + hcr_base),
  536. ioread32(CA + hcr_base),
  537. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  538. ata_port_dbg(ap, "CmdStat = 0x%x\n",
  539. ioread32(host_priv->csr_base + COMMANDSTAT));
  540. /* disable interrupts on the controller/port */
  541. temp = ioread32(hcr_base + HCONTROL);
  542. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  543. ata_port_dbg(ap, "HControl = 0x%x, HStatus = 0x%x\n",
  544. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  545. }
  546. static void sata_fsl_thaw(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. /* ack. any pending IRQs for this controller/port */
  552. temp = ioread32(hcr_base + HSTATUS);
  553. ata_port_dbg(ap, "pending IRQs = 0x%x\n", (temp & 0x3F));
  554. if (temp & 0x3F)
  555. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  556. /* enable interrupts on the controller/port */
  557. temp = ioread32(hcr_base + HCONTROL);
  558. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  559. ata_port_dbg(ap, "HControl = 0x%x, HStatus = 0x%x\n",
  560. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  561. }
  562. static void sata_fsl_pmp_attach(struct ata_port *ap)
  563. {
  564. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  565. void __iomem *hcr_base = host_priv->hcr_base;
  566. u32 temp;
  567. temp = ioread32(hcr_base + HCONTROL);
  568. iowrite32((temp | HCONTROL_PMP_ATTACHED), hcr_base + HCONTROL);
  569. }
  570. static void sata_fsl_pmp_detach(struct ata_port *ap)
  571. {
  572. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  573. void __iomem *hcr_base = host_priv->hcr_base;
  574. u32 temp;
  575. temp = ioread32(hcr_base + HCONTROL);
  576. temp &= ~HCONTROL_PMP_ATTACHED;
  577. iowrite32(temp, hcr_base + HCONTROL);
  578. /* enable interrupts on the controller/port */
  579. temp = ioread32(hcr_base + HCONTROL);
  580. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  581. }
  582. static int sata_fsl_port_start(struct ata_port *ap)
  583. {
  584. struct device *dev = ap->host->dev;
  585. struct sata_fsl_port_priv *pp;
  586. void *mem;
  587. dma_addr_t mem_dma;
  588. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  589. void __iomem *hcr_base = host_priv->hcr_base;
  590. u32 temp;
  591. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  592. if (!pp)
  593. return -ENOMEM;
  594. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  595. GFP_KERNEL);
  596. if (!mem) {
  597. kfree(pp);
  598. return -ENOMEM;
  599. }
  600. pp->cmdslot = mem;
  601. pp->cmdslot_paddr = mem_dma;
  602. mem += SATA_FSL_CMD_SLOT_SIZE;
  603. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  604. pp->cmdentry = mem;
  605. pp->cmdentry_paddr = mem_dma;
  606. ap->private_data = pp;
  607. ata_port_dbg(ap, "CHBA = 0x%lx, cmdentry_phys = 0x%lx\n",
  608. (unsigned long)pp->cmdslot_paddr,
  609. (unsigned long)pp->cmdentry_paddr);
  610. /* Now, update the CHBA register in host controller cmd register set */
  611. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  612. /*
  613. * Now, we can bring the controller on-line & also initiate
  614. * the COMINIT sequence, we simply return here and the boot-probing
  615. * & device discovery process is re-initiated by libATA using a
  616. * Softreset EH (dummy) session. Hence, boot probing and device
  617. * discovey will be part of sata_fsl_softreset() callback.
  618. */
  619. temp = ioread32(hcr_base + HCONTROL);
  620. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  621. ata_port_dbg(ap, "HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  622. ata_port_dbg(ap, "HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  623. ata_port_dbg(ap, "CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  624. return 0;
  625. }
  626. static void sata_fsl_port_stop(struct ata_port *ap)
  627. {
  628. struct device *dev = ap->host->dev;
  629. struct sata_fsl_port_priv *pp = ap->private_data;
  630. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  631. void __iomem *hcr_base = host_priv->hcr_base;
  632. u32 temp;
  633. /*
  634. * Force host controller to go off-line, aborting current operations
  635. */
  636. temp = ioread32(hcr_base + HCONTROL);
  637. temp &= ~HCONTROL_ONLINE_PHY_RST;
  638. temp |= HCONTROL_FORCE_OFFLINE;
  639. iowrite32(temp, hcr_base + HCONTROL);
  640. /* Poll for controller to go offline - should happen immediately */
  641. ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  642. ap->private_data = NULL;
  643. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  644. pp->cmdslot, pp->cmdslot_paddr);
  645. kfree(pp);
  646. }
  647. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  648. {
  649. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  650. void __iomem *hcr_base = host_priv->hcr_base;
  651. struct ata_taskfile tf;
  652. u32 temp;
  653. temp = ioread32(hcr_base + SIGNATURE);
  654. ata_port_dbg(ap, "HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  655. ata_port_dbg(ap, "HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  656. tf.lbah = (temp >> 24) & 0xff;
  657. tf.lbam = (temp >> 16) & 0xff;
  658. tf.lbal = (temp >> 8) & 0xff;
  659. tf.nsect = temp & 0xff;
  660. return ata_port_classify(ap, &tf);
  661. }
  662. static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
  663. unsigned long deadline)
  664. {
  665. struct ata_port *ap = link->ap;
  666. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  667. void __iomem *hcr_base = host_priv->hcr_base;
  668. u32 temp;
  669. int i = 0;
  670. unsigned long start_jiffies;
  671. try_offline_again:
  672. /*
  673. * Force host controller to go off-line, aborting current operations
  674. */
  675. temp = ioread32(hcr_base + HCONTROL);
  676. temp &= ~HCONTROL_ONLINE_PHY_RST;
  677. iowrite32(temp, hcr_base + HCONTROL);
  678. /* Poll for controller to go offline */
  679. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE,
  680. 1, 500);
  681. if (temp & ONLINE) {
  682. ata_port_err(ap, "Hardreset failed, not off-lined %d\n", i);
  683. /*
  684. * Try to offline controller atleast twice
  685. */
  686. i++;
  687. if (i == 2)
  688. goto err;
  689. else
  690. goto try_offline_again;
  691. }
  692. ata_port_dbg(ap, "hardreset, controller off-lined\n"
  693. "HStatus = 0x%x HControl = 0x%x\n",
  694. ioread32(hcr_base + HSTATUS),
  695. ioread32(hcr_base + HCONTROL));
  696. /*
  697. * PHY reset should remain asserted for atleast 1ms
  698. */
  699. ata_msleep(ap, 1);
  700. sata_set_spd(link);
  701. /*
  702. * Now, bring the host controller online again, this can take time
  703. * as PHY reset and communication establishment, 1st D2H FIS and
  704. * device signature update is done, on safe side assume 500ms
  705. * NOTE : Host online status may be indicated immediately!!
  706. */
  707. temp = ioread32(hcr_base + HCONTROL);
  708. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  709. temp |= HCONTROL_PMP_ATTACHED;
  710. iowrite32(temp, hcr_base + HCONTROL);
  711. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  712. if (!(temp & ONLINE)) {
  713. ata_port_err(ap, "Hardreset failed, not on-lined\n");
  714. goto err;
  715. }
  716. ata_port_dbg(ap, "controller off-lined & on-lined\n"
  717. "HStatus = 0x%x HControl = 0x%x\n",
  718. ioread32(hcr_base + HSTATUS),
  719. ioread32(hcr_base + HCONTROL));
  720. /*
  721. * First, wait for the PHYRDY change to occur before waiting for
  722. * the signature, and also verify if SStatus indicates device
  723. * presence
  724. */
  725. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  726. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  727. ata_port_warn(ap, "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  728. ioread32(hcr_base + HSTATUS));
  729. *class = ATA_DEV_NONE;
  730. return 0;
  731. }
  732. /*
  733. * Wait for the first D2H from device,i.e,signature update notification
  734. */
  735. start_jiffies = jiffies;
  736. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0x10,
  737. 500, jiffies_to_msecs(deadline - start_jiffies));
  738. if ((temp & 0xFF) != 0x18) {
  739. ata_port_warn(ap, "No Signature Update\n");
  740. *class = ATA_DEV_NONE;
  741. goto do_followup_srst;
  742. } else {
  743. ata_port_info(ap, "Signature Update detected @ %d msecs\n",
  744. jiffies_to_msecs(jiffies - start_jiffies));
  745. *class = sata_fsl_dev_classify(ap);
  746. return 0;
  747. }
  748. do_followup_srst:
  749. /*
  750. * request libATA to perform follow-up softreset
  751. */
  752. return -EAGAIN;
  753. err:
  754. return -EIO;
  755. }
  756. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  757. unsigned long deadline)
  758. {
  759. struct ata_port *ap = link->ap;
  760. struct sata_fsl_port_priv *pp = ap->private_data;
  761. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  762. void __iomem *hcr_base = host_priv->hcr_base;
  763. int pmp = sata_srst_pmp(link);
  764. u32 temp;
  765. struct ata_taskfile tf;
  766. u8 *cfis;
  767. u32 Serror;
  768. if (ata_link_offline(link)) {
  769. *class = ATA_DEV_NONE;
  770. return 0;
  771. }
  772. /*
  773. * Send a device reset (SRST) explicitly on command slot #0
  774. * Check : will the command queue (reg) be cleared during offlining ??
  775. * Also we will be online only if Phy commn. has been established
  776. * and device presence has been detected, therefore if we have
  777. * reached here, we can send a command to the target device
  778. */
  779. ata_tf_init(link->device, &tf);
  780. cfis = (u8 *) &pp->cmdentry->cfis;
  781. /* device reset/SRST is a control register update FIS, uses tag0 */
  782. sata_fsl_setup_cmd_hdr_entry(ap, pp, 0,
  783. SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  784. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  785. ata_tf_to_fis(&tf, pmp, 0, cfis);
  786. ata_port_dbg(ap, "Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  787. cfis[0], cfis[1], cfis[2], cfis[3]);
  788. /*
  789. * Queue SRST command to the controller/device, ensure that no
  790. * other commands are active on the controller/device
  791. */
  792. ata_port_dbg(ap, "CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  793. ioread32(CQ + hcr_base),
  794. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  795. iowrite32(0xFFFF, CC + hcr_base);
  796. if (pmp != SATA_PMP_CTRL_PORT)
  797. iowrite32(pmp, CQPMP + hcr_base);
  798. iowrite32(1, CQ + hcr_base);
  799. temp = ata_wait_register(ap, CQ + hcr_base, 0x1, 0x1, 1, 5000);
  800. if (temp & 0x1) {
  801. ata_port_warn(ap, "ATA_SRST issue failed\n");
  802. ata_port_dbg(ap, "Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  803. ioread32(CQ + hcr_base),
  804. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  805. sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
  806. ata_port_dbg(ap, "HStatus = 0x%x HControl = 0x%x Serror = 0x%x\n",
  807. ioread32(hcr_base + HSTATUS),
  808. ioread32(hcr_base + HCONTROL),
  809. Serror);
  810. goto err;
  811. }
  812. ata_msleep(ap, 1);
  813. /*
  814. * SATA device enters reset state after receiving a Control register
  815. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  816. * FIS with SRST bit cleared, then the device does internal diags &
  817. * initialization, followed by indicating it's initialization status
  818. * using ATA signature D2H register FIS to the host controller.
  819. */
  820. sata_fsl_setup_cmd_hdr_entry(ap, pp, 0,
  821. CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
  822. 0, 0, 5);
  823. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  824. ata_tf_to_fis(&tf, pmp, 0, cfis);
  825. if (pmp != SATA_PMP_CTRL_PORT)
  826. iowrite32(pmp, CQPMP + hcr_base);
  827. iowrite32(1, CQ + hcr_base);
  828. ata_msleep(ap, 150); /* ?? */
  829. /*
  830. * The above command would have signalled an interrupt on command
  831. * complete, which needs special handling, by clearing the Nth
  832. * command bit of the CCreg
  833. */
  834. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  835. *class = ATA_DEV_NONE;
  836. /* Verify if SStatus indicates device presence */
  837. if (ata_link_online(link)) {
  838. /*
  839. * if we are here, device presence has been detected,
  840. * 1st D2H FIS would have been received, but sfis in
  841. * command desc. is not updated, but signature register
  842. * would have been updated
  843. */
  844. *class = sata_fsl_dev_classify(ap);
  845. ata_port_dbg(ap, "ccreg = 0x%x\n", ioread32(hcr_base + CC));
  846. ata_port_dbg(ap, "cereg = 0x%x\n", ioread32(hcr_base + CE));
  847. }
  848. return 0;
  849. err:
  850. return -EIO;
  851. }
  852. static void sata_fsl_error_handler(struct ata_port *ap)
  853. {
  854. sata_pmp_error_handler(ap);
  855. }
  856. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  857. {
  858. if (qc->flags & ATA_QCFLAG_FAILED)
  859. qc->err_mask |= AC_ERR_OTHER;
  860. if (qc->err_mask) {
  861. /* make DMA engine forget about the failed command */
  862. }
  863. }
  864. static void sata_fsl_error_intr(struct ata_port *ap)
  865. {
  866. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  867. void __iomem *hcr_base = host_priv->hcr_base;
  868. u32 hstatus, dereg=0, cereg = 0, SError = 0;
  869. unsigned int err_mask = 0, action = 0;
  870. int freeze = 0, abort=0;
  871. struct ata_link *link = NULL;
  872. struct ata_queued_cmd *qc = NULL;
  873. struct ata_eh_info *ehi;
  874. hstatus = ioread32(hcr_base + HSTATUS);
  875. cereg = ioread32(hcr_base + CE);
  876. /* first, analyze and record host port events */
  877. link = &ap->link;
  878. ehi = &link->eh_info;
  879. ata_ehi_clear_desc(ehi);
  880. /*
  881. * Handle & Clear SError
  882. */
  883. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  884. if (unlikely(SError & 0xFFFF0000))
  885. sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
  886. ata_port_dbg(ap, "hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  887. hstatus, cereg, ioread32(hcr_base + DE), SError);
  888. /* handle fatal errors */
  889. if (hstatus & FATAL_ERROR_DECODE) {
  890. ehi->err_mask |= AC_ERR_ATA_BUS;
  891. ehi->action |= ATA_EH_SOFTRESET;
  892. freeze = 1;
  893. }
  894. /* Handle SDB FIS receive & notify update */
  895. if (hstatus & INT_ON_SNOTIFY_UPDATE)
  896. sata_async_notification(ap);
  897. /* Handle PHYRDY change notification */
  898. if (hstatus & INT_ON_PHYRDY_CHG) {
  899. ata_port_dbg(ap, "PHYRDY change indication\n");
  900. /* Setup a soft-reset EH action */
  901. ata_ehi_hotplugged(ehi);
  902. ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
  903. freeze = 1;
  904. }
  905. /* handle single device errors */
  906. if (cereg) {
  907. /*
  908. * clear the command error, also clears queue to the device
  909. * in error, and we can (re)issue commands to this device.
  910. * When a device is in error all commands queued into the
  911. * host controller and at the device are considered aborted
  912. * and the queue for that device is stopped. Now, after
  913. * clearing the device error, we can issue commands to the
  914. * device to interrogate it to find the source of the error.
  915. */
  916. abort = 1;
  917. ata_port_dbg(ap, "single device error, CE=0x%x, DE=0x%x\n",
  918. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  919. /* find out the offending link and qc */
  920. if (ap->nr_pmp_links) {
  921. unsigned int dev_num;
  922. dereg = ioread32(hcr_base + DE);
  923. iowrite32(dereg, hcr_base + DE);
  924. iowrite32(cereg, hcr_base + CE);
  925. dev_num = ffs(dereg) - 1;
  926. if (dev_num < ap->nr_pmp_links && dereg != 0) {
  927. link = &ap->pmp_link[dev_num];
  928. ehi = &link->eh_info;
  929. qc = ata_qc_from_tag(ap, link->active_tag);
  930. /*
  931. * We should consider this as non fatal error,
  932. * and TF must be updated as done below.
  933. */
  934. err_mask |= AC_ERR_DEV;
  935. } else {
  936. err_mask |= AC_ERR_HSM;
  937. action |= ATA_EH_HARDRESET;
  938. freeze = 1;
  939. }
  940. } else {
  941. dereg = ioread32(hcr_base + DE);
  942. iowrite32(dereg, hcr_base + DE);
  943. iowrite32(cereg, hcr_base + CE);
  944. qc = ata_qc_from_tag(ap, link->active_tag);
  945. /*
  946. * We should consider this as non fatal error,
  947. * and TF must be updated as done below.
  948. */
  949. err_mask |= AC_ERR_DEV;
  950. }
  951. }
  952. /* record error info */
  953. if (qc)
  954. qc->err_mask |= err_mask;
  955. else
  956. ehi->err_mask |= err_mask;
  957. ehi->action |= action;
  958. /* freeze or abort */
  959. if (freeze)
  960. ata_port_freeze(ap);
  961. else if (abort) {
  962. if (qc)
  963. ata_link_abort(qc->dev->link);
  964. else
  965. ata_port_abort(ap);
  966. }
  967. }
  968. static void sata_fsl_host_intr(struct ata_port *ap)
  969. {
  970. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  971. void __iomem *hcr_base = host_priv->hcr_base;
  972. u32 hstatus, done_mask = 0;
  973. struct ata_queued_cmd *qc;
  974. u32 SError;
  975. u32 tag;
  976. u32 status_mask = INT_ON_ERROR;
  977. hstatus = ioread32(hcr_base + HSTATUS);
  978. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  979. /* Read command completed register */
  980. done_mask = ioread32(hcr_base + CC);
  981. /* Workaround for data length mismatch errata */
  982. if (unlikely(hstatus & INT_ON_DATA_LENGTH_MISMATCH)) {
  983. ata_qc_for_each_with_internal(ap, qc, tag) {
  984. if (qc && ata_is_atapi(qc->tf.protocol)) {
  985. u32 hcontrol;
  986. /* Set HControl[27] to clear error registers */
  987. hcontrol = ioread32(hcr_base + HCONTROL);
  988. iowrite32(hcontrol | CLEAR_ERROR,
  989. hcr_base + HCONTROL);
  990. /* Clear HControl[27] */
  991. iowrite32(hcontrol & ~CLEAR_ERROR,
  992. hcr_base + HCONTROL);
  993. /* Clear SError[E] bit */
  994. sata_fsl_scr_write(&ap->link, SCR_ERROR,
  995. SError);
  996. /* Ignore fatal error and device error */
  997. status_mask &= ~(INT_ON_SINGL_DEVICE_ERR
  998. | INT_ON_FATAL_ERR);
  999. break;
  1000. }
  1001. }
  1002. }
  1003. if (unlikely(SError & 0xFFFF0000)) {
  1004. ata_port_dbg(ap, "serror @host_intr : 0x%x\n", SError);
  1005. sata_fsl_error_intr(ap);
  1006. }
  1007. if (unlikely(hstatus & status_mask)) {
  1008. ata_port_dbg(ap, "error interrupt!!\n");
  1009. sata_fsl_error_intr(ap);
  1010. return;
  1011. }
  1012. ata_port_dbg(ap, "Status of all queues :\n");
  1013. ata_port_dbg(ap, "done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%llx\n",
  1014. done_mask,
  1015. ioread32(hcr_base + CA),
  1016. ioread32(hcr_base + CE),
  1017. ioread32(hcr_base + CQ),
  1018. ap->qc_active);
  1019. if (done_mask & ap->qc_active) {
  1020. int i;
  1021. /* clear CC bit, this will also complete the interrupt */
  1022. iowrite32(done_mask, hcr_base + CC);
  1023. ata_port_dbg(ap, "Status of all queues: done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  1024. done_mask, ioread32(hcr_base + CA),
  1025. ioread32(hcr_base + CE));
  1026. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  1027. if (done_mask & (1 << i))
  1028. ata_port_dbg(ap, "completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  1029. i, ioread32(hcr_base + CC),
  1030. ioread32(hcr_base + CA));
  1031. }
  1032. ata_qc_complete_multiple(ap, ata_qc_get_active(ap) ^ done_mask);
  1033. return;
  1034. } else if ((ap->qc_active & (1ULL << ATA_TAG_INTERNAL))) {
  1035. iowrite32(1, hcr_base + CC);
  1036. qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  1037. ata_port_dbg(ap, "completing non-ncq cmd, CC=0x%x\n",
  1038. ioread32(hcr_base + CC));
  1039. if (qc) {
  1040. ata_qc_complete(qc);
  1041. }
  1042. } else {
  1043. /* Spurious Interrupt!! */
  1044. ata_port_dbg(ap, "spurious interrupt!!, CC = 0x%x\n",
  1045. ioread32(hcr_base + CC));
  1046. iowrite32(done_mask, hcr_base + CC);
  1047. return;
  1048. }
  1049. }
  1050. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  1051. {
  1052. struct ata_host *host = dev_instance;
  1053. struct sata_fsl_host_priv *host_priv = host->private_data;
  1054. void __iomem *hcr_base = host_priv->hcr_base;
  1055. u32 interrupt_enables;
  1056. unsigned handled = 0;
  1057. struct ata_port *ap;
  1058. /* ack. any pending IRQs for this controller/port */
  1059. interrupt_enables = ioread32(hcr_base + HSTATUS);
  1060. interrupt_enables &= 0x3F;
  1061. if (!interrupt_enables)
  1062. return IRQ_NONE;
  1063. spin_lock(&host->lock);
  1064. /* Assuming one port per host controller */
  1065. ap = host->ports[0];
  1066. if (ap) {
  1067. sata_fsl_host_intr(ap);
  1068. } else {
  1069. dev_warn(host->dev, "interrupt on disabled port 0\n");
  1070. }
  1071. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  1072. handled = 1;
  1073. spin_unlock(&host->lock);
  1074. return IRQ_RETVAL(handled);
  1075. }
  1076. /*
  1077. * Multiple ports are represented by multiple SATA controllers with
  1078. * one port per controller
  1079. */
  1080. static int sata_fsl_init_controller(struct ata_host *host)
  1081. {
  1082. struct sata_fsl_host_priv *host_priv = host->private_data;
  1083. void __iomem *hcr_base = host_priv->hcr_base;
  1084. u32 temp;
  1085. /*
  1086. * NOTE : We cannot bring the controller online before setting
  1087. * the CHBA, hence main controller initialization is done as
  1088. * part of the port_start() callback
  1089. */
  1090. /* sata controller to operate in enterprise mode */
  1091. temp = ioread32(hcr_base + HCONTROL);
  1092. iowrite32(temp & ~HCONTROL_LEGACY, hcr_base + HCONTROL);
  1093. /* ack. any pending IRQs for this controller/port */
  1094. temp = ioread32(hcr_base + HSTATUS);
  1095. if (temp & 0x3F)
  1096. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  1097. /* Keep interrupts disabled on the controller */
  1098. temp = ioread32(hcr_base + HCONTROL);
  1099. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  1100. /* Disable interrupt coalescing control(icc), for the moment */
  1101. dev_dbg(host->dev, "icc = 0x%x\n", ioread32(hcr_base + ICC));
  1102. iowrite32(0x01000000, hcr_base + ICC);
  1103. /* clear error registers, SError is cleared by libATA */
  1104. iowrite32(0x00000FFFF, hcr_base + CE);
  1105. iowrite32(0x00000FFFF, hcr_base + DE);
  1106. /*
  1107. * reset the number of command complete bits which will cause the
  1108. * interrupt to be signaled
  1109. */
  1110. fsl_sata_set_irq_coalescing(host, intr_coalescing_count,
  1111. intr_coalescing_ticks);
  1112. /*
  1113. * host controller will be brought on-line, during xx_port_start()
  1114. * callback, that should also initiate the OOB, COMINIT sequence
  1115. */
  1116. dev_dbg(host->dev, "HStatus = 0x%x HControl = 0x%x\n",
  1117. ioread32(hcr_base + HSTATUS), ioread32(hcr_base + HCONTROL));
  1118. return 0;
  1119. }
  1120. static void sata_fsl_host_stop(struct ata_host *host)
  1121. {
  1122. struct sata_fsl_host_priv *host_priv = host->private_data;
  1123. iounmap(host_priv->hcr_base);
  1124. kfree(host_priv);
  1125. }
  1126. /*
  1127. * scsi mid-layer and libata interface structures
  1128. */
  1129. static struct scsi_host_template sata_fsl_sht = {
  1130. ATA_NCQ_SHT_QD("sata_fsl", SATA_FSL_QUEUE_DEPTH),
  1131. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  1132. .dma_boundary = ATA_DMA_BOUNDARY,
  1133. };
  1134. static struct ata_port_operations sata_fsl_ops = {
  1135. .inherits = &sata_pmp_port_ops,
  1136. .qc_defer = ata_std_qc_defer,
  1137. .qc_prep = sata_fsl_qc_prep,
  1138. .qc_issue = sata_fsl_qc_issue,
  1139. .qc_fill_rtf = sata_fsl_qc_fill_rtf,
  1140. .scr_read = sata_fsl_scr_read,
  1141. .scr_write = sata_fsl_scr_write,
  1142. .freeze = sata_fsl_freeze,
  1143. .thaw = sata_fsl_thaw,
  1144. .softreset = sata_fsl_softreset,
  1145. .hardreset = sata_fsl_hardreset,
  1146. .pmp_softreset = sata_fsl_softreset,
  1147. .error_handler = sata_fsl_error_handler,
  1148. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1149. .port_start = sata_fsl_port_start,
  1150. .port_stop = sata_fsl_port_stop,
  1151. .host_stop = sata_fsl_host_stop,
  1152. .pmp_attach = sata_fsl_pmp_attach,
  1153. .pmp_detach = sata_fsl_pmp_detach,
  1154. };
  1155. static const struct ata_port_info sata_fsl_port_info[] = {
  1156. {
  1157. .flags = SATA_FSL_HOST_FLAGS,
  1158. .pio_mask = ATA_PIO4,
  1159. .udma_mask = ATA_UDMA6,
  1160. .port_ops = &sata_fsl_ops,
  1161. },
  1162. };
  1163. static int sata_fsl_probe(struct platform_device *ofdev)
  1164. {
  1165. int retval = -ENXIO;
  1166. void __iomem *hcr_base = NULL;
  1167. void __iomem *ssr_base = NULL;
  1168. void __iomem *csr_base = NULL;
  1169. struct sata_fsl_host_priv *host_priv = NULL;
  1170. int irq;
  1171. struct ata_host *host = NULL;
  1172. u32 temp;
  1173. struct ata_port_info pi = sata_fsl_port_info[0];
  1174. const struct ata_port_info *ppi[] = { &pi, NULL };
  1175. dev_info(&ofdev->dev, "Sata FSL Platform/CSB Driver init\n");
  1176. hcr_base = of_iomap(ofdev->dev.of_node, 0);
  1177. if (!hcr_base)
  1178. goto error_exit_with_cleanup;
  1179. ssr_base = hcr_base + 0x100;
  1180. csr_base = hcr_base + 0x140;
  1181. if (!of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc8315-sata")) {
  1182. temp = ioread32(csr_base + TRANSCFG);
  1183. temp = temp & 0xffffffe0;
  1184. iowrite32(temp | TRANSCFG_RX_WATER_MARK, csr_base + TRANSCFG);
  1185. }
  1186. dev_dbg(&ofdev->dev, "@reset i/o = 0x%x\n",
  1187. ioread32(csr_base + TRANSCFG));
  1188. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1189. if (!host_priv)
  1190. goto error_exit_with_cleanup;
  1191. host_priv->hcr_base = hcr_base;
  1192. host_priv->ssr_base = ssr_base;
  1193. host_priv->csr_base = csr_base;
  1194. irq = platform_get_irq(ofdev, 0);
  1195. if (irq < 0) {
  1196. retval = irq;
  1197. goto error_exit_with_cleanup;
  1198. }
  1199. host_priv->irq = irq;
  1200. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,pq-sata-v2"))
  1201. host_priv->data_snoop = DATA_SNOOP_ENABLE_V2;
  1202. else
  1203. host_priv->data_snoop = DATA_SNOOP_ENABLE_V1;
  1204. /* allocate host structure */
  1205. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1206. if (!host) {
  1207. retval = -ENOMEM;
  1208. goto error_exit_with_cleanup;
  1209. }
  1210. /* host->iomap is not used currently */
  1211. host->private_data = host_priv;
  1212. /* initialize host controller */
  1213. sata_fsl_init_controller(host);
  1214. /*
  1215. * Now, register with libATA core, this will also initiate the
  1216. * device discovery process, invoking our port_start() handler &
  1217. * error_handler() to execute a dummy Softreset EH session
  1218. */
  1219. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1220. &sata_fsl_sht);
  1221. host_priv->intr_coalescing.show = fsl_sata_intr_coalescing_show;
  1222. host_priv->intr_coalescing.store = fsl_sata_intr_coalescing_store;
  1223. sysfs_attr_init(&host_priv->intr_coalescing.attr);
  1224. host_priv->intr_coalescing.attr.name = "intr_coalescing";
  1225. host_priv->intr_coalescing.attr.mode = S_IRUGO | S_IWUSR;
  1226. retval = device_create_file(host->dev, &host_priv->intr_coalescing);
  1227. if (retval)
  1228. goto error_exit_with_cleanup;
  1229. host_priv->rx_watermark.show = fsl_sata_rx_watermark_show;
  1230. host_priv->rx_watermark.store = fsl_sata_rx_watermark_store;
  1231. sysfs_attr_init(&host_priv->rx_watermark.attr);
  1232. host_priv->rx_watermark.attr.name = "rx_watermark";
  1233. host_priv->rx_watermark.attr.mode = S_IRUGO | S_IWUSR;
  1234. retval = device_create_file(host->dev, &host_priv->rx_watermark);
  1235. if (retval) {
  1236. device_remove_file(&ofdev->dev, &host_priv->intr_coalescing);
  1237. goto error_exit_with_cleanup;
  1238. }
  1239. return 0;
  1240. error_exit_with_cleanup:
  1241. if (host)
  1242. ata_host_detach(host);
  1243. if (hcr_base)
  1244. iounmap(hcr_base);
  1245. kfree(host_priv);
  1246. return retval;
  1247. }
  1248. static int sata_fsl_remove(struct platform_device *ofdev)
  1249. {
  1250. struct ata_host *host = platform_get_drvdata(ofdev);
  1251. struct sata_fsl_host_priv *host_priv = host->private_data;
  1252. device_remove_file(&ofdev->dev, &host_priv->intr_coalescing);
  1253. device_remove_file(&ofdev->dev, &host_priv->rx_watermark);
  1254. ata_host_detach(host);
  1255. return 0;
  1256. }
  1257. #ifdef CONFIG_PM_SLEEP
  1258. static int sata_fsl_suspend(struct platform_device *op, pm_message_t state)
  1259. {
  1260. struct ata_host *host = platform_get_drvdata(op);
  1261. ata_host_suspend(host, state);
  1262. return 0;
  1263. }
  1264. static int sata_fsl_resume(struct platform_device *op)
  1265. {
  1266. struct ata_host *host = platform_get_drvdata(op);
  1267. struct sata_fsl_host_priv *host_priv = host->private_data;
  1268. int ret;
  1269. void __iomem *hcr_base = host_priv->hcr_base;
  1270. struct ata_port *ap = host->ports[0];
  1271. struct sata_fsl_port_priv *pp = ap->private_data;
  1272. ret = sata_fsl_init_controller(host);
  1273. if (ret) {
  1274. dev_err(&op->dev, "Error initializing hardware\n");
  1275. return ret;
  1276. }
  1277. /* Recovery the CHBA register in host controller cmd register set */
  1278. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  1279. iowrite32((ioread32(hcr_base + HCONTROL)
  1280. | HCONTROL_ONLINE_PHY_RST
  1281. | HCONTROL_SNOOP_ENABLE
  1282. | HCONTROL_PMP_ATTACHED),
  1283. hcr_base + HCONTROL);
  1284. ata_host_resume(host);
  1285. return 0;
  1286. }
  1287. #endif
  1288. static const struct of_device_id fsl_sata_match[] = {
  1289. { .compatible = "fsl,pq-sata", },
  1290. { .compatible = "fsl,pq-sata-v2", },
  1291. { /* sentinel */ }
  1292. };
  1293. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1294. static struct platform_driver fsl_sata_driver = {
  1295. .driver = {
  1296. .name = "fsl-sata",
  1297. .of_match_table = fsl_sata_match,
  1298. },
  1299. .probe = sata_fsl_probe,
  1300. .remove = sata_fsl_remove,
  1301. #ifdef CONFIG_PM_SLEEP
  1302. .suspend = sata_fsl_suspend,
  1303. .resume = sata_fsl_resume,
  1304. #endif
  1305. };
  1306. module_platform_driver(fsl_sata_driver);
  1307. MODULE_LICENSE("GPL");
  1308. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1309. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1310. MODULE_VERSION("1.10");