ti-gpmc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments GPMC Driver
  4. *
  5. * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #include <asm/io.h>
  8. #include <asm/arch/sys_proto.h>
  9. #include <clk.h>
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/device_compat.h>
  14. #include <dm/devres.h>
  15. #include <dm/lists.h>
  16. #include <linux/mtd/omap_gpmc.h>
  17. #include <linux/ioport.h>
  18. #include <linux/io.h>
  19. #include "ti-gpmc.h"
  20. enum gpmc_clk_domain {
  21. GPMC_CD_FCLK,
  22. GPMC_CD_CLK
  23. };
  24. struct gpmc_cs_data {
  25. const char *name;
  26. #define GPMC_CS_RESERVED BIT(0)
  27. u32 flags;
  28. };
  29. struct ti_gpmc {
  30. void __iomem *base;
  31. u32 cs_num;
  32. u32 nr_waitpins;
  33. struct clk *l3_clk;
  34. u32 capability_flags;
  35. struct resource data;
  36. };
  37. static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
  38. static unsigned int gpmc_cs_num = GPMC_CS_NUM;
  39. static unsigned int gpmc_nr_waitpins;
  40. static unsigned int gpmc_capability;
  41. static void __iomem *gpmc_base;
  42. static struct clk *gpmc_l3_clk;
  43. /* Public, as required by nand/raw/omap_gpmc.c */
  44. const struct gpmc *gpmc_cfg;
  45. /*
  46. * The first 1MB of GPMC address space is typically mapped to
  47. * the internal ROM. Never allocate the first page, to
  48. * facilitate bug detection; even if we didn't boot from ROM.
  49. * As GPMC minimum partition size is 16MB we can only start from
  50. * there.
  51. */
  52. #define GPMC_MEM_START 0x1000000
  53. #define GPMC_MEM_END 0x3FFFFFFF
  54. static void gpmc_write_reg(int idx, u32 val)
  55. {
  56. writel_relaxed(val, gpmc_base + idx);
  57. }
  58. static u32 gpmc_read_reg(int idx)
  59. {
  60. return readl_relaxed(gpmc_base + idx);
  61. }
  62. static void gpmc_cs_write_reg(int cs, int idx, u32 val)
  63. {
  64. void __iomem *reg_addr;
  65. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  66. writel_relaxed(val, reg_addr);
  67. }
  68. static u32 gpmc_cs_read_reg(int cs, int idx)
  69. {
  70. void __iomem *reg_addr;
  71. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  72. return readl_relaxed(reg_addr);
  73. }
  74. static unsigned long gpmc_get_fclk_period(void)
  75. {
  76. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  77. rate /= 1000;
  78. rate = 1000000000 / rate; /* In picoseconds */
  79. return rate;
  80. }
  81. /**
  82. * gpmc_get_clk_period - get period of selected clock domain in ps
  83. * @cs: Chip Select Region.
  84. * @cd: Clock Domain.
  85. *
  86. * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
  87. * prior to calling this function with GPMC_CD_CLK.
  88. */
  89. static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
  90. {
  91. unsigned long tick_ps = gpmc_get_fclk_period();
  92. u32 l;
  93. int div;
  94. switch (cd) {
  95. case GPMC_CD_CLK:
  96. /* get current clk divider */
  97. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  98. div = (l & 0x03) + 1;
  99. /* get GPMC_CLK period */
  100. tick_ps *= div;
  101. break;
  102. case GPMC_CD_FCLK:
  103. default:
  104. break;
  105. }
  106. return tick_ps;
  107. }
  108. static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
  109. enum gpmc_clk_domain cd)
  110. {
  111. unsigned long tick_ps;
  112. /* Calculate in picosecs to yield more exact results */
  113. tick_ps = gpmc_get_clk_period(cs, cd);
  114. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  115. }
  116. static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  117. {
  118. return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
  119. }
  120. static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  121. {
  122. unsigned long tick_ps;
  123. /* Calculate in picosecs to yield more exact results */
  124. tick_ps = gpmc_get_fclk_period();
  125. return (time_ps + tick_ps - 1) / tick_ps;
  126. }
  127. static __maybe_unused unsigned int gpmc_clk_ticks_to_ns(unsigned int ticks, int cs,
  128. enum gpmc_clk_domain cd)
  129. {
  130. return ticks * gpmc_get_clk_period(cs, cd) / 1000;
  131. }
  132. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  133. {
  134. u32 l;
  135. l = gpmc_cs_read_reg(cs, reg);
  136. if (value)
  137. l |= mask;
  138. else
  139. l &= ~mask;
  140. gpmc_cs_write_reg(cs, reg, l);
  141. }
  142. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  143. {
  144. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  145. GPMC_CONFIG1_TIME_PARA_GRAN,
  146. p->time_para_granularity);
  147. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  148. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  149. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  150. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  151. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  152. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  153. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  154. GPMC_CONFIG4_WEEXTRADELAY, p->we_extra_delay);
  155. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  156. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  157. p->cycle2cyclesamecsen);
  158. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  159. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  160. p->cycle2cyclediffcsen);
  161. }
  162. #if IS_ENABLED(CONFIG_TI_GPMC_DEBUG)
  163. /**
  164. * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
  165. * @cs: Chip Select Region
  166. * @reg: GPMC_CS_CONFIGn register offset.
  167. * @st_bit: Start Bit
  168. * @end_bit: End Bit. Must be >= @st_bit.
  169. * @max: Maximum parameter value (before optional @shift).
  170. * If 0, maximum is as high as @st_bit and @end_bit allow.
  171. * @name: DTS node name, w/o "gpmc,"
  172. * @cd: Clock Domain of timing parameter.
  173. * @shift: Parameter value left shifts @shift, which is then printed instead of value.
  174. * @raw: Raw Format Option.
  175. * raw format: gpmc,name = <value>
  176. * tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
  177. * Where x ns -- y ns result in the same tick value.
  178. * When @max is exceeded, "invalid" is printed inside comment.
  179. * @noval: Parameter values equal to 0 are not printed.
  180. * @return: Specified timing parameter (after optional @shift).
  181. *
  182. */
  183. static int get_gpmc_timing_reg(/* timing specifiers */
  184. int cs, int reg, int st_bit, int end_bit, int max,
  185. const char *name, const enum gpmc_clk_domain cd,
  186. /* value transform */
  187. int shift,
  188. /* format specifiers */
  189. bool raw, bool noval)
  190. {
  191. u32 l;
  192. int nr_bits;
  193. int mask;
  194. bool invalid;
  195. l = gpmc_cs_read_reg(cs, reg);
  196. nr_bits = end_bit - st_bit + 1;
  197. mask = (1 << nr_bits) - 1;
  198. l = (l >> st_bit) & mask;
  199. if (!max)
  200. max = mask;
  201. invalid = l > max;
  202. if (shift)
  203. l = (shift << l);
  204. if (noval && l == 0)
  205. return 0;
  206. if (!raw) {
  207. /* DTS tick format for timings in ns */
  208. unsigned int time_ns;
  209. unsigned int time_ns_min = 0;
  210. if (l)
  211. time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
  212. time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
  213. pr_info("gpmc,%s = <%u>; /* %u ns - %u ns; %i ticks%s*/\n",
  214. name, time_ns, time_ns_min, time_ns, l,
  215. invalid ? "; invalid " : " ");
  216. } else {
  217. /* raw format */
  218. pr_info("gpmc,%s = <%u>;%s\n", name, l,
  219. invalid ? " /* invalid */" : "");
  220. }
  221. return l;
  222. }
  223. #define GPMC_PRINT_CONFIG(cs, config) \
  224. pr_info("CS%i %s: 0x%08x\n", cs, #config, \
  225. gpmc_cs_read_reg(cs, config))
  226. #define GPMC_GET_RAW(reg, st, end, field) \
  227. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
  228. #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
  229. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
  230. #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
  231. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
  232. #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
  233. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
  234. #define GPMC_GET_TICKS(reg, st, end, field) \
  235. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
  236. #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
  237. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
  238. #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
  239. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
  240. static void gpmc_show_regs(int cs, const char *desc)
  241. {
  242. pr_info("gpmc cs%i %s:\n", cs, desc);
  243. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
  244. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
  245. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
  246. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
  247. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
  248. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
  249. }
  250. /*
  251. * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
  252. * see commit c9fb809.
  253. */
  254. static void gpmc_cs_show_timings(int cs, const char *desc)
  255. {
  256. gpmc_show_regs(cs, desc);
  257. pr_info("gpmc cs%i access configuration:\n", cs);
  258. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
  259. GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
  260. GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 12, 13, 1,
  261. GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
  262. GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
  263. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
  264. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
  265. GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
  266. GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
  267. "burst-length");
  268. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
  269. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
  270. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
  271. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
  272. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
  273. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
  274. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
  275. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
  276. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
  277. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
  278. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
  279. pr_info("gpmc cs%i timings configuration:\n", cs);
  280. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
  281. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
  282. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
  283. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
  284. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
  285. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
  286. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  287. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
  288. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
  289. "adv-aad-mux-rd-off-ns");
  290. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
  291. "adv-aad-mux-wr-off-ns");
  292. }
  293. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
  294. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
  295. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  296. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
  297. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
  298. }
  299. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
  300. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
  301. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
  302. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
  303. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
  304. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
  305. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
  306. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
  307. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  308. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  309. "wait-monitoring-ns", GPMC_CD_CLK);
  310. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  311. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  312. "clk-activation-ns", GPMC_CD_FCLK);
  313. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
  314. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
  315. }
  316. #else
  317. static inline void gpmc_cs_show_timings(int cs, const char *desc)
  318. {
  319. }
  320. #endif
  321. /**
  322. * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
  323. * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
  324. * prior to calling this function with @cd equal to GPMC_CD_CLK.
  325. *
  326. * @cs: Chip Select Region.
  327. * @reg: GPMC_CS_CONFIGn register offset.
  328. * @st_bit: Start Bit
  329. * @end_bit: End Bit. Must be >= @st_bit.
  330. * @max: Maximum parameter value.
  331. * If 0, maximum is as high as @st_bit and @end_bit allow.
  332. * @time: Timing parameter in ns.
  333. * @cd: Timing parameter clock domain.
  334. * @name: Timing parameter name.
  335. * @return: 0 on success, -1 on error.
  336. */
  337. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
  338. int time, enum gpmc_clk_domain cd, const char *name)
  339. {
  340. u32 l;
  341. int ticks, mask, nr_bits;
  342. if (time == 0)
  343. ticks = 0;
  344. else
  345. ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
  346. nr_bits = end_bit - st_bit + 1;
  347. mask = (1 << nr_bits) - 1;
  348. if (!max)
  349. max = mask;
  350. if (ticks > max) {
  351. pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
  352. __func__, cs, name, time, ticks, max);
  353. return -1;
  354. }
  355. l = gpmc_cs_read_reg(cs, reg);
  356. if (IS_ENABLED(CONFIG_TI_GPMC_DEBUG)) {
  357. pr_info("GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  358. cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
  359. (l >> st_bit) & mask, time);
  360. }
  361. l &= ~(mask << st_bit);
  362. l |= ticks << st_bit;
  363. gpmc_cs_write_reg(cs, reg, l);
  364. return 0;
  365. }
  366. /**
  367. * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
  368. * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
  369. * read --> don't sample bus too early
  370. * write --> data is longer on bus
  371. *
  372. * Formula:
  373. * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
  374. * / waitmonitoring_ticks)
  375. * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
  376. * div <= 0 check.
  377. *
  378. * @wait_monitoring: WAITMONITORINGTIME in ns.
  379. * @return: -1 on failure to scale, else proper divider > 0.
  380. */
  381. static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
  382. {
  383. int div = gpmc_ns_to_ticks(wait_monitoring);
  384. div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
  385. div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
  386. if (div > 4)
  387. return -1;
  388. if (div <= 0)
  389. div = 1;
  390. return div;
  391. }
  392. /**
  393. * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
  394. * @sync_clk: GPMC_CLK period in ps.
  395. * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
  396. * Else, returns -1.
  397. */
  398. static int gpmc_calc_divider(unsigned int sync_clk)
  399. {
  400. int div = gpmc_ps_to_ticks(sync_clk);
  401. if (div > 4)
  402. return -1;
  403. if (div <= 0)
  404. div = 1;
  405. return div;
  406. }
  407. /**
  408. * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
  409. * @cs: Chip Select Region.
  410. * @t: GPMC timing parameters.
  411. * @s: GPMC timing settings.
  412. * @return: 0 on success, -1 on error.
  413. */
  414. static int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
  415. const struct gpmc_settings *s)
  416. {
  417. int div, ret;
  418. u32 l;
  419. div = gpmc_calc_divider(t->sync_clk);
  420. if (div < 0)
  421. return -EINVAL;
  422. /*
  423. * See if we need to change the divider for waitmonitoringtime.
  424. *
  425. * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
  426. * pure asynchronous accesses, i.e. both read and write asynchronous.
  427. * However, only do so if WAITMONITORINGTIME is actually used, i.e.
  428. * either WAITREADMONITORING or WAITWRITEMONITORING is set.
  429. *
  430. * This statement must not change div to scale async WAITMONITORINGTIME
  431. * to protect mixed synchronous and asynchronous accesses.
  432. *
  433. * We raise an error later if WAITMONITORINGTIME does not fit.
  434. */
  435. if (!s->sync_read && !s->sync_write &&
  436. (s->wait_on_read || s->wait_on_write)
  437. ) {
  438. div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
  439. if (div < 0) {
  440. pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
  441. __func__,
  442. t->wait_monitoring
  443. );
  444. return -ENXIO;
  445. }
  446. }
  447. ret = 0;
  448. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 0, 3, 0, t->cs_on,
  449. GPMC_CD_FCLK, "cs_on");
  450. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 8, 12, 0, t->cs_rd_off,
  451. GPMC_CD_FCLK, "cs_rd_off");
  452. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 16, 20, 0, t->cs_wr_off,
  453. GPMC_CD_FCLK, "cs_wr_off");
  454. if (ret)
  455. return -ENXIO;
  456. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 0, 3, 0, t->adv_on,
  457. GPMC_CD_FCLK, "adv_on");
  458. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 8, 12, 0, t->adv_rd_off,
  459. GPMC_CD_FCLK, "adv_rd_off");
  460. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 16, 20, 0, t->adv_wr_off,
  461. GPMC_CD_FCLK, "adv_wr_off");
  462. if (ret)
  463. return -ENXIO;
  464. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  465. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 4, 6, 0,
  466. t->adv_aad_mux_on, GPMC_CD_FCLK,
  467. "adv_aad_mux_on");
  468. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 24, 26, 0,
  469. t->adv_aad_mux_rd_off, GPMC_CD_FCLK,
  470. "adv_aad_mux_rd_off");
  471. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 28, 30, 0,
  472. t->adv_aad_mux_wr_off, GPMC_CD_FCLK,
  473. "adv_aad_mux_wr_off");
  474. if (ret)
  475. return -ENXIO;
  476. }
  477. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 0, 3, 0, t->oe_on,
  478. GPMC_CD_FCLK, "oe_on");
  479. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 8, 12, 0, t->oe_off,
  480. GPMC_CD_FCLK, "oe_off");
  481. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  482. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 4, 6, 0,
  483. t->oe_aad_mux_on, GPMC_CD_FCLK,
  484. "oe_aad_mux_on");
  485. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 13, 15, 0,
  486. t->oe_aad_mux_off, GPMC_CD_FCLK,
  487. "oe_aad_mux_off");
  488. }
  489. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 16, 19, 0, t->we_on,
  490. GPMC_CD_FCLK, "we_on");
  491. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 24, 28, 0, t->we_off,
  492. GPMC_CD_FCLK, "we_off");
  493. if (ret)
  494. return -ENXIO;
  495. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 0, 4, 0, t->rd_cycle,
  496. GPMC_CD_FCLK, "rd_cycle");
  497. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 8, 12, 0, t->wr_cycle,
  498. GPMC_CD_FCLK, "wr_cycle");
  499. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 16, 20, 0, t->access,
  500. GPMC_CD_FCLK, "access");
  501. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 24, 27, 0,
  502. t->page_burst_access, GPMC_CD_FCLK,
  503. "page_burst_access");
  504. if (ret)
  505. return -ENXIO;
  506. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 0, 3, 0,
  507. t->bus_turnaround, GPMC_CD_FCLK,
  508. "bus_turnaround");
  509. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 8, 11, 0,
  510. t->cycle2cycle_delay, GPMC_CD_FCLK,
  511. "cycle2cycle_delay");
  512. if (ret)
  513. return -ENXIO;
  514. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) {
  515. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 16, 19, 0,
  516. t->wr_data_mux_bus, GPMC_CD_FCLK,
  517. "wr_data_mux_bus");
  518. if (ret)
  519. return -ENXIO;
  520. }
  521. if (gpmc_capability & GPMC_HAS_WR_ACCESS) {
  522. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 24, 28, 0,
  523. t->wr_access, GPMC_CD_FCLK,
  524. "wr_access");
  525. if (ret)
  526. return -ENXIO;
  527. }
  528. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  529. l &= ~0x03;
  530. l |= (div - 1);
  531. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  532. ret = 0;
  533. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG1, 18, 19,
  534. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  535. t->wait_monitoring, GPMC_CD_CLK,
  536. "wait_monitoring");
  537. ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG1, 25, 26,
  538. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  539. t->clk_activation, GPMC_CD_FCLK,
  540. "clk_activation");
  541. if (ret)
  542. return -ENXIO;
  543. if (IS_ENABLED(CONFIG_TI_GPMC_DEBUG)) {
  544. pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
  545. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  546. }
  547. gpmc_cs_bool_timings(cs, &t->bool_timings);
  548. gpmc_cs_show_timings(cs, "after gpmc_set_timings");
  549. return 0;
  550. }
  551. static int gpmc_cs_set_memconf(int cs, resource_size_t base, u32 size)
  552. {
  553. u32 l;
  554. u32 mask;
  555. /*
  556. * Ensure that base address is aligned on a
  557. * boundary equal to or greater than size.
  558. */
  559. if (base & (size - 1))
  560. return -EINVAL;
  561. base >>= GPMC_CHUNK_SHIFT;
  562. mask = (1 << GPMC_SECTION_SHIFT) - size;
  563. mask >>= GPMC_CHUNK_SHIFT;
  564. mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
  565. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  566. l &= ~GPMC_CONFIG7_MASK;
  567. l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
  568. l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
  569. l |= GPMC_CONFIG7_CSVALID;
  570. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  571. return 0;
  572. }
  573. static void gpmc_cs_enable_mem(int cs)
  574. {
  575. u32 l;
  576. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  577. l |= GPMC_CONFIG7_CSVALID;
  578. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  579. }
  580. static void gpmc_cs_disable_mem(int cs)
  581. {
  582. u32 l;
  583. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  584. l &= ~GPMC_CONFIG7_CSVALID;
  585. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  586. }
  587. static void gpmc_cs_set_reserved(int cs, int reserved)
  588. {
  589. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  590. gpmc->flags |= GPMC_CS_RESERVED;
  591. }
  592. static bool gpmc_cs_reserved(int cs)
  593. {
  594. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  595. return gpmc->flags & GPMC_CS_RESERVED;
  596. }
  597. static unsigned long gpmc_mem_align(unsigned long size)
  598. {
  599. int order;
  600. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  601. order = GPMC_CHUNK_SHIFT - 1;
  602. do {
  603. size >>= 1;
  604. order++;
  605. } while (size);
  606. size = 1 << order;
  607. return size;
  608. }
  609. static int gpmc_cs_request(ofnode node, int cs, struct resource *res)
  610. {
  611. int r = -1;
  612. u32 size;
  613. resource_size_t addr_base = res->start;
  614. if (cs >= gpmc_cs_num) {
  615. pr_err("%s: requested chip-select is disabled\n", __func__);
  616. return -ENODEV;
  617. }
  618. size = gpmc_mem_align(resource_size(res));
  619. if (size > (1 << GPMC_SECTION_SHIFT))
  620. return -ENOMEM;
  621. if (gpmc_cs_reserved(cs)) {
  622. r = -EBUSY;
  623. goto out;
  624. }
  625. if (addr_base & (SZ_16M - 1)) {
  626. pr_err("CS region should be aligned to 16M boundary\n");
  627. goto out;
  628. }
  629. /* Disable CS while changing base address and size mask */
  630. gpmc_cs_disable_mem(cs);
  631. r = gpmc_cs_set_memconf(cs, addr_base, size);
  632. if (r < 0)
  633. goto out;
  634. /* Enable CS */
  635. gpmc_cs_enable_mem(cs);
  636. gpmc_cs_set_reserved(cs, 1);
  637. out:
  638. return r;
  639. }
  640. static void gpmc_cs_free(int cs)
  641. {
  642. if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
  643. pr_warn("Trying to free non-reserved GPMC CS%d\n", cs);
  644. return;
  645. }
  646. gpmc_cs_disable_mem(cs);
  647. gpmc_cs_set_reserved(cs, 0);
  648. }
  649. /**
  650. * gpmc_configure - write request to configure gpmc
  651. * @cmd: command type
  652. * @wval: value to write
  653. * @return status of the operation
  654. */
  655. static int gpmc_configure(int cmd, int wval)
  656. {
  657. u32 regval;
  658. switch (cmd) {
  659. case GPMC_CONFIG_WP:
  660. regval = gpmc_read_reg(GPMC_CONFIG);
  661. if (wval)
  662. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  663. else
  664. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  665. gpmc_write_reg(GPMC_CONFIG, regval);
  666. break;
  667. default:
  668. pr_err("%s: command not supported\n", __func__);
  669. return -EINVAL;
  670. }
  671. return 0;
  672. }
  673. /**
  674. * gpmc_cs_program_settings - programs non-timing related settings
  675. * @cs: GPMC chip-select to program
  676. * @p: pointer to GPMC settings structure
  677. *
  678. * Programs non-timing related settings for a GPMC chip-select, such as
  679. * bus-width, burst configuration, etc. Function should be called once
  680. * for each chip-select that is being used and must be called before
  681. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  682. * register will be initialised to zero by this function. Returns 0 on
  683. * success and appropriate negative error code on failure.
  684. */
  685. static int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  686. {
  687. u32 config1;
  688. if (!p->device_width || p->device_width > GPMC_DEVWIDTH_16BIT) {
  689. pr_err("%s: invalid width %d!", __func__, p->device_width);
  690. return -EINVAL;
  691. }
  692. /* Address-data multiplexing not supported for NAND devices */
  693. if (p->device_nand && p->mux_add_data) {
  694. pr_err("%s: invalid configuration!\n", __func__);
  695. return -EINVAL;
  696. }
  697. if (p->mux_add_data > GPMC_MUX_AD ||
  698. (p->mux_add_data == GPMC_MUX_AAD &&
  699. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  700. pr_err("%s: invalid multiplex configuration!\n", __func__);
  701. return -EINVAL;
  702. }
  703. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  704. if (p->burst_read || p->burst_write) {
  705. switch (p->burst_len) {
  706. case GPMC_BURST_4:
  707. case GPMC_BURST_8:
  708. case GPMC_BURST_16:
  709. break;
  710. default:
  711. pr_err("%s: invalid page/burst-length (%d)\n",
  712. __func__, p->burst_len);
  713. return -EINVAL;
  714. }
  715. }
  716. if (p->wait_pin > gpmc_nr_waitpins) {
  717. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  718. return -EINVAL;
  719. }
  720. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  721. if (p->sync_read)
  722. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  723. if (p->sync_write)
  724. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  725. if (p->wait_on_read)
  726. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  727. if (p->wait_on_write)
  728. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  729. if (p->wait_on_read || p->wait_on_write)
  730. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  731. if (p->device_nand)
  732. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  733. if (p->mux_add_data)
  734. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  735. if (p->burst_read)
  736. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  737. if (p->burst_write)
  738. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  739. if (p->burst_read || p->burst_write) {
  740. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  741. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  742. }
  743. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  744. return 0;
  745. }
  746. static void gpmc_cs_set_name(int cs, const char *name)
  747. {
  748. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  749. gpmc->name = name;
  750. }
  751. static const char *gpmc_cs_get_name(int cs)
  752. {
  753. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  754. return gpmc->name;
  755. }
  756. /**
  757. * gpmc_read_settings_dt - read gpmc settings from device-tree
  758. * @np: pointer to device-tree node for a gpmc child device
  759. * @p: pointer to gpmc settings structure
  760. *
  761. * Reads the GPMC settings for a GPMC child device from device-tree and
  762. * stores them in the GPMC settings structure passed. The GPMC settings
  763. * structure is initialised to zero by this function and so any
  764. * previously stored settings will be cleared.
  765. */
  766. static void gpmc_read_settings_dt(ofnode np, struct gpmc_settings *p)
  767. {
  768. memset(p, 0, sizeof(struct gpmc_settings));
  769. p->sync_read = ofnode_read_bool(np, "gpmc,sync-read");
  770. p->sync_write = ofnode_read_bool(np, "gpmc,sync-write");
  771. ofnode_read_u32(np, "gpmc,device-width", &p->device_width);
  772. ofnode_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
  773. if (!ofnode_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
  774. p->burst_wrap = ofnode_read_bool(np, "gpmc,burst-wrap");
  775. p->burst_read = ofnode_read_bool(np, "gpmc,burst-read");
  776. p->burst_write = ofnode_read_bool(np, "gpmc,burst-write");
  777. if (!p->burst_read && !p->burst_write)
  778. pr_warn("%s: page/burst-length set but not used!\n",
  779. __func__);
  780. }
  781. if (!ofnode_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
  782. p->wait_on_read = ofnode_read_bool(np,
  783. "gpmc,wait-on-read");
  784. p->wait_on_write = ofnode_read_bool(np,
  785. "gpmc,wait-on-write");
  786. if (!p->wait_on_read && !p->wait_on_write)
  787. pr_debug("%s: rd/wr wait monitoring not enabled!\n",
  788. __func__);
  789. }
  790. }
  791. static void gpmc_read_timings_dt(ofnode np,
  792. struct gpmc_timings *gpmc_t)
  793. {
  794. struct gpmc_bool_timings *p;
  795. if (!gpmc_t)
  796. return;
  797. memset(gpmc_t, 0, sizeof(*gpmc_t));
  798. /* minimum clock period for syncronous mode */
  799. ofnode_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
  800. /* chip select timtings */
  801. ofnode_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
  802. ofnode_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
  803. ofnode_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
  804. /* ADV signal timings */
  805. ofnode_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
  806. ofnode_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
  807. ofnode_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
  808. ofnode_read_u32(np, "gpmc,adv-aad-mux-on-ns",
  809. &gpmc_t->adv_aad_mux_on);
  810. ofnode_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
  811. &gpmc_t->adv_aad_mux_rd_off);
  812. ofnode_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
  813. &gpmc_t->adv_aad_mux_wr_off);
  814. /* WE signal timings */
  815. ofnode_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
  816. ofnode_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
  817. /* OE signal timings */
  818. ofnode_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
  819. ofnode_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
  820. ofnode_read_u32(np, "gpmc,oe-aad-mux-on-ns",
  821. &gpmc_t->oe_aad_mux_on);
  822. ofnode_read_u32(np, "gpmc,oe-aad-mux-off-ns",
  823. &gpmc_t->oe_aad_mux_off);
  824. /* access and cycle timings */
  825. ofnode_read_u32(np, "gpmc,page-burst-access-ns",
  826. &gpmc_t->page_burst_access);
  827. ofnode_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
  828. ofnode_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
  829. ofnode_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
  830. ofnode_read_u32(np, "gpmc,bus-turnaround-ns",
  831. &gpmc_t->bus_turnaround);
  832. ofnode_read_u32(np, "gpmc,cycle2cycle-delay-ns",
  833. &gpmc_t->cycle2cycle_delay);
  834. ofnode_read_u32(np, "gpmc,wait-monitoring-ns",
  835. &gpmc_t->wait_monitoring);
  836. ofnode_read_u32(np, "gpmc,clk-activation-ns",
  837. &gpmc_t->clk_activation);
  838. /* only applicable to OMAP3+ */
  839. ofnode_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
  840. ofnode_read_u32(np, "gpmc,wr-data-mux-bus-ns",
  841. &gpmc_t->wr_data_mux_bus);
  842. /* bool timing parameters */
  843. p = &gpmc_t->bool_timings;
  844. p->cycle2cyclediffcsen =
  845. ofnode_read_bool(np, "gpmc,cycle2cycle-diffcsen");
  846. p->cycle2cyclesamecsen =
  847. ofnode_read_bool(np, "gpmc,cycle2cycle-samecsen");
  848. p->we_extra_delay = ofnode_read_bool(np, "gpmc,we-extra-delay");
  849. p->oe_extra_delay = ofnode_read_bool(np, "gpmc,oe-extra-delay");
  850. p->adv_extra_delay = ofnode_read_bool(np, "gpmc,adv-extra-delay");
  851. p->cs_extra_delay = ofnode_read_bool(np, "gpmc,cs-extra-delay");
  852. p->time_para_granularity =
  853. ofnode_read_bool(np, "gpmc,time-para-granularity");
  854. }
  855. /**
  856. * gpmc_probe_generic_child - configures the gpmc for a child device
  857. * @dev: pointer to gpmc platform device
  858. * @child: pointer to device-tree node for child device
  859. *
  860. * Allocates and configures a GPMC chip-select for a child device.
  861. * Returns 0 on success and appropriate negative error code on failure.
  862. */
  863. static int gpmc_probe_generic_child(struct udevice *dev,
  864. ofnode child)
  865. {
  866. struct gpmc_settings gpmc_s;
  867. struct gpmc_timings gpmc_t;
  868. struct resource res;
  869. const char *name;
  870. int ret;
  871. u32 val, cs;
  872. if (ofnode_read_u32(child, "reg", &cs) < 0) {
  873. dev_err(dev, "can't get reg property of child %s\n",
  874. ofnode_get_name(child));
  875. return -ENODEV;
  876. }
  877. if (ofnode_read_resource(child, 0, &res) < 0) {
  878. dev_err(dev, "%s has malformed 'reg' property\n",
  879. ofnode_get_name(child));
  880. return -ENODEV;
  881. }
  882. /*
  883. * Check if we have multiple instances of the same device
  884. * on a single chip select. If so, use the already initialized
  885. * timings.
  886. */
  887. name = gpmc_cs_get_name(cs);
  888. if (name && !strcmp(name, ofnode_get_name(child)))
  889. goto no_timings;
  890. ret = gpmc_cs_request(child, cs, &res);
  891. if (ret < 0) {
  892. dev_err(dev, "cannot request GPMC CS %d\n", cs);
  893. return ret;
  894. }
  895. gpmc_cs_set_name(cs, ofnode_get_name(child));
  896. gpmc_read_settings_dt(child, &gpmc_s);
  897. gpmc_read_timings_dt(child, &gpmc_t);
  898. /*
  899. * For some GPMC devices we still need to rely on the bootloader
  900. * timings because the devices can be connected via FPGA.
  901. * REVISIT: Add timing support from slls644g.pdf.
  902. */
  903. if (!gpmc_t.cs_rd_off) {
  904. pr_warn("enable GPMC debug to configure .dts timings for CS%i\n",
  905. cs);
  906. gpmc_cs_show_timings(cs,
  907. "please add GPMC bootloader timings to .dts");
  908. goto no_timings;
  909. }
  910. /* CS must be disabled while making changes to gpmc configuration */
  911. gpmc_cs_disable_mem(cs);
  912. if (!ofnode_read_u32(child, "nand-bus-width", &val)) {
  913. /* NAND specific setup */
  914. ofnode_read_u32(child, "nand-bus-width", &val);
  915. switch (val) {
  916. case 8:
  917. gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
  918. break;
  919. case 16:
  920. gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
  921. break;
  922. default:
  923. dev_err(dev, "%s: invalid 'nand-bus-width'\n",
  924. ofnode_get_name(child));
  925. ret = -EINVAL;
  926. goto err;
  927. }
  928. /* disable write protect */
  929. gpmc_configure(GPMC_CONFIG_WP, 0);
  930. gpmc_s.device_nand = true;
  931. } else {
  932. ret = ofnode_read_u32(child, "bank-width",
  933. &gpmc_s.device_width);
  934. if (ret < 0 && !gpmc_s.device_width) {
  935. dev_err(dev,
  936. "%s has no 'gpmc,device-width' property\n",
  937. ofnode_get_name(child));
  938. goto err;
  939. }
  940. }
  941. gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
  942. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  943. if (ret < 0)
  944. goto err;
  945. ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
  946. if (ret) {
  947. dev_err(dev, "failed to set gpmc timings for: %s\n",
  948. ofnode_get_name(child));
  949. goto err;
  950. }
  951. /* Clear limited address i.e. enable A26-A11 */
  952. val = gpmc_read_reg(GPMC_CONFIG);
  953. val &= ~GPMC_CONFIG_LIMITEDADDRESS;
  954. gpmc_write_reg(GPMC_CONFIG, val);
  955. /* Enable CS region */
  956. gpmc_cs_enable_mem(cs);
  957. no_timings:
  958. return 0;
  959. err:
  960. gpmc_cs_free(cs);
  961. return ret;
  962. }
  963. static void gpmc_probe_dt_children(struct udevice *dev)
  964. {
  965. int ret;
  966. ofnode child;
  967. ofnode_for_each_subnode(child, dev_ofnode(dev)) {
  968. ret = gpmc_probe_generic_child(dev, child);
  969. if (ret) {
  970. dev_err(dev, "Cannot parse child %s:%d",
  971. ofnode_get_name(child), ret);
  972. }
  973. }
  974. }
  975. static int gpmc_parse_dt(struct udevice *dev, struct ti_gpmc *gpmc)
  976. {
  977. int ret;
  978. u32 val;
  979. ret = ofnode_read_u32(dev_ofnode(dev), "gpmc,num-cs",
  980. &val);
  981. if (ret < 0) {
  982. pr_err("%s: number of chip-selects not defined\n", __func__);
  983. return ret;
  984. } else if (val < 1) {
  985. pr_err("%s: all chip-selects are disabled\n", __func__);
  986. return -EINVAL;
  987. } else if (val > GPMC_CS_NUM) {
  988. pr_err("%s: number of supported chip-selects cannot be > %d\n",
  989. __func__, GPMC_CS_NUM);
  990. return -EINVAL;
  991. }
  992. gpmc->cs_num = val;
  993. gpmc_cs_num = val;
  994. ret = ofnode_read_u32(dev_ofnode(dev), "gpmc,num-waitpins",
  995. &gpmc->nr_waitpins);
  996. if (ret < 0) {
  997. pr_err("%s: number of wait pins not found!\n", __func__);
  998. return ret;
  999. }
  1000. gpmc_nr_waitpins = gpmc->nr_waitpins;
  1001. return 0;
  1002. }
  1003. static int gpmc_probe(struct udevice *dev)
  1004. {
  1005. struct ti_gpmc *priv = dev_get_priv(dev);
  1006. int ret;
  1007. struct resource res;
  1008. ret = dev_read_resource_byname(dev, "cfg", &res);
  1009. if (ret) {
  1010. /* Legacy DT */
  1011. dev_read_resource(dev, 0, &res);
  1012. priv->base = devm_ioremap(dev, res.start, resource_size(&res));
  1013. priv->data.start = GPMC_MEM_START;
  1014. priv->data.end = GPMC_MEM_END;
  1015. } else {
  1016. priv->base = devm_ioremap(dev, res.start, resource_size(&res));
  1017. ret = dev_read_resource_byname(dev, "data", &res);
  1018. if (ret)
  1019. return -ENOENT;
  1020. priv->data = res;
  1021. }
  1022. if (!priv->base)
  1023. return -ENOMEM;
  1024. gpmc_cfg = (struct gpmc *)priv->base;
  1025. gpmc_base = priv->base;
  1026. priv->l3_clk = devm_clk_get(dev, "fck");
  1027. if (IS_ERR(priv->l3_clk))
  1028. return PTR_ERR(priv->l3_clk);
  1029. if (!clk_get_rate(priv->l3_clk))
  1030. return -EINVAL;
  1031. gpmc_l3_clk = priv->l3_clk;
  1032. ret = gpmc_parse_dt(dev, priv);
  1033. if (ret)
  1034. return ret;
  1035. priv->capability_flags = dev->driver->of_match->data;
  1036. gpmc_capability = priv->capability_flags;
  1037. gpmc_probe_dt_children(dev);
  1038. return 0;
  1039. }
  1040. #define GPMC_DATA_REV2_4 0
  1041. #define GPMC_DATA_REV5 (GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS)
  1042. #define GPMC_DATA_REV6 (GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS | GPMC_HAS_MUX_AAD)
  1043. static const struct udevice_id gpmc_dt_ids[] = {
  1044. { .compatible = "ti,am64-gpmc", .data = GPMC_DATA_REV6, },
  1045. { .compatible = "ti,am3352-gpmc", .data = GPMC_DATA_REV5, },
  1046. { .compatible = "ti,omap2420-gpmc", .data = GPMC_DATA_REV2_4, },
  1047. { .compatible = "ti,omap2430-gpmc", .data = GPMC_DATA_REV2_4, },
  1048. { .compatible = "ti,omap3430-gpmc", .data = GPMC_DATA_REV5, },
  1049. { .compatible = "ti,omap4430-gpmc", .data = GPMC_DATA_REV6, },
  1050. { } /* sentinel */
  1051. };
  1052. U_BOOT_DRIVER(ti_gpmc) = {
  1053. .name = "ti-gpmc",
  1054. .id = UCLASS_MEMORY,
  1055. .of_match = gpmc_dt_ids,
  1056. .probe = gpmc_probe,
  1057. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  1058. };