sata_gemini.c 11 KB

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