sata_highbank.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Calxeda Highbank AHCI SATA platform driver
  3. * Copyright 2012 Calxeda, Inc.
  4. *
  5. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/gfp.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/err.h>
  25. #include <linux/io.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/device.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_address.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/libata.h>
  32. #include <linux/ahci_platform.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/export.h>
  36. #include "ahci.h"
  37. #define CPHY_MAP(dev, addr) ((((dev) & 0x1f) << 7) | (((addr) >> 9) & 0x7f))
  38. #define CPHY_ADDR(addr) (((addr) & 0x1ff) << 2)
  39. #define SERDES_CR_CTL 0x80a0
  40. #define SERDES_CR_ADDR 0x80a1
  41. #define SERDES_CR_DATA 0x80a2
  42. #define CR_BUSY 0x0001
  43. #define CR_START 0x0001
  44. #define CR_WR_RDN 0x0002
  45. #define CPHY_RX_INPUT_STS 0x2002
  46. #define CPHY_SATA_OVERRIDE 0x4000
  47. #define CPHY_OVERRIDE 0x2005
  48. #define SPHY_LANE 0x100
  49. #define SPHY_HALF_RATE 0x0001
  50. #define CPHY_SATA_DPLL_MODE 0x0700
  51. #define CPHY_SATA_DPLL_SHIFT 8
  52. #define CPHY_SATA_DPLL_RESET (1 << 11)
  53. #define CPHY_PHY_COUNT 6
  54. #define CPHY_LANE_COUNT 4
  55. #define CPHY_PORT_COUNT (CPHY_PHY_COUNT * CPHY_LANE_COUNT)
  56. static DEFINE_SPINLOCK(cphy_lock);
  57. /* Each of the 6 phys can have up to 4 sata ports attached to i. Map 0-based
  58. * sata ports to their phys and then to their lanes within the phys
  59. */
  60. struct phy_lane_info {
  61. void __iomem *phy_base;
  62. u8 lane_mapping;
  63. u8 phy_devs;
  64. };
  65. static struct phy_lane_info port_data[CPHY_PORT_COUNT];
  66. static u32 __combo_phy_reg_read(u8 sata_port, u32 addr)
  67. {
  68. u32 data;
  69. u8 dev = port_data[sata_port].phy_devs;
  70. spin_lock(&cphy_lock);
  71. writel(CPHY_MAP(dev, addr), port_data[sata_port].phy_base + 0x800);
  72. data = readl(port_data[sata_port].phy_base + CPHY_ADDR(addr));
  73. spin_unlock(&cphy_lock);
  74. return data;
  75. }
  76. static void __combo_phy_reg_write(u8 sata_port, u32 addr, u32 data)
  77. {
  78. u8 dev = port_data[sata_port].phy_devs;
  79. spin_lock(&cphy_lock);
  80. writel(CPHY_MAP(dev, addr), port_data[sata_port].phy_base + 0x800);
  81. writel(data, port_data[sata_port].phy_base + CPHY_ADDR(addr));
  82. spin_unlock(&cphy_lock);
  83. }
  84. static void combo_phy_wait_for_ready(u8 sata_port)
  85. {
  86. while (__combo_phy_reg_read(sata_port, SERDES_CR_CTL) & CR_BUSY)
  87. udelay(5);
  88. }
  89. static u32 combo_phy_read(u8 sata_port, u32 addr)
  90. {
  91. combo_phy_wait_for_ready(sata_port);
  92. __combo_phy_reg_write(sata_port, SERDES_CR_ADDR, addr);
  93. __combo_phy_reg_write(sata_port, SERDES_CR_CTL, CR_START);
  94. combo_phy_wait_for_ready(sata_port);
  95. return __combo_phy_reg_read(sata_port, SERDES_CR_DATA);
  96. }
  97. static void combo_phy_write(u8 sata_port, u32 addr, u32 data)
  98. {
  99. combo_phy_wait_for_ready(sata_port);
  100. __combo_phy_reg_write(sata_port, SERDES_CR_ADDR, addr);
  101. __combo_phy_reg_write(sata_port, SERDES_CR_DATA, data);
  102. __combo_phy_reg_write(sata_port, SERDES_CR_CTL, CR_WR_RDN | CR_START);
  103. }
  104. static void highbank_cphy_disable_overrides(u8 sata_port)
  105. {
  106. u8 lane = port_data[sata_port].lane_mapping;
  107. u32 tmp;
  108. if (unlikely(port_data[sata_port].phy_base == NULL))
  109. return;
  110. tmp = combo_phy_read(sata_port, CPHY_RX_INPUT_STS + lane * SPHY_LANE);
  111. tmp &= ~CPHY_SATA_OVERRIDE;
  112. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  113. }
  114. static void cphy_override_rx_mode(u8 sata_port, u32 val)
  115. {
  116. u8 lane = port_data[sata_port].lane_mapping;
  117. u32 tmp;
  118. tmp = combo_phy_read(sata_port, CPHY_RX_INPUT_STS + lane * SPHY_LANE);
  119. tmp &= ~CPHY_SATA_OVERRIDE;
  120. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  121. tmp |= CPHY_SATA_OVERRIDE;
  122. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  123. tmp &= ~CPHY_SATA_DPLL_MODE;
  124. tmp |= val << CPHY_SATA_DPLL_SHIFT;
  125. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  126. tmp |= CPHY_SATA_DPLL_RESET;
  127. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  128. tmp &= ~CPHY_SATA_DPLL_RESET;
  129. combo_phy_write(sata_port, CPHY_OVERRIDE + lane * SPHY_LANE, tmp);
  130. msleep(15);
  131. }
  132. static void highbank_cphy_override_lane(u8 sata_port)
  133. {
  134. u8 lane = port_data[sata_port].lane_mapping;
  135. u32 tmp, k = 0;
  136. if (unlikely(port_data[sata_port].phy_base == NULL))
  137. return;
  138. do {
  139. tmp = combo_phy_read(sata_port, CPHY_RX_INPUT_STS +
  140. lane * SPHY_LANE);
  141. } while ((tmp & SPHY_HALF_RATE) && (k++ < 1000));
  142. cphy_override_rx_mode(sata_port, 3);
  143. }
  144. static int highbank_initialize_phys(struct device *dev, void __iomem *addr)
  145. {
  146. struct device_node *sata_node = dev->of_node;
  147. int phy_count = 0, phy, port = 0;
  148. void __iomem *cphy_base[CPHY_PHY_COUNT];
  149. struct device_node *phy_nodes[CPHY_PHY_COUNT];
  150. memset(port_data, 0, sizeof(struct phy_lane_info) * CPHY_PORT_COUNT);
  151. memset(phy_nodes, 0, sizeof(struct device_node*) * CPHY_PHY_COUNT);
  152. do {
  153. u32 tmp;
  154. struct of_phandle_args phy_data;
  155. if (of_parse_phandle_with_args(sata_node,
  156. "calxeda,port-phys", "#phy-cells",
  157. port, &phy_data))
  158. break;
  159. for (phy = 0; phy < phy_count; phy++) {
  160. if (phy_nodes[phy] == phy_data.np)
  161. break;
  162. }
  163. if (phy_nodes[phy] == NULL) {
  164. phy_nodes[phy] = phy_data.np;
  165. cphy_base[phy] = of_iomap(phy_nodes[phy], 0);
  166. if (cphy_base[phy] == NULL) {
  167. return 0;
  168. }
  169. phy_count += 1;
  170. }
  171. port_data[port].lane_mapping = phy_data.args[0];
  172. of_property_read_u32(phy_nodes[phy], "phydev", &tmp);
  173. port_data[port].phy_devs = tmp;
  174. port_data[port].phy_base = cphy_base[phy];
  175. of_node_put(phy_data.np);
  176. port += 1;
  177. } while (port < CPHY_PORT_COUNT);
  178. return 0;
  179. }
  180. static int ahci_highbank_hardreset(struct ata_link *link, unsigned int *class,
  181. unsigned long deadline)
  182. {
  183. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  184. struct ata_port *ap = link->ap;
  185. struct ahci_port_priv *pp = ap->private_data;
  186. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  187. struct ata_taskfile tf;
  188. bool online;
  189. u32 sstatus;
  190. int rc;
  191. int retry = 10;
  192. ahci_stop_engine(ap);
  193. /* clear D2H reception area to properly wait for D2H FIS */
  194. ata_tf_init(link->device, &tf);
  195. tf.command = ATA_BUSY;
  196. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  197. do {
  198. highbank_cphy_disable_overrides(link->ap->port_no);
  199. rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
  200. highbank_cphy_override_lane(link->ap->port_no);
  201. /* If the status is 1, we are connected, but the link did not
  202. * come up. So retry resetting the link again.
  203. */
  204. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  205. break;
  206. if (!(sstatus & 0x3))
  207. break;
  208. } while (!online && retry--);
  209. ahci_start_engine(ap);
  210. if (online)
  211. *class = ahci_dev_classify(ap);
  212. return rc;
  213. }
  214. static struct ata_port_operations ahci_highbank_ops = {
  215. .inherits = &ahci_ops,
  216. .hardreset = ahci_highbank_hardreset,
  217. };
  218. static const struct ata_port_info ahci_highbank_port_info = {
  219. .flags = AHCI_FLAG_COMMON,
  220. .pio_mask = ATA_PIO4,
  221. .udma_mask = ATA_UDMA6,
  222. .port_ops = &ahci_highbank_ops,
  223. };
  224. static struct scsi_host_template ahci_highbank_platform_sht = {
  225. AHCI_SHT("highbank-ahci"),
  226. };
  227. static const struct of_device_id ahci_of_match[] = {
  228. { .compatible = "calxeda,hb-ahci" },
  229. {},
  230. };
  231. MODULE_DEVICE_TABLE(of, ahci_of_match);
  232. static int ahci_highbank_probe(struct platform_device *pdev)
  233. {
  234. struct device *dev = &pdev->dev;
  235. struct ahci_host_priv *hpriv;
  236. struct ata_host *host;
  237. struct resource *mem;
  238. int irq;
  239. int n_ports;
  240. int i;
  241. int rc;
  242. struct ata_port_info pi = ahci_highbank_port_info;
  243. const struct ata_port_info *ppi[] = { &pi, NULL };
  244. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. if (!mem) {
  246. dev_err(dev, "no mmio space\n");
  247. return -EINVAL;
  248. }
  249. irq = platform_get_irq(pdev, 0);
  250. if (irq <= 0) {
  251. dev_err(dev, "no irq\n");
  252. return -EINVAL;
  253. }
  254. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  255. if (!hpriv) {
  256. dev_err(dev, "can't alloc ahci_host_priv\n");
  257. return -ENOMEM;
  258. }
  259. hpriv->flags |= (unsigned long)pi.private_data;
  260. hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
  261. if (!hpriv->mmio) {
  262. dev_err(dev, "can't map %pR\n", mem);
  263. return -ENOMEM;
  264. }
  265. rc = highbank_initialize_phys(dev, hpriv->mmio);
  266. if (rc)
  267. return rc;
  268. ahci_save_initial_config(dev, hpriv, 0, 0);
  269. /* prepare host */
  270. if (hpriv->cap & HOST_CAP_NCQ)
  271. pi.flags |= ATA_FLAG_NCQ;
  272. if (hpriv->cap & HOST_CAP_PMP)
  273. pi.flags |= ATA_FLAG_PMP;
  274. ahci_set_em_messages(hpriv, &pi);
  275. /* CAP.NP sometimes indicate the index of the last enabled
  276. * port, at other times, that of the last possible port, so
  277. * determining the maximum port number requires looking at
  278. * both CAP.NP and port_map.
  279. */
  280. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  281. host = ata_host_alloc_pinfo(dev, ppi, n_ports);
  282. if (!host) {
  283. rc = -ENOMEM;
  284. goto err0;
  285. }
  286. host->private_data = hpriv;
  287. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  288. host->flags |= ATA_HOST_PARALLEL_SCAN;
  289. if (pi.flags & ATA_FLAG_EM)
  290. ahci_reset_em(host);
  291. for (i = 0; i < host->n_ports; i++) {
  292. struct ata_port *ap = host->ports[i];
  293. ata_port_desc(ap, "mmio %pR", mem);
  294. ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
  295. /* set enclosure management message type */
  296. if (ap->flags & ATA_FLAG_EM)
  297. ap->em_message_type = hpriv->em_msg_type;
  298. /* disabled/not-implemented port */
  299. if (!(hpriv->port_map & (1 << i)))
  300. ap->ops = &ata_dummy_port_ops;
  301. }
  302. rc = ahci_reset_controller(host);
  303. if (rc)
  304. goto err0;
  305. ahci_init_controller(host);
  306. ahci_print_info(host, "platform");
  307. rc = ata_host_activate(host, irq, ahci_interrupt, 0,
  308. &ahci_highbank_platform_sht);
  309. if (rc)
  310. goto err0;
  311. return 0;
  312. err0:
  313. return rc;
  314. }
  315. #ifdef CONFIG_PM_SLEEP
  316. static int ahci_highbank_suspend(struct device *dev)
  317. {
  318. struct ata_host *host = dev_get_drvdata(dev);
  319. struct ahci_host_priv *hpriv = host->private_data;
  320. void __iomem *mmio = hpriv->mmio;
  321. u32 ctl;
  322. int rc;
  323. if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  324. dev_err(dev, "firmware update required for suspend/resume\n");
  325. return -EIO;
  326. }
  327. /*
  328. * AHCI spec rev1.1 section 8.3.3:
  329. * Software must disable interrupts prior to requesting a
  330. * transition of the HBA to D3 state.
  331. */
  332. ctl = readl(mmio + HOST_CTL);
  333. ctl &= ~HOST_IRQ_EN;
  334. writel(ctl, mmio + HOST_CTL);
  335. readl(mmio + HOST_CTL); /* flush */
  336. rc = ata_host_suspend(host, PMSG_SUSPEND);
  337. if (rc)
  338. return rc;
  339. return 0;
  340. }
  341. static int ahci_highbank_resume(struct device *dev)
  342. {
  343. struct ata_host *host = dev_get_drvdata(dev);
  344. int rc;
  345. if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
  346. rc = ahci_reset_controller(host);
  347. if (rc)
  348. return rc;
  349. ahci_init_controller(host);
  350. }
  351. ata_host_resume(host);
  352. return 0;
  353. }
  354. #endif
  355. SIMPLE_DEV_PM_OPS(ahci_highbank_pm_ops,
  356. ahci_highbank_suspend, ahci_highbank_resume);
  357. static struct platform_driver ahci_highbank_driver = {
  358. .remove = ata_platform_remove_one,
  359. .driver = {
  360. .name = "highbank-ahci",
  361. .owner = THIS_MODULE,
  362. .of_match_table = ahci_of_match,
  363. .pm = &ahci_highbank_pm_ops,
  364. },
  365. .probe = ahci_highbank_probe,
  366. };
  367. module_platform_driver(ahci_highbank_driver);
  368. MODULE_DESCRIPTION("Calxeda Highbank AHCI SATA platform driver");
  369. MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@calxeda.com>");
  370. MODULE_LICENSE("GPL");
  371. MODULE_ALIAS("sata:highbank");