ufshcd-pltfrm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Universal Flash Storage Host controller Platform bus based glue driver
  4. * Copyright (C) 2011-2013 Samsung India Software Operations
  5. *
  6. * Authors:
  7. * Santosh Yaraganavi <santosh.sy@samsung.com>
  8. * Vinayak Holikatti <h.vinayak@samsung.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_opp.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/of.h>
  16. #include <ufs/ufshcd.h>
  17. #include "ufshcd-pltfrm.h"
  18. #include <ufs/unipro.h>
  19. #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
  20. static int ufshcd_parse_clock_info(struct ufs_hba *hba)
  21. {
  22. int ret = 0;
  23. int cnt;
  24. int i;
  25. struct device *dev = hba->dev;
  26. struct device_node *np = dev->of_node;
  27. const char *name;
  28. u32 *clkfreq = NULL;
  29. struct ufs_clk_info *clki;
  30. ssize_t sz = 0;
  31. if (!np)
  32. goto out;
  33. cnt = of_property_count_strings(np, "clock-names");
  34. if (!cnt || (cnt == -EINVAL)) {
  35. dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
  36. __func__);
  37. } else if (cnt < 0) {
  38. dev_err(dev, "%s: count clock strings failed, err %d\n",
  39. __func__, cnt);
  40. ret = cnt;
  41. }
  42. if (cnt <= 0)
  43. goto out;
  44. sz = of_property_count_u32_elems(np, "freq-table-hz");
  45. if (sz <= 0) {
  46. dev_info(dev, "freq-table-hz property not specified\n");
  47. goto out;
  48. }
  49. if (sz != 2 * cnt) {
  50. dev_err(dev, "%s len mismatch\n", "freq-table-hz");
  51. ret = -EINVAL;
  52. goto out;
  53. }
  54. clkfreq = devm_kcalloc(dev, sz, sizeof(*clkfreq),
  55. GFP_KERNEL);
  56. if (!clkfreq) {
  57. ret = -ENOMEM;
  58. goto out;
  59. }
  60. ret = of_property_read_u32_array(np, "freq-table-hz",
  61. clkfreq, sz);
  62. if (ret && (ret != -EINVAL)) {
  63. dev_err(dev, "%s: error reading array %d\n",
  64. "freq-table-hz", ret);
  65. return ret;
  66. }
  67. for (i = 0; i < sz; i += 2) {
  68. ret = of_property_read_string_index(np, "clock-names", i/2,
  69. &name);
  70. if (ret)
  71. goto out;
  72. clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
  73. if (!clki) {
  74. ret = -ENOMEM;
  75. goto out;
  76. }
  77. clki->min_freq = clkfreq[i];
  78. clki->max_freq = clkfreq[i+1];
  79. clki->name = devm_kstrdup(dev, name, GFP_KERNEL);
  80. if (!clki->name) {
  81. ret = -ENOMEM;
  82. goto out;
  83. }
  84. if (!strcmp(name, "ref_clk"))
  85. clki->keep_link_active = true;
  86. dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
  87. clki->min_freq, clki->max_freq, clki->name);
  88. list_add_tail(&clki->list, &hba->clk_list_head);
  89. }
  90. out:
  91. return ret;
  92. }
  93. static bool phandle_exists(const struct device_node *np,
  94. const char *phandle_name, int index)
  95. {
  96. struct device_node *parse_np = of_parse_phandle(np, phandle_name, index);
  97. if (parse_np)
  98. of_node_put(parse_np);
  99. return parse_np != NULL;
  100. }
  101. #define MAX_PROP_SIZE 32
  102. int ufshcd_populate_vreg(struct device *dev, const char *name,
  103. struct ufs_vreg **out_vreg, bool skip_current)
  104. {
  105. char prop_name[MAX_PROP_SIZE];
  106. struct ufs_vreg *vreg = NULL;
  107. struct device_node *np = dev->of_node;
  108. if (!np) {
  109. dev_err(dev, "%s: non DT initialization\n", __func__);
  110. goto out;
  111. }
  112. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
  113. if (!phandle_exists(np, prop_name, 0)) {
  114. dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
  115. __func__, prop_name);
  116. goto out;
  117. }
  118. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  119. if (!vreg)
  120. return -ENOMEM;
  121. vreg->name = devm_kstrdup(dev, name, GFP_KERNEL);
  122. if (!vreg->name)
  123. return -ENOMEM;
  124. if (skip_current) {
  125. vreg->max_uA = 0;
  126. goto out;
  127. }
  128. snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
  129. if (of_property_read_u32(np, prop_name, &vreg->max_uA)) {
  130. dev_info(dev, "%s: unable to find %s\n", __func__, prop_name);
  131. vreg->max_uA = 0;
  132. }
  133. out:
  134. *out_vreg = vreg;
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(ufshcd_populate_vreg);
  138. /**
  139. * ufshcd_parse_regulator_info - get regulator info from device tree
  140. * @hba: per adapter instance
  141. *
  142. * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
  143. * If any of the supplies are not defined it is assumed that they are always-on
  144. * and hence return zero. If the property is defined but parsing is failed
  145. * then return corresponding error.
  146. *
  147. * Return: 0 upon success; < 0 upon failure.
  148. */
  149. static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
  150. {
  151. int err;
  152. struct device *dev = hba->dev;
  153. struct ufs_vreg_info *info = &hba->vreg_info;
  154. err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba, true);
  155. if (err)
  156. goto out;
  157. err = ufshcd_populate_vreg(dev, "vcc", &info->vcc, false);
  158. if (err)
  159. goto out;
  160. err = ufshcd_populate_vreg(dev, "vccq", &info->vccq, false);
  161. if (err)
  162. goto out;
  163. err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2, false);
  164. out:
  165. return err;
  166. }
  167. static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
  168. {
  169. struct device *dev = hba->dev;
  170. int ret;
  171. ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
  172. &hba->lanes_per_direction);
  173. if (ret) {
  174. dev_dbg(hba->dev,
  175. "%s: failed to read lanes-per-direction, ret=%d\n",
  176. __func__, ret);
  177. hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
  178. }
  179. }
  180. /**
  181. * ufshcd_parse_clock_min_max_freq - Parse MIN and MAX clocks freq
  182. * @hba: per adapter instance
  183. *
  184. * This function parses MIN and MAX frequencies of all clocks required
  185. * by the host drivers.
  186. *
  187. * Returns 0 for success and non-zero for failure
  188. */
  189. static int ufshcd_parse_clock_min_max_freq(struct ufs_hba *hba)
  190. {
  191. struct list_head *head = &hba->clk_list_head;
  192. struct ufs_clk_info *clki;
  193. struct dev_pm_opp *opp;
  194. unsigned long freq;
  195. u8 idx = 0;
  196. list_for_each_entry(clki, head, list) {
  197. if (!clki->name)
  198. continue;
  199. clki->clk = devm_clk_get(hba->dev, clki->name);
  200. if (IS_ERR(clki->clk))
  201. continue;
  202. /* Find Max Freq */
  203. freq = ULONG_MAX;
  204. opp = dev_pm_opp_find_freq_floor_indexed(hba->dev, &freq, idx);
  205. if (IS_ERR(opp)) {
  206. dev_err(hba->dev, "Failed to find OPP for MAX frequency\n");
  207. return PTR_ERR(opp);
  208. }
  209. clki->max_freq = dev_pm_opp_get_freq_indexed(opp, idx);
  210. dev_pm_opp_put(opp);
  211. /* Find Min Freq */
  212. freq = 0;
  213. opp = dev_pm_opp_find_freq_ceil_indexed(hba->dev, &freq, idx);
  214. if (IS_ERR(opp)) {
  215. dev_err(hba->dev, "Failed to find OPP for MIN frequency\n");
  216. return PTR_ERR(opp);
  217. }
  218. clki->min_freq = dev_pm_opp_get_freq_indexed(opp, idx++);
  219. dev_pm_opp_put(opp);
  220. }
  221. return 0;
  222. }
  223. static int ufshcd_parse_operating_points(struct ufs_hba *hba)
  224. {
  225. struct device *dev = hba->dev;
  226. struct device_node *np = dev->of_node;
  227. struct dev_pm_opp_config config = {};
  228. struct ufs_clk_info *clki;
  229. const char **clk_names;
  230. int cnt, i, ret;
  231. if (!of_property_present(np, "operating-points-v2"))
  232. return 0;
  233. if (of_property_present(np, "freq-table-hz")) {
  234. dev_err(dev, "%s: operating-points and freq-table-hz are incompatible\n",
  235. __func__);
  236. return -EINVAL;
  237. }
  238. cnt = of_property_count_strings(np, "clock-names");
  239. if (cnt <= 0) {
  240. dev_err(dev, "%s: Missing clock-names\n", __func__);
  241. return -ENODEV;
  242. }
  243. /* OPP expects clk_names to be NULL terminated */
  244. clk_names = devm_kcalloc(dev, cnt + 1, sizeof(*clk_names), GFP_KERNEL);
  245. if (!clk_names)
  246. return -ENOMEM;
  247. /*
  248. * We still need to get reference to all clocks as the UFS core uses
  249. * them separately.
  250. */
  251. for (i = 0; i < cnt; i++) {
  252. ret = of_property_read_string_index(np, "clock-names", i,
  253. &clk_names[i]);
  254. if (ret)
  255. return ret;
  256. clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
  257. if (!clki)
  258. return -ENOMEM;
  259. clki->name = devm_kstrdup(dev, clk_names[i], GFP_KERNEL);
  260. if (!clki->name)
  261. return -ENOMEM;
  262. if (!strcmp(clk_names[i], "ref_clk"))
  263. clki->keep_link_active = true;
  264. list_add_tail(&clki->list, &hba->clk_list_head);
  265. }
  266. config.clk_names = clk_names,
  267. config.config_clks = ufshcd_opp_config_clks;
  268. ret = devm_pm_opp_set_config(dev, &config);
  269. if (ret)
  270. return ret;
  271. ret = devm_pm_opp_of_add_table(dev);
  272. if (ret) {
  273. dev_err(dev, "Failed to add OPP table: %d\n", ret);
  274. return ret;
  275. }
  276. ret = ufshcd_parse_clock_min_max_freq(hba);
  277. if (ret)
  278. return ret;
  279. hba->use_pm_opp = true;
  280. return 0;
  281. }
  282. /**
  283. * ufshcd_negotiate_pwr_params - find power mode settings that are supported by
  284. * both the controller and the device
  285. * @host_params: pointer to host parameters
  286. * @dev_max: pointer to device attributes
  287. * @agreed_pwr: returned agreed attributes
  288. *
  289. * Return: 0 on success, non-zero value on failure.
  290. */
  291. int ufshcd_negotiate_pwr_params(const struct ufs_host_params *host_params,
  292. const struct ufs_pa_layer_attr *dev_max,
  293. struct ufs_pa_layer_attr *agreed_pwr)
  294. {
  295. int min_host_gear;
  296. int min_dev_gear;
  297. bool is_dev_sup_hs = false;
  298. bool is_host_max_hs = false;
  299. if (dev_max->pwr_rx == FAST_MODE)
  300. is_dev_sup_hs = true;
  301. if (host_params->desired_working_mode == UFS_HS_MODE) {
  302. is_host_max_hs = true;
  303. min_host_gear = min_t(u32, host_params->hs_rx_gear,
  304. host_params->hs_tx_gear);
  305. } else {
  306. min_host_gear = min_t(u32, host_params->pwm_rx_gear,
  307. host_params->pwm_tx_gear);
  308. }
  309. /*
  310. * device doesn't support HS but host_params->desired_working_mode is HS,
  311. * thus device and host_params don't agree
  312. */
  313. if (!is_dev_sup_hs && is_host_max_hs) {
  314. pr_info("%s: device doesn't support HS\n",
  315. __func__);
  316. return -ENOTSUPP;
  317. } else if (is_dev_sup_hs && is_host_max_hs) {
  318. /*
  319. * since device supports HS, it supports FAST_MODE.
  320. * since host_params->desired_working_mode is also HS
  321. * then final decision (FAST/FASTAUTO) is done according
  322. * to pltfrm_params as it is the restricting factor
  323. */
  324. agreed_pwr->pwr_rx = host_params->rx_pwr_hs;
  325. agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
  326. } else {
  327. /*
  328. * here host_params->desired_working_mode is PWM.
  329. * it doesn't matter whether device supports HS or PWM,
  330. * in both cases host_params->desired_working_mode will
  331. * determine the mode
  332. */
  333. agreed_pwr->pwr_rx = host_params->rx_pwr_pwm;
  334. agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
  335. }
  336. /*
  337. * we would like tx to work in the minimum number of lanes
  338. * between device capability and vendor preferences.
  339. * the same decision will be made for rx
  340. */
  341. agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
  342. host_params->tx_lanes);
  343. agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
  344. host_params->rx_lanes);
  345. /* device maximum gear is the minimum between device rx and tx gears */
  346. min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
  347. /*
  348. * if both device capabilities and vendor pre-defined preferences are
  349. * both HS or both PWM then set the minimum gear to be the chosen
  350. * working gear.
  351. * if one is PWM and one is HS then the one that is PWM get to decide
  352. * what is the gear, as it is the one that also decided previously what
  353. * pwr the device will be configured to.
  354. */
  355. if ((is_dev_sup_hs && is_host_max_hs) ||
  356. (!is_dev_sup_hs && !is_host_max_hs)) {
  357. agreed_pwr->gear_rx =
  358. min_t(u32, min_dev_gear, min_host_gear);
  359. } else if (!is_dev_sup_hs) {
  360. agreed_pwr->gear_rx = min_dev_gear;
  361. } else {
  362. agreed_pwr->gear_rx = min_host_gear;
  363. }
  364. agreed_pwr->gear_tx = agreed_pwr->gear_rx;
  365. agreed_pwr->hs_rate = host_params->hs_rate;
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(ufshcd_negotiate_pwr_params);
  369. void ufshcd_init_host_params(struct ufs_host_params *host_params)
  370. {
  371. *host_params = (struct ufs_host_params){
  372. .tx_lanes = UFS_LANE_2,
  373. .rx_lanes = UFS_LANE_2,
  374. .hs_rx_gear = UFS_HS_G3,
  375. .hs_tx_gear = UFS_HS_G3,
  376. .pwm_rx_gear = UFS_PWM_G4,
  377. .pwm_tx_gear = UFS_PWM_G4,
  378. .rx_pwr_pwm = SLOW_MODE,
  379. .tx_pwr_pwm = SLOW_MODE,
  380. .rx_pwr_hs = FAST_MODE,
  381. .tx_pwr_hs = FAST_MODE,
  382. .hs_rate = PA_HS_MODE_B,
  383. .desired_working_mode = UFS_HS_MODE,
  384. };
  385. }
  386. EXPORT_SYMBOL_GPL(ufshcd_init_host_params);
  387. /**
  388. * ufshcd_pltfrm_init - probe routine of the driver
  389. * @pdev: pointer to Platform device handle
  390. * @vops: pointer to variant ops
  391. *
  392. * Return: 0 on success, non-zero value on failure.
  393. */
  394. int ufshcd_pltfrm_init(struct platform_device *pdev,
  395. const struct ufs_hba_variant_ops *vops)
  396. {
  397. struct ufs_hba *hba;
  398. void __iomem *mmio_base;
  399. int irq, err;
  400. struct device *dev = &pdev->dev;
  401. mmio_base = devm_platform_ioremap_resource(pdev, 0);
  402. if (IS_ERR(mmio_base))
  403. return PTR_ERR(mmio_base);
  404. irq = platform_get_irq(pdev, 0);
  405. if (irq < 0)
  406. return irq;
  407. err = ufshcd_alloc_host(dev, &hba);
  408. if (err) {
  409. dev_err(dev, "Allocation failed\n");
  410. return err;
  411. }
  412. hba->vops = vops;
  413. err = ufshcd_parse_clock_info(hba);
  414. if (err) {
  415. dev_err(dev, "%s: clock parse failed %d\n",
  416. __func__, err);
  417. return err;
  418. }
  419. err = ufshcd_parse_regulator_info(hba);
  420. if (err) {
  421. dev_err(dev, "%s: regulator init failed %d\n",
  422. __func__, err);
  423. return err;
  424. }
  425. ufshcd_init_lanes_per_dir(hba);
  426. err = ufshcd_parse_operating_points(hba);
  427. if (err) {
  428. dev_err(dev, "%s: OPP parse failed %d\n", __func__, err);
  429. return err;
  430. }
  431. err = ufshcd_init(hba, mmio_base, irq);
  432. if (err) {
  433. dev_err_probe(dev, err, "Initialization failed with error %d\n",
  434. err);
  435. return err;
  436. }
  437. pm_runtime_set_active(dev);
  438. pm_runtime_enable(dev);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
  442. /**
  443. * ufshcd_pltfrm_remove - Remove ufshcd platform
  444. * @pdev: pointer to Platform device handle
  445. */
  446. void ufshcd_pltfrm_remove(struct platform_device *pdev)
  447. {
  448. struct ufs_hba *hba = platform_get_drvdata(pdev);
  449. pm_runtime_get_sync(&pdev->dev);
  450. ufshcd_remove(hba);
  451. pm_runtime_disable(&pdev->dev);
  452. pm_runtime_put_noidle(&pdev->dev);
  453. }
  454. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_remove);
  455. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  456. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  457. MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
  458. MODULE_LICENSE("GPL");