1
0

sata_gemini.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cortina Systems Gemini SATA bridge add-on to Faraday FTIDE010
  4. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/bitops.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/regmap.h>
  12. #include <linux/delay.h>
  13. #include <linux/of.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include "sata_gemini.h"
  18. #define DRV_NAME "gemini_sata_bridge"
  19. /**
  20. * struct sata_gemini - a state container for a Gemini SATA bridge
  21. * @dev: the containing device
  22. * @base: remapped I/O memory base
  23. * @muxmode: the current muxing mode
  24. * @ide_pins: if the device is using the plain IDE interface pins
  25. * @sata_bridge: if the device enables the SATA bridge
  26. * @sata0_pclk: SATA0 PCLK handler
  27. * @sata1_pclk: SATA1 PCLK handler
  28. */
  29. struct sata_gemini {
  30. struct device *dev;
  31. void __iomem *base;
  32. enum gemini_muxmode muxmode;
  33. bool ide_pins;
  34. bool sata_bridge;
  35. struct clk *sata0_pclk;
  36. struct clk *sata1_pclk;
  37. };
  38. /* Miscellaneous Control Register */
  39. #define GEMINI_GLOBAL_MISC_CTRL 0x30
  40. /*
  41. * Values of IDE IOMUX bits in the misc control register
  42. *
  43. * Bits 26:24 are "IDE IO Select", which decides what SATA
  44. * adapters are connected to which of the two IDE/ATA
  45. * controllers in the Gemini. We can connect the two IDE blocks
  46. * to one SATA adapter each, both acting as master, or one IDE
  47. * blocks to two SATA adapters so the IDE block can act in a
  48. * master/slave configuration.
  49. *
  50. * We also bring out different blocks on the actual IDE
  51. * pins (not SATA pins) if (and only if) these are muxed in.
  52. *
  53. * 111-100 - Reserved
  54. * Mode 0: 000 - ata0 master <-> sata0
  55. * ata1 master <-> sata1
  56. * ata0 slave interface brought out on IDE pads
  57. * Mode 1: 001 - ata0 master <-> sata0
  58. * ata1 master <-> sata1
  59. * ata1 slave interface brought out on IDE pads
  60. * Mode 2: 010 - ata1 master <-> sata1
  61. * ata1 slave <-> sata0
  62. * ata0 master and slave interfaces brought out
  63. * on IDE pads
  64. * Mode 3: 011 - ata0 master <-> sata0
  65. * ata1 slave <-> sata1
  66. * ata1 master and slave interfaces brought out
  67. * on IDE pads
  68. */
  69. #define GEMINI_IDE_IOMUX_MASK (7 << 24)
  70. #define GEMINI_IDE_IOMUX_MODE0 (0 << 24)
  71. #define GEMINI_IDE_IOMUX_MODE1 (1 << 24)
  72. #define GEMINI_IDE_IOMUX_MODE2 (2 << 24)
  73. #define GEMINI_IDE_IOMUX_MODE3 (3 << 24)
  74. #define GEMINI_IDE_IOMUX_SHIFT (24)
  75. /*
  76. * Registers directly controlling the PATA<->SATA adapters
  77. */
  78. #define GEMINI_SATA_ID 0x00
  79. #define GEMINI_SATA_PHY_ID 0x04
  80. #define GEMINI_SATA0_STATUS 0x08
  81. #define GEMINI_SATA1_STATUS 0x0c
  82. #define GEMINI_SATA0_CTRL 0x18
  83. #define GEMINI_SATA1_CTRL 0x1c
  84. #define GEMINI_SATA_STATUS_BIST_DONE BIT(5)
  85. #define GEMINI_SATA_STATUS_BIST_OK BIT(4)
  86. #define GEMINI_SATA_STATUS_PHY_READY BIT(0)
  87. #define GEMINI_SATA_CTRL_PHY_BIST_EN BIT(14)
  88. #define GEMINI_SATA_CTRL_PHY_FORCE_IDLE BIT(13)
  89. #define GEMINI_SATA_CTRL_PHY_FORCE_READY BIT(12)
  90. #define GEMINI_SATA_CTRL_PHY_AFE_LOOP_EN BIT(10)
  91. #define GEMINI_SATA_CTRL_PHY_DIG_LOOP_EN BIT(9)
  92. #define GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN BIT(4)
  93. #define GEMINI_SATA_CTRL_ATAPI_EN BIT(3)
  94. #define GEMINI_SATA_CTRL_BUS_WITH_20 BIT(2)
  95. #define GEMINI_SATA_CTRL_SLAVE_EN BIT(1)
  96. #define GEMINI_SATA_CTRL_EN BIT(0)
  97. /*
  98. * There is only ever one instance of this bridge on a system,
  99. * so create a singleton so that the FTIDE010 instances can grab
  100. * a reference to it.
  101. */
  102. static struct sata_gemini *sg_singleton;
  103. struct sata_gemini *gemini_sata_bridge_get(void)
  104. {
  105. if (sg_singleton)
  106. return sg_singleton;
  107. return ERR_PTR(-EPROBE_DEFER);
  108. }
  109. EXPORT_SYMBOL(gemini_sata_bridge_get);
  110. bool gemini_sata_bridge_enabled(struct sata_gemini *sg, bool is_ata1)
  111. {
  112. if (!sg->sata_bridge)
  113. return false;
  114. /*
  115. * In muxmode 2 and 3 one of the ATA controllers is
  116. * actually not connected to any SATA bridge.
  117. */
  118. if ((sg->muxmode == GEMINI_MUXMODE_2) &&
  119. !is_ata1)
  120. return false;
  121. if ((sg->muxmode == GEMINI_MUXMODE_3) &&
  122. is_ata1)
  123. return false;
  124. return true;
  125. }
  126. EXPORT_SYMBOL(gemini_sata_bridge_enabled);
  127. enum gemini_muxmode gemini_sata_get_muxmode(struct sata_gemini *sg)
  128. {
  129. return sg->muxmode;
  130. }
  131. EXPORT_SYMBOL(gemini_sata_get_muxmode);
  132. static int gemini_sata_setup_bridge(struct sata_gemini *sg,
  133. unsigned int bridge)
  134. {
  135. unsigned long timeout = jiffies + (HZ * 1);
  136. bool bridge_online;
  137. u32 val;
  138. if (bridge == 0) {
  139. val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
  140. /* SATA0 slave mode is only used in muxmode 2 */
  141. if (sg->muxmode == GEMINI_MUXMODE_2)
  142. val |= GEMINI_SATA_CTRL_SLAVE_EN;
  143. writel(val, sg->base + GEMINI_SATA0_CTRL);
  144. } else {
  145. val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
  146. /* SATA1 slave mode is only used in muxmode 3 */
  147. if (sg->muxmode == GEMINI_MUXMODE_3)
  148. val |= GEMINI_SATA_CTRL_SLAVE_EN;
  149. writel(val, sg->base + GEMINI_SATA1_CTRL);
  150. }
  151. /* Vendor code waits 10 ms here */
  152. msleep(10);
  153. /* Wait for PHY to become ready */
  154. do {
  155. msleep(100);
  156. if (bridge == 0)
  157. val = readl(sg->base + GEMINI_SATA0_STATUS);
  158. else
  159. val = readl(sg->base + GEMINI_SATA1_STATUS);
  160. if (val & GEMINI_SATA_STATUS_PHY_READY)
  161. break;
  162. } while (time_before(jiffies, timeout));
  163. bridge_online = !!(val & GEMINI_SATA_STATUS_PHY_READY);
  164. dev_info(sg->dev, "SATA%d PHY %s\n", bridge,
  165. bridge_online ? "ready" : "not ready");
  166. return bridge_online ? 0: -ENODEV;
  167. }
  168. int gemini_sata_start_bridge(struct sata_gemini *sg, unsigned int bridge)
  169. {
  170. struct clk *pclk;
  171. int ret;
  172. if (bridge == 0)
  173. pclk = sg->sata0_pclk;
  174. else
  175. pclk = sg->sata1_pclk;
  176. ret = clk_enable(pclk);
  177. if (ret)
  178. return ret;
  179. msleep(10);
  180. /* Do not keep clocking a bridge that is not online */
  181. ret = gemini_sata_setup_bridge(sg, bridge);
  182. if (ret)
  183. clk_disable(pclk);
  184. return ret;
  185. }
  186. EXPORT_SYMBOL(gemini_sata_start_bridge);
  187. void gemini_sata_stop_bridge(struct sata_gemini *sg, unsigned int bridge)
  188. {
  189. if (bridge == 0)
  190. clk_disable(sg->sata0_pclk);
  191. else if (bridge == 1)
  192. clk_disable(sg->sata1_pclk);
  193. }
  194. EXPORT_SYMBOL(gemini_sata_stop_bridge);
  195. static int gemini_sata_bridge_init(struct sata_gemini *sg)
  196. {
  197. struct device *dev = sg->dev;
  198. u32 sata_id, sata_phy_id;
  199. int ret;
  200. sg->sata0_pclk = devm_clk_get(dev, "SATA0_PCLK");
  201. if (IS_ERR(sg->sata0_pclk)) {
  202. dev_err(dev, "no SATA0 PCLK");
  203. return -ENODEV;
  204. }
  205. sg->sata1_pclk = devm_clk_get(dev, "SATA1_PCLK");
  206. if (IS_ERR(sg->sata1_pclk)) {
  207. dev_err(dev, "no SATA1 PCLK");
  208. return -ENODEV;
  209. }
  210. ret = clk_prepare_enable(sg->sata0_pclk);
  211. if (ret) {
  212. dev_err(dev, "failed to enable SATA0 PCLK\n");
  213. return ret;
  214. }
  215. ret = clk_prepare_enable(sg->sata1_pclk);
  216. if (ret) {
  217. dev_err(dev, "failed to enable SATA1 PCLK\n");
  218. clk_disable_unprepare(sg->sata0_pclk);
  219. return ret;
  220. }
  221. sata_id = readl(sg->base + GEMINI_SATA_ID);
  222. sata_phy_id = readl(sg->base + GEMINI_SATA_PHY_ID);
  223. sg->sata_bridge = true;
  224. clk_disable(sg->sata0_pclk);
  225. clk_disable(sg->sata1_pclk);
  226. dev_info(dev, "SATA ID %08x, PHY ID: %08x\n", sata_id, sata_phy_id);
  227. return 0;
  228. }
  229. static int gemini_setup_ide_pins(struct device *dev)
  230. {
  231. struct pinctrl *p;
  232. struct pinctrl_state *ide_state;
  233. int ret;
  234. p = devm_pinctrl_get(dev);
  235. if (IS_ERR(p))
  236. return PTR_ERR(p);
  237. ide_state = pinctrl_lookup_state(p, "ide");
  238. if (IS_ERR(ide_state))
  239. return PTR_ERR(ide_state);
  240. ret = pinctrl_select_state(p, ide_state);
  241. if (ret) {
  242. dev_err(dev, "could not select IDE state\n");
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. static int gemini_sata_probe(struct platform_device *pdev)
  248. {
  249. struct device *dev = &pdev->dev;
  250. struct device_node *np = dev->of_node;
  251. struct sata_gemini *sg;
  252. struct regmap *map;
  253. enum gemini_muxmode muxmode;
  254. u32 gmode;
  255. u32 gmask;
  256. int ret;
  257. sg = devm_kzalloc(dev, sizeof(*sg), GFP_KERNEL);
  258. if (!sg)
  259. return -ENOMEM;
  260. sg->dev = dev;
  261. sg->base = devm_platform_ioremap_resource(pdev, 0);
  262. if (IS_ERR(sg->base))
  263. return PTR_ERR(sg->base);
  264. map = syscon_regmap_lookup_by_phandle(np, "syscon");
  265. if (IS_ERR(map)) {
  266. dev_err(dev, "no global syscon\n");
  267. return PTR_ERR(map);
  268. }
  269. /* Set up the SATA bridge if need be */
  270. if (of_property_read_bool(np, "cortina,gemini-enable-sata-bridge")) {
  271. ret = gemini_sata_bridge_init(sg);
  272. if (ret)
  273. return ret;
  274. }
  275. if (of_property_read_bool(np, "cortina,gemini-enable-ide-pins"))
  276. sg->ide_pins = true;
  277. if (!sg->sata_bridge && !sg->ide_pins) {
  278. dev_err(dev, "neither SATA bridge or IDE output enabled\n");
  279. ret = -EINVAL;
  280. goto out_unprep_clk;
  281. }
  282. ret = of_property_read_u32(np, "cortina,gemini-ata-muxmode", &muxmode);
  283. if (ret) {
  284. dev_err(dev, "could not parse ATA muxmode\n");
  285. goto out_unprep_clk;
  286. }
  287. if (muxmode > GEMINI_MUXMODE_3) {
  288. dev_err(dev, "illegal muxmode %d\n", muxmode);
  289. ret = -EINVAL;
  290. goto out_unprep_clk;
  291. }
  292. sg->muxmode = muxmode;
  293. gmask = GEMINI_IDE_IOMUX_MASK;
  294. gmode = (muxmode << GEMINI_IDE_IOMUX_SHIFT);
  295. ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, gmask, gmode);
  296. if (ret) {
  297. dev_err(dev, "unable to set up IDE muxing\n");
  298. ret = -ENODEV;
  299. goto out_unprep_clk;
  300. }
  301. /*
  302. * Route out the IDE pins if desired.
  303. * This is done by looking up a special pin control state called
  304. * "ide" that will route out the IDE pins.
  305. */
  306. if (sg->ide_pins) {
  307. ret = gemini_setup_ide_pins(dev);
  308. if (ret)
  309. return ret;
  310. }
  311. dev_info(dev, "set up the Gemini IDE/SATA nexus\n");
  312. platform_set_drvdata(pdev, sg);
  313. sg_singleton = sg;
  314. return 0;
  315. out_unprep_clk:
  316. if (sg->sata_bridge) {
  317. clk_unprepare(sg->sata1_pclk);
  318. clk_unprepare(sg->sata0_pclk);
  319. }
  320. return ret;
  321. }
  322. static void gemini_sata_remove(struct platform_device *pdev)
  323. {
  324. struct sata_gemini *sg = platform_get_drvdata(pdev);
  325. if (sg->sata_bridge) {
  326. clk_unprepare(sg->sata1_pclk);
  327. clk_unprepare(sg->sata0_pclk);
  328. }
  329. sg_singleton = NULL;
  330. }
  331. static const struct of_device_id gemini_sata_of_match[] = {
  332. { .compatible = "cortina,gemini-sata-bridge", },
  333. { /* sentinel */ }
  334. };
  335. MODULE_DEVICE_TABLE(of, gemini_sata_of_match);
  336. static struct platform_driver gemini_sata_driver = {
  337. .driver = {
  338. .name = DRV_NAME,
  339. .of_match_table = gemini_sata_of_match,
  340. },
  341. .probe = gemini_sata_probe,
  342. .remove = gemini_sata_remove,
  343. };
  344. module_platform_driver(gemini_sata_driver);
  345. MODULE_DESCRIPTION("low level driver for Cortina Systems Gemini SATA bridge");
  346. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  347. MODULE_LICENSE("GPL");
  348. MODULE_ALIAS("platform:" DRV_NAME);