ti-aemif.c 12 KB

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