ahci_xgene.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * AppliedMicro X-Gene SoC SATA Host Controller Driver
  3. *
  4. * Copyright (c) 2014, Applied Micro Circuits Corporation
  5. * Author: Loc Ho <lho@apm.com>
  6. * Tuan Phan <tphan@apm.com>
  7. * Suman Tripathi <stripathi@apm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * NOTE: PM support is not currently available.
  23. *
  24. */
  25. #include <linux/acpi.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/ahci_platform.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/phy/phy.h>
  33. #include "ahci.h"
  34. #define DRV_NAME "xgene-ahci"
  35. /* Max # of disk per a controller */
  36. #define MAX_AHCI_CHN_PERCTR 2
  37. /* MUX CSR */
  38. #define SATA_ENET_CONFIG_REG 0x00000000
  39. #define CFG_SATA_ENET_SELECT_MASK 0x00000001
  40. /* SATA core host controller CSR */
  41. #define SLVRDERRATTRIBUTES 0x00000000
  42. #define SLVWRERRATTRIBUTES 0x00000004
  43. #define MSTRDERRATTRIBUTES 0x00000008
  44. #define MSTWRERRATTRIBUTES 0x0000000c
  45. #define BUSCTLREG 0x00000014
  46. #define IOFMSTRWAUX 0x00000018
  47. #define INTSTATUSMASK 0x0000002c
  48. #define ERRINTSTATUS 0x00000030
  49. #define ERRINTSTATUSMASK 0x00000034
  50. /* SATA host AHCI CSR */
  51. #define PORTCFG 0x000000a4
  52. #define PORTADDR_SET(dst, src) \
  53. (((dst) & ~0x0000003f) | (((u32)(src)) & 0x0000003f))
  54. #define PORTPHY1CFG 0x000000a8
  55. #define PORTPHY1CFG_FRCPHYRDY_SET(dst, src) \
  56. (((dst) & ~0x00100000) | (((u32)(src) << 0x14) & 0x00100000))
  57. #define PORTPHY2CFG 0x000000ac
  58. #define PORTPHY3CFG 0x000000b0
  59. #define PORTPHY4CFG 0x000000b4
  60. #define PORTPHY5CFG 0x000000b8
  61. #define SCTL0 0x0000012C
  62. #define PORTPHY5CFG_RTCHG_SET(dst, src) \
  63. (((dst) & ~0xfff00000) | (((u32)(src) << 0x14) & 0xfff00000))
  64. #define PORTAXICFG_EN_CONTEXT_SET(dst, src) \
  65. (((dst) & ~0x01000000) | (((u32)(src) << 0x18) & 0x01000000))
  66. #define PORTAXICFG 0x000000bc
  67. #define PORTAXICFG_OUTTRANS_SET(dst, src) \
  68. (((dst) & ~0x00f00000) | (((u32)(src) << 0x14) & 0x00f00000))
  69. #define PORTRANSCFG 0x000000c8
  70. #define PORTRANSCFG_RXWM_SET(dst, src) \
  71. (((dst) & ~0x0000007f) | (((u32)(src)) & 0x0000007f))
  72. /* SATA host controller AXI CSR */
  73. #define INT_SLV_TMOMASK 0x00000010
  74. /* SATA diagnostic CSR */
  75. #define CFG_MEM_RAM_SHUTDOWN 0x00000070
  76. #define BLOCK_MEM_RDY 0x00000074
  77. /* Max retry for link down */
  78. #define MAX_LINK_DOWN_RETRY 3
  79. enum xgene_ahci_version {
  80. XGENE_AHCI_V1 = 1,
  81. XGENE_AHCI_V2,
  82. };
  83. struct xgene_ahci_context {
  84. struct ahci_host_priv *hpriv;
  85. struct device *dev;
  86. u8 last_cmd[MAX_AHCI_CHN_PERCTR]; /* tracking the last command issued*/
  87. u32 class[MAX_AHCI_CHN_PERCTR]; /* tracking the class of device */
  88. void __iomem *csr_core; /* Core CSR address of IP */
  89. void __iomem *csr_diag; /* Diag CSR address of IP */
  90. void __iomem *csr_axi; /* AXI CSR address of IP */
  91. void __iomem *csr_mux; /* MUX CSR address of IP */
  92. };
  93. static int xgene_ahci_init_memram(struct xgene_ahci_context *ctx)
  94. {
  95. dev_dbg(ctx->dev, "Release memory from shutdown\n");
  96. writel(0x0, ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN);
  97. readl(ctx->csr_diag + CFG_MEM_RAM_SHUTDOWN); /* Force a barrier */
  98. msleep(1); /* reset may take up to 1ms */
  99. if (readl(ctx->csr_diag + BLOCK_MEM_RDY) != 0xFFFFFFFF) {
  100. dev_err(ctx->dev, "failed to release memory from shutdown\n");
  101. return -ENODEV;
  102. }
  103. return 0;
  104. }
  105. /**
  106. * xgene_ahci_poll_reg_val- Poll a register on a specific value.
  107. * @ap : ATA port of interest.
  108. * @reg : Register of interest.
  109. * @val : Value to be attained.
  110. * @interval : waiting interval for polling.
  111. * @timeout : timeout for achieving the value.
  112. */
  113. static int xgene_ahci_poll_reg_val(struct ata_port *ap,
  114. void __iomem *reg, unsigned
  115. int val, unsigned long interval,
  116. unsigned long timeout)
  117. {
  118. unsigned long deadline;
  119. unsigned int tmp;
  120. tmp = ioread32(reg);
  121. deadline = ata_deadline(jiffies, timeout);
  122. while (tmp != val && time_before(jiffies, deadline)) {
  123. ata_msleep(ap, interval);
  124. tmp = ioread32(reg);
  125. }
  126. return tmp;
  127. }
  128. /**
  129. * xgene_ahci_restart_engine - Restart the dma engine.
  130. * @ap : ATA port of interest
  131. *
  132. * Waits for completion of multiple commands and restarts
  133. * the DMA engine inside the controller.
  134. */
  135. static int xgene_ahci_restart_engine(struct ata_port *ap)
  136. {
  137. struct ahci_host_priv *hpriv = ap->host->private_data;
  138. struct ahci_port_priv *pp = ap->private_data;
  139. void __iomem *port_mmio = ahci_port_base(ap);
  140. u32 fbs;
  141. /*
  142. * In case of PMP multiple IDENTIFY DEVICE commands can be
  143. * issued inside PxCI. So need to poll PxCI for the
  144. * completion of outstanding IDENTIFY DEVICE commands before
  145. * we restart the DMA engine.
  146. */
  147. if (xgene_ahci_poll_reg_val(ap, port_mmio +
  148. PORT_CMD_ISSUE, 0x0, 1, 100))
  149. return -EBUSY;
  150. ahci_stop_engine(ap);
  151. ahci_start_fis_rx(ap);
  152. /*
  153. * Enable the PxFBS.FBS_EN bit as it
  154. * gets cleared due to stopping the engine.
  155. */
  156. if (pp->fbs_supported) {
  157. fbs = readl(port_mmio + PORT_FBS);
  158. writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
  159. fbs = readl(port_mmio + PORT_FBS);
  160. }
  161. hpriv->start_engine(ap);
  162. return 0;
  163. }
  164. /**
  165. * xgene_ahci_qc_issue - Issue commands to the device
  166. * @qc: Command to issue
  167. *
  168. * Due to Hardware errata for IDENTIFY DEVICE command, the controller cannot
  169. * clear the BSY bit after receiving the PIO setup FIS. This results in the dma
  170. * state machine goes into the CMFatalErrorUpdate state and locks up. By
  171. * restarting the dma engine, it removes the controller out of lock up state.
  172. *
  173. * Due to H/W errata, the controller is unable to save the PMP
  174. * field fetched from command header before sending the H2D FIS.
  175. * When the device returns the PMP port field in the D2H FIS, there is
  176. * a mismatch and results in command completion failure. The
  177. * workaround is to write the pmp value to PxFBS.DEV field before issuing
  178. * any command to PMP.
  179. */
  180. static unsigned int xgene_ahci_qc_issue(struct ata_queued_cmd *qc)
  181. {
  182. struct ata_port *ap = qc->ap;
  183. struct ahci_host_priv *hpriv = ap->host->private_data;
  184. struct xgene_ahci_context *ctx = hpriv->plat_data;
  185. int rc = 0;
  186. u32 port_fbs;
  187. void *port_mmio = ahci_port_base(ap);
  188. /*
  189. * Write the pmp value to PxFBS.DEV
  190. * for case of Port Mulitplier.
  191. */
  192. if (ctx->class[ap->port_no] == ATA_DEV_PMP) {
  193. port_fbs = readl(port_mmio + PORT_FBS);
  194. port_fbs &= ~PORT_FBS_DEV_MASK;
  195. port_fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
  196. writel(port_fbs, port_mmio + PORT_FBS);
  197. }
  198. if (unlikely((ctx->last_cmd[ap->port_no] == ATA_CMD_ID_ATA) ||
  199. (ctx->last_cmd[ap->port_no] == ATA_CMD_PACKET) ||
  200. (ctx->last_cmd[ap->port_no] == ATA_CMD_SMART)))
  201. xgene_ahci_restart_engine(ap);
  202. rc = ahci_qc_issue(qc);
  203. /* Save the last command issued */
  204. ctx->last_cmd[ap->port_no] = qc->tf.command;
  205. return rc;
  206. }
  207. static bool xgene_ahci_is_memram_inited(struct xgene_ahci_context *ctx)
  208. {
  209. void __iomem *diagcsr = ctx->csr_diag;
  210. return (readl(diagcsr + CFG_MEM_RAM_SHUTDOWN) == 0 &&
  211. readl(diagcsr + BLOCK_MEM_RDY) == 0xFFFFFFFF);
  212. }
  213. /**
  214. * xgene_ahci_read_id - Read ID data from the specified device
  215. * @dev: device
  216. * @tf: proposed taskfile
  217. * @id: data buffer
  218. *
  219. * This custom read ID function is required due to the fact that the HW
  220. * does not support DEVSLP.
  221. */
  222. static unsigned int xgene_ahci_read_id(struct ata_device *dev,
  223. struct ata_taskfile *tf, u16 *id)
  224. {
  225. u32 err_mask;
  226. err_mask = ata_do_dev_read_id(dev, tf, id);
  227. if (err_mask)
  228. return err_mask;
  229. /*
  230. * Mask reserved area. Word78 spec of Link Power Management
  231. * bit15-8: reserved
  232. * bit7: NCQ autosence
  233. * bit6: Software settings preservation supported
  234. * bit5: reserved
  235. * bit4: In-order sata delivery supported
  236. * bit3: DIPM requests supported
  237. * bit2: DMA Setup FIS Auto-Activate optimization supported
  238. * bit1: DMA Setup FIX non-Zero buffer offsets supported
  239. * bit0: Reserved
  240. *
  241. * Clear reserved bit 8 (DEVSLP bit) as we don't support DEVSLP
  242. */
  243. id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
  244. return 0;
  245. }
  246. static void xgene_ahci_set_phy_cfg(struct xgene_ahci_context *ctx, int channel)
  247. {
  248. void __iomem *mmio = ctx->hpriv->mmio;
  249. u32 val;
  250. dev_dbg(ctx->dev, "port configure mmio 0x%p channel %d\n",
  251. mmio, channel);
  252. val = readl(mmio + PORTCFG);
  253. val = PORTADDR_SET(val, channel == 0 ? 2 : 3);
  254. writel(val, mmio + PORTCFG);
  255. readl(mmio + PORTCFG); /* Force a barrier */
  256. /* Disable fix rate */
  257. writel(0x0001fffe, mmio + PORTPHY1CFG);
  258. readl(mmio + PORTPHY1CFG); /* Force a barrier */
  259. writel(0x28183219, mmio + PORTPHY2CFG);
  260. readl(mmio + PORTPHY2CFG); /* Force a barrier */
  261. writel(0x13081008, mmio + PORTPHY3CFG);
  262. readl(mmio + PORTPHY3CFG); /* Force a barrier */
  263. writel(0x00480815, mmio + PORTPHY4CFG);
  264. readl(mmio + PORTPHY4CFG); /* Force a barrier */
  265. /* Set window negotiation */
  266. val = readl(mmio + PORTPHY5CFG);
  267. val = PORTPHY5CFG_RTCHG_SET(val, 0x300);
  268. writel(val, mmio + PORTPHY5CFG);
  269. readl(mmio + PORTPHY5CFG); /* Force a barrier */
  270. val = readl(mmio + PORTAXICFG);
  271. val = PORTAXICFG_EN_CONTEXT_SET(val, 0x1); /* Enable context mgmt */
  272. val = PORTAXICFG_OUTTRANS_SET(val, 0xe); /* Set outstanding */
  273. writel(val, mmio + PORTAXICFG);
  274. readl(mmio + PORTAXICFG); /* Force a barrier */
  275. /* Set the watermark threshold of the receive FIFO */
  276. val = readl(mmio + PORTRANSCFG);
  277. val = PORTRANSCFG_RXWM_SET(val, 0x30);
  278. writel(val, mmio + PORTRANSCFG);
  279. }
  280. /**
  281. * xgene_ahci_do_hardreset - Issue the actual COMRESET
  282. * @link: link to reset
  283. * @deadline: deadline jiffies for the operation
  284. * @online: Return value to indicate if device online
  285. *
  286. * Due to the limitation of the hardware PHY, a difference set of setting is
  287. * required for each supported disk speed - Gen3 (6.0Gbps), Gen2 (3.0Gbps),
  288. * and Gen1 (1.5Gbps). Otherwise during long IO stress test, the PHY will
  289. * report disparity error and etc. In addition, during COMRESET, there can
  290. * be error reported in the register PORT_SCR_ERR. For SERR_DISPARITY and
  291. * SERR_10B_8B_ERR, the PHY receiver line must be reseted. Also during long
  292. * reboot cycle regression, sometimes the PHY reports link down even if the
  293. * device is present because of speed negotiation failure. so need to retry
  294. * the COMRESET to get the link up. The following algorithm is followed to
  295. * proper configure the hardware PHY during COMRESET:
  296. *
  297. * Alg Part 1:
  298. * 1. Start the PHY at Gen3 speed (default setting)
  299. * 2. Issue the COMRESET
  300. * 3. If no link, go to Alg Part 3
  301. * 4. If link up, determine if the negotiated speed matches the PHY
  302. * configured speed
  303. * 5. If they matched, go to Alg Part 2
  304. * 6. If they do not matched and first time, configure the PHY for the linked
  305. * up disk speed and repeat step 2
  306. * 7. Go to Alg Part 2
  307. *
  308. * Alg Part 2:
  309. * 1. On link up, if there are any SERR_DISPARITY and SERR_10B_8B_ERR error
  310. * reported in the register PORT_SCR_ERR, then reset the PHY receiver line
  311. * 2. Go to Alg Part 4
  312. *
  313. * Alg Part 3:
  314. * 1. Check the PORT_SCR_STAT to see whether device presence detected but PHY
  315. * communication establishment failed and maximum link down attempts are
  316. * less than Max attempts 3 then goto Alg Part 1.
  317. * 2. Go to Alg Part 4.
  318. *
  319. * Alg Part 4:
  320. * 1. Clear any pending from register PORT_SCR_ERR.
  321. *
  322. * NOTE: For the initial version, we will NOT support Gen1/Gen2. In addition
  323. * and until the underlying PHY supports an method to reset the receiver
  324. * line, on detection of SERR_DISPARITY or SERR_10B_8B_ERR errors,
  325. * an warning message will be printed.
  326. */
  327. static int xgene_ahci_do_hardreset(struct ata_link *link,
  328. unsigned long deadline, bool *online)
  329. {
  330. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  331. struct ata_port *ap = link->ap;
  332. struct ahci_host_priv *hpriv = ap->host->private_data;
  333. struct xgene_ahci_context *ctx = hpriv->plat_data;
  334. struct ahci_port_priv *pp = ap->private_data;
  335. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  336. void __iomem *port_mmio = ahci_port_base(ap);
  337. struct ata_taskfile tf;
  338. int link_down_retry = 0;
  339. int rc;
  340. u32 val, sstatus;
  341. do {
  342. /* clear D2H reception area to properly wait for D2H FIS */
  343. ata_tf_init(link->device, &tf);
  344. tf.command = ATA_BUSY;
  345. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  346. rc = sata_link_hardreset(link, timing, deadline, online,
  347. ahci_check_ready);
  348. if (*online) {
  349. val = readl(port_mmio + PORT_SCR_ERR);
  350. if (val & (SERR_DISPARITY | SERR_10B_8B_ERR))
  351. dev_warn(ctx->dev, "link has error\n");
  352. break;
  353. }
  354. sata_scr_read(link, SCR_STATUS, &sstatus);
  355. } while (link_down_retry++ < MAX_LINK_DOWN_RETRY &&
  356. (sstatus & 0xff) == 0x1);
  357. /* clear all errors if any pending */
  358. val = readl(port_mmio + PORT_SCR_ERR);
  359. writel(val, port_mmio + PORT_SCR_ERR);
  360. return rc;
  361. }
  362. static int xgene_ahci_hardreset(struct ata_link *link, unsigned int *class,
  363. unsigned long deadline)
  364. {
  365. struct ata_port *ap = link->ap;
  366. struct ahci_host_priv *hpriv = ap->host->private_data;
  367. void __iomem *port_mmio = ahci_port_base(ap);
  368. bool online;
  369. int rc;
  370. u32 portcmd_saved;
  371. u32 portclb_saved;
  372. u32 portclbhi_saved;
  373. u32 portrxfis_saved;
  374. u32 portrxfishi_saved;
  375. /* As hardreset resets these CSR, save it to restore later */
  376. portcmd_saved = readl(port_mmio + PORT_CMD);
  377. portclb_saved = readl(port_mmio + PORT_LST_ADDR);
  378. portclbhi_saved = readl(port_mmio + PORT_LST_ADDR_HI);
  379. portrxfis_saved = readl(port_mmio + PORT_FIS_ADDR);
  380. portrxfishi_saved = readl(port_mmio + PORT_FIS_ADDR_HI);
  381. ahci_stop_engine(ap);
  382. rc = xgene_ahci_do_hardreset(link, deadline, &online);
  383. /* As controller hardreset clears them, restore them */
  384. writel(portcmd_saved, port_mmio + PORT_CMD);
  385. writel(portclb_saved, port_mmio + PORT_LST_ADDR);
  386. writel(portclbhi_saved, port_mmio + PORT_LST_ADDR_HI);
  387. writel(portrxfis_saved, port_mmio + PORT_FIS_ADDR);
  388. writel(portrxfishi_saved, port_mmio + PORT_FIS_ADDR_HI);
  389. hpriv->start_engine(ap);
  390. if (online)
  391. *class = ahci_dev_classify(ap);
  392. return rc;
  393. }
  394. static void xgene_ahci_host_stop(struct ata_host *host)
  395. {
  396. struct ahci_host_priv *hpriv = host->private_data;
  397. ahci_platform_disable_resources(hpriv);
  398. }
  399. /**
  400. * xgene_ahci_pmp_softreset - Issue the softreset to the drives connected
  401. * to Port Multiplier.
  402. * @link: link to reset
  403. * @class: Return value to indicate class of device
  404. * @deadline: deadline jiffies for the operation
  405. *
  406. * Due to H/W errata, the controller is unable to save the PMP
  407. * field fetched from command header before sending the H2D FIS.
  408. * When the device returns the PMP port field in the D2H FIS, there is
  409. * a mismatch and results in command completion failure. The workaround
  410. * is to write the pmp value to PxFBS.DEV field before issuing any command
  411. * to PMP.
  412. */
  413. static int xgene_ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
  414. unsigned long deadline)
  415. {
  416. int pmp = sata_srst_pmp(link);
  417. struct ata_port *ap = link->ap;
  418. u32 rc;
  419. void *port_mmio = ahci_port_base(ap);
  420. u32 port_fbs;
  421. /*
  422. * Set PxFBS.DEV field with pmp
  423. * value.
  424. */
  425. port_fbs = readl(port_mmio + PORT_FBS);
  426. port_fbs &= ~PORT_FBS_DEV_MASK;
  427. port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
  428. writel(port_fbs, port_mmio + PORT_FBS);
  429. rc = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
  430. return rc;
  431. }
  432. /**
  433. * xgene_ahci_softreset - Issue the softreset to the drive.
  434. * @link: link to reset
  435. * @class: Return value to indicate class of device
  436. * @deadline: deadline jiffies for the operation
  437. *
  438. * Due to H/W errata, the controller is unable to save the PMP
  439. * field fetched from command header before sending the H2D FIS.
  440. * When the device returns the PMP port field in the D2H FIS, there is
  441. * a mismatch and results in command completion failure. The workaround
  442. * is to write the pmp value to PxFBS.DEV field before issuing any command
  443. * to PMP. Here is the algorithm to detect PMP :
  444. *
  445. * 1. Save the PxFBS value
  446. * 2. Program PxFBS.DEV with pmp value send by framework. Framework sends
  447. * 0xF for both PMP/NON-PMP initially
  448. * 3. Issue softreset
  449. * 4. If signature class is PMP goto 6
  450. * 5. restore the original PxFBS and goto 3
  451. * 6. return
  452. */
  453. static int xgene_ahci_softreset(struct ata_link *link, unsigned int *class,
  454. unsigned long deadline)
  455. {
  456. int pmp = sata_srst_pmp(link);
  457. struct ata_port *ap = link->ap;
  458. struct ahci_host_priv *hpriv = ap->host->private_data;
  459. struct xgene_ahci_context *ctx = hpriv->plat_data;
  460. void *port_mmio = ahci_port_base(ap);
  461. u32 port_fbs;
  462. u32 port_fbs_save;
  463. u32 retry = 1;
  464. u32 rc;
  465. port_fbs_save = readl(port_mmio + PORT_FBS);
  466. /*
  467. * Set PxFBS.DEV field with pmp
  468. * value.
  469. */
  470. port_fbs = readl(port_mmio + PORT_FBS);
  471. port_fbs &= ~PORT_FBS_DEV_MASK;
  472. port_fbs |= pmp << PORT_FBS_DEV_OFFSET;
  473. writel(port_fbs, port_mmio + PORT_FBS);
  474. softreset_retry:
  475. rc = ahci_do_softreset(link, class, pmp,
  476. deadline, ahci_check_ready);
  477. ctx->class[ap->port_no] = *class;
  478. if (*class != ATA_DEV_PMP) {
  479. /*
  480. * Retry for normal drives without
  481. * setting PxFBS.DEV field with pmp value.
  482. */
  483. if (retry--) {
  484. writel(port_fbs_save, port_mmio + PORT_FBS);
  485. goto softreset_retry;
  486. }
  487. }
  488. return rc;
  489. }
  490. static struct ata_port_operations xgene_ahci_v1_ops = {
  491. .inherits = &ahci_ops,
  492. .host_stop = xgene_ahci_host_stop,
  493. .hardreset = xgene_ahci_hardreset,
  494. .read_id = xgene_ahci_read_id,
  495. .qc_issue = xgene_ahci_qc_issue,
  496. .softreset = xgene_ahci_softreset,
  497. .pmp_softreset = xgene_ahci_pmp_softreset
  498. };
  499. static const struct ata_port_info xgene_ahci_v1_port_info = {
  500. .flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
  501. .pio_mask = ATA_PIO4,
  502. .udma_mask = ATA_UDMA6,
  503. .port_ops = &xgene_ahci_v1_ops,
  504. };
  505. static struct ata_port_operations xgene_ahci_v2_ops = {
  506. .inherits = &ahci_ops,
  507. .host_stop = xgene_ahci_host_stop,
  508. .hardreset = xgene_ahci_hardreset,
  509. .read_id = xgene_ahci_read_id,
  510. };
  511. static const struct ata_port_info xgene_ahci_v2_port_info = {
  512. .flags = AHCI_FLAG_COMMON | ATA_FLAG_PMP,
  513. .pio_mask = ATA_PIO4,
  514. .udma_mask = ATA_UDMA6,
  515. .port_ops = &xgene_ahci_v2_ops,
  516. };
  517. static int xgene_ahci_hw_init(struct ahci_host_priv *hpriv)
  518. {
  519. struct xgene_ahci_context *ctx = hpriv->plat_data;
  520. int i;
  521. int rc;
  522. u32 val;
  523. /* Remove IP RAM out of shutdown */
  524. rc = xgene_ahci_init_memram(ctx);
  525. if (rc)
  526. return rc;
  527. for (i = 0; i < MAX_AHCI_CHN_PERCTR; i++)
  528. xgene_ahci_set_phy_cfg(ctx, i);
  529. /* AXI disable Mask */
  530. writel(0xffffffff, hpriv->mmio + HOST_IRQ_STAT);
  531. readl(hpriv->mmio + HOST_IRQ_STAT); /* Force a barrier */
  532. writel(0, ctx->csr_core + INTSTATUSMASK);
  533. val = readl(ctx->csr_core + INTSTATUSMASK); /* Force a barrier */
  534. dev_dbg(ctx->dev, "top level interrupt mask 0x%X value 0x%08X\n",
  535. INTSTATUSMASK, val);
  536. writel(0x0, ctx->csr_core + ERRINTSTATUSMASK);
  537. readl(ctx->csr_core + ERRINTSTATUSMASK); /* Force a barrier */
  538. writel(0x0, ctx->csr_axi + INT_SLV_TMOMASK);
  539. readl(ctx->csr_axi + INT_SLV_TMOMASK);
  540. /* Enable AXI Interrupt */
  541. writel(0xffffffff, ctx->csr_core + SLVRDERRATTRIBUTES);
  542. writel(0xffffffff, ctx->csr_core + SLVWRERRATTRIBUTES);
  543. writel(0xffffffff, ctx->csr_core + MSTRDERRATTRIBUTES);
  544. writel(0xffffffff, ctx->csr_core + MSTWRERRATTRIBUTES);
  545. /* Enable coherency */
  546. val = readl(ctx->csr_core + BUSCTLREG);
  547. val &= ~0x00000002; /* Enable write coherency */
  548. val &= ~0x00000001; /* Enable read coherency */
  549. writel(val, ctx->csr_core + BUSCTLREG);
  550. val = readl(ctx->csr_core + IOFMSTRWAUX);
  551. val |= (1 << 3); /* Enable read coherency */
  552. val |= (1 << 9); /* Enable write coherency */
  553. writel(val, ctx->csr_core + IOFMSTRWAUX);
  554. val = readl(ctx->csr_core + IOFMSTRWAUX);
  555. dev_dbg(ctx->dev, "coherency 0x%X value 0x%08X\n",
  556. IOFMSTRWAUX, val);
  557. return rc;
  558. }
  559. static int xgene_ahci_mux_select(struct xgene_ahci_context *ctx)
  560. {
  561. u32 val;
  562. /* Check for optional MUX resource */
  563. if (!ctx->csr_mux)
  564. return 0;
  565. val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
  566. val &= ~CFG_SATA_ENET_SELECT_MASK;
  567. writel(val, ctx->csr_mux + SATA_ENET_CONFIG_REG);
  568. val = readl(ctx->csr_mux + SATA_ENET_CONFIG_REG);
  569. return val & CFG_SATA_ENET_SELECT_MASK ? -1 : 0;
  570. }
  571. static struct scsi_host_template ahci_platform_sht = {
  572. AHCI_SHT(DRV_NAME),
  573. };
  574. #ifdef CONFIG_ACPI
  575. static const struct acpi_device_id xgene_ahci_acpi_match[] = {
  576. { "APMC0D0D", XGENE_AHCI_V1},
  577. { "APMC0D32", XGENE_AHCI_V2},
  578. {},
  579. };
  580. MODULE_DEVICE_TABLE(acpi, xgene_ahci_acpi_match);
  581. #endif
  582. static const struct of_device_id xgene_ahci_of_match[] = {
  583. {.compatible = "apm,xgene-ahci", .data = (void *) XGENE_AHCI_V1},
  584. {.compatible = "apm,xgene-ahci-v2", .data = (void *) XGENE_AHCI_V2},
  585. {},
  586. };
  587. MODULE_DEVICE_TABLE(of, xgene_ahci_of_match);
  588. static int xgene_ahci_probe(struct platform_device *pdev)
  589. {
  590. struct device *dev = &pdev->dev;
  591. struct ahci_host_priv *hpriv;
  592. struct xgene_ahci_context *ctx;
  593. struct resource *res;
  594. const struct of_device_id *of_devid;
  595. enum xgene_ahci_version version = XGENE_AHCI_V1;
  596. const struct ata_port_info *ppi[] = { &xgene_ahci_v1_port_info,
  597. &xgene_ahci_v2_port_info };
  598. int rc;
  599. hpriv = ahci_platform_get_resources(pdev);
  600. if (IS_ERR(hpriv))
  601. return PTR_ERR(hpriv);
  602. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  603. if (!ctx)
  604. return -ENOMEM;
  605. hpriv->plat_data = ctx;
  606. ctx->hpriv = hpriv;
  607. ctx->dev = dev;
  608. /* Retrieve the IP core resource */
  609. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  610. ctx->csr_core = devm_ioremap_resource(dev, res);
  611. if (IS_ERR(ctx->csr_core))
  612. return PTR_ERR(ctx->csr_core);
  613. /* Retrieve the IP diagnostic resource */
  614. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  615. ctx->csr_diag = devm_ioremap_resource(dev, res);
  616. if (IS_ERR(ctx->csr_diag))
  617. return PTR_ERR(ctx->csr_diag);
  618. /* Retrieve the IP AXI resource */
  619. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  620. ctx->csr_axi = devm_ioremap_resource(dev, res);
  621. if (IS_ERR(ctx->csr_axi))
  622. return PTR_ERR(ctx->csr_axi);
  623. /* Retrieve the optional IP mux resource */
  624. res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
  625. if (res) {
  626. void __iomem *csr = devm_ioremap_resource(dev, res);
  627. if (IS_ERR(csr))
  628. return PTR_ERR(csr);
  629. ctx->csr_mux = csr;
  630. }
  631. of_devid = of_match_device(xgene_ahci_of_match, dev);
  632. if (of_devid) {
  633. if (of_devid->data)
  634. version = (enum xgene_ahci_version) of_devid->data;
  635. }
  636. #ifdef CONFIG_ACPI
  637. else {
  638. const struct acpi_device_id *acpi_id;
  639. struct acpi_device_info *info;
  640. acpi_status status;
  641. acpi_id = acpi_match_device(xgene_ahci_acpi_match, &pdev->dev);
  642. if (!acpi_id) {
  643. dev_warn(&pdev->dev, "No node entry in ACPI table. Assume version1\n");
  644. version = XGENE_AHCI_V1;
  645. } else if (acpi_id->driver_data) {
  646. version = (enum xgene_ahci_version) acpi_id->driver_data;
  647. status = acpi_get_object_info(ACPI_HANDLE(&pdev->dev), &info);
  648. if (ACPI_FAILURE(status)) {
  649. dev_warn(&pdev->dev, "%s: Error reading device info. Assume version1\n",
  650. __func__);
  651. version = XGENE_AHCI_V1;
  652. } else if (info->valid & ACPI_VALID_CID) {
  653. version = XGENE_AHCI_V2;
  654. }
  655. }
  656. }
  657. #endif
  658. dev_dbg(dev, "VAddr 0x%p Mmio VAddr 0x%p\n", ctx->csr_core,
  659. hpriv->mmio);
  660. /* Select ATA */
  661. if ((rc = xgene_ahci_mux_select(ctx))) {
  662. dev_err(dev, "SATA mux selection failed error %d\n", rc);
  663. return -ENODEV;
  664. }
  665. if (xgene_ahci_is_memram_inited(ctx)) {
  666. dev_info(dev, "skip clock and PHY initialization\n");
  667. goto skip_clk_phy;
  668. }
  669. /* Due to errata, HW requires full toggle transition */
  670. rc = ahci_platform_enable_clks(hpriv);
  671. if (rc)
  672. goto disable_resources;
  673. ahci_platform_disable_clks(hpriv);
  674. rc = ahci_platform_enable_resources(hpriv);
  675. if (rc)
  676. goto disable_resources;
  677. /* Configure the host controller */
  678. xgene_ahci_hw_init(hpriv);
  679. skip_clk_phy:
  680. switch (version) {
  681. case XGENE_AHCI_V1:
  682. hpriv->flags = AHCI_HFLAG_NO_NCQ;
  683. break;
  684. case XGENE_AHCI_V2:
  685. hpriv->flags |= AHCI_HFLAG_YES_FBS | AHCI_HFLAG_EDGE_IRQ;
  686. break;
  687. default:
  688. break;
  689. }
  690. rc = ahci_platform_init_host(pdev, hpriv, ppi[version - 1],
  691. &ahci_platform_sht);
  692. if (rc)
  693. goto disable_resources;
  694. dev_dbg(dev, "X-Gene SATA host controller initialized\n");
  695. return 0;
  696. disable_resources:
  697. ahci_platform_disable_resources(hpriv);
  698. return rc;
  699. }
  700. static struct platform_driver xgene_ahci_driver = {
  701. .probe = xgene_ahci_probe,
  702. .remove = ata_platform_remove_one,
  703. .driver = {
  704. .name = DRV_NAME,
  705. .of_match_table = xgene_ahci_of_match,
  706. .acpi_match_table = ACPI_PTR(xgene_ahci_acpi_match),
  707. },
  708. };
  709. module_platform_driver(xgene_ahci_driver);
  710. MODULE_DESCRIPTION("APM X-Gene AHCI SATA driver");
  711. MODULE_AUTHOR("Loc Ho <lho@apm.com>");
  712. MODULE_LICENSE("GPL");
  713. MODULE_VERSION("0.4");