ufshcd-pltfrm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <[email protected]>
  8. * Vinayak Holikatti <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/of.h>
  14. #include <ufs/ufshcd.h>
  15. #include "ufshcd-pltfrm.h"
  16. #include <ufs/unipro.h>
  17. #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
  18. static int ufshcd_parse_clock_info(struct ufs_hba *hba)
  19. {
  20. int ret = 0;
  21. int cnt;
  22. int i;
  23. struct device *dev = hba->dev;
  24. struct device_node *np = dev->of_node;
  25. const char *name;
  26. u32 *clkfreq = NULL;
  27. struct ufs_clk_info *clki;
  28. int len = 0;
  29. size_t sz = 0;
  30. if (!np)
  31. goto out;
  32. cnt = of_property_count_strings(np, "clock-names");
  33. if (!cnt || (cnt == -EINVAL)) {
  34. dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
  35. __func__);
  36. } else if (cnt < 0) {
  37. dev_err(dev, "%s: count clock strings failed, err %d\n",
  38. __func__, cnt);
  39. ret = cnt;
  40. }
  41. if (cnt <= 0)
  42. goto out;
  43. if (!of_get_property(np, "freq-table-hz", &len)) {
  44. dev_info(dev, "freq-table-hz property not specified\n");
  45. goto out;
  46. }
  47. if (len <= 0)
  48. goto out;
  49. sz = len / sizeof(*clkfreq);
  50. if (sz != 2 * cnt) {
  51. dev_err(dev, "%s len mismatch\n", "freq-table-hz");
  52. ret = -EINVAL;
  53. goto out;
  54. }
  55. clkfreq = devm_kcalloc(dev, sz, sizeof(*clkfreq),
  56. GFP_KERNEL);
  57. if (!clkfreq) {
  58. ret = -ENOMEM;
  59. goto out;
  60. }
  61. ret = of_property_read_u32_array(np, "freq-table-hz",
  62. clkfreq, sz);
  63. if (ret && (ret != -EINVAL)) {
  64. dev_err(dev, "%s: error reading array %d\n",
  65. "freq-table-hz", ret);
  66. return ret;
  67. }
  68. for (i = 0; i < sz; i += 2) {
  69. ret = of_property_read_string_index(np, "clock-names", i/2,
  70. &name);
  71. if (ret)
  72. goto out;
  73. clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
  74. if (!clki) {
  75. ret = -ENOMEM;
  76. goto out;
  77. }
  78. clki->min_freq = clkfreq[i];
  79. clki->max_freq = clkfreq[i+1];
  80. clki->name = devm_kstrdup(dev, name, GFP_KERNEL);
  81. if (!clki->name) {
  82. ret = -ENOMEM;
  83. goto out;
  84. }
  85. if (!strcmp(name, "ref_clk"))
  86. clki->keep_link_active = true;
  87. dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
  88. clki->min_freq, clki->max_freq, clki->name);
  89. list_add_tail(&clki->list, &hba->clk_list_head);
  90. }
  91. out:
  92. return ret;
  93. }
  94. static bool phandle_exists(const struct device_node *np,
  95. const char *phandle_name, int index)
  96. {
  97. struct device_node *parse_np = of_parse_phandle(np, phandle_name, index);
  98. if (parse_np)
  99. of_node_put(parse_np);
  100. return parse_np != NULL;
  101. }
  102. #define MAX_PROP_SIZE 32
  103. int ufshcd_populate_vreg(struct device *dev, const char *name,
  104. struct ufs_vreg **out_vreg)
  105. {
  106. char prop_name[MAX_PROP_SIZE];
  107. struct ufs_vreg *vreg = NULL;
  108. struct device_node *np = dev->of_node;
  109. if (!np) {
  110. dev_err(dev, "%s: non DT initialization\n", __func__);
  111. goto out;
  112. }
  113. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
  114. if (!phandle_exists(np, prop_name, 0)) {
  115. dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
  116. __func__, prop_name);
  117. goto out;
  118. }
  119. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  120. if (!vreg)
  121. return -ENOMEM;
  122. vreg->name = devm_kstrdup(dev, name, GFP_KERNEL);
  123. if (!vreg->name)
  124. return -ENOMEM;
  125. snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
  126. if (of_property_read_u32(np, prop_name, &vreg->max_uA)) {
  127. dev_info(dev, "%s: unable to find %s\n", __func__, prop_name);
  128. vreg->max_uA = 0;
  129. }
  130. out:
  131. *out_vreg = vreg;
  132. return 0;
  133. }
  134. EXPORT_SYMBOL_GPL(ufshcd_populate_vreg);
  135. /**
  136. * ufshcd_parse_regulator_info - get regulator info from device tree
  137. * @hba: per adapter instance
  138. *
  139. * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
  140. * If any of the supplies are not defined it is assumed that they are always-on
  141. * and hence return zero. If the property is defined but parsing is failed
  142. * then return corresponding error.
  143. */
  144. static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
  145. {
  146. int err;
  147. struct device *dev = hba->dev;
  148. struct ufs_vreg_info *info = &hba->vreg_info;
  149. err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
  150. if (err)
  151. goto out;
  152. err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
  153. if (err)
  154. goto out;
  155. err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
  156. if (err)
  157. goto out;
  158. err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
  159. out:
  160. return err;
  161. }
  162. void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
  163. {
  164. ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
  165. }
  166. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
  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_get_pwr_dev_param - get finally agreed attributes for
  182. * power mode change
  183. * @pltfrm_param: pointer to platform parameters
  184. * @dev_max: pointer to device attributes
  185. * @agreed_pwr: returned agreed attributes
  186. *
  187. * Returns 0 on success, non-zero value on failure
  188. */
  189. int ufshcd_get_pwr_dev_param(const struct ufs_dev_params *pltfrm_param,
  190. const struct ufs_pa_layer_attr *dev_max,
  191. struct ufs_pa_layer_attr *agreed_pwr)
  192. {
  193. int min_pltfrm_gear;
  194. int min_dev_gear;
  195. bool is_dev_sup_hs = false;
  196. bool is_pltfrm_max_hs = false;
  197. if (dev_max->pwr_rx == FAST_MODE)
  198. is_dev_sup_hs = true;
  199. if (pltfrm_param->desired_working_mode == UFS_HS_MODE) {
  200. is_pltfrm_max_hs = true;
  201. min_pltfrm_gear = min_t(u32, pltfrm_param->hs_rx_gear,
  202. pltfrm_param->hs_tx_gear);
  203. } else {
  204. min_pltfrm_gear = min_t(u32, pltfrm_param->pwm_rx_gear,
  205. pltfrm_param->pwm_tx_gear);
  206. }
  207. /*
  208. * device doesn't support HS but
  209. * pltfrm_param->desired_working_mode is HS,
  210. * thus device and pltfrm_param don't agree
  211. */
  212. if (!is_dev_sup_hs && is_pltfrm_max_hs) {
  213. pr_info("%s: device doesn't support HS\n",
  214. __func__);
  215. return -ENOTSUPP;
  216. } else if (is_dev_sup_hs && is_pltfrm_max_hs) {
  217. /*
  218. * since device supports HS, it supports FAST_MODE.
  219. * since pltfrm_param->desired_working_mode is also HS
  220. * then final decision (FAST/FASTAUTO) is done according
  221. * to pltfrm_params as it is the restricting factor
  222. */
  223. agreed_pwr->pwr_rx = pltfrm_param->rx_pwr_hs;
  224. agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
  225. } else {
  226. /*
  227. * here pltfrm_param->desired_working_mode is PWM.
  228. * it doesn't matter whether device supports HS or PWM,
  229. * in both cases pltfrm_param->desired_working_mode will
  230. * determine the mode
  231. */
  232. agreed_pwr->pwr_rx = pltfrm_param->rx_pwr_pwm;
  233. agreed_pwr->pwr_tx = agreed_pwr->pwr_rx;
  234. }
  235. /*
  236. * we would like tx to work in the minimum number of lanes
  237. * between device capability and vendor preferences.
  238. * the same decision will be made for rx
  239. */
  240. agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
  241. pltfrm_param->tx_lanes);
  242. agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
  243. pltfrm_param->rx_lanes);
  244. /* device maximum gear is the minimum between device rx and tx gears */
  245. min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
  246. /*
  247. * if both device capabilities and vendor pre-defined preferences are
  248. * both HS or both PWM then set the minimum gear to be the chosen
  249. * working gear.
  250. * if one is PWM and one is HS then the one that is PWM get to decide
  251. * what is the gear, as it is the one that also decided previously what
  252. * pwr the device will be configured to.
  253. */
  254. if ((is_dev_sup_hs && is_pltfrm_max_hs) ||
  255. (!is_dev_sup_hs && !is_pltfrm_max_hs)) {
  256. agreed_pwr->gear_rx =
  257. min_t(u32, min_dev_gear, min_pltfrm_gear);
  258. } else if (!is_dev_sup_hs) {
  259. agreed_pwr->gear_rx = min_dev_gear;
  260. } else {
  261. agreed_pwr->gear_rx = min_pltfrm_gear;
  262. }
  263. agreed_pwr->gear_tx = agreed_pwr->gear_rx;
  264. agreed_pwr->hs_rate = pltfrm_param->hs_rate;
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(ufshcd_get_pwr_dev_param);
  268. void ufshcd_init_pwr_dev_param(struct ufs_dev_params *dev_param)
  269. {
  270. *dev_param = (struct ufs_dev_params){
  271. .tx_lanes = 2,
  272. .rx_lanes = 2,
  273. .hs_rx_gear = UFS_HS_G3,
  274. .hs_tx_gear = UFS_HS_G3,
  275. .pwm_rx_gear = UFS_PWM_G4,
  276. .pwm_tx_gear = UFS_PWM_G4,
  277. .rx_pwr_pwm = SLOW_MODE,
  278. .tx_pwr_pwm = SLOW_MODE,
  279. .rx_pwr_hs = FAST_MODE,
  280. .tx_pwr_hs = FAST_MODE,
  281. .hs_rate = PA_HS_MODE_B,
  282. .desired_working_mode = UFS_HS_MODE,
  283. };
  284. }
  285. EXPORT_SYMBOL_GPL(ufshcd_init_pwr_dev_param);
  286. /**
  287. * ufshcd_pltfrm_init - probe routine of the driver
  288. * @pdev: pointer to Platform device handle
  289. * @vops: pointer to variant ops
  290. *
  291. * Returns 0 on success, non-zero value on failure
  292. */
  293. int ufshcd_pltfrm_init(struct platform_device *pdev,
  294. const struct ufs_hba_variant_ops *vops)
  295. {
  296. struct ufs_hba *hba;
  297. void __iomem *mmio_base;
  298. int irq, err;
  299. struct device *dev = &pdev->dev;
  300. mmio_base = devm_platform_ioremap_resource(pdev, 0);
  301. if (IS_ERR(mmio_base)) {
  302. err = PTR_ERR(mmio_base);
  303. goto out;
  304. }
  305. irq = platform_get_irq(pdev, 0);
  306. if (irq < 0) {
  307. err = irq;
  308. goto out;
  309. }
  310. err = ufshcd_alloc_host(dev, &hba);
  311. if (err) {
  312. dev_err(dev, "Allocation failed\n");
  313. goto out;
  314. }
  315. hba->vops = vops;
  316. err = ufshcd_parse_clock_info(hba);
  317. if (err) {
  318. dev_err(dev, "%s: clock parse failed %d\n",
  319. __func__, err);
  320. goto dealloc_host;
  321. }
  322. err = ufshcd_parse_regulator_info(hba);
  323. if (err) {
  324. dev_err(dev, "%s: regulator init failed %d\n",
  325. __func__, err);
  326. goto dealloc_host;
  327. }
  328. ufshcd_init_lanes_per_dir(hba);
  329. err = ufshcd_init(hba, mmio_base, irq);
  330. if (err) {
  331. dev_err(dev, "Initialization failed\n");
  332. goto dealloc_host;
  333. }
  334. pm_runtime_set_active(dev);
  335. pm_runtime_enable(dev);
  336. return 0;
  337. dealloc_host:
  338. ufshcd_dealloc_host(hba);
  339. out:
  340. return err;
  341. }
  342. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
  343. MODULE_AUTHOR("Santosh Yaragnavi <[email protected]>");
  344. MODULE_AUTHOR("Vinayak Holikatti <[email protected]>");
  345. MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
  346. MODULE_LICENSE("GPL");