ti-aemif.c 12 KB

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