ti-aemif.c 12 KB

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