ti-aemif.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI AEMIF driver
  4. *
  5. * Copyright (C) 2010 - 2013 Texas Instruments Incorporated. http://www.ti.com/
  6. *
  7. * Authors:
  8. * Murali Karicheri <m-karicheri2@ti.com>
  9. * Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #define TA_SHIFT 2
  20. #define RHOLD_SHIFT 4
  21. #define RSTROBE_SHIFT 7
  22. #define RSETUP_SHIFT 13
  23. #define WHOLD_SHIFT 17
  24. #define WSTROBE_SHIFT 20
  25. #define WSETUP_SHIFT 26
  26. #define EW_SHIFT 30
  27. #define SSTROBE_SHIFT 31
  28. #define TA(x) ((x) << TA_SHIFT)
  29. #define RHOLD(x) ((x) << RHOLD_SHIFT)
  30. #define RSTROBE(x) ((x) << RSTROBE_SHIFT)
  31. #define RSETUP(x) ((x) << RSETUP_SHIFT)
  32. #define WHOLD(x) ((x) << WHOLD_SHIFT)
  33. #define WSTROBE(x) ((x) << WSTROBE_SHIFT)
  34. #define WSETUP(x) ((x) << WSETUP_SHIFT)
  35. #define EW(x) ((x) << EW_SHIFT)
  36. #define SSTROBE(x) ((x) << SSTROBE_SHIFT)
  37. #define ASIZE_MAX 0x1
  38. #define TA_MAX 0x3
  39. #define RHOLD_MAX 0x7
  40. #define RSTROBE_MAX 0x3f
  41. #define RSETUP_MAX 0xf
  42. #define WHOLD_MAX 0x7
  43. #define WSTROBE_MAX 0x3f
  44. #define WSETUP_MAX 0xf
  45. #define EW_MAX 0x1
  46. #define SSTROBE_MAX 0x1
  47. #define NUM_CS 4
  48. #define TA_VAL(x) (((x) & TA(TA_MAX)) >> TA_SHIFT)
  49. #define RHOLD_VAL(x) (((x) & RHOLD(RHOLD_MAX)) >> RHOLD_SHIFT)
  50. #define RSTROBE_VAL(x) (((x) & RSTROBE(RSTROBE_MAX)) >> RSTROBE_SHIFT)
  51. #define RSETUP_VAL(x) (((x) & RSETUP(RSETUP_MAX)) >> RSETUP_SHIFT)
  52. #define WHOLD_VAL(x) (((x) & WHOLD(WHOLD_MAX)) >> WHOLD_SHIFT)
  53. #define WSTROBE_VAL(x) (((x) & WSTROBE(WSTROBE_MAX)) >> WSTROBE_SHIFT)
  54. #define WSETUP_VAL(x) (((x) & WSETUP(WSETUP_MAX)) >> WSETUP_SHIFT)
  55. #define EW_VAL(x) (((x) & EW(EW_MAX)) >> EW_SHIFT)
  56. #define SSTROBE_VAL(x) (((x) & SSTROBE(SSTROBE_MAX)) >> SSTROBE_SHIFT)
  57. #define NRCSR_OFFSET 0x00
  58. #define AWCCR_OFFSET 0x04
  59. #define A1CR_OFFSET 0x10
  60. #define ACR_ASIZE_MASK 0x3
  61. #define ACR_EW_MASK BIT(30)
  62. #define ACR_SSTROBE_MASK BIT(31)
  63. #define ASIZE_16BIT 1
  64. #define CONFIG_MASK (TA(TA_MAX) | \
  65. RHOLD(RHOLD_MAX) | \
  66. RSTROBE(RSTROBE_MAX) | \
  67. RSETUP(RSETUP_MAX) | \
  68. WHOLD(WHOLD_MAX) | \
  69. WSTROBE(WSTROBE_MAX) | \
  70. WSETUP(WSETUP_MAX) | \
  71. EW(EW_MAX) | SSTROBE(SSTROBE_MAX) | \
  72. ASIZE_MAX)
  73. /**
  74. * struct aemif_cs_data: structure to hold cs parameters
  75. * @cs: chip-select number
  76. * @wstrobe: write strobe width, ns
  77. * @rstrobe: read strobe width, ns
  78. * @wsetup: write setup width, ns
  79. * @whold: write hold width, ns
  80. * @rsetup: read setup width, ns
  81. * @rhold: read hold width, ns
  82. * @ta: minimum turn around time, ns
  83. * @enable_ss: enable/disable select strobe mode
  84. * @enable_ew: enable/disable extended wait mode
  85. * @asize: width of the asynchronous device's data bus
  86. */
  87. struct aemif_cs_data {
  88. u8 cs;
  89. u16 wstrobe;
  90. u16 rstrobe;
  91. u8 wsetup;
  92. u8 whold;
  93. u8 rsetup;
  94. u8 rhold;
  95. u8 ta;
  96. u8 enable_ss;
  97. u8 enable_ew;
  98. u8 asize;
  99. };
  100. /**
  101. * struct aemif_device: structure to hold device data
  102. * @base: base address of AEMIF registers
  103. * @clk: source clock
  104. * @clk_rate: clock's rate in kHz
  105. * @num_cs: number of assigned chip-selects
  106. * @cs_offset: start number of cs nodes
  107. * @cs_data: array of chip-select settings
  108. */
  109. struct aemif_device {
  110. void __iomem *base;
  111. struct clk *clk;
  112. unsigned long clk_rate;
  113. u8 num_cs;
  114. int cs_offset;
  115. struct aemif_cs_data cs_data[NUM_CS];
  116. };
  117. /**
  118. * aemif_calc_rate - calculate timing data.
  119. * @pdev: platform device to calculate for
  120. * @wanted: The cycle time needed in nanoseconds.
  121. * @clk: The input clock rate in kHz.
  122. * @max: The maximum divider value that can be programmed.
  123. *
  124. * On success, returns the calculated timing value minus 1 for easy
  125. * programming into AEMIF timing registers, else negative errno.
  126. */
  127. static int aemif_calc_rate(struct platform_device *pdev, int wanted,
  128. unsigned long clk, int max)
  129. {
  130. int result;
  131. result = DIV_ROUND_UP((wanted * clk), NSEC_PER_MSEC) - 1;
  132. dev_dbg(&pdev->dev, "%s: result %d from %ld, %d\n", __func__, result,
  133. clk, wanted);
  134. /* It is generally OK to have a more relaxed timing than requested... */
  135. if (result < 0)
  136. result = 0;
  137. /* ... But configuring tighter timings is not an option. */
  138. else if (result > max)
  139. result = -EINVAL;
  140. return result;
  141. }
  142. /**
  143. * aemif_config_abus - configure async bus parameters
  144. * @pdev: platform device to configure for
  145. * @csnum: aemif chip select number
  146. *
  147. * This function programs the given timing values (in real clock) into the
  148. * AEMIF registers taking the AEMIF clock into account.
  149. *
  150. * This function does not use any locking while programming the AEMIF
  151. * because it is expected that there is only one user of a given
  152. * chip-select.
  153. *
  154. * Returns 0 on success, else negative errno.
  155. */
  156. static int aemif_config_abus(struct platform_device *pdev, int csnum)
  157. {
  158. struct aemif_device *aemif = platform_get_drvdata(pdev);
  159. struct aemif_cs_data *data = &aemif->cs_data[csnum];
  160. int ta, rhold, rstrobe, rsetup, whold, wstrobe, wsetup;
  161. unsigned long clk_rate = aemif->clk_rate;
  162. unsigned offset;
  163. u32 set, val;
  164. offset = A1CR_OFFSET + (data->cs - aemif->cs_offset) * 4;
  165. ta = aemif_calc_rate(pdev, data->ta, clk_rate, TA_MAX);
  166. rhold = aemif_calc_rate(pdev, data->rhold, clk_rate, RHOLD_MAX);
  167. rstrobe = aemif_calc_rate(pdev, data->rstrobe, clk_rate, RSTROBE_MAX);
  168. rsetup = aemif_calc_rate(pdev, data->rsetup, clk_rate, RSETUP_MAX);
  169. whold = aemif_calc_rate(pdev, data->whold, clk_rate, WHOLD_MAX);
  170. wstrobe = aemif_calc_rate(pdev, data->wstrobe, clk_rate, WSTROBE_MAX);
  171. wsetup = aemif_calc_rate(pdev, data->wsetup, clk_rate, WSETUP_MAX);
  172. if (ta < 0 || rhold < 0 || rstrobe < 0 || rsetup < 0 ||
  173. whold < 0 || wstrobe < 0 || wsetup < 0) {
  174. dev_err(&pdev->dev, "%s: cannot get suitable timings\n",
  175. __func__);
  176. return -EINVAL;
  177. }
  178. set = TA(ta) | RHOLD(rhold) | RSTROBE(rstrobe) | RSETUP(rsetup) |
  179. WHOLD(whold) | WSTROBE(wstrobe) | WSETUP(wsetup);
  180. set |= (data->asize & ACR_ASIZE_MASK);
  181. if (data->enable_ew)
  182. set |= ACR_EW_MASK;
  183. if (data->enable_ss)
  184. set |= ACR_SSTROBE_MASK;
  185. val = readl(aemif->base + offset);
  186. val &= ~CONFIG_MASK;
  187. val |= set;
  188. writel(val, aemif->base + offset);
  189. return 0;
  190. }
  191. static inline int aemif_cycles_to_nsec(int val, unsigned long clk_rate)
  192. {
  193. return ((val + 1) * NSEC_PER_MSEC) / clk_rate;
  194. }
  195. /**
  196. * aemif_get_hw_params - function to read hw register values
  197. * @pdev: platform device to read for
  198. * @csnum: aemif chip select number
  199. *
  200. * This function reads the defaults from the registers and update
  201. * the timing values. Required for get/set commands and also for
  202. * the case when driver needs to use defaults in hardware.
  203. */
  204. static void aemif_get_hw_params(struct platform_device *pdev, int csnum)
  205. {
  206. struct aemif_device *aemif = platform_get_drvdata(pdev);
  207. struct aemif_cs_data *data = &aemif->cs_data[csnum];
  208. unsigned long clk_rate = aemif->clk_rate;
  209. u32 val, offset;
  210. offset = A1CR_OFFSET + (data->cs - aemif->cs_offset) * 4;
  211. val = readl(aemif->base + offset);
  212. data->ta = aemif_cycles_to_nsec(TA_VAL(val), clk_rate);
  213. data->rhold = aemif_cycles_to_nsec(RHOLD_VAL(val), clk_rate);
  214. data->rstrobe = aemif_cycles_to_nsec(RSTROBE_VAL(val), clk_rate);
  215. data->rsetup = aemif_cycles_to_nsec(RSETUP_VAL(val), clk_rate);
  216. data->whold = aemif_cycles_to_nsec(WHOLD_VAL(val), clk_rate);
  217. data->wstrobe = aemif_cycles_to_nsec(WSTROBE_VAL(val), clk_rate);
  218. data->wsetup = aemif_cycles_to_nsec(WSETUP_VAL(val), clk_rate);
  219. data->enable_ew = EW_VAL(val);
  220. data->enable_ss = SSTROBE_VAL(val);
  221. data->asize = val & ASIZE_MAX;
  222. }
  223. /**
  224. * of_aemif_parse_abus_config - parse CS configuration from DT
  225. * @pdev: platform device to parse for
  226. * @np: device node ptr
  227. *
  228. * This function update the emif async bus configuration based on the values
  229. * configured in a cs device binding node.
  230. */
  231. static int of_aemif_parse_abus_config(struct platform_device *pdev,
  232. struct device_node *np)
  233. {
  234. struct aemif_device *aemif = platform_get_drvdata(pdev);
  235. struct aemif_cs_data *data;
  236. u32 cs;
  237. u32 val;
  238. if (of_property_read_u32(np, "ti,cs-chipselect", &cs)) {
  239. dev_dbg(&pdev->dev, "cs property is required");
  240. return -EINVAL;
  241. }
  242. if (cs - aemif->cs_offset >= NUM_CS || cs < aemif->cs_offset) {
  243. dev_dbg(&pdev->dev, "cs number is incorrect %d", cs);
  244. return -EINVAL;
  245. }
  246. if (aemif->num_cs >= NUM_CS) {
  247. dev_dbg(&pdev->dev, "cs count is more than %d", NUM_CS);
  248. return -EINVAL;
  249. }
  250. data = &aemif->cs_data[aemif->num_cs];
  251. data->cs = cs;
  252. /* read the current value in the hw register */
  253. aemif_get_hw_params(pdev, aemif->num_cs++);
  254. /* override the values from device node */
  255. if (!of_property_read_u32(np, "ti,cs-min-turnaround-ns", &val))
  256. data->ta = val;
  257. if (!of_property_read_u32(np, "ti,cs-read-hold-ns", &val))
  258. data->rhold = val;
  259. if (!of_property_read_u32(np, "ti,cs-read-strobe-ns", &val))
  260. data->rstrobe = val;
  261. if (!of_property_read_u32(np, "ti,cs-read-setup-ns", &val))
  262. data->rsetup = val;
  263. if (!of_property_read_u32(np, "ti,cs-write-hold-ns", &val))
  264. data->whold = val;
  265. if (!of_property_read_u32(np, "ti,cs-write-strobe-ns", &val))
  266. data->wstrobe = val;
  267. if (!of_property_read_u32(np, "ti,cs-write-setup-ns", &val))
  268. data->wsetup = val;
  269. if (!of_property_read_u32(np, "ti,cs-bus-width", &val))
  270. if (val == 16)
  271. data->asize = 1;
  272. data->enable_ew = of_property_read_bool(np, "ti,cs-extended-wait-mode");
  273. data->enable_ss = of_property_read_bool(np, "ti,cs-select-strobe-mode");
  274. return 0;
  275. }
  276. static const struct of_device_id aemif_of_match[] = {
  277. { .compatible = "ti,davinci-aemif", },
  278. { .compatible = "ti,da850-aemif", },
  279. {},
  280. };
  281. MODULE_DEVICE_TABLE(of, aemif_of_match);
  282. static int aemif_probe(struct platform_device *pdev)
  283. {
  284. int i;
  285. int ret = -ENODEV;
  286. struct device *dev = &pdev->dev;
  287. struct device_node *np = dev->of_node;
  288. struct aemif_device *aemif;
  289. aemif = devm_kzalloc(dev, sizeof(*aemif), GFP_KERNEL);
  290. if (!aemif)
  291. return -ENOMEM;
  292. platform_set_drvdata(pdev, aemif);
  293. aemif->clk = devm_clk_get_enabled(dev, NULL);
  294. if (IS_ERR(aemif->clk))
  295. return dev_err_probe(dev, PTR_ERR(aemif->clk),
  296. "cannot get clock 'aemif'\n");
  297. aemif->clk_rate = clk_get_rate(aemif->clk) / MSEC_PER_SEC;
  298. if (np && of_device_is_compatible(np, "ti,da850-aemif"))
  299. aemif->cs_offset = 2;
  300. aemif->base = devm_platform_ioremap_resource(pdev, 0);
  301. if (IS_ERR(aemif->base))
  302. return PTR_ERR(aemif->base);
  303. if (np) {
  304. /*
  305. * For every controller device node, there is a cs device node
  306. * that describe the bus configuration parameters. This
  307. * functions iterate over these nodes and update the cs data
  308. * array.
  309. */
  310. for_each_available_child_of_node_scoped(np, child_np) {
  311. ret = of_aemif_parse_abus_config(pdev, child_np);
  312. if (ret < 0)
  313. return ret;
  314. }
  315. }
  316. for (i = 0; i < aemif->num_cs; i++) {
  317. ret = aemif_config_abus(pdev, i);
  318. if (ret < 0) {
  319. dev_err(dev, "Error configuring chip select %d\n",
  320. aemif->cs_data[i].cs);
  321. return ret;
  322. }
  323. }
  324. /*
  325. * Create a child devices explicitly from here to guarantee that the
  326. * child will be probed after the AEMIF timing parameters are set.
  327. */
  328. if (np) {
  329. for_each_available_child_of_node_scoped(np, child_np) {
  330. ret = of_platform_populate(child_np, NULL, NULL, dev);
  331. if (ret < 0)
  332. return ret;
  333. }
  334. }
  335. return 0;
  336. }
  337. static struct platform_driver aemif_driver = {
  338. .probe = aemif_probe,
  339. .driver = {
  340. .name = "ti-aemif",
  341. .of_match_table = of_match_ptr(aemif_of_match),
  342. },
  343. };
  344. module_platform_driver(aemif_driver);
  345. MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
  346. MODULE_AUTHOR("Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>");
  347. MODULE_DESCRIPTION("Texas Instruments AEMIF driver");
  348. MODULE_LICENSE("GPL v2");
  349. MODULE_ALIAS("platform:" KBUILD_MODNAME);